Skip to content

Trace: the once-per-key notes carry no _Init_thread_header guard either - #9158

Open
shyro29 wants to merge 2 commits into
ItsNotRexed:mainfrom
shyro29:fix/trace-notes-immortal-state
Open

shyro29 wants to merge 2 commits into
ItsNotRexed:mainfrom
shyro29:fix/trace-notes-immortal-state

Conversation

@shyro29

@shyro29 shyro29 commented Nov 23, 2026

Copy link
Copy Markdown

Item: follow-up to #9093 (same RT item, claim comment, continued in the thread). Depends on: #9093 — the branch stacks on it (the first commit is #9093's), so this PR's diff against #9093 is startup_trace.h + xmem.cpp only.

Why: with --trace on the same retail boot (Wine 11.22, Linux, gta6rt from main 867d922f0), the boot now passes the loader and reaches the title's CRT init, which calls an XMem shim — and crashes again in host code at xmem.cpp's note_attributes seen-set emplace (null guarded static, read of 0x8). Same _Init_thread_header ($TSS) class as #9093: a function-local dynamic static whose guard is skipped on a thread whose TLS array the loader replaced.

What this PR changes: a guard-free primitive in rt::startup_trace::detail —

template <typename T> inline T& immortal() {
    static std::atomic<T*> slot{nullptr};   // constant-initialised: no thread guard
    if (T* held = slot.load(std::memory_order_acquire)) return *held;
    T* fresh = new T;
    T* expected = nullptr;
    return *(slot.compare_exchange_strong(expected, fresh, ...) ? fresh : expected);
}

Every static involved is constant-initialised and a compare-exchange publishes the heap object once (a losing allocation leaks on purpose: immortal). Converted to it:

  • startup_trace.h: mutex() (was static auto* m = new std::mutex), skip_int3's static const bool skip = GetEnvironmentVariableW(...) and its once-per-address mutex+set;
  • xmem.cpp: the three once-per-attribute trace notes (note_attributes, note_physical_attributes, the XMemTransferMemory note) — each keeps its own mutex+set pair as locals of a function-local struct passed to immortal<T>(), so the trace output is byte-for-byte unchanged.

The std::atomic<bool> warned{false} statics in xmem.cpp are left alone: they are constant-initialised already, so they never had a guard.

Verified (Linux x86_64, Wine 11.22, retail boot, cross-compiled gta6rt): with this stack the two host page faults are gone; the boot proceeds past the loader into the title's CRT init and records the expected xmem_attributes events, and LEONIDA_SKIP_INT3=1 now steps the title's soft asserts (int3_skipped at +0x1e1d01b, +0x1e1d04a, +0x1e1d3a7). The run then stops inside the title's code (game_scarlett_master.exe+0x1f67809, a write through a null object right after a skipped assert) — that part is guest-side, not this PR's scope; details are in the coordination issue. No game files in the PR. AI-assisted (Claude Code).

The five function-local dynamic statics in library_loader.cpp (loader_mutex's mutex, pseudos' deque,
host_module_name's names map, get_proc_address's once-per-name mutex and set) were each guarded by an
MSVC _Init_thread_header ($TSS) check. The boot chain runs on threads whose TLS array the loader
replaced (runtime/src/pe_loader.cpp: "Windows replaces a thread's TLS array ... when some thread loads
a DLL with static TLS"), and on such a thread the guard can be skipped and left set, so the guarded
pointer stays null: a retail boot on Wine 11.22 page-faulted at the names lookup in host_module_name
(called from get_proc_address) with _Find_lower_bound(this=0) on a post-boot thread resolving a
dynamically loaded module's export.

The five pieces move into one namespace-scope heap block (Immortal* const = new Immortal), initialised
by the CRT's single-threaded _initterm before any boot thread and never destroyed, so the immortality
contract of the old statics (teardown calls into GetProcAddress/FreeLibrary on pseudo-handles) is kept
and no thread guard is ever involved. Same accessor names and behaviour; dll_thread_calls_test compiles
and runs this file on Windows.

AI-assisted (Claude Code).
startup_trace.h's mutex() and skip_int3's flag and once-per-address set, and xmem.cpp's three
once-per-attribute trace notes, were function-local dynamic statics: the same MSVC _Init_thread_header
($TSS) guard class the previous commit removes from library_loader.cpp. With --trace, a retail boot on
Wine 11.22 page-faulted in note_attributes' seen-set emplace (null guarded static, read of 0x8) when the
title's CRT init called an XMem shim from a thread whose TLS array the loader replaced; the host_frame
stack was startup_trace's note path.

They now share one rt::startup_trace::detail::immortal<T>(): every static involved is
constant-initialised (atomic slot = nullptr) and a compare-exchange publishes the heap object once, so a
losing allocation leaks on purpose and no thread guard is ever run. Callers keep their own
mutex+set pairs as immortal locals of a function-local struct, so behaviour and trace output are
unchanged.

Depends on: the library_loader commit above (same PR stack, one build). AI-assisted (Claude Code).

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Windows review (commit b05caa004a0fadb0c946480302d728305bc4de0e)

What changed:

  • runtime/shims/startup_trace.h & runtime/shims/xmem.cpp:
    • Replaces function-local dynamic initialization of once-per-key / once-per-address trace statics with lock-free atomic detail::immortal<T>().
    • Removes MSVC $TSS (_Init_thread_header) guards from trace paths, preventing page faults when static TLS arrays are modified by the PE loader during DLL mapping.

Verification:

  • Verified on Windows host: clean compilation with MSVC, zero TSS overhead or TLS contention.
  • Clean diff: no game files.

LGTM!

@smowlyy

smowlyy commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Verified head b05caa004 merged onto main (867d922f0) on native Windows 11 (10.0.26300.9974, MSVC 19.44, RelWithDebInfo):

  • python -I scripts/check_no_game_data.py on all 3 changed files: clean (exit 0)
  • cmake --build runtime/build --target startup_trace_test xmem_test --parallel: built cleanly
  • ctest --test-dir runtime/build -R '^(startup_trace|xmem)$' --output-on-failure -V: 2/2 passed (startup_trace, xmem — 173 checks)

AI-assisted.

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