commit 56240f54c987d639080b188a733068a837ed75b9
parent c0098bd8f0bde327dc6d16e2a22002ccb28a2980
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 10:28:38 +0000
Implement guest C locale data and per-thread IPv4 formatting
Diffstat:
6 files changed, 239 insertions(+), 2 deletions(-)
diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux
@@ -197,6 +197,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric
./build/linux/stdio-input
./build/linux/stricmp
./build/linux/strncmp
+ ./build/linux/locale
./build/linux/atoi
./build/linux/graphics
./build/linux/window
@@ -285,3 +286,8 @@ build/linux/mutex: tests/mutex.c runtime/linux_sync.c runtime/guest_memory.c run
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -o $@
test: build/linux/mutex
+
+build/linux/locale: tests/locale.c runtime/guest_crt.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_crt.h runtime/guest_heap.h runtime/guest_memory.h
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
+
+test: build/linux/locale
diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c
@@ -106,6 +106,12 @@ uint32_t guest_crt_data(const char *name)
if (!initialized) {
if (!guest_map("CRT data", 0x08500000u, 0x10000u)) abort();
MEM32(0x08500030u) = 1;
+ MEM8(0x08500100u) = 'C';
+ /* VC6 lconv has ten guest string pointers followed by eight CHAR_MAX fields. */
+ MEM8(0x08500104u) = '.';
+ for (unsigned i = 0; i < 10; ++i)
+ MEM32(0x08500120u + i * 4) = i ? 0x08500106u : 0x08500104u;
+ memset((void *)ADDR(0x08500148u), 127, 8);
/* MSVCRT _iobuf is eight DWORDs; host FILE objects never enter this array. */
MEM32(0x08501000u) = 0x08502000u;
MEM32(0x08501008u) = 0x08502000u;
@@ -757,6 +763,32 @@ static void crt_atoi(void)
g_esp += 4;
}
+static void crt_setlocale(void)
+{
+ int32_t category = MEM32(g_esp + 4);
+ uint32_t locale = MEM32(g_esp + 8);
+ g_esp += 4;
+ g_eax = 0;
+ if (category < 0 || category > 5) {
+ MEM32(guest_crt_errno_address()) = 22;
+ return;
+ }
+ if (locale && strcmp((char *)ADDR(locale), "C")) {
+ fprintf(stderr, "Unsupported guest CRT locale: %s\n", (char *)ADDR(locale));
+ abort();
+ }
+ /* Every supported category retains the initial C locale in guest storage. */
+ guest_crt_data("__lc_codepage");
+ g_eax = 0x08500100u;
+}
+
+static void crt_localeconv(void)
+{
+ guest_crt_data("__lc_codepage");
+ g_eax = 0x08500120u;
+ g_esp += 4;
+}
+
recomp_func_t guest_crt_lookup(const char *name)
{
static const struct { const char *name; recomp_func_t function; } functions[] = {
@@ -793,6 +825,8 @@ recomp_func_t guest_crt_lookup(const char *name)
{"memcmp", crt_memcmp}, {"memchr", crt_memchr},
{"sscanf", crt_sscanf},
{"atoi", crt_atoi},
+ {"setlocale", crt_setlocale},
+ {"localeconv", crt_localeconv},
};
for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i)
if (!strcmp(name, functions[i].name)) return functions[i].function;
diff --git a/decompiled/runtime/linux_network.c b/decompiled/runtime/linux_network.c
@@ -16,6 +16,16 @@ static void network_short(void)
g_esp += 8;
}
+static void address_string(void)
+{
+ struct in_addr address = {MEM32(g_esp + 4)};
+ /* The returned string belongs to the guest thread, not libc's host TLS. */
+ g_eax = g_fs_base + 0x2100;
+ if (!inet_ntop(AF_INET, &address, (char *)ADDR(g_eax), 16)) abort();
+ MEM32(g_fs_base + 0x34) = 0;
+ g_esp += 8;
+}
+
static void wsa_startup(void)
{
uint16_t requested = MEM32(g_esp + 4);
@@ -90,6 +100,7 @@ recomp_func_t linux_network_lookup(const char *name, uint32_t ordinal)
{"gethostname", 57, host_name},
{"htonl", 8, network_long}, {"htons", 9, network_short},
{"ntohl", 14, network_long}, {"ntohs", 15, network_short},
+ {"inet_ntoa", 12, address_string},
};
for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i)
if (name ? !strcmp(name, functions[i].name) : ordinal == functions[i].ordinal)
diff --git a/decompiled/tests/locale.c b/decompiled/tests/locale.c
@@ -0,0 +1,101 @@
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include "guest_crt.h"
+#include "guest_memory.h"
+recomp_func_t recomp_lookup(uint32_t address) { abort(); }
+recomp_func_t recomp_lookup_import(uint32_t address) { abort(); }
+#endif
+
+static void conversion_data(void)
+{
+#ifdef _WIN32
+ void *(__cdecl *convert)(void) = (void *)GetProcAddress(GetModuleHandleA("msvcrt.dll"), "localeconv");
+ int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(GetModuleHandleA("msvcrt.dll"), "_errno");
+ *get_errno() = 777;
+#else
+ MEM32(guest_crt_errno_address()) = 777;
+ g_esp = 0x5000ff00;
+#endif
+
+#ifdef _WIN32
+ const unsigned char *data = convert();
+#else
+ guest_crt_lookup("localeconv")();
+ const unsigned char *data = (void *)ADDR(g_eax);
+#endif
+
+ for (unsigned i = 0; i < 10; ++i) {
+ uint32_t pointer;
+ memcpy(&pointer, data + i * 4, 4);
+#ifdef _WIN32
+ const char *value = (const char *)(uintptr_t)pointer;
+#else
+ const char *value = (const char *)ADDR(pointer);
+#endif
+ assert(!strcmp(value, i ? "" : "."));
+ }
+ for (unsigned i = 40; i < 48; ++i) assert(data[i] == 127);
+#ifdef _WIN32
+ assert(*get_errno() == 777 && convert() == data);
+#else
+ assert(MEM32(guest_crt_errno_address()) == 777 && g_esp == 0x5000ff04);
+ g_esp = 0x5000ff00;
+ guest_crt_lookup("localeconv")();
+ assert((void *)ADDR(g_eax) == data);
+#endif
+}
+
+int main(void)
+{
+#ifdef _WIN32
+ HMODULE library = LoadLibraryA("msvcrt.dll");
+ char *(__cdecl *set_locale)(int, const char *) = (void *)GetProcAddress(library, "setlocale");
+ int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(library, "_errno");
+ assert(set_locale && get_errno);
+#else
+ assert(guest_map("locale test", 0x50000000, 0x10000));
+ g_fs_base = 0x50000000;
+ strcpy((char *)ADDR(0x50000100), "C");
+#endif
+ for (int category = -1; category <= 6; ++category) {
+ for (int query = 0; query < 2; ++query) {
+#ifdef _WIN32
+ *get_errno() = 777;
+#else
+ MEM32(guest_crt_errno_address()) = 777;
+ g_esp = 0x5000ff00;
+ MEM32(g_esp + 4) = category;
+ MEM32(g_esp + 8) = query ? 0 : 0x50000100;
+ g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456;
+#endif
+
+#ifdef _WIN32
+ const char *result = set_locale(category, query ? NULL : "C");
+ int error = *get_errno();
+#else
+ guest_crt_lookup("setlocale")();
+ const char *result = g_eax ? (char *)ADDR(g_eax) : NULL;
+ int error = MEM32(guest_crt_errno_address());
+#endif
+
+ if (category < 0 || category > 5) {
+ assert(!result && error == 22);
+ } else {
+ assert(result && !strcmp(result, "C") && error == 777);
+ }
+#ifndef _WIN32
+ assert(g_esp == 0x5000ff04);
+ assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456);
+ assert(MEM32(g_esp) == (uint32_t)category);
+#endif
+ }
+ }
+ conversion_data();
+ puts("locale: six C-locale categories, queries, invalid categories, errno and guest ABI passed");
+ puts("localeconv: VC6 48-byte layout, ten strings, eight monetary fields, stable storage and errno passed");
+}
diff --git a/decompiled/tests/network_startup.c b/decompiled/tests/network_startup.c
@@ -8,17 +8,18 @@
#include "linux_network.h"
#include "guest_memory.h"
#include <sys/utsname.h>
+#include <pthread.h>
static uint32_t call(uint32_t ordinal, unsigned count, const uint32_t *arguments)
{
- g_esp = 0x5000ff00u;
+ g_esp = g_fs_base + 0xef00;
for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = arguments[i];
g_ebx = 0x12345678;
g_esi = 0x23456789;
g_edi = 0x3456789a;
g_ebp = 0x456789ab;
linux_network_lookup(NULL, ordinal)();
- assert(g_esp == 0x5000ff00u + (count + 1) * 4);
+ assert(g_esp == g_fs_base + 0xef00 + (count + 1) * 4);
assert(g_ebx == 0x12345678 && g_esi == 0x23456789);
assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
return g_eax;
@@ -133,6 +134,63 @@ static void hostname_queries(int initialized)
}
}
+static const char *format_address(uint32_t address)
+{
+#ifdef _WIN32
+ struct in_addr input;
+ input.s_addr = address;
+ return inet_ntoa(input);
+#else
+ return (char *)ADDR(call(12, 1, &address));
+#endif
+}
+
+#ifdef _WIN32
+static DWORD WINAPI format_worker(void *argument)
+#else
+static void *format_worker(void *argument)
+#endif
+{
+#ifndef _WIN32
+ g_fs_base = 0x50010000;
+#endif
+ assert(!strcmp(format_address(0xffffffff), "255.255.255.255"));
+ return 0;
+}
+
+static void address_strings(void)
+{
+ const struct { uint32_t address; const char *text; } cases[] = {
+ {0, "0.0.0.0"}, {0xffffffff, "255.255.255.255"},
+ {0x0100007f, "127.0.0.1"}, {0x2a6433c6, "198.51.100.42"},
+ {0x010200c0, "192.0.2.1"},
+ };
+ for (unsigned i = 0; i < 5; ++i) {
+ set_error(777);
+
+ const char *text = format_address(cases[i].address);
+
+ assert(text && !strcmp(text, cases[i].text));
+ assert(get_error() == 0);
+ }
+
+ const char *main_text = format_address(0x0100007f);
+
+#ifdef _WIN32
+ HANDLE thread = CreateThread(NULL, 0, format_worker, NULL, 0, NULL);
+ assert(thread && WaitForSingleObject(thread, 5000) == WAIT_OBJECT_0);
+ assert(CloseHandle(thread));
+#else
+ pthread_t thread;
+ assert(!pthread_create(&thread, NULL, format_worker, NULL));
+ assert(!pthread_join(thread, NULL));
+#endif
+
+ assert(!strcmp(main_text, "127.0.0.1"));
+ assert(format_address(0) == main_text);
+ assert(!strcmp(main_text, "0.0.0.0"));
+}
+
static void byte_order(void)
{
const char *names[] = {"htonl", "htons", "ntohl", "ntohs"};
@@ -172,13 +230,17 @@ int main(void)
HMODULE library = LoadLibraryA("ws2_32.dll");
assert(GetProcAddress(library, "WSAStartup") == GetProcAddress(library, (const char *)115));
assert(GetProcAddress(library, "gethostname") == GetProcAddress(library, (const char *)57));
+ assert(GetProcAddress(library, "inet_ntoa") == GetProcAddress(library, (const char *)12));
#else
assert(guest_map("network test memory", 0x50000000u, 0x10000u));
+ assert(guest_map("network worker memory", 0x50010000u, 0x10000u));
g_fs_base = 0x50001000u;
assert(linux_network_lookup("WSAStartup", 0) == linux_network_lookup(NULL, 115));
assert(linux_network_lookup("gethostname", 0) == linux_network_lookup(NULL, 57));
+ assert(linux_network_lookup("inet_ntoa", 0) == linux_network_lookup(NULL, 12));
#endif
byte_order();
+ address_strings();
hostname_queries(0);
cleanup(-1);
const struct { uint16_t version; uint32_t error; } versions[] = {
@@ -190,6 +252,7 @@ int main(void)
assert(initialized == 9);
hostname_queries(1);
byte_order();
+ address_strings();
for (unsigned i = 0; i < initialized; ++i) cleanup(0);
cleanup(-1);
assert(startup(2, 0) == 0);
@@ -200,5 +263,6 @@ int main(void)
printf("network startup: 12 negotiations, WSADATA32 layout, nested cleanup and errors, digest %08x\n", digest);
puts("hostname: 24 buffer and initialization cases, native host name and guest ABI passed");
puts("byte order: all 16-bit values and six 32-bit cases, before/after startup, errors preserved");
+ puts("inet_ntoa: five addresses, buffer reuse, per-thread storage and cleared errors before/after startup");
return 0;
}
diff --git a/tools/reconstruction/test_portable_locale.py b/tools/reconstruction/test_portable_locale.py
@@ -0,0 +1,21 @@
+"""Compare guest C-locale behavior with Windows MSVCRT."""
+import pathlib
+import subprocess
+
+
+root = pathlib.Path(__file__).resolve().parents[2]
+decompiled = root / "decompiled"
+work = root / "tools/reconstruction/work/portable-locale"
+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/locale.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:]
+wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"]
+subprocess.run(wsl + ["make", "-s", "-f", "Makefile.linux", "build/linux/locale"], check=True)
+actual = subprocess.check_output(wsl + ["./build/linux/locale"], text=True, timeout=30)
+assert actual == expected, (expected, actual)
+print("Windows == Linux contract: " + actual.strip())