From 66a7dbc1cd49cc29b34ceeab3020d42ca85f03d2 Mon Sep 17 00:00:00 2001 From: mariuslozo-boop <254631011+mariuslozo-boop@users.noreply.github.com> Date: Mon, 23 Nov 2026 00:12:40 +0100 Subject: Loader: a mapped executable owns module TLS index 0 Windows gives the process executable module TLS index 0, and MSVC code built with /GA reads TEB->ThreadLocalStoragePointer[0] without loading _tls_index. The loader gave a mapped EXE a gta6rt_tls_host.dll clone's index instead, so a thread_local int v = 42 EXE built /O2 /GA /MD printed tls_index=6 value=0 under gta6rt --run (natively tls_index=0 value=42). Index 0 belongs to gta6rt.exe itself (and to each test EXE that links pe_loader.cpp): measured through _tls_index == 0 before any mapping and the new tls_index0 trace event. All of its own TLS reads go through _tls_index, so the first time an executable is mapped the loader loads one more helper clone (leonida_tls_host__home.dll), stops the other threads, swaps slots 0 and the clone's index on every thread and writes that index into the owner's _tls_index. Threads started later swap their own slots first thing in the helper's DLL_THREAD_ATTACH. Both pointers are OS-allocated blocks of that thread and the OS frees every slot at thread exit, so the swap is permanent and nothing is restored. The executable then takes slot 0 (kMainImageSlot) through the existing per-thread install, thread events and ~Loader paths; DLLs keep the helper indices. A second executable mapped while one is live, or a move that cannot be made (a thread kept running in the owner's code for 250 ms), gets a helper index and a stderr note. Measured on Windows 11 26300 to make the swap safe: a new thread publishes its TLS array in the TEB only once it is filled (LdrpAllocateTls), an ending one clears it before freeing the blocks (LdrpFreeTls), LoadLibrary of a TLS DLL updates other threads in one NtSetInformationProcess call, and an EXE's TLS callbacks run after every DllMain for thread attach and detach. Tests: loader_tls_index0 maps tiny_tls_ga.exe (built /GA, code shape checked) next to tiny_tls.dll: 42 on the loading thread, a pre-existing thread and new threads, this EXE's own TLS intact on all of them, two runs in a row, a second executable at once, and a busy thread holding the move off. run_tiny_tls_ga runs it under gta6rt --run and --init and checks the trace names gta6rt.exe. Co-Authored-By: Claude Opus 5.5 --- runtime/src/tls_host.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'runtime/src/tls_host.cpp') diff --git a/runtime/src/tls_host.cpp b/runtime/src/tls_host.cpp index ab234cbd9..a55941873 100644 --- a/runtime/src/tls_host.cpp +++ b/runtime/src/tls_host.cpp @@ -5,6 +5,11 @@ // __declspec(thread) access in mapped code reads a valid TEB slot. The anchor itself is never used; the loader saves // and restores the helper's own block, so the helper's CRT TLS state is not disturbed. // +// One more clone, leonida_tls_host__home.dll, is loaded the first time an executable is mapped: the runtime's +// own EXE, which Windows gave module TLS index 0, moves its block to that clone's index on every thread so the mapped +// executable can take index 0. That clone stays loaded until the process ends, and its slot then holds the runtime's +// live TLS, so nothing in this DLL may read or write its own TLS (gta6rt_tls_host_keep is never called). +// // The Windows loader also calls this DLL's DllMain with DLL_THREAD_ATTACH on every new thread (after it has built that // thread's TLS array) and with DLL_THREAD_DETACH when a thread exits (before it frees the array). The loader registers // a callback here so it can give each new thread its own copy of the mapped image's TLS, and put this DLL's block back -- cgit v1.2.3