Last active
March 15, 2023 03:55
-
-
Save ksqsf/b90877ae12c293c933800e3ead11a2e3 to your computer and use it in GitHub Desktop.
Catch SIGSEGV in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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