diff --git a/crates/crash-report/src/native.rs b/crates/crash-report/src/native.rs index 335f1912628a69b9b2e640f68195ffe6e97c8786..2ca6781c7ae749635a4f90b81be8c03da79d75d2 100644 --- a/crates/crash-report/src/native.rs +++ b/crates/crash-report/src/native.rs @@ -14,7 +14,8 @@ pub(super) fn prepare(path: &Path) -> std::io::Result<()> { use std::os::unix::ffi::OsStrExt; let path = std::ffi::CString::new(path.as_os_str().as_bytes())?; let _ = PATH.set(path); - // Linux forwards its existing signal handler, preserving its symbolized log. + // Linux forwards its existing signal handler, preserving its symbolized log. A Swift + // runtime failure (a force unwrap of nil, an index out of range) traps as SIGTRAP. #[cfg(any(target_os = "macos", target_os = "ios"))] for signal in [ libc::SIGSEGV, @@ -22,6 +23,7 @@ pub(super) fn prepare(path: &Path) -> std::io::Result<()> { libc::SIGILL, libc::SIGFPE, libc::SIGABRT, + libc::SIGTRAP, ] { let mut action: libc::sigaction = unsafe { std::mem::zeroed() }; action.sa_sigaction = fatal as *const () as libc::sighandler_t; @@ -119,6 +121,8 @@ pub unsafe fn record_signal(signal: i32, info: *const libc::siginfo_t) { extern "C" fn fatal(signal: i32, info: *mut libc::siginfo_t, _: *mut libc::c_void) { unsafe { record_signal(signal, info); + // SA_RESETHAND leaves SIGILL and SIGTRAP caught, which would come straight back here. + libc::signal(signal, libc::SIG_DFL); libc::raise(signal); } } diff --git a/crates/crash-report/tests/native.rs b/crates/crash-report/tests/native.rs index d0d7c50b546759c82c67be9a4e6f9c1bbdba9765..7726bd31f8a0db237dc28216656ac116fce12fc4 100644 --- a/crates/crash-report/tests/native.rs +++ b/crates/crash-report/tests/native.rs @@ -26,6 +26,10 @@ fn crash_child() { crash_report::conceal("AI.one"); let _ = std::panic::catch_unwind(|| panic!("Cannot read section AI")); } + #[cfg(target_vendor = "apple")] + if std::env::var_os("SNOWBOUND_TEST_TRAP").is_some() { + unsafe { libc::raise(libc::SIGTRAP) }; + } std::process::abort(); } @@ -36,10 +40,13 @@ fn fatal_signals_leave_a_report_and_preserve_the_first_panic() { process::{Command, Stdio}, }; let folder = tempfile::tempdir().unwrap(); - for panic in [false, true] { - let report = folder - .path() - .join(if panic { "panic.txt" } else { "native.txt" }); + let trap = cfg!(target_vendor = "apple"); + for (panic, trap) in [(false, false), (true, false), (false, trap)] { + let report = folder.path().join(match (panic, trap) { + (true, _) => "panic.txt", + (_, true) => "trap.txt", + _ => "native.txt", + }); let mut child = Command::new(std::env::current_exe().unwrap()); child .args(["--exact", "crash_child", "--nocapture"]) @@ -49,7 +56,11 @@ fn fatal_signals_leave_a_report_and_preserve_the_first_panic() { if panic { child.env("SNOWBOUND_TEST_PANIC", "1"); } - assert_eq!(child.status().unwrap().signal(), Some(libc::SIGABRT)); + if trap { + child.env("SNOWBOUND_TEST_TRAP", "1"); + } + let signal = if trap { libc::SIGTRAP } else { libc::SIGABRT }; + assert_eq!(child.status().unwrap().signal(), Some(signal)); let saved = std::fs::read_to_string(report).unwrap(); assert!( saved.starts_with("Snowbound test, native-test\nSystem: test system\n"), @@ -62,7 +73,10 @@ fn fatal_signals_leave_a_report_and_preserve_the_first_panic() { ); assert!(!saved.contains("Native signal"), "{saved}"); } else { - assert!(saved.contains("Native signal:"), "{saved}"); + assert!( + saved.contains(&format!("Native signal: 0x{signal:016x}\n")), + "{saved}" + ); assert!(!saved.contains("Fault address:"), "{saved}"); } }