Skip to content

runtime: print the call chain of a fatal fault - #2276

Open
OldSalty wants to merge 3 commits into
ProjectLeonida:mainfrom
OldSalty:fault-stacks
Open

OldSalty wants to merge 3 commits into
ProjectLeonida:mainfrom
OldSalty:fault-stacks

Conversation

@OldSalty

@OldSalty OldSalty commented Nov 26, 2026 •

Copy link
Copy Markdown

A run that ends in an exception nothing handles saves the session pipeline cache but says nothing about where the fault was: the log has unhandled exception 0xc0000005 and no call chain, so a fault in the title or one of its DLLs stays a guess. This prints the fault, what the faulting context's registers held, and the faulting thread's call chain before the caches are written.

Item: none listed yet -- claimed in #1608 (see the comment linked below).

Three commits; the third answers the review's MAJOR (reconciliation in the comment below).

  • Fault line: the access and the address for an access violation ([rt] fault fatal: read of 0xf at <module>+0x...), or the exception code otherwise.
  • The faulting thread: its chain walked straight from the exception context. The filter runs on the faulting thread and that stack is live under it, so this needs no copy, unlike a sampled thread.
  • The registers: [rt] fault fatal regs: rip ... rsp ... rbp ... rax ..., plus for each register pointing at committed readable memory a 48-byte hexdump of what it points at ([rt] fault fatal mem rax 0x...:...). A faulting read followed a pointer; the pointers still in the registers say which one, and the bytes say what the title was reading there.
  • Naming without locks or heap (the MAJOR fix): a fatal report names an address only from a fixed table of ranges published at map time (base, size, name; revoked by the owner that unmaps them), so it takes neither ranges_mutex_ nor g_lookup_mutex, allocates nothing, and writes through _write(_fileno(stderr)) instead of stdio. A thread that faults in the DLL load path, where it already holds the loader's lock exclusively, therefore still reports instead of waiting on itself. Addresses in no published range print as bare hex.
  • The fatal path's own hazards: no mutex of its own (call_once already serialises the writers), no other-thread snapshot -- that allocates and suspends every thread; the report prints [rt] stacks fatal: other threads not sampled instead -- and the whole report runs under __try, so a fault of the report's own prints the report itself faulted rather than re-entering call_once.
  • The other threads: the LEONIDA_STACKS snapshot (snapshot_threads) still runs on the non-fatal paths (assert, device loss), where the registry and the filter's own mutex are safe to take and the title's worker pool is worth having in the same log.
  • Order: the report runs first in the filter, while the stacks are live, then the compile stop and the cache write as before; the exception still goes on to the previous filter, so the exit code is unchanged. Nothing happens unless a run dies.
  • Tests: thread_stacks_test gains a guarded null read with stderr redirected to a temp file: the fault line names the access and the instruction, frame #0 is that same instruction, the chain unwinds out of the function the fault happened in to the function that called it, a published range names the fault's place and becomes a bare number once its owner revokes it, the other-thread line carries a synchronised helper's tid and a known frame, and the non-fatal path still names through the registry and snapshots the other threads. 39 checks, no game data.
  • Tested: the call-chain commit on the retail package, where it named a faulting read and unwound six title frames out of the title's own job-worker pool -- a site two of three boots hit and no log had ever placed; the registers and lock-free commits by thread_stacks_test on Windows (MSVC, /W4, 0 warnings), retail run to follow.

@kawaii-shakal kawaii-shakal 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.

Reviewed by Claude Code (AI-assisted) on my behalf. Approving: this is the report I was missing when the title
faults.

  • The fault and the faulting thread are printed first, before the compile waits and the cache write, so they come
    out even if the cache write hangs.
  • The other threads are sampled the safe way: only system calls while a thread is suspended, and labels are
    allocated before sampling.
  • The faulting thread is unwound on its live stack inside the __try of unwind_frame. That is fine from the filter.

Non-blocking ideas, which I'm happy to do as a follow-up on top of this:

  1. Registers of the faulting context (rip, rsp and the GPRs). With module+RVA alone it is hard to tell which
    pointer was bad.
  2. The parameters of exceptions other than access violations, e.g. 0xE06D7363 (a C++ throw) carries the thrown
    type's info.
  3. A crash log file next to the saves or logs that keeps the report after the console window closes, plus a line
    with the CPU brand and the Windows build. Intel vs AMD decides which emulated Zen 2 instructions run.
  4. The filter now allocates (std::string, std::vector, module lookup) where before it only printed. If the fault
    came from inside the heap or while the loader's lookup mutex was held, that can hang instead of exiting. A nested
    fault inside call_once would block too, though that risk was already there. Not a reason to hold the PR, maybe
    one comment saying so.

Not built or run here: Windows-only, so this review is from reading the code.


Generated by Claude Code

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Verified on Windows 11 (10.0.26200.9974) with MSVC 19.44.35228 (x64, /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) against main d48a957bb + 4469914649 (claim #2570#issuecomment-7239117859):

  • Line-by-line diff audit: Clean (runtime/src/main.cpp, runtime/src/thread_stacks.cpp, runtime/src/thread_stacks.h, runtime/test/thread_stacks_test.cpp); adds noexcept rt::print_fault_stacks to log access-violation / exception details, unwind the live faulting CONTEXT directly on the crashing thread before save_caches_on_fatal_exception waits on pipeline compiles, and snapshot remaining threads. check_no_game_data.py clean.
  • Red (d48a957bb + PR test): thread_stacks_test.cpp fails to build (rc=2, error C2039: 'print_fault_stacks': is not a member of 'rt').
  • Green (d48a957bb + 4469914649 merge): Auto-merges cleanly (rc=0), compiles thread_stacks_test.exe with 0 warnings under /W4 /permissive- and passes (19 checks, 0 failures, rc=0), and compiles runtime/src/main.cpp cleanly (0 new warnings).

AI-assisted.

@TheGeogeo

Copy link
Copy Markdown
Contributor

Duplicate check (diff reading only, no build): not a duplicate. #2306 is stacked on this PR (Depends on: #2276, and its head contains this commit), so #2276 lands first and #2306 after its rebase (it is labeled needs rebase).

Heads-up: #2276 and #2306 both conflict in runtime/src/main.cpp with #2555 (git merge-tree). #2555 also reports a fatal exception's place, stack and registers. Worth deciding which fatal report wins before merging either side.

AI-assisted (Claude Code).

@timbot103 timbot103 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.

Static audit of 446991464 only. No builds, tests, or GPU runs.

  • [MAJOR] runtime/src/thread_stacks.cpp:235 — If a runtime DLL load takes an unhandled access violation while adding a range under ranges_mutex_ (runtime/src/pe_loader.cpp:1731), the filter calls module_offset on that same thread before unwinding. Loader::find_address then waits for shared ownership of the lock the faulting thread already holds exclusively (runtime/src/pe_loader.cpp:1587). This stalls before the fault details, cache save, and filter chaining. Capture raw fault data and frame addresses in fixed storage. Use prepublished immutable data for in-filter names and move heap-allocating thread snapshots outside the filter.
  • [MINOR] runtime/test/thread_stacks_test.cpp:246 — The prefix assertion passes for both 0 threads and thread list failed. Keep a synchronized helper alive during the fault and verify its thread ID and a known frame in the snapshot.

AI-assisted.

@enlight3d

Copy link
Copy Markdown
Contributor

RUN of #2276 (runtime: print the call chain of a fatal fault), local validation, nothing pushed.

Heads: PR head 4469914, main c22a0a7.
Merged: yes, git merge --no-ff of the PR head into main, no conflict (merge commit 73225cbad).

Commands (RelWithDebInfo, Ninja, LEONIDA_REQUIRE_VULKAN=ON, static SDL3):
cmake --build build --target thread_stacks_test gta6rt
ctest -R "^thread_stacks$" --output-on-failure -j1

Green (merged): thread_stacks passes, 22 checks, 0 failures (0.25 s). gta6rt builds. The test is a CPU test (it faults on purpose and reads its own stderr capture), not a GPU one. Other tests whose source the PR touches: none (thread_stacks_test is the only target that compiles thread_stacks.cpp besides gta6rt).

Red:

  • Build kind: with main.cpp, thread_stacks.cpp and thread_stacks.h taken back to main and the test kept, the test no longer compiles: error C2039, 'print_fault_stacks' is not a member of 'rt' (new API).
  • Behavioural kind: keeping the declaration and replacing the function body with an empty one, the test builds and fails: 18 checks, 2 failures ("the fault names the access and the instruction", "the capture holds the faulting thread's chain"). So the test does exercise the fault line and the faulting thread's chain.

Link: no new imports vs main (gta6rt.exe, 703 DLL!symbol entries from dumpbin /imports, identical lists).

Note for the maintainer: #2276 (and #2306, which contains it) conflicts with #2555 in runtime/src/main.cpp (checked with git merge-tree of the two heads: CONFLICT in runtime/src/main.cpp). Both add a crash report with registers and stack to the fatal-exception filter, so only one of the two can go in as is, and the maintainer must pick one.

The review of timbot103 on this head (the filter calls module_offset, which takes the loader's shared lock, on the faulting thread) was not tested here: this run only shows the new test and the build pass.

Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44.

A run that ended in an unhandled exception saved the pipeline caches
but said nothing about where the title faulted: the log had the
exception code and no call chain. print_fault_stacks names the access
and the address, walks the faulting thread's own chain from the
exception context (the filter runs on that thread, so its stack is
live), and snapshots the other threads as LEONIDA_STACKS does. On the
retail package this unwound a faulting chain of six title frames out
of the title's worker pool and named the read that faulted, which no
earlier log did. thread_stacks_test covers it with a guarded null read
captured to a file.
The faults that end a run follow pointers to garbage, and a faulting
address computed base+index says nothing about which pointer was
wrong: only the registers still hold the operands. Print the faulting
context's registers, then the bytes near each one that points at
committed readable memory (the fault's own address first, 48 bytes
each, ten lines at most), so the title's data next to the bad pointer
can be read out of the log.
print_fault_stacks asked the module registry for a name, which takes
g_lookup_mutex and then Loader::find_address's shared ownership of
ranges_mutex_: a thread that faults in the DLL load path, holding that
mutex exclusively, waits on itself and never reaches the fault line,
the cache save or the title's filter. It also allocated, and took a
snapshot of every other thread, in the one place where the heap may be
what broke.

A fatal report now names an address only from a fixed table published
as images are mapped, writes with a raw stderr write instead of stdio,
and leaves the other threads to print_thread_stacks_now. The whole
report runs under __try, so a fault of its own says so instead of
re-entering the filter's call_once.
@OldSalty

Copy link
Copy Markdown
Author

Agreed on the MAJOR — that's the thing I'd want fixed before this lands.

print_fault_stacks → shims::module_offset (runtime/shims/registry.cpp) takes g_lookup_mutex and calls the registered lookup (lookup_in_loader, runtime/src/pe_loader.cpp:1336, registered at :1409) → Loader::find_address, which wants shared ownership of ranges_mutex_ (:1632). A thread that faults in the DLL load path already holds it exclusively (:1776), so the filter waits on itself: no fault line, no cache write, no chaining to the title's filter. @kawaii-shakal's allocation point is the same root cause.

The branch is now rebased onto current main; the fix is the last of three commits (the first two are the reviewed call-chain and registers output, ported as-is), and it is built with MSVC and covered by thread_stacks_test.

Plan:

  • Fault data and the faulting thread's frame addresses stay in fixed storage (already the case).
  • Inside the filter, an address is named only from data published before the fault: image base and size (plus the ISA stub ranges) as a fixed published table, written at map time. Output is image+0xRVA or raw hex; no ranges_mutex_, no g_lookup_mutex, no heap, and output goes through a raw stderr write, not stdio.
  • The other-thread snapshot moves out of the fatal path — it allocates and suspends other threads. It stays on the assert and device-loss paths.
  • The fatal path drops its own mutex (call_once already serialises the writers) and runs the whole report under __try, so a fault of the report's own says so instead of re-entering call_once.
  • Host-module names stay for callers that aren't a fatal filter. If naming has to happen in the filter as well, that's a prepublished immutable table, as a separate change.

[MINOR] at runtime/test/thread_stacks_test.cpp: agreed — keep a synchronised helper alive across the snapshot and assert its tid and a known frame, not just the tag prefix, which the "0 threads" and "thread list failed" lines both satisfy.

On #2555: same insertion point, complementary output (RVA, registers, vtable, against a real unwind of the faulting thread and the other threads — and this branch now also prints the faulting context's registers and a hexdump of what they point at, which is idea 1 from the review). We don't contest the item name. Either rebase this onto #2555 and keep only the unwinding, or merge the two reports — your call. One dependency: #2306 is stacked on this branch, so it needs a base either way.

@fiestorra

Copy link
Copy Markdown
Contributor

Verified with a negative control, and the result is worth knowing: the tests pass with the header reverted, so they do not observe the change.

1. It applies and builds. merge-tree clean onto main d367675c7.

2. With the branch:

ctest -R '^thread_stacks'
100% tests passed, 0 tests failed out of the target's tests

3. The control - revert only the source change, keep the test, rebuild:

git checkout origin/main -- runtime/src/thread_stacks.h runtime/src/main.cpp
cmake --build … --target thread_stacks_test
ctest -R '^thread_stacks'
100% tests passed, 0 tests failed           <- still green

What that says: the per-thread stack capture is not observed by $(System.Collections.Hashtable.test). The test keeps passing when both the header and its wiring in main.cpp are put back to main's versions.

What I am not saying: that the change is wrong. Two readings are possible and I did not choose between them - the test may be a unit test of a header whose behaviour is unchanged in the cases it covers (in which case green either way is correct and the change is elsewhere), or it may be a test that was written beside the change rather than against it.

What would settle it in one case: a test that drives the path through the new code - e.g. for this one, the per-thread stack capture with an input that only the new code handles - and asserts on the result. If no such input exists, the change is additive and the control is expected to be green, which is also a useful thing to write in the pull request.

Why I am commenting: I verify open pull requests that replay onto current main, build the binaries their tests name, run them, then revert only the source change. A green control is a real result, and it is the one a reviewer cannot get from the diff.

Written with an AI assistant.

@KelboLive

Copy link
Copy Markdown

Independent check on Windows (not the author), at e480a5e7b.

  • Built thread_stacks_test and ran it: 39 checks, 0 failures — including the capture's file opens, a published range naming the fault's place, and the same address becoming a bare number once the owner revokes it.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711.

AI-assisted: yes (opencode).

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.

8 participants