wasm-bindgen: Panic when used on .wasm built with -C link-arg=--shared

When trying to use wasm-bindgen on a .wasm file built with -C link-arg=--shared, it panics when using closures.

Steps to Reproduce

$ rustup default beta (or nightly)
$ cargo init --lib
$ cat src/lib.rs
use wasm_bindgen::prelude::*;

#[no_mangle]
pub extern "C" fn foo() {
    Closure::wrap(Box::new(move || { }) as Box<dyn FnMut()>);
}
$ cat .cargo/config
[target.wasm32-unknown-unknown]
rustflags = [
    "-C", "link-arg=--shared",
]
$ cargo build --target=wasm32-unknown-unknown
$ wasm-bindgen target/wasm32-unknown-unknown/debug/file.wasm --out-dir out
thread 'main' panicked at 'out of bounds read of function table', src/libcore/option.rs:1038:5

Additional Context

Using the latest wasm-bindgen from github also panics, but at a different location.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Hi there, would it be possible to revisit this?

I’m deep in the weeds of using the dynamic link system of wasm-ld and it seems like it’s wasm-bindgen that’s falling over on me.

For Dioxus 0.7 I’ve implemented

  • wasm lazy loading
  • wasm module splitting
  • wasm hot patching

I got hot-patching working in two flavors

  • setting table-base and global-base manually
  • using the --pie and --experimental-pic flags in conjuction with -Ctarget-cpu=mvp and -Crelocation-model=pic

The table-base situation seemed to work fine but the dynamic approach has caused me all sorts of headaches. The current problem I’m facing is that the JS bindings seem to just stop working properly when loading the module, in very weird ways.

What part of wasm-bindgen would need to be overhauled to get dynamic linking and loading working in wasm?