aorecomp

Anarchy Online classic in the browser

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


commit 619e79516921acb2025d19cbdafe217d8b9e02e0
parent 0b6085b9fead0279cd972135f22ba8009a41f148
Author: anon <anon@example.com>
Date:   Sat, 17 Oct 2026 01:09:31 +0000

Skip zero-count archive string copies before guest address translation

Diffstat:
Msrc/Makefile.linux | 8++++++++
Msrc/game/ar.dll/lifted-0001.c | 8++++----
Asrc/tests/archive_string.c | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 98 insertions(+), 4 deletions(-)

diff --git a/src/Makefile.linux b/src/Makefile.linux @@ -227,6 +227,8 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/vertex-upload-paged ./build/linux/terrain-texture ./build/linux/terrain-texture-paged + ./build/linux/archive-string + ./build/linux/archive-string-paged ./build/linux/guest-memory 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 @@ -455,3 +457,9 @@ build/linux/terrain-texture build/linux/terrain-texture-paged: tests/terrain_tex $(CC) $(CPPFLAGS) $(CFLAGS) $(if $(filter %-paged,$@),-U__linux__) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ test: build/linux/terrain-texture build/linux/terrain-texture-paged + +build/linux/archive-string build/linux/archive-string-paged: tests/archive_string.c game/ar.dll/lifted-0001.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(if $(filter %-paged,$@),-U__linux__) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ + +test: build/linux/archive-string build/linux/archive-string-paged diff --git a/src/game/ar.dll/lifted-0001.c b/src/game/ar.dll/lifted-0001.c @@ -1639,13 +1639,13 @@ L_24003994: if (2u) _cf = (((ecx) >> 1) & 1u); /* 0x2400399B: 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; /* 0x2400399B: shr ecx, 2 */ - { uint32_t _n = ecx, _b = _n * 4u; /* 0x2400399E: 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; } + if (ecx) { uint32_t _n = ecx, _b = _n * 4u; /* 0x2400399E: 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; } ecx = edx; /* 0x240039A0: mov ecx, edx */ _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(3); ecx = ecx & 3; /* 0x240039A2: and ecx, 3 */ _cf = 0; _flag_k = FK_TEST; - { uint32_t _n = ecx, _b = _n * 1u; /* 0x240039A5: 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; } + if (ecx) { uint32_t _n = ecx, _b = _n * 1u; /* 0x240039A5: 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; } eax = MEM32(ebp + (-0x18)); /* 0x240039A7: mov eax, dword ptr [ebp - 0x18] */ MEM32(ebp + (-0x14)) = ebx; /* 0x240039AA: mov dword ptr [ebp - 0x14], ebx */ MEM8(eax + ebx) = (uint8_t)(0); /* 0x240039AD: mov byte ptr [eax + ebx], 0 */ @@ -1763,13 +1763,13 @@ L_24003A4B: if (2u) _cf = (((ecx) >> 1) & 1u); /* 0x24003A52: 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; /* 0x24003A52: shr ecx, 2 */ - { uint32_t _n = ecx, _b = _n * 4u; /* 0x24003A55: 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; } + if (ecx) { uint32_t _n = ecx, _b = _n * 4u; /* 0x24003A55: 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; } ecx = eax; /* 0x24003A57: mov ecx, eax */ _flag_a = (uint32_t)(ecx); _flag_b = (uint32_t)(3); ecx = ecx & 3; /* 0x24003A59: and ecx, 3 */ _cf = 0; _flag_k = FK_TEST; - { uint32_t _n = ecx, _b = _n * 1u; /* 0x24003A5C: 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; } + if (ecx) { uint32_t _n = ecx, _b = _n * 1u; /* 0x24003A5C: 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; } ecx = MEM32(ebp + 0xC); /* 0x24003A5E: mov ecx, dword ptr [ebp + 0xc] */ PUSH32(esp, ebx); /* 0x24003A61: push ebx */ RECOMP_ICALL(MEM32(0x2400B03C)); /* 0x24003A62: call dword ptr [0x2400b03c] */ diff --git a/src/tests/archive_string.c b/src/tests/archive_string.c @@ -0,0 +1,86 @@ +#include "recomp_types.h" +#include <assert.h> + +void sub_24003930(void); +static unsigned length, allocations; + +static void read_string(void) +{ + assert(MEM32(g_esp + 8) == 6); + MEM32(MEM32(g_esp + 12)) = 0x50010000 - length; + MEM32(MEM32(g_esp + 16)) = length; + g_eax = 0; + g_esp += 24; +} + +static void grow_string(void) +{ + uint32_t data = allocations++ ? 0x53002001 : 0x51001001; + MEM32(g_ecx + 4) = data; + MEM32(g_ecx + 12) = MEM32(g_esp + 4); + MEM8(data - 1) = 1; + g_eax = 1; + g_esp += 12; +} + +static void tidy_string(void) { g_esp += 8; } +static void set_length(void) +{ + MEM32(g_ecx + 8) = MEM32(g_esp + 4); + MEM8(MEM32(g_ecx + 4) + MEM32(g_ecx + 8)) = 0; + g_esp += 8; +} + +recomp_func_t recomp_lookup(uint32_t address) +{ + switch (address) { + case 0x24004200: return read_string; + case 0x100: return grow_string; + case 0x104: return tidy_string; + case 0x108: return set_length; + default: fprintf(stderr, "Unexpected archive-string call %08x\n", address); abort(); + } +} +recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } +recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } + +int main(void) +{ + for (unsigned i = 0; i < 4; ++i) + assert(guest_map("archive string", 0x50000000 + i * 0x1000000, 0x10000)); + assert(guest_map("archive imports", 0x24000000, 0x10000)); + MEM32(0x2400B048) = 0x100; + MEM32(0x2400B020) = 0x104; + MEM32(0x2400B03C) = 0x108; + MEM32(0x2400B044) = 0x53003000; + const unsigned cases[][2] = {{4, 4}, {0, 0}, {1, 1}, {3, 3}, {8, 8}, {8, 4}, {8, 3}}; + for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { + length = cases[i][0]; + allocations = 0; + memset((void *)ADDR(0x51000000), 0xa5, 0x10000); + memset((void *)ADDR(0x53000000), 0xa5, 0x10000); + memset((void *)ADDR(0x53000000), 0, 16); + MEM32(0x53003000) = cases[i][1]; + for (unsigned j = 0; j < length; ++j) MEM8(0x50010000 - length + j) = j + 1; + g_fs_base = 0x53001000; + MEM32(g_fs_base) = 0xffffffff; + g_esp = 0x5200ff00; + MEM32(g_esp + 4) = 0x42; + MEM32(g_esp + 8) = 0x53000000; + MEM32(g_esp + 12) = 0; + g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; + + sub_24003930(); + + assert(g_eax == 0 && g_esp == 0x5200ff10); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); + assert(MEM32(g_fs_base) == 0xffffffff); + assert(MEM32(0x53000008) == cases[i][1]); + uint32_t data = MEM32(0x53000004); + for (unsigned j = 0; j < cases[i][1]; ++j) assert(MEM8(data + j) == j + 1); + assert(MEM8(data + cases[i][1]) == 0); + assert(MEM8(data + cases[i][1] + 1) == 0xa5); + assert(MEM8(data - 2) == 0xa5); + } + puts("archive string: page-ending and empty strings, truncation, canaries, FS and ABI passed"); +}