commit cdbae42d5cea713706bc52f9043db335c1e6fe89
parent a9eecc58f9847224f4f668f9a4e2aa815c005fb1
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 00:18:14 +0000
Port MSVCRT exception objects and virtual dispatch to Linux
Diffstat:
7 files changed, 398 insertions(+), 2 deletions(-)
diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux
@@ -3,7 +3,7 @@ CPPFLAGS := -Iruntime -Isrc -Idata -DRECOMP_PORTABLE
CFLAGS := -O2 -fno-strict-aliasing
GAME_SOURCES := $(wildcard src/*/*.c) src/dispatch.c
DATA_SOURCES := $(wildcard data/*.c)
-RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_platform.c runtime/linux_sync.c runtime/linux_tls.c runtime/linux_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c
+RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_platform.c runtime/linux_sync.c runtime/linux_tls.c runtime/linux_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c
VENDOR_SOURCES := $(wildcard vendor/msvcp60/source/*.c) $(wildcard vendor/ace/source/*.c)
OBJECTS := $(patsubst %.c,build/linux/%.o,$(GAME_SOURCES) $(DATA_SOURCES) $(RUNTIME_SOURCES) $(VENDOR_SOURCES))
@@ -92,11 +92,15 @@ build/linux/delete-file: tests/delete_file.c runtime/linux_platform.c runtime/gu
mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@
+build/linux/guest-exception: tests/guest_exception.c runtime/guest_exception.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_exception.h runtime/guest_heap.h runtime/guest_memory.h
+ mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
+
build/linux/guest-stdio: tests/guest_stdio.c runtime/guest_stdio.c runtime/guest_crt.c runtime/guest_format.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_stdio.h runtime/guest_crt.h runtime/guest_format.h runtime/guest_heap.h runtime/guest_memory.h
mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
-test: build/linux/linecrypt build/linux/guest-heap build/linux/guest-crt build/linux/linux-platform build/linux/msvcp-strings build/linux/ace-token build/linux/msvcp-stdio-data build/linux/guest-eh build/linux/guest-rand build/linux/semaphore build/linux/guest-scanf build/linux/network-startup build/linux/os-version build/linux/guest-format build/linux/event build/linux/tls build/linux/module-filename build/linux/delete-file build/linux/guest-stdio
+test: build/linux/linecrypt build/linux/guest-heap build/linux/guest-crt build/linux/linux-platform build/linux/msvcp-strings build/linux/ace-token build/linux/msvcp-stdio-data build/linux/guest-eh build/linux/guest-rand build/linux/semaphore build/linux/guest-scanf build/linux/network-startup build/linux/os-version build/linux/guest-format build/linux/event build/linux/tls build/linux/module-filename build/linux/delete-file build/linux/guest-stdio build/linux/guest-exception
./build/linux/linecrypt
./build/linux/guest-heap
./build/linux/guest-crt
@@ -116,5 +120,6 @@ test: build/linux/linecrypt build/linux/guest-heap build/linux/guest-crt build/l
./build/linux/module-filename
./build/linux/delete-file
./build/linux/guest-stdio
+ ./build/linux/guest-exception
-include $(OBJECTS:.o=.d)
diff --git a/decompiled/runtime/guest_exception.c b/decompiled/runtime/guest_exception.c
@@ -0,0 +1,106 @@
+#include "guest_exception.h"
+#include "guest_heap.h"
+
+static uint32_t exception_data;
+
+static uint32_t exception_vtable(void)
+{
+ if (!exception_data) {
+ exception_data = guest_malloc(32);
+ if (!exception_data) abort();
+ /* RTTI is not implemented; these two slots dispatch to native C adapters. */
+ MEM32(exception_data) = 0;
+ MEM32(exception_data + 4) = 0xf1000000u;
+ MEM32(exception_data + 8) = 0xf1000004u;
+ memcpy((void *)ADDR(exception_data + 12), "Unknown exception", 18);
+ }
+ return exception_data + 4;
+}
+
+static void construct(uint32_t object, uint32_t message, int owned)
+{
+ MEM32(object) = exception_vtable();
+ MEM32(object + 8) = 0;
+ if (message && owned) {
+ uint32_t size = strlen((const char *)ADDR(message)) + 1;
+ uint32_t copy = guest_malloc(size);
+ if (copy) {
+ memcpy((void *)ADDR(copy), (const void *)ADDR(message), size);
+ MEM32(object + 8) = 1;
+ }
+ message = copy;
+ }
+ MEM32(object + 4) = message;
+}
+
+static void exception_ctor(void)
+{
+ construct(g_ecx, 0, 0);
+ g_eax = g_ecx;
+ g_esp += 4;
+}
+
+static void exception_message_ctor(void)
+{
+ construct(g_ecx, MEM32(MEM32(g_esp + 4)), 1);
+ g_eax = g_ecx;
+ g_esp += 8;
+}
+
+static void exception_copy_ctor(void)
+{
+ uint32_t source = MEM32(g_esp + 4);
+ construct(g_ecx, MEM32(source + 4), MEM32(source + 8) != 0);
+ g_eax = g_ecx;
+ g_esp += 8;
+}
+
+static void destroy(uint32_t object)
+{
+ MEM32(object) = exception_vtable();
+ if (MEM32(object + 8)) guest_free(MEM32(object + 4));
+}
+
+static void exception_dtor(void)
+{
+ destroy(g_ecx);
+ g_esp += 4;
+}
+
+static void exception_deleting_dtor(void)
+{
+ uint32_t object = g_ecx, flags = MEM32(g_esp + 4);
+ if (flags & 2) {
+ uint32_t count = MEM32(object - 4);
+ while (count) destroy(object + --count * 12);
+ object -= 4;
+ } else destroy(object);
+ if (flags & 1) guest_free(object);
+ g_eax = object;
+ g_esp += 8;
+}
+
+static void exception_what(void)
+{
+ exception_vtable();
+ g_eax = MEM32(g_ecx + 4);
+ if (!g_eax) g_eax = exception_data + 12;
+ g_esp += 4;
+}
+
+recomp_func_t guest_exception_virtual(uint32_t address)
+{
+ if (address == 0xf1000000u) return exception_deleting_dtor;
+ if (address == 0xf1000004u) return exception_what;
+ return NULL;
+}
+
+recomp_func_t guest_exception_lookup(const char *name)
+{
+ if (!strcmp(name, "??0exception@@QAE@XZ")) return exception_ctor;
+ if (!strcmp(name, "??0exception@@QAE@ABQBD@Z")) return exception_message_ctor;
+ if (!strcmp(name, "??0exception@@QAE@ABV0@@Z")) return exception_copy_ctor;
+ if (!strcmp(name, "??1exception@@UAE@XZ")) return exception_dtor;
+ if (!strcmp(name, "?what@exception@@UBEPBDXZ")) return exception_what;
+ return NULL;
+}
diff --git a/decompiled/runtime/guest_exception.h b/decompiled/runtime/guest_exception.h
@@ -0,0 +1,7 @@
+#ifndef GUEST_EXCEPTION_H
+#define GUEST_EXCEPTION_H
+#include "recomp_types.h"
+
+recomp_func_t guest_exception_lookup(const char *name);
+recomp_func_t guest_exception_virtual(uint32_t address);
+#endif
diff --git a/decompiled/runtime/linux_host.c b/decompiled/runtime/linux_host.c
@@ -4,6 +4,7 @@
#include "guest_crt.h"
#include "guest_format.h"
#include "guest_stdio.h"
+#include "guest_exception.h"
#include "linux_platform.h"
#include "linux_sync.h"
#include "linux_tls.h"
@@ -97,6 +98,8 @@ recomp_func_t recomp_lookup_manual(uint32_t va) { (void)va; return NULL; }
recomp_func_t recomp_lookup(uint32_t va)
{
+ recomp_func_t adapter = guest_exception_virtual(va);
+ if (adapter) return adapter;
const recomp_dispatch_entry_t *table = recomp_dispatch_table;
uint32_t lo = 0, hi = recomp_dispatch_count;
if (va >= 0x46000000u && va < 0x47000000u) {
@@ -189,6 +192,7 @@ recomp_func_t recomp_lookup_import(uint32_t va)
recomp_func_t function = guest_stdio_lookup(imports[current_import].name);
if (!function) function = guest_crt_lookup(imports[current_import].name);
if (!function) function = guest_format_lookup(imports[current_import].name);
+ if (!function) function = guest_exception_lookup(imports[current_import].name);
if (function) return function;
}
if (!strcasecmp(imports[current_import].dll, "msvcp60.dll") && imports[current_import].name) {
diff --git a/decompiled/tests/guest_exception.c b/decompiled/tests/guest_exception.c
@@ -0,0 +1,178 @@
+#include "recomp_types.h"
+#include "guest_memory.h"
+#include <assert.h>
+#ifdef _WIN32
+#include <windows.h>
+static HMODULE crt;
+ptrdiff_t g_mem_base;
+#else
+#include "guest_exception.h"
+#include "guest_heap.h"
+#endif
+
+static uint32_t call(const char *name, uint32_t function_address, uint32_t object,
+ unsigned count, uint32_t argument)
+{
+#ifdef _WIN32
+ void *function = name ? (void *)GetProcAddress(crt, name) : (void *)(uintptr_t)function_address;
+ assert(function);
+ if (count) return ((uint32_t (__thiscall *)(void *, uint32_t))function)((void *)(uintptr_t)object, argument);
+ return ((uint32_t (__thiscall *)(void *))function)((void *)(uintptr_t)object);
+#else
+ recomp_func_t function = name ? guest_exception_lookup(name) : guest_exception_virtual(function_address);
+ assert(function);
+ g_esp = 0x5000ff00u;
+ g_ecx = object;
+ g_ebx = 0x12345678;
+ g_esi = 0x23456789;
+ g_edi = 0x3456789a;
+ g_ebp = 0x456789ab;
+ MEM32(g_esp) = RECOMP_RETADDR;
+ MEM32(g_esp + 4) = argument;
+ function();
+ assert(g_esp == 0x5000ff04u + count * 4);
+ assert(g_ebx == 0x12345678 && g_esi == 0x23456789);
+ assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
+ return g_eax;
+#endif
+}
+
+static uint32_t allocate(unsigned size)
+{
+#ifdef _WIN32
+ return ((uint32_t (__cdecl *)(unsigned))GetProcAddress(crt, "malloc"))(size);
+#else
+ return guest_malloc(size);
+#endif
+}
+
+static void release(uint32_t address)
+{
+#ifdef _WIN32
+ ((void (__cdecl *)(uint32_t))GetProcAddress(crt, "free"))(address);
+#else
+ guest_free(address);
+#endif
+}
+
+static uint32_t vtable;
+static uint32_t digest = 2166136261u;
+static unsigned snapshots;
+
+static void check(uint32_t object, const char *expected, unsigned owned)
+{
+ assert(MEM32(object) == vtable);
+ assert(MEM32(object + 8) == owned);
+ assert(MEM32(object - 4) == 0xa5a5a5a5u && MEM32(object + 12) == 0xa5a5a5a5u);
+ uint32_t text = call("?what@exception@@UBEPBDXZ", 0, object, 0, 0);
+ assert(call(NULL, MEM32(vtable + 4), object, 0, 0) == text);
+ assert(!strcmp((const char *)ADDR(text), expected));
+ digest = (digest ^ owned) * 16777619u;
+ for (unsigned i = 0; expected[i]; ++i) digest = (digest ^ (unsigned char)expected[i]) * 16777619u;
+ ++snapshots;
+}
+
+static void default_and_null_message(void)
+{
+ memset((void *)ADDR(0x50001000u), 0xa5, 64);
+ MEM32(0x50002000u) = 0;
+
+ assert(call("??0exception@@QAE@XZ", 0, 0x50001004u, 0, 0) == 0x50001004u);
+ vtable = MEM32(0x50001004u);
+ assert(vtable);
+ assert(call("??0exception@@QAE@ABQBD@Z", 0, 0x50001024u, 1, 0x50002000u) == 0x50001024u);
+
+ assert(MEM32(0x50001008u) == 0 && MEM32(0x50001028u) == 0);
+ check(0x50001004u, "Unknown exception", 0);
+ check(0x50001024u, "Unknown exception", 0);
+ call("??1exception@@UAE@XZ", 0, 0x50001004u, 0, 0);
+ call("??1exception@@UAE@XZ", 0, 0x50001024u, 0, 0);
+}
+
+static void owned_messages_copy_independently(void)
+{
+ for (unsigned length = 0; length < 256; ++length) {
+ char expected[256];
+ for (unsigned i = 0; i < length; ++i) expected[i] = '!' + (i * 17 + length) % 90;
+ expected[length] = 0;
+ memcpy((void *)ADDR(0x50003000u), expected, length + 1);
+ MEM32(0x50002000u) = 0x50003000u;
+ memset((void *)ADDR(0x50001000u), 0xa5, 64);
+
+ assert(call("??0exception@@QAE@ABQBD@Z", 0, 0x50001004u, 1, 0x50002000u) == 0x50001004u);
+ assert(call("??0exception@@QAE@ABV0@@Z", 0, 0x50001024u, 1, 0x50001004u) == 0x50001024u);
+ memset((void *)ADDR(0x50003000u), 0x7f, length);
+
+ assert(MEM32(0x50001008u) != 0x50003000u);
+ assert(MEM32(0x50001008u) != MEM32(0x50001028u));
+ check(0x50001004u, expected, 1);
+ check(0x50001024u, expected, 1);
+ call("??1exception@@UAE@XZ", 0, 0x50001004u, 0, 0);
+ check(0x50001024u, expected, 1);
+ call("??1exception@@UAE@XZ", 0, 0x50001024u, 0, 0);
+ }
+}
+
+static void borrowed_message_keeps_alias(void)
+{
+ memset((void *)ADDR(0x50001000u), 0xa5, 64);
+ call("??0exception@@QAE@XZ", 0, 0x50001004u, 0, 0);
+ memcpy((void *)ADDR(0x50003000u), "borrowed", 9);
+ MEM32(0x50001008u) = 0x50003000u;
+
+ call("??0exception@@QAE@ABV0@@Z", 0, 0x50001024u, 1, 0x50001004u);
+ MEM8(0x50003000u) = 'B';
+
+ assert(MEM32(0x50001028u) == 0x50003000u);
+ check(0x50001004u, "Borrowed", 0);
+ check(0x50001024u, "Borrowed", 0);
+ MEM32(0x50001004u) = 0x12345678u; /* A derived class installs its own vtable. */
+ call("??1exception@@UAE@XZ", 0, 0x50001004u, 0, 0);
+ assert(MEM32(0x50001004u) == vtable);
+ check(0x50001024u, "Borrowed", 0);
+ call("??1exception@@UAE@XZ", 0, 0x50001024u, 0, 0);
+ assert(!strcmp((const char *)ADDR(0x50003000u), "Borrowed"));
+}
+
+static void virtual_destruction(void)
+{
+ for (unsigned flags = 0; flags < 4; ++flags) {
+ unsigned count = flags & 2 ? 3 : 1;
+ uint32_t allocation = allocate(4 + count * 12);
+ assert(allocation);
+ uint32_t object = allocation + (flags & 2 ? 4 : 0);
+ if (flags & 2) MEM32(allocation) = count;
+ MEM32(0x50002000u) = 0x50003000u;
+ for (unsigned i = 0; i < count; ++i)
+ call("??0exception@@QAE@ABQBD@Z", 0, object + i * 12, 1, 0x50002000u);
+
+ uint32_t result = call(NULL, MEM32(vtable), object, 1, flags);
+
+ assert(result == allocation);
+ if (!(flags & 1)) {
+ for (unsigned i = 0; i < count; ++i) {
+ assert(MEM32(object + i * 12) == vtable);
+ assert(MEM32(object + i * 12 + 8) == 1);
+ }
+ release(allocation);
+ }
+ }
+}
+
+int main(void)
+{
+#ifdef _WIN32
+ crt = LoadLibraryA("msvcrt.dll");
+ assert(crt);
+ assert(VirtualAlloc((void *)0x50000000u, 0x10000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
+#else
+ assert(guest_map("exception fixture", 0x50000000u, 0x10000));
+#endif
+ default_and_null_message();
+ owned_messages_copy_independently();
+ borrowed_message_keeps_alias();
+ virtual_destruction();
+ assert(snapshots == 773 && digest == 0x421cabd1u);
+ printf("CRT exception: %u snapshots, digest %08x, message ownership, virtual calls and destruction passed\n", snapshots, digest);
+ return 0;
+}
diff --git a/docs/linux-crt-exception.md b/docs/linux-crt-exception.md
@@ -0,0 +1,70 @@
+# Native exception objects
+
+`decompiled/runtime/guest_exception.c` implements the reached MSVCRT `exception`
+object boundary in portable C. Its 12-byte guest object contains a 32-bit guest
+vtable address, a 32-bit message address and a 32-bit ownership flag. The vtable
+contains dispatch identifiers `0xf1000000` and `0xf1000004`, resolved by the Linux
+host to the deleting destructor and `what`. No host pointers or original machine
+code enter these objects.
+
+The default, message and copy constructors return the guest object and preserve
+thiscall stack cleanup. Owned messages are copied into the guest heap; borrowed
+messages retain their aliases. A null message yields `Unknown exception` from
+`what`. The destructor restores the base vtable and frees owned message storage
+without clearing the message/ownership fields. Virtual destruction supports
+scalar objects and arrays with the preceding 32-bit element count, including
+the flag controlling whether the object allocation is freed.
+
+## Binary evidence
+
+REA/Ghidra 12.1.4 inspected `C:/Windows/SysWOW64/msvcrt.dll`, SHA-256
+`23a918921c545140d189ce6dc68590bacdeb0ab2f3e3a9a2d722c5aff45ce64f`.
+The following static observations establish layout and instruction-level behavior;
+they do not themselves establish runtime equivalence:
+
+| Procedure | Address | REA evidence |
+| --- | --- | --- |
+| `??0exception@@QAE@XZ` | `0x10039ad0` | `ev_723eb2135ae40eadab76795c45ec3c65082de0887fd402d1e3810b4a33b74009` |
+| `??0exception@@QAE@ABQBD@Z` | `0x100399c0` | `ev_452d3f3d09772fd3b090cdaabea3de08c3d545a3c04ab57db741b780860ffce1` |
+| `??0exception@@QAE@ABV0@@Z` | `0x10039a60` | `ev_e150177a02c15e9509cdc06029ef57d7e667095c2d90e7b20236ff50a9dc2c41` |
+| `??1exception@@UAE@XZ` | `0x10039b30` | `ev_89dc66ed9015e32f924057d6c49074722e7a4555808464b5d204e6a0ff117339` |
+| `?what@exception@@UBEPBDXZ` | `0x10039e80` | `ev_97107b020f4aefb19f0d3d1597768444c6407e90cc6025dc7af0e3e7e58ac8b1` |
+| Virtual deleting destructor | `0x10039d00` | `ev_09ded587cfa7e135716a5506d8fde58d714d95cbb837d480e45c7bfaaf6d8268` |
+
+The two vtable slots at `0x10001004` are established by
+`ev_5ba8ddd0c8347ffe05a9149a146b290f85529bad58ce79cdb784b5451700fbf4`;
+the fallback message by
+`ev_098a74153f417dc32a523d760f840d822d887f88ed1c91acb6fe15e591ac4e28`.
+
+## Verification and limits
+
+From the repository root:
+
+```powershell
+& 'python3' tools/reconstruction/test_portable_exception.py
+wsl --cd /path/to/ao/decompiled --exec make -f Makefile.linux -j8 all test
+```
+
+The same fixture invokes actual x86 MSVCRT exports on Windows and the production
+C adapters on Linux x86-64. Both pass 773 snapshots with digest `421cabd1`:
+default/null messages, lengths 0 through 255, independent owned copies, borrowed
+aliases, canaries, virtual `what`, restoration of a derived vtable during base
+destruction, and all four scalar/array deletion flag combinations. Linux also
+checks stack balance and callee-saved registers on every call.
+
+This does not implement RTTI: the synthetic vtable's preceding locator is zero,
+and `__RTtypeid`/dynamic casts remain unresolved. Assignment, allocation-failure
+equivalence, typed catches and exception-unwind destruction are not validated
+by this fixture. Direct/virtual object destruction is tested independently of
+throwing. The existing generated throw/catch fixture remains a separate test.
+
+All twenty Linux test programs pass in
+`tools/reconstruction/work/linux-exception-build.log`. Real client startup now
+passes exception construction and reaches `ShellExecuteA` in `sub_2F009AA0`.
+The debugger observes `open`, `dxdiag.exe`, `DXDiagOutput.txt` as its operation,
+file and parameters. Its call stack is `sub_28001000` -> `sub_2F0070F0` ->
+`sub_2F009AA0`. The missing call stops explicitly; no diagnostic program is
+launched. Evidence is in `linux-startup-exception.log` and
+`linux-exception-shell-diagnostic.log` under `tools/reconstruction/work`.
+The existing client stderr log was restored after each run. There is still no
+Linux window, rendering or gameplay validation.
diff --git a/tools/reconstruction/test_portable_exception.py b/tools/reconstruction/test_portable_exception.py
@@ -0,0 +1,26 @@
+"""Compare guest exception objects with Windows x86 MSVCRT."""
+import pathlib
+import subprocess
+
+
+root = pathlib.Path(__file__).resolve().parents[2]
+decompiled = root / "decompiled"
+work = root / "tools/reconstruction/work/portable-exception"
+work.mkdir(parents=True, exist_ok=True)
+reference = work / "reference.exe"
+subprocess.run([
+ str(root / "tools/rust/llvm-mingw-20261006-ucrt-x86_64/bin/i686-w64-mingw32-clang.exe"),
+ "-O2", "-Iruntime", "tests/guest_exception.c", "-o", str(reference),
+], cwd=decompiled, check=True)
+expected = subprocess.check_output([str(reference)], text=True, timeout=30)
+linux_path = "/mnt/" + decompiled.drive[0].lower() + decompiled.as_posix()[2:]
+linux_output = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] + "/translated"
+wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"]
+subprocess.run(wsl + [
+ "gcc", "-O2", "-DRECOMP_PORTABLE", "-Iruntime", "-fno-strict-aliasing",
+ "tests/guest_exception.c", "runtime/guest_exception.c", "runtime/guest_heap.c",
+ "runtime/guest_memory.c", "runtime/recomp_state.c", "-lm", "-o", linux_output,
+], check=True)
+actual = subprocess.check_output(wsl + [linux_output], text=True, timeout=30)
+assert actual == expected, (expected, actual)
+print("Windows == Linux: " + actual.strip())