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

Closed
opened 2026-11-26 20:19:51 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/994
Author: @roanh47
Created: 11/24/2026
Status: ❌ Closed

Base: main ← Head: pr/crash-stack


📝 Commits (2)

  • a8b754a runtime: opt-in unhandled-exception log with code, module+RVA, thread and stack (RT-CRASH-STACK)
  • 6fa4c87 runtime: keep the crash log handler lock-free and cover held-lock faults (RT-CRASH-STACK)

📊 Changes

12 files changed (+1030 additions, -15 deletions)

View changed files

📝 docs/boot.md (+6 -0)
📝 runtime/CMakeLists.txt (+13 -0)
📝 runtime/README.md (+31 -0)
➕ runtime/shims/crash_log.h (+316 -0)
➕ runtime/shims/module_ranges.h (+153 -0)
📝 runtime/shims/registry.cpp (+5 -0)
📝 runtime/shims/shims.h (+5 -0)
➕ runtime/shims/stack_walk.h (+53 -0)
📝 runtime/shims/startup_trace.h (+5 -14)
📝 runtime/src/main.cpp (+4 -0)
📝 runtime/src/pe_loader.cpp (+8 -1)
➕ runtime/test/crash_log_test.cpp (+431 -0)

📄 Description

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

Claim: https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7137294269

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).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/994 **Author:** [@roanh47](https://github.com/roanh47) **Created:** 11/24/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `pr/crash-stack` --- ### 📝 Commits (2) - [`a8b754a`](https://github.com/ubiquitarianism/leonida/commit/a8b754acb7b60580fa0d12760a9b7f112fdda887) runtime: opt-in unhandled-exception log with code, module+RVA, thread and stack (RT-CRASH-STACK) - [`6fa4c87`](https://github.com/ubiquitarianism/leonida/commit/6fa4c872a8f479869d436fda06b782299de31827) runtime: keep the crash log handler lock-free and cover held-lock faults (RT-CRASH-STACK) ### 📊 Changes **12 files changed** (+1030 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `docs/boot.md` (+6 -0) 📝 `runtime/CMakeLists.txt` (+13 -0) 📝 `runtime/README.md` (+31 -0) ➕ `runtime/shims/crash_log.h` (+316 -0) ➕ `runtime/shims/module_ranges.h` (+153 -0) 📝 `runtime/shims/registry.cpp` (+5 -0) 📝 `runtime/shims/shims.h` (+5 -0) ➕ `runtime/shims/stack_walk.h` (+53 -0) 📝 `runtime/shims/startup_trace.h` (+5 -14) 📝 `runtime/src/main.cpp` (+4 -0) 📝 `runtime/src/pe_loader.cpp` (+8 -1) ➕ `runtime/test/crash_log_test.cpp` (+431 -0) </details> ### 📄 Description Claimed in the coordination issue as: RT-CRASH-STACK Claim: https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7137294269 ## 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). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:19:51 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1030
No description provided.