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

Merged
Tyrannys merged 1 commit from upstream-pr/2499 into main 2026-11-28 17:51:52 +00:00
Tyrannys commented 2026-11-27 19:41:06 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-27T19:41:06Z.


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_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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5532191988 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2499). Originally posted by [Tyrannys](https://github.com/Tyrannys) on 2026-11-27T19:41:06Z. --- 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_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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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](https://claude.com/claude-code)
lixonik commented 2026-11-27 21:40:57 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-27T21:40:57Z.

Review: APPROVED


Reviewed the diff and tested locally on Windows 11 (MSVC 19.44, Ninja Release).

  • Loader::load_runtime_module_w() now opens RuntimeLoadScope dynamic(runtime_loads_) under mutex_, matching load_runtime_module() so wide dynamic loads pass a null lpvReserved to DllMain(DLL_PROCESS_ATTACH).
  • Moving the /GA index-0 TLS read rewrite (patch_ga_tls_reads / m.tls_ga_slot) before publishing &m into g_tls_slot_owners[slot] closes the race where a concurrent thread event (install_thread_block) could see the owner before m.tls_ga_slot was set.
  • Built loader_test and loader_tls_index0_test and ran ctest --output-on-failure:
    • loader: passed (100 checks, 0 failed, including the wide run-time load lpvReserved == NULL check).
    • loader_tls_index0, loader_tls_index0_threads, loader_tls_index0_noreserve: 3/3 passed (including the tls_ga_slot / tls_ga_sites check in scenario_noreserve).

AI-assisted (Antigravity, Gemini).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6093565881 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2499#pullrequestreview-6093565881). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-27T21:40:57Z. **Review:** APPROVED --- Reviewed the diff and tested locally on Windows 11 (MSVC 19.44, Ninja Release). - `Loader::load_runtime_module_w()` now opens `RuntimeLoadScope dynamic(runtime_loads_)` under `mutex_`, matching `load_runtime_module()` so wide dynamic loads pass a null `lpvReserved` to `DllMain(DLL_PROCESS_ATTACH)`. - Moving the `/GA` index-0 TLS read rewrite (`patch_ga_tls_reads` / `m.tls_ga_slot`) before publishing `&m` into `g_tls_slot_owners[slot]` closes the race where a concurrent thread event (`install_thread_block`) could see the owner before `m.tls_ga_slot` was set. - Built `loader_test` and `loader_tls_index0_test` and ran `ctest --output-on-failure`: - `loader`: passed (100 checks, 0 failed, including the wide run-time load `lpvReserved == NULL` check). - `loader_tls_index0`, `loader_tls_index0_threads`, `loader_tls_index0_noreserve`: 3/3 passed (including the `tls_ga_slot` / `tls_ga_sites` check in `scenario_noreserve`). AI-assisted (Antigravity, Gemini).
timbot103 commented 2026-11-28 02:01:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-28T02:01:40Z.


Reviewed at head 1a49f1375 on Windows 11 (MSVC 2022 19.44).

Verification

  • Built 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

  • Wide and narrow runtime load paths now share the same scoped load depth under the loader mutex (pe_loader.cpp:1483-1516), captured before import binding (1423-1428) so dependencies inherit dynamic DLL_PROCESS_ATTACH (1664-1665).
  • Setting tls_ga_slot and completing the /GA patch 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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7232078473 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2499#issuecomment-7232078473). Originally posted by [timbot103](https://github.com/timbot103) on 2026-11-28T02:01:40Z. --- Reviewed at head `1a49f1375` on Windows 11 (MSVC 2022 `19.44`). ### Verification - Built `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 - Wide and narrow runtime load paths now share the same scoped load depth under the loader mutex (`pe_loader.cpp:1483-1516`), captured before import binding (`1423-1428`) so dependencies inherit dynamic `DLL_PROCESS_ATTACH` (`1664-1665`). - Setting `tls_ga_slot` and completing the `/GA` patch 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.
MoonEyes commented 2026-11-28 17:51:50 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:51:50Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243345957 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2499#issuecomment-7243345957). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:51:50Z. --- 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.
OreoFR commented 2026-11-28 22:03:20 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T22:03:20Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head 1a49f1375; green run in a merge package of 11 PRs, no new ctest failure vs main
  • Test: ctest -R "^(loader|loader_tls_index0|loader_tls_index0_threads|loader_tls_index0_noreserve)$"
  • Red on main: loader 1 failure (DllMain of a runtime wide load gets a non-NULL lpvReserved); the 3 loader_tls_index0 tests pass
  • Green with the PR: 4/4 OK
  • Verdict: READY (the tls_ga_slot part of loader_tls_index0_test has no red)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246463795 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2499#issuecomment-7246463795). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T22:03:20Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `1a49f1375`; green run in a merge package of 11 PRs, no new ctest failure vs main - Test: `ctest -R "^(loader|loader_tls_index0|loader_tls_index0_threads|loader_tls_index0_noreserve)$"` - Red on main: loader 1 failure (DllMain of a runtime wide load gets a non-NULL lpvReserved); the 3 loader_tls_index0 tests pass - Green with the PR: 4/4 OK - Verdict: READY (the tls_ga_slot part of loader_tls_index0_test has no red) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2499
No description provided.