Skip to content

runtime: remove TLS-0 static init guards and thread_local in d3d12_vulkan_device, d3d12_com, queue_holds and shader_library - #8364

Closed
whoalin1 wants to merge 1 commit into
ProjectLeonida:mainfrom
whoalin1:d3d12-tls0-static-guards
Closed

whoalin1 wants to merge 1 commit into
ProjectLeonida:mainfrom
whoalin1:d3d12-tls0-static-guards

Conversation

@whoalin1

Copy link
Copy Markdown
Contributor

Claim: #8186 (comment)

Why this blocks retail EXE execution

When the mapped retail executable owns static TLS index 0 (#4313), guest threads run with TEB->ThreadLocalStoragePointer[0] holding guest TLS data instead of the host CRT's _Init_thread_epoch. Under a zeroed guest TLS block (epoch == 0), MSVC's $TSS > _Init_thread_epoch (0 > 0) check skips initialization of function-local dynamic statics, while thread_local variables index directly into the guest's TLS slot-0 memory.

This PR removes the remaining TLS-slot-0 dependencies across the four core Vulkan D3D12 runtime files not covered by #7175, #7198, #7359, #7824, #7957, #8178, #8195, or svds12343's JsonReader LC_NUMERIC item:

  • runtime/src/queue_holds.h: replace static thread_local bool pumping in QueueHolds::in_pump() with a per-instance std::atomic<std::thread::id> pump_thread_{} and RAII PumpScope (#include <thread>), avoiding reads/writes to guest TLS slot 0 on every queue fence signal/pump.
  • runtime/src/shader_library.h: replace the function-local static const auto tables in zlib_inflate() (BTYPE=1 fixed Huffman) with inline constexpr detail::build_huffman + detail::kFixedHuffmanTables.
  • runtime/shims/d3d12_com.cpp: replace function-local static std::mutex / static std::set in rt::d3d12::stub() and note_once() with constant-initialized SRWLOCK (SRWLOCK_INIT) and lazily allocated sets.
  • runtime/shims/d3d12_vulkan_device.cpp: replace function-local dynamic statics in log_once(), report_subgroup_unavailable(), process_shader_cache_registry() (OpenShaderCacheSession / CreateShaderCacheSession), and create_vulkan_device_standalone() (load_vulkan_library()) with SRWLOCK (SRWLOCK_INIT) and acquire/release std::atomic.

Verification (synthetic only, no game files)

  • Added regression test: runtime/test/d3d12_tls_safe_test.cpp (CTest d3d12_tls_safe), which swaps TEB->ThreadLocalStoragePointer[0] to a PAGE_READONLY zero page (re-reading TEB on teardown if LoadLibrary expands the TLS pointer array) and a writable 0xA5 canary page, exercising fixed-Huffman zlib_inflate, concurrent rt::d3d12::stub, QueueHolds, create_vulkan_device_standalone, ID3D12Device9::CreateShaderCacheSession, CheckFeatureSupport (log_once), ExecuteCommandLists(1, nullptr) (note_once), and RSSetViewports(2, ...) (stub).
  • Baseline negative control (origin/main dde82f27e): d3d12_tls_safe_test.exe failed (rc=1, FAIL line 109: leonida::zlib_inflate(kFixedHuffmanStream, out) because $TSS1 > 0 skipped fixed-Huffman table construction).
  • MSVC dumpbin /SYMBOLS (RelWithDebInfo):
    • d3d12_com.cpp.obj: removed _Init_thread_header, _tls_index, $TSS0 (stub, note_once), and pumping -> 0 _Init_thread_header/_tls_index/$TSS hits remain.
    • d3d12_vulkan_device.cpp.obj: removed _Init_thread_header, _tls_index, $TSS0/$TSS1 (zlib_inflate, log_once, report_subgroup_unavailable, process_shader_cache_registry, create_vulkan_device_standalone), and pumping -> 0 _Init_thread_header/_tls_index/$TSS hits remain.
    • shader_library_test.cpp.obj: 3 hits (_Init_thread_header, $TSS1, _tls_index) -> 0 hits.
  • CPU & Dual-GPU runs (LEONIDA_VK_VALIDATION=1, VK_LOADER_LAYERS_DISABLE=~implicit~):
    • CPU: shader_library_test.exe -> rc=0.
    • NVIDIA GeForce RTX 4070 SUPER (*nv-vk*, 4070): d3d12_tls_safe_test.exe (rc=0), d3d12_com_test.exe (rc=0), d3d12_shader_cache_test.exe (rc=0), 0 validation errors.
    • AMD Ryzen 5 7600 RDNA2 128B iGPU (*amd-vulkan*, radeon): d3d12_tls_safe_test.exe (rc=0), d3d12_com_test.exe (rc=0), d3d12_shader_cache_test.exe (rc=0), 0 validation errors.
    • git diff --check origin/main: clean (rc=0).
    • tests/test_runtime_paths.py: 4/5 passed, 1 pre-existing main failure (tools/replace_retry.py: missing).

AI-assisted.

…kan D3D12 runtime

Co-authored-by: Gemini <noreply@google.com>
@lohant27-netizen

Copy link
Copy Markdown

Tested 9fe1e2e60 on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release, merged onto main 2a3b3b554 together with #8457, #8454, #8373, #8364 and #8340 (no conflicts between them): pass.

@RAINighty

Copy link
Copy Markdown

Tested 9fe1e2e60 merged with main 867d922f0 (clean merge) on Windows 11 (10.0.26300), Intel Core i7-14650HX, MSVC 19.44.35228, RelWithDebInfo (Ninja), CPU-only tests: pass.

  • With this PR: d3d12_tls_safe (new), d3d12_com, d3d12_compute and shader_library pass; shader_library_corpus is skipped (no corpus here). 4 of 5 passed, 1 skipped.
  • Negative control: this PR's d3d12_tls_safe_test.cpp with main's d3d12_com.cpp, d3d12_vulkan_device.cpp, queue_holds.h and shader_library.h: d3d12_tls_safe fails (the other four unchanged), so the new test does catch the problem.
  • Code not reviewed beyond running it. No GPU or game files used by these tests.

AI-assisted (Claude Code).

@MiguelMatMar

Copy link
Copy Markdown

Tested 9fe1e2e on NVIDIA GeForce RTX 4080 SUPER, driver 621.24 (Vulkan 1.4.357), Windows 11 Pro 26300, MSVC 14.44, Ninja RelWithDebInfo, applied on main 6dda063, no game data: pass.

  • ctest -R ^d3d12_tls_safe$: passed.
  • Negative control: only the test-side files applied (no source change): d3d12_tls_safe fails, so the test does catch the TLS-slot-0 dependency on main.

(AI-assisted check, Claude Code.)

dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…s and thread_local in d3d12_vulkan_device, d3d12_com, queue_holds and shader_library

Squashed from ItsNotRexed#8364 by whoalin1, head 9fe1e2e (security scan PASS).

Applied as the PR's own diff (gh pr diff); head matches the watcher's scan.
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.

5 participants