commit e49ca4216072330b84dc21a82c47c0de52d3be51
parent 14392a621187c160e1d1c0fe3f64da0b1298af8c
Author: anon <anon@example.com>
Date: Wed, 14 Oct 2026 23:18:58 +0000
Add Linux semaphore lifecycle, guest scanf and Winsock startup
Diffstat:
13 files changed, 1029 insertions(+), 2 deletions(-)
diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux
@@ -3,7 +3,7 @@ CPPFLAGS := -Iruntime -Isrc -Idata -DRECOMP_PORTABLE
CFLAGS := -O2 -fno-strict-aliasing
GAME_SOURCES := $(wildcard src/*/*.c) src/dispatch.c
DATA_SOURCES := $(wildcard data/*.c)
-RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_platform.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c
+RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_platform.c runtime/linux_sync.c runtime/linux_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c
VENDOR_SOURCES := $(wildcard vendor/msvcp60/source/*.c) $(wildcard vendor/ace/source/*.c)
OBJECTS := $(patsubst %.c,build/linux/%.o,$(GAME_SOURCES) $(DATA_SOURCES) $(RUNTIME_SOURCES) $(VENDOR_SOURCES))
@@ -56,7 +56,19 @@ build/linux/guest-rand: tests/guest_rand.c runtime/guest_crt.c runtime/guest_hea
mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@
-test: 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: tests/semaphore.c runtime/linux_sync.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/linux_sync.h runtime/guest_memory.h
+ mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
+
+build/linux/guest-scanf: tests/guest_scanf.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_memory.h runtime/guest_heap.h
+ mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
+
+build/linux/network-startup: tests/network_startup.c runtime/linux_network.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/linux_network.h runtime/guest_memory.h
+ mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
+
+test: 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/linecrypt
./build/linux/guest-heap
./build/linux/guest-crt
@@ -66,5 +78,8 @@ test: build/linux/linecrypt build/linux/guest-heap build/linux/guest-crt build/l
./build/linux/msvcp-stdio-data
./build/linux/guest-eh
./build/linux/guest-rand
+ ./build/linux/semaphore
+ ./build/linux/guest-scanf
+ ./build/linux/network-startup
-include $(OBJECTS:.o=.d)
diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c
@@ -1,6 +1,7 @@
#include "guest_crt.h"
#include "guest_heap.h"
#include "guest_memory.h"
+#include <ctype.h>
static _Thread_local uint32_t random_state = 1;
static uint32_t user_matherr;
@@ -371,6 +372,138 @@ static void crt_memset(void)
g_esp += 4;
}
+static void crt_sscanf(void)
+{
+ const char *input = (const char *)ADDR(MEM32(g_esp + 4));
+ const char *start = input;
+ const char *format = (const char *)ADDR(MEM32(g_esp + 8));
+ char *conversion = malloc(strlen(format) + 4);
+ if (!conversion) { perror("scanf format allocation"); abort(); }
+ uint32_t argument = g_esp + 12;
+ int assigned = 0, completed = 0;
+ while (*format) {
+ if (isspace((unsigned char)*format)) {
+ ++format;
+ while (isspace((unsigned char)*input)) ++input;
+ continue;
+ }
+ if (*format != '%' || format[1] == '%') {
+ if (*format == '%') ++format;
+ if (*input != *format) {
+ if (!*input && !completed) assigned = -1;
+ break;
+ }
+ ++input;
+ ++format;
+ continue;
+ }
+
+ /* Parse one conversion; translate Windows long/pointer widths explicitly. */
+ ++format;
+ int suppressed = *format == '*';
+ if (suppressed) ++format;
+ char *output = conversion;
+ *output++ = '%';
+ if (suppressed) *output++ = '*';
+ uint32_t width = 0;
+ while (isdigit((unsigned char)*format)) {
+ if (width > (INT32_MAX - (*format - '0')) / 10) goto unsupported;
+ width = width * 10 + (*format - '0');
+ *output++ = *format++;
+ }
+ int length = 0;
+ if (*format == 'h') {
+ ++format;
+ length = 2;
+ if (*format == 'h') { ++format; length = 1; }
+ } else if (*format == 'l') {
+ ++format;
+ length = 4;
+ if (*format == 'l') { ++format; length = 8; }
+ } else if (*format == 'L') {
+ ++format;
+ length = 8;
+ } else if (!strncmp(format, "I64", 3)) {
+ format += 3;
+ length = 8;
+ } else if (!strncmp(format, "I32", 3)) {
+ format += 3;
+ length = 4;
+ }
+ char type = *format;
+ if (!type) goto unsupported;
+ ++format;
+ uint32_t destination = 0;
+ if (!suppressed) { destination = MEM32(argument); argument += 4; }
+ if (type == 'n') {
+ uint32_t consumed = (uint32_t)(input - start);
+ if (!suppressed) {
+ if (length == 1) MEM8(destination) = consumed;
+ else if (length == 2) MEM16(destination) = consumed;
+ else if (length == 8) MEM64(destination) = consumed;
+ else MEM32(destination) = consumed;
+ }
+ ++completed;
+ continue;
+ }
+ if (type == 'c' && !length) {
+ /* MSVCRT accepts a short character field at EOF; glibc can reject it. */
+ uint32_t consumed = 0;
+ while (consumed < (width ? width : 1) && input[consumed]) ++consumed;
+ if (!consumed) {
+ if (!completed) assigned = -1;
+ break;
+ }
+ if (!suppressed) { memcpy((void *)ADDR(destination), input, consumed); ++assigned; }
+ input += consumed;
+ ++completed;
+ continue;
+ }
+ if (strchr("diouxX", type)) {
+ if (length == 1) { *output++ = 'h'; *output++ = 'h'; }
+ if (length == 2) *output++ = 'h';
+ if (length == 8) { *output++ = 'l'; *output++ = 'l'; }
+ } else if (strchr("feEgG", type)) {
+ if (length && length != 4 && length != 8) goto unsupported;
+ if (length) *output++ = 'l'; /* Windows long double is also 64-bit. */
+ } else if (type == 'p') {
+ if (length) goto unsupported;
+ type = 'x'; /* Guest pointers are 32-bit hexadecimal integers. */
+ } else if (strchr("sc[", type)) {
+ if (length) goto unsupported; /* Wide strings need UTF-16 conversion. */
+ } else goto unsupported;
+ *output++ = type;
+ if (type == '[') {
+ if (*format == '^') *output++ = *format++;
+ if (*format == ']') *output++ = *format++;
+ while (*format && *format != ']') *output++ = *format++;
+ if (!*format) goto unsupported;
+ *output++ = *format++;
+ }
+ strcpy(output, "%n");
+
+ /* Native scanf only receives translated pointers with compatible sizes. */
+ int consumed = -1;
+ int result = suppressed ? sscanf(input, conversion, &consumed) :
+ sscanf(input, conversion, (void *)ADDR(destination), &consumed);
+ if (consumed < 0) {
+ if (result == EOF && !completed) assigned = -1;
+ break;
+ }
+ input += consumed;
+ ++completed;
+ if (!suppressed) ++assigned;
+ }
+ free(conversion);
+ g_eax = assigned;
+ g_esp += 4;
+ return;
+
+unsupported:
+ fprintf(stderr, "Unsupported guest sscanf format: %s\n", (char *)ADDR(MEM32(g_esp + 8)));
+ abort();
+}
+
recomp_func_t guest_crt_lookup(const char *name)
{
static const struct { const char *name; recomp_func_t function; } functions[] = {
@@ -391,6 +524,7 @@ recomp_func_t guest_crt_lookup(const char *name)
{"_EH_prolog", crt_eh_prolog},
{"strlen", crt_strlen}, {"strcmp", crt_strcmp},
{"memcmp", crt_memcmp}, {"memchr", crt_memchr},
+ {"sscanf", crt_sscanf},
};
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_host.c b/decompiled/runtime/linux_host.c
@@ -3,6 +3,8 @@
#include "game-modules.h"
#include "guest_crt.h"
#include "linux_platform.h"
+#include "linux_sync.h"
+#include "linux_network.h"
#include "guest_msvcp.h"
#include "../vendor/msvcp60/source/module.h"
#include "../vendor/ace/source/module.h"
@@ -154,6 +156,11 @@ recomp_func_t recomp_lookup_import(uint32_t va)
exit(3);
}
current_import = (va - 0xf0000000u) / 4;
+ if (!strcasecmp(imports[current_import].dll, "ws2_32.dll") ||
+ !strcasecmp(imports[current_import].dll, "wsock32.dll")) {
+ recomp_func_t function = linux_network_lookup(imports[current_import].name, imports[current_import].ordinal);
+ if (function) return function;
+ }
if (!strcasecmp(imports[current_import].dll, "kernel32.dll") &&
imports[current_import].name &&
!strcmp(imports[current_import].name, "GetModuleHandleA"))
@@ -161,6 +168,10 @@ recomp_func_t recomp_lookup_import(uint32_t va)
if (imports[current_import].name) {
recomp_func_t function = linux_platform_lookup(imports[current_import].dll, imports[current_import].name);
if (function) return function;
+ if (!strcasecmp(imports[current_import].dll, "kernel32.dll")) {
+ function = linux_sync_lookup(imports[current_import].name);
+ if (function) return function;
+ }
}
if (!strcasecmp(imports[current_import].dll, "kernel32.dll") &&
imports[current_import].name &&
diff --git a/decompiled/runtime/linux_network.c b/decompiled/runtime/linux_network.c
@@ -0,0 +1,64 @@
+#include "linux_network.h"
+
+static unsigned startup_count;
+
+static void wsa_startup(void)
+{
+ uint16_t requested = MEM32(g_esp + 4);
+ uint32_t data = MEM32(g_esp + 8);
+ uint16_t version = 0x0202;
+ if ((requested & 0xff) == 1) version = requested >> 8 ? 0x0101 : 1;
+ else if ((requested & 0xff) == 2 && (requested >> 8) < 2) version = requested;
+ g_eax = 10014; /* WSAEFAULT */
+ if (data) {
+ /* WSADATA is 400 bytes on x86; padding and lpVendorInfo remain untouched. */
+ MEM16(data) = version;
+ MEM16(data + 2) = 0x0202;
+ strcpy((char *)ADDR(data + 4), "WinSock 2.0");
+ strcpy((char *)ADDR(data + 261), "Running");
+ MEM16(data + 390) = (version & 0xff) == 1 ? 32767 : 0;
+ MEM16(data + 392) = (version & 0xff) == 1 ? 65467 : 0;
+ g_eax = 10092; /* WSAVERNOTSUPPORTED */
+ if (requested & 0xff) {
+ if (!startup_count) MEM32(g_fs_base + 0x34) = 0;
+ ++startup_count;
+ g_eax = 0;
+ }
+ }
+ g_esp += 12;
+}
+
+static void wsa_cleanup(void)
+{
+ g_eax = UINT32_MAX;
+ if (startup_count) {
+ --startup_count;
+ MEM32(g_fs_base + 0x34) = 0;
+ g_eax = 0;
+ } else MEM32(g_fs_base + 0x34) = 10093; /* WSANOTINITIALISED */
+ g_esp += 4;
+}
+
+static void wsa_get_last_error(void)
+{
+ g_eax = MEM32(g_fs_base + 0x34);
+ g_esp += 4;
+}
+
+static void wsa_set_last_error(void)
+{
+ MEM32(g_fs_base + 0x34) = MEM32(g_esp + 4);
+ g_esp += 8;
+}
+
+recomp_func_t linux_network_lookup(const char *name, uint32_t ordinal)
+{
+ static const struct { const char *name; uint32_t ordinal; recomp_func_t function; } functions[] = {
+ {"WSAStartup", 115, wsa_startup}, {"WSACleanup", 116, wsa_cleanup},
+ {"WSAGetLastError", 111, wsa_get_last_error}, {"WSASetLastError", 112, wsa_set_last_error},
+ };
+ for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i)
+ if (name ? !strcmp(name, functions[i].name) : ordinal == functions[i].ordinal)
+ return functions[i].function;
+ return NULL;
+}
diff --git a/decompiled/runtime/linux_network.h b/decompiled/runtime/linux_network.h
@@ -0,0 +1,5 @@
+#ifndef LINUX_NETWORK_H
+#define LINUX_NETWORK_H
+#include "recomp_types.h"
+recomp_func_t linux_network_lookup(const char *name, uint32_t ordinal);
+#endif
diff --git a/decompiled/runtime/linux_sync.c b/decompiled/runtime/linux_sync.c
@@ -0,0 +1,250 @@
+#define _GNU_SOURCE
+#include "linux_sync.h"
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/futex.h>
+#include <stdatomic.h>
+#include <sys/file.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <time.h>
+#include <unistd.h>
+
+/* Shared state contains no pointers. Each open handle holds a shared file lock,
+ * so the kernel releases the last reference even after an abnormal process exit. */
+struct semaphore_state {
+ _Atomic uint32_t count;
+ uint32_t maximum;
+ char name[260];
+};
+_Static_assert(sizeof(_Atomic uint32_t) == 4 && ATOMIC_INT_LOCK_FREE == 2,
+ "Linux futexes require lock-free 32-bit counters");
+
+struct semaphore_handle {
+ uint32_t id, access;
+ int fd;
+ struct semaphore_state *state;
+ struct semaphore_handle *next;
+};
+static struct semaphore_handle *handles;
+static uint32_t next_handle = 0x100;
+
+static void system_failure(const char *operation)
+{
+ perror(operation);
+ abort();
+}
+
+static struct semaphore_handle *find_handle(uint32_t id)
+{
+ for (struct semaphore_handle *handle = handles; handle; handle = handle->next)
+ if (handle->id == id) return handle;
+ MEM32(g_fs_base + 0x34) = 6; /* ERROR_INVALID_HANDLE */
+ return NULL;
+}
+
+static uint32_t open_semaphore(const char *name, int create, int32_t initial,
+ int32_t maximum, uint32_t access)
+{
+ int directory = -1, fd = -1, existing = 0;
+ struct semaphore_state *state;
+ if (name) {
+ if (!*name || strlen(name) >= 260 || strchr(name, '\\')) {
+ fprintf(stderr, "Unsupported semaphore namespace/name: %s\n", name);
+ abort();
+ }
+ /* Serialize namespace changes independently of the object's lifetime locks. */
+ char path[80];
+ snprintf(path, sizeof(path), "/tmp/ao-native-sync-%u", (unsigned)getuid());
+ if (mkdir(path, 0700) && errno != EEXIST) system_failure("semaphore namespace mkdir");
+ directory = open(path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
+ if (directory < 0) system_failure("semaphore namespace open");
+ struct stat info;
+ if (fstat(directory, &info)) system_failure("semaphore namespace stat");
+ if (info.st_uid != getuid() || (info.st_mode & 077)) {
+ fprintf(stderr, "Semaphore namespace must be owned by this user with mode 0700\n");
+ abort();
+ }
+ if (flock(directory, LOCK_EX)) system_failure("semaphore namespace lock");
+
+ /* Hash only the filename; verify the full case-sensitive name before sharing. */
+ uint64_t hash = 14695981039346656037ull;
+ for (const unsigned char *p = (const unsigned char *)name; *p; ++p)
+ hash = (hash ^ *p) * 1099511628211ull;
+ snprintf(path, sizeof(path), "%016llx", (unsigned long long)hash);
+ fd = openat(directory, path, O_RDWR | O_NOFOLLOW | O_CLOEXEC | (create ? O_CREAT : 0), 0600);
+ if (fd < 0) {
+ if (errno != ENOENT) system_failure("semaphore object open");
+ close(directory);
+ MEM32(g_fs_base + 0x34) = 2; /* ERROR_FILE_NOT_FOUND */
+ return 0;
+ }
+ if (flock(fd, LOCK_EX | LOCK_NB)) {
+ if (errno != EWOULDBLOCK) system_failure("semaphore lifetime lock");
+ existing = 1;
+ }
+ if (!existing && !create) {
+ close(fd);
+ close(directory);
+ MEM32(g_fs_base + 0x34) = 2;
+ return 0;
+ }
+ }
+
+ if (!existing && (maximum <= 0 || initial < 0 || initial > maximum)) {
+ if (fd >= 0) close(fd);
+ if (directory >= 0) close(directory);
+ MEM32(g_fs_base + 0x34) = 87; /* ERROR_INVALID_PARAMETER */
+ return 0;
+ }
+ if (fd >= 0) {
+ if (!existing && ftruncate(fd, sizeof(*state))) system_failure("semaphore object resize");
+ struct stat info;
+ if (fstat(fd, &info)) system_failure("semaphore object stat");
+ if (info.st_size != sizeof(*state)) {
+ fprintf(stderr, "Incompatible live semaphore storage\n");
+ abort();
+ }
+ }
+ state = mmap(NULL, sizeof(*state), PROT_READ | PROT_WRITE,
+ MAP_SHARED | (fd < 0 ? MAP_ANONYMOUS : 0), fd, 0);
+ if (state == MAP_FAILED) system_failure("semaphore mapping");
+ if (!existing) {
+ atomic_init(&state->count, initial);
+ state->maximum = maximum;
+ strcpy(state->name, name ? name : "");
+ } else if (strcmp(state->name, name)) {
+ fprintf(stderr, "Semaphore name hash collision\n");
+ abort();
+ }
+ if (fd >= 0 && flock(fd, LOCK_SH)) system_failure("semaphore shared lifetime lock");
+ if (directory >= 0) close(directory);
+
+ struct semaphore_handle *handle = malloc(sizeof(*handle));
+ if (!handle) system_failure("semaphore handle allocation");
+ handle->id = next_handle;
+ next_handle += 4;
+ handle->access = access;
+ handle->fd = fd;
+ handle->state = state;
+ handle->next = handles;
+ handles = handle;
+ if (create) MEM32(g_fs_base + 0x34) = existing ? 183 : 0; /* ERROR_ALREADY_EXISTS */
+ return handle->id;
+}
+
+static void create_semaphore(void)
+{
+ if (MEM32(g_esp + 4)) {
+ fprintf(stderr, "Semaphore security attributes/inheritance are not implemented\n");
+ abort();
+ }
+ uint32_t name = MEM32(g_esp + 16);
+ g_eax = open_semaphore(name ? (const char *)ADDR(name) : NULL, 1,
+ MEM32(g_esp + 8), MEM32(g_esp + 12), 0x1f0003);
+ g_esp += 20;
+}
+
+static void open_named_semaphore(void)
+{
+ uint32_t access = MEM32(g_esp + 4);
+ if (MEM32(g_esp + 8) || (access & ~0x1f0003u)) {
+ fprintf(stderr, "Semaphore inheritance/generic access masks are not implemented\n");
+ abort();
+ }
+ uint32_t name = MEM32(g_esp + 12);
+ g_eax = 0;
+ if (name) g_eax = open_semaphore((const char *)ADDR(name), 0, 0, 0, access);
+ else MEM32(g_fs_base + 0x34) = 87;
+ g_esp += 16;
+}
+
+static void close_handle(void)
+{
+ struct semaphore_handle *handle = find_handle(MEM32(g_esp + 4));
+ g_eax = 0;
+ if (handle) {
+ struct semaphore_handle **link = &handles;
+ while (*link != handle) link = &(*link)->next;
+ *link = handle->next;
+ if (munmap(handle->state, sizeof(*handle->state))) system_failure("semaphore unmap");
+ if (handle->fd >= 0 && close(handle->fd)) system_failure("semaphore handle close");
+ free(handle);
+ g_eax = 1;
+ }
+ g_esp += 8;
+}
+
+static void release_semaphore(void)
+{
+ struct semaphore_handle *handle = find_handle(MEM32(g_esp + 4));
+ int32_t increment = MEM32(g_esp + 8);
+ g_eax = 0;
+ if (handle && !(handle->access & 2)) MEM32(g_fs_base + 0x34) = 5;
+ else if (handle && increment <= 0) MEM32(g_fs_base + 0x34) = 87;
+ else if (handle) {
+ uint32_t count = atomic_load(&handle->state->count);
+ for (;;) {
+ if ((uint32_t)increment > handle->state->maximum - count) {
+ MEM32(g_fs_base + 0x34) = 298; /* ERROR_TOO_MANY_POSTS */
+ break;
+ }
+ if (atomic_compare_exchange_weak(&handle->state->count, &count, count + increment)) {
+ uint32_t previous = MEM32(g_esp + 12);
+ if (previous) MEM32(previous) = count;
+ if (syscall(SYS_futex, &handle->state->count, FUTEX_WAKE, increment, NULL, NULL, 0) < 0)
+ system_failure("semaphore wake");
+ g_eax = 1;
+ break;
+ }
+ }
+ }
+ g_esp += 16;
+}
+
+static void wait_for_single_object(void)
+{
+ struct semaphore_handle *handle = find_handle(MEM32(g_esp + 4));
+ uint32_t milliseconds = MEM32(g_esp + 8);
+ g_eax = UINT32_MAX; /* WAIT_FAILED */
+ if (handle && !(handle->access & 0x100000)) MEM32(g_fs_base + 0x34) = 5;
+ else if (handle) {
+ struct timespec deadline;
+ if (milliseconds != UINT32_MAX) {
+ if (clock_gettime(CLOCK_MONOTONIC, &deadline)) system_failure("semaphore clock");
+ deadline.tv_sec += milliseconds / 1000;
+ deadline.tv_nsec += (milliseconds % 1000) * 1000000;
+ if (deadline.tv_nsec >= 1000000000) { ++deadline.tv_sec; deadline.tv_nsec -= 1000000000; }
+ }
+ for (;;) {
+ uint32_t count = atomic_load(&handle->state->count);
+ if (count) {
+ if (atomic_compare_exchange_weak(&handle->state->count, &count, count - 1)) {
+ g_eax = 0; /* WAIT_OBJECT_0 */
+ break;
+ }
+ continue;
+ }
+ if (!milliseconds) { g_eax = 258; break; }
+ if (!syscall(SYS_futex, &handle->state->count, FUTEX_WAIT_BITSET, 0,
+ milliseconds == UINT32_MAX ? NULL : &deadline, NULL, FUTEX_BITSET_MATCH_ANY)) continue;
+ if (errno == EAGAIN || errno == EINTR) continue;
+ if (errno == ETIMEDOUT) { g_eax = 258; break; }
+ system_failure("semaphore wait");
+ }
+ }
+ g_esp += 12;
+}
+
+recomp_func_t linux_sync_lookup(const char *name)
+{
+ static const struct { const char *name; recomp_func_t function; } functions[] = {
+ {"CreateSemaphoreA", create_semaphore}, {"OpenSemaphoreA", open_named_semaphore},
+ {"CloseHandle", close_handle}, {"ReleaseSemaphore", release_semaphore},
+ {"WaitForSingleObject", wait_for_single_object},
+ };
+ for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i)
+ if (!strcmp(name, functions[i].name)) return functions[i].function;
+ return NULL;
+}
diff --git a/decompiled/runtime/linux_sync.h b/decompiled/runtime/linux_sync.h
@@ -0,0 +1,5 @@
+#ifndef LINUX_SYNC_H
+#define LINUX_SYNC_H
+#include "recomp_types.h"
+recomp_func_t linux_sync_lookup(const char *name);
+#endif
diff --git a/decompiled/tests/guest_scanf.c b/decompiled/tests/guest_scanf.c
@@ -0,0 +1,96 @@
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+static int (__cdecl *scan)(const char *, const char *, ...);
+#else
+#include "guest_crt.h"
+#include "guest_memory.h"
+
+recomp_func_t recomp_lookup(uint32_t address)
+{
+ fprintf(stderr, "Unexpected scanf-test guest call %08x\n", address);
+ abort();
+}
+recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); }
+#endif
+
+static uint32_t digest = 2166136261u;
+static unsigned cases;
+
+static void compare_case(const char *input, const char *format, int expected, int alias)
+{
+ unsigned char output[8][32];
+ memset(output, 0xa5, sizeof(output));
+#ifndef _WIN32
+ strcpy((char *)ADDR(0x50001000u), input);
+ strcpy((char *)ADDR(0x50002000u), format);
+ memcpy((void *)ADDR(0x50003000u), output, sizeof(output));
+ g_esp = 0x5000ff00u;
+ MEM32(g_esp + 4) = 0x50001000u;
+ MEM32(g_esp + 8) = 0x50002000u;
+ for (unsigned i = 0; i < 8; ++i) MEM32(g_esp + 12 + i * 4) = 0x50003004u + (alias ? 0 : i * 32);
+ g_ebx = 0x12345678;
+ g_esi = 0x23456789;
+ g_edi = 0x3456789a;
+ g_ebp = 0x456789ab;
+#endif
+
+#ifdef _WIN32
+ int result = scan(input, format, output[0] + 4, output[alias ? 0 : 1] + 4,
+ output[alias ? 0 : 2] + 4, output[alias ? 0 : 3] + 4, output[alias ? 0 : 4] + 4,
+ output[alias ? 0 : 5] + 4, output[alias ? 0 : 6] + 4, output[alias ? 0 : 7] + 4);
+#else
+ guest_crt_lookup("sscanf")();
+ int result = (int)g_eax;
+ memcpy(output, (void *)ADDR(0x50003000u), sizeof(output));
+ assert(g_esp == 0x5000ff04u);
+ assert(g_ebx == 0x12345678 && g_esi == 0x23456789);
+ assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
+#endif
+
+ if (result != expected) fprintf(stderr, "scanf case %u: '%s' / '%s': got %d expected %d\n",
+ cases, input, format, result, expected);
+ assert(result == expected);
+ digest = (digest ^ (uint32_t)result) * 16777619u;
+ for (unsigned i = 0; i < sizeof(output); ++i)
+ digest = (digest ^ ((unsigned char *)output)[i]) * 16777619u;
+ ++cases;
+}
+
+int main(void)
+{
+#ifdef _WIN32
+ scan = (void *)GetProcAddress(LoadLibraryA("msvcrt.dll"), "sscanf");
+ assert(scan);
+#else
+ assert(guest_map("scanf test memory", 0x50000000u, 0x10000u));
+#endif
+ compare_case("localtest localtest 73 2130706433 7511 0 0", "%s %s %s %s %s %s %s", 7, 1);
+ compare_case("one\ttwo three\n", "%s%s%s%n", 3, 0);
+ compare_case("", "%s", -1, 0);
+ compare_case(" ", "%s", -1, 0);
+ compare_case("", "%n%s", 0, 0);
+ compare_case("42", "%*d%s", 0, 0);
+ compare_case("abc def", "%2s%s %s%n", 3, 0);
+ compare_case("abc123", "%3c%d%n", 2, 0);
+ compare_case("ab", "%3c%n", 1, 0);
+ compare_case("abc123X", "%[a-z]%d%c%n", 3, 0);
+ compare_case("]^a---", "%[]^a]%[^a-z]%n", 2, 0);
+ compare_case("x=42%", "x=%d%%%n", 1, 0);
+ compare_case("y=42", "x=%d", 0, 0);
+ compare_case("x", "x%d", -1, 0);
+ compare_case("1 x", "%d%d", 1, 0);
+ compare_case("0x12 077 -12 4294967295", "%i%i%ld%lu%n", 4, 0);
+ compare_case("-32768 65535 7fffffff ffffffff", "%hd%hu%lx%p%n", 4, 0);
+ compare_case("-9223372036854775808 18446744073709551615", "%I64d%I64u%n", 2, 0);
+ compare_case("1.25 -0 12.5 1e2", "%f%f%lf%Lf%n", 4, 0);
+ compare_case("ignore 42 done", "%*s%d %s%n", 2, 0);
+ assert(cases == 20 && digest == 0x8ce564c4);
+ printf("scanf: %u cases, digest %08x, guest widths, aliases, EOF and canaries passed\n", cases, digest);
+ return 0;
+}
diff --git a/decompiled/tests/network_startup.c b/decompiled/tests/network_startup.c
@@ -0,0 +1,120 @@
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#ifdef _WIN32
+#include <winsock2.h>
+#else
+#include "linux_network.h"
+#include "guest_memory.h"
+
+static uint32_t call(uint32_t ordinal, unsigned count, const uint32_t *arguments)
+{
+ g_esp = 0x5000ff00u;
+ 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_ebx == 0x12345678 && g_esi == 0x23456789);
+ assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
+ return g_eax;
+}
+#endif
+
+static uint32_t digest = 2166136261u;
+static void hash(uint32_t value) { digest = (digest ^ value) * 16777619u; }
+
+static void set_error(uint32_t value)
+{
+#ifdef _WIN32
+ WSASetLastError(value);
+ assert(GetLastError() == value);
+#else
+ call(112, 1, &value);
+ assert(MEM32(g_fs_base + 0x34) == value);
+#endif
+}
+
+static uint32_t get_error(void)
+{
+#ifdef _WIN32
+ return WSAGetLastError();
+#else
+ return call(111, 0, NULL);
+#endif
+}
+
+static int startup(uint16_t version, uint32_t expected_error)
+{
+ unsigned char bytes[408];
+ memset(bytes, 0xa5, sizeof(bytes));
+ set_error(777);
+#ifndef _WIN32
+ memcpy((void *)ADDR(0x50003000u), bytes, sizeof(bytes));
+ uint32_t args[] = {version, 0x50003004u};
+#endif
+
+#ifdef _WIN32
+ int result = WSAStartup(version, (WSADATA *)(bytes + 4));
+#else
+ int result = call(115, 2, args);
+ memcpy(bytes, (void *)ADDR(0x50003000u), sizeof(bytes));
+#endif
+
+ assert(result == ((version & 0xff) ? 0 : 10092));
+ assert(get_error() == expected_error);
+ assert(bytes[0] == 0xa5 && bytes[3] == 0xa5 && bytes[404] == 0xa5 && bytes[407] == 0xa5);
+ assert(!strcmp((char *)bytes + 8, "WinSock 2.0"));
+ assert(!strcmp((char *)bytes + 265, "Running"));
+ hash(result);
+ for (unsigned i = 0; i < sizeof(bytes); ++i) hash(bytes[i]);
+ return result;
+}
+
+static void cleanup(int expected)
+{
+ set_error(777);
+
+#ifdef _WIN32
+ int result = WSACleanup();
+#else
+ int result = call(116, 0, NULL);
+#endif
+
+ assert(result == expected);
+ assert(get_error() == (expected ? 10093 : 0));
+ hash(result);
+ hash(get_error());
+}
+
+int main(void)
+{
+#ifdef _WIN32
+ assert(sizeof(WSADATA) == 400);
+ HMODULE library = LoadLibraryA("ws2_32.dll");
+ assert(GetProcAddress(library, "WSAStartup") == GetProcAddress(library, (const char *)115));
+#else
+ assert(guest_map("network test memory", 0x50000000u, 0x10000u));
+ g_fs_base = 0x50001000u;
+ assert(linux_network_lookup("WSAStartup", 0) == linux_network_lookup(NULL, 115));
+#endif
+ cleanup(-1);
+ const struct { uint16_t version; uint32_t error; } versions[] = {
+ {0, 777}, {0x0100, 777}, {1, 0}, {0x0101, 777}, {0xff01, 777},
+ {2, 777}, {0x0102, 777}, {0x0202, 777}, {0x0302, 777}, {3, 777}, {0xffff, 777},
+ };
+ unsigned initialized = 0;
+ for (unsigned i = 0; i < 11; ++i) initialized += startup(versions[i].version, versions[i].error) == 0;
+ assert(initialized == 9);
+ for (unsigned i = 0; i < initialized; ++i) cleanup(0);
+ cleanup(-1);
+ assert(startup(2, 0) == 0);
+ cleanup(0);
+ cleanup(-1);
+ assert(digest == 0xd6dc7661);
+ printf("network startup: 12 negotiations, WSADATA32 layout, nested cleanup and errors, digest %08x\n", digest);
+ return 0;
+}
diff --git a/decompiled/tests/semaphore.c b/decompiled/tests/semaphore.c
@@ -0,0 +1,221 @@
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#else
+#include "linux_sync.h"
+#include "guest_memory.h"
+#include <signal.h>
+#include <time.h>
+#include <unistd.h>
+
+static uint32_t call(const char *name, unsigned count, const uint32_t *arguments)
+{
+ g_esp = 0x5000ff00u;
+ MEM32(g_esp) = RECOMP_RETADDR;
+ 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_sync_lookup(name)();
+ assert(g_esp == 0x5000ff00u + (count + 1) * 4);
+ assert(g_ebx == 0x12345678 && g_esi == 0x23456789);
+ assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
+ return g_eax;
+}
+#endif
+
+static uint32_t error(void)
+{
+#ifdef _WIN32
+ return GetLastError();
+#else
+ return MEM32(g_fs_base + 0x34);
+#endif
+}
+
+static uint32_t create(const char *name, int initial, int maximum)
+{
+#ifdef _WIN32
+ return (uintptr_t)CreateSemaphoreA(NULL, initial, maximum, name);
+#else
+ if (name) strcpy((char *)ADDR(0x50002000u), name);
+ uint32_t args[] = {0, initial, maximum, name ? 0x50002000u : 0};
+ return call("CreateSemaphoreA", 4, args);
+#endif
+}
+
+static uint32_t open_named(const char *name, uint32_t access)
+{
+#ifdef _WIN32
+ return (uintptr_t)OpenSemaphoreA(access, FALSE, name);
+#else
+ strcpy((char *)ADDR(0x50002000u), name);
+ uint32_t args[] = {access, 0, 0x50002000u};
+ return call("OpenSemaphoreA", 3, args);
+#endif
+}
+
+static uint32_t close_object(uint32_t handle)
+{
+#ifdef _WIN32
+ return CloseHandle((HANDLE)(uintptr_t)handle);
+#else
+ return call("CloseHandle", 1, &handle);
+#endif
+}
+
+static uint32_t wait_object(uint32_t handle, uint32_t milliseconds)
+{
+#ifdef _WIN32
+ return WaitForSingleObject((HANDLE)(uintptr_t)handle, milliseconds);
+#else
+ uint32_t args[] = {handle, milliseconds};
+ return call("WaitForSingleObject", 2, args);
+#endif
+}
+
+static int32_t previous;
+static uint32_t release(uint32_t handle, int count)
+{
+#ifdef _WIN32
+ return ReleaseSemaphore((HANDLE)(uintptr_t)handle, count, (LONG *)&previous);
+#else
+ MEM32(0x50003000u) = previous;
+ uint32_t args[] = {handle, count, 0x50003000u};
+ uint32_t result = call("ReleaseSemaphore", 3, args);
+ previous = MEM32(0x50003000u);
+ return result;
+#endif
+}
+
+static double seconds(void)
+{
+#ifdef _WIN32
+ LARGE_INTEGER counter, frequency;
+ assert(QueryPerformanceCounter(&counter) && QueryPerformanceFrequency(&frequency));
+ return (double)counter.QuadPart / frequency.QuadPart;
+#else
+ struct timespec now;
+ assert(!clock_gettime(CLOCK_MONOTONIC, &now));
+ return now.tv_sec + now.tv_nsec * 1e-9;
+#endif
+}
+
+static void bounded_counts_and_errors(void)
+{
+ assert(!create(NULL, -1, 3) && error() == 87);
+ assert(!create(NULL, 4, 3) && error() == 87);
+ assert(!create(NULL, 0, 0) && error() == 87);
+ uint32_t handle = create(NULL, 2, 3);
+ assert(handle);
+
+ assert(wait_object(handle, 0) == 0);
+ assert(wait_object(handle, 0) == 0);
+ assert(wait_object(handle, 0) == 258);
+ assert(release(handle, 2) && previous == 0);
+ assert(wait_object(handle, 0) == 0);
+ previous = 123;
+ assert(!release(handle, 3) && error() == 298 && previous == 123);
+ assert(release(handle, 2) && previous == 1);
+ assert(!release(handle, 0) && error() == 87);
+ assert(!release(handle, -1) && error() == 87);
+
+ assert(wait_object(handle, 0) == 0);
+ assert(wait_object(handle, 0) == 0);
+ assert(wait_object(handle, 0) == 0);
+ double before = seconds();
+ assert(wait_object(handle, 25) == 258);
+ assert(seconds() - before >= 0.015);
+ assert(close_object(handle));
+ assert(!close_object(handle) && error() == 6);
+ assert(wait_object(handle, 0) == UINT32_MAX && error() == 6);
+}
+
+static void named_lifetime_and_access(const char *name)
+{
+ assert(!open_named(name, 2) && error() == 2);
+ uint32_t original = create(name, 1, 3);
+ assert(original && error() == 0);
+ uint32_t second = create(name, 0, 99);
+ assert(second && second != original && error() == 183);
+ uint32_t modify = open_named(name, 2);
+ uint32_t synchronize = open_named(name, 0x100000);
+ assert(modify && synchronize);
+
+ assert(wait_object(modify, 0) == UINT32_MAX && error() == 5);
+ assert(!release(synchronize, 1) && error() == 5);
+ assert(wait_object(synchronize, 0) == 0);
+ assert(wait_object(second, 0) == 258);
+ assert(release(modify, 3) && previous == 0);
+ assert(!release(second, 1) && error() == 298);
+ assert(close_object(original));
+
+ assert(wait_object(second, 0) == 0);
+ assert(close_object(second) && close_object(modify) && close_object(synchronize));
+ assert(!open_named(name, 2) && error() == 2);
+ uint32_t replacement = create(name, 0, 1);
+ assert(replacement && error() == 0 && wait_object(replacement, 0) == 258);
+ assert(close_object(replacement));
+}
+
+int main(int argc, char **argv)
+{
+#ifndef _WIN32
+ assert(guest_map("semaphore test memory", 0x50000000u, 0x10000u));
+ g_fs_base = 0x50001000u;
+#endif
+ /* Child modes let the differential runner coordinate separate processes. */
+ if (argc == 3) {
+#ifndef _WIN32
+ alarm(30); /* Bound child lifetime even if the WSL launcher is interrupted. */
+#endif
+ const char *mode = argv[1], *name = argv[2];
+ if (!strcmp(mode, "hold")) {
+ assert(create(name, 0, 1));
+ puts("ready");
+ fflush(stdout);
+ getchar();
+#ifdef _WIN32
+ TerminateProcess(GetCurrentProcess(), 9);
+#else
+ kill(getpid(), SIGKILL);
+#endif
+ abort();
+ }
+ uint32_t handle = open_named(name, 0x100002);
+ if (!strcmp(mode, "probe")) {
+ if (handle) { puts("exists"); assert(close_object(handle)); }
+ else { assert(error() == 2); puts("missing"); }
+ return 0;
+ }
+ assert(handle);
+ if (!strcmp(mode, "wait") || !strcmp(mode, "wait-infinite")) {
+ puts("waiting");
+ fflush(stdout);
+ assert(wait_object(handle, !strcmp(mode, "wait") ? 5000 : UINT32_MAX) == 0);
+ puts("awakened");
+ } else {
+ assert(!strcmp(mode, "release"));
+ assert(release(handle, 1));
+ puts("released");
+ }
+ assert(close_object(handle));
+ return 0;
+ }
+ char name[80];
+#ifdef _WIN32
+ snprintf(name, sizeof(name), "AO_Semaphore_Test_%lu", GetCurrentProcessId());
+#else
+ snprintf(name, sizeof(name), "AO_Semaphore_Test_%u", (unsigned)getpid());
+#endif
+ bounded_counts_and_errors();
+ named_lifetime_and_access(name);
+ puts("semaphores: counts, limits, errors, waits, access rights and named lifetime passed");
+ return 0;
+}
diff --git a/tools/reconstruction/test_portable_network_startup.py b/tools/reconstruction/test_portable_network_startup.py
@@ -0,0 +1,26 @@
+"""Compare Winsock startup, cleanup and error state with the native Linux adapters."""
+import pathlib
+import subprocess
+
+
+root = pathlib.Path(__file__).resolve().parents[2]
+decompiled = root / "decompiled"
+work = root / "tools/reconstruction/work/portable-network-startup"
+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/network_startup.c", "-lws2_32", "-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/network_startup.c", "runtime/linux_network.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_scanf.py b/tools/reconstruction/test_portable_scanf.py
@@ -0,0 +1,26 @@
+"""Compare native Windows MSVCRT sscanf with the guest ABI adapter."""
+import pathlib
+import subprocess
+
+
+root = pathlib.Path(__file__).resolve().parents[2]
+decompiled = root / "decompiled"
+work = root / "tools/reconstruction/work/portable-scanf"
+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/guest_scanf.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/guest_scanf.c", "runtime/guest_crt.c", "runtime/guest_memory.c",
+ "runtime/guest_heap.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 MSVCRT == Linux: " + actual.strip())
diff --git a/tools/reconstruction/test_portable_semaphore.py b/tools/reconstruction/test_portable_semaphore.py
@@ -0,0 +1,54 @@
+"""Compare Linux semaphore adapters with Windows, including cross-process lifetime."""
+import os
+import pathlib
+import subprocess
+
+
+root = pathlib.Path(__file__).resolve().parents[2]
+decompiled = root / "decompiled"
+work = root / "tools/reconstruction/work/portable-semaphore"
+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/semaphore.c", "-o", str(reference),
+], cwd=decompiled, check=True)
+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/semaphore.c", "runtime/linux_sync.c", "runtime/guest_memory.c",
+ "runtime/recomp_state.c", "-lm", "-o", linux_output,
+], check=True)
+expected = subprocess.check_output([str(reference)], text=True, timeout=30)
+actual = subprocess.check_output(wsl + [linux_output], text=True, timeout=30)
+assert actual == expected, (expected, actual)
+print("Windows == Linux: " + actual.strip(), flush=True)
+
+# Independent processes open the same object, wait, release and survive another close.
+for command in [[str(reference)], wsl + [linux_output]]:
+ name = f"AO_Semaphore_Process_Test_{os.getpid()}"
+ holder = subprocess.Popen(command + ["hold", name], stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE, text=True)
+ try:
+ assert holder.stdout.readline().strip() == "ready"
+ assert subprocess.check_output(command + ["probe", name], text=True, timeout=10).strip() == "exists"
+ for mode in ["wait", "wait-infinite"]:
+ waiter = subprocess.Popen(command + [mode, name], stdout=subprocess.PIPE, text=True)
+ try:
+ assert waiter.stdout.readline().strip() == "waiting"
+ assert subprocess.check_output(command + ["release", name], text=True, timeout=10).strip() == "released"
+ assert waiter.communicate(timeout=10)[0].strip() == "awakened"
+ assert waiter.returncode == 0
+ finally:
+ if waiter.poll() is None:
+ waiter.terminate()
+ waiter.wait(timeout=10)
+ assert subprocess.check_output(command + ["probe", name], text=True, timeout=10).strip() == "exists"
+ finally:
+ # The child deliberately dies without CloseHandle, on either OS.
+ holder.communicate(input="x\n", timeout=10)
+ assert holder.returncode != 0
+ assert subprocess.check_output(command + ["probe", name], text=True, timeout=10).strip() == "missing"
+print("Windows == Linux: cross-process wait/release and last-handle cleanup after process death passed")