Skip to content

runtime/shims: remove thread_local and function-local static guards in d3d12_xs, file_system and xgameruntime - #7957

Closed
DevilNine wants to merge 1 commit into
ProjectLeonida:mainfrom
DevilNine:fix/shims-remove-tls-static-guards
Closed

DevilNine wants to merge 1 commit into
ProjectLeonida:mainfrom
DevilNine:fix/shims-remove-tls-static-guards

Conversation

@DevilNine

Copy link
Copy Markdown

Summary

Follow-up in the RT-488 class (companion to #7074, #7175, #7198, #7359, and #7824, with zero file overlap) removing remaining static module TLS (thread_local -> _tls_index) and MSVC thread-safe function-local static initialization guards (_Init_thread_header -> _Init_thread_epoch in module TLS) across four untouched shims.

Claimed in #7804 (comment).

Changes

  • runtime/shims/d3d12_xs.cpp:
    • Replace all 6 function-local dynamic statics (d3d12.dll handle, 1.2 serializer support probe, and 4 host(...) exports) with namespace-scope INIT_ONCE + InitOnceExecuteOnce (passing object-pointer context structs; no function-pointer-to-void* cast).
  • runtime/shims/file_system.cpp:
    • Replace host_copy_file2()'s function-local static with INIT_ONCE and replace copy-up's std::mutex with SRWLOCK_INIT.
  • runtime/shims/xgameruntime.cpp & runtime/shims/xgameruntime_gamesave.cpp:
    • Replace thread_local bool t_time_sensitive and thread_local uint32_t t_read_count with Win32 TlsAlloc slots initialized via INIT_ONCE, preserving Win32 GetLastError() across TlsGetValue/TlsSetValue.
  • runtime/test/xgameruntime_test.cpp:
    • Add per-thread isolation + GetLastError() preservation regression checks for XThreadingImpl slots 27 and 30 (XThreadSetTimeSensitive / XThreadIsTimeSensitive).

Symbol verification (dumpbin /SYMBOLS, MSVC 19.44.35229.0 RelWithDebInfo on main dde82f2)

Object Before (main) After (this PR)
d3d12_xs.cpp.obj _Init_thread_header=1, _tls_index=1 0, 0
xgameruntime.cpp.obj _Init_thread_header=0, _tls_index=1 0, 0
xgameruntime_gamesave.cpp.obj _Init_thread_header=0, _tls_index=1 0, 0
file_system.cpp.obj local `` guard in host_copy_file2 + `startup_trace.h` 0 local `` guards in file_system.cpp (only inline `startup_trace::detail::mutex()` remains until #7824 lands)

Overlap check

Zero files in common with #7074 (library_loader.cpp), #7175 (registry.cpp), #7198 (winhttp_offline.cpp), #7359 (gameinput.cpp, dxgi_com.cpp, xmem.cpp), #7824 / #7723 (startup_trace.h, xgameruntime_offline.cpp), or active d3d12_xs_device.cpp PRs. No game files or game data are included.

Testing

  • Environment: Windows 11, Intel Core i5-12400F, NVIDIA GeForce RTX 3060 12 GB (driver 32.0.16.2124), MSVC 19.44.35229.0, Windows SDK 10.0.26100.0, CMake + Ninja RelWithDebInfo on main (dde82f2).
  • CTest (6/6 passed, 0 failed): device_proxy, device_proxy_trace, root_sig, file_system, xgameruntime, xgameruntime_gamesave.
  • Hygiene: python -I scripts/check_no_game_data.py exited 0 on all 5 changed files.

AI-assisted (Gemini / Antigravity).

@eizispark

Copy link
Copy Markdown

Tested b8c7363 merged onto main (867d922) on AMD Radeon RX 9070 XT, Adrenalin 26.11.1 (LLPC / Vulkan 1.4.359), Windows 11:

  • cmake --build runtime/build targeted (gta6rt, pe_loader_test, xgameruntime_test, xgameruntime_user_test, xgameruntime_gamesave_test, xgameruntime_store_test, xgameruntime_offline_test, file_system_test, xs_proxy_mock_test, d3d12_draw_test): clean build
  • ctest (xs_proxy_mock* 6, file_system 1, xgameruntime* 8, run_tiny_game* 10, pe_loader* 2 with LEONIDA_GAME_DIR set, d3d12_draw 1): 28/28 passed
  • gta6rt --dry-run against the retail package: exit code 0

@ersinhf

ersinhf commented Nov 23, 2026

Copy link
Copy Markdown

Tested 1280c45c (this head merged onto main 867d922f, clean merge) on Windows 11 build 26200, MSVC 14.44 (VS 2022 17.14 Build Tools), CMake 3.31.6 + Ninja, Vulkan SDK 1.4.363, RTX 4070 / NVIDIA 621.14: no regression.

This is the first check on the head (0 comments before it), so it is a new data point rather than a re-check.

AI-assisted: written with DeepSeek V4.1 Flash in an OpenCode session. Every number and log line above comes from a real run on the machine named in it.

@singul-oss

Copy link
Copy Markdown

AI-assisted review and test. Pass, no regressions.

Tested b8c73630 merged onto main 867d922f (clean merge) on Windows 11 Home 10.0.26200, NVIDIA GeForce RTX 5080 (driver 610.88), MSVC 14.44.35207, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, Visual Studio generator, RelWithDebInfo.

  • Full runtime build: succeeds, no new warnings in the changed shims.
  • ctest -C RelWithDebInfo: 272/275 on both main and this PR. The same three fail on both, so they're main's own on this GPU: d3d12_vulkan_device, d3d12_depth_resolve (d3d12_depth_resolve_test: run a refused resolve as clears, refused list, copy (red since #6610) #7985), d3d12_indirect_rt. xgameruntime (with the new per-thread and LastError checks), xgameruntime_gamesave, file_system, root_sig, device_proxy and device_proxy_trace all pass.
  • Symbols (dumpbin /SYMBOLS on gta6rt.dir objects, _Init_thread_header / _tls_index):
Object main this PR
d3d12_xs.obj 1 / 1 0 / 0
file_system.obj 1 / 1 1 / 1
xgameruntime.obj 0 / 1 0 / 0
xgameruntime_gamesave.obj 0 / 1 0 / 0

In file_system.obj the guard on main belongs to host_copy_file2 and to startup_trace::detail::mutex(). With this PR only the startup_trace one remains, matching the description (until #7824).

Code read: I agree with it. Saving and restoring the last error around TlsGetValue is needed, because it clears the last error on success. SRWLOCK_INIT is constant-initialized, so the function-local static gets no guard. The nested INIT_ONCEs (resolve_proc_once → host) use separate once-objects.

One non-blocking note: the two TlsAlloc slots are never TlsFreed. That only matters if the shim DLL can be unloaded and reloaded, which I don't think happens today.

@Maraco91

Copy link
Copy Markdown
Contributor

Tested b8c73630 merged onto main 867d922f (clean merge, in one tree with #8942's 473d7246, which touches only runtime/src/shader_library.h and its test) on NVIDIA GeForce RTX 3080 Ti, driver 616.56, Windows 11 26200, MSVC 19.42 (VS 2022 Build Tools) + Ninja via scripts\build_runtime.ps1, RelWithDebInfo: pass, and the symbol table of the description reproduces on a second compiler version.

dumpbin /SYMBOLS on the gta6rt objects, counting lines that name each symbol, main → this PR:

object _tls_index _Init_thread_header _Init_thread_epoch
d3d12_xs.cpp.obj 1 → 0 1 → 0 1 → 0
xgameruntime.cpp.obj 1 → 0 0 → 0 0 → 0
xgameruntime_gamesave.cpp.obj 1 → 0 0 → 0 0 → 0
file_system.cpp.obj 1 → 1 1 → 1 1 → 1

The last row is the one the description already explains (the inline startup_trace::detail::mutex() until #7824); I did not check that this is the only remaining source.

  • ctest -R on xgameruntime* (8), file_system, device_proxy*, xs_proxy_mock* (6), d3d12_xs_root_signature, pe_loader, loader*, run_tiny_game* (10) and shader_library: 31 of 31 pass.
  • Full ctest: 275 tests, 3 failed, the same three this card has on main (d3d12_vulkan_device, d3d12_depth_resolve, d3d12_indirect_rt).

One nit: runtime/shims/file_system.cpp:426 still says // under s_copy_mutex, and the lock is s_copy_lock now.

AI-assisted (Claude Code); no game files.

@dragoso

dragoso commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested b8c736307 merged onto main 867d922f0 (clean merge) on Windows 11 (build 26300), MSVC 14.51.36231 (VS 18 Build Tools), Ninja RelWithDebInfo, NVIDIA GeForce RTX 5060 Ti (driver 32.0.16.1742) + AMD Radeon iGPU: pass.

  • Built gta6rt and every test target that compiles one of the four changed shims: root_sig_test, device_proxy_test, file_system_test, pe_loader_test, test_pe_loader_bounds, and the five xgameruntime*_test.
  • ctest -R "root_sig|device_proxy|file_system|xgameruntime|pe_loader": 14/14 pass (pe_loader, pe_loader_bounds, device_proxy, device_proxy_trace, root_sig, file_system, xgameruntime, _user, _gamesave, _store, _offline, _offline_hdr, _user_gamertag, _user_gamertag_long). LEONIDA_GAME_DIR was unset.
  • The symbol claim holds. I counted the symbol names in each gta6rt object, local main build vs this PR:
Object main this PR
d3d12_xs.cpp.obj _Init_thread_header 1, _tls_index 1 0, 0
xgameruntime.cpp.obj 0, 1 0, 0
xgameruntime_gamesave.cpp.obj 0, 1 0, 0
file_system.cpp.obj 1, 1 1, 1 (the inline startup_trace::detail::mutex() the description names, until #7824)

Code read, no problems found:

  • d3d12_xs: each former local static has its own INIT_ONCE. resolve_proc_once calls host(), which runs a different INIT_ONCE (g_d3d12_mod_once), so the nesting can't deadlock. InitOnceExecuteOnce orders the slot's write before every reader. As before, a missing d3d12.dll isn't retried, and the 1.2 probe uses the first caller's fn.
  • file_system: static SRWLOCK s_copy_lock = SRWLOCK_INIT is constant-initialized, so it gets no guard. The RAII wrapper releases on an exception and isn't recursive, the same as the old std::mutex. copy_file2 returns an HRESULT, so the extra InitOnceExecuteOnce in host_copy_file2() can't leak a changed last-error value to the caller.
  • xgameruntime / gamesave: a TlsAlloc slot reads as 0 on any thread, including a mapped guest thread that never ran the shim's TLS setup, which matches the old defaults (false, 0). Saving and restoring GetLastError() is needed, since TlsGetValue clears it on success, and the new test checks it.

Notes, none blocking:

  1. The new isolation test runs on a host std::thread, not a mapped guest thread, which is the case this PR is about. run_tiny_game* (eizispark's run) covers that path only indirectly.
  2. If TlsAlloc ever returns TLS_OUT_OF_INDEXES, XThreadSetTimeSensitive and the read count are dropped silently. That only happens after 1,088 slots, so at most a one-time log line would help.
  3. The gamesave read-count change has no new test of its own. The existing xgameruntime_gamesave cases pass through it.

No game files. AI-assisted (Claude Code).

@myokex

myokex commented Nov 23, 2026

Copy link
Copy Markdown

Tested b8c736307 merged onto main 867d922f0 on Windows 11, AMD Radeon RX 7800 XT (driver 32.0.32029.2010), MSVC 2022 RelWithDebInfo, Vulkan SDK 1.4.363.0, synthetic only, no game files: pass.

Full ctest -j8: 260/265 pass, the same 5 that fail on main 867d922f0 alone fails the same 5 on this card (vk_queue_query_sync, d3d12_vulkan_device, d3d12_texture_tiles, d3d12_depth_resolve, d3d12_pipeline_library_host, known AMD host issues with open fixes); no new failure. All eight xgameruntime* tests pass, including the new per-thread / LastError check for slots 27 and 30.

AI-assisted (Claude Code); I read the full diff before building.

@SpaghettiMaster545

Copy link
Copy Markdown

Tested b8c736307 merged onto main 867d922f0 (clean merge) on Windows 11 (26100), MSVC 14.50 (VS 2026) RelWithDebInfo + Ninja, with an RTX 5070 Ti (621.24). Synthetic tests only, no game files: pass, no regression. Most of this matches the earlier comments, so here are only the parts that are new:

AI-assisted (Claude Code).

dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…function-local static guards in d3d12_xs, file_system and xgameruntime

Squashed from ItsNotRexed#7957 by DevilNine, head b8c7363 (security scan PASS).

Clean merge on top of ItsNotRexed#6582's XErrorReport and the Wave 0 static-guard PRs (ItsNotRexed#7175, ItsNotRexed#7198, ItsNotRexed#7359).
dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…ocal static guards)

The descriptor-range table and heap-allocator locks (ItsNotRexed#7021) become constant-initialized SRWLOCK wrappers with a lazily made, immortal range vector; LEONIDA_XMEM_RESOURCE_BACKING (ItsNotRexed#7411) is cached in an atomic; XboxBvhBuilder's registration (ItsNotRexed#7516) makes its table with an immortal new. A function-local static with a dynamic initializer takes the CRT's thread-safe-init guard, which must not run on the title's thread once it owns TLS index 0 (the Wave 0 crash class).
dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…namic statics in d3d12_xs, d3d12_xs_device and xgameruntime_offline

Squashed from ItsNotRexed#8178 by piyonchu, head aefa7be (security scan PASS).

Applied as the PR's own diff (gh pr diff). One pattern per file: d3d12_xs.cpp keeps ItsNotRexed#7957's global INIT_ONCE helpers (already on main), so this PR's local-static INIT_ONCE versions there are not taken; d3d12_xs_device.cpp and xgameruntime_offline.cpp take this PR's INIT_ONCE form. The device vtable's INIT_ONCE keeps ItsNotRexed#7078's slot 25/56 struct-return entries (this PR's version predates them), and ItsNotRexed#2490's list vtable gets the same INIT_ONCE form, since lists are made on title threads.
dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…kQueueIsEmpty are forwarded to the real task queue

Squashed from ItsNotRexed#8588 by tedin7, head e09f0a9 (security scan PASS).

Applied as the PR's own diff (gh pr diff); head matches the watcher's scan. xgameruntime_test keeps Wave 1's per-thread time-sensitive flag checks (ItsNotRexed#7957); the old slot-8 comment that preceded this PR's new slot-8 test is dropped.
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.

9 participants