Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jat9292 opened this issue Sep 8, 2023 · 3 comments
Open

Comments

@jat9292
Copy link

jat9292 commented Sep 8, 2023

🐛 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
Copy link

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
Copy link

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
Copy link

llecoq commented Feb 7, 2024

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants