commit b169056c5a72e031ba52df9db6b10d0ea57b67d8
parent a533719b0146720d9bdac2c3228dc57170cb1419
Author: anon <anon@example.com>
Date: Tue, 13 Oct 2026 14:53:20 +0000
Run native guest threads in shared-memory browser workers
Diffstat:
2 files changed, 99 insertions(+), 6 deletions(-)
diff --git a/docs/browser-porting.md b/docs/browser-porting.md
@@ -1116,9 +1116,9 @@ returns zero. A zero creation flag starts execution immediately, while
`CREATE_SUSPENDED` requires a later resume. The caller closes the handle.
See [MSVCRT thread creation](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/beginthread-beginthreadex).
-This is the verified browser startup boundary after the descriptor-pool
-callbacks. Continuing requires a separately runnable guest thread with its
-own CPU context, stack, TEB/FS base and TLS, while sharing the process memory
-and synchronization state. The existing native host thread launcher does
-not supply a browser worker launcher. Resource-worker startup should be
-verified before treating later resource loading or world rendering as working.
+The thread needs its own CPU context, stack, TEB/FS base and TLS, while sharing
+process memory and synchronization state. The existing native host launcher
+does not provide a browser worker launcher. See [native thread contracts](browser-threads.md)
+for the ACE entry adapter, lock ordering and shared-WASM requirements.
+Resource-worker execution must be verified before treating resource loading
+or world rendering as working.
diff --git a/docs/browser-threads.md b/docs/browser-threads.md
@@ -0,0 +1,93 @@
+# Native thread contracts
+
+These observations refer to AO 13.2 in `client-target`. The inspected ACE
+SHA-256 is `a1a0d96a5ff605f6611b8584204dad855914b1cbeedc0389f3a68279e015c22d`.
+Addresses below are image RVAs; ACE is mapped at guest base `0x410000`.
+
+## Resource-worker startup
+
+PPR's `ResourceWorker_t::open` calls `ACE_Task_Base::activate`. ACE's
+`activate` at `0x893b0` passes `svc_run` (`0x89520`) and the task object into
+its thread manager. `svc_run` calls the task's virtual `svc` at vtable +0x58,
+then `close` at +0x4c. PPR supplies these methods at `0xdf40` and `0xdf30`.
+
+`ACE_OS::thr_create` at `0x55e00` calls `_beginthreadex` at `0x55ed9`.
+The six cdecl arguments are security attributes (zero), stack size, entry
+address, adapter object, creation flags and a thread-ID output pointer.
+The guest return address is `0x465edf`. A nonzero requested priority causes
+suspended creation, followed by `SetThreadPriority` and `ResumeThread` unless
+the caller explicitly requested suspension.
+
+The entry adapter at `0x55d40` is `mov ecx,[esp+4]; jmp 0x55b60`.
+Although the CRT entry contract normally uses stdcall, this ACE thunk does
+not return with `ret 4`. Its invoke method installs per-thread ACE state,
+calls the stored function and argument, runs TLS destructors and calls
+`_endthreadex`. That API ends the thread without returning to guest code.
+
+`spawn_i` at `0x8aff0` holds the descriptor's critical section at object
++0x38 across thread creation and registration. Calling the child entry
+synchronously inside `_beginthreadex` can deadlock; it needs independent
+execution. ACE's logger at `0x39020` uses `TlsGetValue` and lazily allocates
+thread-local state. Sharing one TEB, TLS array or CRT errno pointer between
+threads breaks this contract.
+
+REA evidence:
+
+- `ev_b51fc8808b408ddfdb764dc845b0c79fe0db6d68833833a59145410750052cdb`: thread creation arguments.
+- `ev_2ed2f4c34a57def23fc6d940864b73af90603e4b4f9f4998218c3eeceb660623`: entry thunk.
+- `ev_c388a1f5bcdfac4ab0fe26ef08daa0dd519f35b1818daf937fa1e4b1afdf616b`: invoke and thread exit.
+- `ev_1e1793a2aa722761a59aa0c0363c8cfdac255b55e4533ada502d782b8131a597`: task activation.
+- `ev_5c129767925f931238574e9a8fee640d80ddb302946bfc2e4268d693f04adfa1`: task service dispatch.
+- `ev_743c5c400e99410bab545c52a249ceefce06bd537d256746840f0b99e90f240d`: thread-local logger.
+
+## Shared WASM execution
+
+Each guest thread needs a separate x86 CPU context, guest stack, TEB/FS base,
+thread ID and TLS. The PE images, guest heap and synchronization objects are
+process-wide. Each browser Worker also needs its own WASM stack and Rust TLS;
+these are separate from the emulated x86 structures.
+
+wasm-bindgen's thread transform initializes each instance's stack and TLS
+over imported shared memory. A child can instantiate the same compiled module
+with the existing `WebAssembly.Memory`, then execute an exclusively transferred
+Rust context allocation. It must not reload the guest images, remount assets
+or repeat process initialization. JS references belong to individual Worker
+instances and cannot be treated as process-wide Rust values.
+
+Route synchronous host requests from every Worker directly to the main page.
+Routing them through a parent Worker fails when that parent is blocked in
+guest code. Ending the process must terminate all its Workers. Ending one
+guest thread must stop its guest CPU loop, run applicable DLL_THREAD_DETACH
+callbacks, publish its exit code and wake waiters. DLLs that call
+`DisableThreadLibraryCalls` do not receive these per-thread notifications.
+
+Critical sections and CRT locks are recursive and owned by guest thread IDs.
+Events and semaphores must actually block and wake across Workers. Rust
+`Instant` is unavailable on this WASM target, so timed waits need the host
+monotonic counter. `performance.now()` has a different origin in each Worker;
+`performance.timeOrigin + performance.now()` supplies a common origin.
+
+Primary source: [wasm-bindgen thread transform at 0.2.129](https://github.com/wasm-bindgen/wasm-bindgen/blob/0.2.129/crates/cli-support/src/transforms/threads/mod.rs).
+
+## Verification limits
+
+Native tests establish suspension/resume, independent TEB/TLS/errno, handle
+waiting, thread exit and contended synchronization. A JS fixture checks shared
+memory, direct host replies and Worker cleanup. Neither proves that ACE's
+resource service runs in the browser; verify that against the real client.
+
+With the thread-creation import implemented, browser startup reaches MSVCRT
+`time` from guest return address `0x9d26a0`. The main thread can reach a missing
+import before a new Worker finishes instantiating, so the absence of a child
+progress line during that failure is not evidence that creation failed.
+
+This client predates Visual Studio 2005: `time_t` is signed 32-bit seconds
+since the Unix epoch. `time` returns the value and, when its pointer argument
+is nonzero, writes exactly four bytes. See Microsoft's
+[historical time_t width](https://learn.microsoft.com/en-us/cpp/c-runtime-library/time-management)
+and [time contract](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/time-time32-time64).
+
+Still unverified: live DLL thread callbacks, ACE TLS destructor callbacks,
+resource queue processing and rendering. Named event/mutex sharing,
+`WaitForMultipleObjects`, abandoned mutexes and `_beginthread` also require
+further compatibility work when reached.