aorecomp

Anarchy Online classic in the browser

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


commit 1144050fe2e9bf982dc7e57d7dcc8ef2a767c9a8
parent 97d2de3d844324f3c6a74367b8017760e3c7dfdf
Author: anon <anon@example.com>
Date:   Fri, 16 Oct 2026 06:34:49 +0000

Bridge native qsort to reconstructed comparison callbacks

Diffstat:
Mdecompiled/Makefile.linux | 6++++++
Mdecompiled/runtime/guest_crt.c | 35+++++++++++++++++++++++++++++++++++
Adecompiled/tests/qsort.c | 132+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_qsort.py | 20++++++++++++++++++++
4 files changed, 193 insertions(+), 0 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -212,6 +212,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/surfaces ./build/linux/devices build/linux/device-pixels.rgb ./build/linux/data-record + ./build/linux/qsort build/linux/surfaces: tests/surfaces.c runtime/linux_graphics.c runtime/linux_window.c runtime/gl_renderer.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_graphics.h runtime/linux_window.h runtime/gl_renderer.h runtime/guest_heap.h runtime/guest_memory.h runtime/recomp_types.h mkdir -p $(@D) @@ -346,3 +347,8 @@ build/linux/data-record: tests/data_record.c src/ar.dll/lifted-0001.c runtime/gu $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ test: build/linux/data-record + +build/linux/qsort: tests/qsort.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 + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ + +test: build/linux/qsort diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c @@ -1,3 +1,4 @@ +#define _GNU_SOURCE #include "guest_crt.h" #include "guest_heap.h" #include "guest_memory.h" @@ -25,6 +26,39 @@ static void crt_errno(void) g_esp += 4; } +struct sort_context { uint32_t base; const unsigned char *memory; recomp_func_t compare; }; + +static int compare_guest_elements(const void *left, const void *right, void *context) +{ + struct sort_context *sort = context; + uint32_t stack = g_esp, caller = g_cur_func; + PUSH32(g_esp, sort->base + ((const unsigned char *)right - sort->memory)); + PUSH32(g_esp, sort->base + ((const unsigned char *)left - sort->memory)); + PUSH32(g_esp, RECOMP_RETADDR); + sort->compare(); + if (g_esp != stack - 8) { + fprintf(stderr, "Guest qsort comparator returned with an unbalanced stack\n"); + abort(); + } + g_esp = stack; + g_cur_func = caller; + return (int32_t)g_eax; +} + +static void crt_qsort(void) +{ + uint32_t base = MEM32(g_esp + 4), count = MEM32(g_esp + 8); + uint32_t width = MEM32(g_esp + 12), comparator = MEM32(g_esp + 16); + if (count > 1 && width) { + recomp_func_t compare = recomp_lookup(comparator); + if (!compare) compare = recomp_lookup_import(comparator); + if (!compare) { fprintf(stderr, "Missing qsort comparator %08x\n", comparator); abort(); } + struct sort_context context = {base, (const unsigned char *)ADDR(base), compare}; + qsort_r((void *)context.memory, count, width, compare_guest_elements, &context); + } + g_esp += 4; +} + static void crt_set_error_mode(void) { int mode = (int32_t)MEM32(g_esp + 4); @@ -924,6 +958,7 @@ recomp_func_t guest_crt_lookup(const char *name) {"_controlfp", crt_controlfp}, {"malloc", crt_malloc}, {"free", crt_free}, {"realloc", crt_realloc}, {"srand", crt_srand}, {"rand", crt_rand}, + {"qsort", crt_qsort}, {"calloc", crt_calloc}, {"strncpy", crt_strncpy}, {"strchr", crt_strchr}, {"strcpy", crt_strcpy}, {"strstr", crt_strstr}, diff --git a/decompiled/tests/qsort.c b/decompiled/tests/qsort.c @@ -0,0 +1,132 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#include <windows.h> +static void (__cdecl *native_sort)(void *, size_t, size_t, int (__cdecl *)(const void *, const void *)); +#else +#include "guest_crt.h" +#include "guest_memory.h" +#endif + +static unsigned width, nested, calls; +static uint32_t base, bytes; +static void sort(uint32_t address, unsigned count, unsigned size); +static void *memory(uint32_t address) +{ +#ifdef _WIN32 + return (void *)(uintptr_t)address; +#else + return (void *)ADDR(address); +#endif +} + +static int compare(const void *left, const void *right) +{ + assert((const unsigned char *)left >= (const unsigned char *)memory(base)); + assert((const unsigned char *)left < (const unsigned char *)memory(base) + bytes); + assert((const unsigned char *)right >= (const unsigned char *)memory(base)); + assert((const unsigned char *)right < (const unsigned char *)memory(base) + bytes); + ++calls; + if (nested == 1) { + nested = 2; + unsigned saved_width = width; + uint32_t saved_base = base, saved_bytes = bytes; + const unsigned char input[] = {9, 1, 7, 3}; + memcpy(memory(base + bytes + 16), input, 4); + sort(base + bytes + 16, 4, 1); + assert(!memcmp(memory(saved_base + saved_bytes + 16), "\1\3\7\11", 4)); + width = saved_width; base = saved_base; bytes = saved_bytes; + } + int a, b; + if (width == 1) { a = *(const unsigned char *)left; b = *(const unsigned char *)right; } + else { memcpy(&a, left, 4); memcpy(&b, right, 4); } + return a < b ? -123 : a > b ? 456 : 0; +} + +#ifndef _WIN32 +static void guest_compare(void) +{ + g_cur_func = 0x11110000; + g_eax = compare(memory(MEM32(g_esp + 4)), memory(MEM32(g_esp + 8))); + g_ecx = 0xabcdef01; g_edx = 0x23456789; + g_esp += 4; +} +recomp_func_t recomp_lookup(uint32_t address) { return address == 0x11110000 ? guest_compare : NULL; } +recomp_func_t recomp_lookup_import(uint32_t address) { assert(address == 0xf0000000); return guest_compare; } +#endif + +static void sort(uint32_t address, unsigned count, unsigned size) +{ + base = address; bytes = count * size; width = size; +#ifdef _WIN32 + native_sort(memory(address), count, size, compare); +#else + uint32_t stack = g_esp, caller = g_cur_func; + g_esp -= 20; + MEM32(g_esp) = RECOMP_RETADDR; + MEM32(g_esp + 4) = address; MEM32(g_esp + 8) = count; + MEM32(g_esp + 12) = size; + MEM32(g_esp + 16) = nested ? 0xf0000000 : 0x11110000; + guest_crt_lookup("qsort")(); + assert(g_esp == stack - 16 && g_cur_func == caller); + assert(g_ebx == 0x12345678 && g_esi == 0x23456789); + assert(g_edi == 0x3456789a && g_ebp == 0x456789ab); + g_esp = stack; +#endif +} + +int main(void) +{ +#ifdef _WIN32 + native_sort = (void *)GetProcAddress(LoadLibraryA("msvcrt.dll"), "qsort"); + assert(native_sort); + unsigned char storage[16384]; + uint32_t address = (uintptr_t)storage + 1; +#else + assert(guest_map("qsort arrays", 0x50000000, 0x20000)); + assert(guest_map("qsort stack", 0x51000000, 0x10000)); + g_esp = 0x5100ff00; + g_cur_func = 0x22220000; + g_ebx = 0x12345678; g_esi = 0x23456789; + g_edi = 0x3456789a; g_ebp = 0x456789ab; + uint32_t address = 0x5000ff01; +#endif + const unsigned sizes[] = {1, 4, 8, 64}, counts[] = {0, 1, 2, 8, 31, 100, 200}; + uint32_t digest = 2166136261u; + for (unsigned s = 0; s < 4; ++s) + for (unsigned c = 0; c < 7; ++c) { + unsigned char *data = memory(address); + data[-1] = data[counts[c] * sizes[s]] = 0xa5; + for (unsigned i = 0; i < counts[c]; ++i) { + int key = (37 * i) % counts[c]; + memset(data + i * sizes[s], key, sizes[s]); + if (sizes[s] > 1) { key -= 100; memcpy(data + i * sizes[s], &key, 4); } + } + calls = 0; + nested = c == 6 ? 1 : 0; + + sort(address, counts[c], sizes[s]); + + assert(data[-1] == 0xa5 && data[counts[c] * sizes[s]] == 0xa5); + assert(counts[c] > 1 ? calls > 0 : calls == 0); + if (c == 6) assert(nested == 2); + for (unsigned i = 0; i < counts[c]; ++i) { + int key = i - 100; + if (sizes[s] == 1) assert(data[i] == i); + else assert(!memcmp(data + i * sizes[s], &key, 4)); + for (unsigned j = 4; j < sizes[s]; ++j) assert(data[i * sizes[s] + j] == i); + } + for (unsigned i = 0; i < counts[c] * sizes[s]; ++i) digest = (digest ^ data[i]) * 16777619u; + } + const int32_t input[] = {INT32_MAX, 0, INT32_MIN, -1, 0, INT32_MAX}; + const int32_t expected[] = {INT32_MIN, -1, 0, 0, INT32_MAX, INT32_MAX}; + memcpy(memory(address), input, sizeof(input)); + nested = 0; + + sort(address, 6, 4); + + assert(!memcmp(memory(address), expected, sizeof(expected))); + printf("qsort: 29 array cases, nested/import callbacks, byte canaries and cdecl ABI; digest %08x\n", digest); +} diff --git a/tools/reconstruction/test_portable_qsort.py b/tools/reconstruction/test_portable_qsort.py @@ -0,0 +1,20 @@ +"""Compare reconstructed qsort callbacks and array contents with Windows MSVCRT.""" +import pathlib +import subprocess + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-qsort" +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/qsort.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/qsort"], check=True) +actual = subprocess.check_output(wsl + ["./build/linux/qsort"], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux: " + actual.strip())