commit 1861583fd43bd034404c00c086065a88fa5c8af5
parent 2f67192c583eb67dda3e907f3b01d70ac34b0298
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 11:30:55 +0000
Implement guest Windows path decomposition on Linux
Diffstat:
4 files changed, 143 insertions(+), 0 deletions(-)
diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux
@@ -198,6 +198,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric
./build/linux/stricmp
./build/linux/strncmp
./build/linux/locale
+ ./build/linux/splitpath
./build/linux/atoi
./build/linux/graphics
./build/linux/window
@@ -291,3 +292,8 @@ build/linux/locale: tests/locale.c runtime/guest_crt.c runtime/guest_heap.c runt
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
test: build/linux/locale
+
+build/linux/splitpath: tests/splitpath.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/splitpath
diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c
@@ -789,6 +789,28 @@ static void crt_localeconv(void)
g_esp += 4;
}
+static void crt_splitpath(void)
+{
+ const char *path = (const char *)ADDR(MEM32(g_esp + 4));
+ const char *directory = path + (*path && path[1] == ':' ? 2 : 0);
+ const char *filename = directory, *extension = NULL, *end = directory;
+ /* Separators discard dots in directory names; only the final dot is an extension. */
+ for (; *end; ++end) {
+ if (*end == '/' || *end == '\\') { filename = end + 1; extension = NULL; }
+ else if (*end == '.') extension = end;
+ }
+ if (!extension) extension = end;
+ const char *parts[] = {path, directory, filename, extension, end};
+ for (unsigned i = 0; i < 4; ++i) {
+ uint32_t output = MEM32(g_esp + 8 + i * 4);
+ if (!output) continue;
+ size_t length = parts[i + 1] - parts[i];
+ memcpy((void *)ADDR(output), parts[i], length);
+ MEM8(output + length) = 0;
+ }
+ g_esp += 4;
+}
+
recomp_func_t guest_crt_lookup(const char *name)
{
static const struct { const char *name; recomp_func_t function; } functions[] = {
@@ -827,6 +849,7 @@ recomp_func_t guest_crt_lookup(const char *name)
{"atoi", crt_atoi},
{"setlocale", crt_setlocale},
{"localeconv", crt_localeconv},
+ {"_splitpath", crt_splitpath},
};
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/tests/splitpath.c b/decompiled/tests/splitpath.c
@@ -0,0 +1,92 @@
+#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
+
+int main(void)
+{
+ static const char *cases[][5] = {
+ {"", "", "", "", ""},
+ {"song.wav", "", "", "song", ".wav"},
+ {"music", "", "", "music", ""},
+ {"C:\\AO\\audio\\song.wav", "C:", "\\AO\\audio\\", "song", ".wav"},
+ {"c:song.wav", "c:", "", "song", ".wav"},
+ {"C:", "C:", "", "", ""},
+ {"1:file", "1:", "", "file", ""},
+ {"a/b\\c.wav", "", "a/b\\", "c", ".wav"},
+ {"a.b/c", "", "a.b/", "c", ""},
+ {"/one/two/", "", "/one/two/", "", ""},
+ {"\\\\host\\share\\file.ext", "", "\\\\host\\share\\", "file", ".ext"},
+ {"file.tar.gz", "", "", "file.tar", ".gz"},
+ {".hidden", "", "", "", ".hidden"},
+ {".", "", "", "", "."},
+ {"..", "", "", ".", "."},
+ {"file.", "", "", "file", "."},
+ {".././file", "", ".././", "file", ""},
+ {"file:stream", "", "", "file:stream", ""},
+ };
+#ifdef _WIN32
+ HMODULE library = LoadLibraryA("msvcrt.dll");
+ void (__cdecl *split)(const char *, char *, char *, char *, char *) =
+ (void *)GetProcAddress(library, "_splitpath");
+ int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(library, "_errno");
+ assert(split && get_errno);
+#else
+ assert(guest_map("splitpath test", 0x50000000, 0x20000));
+ g_fs_base = 0x50000000;
+#endif
+ for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) {
+ for (unsigned mask = 0; mask < 16; ++mask) {
+#ifdef _WIN32
+ char path[256], output[4][260];
+ *get_errno() = 777;
+#else
+ char *path = (char *)ADDR(0x5000fff0);
+ char (*output)[260] = (void *)ADDR(0x50000100);
+ MEM32(guest_crt_errno_address()) = 777;
+ g_esp = 0x5000fe00;
+ MEM32(g_esp) = RECOMP_RETADDR;
+ MEM32(g_esp + 4) = 0x5000fff0;
+ g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456;
+#endif
+ strcpy(path, cases[i][0]);
+ memset(output, 0xa5, 4 * 260);
+
+#ifdef _WIN32
+ split(path, mask & 1 ? output[0] + 1 : NULL, mask & 2 ? output[1] + 1 : NULL,
+ mask & 4 ? output[2] + 1 : NULL, mask & 8 ? output[3] + 1 : NULL);
+#else
+ for (unsigned part = 0; part < 4; ++part)
+ MEM32(g_esp + 8 + part * 4) = mask & (1u << part) ? 0x50000101 + part * 260 : 0;
+ guest_crt_lookup("_splitpath")();
+#endif
+
+ assert(!strcmp(path, cases[i][0]));
+ for (unsigned part = 0; part < 4; ++part) {
+ size_t written = 0;
+ if (mask & (1u << part)) {
+ assert(!strcmp(output[part] + 1, cases[i][part + 1]));
+ written = strlen(cases[i][part + 1]) + 1;
+ }
+ assert((unsigned char)output[part][0] == 0xa5);
+ for (unsigned byte = written + 1; byte < 260; ++byte)
+ assert((unsigned char)output[part][byte] == 0xa5);
+ }
+#ifdef _WIN32
+ assert(*get_errno() == 777);
+#else
+ assert(MEM32(guest_crt_errno_address()) == 777 && g_esp == 0x5000fe04);
+ assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456);
+#endif
+ }
+ }
+ puts("splitpath: 18 paths, all 16 output masks, canaries, errno and guest ABI passed");
+}
diff --git a/tools/reconstruction/test_portable_splitpath.py b/tools/reconstruction/test_portable_splitpath.py
@@ -0,0 +1,22 @@
+"""Compare guest path splitting with Windows MSVCRT."""
+import pathlib
+import subprocess
+
+
+root = pathlib.Path(__file__).resolve().parents[2]
+decompiled = root / "decompiled"
+work = root / "tools/reconstruction/work/portable-splitpath"
+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/splitpath.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/splitpath"], check=True)
+actual = subprocess.check_output(wsl + ["./build/linux/splitpath"], text=True, timeout=30)
+assert actual == expected, (expected, actual)
+print("Windows == Linux contract: " + actual.strip())
+