commit 46a6bea9e7b0698be70f4770b8a006535dd42d8e
parent 93d41df0fbc914bdd8674ac8e3171edc53e8cbd3
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 06:17:41 +0000
Read the AO resource database through native Linux file adapters
Diffstat:
9 files changed, 570 insertions(+), 7 deletions(-)
diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux
@@ -3,7 +3,7 @@ CPPFLAGS := -Iruntime -Isrc -Idata -DRECOMP_PORTABLE
CFLAGS := -O2 -fno-strict-aliasing
GAME_SOURCES := $(wildcard src/*/*.c) src/dispatch.c
DATA_SOURCES := $(wildcard data/*.c)
-RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_tls.c runtime/linux_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c
+RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c
VENDOR_SOURCES := $(wildcard vendor/msvcp60/source/*.c) $(wildcard vendor/ace/source/*.c)
OBJECTS := $(patsubst %.c,build/linux/%.o,$(GAME_SOURCES) $(DATA_SOURCES) $(RUNTIME_SOURCES) $(VENDOR_SOURCES))
@@ -128,7 +128,7 @@ build/linux/guest-stat: tests/guest_stat.c runtime/guest_stdio.c runtime/guest_c
mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
-build/linux/dynamic-imports: tests/dynamic_imports.c runtime/linux_imports.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_sync.c runtime/linux_tls.c runtime/linux_network.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_format.c runtime/guest_exception.c runtime/guest_msvcp.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c $(wildcard runtime/*.h)
+build/linux/dynamic-imports: tests/dynamic_imports.c runtime/linux_imports.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_network.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_format.c runtime/guest_exception.c runtime/guest_msvcp.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c $(wildcard runtime/*.h)
mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -lSDL2 -lGL -o $@
@@ -158,6 +158,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric
./build/linux/semaphore
./build/linux/guest-scanf
./build/linux/network-startup
+ ./build/linux/files
./build/linux/os-version
./build/linux/guest-format
./build/linux/event
@@ -192,6 +193,12 @@ build/linux/devices: tests/devices.c runtime/linux_graphics.c runtime/linux_wind
test: build/linux/devices
+build/linux/files: tests/files.c runtime/linux_file.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_file.h runtime/guest_memory.h runtime/recomp_types.h
+ mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
+
+test: build/linux/files
+
-include $(OBJECTS:.o=.d)
build/linux/getenv: tests/getenv.c runtime/guest_crt.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/guest_crt.h runtime/guest_heap.h runtime/guest_memory.h runtime/recomp_types.h
diff --git a/decompiled/runtime/guest_stdio.c b/decompiled/runtime/guest_stdio.c
@@ -300,11 +300,11 @@ static void crt_fileno(void)
g_esp += 4;
}
-static void print_file(int list)
+static void print_file(uint32_t file, uint32_t format, uint32_t arguments)
{
- struct stream *stream = find_stream(MEM32(g_esp + 4));
+ struct stream *stream = find_stream(file);
uint32_t length;
- char *text = guest_format_string(MEM32(g_esp + 8), list ? MEM32(g_esp + 12) : g_esp + 12, &length);
+ char *text = guest_format_string(format, arguments, &length);
g_eax = length;
for (uint32_t i = 0; i < length; ++i)
if (put_character(stream, (unsigned char)text[i]) < 0) { g_eax = UINT32_MAX; break; }
@@ -534,8 +534,9 @@ static void crt_stat(void)
g_esp += 4;
}
-static void crt_fprintf(void) { print_file(0); }
-static void crt_vfprintf(void) { print_file(1); }
+static void crt_fprintf(void) { print_file(MEM32(g_esp + 4), MEM32(g_esp + 8), g_esp + 12); }
+static void crt_vfprintf(void) { print_file(MEM32(g_esp + 4), MEM32(g_esp + 8), MEM32(g_esp + 12)); }
+static void crt_printf(void) { print_file(guest_crt_data("_iob") + 32, MEM32(g_esp + 4), g_esp + 8); }
recomp_func_t guest_stdio_lookup(const char *name)
{
@@ -544,6 +545,7 @@ recomp_func_t guest_stdio_lookup(const char *name)
{"fflush", crt_fflush}, {"fputc", crt_fputc}, {"fwrite", crt_fwrite},
{"fputs", crt_fputs}, {"setvbuf", crt_setvbuf}, {"_fileno", crt_fileno},
{"fprintf", crt_fprintf}, {"vfprintf", crt_vfprintf},
+ {"printf", crt_printf},
{"_chmod", crt_chmod}, {"_stat", crt_stat},
{"ftell", crt_ftell}, {"fseek", crt_fseek}, {"fread", crt_fread},
{"fgetc", crt_fgetc}, {"getc", crt_fgetc}, {"_filbuf", crt_filbuf},
diff --git a/decompiled/runtime/linux_file.c b/decompiled/runtime/linux_file.c
@@ -0,0 +1,200 @@
+#define _GNU_SOURCE
+#include "linux_file.h"
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+/* Guest handles identify host-owned descriptors, never host pointers. */
+struct guest_file {
+ uint32_t id;
+ int descriptor, unbuffered;
+ struct guest_file *next;
+};
+static struct guest_file *handles;
+/* src/hhr.dll/lifted-0007.c: sub_31015A7C rejects negative file handles. */
+static uint32_t next_handle = 0x05000000u;
+
+static struct guest_file *find_handle(uint32_t id)
+{
+ for (struct guest_file *handle = handles; handle; handle = handle->next)
+ if (handle->id == id) return handle;
+ MEM32(g_fs_base + 0x34) = 6; /* ERROR_INVALID_HANDLE */
+ return NULL;
+}
+
+static uint32_t file_error(int error)
+{
+ switch (error) {
+ case ENOENT: return 2;
+ case ENOTDIR: return 3;
+ case EMFILE: case ENFILE: return 4;
+ case EACCES: case EPERM: case EISDIR: return 5;
+ case EBADF: return 6;
+ case EINVAL: return 87;
+ case ENOMEM: return 8;
+ case ENAMETOOLONG: return 206;
+ case EFAULT: return 998;
+ case EIO: return 1117;
+ default:
+ fprintf(stderr, "Unhandled native file error: %s\n", strerror(error));
+ abort();
+ }
+}
+
+static void create_file(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;
+ if (MEM32(g_esp + 8) != 0x80000000u || MEM32(g_esp + 16) ||
+ (!buffered && !unbuffered) || 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));
+ abort();
+ }
+ if (strchr(name, ':') || !strncmp(name, "\\\\", 2)) {
+ fprintf(stderr, "Windows drive/UNC file paths are not implemented: %s\n", name);
+ abort();
+ }
+
+ /* OPEN_ALWAYS reports whether it created a file without truncating existing data. */
+ 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 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);
+ uint32_t error = descriptor < 0 ? file_error(errno) : 0;
+ if (error == 2) {
+ char *slash = strrchr(path, '/');
+ if (slash && slash != path) {
+ *slash = 0;
+ struct stat parent;
+ if (stat(path, &parent) || !S_ISDIR(parent.st_mode)) error = 3;
+ }
+ }
+ free(path);
+ if (descriptor >= 0) {
+ struct stat info;
+ if (fstat(descriptor, &info)) error = file_error(errno);
+ else if (!S_ISREG(info.st_mode)) error = 5;
+ if (error) close(descriptor);
+ }
+
+ g_eax = UINT32_MAX;
+ if (!error) {
+ if (next_handle >= 0x06000000u) abort();
+ struct guest_file *handle = malloc(sizeof(*handle));
+ if (!handle) abort();
+ handle->id = next_handle;
+ next_handle += 4;
+ handle->descriptor = descriptor;
+ handle->unbuffered = unbuffered;
+ handle->next = handles;
+ handles = handle;
+ g_eax = handle->id;
+ }
+ MEM32(g_fs_base + 0x34) = error ? error : (unbuffered && !created ? 183 : 0);
+ g_esp += 32;
+}
+
+static void read_file(void)
+{
+ uint32_t output = MEM32(g_esp + 8), size = MEM32(g_esp + 12);
+ uint32_t count = MEM32(g_esp + 16);
+ if (!count || MEM32(g_esp + 20)) {
+ fprintf(stderr, "Overlapped/uncounted ReadFile is not implemented\n");
+ abort();
+ }
+ MEM32(count) = 0;
+ struct guest_file *handle = find_handle(MEM32(g_esp + 4));
+ g_eax = 0;
+ g_esp += 24;
+ if (!handle) return;
+ if (!output && size) { MEM32(g_fs_base + 0x34) = 998; return; }
+
+ /* Preserve the guest's 512-byte sector alignment despite relocated host memory. */
+ void *buffer = output ? (void *)ADDR(output) : NULL;
+ if (handle->unbuffered && size) {
+ off_t position = lseek(handle->descriptor, 0, SEEK_CUR);
+ if (position < 0) { MEM32(g_fs_base + 0x34) = file_error(errno); return; }
+ if ((output | size | position) & 511) { MEM32(g_fs_base + 0x34) = 87; return; }
+ if (posix_memalign(&buffer, 4096, size)) { MEM32(g_fs_base + 0x34) = 8; return; }
+ }
+ ssize_t result;
+ do {
+ result = read(handle->descriptor, buffer, size);
+ } while (result < 0 && errno == EINTR);
+ if (result < 0) MEM32(g_fs_base + 0x34) = file_error(errno);
+ if (handle->unbuffered && size) {
+ if (result > 0) memcpy((void *)ADDR(output), buffer, result);
+ free(buffer);
+ }
+ if (result >= 0) { MEM32(count) = result; g_eax = 1; }
+}
+
+static void set_file_pointer(void)
+{
+ struct guest_file *handle = find_handle(MEM32(g_esp + 4));
+ uint32_t low = MEM32(g_esp + 8), high = MEM32(g_esp + 12);
+ uint32_t method = MEM32(g_esp + 16);
+ g_eax = UINT32_MAX;
+ if (handle) {
+ int64_t distance = high ? (int64_t)(int32_t)MEM32(high) * 0x100000000ll + low : (int32_t)low;
+ int64_t base = 0;
+ uint32_t error = 0;
+ if (method == 1) {
+ base = lseek(handle->descriptor, 0, SEEK_CUR);
+ if (base < 0) error = file_error(errno);
+ } else if (method == 2) {
+ struct stat info;
+ if (fstat(handle->descriptor, &info)) error = file_error(errno);
+ else base = info.st_size;
+ } else if (method != 0) error = 87; /* ERROR_INVALID_PARAMETER */
+
+ /* Validate before moving, so failed seeks leave the cursor and high word intact. */
+ if (!error) {
+ if (distance < -base) error = 131; /* ERROR_NEGATIVE_SEEK */
+ else if (distance > INT64_MAX - base || (!high && base + distance > UINT32_MAX)) error = 87;
+ else if (handle->unbuffered && ((base + distance) & 511)) error = 87;
+ else {
+ int64_t position = lseek(handle->descriptor, base + distance, SEEK_SET);
+ if (position < 0) error = file_error(errno);
+ else {
+ g_eax = (uint32_t)position;
+ if (high) MEM32(high) = (uint64_t)position >> 32;
+ if (g_eax == UINT32_MAX) MEM32(g_fs_base + 0x34) = 0;
+ }
+ }
+ }
+ if (error) MEM32(g_fs_base + 0x34) = error;
+ }
+ g_esp += 20;
+}
+
+static void close_file(void)
+{
+ struct guest_file *handle = find_handle(MEM32(g_esp + 4));
+ g_eax = 0;
+ if (handle) {
+ struct guest_file **link = &handles;
+ while (*link != handle) link = &(*link)->next;
+ *link = handle->next;
+ g_eax = close(handle->descriptor) == 0;
+ if (!g_eax) MEM32(g_fs_base + 0x34) = file_error(errno);
+ free(handle);
+ }
+ g_esp += 8;
+}
+
+recomp_func_t linux_file_lookup(const char *name)
+{
+ if (!strcmp(name, "CreateFileA")) return create_file;
+ if (!strcmp(name, "ReadFile")) return read_file;
+ if (!strcmp(name, "SetFilePointer")) return set_file_pointer;
+ if (!strcmp(name, "CloseHandle")) return close_file;
+ return NULL;
+}
diff --git a/decompiled/runtime/linux_file.h b/decompiled/runtime/linux_file.h
@@ -0,0 +1,5 @@
+#ifndef LINUX_FILE_H
+#define LINUX_FILE_H
+#include "recomp_types.h"
+recomp_func_t linux_file_lookup(const char *name);
+#endif
diff --git a/decompiled/runtime/linux_imports.c b/decompiled/runtime/linux_imports.c
@@ -6,6 +6,7 @@
#include "guest_msvcp.h"
#include "linux_platform.h"
#include "linux_sync.h"
+#include "linux_file.h"
#include "linux_tls.h"
#include "linux_network.h"
#include "linux_graphics.h"
@@ -93,11 +94,19 @@ static void get_proc_address(void)
g_esp += 12;
}
+static void close_handle(void)
+{
+ if ((MEM32(g_esp + 4) & 0xff000000u) == 0x05000000u)
+ linux_file_lookup("CloseHandle")();
+ else linux_sync_lookup("CloseHandle")();
+}
+
static recomp_func_t library_lookup(const char *name)
{
if (!strcmp(name, "LoadLibraryA")) return load_library;
if (!strcmp(name, "FreeLibrary")) return free_library;
if (!strcmp(name, "GetProcAddress")) return get_proc_address;
+ if (!strcmp(name, "CloseHandle")) return close_handle;
return NULL;
}
@@ -125,6 +134,7 @@ recomp_func_t recomp_lookup_import(uint32_t va)
if (function) return function;
if (!strcasecmp(imports[current_import].dll, "kernel32.dll")) {
function = library_lookup(imports[current_import].name);
+ if (!function) function = linux_file_lookup(imports[current_import].name);
if (!function) function = linux_module_lookup(imports[current_import].name);
if (!function) function = linux_sync_lookup(imports[current_import].name);
if (!function) function = linux_tls_lookup(imports[current_import].name);
diff --git a/decompiled/tests/dynamic_imports.c b/decompiled/tests/dynamic_imports.c
@@ -117,6 +117,38 @@ static void unresolved_window_call_fails_explicitly(void)
assert(strstr(message, "user32.dll!GetMessageW ordinal=0 caller=280028f0"));
}
+static void file_and_event_handles_close_through_imports(void)
+{
+ char path[] = "/tmp/ao-file-import-XXXXXX";
+ int descriptor = mkstemp(path);
+ assert(descriptor >= 0 && !close(descriptor));
+ strcpy((char *)ADDR(0x50002000u), path);
+ g_esp = 0x5000ff00u;
+ MEM32(g_esp + 4) = 0x50002000u;
+ MEM32(g_esp + 8) = 0x80000000u;
+ MEM32(g_esp + 12) = 1;
+ MEM32(g_esp + 16) = 0;
+ MEM32(g_esp + 20) = 3;
+ MEM32(g_esp + 24) = 0x80;
+ MEM32(g_esp + 28) = 0;
+
+ recomp_lookup_import(linux_import_bind("kernel32.dll", "CreateFileA", 0))();
+ uint32_t file = g_eax;
+ assert(file != UINT32_MAX && g_esp == 0x5000ff20u);
+ g_esp = 0x5000ff00u;
+ for (unsigned i = 1; i <= 4; ++i) MEM32(g_esp + i * 4) = 0;
+ recomp_lookup_import(linux_import_bind("kernel32.dll", "CreateEventA", 0))();
+ uint32_t event = g_eax;
+ assert(event && event != file && g_esp == 0x5000ff14u);
+ uint32_t file_closed = call("CloseHandle", file, 0);
+ uint32_t event_closed = call("CloseHandle", event, 0);
+
+ assert(file_closed == 1 && event_closed == 1);
+ assert(call("CloseHandle", file, 0) == 0 && MEM32(g_fs_base + 0x34) == 6);
+ assert(call("CloseHandle", event, 0) == 0 && MEM32(g_fs_base + 0x34) == 6);
+ assert(!unlink(path));
+}
+
int main(void)
{
assert(guest_map("dynamic imports test", 0x50000000u, 0x10000u));
@@ -125,6 +157,7 @@ int main(void)
failed_lookups_report_errors();
import_names_are_owned_and_calls_are_dispatched();
unresolved_window_call_fails_explicitly();
+ file_and_event_handles_close_through_imports();
puts("dynamic imports: 14 window symbols, stable guest IDs, lifetime, errors, dispatch and ABI passed");
return 0;
}
diff --git a/decompiled/tests/files.c b/decompiled/tests/files.c
@@ -0,0 +1,269 @@
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include "linux_file.h"
+#include "guest_memory.h"
+#include <unistd.h>
+
+static uint32_t call(const char *name, unsigned count, const uint32_t *args)
+{
+ g_esp = 0x5000ff00u;
+ for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = args[i];
+ g_ebx = 0x12345678;
+ g_esi = 0x23456789;
+ g_edi = 0x3456789a;
+ g_ebp = 0x456789ab;
+ linux_file_lookup(name)();
+ assert(g_esp == 0x5000ff00u + (count + 1) * 4);
+ assert(g_ebx == 0x12345678 && g_esi == 0x23456789);
+ assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
+ return g_eax;
+}
+#endif
+
+static uint32_t check_open(const char *path, unsigned expected_error)
+{
+#ifdef _WIN32
+ SetLastError(77);
+#else
+ strcpy((char *)ADDR(0x50002000u), path);
+ MEM32(g_fs_base + 0x34) = 77;
+ uint32_t args[] = {0x50002000u, 0x80000000u, 1, 0, 3, 0x80, 0};
+#endif
+
+#ifdef _WIN32
+ uint32_t handle = (uintptr_t)CreateFileA(path, GENERIC_READ, FILE_SHARE_READ,
+ NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+ unsigned error = GetLastError();
+#else
+ uint32_t handle = call("CreateFileA", 7, args);
+ unsigned error = MEM32(g_fs_base + 0x34);
+#endif
+
+ if (error != expected_error) fprintf(stderr, "open %s: error %u\n", path, error);
+ assert(error == expected_error);
+ assert((handle == UINT32_MAX) == (expected_error != 0));
+ if (!expected_error) assert((int32_t)handle >= 0);
+ return handle;
+}
+
+static void check_read(uint32_t handle, unsigned requested, const char *expected,
+ unsigned expected_count, unsigned expected_error)
+{
+ unsigned char bytes[24];
+ uint32_t count[] = {0xa5a5a5a5, 99, 0xa5a5a5a5};
+ memset(bytes, 0xa5, sizeof(bytes));
+#ifdef _WIN32
+ SetLastError(77);
+#else
+ memcpy((void *)ADDR(0x50003000u), bytes, sizeof(bytes));
+ memcpy((void *)ADDR(0x50004000u), count, sizeof(count));
+ MEM32(g_fs_base + 0x34) = 77;
+ uint32_t args[] = {handle, requested ? 0x50003004u : 0, requested, 0x50004004u, 0};
+#endif
+
+#ifdef _WIN32
+ int result = ReadFile((HANDLE)(uintptr_t)handle, requested ? bytes + 4 : NULL,
+ requested, (DWORD *)&count[1], NULL);
+ unsigned error = GetLastError();
+#else
+ int result = call("ReadFile", 5, args);
+ unsigned error = MEM32(g_fs_base + 0x34);
+ memcpy(bytes, (void *)ADDR(0x50003000u), sizeof(bytes));
+ memcpy(count, (void *)ADDR(0x50004000u), sizeof(count));
+#endif
+
+ assert(result == (expected_error == 77));
+ assert(error == expected_error && count[1] == expected_count);
+ assert(count[0] == 0xa5a5a5a5 && count[2] == 0xa5a5a5a5);
+ assert(!memcmp(bytes + 4, expected, expected_count));
+ for (unsigned i = 0; i < 4; ++i) assert(bytes[i] == 0xa5);
+ for (unsigned i = 4 + expected_count; i < sizeof(bytes); ++i) assert(bytes[i] == 0xa5);
+}
+
+static void seek_preserves_positions_and_high_word_on_errors(uint32_t handle)
+{
+ const struct { int32_t low, high; unsigned has_high, method, result; int32_t output_high; unsigned error; } cases[] = {
+ {0, 0, 0, 0, 0, 0, 77}, {2, 0, 0, 0, 2, 0, 77},
+ {-1, 0, 0, 1, 1, 0, 77}, {-1, 0, 0, 0, UINT32_MAX, 0, 131},
+ {0, 0, 0, 9, UINT32_MAX, 0, 87}, {-1, 0, 0, 2, 5, 0, 77},
+ {-1, 0, 1, 0, UINT32_MAX, 0, 0}, {16, 1, 1, 0, 16, 1, 77},
+ {0, 0, 0, 1, UINT32_MAX, 0, 87}, {0, 0, 1, 1, 16, 1, 77},
+ {0, -1, 1, 0, UINT32_MAX, -1, 131}, {0, 0, 1, 1, 16, 1, 77},
+ {2, 0, 1, 0, 2, 0, 77},
+ };
+ for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) {
+ uint32_t high[] = {0xa5a5a5a5, cases[i].high, 0xa5a5a5a5};
+#ifdef _WIN32
+ SetLastError(77);
+#else
+ memcpy((void *)ADDR(0x50004000u), high, sizeof(high));
+ MEM32(g_fs_base + 0x34) = 77;
+ uint32_t args[] = {handle, cases[i].low, cases[i].has_high ? 0x50004004u : 0, cases[i].method};
+#endif
+
+#ifdef _WIN32
+ uint32_t result = SetFilePointer((HANDLE)(uintptr_t)handle, cases[i].low,
+ cases[i].has_high ? (LONG *)&high[1] : NULL, cases[i].method);
+ unsigned error = GetLastError();
+#else
+ uint32_t result = call("SetFilePointer", 4, args);
+ unsigned error = MEM32(g_fs_base + 0x34);
+ memcpy(high, (void *)ADDR(0x50004000u), sizeof(high));
+#endif
+
+ assert(result == cases[i].result && error == cases[i].error);
+ assert(high[0] == 0xa5a5a5a5 && high[2] == 0xa5a5a5a5);
+ assert(high[1] == (uint32_t)cases[i].output_high);
+ }
+}
+
+static void check_close(uint32_t handle, int expected)
+{
+#ifdef _WIN32
+ SetLastError(77);
+#else
+ MEM32(g_fs_base + 0x34) = 77;
+#endif
+
+#ifdef _WIN32
+ int result = CloseHandle((HANDLE)(uintptr_t)handle);
+ unsigned error = GetLastError();
+#else
+ int result = call("CloseHandle", 1, &handle);
+ unsigned error = MEM32(g_fs_base + 0x34);
+#endif
+
+ assert(result == expected && error == (expected ? 77 : 6));
+}
+
+static void unbuffered_database_reads_use_aligned_guest_buffers(void)
+{
+#ifdef _WIN32
+ unsigned char *buffer = VirtualAlloc(NULL, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
+ assert(buffer);
+#else
+ unsigned char *buffer = (void *)ADDR(0x50005000u);
+#endif
+ const char *paths[] = {"fixture.dat", "created.dat"};
+ for (unsigned file_index = 0; file_index < 2; ++file_index) {
+#ifdef _WIN32
+ uint32_t handle = (uintptr_t)CreateFileA(paths[file_index], GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
+ NULL, OPEN_ALWAYS, FILE_FLAG_NO_BUFFERING | FILE_FLAG_RANDOM_ACCESS, NULL);
+ unsigned error = GetLastError();
+#else
+ strcpy((char *)ADDR(0x50002000u), paths[file_index]);
+ uint32_t args[] = {0x50002000u, 0x80000000u, 3, 0, 4, 0x30000000u, 0};
+ uint32_t handle = call("CreateFileA", 7, args);
+ unsigned error = MEM32(g_fs_base + 0x34);
+#endif
+ assert((int32_t)handle >= 0 && error == (file_index ? 0 : 183));
+ const struct { unsigned size, offset, count, error; } cases[] = {
+ {1, 0, 0, 87}, {512, 1, 0, 87}, {512, 0, 6, 77}, {0, 0, 0, 77},
+ };
+ for (unsigned i = 0; i < 4; ++i) {
+ memset(buffer, 0xa5, 1024);
+ uint32_t count = 99;
+#ifdef _WIN32
+ assert(SetFilePointer((HANDLE)(uintptr_t)handle, 0, NULL, FILE_BEGIN) == 0);
+ SetLastError(77);
+#else
+ uint32_t seek[] = {handle, 0, 0, 0};
+ assert(call("SetFilePointer", 4, seek) == 0);
+ MEM32(0x50004004u) = 99;
+ MEM32(g_fs_base + 0x34) = 77;
+ uint32_t read[] = {handle, 0x50005000u + cases[i].offset, cases[i].size, 0x50004004u, 0};
+#endif
+
+#ifdef _WIN32
+ int result = ReadFile((HANDLE)(uintptr_t)handle, buffer + cases[i].offset,
+ cases[i].size, (DWORD *)&count, NULL);
+ error = GetLastError();
+#else
+ int result = call("ReadFile", 5, read);
+ error = MEM32(g_fs_base + 0x34);
+ count = MEM32(0x50004004u);
+#endif
+
+ assert(result == (cases[i].error == 77) && error == cases[i].error);
+ assert(count == (file_index ? 0 : cases[i].count));
+ assert(!memcmp(buffer, "A\0\xff" "B\r\n", count));
+ for (unsigned j = count; j < 1024; ++j) assert(buffer[j] == 0xa5);
+ }
+#ifdef _WIN32
+ uint32_t invalid_seek = SetFilePointer((HANDLE)(uintptr_t)handle, 1, NULL, FILE_BEGIN);
+ unsigned seek_error = GetLastError();
+ uint32_t position = SetFilePointer((HANDLE)(uintptr_t)handle, 0, NULL, FILE_CURRENT);
+#else
+ uint32_t seek[] = {handle, 1, 0, 0};
+ uint32_t invalid_seek = call("SetFilePointer", 4, seek);
+ unsigned seek_error = MEM32(g_fs_base + 0x34);
+ seek[1] = 0;
+ seek[3] = 1;
+ uint32_t position = call("SetFilePointer", 4, seek);
+#endif
+ assert(invalid_seek == UINT32_MAX && seek_error == 87 && position == 0);
+ check_close(handle, 1);
+ }
+#ifdef _WIN32
+ assert(VirtualFree(buffer, 0, MEM_RELEASE));
+#endif
+ assert(!remove("created.dat"));
+}
+
+int main(void)
+{
+ /* Own a disposable directory and create the binary fixture through native stdio. */
+#ifdef _WIN32
+ char temporary[MAX_PATH], directory[MAX_PATH];
+ assert(GetTempPathA(sizeof(temporary), temporary));
+ assert(GetTempFileNameA(temporary, "aof", 0, directory));
+ assert(DeleteFileA(directory) && CreateDirectoryA(directory, NULL));
+ assert(SetCurrentDirectoryA(directory));
+#else
+ assert(guest_map("file test memory", 0x50000000u, 0x10000u));
+ g_fs_base = 0x50001000u;
+ char directory[] = "/tmp/ao-file-test-XXXXXX";
+ assert(mkdtemp(directory) && !chdir(directory));
+#endif
+ FILE *file = fopen("fixture.dat", "wb");
+ assert(file && fwrite("A\0\xff" "B\r\n", 1, 6, file) == 6 && !fclose(file));
+
+ check_open("missing.dat", 2);
+ check_open("missing\\fixture.dat", 3);
+ check_open("fixture.dat\\child", 3);
+ check_open(".", 5);
+ uint32_t first = check_open(".\\fixture.dat", 0);
+ uint32_t second = check_open("./fixture.dat", 0);
+ assert(first != second);
+ check_read(first, 2, "A\0", 2, 77);
+ check_read(first, 12, "\xff" "B\r\n", 4, 77);
+ check_read(first, 1, "", 0, 77);
+ check_read(first, 0, "", 0, 77);
+ check_read(second, 12, "A\0\xff" "B\r\n", 6, 77);
+ seek_preserves_positions_and_high_word_on_errors(first);
+ check_read(first, 12, "\xff" "B\r\n", 4, 77);
+ check_close(first, 1);
+ check_read(first, 12, "", 0, 6);
+ check_close(first, 0);
+ check_close(second, 1);
+ unbuffered_database_reads_use_aligned_guest_buffers();
+
+ /* The read-only adapter must leave the fixture intact. */
+ file = fopen("fixture.dat", "rb");
+ unsigned char bytes[7];
+ assert(file && fread(bytes, 1, sizeof(bytes), file) == 6 && !fclose(file));
+ assert(!memcmp(bytes, "A\0\xff" "B\r\n", 6));
+ assert(!remove("fixture.dat"));
+#ifdef _WIN32
+ assert(SetCurrentDirectoryA(temporary) && RemoveDirectoryA(directory));
+#else
+ 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");
+}
diff --git a/decompiled/tests/guest_stdio.c b/decompiled/tests/guest_stdio.c
@@ -270,6 +270,16 @@ static void fwrite_returns_element_counts(void)
check_contents("elements.bin", "abcdef", 6);
}
+static void formatted_stdout_uses_guest_arguments(uint32_t stdout_file)
+{
+ uint32_t args[] = {string("Using RdbDSN %s\n"), string("fixture")};
+
+ uint32_t length = call("printf", 2, args);
+ uint32_t flushed = call("fflush", 1, &stdout_file);
+
+ assert(length == 21 && flushed == 0);
+}
+
int main(void)
{
#ifdef _WIN32
@@ -296,6 +306,7 @@ int main(void)
additional_streams_have_independent_buffers_and_descriptors();
fwrite_returns_element_counts();
cexit_preserves_pending_stream_state();
+ formatted_stdout_uses_guest_arguments(stderr_file - 32);
assert(!remove("text.txt") && !remove("binary.bin") && !remove("unbuffered.txt") && !remove("closed.txt") && !remove("exit.txt") && !remove("formatted.txt") && !remove("many.bin") && !remove("elements.bin"));
#ifdef _WIN32
assert(SetCurrentDirectoryA(temporary) && RemoveDirectoryA(directory));
diff --git a/tools/reconstruction/test_portable_files.py b/tools/reconstruction/test_portable_files.py
@@ -0,0 +1,26 @@
+"""Compare native Linux asset file access with Windows using disposable files."""
+import pathlib
+import subprocess
+
+
+root = pathlib.Path(__file__).resolve().parents[2]
+decompiled = root / "decompiled"
+work = root / "tools/reconstruction/work/portable-files"
+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/files.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/files.c", "runtime/linux_file.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())