Catch SIGTRAP in the test runner

Fixes `panic` for Darwin.
This commit is contained in:
Feoramund
2024-06-28 19:05:20 -04:00
parent 929cc48703
commit 13539d3be1
2 changed files with 11 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ Stop_Reason :: enum {
Illegal_Instruction,
Arithmetic_Error,
Segmentation_Fault,
Unhandled_Trap,
}
test_assertion_failure_proc :: proc(prefix, message: string, loc: runtime.Source_Code_Location) -> ! {

View File

@@ -19,6 +19,11 @@ import "core:os"
@(private="file", thread_local)
local_test_index: libc.sig_atomic_t
// Windows does not appear to have a SIGTRAP, so this is defined here, instead
// of in the libc package, just so there's no confusion about it being
// available there.
SIGTRAP :: 5
@(private="file")
stop_runner_callback :: proc "c" (sig: libc.int) {
prev := intrinsics.atomic_add(&stop_runner_flag, 1)
@@ -110,6 +115,10 @@ _setup_signal_handler :: proc() {
// For tests:
// Catch asserts and panics.
libc.signal(libc.SIGILL, stop_test_callback)
when ODIN_OS == .Linux || ODIN_OS == .FreeBSD || ODIN_OS == .Haiku || ODIN_OS == .OpenBSD || ODIN_OS == .NetBSD || ODIN_OS == .Darwin {
// Catch panics on Darwin and unhandled calls to `debug_trap`.
libc.signal(SIGTRAP, stop_test_callback)
}
// Catch arithmetic errors.
libc.signal(libc.SIGFPE, stop_test_callback)
// Catch segmentation faults (illegal memory access).
@@ -141,6 +150,7 @@ _should_stop_test :: proc() -> (test_index: int, reason: Stop_Reason, ok: bool)
case libc.SIGFPE: reason = .Arithmetic_Error
case libc.SIGILL: reason = .Illegal_Instruction
case libc.SIGSEGV: reason = .Segmentation_Fault
case SIGTRAP: reason = .Unhandled_Trap
}
ok = true
}