aorecomp

Anarchy Online classic in the browser

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


commit 6e25001c5c8ddbf479744880d54b335caf46618c
parent daacf8598a2ae410596952bb68276d7096288a40
Author: anon <anon@example.com>
Date:   Wed, 14 Oct 2026 23:51:21 +0000

Advance Linux startup through error handling and log deletion

Diffstat:
Mdecompiled/Makefile.linux | 12+++++++++++-
Mdecompiled/runtime/guest_crt.c | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/runtime/linux_platform.c | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adecompiled/tests/delete_file.c | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/tests/guest_crt.c | 113+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/tests/linux_platform.c | 22++++++++++++++++++++++
Adecompiled/tests/module_filename.c | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_delete_file.py | 26++++++++++++++++++++++++++
Atools/reconstruction/test_portable_module_filename.py | 26++++++++++++++++++++++++++
9 files changed, 516 insertions(+), 1 deletion(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -84,7 +84,15 @@ build/linux/tls: tests/tls.c runtime/linux_tls.c runtime/guest_memory.c runtime/ mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -o $@ -test: 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: tests/module_filename.c runtime/linux_platform.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/linux_platform.h runtime/guest_memory.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@ + +build/linux/delete-file: tests/delete_file.c runtime/linux_platform.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/linux_platform.h runtime/guest_memory.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@ + +test: 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/linecrypt ./build/linux/guest-heap ./build/linux/guest-crt @@ -101,5 +109,7 @@ test: build/linux/linecrypt build/linux/guest-heap build/linux/guest-crt build/l ./build/linux/guest-format ./build/linux/event ./build/linux/tls + ./build/linux/module-filename + ./build/linux/delete-file -include $(OBJECTS:.o=.d) diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c @@ -7,6 +7,81 @@ static _Thread_local uint32_t random_state = 1; static uint32_t user_matherr; static uint32_t *exit_callbacks; static size_t exit_callback_count; +static int error_mode; +static uint32_t abort_handler; + +static uint32_t errno_address(void) +{ + /* Private CRT storage inside the host-owned 64 KiB guest thread block. */ + return g_fs_base + 0x2000; +} + +static void crt_errno(void) +{ + g_eax = errno_address(); + g_esp += 4; +} + +static void crt_set_error_mode(void) +{ + int mode = (int32_t)MEM32(g_esp + 4); + g_eax = error_mode; + if (mode >= 0 && mode <= 2) error_mode = mode; + else if (mode != 3) { + MEM32(errno_address()) = 22; /* MSVCRT EINVAL */ + g_eax = UINT32_MAX; + } + g_esp += 4; +} + +static void require_abort_signal(void) +{ + if (MEM32(g_esp + 4) != 22) { + fprintf(stderr, "Unsupported guest CRT signal %u\n", MEM32(g_esp + 4)); + abort(); + } +} + +static void crt_signal(void) +{ + require_abort_signal(); + g_eax = abort_handler; + abort_handler = MEM32(g_esp + 8); + g_esp += 4; +} + +static void deliver_abort_signal(void) +{ + if (!abort_handler) abort(); + if (abort_handler == 1) return; /* SIG_IGN */ + uint32_t handler = abort_handler, caller = g_cur_func, stack = g_esp; + abort_handler = 0; /* MSVCRT resets the handler before synchronous delivery. */ + recomp_func_t function = recomp_lookup(handler); + if (!function) function = recomp_lookup_import(handler); + PUSH32(g_esp, 22); + PUSH32(g_esp, RECOMP_RETADDR); + function(); + if (g_esp != stack - 4) { + fprintf(stderr, "Guest SIGABRT handler returned with an unbalanced stack\n"); + abort(); + } + g_esp = stack; + g_cur_func = caller; +} + +static void crt_raise(void) +{ + require_abort_signal(); + deliver_abort_signal(); + g_eax = 0; + g_esp += 4; +} + +static void crt_abort(void) +{ + deliver_abort_signal(); + abort(); /* abort terminates even when the handler returns or ignores SIGABRT. */ +} uint32_t guest_crt_data(const char *name) { @@ -328,6 +403,15 @@ static void crt_strlen(void) g_esp += 4; } +static void crt_strrchr(void) +{ + uint32_t start = MEM32(g_esp + 4); + const char *text = (const char *)ADDR(start); + const char *found = strrchr(text, (int)MEM32(g_esp + 8)); + g_eax = found ? start + (uint32_t)(found - text) : 0; + g_esp += 4; +} + static void crt_strcmp(void) { g_eax = (uint32_t)strcmp((const char *)ADDR(MEM32(g_esp + 4)), @@ -507,6 +591,9 @@ unsupported: recomp_func_t guest_crt_lookup(const char *name) { static const struct { const char *name; recomp_func_t function; } functions[] = { + {"_errno", crt_errno}, + {"_set_error_mode", crt_set_error_mode}, + {"signal", crt_signal}, {"raise", crt_raise}, {"abort", crt_abort}, {"__set_app_type", crt_set_app_type}, {"__p__fmode", crt_p_fmode}, {"__p__commode", crt_p_commode}, {"__setusermatherr", crt_setusermatherr}, {"__getmainargs", crt_getmainargs}, @@ -523,6 +610,7 @@ recomp_func_t guest_crt_lookup(const char *name) {"floor", crt_floor}, {"_EH_prolog", crt_eh_prolog}, {"strlen", crt_strlen}, {"strcmp", crt_strcmp}, + {"strrchr", crt_strrchr}, {"_mbsrchr", crt_strrchr}, /* Initial single-byte locale. */ {"memcmp", crt_memcmp}, {"memchr", crt_memchr}, {"sscanf", crt_sscanf}, }; diff --git a/decompiled/runtime/linux_platform.c b/decompiled/runtime/linux_platform.c @@ -1,9 +1,11 @@ #define _GNU_SOURCE #include "linux_platform.h" #include <errno.h> +#include <limits.h> #include <pthread.h> #include <sched.h> #include <strings.h> +#include <sys/stat.h> #include <time.h> #include <unistd.h> @@ -14,6 +16,7 @@ struct critical_section { struct critical_section *next; }; static struct critical_section *critical_sections; +static uint32_t unhandled_exception_filter; static void check_pthread(int error) { @@ -190,6 +193,78 @@ static void get_version(void) g_esp += 8; } +static void set_unhandled_exception_filter(void) +{ + /* Preserve guest callback registration; native fault dispatch is not implemented. */ + g_eax = unhandled_exception_filter; + unhandled_exception_filter = MEM32(g_esp + 4); + g_esp += 8; +} + +static void get_module_filename(void) +{ + if (MEM32(g_esp + 4)) { + fprintf(stderr, "Module filenames for compiled DLL handles are not implemented\n"); + abort(); + } + /* The native executable's real path is also valid for Linux filesystem calls. */ + char path[PATH_MAX]; + ssize_t length = readlink("/proc/self/exe", path, sizeof(path)); + if (length < 0 || length == sizeof(path)) { perror("executable path"); abort(); } + uint32_t destination = MEM32(g_esp + 8), size = MEM32(g_esp + 12); + uint32_t count = length < size ? (uint32_t)length : size; + if (size) { + uint32_t copied = count < size ? count : size - 1; + memcpy((void *)ADDR(destination), path, copied); + MEM8(destination + copied) = 0; + } + MEM32(g_fs_base + 0x34) = length >= size ? 122 : 0; + g_eax = count; + g_esp += 16; +} + +static void delete_file(void) +{ + const char *name = (const char *)ADDR(MEM32(g_esp + 4)); + if ((name[0] && name[1] == ':') || (name[0] == '\\' && name[1] == '\\')) { + fprintf(stderr, "Windows drive/UNC file paths are not implemented: %s\n", name); + abort(); + } + char *path = strdup(name); + if (!path) abort(); + for (char *p = path; *p; ++p) if (*p == '\\') *p = '/'; + struct stat info; + int error = 0; + if (!*path) error = 3; /* ERROR_PATH_NOT_FOUND */ + else if (lstat(path, &info)) error = errno; + else if (S_ISDIR(info.st_mode) || (S_ISREG(info.st_mode) && !(info.st_mode & 0222))) error = EACCES; + else if (unlink(path)) error = errno; + g_eax = !error; + if (error) { + uint32_t windows_error; + if (!*path) windows_error = 3; + else if (error == ENOENT) { + windows_error = 2; /* Distinguish a missing leaf from a missing parent. */ + char *slash = strrchr(path, '/'); + if (slash) { + if (slash == path) slash[1] = 0; + else *slash = 0; + if (stat(path, &info) || !S_ISDIR(info.st_mode)) windows_error = 3; + } + } else if (error == ENOTDIR) windows_error = 3; + else if (error == EACCES || error == EPERM || error == EISDIR) windows_error = 5; + else if (error == ENAMETOOLONG) windows_error = 206; + else if (error == EROFS) windows_error = 19; + else { + fprintf(stderr, "Unhandled DeleteFile error for %s: %s\n", name, strerror(error)); + abort(); + } + MEM32(g_fs_base + 0x34) = windows_error; + } + free(path); + 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; @@ -203,6 +278,9 @@ recomp_func_t linux_platform_lookup(const char *dll, const char *name) {"GetCurrentProcessId", current_process_id}, {"GetStartupInfoA", get_startup_info}, {"GetVersionExA", get_version}, + {"SetUnhandledExceptionFilter", set_unhandled_exception_filter}, + {"GetModuleFileNameA", get_module_filename}, + {"DeleteFileA", delete_file}, {"InterlockedExchange", interlocked_exchange}, {"InterlockedIncrement", interlocked_increment}, {"InterlockedDecrement", interlocked_decrement}, diff --git a/decompiled/tests/delete_file.c b/decompiled/tests/delete_file.c @@ -0,0 +1,88 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#ifdef _WIN32 +#include <windows.h> +#else +#include "linux_platform.h" +#include "guest_memory.h" +#include <sys/stat.h> +#include <unistd.h> +#endif + +static void check_delete(const char *name, int expected_result, unsigned expected_error) +{ +#ifdef _WIN32 + SetLastError(777); +#else + strcpy((char *)ADDR(0x50002000u), name); + MEM32(g_fs_base + 0x34) = 777; + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = 0x50002000u; +#endif + +#ifdef _WIN32 + int result = DeleteFileA(name); + unsigned error = GetLastError(); +#else + linux_platform_lookup("KERNEL32.dll", "DeleteFileA")(); + int result = g_eax; + unsigned error = MEM32(g_fs_base + 0x34); + assert(g_esp == 0x5000ff08u); +#endif + + if (result != expected_result || error != expected_error) + fprintf(stderr, "DeleteFile %s: result %d error %u\n", name, result, error); + assert(result == expected_result && error == expected_error); +} + +int main(void) +{ + /* Tests own a fresh temporary directory on each OS. */ +#ifdef _WIN32 + char temporary[MAX_PATH], directory[MAX_PATH]; + assert(GetTempPathA(sizeof(temporary), temporary)); + assert(GetTempFileNameA(temporary, "aot", 0, directory)); + assert(DeleteFileA(directory) && CreateDirectoryA(directory, NULL)); + assert(SetCurrentDirectoryA(directory) && CreateDirectoryA("nested", NULL)); +#else + assert(guest_map("DeleteFile test memory", 0x50000000u, 0x10000u)); + g_fs_base = 0x50001000u; + char directory[] = "/tmp/ao-delete-test-XXXXXX"; + assert(mkdtemp(directory)); + assert(!chdir(directory) && !mkdir("nested", 0700)); +#endif + FILE *file = fopen("plain.txt", "w"); + assert(file && fputs("test", file) >= 0 && !fclose(file)); + check_delete("plain.txt", 1, 777); + assert(!fopen("plain.txt", "r")); + check_delete("plain.txt", 0, 2); + check_delete("missing\\file.txt", 0, 3); + check_delete("", 0, 3); + check_delete("nested", 0, 5); + + file = fopen("nested/readonly.txt", "w"); + assert(file && !fclose(file)); +#ifdef _WIN32 + assert(SetFileAttributesA("nested/readonly.txt", FILE_ATTRIBUTE_READONLY)); +#else + assert(!chmod("nested/readonly.txt", 0400)); +#endif + check_delete("nested\\readonly.txt", 0, 5); +#ifdef _WIN32 + assert(SetFileAttributesA("nested/readonly.txt", FILE_ATTRIBUTE_NORMAL)); +#else + assert(!chmod("nested/readonly.txt", 0600)); +#endif + check_delete("nested\\readonly.txt", 1, 777); + assert(!fopen("nested/readonly.txt", "r")); +#ifdef _WIN32 + assert(RemoveDirectoryA("nested") && SetCurrentDirectoryA(temporary) && RemoveDirectoryA(directory)); +#else + assert(!rmdir("nested") && !chdir("/tmp") && !rmdir(directory)); +#endif + puts("DeleteFile: deletion, backslashes, missing paths, directories and readonly files passed"); + return 0; +} diff --git a/decompiled/tests/guest_crt.c b/decompiled/tests/guest_crt.c @@ -17,10 +17,19 @@ static void second_constructor(void) g_esp += 4; } +static void abort_callback(void) +{ + assert(MEM32(g_esp + 4) == 22); + ++MEM32(0x50003000u); + g_cur_func = 0x44440000u; + g_esp += 4; +} + recomp_func_t recomp_lookup(uint32_t address) { if (address == 0x11110000u) return first_constructor; if (address == 0x22220000u) return second_constructor; + if (address == 0x44440000u) return abort_callback; return NULL; } @@ -244,6 +253,106 @@ static void exit_callbacks_run_once_in_reverse_order(void) assert(g_esp == 0x5000ff08u && g_cur_func == 0x33330000u); } +static void errno_storage_follows_the_guest_thread(void) +{ + assert(guest_map("first CRT thread", 0x51000000u, 0x10000u)); + assert(guest_map("second CRT thread", 0x51010000u, 0x10000u)); + uint32_t saved_thread = g_fs_base; + g_fs_base = 0x51000000u; + MEM32(g_fs_base + 0x34) = 777; + g_esp = 0x5000ff00u; + + guest_crt_lookup("_errno")(); + uint32_t first = g_eax; + assert(MEM32(first) == 0); + MEM32(first) = 22; + guest_crt_lookup("_errno")(); + assert(g_eax == first && MEM32(g_eax) == 22); + g_fs_base = 0x51010000u; + guest_crt_lookup("_errno")(); + uint32_t second = g_eax; + assert(MEM32(second) == 0); + MEM32(second) = 12; + g_fs_base = 0x51000000u; + guest_crt_lookup("_errno")(); + + assert(g_eax == first && first != second); + assert(MEM32(first) == 22 && MEM32(second) == 12); + assert(MEM32(g_fs_base + 0x34) == 777); + assert(g_esp == 0x5000ff10u); + g_fs_base = saved_thread; +} + +static void reverse_string_search_returns_guest_addresses(void) +{ + strcpy((char *)ADDR(0x50006000u), "a.b.c\xff"); + const uint32_t chars[] = {'.', 'x', 0, 0x12e, 0xff, 0x1ff, UINT32_MAX}; + const uint32_t expected[] = {0x50006003u, 0, 0x50006006u, 0x50006003u, + 0x50006005u, 0x50006005u, 0x50006005u}; + const char *names[] = {"strrchr", "_mbsrchr"}; + for (unsigned i = 0; i < 14; ++i) { + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = 0x50006000u; + MEM32(g_esp + 8) = chars[i % 7]; + + guest_crt_lookup(names[i / 7])(); + + assert(g_eax == expected[i % 7]); + assert(g_esp == 0x5000ff04u); + } +} + +static void error_mode_preserves_previous_mode_and_errno(void) +{ + uint32_t saved_thread = g_fs_base; + g_fs_base = 0x51000000u; + g_esp = 0x5000ff00u; + guest_crt_lookup("_errno")(); + uint32_t error = g_eax; + const int modes[] = {3, 1, 3, 2, 3, 0, 3, 4, -1, 3}; + const int previous[] = {0, 0, 1, 1, 2, 2, 0, -1, -1, 0}; + for (unsigned i = 0; i < 10; ++i) { + MEM32(error) = 777; + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = modes[i]; + + guest_crt_lookup("_set_error_mode")(); + + assert((int32_t)g_eax == previous[i]); + assert(MEM32(error) == (modes[i] == 4 || modes[i] == -1 ? 22 : 777)); + assert(g_esp == 0x5000ff04u); + } + g_fs_base = saved_thread; +} + +static void synchronous_abort_signal_calls_guest_handler_once(void) +{ + MEM32(0x50003000u) = 0; + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = 22; + MEM32(g_esp + 8) = 0x44440000u; + guest_crt_lookup("signal")(); + assert(g_eax == 0 && g_esp == 0x5000ff04u); + g_cur_func = 0x55550000u; + g_esp = 0x5000ff00u; + + guest_crt_lookup("raise")(); + + assert(MEM32(0x50003000u) == 1 && g_eax == 0); + assert(g_esp == 0x5000ff04u && g_cur_func == 0x55550000u); + g_esp = 0x5000ff00u; + MEM32(g_esp + 8) = 1; + guest_crt_lookup("signal")(); + assert(g_eax == 0); /* Delivery reset the installed handler. */ + g_esp = 0x5000ff00u; + guest_crt_lookup("raise")(); + assert(g_eax == 0 && MEM32(0x50003000u) == 1); + g_esp = 0x5000ff00u; + MEM32(g_esp + 8) = 0; + guest_crt_lookup("signal")(); + assert(g_eax == 1); +} + int main(void) { assert(guest_map("test memory and stack", 0x50000000u, 0x10000u)); @@ -258,6 +367,10 @@ int main(void) startup_exposes_writable_crt_modes(); startup_selects_53_bit_floating_point(); exit_callbacks_run_once_in_reverse_order(); + errno_storage_follows_the_guest_thread(); + reverse_string_search_returns_guest_addresses(); + error_mode_preserves_previous_mode_and_errno(); + synchronous_abort_signal_calls_guest_handler_once(); 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 @@ -166,6 +166,27 @@ static void process_id_is_the_native_process(void) assert(MEM32(g_fs_base + 0x34) == 777); } +static void exception_filter_registration_returns_previous_guest_callback(void) +{ + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = 0x28001234; + MEM32(g_fs_base + 0x34) = 777; + + linux_platform_lookup("KERNEL32.dll", "SetUnhandledExceptionFilter")(); + assert(g_eax == 0); + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = 0x28005678; + linux_platform_lookup("KERNEL32.dll", "SetUnhandledExceptionFilter")(); + assert(g_eax == 0x28001234); + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = 0; + linux_platform_lookup("KERNEL32.dll", "SetUnhandledExceptionFilter")(); + + assert(g_eax == 0x28005678); + assert(g_esp == 0x5000ff08u); + assert(MEM32(g_fs_base + 0x34) == 777); +} + int main(void) { assert(guest_map("test memory and stack", 0x50000000u, 0x10000u)); @@ -176,6 +197,7 @@ int main(void) interlocked_operations_preserve_32_bit_results(); startup_info_uses_32_bit_layout(); process_id_is_the_native_process(); + exception_filter_registration_returns_previous_guest_callback(); puts("Linux platform: clocks, AO delta timer, last error, recursive locks and atomics passed"); return 0; } diff --git a/decompiled/tests/module_filename.c b/decompiled/tests/module_filename.c @@ -0,0 +1,64 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#include <windows.h> +#else +#include "linux_platform.h" +#include "guest_memory.h" +#include <unistd.h> +#endif + +int main(void) +{ + char path[4096]; +#ifdef _WIN32 + unsigned length = GetModuleFileNameA(NULL, path, sizeof(path)); + assert(length && length < sizeof(path)); +#else + ssize_t size = readlink("/proc/self/exe", path, sizeof(path)); + assert(size > 0 && size < sizeof(path)); + unsigned length = size; + path[length] = 0; + assert(guest_map("module filename test memory", 0x50000000u, 0x10000u)); + g_fs_base = 0x50001000u; +#endif + const unsigned sizes[] = {0, 1, 12, length - 1, length, length + 1, 4096}; + for (unsigned i = 0; i < 7; ++i) { + unsigned char output[4104], expected[4104]; + memset(output, 0xa5, sizeof(output)); + memset(expected, 0xa5, sizeof(expected)); + if (sizes[i]) { + unsigned copied = length < sizes[i] ? length : sizes[i] - 1; + memcpy(expected + 4, path, copied); + expected[4 + copied] = 0; + } +#ifdef _WIN32 + SetLastError(777); +#else + memcpy((void *)ADDR(0x50002000u), output, sizeof(output)); + MEM32(g_fs_base + 0x34) = 777; + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = 0; + MEM32(g_esp + 8) = 0x50002004u; + MEM32(g_esp + 12) = sizes[i]; +#endif + +#ifdef _WIN32 + unsigned result = GetModuleFileNameA(NULL, (char *)output + 4, sizes[i]); + unsigned error = GetLastError(); +#else + linux_platform_lookup("KERNEL32.dll", "GetModuleFileNameA")(); + unsigned result = g_eax, error = MEM32(g_fs_base + 0x34); + memcpy(output, (void *)ADDR(0x50002000u), sizeof(output)); + assert(g_esp == 0x5000ff10u); +#endif + + assert(result == (length < sizes[i] ? length : sizes[i])); + assert(error == (length < sizes[i] ? 0 : 122)); + assert(!memcmp(output, expected, sizeof(output))); + } + puts("module filename: real executable path, 7 buffer sizes, errors and canaries passed"); + return 0; +} diff --git a/tools/reconstruction/test_portable_delete_file.py b/tools/reconstruction/test_portable_delete_file.py @@ -0,0 +1,26 @@ +"""Compare Linux file deletion with Windows in isolated temporary directories.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-delete-file" +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/delete_file.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/delete_file.c", "runtime/linux_platform.c", "runtime/guest_memory.c", + "runtime/recomp_state.c", "-pthread", "-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_module_filename.py b/tools/reconstruction/test_portable_module_filename.py @@ -0,0 +1,26 @@ +"""Compare Windows/Linux executable-path buffer and error semantics.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-module-filename" +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/module_filename.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/module_filename.c", "runtime/linux_platform.c", "runtime/guest_memory.c", + "runtime/recomp_state.c", "-pthread", "-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 contract: " + actual.strip())