solar_cli/
sigsegv_handler.rs

1//! Signal handler to extract a backtrace from stack overflow.
2//!
3//! Implementation modified from [`rustc`](https://github.com/rust-lang/rust/blob/3dee9775a8c94e701a08f7b2df2c444f353d8699/compiler/rustc_driver_impl/src/signal_handler.rs).
4
5use std::{
6    alloc::{Layout, alloc},
7    fmt, mem, ptr,
8};
9
10unsafe extern "C" {
11    fn backtrace_symbols_fd(buffer: *const *mut libc::c_void, size: libc::c_int, fd: libc::c_int);
12}
13
14fn backtrace_stderr(buffer: &[*mut libc::c_void]) {
15    let size = buffer.len().try_into().unwrap_or_default();
16    unsafe { backtrace_symbols_fd(buffer.as_ptr(), size, libc::STDERR_FILENO) };
17}
18
19/// Unbuffered, unsynchronized writer to stderr.
20///
21/// Only acceptable because everything will end soon anyways.
22struct RawStderr(());
23
24impl fmt::Write for RawStderr {
25    fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
26        let ret = unsafe { libc::write(libc::STDERR_FILENO, s.as_ptr().cast(), s.len()) };
27        if ret == -1 { Err(fmt::Error) } else { Ok(()) }
28    }
29}
30
31/// We don't really care how many bytes we actually get out. SIGSEGV comes for our head.
32/// Splash stderr with letters of our own blood to warn our friends about the monster.
33macro_rules! raw_errln {
34    ($($tokens:tt)*) => {
35        let _ = ::core::fmt::Write::write_fmt(&mut RawStderr(()), format_args!($($tokens)*));
36        let _ = ::core::fmt::Write::write_char(&mut RawStderr(()), '\n');
37    };
38}
39
40/// Signal handler installed for SIGSEGV
41extern "C" fn print_stack_trace(_: libc::c_int) {
42    const MAX_FRAMES: usize = 256;
43    let mut stack_trace = [ptr::null_mut(); MAX_FRAMES];
44    let stack = unsafe {
45        // Collect return addresses
46        let depth = libc::backtrace(stack_trace.as_mut_ptr(), MAX_FRAMES as i32);
47        if depth == 0 {
48            return;
49        }
50        &stack_trace.as_slice()[0..(depth as _)]
51    };
52
53    // Just a stack trace is cryptic. Explain what we're doing.
54    raw_errln!("error: solar interrupted by SIGSEGV, printing backtrace\n");
55    let mut written = 1;
56    let mut consumed = 0;
57    // Begin elaborating return addrs into symbols and writing them directly to stderr
58    // Most backtraces are stack overflow, most stack overflows are from recursion
59    // Check for cycles before writing 250 lines of the same ~5 symbols
60    let cycled = |(runner, walker)| runner == walker;
61    let mut cyclic = false;
62    if let Some(period) = stack.iter().skip(1).step_by(2).zip(stack).position(cycled) {
63        let period = period.saturating_add(1); // avoid "what if wrapped?" branches
64        let Some(offset) = stack.iter().skip(period).zip(stack).position(cycled) else {
65            // impossible.
66            return;
67        };
68
69        // Count matching trace slices, else we could miscount "biphasic cycles"
70        // with the same period + loop entry but a different inner loop
71        let next_cycle = stack[offset..].chunks_exact(period).skip(1);
72        let cycles = 1 + next_cycle
73            .zip(stack[offset..].chunks_exact(period))
74            .filter(|(next, prev)| next == prev)
75            .count();
76        backtrace_stderr(&stack[..offset]);
77        written += offset;
78        consumed += offset;
79        if cycles > 1 {
80            raw_errln!("\n### cycle encountered after {offset} frames with period {period}");
81            backtrace_stderr(&stack[consumed..consumed + period]);
82            raw_errln!("### recursed {cycles} times\n");
83            written += period + 4;
84            consumed += period * cycles;
85            cyclic = true;
86        };
87    }
88    let rem = &stack[consumed..];
89    backtrace_stderr(rem);
90    raw_errln!("");
91    written += rem.len() + 1;
92
93    let random_depth = || 8 * 16; // chosen by random diceroll (2d20)
94    if cyclic || stack.len() > random_depth() {
95        // technically speculation, but assert it with confidence anyway.
96        // We only arrived in this signal handler because bad things happened
97        // and this message is for explaining it's not the programmer's fault
98        raw_errln!("note: solar unexpectedly overflowed its stack! this is a bug");
99        written += 1;
100    }
101    if stack.len() == MAX_FRAMES {
102        raw_errln!("note: maximum backtrace depth reached, frames may have been lost");
103        written += 1;
104    }
105    raw_errln!("note: we would appreciate a report at https://github.com/paradigmxyz/solar");
106    written += 1;
107    if written > 24 {
108        // We probably just scrolled the earlier "we got SIGSEGV" message off the terminal
109        raw_errln!("note: backtrace dumped due to SIGSEGV! resuming signal");
110    }
111}
112
113/// Installs a SIGSEGV handler.
114///
115/// When SIGSEGV is delivered to the process, print a stack trace and then exit.
116pub fn install() {
117    unsafe {
118        let alt_stack_size: usize = min_sigstack_size() + 64 * 1024;
119        let mut alt_stack: libc::stack_t = mem::zeroed();
120        alt_stack.ss_sp = alloc(Layout::from_size_align(alt_stack_size, 1).unwrap()).cast();
121        alt_stack.ss_size = alt_stack_size;
122        libc::sigaltstack(&alt_stack, ptr::null_mut());
123
124        let mut sa: libc::sigaction = mem::zeroed();
125        sa.sa_sigaction = print_stack_trace as libc::sighandler_t;
126        sa.sa_flags = libc::SA_NODEFER | libc::SA_RESETHAND | libc::SA_ONSTACK;
127        libc::sigemptyset(&mut sa.sa_mask);
128        libc::sigaction(libc::SIGSEGV, &sa, ptr::null_mut());
129    }
130}
131
132/// Modern kernels on modern hardware can have dynamic signal stack sizes.
133#[cfg(any(target_os = "linux", target_os = "android"))]
134fn min_sigstack_size() -> usize {
135    const AT_MINSIGSTKSZ: core::ffi::c_ulong = 51;
136    let dynamic_sigstksz = unsafe { libc::getauxval(AT_MINSIGSTKSZ) };
137    // If getauxval couldn't find the entry, it returns 0,
138    // so take the higher of the "constant" and auxval.
139    // This transparently supports older kernels which don't provide AT_MINSIGSTKSZ
140    libc::MINSIGSTKSZ.max(dynamic_sigstksz as _)
141}
142
143/// Not all OS support hardware where this is needed.
144#[cfg(not(any(target_os = "linux", target_os = "android")))]
145fn min_sigstack_size() -> usize {
146    libc::MINSIGSTKSZ
147}