aorecomp

Anarchy Online classic in the browser

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


commit 8a2df066b80c71da884d3940c07b2c500e263042
parent e6f56aade9851f8ed9699b86c2ba905cb077a386
Author: anon <anon@example.com>
Date:   Fri, 16 Oct 2026 08:23:26 +0000

Fix Linux client connection-failure reporting

Diffstat:
Mdecompiled/Makefile.linux | 6++++++
Mdecompiled/runtime/guest_crt.c | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/runtime/guest_format.c | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/runtime/linux_file.c | 28++++++++++++++++++++++++++--
Mdecompiled/runtime/linux_imports.c | 12++++++++++++
Mdecompiled/src/ddr.dll/lifted-0026.c | 8++++++--
Adecompiled/tests/crt_time.c | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/tests/dynamic_imports.c | 61++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mdecompiled/tests/files.c | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/tests/guest_format.c | 25+++++++++++++++++++++++++
Mdecompiled/tests/locale.c | 123+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdocs/linux-import-coverage.json | 16++++++++--------
Atools/reconstruction/test_portable_crt_time.py | 22++++++++++++++++++++++
13 files changed, 635 insertions(+), 13 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -198,6 +198,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/stricmp ./build/linux/strncmp ./build/linux/locale + ./build/linux/crt-time ./build/linux/splitpath ./build/linux/atoi ./build/linux/atof @@ -304,6 +305,11 @@ build/linux/locale: tests/locale.c runtime/guest_crt.c runtime/guest_heap.c runt test: build/linux/locale +build/linux/crt-time: tests/crt_time.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/crt-time + build/linux/splitpath: tests/splitpath.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 $@ diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c @@ -600,6 +600,56 @@ static void crt_time(void) g_esp += 4; } +static void crt_gmtime(void) +{ + uint32_t input = MEM32(g_esp + 4); + g_esp += 4; + g_eax = 0; + if (!input || (int32_t)MEM32(input) < -43200) { + MEM32(guest_crt_errno_address()) = 22; + return; + } + + /* Legacy MSVCRT accepts twelve hours before the epoch without normalizing. */ + int32_t seconds = MEM32(input); + time_t timestamp = seconds < 0 ? 0 : seconds; + struct tm converted; + gmtime_r(&timestamp, &converted); + if (seconds < 0) { + converted.tm_sec = seconds % 60; + converted.tm_min = seconds / 60 % 60; + converted.tm_hour = seconds / 3600; + } + int32_t fields[] = {converted.tm_sec, converted.tm_min, converted.tm_hour, + converted.tm_mday, converted.tm_mon, converted.tm_year, + converted.tm_wday, converted.tm_yday, converted.tm_isdst}; + g_eax = g_fs_base + 0x2040; + memcpy((void *)ADDR(g_eax), fields, sizeof(fields)); +} + +static void crt_asctime(void) +{ + uint32_t input = MEM32(g_esp + 4); + g_esp += 4; + g_eax = 0; + int32_t fields[9]; + if (input) memcpy(fields, (void *)ADDR(input), sizeof(fields)); + if (!input || fields[0] < 0 || fields[0] > 59 || fields[1] < 0 || fields[1] > 59 || + fields[2] < 0 || fields[2] > 23 || fields[3] < 1 || fields[3] > 31 || + fields[4] < 0 || fields[4] > 11 || fields[5] < 0 || fields[5] > 8099 || + fields[6] < 0 || fields[6] > 6) { + MEM32(guest_crt_errno_address()) = 22; + return; + } + + /* MSVCRT pads the day with zero and returns a per-thread 26-byte buffer. */ + g_eax = g_fs_base + 0x2080; + snprintf((char *)ADDR(g_eax), 26, "%.3s %.3s %02d %02d:%02d:%02d %04d\n", + "SunMonTueWedThuFriSat" + fields[6] * 3, + "JanFebMarAprMayJunJulAugSepOctNovDec" + fields[4] * 3, + fields[3], fields[2], fields[1], fields[0], fields[5] + 1900); +} + static void crt_strcpy(void) { g_eax = MEM32(g_esp + 4); @@ -941,6 +991,61 @@ static void crt_localeconv(void) g_esp += 4; } +static void crt_getdays(void) +{ + /* The C locale returns a caller-owned list, starting with Sunday. */ + const char names[] = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:Wednesday" + ":Thu:Thursday:Fri:Friday:Sat:Saturday"; + g_eax = guest_malloc(sizeof(names)); + if (g_eax) memcpy((void *)ADDR(g_eax), names, sizeof(names)); + else MEM32(guest_crt_errno_address()) = 12; + g_esp += 4; +} + +static void crt_getmonths(void) +{ + /* The C locale returns a caller-owned list, starting with January. */ + const char names[] = ":Jan:January:Feb:February:Mar:March:Apr:April:May:May:Jun:June" + ":Jul:July:Aug:August:Sep:September:Oct:October:Nov:November:Dec:December"; + g_eax = guest_malloc(sizeof(names)); + if (g_eax) memcpy((void *)ADDR(g_eax), names, sizeof(names)); + else MEM32(guest_crt_errno_address()) = 12; + g_esp += 4; +} + +static void crt_gettnames(void) +{ + const char *names[] = { + "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", + "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", + "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", + "January", "February", "March", "April", "May", "June", "July", + "August", "September", "October", "November", "December", + "AM", "PM", "MM/dd/yy", "dddd, MMMM dd, yyyy", "HH:mm:ss", + }; + /* VC6 time data: 43 guest pointers, LCID, calendar type and reference count. */ + uint32_t size = 184; + for (unsigned i = 0; i < 43; ++i) size += strlen(names[i]) + 1; + g_eax = guest_malloc(size); + g_esp += 4; + if (!g_eax) { + MEM32(guest_crt_errno_address()) = 12; + return; + } + + /* Strings belong to the same allocation so the caller can free the table. */ + uint32_t output = g_eax + 184; + for (unsigned i = 0; i < 43; ++i) { + uint32_t length = strlen(names[i]) + 1; + MEM32(g_eax + i * 4) = output; + memcpy((void *)ADDR(output), names[i], length); + output += length; + } + MEM32(g_eax + 172) = 1033; + MEM32(g_eax + 176) = 1; + MEM32(g_eax + 180) = 1; +} + static void crt_splitpath(void) { const char *path = (const char *)ADDR(MEM32(g_esp + 4)); @@ -983,6 +1088,7 @@ recomp_func_t guest_crt_lookup(const char *name) {"strcpy", crt_strcpy}, {"strstr", crt_strstr}, {"time", crt_time}, + {"gmtime", crt_gmtime}, {"asctime", crt_asctime}, {"memcpy", crt_memcpy}, {"memmove", crt_memmove}, {"memset", crt_memset}, {"_initterm", crt_initterm}, {"__dllonexit", crt_dllonexit}, @@ -1010,6 +1116,8 @@ recomp_func_t guest_crt_lookup(const char *name) {"atoi", crt_atoi}, {"setlocale", crt_setlocale}, {"localeconv", crt_localeconv}, + {"_Getdays", crt_getdays}, {"_Getmonths", crt_getmonths}, + {"_Gettnames", crt_gettnames}, {"_splitpath", crt_splitpath}, }; for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i) diff --git a/decompiled/runtime/guest_format.c b/decompiled/runtime/guest_format.c @@ -143,6 +143,78 @@ static void format_output(struct output_buffer *output, const char *format, uint ++output->bytes[digit - 1]; /* The rounded-down final digit is even, so no carry. */ } } + } else if (type == 'g' && bits == 32) { + double value = MEMD(arguments); + arguments += 8; + if (!isfinite(value) || precision > 17) unsupported(start); + int digits = precision < 0 ? 6 : precision ? precision : 1; + char scientific[64]; + snprintf(scientific, sizeof(scientific), "%.17e", value); + int decimal_exponent = atoi(strchr(scientific, 'e') + 1); + int64_t places = (int64_t)digits - 1 - decimal_exponent; + int round_away = 0; + + /* Detect exact halfway cases where MSVCRT rounds away from zero. */ + uint64_t representation; + memcpy(&representation, &value, sizeof(value)); + int exponent = (representation >> 52) & 0x7ff; + uint64_t significand = representation & 0xfffffffffffffull; + if (exponent) significand |= 0x10000000000000ull; + exponent = exponent ? exponent - 1075 : -1074; + if (significand) { + while (!(significand & 1)) { significand >>= 1; ++exponent; } + int64_t remaining = places; + while (remaining < 0 && !(significand % 5)) { significand /= 5; ++remaining; } + round_away = remaining >= 0 && exponent + places == -1 && (significand & 3) == 1; + } + + *end++ = 'g'; + *end = 0; + struct output_buffer number = {NULL, 0}; + append_value(&number, native, 0, precision, value); + number.bytes[number.length] = 0; + char *power = strchr(number.bytes, 'e'); + if (round_away) { + /* Scientific %g can omit trailing zeroes that must become nonzero. */ + if (places < 0) { + unsigned present = 0; + int point = 0; + for (char *p = number.bytes; p < power; ++p) { + present += *p >= '0' && *p <= '9'; + point |= *p == '.'; + } + if (present < (unsigned)digits) { + size_t offset = power - number.bytes, old_length = number.length; + size_t missing = digits - present, added = missing + !point; + for (size_t i = 0; i < added; ++i) append(&number, "0", 1); + memmove(number.bytes + offset + added, number.bytes + offset, old_length - offset); + power = number.bytes + offset + added; + if (!point) number.bytes[offset++] = '.'; + memset(number.bytes + offset, '0', missing); + } + } + char *last = power ? power : number.bytes + number.length; + ++last[-1]; /* The rounded-down digit is even, so there is no carry. */ + number.bytes[number.length] = 0; + } + if (power && strlen(power + 2) == 2) { + /* Legacy MSVCRT uses at least three exponent digits. */ + size_t offset = power - number.bytes + 2; + append(&number, "", 1); + memmove(number.bytes + offset + 1, number.bytes + offset, number.length - offset - 1); + number.bytes[offset] = '0'; + } + + int left = width < 0 || strchr(native, '-') != NULL; + size_t field = width < 0 ? -(int64_t)width : width; + size_t padding = field > number.length ? field - number.length : 0; + int zero = !left && strchr(native, '0') != NULL; + size_t sign = zero && (number.bytes[0] == '+' || number.bytes[0] == '-' || number.bytes[0] == ' '); + if (sign) append(output, number.bytes, 1); + if (!left) for (size_t i = 0; i < padding; ++i) append(output, zero ? "0" : " ", 1); + append(output, number.bytes + sign, number.length - sign); + if (left) for (size_t i = 0; i < padding; ++i) append(output, " ", 1); + free(number.bytes); } else if ((type == 's' || type == 'c') && !wide && bits != 64) { uint32_t value = MEM32(arguments); arguments += 4; diff --git a/decompiled/runtime/linux_file.c b/decompiled/runtime/linux_file.c @@ -3,6 +3,7 @@ #include "linux_file.h" #include <errno.h> #include <fcntl.h> +#include <sys/file.h> #include <sys/stat.h> #include <unistd.h> @@ -48,8 +49,9 @@ static void create_file_locked(void) const char *name = (const char *)ADDR(MEM32(g_esp + 4)); int buffered = MEM32(g_esp + 12) == 1 && MEM32(g_esp + 20) == 3 && MEM32(g_esp + 24) == 0x80; int unbuffered = MEM32(g_esp + 12) == 3 && MEM32(g_esp + 20) == 4 && MEM32(g_esp + 24) == 0x30000000; + int exclusive = MEM32(g_esp + 12) == 0 && MEM32(g_esp + 20) == 3 && MEM32(g_esp + 24) == 0x80000000; if (MEM32(g_esp + 8) != 0x80000000u || MEM32(g_esp + 16) || - (!buffered && !unbuffered) || MEM32(g_esp + 28)) { + (!buffered && !unbuffered && !exclusive) || MEM32(g_esp + 28)) { fprintf(stderr, "Unsupported CreateFileA mode for %s: %08x %x %x %x %x %x\n", name, MEM32(g_esp + 8), MEM32(g_esp + 12), MEM32(g_esp + 16), MEM32(g_esp + 20), MEM32(g_esp + 24), MEM32(g_esp + 28)); @@ -64,7 +66,7 @@ static void create_file_locked(void) char *path = strdup(name); if (!path) abort(); for (char *p = path; *p; ++p) if (*p == '\\') *p = '/'; - int options = O_RDONLY | O_CLOEXEC | O_NONBLOCK | (unbuffered ? O_DIRECT : 0); + int options = O_RDONLY | O_CLOEXEC | O_NONBLOCK | (unbuffered ? O_DIRECT : 0) | (exclusive ? O_SYNC : 0); int descriptor = open(path, options | (unbuffered ? O_CREAT | O_EXCL : 0), 0666); int created = unbuffered && descriptor >= 0; if (unbuffered && descriptor < 0 && errno == EEXIST) descriptor = open(path, options); @@ -82,6 +84,9 @@ static void create_file_locked(void) struct stat info; if (fstat(descriptor, &info)) error = file_error(errno); else if (!S_ISREG(info.st_mode)) error = 5; + /* All supported modes are read-only; shared/exclusive locks enforce sharing. */ + else if (flock(descriptor, (exclusive ? LOCK_EX : LOCK_SH) | LOCK_NB)) + error = errno == EWOULDBLOCK ? 32 : file_error(errno); if (error) close(descriptor); } @@ -102,6 +107,23 @@ static void create_file_locked(void) g_esp += 32; } +static void get_file_size_locked(void) +{ + struct guest_file *handle = find_handle(MEM32(g_esp + 4)); + uint32_t high = MEM32(g_esp + 8); + g_eax = UINT32_MAX; + g_esp += 12; + if (!handle) return; + struct stat info; + if (fstat(handle->descriptor, &info)) { + MEM32(g_fs_base + 0x34) = file_error(errno); + return; + } + g_eax = (uint64_t)info.st_size; + if (high) MEM32(high) = (uint64_t)info.st_size >> 32; + if (g_eax == UINT32_MAX) MEM32(g_fs_base + 0x34) = 0; +} + static void read_file_locked(void) { uint32_t output = MEM32(g_esp + 8), size = MEM32(g_esp + 12); @@ -201,6 +223,7 @@ static void call_serialized(recomp_func_t function) } static void create_file(void) { call_serialized(create_file_locked); } +static void get_file_size(void) { call_serialized(get_file_size_locked); } static void read_file(void) { call_serialized(read_file_locked); } static void set_file_pointer(void) { call_serialized(set_file_pointer_locked); } static void close_file(void) { call_serialized(close_file_locked); } @@ -208,6 +231,7 @@ static void close_file(void) { call_serialized(close_file_locked); } recomp_func_t linux_file_lookup(const char *name) { if (!strcmp(name, "CreateFileA")) return create_file; + if (!strcmp(name, "GetFileSize")) return get_file_size; if (!strcmp(name, "ReadFile")) return read_file; if (!strcmp(name, "SetFilePointer")) return set_file_pointer; if (!strcmp(name, "CloseHandle")) return close_file; diff --git a/decompiled/runtime/linux_imports.c b/decompiled/runtime/linux_imports.c @@ -113,6 +113,17 @@ static void get_proc_address(void) g_esp += 12; } +static void crt_exit(void) +{ + int status = MEM32(g_esp + 4); + /* Run guest termination callbacks before flushing guest FILE buffers. */ + guest_crt_lookup("_cexit")(); + PUSH32(g_esp, 0); + PUSH32(g_esp, RECOMP_RETADDR); + guest_stdio_lookup("fflush")(); + exit(status); +} + static void close_handle(void) { if ((MEM32(g_esp + 4) & 0xff000000u) == 0x05000000u) @@ -181,6 +192,7 @@ recomp_func_t recomp_lookup_import(uint32_t va) } } if (!strcasecmp(imports[current_import].dll, "msvcrt.dll") && imports[current_import].name) { + if (!strcmp(imports[current_import].name, "exit")) return crt_exit; if (!strcmp(imports[current_import].name, "longjmp")) return guest_longjmp; if (!strcmp(imports[current_import].name, "_CxxThrowException")) { extern void native32_shim_CxxThrowException(void); diff --git a/decompiled/src/ddr.dll/lifted-0026.c b/decompiled/src/ddr.dll/lifted-0026.c @@ -3866,8 +3866,13 @@ L_2B060EBF: MEM32(ebp + (-0xC)) = 0x2B0EA490u; /* 0x2B060ED4: mov dword ptr [ebp - 0xc], 0x2b0ea490 */ MEM32(ebp + (-0x8)) = eax; /* 0x2B060EDB: mov dword ptr [ebp - 8], eax */ MEM32(ebp + (-0x4)) = 0; /* 0x2B060EDE: mov dword ptr [ebp - 4], 0 */ +#ifdef RECOMP_PORTABLE + /* The portable client does not restart the Windows launcher. */ + eax = UINT32_MAX; +#else RECOMP_ICALL(MEM32(0x2B0B51C0)); /* 0x2B060EE5: call dword ptr [0x2b0b51c0] */ RECOMP_FLAGS_IN(); +#endif _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(0xCu); _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); esp = esp + 0xCu; /* 0x2B060EEB: add esp, 0xc */ @@ -9676,4 +9681,4 @@ L_2B062260: ebp = POP32_VAL(esp); /* 0x2B062278: pop ebp */ RECOMP_FLAGS_OUT(); esp += 4; { RECOMP_REGS_OUT(); return; } /* 0x2B062279: ret */ RECOMP_REGS_OUT(); -} -\ No newline at end of file +} diff --git a/decompiled/tests/crt_time.c b/decompiled/tests/crt_time.c @@ -0,0 +1,100 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#include <windows.h> +#else +#include "guest_crt.h" +#include "guest_memory.h" +recomp_func_t recomp_lookup(uint32_t address) { abort(); } +recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } +#endif + +int main(void) +{ + const struct { int32_t seconds, fields[9]; const char *text; } cases[] = { + {0, {0, 0, 0, 1, 0, 70, 4, 0, 0}, "Thu Jan 01 00:00:00 1970\n"}, + {86400, {0, 0, 0, 2, 0, 70, 5, 1, 0}, "Fri Jan 02 00:00:00 1970\n"}, + {951782400, {0, 0, 0, 29, 1, 100, 2, 59, 0}, "Tue Feb 29 00:00:00 2000\n"}, + {2147483647, {7, 14, 3, 19, 0, 138, 2, 18, 0}, "Tue Jan 19 03:14:07 2038\n"}, + {-1, {-1, 0, 0, 1, 0, 70, 4, 0, 0}, NULL}, + {-43200, {0, 0, -12, 1, 0, 70, 4, 0, 0}, NULL}, + }; +#ifdef _WIN32 + HMODULE library = LoadLibraryA("msvcrt.dll"); + int32_t *(__cdecl *convert)(const int32_t *) = (void *)GetProcAddress(library, "gmtime"); + char *(__cdecl *format)(const int32_t *) = (void *)GetProcAddress(library, "asctime"); + int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(library, "_errno"); + assert(convert && format && get_errno); +#else + assert(guest_map("CRT time test", 0x50000000, 0x10000)); + g_fs_base = 0x50000000; +#endif + const int32_t *previous = NULL; + for (unsigned i = 0; i < 6; ++i) { +#ifdef _WIN32 + *get_errno() = 777; +#else + MEM32(guest_crt_errno_address()) = 777; + MEM32(0x50000100) = cases[i].seconds; + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = 0x50000100; + g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; +#endif + +#ifdef _WIN32 + const int32_t *fields = convert(&cases[i].seconds); +#else + guest_crt_lookup("gmtime")(); + assert(g_eax && g_esp == 0x5000ff04); + uint32_t address = g_eax; + const int32_t *fields = (void *)ADDR(address); +#endif + + assert(fields && !memcmp(fields, cases[i].fields, 36)); + assert(!previous || previous == fields); + previous = fields; +#ifdef _WIN32 + assert(*get_errno() == 777); + const char *text = format(fields); + int error = *get_errno(); +#else + assert(MEM32(guest_crt_errno_address()) == 777); + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = address; + guest_crt_lookup("asctime")(); + const char *text = g_eax ? (char *)ADDR(g_eax) : NULL; + int error = MEM32(guest_crt_errno_address()); + assert(g_esp == 0x5000ff04); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); +#endif + if (cases[i].text) assert(text && !strcmp(text, cases[i].text) && error == 777); + else assert(!text && error == 22); + } + const int32_t invalid[] = {-43201, INT32_MIN}; + for (unsigned i = 0; i < 2; ++i) { +#ifdef _WIN32 + *get_errno() = 777; +#else + MEM32(guest_crt_errno_address()) = 777; + MEM32(0x50000100) = invalid[i]; + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = 0x50000100; +#endif + +#ifdef _WIN32 + const int32_t *result = convert(&invalid[i]); +#else + guest_crt_lookup("gmtime")(); +#endif + +#ifdef _WIN32 + assert(!result && *get_errno() == 22); +#else + assert(!g_eax && MEM32(guest_crt_errno_address()) == 22); + assert(g_esp == 0x5000ff04); +#endif + } + puts("CRT time: epoch, leap day, 2038 limit, negative boundary, invalid times, x86 layout, stable storage, errno and ABI passed"); +} diff --git a/decompiled/tests/dynamic_imports.c b/decompiled/tests/dynamic_imports.c @@ -1,14 +1,32 @@ #include "linux_imports.h" #include "guest_memory.h" +#include "guest_crt.h" +#include "guest_stdio.h" #include <assert.h> +#include <sys/wait.h> #include <unistd.h> +static uint32_t exit_stream; +static void exit_callback(void) +{ + uint32_t stack = g_esp; + PUSH32(g_esp, exit_stream); + PUSH32(g_esp, 'C'); + PUSH32(g_esp, RECOMP_RETADDR); + guest_stdio_lookup("fputc")(); + g_esp = stack + 4; +} + static void thread_entry(void) { g_eax = MEM32(g_esp + 4) + 1; g_esp += 8; } -recomp_func_t recomp_lookup(uint32_t address) { return address == 0x11001000 ? thread_entry : NULL; } +recomp_func_t recomp_lookup(uint32_t address) +{ + if (address == 0x11002000) return exit_callback; + return address == 0x11001000 ? thread_entry : NULL; +} recomp_func_t linux_module_lookup(const char *name) { (void)name; return NULL; } int linux_module_entry(unsigned index, const char **name, uint32_t *base, uint32_t *size) { return 0; } uint32_t linux_module_export(uint32_t handle, const char *name, uint32_t ordinal) @@ -194,10 +212,50 @@ static void game_exports_return_callable_guest_addresses(void) assert(g_eax == 42 && g_esp == 0x5000ff08); } +static void process_exit_runs_callbacks_and_flushes_guest_files(void) +{ + char path[] = "/tmp/ao-exit-XXXXXX"; + int descriptor = mkstemp(path); + assert(descriptor >= 0); + strcpy((char *)ADDR(0x50002000), path); + strcpy((char *)ADDR(0x50002100), "wb"); + fflush(NULL); + pid_t child = fork(); + assert(child >= 0); + if (!child) { + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = 0x50002000; + MEM32(g_esp + 8) = 0x50002100; + guest_stdio_lookup("fopen")(); + exit_stream = g_eax; + assert(exit_stream); + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = 'B'; + MEM32(g_esp + 8) = exit_stream; + guest_stdio_lookup("fputc")(); + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = 0x11002000; + guest_crt_lookup("_onexit")(); + + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = 37; + recomp_lookup_import(linux_import_bind("msvcrt.dll", "exit", 0))(); + _exit(99); + } + + int status; + assert(waitpid(child, &status, 0) == child); + assert(WIFEXITED(status) && WEXITSTATUS(status) == 37); + char bytes[3]; + assert(read(descriptor, bytes, sizeof(bytes)) == 2 && !memcmp(bytes, "BC", 2)); + assert(!close(descriptor) && !unlink(path)); +} + int main(void) { assert(guest_map("dynamic imports test", 0x50000000u, 0x10000u)); g_fs_base = 0x50004000u; + process_exit_runs_callbacks_and_flushes_guest_files(); window_symbols_survive_library_release(); failed_lookups_report_errors(); import_names_are_owned_and_calls_are_dispatched(); @@ -206,5 +264,6 @@ int main(void) thread_handles_dispatch_through_imports(); game_exports_return_callable_guest_addresses(); puts("dynamic imports: 14 window symbols, stable guest IDs, lifetime, errors, dispatch and ABI passed"); + puts("exit: guest callbacks, buffered output and process status passed"); return 0; } diff --git a/decompiled/tests/files.c b/decompiled/tests/files.c @@ -216,6 +216,70 @@ static void unbuffered_database_reads_use_aligned_guest_buffers(void) assert(!remove("created.dat")); } +static void exclusive_diagnostic_reads(void) +{ + const char *paths[] = {"missing.dat", "fixture.dat", "fixture.dat"}; + uint32_t handles[3]; + const unsigned errors[] = {2, 0, 32}; + for (unsigned i = 0; i < 3; ++i) { +#ifdef _WIN32 + SetLastError(77); +#else + strcpy((char *)ADDR(0x50002000u), paths[i]); + MEM32(g_fs_base + 0x34) = 77; + uint32_t args[] = {0x50002000u, 0x80000000u, 0, 0, 3, 0x80000000u, 0}; +#endif + +#ifdef _WIN32 + handles[i] = (uintptr_t)CreateFileA(paths[i], GENERIC_READ, 0, NULL, + OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL); + unsigned error = GetLastError(); +#else + handles[i] = call("CreateFileA", 7, args); + unsigned error = MEM32(g_fs_base + 0x34); +#endif + + assert(error == errors[i]); + assert((handles[i] == UINT32_MAX) == (i != 1)); + } + check_open("fixture.dat", 32); + check_read(handles[1], 6, "A\0\xff" "B\r\n", 6, 77); + check_close(handles[1], 1); + check_close(check_open("fixture.dat", 0), 1); +} + +static void file_size_reports_bytes_and_invalid_handles(void) +{ + uint32_t handle = check_open("fixture.dat", 0); + for (unsigned invalid = 0; invalid < 2; ++invalid) { + for (unsigned with_high = 0; with_high < 2; ++with_high) { + uint32_t high = 77; +#ifdef _WIN32 + SetLastError(77); +#else + MEM32(g_fs_base + 0x34) = 77; + MEM32(0x50002000u) = high; + uint32_t args[] = {invalid ? UINT32_MAX : handle, with_high ? 0x50002000u : 0}; +#endif + +#ifdef _WIN32 + uint32_t size = GetFileSize((HANDLE)(uintptr_t)(invalid ? UINT32_MAX : handle), + with_high ? (DWORD *)&high : NULL); + unsigned error = GetLastError(); +#else + uint32_t size = call("GetFileSize", 2, args); + unsigned error = MEM32(g_fs_base + 0x34); + high = MEM32(0x50002000u); +#endif + + assert(size == (invalid ? UINT32_MAX : 6)); + assert(error == (invalid ? 6 : 77)); + assert(high == (!invalid && with_high ? 0 : 77)); + } + } + check_close(handle, 1); +} + int main(void) { /* Own a disposable directory and create the binary fixture through native stdio. */ @@ -253,6 +317,8 @@ int main(void) check_close(first, 0); check_close(second, 1); unbuffered_database_reads_use_aligned_guest_buffers(); + exclusive_diagnostic_reads(); + file_size_reports_bytes_and_invalid_handles(); /* The read-only adapter must leave the fixture intact. */ file = fopen("fixture.dat", "rb"); @@ -266,4 +332,5 @@ int main(void) assert(!chdir("/tmp") && !rmdir(directory)); #endif puts("files: shared opens, binary/direct reads, 13 seek cases, independent positions, EOF, alignment, errors, close and guest ABI passed"); + puts("diagnostic files: exclusive read/write-through mode, sharing conflicts, missing files, size and invalid handles passed"); } diff --git a/decompiled/tests/guest_format.c b/decompiled/tests/guest_format.c @@ -136,6 +136,31 @@ int main(void) 0, 0xc00c0000, 0, 0xc0120000}, "4 5 -4 -5"); compare_case("%f", (uint32_t[16]){0, 0x3f800000}, "0.007813"); assert(cases == 480 && digest == 0x401f5575); + compare_case("%.*g %d", (uint32_t[16]){6, 0, 0x3ff40000, 7}, "1.25 7"); + compare_case("%.2g %.2g", (uint32_t[16]){0, 0x3ff40000, 0, 0xbff40000}, "1.3 -1.3"); + compare_case("%g %g", (uint32_t[16]){0, 0, 0, 0x80000000}, "0 -0"); + compare_case("%.0g %.1g", (uint32_t[16]){0, 0x40040000, 0, 0x40230000}, "3 1e+001"); + compare_case("%g %g", (uint32_t[16]){0, 0x412e8480, 0, 0x3eb00000}, "1e+006 9.53674e-007"); + compare_case("[%+012.1g][%-12.1g]", (uint32_t[16]){0, 0x412e8480, 0, 0x412e8480}, "[+000001e+006][1e+006 ]"); + compare_case("%#.6g", (uint32_t[16]){0, 0x3ff40000}, "1.25000"); + compare_case("%.*g", (uint32_t[16]){(uint32_t)-1, 0, 0x3ff40000}, "1.25"); + double general = 1250; + memcpy(floating, &general, sizeof(general)); + compare_case("%.2g", floating, "1.3e+003"); + general = 2.675; + memcpy(floating, &general, sizeof(general)); + compare_case("%.3g", floating, "2.67"); + assert(cases == 640 && digest == 0x686ca2b7); + general = 0.500003814697265625; + memcpy(floating, &general, sizeof(general)); + compare_case("%.17g", floating, "0.50000381469726563"); + general = 1050; + memcpy(floating, &general, sizeof(general)); + compare_case("%.2g", floating, "1.1e+003"); + general = 10.5; + memcpy(floating, &general, sizeof(general)); + compare_case("%.2g", floating, "11"); + assert(cases == 688 && digest == 0x2c8e2b85); printf("printf: %u cases, digest %08x, guest arguments, truncation and canaries passed\n", cases, digest); return 0; } diff --git a/decompiled/tests/locale.c b/decompiled/tests/locale.c @@ -11,6 +11,124 @@ recomp_func_t recomp_lookup(uint32_t address) { abort(); } recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } #endif +static void time_names(const char *name, const char *expected) +{ +#ifdef _WIN32 + HMODULE library = GetModuleHandleA("msvcrt.dll"); + char *(__cdecl *get_names)(void) = (void *)GetProcAddress(library, name); + void (__cdecl *release)(void *) = (void *)GetProcAddress(library, "free"); + int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(library, "_errno"); + assert(get_names && release && get_errno); + *get_errno() = 777; +#else + recomp_func_t get_names = guest_crt_lookup(name); + assert(get_names); + MEM32(guest_crt_errno_address()) = 777; + g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; +#endif + char *names[2]; +#ifndef _WIN32 + uint32_t addresses[2]; +#endif + + for (unsigned i = 0; i < 2; ++i) { +#ifdef _WIN32 + names[i] = get_names(); +#else + g_esp = 0x5000ff00; + get_names(); + addresses[i] = g_eax; + names[i] = g_eax ? (char *)ADDR(g_eax) : NULL; + assert(g_esp == 0x5000ff04); +#endif + } + + assert(names[0] && names[1] && names[0] != names[1]); + assert(!strcmp(names[0], expected) && !strcmp(names[1], expected)); + names[0][0] = '!'; + assert(names[1][0] == ':'); +#ifdef _WIN32 + assert(*get_errno() == 777); + release(names[0]); + release(names[1]); +#else + assert(MEM32(guest_crt_errno_address()) == 777); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); + for (unsigned i = 0; i < 2; ++i) { + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = addresses[i]; + guest_crt_lookup("free")(); + assert(g_esp == 0x5000ff04); + } +#endif +} + +static void time_data(void) +{ + const char *expected[] = { + "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", + "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", + "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", + "January", "February", "March", "April", "May", "June", "July", + "August", "September", "October", "November", "December", + "AM", "PM", "MM/dd/yy", "dddd, MMMM dd, yyyy", "HH:mm:ss", + }; +#ifdef _WIN32 + HMODULE library = GetModuleHandleA("msvcrt.dll"); + void *(__cdecl *get_names)(void) = (void *)GetProcAddress(library, "_Gettnames"); + void (__cdecl *release)(void *) = (void *)GetProcAddress(library, "free"); + int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(library, "_errno"); + assert(get_names && release && get_errno); + *get_errno() = 777; +#else + MEM32(guest_crt_errno_address()) = 777; + g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; + uint32_t addresses[2]; +#endif + uint32_t *tables[2]; + char *strings[2][43]; + + for (unsigned i = 0; i < 2; ++i) { +#ifdef _WIN32 + tables[i] = get_names(); +#else + g_esp = 0x5000ff00; + guest_crt_lookup("_Gettnames")(); + assert(g_eax && g_esp == 0x5000ff04); + addresses[i] = g_eax; + tables[i] = (void *)ADDR(g_eax); +#endif + } + + assert(tables[0] && tables[1] && tables[0] != tables[1]); + for (unsigned i = 0; i < 2; ++i) { + for (unsigned j = 0; j < 43; ++j) { +#ifdef _WIN32 + strings[i][j] = (char *)(uintptr_t)tables[i][j]; +#else + strings[i][j] = (char *)ADDR(tables[i][j]); +#endif + assert(!strcmp(strings[i][j], expected[j])); + } + assert(tables[i][43] == 1033 && tables[i][44] == 1 && tables[i][45] == 1); + } + strings[0][0][0] = '!'; + assert(!strcmp(strings[1][0], "Sun")); +#ifdef _WIN32 + assert(*get_errno() == 777); + release(tables[0]); + release(tables[1]); +#else + assert(MEM32(guest_crt_errno_address()) == 777); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); + for (unsigned i = 0; i < 2; ++i) { + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = addresses[i]; + guest_crt_lookup("free")(); + } +#endif +} + static void conversion_data(void) { #ifdef _WIN32 @@ -96,6 +214,11 @@ int main(void) } } conversion_data(); + time_names("_Getdays", ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday"); + time_names("_Getmonths", ":Jan:January:Feb:February:Mar:March:Apr:April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:October:Nov:November:Dec:December"); + time_data(); puts("locale: six C-locale categories, queries, invalid categories, errno and guest ABI passed"); puts("localeconv: VC6 48-byte layout, ten strings, eight monetary fields, stable storage and errno passed"); + puts("_Getdays/_Getmonths: C-locale names, independent writable allocations, free, errno and guest ABI passed"); + puts("_Gettnames: 43 C-locale strings, x86 table layout, independent storage, free, errno and guest ABI passed"); } diff --git a/docs/linux-import-coverage.json b/docs/linux-import-coverage.json @@ -1,5 +1,5 @@ { - "executable_sha256": "397c8d120945e9234652306e32e7c97862246d879f7459c70e0b5b60e798c835", + "executable_sha256": "1631f15698a0c91199ac19e585b1c0a23fa90299b9285636585640b0fc4f2411", "imports": [ { "dll": "KERNEL32.dll", @@ -329,7 +329,7 @@ "dll": "KERNEL32.dll", "name": "GetFileSize", "ordinal": 0, - "adapter_found": false + "adapter_found": true }, { "dll": "KERNEL32.dll", @@ -689,7 +689,7 @@ "dll": "MSVCRT.dll", "name": "exit", "ordinal": 0, - "adapter_found": false + "adapter_found": true }, { "dll": "MSVCRT.dll", @@ -1487,13 +1487,13 @@ "dll": "MSVCRT.dll", "name": "asctime", "ordinal": 0, - "adapter_found": false + "adapter_found": true }, { "dll": "MSVCRT.dll", "name": "gmtime", "ordinal": 0, - "adapter_found": false + "adapter_found": true }, { "dll": "MSVCRT.dll", @@ -2375,19 +2375,19 @@ "dll": "MSVCRT.dll", "name": "_Gettnames", "ordinal": 0, - "adapter_found": false + "adapter_found": true }, { "dll": "MSVCRT.dll", "name": "_Getdays", "ordinal": 0, - "adapter_found": false + "adapter_found": true }, { "dll": "MSVCRT.dll", "name": "_Getmonths", "ordinal": 0, - "adapter_found": false + "adapter_found": true }, { "dll": "MSVCRT.dll", diff --git a/tools/reconstruction/test_portable_crt_time.py b/tools/reconstruction/test_portable_crt_time.py @@ -0,0 +1,22 @@ +"""Compare guest UTC time conversion with Windows MSVCRT.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-crt-time" +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/crt_time.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/crt-time"], check=True) +actual = subprocess.check_output(wsl + ["./build/linux/crt-time"], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux contract: " + actual.strip()) +