| 1 | #![cfg(unix)] |
| 2 | |
| 3 | #[test] |
| 4 | fn crash_child() { |
| 5 | let Some(path) = std::env::var_os("SNOWBOUND_TEST_CRASH") else { |
| 6 | return; |
| 7 | }; |
| 8 | crash_report::hook("test", "native-test".into(), "test system".into(), |_| {}); |
| 9 | crash_report::set_path(path.into()).unwrap(); |
| 10 | #[cfg(target_os = "linux")] |
| 11 | { |
| 12 | extern "C" fn fatal(signal: i32, info: *mut libc::siginfo_t, _: *mut libc::c_void) { |
| 13 | unsafe { |
| 14 | crash_report::record_signal(signal, info); |
| 15 | libc::raise(signal); |
| 16 | } |
| 17 | } |
| 18 | let mut action: libc::sigaction = unsafe { std::mem::zeroed() }; |
| 19 | action.sa_sigaction = fatal as *const () as libc::sighandler_t; |
| 20 | action.sa_flags = libc::SA_SIGINFO | libc::SA_RESETHAND; |
| 21 | unsafe { |
| 22 | libc::sigaction(libc::SIGABRT, &action, std::ptr::null_mut()); |
| 23 | } |
| 24 | } |
| 25 | if std::env::var_os("SNOWBOUND_TEST_PANIC").is_some() { |
| 26 | crash_report::conceal("AI.one"); |
| 27 | let _ = std::panic::catch_unwind(|| panic!("Cannot read section AI")); |
| 28 | } |
| 29 | #[cfg(target_vendor = "apple")] |
| 30 | if std::env::var_os("SNOWBOUND_TEST_TRAP").is_some() { |
| 31 | unsafe { libc::raise(libc::SIGTRAP) }; |
| 32 | } |
| 33 | std::process::abort(); |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn fatal_signals_leave_a_report_and_preserve_the_first_panic() { |
| 38 | use std::{ |
| 39 | os::unix::process::ExitStatusExt, |
| 40 | process::{Command, Stdio}, |
| 41 | }; |
| 42 | let folder = tempfile::tempdir().unwrap(); |
| 43 | let trap = cfg!(target_vendor = "apple"); |
| 44 | for (panic, trap) in [(false, false), (true, false), (false, trap)] { |
| 45 | let report = folder.path().join(match (panic, trap) { |
| 46 | (true, _) => "panic.txt", |
| 47 | (_, true) => "trap.txt", |
| 48 | _ => "native.txt", |
| 49 | }); |
| 50 | let mut child = Command::new(std::env::current_exe().unwrap()); |
| 51 | child |
| 52 | .args(["--exact", "crash_child", "--nocapture"]) |
| 53 | .env("SNOWBOUND_TEST_CRASH", &report) |
| 54 | .stdout(Stdio::null()) |
| 55 | .stderr(Stdio::null()); |
| 56 | if panic { |
| 57 | child.env("SNOWBOUND_TEST_PANIC", "1"); |
| 58 | } |
| 59 | if trap { |
| 60 | child.env("SNOWBOUND_TEST_TRAP", "1"); |
| 61 | } |
| 62 | let signal = if trap { libc::SIGTRAP } else { libc::SIGABRT }; |
| 63 | assert_eq!(child.status().unwrap().signal(), Some(signal)); |
| 64 | let saved = std::fs::read_to_string(report).unwrap(); |
| 65 | assert!( |
| 66 | saved.starts_with("Snowbound test, native-test\nSystem: test system\n"), |
| 67 | "{saved}" |
| 68 | ); |
| 69 | if panic { |
| 70 | assert!( |
| 71 | saved.contains("Panic: Cannot read section <name>"), |
| 72 | "{saved}" |
| 73 | ); |
| 74 | assert!(!saved.contains("Native signal"), "{saved}"); |
| 75 | } else { |
| 76 | assert!( |
| 77 | saved.contains(&format!("Native signal: 0x{signal:016x}\n")), |
| 78 | "{saved}" |
| 79 | ); |
| 80 | assert!(!saved.contains("Fault address:"), "{saved}"); |
| 81 | } |
| 82 | } |
| 83 | } |