Repository navigation
fix(runtime): avoid dynamic static initialization in library_loader for TLS-0 safety - #9023
Cryptoteep wants to merge 1 commit into
Conversation
…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.
|
Independent verification of #9023 at Environment: Debian 13 x86-64, Wine 10.0 (Debian), clang-cl 23.1.6 + xwin, RelWithDebInfo; RTX 3060 / NVIDIA 580.196.07.
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. |
|
Tested TLS slot 0, A/B. I took #8195's
Full runtime, no regressions. The whole runtime builds cleanly from this head with
This PR and #8195 change the same functions in No game files. AI-assisted (Claude Code). |
|
Tested For this change. 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
Under Wine the tree is test by test the same as 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 |
|
Windows check of The static-initialization change builds and the module-name paths still pass. I did not run the 143-check |
|
Tested
AI-assisted: yes |
Closes #8933
References #8452, #8364
Problem
In #8933, calling
GetProcAddresson a host module from a guest thread triggered an unhandled exception:Cause
In
runtime/shims/library_loader.cpp,host_module_name()used a function-local dynamic static: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.namesremainsnullptr, causingnames->find(module)to invoke_Find_lower_boundonthis == 0x0, crashing with a page fault. Similar function-local statics existed forloader_mutex(),pseudos(), andseen.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_seenallocated underLoaderLockThis eliminates all
_Init_thread_header,_Init_thread_footer, and$TSSsymbol guards fromlibrary_loader.cpp.Verification
dumpbin /SYMBOLS runtime/build/CMakeFiles/xgameruntime_test.dir/shims/library_loader.cpp.obj: 0 hits for_Init_threador$TSS.runtime/build/xgameruntime_test.exe: OK (143 checks passed).runtime/build:module_names: Passedrun_module_names: Passeddll_thread_calls: Passedxgameruntime: Passedrun_tiny_gdk_load: Passedpython -I scripts/check_no_game_data.py runtime/shims/library_loader.cpp: clean.