Skip to content

Instantly share code, notes, and snippets.

@losfair
Created July 1, 2020 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save losfair/15aeb363095f1652c03b7edd76fb6378 to your computer and use it in GitHub Desktop.
Save losfair/15aeb363095f1652c03b7edd76fb6378 to your computer and use it in GitHub Desktop.
use backtrace::Backtrace;
use std::{mem, ptr};
#[inline(never)]
fn f(x: i32) -> i32 {
if x == 0 || x == 1 {
1
} else {
f(x - 1) + f(x - 2)
}
}
fn main() {
unsafe {
let mut handler: libc::sigaction = mem::zeroed();
handler.sa_flags = libc::SA_ONSTACK;
handler.sa_sigaction = trap_handler as usize;
libc::sigemptyset(&mut handler.sa_mask);
assert_eq!(libc::sigaction(libc::SIGSEGV, &handler, ptr::null_mut()), 0);
// Backtracing from a normal SIGSEGV works
//println!("Before invalid write");
//ptr::write_volatile(0 as *mut u32, 0);
//println!("After invalid write");
// Backtracing from a stack overflow crashes
println!("Before stack overflow");
println!("{}", f(0xfffffff));
println!("After stack overflow");
}
}
unsafe extern "C" fn trap_handler(
_: libc::c_int
) {
println!("Backtrace begin");
let backtrace = Backtrace::new_unresolved();
println!("Backtrace result: {:?}", backtrace);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment