app: a fatal signal is logged before the process dies by it (#392) #478

Open
github-bot wants to merge 3 commits from github-bot/gh-474 into main AGit
Owner

@⁠AnastasisKon on GitHub · 2026-11-27 21:50 UTC · original
AnastasisKon:vibe/fatal-signal-4f9c68 → main · +204 −1 · 4 files · 3 commits · open

What this changes

Refs #395.

A run that ends in a segfault leaves runs/<date>/victory.log without a line naming the fault: nothing on the native host says anything when the game's own code faults. In #395 the run reached frame 1678 and died at 122 s with Segmentation fault: 11, after GPU errors, and the log had to be read backwards from the last unrelated line to guess what happened.

log_fatal_signals() installs a handler for SIGSEGV, SIGBUS, SIGILL and SIGFPE that writes one fixed line —

victory: fatal signal 11 (code 1) at address 0x0000000000000000

— the signal, its si_code and si_addr(), through async-signal-safe calls only: a 65-byte line built on the stack, write(2), no allocation, no tracing. It then re-raises the fault; SA_RESETHAND restored the default action, so the process still dies by the signal with the same exit status and the same crash report as before. Nothing about how a run ends changes — the log just says why.

Compiled on cfg(unix) only, and the call site in main is #[cfg(unix)], so Windows is untouched.

How it was tested

  • cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory: all pass — victory bin 14, victory-hle 327 plus native_probe 17, victory-loader 29, 0 failed.
  • cargo test -p victory on the arm64 host: the bin's 9 tests pass, the new one (fatal_signal_tests::a_fatal_signal_is_logged_before_the_process_dies_by_it) included.
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings: no warnings.
  • cargo xtask policy: 398 files ok.
  • Ran the game: not possible on this Mac, which has no copy of the title. What a run has to show: a crash the title causes leaves one victory: fatal signal … line in victory.log before the fault, and the run still ends with the status it ended with before.

The new test forks a child, has it install the logger and raise SIGSEGV, reads the line from its stderr, and asserts WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV — so it would catch a handler that swallows the signal or changes the exit status, not just one that prints nothing.

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md: none added — the line reports what the host's own kernel supplies
  • New diagnostics are behind a VICTORY_* variable and documented: not a diagnostic switch — it writes one line only when the process is already dying, allocates nothing and can be turned off by not crashing. Say the word if you would rather have it behind VICTORY_LOG_FATAL_SIGNALS=1.
**@⁠AnastasisKon** on GitHub · 2026-11-27 21:50 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/474) `AnastasisKon:vibe/fatal-signal-4f9c68` → `main` · +204 −1 · 4 files · 3 commits · open ## What this changes Refs #395. A run that ends in a segfault leaves `runs/<date>/victory.log` without a line naming the fault: nothing on the native host says anything when the game's own code faults. In #395 the run reached frame 1678 and died at 122 s with `Segmentation fault: 11`, after GPU errors, and the log had to be read backwards from the last unrelated line to guess what happened. `log_fatal_signals()` installs a handler for `SIGSEGV`, `SIGBUS`, `SIGILL` and `SIGFPE` that writes one fixed line — ``` victory: fatal signal 11 (code 1) at address 0x0000000000000000 ``` — the signal, its `si_code` and `si_addr()`, through async-signal-safe calls only: a 65-byte line built on the stack, `write(2)`, no allocation, no `tracing`. It then re-raises the fault; `SA_RESETHAND` restored the default action, so the process still dies by the signal with the same exit status and the same crash report as before. Nothing about how a run ends changes — the log just says why. Compiled on `cfg(unix)` only, and the call site in `main` is `#[cfg(unix)]`, so Windows is untouched. ## How it was tested - [x] `cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory`: all pass — victory bin 14, victory-hle 327 plus native_probe 17, victory-loader 29, 0 failed. - [x] `cargo test -p victory` on the arm64 host: the bin's 9 tests pass, the new one (`fatal_signal_tests::a_fatal_signal_is_logged_before_the_process_dies_by_it`) included. - [x] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings`: no warnings. - [x] `cargo xtask policy`: 398 files ok. - [ ] Ran the game: not possible on this Mac, which has no copy of the title. What a run has to show: a crash the title causes leaves one `victory: fatal signal …` line in `victory.log` before the fault, and the run still ends with the status it ended with before. The new test forks a child, has it install the logger and raise `SIGSEGV`, reads the line from its stderr, and asserts `WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV` — so it would catch a handler that swallows the signal or changes the exit status, not just one that prints nothing. ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md: none added — the line reports what the host's own kernel supplies - [x] New diagnostics are behind a `VICTORY_*` variable and documented: not a diagnostic switch — it writes one line only when the process is already dying, allocates nothing and can be turned off by not crashing. Say the word if you would rather have it behind `VICTORY_LOG_FATAL_SIGNALS=1`.
A run that ends in a segfault leaves the log without a line naming the
fault: nothing else says anything when native code faults (#392: a 122 s
run died at "Segmentation fault: 11" with no crash line, after GPU
errors). SIGSEGV, SIGBUS, SIGILL and SIGFPE now write one line to
stderr — the signal, its code and the faulting address — through
async-signal-safe calls only, then re-raise: SA_RESETHAND restored the
default action, so the exit code and the crash report stay as before.

A fork test checks the line, and that the child still dies by the signal
with the status a run without the logger leaves.

Refs #392
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 21:54:13 +00:00
Author
Owner

@⁠Olima34 on GitHub · 2026-11-28 00:41 UTC · original · requested changes

Thanks @⁠AnastasisKon, a line naming the fault is worth having, and keeping the handler async-signal-safe is right. We read the whole diff (e5bc7a3d) while helping the maintainer clear the queue, and there's one problem with how it interacts with the loader's own handler:

In a real run, SA_RESETHAND never applies, so the re-raise can loop instead of ending the process. log_fatal_signals() runs at the start of main. Later, when the title's first thread is set up, victory_loader::native::seh::install() (called from teb.rs) installs its own SIGSEGV/SIGBUS/SIGILL/SIGFPE handler and saves the previous action (this logger) in OLD for faults that aren't the title's. So the kernel delivers to the loader's on_signal, which calls the logger through chain(), not the kernel. The disposition is still the loader's handler. The logger's raise(sig) then stays pending (the signal is blocked inside the handler) and comes back to on_signal once it returns, and the faulting instruction runs again too. The SIG_DFL branch in chain() is what used to end the process there, and it no longer runs. As we read it, a crash would become a loop of "fatal signal" lines rather than a dead process (we haven't run it).

Suggested fix: in fatal, set the disposition back to SIG_DFL yourself before raise (sigaction is async-signal-safe), rather than relying on SA_RESETHAND. That covers both the direct and the chained path. The test would catch it if, after log_fatal_signals(), the child installs a second handler that chains to the saved one the way seh::signal::chain does (or calls seh::install() itself on x86_64), then faults. It should still see one line and WTERMSIG == SIGSEGV.

Smaller, optional: the docs don't mention the line. One sentence in docs/native-host.md (near the stop-on-signal paragraph) would help testers know to look for victory: fatal signal. CI is green on this head.

**@⁠Olima34** on GitHub · 2026-11-28 00:41 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/474#pullrequestreview-6094703293) · **requested changes** Thanks @⁠AnastasisKon, a line naming the fault is worth having, and keeping the handler async-signal-safe is right. We read the whole diff (e5bc7a3d) while helping the maintainer clear the queue, and there's one problem with how it interacts with the loader's own handler: **In a real run, `SA_RESETHAND` never applies, so the re-raise can loop instead of ending the process.** `log_fatal_signals()` runs at the start of `main`. Later, when the title's first thread is set up, `victory_loader::native::seh::install()` (called from `teb.rs`) installs its own `SIGSEGV`/`SIGBUS`/`SIGILL`/`SIGFPE` handler and saves the previous action (this logger) in `OLD` for faults that aren't the title's. So the kernel delivers to the loader's `on_signal`, which calls the logger through `chain()`, not the kernel. The disposition is still the loader's handler. The logger's `raise(sig)` then stays pending (the signal is blocked inside the handler) and comes back to `on_signal` once it returns, and the faulting instruction runs again too. The `SIG_DFL` branch in `chain()` is what used to end the process there, and it no longer runs. As we read it, a crash would become a loop of "fatal signal" lines rather than a dead process (we haven't run it). Suggested fix: in `fatal`, set the disposition back to `SIG_DFL` yourself before `raise` (`sigaction` is async-signal-safe), rather than relying on `SA_RESETHAND`. That covers both the direct and the chained path. The test would catch it if, after `log_fatal_signals()`, the child installs a second handler that chains to the saved one the way `seh::signal::chain` does (or calls `seh::install()` itself on x86_64), then faults. It should still see one line and `WTERMSIG == SIGSEGV`. Smaller, optional: the docs don't mention the line. One sentence in docs/native-host.md (near the stop-on-signal paragraph) would help testers know to look for `victory: fatal signal`. CI is green on this head.
Author
Owner

@⁠AnastasisKon on GitHub · 2026-11-28 12:31 UTC · original

Re-requesting review: conflicts with current main are resolved, CI is green on the latest head, and the earlier approval was noted as still standing. Ready to merge when you are.

**@⁠AnastasisKon** on GitHub · 2026-11-28 12:31 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/474#issuecomment-7239291289) Re-requesting review: conflicts with current main are resolved, CI is green on the latest head, and the earlier approval was noted as still standing. Ready to merge when you are.
When the loader's seh::install() has replaced the logger's handler and
seh::signal::chain calls it, SA_RESETHAND never applied (the kernel
entered the loader's handler), so the bare raise looped through the
chained handler instead of ending the process. sigaction is
async-signal-safe; set SIG_DFL explicitly, covering both the direct
and the chained path. New test a_chained_fatal_signal_is_logged_once_and_still_kills
installs a second handler that chains to the saved one the way chain
does and faults: exactly one 'victory: fatal signal' line, WTERMSIG ==
SIGSEGV (without the reset this test hangs in the loop). Also one
sentence in docs/native-host.md telling testers to look for the line.
Author
Owner

@⁠AnastasisKon on GitHub · 2026-11-28 15:30 UTC · original

Fixed as suggested, on top of a merge of current public main (26817285):

  • fatal now resets the disposition to SIG_DFL with sigaction (async-signal-safe) before raise, instead of relying on SA_RESETHAND — so the chained path (seh::install() replaced the handler, seh::signal::chain calls the logger from the loader's on_signal) also ends the process instead of looping.
  • New test a_chained_fatal_signal_is_logged_once_and_still_kills: after log_fatal_signals(), the child installs a second SA_SIGINFO handler that forwards to the saved previous action exactly like chain does, then faults. It asserts one victory: fatal signal 11 (code …) line and WTERMSIG == SIGSEGV. Verified it is not vacuous: with the sigaction reset removed, this test hangs in the loop (killed at 60 s); with it, it passes in 0.17 s.
  • One sentence in docs/native-host.md (stop-on-signal paragraph) telling testers to look for the victory: fatal signal line.

Gates on the new head e1a6d54d: cargo test -p victory-hle -p victory -p victory-gpu -p xtask (host) green incl. both fatal-signal tests; cargo test --target x86_64-apple-darwin -p victory green; cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings clean; cargo xtask policy 404 files ok.

**@⁠AnastasisKon** on GitHub · 2026-11-28 15:30 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/474#issuecomment-7241516052) Fixed as suggested, on top of a merge of current public `main` (26817285): - `fatal` now resets the disposition to `SIG_DFL` with `sigaction` (async-signal-safe) before `raise`, instead of relying on `SA_RESETHAND` — so the chained path (`seh::install()` replaced the handler, `seh::signal::chain` calls the logger from the loader's `on_signal`) also ends the process instead of looping. - New test `a_chained_fatal_signal_is_logged_once_and_still_kills`: after `log_fatal_signals()`, the child installs a second `SA_SIGINFO` handler that forwards to the saved previous action exactly like `chain` does, then faults. It asserts one `victory: fatal signal 11 (code …)` line and `WTERMSIG == SIGSEGV`. Verified it is not vacuous: with the `sigaction` reset removed, this test hangs in the loop (killed at 60 s); with it, it passes in 0.17 s. - One sentence in `docs/native-host.md` (stop-on-signal paragraph) telling testers to look for the `victory: fatal signal` line. Gates on the new head e1a6d54d: `cargo test -p victory-hle -p victory -p victory-gpu -p xtask` (host) green incl. both fatal-signal tests; `cargo test --target x86_64-apple-darwin -p victory` green; `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings` clean; `cargo xtask policy` 404 files ok.
Author
Owner

@⁠AnastasisKon on GitHub · 2026-11-28 18:16 UTC · original

The suggested fix is already the current head e1a6d54d: fatal now calls sigaction(sig, SIG_DFL) itself (async-signal-safe) before raise, instead of relying on SA_RESETHAND, so the chained path — seh::install() having replaced the disposition and seh::signal::chain calling the logger from the loader's on_signal — also ends the process rather than looping. The regression test a_chained_fatal_signal_is_logged_once_and_still_kills installs a second SA_SIGINFO handler that forwards to the saved previous action exactly like chain does, then faults: it asserts one log line and WTERMSIG == SIGSEGV (and hangs without the sigaction reset). The docs line is there too, at docs/native-host.md ("logs one line naming it first — victory: fatal signal <n> ... — and then dies by the signal as before"). The branch contains public main 26817285 with no conflicts, and CI is green on e1a6d54d. Could you re-read the head and dismiss the older review if it holds up?

**@⁠AnastasisKon** on GitHub · 2026-11-28 18:16 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/474#issuecomment-7243669513) The suggested fix is already the current head e1a6d54d: `fatal` now calls `sigaction(sig, SIG_DFL)` itself (async-signal-safe) before `raise`, instead of relying on `SA_RESETHAND`, so the chained path — `seh::install()` having replaced the disposition and `seh::signal::chain` calling the logger from the loader's `on_signal` — also ends the process rather than looping. The regression test `a_chained_fatal_signal_is_logged_once_and_still_kills` installs a second SA_SIGINFO handler that forwards to the saved previous action exactly like `chain` does, then faults: it asserts one log line and `WTERMSIG == SIGSEGV` (and hangs without the sigaction reset). The docs line is there too, at docs/native-host.md ("logs one line naming it first — `victory: fatal signal <n> ...` — and then dies by the signal as before"). The branch contains public `main` 26817285 with no conflicts, and CI is green on e1a6d54d. Could you re-read the head and dismiss the older review if it holds up?
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/478/head:github-bot/gh-474'
git switch 'github-bot/gh-474'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-474'
git switch 'github-bot/gh-474'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-474'
git switch 'github-bot/gh-474'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-474'
git switch 'main'
git merge --squash 'github-bot/gh-474'
git switch 'main'
git merge --ff-only 'github-bot/gh-474'
git switch 'main'
git merge 'github-bot/gh-474'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.