OLD-8364: runtime: remove TLS-0 static init guards and thread_local in core Vulkan D3D12 runtime #768

Closed
github-bot wants to merge 1 commit from gh-pr/768/head into gh-pr/768/base
Owner

@⁠whoalin1 on GitHub · 2026-11-24 13:35 UTC · original
whoalin1:d3d12-tls0-static-guards → main · +652 −78 · 13 files · 1 commits · closed without merging

Restores my open PR from the original repository (OLD-8364), rebased onto current main (2d73c242f, updated for ZlibError return from inflate_codes and kRev4 constexpr placement).

Claim: #14

Summary

When guest retail threads run with their own TLS block in gs:[0x58] slot 0, MSVC function-local magic statics (_Init_thread_header / _tls_index / $TSS) and thread_local variables crash on first touch inside D3D12 runtime calls.

This PR removes all MSVC static initialization guards and thread_local uses from the core Vulkan D3D12 runtime and adds a TLS-slot-0-poisoned regression test:

  • runtime/shims/d3d12_vulkan_device.cpp: replaces function-local magic statics and thread_local with TLS-0-safe INIT_ONCE / lock-free initialization.
  • runtime/shims/d3d12_com.cpp: removes function-local static std::mutex / static std::set guards in log_once / note_once.
  • runtime/src/queue_holds.h: eliminates thread_local / magic-static TLS-slot-0 dependencies.
  • runtime/src/shader_library.h: promotes deflate fixed Huffman tables (kFixedHuffmanTables) and kRev4 to inline constexpr at namespace scope so fixed-Huffman zlib inflation never emits a function-local static init guard.
  • runtime/test/d3d12_tls_safe_test.cpp & runtime/CMakeLists.txt: adds d3d12_tls_safe test that poisons TLS slot 0 before exercising device creation, shader cache, log_once, note_once, and unimplemented-stub logging across threads.

Verification (rebased on 2d73c242f, LEONIDA_VK_VALIDATION=1)

  • dumpbin /SYMBOLS on d3d12_vulkan_device.cpp.obj and d3d12_com.cpp.obj: 0 hits for _Init_thread_header, _tls_index, or $TSS
  • CPU: shader_library_test — pass (rc=0)
  • NVIDIA GeForce RTX 4070 SUPER (Windows 11, driver 610.88):
    • d3d12_tls_safe_test (rc=0, 0 validation errors)
    • d3d12_com_test (rc=0, 0 validation errors)
    • d3d12_shader_cache_test (rc=0, 0 validation errors)
  • AMD Radeon(TM) Graphics (Ryzen 5 7600 RDNA2 iGPU, Windows 11, driver 24.10.24, 128B push constants):
    • d3d12_tls_safe_test (rc=0, 0 validation errors)
    • d3d12_com_test (rc=0, 0 validation errors)
    • d3d12_shader_cache_test (rc=0, 0 validation errors)

AI-assisted (Gemini 4 Neon).

**@⁠whoalin1** on GitHub · 2026-11-24 13:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/768) `whoalin1:d3d12-tls0-static-guards` → `main` · +652 −78 · 13 files · 1 commits · closed without merging Restores my open PR from the original repository (OLD-8364), rebased onto current `main` (`2d73c242f`, updated for `ZlibError` return from `inflate_codes` and `kRev4` constexpr placement). Claim: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 ### Summary When guest retail threads run with their own TLS block in `gs:[0x58]` slot 0, MSVC function-local magic statics (`_Init_thread_header` / `_tls_index` / `$TSS`) and `thread_local` variables crash on first touch inside D3D12 runtime calls. This PR removes all MSVC static initialization guards and `thread_local` uses from the core Vulkan D3D12 runtime and adds a TLS-slot-0-poisoned regression test: - `runtime/shims/d3d12_vulkan_device.cpp`: replaces function-local magic statics and `thread_local` with TLS-0-safe `INIT_ONCE` / lock-free initialization. - `runtime/shims/d3d12_com.cpp`: removes function-local `static std::mutex` / `static std::set` guards in `log_once` / `note_once`. - `runtime/src/queue_holds.h`: eliminates `thread_local` / magic-static TLS-slot-0 dependencies. - `runtime/src/shader_library.h`: promotes deflate fixed Huffman tables (`kFixedHuffmanTables`) and `kRev4` to `inline constexpr` at namespace scope so fixed-Huffman zlib inflation never emits a function-local static init guard. - `runtime/test/d3d12_tls_safe_test.cpp` & `runtime/CMakeLists.txt`: adds `d3d12_tls_safe` test that poisons TLS slot 0 before exercising device creation, shader cache, `log_once`, `note_once`, and unimplemented-stub logging across threads. ### Verification (rebased on `2d73c242f`, `LEONIDA_VK_VALIDATION=1`) - **`dumpbin /SYMBOLS`** on `d3d12_vulkan_device.cpp.obj` and `d3d12_com.cpp.obj`: **0** hits for `_Init_thread_header`, `_tls_index`, or `$TSS` - **CPU**: `shader_library_test` — pass (`rc=0`) - **NVIDIA GeForce RTX 4070 SUPER (Windows 11, driver 610.88)**: - `d3d12_tls_safe_test` (`rc=0`, 0 validation errors) - `d3d12_com_test` (`rc=0`, 0 validation errors) - `d3d12_shader_cache_test` (`rc=0`, 0 validation errors) - **AMD Radeon(TM) Graphics (Ryzen 5 7600 RDNA2 iGPU, Windows 11, driver 24.10.24, 128B push constants)**: - `d3d12_tls_safe_test` (`rc=0`, 0 validation errors) - `d3d12_com_test` (`rc=0`, 0 validation errors) - `d3d12_shader_cache_test` (`rc=0`, 0 validation errors) AI-assisted (Gemini 4 Neon).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:20:22 +00:00
github-bot closed this pull request 2026-11-26 09:20:22 +00:00
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 16:37 UTC · original

Thanks for restoring this, @⁠whoalin1.

  • main now moves gta6rt.exe's own TLS off index 0 at start (rt::reserve_tls_index0, see runtime/README.md). The runtime's statics go through _tls_index and never read slot 0, so this case no longer comes up.
  • The new test poisons slot 0 in a state main doesn't create for the runtime.
  • Could you close this, or show a path on main where runtime code still reads slot 0?

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

**@⁠aslan2142** on GitHub · 2026-11-25 16:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/768#issuecomment-7168282737) Thanks for restoring this, @⁠whoalin1. - `main` now moves `gta6rt.exe`'s own TLS off index 0 at start (`rt::reserve_tls_index0`, see `runtime/README.md`). The runtime's statics go through `_tls_index` and never read slot 0, so this case no longer comes up. - The new test poisons slot 0 in a state `main` doesn't create for the runtime. - Could you close this, or show a path on `main` where runtime code still reads slot 0? AI-assisted (Claude Code, Claude Opus 5.5).
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-25 16:40 UTC · original

Confirmed on main — rt::reserve_tls_index0() in .CRT$XCT moves gta6rt.exe's _tls_index off slot 0 before any runtime static initializers run (and the fallback rewrites the guest EXE's /GA reads instead of touching slot 0), so runtime static guards and thread_locals no longer read slot 0. Closing as superseded by rt::reserve_tls_index0().

**@⁠whoalin1** on GitHub · 2026-11-25 16:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/768#issuecomment-7168369151) Confirmed on `main` — `rt::reserve_tls_index0()` in `.CRT$XCT` moves `gta6rt.exe`'s `_tls_index` off slot 0 before any runtime static initializers run (and the fallback rewrites the guest EXE's `/GA` reads instead of touching slot 0), so runtime static guards and `thread_local`s no longer read slot 0. Closing as superseded by `rt::reserve_tls_index0()`.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.