Skip to content

pe_loader: give a thread that existed at load() the /GA TLS view (EGEN-TLS-GA-EXISTING-THREAD) - #9478

Closed
Vonsek wants to merge 1 commit into
ProjectLeonida:mainfrom
Vonsek:deepseek/5-tls-ga-existing-threads
Closed

Vonsek wants to merge 1 commit into
ProjectLeonida:mainfrom
Vonsek:deepseek/5-tls-ga-existing-threads

Conversation

@Vonsek

@Vonsek Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: EGEN-TLS-GA-EXISTING-THREAD (claim)

Depends on: merged b7a3fed28 (the /GA TLS rewrite this completes). Conflicts with #8379, #7524 and #9171, which give the title TLS index 0 by a different design; whichever lands first, this one is a small change on top of the view in b7a3fed28.

What and why

patch_ga_tls_reads() rewrites every mov r64, gs:[58h] in a mapped EXE to read TlsSlots[tls_ga_slot], and install_thread_block() gives that TlsAlloc slot, in each thread, a small view with the image's TLS block at [0] and [tls_index]. install_on_existing_threads() — the pass that gives the threads already running at load() their copy of the TLS template — never created that view. Such a thread has the module TLS block but a NULL array in the TlsAlloc slot, so the first rewritten read on it faults.

The pass now allocates the view for each of those threads, writes it into the thread's TEB through the same field it already uses for the module TLS array (0x1480 + 8 * slot), and keeps it in ExistingThreadBlock so the thread's own detach event, ~Loader or a reused thread id clears the slot and frees it. A view that cannot be written takes the block back like a failed install, with one log line.

This is not the current boot stop: with only the loading thread alive when the title is mapped, --dry-run and --init on a retail 1.0.2.1 copy at 6312c057f are exit 0 (5 modules mapped / 5 initialised, 53,246 sites rewritten, module TLS index 5). It is the one place the merged rewrite left a thread inconsistent, and no test covered the rewrite at all; loader_ga now does.

Tested

Windows 11, MSVC 19.44 (BuildTools 17.14), CPU only (no GPU involved), RelWithDebInfo.

  • ctest -R "loader_ga|loader_tls|pe_loader": 4/4 pass (loader_ga, loader_tls, pe_loader, pe_loader_bounds).
  • loader_test directly with LEONIDA_TEST_DIR on a folder the antivirus does not scan: 88 checks, 0 failed.
  • Negative control, same test binary, parent 5d74c2723 with only runtime/src/pe_loader.cpp reverted:
    FAIL a thread that existed at load() has the /GA view in its TlsAlloc slot, FAIL that thread reads the template through its view (2 failures, exit 1); the loading-thread, new-thread and new-thread TLS-callback checks pass on the parent too. With the change: 9/9 pass, exit 0.
  • python -I scripts/check_no_game_data.py on the new files: exit 0.

loader_ga_test maps tiny_ga_exe, a fixture EXE built from repository source with __declspec(thread) int = 7, /GA, an exported getter/setter and a .CRT$XLB TLS callback that reads the template on every DLL_THREAD_ATTACH (as the CRT's callback does on a title's threads). No game files, no package DLL and no game data are involved.

Remaining to verify locally, agent 1's phase: --run of the retail executable. Nothing here changes its first stop; the view is only added where it was missing.

AI-assisted: yes (Claude).

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. (No translator rule: this is the runtime loader.)

Tag: SHAYDERRR

…N-TLS-GA-EXISTING-THREAD)

patch_ga_tls_reads() rewrites each `mov r64, gs:[58h]` in a mapped EXE to read
TlsSlots[tls_ga_slot]; install_thread_block() gives that slot, in each thread, a
small view of the image's block. install_on_existing_threads() gives the threads
that were already running at load() their copy of the template but never made the
view, so the first rewritten read on such a thread reads a NULL array and faults.

The pass now allocates the view for each of those threads, writes it into the
thread's TEB through the same field it already uses for the module TLS array, and
keeps it in ExistingThreadBlock so the thread's detach event, ~Loader or a reused
thread id clears the slot and frees it. A view that cannot be written takes the
block back like a failed install, with one log line.

No test covered the merged rewrite; loader_ga_test maps a locally built /GA
fixture EXE and checks the loading thread, a thread that existed at load(), and a
thread started afterwards, whose TLS callback reads the template as the CRT's
does. The two pre-existing-thread checks fail on the parent (view NULL) and pass
here. Not the current boot stop: --dry-run and --init on a retail 1.0.2.1 copy at
6312c05 are exit 0, because only the loading thread is alive when the title is
mapped today.

Tested: Windows 11, MSVC 19.44 (BuildTools 17.14), CPU only, RelWithDebInfo;
ctest -R "loader_ga|loader_tls|pe_loader" (4/4), and loader_test directly
(88 checks, 0 failed) with LEONIDA_TEST_DIR on a folder the antivirus does not
scan.
@milas6669

Copy link
Copy Markdown

Same fix as #9410 by the same author, opened 14 minutes earlier under another item ID (EGEN-GA-TLS-EXISTING-THREADS); this is the comparison, in case the coordinator wants to keep one. Native Windows 11 26200, MSVC 19.44 RelWithDebInfo, RTX 3070.

#9478 at 9c395e493, merged onto main 5d74c2723-era 867d922f0 (clean, 5 files, +204/-3): ctest -R "loader|pe_loader|tls|run_tiny" 21/21 pass, including the new loader_ga (it maps a locally built /GA EXE, so it runs the rewritten reads end to end) and loader_tls. Negative control, only pe_loader.cpp put back to main's: loader_ga_test exits 2. #9410 at 9640c8484: 20/20 including loader_tls, negative control loader_tls_test exits 2 (checked on the PR).

What differs in pe_loader.cpp (they conflict with each other there, git merge-tree):

I did not find a behavioural difference that makes one wrong. Keeping #9478's end-to-end test with #9410's saved-value restore and forget_ended_thread_id path would be the combination I would look at. No game data. AI-assisted (Claude Code).

@BatSmacker84

Copy link
Copy Markdown

Tested 9c395e493 merged onto main 5d74c2723 (clean) against the retail executable on Windows 11 26200: pass on the static claim.

  • The executable's executable sections have 53,246 matches for the loader's TLS rewrite pattern.
  • Not checked statically: the five maps/initializations, TLS index 5, the exit status and where startup stops; those come from the reported run. Suggested wording: "The loader scans executable sections for the supported encoding. The reported run with only the loading thread present completed initialization."

AI-assisted: Codex GPT-6.2 Sol ran the checks, Claude Opus 5.5 reviewed the report. The retail executable (SHA-256 e95020b2…8993) and package PE images were read in place; static reads and CPU tests only, and only names and counts are given.

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Current-base check on the head as it stands (9c395e493, whose parent is main 5d74c2723, i.e. it already contains merged b7a3fed28):

  • cmake --build . --target loader_test loader_ga_test loader_tls_test pe_loader_test test_pe_loader_bounds (Ninja, RelWithDebInfo, MSVC 19.44), then ctest -R "loader_ga|loader_tls|^loader$|pe_loader|pe_loader_bounds": 5/5 pass (loader 41.9 s, pe_loader 0.4 s, pe_loader_bounds 2.9 s, loader_tls 3.6 s, loader_ga 0.9 s), 0 failed.
  • LEONIDA_TEST_DIR pointed at a folder the antivirus does not scan.

No source change was needed for the current base; CI on this head is still waiting for approval.

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Encoding coverage of the merged rewrite, measured this pass on the local 1.0.2.1 image (read-only static scan, counts only, no game data):

  • .text: 53,246 reads of TEB->ThreadLocalStoragePointer (mov r64, gs:[58h]) — exactly the 53,246 sites patch_ga_tls_reads() reports — and 0 in any other encoding (no REX-less mov r32, gs:[58h], no lea/add/cmp/test/store form).
  • The other executable sections carry none.

So the single 9-byte encoding the rewrite supports covers every site in this image; limitation 2 raised in #9223 (heretic1321) is not reachable on it. Limitation 1 (a thread that existed at load()) is what this PR fixes.

No source change was needed for the current base 5d74c2723; the branch is that commit plus 9c395e493. ctest -R "loader|pe_loader" on a fresh build of the branch: 5/5 pass (loader, pe_loader, pe_loader_bounds, loader_tls, loader_ga). CI on this head still waits for approval.

AI-assisted: yes (Claude).

@OGShotz

OGShotz commented Nov 23, 2026

Copy link
Copy Markdown

Tested 9c395e4 on Windows 11 x64 with MSVC 19.44 (VS 2022 generator, RelWithDebInfo), on an AMD Ryzen 7 3700X. CPU only, AI-assisted.

  • ctest -R "loader_ga|loader_tls|pe_loader": 4/4 passed. I set LEONIDA_TEST_DIR to a folder of its own.
  • Full CTest run of this branch's build: 113/113 passed. This shell had no VULKAN_SDK, so the Vulkan tests weren't configured.
  • I reproduced the negative control: with only runtime/src/pe_loader.cpp checked out from the merge base 5d74c2723, loader_ga fails with exactly the two lines in the description: a thread that existed at load() has the /GA view in its TlsAlloc slot and that thread reads the template through its view. With the change it passes.
  • Code read: the failure path in install_on_existing_threads gives the block back the same way an install failure does. forget_ended_thread_id now calls remove_existing_block with vector = nullptr, so a reused thread id frees both the block and the view without writing into the new thread's TEB. That's the right fix for the leak the old bare VirtualFree(it->block) would have had once a view existed.

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.

5 participants