Repository navigation
Conversation
…S block Windows builds a new thread's TLS array before it calls the DLLs. A load() in between gives that thread a copy of the image's TLS template (install_on_existing_threads) and records it under the thread's id. The thread's own DLL_THREAD_ATTACH event then began with forget_ended_thread_id, which took the record for a dead thread's: it freed the block still in the thread's slot, the install that followed saved the dangling pointer as the helper's block, and DLL_THREAD_DETACH put it back for Windows to free (an access violation at thread exit). A record whose block is the one in this thread's slot is the thread's own and is kept; install_thread_block then leaves the copy alone and detach releases it through the existing path. loader_tls_test holds a thread in its attach with a small fixture DLL loaded ahead of the helper, runs load(), and checks the slot Windows gets back. Controls: a thread started after load(), and a terminated thread's block still being freed when its id is used again. The held-thread case is skipped under Wine, where it has not been run. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Tested
|
|
Independent build + test: PASS AI-assisted (Claude Code). Windows 11 x64, MSVC 19.44.35220, Ninja RelWithDebInfo. GPU (not used): NVIDIA GeForce RTX 3080 driver 621.24 / 32.0.16.2124. Head
So the new fixture reproduces the race on |
|
Tested
AI-assisted (Claude Code). |
…e it is attaching keeps its TLS block Squashed from ItsNotRexed#8512 by Maraco91, head 9867b72 (security scan PASS). Applied as the PR's own diff (gh pr diff): its branch sits on a newer upstream main.
Claim: #8344 (comment)
@ItsNotRexed: an exe PR, as you asked.
What
A thread that
load()reaches while it is still attaching keeps its TLS block.Windows builds a new thread's TLS array first and then calls every DLL's
DLL_THREAD_ATTACH, the loader's helper (gta6rt_tls_host.dll) among the last. Aload()that runs in between sees a running thread with a helper block, soinstall_on_existing_threadsgives it a copy of the image's TLS template and records it under the thread's id, as it does for any thread that was already there.The thread's own attach event then starts with
forget_ended_thread_id(runtime/src/pe_loader.cpp), which took every record under the current thread id for one left by a dead thread:install_thread_blockno longer found the record, allocated a second block, and saved the dangling pointer as "what the slot held before";DLL_THREAD_DETACHthat dangling pointer went back into the slot, for Windows to free as the helper DLL's block.The fix keeps a record whose block is the one in this thread's slot: that record is this thread's own.
install_thread_blockthen leaves the copy alone (itshas_existing_thread_blockcheck is there for a blockload()already gave the thread, and on this path the record was always gone by then), and thread detach takes it out through the existing path. A record of a thread that really ended cannot be mistaken for it: its block is still allocated, so nothing Windows or the loader put into the new thread's array can have its address.Why
It needs a thread to start while an image with static TLS is being loaded: the executable and its DLLs at startup, or a
LoadLibraryat run time while worker threads are being created. In a stand-alone probe on this machine (a DLL loaded ahead of the helper holding one thread in its attach whileload()ran) the slot held the freed loader block at thread exit in 3 of 3 runs, and without repairing the slot the process ended with0xC0000005when that thread exited.How often the retail title hits the window is not measured: it is timing-dependent, I did not run the title, and no report in #7154 names it. When it is hit the result is an access violation at a thread's exit, far from the cause.
Not the TLS index 0 work (#7524) and not #7884 (which threads
load()enumerates): both touch nearby lines, neither changes this function's logic as of their current heads (#7524 keeps the unconditional free).Tested
Windows 11 (10.0.26200), MSVC 19.42 (VS 2022 Build Tools), RelWithDebInfo. No GPU involved, no game files.
main@6dda0630b'spe_loader.cpp): exit 1, one failure, no crash and no hang:loader_tls_test: OK (100 checks), exit 0; 25 of 25 runs in a loop.test/fixtures/tls_attach_hold.cpp, no CRT, kernel32 only) is loaded by the test before anything loads the helper, so itsDllMainruns before the helper's on attach and after it on detach. It holds the watched thread in its attach on an event while the test runsload(), and records the slot at attach and at detach. Every wait has a 10 s timeout that becomes a test failure. If the loader leaves a wrong pointer in the slot, the fixture puts Windows' block back before the thread exits, so the unfixed loader fails an assertion instead of crashing the test.load()returned (one copy, the template value, the helper's block back at detach, the copy freed);test_terminated_thread, unchanged;test_reused_thread_id: a threadload()gave a block is terminated, and once Windows hands its id to a new thread the stale block is freed, so the fix did not stop the function doing its job. When an id comes back is Windows' business (about 580 threads later here); the check only asserts if it does within 4,096 threads and prints a note otherwise.ctestforloader,pe_loader,pe_loader_bounds,loader_tls,dll_thread_calls: 5 of 5 passed.python -I scripts/check_no_game_data.pyon the four changed files: exit 0.Not tested, and one thing to check before merge: I have no Wine here, and the
winejob (which requiresloader_tls) runs only after a merge. The held-thread case rests on Windows'DllMainorder and onload()needing nothing from the loader lock the held thread owns, so it is skipped under Wine with a printedSKIPline rather than risk that job; the fix itself is not conditional. If someone with Wine can runloader_tls_testwith that skip removed and post the result, the skip can go.test_reused_thread_idis not skipped: it makes the same loader calls astest_terminated_thread, which already runs there.AI-assisted (Claude Code).
🤖 Generated with Claude Code