commit e786b3802e7cccd397e7bc02076c020d6be1634a
parent 6a8f0575c8979575d960d3b4c231c2becaec660d
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 11:40:48 +0000
Implement automatic-lifetime CRT threads on Linux
Diffstat:
2 files changed, 126 insertions(+), 24 deletions(-)
diff --git a/decompiled/runtime/linux_thread.c b/decompiled/runtime/linux_thread.c
@@ -14,7 +14,7 @@ void recomp_eh_thread_exit(void);
struct guest_thread {
uint32_t handle, id, entry, argument, allocation, stack_size, exit_code;
unsigned references;
- int complete, suspended;
+ int complete, suspended, automatic;
pthread_cond_t changed;
struct guest_thread *next;
};
@@ -43,6 +43,16 @@ static void release_thread_locked(struct guest_thread *thread)
free(thread);
}
+static void close_thread_locked(struct guest_thread *thread)
+{
+ struct guest_thread **link = &threads;
+ while (*link && *link != thread) link = &(*link)->next;
+ if (*link) {
+ *link = thread->next;
+ release_thread_locked(thread);
+ }
+}
+
static void finish_thread(void *argument)
{
struct guest_thread *thread = argument;
@@ -54,6 +64,7 @@ static void finish_thread(void *argument)
check_pthread(pthread_mutex_lock(&mutex));
thread->complete = 1;
check_pthread(pthread_cond_broadcast(&thread->changed));
+ if (thread->automatic) close_thread_locked(thread);
release_thread_locked(thread);
check_pthread(pthread_mutex_unlock(&mutex));
}
@@ -72,7 +83,7 @@ static void *run_thread(void *argument)
MEM32(g_fs_base + 0x20) = getpid();
MEM32(g_fs_base + 0x24) = gettid();
- /* Publish the real host thread ID before _beginthreadex returns. */
+ /* Publish the real host thread ID before returning from thread creation. */
check_pthread(pthread_mutex_lock(&mutex));
thread->id = gettid();
check_pthread(pthread_cond_broadcast(&thread->changed));
@@ -90,26 +101,20 @@ static void *run_thread(void *argument)
abort();
}
entry();
+ if (thread->automatic) g_esp += 4; /* cdecl caller removes the argument. */
if (g_esp != stack) {
- fprintf(stderr, "Guest thread %08x returned with unbalanced stdcall stack\n", thread->entry);
+ fprintf(stderr, "Guest thread %08x returned with unbalanced stack\n", thread->entry);
abort();
}
- thread->exit_code = g_eax;
+ thread->exit_code = thread->automatic ? 0 : g_eax;
pthread_cleanup_pop(1);
return NULL;
}
-static void begin_thread(void)
+static void create_thread(uint32_t stack_size, uint32_t entry, uint32_t argument,
+ uint32_t flags, uint32_t id, int automatic)
{
- uint32_t stack_size = MEM32(g_esp + 8), entry = MEM32(g_esp + 12);
- uint32_t argument = MEM32(g_esp + 16), flags = MEM32(g_esp + 20);
- uint32_t id = MEM32(g_esp + 24);
- if (MEM32(g_esp + 4) || (flags & ~4u)) {
- fprintf(stderr, "Thread security attributes/creation flags %08x are not implemented\n", flags);
- abort();
- }
- g_esp += 4; /* _beginthreadex is cdecl; its entry function is stdcall. */
- g_eax = 0;
+ g_eax = automatic ? UINT32_MAX : 0;
if (!entry) { MEM32(guest_crt_errno_address()) = 22; return; }
if (!stack_size) stack_size = 0x400000;
uint64_t rounded = ((uint64_t)stack_size + 0xffffu) & ~0xffffull;
@@ -122,8 +127,10 @@ static void begin_thread(void)
thread->entry = entry;
thread->argument = argument;
thread->stack_size = rounded;
- thread->references = 2;
+ /* Creation retains a reference even if an automatic thread exits immediately. */
+ thread->references = 3;
thread->suspended = flags == 4;
+ thread->automatic = automatic;
pthread_condattr_t attributes;
check_pthread(pthread_condattr_init(&attributes));
check_pthread(pthread_condattr_setclock(&attributes, CLOCK_MONOTONIC));
@@ -151,10 +158,31 @@ static void begin_thread(void)
while (!thread->id) check_pthread(pthread_cond_wait(&thread->changed, &mutex));
if (id) MEM32(id) = thread->id;
g_eax = thread->handle;
+ release_thread_locked(thread);
}
check_pthread(pthread_mutex_unlock(&mutex));
}
+static void begin_thread(void)
+{
+ uint32_t stack_size = MEM32(g_esp + 8), entry = MEM32(g_esp + 12);
+ uint32_t argument = MEM32(g_esp + 16), flags = MEM32(g_esp + 20);
+ uint32_t id = MEM32(g_esp + 24);
+ if (MEM32(g_esp + 4) || (flags & ~4u)) {
+ fprintf(stderr, "Thread security attributes/creation flags %08x are not implemented\n", flags);
+ abort();
+ }
+ g_esp += 4; /* _beginthreadex is cdecl; its entry function is stdcall. */
+ create_thread(stack_size, entry, argument, flags, id, 0);
+}
+
+static void begin_automatic_thread(void)
+{
+ uint32_t entry = MEM32(g_esp + 4), stack_size = MEM32(g_esp + 8), argument = MEM32(g_esp + 12);
+ g_esp += 4;
+ create_thread(stack_size, entry, argument, 0, 0, 1);
+}
+
static void end_thread(void)
{
if (!current_thread) {
@@ -165,17 +193,23 @@ static void end_thread(void)
pthread_exit(NULL);
}
+static void end_automatic_thread(void)
+{
+ if (!current_thread) {
+ fprintf(stderr, "_endthread outside a created guest thread\n");
+ abort();
+ }
+ current_thread->exit_code = 0;
+ current_thread->automatic = 1;
+ pthread_exit(NULL);
+}
+
static void close_thread(void)
{
check_pthread(pthread_mutex_lock(&mutex));
struct guest_thread *thread = find_thread_locked(MEM32(g_esp + 4));
g_eax = thread != NULL;
- if (thread) {
- struct guest_thread **link = &threads;
- while (*link != thread) link = &(*link)->next;
- *link = thread->next;
- release_thread_locked(thread);
- }
+ if (thread) close_thread_locked(thread);
check_pthread(pthread_mutex_unlock(&mutex));
g_esp += 8;
}
@@ -234,6 +268,8 @@ recomp_func_t linux_thread_lookup(const char *name)
{
if (!strcmp(name, "_beginthreadex")) return begin_thread;
if (!strcmp(name, "_endthreadex")) return end_thread;
+ if (!strcmp(name, "_beginthread")) return begin_automatic_thread;
+ if (!strcmp(name, "_endthread")) return end_automatic_thread;
if (!strcmp(name, "CloseHandle")) return close_thread;
if (!strcmp(name, "WaitForSingleObject")) return wait_thread;
if (!strcmp(name, "GetExitCodeThread")) return get_exit_code;
diff --git a/decompiled/tests/threads.c b/decompiled/tests/threads.c
@@ -6,6 +6,8 @@
#include <windows.h>
static uintptr_t (__cdecl *native_begin)(void *, unsigned, unsigned (__stdcall *)(void *), void *, unsigned, unsigned *);
static void (__cdecl *native_end)(unsigned);
+static uintptr_t (__cdecl *native_begin_automatic)(void (__cdecl *)(void *), unsigned, void *);
+static void (__cdecl *native_end_automatic)(void);
static int *(__cdecl *native_errno)(void);
static CRITICAL_SECTION counter_lock;
static unsigned counter;
@@ -251,10 +253,32 @@ static unsigned worker(uint32_t tag)
return 0x12345600 + tag;
}
+static void automatic_worker(uint32_t mode)
+{
+ assert(tls_get() == 0);
+ tls_set(0x76543210);
+ if (mode != 2) {
+ signal_semaphore(ready, 1);
+ assert(wait(gate, 10000, 0) == 0);
+ }
+ assert(tls_get() == 0x76543210);
+ signal_semaphore(done, 1);
+ if (mode == 1) {
#ifdef _WIN32
+ native_end_automatic();
+#else
+ call(linux_thread_lookup("_endthread"), 0, NULL, 1);
+#endif
+ abort();
+ }
+}
+
+#ifdef _WIN32
+static void __cdecl automatic_entry(void *argument) { automatic_worker((uintptr_t)argument); }
static unsigned __stdcall returning_entry(void *argument) { return worker((uintptr_t)argument); }
static unsigned __stdcall exiting_entry(void *argument) { native_end(worker((uintptr_t)argument)); abort(); }
#else
+static void automatic_entry(void) { automatic_worker(MEM32(g_esp + 4)); g_esp += 4; }
static void returning_entry(void) { g_eax = worker(MEM32(g_esp + 4)); g_esp += 8; }
static void exiting_entry(void)
{
@@ -270,6 +294,7 @@ recomp_func_t recomp_lookup(uint32_t address)
{
if (address == 0x11001000) return returning_entry;
if (address == 0x11002000) return exiting_entry;
+ if (address == 0x11003000) return automatic_entry;
if (address == 0x42001000) return sub_42001000;
if (address == 0x42001100) return sub_42001100;
if (address == 0x42002000) return native32_shim_CxxThrowException;
@@ -345,6 +370,41 @@ static void closing_a_running_thread_does_not_terminate_it(void)
assert(id == observed_ids[3]);
}
+static void automatic_thread_closes_its_handle(uint32_t mode)
+{
+#ifdef _WIN32
+ uint32_t thread = native_begin_automatic(automatic_entry, 0, (void *)(uintptr_t)mode);
+#else
+ uint32_t args[] = {0x11003000, 0, mode};
+ uint32_t thread = call(linux_thread_lookup("_beginthread"), 3, args, 1);
+#endif
+ assert(thread && thread != UINT32_MAX);
+ if (mode != 2) assert(wait(ready, 10000, 0) == 0 && exit_code(thread) == 259);
+
+ if (mode != 2) signal_semaphore(gate, 1);
+ assert(wait(done, 10000, 0) == 0);
+
+ /* The semaphore precedes CRT cleanup; wait for automatic handle invalidation. */
+ int open = 1;
+ for (unsigned i = 0; open && i < 1000; ++i) {
+#ifdef _WIN32
+ DWORD code;
+ open = GetExitCodeThread((HANDLE)(uintptr_t)thread, &code);
+ if (!open) assert(GetLastError() == 6);
+ else Sleep(1);
+#else
+ uint32_t query[] = {thread, g_esp - 0x200};
+ open = call(linux_thread_lookup("GetExitCodeThread"), 2, query, 0);
+ if (!open) assert(MEM32(g_fs_base + 0x34) == 6);
+ else {
+ uint32_t delay = 1;
+ call(linux_platform_lookup("kernel32.dll", "Sleep"), 1, &delay, 0);
+ }
+#endif
+ }
+ assert(!open && tls_get() == 0xabcdef01);
+}
+
int main(void)
{
#ifdef _WIN32
@@ -352,8 +412,10 @@ int main(void)
assert(crt);
native_begin = (void *)GetProcAddress(crt, "_beginthreadex");
native_end = (void *)GetProcAddress(crt, "_endthreadex");
+ native_begin_automatic = (void *)GetProcAddress(crt, "_beginthread");
+ native_end_automatic = (void *)GetProcAddress(crt, "_endthread");
native_errno = (void *)GetProcAddress(crt, "_errno");
- assert(native_begin && native_end && native_errno);
+ assert(native_begin && native_end && native_begin_automatic && native_end_automatic && native_errno);
tls_index = TlsAlloc();
InitializeCriticalSection(&counter_lock);
#else
@@ -381,13 +443,16 @@ int main(void)
suspended_thread_returns_and_remains_signaled();
concurrent_threads_keep_private_state();
closing_a_running_thread_does_not_terminate_it();
+ automatic_thread_closes_its_handle(0);
+ automatic_thread_closes_its_handle(1);
+ for (unsigned i = 0; i < 32; ++i) automatic_thread_closes_its_handle(2);
#ifndef _WIN32
/* The closed thread signals done before its detach callback completes. */
- for (unsigned i = 0; atomic_load(&detached) < 4 && i < 1000; ++i) {
+ for (unsigned i = 0; atomic_load(&detached) < 38 && i < 1000; ++i) {
uint32_t delay = 1;
call(linux_platform_lookup("kernel32.dll", "Sleep"), 1, &delay, 0);
}
- assert(attached == 4 && detached == 4);
+ assert(attached == 38 && detached == 38);
assert(!pthread_barrier_destroy(&cpu_barrier));
assert(call(linux_tls_lookup("TlsFree"), 1, &tls_index, 0));
call(linux_platform_lookup("kernel32.dll", "DeleteCriticalSection"), 1, §ion, 0);
@@ -397,5 +462,6 @@ int main(void)
#endif
assert(close_handle(ready, 0) && close_handle(gate, 0) && close_handle(done, 0));
puts("Threads: concurrent execution, private TLS/errno, IDs, suspended start, waits, exit codes and handle lifetime passed");
+ puts("_beginthread: cdecl arguments, returned/explicit exit, automatic handle close and 32 immediate exits passed");
return 0;
}