rustwasm / wasm-pack

📦✨ your favorite rust -> wasm workflow tool!
https://rustwasm.github.io/wasm-pack/
Apache License 2.0
6.34k stars 411 forks source link

Issue with shared memory and linking with rust-lld in multithreading #1328

Open jat9292 opened 1 year ago

jat9292 commented 1 year ago

🐛 Bug description

Hello, I keep getting an error when trying to compile a multithreaded Rust library to WASM using wasm-pack. I used the following recommended flags for compilation : export RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals' before cargo build --target wasm32-unknown-unknown -Z build-std=panic_abort,std. But then, when trying wasm-pack build --target web (as well as wasm-pack build --target no-modules) I keep getting this error :

error: linking with `rust-lld` failed: exit status: 1

rust-lld: error: --shared-memory is disallowed by std-57093ec1f6212391.std.2002a83916d4b5df-cgu.0.rcgu.o because it was not compiled with 'atomics' or 'bulk-memory' features.

which is related to the flags that I setup (I need them to be compatible with multithreading btw). I also tried to replace std::thread with wasm_thread crate but got exactly the same issue when using the flags for multithreading. Any idea on how to solve my issue, please? Any help would be greatly appreciated :pray_tone2:

Steps to reproduce

You can clone my repo and do previous commands in this directory.

🤔 Expected Behavior

wasm-packshould build my WASM package.

🌍 Your environment

Include the relevant details of your environment. wasm-pack version: 0.12.1 rustc version: rustc 1.74.0-nightly (84a9f4c6e 2023-08-29)

FlippingBinary commented 11 months ago

Are you using a nightly build? According to the folks at GoogleChromeLabs/wasm-bindgen-rayon:

the Rust standard library for the WebAssembly target is built without threads support to ensure maximum portability.

Since we do want standard APIs like Mutex, Arc and so on to work, you'll need to use the nightly compiler toolchain and pass some flags to rebuild the standard library in addition to your own code.

In order to reduce risk of breakages, it's strongly recommended to use a fixed nightly version. For example, the latest stable Rust at the moment of writing is version 1.66, which corresponds to nightly-2022-12-12, which was tested and works with this crate.

Their advice:

Put a string nightly-2022-12-12 in a rust-toolchain file in your project directory. This tells Rustup to use nightly toolchain by default for your project.

Source: https://github.com/GoogleChromeLabs/wasm-bindgen-rayon#using-config-files

ArhanChaudhary commented 10 months ago

Very interestingly, compiling the raytracer example with RUSTFLAGS set produces this error, but creating a .cargo/config.toml with a similar configuration:

[unstable]
build-std = ['std', 'panic_abort']

[build]
target = "wasm32-unknown-unknown"
rustflags = '-Ctarget-feature=+atomics,+bulk-memory,+mutable-globals'

works about fine. Really strange, I may look into this later and open up issue.

In the case that this still doesn't work for whatever reason, you can manually specify the cargo run flags:

$ wasm-pack build . --target no-modules -- -Z build-std=panic_abort,std

llecoq commented 9 months ago

Very interestingly, compiling the raytracer example with RUSTFLAGS set produces this error, but creating a .cargo/config.toml with a similar configuration:

[unstable]
build-std = ['std', 'panic_abort']

[build]
target = "wasm32-unknown-unknown"
rustflags = '-Ctarget-feature=+atomics,+bulk-memory,+mutable-globals'

works about fine. Really strange, I may look into this later and open up issue.

In the case that this still doesn't work for whatever reason, you can manually specify the cargo run flags:

$ wasm-pack build . --target no-modules -- -Z build-std=panic_abort,std

This made the trick for me! Thanks :)