aorecomp

Anarchy Online classic in the browser

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


commit fd9e2a9b15caf856f893bb7f8045db8b5f332269
parent 60af6e70b6f2d099c933812692dcc4f5e139ee2c
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 01:04:47 +0000

Advance native Linux startup through preference reads to graphics discovery

Diffstat:
Mdecompiled/Makefile.linux | 12+++++++++++-
Mdecompiled/runtime/guest_crt.c | 16++++++++++++++++
Mdecompiled/runtime/guest_stdio.c | 145++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Adecompiled/tests/stdio_input.c | 257+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adecompiled/tests/stricmp.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_stdio_input.py | 27+++++++++++++++++++++++++++
Atools/reconstruction/test_portable_stricmp.py | 26++++++++++++++++++++++++++
7 files changed, 536 insertions(+), 5 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -96,6 +96,10 @@ build/linux/new-handler: tests/new_handler.c runtime/guest_crt.c runtime/guest_h mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ +build/linux/stricmp: tests/stricmp.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 + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ + build/linux/platform-diagnostics: tests/platform_diagnostics.c runtime/linux_diagnostics.c runtime/linux_platform.c runtime/guest_crt.c runtime/guest_msvcp.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c src/ffr.dll/lifted-0002.c data/ffr_dll.c runtime/recomp_types.h runtime/platform_diagnostics.h runtime/linux_platform.h runtime/guest_crt.h runtime/guest_msvcp.h runtime/guest_memory.h runtime/guest_heap.h mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ @@ -108,6 +112,10 @@ build/linux/guest-stdio: tests/guest_stdio.c runtime/guest_stdio.c runtime/guest mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ +build/linux/stdio-input: tests/stdio_input.c runtime/guest_stdio.c runtime/guest_crt.c runtime/guest_format.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_stdio.h runtime/guest_crt.h runtime/guest_format.h runtime/guest_heap.h runtime/guest_memory.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ + build/linux/chmod: tests/chmod.c runtime/guest_stdio.c runtime/guest_crt.c runtime/guest_format.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_stdio.h runtime/guest_crt.h runtime/guest_format.h runtime/guest_heap.h runtime/guest_memory.h mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ @@ -124,7 +132,7 @@ build/linux/create-directory: tests/create_directory.c runtime/linux_platform.c mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@ -test: build/linux/guest-stat build/linux/chmod build/linux/create-directory build/linux/dynamic-imports build/linux/linecrypt build/linux/guest-heap build/linux/guest-crt build/linux/linux-platform build/linux/msvcp-strings build/linux/ace-token build/linux/msvcp-stdio-data build/linux/guest-eh build/linux/guest-rand build/linux/semaphore build/linux/guest-scanf build/linux/network-startup build/linux/os-version build/linux/guest-format build/linux/event build/linux/tls build/linux/module-filename build/linux/delete-file build/linux/guest-stdio build/linux/guest-exception build/linux/platform-diagnostics build/linux/new-handler +test: build/linux/stricmp build/linux/stdio-input build/linux/guest-stat build/linux/chmod build/linux/create-directory build/linux/dynamic-imports build/linux/linecrypt build/linux/guest-heap build/linux/guest-crt build/linux/linux-platform build/linux/msvcp-strings build/linux/ace-token build/linux/msvcp-stdio-data build/linux/guest-eh build/linux/guest-rand build/linux/semaphore build/linux/guest-scanf build/linux/network-startup build/linux/os-version build/linux/guest-format build/linux/event build/linux/tls build/linux/module-filename build/linux/delete-file build/linux/guest-stdio build/linux/guest-exception build/linux/platform-diagnostics build/linux/new-handler ./build/linux/linecrypt ./build/linux/guest-heap ./build/linux/guest-crt @@ -151,5 +159,7 @@ test: build/linux/guest-stat build/linux/chmod build/linux/create-directory buil ./build/linux/create-directory ./build/linux/chmod ./build/linux/guest-stat + ./build/linux/stdio-input + ./build/linux/stricmp -include $(OBJECTS:.o=.d) diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c @@ -464,6 +464,21 @@ static void crt_strrchr(void) g_esp += 4; } +static void crt_stricmp(void) +{ + const unsigned char *left = (const unsigned char *)ADDR(MEM32(g_esp + 4)); + const unsigned char *right = (const unsigned char *)ADDR(MEM32(g_esp + 8)); + unsigned a, b; + /* The guest CRT currently supports the initial C locale only. */ + do { + a = *left++; b = *right++; + if (a >= 'A' && a <= 'Z') a += 'a' - 'A'; + if (b >= 'A' && b <= 'Z') b += 'a' - 'A'; + } while (a && a == b); + g_eax = (uint32_t)((a > b) - (a < b)); + g_esp += 4; +} + static void crt_strcmp(void) { g_eax = (uint32_t)strcmp((const char *)ADDR(MEM32(g_esp + 4)), @@ -664,6 +679,7 @@ recomp_func_t guest_crt_lookup(const char *name) {"floor", crt_floor}, {"_EH_prolog", crt_eh_prolog}, {"strlen", crt_strlen}, {"strcmp", crt_strcmp}, + {"_stricmp", crt_stricmp}, {"strrchr", crt_strrchr}, {"_mbsrchr", crt_strrchr}, /* Initial single-byte locale. */ {"memcmp", crt_memcmp}, {"memchr", crt_memchr}, {"sscanf", crt_sscanf}, diff --git a/decompiled/runtime/guest_stdio.c b/decompiled/runtime/guest_stdio.c @@ -80,10 +80,9 @@ static int flush_stream(struct stream *stream) uint32_t file = stream->file, flags = MEM32(file + 12); if (stream->descriptor < 0) return file_error(EBADF); if (!(flags & 2)) { - if (MEM32(file + 4)) { - fprintf(stderr, "Flushing buffered guest input is not implemented\n"); - abort(); - } + /* Legacy fflush discards unread bytes without moving the descriptor. */ + MEM32(file) = MEM32(file + 8); + MEM32(file + 4) = 0; return 0; } uint32_t base = MEM32(file + 8), pointer = MEM32(file); @@ -313,6 +312,142 @@ static void print_file(int list) g_esp += 4; } +static int64_t stream_position(struct stream *stream) +{ + if (stream->descriptor < 0) return file_error(EBADF); + off_t position = lseek(stream->descriptor, 0, SEEK_CUR); + if (position < 0) return file_error(errno); + uint32_t file = stream->file, flags = MEM32(file + 12); + if (flags & 1) { + if (stream->text && MEM32(file + 4)) { + fprintf(stderr, "Positioning buffered guest text input is not implemented\n"); + abort(); + } + position -= MEM32(file + 4); + } else if ((flags & 2) && !(flags & 4)) { + uint32_t start = MEM32(file + 8), end = MEM32(file); + position += end - start; + if (stream->text) + for (uint32_t p = start; p < end; ++p) if (MEM8(p) == '\n') ++position; + } + return position; +} + +static void crt_ftell(void) +{ + int64_t position = stream_position(find_stream(MEM32(g_esp + 4))); + g_eax = position > INT32_MAX ? file_error(EINVAL) : (uint32_t)position; + g_esp += 4; +} + +static void crt_fseek(void) +{ + struct stream *stream = find_stream(MEM32(g_esp + 4)); + int64_t offset = (int32_t)MEM32(g_esp + 8); + uint32_t origin = MEM32(g_esp + 12), file = stream->file; + if (origin > 2) { g_eax = file_error(EINVAL); g_esp += 4; return; } + if (origin == SEEK_CUR) { + int64_t position = stream_position(stream); + if (position < 0) { g_eax = UINT32_MAX; g_esp += 4; return; } + offset += position; + origin = SEEK_SET; + } + MEM32(file + 12) &= ~0x10u; /* A seek clears EOF, including failed seeks. */ + if (flush_stream(stream)) g_eax = UINT32_MAX; + else { + if (MEM32(file + 12) & 0x80) MEM32(file + 12) &= ~3u; + else if ((MEM32(file + 12) & 9) == 9 && !(MEM32(file + 12) & 0x400)) + MEM32(file + 24) = 512; + g_eax = lseek(stream->descriptor, offset, origin) < 0 ? file_error(errno) : 0; + } + g_esp += 4; +} + +static int fill_input(struct stream *stream) +{ + uint32_t file = stream->file, flags = MEM32(file + 12); + MEM32(file + 4) = 0; + if (stream->descriptor < 0 || !(flags & (1 | 0x80)) || (flags & 2)) { + MEM32(file + 12) |= 0x20; + return file_error(EBADF); + } + if (flags & 0x10) return -1; + if (stream->text) { + fprintf(stderr, "Guest text input translation is not implemented\n"); + abort(); + } + MEM32(file + 12) |= 1; + if (!MEM32(file + 8)) { + uint32_t buffer = guest_malloc(4096); + if (!buffer) return file_error(ENOMEM); + MEM32(file + 8) = buffer; + MEM32(file + 24) = 4096; + MEM32(file + 12) |= 8; + } + MEM32(file) = MEM32(file + 8); + ssize_t count; + do count = read(stream->descriptor, (void *)ADDR(MEM32(file)), flags & 4 ? 1 : MEM32(file + 24)); + while (count < 0 && errno == EINTR); + if (count <= 0) { + MEM32(file + 12) |= count < 0 ? 0x20 : 0x10; + return count < 0 ? file_error(errno) : -1; + } + MEM32(file + 4) = count - 1; + int character = MEM8(MEM32(file)); + ++MEM32(file); + return character; +} + +static void crt_filbuf(void) +{ + g_eax = fill_input(find_stream(MEM32(g_esp + 4))); + g_esp += 4; +} + +static void crt_fgetc(void) +{ + uint32_t file = MEM32(g_esp + 4); + struct stream *stream = find_stream(file); + if ((MEM32(file + 12) & 1) && (int32_t)MEM32(file + 4) > 0) { + --MEM32(file + 4); + g_eax = MEM8(MEM32(file)); + ++MEM32(file); + } else g_eax = fill_input(stream); + g_esp += 4; +} + +static void crt_fread(void) +{ + uint32_t output = MEM32(g_esp + 4), size = MEM32(g_esp + 8), count = MEM32(g_esp + 12); + if (!size || !count) { g_eax = 0; g_esp += 4; return; } + uint64_t total = (uint64_t)size * count, copied = 0; + if (total > UINT32_MAX) { file_error(EINVAL); g_eax = 0; g_esp += 4; return; } + struct stream *stream = find_stream(MEM32(g_esp + 16)); + uint32_t file = stream->file; + if (!(MEM32(file + 12) & (1 | 0x80)) || (MEM32(file + 12) & 2)) { + fill_input(stream); + g_eax = 0; + g_esp += 4; + return; + } + while (copied < total) { + if ((int32_t)MEM32(file + 4) > 0) { + uint32_t available = MEM32(file + 4); + if (available > total - copied) available = total - copied; + memcpy((void *)ADDR(output + copied), (const void *)ADDR(MEM32(file)), available); + MEM32(file) += available; + MEM32(file + 4) -= available; + copied += available; + } else { + int character = fill_input(stream); + if (character < 0) break; + MEM8(output + copied++) = character; + } + } + g_eax = copied / size; + g_esp += 4; +} + static void crt_chmod(void) { const char *name = (const char *)ADDR(MEM32(g_esp + 4)); @@ -410,6 +545,8 @@ recomp_func_t guest_stdio_lookup(const char *name) {"fputs", crt_fputs}, {"setvbuf", crt_setvbuf}, {"_fileno", crt_fileno}, {"fprintf", crt_fprintf}, {"vfprintf", crt_vfprintf}, {"_chmod", crt_chmod}, {"_stat", crt_stat}, + {"ftell", crt_ftell}, {"fseek", crt_fseek}, {"fread", crt_fread}, + {"fgetc", crt_fgetc}, {"getc", crt_fgetc}, {"_filbuf", crt_filbuf}, }; for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i) if (!strcmp(name, functions[i].name)) return functions[i].function; diff --git a/decompiled/tests/stdio_input.c b/decompiled/tests/stdio_input.c @@ -0,0 +1,257 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#ifdef _WIN32 +#include <windows.h> +static HMODULE crt; +#else +#include "guest_stdio.h" +#include "guest_crt.h" +#include "guest_memory.h" +#include <unistd.h> +recomp_func_t recomp_lookup(uint32_t address) { fprintf(stderr, "Unexpected stdio guest callback %08x\n", address); abort(); } +recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } +#endif + +static void *memory(uint32_t address) +{ +#ifdef _WIN32 + return (void *)(uintptr_t)address; +#else + return (void *)ADDR(address); +#endif +} + +static uint32_t address(const void *bytes, unsigned size) +{ +#ifdef _WIN32 + return (uintptr_t)bytes; +#else + static uint32_t next = 0x50004000u; + uint32_t result = next; + memcpy(memory(result), bytes, size); + next += (size + 3) & ~3u; + assert(next < 0x5000e000u); + return result; +#endif +} + +static uint32_t string(const char *text) { return address(text, strlen(text) + 1); } + +static uint32_t call(const char *name, unsigned count, const uint32_t *arguments) +{ +#ifdef _WIN32 + uint32_t args[4] = {0}; + if (count) memcpy(args, arguments, count * 4); + uint32_t (__cdecl *function)(uint32_t, uint32_t, uint32_t, uint32_t) = (void *)GetProcAddress(crt, name); + assert(function); + return function(args[0], args[1], args[2], args[3]); +#else + g_esp = 0x5000ff00u; + if (count) memcpy(memory(g_esp + 4), arguments, count * 4); + g_ebx = 0x12345678; + g_esi = 0x23456789; + g_edi = 0x3456789a; + g_ebp = 0x456789ab; + recomp_func_t function = guest_stdio_lookup(name); + if (!function) function = guest_crt_lookup(name); + assert(function); + function(); + assert(g_esp == 0x5000ff04u); + assert(g_ebx == 0x12345678 && g_esi == 0x23456789); + assert(g_edi == 0x3456789a && g_ebp == 0x456789ab); + return g_eax; +#endif +} + +static uint32_t open_file(const char *name, const char *mode) +{ + uint32_t args[] = {string(name), string(mode)}; + return call("fopen", 2, args); +} + +static void check_contents(const char *path, const void *expected, unsigned size) +{ + unsigned char bytes[256]; + FILE *file = fopen(path, "rb"); + assert(file); + size_t count = fread(bytes, 1, sizeof(bytes), file); + assert(!fclose(file)); + if (count != size || memcmp(bytes, expected, size)) { + printf("File %s: %u bytes, expected %u\n", path, (unsigned)count, size); + fflush(stdout); + } + assert(count == size && !memcmp(bytes, expected, size)); +} + +static void binary_position_and_direct_buffer_consumption(void) +{ + uint32_t file = open_file("input.bin", "rb"); + uint32_t seek[] = {file, 0, 2}; + uint32_t *fields = memory(file); + + assert(call("ftell", 1, &file) == 0); + assert(call("fseek", 3, seek) == 0); + assert(call("ftell", 1, &file) == 6); + seek[1] = -2; seek[2] = 1; + assert(call("fseek", 3, seek) == 0); + assert(call("fgetc", 1, &file) == 'e'); + assert(call("ftell", 1, &file) == 5); + seek[1] = 1; seek[2] = 0; + assert(call("fseek", 3, seek) == 0); + assert(call("fgetc", 1, &file) == 'b'); + assert(fields[1] == 4 && *(unsigned char *)memory(fields[0]) == 'c'); + ++fields[0]; + --fields[1]; + uint32_t position = call("ftell", 1, &file); + uint32_t next = call("getc", 1, &file); + + assert(position == 3 && next == 'd'); + assert(call("ftell", 1, &file) == 4); + assert(call("fclose", 1, &file) == 0); +} + +static void partial_elements_and_eof_reset(void) +{ + uint32_t file = open_file("input.bin", "rb"); + unsigned char storage[12]; memset(storage, 0xa5, sizeof(storage)); + uint32_t bytes = address(storage, sizeof(storage)); + uint32_t read[] = {bytes, 4, 2, file}; + uint32_t seek[] = {file, 0, 0}; + uint32_t *fields = memory(file); + + uint32_t count = call("fread", 4, read); + + assert(count == 1 && !memcmp(memory(bytes), "abcdef", 6)); + assert(((unsigned char *)memory(bytes))[6] == 0xa5 && (fields[3] & 0x10)); + assert(call("ftell", 1, &file) == 6 && call("fgetc", 1, &file) == UINT32_MAX); + assert(call("fseek", 3, seek) == 0 && !(fields[3] & 0x10)); + assert(call("_filbuf", 1, &file) == 'a'); + assert(call("fflush", 1, &file) == 0 && call("ftell", 1, &file) == 6); + assert(call("fgetc", 1, &file) == UINT32_MAX); + assert(call("fclose", 1, &file) == 0); +} + +static void update_stream_changes_direction_after_seek(void) +{ + uint32_t file = open_file("update.bin", "w+b"); + uint32_t write[] = {string("abcdef"), 1, 6, file}; + uint32_t seek[] = {file, 2, 0}; + unsigned char storage[8] = {0}; + uint32_t bytes = address(storage, sizeof(storage)); + uint32_t read[] = {bytes, 1, 6, file}; + uint32_t put[] = {'Z', file}; + + assert(call("fwrite", 4, write) == 6 && call("ftell", 1, &file) == 6); + assert(call("fseek", 3, seek) == 0 && call("fgetc", 1, &file) == 'c'); + seek[1] = -1; seek[2] = 1; + assert(call("fseek", 3, seek) == 0 && call("fputc", 2, put) == 'Z'); + seek[1] = 0; seek[2] = 0; + assert(call("fseek", 3, seek) == 0 && call("fread", 4, read) == 6); + + assert(!memcmp(memory(bytes), "abZdef", 6)); + assert(call("fclose", 1, &file) == 0); + check_contents("update.bin", "abZdef", 6); +} + +static void text_output_position_includes_crlf(void) +{ + uint32_t file = open_file("text.txt", "wt"); + uint32_t write[] = {string("a\nb"), 1, 3, file}; + uint32_t seek[] = {file, 1, 0}; + uint32_t put[] = {'X', file}; + + assert(call("fwrite", 4, write) == 3); + uint32_t position = call("ftell", 1, &file); + assert(call("fseek", 3, seek) == 0 && call("fputc", 2, put) == 'X'); + assert(call("fclose", 1, &file) == 0); + + assert(position == 4); + check_contents("text.txt", "aX\nb", 4); +} + +static void binary_reads_cross_buffer_boundaries(void) +{ + unsigned char expected[9000], storage[9002]; + for (unsigned i = 0; i < sizeof(expected); ++i) expected[i] = i % 256; + FILE *native = fopen("large.bin", "wb"); + assert(native && fwrite(expected, 1, sizeof(expected), native) == sizeof(expected) && !fclose(native)); + memset(storage, 0xa5, sizeof(storage)); + uint32_t bytes = address(storage, sizeof(storage)); + uint32_t file = open_file("large.bin", "rb"); + uint32_t read[] = {bytes, 7, 1286, file}; + + uint32_t count = call("fread", 4, read); + + assert(count == 1285 && !memcmp(memory(bytes), expected, sizeof(expected))); + assert(((unsigned char *)memory(bytes))[9000] == 0xa5); + assert(call("ftell", 1, &file) == 9000); + assert(call("fclose", 1, &file) == 0 && !remove("large.bin")); +} + +static void input_buffers_and_invalid_operations(void) +{ + uint32_t file = open_file("input.bin", "rb"); + unsigned char storage[16] = {0}; + uint32_t buffer = address(storage, sizeof(storage)); + uint32_t set[] = {file, buffer, 0, 4}; + uint32_t seek[] = {file, -1, 0}; + uint32_t read[] = {buffer + 8, 0, 2, file}; + + assert(call("setvbuf", 4, set) == 0); + for (unsigned i = 0; i < 6; ++i) assert(call("fgetc", 1, &file) == 'a' + i); + assert(call("fseek", 3, seek) == UINT32_MAX); + assert(*(uint32_t *)memory(call("_errno", 0, NULL)) == 22); + seek[1] = 0; seek[2] = 99; + assert(call("fseek", 3, seek) == UINT32_MAX); + assert(call("fread", 4, read) == 0); + + assert(call("ftell", 1, &file) == 6); + assert(call("fclose", 1, &file) == 0); + assert(call("ftell", 1, &file) == UINT32_MAX); + assert(*(uint32_t *)memory(call("_errno", 0, NULL)) == 9); + file = open_file("input.bin", "rb"); + set[0] = file; set[1] = 0; set[2] = 4; set[3] = 0; + assert(call("setvbuf", 4, set) == 0 && call("fgetc", 1, &file) == 'a'); + assert(call("ftell", 1, &file) == 1 && call("fclose", 1, &file) == 0); + file = open_file("write-only.bin", "wb"); + read[1] = read[2] = 1; read[3] = file; + assert(call("fread", 4, read) == 0 && (((uint32_t *)memory(file))[3] & 0x20)); + assert(call("fclose", 1, &file) == 0); +} + +int main(void) +{ +#ifdef _WIN32 + crt = LoadLibraryA("msvcrt.dll"); + assert(crt); + char temporary[MAX_PATH], directory[MAX_PATH]; + assert(GetTempPathA(sizeof(temporary), temporary)); + assert(GetTempFileNameA(temporary, "asi", 0, directory)); + assert(DeleteFileA(directory) && CreateDirectoryA(directory, NULL) && SetCurrentDirectoryA(directory)); +#else + assert(guest_map("stdio input test", 0x50000000u, 0x10000u)); + g_fs_base = 0x50001000u; + char directory[] = "/tmp/ao-input-test-XXXXXX"; + assert(mkdtemp(directory) && !chdir(directory)); +#endif + FILE *file = fopen("input.bin", "wb"); + assert(file && fwrite("abcdef", 1, 6, file) == 6 && !fclose(file)); + binary_position_and_direct_buffer_consumption(); + partial_elements_and_eof_reset(); + update_stream_changes_direction_after_seek(); + text_output_position_includes_crlf(); + input_buffers_and_invalid_operations(); + binary_reads_cross_buffer_boundaries(); + assert(!remove("input.bin") && !remove("update.bin") && !remove("text.txt") && !remove("write-only.bin")); +#ifdef _WIN32 + assert(SetCurrentDirectoryA(temporary) && RemoveDirectoryA(directory)); +#else + assert(!chdir("/tmp") && !rmdir(directory)); +#endif + puts("stdio input: positions, binary reads, direct buffers, partial elements, EOF, update streams, text output and ABI passed"); + return 0; +} + diff --git a/decompiled/tests/stricmp.c b/decompiled/tests/stricmp.c @@ -0,0 +1,58 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#include <windows.h> +static int (__cdecl *compare)(const char *, const char *); +#else +#include "guest_crt.h" +#include "guest_memory.h" +recomp_func_t recomp_lookup(uint32_t address) { (void)address; abort(); } +recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } +#endif + +static int call(const char *left, const char *right) +{ +#ifdef _WIN32 + return compare(left, right); +#else + strcpy((char *)ADDR(0x50000100u), left); + strcpy((char *)ADDR(0x50000200u), right); + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = 0x50000100u; + MEM32(g_esp + 8) = 0x50000200u; + g_ebx = 0x12345678u; g_esi = 0x23456789u; + g_edi = 0x3456789au; g_ebp = 0x456789abu; + guest_crt_lookup("_stricmp")(); + assert(g_esp == 0x5000ff04u); + assert(g_ebx == 0x12345678u && g_esi == 0x23456789u); + assert(g_edi == 0x3456789au && g_ebp == 0x456789abu); + return (int32_t)g_eax; +#endif +} + +int main(void) +{ +#ifdef _WIN32 + compare = (void *)GetProcAddress(LoadLibraryA("msvcrt.dll"), "_stricmp"); + assert(compare); +#else + assert(guest_map("stricmp test", 0x50000000u, 0x10000u)); +#endif + uint32_t digest = 2166136261u; + + for (unsigned a = 0; a < 256; ++a) + for (unsigned b = 0; b < 256; ++b) { + char left[] = {'A', (char)a, 'B', 0}; + char right[] = {'a', (char)b, 'b', 0}; + digest = (digest ^ (uint32_t)call(left, right)) * 16777619u; + } + + assert(call("ChatFontSize", "chatfontsize") == 0); + assert(call("", "A") == -1 && call("Z", "a") == 1); + assert(call("a_", "a[") == 1 && call("abc", "ABCD") == -1); + assert(digest == 0x381d58d1u); + printf("stricmp: 65536 byte pairs, digest %08x, C-locale prefixes and ABI passed\n", digest); + return 0; +} diff --git a/tools/reconstruction/test_portable_stdio_input.py b/tools/reconstruction/test_portable_stdio_input.py @@ -0,0 +1,27 @@ +"""Compare guest stream positioning and binary input with Windows x86 MSVCRT.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-stdio-input" +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/stdio_input.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:] +linux_output = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] + "/translated" +wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] +subprocess.run(wsl + [ + "gcc", "-O2", "-DRECOMP_PORTABLE", "-Iruntime", "-fno-strict-aliasing", + "tests/stdio_input.c", "runtime/guest_stdio.c", "runtime/guest_crt.c", "runtime/guest_format.c", + "runtime/guest_heap.c", "runtime/guest_memory.c", "runtime/recomp_state.c", + "-lm", "-o", linux_output, +], check=True) +actual = subprocess.check_output(wsl + [linux_output], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux: " + actual.strip()) diff --git a/tools/reconstruction/test_portable_stricmp.py b/tools/reconstruction/test_portable_stricmp.py @@ -0,0 +1,26 @@ +"""Compare guest C-locale case-insensitive comparisons with Windows x86 MSVCRT.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-stricmp" +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/stricmp.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:] +linux_output = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] + "/translated" +wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] +subprocess.run(wsl + [ + "gcc", "-O2", "-DRECOMP_PORTABLE", "-Iruntime", "-fno-strict-aliasing", + "tests/stricmp.c", "runtime/guest_crt.c", "runtime/guest_heap.c", + "runtime/guest_memory.c", "runtime/recomp_state.c", "-lm", "-o", linux_output, +], check=True) +actual = subprocess.check_output(wsl + [linux_output], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux: " + actual.strip())