Repository navigation
Instantiate the browser module owner before warming its thread pool - #19
Merged
Merged
Conversation
The first WASI threads instance initializes the owner's TLS base. Warming secondary instances first leaves the owner's TLS overlapping its stack. Instantiate the owner first and defer constructors until the pool is ready. Co-Authored-By: Codex <noreply@openai.com>
Compile a synthetic TLS and stack fixture through the SDK. Require independent owner and child TLS with constructor spawning in Chromium, Firefox and WebKit. Use secondary-first Node instantiation as the corruption negative control. Co-Authored-By: Codex <noreply@openai.com>
Use a test-local compiler wrapper to request the linker layout whose uninitialized owner TLS overlaps its stack, independent of toolchain defaults. Co-Authored-By: Codex <noreply@openai.com>
Co-Authored-By: Codex <noreply@openai.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
The browser host awaited its warm thread pool before it instantiated the owning instance. A wasi-threads module with the stack-first layout sets an instance's thread-local base only in the instance that wins the shared-memory initialization; every later instance skips that assignment. With a pool instance first, the owner's thread-local base stayed at zero instead of 65,536, so its thread-local block sat on top of its own main stack (0 to 65,536). Ordinary stack writes then changed thread-local data, and results came out wrong without any error.
Node was never affected: it creates pool instances lazily, after the owner. The fault needs a browser host with the thread option on (
enableBrowserWasiThreads, or the__SDM_ENABLE_BROWSER_WASI_THREADS__global); with the option off there is no pool and no fault. Every released version up to and including 0.10.1 has it.How it was found
The
propagator/sdn-apmodule returned different positions in Chromium with a prewarmed pool than on one thread, while matching in Node. In released sdn-ap 0.6.63 the thread-local block is 343,988 bytes: it spanned 0 to 343,988 with the pool first, 65,536 to 409,524 with the owner first. Its frame output differs from the one-thread result at 2, 4 and 8 total threads; a sampled sweep of direct states agreed, so whether a given workload shows it depends on how deep the owner's stack goes.Shown both ways: forcing the pool-first order in Node reproduces the corruption, and owner-first in Chromium removes it.
The change
src/host/browserModuleHarness.js: the owner is instantiated first, then the pool is created, then constructors run. Thewasi.thread-spawnimport closes over the thread host, so constructors can still spawn threads. No change to guest code, pool sizing, the Node worker path or the WasmEdge path.The regression test
test/browser-owner-tls.test.jscompiles a small fixture (a thread-local array and a stack array, no other content) through the SDK with the stack-first layout. In Node, Chromium, Firefox and WebKit it requires the owner's thread-local data to lie outside its stack, a canary to survive stack writes and a child thread, and a constructor to be able to spawn. The pool-first order in Node is the negative control and must corrupt the canary. No binary fixture is committed. The test is part oftest:host-surfaces.What was run
npm testandcheck:compliancepass.For consumers
Take a release that contains this change. Compiled modules do not need a rebuild: the change is host JavaScript only. A consumer that bundles or vendors the SDK's host code rebuilds and redeploys that bundle. Until then, leave the browser thread option off.
🤖 Generated with Claude Code
https://claude.ai/code/session_01PT7jJdf1uRvH9udhT59VMh