Skip to content

fix(runtime): avoid dynamic static initialization in library_loader for TLS-0 safety - #9023

Closed
Cryptoteep wants to merge 1 commit into
ProjectLeonida:mainfrom
Cryptoteep:fix/library-loader-tls0-host-module-name
Closed

Cryptoteep wants to merge 1 commit into
ProjectLeonida:mainfrom
Cryptoteep:fix/library-loader-tls0-host-module-name

Conversation

@Cryptoteep

Copy link
Copy Markdown
Contributor

Closes #8933
References #8452, #8364

Problem

In #8933, calling GetProcAddress on a host module from a guest thread triggered an unhandled exception:

Unhandled exception: page fault on read access to 0x0000000000000000 in 64-bit code (0x007ff6950bd7d2).
Backtrace:
=>0 0x007ff6950bd7d2 _Find_lower_bound(this=0000000000000000, _Keyval=000000D5F09C1C40) in gta6rt
  1 0x007ff6950bd7d2 _Find
  2 0x007ff6950bd7d2 find
  3 0x007ff6950bd7d2 rt::shims::library_loader::anonymous namespace::host_module_name+0x82
  4 0x007ff6950bcdbb rt::shims::library_loader::anonymous namespace::get_proc_address+0x5ab

Cause

In runtime/shims/library_loader.cpp, host_module_name() used a function-local dynamic static:

static auto* names = new std::map<HMODULE, std::string>;

MSVC compiles function-local dynamic statics with thread-safe guards (_Init_thread_header/_Init_thread_footer), checking $TSS0 > _Init_thread_epoch. When guest threads run with static TLS slot 0 mapped to guest data or with _Init_thread_epoch == 0 (#8452), MSVC's guard check skips initialization entirely. names remains nullptr, causing names->find(module) to invoke _Find_lower_bound on this == 0x0, crashing with a page fault. Similar function-local statics existed for loader_mutex(), pseudos(), and seen.

Fix

Replace the function-local dynamic statics with constant-initialized SRWLOCK (SRWLOCK_INIT) and lazily allocated pointers under lock:

  • g_loader_srw (SRWLOCK_INIT)
  • g_pseudos, g_host_module_names, g_unserved_seen allocated under LoaderLock

This eliminates all _Init_thread_header, _Init_thread_footer, and $TSS symbol guards from library_loader.cpp.

Verification

  • dumpbin /SYMBOLS runtime/build/CMakeFiles/xgameruntime_test.dir/shims/library_loader.cpp.obj: 0 hits for _Init_thread or $TSS.
  • runtime/build/xgameruntime_test.exe: OK (143 checks passed).
  • CTest runs in runtime/build:
    • module_names: Passed
    • run_module_names: Passed
    • dll_thread_calls: Passed
    • xgameruntime: Passed
    • run_tiny_gdk_load: Passed
  • python -I scripts/check_no_game_data.py runtime/shims/library_loader.cpp: clean.

…or TLS-0 safety

In library_loader.cpp, function-local dynamic statics (names, pseudos, seen)
rely on CRT thread-safe static initialization guards (_Init_thread_header and
_Init_thread_footer). When guest threads run with static TLS slot 0 mapped
to guest data or with _Init_thread_epoch == 0 (ItsNotRexed#8452), MSVC's guard check
skips pointer construction, leaving the pointers null. When host_module_name
called names->find(module), this triggered a null pointer dereference
(_Find_lower_bound crash at address 0, ItsNotRexed#8933).

Replace function-local dynamic statics with an SRWLOCK (SRWLOCK_INIT) and
lazily allocated pointers under lock. Verifiable with dumpbin: all
_Init_thread_header and $TSS symbols are eliminated from library_loader.cpp.obj.
@yellowcooln

Copy link
Copy Markdown

Independent verification of #9023 at ca69cee5cd7ace7917ff65d24aec8fdfdd8890b8, applied separately to main@867d922f09b86583859796a174df89555fb32da8.

Environment: Debian 13 x86-64, Wine 10.0 (Debian), clang-cl 23.1.6 + xwin, RelWithDebInfo; RTX 3060 / NVIDIA 580.196.07.

  • Negative control: the synthetic TLS-slot-0 regression crashes against unmodified main (Wine exit 5). Actual startup reproduces the null-pointer fault in host_module_name() against freshly built main.
  • For a loader-only regression I used the synthetic probe from runtime/shims: remove function-local static init guards in library_loader and d3d12_trace #8195 with tracing calls and its two tracing checks removed: 34 checks, 0 failures. This is an external scratch harness, not a test shipped by fix(runtime): avoid dynamic static initialization in library_loader for TLS-0 safety #9023.
  • Targeted CTest: 11/11 passed, no skips, with ctest -R '^(library_loader_tls_safe|module_names|dll_thread_calls|xgameruntime.*)$' --output-on-failure.
  • Actual startup A/B with the same input, Wine prefix, overlay and launch settings: the candidate gets past the original loader fault and reaches a separate title-side breakpoint exception (0x80000003, Wine exit 3).

This verifies the loader-crash correction on this Linux/Wine setup, not a rendered frame or playable game. No game files or private launch logs attached.

@JonaxScript

Copy link
Copy Markdown

Tested ca69cee5c (base main 867d922f0) with native MSVC (cl 19.44, VS 2022 Build Tools, Windows SDK 10.0.26100), Windows 11 Pro 26300: pass. The Wine check above used clang-cl, but the guard this PR removes (_Init_thread_header) is MSVC's thread-safe-statics code, so this run rebuilds it with cl.exe itself.

TLS slot 0, A/B. I took #8195's library_loader_tls_safe_test.cpp and removed its d3d12_trace calls (the include, lines 143–149, 191 and 271), as the Wine check did. I built it with the CMake target's sources (the test, shims/library_loader.cpp and shims/registry.cpp) and flags (/std:c++20 /O2 /MD /W4 /permissive- /utf-8 /EHsc, /OPT:NOREF), once against each library_loader.cpp. Both builds give 0 warnings.

library_loader.cpp 5 runs
main 867d922f0 5/5 crash, exit 0xC0000005 (access violation)
this PR 5/5 library_loader_tls_safe_test: OK (34 checks, 0 failures)

Full runtime, no regressions. The whole runtime builds cleanly from this head with scripts/build_runtime.ps1 (MSVC, Ninja, RelWithDebInfo). I ran ctest on an RX 7900 XTX (AMD 26.11.2) and compared it with a main 867d922f0 build on the same machine:

This PR and #8195 change the same functions in library_loader.cpp; #8195 also covers d3d12_trace.h.

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

@comtek21516

Copy link
Copy Markdown

Tested ca69cee5c on main at 867d922f0, in one tree with #9083, #8527, #7571 (four clean merges): pass.

For this change. loader, loader_tls, run_tiny_bundled_load and run_tiny_gdk_load, which go through library_loader.cpp, pass natively; loader and loader_tls pass under Wine as well (the Wine runner skips the two run_tiny_* ones, as on main). As with the other static-initialization changes, no test here calls the loader from a guest thread whose TLS slot 0 belongs to the title, so this shows the replacement behaves like the statics, not that the case it is for is fixed.

Setup. Built with MSVC 14.52 (Visual Studio 18), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.1, Ninja RelWithDebInfo on Windows Server 2025 (no Vulkan driver there), then the same binaries run through scripts/run_wine_tests.py under wine-11.0 (WineHQ stable, xvfb-run) on a Quadro RTX 4000 (Turing), NVIDIA 615.71.09, Ubuntu 22.04.5, where the layer tests report device: Quadro RTX 4000.

passed failed skipped
native Windows, this tree 147 4 124
Wine on the GPU, main 867d922f0 210 12 54
Wine on the GPU, this tree 210 12 54

Under Wine the tree is test by test the same as main. The four native failures are gameinput_host, d3d12_sparse_queue_family, vk_bindless_ps_tex and vk_bindless_ps_imgatomic, which fail the same way on main on that machine (#7452). The 12 under Wine are the same 12 as on main (host comparisons Wine cannot answer, and d3d12_xdxr_rt, d3d12_indirect_rt, d3d12_indirect_dispatch_count).

Because the four were built together, there is no per-PR negative control in this run: a regression would have shown as a difference from main, and there is none. No game files used. AI-assisted (Claude Code).

@ddaikodaiko

Copy link
Copy Markdown

Windows check of ca69cee: Windows 10 Pro 10.0.19045, MSVC 19.44.35229, Windows SDK 10.0.26100, CMake 3.31 + Ninja. No game files.

cmake --build runtime\build --target xgameruntime_test -j 10
ctest --test-dir runtime\build -R "module_names" --output-on-failure
1/2 Test #15: module_names .....................   Passed    0.33 sec
2/2 Test #16: run_module_names .................   Passed    1.72 sec
100% tests passed, 0 tests failed out of 2

The static-initialization change builds and the module-name paths still pass. I did not run the 143-check xgameruntime_test directly (only the two CTests above), and the TLS-0 ordering itself needs a case where a loader callback runs before static init, which this run does not force.

@sgonzalez-glitch

Copy link
Copy Markdown

Tested ca69cee5c on main 6312c057f (on its own: it conflicts with #9093 in runtime/shims/library_loader.cpp), on NVIDIA GeForce RTX 2080, driver 621.24 (32.0.16.2124), Windows 11, MSVC 19.44 RelWithDebInfo: pass.

AI-assisted: yes

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.

_Find_lower_bound crash

7 participants