Skip to content

runtime/shims: remove function-local static init guards in library_loader and d3d12_trace - #8195

Open
auswhalen999-debug wants to merge 1 commit into
ItsNotRexed:mainfrom
auswhalen999-debug:pr/tls0-library-loader-trace
Open

auswhalen999-debug wants to merge 1 commit into
ItsNotRexed:mainfrom
auswhalen999-debug:pr/tls0-library-loader-trace

Conversation

@auswhalen999-debug

Copy link
Copy Markdown

Claimed in the coordination issue as: #8047 (comment) (RT-488 follow-up replacing closed unmerged #7074; strictly disjoint from open PRs #7175, #7198, #7359, #7824, and #7957)

What and why

When a mapped guest executable owns static TLS index 0, TEB->ThreadLocalStoragePointer[0] points to the guest's TLS block rather than the host executable's CRT slot holding _Init_thread_epoch. Under MSVC and clang-cl thread-safe static initialization, any dynamically initialized function-local static emits an _Init_thread_header guard that compares its guard variable (0 before first use) against _Init_thread_epoch read from slot 0; a zeroed guest TLS block causes the guard to skip initialization and dereference a null pointer (or touch an uninitialized mutex).

  • runtime/shims/library_loader.cpp: Replaced all five function-local dynamic statics (loader_mutex(), pseudos(), host_module_name()'s names, and get_proc_address()'s m and seen) with constant-initialized SRWLOCK (SRWLOCK_INIT) locks and file-scope null pointers lazily allocated under their respective locks (g_loader_lock for g_pseudos/g_host_names, separate g_seen_lock for g_seen_unserved). Preserves teardown immortality (DLL_PROCESS_DETACH / atexit) and eliminates all _Init_thread_header guards.
  • runtime/shims/d3d12_trace.h: Replaced record()'s function-local static std::mutex and sequence with constant-initialized inline SRWLOCK trace_lock = SRWLOCK_INIT; and inline unsigned long long sequence = 0; acquired via AcquireSRWLockExclusive / ReleaseSRWLockExclusive, keeping record() noexcept, teardown-immortal, and free of function-local static init guards across MSVC/CRT toolsets.
  • runtime/test/library_loader_tls_safe_test.cpp & runtime/CMakeLists.txt: Added synthetic CTest library_loader_tls_safe, which swaps TEB->ThreadLocalStoragePointer[0] on worker threads to a zeroed page before first use of library_loader and rt::d3d12_trace::record, then exercises single-threaded first-use and 4-thread concurrent calls across both.

Tested

  • Environment: Native Windows 11, clang-cl 23.1.6 + lld-link 23.1.6 + Windows SDK 10.0.26100 (RelWithDebInfo, /W4 /permissive- /utf-8 /EHsc; CPU-only synthetic tests, no game files).
  • Object-file guard-symbol check (llvm-nm):
    • Before (origin/main dde82f27e, library_loader.cpp.obj): 5 $TSS* guard variables (loader_mutex, pseudos, host_module_name, get_proc_address x2) + _Init_thread_header / _Init_thread_footer / _Init_thread_abort / _Init_thread_epoch.
    • After (library_loader.cpp.obj and library_loader_tls_safe_test.cpp.obj): 0 $TSS* / _Init_thread_* symbols.
  • Negative control (unmodified origin/main dde82f27e): library_loader_tls_safe_test.exe crashed deterministically with 0xC0000005 (STATUS_ACCESS_VIOLATION) on first call into library_loader.cpp under poisoned TLS slot 0.
  • After fix:
    • library_loader_tls_safe_test.exe: PASS (36 checks, 0 failures, exit 0).
    • ctest -R ^(library_loader_tls_safe|dll_thread_calls|loader|pe_loader|d3d12_gcl8|d3d12_trace)$: 6/6 PASS (loader, pe_loader, dll_thread_calls, library_loader_tls_safe, d3d12_gcl8, d3d12_trace).
    • Full gta6rt target built cleanly with 0 compiler warnings in library_loader.cpp, d3d12_trace.h, and library_loader_tls_safe_test.cpp.
    • python -I scripts/check_no_game_data.py clean, git diff --check clean, 0 CRLF.

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.

@morodirule

Copy link
Copy Markdown

Tested 32d1b90 on Arch Linux + clang-cl 23.1.5/xwin + Wine 11.22 (the exact setup where vanilla main crashes in host_module_name): new library_loader_tls_safe_test builds and passes 36/36, exit 0.

This is the correct fix for the crash I analyzed on #7804: function-local guarded statics misfire on the TLS-host thread (epoch reads 0), and namespace-scope SRWLOCK_INIT + lazy pointers under the lock remove the guard from the equation. Verified the mechanism, not just the symptoms.

Two follow-ups for the author (non-blocking): the same guarded-static pattern remains reachable on the title thread in d3d12_com.cpp (note/unserved-call mutexes+sets) and d3d12_vulkan_device.cpp:4285 (ShaderCacheRegistry pointer) — worth the same treatment in a follow-up so the next early-boot crash isn't the same bug one file over. No game files used. AI-assisted.

@nschlaepfer

Copy link
Copy Markdown
Contributor

Independent retail-entry A/B on main@dde82f27e994, with MSVC x64 and Wine 9.0 in an offline Ubuntu guest under QEMU on Apple Silicon. Both arms used the same title input, TLS companion, entry instrumentation and synthetic control. Only the runtime executable changed.

With the baseline, the first terminal host failure mapped to host_module_name, reached through get_proc_address. With this PR at 32d1b90, the run cleared that operation and reached the attribute bookkeeping in XMemVirtualAlloc before stopping. Symbol maps were checked against each tested executable’s code section. Both initializers passed, and neither run recorded a D3D12 call or title-owned frame.

Thanks to auswhalen999-debug for this patch, KewaiiGamer for #7074, and isfcrctfs for the earlier report of both sites in #7398. The next bookkeeping site is covered separately by tedin7’s #7359. AI-assisted verification with Claude Opus 5.5 and Codex.

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