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

Open
AnastasisKon wants to merge 3 commits from upstream-pr/474 into main
AnastasisKon commented 2026-11-27 21:50:39 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-27T21:50:39Z.


What this changes

Refs #392.

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 #392 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.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5533539148 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/474). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-27T21:50:39Z. --- ## What this changes Refs #392. 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 #392 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`.
Olima34 commented 2026-11-28 00:41:40 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-28T00:41:40Z.

Review: CHANGES_REQUESTED


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6094703293 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/474#pullrequestreview-6094703293). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-28T00:41:40Z. **Review:** CHANGES_REQUESTED --- 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.
AnastasisKon commented 2026-11-28 12:31:54 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T12:31:54Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7239291289 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/474#issuecomment-7239291289). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-28T12:31:54Z. --- 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 commented 2026-11-28 15:30:16 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T15:30:16Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7241516052 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/474#issuecomment-7241516052). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-28T15:30:16Z. --- 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 commented 2026-11-28 18:16:49 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T18:16:49Z.


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?

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7243669513 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/474#issuecomment-7243669513). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-28T18:16:49Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/474:upstream-pr/474'
git switch 'upstream-pr/474'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/474'
git switch 'upstream-pr/474'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/474'
git switch 'upstream-pr/474'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/474'
git switch 'main'
git merge --squash 'upstream-pr/474'
git switch 'main'
git merge --ff-only 'upstream-pr/474'
git switch 'main'
git merge 'upstream-pr/474'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
LakshanSivakumar/Native-Victory-macOS!474
No description provided.