Skip to content

Instantly share code, notes, and snippets.

@Forsworns
Forked from ksqsf/sigsegv.rs
Created March 15, 2023 03:55
Show Gist options
  • Save Forsworns/65981625113c15477414731c16dea097 to your computer and use it in GitHub Desktop.
Save Forsworns/65981625113c15477414731c16dea097 to your computer and use it in GitHub Desktop.
Catch SIGSEGV in Rust
#![feature(libc)]
extern crate libc;
use libc::*;
use std::panic::*;
use std::ptr::*;
fn main() {
unsafe {
let f = handler as *const fn(c_int);
signal(SIGSEGV, f as size_t);
}
crash();
crash();
crash();
println!("still working");
}
fn crash() {
unsafe {
let _ = catch_unwind(|| {
let p: *mut i32 = null_mut();
*p = 1;
});
}
}
unsafe extern "C" fn handler(signum: c_int) {
println!("received signal {}", signum);
let mut sigs = std::mem::uninitialized::<sigset_t>();
sigemptyset(&mut sigs);
sigaddset(&mut sigs, signum);
sigprocmask(SIG_UNBLOCK, &sigs, std::ptr::null_mut());
panic!("SIGV!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment