Skip to content

loader: wide run-time loads are dynamic; set tls_ga_slot before publishing (RT-LOADER-RUNTIME-SCOPE-AND-TLS-SLOT) - #2499

Open
Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:rt-loader-runtime-scope-and-tls-slot
Open

Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:rt-loader-runtime-scope-and-tls-slot

Conversation

@Tyrannys

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-LOADER-RUNTIME-SCOPE-AND-TLS-SLOT (#1608 (comment))

Found by @kevincommits, item 3 of #1608 (comment). Thank you.

What and why

1. Wide run-time loads were treated as static loads. Loader::load_runtime_module and Loader::resolve_module_export open a RuntimeLoadScope, so the modules they map get dynamic_load = true, and DllMain(DLL_PROCESS_ATTACH) gets a null lpvReserved, as Win32 does for a dynamic load. Loader::load_runtime_module_w (bundled DLLs through LoadLibraryW / LoadLibraryExW) did not open the scope, so DllMain got the static load's non-null &context. It now opens the scope, in the same place as the narrow path.

2. m.tls_ga_slot was written after the slot owner was published. Loader::initialize_tls publishes g_tls_slot_owners[slot] under ExclusiveTlsLock, with the comment "The fields are complete before a thread event can see the owner". It then released the lock, and only after that patched the executable's /GA reads (patch_ga_tls_reads) and set m.tls_ga_slot. A thread starting in between runs install_thread_block under SharedTlsLock and reads m.tls_ga_slot without synchronization. Now the TlsAlloc, the patch and the tls_ga_slot write all happen before the owner is published. The moved block now uses the executable flag the function already computes, instead of re-reading the headers. Its log lines are unchanged.

Tested

Windows 11 Pro 26300, MSVC 14.52 (VS 2026), RelWithDebInfo, on main 43a9d562d. Profile M: RTX 4070 SUPER 12 GB, NVIDIA 621.01, 64 GB. Built at --parallel 4, below-normal priority, while a game boot was running on the same machine. No game files. CPU only.

  • loader_test: new check a wide run-time load's DllMain gets a NULL lpvReserved. It loads the same lifecycle fixture as the narrow case through load_runtime_module_w. It fails on main (lpvReserved was non-NULL) and passes with the fix. The static and narrow cases still pass.
  • loader_tls_index0_test noreserve: one new check that the executable, mapped at a helper index, gets tls_ga_slot and patched /GA sites. This passes on main too; it only confirms the moved code still runs.
  • About the race: I also tried a stress test (4 threads starting 64 threads each while the /GA executable loads, 40 loads, then each thread checks its own /GA view). It found nothing on main: 0 of 30,720 threads in three runs. The reason is in install_on_existing_threads. It runs after the write and gives every thread alive then a block and a view. So a thread that attached in the window is fixed before load() returns. What is left on main is the unsynchronized write and read of m.tls_ga_slot, plus a first block without a view that the later pass replaces. This change removes both. I did not keep the stress test, because it cannot fail on main.
  • ctest -R "loader|run_tiny|module_names|library_loader|tls": 30/30 pass. git diff --check and scripts/check_no_game_data.py on the three files: clean.

Not run: the full suite, because a boot was running.

Open PRs touching pe_loader.cpp (#1461, #664, #2190, #2414) change other parts of it.

GPUs tested: n/a (CPU-only loader change; machine above)

AI-assisted: yes. AI-assisted (Claude Code).

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • 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: n/a (runtime loader).

🤖 Generated with Claude Code

…shing (RT-LOADER-RUNTIME-SCOPE-AND-TLS-SLOT)

Loader::load_runtime_module_w (LoadLibraryW / LoadLibraryExW of a
bundled DLL) did not open a RuntimeLoadScope, unlike the narrow path and
resolve_module_export. The module was mapped as a static load, so its
DllMain(DLL_PROCESS_ATTACH) got a non-NULL lpvReserved. It now opens the
scope too.

Loader::initialize_tls patched an executable's /GA TLS reads and wrote
m.tls_ga_slot after it had published the slot owner and released
ExclusiveTlsLock, while a thread starting then reads m.tls_ga_slot in
install_thread_block under SharedTlsLock. The patch and the write now
happen before the owner is published.

loader_test checks the wide path's lpvReserved (fails on main);
loader_tls_index0_test checks that the noreserve executable gets its
/GA slot.

Found by kevincommits on ProjectLeonida#1608.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant