aorecomp

Anarchy Online classic in the browser

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


commit 1ea7b524e3456d244f74b38cca4c05fcfc573caf
parent f969ed81f968744525c6f207678d45d335190a78
Author: anon <anon@example.com>
Date:   Tue, 13 Oct 2026 02:34:12 +0000

Execute AO DLL and CRT startup in native and browser runtimes

Diffstat:
M.gitignore | 3+++
Mdocs/browser-porting.md | 110++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Mserver/main.go | 8+++++++-
3 files changed, 117 insertions(+), 4 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -16,6 +16,9 @@ browser/native-render/src/generated/ browser/native-render/data/ browser/native-render/report.html browser/native-render/linked-generated/ +browser/native-client/generated/ +browser/native-client/pkg/ +browser/native-client/src/imports.rs tools/*.json tools/*.png tools/*.jpg diff --git a/docs/browser-porting.md b/docs/browser-porting.md @@ -432,9 +432,113 @@ are explicitly null in this bounded image. Calling those paths would fail. Forwarders are rejected. DLL initialization, TLS and full dependency loading remain unfinished; success here does not establish client startup. -The next startup work needs to retain this native import path while connecting -the remaining imports to Windows/CRT implementations, discover the required -DLL initialization order, and register dynamically requested guest exports. +### DLL startup and CRT contracts + +The pinned 13.2 Clientr ordinary-import closure contains 34 PE images. Its +static dependency graph has no cycles. These images have no TLS directories +or forwarded exports. This does not rule out dependencies requested later +through LoadLibrary. Most DLLs prefer `0x10000000`; MSVCP60 prefers +`0x780c0000`, MSS32 `0x21100000`, and Clientr `0x400000`. Compact rebasing is +necessary for a shared guest address space. + +Windows invokes a DLL entrypoint with `(module_base, DLL_PROCESS_ATTACH=1, +reserved)` before entering the executable. The reserved argument is nonzero +for static process loading and zero for LoadLibrary, as specified by +[Microsoft's DllMain contract](https://learn.microsoft.com/en-us/windows/win32/Dlls/dllmain). +The entrypoint may be a CRT +wrapper, not the application's DllMain. It allocates destructor-registration +tables, copies CRT data and invokes `_initterm` before reaching user DllMain. +Returning success while skipping those callbacks leaves C++ globals +unconstructed. MR alone has twelve callbacks; ACE has 1,386. MSVCP60 must +initialize before AO DLLs construct its iostream objects. MSS32 and ctreestd +have embedded CRT initialization loops rather than imported `_initterm`. + +The native imports must retain their PE names, ordinals and original calling +conventions. Host Windows APIs generally use stdcall; MSVCRT's ordinary C +functions use cdecl, with the caller removing arguments. Theseus's generated +suffix `_stdcall` is a dispatcher naming convention, not sufficient evidence +for the guest ABI. A cdecl host adapter removes only the return address. +Data imports such as `_adjust_fdiv`, `_iob` and `_pctype` must point to guest +variables, not callable trampolines. `_pctype` itself is a pointer-valued +variable. The legacy FILE layout used by `_iob` is 32 bytes; MSVCP60 refers +to stdin, stdout and stderr at offsets 0, 32 and 64. + +`_EH_prolog` is a special assembly helper, not an ordinary C function. With +entry ESP=S and an exception handler in EAX, it places the previous FS:[0] +at S-12, the handler at S-8, and the initial exception state -1 at S-4. +It replaces the old return cell at S with the saved EBP, sets EBP=S and +FS:[0]=S-12, and resumes the caller with ESP=S-12. The compatibility reference +is [Wine's `_EH_prolog`](https://github.com/wine-mirror/wine/blob/master/dlls/msvcrt/except_i386.c). +Implementing this frame setup does not implement exception unwinding. + +Code discovery must distinguish executable sections from the address span +covering multiple DLLs. Even executable sections can contain jump tables. +Inspected CRT callback tables are better startup seeds than treating all +data-held code addresses as function entrypoints. Native PE imports whose +exports lie in executable sections must be followed when reached code reads +their IAT entries: loading addresses into registers can hide subsequent calls. +Record other runtime-observed targets +as module RVAs and check them against the pinned image. + +On 2026-10-12, native and browser WASM runs both completed all 33 actual DLL +process attaches and entered Clientr's executable CRT startup. +HR's entry is RVA `0x12e1`; successful attach returns 1, +increments the count at RVA `0x3024`, and creates the on-exit table referenced +at RVA `0x3030`. MSVCP60's entry is RVA `0x1000`, and its eighteen initializer +callbacks execute through the CRT adapter. ACE constructs MSVCP60's +`ios_base::Init` at RVA `0x1703` and `_Winit` at RVA `0x1e7b` through imports. +LR's array initializer passes unexported constructor/destructor callbacks +at RVAs `0x6530`/`0x7630` to its iterator at RVA `0x8e00`; these need +explicit discovery even though the containing initializer is translated. +ER's iterator at RVA `0x10d88` constructs five 256-byte elements through +the unexported constructor at RVA `0x4390`. EER's iterator at RVA `0x82d0` +uses callbacks at RVAs `0x21c0`/`0x7910` for two arrays of 16-byte elements. +These are startup milestones, not proof of +complete client startup or world rendering. + +Clientr's executable CRT sets GUI application type 2, obtains writable +`_fmode`/`_commode` variables through pointer-returning imports, registers a +math error handler, builds argv/environment through `__getmainargs`, and +executes its initializer tables. It then scans `_acmdln` itself and calls +WinMain at RVA `0x1080` from RVA `0x45b5`. DLL attach success therefore does +not establish that the executable has reached WinMain. +The current harness supplies only `Clientr.exe` as its command line; actual +game launch arguments and environment still need the original launcher's +contract. +The observed executable initializer path reaches ACE's Winsock startup +before WinMain. Its `ws2_32.dll` import by ordinal 115 is `WSAStartup`, as +confirmed against the Windows PE export table. In the pinned ACE image, +the call continues at RVA `0x58b3d`. Native and WASM runs currently stop +there; DLL initialization is complete, but Winsock initialization and the +browser's transport to the local server remain unresolved. + +Floating-point CRT returns use x87 ST(0). For example, LLR's `floor` call +at RVA `0x2bb8` passes an eight-byte double on the stack, immediately uses +the returned FPU value in `fsubr`, and then removes the eight-byte argument. +An integer-only CRT dispatcher silently breaks this calling convention. +Miles DLL initialization also requires Windows directory queries and the +performance-counter pair. The counter must advance monotonically and use +the units reported by `QueryPerformanceFrequency`; see Microsoft's +[performance counter guidance](https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps). + +The embedded ctreestd CRT calls `GetStartupInfoA` at RVA `0x3cbcb` with a +stack output buffer, then checks `cbReserved2` and `lpReserved2` for inherited +file descriptors. An adapter must overwrite that buffer, including `cb`; +its initial contents do not specify the output capacity. The 32-bit +STARTUPINFOA layout is 68 bytes. See Microsoft's +[GetStartupInfo contract](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getstartupinfow). +MSVCRT `rand` uses a per-thread 32-bit seed, updated by +`seed * 214013 + 2531011` with wraparound, and returns bits 16 through 30. +Its initial seed is 1; `srand` replaces it. The compatibility reference is +[Wine's rand/srand](https://github.com/wine-mirror/wine/blob/master/dlls/msvcrt/misc.c). + +The browser host uses a worker and shared memory because native Win32 calls +can synchronously wait for the main page to handle host operations. It needs +COOP/COEP headers and an atomics-enabled WASM build with imported shared +memory. Shared memory alone does not supply CreateThread: Theseus's browser +host still lacks the worker/thread glue for its Rust thread-spawn path. +Direct3D, asset delivery and browser-compatible networking remain separate +requirements after DLL initialization. ### Graphics and editable Rust boundaries diff --git a/server/main.go b/server/main.go @@ -21,7 +21,13 @@ func main() { http.HandleFunc("GET /users/{character}", server.userPage) http.HandleFunc("POST /users", server.createUser) http.HandleFunc("POST /users/{character}", server.editUser) - http.Handle("GET /browser/", http.StripPrefix("/browser/", http.FileServer(http.Dir("browser")))) + browserFiles := http.StripPrefix("/browser/", http.FileServer(http.Dir("browser"))) + http.HandleFunc("GET /browser/", func(w http.ResponseWriter, r *http.Request) { + // Shared WASM memory lets the client worker wait for browser host calls. + w.Header().Set("Cross-Origin-Opener-Policy", "same-origin") + w.Header().Set("Cross-Origin-Embedder-Policy", "require-corp") + browserFiles.ServeHTTP(w, r) + }) http.HandleFunc("POST /time", server.setGameTime) http.HandleFunc("POST /teleport/{character}", server.teleportPlayer) http.HandleFunc("POST /reload-npcs", server.reloadNPCData)