commit 01a6d9172f0b9628dad5693955c6db3e9e1af06f
parent 4392423694c90841681544faf1b45131b3c55701
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 04:27:54 +0000
Load native icons from reconstructed data and support guest getenv
Diffstat:
7 files changed, 295 insertions(+), 6 deletions(-)
diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux
@@ -140,14 +140,15 @@ build/linux/graphics: tests/graphics.c runtime/linux_graphics.c runtime/linux_wi
mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lSDL2 -lGL -o $@
-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
+build/linux/window: tests/window.c data/Clientr_exe.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 $@
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lSDL2 -lGL -lX11 -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
+ ./build/linux/getenv
./build/linux/linux-platform
./build/linux/msvcp-strings
./build/linux/ace-token
@@ -192,3 +193,9 @@ build/linux/devices: tests/devices.c runtime/linux_graphics.c runtime/linux_wind
test: build/linux/devices
-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
+ mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
+
+test: build/linux/getenv
diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c
@@ -464,6 +464,33 @@ static void crt_strrchr(void)
g_esp += 4;
}
+static void crt_getenv(void)
+{
+ uint32_t name = MEM32(g_esp + 4);
+ g_esp += 4;
+ g_eax = 0;
+ if (!name) { MEM32(guest_crt_errno_address()) = 22; return; }
+ const unsigned char *query = (const unsigned char *)ADDR(name);
+ size_t length = strlen((const char *)query);
+ uint32_t environment = MEM32(guest_crt_data("_environ"));
+ for (uint32_t i = 0; MEM32(environment + i * 4); ++i) {
+ uint32_t entry = MEM32(environment + i * 4);
+ const unsigned char *value = (const unsigned char *)ADDR(entry);
+ size_t matched = 0;
+ while (matched < length) {
+ unsigned a = query[matched], b = value[matched];
+ if (a >= 'A' && a <= 'Z') a += 'a' - 'A';
+ if (b >= 'A' && b <= 'Z') b += 'a' - 'A';
+ if (a != b) break;
+ ++matched;
+ }
+ if (matched == length && value[matched] == '=') {
+ g_eax = entry + length + 1;
+ return;
+ }
+ }
+}
+
static void crt_tolower(void)
{
g_eax = MEM32(g_esp + 4);
@@ -687,6 +714,7 @@ recomp_func_t guest_crt_lookup(const char *name)
{"__set_app_type", crt_set_app_type}, {"__p__fmode", crt_p_fmode},
{"__p__commode", crt_p_commode}, {"__setusermatherr", crt_setusermatherr},
{"__getmainargs", crt_getmainargs},
+ {"getenv", crt_getenv},
{"_controlfp", crt_controlfp},
{"malloc", crt_malloc}, {"free", crt_free}, {"realloc", crt_realloc},
{"srand", crt_srand}, {"rand", crt_rand},
diff --git a/decompiled/runtime/linux_window.c b/decompiled/runtime/linux_window.c
@@ -21,6 +21,11 @@ static struct display_context {
struct display_context *next;
} *display_contexts;
static uint32_t next_context = 0xf4300000u;
+static struct window_icon {
+ uint32_t instance, name, handle;
+ SDL_Surface *pixels;
+ struct window_icon *next;
+} *icons;
struct SDL_Window *linux_window_native(uint32_t handle)
{
@@ -62,13 +67,60 @@ static struct window_class *find_class(uint32_t name, uint32_t instance, int wid
return found;
}
+static uint32_t icon_resource(uint32_t instance, uint32_t type, uint32_t name)
+{
+ /* Only resource-directory metadata and data are read, never executable bytes. */
+ uint32_t root = instance + MEM32(instance + MEM32(instance + 0x3c) + 24 + 112);
+ uint32_t directory = root, keys[] = {type, name, 1033};
+ for (unsigned level = 0; level < 3; ++level) {
+ uint32_t named = MEM16(directory + 12), count = MEM16(directory + 14), entry = 0;
+ for (uint32_t i = 0; i < count; ++i) {
+ uint32_t address = directory + 16 + (named + i) * 8;
+ if (MEM32(address) == keys[level]) { entry = MEM32(address + 4); break; }
+ }
+ if (!entry) return 0;
+ if (!!(entry & 0x80000000u) != (level != 2)) unsupported("resource directory", entry);
+ directory = root + (entry & 0x7fffffffu);
+ }
+ return instance + MEM32(directory);
+}
+
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;
+ /* IDI_APPLICATION means the native window manager's default application icon. */
+ if (!instance && name == 32512) { g_eax = 0xf4100000u; return; }
+ if (!instance || name > 0xffff) unsupported("icon resource", name);
+ for (struct window_icon *icon = icons; icon; icon = icon->next)
+ if (icon->instance == instance && icon->name == name) { g_eax = icon->handle; return; }
+ uint32_t group = icon_resource(instance, 14, name);
+ g_eax = 0;
+ if (!group) { MEM32(g_fs_base + 0x34) = 1813; return; }
+ if (MEM16(group) || MEM16(group + 2) != 1 || MEM16(group + 4) != 1 ||
+ MEM8(group + 6) != 32 || MEM8(group + 7) != 32 || MEM16(group + 12) != 4)
+ unsupported("icon group format", name);
+ uint32_t dib = icon_resource(instance, 3, MEM16(group + 18));
+ if (!dib || MEM32(group + 14) != 744 || MEM32(dib) != 40 || MEM32(dib + 4) != 32 ||
+ MEM32(dib + 8) != 64 || MEM16(dib + 12) != 1 || MEM16(dib + 14) != 4 || MEM32(dib + 16))
+ unsupported("icon bitmap format", name);
+
+ /* AO's four-bit icon has a BGR palette, bottom-up XOR rows and a one-bit AND mask. */
+ struct window_icon *icon = calloc(1, sizeof(*icon));
+ if (!icon) abort();
+ icon->pixels = SDL_CreateRGBSurfaceWithFormat(0, 32, 32, 32, SDL_PIXELFORMAT_ARGB8888);
+ if (!icon->pixels) abort();
+ for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
+ uint32_t index = (MEM8(dib + 104 + (31 - y) * 16 + x / 2) >> (x & 1 ? 0 : 4)) & 15;
+ uint32_t color = MEM32(dib + 40 + index * 4) & 0xffffff;
+ int masked = MEM8(dib + 616 + (31 - y) * 4 + x / 8) & (0x80 >> (x & 7));
+ if (masked && color) unsupported("inverting icon pixel", name);
+ ((uint32_t *)icon->pixels->pixels)[y * 32 + x] = color | (masked ? 0 : 0xff000000u);
+ }
+ icon->instance = instance; icon->name = name;
+ icon->handle = icons ? icons->handle + 1 : 0xf4400000u;
+ icon->next = icons; icons = icon;
+ g_eax = icon->handle;
}
static void stock_object(void)
@@ -424,6 +476,21 @@ static void set_window_long(void)
g_esp += 16;
}
+static void set_class_long(void)
+{
+ uint32_t handle = MEM32(g_esp + 4), index = MEM32(g_esp + 8), value = MEM32(g_esp + 12);
+ g_esp += 16;
+ g_eax = 0;
+ if (!client.handle || handle != client.handle) { MEM32(g_fs_base + 0x34) = 1400; return; }
+ if (index != 0xfffffff2u) unsupported("class field", index);
+ struct window_icon *icon = icons;
+ while (icon && icon->handle != value) icon = icon->next;
+ if (!icon) unsupported("class icon handle", value);
+ g_eax = client.class->icon;
+ client.class->icon = value;
+ SDL_SetWindowIcon(linux_graphics_window(), icon->pixels);
+}
+
static void get_window_long(void)
{
uint32_t handle = MEM32(g_esp + 4), index = MEM32(g_esp + 8);
@@ -663,6 +730,7 @@ recomp_func_t linux_window_lookup(const char *dll, const char *name)
if (!strcmp(name, "SetFocus")) return set_focus;
if (!strcmp(name, "GetFocus")) return get_focus;
if (!strcmp(name, "SetWindowLongA") || !strcmp(name, "SetWindowLongW")) return set_window_long;
+ if (!strcmp(name, "SetClassLongA") || !strcmp(name, "SetClassLongW")) return set_class_long;
if (!strcmp(name, "GetWindowLongA") || !strcmp(name, "GetWindowLongW")) return get_window_long;
if (!strcmp(name, "AdjustWindowRectEx")) return adjust_window_rect;
if (!strcmp(name, "GetSystemMetrics")) return system_metrics;
diff --git a/decompiled/tests/getenv.c b/decompiled/tests/getenv.c
@@ -0,0 +1,86 @@
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#ifdef _WIN32
+#include <windows.h>
+static char *(__cdecl *get_variable)(const char *);
+static int *(__cdecl *crt_errno)(void);
+#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 char *lookup(const char *name)
+{
+#ifdef _WIN32
+ return get_variable(name);
+#else
+ if (name) strcpy((char *)ADDR(0x50000100u), name);
+ g_esp = 0x5000ff00u;
+ MEM32(g_esp) = RECOMP_RETADDR;
+ MEM32(g_esp + 4) = name ? 0x50000100u : 0;
+ g_ebx = 0x12345678; g_esi = 0x23456789; g_edi = 0x3456789a; g_ebp = 0x456789ab;
+ guest_crt_lookup("getenv")();
+ assert(g_esp == 0x5000ff04u && MEM32(g_esp) == (name ? 0x50000100u : 0));
+ assert(g_ebx == 0x12345678 && g_esi == 0x23456789 && g_edi == 0x3456789a && g_ebp == 0x456789ab);
+ return (char *)ADDR(g_eax);
+#endif
+}
+
+int main(void)
+{
+ char setting[] = "AO_NAME=Setupf";
+ char *environment[] = {setting, "AO_NAME_MORE=suffix", "Empty=", "A=B=C",
+ "MiXeD=first", "mixed=second", "=C:=C:\\fake", "\xc0VAR=extended", NULL};
+ const struct { const char *name, *value; int entry; } cases[] = {
+ {"AO_NAME", "Setupf", 0}, {"ao_name", "Setupf", 0}, {"AO", NULL, -1},
+ {"AO_NAME_MORE", "suffix", 1}, {"empty", "", 2}, {"A", "B=C", 3},
+ {"A=B", "C", 3}, {"mixed", "first", 4}, {"=C:", "C:\\fake", 6},
+ {"", "C:=C:\\fake", 6}, {"MISSING", NULL, -1}, {NULL, NULL, -1},
+ {"\xc0var", "extended", 7}, {"\xe0VAR", NULL, -1}
+ };
+#ifdef _WIN32
+ HMODULE module = LoadLibraryA("msvcrt.dll");
+ get_variable = (void *)GetProcAddress(module, "getenv");
+ crt_errno = (void *)GetProcAddress(module, "_errno");
+ char ***table = (void *)GetProcAddress(module, "_environ");
+ assert(get_variable && crt_errno && table);
+ get_variable("AO_UNIT_ABSENT");
+ char **saved = *table;
+ *table = environment;
+ int *error = crt_errno();
+#else
+ assert(guest_map("getenv test", 0x50000000u, 0x10000u));
+ g_fs_base = 0x50004000u;
+ char *arguments[] = {"./game", NULL};
+ guest_crt_startup(1, arguments, environment);
+ int *error = (void *)ADDR(guest_crt_errno_address());
+#endif
+ for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) {
+ *error = 77;
+
+ char *value = lookup(cases[i].name);
+
+ assert(*error == (cases[i].name ? 77 : 22));
+ if (!cases[i].value) assert(!value);
+ else {
+ assert(value && !strcmp(value, cases[i].value));
+#ifdef _WIN32
+ assert(value == environment[cases[i].entry] + strlen(cases[i].name) + 1);
+#else
+ uint32_t table = MEM32(guest_crt_data("_environ"));
+ assert(g_eax == MEM32(table + cases[i].entry * 4) + strlen(cases[i].name) + 1);
+#endif
+ }
+ }
+ lookup("AO_NAME")[0] = 'X';
+ assert(!strcmp(lookup("ao_name"), "Xetupf"));
+#ifdef _WIN32
+ *table = saved;
+#endif
+ puts("getenv: case folding, exact prefixes, empty values, duplicate order, guest pointers, writable values and errno passed");
+ return 0;
+}
diff --git a/decompiled/tests/window.c b/decompiled/tests/window.c
@@ -8,15 +8,22 @@
_Static_assert(sizeof(WNDCLASSA) == 40, "guest window class size");
_Static_assert(sizeof(CREATESTRUCTA) == 48, "guest creation record size");
static uint32_t scratch[256];
+uint32_t guest_map(const char *name, uint32_t base, uint32_t size)
+{
+ assert(VirtualAlloc((void *)(uintptr_t)base, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE) == (void *)(uintptr_t)base);
+ return size;
+}
#else
#include "linux_window.h"
#include "linux_graphics.h"
#include "guest_memory.h"
#include <SDL2/SDL.h>
+#include <SDL2/SDL_syswm.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
+void game_data_Clientr_exe(void);
static uint32_t instance, callback, atom, args[12], messages[32], message_count, reject;
static int wide;
@@ -405,6 +412,71 @@ static void window_geometry_preserves_requested_client_area(void)
assert(call("DestroyWindow", 1, &window));
}
+static void reconstructed_icon_matches_native_window_pixels(void)
+{
+ uint32_t icon_args[] = {0x28000000u, 120}, default_args[] = {0, 32512};
+ uint32_t window = call("CreateWindowExA", 12, args);
+ assert(window);
+ uint32_t icon = call("LoadIconA", 2, icon_args), expected_previous = call("LoadIconA", 2, default_args);
+ assert(icon && call("LoadIconA", 2, icon_args) == icon);
+ uint32_t set[] = {window, 0xfffffff2u, icon};
+ uint32_t rgba[1024];
+
+ uint32_t previous = call("SetClassLongA", 3, set);
+#ifdef _WIN32
+ BITMAPINFO info = {0};
+ info.bmiHeader.biSize = 40; info.bmiHeader.biWidth = 32; info.bmiHeader.biHeight = -32;
+ info.bmiHeader.biPlanes = 1; info.bmiHeader.biBitCount = 32;
+ uint32_t *pixels;
+ HDC dc = CreateCompatibleDC(NULL);
+ HBITMAP bitmap = CreateDIBSection(dc, &info, DIB_RGB_COLORS, (void **)&pixels, NULL, 0);
+ assert(dc && bitmap);
+ HGDIOBJ original = SelectObject(dc, bitmap);
+ memset(pixels, 0, sizeof(rgba));
+ assert(DrawIconEx(dc, 0, 0, (HICON)(uintptr_t)icon, 32, 32, 0, NULL, DI_NORMAL));
+ GdiFlush();
+ memcpy(rgba, pixels, sizeof(rgba));
+ memset(pixels, 0xff, sizeof(rgba));
+ assert(DrawIconEx(dc, 0, 0, (HICON)(uintptr_t)icon, 32, 32, 0, NULL, DI_NORMAL));
+ GdiFlush();
+ for (unsigned i = 0; i < 1024; ++i) {
+ uint32_t rgb = rgba[i] & 0xffffff;
+ assert((pixels[i] & 0xffffff) == rgb || (pixels[i] & 0xffffff) == (rgb ^ 0xffffff));
+ rgba[i] = rgb | ((pixels[i] & 0xffffff) == rgb ? 0xff000000u : 0);
+ }
+ SelectObject(dc, original); DeleteObject(bitmap); DeleteDC(dc);
+#else
+ SDL_SysWMinfo info;
+ SDL_VERSION(&info.version);
+ assert(SDL_GetWindowWMInfo(linux_graphics_window(), &info) && info.subsystem == SDL_SYSWM_X11);
+ Atom property = XInternAtom(info.info.x11.display, "_NET_WM_ICON", False), type;
+ int format;
+ unsigned long count, remaining;
+ unsigned char *pixels;
+ assert(!XGetWindowProperty(info.info.x11.display, info.info.x11.window, property,
+ 0, 1026, False, AnyPropertyType, &type, &format, &count, &remaining, &pixels));
+ assert(format == 32 && count == 1026 && !remaining);
+ unsigned long *values = (void *)pixels;
+ assert(values[0] == 32 && values[1] == 32);
+ for (unsigned i = 0; i < 1024; ++i) rgba[i] = values[i + 2];
+ XFree(pixels);
+#endif
+
+ assert(previous == expected_previous && call("SetClassLongA", 3, set) == icon);
+ uint32_t digest = 2166136261u, transparent = 0;
+ for (unsigned i = 0; i < 1024; ++i) {
+ transparent += rgba[i] >> 24 == 0;
+ for (unsigned byte = 0; byte < 4; ++byte) digest = (digest ^ ((rgba[i] >> (byte * 8)) & 255)) * 16777619u;
+ }
+ assert(digest == 0x0f22d6ecu && transparent == 646);
+ printf("icon: 1024 ARGB pixels, digest=%08x, transparent=%u\n", digest, transparent);
+ icon_args[1] = 121;
+ uint32_t missing = call("LoadIconA", 2, icon_args), missing_error = error();
+ assert(!missing && missing_error == 1813);
+ assert(call("DestroyWindow", 1, &window));
+ assert(!call("SetClassLongA", 3, set) && error() == 1400);
+}
+
static void class_release_invalidates_its_atom(void)
{
uint32_t unregister_args[] = {atom, instance};
@@ -464,6 +536,7 @@ static void screensaver_inhibition_is_process_scoped(void)
int main(void)
{
+ game_data_Clientr_exe();
#ifdef _WIN32
assert(LoadLibraryA("user32.dll") && LoadLibraryA("gdi32.dll"));
instance = (uintptr_t)GetModuleHandleA(NULL);
@@ -487,6 +560,7 @@ int main(void)
focus_activation_and_reentrant_callbacks();
window_geometry_preserves_requested_client_area();
visibility_dispatches_callbacks_and_preserves_geometry();
+ reconstructed_icon_matches_native_window_pixels();
class_release_invalidates_its_atom();
}
wide = 0;
diff --git a/tools/reconstruction/test_portable_getenv.py b/tools/reconstruction/test_portable_getenv.py
@@ -0,0 +1,26 @@
+"""Compare guest environment lookup with Windows x86 MSVCRT."""
+import pathlib
+import subprocess
+
+
+root = pathlib.Path(__file__).resolve().parents[2]
+decompiled = root / "decompiled"
+work = root / "tools/reconstruction/work/portable-getenv"
+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/getenv.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/getenv.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())
diff --git a/tools/reconstruction/test_portable_window.py b/tools/reconstruction/test_portable_window.py
@@ -9,7 +9,7 @@ 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),
+ "-O2", "-Iruntime", "tests/window.c", "data/Clientr_exe.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:]