commit 85b25faa80157b15023a82759b585a4b7c63e0cf
parent 123c50f43e75bba2c6566a84e727ae2a08b1d817
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 12:04:04 +0000
Implement guest integer formatting across Windows CRT radices
Diffstat:
4 files changed, 126 insertions(+), 0 deletions(-)
diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux
@@ -202,6 +202,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric
./build/linux/atoi
./build/linux/atof
./build/linux/ctype
+ ./build/linux/itoa
./build/linux/graphics
./build/linux/window
./build/linux/surfaces
@@ -309,3 +310,8 @@ build/linux/ctype: tests/ctype.c runtime/guest_crt.c runtime/guest_heap.c runtim
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
test: build/linux/ctype
+
+build/linux/itoa: tests/itoa.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/itoa
diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c
@@ -461,6 +461,26 @@ static void crt_isspace(void)
g_esp += 4;
}
+static void crt_itoa(void)
+{
+ uint32_t value = MEM32(g_esp + 4), output = MEM32(g_esp + 8), radix = MEM32(g_esp + 12);
+ if (radix < 2 || radix > 36) { fprintf(stderr, "Invalid _itoa radix %u\n", radix); abort(); }
+ g_eax = output;
+ if (radix == 10 && (int32_t)value < 0) {
+ MEM8(output++) = '-';
+ value = 0u - value;
+ }
+ char digits[32];
+ unsigned count = 0;
+ do {
+ digits[count++] = "0123456789abcdefghijklmnopqrstuvwxyz"[value % radix];
+ value /= radix;
+ } while (value);
+ while (count) MEM8(output++) = digits[--count];
+ MEM8(output) = 0;
+ g_esp += 4;
+}
+
static void crt_eh_prolog(void)
{
/* Match runtime/native32.c:native32_shim_EH_prolog's guest stack layout. */
@@ -875,6 +895,7 @@ recomp_func_t guest_crt_lookup(const char *name)
{"floor", crt_floor},
{"atof", crt_atof},
{"isspace", crt_isspace},
+ {"_itoa", crt_itoa},
{"_EH_prolog", crt_eh_prolog},
{"strlen", crt_strlen}, {"strcmp", crt_strcmp},
{"strncmp", crt_strncmp},
diff --git a/decompiled/tests/itoa.c b/decompiled/tests/itoa.c
@@ -0,0 +1,77 @@
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#ifdef _WIN32
+#include <windows.h>
+static char *(__cdecl *convert)(int, char *, int);
+static int *error_number;
+#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 const char *conversion(uint32_t value, unsigned radix)
+{
+#ifdef _WIN32
+ static char storage[40];
+ *error_number = 777;
+#else
+ char *storage = (char *)ADDR(0x50000100);
+ MEM32(guest_crt_errno_address()) = 777;
+ g_esp = 0x5000ff00;
+ MEM32(g_esp + 4) = value;
+ MEM32(g_esp + 8) = 0x50000101;
+ MEM32(g_esp + 12) = radix;
+ g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456;
+#endif
+ memset(storage, 0xa5, 40);
+
+#ifdef _WIN32
+ const char *result = convert(value, storage + 1, radix);
+#else
+ guest_crt_lookup("_itoa")();
+ const char *result = (char *)ADDR(g_eax);
+#endif
+
+ assert(result == storage + 1 && (unsigned char)storage[0] == 0xa5);
+ for (unsigned i = strlen(result) + 2; i < 40; ++i) assert((unsigned char)storage[i] == 0xa5);
+#ifdef _WIN32
+ assert(*error_number == 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
+ return result;
+}
+
+int main(void)
+{
+#ifdef _WIN32
+ HMODULE library = LoadLibraryA("msvcrt.dll");
+ convert = (void *)GetProcAddress(library, "_itoa");
+ int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(library, "_errno");
+ assert(convert && get_errno);
+ error_number = get_errno();
+#else
+ assert(guest_map("itoa test", 0x50000000, 0x10000));
+ g_fs_base = 0x50000000;
+#endif
+ assert(!strcmp(conversion(0, 10), "0"));
+ assert(!strcmp(conversion(0x80000000, 10), "-2147483648"));
+ assert(!strcmp(conversion(0x7fffffff, 10), "2147483647"));
+ assert(!strcmp(conversion(UINT32_MAX, 16), "ffffffff"));
+ assert(!strcmp(conversion(UINT32_MAX, 2), "11111111111111111111111111111111"));
+ assert(!strcmp(conversion(35, 36), "z"));
+ uint32_t digest = 2166136261u;
+ for (unsigned radix = 2; radix <= 36; ++radix) {
+ for (int value = -1000; value <= 1000; ++value) {
+ const char *result = conversion(value, radix);
+ do { digest = (digest ^ (unsigned char)*result) * 16777619u; } while (*result++);
+ }
+ }
+ assert(digest == 0xdf3a9dd3);
+ printf("itoa: 70035 conversions across bases 2..36 plus limits, digest %08x, canaries, errno and guest ABI passed\n", digest);
+}
diff --git a/tools/reconstruction/test_portable_itoa.py b/tools/reconstruction/test_portable_itoa.py
@@ -0,0 +1,22 @@
+"""Compare guest integer formatting with Windows MSVCRT."""
+import pathlib
+import subprocess
+
+
+root = pathlib.Path(__file__).resolve().parents[2]
+decompiled = root / "decompiled"
+work = root / "tools/reconstruction/work/portable-itoa"
+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/itoa.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/itoa"], check=True)
+actual = subprocess.check_output(wsl + ["./build/linux/itoa"], text=True, timeout=30)
+assert actual == expected, (expected, actual)
+print("Windows == Linux contract: " + actual.strip())
+