kona_cli/
sigsegv_handler.rs

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