aorecomp

Anarchy Online classic in the browser

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


commit 93d41df0fbc914bdd8674ac8e3171edc53e8cbd3
parent fadd291e41d77297aae72a25dc213c94a5fc0c30
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 05:50:33 +0000

Advance native Linux startup to the resource database

Diffstat:
Mdecompiled/runtime/guest_crt.c | 29+++++++++++++++++++++++++++++
Mdecompiled/runtime/linux_network.c | 19+++++++++++++++++++
Mdecompiled/runtime/linux_platform.c | 8++++++++
Mdecompiled/tests/guest_crt.c | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/tests/linux_platform.c | 22++++++++++++++++++++++
Mdecompiled/tests/network_startup.c | 49+++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 208 insertions(+), 0 deletions(-)

diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c @@ -2,6 +2,7 @@ #include "guest_heap.h" #include "guest_memory.h" #include <ctype.h> +#include <time.h> static _Thread_local uint32_t random_state = 1; static uint32_t user_matherr; @@ -432,6 +433,22 @@ static void crt_eh_prolog(void) MEM32(g_esp - 4) = g_eax; } +static void crt_time(void) +{ + time_t now = time(NULL); + g_eax = now >= 0 && now <= INT32_MAX ? (uint32_t)now : UINT32_MAX; + uint32_t output = MEM32(g_esp + 4); + if (output) MEM32(output) = g_eax; + g_esp += 4; +} + +static void crt_strcpy(void) +{ + g_eax = MEM32(g_esp + 4); + strcpy((char *)ADDR(g_eax), (const char *)ADDR(MEM32(g_esp + 8))); + g_esp += 4; +} + static void crt_strncpy(void) { uint32_t dst = MEM32(g_esp + 4); @@ -449,6 +466,15 @@ static void crt_strchr(void) g_esp += 4; } +static void crt_strstr(void) +{ + uint32_t start = MEM32(g_esp + 4); + const char *text = (const char *)ADDR(start); + const char *found = strstr(text, (const char *)ADDR(MEM32(g_esp + 8))); + g_eax = found ? start + (uint32_t)(found - text) : 0; + g_esp += 4; +} + static void crt_strlen(void) { g_eax = (uint32_t)strlen((const char *)ADDR(MEM32(g_esp + 4))); @@ -719,6 +745,9 @@ recomp_func_t guest_crt_lookup(const char *name) {"malloc", crt_malloc}, {"free", crt_free}, {"realloc", crt_realloc}, {"srand", crt_srand}, {"rand", crt_rand}, {"calloc", crt_calloc}, {"strncpy", crt_strncpy}, {"strchr", crt_strchr}, + {"strcpy", crt_strcpy}, + {"strstr", crt_strstr}, + {"time", crt_time}, {"memcpy", crt_memcpy}, {"memmove", crt_memmove}, {"memset", crt_memset}, {"_initterm", crt_initterm}, {"__dllonexit", crt_dllonexit}, diff --git a/decompiled/runtime/linux_network.c b/decompiled/runtime/linux_network.c @@ -1,4 +1,5 @@ #include "linux_network.h" +#include <unistd.h> static unsigned startup_count; @@ -51,11 +52,29 @@ static void wsa_set_last_error(void) g_esp += 8; } +static void host_name(void) +{ + uint32_t output = MEM32(g_esp + 4); + int32_t size = MEM32(g_esp + 8); + char name[256]; + uint32_t error = 0; + if (!startup_count) error = 10093; /* WSANOTINITIALISED */ + else if (!output || size <= 0) error = 10014; /* WSAEFAULT */ + else if (gethostname(name, sizeof(name))) error = 10050; /* WSAENETDOWN */ + else if ((size_t)size <= strlen(name)) error = 10014; + else memcpy((void *)ADDR(output), name, strlen(name) + 1); + + MEM32(g_fs_base + 0x34) = error; + g_eax = error ? UINT32_MAX : 0; + g_esp += 12; +} + recomp_func_t linux_network_lookup(const char *name, uint32_t ordinal) { static const struct { const char *name; uint32_t ordinal; recomp_func_t function; } functions[] = { {"WSAStartup", 115, wsa_startup}, {"WSACleanup", 116, wsa_cleanup}, {"WSAGetLastError", 111, wsa_get_last_error}, {"WSASetLastError", 112, wsa_set_last_error}, + {"gethostname", 57, host_name}, }; for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i) if (name ? !strcmp(name, functions[i].name) : ordinal == functions[i].ordinal) diff --git a/decompiled/runtime/linux_platform.c b/decompiled/runtime/linux_platform.c @@ -294,6 +294,13 @@ static void create_directory(void) g_esp += 12; } +static void set_handle_count(void) +{ + /* Win32 keeps this DOS compatibility call as a no-op. */ + g_eax = MEM32(g_esp + 4); + 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; @@ -311,6 +318,7 @@ recomp_func_t linux_platform_lookup(const char *dll, const char *name) {"GetModuleFileNameA", get_module_filename}, {"DeleteFileA", delete_file}, {"CreateDirectoryA", create_directory}, + {"SetHandleCount", set_handle_count}, {"InterlockedExchange", interlocked_exchange}, {"InterlockedIncrement", interlocked_increment}, {"InterlockedDecrement", interlocked_decrement}, diff --git a/decompiled/tests/guest_crt.c b/decompiled/tests/guest_crt.c @@ -2,6 +2,7 @@ #include "guest_memory.h" #include "guest_heap.h" #include <assert.h> +#include <time.h> static void first_constructor(void) { @@ -353,6 +354,83 @@ static void synchronous_abort_signal_calls_guest_handler_once(void) assert(g_eax == 1); } +static void strcpy_returns_guest_pointer_and_copies_terminator(void) +{ + assert(guest_map("strcpy source", 0x53000000u, 0x20000u)); + assert(guest_map("strcpy destination", 0x52000000u, 0x20000u)); + const char *inputs[] = {"", "a", "cd_image\\data\\db\\ResourceDatabase.dat", "\x81\xff\x7f"}; + for (unsigned i = 0; i < sizeof(inputs) / sizeof(inputs[0]); ++i) { + size_t length = strlen(inputs[i]); + memcpy((void *)ADDR(0x5300fffeu), inputs[i], length + 1); + memset((void *)ADDR(0x5200fffcu), 0xa5, 64); + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = 0x5200fffdu; + MEM32(g_esp + 8) = 0x5300fffeu; + g_ebx = 0x12345678; + g_esi = 0x23456789; + g_edi = 0x3456789a; + g_ebp = 0x456789ab; + + guest_crt_lookup("strcpy")(); + + assert(g_eax == 0x5200fffdu && g_esp == 0x5000ff04u); + assert(!memcmp((void *)ADDR(0x5200fffdu), inputs[i], length + 1)); + assert(!memcmp((void *)ADDR(0x5300fffeu), inputs[i], length + 1)); + assert(MEM8(0x5200fffcu) == 0xa5); + for (unsigned offset = length + 1; offset < 63; ++offset) + assert(MEM8(0x5200fffdu + offset) == 0xa5); + assert(g_ebx == 0x12345678 && g_esi == 0x23456789); + assert(g_edi == 0x3456789a && g_ebp == 0x456789ab); + } +} + +static void time_writes_only_a_32_bit_guest_value(void) +{ + for (unsigned output = 0; output < 2; ++output) { + memset((void *)ADDR(0x50007000u), 0xa5, 12); + MEM32(guest_crt_errno_address()) = 777; + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = output ? 0x50007004u : 0; + g_ebx = 0x12345678; + g_esi = 0x23456789; + g_edi = 0x3456789a; + g_ebp = 0x456789ab; + time_t before = time(NULL); + + guest_crt_lookup("time")(); + time_t after = time(NULL); + + assert(g_eax >= before && g_eax <= after); + assert(MEM32(0x50007000u) == 0xa5a5a5a5); + assert(MEM32(0x50007008u) == 0xa5a5a5a5); + assert(MEM32(0x50007004u) == (output ? g_eax : 0xa5a5a5a5)); + assert(MEM32(guest_crt_errno_address()) == 777); + assert(g_esp == 0x5000ff04u); + assert(g_ebx == 0x12345678 && g_esi == 0x23456789); + assert(g_edi == 0x3456789a && g_ebp == 0x456789ab); + } +} + +static void substring_search_returns_guest_addresses(void) +{ + strcpy((char *)ADDR(0x50008003u), "aaabaaa\xff"); + const char *needles[] = {"", "aaa", "aab", "ba", "\xff", "aaabaaa\xff", "abz", "aaabaaa\xff!"}; + const uint32_t expected[] = {0x50008003u, 0x50008003u, 0x50008004u, 0x50008006u, + 0x5000800au, 0x50008003u, 0, 0}; + for (unsigned i = 0; i < sizeof(needles) / sizeof(needles[0]); ++i) { + strcpy((char *)ADDR(0x50009001u), needles[i]); + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = 0x50008003u; + MEM32(g_esp + 8) = 0x50009001u; + + guest_crt_lookup("strstr")(); + + assert(g_eax == expected[i] && g_esp == 0x5000ff04u); + assert(!strcmp((char *)ADDR(0x50008003u), "aaabaaa\xff")); + assert(!strcmp((char *)ADDR(0x50009001u), needles[i])); + } +} + int main(void) { assert(guest_map("test memory and stack", 0x50000000u, 0x10000u)); @@ -372,6 +450,9 @@ int main(void) reverse_string_search_returns_guest_addresses(); error_mode_preserves_previous_mode_and_errno(); synchronous_abort_signal_calls_guest_handler_once(); + strcpy_returns_guest_pointer_and_copies_terminator(); + time_writes_only_a_32_bit_guest_value(); + substring_search_returns_guest_addresses(); assert(MEM32(guest_crt_data("_adjust_fdiv")) == 0); puts("guest CRT: constructors, heap, EH frame, FILE data, floor and startup state passed"); return 0; diff --git a/decompiled/tests/linux_platform.c b/decompiled/tests/linux_platform.c @@ -187,6 +187,27 @@ static void exception_filter_registration_returns_previous_guest_callback(void) assert(MEM32(g_fs_base + 0x34) == 777); } +static void handle_count_preserves_the_requested_value(void) +{ + const uint32_t counts[] = {0, 1, 20, 255, 512, 65536, UINT32_MAX}; + for (unsigned i = 0; i < sizeof(counts) / sizeof(counts[0]); ++i) { + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = counts[i]; + MEM32(g_fs_base + 0x34) = 777; + g_ebx = 0x12345678; + g_esi = 0x23456789; + g_edi = 0x3456789a; + g_ebp = 0x456789ab; + + linux_platform_lookup("KERNEL32.dll", "SetHandleCount")(); + + assert(g_eax == counts[i] && g_esp == 0x5000ff08u); + assert(MEM32(g_fs_base + 0x34) == 777); + assert(g_ebx == 0x12345678 && g_esi == 0x23456789); + assert(g_edi == 0x3456789a && g_ebp == 0x456789ab); + } +} + int main(void) { assert(guest_map("test memory and stack", 0x50000000u, 0x10000u)); @@ -198,6 +219,7 @@ int main(void) startup_info_uses_32_bit_layout(); process_id_is_the_native_process(); exception_filter_registration_returns_previous_guest_callback(); + handle_count_preserves_the_requested_value(); puts("Linux platform: clocks, AO delta timer, last error, recursive locks and atomics passed"); return 0; } diff --git a/decompiled/tests/network_startup.c b/decompiled/tests/network_startup.c @@ -7,6 +7,7 @@ #else #include "linux_network.h" #include "guest_memory.h" +#include <sys/utsname.h> static uint32_t call(uint32_t ordinal, unsigned count, const uint32_t *arguments) { @@ -90,17 +91,62 @@ static void cleanup(int expected) hash(get_error()); } +static void hostname_queries(int initialized) +{ + char expected_name[256]; +#ifdef _WIN32 + DWORD size = sizeof(expected_name); + assert(GetComputerNameExA(ComputerNameDnsHostname, expected_name, &size)); +#else + struct utsname system; + assert(uname(&system) == 0); + strcpy(expected_name, system.nodename); +#endif + int length = strlen(expected_name); + assert(length > 0 && length < 256); + const struct { int size; int null_buffer; int error; } cases[] = { + {-1, 0, 10014}, {0, 0, 10014}, {1, 0, 10014}, {length, 0, 10014}, + {length + 1, 0, 0}, {256, 0, 0}, {256, 1, 10014}, {0, 1, 10014}, + }; + for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { + unsigned char bytes[264], expected[264]; + memset(bytes, 0xa5, sizeof(bytes)); + memset(expected, 0xa5, sizeof(expected)); + int error = initialized ? cases[i].error : 10093; + if (!error) memcpy(expected + 4, expected_name, length + 1); + set_error(777); +#ifndef _WIN32 + memcpy((void *)ADDR(0x50003000u), bytes, sizeof(bytes)); + uint32_t args[] = {cases[i].null_buffer ? 0 : 0x50003004u, cases[i].size}; +#endif + +#ifdef _WIN32 + int result = gethostname(cases[i].null_buffer ? NULL : (char *)bytes + 4, cases[i].size); +#else + int result = call(57, 2, args); + memcpy(bytes, (void *)ADDR(0x50003000u), sizeof(bytes)); +#endif + + assert(result == (error ? -1 : 0)); + assert(get_error() == (uint32_t)error); + assert(!memcmp(bytes, expected, sizeof(bytes))); + } +} + int main(void) { #ifdef _WIN32 assert(sizeof(WSADATA) == 400); HMODULE library = LoadLibraryA("ws2_32.dll"); assert(GetProcAddress(library, "WSAStartup") == GetProcAddress(library, (const char *)115)); + assert(GetProcAddress(library, "gethostname") == GetProcAddress(library, (const char *)57)); #else assert(guest_map("network test memory", 0x50000000u, 0x10000u)); g_fs_base = 0x50001000u; assert(linux_network_lookup("WSAStartup", 0) == linux_network_lookup(NULL, 115)); + assert(linux_network_lookup("gethostname", 0) == linux_network_lookup(NULL, 57)); #endif + hostname_queries(0); cleanup(-1); const struct { uint16_t version; uint32_t error; } versions[] = { {0, 777}, {0x0100, 777}, {1, 0}, {0x0101, 777}, {0xff01, 777}, @@ -109,12 +155,15 @@ int main(void) unsigned initialized = 0; for (unsigned i = 0; i < 11; ++i) initialized += startup(versions[i].version, versions[i].error) == 0; assert(initialized == 9); + hostname_queries(1); for (unsigned i = 0; i < initialized; ++i) cleanup(0); cleanup(-1); assert(startup(2, 0) == 0); cleanup(0); cleanup(-1); assert(digest == 0xd6dc7661); + hostname_queries(0); printf("network startup: 12 negotiations, WSADATA32 layout, nested cleanup and errors, digest %08x\n", digest); + puts("hostname: 24 buffer and initialization cases, native host name and guest ABI passed"); return 0; }