aorecomp

Anarchy Online classic in the browser

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


commit 0427b57b5199a3dbe866554b90225644aa4d598c
parent 58810e7162a77088c04641ef29941a5092ef2241
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 00:41:10 +0000

Implement guest CRT allocation-failure callbacks and retries

Diffstat:
Mdecompiled/Makefile.linux | 7++++++-
Mdecompiled/runtime/guest_crt.c | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Mdecompiled/tests/guest_crt.c | 1+
Adecompiled/tests/new_handler.c | 173+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_new_handler.py | 26++++++++++++++++++++++++++
5 files changed, 268 insertions(+), 9 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -92,6 +92,10 @@ 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/new-handler: tests/new_handler.c runtime/guest_crt.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_crt.h runtime/guest_heap.h runtime/guest_memory.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ + build/linux/platform-diagnostics: tests/platform_diagnostics.c runtime/linux_diagnostics.c runtime/linux_platform.c runtime/guest_crt.c runtime/guest_msvcp.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c src/ffr.dll/lifted-0002.c data/ffr_dll.c runtime/recomp_types.h runtime/platform_diagnostics.h runtime/linux_platform.h runtime/guest_crt.h runtime/guest_msvcp.h runtime/guest_memory.h runtime/guest_heap.h mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ @@ -104,7 +108,7 @@ build/linux/guest-stdio: tests/guest_stdio.c runtime/guest_stdio.c runtime/guest 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 build/linux/guest-exception build/linux/platform-diagnostics +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/platform-diagnostics build/linux/new-handler ./build/linux/linecrypt ./build/linux/guest-heap ./build/linux/guest-crt @@ -126,5 +130,6 @@ test: build/linux/linecrypt build/linux/guest-heap build/linux/guest-crt build/l ./build/linux/guest-stdio ./build/linux/guest-exception ./build/linux/platform-diagnostics + ./build/linux/new-handler -include $(OBJECTS:.o=.d) diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c @@ -9,6 +9,8 @@ static uint32_t *exit_callbacks; static size_t exit_callback_count; static int error_mode; static uint32_t abort_handler; +static uint32_t new_handler; +static int new_mode; uint32_t guest_crt_errno_address(void) { @@ -252,10 +254,58 @@ static void crt_initterm(void) g_esp += 4; } +static int call_new_handler(uint32_t size) +{ + if (!new_handler) return 0; + uint32_t caller = g_cur_func, stack = g_esp; + recomp_func_t function = recomp_lookup(new_handler); + if (!function) function = recomp_lookup_import(new_handler); + PUSH32(g_esp, size); + PUSH32(g_esp, RECOMP_RETADDR); + function(); + if (g_esp != stack - 4) { + fprintf(stderr, "Guest new handler returned with an unbalanced stack\n"); + abort(); + } + g_esp = stack; + g_cur_func = caller; + return g_eax != 0; +} + +static void crt_set_new_handler(void) +{ + g_eax = new_handler; + new_handler = MEM32(g_esp + 4); + g_esp += 4; +} + +static void crt_set_new_mode(void) +{ + uint32_t mode = MEM32(g_esp + 4); + g_eax = new_mode; + if (mode <= 1) new_mode = mode; + else { MEM32(guest_crt_errno_address()) = 22; g_eax = UINT32_MAX; } + g_esp += 4; +} + +static uint32_t allocate(uint32_t size, uint32_t old) +{ + uint32_t result = 0; + /* MSVCRT calls the handler once for oversized malloc/realloc, in either mode. */ + if (size > 0xffffffe0u) call_new_handler(size); + else { + do { + result = old ? guest_realloc(old, size) : guest_malloc(size); + } while (!result && new_mode && call_new_handler(size)); + } + if (!result) MEM32(guest_crt_errno_address()) = 12; + return result; +} + /* Every CRT adapter consumes the guest return slot; cdecl callers pop arguments. */ static void crt_malloc(void) { - g_eax = guest_malloc(MEM32(g_esp + 4)); + g_eax = allocate(MEM32(g_esp + 4), 0); g_esp += 4; } @@ -280,14 +330,18 @@ static void crt_free(void) static void crt_realloc(void) { - g_eax = guest_realloc(MEM32(g_esp + 4), MEM32(g_esp + 8)); + uint32_t old = MEM32(g_esp + 4), size = MEM32(g_esp + 8); + if (old && !size) { guest_free(old); g_eax = 0; } + else g_eax = allocate(size, old); g_esp += 4; } static void crt_calloc(void) { uint64_t size = (uint64_t)MEM32(g_esp + 4) * MEM32(g_esp + 8); - g_eax = size <= UINT32_MAX ? guest_malloc((uint32_t)size) : 0; + g_eax = 0; + if (size > 0xffffffe0u) MEM32(guest_crt_errno_address()) = 12; + else g_eax = allocate((uint32_t)size, 0); if (g_eax) memset((void *)ADDR(g_eax), 0, (size_t)size); g_esp += 4; } @@ -342,11 +396,9 @@ static void crt_cexit(void) static void crt_new(void) { - g_eax = guest_malloc(MEM32(g_esp + 4)); - if (!g_eax) { - fprintf(stderr, "Guest operator new: bad_alloc delivery is not implemented\n"); - abort(); - } + uint32_t size = MEM32(g_esp + 4), result; + do { result = allocate(size, 0); } while (!result && call_new_handler(size)); + g_eax = result; g_esp += 4; } @@ -592,6 +644,8 @@ recomp_func_t guest_crt_lookup(const char *name) { static const struct { const char *name; recomp_func_t function; } functions[] = { {"_errno", crt_errno}, + {"?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z", crt_set_new_handler}, + {"?_set_new_mode@@YAHH@Z", crt_set_new_mode}, {"_set_error_mode", crt_set_error_mode}, {"signal", crt_signal}, {"raise", crt_raise}, {"abort", crt_abort}, {"__set_app_type", crt_set_app_type}, {"__p__fmode", crt_p_fmode}, diff --git a/decompiled/tests/guest_crt.c b/decompiled/tests/guest_crt.c @@ -356,6 +356,7 @@ static void synchronous_abort_signal_calls_guest_handler_once(void) int main(void) { assert(guest_map("test memory and stack", 0x50000000u, 0x10000u)); + g_fs_base = 0x50004000u; initializes_callbacks_in_order(); calloc_rejects_32_bit_overflow(); calloc_zeroes_reused_memory(); diff --git a/decompiled/tests/new_handler.c b/decompiled/tests/new_handler.c @@ -0,0 +1,173 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#include <windows.h> +static HMODULE crt; +#else +#include "guest_crt.h" +#include "guest_memory.h" +#endif + +static const char *set_handler = "?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z"; +static unsigned calls, retries, expected_size; + +static int handler(unsigned size) +{ + assert(size == expected_size); + ++calls; + return calls <= retries ? -7 : 0; /* Any nonzero return requests another try. */ +} + +#ifndef _WIN32 +static void guest_handler(void) +{ + g_eax = handler(MEM32(g_esp + 4)); + g_ecx = 0x11223344u; + g_edx = 0x55667788u; + g_cur_func = 0x11110000u; + g_esp += 4; +} +recomp_func_t recomp_lookup(uint32_t address) +{ + assert(address == 0x11110000u); + return guest_handler; +} +recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } +#endif + +static uint32_t call(const char *name, uint32_t first, uint32_t second) +{ +#ifdef _WIN32 + uint32_t (__cdecl *function)(uint32_t, uint32_t) = (void *)GetProcAddress(crt, name); + if (!function) fprintf(stderr, "Missing MSVCRT export %s\n", name); + assert(function); + return function(first, second); +#else + g_esp = 0x5000ff00u; + g_ebx = 0x12345678u; + g_esi = 0x23456789u; + g_edi = 0x3456789au; + g_ebp = 0x456789abu; + g_cur_func = 0x22220000u; + MEM32(g_esp) = RECOMP_RETADDR; + MEM32(g_esp + 4) = first; + MEM32(g_esp + 8) = second; + recomp_func_t function = guest_crt_lookup(name); + assert(function); + function(); + assert(g_esp == 0x5000ff04u && g_cur_func == 0x22220000u); + assert(g_ebx == 0x12345678u && g_esi == 0x23456789u); + assert(g_edi == 0x3456789au && g_ebp == 0x456789abu); + return g_eax; +#endif +} + +static void *memory(uint32_t address) +{ +#ifdef _WIN32 + return (void *)(uintptr_t)address; +#else + return (void *)ADDR(address); +#endif +} + +static void registration_preserves_previous_values(uint32_t callback) +{ + call(set_handler, 0, 0); + call("?_set_new_mode@@YAHH@Z", 0, 0); + + uint32_t previous = call(set_handler, callback, 0); + uint32_t replaced = call(set_handler, callback, 0); + uint32_t mode = call("?_set_new_mode@@YAHH@Z", 1, 0); + uint32_t invalid = call("?_set_new_mode@@YAHH@Z", 2, 0); + + assert(previous == 0 && replaced == callback && mode == 0); + assert(invalid == UINT32_MAX); + assert(*(int *)memory(call("_errno", 0, 0)) == 22); + assert(call("?_set_new_mode@@YAHH@Z", 0, 0) == 1); + assert(call(set_handler, 0, 0) == callback); + assert(call("??2@YAPAXI@Z", 0xfffff000u, 0) == 0); + call(set_handler, callback, 0); +} + +static void allocation_failure(const char *name, uint32_t size, unsigned mode, + unsigned expected_calls) +{ + call("?_set_new_mode@@YAHH@Z", mode, 0); + uint32_t allocation = call("malloc", 32, 0); + assert(allocation); + memset(memory(allocation), 0x5a, 32); + expected_size = size; + calls = 0; + retries = 3; + *(int *)memory(call("_errno", 0, 0)) = 77; + uint32_t first = size, second = 0; + if (!strcmp(name, "realloc")) { first = allocation; second = size; } + if (!strcmp(name, "calloc")) { first = 1; second = size; } + + uint32_t result = call(name, first, second); + int error = *(int *)memory(call("_errno", 0, 0)); + + if (result || calls != expected_calls || error != 12) { + fprintf(stderr, "%s size=%08x mode=%u result=%08x calls=%u expected=%u errno=%d\n", + name, size, mode, result, calls, expected_calls, error); + } + assert(result == 0 && calls == expected_calls && error == 12); + for (unsigned i = 0; i < 32; ++i) assert(((unsigned char *)memory(allocation))[i] == 0x5a); + call("free", allocation, 0); +} + +static void successful_and_zero_allocations(void) +{ + calls = 0; + call("?_set_new_mode@@YAHH@Z", 1, 0); + *(int *)memory(call("_errno", 0, 0)) = 77; + + uint32_t zero = call("??2@YAPAXI@Z", 0, 0); + uint32_t bytes = call("calloc", 8, 4); + uint32_t resized = call("realloc", bytes, 64); + + assert(zero && bytes && resized && !calls); + assert(*(int *)memory(call("_errno", 0, 0)) == 77); + for (unsigned i = 0; i < 32; ++i) assert(((unsigned char *)memory(resized))[i] == 0); + assert(call("realloc", resized, 0) == 0); + call("free", zero, 0); + assert(!calls); +} + +int main(void) +{ +#ifdef _WIN32 + crt = LoadLibraryA("msvcrt.dll"); + assert(crt); + uint32_t callback = (uintptr_t)handler; +#else + assert(guest_map("new-handler fixture", 0x50000000u, 0x10000)); + g_fs_base = 0x50004000u; + uint32_t callback = 0x11110000u; +#endif + registration_preserves_previous_values(callback); + allocation_failure("malloc", 0xfffff000u, 0, 0); + allocation_failure("malloc", 0xfffff000u, 1, 4); + allocation_failure("realloc", 0xfffff000u, 0, 0); + allocation_failure("realloc", 0xfffff000u, 1, 4); + allocation_failure("calloc", 0xfffff000u, 0, 0); + allocation_failure("calloc", 0xfffff000u, 1, 4); + allocation_failure("??2@YAPAXI@Z", 0xfffff000u, 0, 4); + allocation_failure("??2@YAPAXI@Z", 0xfffff000u, 1, 5); + allocation_failure("malloc", UINT32_MAX, 0, 1); + allocation_failure("malloc", UINT32_MAX, 1, 1); + allocation_failure("realloc", UINT32_MAX, 0, 1); + allocation_failure("realloc", UINT32_MAX, 1, 1); + allocation_failure("calloc", UINT32_MAX, 0, 0); + allocation_failure("calloc", UINT32_MAX, 1, 0); + allocation_failure("??2@YAPAXI@Z", UINT32_MAX, 0, 4); + allocation_failure("??2@YAPAXI@Z", UINT32_MAX, 1, 4); + successful_and_zero_allocations(); + call(set_handler, 0, 0); + call("?_set_new_mode@@YAHH@Z", 0, 0); + puts("new handler: registration, modes, 16 failure paths, retries, realloc preservation and ABI passed"); + return 0; +} diff --git a/tools/reconstruction/test_portable_new_handler.py b/tools/reconstruction/test_portable_new_handler.py @@ -0,0 +1,26 @@ +"""Compare guest allocation-failure callbacks with Windows x86 MSVCRT.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-new-handler" +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/new_handler.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/new_handler.c", "runtime/guest_crt.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())