aorecomp

Anarchy Online classic in the browser

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


commit f50056b2e8f947f4f217025abefdc093493bf6b9
parent dc14f99c79fc87460678a58ad1ea7caf83ebea77
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 12:37:05 +0000

Avoid translating empty image-row copy addresses

Diffstat:
Mdecompiled/Makefile.linux | 6++++++
Mdecompiled/src/bbr.dll/lifted-0047.c | 9+++++----
Adecompiled/tests/image_row.c | 45+++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_image_row.py | 22++++++++++++++++++++++
4 files changed, 78 insertions(+), 4 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -202,6 +202,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/atoi ./build/linux/atof ./build/linux/cifmod + ./build/linux/image-row ./build/linux/ctype ./build/linux/itoa ./build/linux/graphics @@ -321,3 +322,8 @@ build/linux/cifmod: tests/cifmod.c runtime/guest_crt.c runtime/guest_heap.c runt $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ test: build/linux/cifmod + +build/linux/image-row: tests/image_row.c src/bbr.dll/lifted-0047.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h + $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ + +test: build/linux/image-row diff --git a/decompiled/src/bbr.dll/lifted-0047.c b/decompiled/src/bbr.dll/lifted-0047.c @@ -134,6 +134,7 @@ L_250CB6D2: RECOMP_REGS_OUT(); } +/* REP MOVS with a zero count must not translate or access either address. */ void sub_250CB6E0(void) { int _fpu_cmp = 0; uint32_t _cf = 0; @@ -183,13 +184,13 @@ L_250CB6F2: if (2u) _cf = (((ecx) >> 1) & 1u); /* 0x250CB716: shr ecx, 2 */ if (2u) { _flag_a = recomp_flags_pack((ecx >> 2u), _cf, 0, 0); _flag_b = 0; _flag_k = FK_EFLAGS; } ecx = ecx >> 2u; /* 0x250CB716: shr ecx, 2 */ - { uint32_t _n = ecx, _b = _n * 4u; /* 0x250CB719: rep movsd dword ptr es:[edi], dword ptr [esi] */ if (_df > 0) { memmove((void*)ADDR(edi), (void*)ADDR(esi), _b); esi += _b; edi += _b; } else { memmove((void*)ADDR(edi - _b + 4u), (void*)ADDR(esi - _b + 4u), _b); esi -= _b; edi -= _b; } ecx = 0; } + { uint32_t _n = ecx, _b = _n * 4u; /* 0x250CB719: rep movsd dword ptr es:[edi], dword ptr [esi] */ if (_n && _df > 0) { memmove((void*)ADDR(edi), (void*)ADDR(esi), _b); esi += _b; edi += _b; } else if (_n) { memmove((void*)ADDR(edi - _b + 4u), (void*)ADDR(esi - _b + 4u), _b); esi -= _b; edi -= _b; } ecx = 0; } ecx = eax; /* 0x250CB71B: mov ecx, eax */ _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(3); ecx = ecx & 3; /* 0x250CB71D: and ecx, 3 */ _cf = 0; _flag_k = FK_TEST; - { uint32_t _n = ecx, _b = _n * 1u; /* 0x250CB720: rep movsb byte ptr es:[edi], byte ptr [esi] */ if (_df > 0) { memmove((void*)ADDR(edi), (void*)ADDR(esi), _b); esi += _b; edi += _b; } else { memmove((void*)ADDR(edi - _b + 1u), (void*)ADDR(esi - _b + 1u), _b); esi -= _b; edi -= _b; } ecx = 0; } + { uint32_t _n = ecx, _b = _n * 1u; /* 0x250CB720: rep movsb byte ptr es:[edi], byte ptr [esi] */ if (_n && _df > 0) { memmove((void*)ADDR(edi), (void*)ADDR(esi), _b); esi += _b; edi += _b; } else if (_n) { memmove((void*)ADDR(edi - _b + 1u), (void*)ADDR(esi - _b + 1u), _b); esi -= _b; edi -= _b; } ecx = 0; } edi = POP32_VAL(esp); /* 0x250CB722: pop edi */ esi = POP32_VAL(esp); /* 0x250CB723: pop esi */ ebx = POP32_VAL(esp); /* 0x250CB724: pop ebx */ @@ -255,13 +256,13 @@ L_250CB778: if (2u) { _flag_a = recomp_flags_pack((ecx >> 2u), _cf, 0, 0); _flag_b = 0; _flag_k = FK_EFLAGS; } ecx = ecx >> 2u; /* 0x250CB77F: shr ecx, 2 */ esi = edx; /* 0x250CB782: mov esi, edx */ - { uint32_t _n = ecx, _b = _n * 4u; /* 0x250CB784: rep movsd dword ptr es:[edi], dword ptr [esi] */ if (_df > 0) { memmove((void*)ADDR(edi), (void*)ADDR(esi), _b); esi += _b; edi += _b; } else { memmove((void*)ADDR(edi - _b + 4u), (void*)ADDR(esi - _b + 4u), _b); esi -= _b; edi -= _b; } ecx = 0; } + { uint32_t _n = ecx, _b = _n * 4u; /* 0x250CB784: rep movsd dword ptr es:[edi], dword ptr [esi] */ if (_n && _df > 0) { memmove((void*)ADDR(edi), (void*)ADDR(esi), _b); esi += _b; edi += _b; } else if (_n) { memmove((void*)ADDR(edi - _b + 4u), (void*)ADDR(esi - _b + 4u), _b); esi -= _b; edi -= _b; } ecx = 0; } ecx = eax; /* 0x250CB786: mov ecx, eax */ _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(3); ecx = ecx & 3; /* 0x250CB788: and ecx, 3 */ _cf = 0; _flag_k = FK_TEST; - { uint32_t _n = ecx, _b = _n * 1u; /* 0x250CB78B: rep movsb byte ptr es:[edi], byte ptr [esi] */ if (_df > 0) { memmove((void*)ADDR(edi), (void*)ADDR(esi), _b); esi += _b; edi += _b; } else { memmove((void*)ADDR(edi - _b + 1u), (void*)ADDR(esi - _b + 1u), _b); esi -= _b; edi -= _b; } ecx = 0; } + { uint32_t _n = ecx, _b = _n * 1u; /* 0x250CB78B: rep movsb byte ptr es:[edi], byte ptr [esi] */ if (_n && _df > 0) { memmove((void*)ADDR(edi), (void*)ADDR(esi), _b); esi += _b; edi += _b; } else if (_n) { memmove((void*)ADDR(edi - _b + 1u), (void*)ADDR(esi - _b + 1u), _b); esi -= _b; edi -= _b; } ecx = 0; } SET_LO8(ecx, MEM8(ebp + 0xF)); /* 0x250CB78D: mov cl, byte ptr [ebp + 0xf] */ L_250CB790: esi = MEM32(ebp + 0x8); /* 0x250CB790: mov esi, dword ptr [ebp + 8] */ diff --git a/decompiled/tests/image_row.c b/decompiled/tests/image_row.c @@ -0,0 +1,45 @@ +#include "recomp_types.h" +#include <assert.h> +#ifdef _WIN32 +#include <windows.h> +#endif + +void sub_250CB6E0(void); +recomp_func_t recomp_lookup(uint32_t address) { abort(); } +recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } +recomp_func_t recomp_lookup_manual(uint32_t address) { abort(); } + +int main(void) +{ + for (unsigned i = 0; i < 4; ++i) { + uint32_t address = 0x50000000u + i * 0x1000000u; +#ifdef _WIN32 + assert(VirtualAlloc((void *)(uintptr_t)address, 0x10000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == (void *)(uintptr_t)address); +#else + assert(guest_map("image row test", address, 0x10000)); +#endif + } + const unsigned lengths[] = {0, 1, 2, 3, 4, 7, 8, 2048}; + for (unsigned i = 0; i < sizeof(lengths) / sizeof(lengths[0]); ++i) { + unsigned length = lengths[i]; + uint32_t input = 0x50010000 - length, output = 0x51010000 - length; + memset((void *)ADDR(0x51000000), 0xa5, 0x10000); + for (unsigned j = 0; j < length; ++j) MEM8(input + j) = j * 13 + 7; + MEM32(0x520000f8) = input - 1; + MEM32(0x520000d4) = length; + MEM8(0x52000117) = 8; + g_esp = 0x5300ff00; + MEM32(g_esp + 4) = 0x52000000; + MEM32(g_esp + 8) = output; + MEM32(g_esp + 12) = 255; + g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; + + sub_250CB6E0(); + + assert(g_esp == 0x5300ff04); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); + for (unsigned j = 0; j < length; ++j) assert(MEM8(output + j) == (unsigned char)(j * 13 + 7)); + assert(MEM8(output - 1) == 0xa5); + } + puts("image row: 8 boundary-ending copies, including zero length; bytes, canaries and ABI passed"); +} diff --git a/tools/reconstruction/test_portable_image_row.py b/tools/reconstruction/test_portable_image_row.py @@ -0,0 +1,22 @@ +"""Compare the actual generated image-row copy on Windows and Linux.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-image-row" +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", "-ffunction-sections", "-fno-strict-aliasing", "tests/image_row.c", + "src/bbr.dll/lifted-0047.c", "runtime/recomp_state.c", "-Wl,--gc-sections", "-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/image-row"], check=True) +actual = subprocess.check_output(wsl + ["./build/linux/image-row"], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux: " + actual.strip())