From 1d7df8705b155339065f36c56e68c3cc1bc8a7bb Mon Sep 17 00:00:00 2001 From: Paul Grandperrin Date: Mon, 23 Apr 2018 12:53:26 +0200 Subject: [PATCH] Lazily set panic hook --- Cargo.toml | 1 + src/lib.rs | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0a4dff7..b0c8bed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ maintenance = { status = "actively-developed" } [dependencies] memmap = "0.6" +lazy_static = "1.0" [dev-dependencies] rand = "0.4" diff --git a/src/lib.rs b/src/lib.rs index 98607ec..69911d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -234,6 +236,18 @@ pub fn fuzz(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(closure: F) where F: Fn(&[u8]) + std::panic::RefUnwindSafe { // get buffer from honggfuzz runtime @@ -245,12 +259,8 @@ pub fn fuzz(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.