runtime: opt-in unhandled-exception log with module+RVA and stack (RT-CRASH-STACK) #994

Closed
github-bot wants to merge 2 commits from gh-pr/994/head into gh-pr/994/base
Owner

@⁠roanh47 on GitHub · 2026-11-24 16:37 UTC · original
pr/crash-stack → main · +1030 −15 · 12 files · 2 commits · closed without merging

Claimed in the coordination issue as: RT-CRASH-STACK

Claim: #14

Why

The startup trace already records module+0xRVA and a 16-frame stack for every exception, but only with --trace, and it records every first-chance exception too, so the one that ended the run can be buried among handled ones. Without --trace there is no module or RVA at all for the current post-CLZERO 0xC0000005.

This adds an opt-in log for exactly the exception that ended the process: it needs no --trace, writes its own file, and adds the faulting context (rip/rsp/rbp) plus the raw faulting address next to module+0xRVA. Termination and the exit code are untouched.

What

LEONIDA_CRASH_LOG (unset or 0: off):

  • LEONIDA_CRASH_LOG=<file> writes the block to that file and stderr.
  • LEONIDA_CRASH_LOG=1 (or on/true) uses the overlay's leonida-crash.log, so it never writes into the package.

runtime/shims/crash_log.h installs a SetUnhandledExceptionFilter handler, so only an exception nothing handled is logged (not the ones a shim's __try or a catch deals with). It returns the previous filter's answer (EXCEPTION_CONTINUE_SEARCH when there was none), so the process still ends with the same exception code; a filter installed before the log still runs.

The handler is lock-free by design, because a fault can happen with the shim registry's lookup lock or the Loader's range lock already held and the log must not turn a crash into a hang:

  • runtime/shims/module_ranges.h is a table of image base/end ranges, published by the Loader as it maps and by install() for the host modules seen then. Readers use a per-slot seqlock over atomics and fixed name buffers: no mutex, no shims::module_offset, no Loader call.
  • the block is built in a fixed stack buffer with hand-written hex/dec and written with WriteFile on handles opened at install, so it takes no CRT/stdio lock and allocates nothing;
  • a corrupt stack, a seqlock that cannot settle or a failing write drops the block instead of changing what the process does. A debugger still receives the exception first.

A sample from the mapped-image test:

[rt] crash: 2026-11-24T19:48:04.163Z unhandled exception 0xC0000005 at crash_image.bin+0x1220 (0x180001220), thread 29760
[rt] crash:   access violation reading address 0x0
[rt] crash:   rip 0x180001220 rsp 0x8576cffc48 rbp 0x0
[rt] crash:   stack:
[rt] crash:     #0 crash_image.bin+0x1220
[rt] crash:     #1 host:crash_log_test.exe+0xda30
[rt] crash:     #2 host:ucrtbase.dll+0x2cd30
[rt] crash:     #3 host:KERNEL32.DLL+0x2dda7
[rt] crash:     #4 host:ntdll.dll+0xc9c5c
[rt] crash: end of unhandled exception 0xC0000005

runtime/shims/stack_walk.h holds the frame walk startup_trace.h already had; the trace uses it too, so the two diagnostics agree on module+0xRVA for mapped images and host:name.dll+0xRVA for host modules.

Known limitation: host DLLs loaded after install() (the game's LoadLibrary, the Vulkan loader) have no entry and their frames print as raw addresses. Mapped images are always current because the Loader republishes its ranges on every map. A second concurrent Loader would replace the first one's table; gta6rt has one.

Docs: runtime/README.md, "Unhandled-exception log: LEONIDA_CRASH_LOG"; docs/boot.md points to it for the post-CLZERO 0xC0000005.

Test

runtime/test/crash_log_test.cpp (CTest crash_log; synthetic, no game files, no GPU):

  • the install rules for unset, 0, 1 and a path, plus close();
  • a child faults on a new thread of this image and the parent checks the code, address, thread id, top frames, the stderr copy, and that the exit code is still 0xC0000005;
  • a second child maps a minimal synthetic PE and faults at crash_image.bin+0x1220, the game's shape of address;
  • a child installs its own unhandled filter before the log; the log must be written and the old filter still run;
  • a child uses a directory as LEONIDA_CRASH_LOG, so the file cannot be opened: the block still reaches stderr and the exit code is unchanged;
  • a child faults while the shim registry's module-lookup lock is held (shims::with_module_lookup_lock_for_test, a new test seam). The old handler would deadlock in module_offset here; this one completes, logs the module+RVA and ends with 0xC0000005;
  • a child runs with the variable unset, still ends with 0xC0000005 and writes nothing.
1/5 Test #14: loader ...........................   Passed   18.89 sec
2/5 Test #23: unimplemented_policy .............   Passed    0.22 sec
3/5 Test #24: startup_trace ....................   Passed    0.14 sec
4/5 Test #25: crash_log ........................   Passed    0.36 sec
5/5 Test #58: int3_skip ........................   Passed    0.09 sec

100% tests passed, 0 tests failed out of 5

pe_loader_fuzz (the Loader now publishes ranges on each map) passed in 41.11 s. crash_log_test itself reports 46 checks, 0 failed.

python -I scripts/check_no_game_data.py <changed files>   OK
python -I tests/test_docs_env_vars.py                     4 tests OK

Built with MSVC 19.44 (VS 2022 BuildTools), RelWithDebInfo, Ninja. No Vulkan SDK in this build, so the Vulkan D3D12 device was left out; nothing here touches it. Tested on Windows 10.0.26300, Intel Core i7-10700F. No GPU or driver path is involved in this change.

AI-assisted (DeepSeek v4.1 Flash 1017).

**@⁠roanh47** on GitHub · 2026-11-24 16:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/994) `pr/crash-stack` → `main` · +1030 −15 · 12 files · 2 commits · closed without merging Claimed in the coordination issue as: RT-CRASH-STACK Claim: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 ## Why The startup trace already records `module+0xRVA` and a 16-frame stack for every exception, but only with `--trace`, and it records every first-chance exception too, so the one that ended the run can be buried among handled ones. Without `--trace` there is no module or RVA at all for the current post-CLZERO `0xC0000005`. This adds an opt-in log for exactly the exception that ended the process: it needs no `--trace`, writes its own file, and adds the faulting context (`rip/rsp/rbp`) plus the raw faulting address next to `module+0xRVA`. Termination and the exit code are untouched. ## What `LEONIDA_CRASH_LOG` (unset or `0`: off): * `LEONIDA_CRASH_LOG=<file>` writes the block to that file and stderr. * `LEONIDA_CRASH_LOG=1` (or `on`/`true`) uses the overlay's `leonida-crash.log`, so it never writes into the package. `runtime/shims/crash_log.h` installs a `SetUnhandledExceptionFilter` handler, so only an exception nothing handled is logged (not the ones a shim's `__try` or a `catch` deals with). It returns the previous filter's answer (`EXCEPTION_CONTINUE_SEARCH` when there was none), so the process still ends with the same exception code; a filter installed before the log still runs. The handler is lock-free by design, because a fault can happen with the shim registry's lookup lock or the Loader's range lock already held and the log must not turn a crash into a hang: * `runtime/shims/module_ranges.h` is a table of image base/end ranges, published by the Loader as it maps and by `install()` for the host modules seen then. Readers use a per-slot seqlock over atomics and fixed name buffers: no mutex, no `shims::module_offset`, no Loader call. * the block is built in a fixed stack buffer with hand-written hex/dec and written with `WriteFile` on handles opened at install, so it takes no CRT/stdio lock and allocates nothing; * a corrupt stack, a seqlock that cannot settle or a failing write drops the block instead of changing what the process does. A debugger still receives the exception first. A sample from the mapped-image test: ```text [rt] crash: 2026-11-24T19:48:04.163Z unhandled exception 0xC0000005 at crash_image.bin+0x1220 (0x180001220), thread 29760 [rt] crash: access violation reading address 0x0 [rt] crash: rip 0x180001220 rsp 0x8576cffc48 rbp 0x0 [rt] crash: stack: [rt] crash: #0 crash_image.bin+0x1220 [rt] crash: #1 host:crash_log_test.exe+0xda30 [rt] crash: #2 host:ucrtbase.dll+0x2cd30 [rt] crash: #3 host:KERNEL32.DLL+0x2dda7 [rt] crash: #4 host:ntdll.dll+0xc9c5c [rt] crash: end of unhandled exception 0xC0000005 ``` `runtime/shims/stack_walk.h` holds the frame walk `startup_trace.h` already had; the trace uses it too, so the two diagnostics agree on `module+0xRVA` for mapped images and `host:name.dll+0xRVA` for host modules. Known limitation: host DLLs loaded after `install()` (the game's `LoadLibrary`, the Vulkan loader) have no entry and their frames print as raw addresses. Mapped images are always current because the Loader republishes its ranges on every map. A second concurrent Loader would replace the first one's table; gta6rt has one. Docs: `runtime/README.md`, "Unhandled-exception log: `LEONIDA_CRASH_LOG`"; `docs/boot.md` points to it for the post-CLZERO `0xC0000005`. ## Test `runtime/test/crash_log_test.cpp` (CTest `crash_log`; synthetic, no game files, no GPU): * the install rules for unset, `0`, `1` and a path, plus `close()`; * a child faults on a new thread of this image and the parent checks the code, address, thread id, top frames, the stderr copy, and that the exit code is still `0xC0000005`; * a second child maps a minimal synthetic PE and faults at `crash_image.bin+0x1220`, the game's shape of address; * a child installs its own unhandled filter before the log; the log must be written and the old filter still run; * a child uses a directory as `LEONIDA_CRASH_LOG`, so the file cannot be opened: the block still reaches stderr and the exit code is unchanged; * a child faults while the shim registry's module-lookup lock is held (`shims::with_module_lookup_lock_for_test`, a new test seam). The old handler would deadlock in `module_offset` here; this one completes, logs the module+RVA and ends with `0xC0000005`; * a child runs with the variable unset, still ends with `0xC0000005` and writes nothing. ```text 1/5 Test #14: loader ........................... Passed 18.89 sec 2/5 Test #23: unimplemented_policy ............. Passed 0.22 sec 3/5 Test #24: startup_trace .................... Passed 0.14 sec 4/5 Test #25: crash_log ........................ Passed 0.36 sec 5/5 Test #58: int3_skip ........................ Passed 0.09 sec 100% tests passed, 0 tests failed out of 5 ``` `pe_loader_fuzz` (the Loader now publishes ranges on each map) passed in 41.11 s. `crash_log_test` itself reports 46 checks, 0 failed. ```text python -I scripts/check_no_game_data.py <changed files> OK python -I tests/test_docs_env_vars.py 4 tests OK ``` Built with MSVC 19.44 (VS 2022 BuildTools), RelWithDebInfo, Ninja. No Vulkan SDK in this build, so the Vulkan D3D12 device was left out; nothing here touches it. Tested on Windows 10.0.26300, Intel Core i7-10700F. No GPU or driver path is involved in this change. AI-assisted (DeepSeek v4.1 Flash 1017).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:32:25 +00:00
github-bot closed this pull request 2026-11-26 09:32:25 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 16:37 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-24 16:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/994#pullrequestreview-6043397183) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠roanh47 on GitHub · 2026-11-24 19:50 UTC · original

Follow-up to the review, pushed as 6fa4c872a:

  • The handler no longer calls shims::module_offset (or any Loader API).
    untime/shims/module_ranges.h publishes image base/end ranges from the Loader as it maps, and the host modules seen at install; the handler reads them with a per-slot seqlock over atomics and fixed name buffers. The block is built in a fixed stack buffer and written with WriteFile on handles opened at install, so it takes no registry/loader lock, no CRT/stdio lock and does not allocate.
  • New coverage: a previous unhandled filter installed before the log (it still runs), a directory used as the log path (block still reaches stderr, exit code unchanged), and a fault while the shim registry's lookup lock is held (--fault-locked; the old path would deadlock there). crash_log_test is now 46 checks, 0 failed; crash_log, startup_trace, loader, unimplemented_policy, int3_skip and pe_loader_fuzz all pass; check_no_game_data and est_docs_env_vars pass.
  • The PR rationale is corrected: startup_trace does record module+0xRVA and a 16-frame stack. The value here is unhandled-only, no --trace, a separate file, and the faulting context (rip/rsp/rbp) plus the raw address.
  • Limitation documented: host DLLs loaded after install print as raw addresses; mapped images are always current.
**@⁠roanh47** on GitHub · 2026-11-24 19:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/994#issuecomment-7142652977) Follow-up to the review, pushed as 6fa4c872a: - The handler no longer calls shims::module_offset (or any Loader API). untime/shims/module_ranges.h publishes image base/end ranges from the Loader as it maps, and the host modules seen at install; the handler reads them with a per-slot seqlock over atomics and fixed name buffers. The block is built in a fixed stack buffer and written with WriteFile on handles opened at install, so it takes no registry/loader lock, no CRT/stdio lock and does not allocate. - New coverage: a previous unhandled filter installed before the log (it still runs), a directory used as the log path (block still reaches stderr, exit code unchanged), and a fault while the shim registry's lookup lock is held (--fault-locked; the old path would deadlock there). crash_log_test is now 46 checks, 0 failed; crash_log, startup_trace, loader, unimplemented_policy, int3_skip and pe_loader_fuzz all pass; check_no_game_data and est_docs_env_vars pass. - The PR rationale is corrected: startup_trace does record module+0xRVA and a 16-frame stack. The value here is unhandled-only, no --trace, a separate file, and the faulting context (rip/rsp/rbp) plus the raw address. - Limitation documented: host DLLs loaded after install print as raw addresses; mapped images are always current.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.