aorecomp

Anarchy Online classic in the browser

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


commit a0460eb12aadbb45573b20f9da856b6d232eb9b3
parent c839e285ee071d531cd11e5b368a8c227ed3921b
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 08:08:20 +0000

Support Linux resource construction through guest RTTI and compiled game exports

Diffstat:
Mdecompiled/Makefile.linux | 32++++++++++++++++++++++++++++++--
Mdecompiled/runtime/guest_crt.c | 17+++++++++++++++++
Mdecompiled/runtime/guest_memory.c | 6++++++
Adecompiled/runtime/guest_rtti.c | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adecompiled/runtime/guest_rtti.h | 7+++++++
Mdecompiled/runtime/linux_host.c | 40++++++++++++++++++++++++++++++++++------
Mdecompiled/runtime/linux_imports.c | 9++++++++-
Mdecompiled/runtime/linux_imports.h | 1+
Mdecompiled/runtime/linux_platform.c | 45+++++++++++++++++++++++++++++++++++++++++++++
Adecompiled/runtime/linux_snapshot.c | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adecompiled/runtime/linux_snapshot.h | 8++++++++
Mdecompiled/tests/dynamic_imports.c | 26++++++++++++++++++++++++++
Adecompiled/tests/memory_status.c | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adecompiled/tests/module_snapshot.c | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adecompiled/tests/rtti.c | 159+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/tests/stricmp.c | 65++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Adecompiled/tests/system_time.c | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_memory_status.py | 21+++++++++++++++++++++
Atools/reconstruction/test_portable_module_snapshot.py | 23+++++++++++++++++++++++
Atools/reconstruction/test_portable_rtti.py | 23+++++++++++++++++++++++
Atools/reconstruction/test_portable_system_time.py | 22++++++++++++++++++++++
21 files changed, 938 insertions(+), 10 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_imports.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.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 +RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.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_rtti.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)) @@ -128,7 +128,7 @@ build/linux/guest-stat: tests/guest_stat.c runtime/guest_stdio.c runtime/guest_c mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ -build/linux/dynamic-imports: tests/dynamic_imports.c runtime/linux_imports.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_network.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_format.c runtime/guest_exception.c runtime/guest_msvcp.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c $(wildcard runtime/*.h) +build/linux/dynamic-imports: tests/dynamic_imports.c runtime/linux_imports.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/linux_network.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_format.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_msvcp.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c $(wildcard runtime/*.h) mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -lSDL2 -lGL -o $@ @@ -165,6 +165,10 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/tls ./build/linux/threads ./build/linux/resource-atomics + ./build/linux/memory-status + ./build/linux/system-time + ./build/linux/rtti + ./build/linux/module-snapshot ./build/linux/module-filename ./build/linux/delete-file ./build/linux/guest-stdio @@ -220,3 +224,27 @@ build/linux/resource-atomics: tests/resource_atomics.c src/hhr.dll/lifted-0000.c $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ test: build/linux/resource-atomics + +build/linux/memory-status: tests/memory_status.c runtime/linux_platform.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h runtime/linux_platform.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@ + +test: build/linux/memory-status + +build/linux/system-time: tests/system_time.c runtime/linux_platform.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h runtime/linux_platform.h + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@ + +test: build/linux/system-time + +build/linux/rtti: tests/rtti.c runtime/guest_rtti.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_rtti.h runtime/guest_memory.h + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ + +test: build/linux/rtti + +build/linux/module-snapshot: tests/module_snapshot.c runtime/linux_snapshot.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/linux_snapshot.h runtime/guest_memory.h + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@ + +test: build/linux/module-snapshot diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c @@ -539,6 +539,22 @@ static void crt_stricmp(void) g_esp += 4; } +static void crt_strnicmp(void) +{ + uint32_t left = MEM32(g_esp + 4), right = MEM32(g_esp + 8); + uint32_t count = MEM32(g_esp + 12); + g_eax = 0; + /* Resolve each guest byte so a bounded comparison can cross guest pages. */ + for (uint32_t i = 0; i < count; ++i) { + unsigned a = MEM8(left + i), b = MEM8(right + i); + if (a >= 'A' && a <= 'Z') a += 'a' - 'A'; + if (b >= 'A' && b <= 'Z') b += 'a' - 'A'; + if (a != b) { g_eax = (uint32_t)((a > b) - (a < b)); break; } + if (!a) break; + } + g_esp += 4; +} + static void crt_strcmp(void) { g_eax = (uint32_t)strcmp((const char *)ADDR(MEM32(g_esp + 4)), @@ -758,6 +774,7 @@ recomp_func_t guest_crt_lookup(const char *name) {"_EH_prolog", crt_eh_prolog}, {"strlen", crt_strlen}, {"strcmp", crt_strcmp}, {"_stricmp", crt_stricmp}, + {"_strnicmp", crt_strnicmp}, {"tolower", crt_tolower}, {"strrchr", crt_strrchr}, {"_mbsrchr", crt_strrchr}, /* Initial single-byte locale. */ {"memcmp", crt_memcmp}, {"memchr", crt_memchr}, diff --git a/decompiled/runtime/guest_memory.c b/decompiled/runtime/guest_memory.c @@ -1,8 +1,13 @@ #include "guest_memory.h" #include <stdio.h> #include <stdlib.h> +#include <stdatomic.h> unsigned char *guest_pages[65536]; +static _Atomic uint64_t mapped_bytes; + +/* Kernel32 memory queries report guest mappings, not the host's 64-bit layout. */ +uint64_t guest_memory_mapped_bytes(void) { return atomic_load(&mapped_bytes); } void guest_memory_fault(uint32_t address) { @@ -31,5 +36,6 @@ uint32_t guest_map(const char *name, uint32_t base, uint32_t size) if (!memory) return 0; for (uint32_t i = 0; i < count; ++i) guest_pages[first + i] = memory + (size_t)i * 0x10000u; + atomic_fetch_add(&mapped_bytes, (uint64_t)count * 0x10000u); return size; } diff --git a/decompiled/runtime/guest_rtti.c b/decompiled/runtime/guest_rtti.c @@ -0,0 +1,76 @@ +#include "guest_rtti.h" + +static int same_type(uint32_t left, uint32_t right) +{ + return left == right || !strcmp((const char *)ADDR(left + 8), (const char *)ADDR(right + 8)); +} + +static uint32_t base_address(uint32_t object, uint32_t descriptor) +{ + uint32_t result = object + MEM32(descriptor + 8); + int32_t vbptr = MEM32(descriptor + 12); + if (vbptr != -1) + result += vbptr + MEM32(MEM32(object + vbptr) + MEM32(descriptor + 16)); + return result; +} + +static void dynamic_cast(void) +{ + uint32_t input = MEM32(g_esp + 4), vf_delta = MEM32(g_esp + 8); + uint32_t source = MEM32(g_esp + 12), target = MEM32(g_esp + 16); + uint32_t reference = MEM32(g_esp + 20); + g_eax = 0; + g_esp += 4; /* __RTDynamicCast is cdecl. */ + if (!input) return; + + /* The x86 locator precedes the vtable and contains guest absolute pointers. */ + uint32_t locator = MEM32(MEM32(input) - 4); + uint32_t object = input - MEM32(locator + 4); + if (MEM32(locator + 8)) object -= MEM32(input - MEM32(locator + 8)); + uint32_t hierarchy = MEM32(locator + 16); + uint32_t count = MEM32(hierarchy + 8), bases = MEM32(hierarchy + 12); + if (MEM32(locator) || MEM32(hierarchy)) { + fprintf(stderr, "Unsupported guest RTTI metadata version\n"); + abort(); + } + + /* Match names across DLLs. Public base trees carry preorder subtree sizes; + * a downcast must contain this particular source subobject, not just its type. */ + uint32_t downcast = 0, crosscast = 0; + int source_found = 0, down_ambiguous = 0, cross_ambiguous = 0; + for (uint32_t i = 0; i < count; ++i) { + uint32_t descriptor = MEM32(bases + i * 4); + if (MEM32(descriptor + 20) & ~2u) { + fprintf(stderr, "Non-public guest RTTI bases are not implemented (attributes %x)\n", + MEM32(descriptor + 20)); + abort(); + } + if (same_type(MEM32(descriptor), source) && base_address(object, descriptor) == input - vf_delta) + source_found = 1; + if (!same_type(MEM32(descriptor), target)) continue; + uint32_t address = base_address(object, descriptor); + if (crosscast && crosscast != address) cross_ambiguous = 1; + crosscast = address; + for (uint32_t j = i; j <= i + MEM32(descriptor + 4); ++j) { + uint32_t child = MEM32(bases + j * 4); + if (same_type(MEM32(child), source) && base_address(object, child) == input - vf_delta) { + if (downcast && downcast != address) down_ambiguous = 1; + downcast = address; + } + } + } + if (source_found) { + if (downcast && !down_ambiguous) g_eax = downcast; + else if (!downcast && !cross_ambiguous) g_eax = crosscast; + } + if (!g_eax && reference) { + fprintf(stderr, "Guest RTTI bad_cast exception is not implemented\n"); + abort(); + } +} + +recomp_func_t guest_rtti_lookup(const char *name) +{ + if (!strcmp(name, "__RTDynamicCast")) return dynamic_cast; + return NULL; +} diff --git a/decompiled/runtime/guest_rtti.h b/decompiled/runtime/guest_rtti.h @@ -0,0 +1,7 @@ +#ifndef GUEST_RTTI_H +#define GUEST_RTTI_H +#include "recomp_types.h" + +recomp_func_t guest_rtti_lookup(const char *name); + +#endif diff --git a/decompiled/runtime/linux_host.c b/decompiled/runtime/linux_host.c @@ -13,11 +13,21 @@ #include <unistd.h> #include <sys/syscall.h> #include "linux_thread.h" +#include "linux_snapshot.h" static struct { const char *name; uint32_t base, entry; int disable_thread_calls; } modules[sizeof(game_modules) / sizeof(game_modules[0]) + 2]; static const int module_count = sizeof(modules) / sizeof(modules[0]); +int linux_module_entry(unsigned index, const char **name, uint32_t *base, uint32_t *size) +{ + if (index >= module_count) return 0; + *name = modules[index].name; + *base = modules[index].base; + *size = MEM32(*base + MEM32(*base + 0x3c) + 80); + return 1; +} + /* The retained PE metadata describes guest imports/exports, never host pointers. */ static uint32_t directory(uint32_t base, unsigned index) { @@ -35,21 +45,35 @@ static uint32_t exported(int module, const char *name, uint32_t ordinal) { uint32_t base = modules[module].base; uint32_t exports = directory(base, 0); + if (exports == base) return 0; uint32_t functions = base + MEM32(exports + 28); if (name) { uint32_t names = base + MEM32(exports + 32); uint32_t ordinals = base + MEM32(exports + 36); for (uint32_t i = 0; i < MEM32(exports + 24); ++i) { - if (!strcmp(name, (const char *)ADDR(base + MEM32(names + i * 4)))) - return base + MEM32(functions + MEM16(ordinals + i * 2) * 4); + if (!strcmp(name, (const char *)ADDR(base + MEM32(names + i * 4)))) { + uint32_t rva = MEM32(functions + MEM16(ordinals + i * 2) * 4); + return rva ? base + rva : 0; + } } } else if (ordinal >= MEM32(exports + 16) && ordinal - MEM32(exports + 16) < MEM32(exports + 20)) { - return base + MEM32(functions + (ordinal - MEM32(exports + 16)) * 4); + uint32_t rva = MEM32(functions + (ordinal - MEM32(exports + 16)) * 4); + return rva ? base + rva : 0; } - fprintf(stderr, "Missing game export: %s!%s ordinal %u\n", - modules[module].name, name ? name : "", ordinal); - exit(3); + return 0; +} + +uint32_t linux_module_export(uint32_t handle, const char *name, uint32_t ordinal) +{ + for (int i = 0; i < module_count; ++i) { + if (modules[i].base != handle) continue; + uint32_t address = exported(i, name, ordinal); + if (!address) MEM32(g_fs_base + 0x34) = 127; /* ERROR_PROC_NOT_FOUND */ + return address; + } + MEM32(g_fs_base + 0x34) = 126; /* ERROR_MOD_NOT_FOUND */ + return 0; } static void bind_modules(void) @@ -72,6 +96,10 @@ static void bind_modules(void) MEM32(slots) = linux_import_bind(dll, name, ordinal); else MEM32(slots) = module >= 0 ? exported(module, name, ordinal) : linux_import_bind(dll, name, ordinal); + if (!MEM32(slots)) { + fprintf(stderr, "Missing game export: %s!%s ordinal %u\n", dll, name ? name : "", ordinal); + exit(3); + } } } } diff --git a/decompiled/runtime/linux_imports.c b/decompiled/runtime/linux_imports.c @@ -3,12 +3,14 @@ #include "guest_stdio.h" #include "guest_format.h" #include "guest_exception.h" +#include "guest_rtti.h" #include "guest_msvcp.h" #include "linux_platform.h" #include "linux_sync.h" #include "linux_file.h" #include "linux_tls.h" #include "linux_thread.h" +#include "linux_snapshot.h" #include <pthread.h> #include <stdatomic.h> #include "linux_network.h" @@ -103,7 +105,8 @@ static void get_proc_address(void) break; } if (!g_eax) MEM32(g_fs_base + 0x34) = 127; /* ERROR_PROC_NOT_FOUND */ - } else MEM32(g_fs_base + 0x34) = 126; + } else g_eax = linux_module_export(module, name > 0xffff ? (const char *)ADDR(name) : NULL, + name <= 0xffff ? name : 0); g_esp += 12; } @@ -113,6 +116,8 @@ static void close_handle(void) linux_file_lookup("CloseHandle")(); else if ((MEM32(g_esp + 4) & 0xff000000u) == 0x06000000u) linux_thread_lookup("CloseHandle")(); + else if ((MEM32(g_esp + 4) & 0xff000000u) == 0x07000000u) + linux_snapshot_lookup("CloseHandle")(); else linux_sync_lookup("CloseHandle")(); } @@ -162,6 +167,7 @@ recomp_func_t recomp_lookup_import(uint32_t va) if (!function) function = linux_sync_lookup(imports[current_import].name); if (!function) function = linux_tls_lookup(imports[current_import].name); if (!function) function = linux_thread_lookup(imports[current_import].name); + if (!function) function = linux_snapshot_lookup(imports[current_import].name); if (function) return function; } } @@ -175,6 +181,7 @@ recomp_func_t recomp_lookup_import(uint32_t va) 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) function = guest_rtti_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/runtime/linux_imports.h b/decompiled/runtime/linux_imports.h @@ -5,5 +5,6 @@ uint32_t linux_import_bind(const char *dll, const char *name, uint32_t ordinal); unsigned linux_import_count(void); recomp_func_t linux_module_lookup(const char *name); +uint32_t linux_module_export(uint32_t handle, const char *name, uint32_t ordinal); #endif diff --git a/decompiled/runtime/linux_platform.c b/decompiled/runtime/linux_platform.c @@ -6,9 +6,12 @@ #include <sched.h> #include <strings.h> #include <sys/stat.h> +#include <sys/sysinfo.h> #include <time.h> #include <unistd.h> +uint64_t guest_memory_mapped_bytes(void); + /* Host mutex objects are keyed by guest addresses, never stored in guest memory. */ struct critical_section { uint32_t address; @@ -134,6 +137,21 @@ static void tick_count(void) g_esp += 4; } +static void system_time_as_file_time(void) +{ + struct timespec now; + if (clock_gettime(CLOCK_REALTIME, &now)) { perror("clock_gettime"); abort(); } + /* FILETIME counts 100 ns intervals from 1601-01-01 UTC. */ + MEM64(MEM32(g_esp + 4)) = ((uint64_t)now.tv_sec + 11644473600ull) * 10000000ull + now.tv_nsec / 100; + g_esp += 8; +} + +static void current_thread_handle(void) +{ + g_eax = 0xfffffffeu; + g_esp += 4; +} + static void sleep_milliseconds(void) { uint32_t milliseconds = MEM32(g_esp + 4); @@ -314,6 +332,30 @@ static void set_handle_count(void) g_esp += 8; } +static void global_memory_status(void) +{ + struct sysinfo info; + if (sysinfo(&info)) { perror("sysinfo"); abort(); } + uint64_t total = (uint64_t)info.totalram * info.mem_unit; + uint64_t available = ((uint64_t)info.freeram + info.bufferram) * info.mem_unit; + uint64_t page_total = total + (uint64_t)info.totalswap * info.mem_unit; + uint64_t page_available = available + (uint64_t)info.freeswap * info.mem_unit; + uint64_t mapped = guest_memory_mapped_bytes(); + uint32_t output = MEM32(g_esp + 4); + + /* MEMORYSTATUS is eight DWORDs for the non-LAA x86 guest. Physical values + * retain its signed 2 GiB ceiling; page-file values saturate at UINT32_MAX. */ + MEM32(output) = 32; + MEM32(output + 4) = (total - available) * 100 / total; + MEM32(output + 8) = total > INT32_MAX ? INT32_MAX : total; + MEM32(output + 12) = available > INT32_MAX ? INT32_MAX : available; + MEM32(output + 16) = page_total > UINT32_MAX ? UINT32_MAX : page_total; + MEM32(output + 20) = page_available > UINT32_MAX ? UINT32_MAX : page_available; + MEM32(output + 24) = 0x7ffe0000; + MEM32(output + 28) = mapped >= 0x7ffe0000 ? 0 : 0x7ffe0000 - mapped; + g_esp += 8; +} + recomp_func_t linux_platform_lookup(const char *dll, const char *name) { if (!strcasecmp(dll, "winmm.dll") && !strcmp(name, "timeGetTime")) return tick_count; @@ -322,6 +364,8 @@ recomp_func_t linux_platform_lookup(const char *dll, const char *name) {"QueryPerformanceFrequency", performance_frequency}, {"QueryPerformanceCounter", performance_counter}, {"GetTickCount", tick_count}, {"Sleep", sleep_milliseconds}, + {"GetSystemTimeAsFileTime", system_time_as_file_time}, + {"GetCurrentThread", current_thread_handle}, {"GetLastError", get_last_error}, {"SetLastError", set_last_error}, {"GetCurrentThreadId", current_thread_id}, {"GetCurrentProcessId", current_process_id}, @@ -332,6 +376,7 @@ recomp_func_t linux_platform_lookup(const char *dll, const char *name) {"DeleteFileA", delete_file}, {"CreateDirectoryA", create_directory}, {"SetHandleCount", set_handle_count}, + {"GlobalMemoryStatus", global_memory_status}, {"InterlockedExchange", interlocked_exchange}, {"InterlockedIncrement", interlocked_increment}, {"InterlockedDecrement", interlocked_decrement}, diff --git a/decompiled/runtime/linux_snapshot.c b/decompiled/runtime/linux_snapshot.c @@ -0,0 +1,99 @@ +#include "linux_snapshot.h" +#include <pthread.h> +#include <unistd.h> + +/* All guest modules are compiled into one ELF and registered before startup. + * Their immutable table is the snapshot; only each handle's cursor can change. */ +static struct snapshot { uint32_t handle; unsigned next; struct snapshot *link; } *snapshots; +static uint32_t next_handle = 0x07000000; +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + +static void create_snapshot(void) +{ + uint32_t flags = MEM32(g_esp + 4), process = MEM32(g_esp + 8); + if (flags != 8 || (process && process != (uint32_t)getpid())) { + fprintf(stderr, "Only current-process module snapshots are implemented: flags=%x pid=%u\n", flags, process); + abort(); + } + struct snapshot *snapshot = calloc(1, sizeof(*snapshot)); + g_eax = UINT32_MAX; + if (snapshot) { + if (pthread_mutex_lock(&mutex)) abort(); + if (next_handle >= 0x08000000) abort(); + snapshot->handle = next_handle; + next_handle += 4; + snapshot->link = snapshots; + snapshots = snapshot; + g_eax = snapshot->handle; + if (pthread_mutex_unlock(&mutex)) abort(); + } else MEM32(g_fs_base + 0x34) = 8; + g_esp += 12; +} + +static struct snapshot *find_snapshot_locked(uint32_t handle) +{ + for (struct snapshot *snapshot = snapshots; snapshot; snapshot = snapshot->link) + if (snapshot->handle == handle) return snapshot; + MEM32(g_fs_base + 0x34) = 6; + return NULL; +} + +static void module_entry(int first) +{ + uint32_t output = MEM32(g_esp + 8); + if (pthread_mutex_lock(&mutex)) abort(); + struct snapshot *snapshot = find_snapshot_locked(MEM32(g_esp + 4)); + g_eax = 0; + if (snapshot) { + if (MEM32(output) < 548) MEM32(g_fs_base + 0x34) = 87; /* ERROR_INVALID_PARAMETER */ + else { + const char *name; + uint32_t base, size; + unsigned index = first ? 0 : snapshot->next; + if (linux_module_entry(index, &name, &base, &size)) { + /* MODULEENTRY32 retains the guest's x86 field and pointer widths. */ + MEM32(output) = 548; + MEM32(output + 4) = 1; + MEM32(output + 8) = getpid(); + MEM32(output + 12) = MEM32(output + 16) = 0xffff; + MEM32(output + 20) = MEM32(output + 28) = base; + MEM32(output + 24) = size; + strcpy((char *)ADDR(output + 32), name); + ssize_t length = readlink("/proc/self/exe", (char *)ADDR(output + 288), 259); + if (length < 0) { perror("module executable path"); abort(); } + MEM8(output + 288 + length) = 0; + snapshot->next = index + 1; + g_eax = 1; + } else MEM32(g_fs_base + 0x34) = 18; /* ERROR_NO_MORE_FILES */ + } + } + if (pthread_mutex_unlock(&mutex)) abort(); + g_esp += 12; +} + +static void module_first(void) { module_entry(1); } +static void module_next(void) { module_entry(0); } + +static void close_snapshot(void) +{ + if (pthread_mutex_lock(&mutex)) abort(); + struct snapshot *snapshot = find_snapshot_locked(MEM32(g_esp + 4)); + g_eax = snapshot != NULL; + if (snapshot) { + struct snapshot **link = &snapshots; + while (*link != snapshot) link = &(*link)->link; + *link = snapshot->link; + free(snapshot); + } + if (pthread_mutex_unlock(&mutex)) abort(); + g_esp += 8; +} + +recomp_func_t linux_snapshot_lookup(const char *name) +{ + if (!strcmp(name, "CreateToolhelp32Snapshot")) return create_snapshot; + if (!strcmp(name, "Module32First")) return module_first; + if (!strcmp(name, "Module32Next")) return module_next; + if (!strcmp(name, "CloseHandle")) return close_snapshot; + return NULL; +} diff --git a/decompiled/runtime/linux_snapshot.h b/decompiled/runtime/linux_snapshot.h @@ -0,0 +1,8 @@ +#ifndef LINUX_SNAPSHOT_H +#define LINUX_SNAPSHOT_H +#include "recomp_types.h" + +recomp_func_t linux_snapshot_lookup(const char *name); +int linux_module_entry(unsigned index, const char **name, uint32_t *base, uint32_t *size); + +#endif diff --git a/decompiled/tests/dynamic_imports.c b/decompiled/tests/dynamic_imports.c @@ -11,6 +11,15 @@ static void thread_entry(void) } recomp_func_t recomp_lookup(uint32_t address) { return address == 0x11001000 ? thread_entry : NULL; } recomp_func_t linux_module_lookup(const char *name) { (void)name; return NULL; } +int linux_module_entry(unsigned index, const char **name, uint32_t *base, uint32_t *size) { return 0; } +uint32_t linux_module_export(uint32_t handle, const char *name, uint32_t ordinal) +{ + if (handle == 0x11000000) { + if (name ? !strcmp(name, "ThreadEntry") : ordinal == 1) return 0x11001000; + MEM32(g_fs_base + 0x34) = 127; + } else MEM32(g_fs_base + 0x34) = 126; + return 0; +} void linux_thread_notify(uint32_t reason) { (void)reason; } void recomp_eh_thread_exit(void) {} void native32_shim_CxxThrowException(void) { abort(); } @@ -176,6 +185,22 @@ static void thread_handles_dispatch_through_imports(void) assert(call("WaitForSingleObject", thread, 0) == UINT32_MAX && MEM32(g_fs_base + 0x34) == 6); } +static void game_exports_return_callable_guest_addresses(void) +{ + strcpy((char *)ADDR(0x50002000), "ThreadEntry"); + MEM32(g_fs_base + 0x34) = 777; + + uint32_t address = call("GetProcAddress", 0x11000000, 0x50002000); + + assert(address == 0x11001000 && MEM32(g_fs_base + 0x34) == 777); + assert(call("GetProcAddress", 0x11000000, 1) == address); + assert(call("GetProcAddress", 0x11000000, 2) == 0 && MEM32(g_fs_base + 0x34) == 127); + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = 41; + recomp_lookup(address)(); + assert(g_eax == 42 && g_esp == 0x5000ff08); +} + int main(void) { assert(guest_map("dynamic imports test", 0x50000000u, 0x10000u)); @@ -186,6 +211,7 @@ int main(void) unresolved_window_call_fails_explicitly(); file_and_event_handles_close_through_imports(); thread_handles_dispatch_through_imports(); + game_exports_return_callable_guest_addresses(); puts("dynamic imports: 14 window symbols, stable guest IDs, lifetime, errors, dispatch and ABI passed"); return 0; } diff --git a/decompiled/tests/memory_status.c b/decompiled/tests/memory_status.c @@ -0,0 +1,72 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#else +#include "linux_platform.h" +#endif + +static uint32_t query(void) +{ + uint32_t values[10]; + memset(values, 0xa5, sizeof(values)); +#ifdef _WIN32 + SetLastError(777); +#else + memcpy((void *)ADDR(0x50001000), values, sizeof(values)); + g_esp = 0x5000ff00; + g_fs_base = 0x50003000; + MEM32(g_esp + 4) = 0x50001004; + MEM32(g_fs_base + 0x34) = 777; + g_ebx = 0x12345678; g_esi = 0x23456789; g_edi = 0x3456789a; g_ebp = 0x456789ab; +#endif + +#ifdef _WIN32 + GlobalMemoryStatus((MEMORYSTATUS *)(values + 1)); + assert(GetLastError() == 777); +#else + linux_platform_lookup("kernel32.dll", "GlobalMemoryStatus")(); + memcpy(values, (void *)ADDR(0x50001000), sizeof(values)); + assert(g_esp == 0x5000ff08 && MEM32(g_fs_base + 0x34) == 777); + assert(g_ebx == 0x12345678 && g_esi == 0x23456789 && g_edi == 0x3456789a && g_ebp == 0x456789ab); +#endif + + assert(values[0] == 0xa5a5a5a5 && values[9] == 0xa5a5a5a5); + assert(values[1] == 32 && values[2] <= 100); + assert(values[3] > 0 && values[3] <= INT32_MAX && values[4] <= values[3]); + assert(values[5] > 0 && values[6] <= values[5]); + assert(values[7] == 0x7ffe0000 && values[8] <= values[7]); + return values[8]; +} + +static void virtual_availability_accounts_for_allocations(void) +{ + uint32_t before = query(); + assert(before >= 0x100000); + +#ifdef _WIN32 + void *memory = VirtualAlloc(NULL, 0x100000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + assert(memory); +#else + assert(guest_map("memory status allocation", 0x53000000, 0x100000)); +#endif + uint32_t after = query(); + + assert(before - after == 0x100000); +#ifdef _WIN32 + assert(VirtualFree(memory, 0, MEM_RELEASE)); +#endif +} + +int main(void) +{ +#ifndef _WIN32 + assert(guest_map("memory status fixture", 0x50000000, 0x10000)); +#endif + virtual_availability_accounts_for_allocations(); + puts("Memory status: x86 layout, bounds, last error, guest ABI and virtual allocation accounting passed"); + return 0; +} diff --git a/decompiled/tests/module_snapshot.c b/decompiled/tests/module_snapshot.c @@ -0,0 +1,121 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#include <tlhelp32.h> +#else +#include "linux_snapshot.h" +#include <unistd.h> + +int linux_module_entry(unsigned index, const char **name, uint32_t *base, uint32_t *size) +{ + if (index >= 2) return 0; + *name = index ? "ar.dll" : "Clientr.exe"; + *base = index ? 0x24000000 : 0x28000000; + *size = index ? 0x10000 : 0x20000; + return 1; +} + +static uint32_t call(const char *name, uint32_t first, uint32_t second) +{ + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = first; + MEM32(g_esp + 8) = second; + g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; + linux_snapshot_lookup(name)(); + assert(g_esp == 0x5000ff00 + (!strcmp(name, "CloseHandle") ? 8 : 12)); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); + return g_eax; +} +#endif + +static uint32_t create(void) +{ +#ifdef _WIN32 + return (uintptr_t)CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 0); +#else + return call("CreateToolhelp32Snapshot", 8, 0); +#endif +} + +static uint32_t query(uint32_t snapshot, uint32_t *values, int first) +{ +#ifdef _WIN32 + return first ? Module32First((HANDLE)(uintptr_t)snapshot, (MODULEENTRY32 *)values) : + Module32Next((HANDLE)(uintptr_t)snapshot, (MODULEENTRY32 *)values); +#else + memcpy((void *)ADDR(0x50001000), values, 552); + uint32_t result = call(first ? "Module32First" : "Module32Next", snapshot, 0x50001000); + memcpy(values, (void *)ADDR(0x50001000), 552); + return result; +#endif +} + +static uint32_t error(void) +{ +#ifdef _WIN32 + return GetLastError(); +#else + return MEM32(g_fs_base + 0x34); +#endif +} + +static uint32_t close_snapshot(uint32_t snapshot) +{ +#ifdef _WIN32 + return CloseHandle((HANDLE)(uintptr_t)snapshot); +#else + return call("CloseHandle", snapshot, 0); +#endif +} + +static void enumeration_preserves_layout_and_independent_cursors(void) +{ + uint32_t first = create(), second = create(), values[138]; + assert(first != UINT32_MAX && second != UINT32_MAX && first != second); + memset(values, 0xa5, sizeof(values)); + values[0] = 548; + + assert(query(first, values, 1)); + uint32_t first_base = values[5]; + assert(query(first, values, 0)); + assert(values[5] != first_base); + assert(query(second, values, 1)); + + assert(values[0] == 548 && values[1] == 1); +#ifdef _WIN32 + assert(values[2] == GetCurrentProcessId()); +#else + assert(values[2] == (uint32_t)getpid()); + assert(values[5] == 0x28000000 && values[6] == 0x20000); + assert(!strcmp((char *)(values + 8), "Clientr.exe")); +#endif + assert(values[5] == first_base && values[5] == values[7] && values[6]); + assert(((char *)values)[32] && ((char *)values)[288]); + assert(values[137] == 0xa5a5a5a5); + while (query(second, values, 0)) {} + assert(error() == 18); + assert(query(second, values, 1) && values[5] == first_base); + values[0] = 0; + assert(!query(first, values, 1) && error() == 87); + values[0] = 548; + assert(close_snapshot(first)); + assert(!query(first, values, 1) && error() == 6); + assert(!close_snapshot(first) && error() == 6); + assert(query(second, values, 1)); + assert(close_snapshot(second)); +} + +int main(void) +{ +#ifndef _WIN32 + assert(guest_map("snapshot fixture", 0x50000000, 0x10000)); + g_fs_base = 0x50003000; +#endif + enumeration_preserves_layout_and_independent_cursors(); + puts("Module snapshots: x86 layout, independent cursors, restart, end/errors, handle lifetime and ABI passed"); + return 0; +} diff --git a/decompiled/tests/rtti.c b/decompiled/tests/rtti.c @@ -0,0 +1,159 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#define MEM32(a) (*(uint32_t *)(uintptr_t)(a)) +#define ADDR(a) ((void *)(uintptr_t)(a)) +static void *(__cdecl *native_cast)(void *, int, void *, void *, int); +#else +#include "guest_rtti.h" +#endif + +static void type(unsigned index, const char *name) +{ + strcpy((char *)ADDR(0x50001008 + index * 64), name); +} + +static void base(unsigned index, unsigned type_index, unsigned children, int offset, int vbptr, int vbslot) +{ + uint32_t descriptor = 0x50002000 + index * 32; + MEM32(0x50003000 + index * 4) = descriptor; + MEM32(descriptor) = 0x50001000 + type_index * 64; + MEM32(descriptor + 4) = children; + MEM32(descriptor + 8) = offset; + MEM32(descriptor + 12) = vbptr; + MEM32(descriptor + 16) = vbslot; + MEM32(descriptor + 20) = 0; +} + +static void layout(unsigned count, unsigned attributes, unsigned source_offset) +{ + memset((void *)ADDR(0x50002000), 0, 0x6000); + MEM32(0x50004004) = attributes; + MEM32(0x50004008) = count; + MEM32(0x5000400c) = 0x50003000; + MEM32(0x50005004) = source_offset; + MEM32(0x5000500c) = 0x50001000; + MEM32(0x50005010) = 0x50004000; + MEM32(0x50006000) = 0x50005000; + MEM32(0x50007000 + source_offset) = 0x50006004; +} + +static uint32_t cast(uint32_t input, int delta, unsigned source, unsigned target, int reference) +{ +#ifdef _WIN32 + return (uintptr_t)native_cast((void *)(uintptr_t)input, delta, + (void *)(uintptr_t)(0x50001000 + source * 64), + (void *)(uintptr_t)(0x50001000 + target * 64), reference); +#else + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = input; + MEM32(g_esp + 8) = delta; + MEM32(g_esp + 12) = 0x50001000 + source * 64; + MEM32(g_esp + 16) = 0x50001000 + target * 64; + MEM32(g_esp + 20) = reference; + g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; + guest_rtti_lookup("__RTDynamicCast")(); + assert(g_esp == 0x5000ff04); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); + return g_eax; +#endif +} + +static void multiple_inheritance_adjusts_secondary_base(void) +{ + layout(4, 1, 24); + base(0, 0, 3, 0, -1, 0); + base(1, 1, 2, 0, -1, 0); + base(2, 2, 0, 0, -1, 0); + base(3, 3, 0, 24, -1, 0); + + uint32_t result = cast(0x50007018, 0, 3, 1, 0); + + assert(result == 0x50007000); + assert(cast(0x50007018, 0, 3, 0, 1) == 0x50007000); + assert(cast(0x50007018, 0, 3, 2, 0) == 0x50007000); + assert(cast(0x50007018, 0, 3, 3, 0) == 0x50007018); + assert(cast(0x50007018, 0, 3, 4, 0) == 0); + assert(cast(0, 0, 3, 1, 0) == 0); + assert(cast(0x50007018, 0, 5, 6, 0) == 0x50007000); +} + +static void repeated_bases_select_the_source_branch(void) +{ + layout(5, 5, 32); + base(0, 0, 4, 0, -1, 0); + base(1, 1, 1, 0, -1, 0); + base(2, 2, 0, 0, -1, 0); + base(3, 1, 1, 32, -1, 0); + base(4, 2, 0, 32, -1, 0); + for (unsigned i = 1; i < 5; ++i) MEM32(0x50002000 + i * 32 + 20) = 2; + + uint32_t result = cast(0x50007020, 0, 2, 1, 0); + + assert(result == 0x50007020); + assert(cast(0x50007020, 0, 2, 0, 0) == 0x50007000); + MEM32(0x50007000) = 0x50006004; + MEM32(0x50005004) = 0; + assert(cast(0x50007000, 0, 0, 1, 0) == 0); +} + +static void virtual_base_displacement_locates_the_shared_base(void) +{ + layout(3, 3, 32); + base(0, 0, 2, 0, -1, 0); + base(1, 1, 1, 0, -1, 0); + base(2, 2, 0, 0, 8, 4); + MEM32(0x50007008) = 0x50008000; + MEM32(0x50008004) = 24; + + uint32_t result = cast(0x50007020, 0, 2, 1, 0); + + assert(result == 0x50007000); + assert(cast(0x50007020, 0, 2, 2, 0) == 0x50007020); +} + +static void vfptr_and_construction_displacements_adjust_the_complete_object(void) +{ + layout(2, 1, 32); + base(0, 0, 1, 0, -1, 0); + base(1, 1, 0, 24, -1, 0); + + uint32_t result = cast(0x50007020, 8, 1, 0, 0); + + assert(result == 0x50007000); + layout(2, 1, 40); + base(0, 0, 1, 0, -1, 0); + base(1, 1, 0, 40, -1, 0); + MEM32(0x50005004) = 32; + MEM32(0x50005008) = 4; + MEM32(0x50007024) = 8; + assert(cast(0x50007028, 0, 1, 0, 0) == 0x50007000); +} + +int main(void) +{ +#ifdef _WIN32 + assert(VirtualAlloc((void *)0x50000000, 0x10000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == (void *)0x50000000); + native_cast = (void *)GetProcAddress(LoadLibraryA("msvcrt.dll"), "__RTDynamicCast"); + assert(native_cast); +#else + assert(guest_map("RTTI fixture", 0x50000000, 0x10000)); +#endif + type(0, ".?AVRDBCATMesh_t@@"); + type(1, ".?AVRDBObject_t@@"); + type(2, ".?AVDbObject_t@@"); + type(3, ".?AVResourceUser_t@@"); + type(4, ".?AVUnrelated@@"); + type(5, ".?AVResourceUser_t@@"); + type(6, ".?AVRDBObject_t@@"); + multiple_inheritance_adjusts_secondary_base(); + repeated_bases_select_the_source_branch(); + virtual_base_displacement_locates_the_shared_base(); + vfptr_and_construction_displacements_adjust_the_complete_object(); + puts("RTTI: public multiple/virtual inheritance, repeated bases, cross-DLL names, null/failure and ABI passed"); + return 0; +} diff --git a/decompiled/tests/stricmp.c b/decompiled/tests/stricmp.c @@ -6,6 +6,7 @@ #include <windows.h> static int (__cdecl *compare)(const char *, const char *); static int (__cdecl *lowercase)(int); +static int (__cdecl *bounded_compare)(const char *, const char *, size_t); #else #include "guest_crt.h" #include "guest_memory.h" @@ -58,12 +59,73 @@ static void lowercase_bytes_and_eof(void) printf("tolower: 256 C-locale bytes and EOF, digest %08x, cdecl ABI passed\n", digest); } +static int bounded_call(const char *left, const char *right, unsigned count) +{ +#ifdef _WIN32 + return bounded_compare(left, right, count); +#else + memcpy((void *)ADDR(0x50000100), left, 4); + memcpy((void *)ADDR(0x50000200), right, 4); + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = 0x50000100; + MEM32(g_esp + 8) = 0x50000200; + MEM32(g_esp + 12) = count; + g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; + guest_crt_lookup("_strnicmp")(); + assert(g_esp == 0x5000ff04 && MEM32(g_esp + 8) == count); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); + return (int32_t)g_eax; +#endif +} + +static void bounded_comparison_stops_at_count_or_nul(void) +{ + uint32_t digest = 2166136261u; + + for (unsigned count = 0; count <= 4; ++count) + for (unsigned a = 0; a < 256; ++a) + for (unsigned b = 0; b < 256; ++b) { + char left[] = {'A', (char)a, 'C', 0}; + char right[] = {'a', (char)b, 'b', 0}; + digest = (digest ^ (uint32_t)bounded_call(left, right, count)) * 16777619u; + } + + assert(bounded_call("ABC", "abd", 2) == 0); + assert(bounded_call("ABC", "abd", 3) < 0); + assert(bounded_call("a\0Z", "A\0b", 4) == 0); + assert(digest == 0x82b580e9u); +#ifndef _WIN32 + /* The byte after these unterminated inputs is unmapped. */ + MEM8(0x5000ffff) = 'A'; + MEM8(0x50000100) = 'a'; + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = 0x5000ffff; + MEM32(g_esp + 8) = 0x50000100; + MEM32(g_esp + 12) = 1; + guest_crt_lookup("_strnicmp")(); + assert(g_eax == 0); + assert(guest_map("strnicmp second page", 0x50010000, 0x10000)); + MEM8(0x50010000) = 'B'; + MEM8(0x50000101) = 'b'; + g_esp = 0x5000ff00; + MEM32(g_esp + 12) = 2; + guest_crt_lookup("_strnicmp")(); + assert(g_eax == 0); + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = MEM32(g_esp + 8) = MEM32(g_esp + 12) = 0; + guest_crt_lookup("_strnicmp")(); + assert(g_eax == 0); +#endif + printf("strnicmp: 327680 bounded byte pairs, digest %08x, prefixes, NUL and ABI passed\n", digest); +} + int main(void) { #ifdef _WIN32 compare = (void *)GetProcAddress(LoadLibraryA("msvcrt.dll"), "_stricmp"); lowercase = (void *)GetProcAddress(GetModuleHandleA("msvcrt.dll"), "tolower"); - assert(compare && lowercase); + bounded_compare = (void *)GetProcAddress(GetModuleHandleA("msvcrt.dll"), "_strnicmp"); + assert(compare && lowercase && bounded_compare); #else assert(guest_map("stricmp test", 0x50000000u, 0x10000u)); #endif @@ -82,5 +144,6 @@ int main(void) assert(digest == 0x381d58d1u); printf("stricmp: 65536 byte pairs, digest %08x, C-locale prefixes and ABI passed\n", digest); lowercase_bytes_and_eof(); + bounded_comparison_stops_at_count_or_nul(); return 0; } diff --git a/decompiled/tests/system_time.c b/decompiled/tests/system_time.c @@ -0,0 +1,76 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <time.h> +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#else +#include "linux_platform.h" +#endif + +static void file_time_uses_utc_epoch_and_guest_layout(void) +{ + uint32_t values[4]; + memset(values, 0xa5, sizeof(values)); + time_t before = time(NULL); +#ifdef _WIN32 + SetLastError(777); +#else + memcpy((void *)ADDR(0x50001000), values, sizeof(values)); + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = 0x50001004; + MEM32(g_fs_base + 0x34) = 777; + g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; +#endif + +#ifdef _WIN32 + GetSystemTimeAsFileTime((FILETIME *)(values + 1)); + assert(GetLastError() == 777); +#else + linux_platform_lookup("kernel32.dll", "GetSystemTimeAsFileTime")(); + memcpy(values, (void *)ADDR(0x50001000), sizeof(values)); + assert(g_esp == 0x5000ff08 && MEM32(g_fs_base + 0x34) == 777); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); +#endif + time_t after = time(NULL); + + uint64_t stamp = ((uint64_t)values[2] << 32) | values[1]; + assert(values[0] == 0xa5a5a5a5 && values[3] == 0xa5a5a5a5); + assert(stamp >= ((uint64_t)before + 11644473600ull) * 10000000ull); + assert(stamp < ((uint64_t)after + 11644473601ull) * 10000000ull); +} + +static void current_thread_returns_pseudo_handle(void) +{ +#ifdef _WIN32 + SetLastError(777); +#else + g_esp = 0x5000ff00; + MEM32(g_fs_base + 0x34) = 777; +#endif + +#ifdef _WIN32 + uint32_t handle = (uintptr_t)GetCurrentThread(); + assert(GetLastError() == 777); +#else + linux_platform_lookup("kernel32.dll", "GetCurrentThread")(); + uint32_t handle = g_eax; + assert(g_esp == 0x5000ff04 && MEM32(g_fs_base + 0x34) == 777); +#endif + + assert(handle == 0xfffffffeu); +} + +int main(void) +{ +#ifndef _WIN32 + assert(guest_map("system time fixture", 0x50000000, 0x10000)); + g_fs_base = 0x50003000; +#endif + file_time_uses_utc_epoch_and_guest_layout(); + current_thread_returns_pseudo_handle(); + puts("System time and thread identity: UTC epoch, layout, last error and guest ABI passed"); + return 0; +} diff --git a/tools/reconstruction/test_portable_memory_status.py b/tools/reconstruction/test_portable_memory_status.py @@ -0,0 +1,21 @@ +"""Check the x86 memory-status contract on Windows and native Linux.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-memory-status" +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", "tests/memory_status.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:] +wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] +subprocess.run(wsl + ["make", "-s", "-f", "Makefile.linux", "build/linux/memory-status"], check=True) +actual = subprocess.check_output(wsl + ["./build/linux/memory-status"], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux contract: " + actual.strip()) diff --git a/tools/reconstruction/test_portable_module_snapshot.py b/tools/reconstruction/test_portable_module_snapshot.py @@ -0,0 +1,23 @@ +"""Check the module snapshot contract on Windows and native Linux.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-module-snapshot" +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", "tests/module_snapshot.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:] +wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] +subprocess.run(wsl + ["make", "-s", "-f", "Makefile.linux", "build/linux/module-snapshot"], check=True) +actual = subprocess.check_output(wsl + ["./build/linux/module-snapshot"], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux contract: " + actual.strip()) + + diff --git a/tools/reconstruction/test_portable_rtti.py b/tools/reconstruction/test_portable_rtti.py @@ -0,0 +1,23 @@ +"""Check the guest RTTI contract on Windows and native Linux.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-rtti" +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", "tests/rtti.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:] +wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] +subprocess.run(wsl + ["make", "-s", "-f", "Makefile.linux", "build/linux/rtti"], check=True) +actual = subprocess.check_output(wsl + ["./build/linux/rtti"], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux contract: " + actual.strip()) + + diff --git a/tools/reconstruction/test_portable_system_time.py b/tools/reconstruction/test_portable_system_time.py @@ -0,0 +1,22 @@ +"""Check the system-time and thread-identity contracts on Windows and native Linux.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-system-time" +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", "tests/system_time.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:] +wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] +subprocess.run(wsl + ["make", "-s", "-f", "Makefile.linux", "build/linux/system-time"], check=True) +actual = subprocess.check_output(wsl + ["./build/linux/system-time"], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux contract: " + actual.strip()) +