loader: wide run-time loads are dynamic; set tls_ga_slot before publishing (RT-LOADER-RUNTIME-SCOPE-AND-TLS-SLOT) #2499
No reviewers
No assignees
Labels
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
No project
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
No due date set.
Reference
leonida/mandy!2499
Loading…
Reference in a new issue
No description provided.
Delete branch "rt-loader-runtime-scope-and-tls-slot"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Pull request successfully merged and closed
Claimed in the coordination issue as: RT-LOADER-RUNTIME-SCOPE-AND-TLS-SLOT (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7226661682)
Found by @kevincommits, item 3 of https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7225958148. Thank you.
What and why
1. Wide run-time loads were treated as static loads.
Loader::load_runtime_moduleandLoader::resolve_module_exportopen aRuntimeLoadScope, so the modules they map getdynamic_load = true, andDllMain(DLL_PROCESS_ATTACH)gets a nulllpvReserved, as Win32 does for a dynamic load.Loader::load_runtime_module_w(bundled DLLs throughLoadLibraryW/LoadLibraryExW) did not open the scope, soDllMaingot the static load's non-null&context. It now opens the scope, in the same place as the narrow path.2.
m.tls_ga_slotwas written after the slot owner was published.Loader::initialize_tlspublishesg_tls_slot_owners[slot]underExclusiveTlsLock, 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/GAreads (patch_ga_tls_reads) and setm.tls_ga_slot. A thread starting in between runsinstall_thread_blockunderSharedTlsLockand readsm.tls_ga_slotwithout synchronization. Now theTlsAlloc, the patch and thetls_ga_slotwrite all happen before the owner is published. The moved block now uses theexecutableflag 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
main43a9d562d. 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 checka wide run-time load's DllMain gets a NULL lpvReserved. It loads the same lifecycle fixture as the narrow case throughload_runtime_module_w. It fails onmain(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, getstls_ga_slotand patched/GAsites. This passes onmaintoo; it only confirms the moved code still runs./GAexecutable loads, 40 loads, then each thread checks its own/GAview). It found nothing onmain: 0 of 30,720 threads in three runs. The reason is ininstall_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 beforeload()returns. What is left onmainis the unsynchronized write and read ofm.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 onmain.ctest -R "loader|run_tiny|module_names|library_loader|tls": 30/30 pass.git diff --checkandscripts/check_no_game_data.pyon 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
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed: n/a (runtime loader).🤖 Generated with Claude Code
Reviewed the diff and tested locally on Windows 11 (MSVC 19.44, Ninja Release).
Loader::load_runtime_module_w()now opensRuntimeLoadScope dynamic(runtime_loads_)undermutex_, matchingload_runtime_module()so wide dynamic loads pass a nulllpvReservedtoDllMain(DLL_PROCESS_ATTACH)./GAindex-0 TLS read rewrite (patch_ga_tls_reads/m.tls_ga_slot) before publishing&mintog_tls_slot_owners[slot]closes the race where a concurrent thread event (install_thread_block) could see the owner beforem.tls_ga_slotwas set.loader_testandloader_tls_index0_testand ranctest --output-on-failure:loader: passed (100 checks, 0 failed, including the wide run-time loadlpvReserved == NULLcheck).loader_tls_index0,loader_tls_index0_threads,loader_tls_index0_noreserve: 3/3 passed (including thetls_ga_slot/tls_ga_sitescheck inscenario_noreserve).AI-assisted (Antigravity, Gemini).
Reviewed at head
1a49f1375on Windows 11 (MSVC 202219.44).Verification
loader_test,loader_tls_index0_test, and fixtures (tiny_tls_ga,tiny_tls,tiny_thread_hook).loader_test: 100/100 checks passed.ctest -R "^loader_tls_index0": 3/3 passed (loader_tls_index0,loader_tls_index0_threads,loader_tls_index0_noreserve).Notes
pe_loader.cpp:1483-1516), captured before import binding (1423-1428) so dependencies inherit dynamicDLL_PROCESS_ATTACH(1664-1665).tls_ga_slotand completing the/GApatch before publishing the owner under the exclusive TLS lock (1895-1931) cleanly pairs with the shared-lock thread-event reader (1150-1164).Approve.
AI-assisted review + local MSVC verification.
Red/green: On main loader_test fails 1 check (DllMain of a wide run-time load gets a non-NULL lpvReserved), and with the PR ctest -R "^(loader|loader_tls_index0|loader_tls_index0_threads|loader_tls_index0_noreserve)$" passes 4/4.
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
Verified by OreoFR
c1ecf04ed+ PR head1a49f1375; green run in a merge package of 11 PRs, no new ctest failure vs mainctest -R "^(loader|loader_tls_index0|loader_tls_index0_threads|loader_tls_index0_noreserve)$"AI-assisted (Claude Code, Claude Opus 5.5).