commit 123c50f43e75bba2c6566a84e727ae2a08b1d817
parent 0b4d79b6b4c1caabd5ff0d7865aa3456b66cc167
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 12:00:31 +0000
Implement guest C-locale whitespace classification
Diffstat:
4 files changed, 89 insertions(+), 0 deletions(-)
diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux
@@ -201,6 +201,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric
./build/linux/splitpath
./build/linux/atoi
./build/linux/atof
+ ./build/linux/ctype
./build/linux/graphics
./build/linux/window
./build/linux/surfaces
@@ -303,3 +304,8 @@ build/linux/atof: tests/atof.c runtime/guest_crt.c runtime/guest_heap.c runtime/
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
test: build/linux/atof
+
+build/linux/ctype: tests/ctype.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/ctype
diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c
@@ -454,6 +454,13 @@ static void crt_atof(void)
g_esp += 4;
}
+static void crt_isspace(void)
+{
+ uint32_t value = MEM32(g_esp + 4);
+ g_eax = value == ' ' || (value >= '\t' && value <= '\r') ? 8 : 0;
+ g_esp += 4;
+}
+
static void crt_eh_prolog(void)
{
/* Match runtime/native32.c:native32_shim_EH_prolog's guest stack layout. */
@@ -867,6 +874,7 @@ recomp_func_t guest_crt_lookup(const char *name)
{"_ftol", crt_ftol},
{"floor", crt_floor},
{"atof", crt_atof},
+ {"isspace", crt_isspace},
{"_EH_prolog", crt_eh_prolog},
{"strlen", crt_strlen}, {"strcmp", crt_strcmp},
{"strncmp", crt_strncmp},
diff --git a/decompiled/tests/ctype.c b/decompiled/tests/ctype.c
@@ -0,0 +1,53 @@
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.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
+
+int main(void)
+{
+#ifdef _WIN32
+ HMODULE library = LoadLibraryA("msvcrt.dll");
+ int (__cdecl *classify)(int) = (void *)GetProcAddress(library, "isspace");
+ int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(library, "_errno");
+ assert(classify && get_errno);
+#else
+ assert(guest_map("ctype test", 0x50000000, 0x10000));
+ g_fs_base = 0x50000000;
+#endif
+ for (int value = -1; value <= 255; ++value) {
+#ifdef _WIN32
+ *get_errno() = 777;
+#else
+ MEM32(guest_crt_errno_address()) = 777;
+ g_esp = 0x5000ff00;
+ MEM32(g_esp + 4) = value;
+ g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456;
+#endif
+
+#ifdef _WIN32
+ int result = classify(value);
+#else
+ guest_crt_lookup("isspace")();
+ int result = g_eax;
+#endif
+
+ switch (value) {
+ case 9: case 10: case 11: case 12: case 13: case 32: assert(result == 8); break;
+ default: assert(!result); break;
+ }
+#ifdef _WIN32
+ assert(*get_errno() == 777);
+#else
+ assert(MEM32(guest_crt_errno_address()) == 777 && g_esp == 0x5000ff04);
+ assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456);
+#endif
+ }
+ puts("isspace: all byte values and EOF, exact C-locale mask, errno and guest ABI passed");
+}
diff --git a/tools/reconstruction/test_portable_ctype.py b/tools/reconstruction/test_portable_ctype.py
@@ -0,0 +1,22 @@
+"""Compare guest character classification with Windows MSVCRT."""
+import pathlib
+import subprocess
+
+
+root = pathlib.Path(__file__).resolve().parents[2]
+decompiled = root / "decompiled"
+work = root / "tools/reconstruction/work/portable-ctype"
+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/ctype.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/ctype"], check=True)
+actual = subprocess.check_output(wsl + ["./build/linux/ctype"], text=True, timeout=30)
+assert actual == expected, (expected, actual)
+print("Windows == Linux contract: " + actual.strip())
+