loader(native): populate static TLS blocks in live threads for late-loaded DLLs #22

Manually merged
github merged 1 commit from gh-pr/22/head into gh-pr/22/base 2026-11-26 14:42:42 +00:00
Owner

@⁠claytonblythe on GitHub · 2026-11-26 09:25 UTC · original
claytonblythe:fix/late-dll-static-tls → main · +103 −28 · 2 files · 1 commits · merged 2026-11-26 09:36 UTC

Summary

Fixes an immediate 0xc0000005 null-pointer read ([0 + 4]) when a title DLL with static TLS (such as amd_fidelityfx_upscaler_xbox.dll on Xbox Series S) is loaded after title threads are already running:

  • teb::register_tls in crates/victory-loader/src/native/teb.rs now allocates and initializes a static TLS block for every already-live thread in LIVE and stores it atomically (Ordering::Release) into that thread's tls_array[index].
  • Uses a consistent lock order (TLS_MODULES -> LIVE) between register_tls and ThreadTeb::new so no thread can miss or double-allocate a module's TLS block.
  • Tracks late-allocated blocks per live thread and frees them cleanly in ThreadTeb::drop.
  • Updates apps/victory/src/native_run.rs log message from a warning to an info log when late static TLS is populated.

Testing

  • Unit test: cargo test --target x86_64-apple-darwin -p victory-loader --lib native::teb::tests — new late_register_tls_populates_already_live_threads test verifies that calling register_tls after a thread's TEB is already live writes _tls_index and populates gs:[0x58][index] with initialized data + zero-filled BSS.
  • Live game test (16 GB Apple M4, --console series-s, VICTORY_GUESS_GRAPHICS=1):
    • Before: crashed immediately with 0xc0000005 (info=[0, 4]) inside amd_fidelityfx_upscaler_xbox.dll at +0x1022 right after a DLL with static TLS loaded late.
    • After: amd_fidelityfx_upscaler_xbox.dll loads and initializes cleanly with zero exceptions, reaching frame 1344 (recorded=2721 encoded=2721, 0 dropped draws) across a 65 s headless run.
  • CI gates: cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings (0 warnings) and cargo xtask policy (363 files ok).
**@⁠claytonblythe** on GitHub · 2026-11-26 09:25 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/22) `claytonblythe:fix/late-dll-static-tls` → `main` · +103 −28 · 2 files · 1 commits · merged 2026-11-26 09:36 UTC ## Summary Fixes an immediate `0xc0000005` null-pointer read (`[0 + 4]`) when a title DLL with static TLS (such as `amd_fidelityfx_upscaler_xbox.dll` on Xbox Series S) is loaded after title threads are already running: - `teb::register_tls` in `crates/victory-loader/src/native/teb.rs` now allocates and initializes a static TLS block for every already-live thread in `LIVE` and stores it atomically (`Ordering::Release`) into that thread's `tls_array[index]`. - Uses a consistent lock order (`TLS_MODULES` -> `LIVE`) between `register_tls` and `ThreadTeb::new` so no thread can miss or double-allocate a module's TLS block. - Tracks late-allocated blocks per live thread and frees them cleanly in `ThreadTeb::drop`. - Updates `apps/victory/src/native_run.rs` log message from a warning to an info log when late static TLS is populated. ## Testing - **Unit test**: `cargo test --target x86_64-apple-darwin -p victory-loader --lib native::teb::tests` — new `late_register_tls_populates_already_live_threads` test verifies that calling `register_tls` after a thread's TEB is already live writes `_tls_index` and populates `gs:[0x58][index]` with initialized data + zero-filled BSS. - **Live game test (16 GB Apple M4, `--console series-s`, `VICTORY_GUESS_GRAPHICS=1`)**: - Before: crashed immediately with `0xc0000005` (`info=[0, 4]`) inside `amd_fidelityfx_upscaler_xbox.dll` at `+0x1022` right after `a DLL with static TLS loaded late`. - After: `amd_fidelityfx_upscaler_xbox.dll` loads and initializes cleanly with zero exceptions, reaching **frame 1344** (`recorded=2721 encoded=2721`, 0 dropped draws) across a 65 s headless run. - **CI gates**: `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings` (0 warnings) and `cargo xtask policy` (363 files ok).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:42:41 +00:00
github manually merged commit 530a7ad512 into gh-pr/22/base 2026-11-26 14:42:42 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 09:36 UTC · original · approved these changes

Tested merged with main (loader tests x3, Windows clippy). Good fix: Series S needs it now that run-native.sh defaults 16 GB Macs to it.

One non-blocking follow-up: the new test registers a TlsTemplate whose start and index_at point at the test's stack (init, tls_index), and it stays registered for the whole test process. Any thread a later test creates copies from that dead stack memory in ThreadTeb::new. Making both static (or leaking them) would close that off. A small follow-up PR is welcome.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 09:36 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/22#pullrequestreview-6071665236) · **approved these changes** Tested merged with main (loader tests x3, Windows clippy). Good fix: Series S needs it now that run-native.sh defaults 16 GB Macs to it. One non-blocking follow-up: the new test registers a TlsTemplate whose `start` and `index_at` point at the test's stack (`init`, `tls_index`), and it stays registered for the whole test process. Any thread a later test creates copies from that dead stack memory in ThreadTeb::new. Making both `static` (or leaking them) would close that off. A small follow-up PR is welcome.
This discussion has been locked. Commenting is limited to contributors.
No description provided.