Repository navigation
Conversation
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>
There was a problem hiding this comment.
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.
| - 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) |
There was a problem hiding this comment.
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.
| - 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) |
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
sbc100
left a comment
There was a problem hiding this comment.
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?
|
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.
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). |
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
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. |
|
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)? |
|
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 ResultsAll 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).
What the PR does not cover
Extending the workaroundRouting 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 How this was tested
|
|
We could extend Though Fable doesn't mention it above, but it seems like the issue should be happening also with any code that calls 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>
|
I'll close this PR since this is too narrow fix. |
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 = 0trap. 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.