Skip to content

Regression on wasm32-wasip1 with +atomics target feature when accessing TLS #166314

Closed as not planned

Description

@ngg

Code

I tried this code:

/// hashset.rs
use std::collections::HashSet;

fn main() {
    let mut seen = HashSet::new();
    assert!(seen.insert(1_u32));
    println!("ok");
}
rustc --target wasm32-wasip1 -C target-feature=+atomics -C debuginfo=1 -o hashset.wasm hashset.rs && wasmtime hashset.wasm

I expected to see this happen: It should compile and run fine, printing ok.

Instead, this happened: It compiled, but wasmtime failed with the following error:

Error: failed to run main module `hashset.wasm`

Caused by:
    0: failed to invoke command default
    1: error while executing at wasm backtrace:
    0:   0x2925 - hashset.wasm!<std[7c0a0f0b853524cc]::hash::random::RandomState>::new::KEYS::{K#0}::{closure#1}
    1:   0x48c1 - hashset.wasm!<<std[7c0a0f0b853524cc]::hash::random::RandomState>::new::KEYS::{K#0}::{closure#1} as core[f19747029855e5e6]::ops::function::FnOnce<(core[f19747029855e5e6]::option::Option<&mut core[f19747029855e5e6]::option::Option<core[f19747029855e5e6]::cell::Cell<(u64, u64)>>>,)>>::call_once
    2:    0x809 - hashset.wasm!<std[7c0a0f0b853524cc]::thread::local::LocalKey<core[f19747029855e5e6]::cell::Cell<(u64, u64)>>>::try_with::<<std[7c0a0f0b853524cc]::hash::random::RandomState>::new::{closure#0}, std[7c0a0f0b853524cc]::hash::random::RandomState>
    3:    0x799 - hashset.wasm!<std[7c0a0f0b853524cc]::thread::local::LocalKey<core[f19747029855e5e6]::cell::Cell<(u64, u64)>>>::with::<<std[7c0a0f0b853524cc]::hash::random::RandomState>::new::{closure#0}, std[7c0a0f0b853524cc]::hash::random::RandomState>
    4:   0x2bcf - hashset.wasm!<std[7c0a0f0b853524cc]::collections::hash::set::HashSet<u32>>::new
    5:   0x2b2f - hashset.wasm!hashset[ef672965bf494934]::main
    6:   0x239b - hashset.wasm!<fn() as core[f19747029855e5e6]::ops::function::FnOnce<()>>::call_once
    7:   0x236f - hashset.wasm!std[7c0a0f0b853524cc]::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()>
    8:   0x28e1 - hashset.wasm!std[7c0a0f0b853524cc]::rt::lang_start::<()>::{closure#0}
    9:   0x6cfc - hashset.wasm!std[7c0a0f0b853524cc]::rt::lang_start_internal
   10:   0x2135 - hashset.wasm!std[7c0a0f0b853524cc]::rt::lang_start::<()>
   11:   0x48f0 - hashset.wasm!__main_void
   12:    0x42e - hashset.wasm!_start
note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
    2: memory fault at wasm address 0x2048e0 in linear memory of size 0x110000
    3: wasm trap: out of bounds memory access

Version it worked on

It most recently worked on: Rust 1.99.1 + wasmtime-cli 51.0.0

Version with regression

wasmtime-cli 51.0.0 + rustc --version --verbose:

rustc 1.100.0 (0ef1bc386 2026-11-09)
binary: rustc
commit-hash: 0ef1bc386059596dada8fb65b50425aab2ea637a
commit-date: 2026-11-09
host: x86_64-unknown-linux-gnu
release: 1.100.0
LLVM version: 23.1.5

wasmtime-cli 51.0.0 + rustc --version --verbose:

rustc 1.102.0-nightly (6905905bb 2026-11-19)
binary: rustc
commit-hash: 6905905bb7727fd0f9223f5fdc9ad4e62236d5a2
commit-date: 2026-11-19
host: x86_64-unknown-linux-gnu
release: 1.102.0-nightly
LLVM version: 23.1.6

Activity

  1. added
    C-bug
    regression-untriaged
    on Nov 21, 2026
  2. added
    needs-triage
    I-prioritize
    on Nov 21, 2026
  3. ngg commented on Nov 21, 2026

    @ngg
    ContributorAuthor

    The wasm32-wasip1 target switched its TLS implementation in #163809, that caused this regression.
    I think the actual bug is in the LLD linker, I'm opening an issue for them as well, along with a potential fix PR.

  4. added
    T-compiler
    regression-from-stable-to-stable
    T-libs
    O-wasi
    and removed
    regression-untriaged
    needs-triage
    on Nov 21, 2026
  5. maxdexh commented on Nov 21, 2026

    @maxdexh
    Member

    cc @alexcrichton (target maintainer)
    (and cc myself as the author of the regressing PR ^^)

    I'm not sure what is going on here, I cannot reproduce this manually with a thread local outside of HashSet?

  6. added
    A-linkers
    A-lld
    and removed
    A-linkers
    on Nov 21, 2026
  7. 8 remaining items

  8. added
    A-target-feature
    and removed
    A-linkage
    on Nov 21, 2026
  9. maxdexh commented on Nov 21, 2026

    @maxdexh
    Member

    Problem also exists on wasip2, but interestingly, not on wasip3? Did the target feature become a default there or something?

  10. bjorn3 commented on Nov 21, 2026

    @bjorn3
    Member

    To put it another way, we should make atomics a target modifier

    isn't that just wasm-wasip1-threads?

    The reason we allow enabling/disabling the atomics target feature at all is for wasm32-unknown-unknown which doesn't have a corresponding multi-threaded target at all. If you want multi-threaded wasm32-unknown-unknown you have to use RUSTFLAGS="-Ctarget-feature=+atomics" cargo build -Zbuild-std --target wasm32-unknown-unknown.

  11. maxdexh commented on Nov 21, 2026

    @maxdexh
    Member

    but #163809 changed this for some reason.

    FWIW the reason was another regression where thread locals didn't get dropped anymore ^^
    also using that implementation was already the plan from another pr, which got partially reverted for wasi due to a bug in the sdk; that partial revert essentially just got reverted with an sdk bump

  12. bjorn3 commented on Nov 21, 2026

    @bjorn3
    Member

    Problem also exists on wasip2, but interestingly, not on wasip3? Did the target feature become a default there or something?

    Wasip3 uses cooperative multitasking, so it doesn't actually need atomics. A mutex just does a regular check of the lock flag and if it is already locked, it preempts to allow another thread to run. And for tls it doesn't use the regular tool convention for tls accesses, but rather inserts calls to something defined by wasi-libc as part of the cooperative multitasking implementation I believe.

  13. bjorn3 commented on Nov 21, 2026

    @bjorn3
    Member

    FWIW the reason was another regression where thread locals didn't get dropped anymore ^^
    also using that implementation was already the plan from another pr, which got partially reverted for wasi due to a bug in the sdk; that partial revert essentially just got reverted with an sdk bump

    Avoiding the no-thread implementation on wasm32-wasip1-threads makes sense. I just don't understand why it is avoided on all wasi targets now, not just those that support threads.

  14. maxdexh commented on Nov 21, 2026

    @maxdexh
    Member

    I guess that sounds reasonable. We can partial-revert the revert from #163809 to unrevert the partial revert of #159733 from #160868.

    More comprehensibly, bring back wasip1 and wasip2 to the state before #159733, while leaving wasip1-threads and wasip3 as they are?

    @rustbot claim

  15. removed
    I-prioritize
    on Nov 22, 2026
  16. rustbot commented on Nov 22, 2026

    @rustbot
    Collaborator

    Assigning P-medium (discussion on Zulip)

  17. added
    P-medium
    on Nov 22, 2026
  18. removed their assignment
    on Nov 23, 2026
  19. maxdexh commented on Nov 23, 2026

    @maxdexh
    Member

    Per discussion with @alexcrichton, closing this as intended breakage (or rather, as "was never supposed to work").

    We should make target-feature=+atomic a target modifier so that rebuiling std becomes required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkersA-lldA-target-featureC-bugO-wasiP-mediumT-compilerT-libsregression-from-stable-to-stable

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions