rustwasm / wasm-bindgen

Facilitating high-level interactions between Wasm modules and JavaScript

Home Page:https://rustwasm.github.io/docs/wasm-bindgen/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generated binary crashes with "RuntimeError: memory access out of bounds"

womeier opened this issue · comments

Description

The wasm-bindgen build command succeeds, but the generated binary crashes with RuntimeError: memory access out of bounds.
main.rs is generated by the https://github.com/AU-COBRA/coq-rust-extraction plugin, it could be a bug there as well. I suspect it's in wasm-bindgen, because the setup works with a standard rust setup.

Working setup without wasm-bindgen

sm_gauss_nat-working.zip

cargo run in the attached project works, it prints the expected result Some(O(PhantomData<&()>)).
(The source program takes an arithmetic expression representing the sum of 1 to n, compiles it to a toy stack-machine
and executes it, then subtracts n*(n+1)/2 from the result, so the expected value is 0.)

Steps to Reproduce

sm_gauss_nat-crashing.zip

In the attached project, the main file is exactly the same as in the working one above,
except for the lines at the bottom related to wasm-bindgen.

  1. wasm-pack build --target nodejs
  2. in pkg/sm_gauss_nat.js: replace write_string with console.log and add wasm.main() at the bottom
  3. then run with nodejs: node --stack-size=1000000 sm_gauss_nat.js
  4. Fails with the following error

Error

wolfgang@laptop:~/sm_gauss_nat-extracted/pkg$ node --stack-size=1000000 sm_gauss_nat.js 
wasm://wasm/0005b416:1

RuntimeError: memory access out of bounds
    at wasm://wasm/0005b416:wasm-function[22]:0x3b74
    at wasm://wasm/0005b416:wasm-function[22]:0x3b3d
    at wasm://wasm/0005b416:wasm-function[22]:0x3b5f
    at wasm://wasm/0005b416:wasm-function[22]:0x3b3d
    at wasm://wasm/0005b416:wasm-function[22]:0x3b5f
    at wasm://wasm/0005b416:wasm-function[22]:0x3b3d
    at wasm://wasm/0005b416:wasm-function[22]:0x3b5f
    at wasm://wasm/0005b416:wasm-function[22]:0x3b3d
    at wasm://wasm/0005b416:wasm-function[22]:0x3b5f
    at wasm://wasm/0005b416:wasm-function[22]:0x3b3d

Node.js v20.11.1

Possibly relevant

The main.rs is generated by a compiler, and the setup with the wasm-bindgen macros works as expected on a few other programs. We have some other Wasm binaries here, which one can run with included Node.js based run script,
I can provide more source code for the working examples in case it helps.

Thanks!