Repository navigation
Keep native memory faults as fatal signals - #1828
Lorenzobattistela merged 4 commits into
Conversation
Lorenzobattistela
left a comment
There was a problem hiding this comment.
Thanks! Re-raising the original signal is the direction we want: the trap prints the memory-fault line, then the process dies by SIGSEGV/SIGBUS so a parent sees the real cause. But this version has a regression, and it breaks a test without updating it.
1. Keep the flush and err_claim(). Only _exit(1) should change. The try-lock flush is there because of #1655: when stdout is a pipe, output already printed is still buffered, and without the flush a fault loses it. ftrylockfile never waits, so the flush is already as safe as a signal handler allows. Dropping err_claim() also takes the trap out of the first-failure-wins handoff, so a fault racing an err_fail on another thread can print two lines again. The trap should be:
// A trap never waits on a lock: it flushes only if free, writes raw, then
// dies by its own signal so a parent sees the fault.
static void err_trap(int sig) {
static const char line[] = ...;
if (err_claim() && ftrylockfile(stdout) == 0) fflush(stdout);
if (write(2, line, sizeof line - 1)) {}
signal(sig, SIG_DFL);
raise(sig);
}
2. tests/io/stack_fault_trap.bend fails. It expects #|BEFORE, the fault line and #|exit 1. With this PR the C lane loses BEFORE (point 1) and exits 138 (SIGBUS on the macOS minis) instead of 1. The test gate requires every probe (check, interp, JS, C) to print the same #| lines. The interpreter and JS lanes turn the overflow into a RangeError and exit 1, so no single expected line fits every lane.
The fix we want is in shard_parse in gates/test.ts: on the C lane only, read a death by SIGBUS/SIGSEGV as exit 1:
} else if (head[0] === "exit" && last !== null && head[1] !== "0") {
const [got, kind] = last;
// The C runtime re-raises a memory fault after its line (SIGBUS, SIGSEGV):
// the other lanes end that fault with exit 1.
const code = kind === "c" && (head[1] === "138" || head[1] === "139")
? "1" : head[1];
const tail = code === "142" ? "timeout" : "exit " + code;
got[kind] = got[kind] === "" ? tail : got[kind] + "\n" + tail;
}
The test's #| lines then stay as they are. Its header still says "never a silent death by signal", so please update that wording to describe the re-raise.
3. Please drop the README lines. README is capped, and this runtime detail doesn't need a user-facing note.
Please run the test gate (at least stack_fault_trap, on macOS) after the change. This is a partial fix for #1624: the embedder fatal hook / termios restore is still open, so please use "Part of #1624" rather than "Fixes".
|
You're right — I dropped too much. Restored Ran the actual one-test shard on macOS arm64: check/interpreter/JS/C all pass with the existing expected lines, while C still dies by SIGBUS (138). Run and artifacts. The Linux parent sees SIGSEGV too. Local repo gate is 54/54; no cap changes. |
|
For the remaining hook, I would call one process-wide, async-signal-safe callback from the winning err_claim() path, before reporting. That lets a C effect restore termios for both ordinary failures and memory faults. Is that the shape you want? comp.ts is currently 65,994/66,000 with the pinned counter, so I am keeping the signal fix separate rather than going over cap or trimming unrelated code. |
Part of #1624.
Memory faults should reach the parent as SIGBUS/SIGSEGV, not exit 1. This keeps
err_claim()and the try-lock stdout flush, prints the diagnostic, then re-raises the original signal.stack_fault_trapkeeps its expected output. The test parser normalizes the C lane's signal status for that comparison; the actual process status is untouched. Removed the README note.The actual single-test shard passes on macOS arm64: check, interpreter, JS and C all agree, buffered
BEFOREsurvives, and the native process dies by SIGBUS (shell status 138). macOS run. The real Linux stack-fault probe preservesBEFOREtoo, with SIGSEGV visible to its parent. Local repo gate: 54/54, caps unchanged.The embedder fatal hook and termios restore are still open.