Skip to content

Failed to compile code targetting wasm32-wasip2 with +atomics,+bulk-memory,+mutable-globals enabled #140971

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

Open
aryan9600 opened this issue May 13, 2025 · 4 comments
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.

Comments

@aryan9600
Copy link

I created a new minimal rust library with crate-type set to cdylib.

pub extern "C" fn add(left: u64, right: u64) -> u64 {
    left + right
}

Compiling this against the wasm32-unknown-unknown target works okay:

❯ RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals' cargo +nightly build --target wasm32-unknown-unknown -Z build-std=std,panic_abort
   Compiling compiler_builtins v0.1.158
   Compiling core v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core)
   Compiling libc v0.2.172
   Compiling object v0.36.7
   Compiling std v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/std)
   Compiling rustc-std-workspace-core v1.99.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/rustc-s
td-workspace-core)
   Compiling alloc v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc)
   Compiling cfg-if v1.0.0
   Compiling adler2 v2.0.0
   Compiling memchr v2.7.4
   Compiling rustc-demangle v0.1.24
   Compiling unwind v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/unwind)
   Compiling dlmalloc v0.2.8
   Compiling rustc-std-workspace-alloc v1.99.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/rustc-
std-workspace-alloc)
   Compiling panic_abort v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/panic_abort)
   Compiling panic_unwind v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/panic_unwind)
   Compiling gimli v0.31.1
   Compiling hashbrown v0.15.3
   Compiling miniz_oxide v0.8.8
   Compiling std_detect v0.1.5 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/stdarch/crates/std_det
ect)
   Compiling addr2line v0.24.2
   Compiling rustc-std-workspace-std v1.99.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/rustc-st
d-workspace-std)
   Compiling rustc-literal-escaper v0.0.2
   Compiling proc_macro v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/proc_macro)
   Compiling wasm-ex v0.1.0 (/Users/sanskarjaiswal/Development/wasm-ex)
warning: unstable feature specified for `-Ctarget-feature`: `atomics`
  |
  = note: this feature is not stably supported; its behavior can change in the future

warning: `wasm-ex` (lib) generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 7.05s

Compiling this against the wasm32-wasip2 target fails:

❯ RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals' cargo +nightly build --target wasm32-wasip2 -Z build-std=std,panic_abort
   Compiling compiler_builtins v0.1.158
   Compiling core v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core)
   Compiling libc v0.2.172
   Compiling object v0.36.7
   Compiling std v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/std)
   Compiling rustc-std-workspace-core v1.99.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/rustc-s
td-workspace-core)
   Compiling alloc v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc)
   Compiling cfg-if v1.0.0
   Compiling memchr v2.7.4
   Compiling adler2 v2.0.0
   Compiling rustc-demangle v0.1.24
   Compiling unwind v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/unwind)
   Compiling rustc-std-workspace-alloc v1.99.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/rustc-
std-workspace-alloc)
   Compiling panic_unwind v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/panic_unwind)
   Compiling panic_abort v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/panic_abort)
   Compiling gimli v0.31.1
   Compiling std_detect v0.1.5 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/stdarch/crates/std_det
ect)
   Compiling miniz_oxide v0.8.8
   Compiling wasi v0.11.0+wasi-snapshot-preview1
   Compiling hashbrown v0.15.3
   Compiling addr2line v0.24.2
   Compiling rustc-std-workspace-std v1.99.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/rustc-st
d-workspace-std)
   Compiling rustc-literal-escaper v0.0.2
   Compiling proc_macro v0.0.0 (/Users/sanskarjaiswal/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/proc_macro)
   Compiling wasm-ex v0.1.0 (/Users/sanskarjaiswal/Development/wasm-ex)
warning: unstable feature specified for `-Ctarget-feature`: `atomics`
  |
  = note: this feature is not stably supported; its behavior can change in the future

error: linking with `wasm-component-ld` failed: exit status: 1
  |
  = note:  "wasm-component-ld" "-flavor" "wasm" "--shared-memory" "--max-memory=1073741824" "--import-memory" "--export" "cabi_realloc" "-z" "stack-size=
1048576" "--stack-first" "--allow-undefined" "--no-demangle" "<2 object files omitted>" "/Users/sanskarjaiswal/Development/wasm-ex/target/wasm32-wasip2/d
ebug/deps/{libstd-e6974f8339de980a.rlib,libpanic_abort-ef7c973c0f49a7b3.rlib,libwasi-d76f2b70906af1dc.rlib,librustc_demangle-a2b444bfd261b1c6.rlib,libstd
_detect-24d3b7f99da05399.rlib,libhashbrown-555fa83f2da96374.rlib,librustc_std_workspace_alloc-418c90a8abd2c0b2.rlib,libminiz_oxide-f1056162f1d389fb.rlib,
libadler2-bfe5e6d99ec23333.rlib,libunwind-4e1cf59691c85b76.rlib,libcfg_if-6422f5890fdf1c76.rlib,liblibc-460067f0d0fac4f8.rlib}.rlib" "-l" "c" "/Users/san
skarjaiswal/Development/wasm-ex/target/wasm32-wasip2/debug/deps/{liballoc-7516e5ef0d2d3fa4.rlib,librustc_std_workspace_core-b2e3c419616fe115.rlib,libcore
-c67d6fd40cbf0c22.rlib,libcompiler_builtins-c030ab949e2d2444.rlib}.rlib" "-L" "<sysroot>/lib/rustlib/wasm32-wasip2/lib/self-contained" "-o" "/Users/sansk
arjaiswal/Development/wasm-ex/target/wasm32-wasip2/debug/deps/wasm_ex.wasm" "--gc-sections" "--no-entry" "-O0"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: rust-lld: error: --shared-memory is disallowed by dlmalloc.o because it was not compiled with 'atomics' or 'bulk-memory' features.
          rust-lld: error: /Users/sanskarjaiswal/Development/wasm-ex/target/wasm32-wasip2/debug/deps/libstd-e6974f8339de980a.rlib(std-e6974f8339de980a.st
d.4ded6a7c9b80894f-cgu.09.rcgu.o): relocation R_WASM_MEMORY_ADDR_TLS_SLEB cannot be used against non-TLS symbol `errno`
          rust-lld: error: /Users/sanskarjaiswal/Development/wasm-ex/target/wasm32-wasip2/debug/deps/libstd-e6974f8339de980a.rlib(std-e6974f8339de980a.st
d.4ded6a7c9b80894f-cgu.09.rcgu.o): relocation R_WASM_MEMORY_ADDR_TLS_SLEB cannot be used against `errno` in non-TLS section: .bss
          rust-lld: error: /Users/sanskarjaiswal/Development/wasm-ex/target/wasm32-wasip2/debug/deps/libstd-e6974f8339de980a.rlib(std-e6974f8339de980a.st
d.4ded6a7c9b80894f-cgu.09.rcgu.o): relocation R_WASM_MEMORY_ADDR_TLS_SLEB cannot be used against non-TLS symbol `errno`
          rust-lld: error: /Users/sanskarjaiswal/Development/wasm-ex/target/wasm32-wasip2/debug/deps/libstd-e6974f8339de980a.rlib(std-e6974f8339de980a.st
d.4ded6a7c9b80894f-cgu.09.rcgu.o): relocation R_WASM_MEMORY_ADDR_TLS_SLEB cannot be used against `errno` in non-TLS section: .bss
          error: failed to invoke LLD: exit status: 1

the error message is complaining about dlmalloc.o not being compiled with the atomics and bulk-memory features for some reason.
interestingly, we see that for the wasm32-unknown-unknown target, the compiler pulls dlmalloc. but for the wasm32-wasip2 target, dlmalloc is not being pulled and yet the compiler complains about dlmalloc.

Meta

rustc --version --verbose:

❯ rustc --version --verbose
rustc 1.89.0-nightly (ce7e97f73 2025-05-11)
binary: rustc
commit-hash: ce7e97f7371af47e0786f74aa169f6ac9473ff4e
commit-date: 2025-05-11
host: aarch64-apple-darwin
release: 1.89.0-nightly
LLVM version: 20.1.4
@aryan9600 aryan9600 added the C-bug Category: This is a bug. label May 13, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 13, 2025
@hanna-kruppe
Copy link
Contributor

The wasi targets link a precompiled copy of wasi-libc (written in C, not recompiled by -Zbuild-std) while the unknown-unknown is pure Rust. The two dlmallocs you noticed are different, one is a Rust crate while the other is written in C and part of wasi-libc.

@aryan9600
Copy link
Author

so if i understand correctly, the only workaround would be to build wasi-libc from scratch with atomics and bulk-memory enabled and use that to build the rust compiler as explained here. this compiler would then use a supported version of dlmalloc and be able to compile the code with atomics enabled. am i on the right track?

@hanna-kruppe
Copy link
Contributor

I don’t know for sure what the extra complications/requirements are beyond enabling Wasm feature, but I suspect there’s a couple because at least for wasip1 both Rust and C have a separate *-threads targets. What are you trying to achieve?

@aryan9600
Copy link
Author

aryan9600 commented May 14, 2025

i'm trying to compile against the wasm32-wasip2 target with the +atomics,+bulk-memory,+mutable-globals features enabled. i'm pulling in a few deps and using tokio to run some futures. some of the deps or tokio are using a condvar wait somewhere. withtout the above futures, the code panics (rightly so). i'm trying to enable these features so that the condvar wait doesn't panic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.
Projects
None yet
Development

No branches or pull requests

3 participants