Skip to content

Work around V8 Liftoff atomic.wait32 bug only when targeting Chrome < 115 or Node.js < 21 - #28344

Closed
juj wants to merge 3 commits into
emscripten-core:mainfrom
juj:futex_wait_liftoff_workaround_min_chrome
Closed

juj wants to merge 3 commits into
emscripten-core:mainfrom
juj:futex_wait_liftoff_workaround_min_chrome

Conversation

@juj

@juj juj commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Before Chrome 115, V8's Liftoff compiler can clobber the register-cached locals of the function that executes memory.atomic.wait32 (https://crbug.com/1432453). In _do_futex_wait() this overwrote the cached thread pointer, which made the subsequent self->wait_addr = 0 trap. See #28313 for the analysis, where this was hit in Chrome 85.

#28313 works around this by doing the wait in a separate function that is called through a volatile function pointer, which costs an indirect call per futex wait and 29 bytes in test_codesize_minimal_pthreads for every multithreaded build.

Apply the same workaround, but only to the builds that target a version of Chrome or Node.js that has the bug, i.e. when MIN_CHROME_VERSION < 115 or MIN_NODE_VERSION < 210000. The V8 fix first shipped in V8 11.5, and every release of Node.js 20 has V8 11.3, so the first Node.js version with the fix is 21.0.0. emscripten_futex_wait.c is built a second time with the workaround enabled into a new system library libfutex_wait_liftoff_workaround, which is linked in ahead of libc in that case, the same way that libprintf_long_double and libc_optz override parts of libc.

The version of emscripten_futex_wait() in libc is made weak, so that the override also works when libc is linked with --whole-archive (-sMAIN_MODULE=1 and EMCC_FORCE_STDLIBS=1), where there would otherwise be two definitions of the function.

The default MIN_CHROME_VERSION is 93 and the default MIN_NODE_VERSION is 180300, so the default multithreaded builds get the workaround, which is why the pthreads, Wasm Workers and Audio Worklet code size tests are rebaselined. Building with both -sMIN_CHROME_VERSION=115 and -sMIN_NODE_VERSION=210000 (or newer, or without targeting the environment in question) avoids it.

Before Chrome 115, V8's Liftoff compiler can clobber the register-cached
locals of the function that executes memory.atomic.wait32
(https://crbug.com/1432453). In _do_futex_wait() this overwrote the
cached thread pointer, which made the subsequent `self->wait_addr = 0`
trap. See emscripten-core#28313 for the analysis, where this was hit in Chrome 85.

emscripten-core#28313 works around this by doing the wait in a separate function that is
called through a volatile function pointer, which costs an indirect call
per futex wait and 29 bytes in test_codesize_minimal_pthreads for every
multithreaded build.

Apply the same workaround, but only to the builds that target a version
of Chrome that has the bug, i.e. when MIN_CHROME_VERSION < 115.
emscripten_futex_wait.c is built a second time with the workaround
enabled into a new system library libfutex_wait_liftoff_workaround, which
is linked in ahead of libc in that case, the same way that
libprintf_long_double and libc_optz override parts of libc.

The version of emscripten_futex_wait() in libc is made weak, so that the
override also works when libc is linked with --whole-archive
(-sMAIN_MODULE=1 and EMCC_FORCE_STDLIBS=1), where there would otherwise
be two definitions of the function.

The default MIN_CHROME_VERSION is 93, so the default multithreaded builds
get the workaround, which is why the pthreads, Wasm Workers and Audio
Worklet code size tests are rebaselined. Building with
-sMIN_CHROME_VERSION=115 or newer avoids it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a workaround for a V8 Liftoff compiler bug in Chrome versions older than 115 that caused multithreaded programs to crash during atomic wait operations. The workaround isolates the atomic wait instruction into a separate function called via a volatile pointer to prevent inlining, packaged as a separate library that conditionally overrides the libc implementation when targeting older Chrome versions. The review feedback correctly notes that the changelog should be updated to clarify that this workaround is enabled by default and can be disabled by setting the minimum Chrome version to 115 or higher.

Comment thread ChangeLog.md Outdated
Comment on lines +46 to +48
- Add workaround for an `atomic.wait32` bug in Chrome versions older than 115
(https://crbug.com/1432453), which made multithreaded programs crash. Remember
to pass -sMIN_CHROME_VERSION=x smaller than 115 to activate it. (#28313)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The changelog states "Remember to pass -sMIN_CHROME_VERSION=x smaller than 115 to activate it." However, since the default MIN_CHROME_VERSION is 93, this workaround is actually enabled by default. It would be clearer to inform users that it is active by default and can be disabled by setting -sMIN_CHROME_VERSION=115 or higher.

Suggested change
- Add workaround for an `atomic.wait32` bug in Chrome versions older than 115
(https://crbug.com/1432453), which made multithreaded programs crash. Remember
to pass -sMIN_CHROME_VERSION=x smaller than 115 to activate it. (#28313)
- Add workaround for an atomic.wait32 bug in Chrome versions older than 115
(https://crbug.com/1432453), which made multithreaded programs crash. This is
enabled by default, and can be disabled by passing -sMIN_CHROME_VERSION=115 or higher. (#28313)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good review.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its pretty sad to have to add this new system library (which gets linked by default) .. and its name is kind of a mouthfull, but if this bug really does exist all the way to 115 then it seems reasonable to me.

Is this complexity with the extra library worth a 20-30 byte codesize cost for multi-threading users? I'm not sure. This kind of codesize fluctuation is the kind of thing we often see when making other changes the codebase.

How would you feel about just making it the default and eating the 20-30 byte code? What if offered to find a 30 byte codesize saving elsewhere to offset it with?

@sbc100

sbc100 commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

So just to be clear, this issue was not solved between chrome 85 and 86, but in fact between chrome 114 and 115?

@juj

juj commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator Author

So just to be clear, this issue was not solved between chrome 85 and 86, but in fact between chrome 114 and 115?

Yes. Kleisauke identified in this comment #28313 (comment) that the crbug is https://issues.chromium.org/issues/40902945.

As per this comment, https://issues.chromium.org/issues/40902945#comment6, the bug was still reproducible in Chrome 114. And the Chrome bug was fixed in 115.

Is this complexity with the extra library worth a 20-30 byte codesize cost for multi-threading users? I'm not sure.

In the workaround PR, there is a use of a volatile function pointer, which can pessimize optimization: https://github.com/emscripten-core/emscripten/pull/28344/changes#diff-bfb331465a181bfe5b7f1e040c28e61c918f6ee11304aae90b9f0ceecb2b4981R155. The whole thing does feel awkward, and there is no clear answer as to when we'd be able to evolve out of that workaround if we land it unconditionally?

If the idea is that we'd remove it immediately in the next version that we'd bump up to Emscripten 7, then would it be much different from not landing any workaround, and telling people to do their builds from their local hotpatched fork?

I'm trying to cater to both sides, so that we wouldn't have a rush to evolve out from the workaround. The workaround is clumsy, I agree (and also I agree system_libs combinatorics have proven to be brittle in the past..). Though I hope we'd be able to more routinely accommodate workarounds like this to C/C++ system libs (in the migration from JS -> C/C++ code we've lost some of that flexibility).

@juj

juj commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator Author

So just to be clear, this issue was not solved between chrome 85 and 86, but in fact between chrome 114 and 115?

Yes. Kleisauke identified in this comment #28313 (comment) that the crbug is https://issues.chromium.org/issues/40902945.

Err, actually. Scratch that. These are not the same issue according to Fable's search. It came back with the following:

The Chrome 85 / Tizen crash and crbug 1432453 are two unrelated Liftoff bugs, with different causes, different version ranges and different fixes. One 81-byte module reproduces both, and running it on official d8 builds flips exactly at the commits I identified from source.

The two bugs

Tizen / Chrome 85 bug crbug 1432453
Cause PrepareBuiltinCall moved the wait operands into the builtin's parameter registers before spilling the locals cached in those registers AtomicWait retyped the address operand's i32 stack slot to intptr, so spilling it overwrote the neighbouring slot
Symptom a local comes back holding the wait's address, expected value or timeout (matches self == futex value in emscripten-core/emscripten#28313) the last local comes back with its low 32 bits zeroed
Introduced V8 4212c303b6 (2020-03-20), first in 8.3.42 V8 cd6114cdbf (2022-11-11), first in 11.0.1
Fixed V8 0acb07c543 (2020-09-25, crrev.com/c/2429032), first in 8.7.195 V8 944c293330 (2023-04-25, crrev.com/c/4465327), first in 11.5.1
Chrome affected 83–86 110–114
Chrome fixed 87 115
Node affected 14.5.0–14.21.3 and all 15.x (needs --experimental-wasm-threads) all 20.x
Node fixed 16.0.0 21.0.0
Architectures not x64-specific in source; only x64 tested x64 only (arm/arm64 use 8-byte slots)

Neither fix was backported: the final 8.6 and 11.4 branch sources and the last Node 14, 15 and 20 releases still have the buggy code.

So hmm, I'll have to think about this a bit more. We are confusing two different issues here.

@sbc100

sbc100 commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

I wonder if it would be possible to write a binaryen pass to work around these issues (e.g. by injecting a new/unused local at the right location where atomic.wait was used)?

@juj

juj commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator Author

I asked Fable to repro the issues and see if the workaround applies.

It reports that coincidentally this outlining workaround does fix both issues, so we are lucky in that sense.

However, quite obviously in hindsight, it also reports that there are several other sources of this issue. It is not enough to patch this one function. Here's what it spat out:

"""

The PR's code works around both bugs in emscripten_futex_wait, but it only covers that one function: other memory.atomic.wait32 call sites still break on the crbug V8 range with the PR applied. The same outlining idiom fixes those too, so a full workaround does exist.

Results

All runs are real Emscripten builds from your checkout, on x64 Node with Liftoff only: Node 15.14.0 (V8 8.6) for the Tizen bug, Node 20.18.0 (V8 11.3) for crbug 1432453, and Node 24 as a control (everything passes there).

Case V8 8.6 (Tizen bug) V8 11.3 (crbug)
emscripten_futex_wait, stock main traps: operation does not support unaligned accesses in _do_futex_wait passes, but only by luck
emscripten_futex_wait, PR's source with the workaround passes passes
5.0.6 futex source (the one from #26816) not run fails: waits return after 1–13 ms instead of 20–1500 ms
5.0.6 futex source + the PR's outlining idiom not run passes
User function calling emscripten_atomic_wait_u32 inline wrong result wrong result
Same function with the wait outlined as in the PR correct correct
emscripten_lock_wait_acquire, emscripten_semaphore_wait_acquire, with PR applied pass return after ~30 ms instead of 100/1500 ms
Same primitives with the idiom applied to emscripten_thread_primitives.c (my proof of concept) pass pass
  • Tizen bug: the stock build reproduces the exact Tizen crash, and the PR's source fixes it on the main thread and on a pthread.
  • crbug: current main is not visibly affected in _do_futex_wait because the local V8 clobbers there is an f64 that is dead after the wait. That depends on compiler output, as 5.0.6 shows.
  • Why outlining works for both: each bug only corrupts locals of the function that executes the wait. The helper has nothing live after the wait, and the caller reaches it through an ordinary call.
  • The volatile pointer is needed: a plain __attribute__((noinline)) helper in my test was inlined back by Binaryen at link time.

What the PR does not cover

  • Lock and semaphore primitives. emscripten_thread_primitives.c calls emscripten_atomic_wait_u32 directly. On V8 11.3 a spurious wake makes the timed lock and semaphore waits give up early, with or without the PR. They did not misbehave on V8 8.6 in my test.
  • User code. emscripten_atomic_wait_u32 / _u64 are inline in emscripten/atomic.h, so a libc override cannot reach them. They break on both V8 ranges.
  • Node 20. The workaround library is selected only on MIN_CHROME_VERSION < 115; it also needs MIN_NODE_VERSION < 210000.

emscripten_wasm_worker_sleep and a wait in a function with no locals (the shape of wasm-ld's __wasm_init_memory) showed no failure on either V8.

Extending the workaround

Routing every libc wait through one outlined helper fixes the library call sites; my proof of concept did this with a forced-include header that redefines emscripten_atomic_wait_u32 when compiling emscripten_thread_primitives.c. User code would need the header to call an out-of-line libc function, or a documented opt-in, since it is compiled before the link-time browser target is known.

How this was tested

  • I did not build the PR branch through its system_libs.py plumbing. I compiled the PR's emscripten_futex_wait.c with libc's flags and the workaround define, and linked that object ahead of libc. The library selection logic and the weak-symbol override for MAIN_MODULE are untested.
  • Node 15 needed two JS-side shims to run current Emscripten output (the node: require prefix and a performance global); the wasm is unmodified.
  • Only x64 was tested, and only with tier-up disabled.

@juj

juj commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator Author

We could extend emscripten_atomic_wait_u32 / _u64 to do the same outlining here.

Though Fable doesn't mention it above, but it seems like the issue should be happening also with any code that calls __builtin_wasm_memory_atomic_wait32 directly as LLVM intrinsics? That would be a bit of a headache.

So it does seem like maybe we'd indeed need a post-build Binaryen link step that outlines all accesses to wait intrinsics behind an uninlined function?

The V8 Liftoff fix for https://crbug.com/1432453 first shipped in V8 11.5.
Every release of Node.js 20 has V8 11.3, so Node.js 20 has the bug as well,
and the first Node.js version with the fix is 21.0.0.

Link in libfutex_wait_liftoff_workaround also when MIN_NODE_VERSION < 210000,
so that building with -sMIN_CHROME_VERSION=115 does not drop the workaround
from a build that still targets Node.js 20.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@juj juj changed the title Work around V8 Liftoff atomic.wait32 bug only when targeting Chrome 115 Work around V8 Liftoff atomic.wait32 bug only when targeting Chrome < 115 or Node.js < 21 Nov 20, 2026
@juj

juj commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator Author

I'll close this PR since this is too narrow fix.

@juj juj closed this Nov 21, 2026
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

Successfully merging this pull request may close these issues.

2 participants