Skip to content

runtime/src: remove TLS-slot-0 dependencies in shader_library and queue_holds - #8326

Open
GustavoEllwanger wants to merge 1 commit into
ItsNotRexed:mainfrom
GustavoEllwanger:fix/tls0-queue-holds-shader-library
Open

GustavoEllwanger wants to merge 1 commit into
ItsNotRexed:mainfrom
GustavoEllwanger:fix/tls0-queue-holds-shader-library

Conversation

@GustavoEllwanger

@GustavoEllwanger GustavoEllwanger commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: #8047 (comment)

What and why

When a mapped Xbox Series executable owns static TLS slot 0 (TEB->ThreadLocalStoragePointer[0], for /GA builds as in #4313 / #7524), host code reached on guest threads cannot rely on MSVC's thread-safe function-local static initialization (_Init_thread_header reads _Init_thread_epoch from slot 0) or host thread_local variables (which alias slot 0).

Two core headers in runtime/src/ reached on guest threads during D3D12/Vulkan work still depended on TLS slot 0:

  1. runtime/src/shader_library.h (zlib_inflate, fixed Huffman blocks)

    • Previously constructed the RFC 1951 fixed-Huffman tables on first use via static const auto tables = [] { ... }();.
    • With TEB->ThreadLocalStoragePointer[0] pointing at a zeroed guest TLS block, MSVC's _Init_thread_header guard skips initialization and leaves zeroed Huffman tables, causing zlib_inflate to fail (false, 0 bytes) on fixed-Huffman streams.
    • Fix: make detail::build_huffman constexpr and build detail::kFixedHuffmanTables at compile time (inline constexpr), eliminating the runtime init guard and any TLS access.
  2. runtime/src/queue_holds.h (QueueHolds::in_pump)

    • Previously tracked re-entrant pumping via static thread_local bool pumping = false;.
    • On a guest thread where TLS slot 0 holds non-zero guest data, in_pump() reads true and causes pump_ready() / remove_fence() to silently skip all held work; when slot 0 is zeroed, in_pump() = true/false writes into the guest's TLS slot 0.
    • Because pumping was global across all QueueHolds<Semaphore> instances, pumping one instance also suppressed pumping a second distinct instance on the same thread.
    • Fix: replace thread_local bool pumping with a per-instance std::atomic<std::thread::id> pump_owner_{} and an RAII PumpOwnerGuard (exception-safe, zero TLS access, independent per instance).
  3. runtime/test/runtime_src_tls_safe_test.cpp + runtime/CMakeLists.txt

    • Adds a portable C++20 regression test (runtime_src_tls_safe) covering fixed-Huffman zlib_inflate, same-instance re-entrancy (pump_ready and remove_fence), cross-instance pumping on the same thread, exception safety if a barrier callback throws, and multi-threaded pumping.
    • On Windows x64, runs first-use and concurrent checks with TEB->ThreadLocalStoragePointer[0] pointed at zeroed, 0x01-filled, and PAGE_READONLY scratch pages, verifying no TLS corruption or skipped work.

Tested

  • Environment: Windows x64, MSVC 19.50.35729 (Visual Studio 2026 BuildTools 18.5.0), Windows SDK 10.0.26100.0, CPU-only (no GPU / no Vulkan SDK / no game files).
  • Reproduced both failures on unmodified main (dde82f27e) before the fix:
    • zlib_inflate on the synthetic fixed-Huffman stream under a zeroed TLS slot 0 page: FAIL (out.size=0).
    • QueueHolds::pump_ready() under a 0x01-filled TLS slot 0 page: FAIL (releases=0).
  • After the fix:
    • runtime_src_tls_safe_test compiled with /std:c++20 /EHsc /W4 /permissive- /utf-8 in both /O2 and /Od (0 warnings): 18 checks, 0 failures.
    • shader_library_test compiled with /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2: SHA-256, inflate (stored/fixed/dynamic), JSON, FSSM extraction, FSPX programs and index lookup OK.
    • runtime/shims/d3d12_xs_device.cpp compiled cleanly (/c /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2, 0 warnings) against the updated shader_library.h.
    • python -I scripts/check_no_game_data.py on all 4 changed/added files: passed (exit 0).

AI-assisted: yes

Checklist

  • 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 C++ headers only)

@lohant27-netizen

Copy link
Copy Markdown

Tested 0f20d3cb8 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 23060e087 together with #8314, #8252, #8326 and #8270 (no conflicts): pass.

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.

3 participants