Skip to content

Commit

Permalink
Lazily set panic hook
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulGrandperrin committed Apr 23, 2018
1 parent abe2b4c commit 1d7df87
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ maintenance = { status = "actively-developed" }

[dependencies]
memmap = "0.6"
lazy_static = "1.0"

[dev-dependencies]
rand = "0.4"
22 changes: 16 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@
//!
//! This crate was inspired by those projects!

#[cfg(all(fuzzing, not(fuzzing_debug)))]
#[macro_use] extern crate lazy_static;

#[cfg(all(fuzzing, fuzzing_debug))]
extern crate memmap;
Expand Down Expand Up @@ -234,6 +236,18 @@ pub fn fuzz<F>(closure: F) where F: Fn(&[u8]) {
std::process::exit(17);
}

// Registers a panic hook that aborts the process before unwinding.
// It is useful to abort before unwinding so that the fuzzer will then be
// able to analyse the process stack frames to tell different bugs appart.
#[cfg(all(fuzzing, not(fuzzing_debug)))]
lazy_static! {
static ref PANIC_HOOK: () = {
std::panic::set_hook(Box::new(|_| {
std::process::abort();
}))
};
}

#[cfg(all(fuzzing, not(fuzzing_debug)))]
pub fn fuzz<F>(closure: F) where F: Fn(&[u8]) + std::panic::RefUnwindSafe {
// get buffer from honggfuzz runtime
Expand All @@ -245,12 +259,8 @@ pub fn fuzz<F>(closure: F) where F: Fn(&[u8]) + std::panic::RefUnwindSafe {
buf = ::std::slice::from_raw_parts(buf_ptr, len_ptr);
}

// Registers a panic hook that aborts the process before unwinding.
// It is useful to abort before unwinding so that the fuzzer will then be
// able to analyse the process stack frames to tell different bugs appart.
std::panic::set_hook(Box::new(|_| {
std::process::abort();
}));
// sets panic hook is not already done
lazy_static::initialize(&PANIC_HOOK);

// We still catch unwinding panics just in case the fuzzed code modifies
// the panic hook.
Expand Down

0 comments on commit 1d7df87

Please sign in to comment.