Skip to content

Commit f843db8

Browse files
committed
revert(windows): drop the TLS thread-heap release
Reverts the DeusData#581 fix mechanism, not the diagnosis. main's dry run passes both windows-11-arm shards and test-windows-guards; this branch fails all three. The only change here that affects process LOAD is the .CRT$XLB detach callback, and the arm64 failures are load-time crashes (STATUS_ILLEGAL_INSTRUCTION, pass=0, secs=0, five suites in the same second) -- while the guards exercise many launcher and payload spawns. The same binary loads fine on a UTM arm64 VM, so the placement is tolerated by some loaders and not others, which is not something to ship on a supported platform. DeusData#581's root cause stands and is documented: a static MinGW link has no DllMain and registers no TLS callback, so mi_thread_done never runs and every thread leaks its heap (607 heaps after 300 requests, 170 MiB against ~300 KiB live). The delivery mechanism needs to be one the loader accepts everywhere -- an FLS callback registered at runtime is the obvious candidate, with no linker section placement involved. Signed-off-by: Martin Vogel <[email protected]>
1 parent d2dee7b commit f843db8

1 file changed

Lines changed: 0 additions & 48 deletions

File tree

‎src/foundation/compat_thread.c‎

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
#include "foundation/constants.h"
88
#include "foundation/compat_thread.h"
99

10-
#include "foundation/platform.h"
11-
12-
#include <mimalloc.h> /* mi_thread_done at thread exit */
13-
1410
#include <pthread.h>
1511
#include <stdlib.h>
1612

@@ -28,50 +24,6 @@ typedef struct {
2824
void *arg;
2925
} win_thread_arg_t;
3026

31-
/* Release each thread's allocator heap at DLL_THREAD_DETACH.
32-
*
33-
* Doing this from the thread wrapper instead crashes: the wrapper returns
34-
* before the CRT's own thread teardown, and abandoning the heap there raced
35-
* with frees still in flight (daemon_ipc_wait_forever_is_interruptible
36-
* segfaulted, rc=139, reproducibly and only with the release enabled). A TLS
37-
* callback is the mechanism mimalloc itself uses under MSVC and runs after all
38-
* other thread cleanup, which is the only point where the heap is genuinely
39-
* unreferenced.
40-
*
41-
* Without any of this, a static MinGW link -- no DllMain, no TLS callback --
42-
* never releases a thread heap at all: 607 heaps after 300 requests, 170 MiB
43-
* held against a ~300 KiB live set, growing without bound (#581). POSIX gets
44-
* this from a pthread TSD destructor, which is why only Windows leaked.
45-
*
46-
* CBM_MI_THREAD_DONE=0 disables it, so one binary can demonstrate both
47-
* behaviours rather than requiring a rebuild to establish causality. */
48-
static bool thread_release_heap_enabled(void) {
49-
static int state = -1;
50-
if (state < 0) {
51-
char buf[CBM_SZ_16];
52-
state =
53-
(cbm_safe_getenv("CBM_MI_THREAD_DONE", buf, sizeof(buf), NULL) != NULL && buf[0] == '0')
54-
? 0
55-
: 1;
56-
}
57-
return state == 1;
58-
}
59-
60-
static void NTAPI cbm_thread_detach_callback(PVOID handle, DWORD reason, PVOID reserved) {
61-
(void)handle;
62-
(void)reserved;
63-
if (reason == DLL_THREAD_DETACH && thread_release_heap_enabled()) {
64-
mi_thread_done();
65-
}
66-
}
67-
68-
/* Park the callback in .CRT$XLB, the table the loader walks. The linker only
69-
* emits a TLS directory when _tls_used is referenced, so anchor it. */
70-
extern const IMAGE_TLS_DIRECTORY64 _tls_used;
71-
static const void *const cbm_tls_anchor __attribute__((used)) = &_tls_used;
72-
__attribute__((section(".CRT$XLB"), used)) PIMAGE_TLS_CALLBACK cbm_thread_detach_tls_cb =
73-
cbm_thread_detach_callback;
74-
7527
static DWORD WINAPI win_thread_wrapper(LPVOID lpParam) {
7628
win_thread_arg_t *a = (win_thread_arg_t *)lpParam;
7729
void *(*fn)(void *) = a->fn;

0 commit comments

Comments
 (0)