aorecomp

Anarchy Online classic in the browser

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


commit 6fa03d222a64248e6b97e488cb62a410e30f2c5a
parent 67b28ca1f9831ab4134ea06077125698f6dd8acc
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 02:03:36 +0000

Create native Linux windows through compiled game callbacks

Diffstat:
Mdecompiled/Makefile.linux | 11++++++++---
Mdecompiled/runtime/linux_graphics.c | 5+++++
Mdecompiled/runtime/linux_graphics.h | 2++
Mdecompiled/runtime/linux_imports.c | 3+++
Adecompiled/runtime/linux_window.c | 273+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adecompiled/runtime/linux_window.h | 7+++++++
Mdecompiled/tests/dynamic_imports.c | 4++--
Adecompiled/tests/window.c | 262+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_window.py | 20++++++++++++++++++++
9 files changed, 582 insertions(+), 5 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_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_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_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_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 $@ @@ -140,7 +140,11 @@ build/linux/graphics: tests/graphics.c runtime/linux_graphics.c runtime/gl_rende mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lSDL2 -lGL -o $@ -test: build/linux/graphics build/linux/atoi 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/window: tests/window.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_window.h runtime/linux_graphics.h runtime/gl_renderer.h runtime/guest_heap.h runtime/guest_memory.h runtime/recomp_types.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lSDL2 -lGL -o $@ + +test: build/linux/window build/linux/graphics build/linux/atoi 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 @@ -171,5 +175,6 @@ test: build/linux/graphics build/linux/atoi build/linux/stricmp build/linux/stdi ./build/linux/stricmp ./build/linux/atoi ./build/linux/graphics + ./build/linux/window -include $(OBJECTS:.o=.d) diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -48,6 +48,11 @@ failed: return 0; } +SDL_Window *linux_graphics_window(void) +{ + return initialize_graphics() ? window : NULL; +} + static struct draw_object *find_object(uint32_t address) { for (struct draw_object *object = objects; object; object = object->next) diff --git a/decompiled/runtime/linux_graphics.h b/decompiled/runtime/linux_graphics.h @@ -4,5 +4,7 @@ recomp_func_t linux_graphics_lookup(const char *name); recomp_func_t linux_graphics_virtual(uint32_t address); +struct SDL_Window; +struct SDL_Window *linux_graphics_window(void); #endif diff --git a/decompiled/runtime/linux_imports.c b/decompiled/runtime/linux_imports.c @@ -9,6 +9,7 @@ #include "linux_tls.h" #include "linux_network.h" #include "linux_graphics.h" +#include "linux_window.h" #include <strings.h> /* External calls receive stable guest identifiers rather than host code addresses. */ @@ -120,6 +121,8 @@ recomp_func_t recomp_lookup_import(uint32_t va) if (imports[current_import].name) { recomp_func_t function = linux_platform_lookup(imports[current_import].dll, imports[current_import].name); if (function) return function; + function = linux_window_lookup(imports[current_import].dll, imports[current_import].name); + if (function) return function; if (!strcasecmp(imports[current_import].dll, "kernel32.dll")) { function = library_lookup(imports[current_import].name); if (!function) function = linux_module_lookup(imports[current_import].name); diff --git a/decompiled/runtime/linux_window.c b/decompiled/runtime/linux_window.c @@ -0,0 +1,273 @@ +#include "linux_window.h" +#include "linux_graphics.h" +#include "guest_heap.h" +#include <SDL2/SDL.h> +#include <strings.h> + +/* Guest classes and handles contain no native pointers. SDL owns decoration. */ +static struct window_class { + char *name; + uint32_t atom, instance, procedure, icon, background; + struct window_class *next; +} *classes; +static struct { + struct window_class *class; + uint32_t handle; +} client; +static uint32_t next_atom = 0xc000, next_handle = 0xf4000000u; + +static void unsupported(const char *operation, uint32_t value) +{ + fprintf(stderr, "Unimplemented native window %s: %08x caller=%08x\n", + operation, value, g_cur_func); + exit(3); +} + +static char *window_text(uint32_t address, int wide) +{ + size_t length = 0; + if (address) { + while (wide ? MEM16(address + length * 2) : MEM8(address + length)) ++length; + } + char *text = malloc(length + 1); + if (!text) abort(); + for (size_t i = 0; i < length; ++i) { + uint32_t character = wide ? MEM16(address + i * 2) : MEM8(address + i); + if (character > 127) unsupported("non-ASCII window text", character); + text[i] = character; + } + text[length] = 0; + return text; +} + +static struct window_class *find_class(uint32_t name, uint32_t instance, int wide) +{ + char *text = name > 0xffffu ? window_text(name, wide) : NULL; + struct window_class *found = NULL; + for (struct window_class *c = classes; c; c = c->next) + if (c->instance == instance && + (text ? !strcasecmp(c->name, text) : c->atom == name)) { found = c; break; } + free(text); + return found; +} + +static void load_icon(void) +{ + uint32_t instance = MEM32(g_esp + 4), name = MEM32(g_esp + 8); + /* IDI_APPLICATION means the native window manager's default application icon. */ + if (instance || name != 32512) unsupported("icon resource", name); + g_eax = 0xf4100000u; + g_esp += 12; +} + +static void stock_object(void) +{ + if (MEM32(g_esp + 4) != 4) unsupported("stock object", MEM32(g_esp + 4)); + g_eax = 0xf4100004u; /* BLACK_BRUSH resource; background painting is not implemented yet. */ + g_esp += 8; +} + +static void register_class(int wide) +{ + uint32_t record = MEM32(g_esp + 4), name = MEM32(record + 36), instance = MEM32(record + 16); + g_eax = 0; + if (name <= 0xffffu || !(wide ? MEM16(name) : MEM8(name)) || !MEM32(record + 4)) MEM32(g_fs_base + 0x34) = 87; + else if (find_class(name, instance, wide)) MEM32(g_fs_base + 0x34) = 1410; /* CLASS_ALREADY_EXISTS */ + else { + if (MEM32(record) & ~8u) unsupported("class style", MEM32(record)); + if (MEM32(record + 8) || MEM32(record + 12)) unsupported("class extra bytes", record); + if (MEM32(record + 24) || MEM32(record + 32)) unsupported("class cursor/menu", record); + if (MEM32(record + 20) && MEM32(record + 20) != 0xf4100000u) unsupported("class icon", MEM32(record + 20)); + if (MEM32(record + 28) && MEM32(record + 28) != 0xf4100004u) unsupported("class background", MEM32(record + 28)); + struct window_class *c = calloc(1, sizeof(*c)); + if (!c) abort(); + c->name = window_text(name, wide); + if (next_atom > 0xffffu) abort(); + c->atom = next_atom++; + c->instance = instance; + c->procedure = MEM32(record + 4); + c->icon = MEM32(record + 20); + c->background = MEM32(record + 28); + c->next = classes; + classes = c; + g_eax = c->atom; + } + g_esp += 8; +} + +static void unregister_class(int wide) +{ + struct window_class *c = find_class(MEM32(g_esp + 4), MEM32(g_esp + 8), wide); + g_eax = 0; + if (!c) MEM32(g_fs_base + 0x34) = MEM32(g_esp + 4) <= 0xffffu ? 6 : 1411; + else if (client.class == c) MEM32(g_fs_base + 0x34) = 1412; /* CLASS_HAS_WINDOWS */ + else { + struct window_class **link = &classes; + while (*link != c) link = &(*link)->next; + *link = c->next; + free(c->name); + free(c); + g_eax = 1; + } + g_esp += 12; +} + +static uint32_t send_message(uint32_t message, uint32_t wparam, uint32_t lparam) +{ + uint32_t stack = g_esp, caller = g_cur_func; + uint32_t ebx = g_ebx, esi = g_esi, edi = g_edi, ebp = g_ebp; + recomp_func_t function = recomp_lookup(client.class->procedure); + if (!function) function = recomp_lookup_import(client.class->procedure); + PUSH32(g_esp, lparam); + PUSH32(g_esp, wparam); + PUSH32(g_esp, message); + PUSH32(g_esp, client.handle); + PUSH32(g_esp, RECOMP_RETADDR); + function(); + if (g_esp != stack) { fprintf(stderr, "Window callback unbalanced the guest stack\n"); abort(); } + g_cur_func = caller; + g_ebx = ebx; g_esi = esi; g_edi = edi; g_ebp = ebp; + return g_eax; +} + +static void default_procedure(void) +{ + uint32_t handle = MEM32(g_esp + 4), message = MEM32(g_esp + 8); + uint32_t wparam = MEM32(g_esp + 12); + g_eax = 0; + if (!client.handle || handle != client.handle) MEM32(g_fs_base + 0x34) = 1400; + else switch (message) { + case 0x81: g_eax = 1; break; /* WM_NCCREATE accepts creation. */ + case 0x83: /* SDL's coordinates describe the client, excluding native decoration. */ + if (wparam) unsupported("nonclient resize", wparam); + break; + case 0x24: /* WM_GETMINMAXINFO is initialized by create_window. */ + case 0x01: /* WM_CREATE */ + case 0x02: /* WM_DESTROY */ + case 0x82: /* WM_NCDESTROY */ + break; + default: unsupported("default message", message); + } + g_esp += 20; +} + +static void release_window(void) +{ + SDL_HideWindow(linux_graphics_window()); + client.handle = 0; + client.class = NULL; +} + +static void create_window(int wide) +{ + uint32_t args[12]; + for (unsigned i = 0; i < 12; ++i) args[i] = MEM32(g_esp + 4 + i * 4); + struct window_class *c = find_class(args[1], args[10], wide); + g_eax = 0; + if (!c) { MEM32(g_fs_base + 0x34) = args[1] <= 0xffffu ? 6 : 1407; g_esp += 52; return; } + if (client.handle) unsupported("additional window", client.handle); + if (args[0] || args[3] || args[8] || args[9]) unsupported("creation style/parent/menu", args[3]); + for (unsigned i = 4; i < 8; ++i) + if ((int32_t)args[i] < 0) unsupported("default/negative geometry", args[i]); + SDL_Window *window = linux_graphics_window(); + if (!window) { MEM32(g_fs_base + 0x34) = 8; g_esp += 52; return; } + char *title = window_text(args[2], wide); + SDL_SetWindowTitle(window, title); + free(title); + SDL_SetWindowPosition(window, args[4], args[5]); + SDL_SetWindowSize(window, args[6] ? args[6] : 1, args[7] ? args[7] : 1); + client.class = c; + client.handle = next_handle++; + + /* Win32's 32-bit CREATESTRUCT, RECT and MINMAXINFO live in guest memory. */ + uint32_t record = guest_malloc(104); + if (!record) abort(); + memset((void *)ADDR(record), 0, 104); + static const unsigned fields[] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; + for (unsigned i = 0; i < 12; ++i) MEM32(record + i * 4) = args[fields[i]]; + MEM32(record + 48) = args[4]; MEM32(record + 52) = args[5]; + MEM32(record + 56) = args[4] + args[6]; MEM32(record + 60) = args[5] + args[7]; + SDL_Rect bounds; + if (SDL_GetDisplayUsableBounds(0, &bounds)) { fprintf(stderr, "%s\n", SDL_GetError()); abort(); } + MEM32(record + 72) = bounds.w; MEM32(record + 76) = bounds.h; + MEM32(record + 80) = bounds.x; MEM32(record + 84) = bounds.y; + MEM32(record + 88) = 1; MEM32(record + 92) = 1; + MEM32(record + 96) = bounds.w; MEM32(record + 100) = bounds.h; + uint32_t sizing[14]; + memcpy(sizing, (const void *)ADDR(record + 48), sizeof(sizing)); + send_message(0x24, 0, record + 64); + if (memcmp(sizing + 4, (const void *)ADDR(record + 64), 40)) unsupported("custom size limits", record + 64); + if (!send_message(0x81, 0, record)) { + send_message(0x82, 0, 0); + release_window(); + } else { + send_message(0x83, 0, record + 48); + if (memcmp(sizing, (const void *)ADDR(record + 48), 16)) unsupported("custom client rectangle", record + 48); + if (send_message(0x01, 0, record) == 0xffffffffu) { + send_message(0x02, 0, 0); + send_message(0x82, 0, 0); + release_window(); + } + } + guest_free(record); + g_eax = client.handle; + if (client.handle) fprintf(stderr, "[linux-window] created %08x: %s\n", client.handle, SDL_GetWindowTitle(window)); + g_esp += 52; +} + +static void destroy_window(void) +{ + if (!client.handle || MEM32(g_esp + 4) != client.handle) { + g_eax = 0; + MEM32(g_fs_base + 0x34) = 1400; + } else { + send_message(0x02, 0, 0); + send_message(0x82, 0, 0); + release_window(); + g_eax = 1; + } + g_esp += 8; +} + +static void system_parameters(void) +{ + uint32_t action = MEM32(g_esp + 4), enabled = MEM32(g_esp + 8); + if (MEM32(g_esp + 12) || (MEM32(g_esp + 16) & ~2u)) unsupported("system parameter arguments", action); + if (action == 0x11) { /* SPI_SETSCREENSAVEACTIVE: process-scoped SDL inhibition. */ + if (!linux_graphics_window()) { g_eax = 0; MEM32(g_fs_base + 0x34) = 8; } + else { + if (enabled) SDL_EnableScreenSaver(); + else SDL_DisableScreenSaver(); + g_eax = SDL_IsScreenSaverEnabled() == !!enabled; + } + } else if (action == 0x56) { + /* SPI_SETPOWEROFFACTIVE is unsupported; do not change desktop power policy. */ + g_eax = 0; + MEM32(g_fs_base + 0x34) = 120; /* ERROR_CALL_NOT_IMPLEMENTED */ + } else unsupported("system parameter action", action); + g_esp += 20; +} + +static void register_class_a(void) { register_class(0); } +static void register_class_w(void) { register_class(1); } +static void unregister_class_a(void) { unregister_class(0); } +static void unregister_class_w(void) { unregister_class(1); } +static void create_window_a(void) { create_window(0); } +static void create_window_w(void) { create_window(1); } + +recomp_func_t linux_window_lookup(const char *dll, const char *name) +{ + if (!strcasecmp(dll, "gdi32.dll")) return !strcmp(name, "GetStockObject") ? stock_object : NULL; + if (strcasecmp(dll, "user32.dll")) return NULL; + if (!strcmp(name, "LoadIconA")) return load_icon; + if (!strcmp(name, "RegisterClassA")) return register_class_a; + if (!strcmp(name, "RegisterClassW")) return register_class_w; + if (!strcmp(name, "UnregisterClassA")) return unregister_class_a; + if (!strcmp(name, "UnregisterClassW")) return unregister_class_w; + if (!strcmp(name, "CreateWindowExA")) return create_window_a; + if (!strcmp(name, "CreateWindowExW")) return create_window_w; + if (!strcmp(name, "DefWindowProcA") || !strcmp(name, "DefWindowProcW")) return default_procedure; + if (!strcmp(name, "DestroyWindow")) return destroy_window; + if (!strcmp(name, "SystemParametersInfoA")) return system_parameters; + return NULL; +} diff --git a/decompiled/runtime/linux_window.h b/decompiled/runtime/linux_window.h @@ -0,0 +1,7 @@ +#ifndef LINUX_WINDOW_H +#define LINUX_WINDOW_H +#include "recomp_types.h" + +recomp_func_t linux_window_lookup(const char *dll, const char *name); + +#endif diff --git a/decompiled/tests/dynamic_imports.c b/decompiled/tests/dynamic_imports.c @@ -92,7 +92,7 @@ static void import_names_are_owned_and_calls_are_dispatched(void) static void unresolved_window_call_fails_explicitly(void) { - strcpy((char *)ADDR(0x50000100u), "CreateWindowExW"); + strcpy((char *)ADDR(0x50000100u), "GetMessageW"); uint32_t symbol = call("GetProcAddress", 0xf2000000u, 0x50000100u); int output[2]; assert(!pipe(output)); @@ -114,7 +114,7 @@ static void unresolved_window_call_fails_explicitly(void) assert(waitpid(child, &status, 0) == child); assert(WIFEXITED(status) && WEXITSTATUS(status) == 3); - assert(strstr(message, "user32.dll!CreateWindowExW ordinal=0 caller=280028f0")); + assert(strstr(message, "user32.dll!GetMessageW ordinal=0 caller=280028f0")); } int main(void) diff --git a/decompiled/tests/window.c b/decompiled/tests/window.c @@ -0,0 +1,262 @@ +#include <assert.h> +#include <ctype.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#include <windows.h> +_Static_assert(sizeof(WNDCLASSA) == 40, "guest window class size"); +_Static_assert(sizeof(CREATESTRUCTA) == 48, "guest creation record size"); +static uint32_t scratch[256]; +#else +#include "linux_window.h" +#include "linux_graphics.h" +#include "guest_memory.h" +#include <SDL2/SDL.h> +static void procedure(void); +recomp_func_t recomp_lookup(uint32_t address) { assert(address == 0x11110000u); return procedure; } +recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } +#endif + +static uint32_t instance, callback, atom, args[12], messages[32], message_count, reject; +static int wide; + +static uint32_t workspace(void) +{ +#ifdef _WIN32 + return (uintptr_t)scratch; +#else + return 0x50001000u; +#endif +} + +static uint32_t *memory(uint32_t address) +{ +#ifdef _WIN32 + return (void *)(uintptr_t)address; +#else + return (void *)ADDR(address); +#endif +} + +static uint32_t call(const char *name, unsigned count, const uint32_t *arguments) +{ + char symbol[32]; + strcpy(symbol, name); + if (wide && strcmp(name, "LoadIconA") && symbol[strlen(symbol) - 1] == 'A') symbol[strlen(symbol) - 1] = 'W'; +#ifdef _WIN32 + HMODULE module = GetModuleHandleA(!strcmp(name, "GetStockObject") ? "gdi32.dll" : "user32.dll"); + FARPROC function = GetProcAddress(module, symbol); + assert(function); + if (count == 1) return ((uint32_t (WINAPI *)(uint32_t))function)(arguments[0]); + if (count == 2) return ((uint32_t (WINAPI *)(uint32_t, uint32_t))function)(arguments[0], arguments[1]); + if (count == 4) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t))function)(arguments[0], arguments[1], arguments[2], arguments[3]); + assert(count == 12); + return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, + uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))function)( + arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], + arguments[6], arguments[7], arguments[8], arguments[9], arguments[10], arguments[11]); +#else + uint32_t stack = g_esp, caller = g_cur_func; + uint32_t ebx = g_ebx, esi = g_esi, edi = g_edi, ebp = g_ebp; + g_esp -= (count + 1) * 4; + MEM32(g_esp) = RECOMP_RETADDR; + for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = arguments[i]; + recomp_func_t function = linux_window_lookup(!strcmp(name, "GetStockObject") ? "gdi32.dll" : "user32.dll", symbol); + assert(function); + function(); + assert(g_esp == stack && g_cur_func == caller); + assert(g_ebx == ebx && g_esi == esi && g_edi == edi && g_ebp == ebp); + return g_eax; +#endif +} + +static void write_text(uint32_t address, const char *text) +{ + if (!wide) strcpy((char *)memory(address), text); + else for (size_t i = 0; i <= strlen(text); ++i) ((uint16_t *)memory(address))[i] = text[i]; +} + +static void check_text(uint32_t address, const char *text) +{ + if (!wide) assert(!strcmp((const char *)memory(address), text)); + else for (size_t i = 0; i <= strlen(text); ++i) assert(((uint16_t *)memory(address))[i] == (unsigned char)text[i]); +} + +static uint32_t error(void) +{ +#ifdef _WIN32 + return GetLastError(); +#else + return MEM32(g_fs_base + 0x34); +#endif +} + +static uint32_t receive(uint32_t handle, uint32_t message, uint32_t wparam, uint32_t lparam) +{ + if (message == 0x24 || message == 0x81 || message == 0x83 || + message == 1 || message == 2 || message == 0x82) { + assert(message_count < 32); + messages[message_count++] = message; + } + if (message == 0x81 || message == 1) { + uint32_t *record = memory(lparam); + assert(handle && !wparam); + assert(record[0] == 0x12345678u && record[1] == instance); + assert(!record[2] && !record[3]); + assert(record[4] == 240 && record[5] == 320 && record[6] == 60 && record[7] == 50); + assert(!record[8] && !record[11]); + check_text(record[9], "AO window test"); + if (record[10] <= 0xffffu) assert(record[10] == atom); + else for (unsigned i = 0; i < 14; ++i) { + unsigned character = wide ? ((uint16_t *)memory(record[10]))[i] : ((unsigned char *)memory(record[10]))[i]; + assert(character < 128 && tolower(character) == "ao class test"[i]); + } + if (reject == message) return message == 0x81 ? 0 : 0xffffffffu; + } + uint32_t forwarded[] = {handle, message, wparam, lparam}; + return call("DefWindowProcA", 4, forwarded); +} + +#ifdef _WIN32 +static LRESULT CALLBACK procedure(HWND handle, UINT message, WPARAM wparam, LPARAM lparam) +{ + return receive((uintptr_t)handle, message, wparam, lparam); +} +#else +static void procedure(void) +{ + g_cur_func = 0x11110000u; + g_eax = receive(MEM32(g_esp + 4), MEM32(g_esp + 8), MEM32(g_esp + 12), MEM32(g_esp + 16)); + g_esp += 20; +} +#endif + +static void class_registration_owns_its_name(void) +{ + uint32_t icon_args[] = {0, 32512}, brush_arg = 4; + uint32_t *record = memory(workspace()); + record[0] = 8; record[1] = callback; record[4] = instance; + record[5] = call("LoadIconA", 2, icon_args); + record[7] = call("GetStockObject", 1, &brush_arg); + record[9] = workspace() + 64; + write_text(record[9], "AO class test"); + uint32_t address = workspace(); + + atom = call("RegisterClassA", 1, &address); + uint32_t duplicate = call("RegisterClassA", 1, &address), duplicate_error = error(); + memset(memory(record[9]), 'x', 13); + write_text(workspace() + 96, "aO ClAsS TeSt"); + + assert(atom && atom <= 0xffffu && record[5] && record[7]); + assert(!duplicate && duplicate_error == 1410); +} + +static void create_and_destroy_dispatch_guest_messages(void) +{ + args[1] = workspace() + 96; + args[2] = workspace() + 128; + write_text(args[2], "AO window test"); + args[4] = 50; args[5] = 60; args[6] = 320; args[7] = 240; + args[10] = instance; args[11] = 0x12345678u; + uint32_t unregister_args[] = {atom, instance}; + + uint32_t window = call("CreateWindowExA", 12, args); + uint32_t busy = call("UnregisterClassA", 2, unregister_args), busy_error = error(); +#ifndef _WIN32 + int width, height; + SDL_GetWindowSize(linux_graphics_window(), &width, &height); + assert(width == 320 && height == 240); + assert(!strcmp(SDL_GetWindowTitle(linux_graphics_window()), "AO window test")); + assert(SDL_GetWindowFlags(linux_graphics_window()) & SDL_WINDOW_HIDDEN); + assert(!(SDL_GetWindowFlags(linux_graphics_window()) & SDL_WINDOW_FULLSCREEN)); +#endif + uint32_t destroyed = call("DestroyWindow", 1, &window); + + assert(window && destroyed && !busy && busy_error == 1412); + static const uint32_t expected[] = {0x24, 0x81, 0x83, 1, 2, 0x82}; + assert(message_count == 6 && !memcmp(messages, expected, sizeof(expected))); +} + +static void rejected_creation_releases_the_window(void) +{ + args[1] = atom; + reject = 0x81; + message_count = 0; + + uint32_t rejected_nonclient = call("CreateWindowExA", 12, args); + + static const uint32_t expected_nc[] = {0x24, 0x81, 0x82}; + assert(!rejected_nonclient && message_count == 3); + assert(!memcmp(messages, expected_nc, sizeof(expected_nc))); + + reject = 1; + message_count = 0; + uint32_t rejected_client = call("CreateWindowExA", 12, args); + static const uint32_t expected_create[] = {0x24, 0x81, 0x83, 1, 2, 0x82}; + assert(!rejected_client && message_count == 6); + assert(!memcmp(messages, expected_create, sizeof(expected_create))); +} + +static void class_release_invalidates_its_atom(void) +{ + uint32_t unregister_args[] = {atom, instance}; + + uint32_t released = call("UnregisterClassA", 2, unregister_args); + uint32_t missing = call("UnregisterClassA", 2, unregister_args), missing_error = error(); + uint32_t window = call("CreateWindowExA", 12, args), create_error = error(); + + assert(released && !missing && missing_error == 6); + assert(!window && create_error == 6); +} + +#ifndef _WIN32 +static void screensaver_inhibition_is_process_scoped(void) +{ + uint32_t parameters[] = {0x11, 1, 0, 2}; + SDL_bool original = SDL_IsScreenSaverEnabled(); + + assert(call("SystemParametersInfoA", 4, parameters)); + assert(SDL_IsScreenSaverEnabled()); + parameters[1] = 0; + uint32_t inhibited = call("SystemParametersInfoA", 4, parameters); + parameters[0] = 0x56; + uint32_t power_policy = call("SystemParametersInfoA", 4, parameters), power_error = error(); + + assert(inhibited && !SDL_IsScreenSaverEnabled()); + assert(!power_policy && power_error == 120); + if (original) SDL_EnableScreenSaver(); +} +#endif + +int main(void) +{ +#ifdef _WIN32 + assert(LoadLibraryA("user32.dll") && LoadLibraryA("gdi32.dll")); + instance = (uintptr_t)GetModuleHandleA(NULL); + callback = (uintptr_t)procedure; +#else + assert(guest_map("window test", 0x50000000u, 0x10000u)); + g_fs_base = 0x50004000u; + g_esp = 0x5000ff00u; + g_cur_func = 0x28004c70u; + g_ebx = 0x12345678u; g_esi = 0x23456789u; + g_edi = 0x3456789au; g_ebp = 0x456789abu; + instance = 0x28000000u; + callback = 0x11110000u; +#endif + for (wide = 0; wide < 2; ++wide) { + message_count = 0; reject = 0; + memset(memory(workspace()), 0, 1024); + class_registration_owns_its_name(); + create_and_destroy_dispatch_guest_messages(); + rejected_creation_releases_the_window(); + class_release_invalidates_its_atom(); + } +#ifndef _WIN32 + wide = 0; + screensaver_inhibition_is_process_scoped(); +#endif + puts("window: ANSI/UTF-16 class ownership, creation callbacks, rejection and destruction passed"); + return 0; +} diff --git a/tools/reconstruction/test_portable_window.py b/tools/reconstruction/test_portable_window.py @@ -0,0 +1,20 @@ +"""Compare window creation callbacks against 32-bit Windows USER32.""" +import pathlib +import subprocess + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-window" +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/window.c", "-luser32", "-lgdi32", "-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/window"], check=True) +actual = subprocess.check_output(wsl + ["env", "GALLIUM_DRIVER=d3d12", "./build/linux/window"], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux: " + actual.strip())