Skip to content

Commit 1d7df87

Browse files
Lazily set panic hook
1 parent abe2b4c commit 1d7df87

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ maintenance = { status = "actively-developed" }
2626

2727
[dependencies]
2828
memmap = "0.6"
29+
lazy_static = "1.0"
2930

3031
[dev-dependencies]
3132
rand = "0.4"

src/lib.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@
192192
//!
193193
//! This crate was inspired by those projects!
194194
195+
#[cfg(all(fuzzing, not(fuzzing_debug)))]
196+
#[macro_use] extern crate lazy_static;
195197

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

239+
// Registers a panic hook that aborts the process before unwinding.
240+
// It is useful to abort before unwinding so that the fuzzer will then be
241+
// able to analyse the process stack frames to tell different bugs appart.
242+
#[cfg(all(fuzzing, not(fuzzing_debug)))]
243+
lazy_static! {
244+
static ref PANIC_HOOK: () = {
245+
std::panic::set_hook(Box::new(|_| {
246+
std::process::abort();
247+
}))
248+
};
249+
}
250+
237251
#[cfg(all(fuzzing, not(fuzzing_debug)))]
238252
pub fn fuzz<F>(closure: F) where F: Fn(&[u8]) + std::panic::RefUnwindSafe {
239253
// get buffer from honggfuzz runtime
@@ -245,12 +259,8 @@ pub fn fuzz<F>(closure: F) where F: Fn(&[u8]) + std::panic::RefUnwindSafe {
245259
buf = ::std::slice::from_raw_parts(buf_ptr, len_ptr);
246260
}
247261

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

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

0 commit comments

Comments
 (0)