aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


docs/browser-threads.md (7008B)


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:

The next observed indirect startup target is ACE RVA 0x13ba0 (guest 0x423ba0). It enters the critical section at object +0x28, checks the field at +0x20 and calls vtable slots +0x90, +0x70 and +0x64. The function returns with ret 8. Its only reported incoming reference is the raw method pointer at RVA 0xacb14; the concrete template class name has not been recovered. Pseudocode has confused stack locals here, so the decoded assembly is the stronger evidence for arguments and cleanup.

The surrounding 40-entry table occupies 0xacac8..0xacb64, immediately after the exported ACE_Message_Queue_Base table at 0xaca84. Its +0x90 slot is 0x14840, which waits through ACE_Condition_Thread_Mutex::wait on the object at +0x74 and rechecks a virtual predicate and the field at +0x20. This supports identifying the table as a concrete ACE message queue, without establishing its exact template name. These method pointers need translation independently of the queue constructors and ordinary imported calls.

REA evidence: ev_e32c33c5e903944149eb978d036c2fd4b5e2460abf565f7c0b322e80e006b4f5 and ev_33c4b092df88b7339c8df4b4bb2b8c69369affb4e84d978a75411317656a1cd8.

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.

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 and time contract.

After that import, browser startup reaches ACE's GetSystemTimeAsFileTime call with guest return address 0x41176e. Its output is eight bytes, low DWORD first, counting 100-nanosecond intervals since 1601-01-01 UTC. Converting Unix milliseconds requires multiplying by 10,000 and adding 116,444,736,000,000,000. See the FILETIME layout.

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.