Skip to content

pe_loader: a thread that existed before load() takes its index-0 (/GA) TLS view - #9410

Closed
Vonsek wants to merge 2 commits into
ProjectLeonida:mainfrom
Vonsek:deepseek/3-ga-tls-slots2
Closed

Vonsek wants to merge 2 commits into
ProjectLeonida:mainfrom
Vonsek:deepseek/3-ga-tls-slots2

Conversation

@Vonsek

@Vonsek Vonsek commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: EGEN-GA-TLS-EXISTING-THREADS (#9235 (comment))

Depends on: #8379, #7524 (they touch the same loader TLS area; this patch fixes the merged /GA rewrite instead of reserving index 0)

What and why

The merged /GA TLS rewrite (b7a3fed28) rewrites each mov r64, gs:[58h] in the mapped EXE to read TEB->TlsSlots[slot]. install_thread_block puts a view there ([0] and [tls_index] = that thread's block) for the loading thread and, through the helper DLL's thread event, for every thread started after load(). install_on_existing_threads() copied the block into a thread that already existed but never wrote its TlsSlots entry, so the first /GA access on such a thread dereferenced TEB->TlsSlots[slot] = NULL. That is the same fault the rewrite removes on the loading thread, on the process thread-pool workers where third_party/xtaskqueue runs a title's callbacks (the comment in that function says they predate most loads).

The change gives those threads the same view and takes it out again (restoring what the slot held) with the block in remove_existing_block, in forget_ended_thread_id (a thread that ended without a detach event) and in the rollback path where the view cannot be written.

The /GA fixture now also keeps its TLS directory when it is linked by lld-link (/INCLUDE:_tls_used): with /GA nothing references _tls_index, so a clang-cl + lld-link build wrote no TLS directory, load() had no module index to give the fixture and the test faulted on module_tls_slots()[kNoTlsIndex] instead of failing a check. Reported by @GeremC with a merged-main Wine run.

Tested

Windows 11, MSVC 14.44.35207, RelWithDebInfo + Ninja, rebased on 5d74c2723, branch head 47083e18e.

New /GA fixture runtime/test/fixtures/tiny_ga.cpp (compiled /GA: __declspec(thread) plus a raw __readgsqword(0x58) export) and a loader_tls_test case that starts a thread before load(), maps the EXE, and calls the exports on the loading thread, the pre-existing thread and a thread started afterwards:

ctest --test-dir <build> -R "loader_tls|^loader$|pe_loader" --output-on-failure
  • Before the thread-view fix, same test: 3 FAIL for the pre-existing thread (it also has the /GA view the patched reads need, its /GA view points at its own block, a /GA access on that thread reads its own block), loader_tls_test: FAILED (95 checks, 3 failures), exit 1. The loader's own load() rewrites... check passes, so the fixture exercises the production rewrite.
  • After it: loader_tls_test: OK (95 checks), exit 0 (three runs in a row); ctest -R "loader_tls|^loader$|pe_loader": 4/4 pass.
  • Fixture without a TLS directory (a copy of the built tiny_ga.exe with IMAGE_DIRECTORY_ENTRY_TLS zeroed, the state an lld-link build was in), same loader_tls_test: the pre-fix binary exits -1073741819 (0xC0000005) after 3 /GA FAILs; the fixed binary reports the 8 /GA FAILs that follow (a thread that existed before load() got a TLS block of its own, its /GA view points at its own block, ...), finishes all 95 checks and exits 1. So a missing directory is a failed check now, not a fault.
  • The rebuilt tiny_ga.exe still has its TLS directory (rva 0x82a0, size 0x28) under link.exe, i.e. /INCLUDE:_tls_used changes nothing there.

Not run here: the clang-cl + lld-link build under Wine (no clang-cl or lld-link on this machine) — re-running GeremC's configuration is what closes that half. Not run against the game copy (F:\CLGame is read-only here): a local gta6rt --run <game exe> is what confirms the thread-side fault is gone there.

AI-assisted: yes (Cline).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

Tag: SHAYDERRR

@milas6669

Copy link
Copy Markdown

Checked head 9640c8484 merged onto main 867d922f0, clean (4 files, +186/-9). Native Windows 11 26200, MSVC 19.44 RelWithDebInfo, RTX 3070. ctest -R "loader|pe_loader|tls|run_tiny": 20/20 pass, including loader_tls and run_tiny_game_tls. Negative control, only runtime/src/pe_loader.cpp put back to main's with the PR's loader_tls_test.cpp and tiny_ga.cpp kept: loader_tls_test exits 2 (it does not pass without the change).

Merge order, git merge-tree against the other open PRs that touch the loader (conflicts that do not already exist between the other PR and main alone):

No defect. No game data. AI-assisted (Claude Code).

@milas6669

Copy link
Copy Markdown

Duplicate note: #9478 is the same fix by the same author, opened 14 minutes later under EGEN-TLS-GA-EXISTING-THREAD. I compared both on Windows, results on #9478. AI-assisted (Claude Code).

@ddaikodaiko

Copy link
Copy Markdown

Windows check of 9640c84: Windows 10 Pro 10.0.19045, MSVC 19.44.35229 (VS 2022 Build Tools), Windows SDK 10.0.26100, CMake 3.31 + Ninja, Python 3.14.3. No game files (the fixture builds its own tiny_ga image).

cmake -S runtime -B runtime\build -G Ninja ... (reconfigure: CMakeLists.txt changed)
cmake --build runtime\build --target loader_tls_test -j 10
ctest --test-dir runtime\build -R "^loader_tls$" --output-on-failure
1/1 Test #19: loader_tls .......................   Passed    0.91 sec

The new /GA case runs and passes on Windows 10 with the MSVC toolchain the code targets.

@GeremC

GeremC commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 9640c84 merged with main 5d74c27 under Wine: loader_tls fails in a clang-cl + lld-link build, and the test then crashes. The cause is in how the fixture is linked, not in the loader change. A one-line fix makes it pass.

On: Linux, Ubuntu 24.04, cross-built with clang-cl 19.1.1 + lld-link + xwin SDK (RelWithDebInfo), run through Wine 9.0; CPU only, no game files.

What happens

  • -- /GA index-0 TLS fails on its first check, load() rewrites the /GA fixture's gs:[58h] reads to a TlsAlloc view, and the loader prints no index-0 TLS reads line for tiny_ga.exe.
  • Then the early thread reads module_tls_slots()[index] with index still kNoTlsIndex and faults: page fault on read access to 0xffffffffffffffff at loader_tls_test.cpp:781, with rdx=0xffffffff in the dump. On Linux that opens winedbg. Failing a check there instead of faulting would keep the rest of the run.

Why

  • The lld-link output tiny_ga.exe has a .tls section but no TLS directory (llvm-readobj --coff-tls-directory prints nothing). So total is 0 at the load and the /GA patch block is skipped.
  • With /GA, clang-cl emits mov rax, gs:[58h]; mov rax, [rax] and never references _tls_index. Nothing pulls in the CRT's _tls_used, so lld-link writes no directory. The 9-byte pattern itself matches patch_ga_tls_reads fine: 65 48 8b 04 25 58 00 00 00.

Fix tried here

target_link_options(tiny_ga PRIVATE /OPT:NOREF /INCLUDE:_tls_used)
  • With it, tiny_ga.exe gets a TLS directory, the loader logs tiny_ga.exe: 2 index-0 TLS reads (gs:[58h]) now read its own TLS (TlsSlots[2]); its module TLS index is 1, and loader_tls_test exits 0 under Wine.
  • Negative control, with the same fixture fix and runtime/src/pe_loader.cpp from main: it fails it also has the /GA view the patched reads need (not null), its /GA view points at its own block and a /GA access on that thread reads its own block. So the test does check this PR's existing-thread path.

/INCLUDE:_tls_used should do no harm under MSVC's link.exe, where the directory is already there, but I haven't checked that on Windows.

AI-assisted (Claude Code).

…) TLS view

The /GA rewrite (b7a3fed) points every mov r64, gs:[58h] at TEB->TlsSlots[slot], whose value in a thread is a view with that thread block at [0] and at [tls_index]. install_thread_block gives the loading thread and every thread started after load() one, but install_on_existing_threads() only wrote the module TLS array entry of a thread that already existed. Its TlsSlots entry stayed NULL, so the first /GA access on such a thread, e.g. a title callback on a process thread-pool worker, read through NULL (the retail boot fault class at +0x104075). Give those threads the same view and restore/free it with the block: remove_existing_block, forget_ended_thread_id (a thread terminated without a detach event) and the rollback path. Measured with loader_tls_test and a /GA fixture EXE: before, the pre-existing thread had no view (3 FAIL, exit 1); after, 95/95 checks pass, exit 0. loader, pe_loader, pe_loader_bounds and loader_tls all pass. Not run against the game copy; a local gta6rt --run is what confirms it there.
…s a check instead of faulting

/GA reads gs:[58h] and references no _tls_index, so a clang-cl + lld-link build wrote no TLS directory: load() then had no module index for the fixture, skipped the index-0 patch, and module_tls_slots()[kNoTlsIndex] in the early thread faulted (0xC0000005) instead of failing a check. Pull the CRT's _tls_used into the link and read the slot only when load() set an index.
@Vonsek
Vonsek force-pushed the deepseek/3-ga-tls-slots2 branch from 9640c84 to 47083e1 Compare November 23, 2026 21:40
@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @GeremC — your Wine run is folded in as 47083e18e, and the branch is rebased on 5d74c2723.

What changed

  • runtime/CMakeLists.txt: tiny_ga now links with /OPT:NOREF /INCLUDE:_tls_used. With /GA the fixture references no _tls_index, so lld-link wrote no TLS directory; pulling the CRT's _tls_used in makes the directory exist for both linkers.
  • runtime/test/loader_tls_test.cpp: the early and late thread bodies read module_tls_slots() only when load() set an index, so a fixture without a TLS directory fails the /GA checks instead of faulting on TlsSlots[kNoTlsIndex].

Run here (Windows 11, MSVC 14.44.35207, RelWithDebInfo + Ninja, CPU only, no game files; no clang-cl/lld-link on this machine so your configuration is the one that still needs a re-run)

  • Reproduced your fault with a fixture that has no TLS directory: a copy of the built tiny_ga.exe with IMAGE_DIRECTORY_ENTRY_TLS zeroed. The pre-fix loader_tls_test prints the three loading-thread /GA FAILs and exits -1073741819 (0xC0000005).
  • Same fixture, fixed binary: the eight /GA FAILs that follow (a thread that existed before load() got a TLS block of its own, its /GA view points at its own block, a /GA access on that thread reads its own block, a thread started after load() gets the same /GA view, ...), then the run continues and ends loader_tls_test: FAILED (95 checks, 8 failures), exit 1.
  • Real fixture, fixed binary: loader_tls_test: OK (95 checks), exit 0; ctest -R "loader_tls|^loader$|pe_loader" 4/4 pass.
  • The rebuilt tiny_ga.exe still carries its TLS directory (rva 0x82a0, size 0x28) under link.exe, so /INCLUDE:_tls_used does not change the MSVC build.

The /GA patch itself is unchanged; what is left is exactly your run: clang-cl + lld-link, Wine 9.0, loader_tls should now reach the production rewrite instead of the fault. AI-assisted (Cline).

@Nevrche

Nevrche commented Nov 23, 2026

Copy link
Copy Markdown

Tested 47083e18e on Windows 10 Pro 22H2 (19045), MSVC 19.44.35230, Windows SDK 10.0.26100, VS 2022 generator, RelWithDebInfo: pass.

Test Result
pe_loader Passed 0.18 s
pe_loader_bounds Passed 0.36 s
loader_tls Passed 1.49 s
dll_thread_calls Passed 0.06 s

4/4 passed in 2.15 s. Worth noting this is the MSVC/link.exe build, not lld-link, so the TLS directory layout your description mentions is exercised through the other toolchain on this machine.

AI-assisted.

@nullmarsh

Copy link
Copy Markdown
Contributor

checked: #9410 | head 47083e1 merged onto main 5d74c27 (clean) | on: native Windows 11 26200, MSVC 14.44 RelWithDebInfo (Ninja), RX 9060 XT box | result: pass. loader_tls_test: OK (95 checks), 3 of 3 runs (--repeat until-fail:3). The pre-existing-thread /GA checks are the ones that fail before the fix: it also has the /GA view the patched reads need, its /GA view points at its own block and a /GA access on that thread reads its own block all PASS, as do the loading-thread and new-thread ones. Also pass: loader, pe_loader, pe_loader_bounds, the 14 run_tiny_* tests, run_tiny_frame and run_tiny_frame_queue_test_settings (20/20). Diff read: no process, network or file-removal calls.

AI-assisted (Claude Code), no game files.

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.

7 participants