Skip to content

pe_loader: a thread load() reaches while it is attaching keeps its TLS block - #8512

Closed
Maraco91 wants to merge 1 commit into
ProjectLeonida:mainfrom
Maraco91:fix/loader-tls-block-thread-mid-attach
Closed

Maraco91 wants to merge 1 commit into
ProjectLeonida:mainfrom
Maraco91:fix/loader-tls-block-thread-mid-attach

Conversation

@Maraco91

Copy link
Copy Markdown
Contributor

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. A load() that runs in between sees a running thread with a helper block, so install_on_existing_threads gives 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:

  1. it freed the block, which was still in the thread's TLS slot, and dropped the record without restoring the slot;
  2. install_thread_block no longer found the record, allocated a second block, and saved the dangling pointer as "what the slot held before";
  3. at DLL_THREAD_DETACH that 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_block then leaves the copy alone (its has_existing_thread_block check is there for a block load() 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 LoadLibrary at 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 while load() ran) the slot held the freed loader block at thread exit in 3 of 3 runs, and without repairing the slot the process ended with 0xC0000005 when 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.

cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_REQUIRE_VULKAN=ON
cmake --build runtime/build --target loader_tls_test
ctest --test-dir runtime/build -C RelWithDebInfo -R "^(loader_tls|loader|pe_loader|pe_loader_bounds|dll_thread_calls)$"
  • Before (the new test on main@6dda0630b's pe_loader.cpp): exit 1, one failure, no crash and no hang:
    FAIL the held thread ends with the helper DLL's block back in its slot
    loader_tls_test: FAILED (100 checks, 1 failure)
    
  • After: loader_tls_test: OK (100 checks), exit 0; 25 of 25 runs in a loop.
  • How the test reaches the window: a small fixture DLL (test/fixtures/tls_attach_hold.cpp, no CRT, kernel32 only) is loaded by the test before anything loads the helper, so its DllMain runs 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 runs load(), 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.
  • Negative controls, passing before and after:
    • the same checks on a thread started after load() returned (one copy, the template value, the helper's block back at detach, the copy freed);
    • test_terminated_thread, unchanged;
    • a new test_reused_thread_id: a thread load() 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.
  • ctest for loader, pe_loader, pe_loader_bounds, loader_tls, dll_thread_calls: 5 of 5 passed.
  • python -I scripts/check_no_game_data.py on the four changed files: exit 0.

Not tested, and one thing to check before merge: I have no Wine here, and the wine job (which requires loader_tls) runs only after a merge. The held-thread case rests on Windows' DllMain order and on load() needing nothing from the loader lock the held thread owns, so it is skipped under Wine with a printed SKIP line rather than risk that job; the fix itself is not conditional. If someone with Wine can run loader_tls_test with that skip removed and post the result, the skip can go. test_reused_thread_id is not skipped: it makes the same loader calls as test_terminated_thread, which already runs there.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…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>
@lohant27-netizen

Copy link
Copy Markdown

Tested 9867b72aa on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release, merged onto main 867d922f0 together with #8542, #8529, #8517, #8513, #8512 and #8530 (no conflicts): pass.

@MartinIndra02

Copy link
Copy Markdown
Contributor

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 9867b72aa, merged locally with current main 867d922f0 (CMakeLists.txt auto-merged). Reconfigured and built loader_tls_test, the new tls_attach_hold fixture, tiny_tls, tiny_tls_cb_only, gta6rt_tls_host, pe_loader_test, loader_test and test_pe_loader_bounds.

Run Result
ctest -R loader --repeat until-fail:3 (loader, pe_loader, pe_loader_bounds, loader_tls) 4/4 passed on all 3 repeats
ctest -R "^loader_tls$" with runtime/src/pe_loader.cpp reverted to main failed: FAIL the held thread ends with the helper DLL's block back in its slot, loader_tls_test: FAILED (100 checks, 1 failure)

So the new fixture reproduces the race on main (a load() during a thread's DLL_THREAD_ATTACH), and the one-line check in forget_ended_thread_id fixes it. Its condition (slots[it->index] == it->block) can't match a record from an ended thread, because that block is still allocated. No issues found.

@RAINighty

Copy link
Copy Markdown

Tested 9867b72aa merged with main 867d922f0 (clean merge) on Windows 11 (10.0.26300), Intel Core i7-14650HX, MSVC 19.44.35228, RelWithDebInfo (Ninja), CPU-only tests: pass.

AI-assisted (Claude Code).

@karearl

karearl commented Nov 23, 2026

Copy link
Copy Markdown

On main here d3d12_vulkan_device (tiles == 1, fixed by #8496/#7125) and d3d12_depth_resolve fail without the PR too; one-off failures in a full -j 8 run were rerun alone 3 times and passed.

AI-assisted (Claude Code).

dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants