192
192
//!
193
193
//! This crate was inspired by those projects!
194
194
195
+ #[ cfg( all( fuzzing, not( fuzzing_debug) ) ) ]
196
+ #[ macro_use] extern crate lazy_static;
195
197
196
198
#[ cfg( all( fuzzing, fuzzing_debug) ) ]
197
199
extern crate memmap;
@@ -234,6 +236,18 @@ pub fn fuzz<F>(closure: F) where F: Fn(&[u8]) {
234
236
std:: process:: exit ( 17 ) ;
235
237
}
236
238
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
+
237
251
#[ cfg( all( fuzzing, not( fuzzing_debug) ) ) ]
238
252
pub fn fuzz < F > ( closure : F ) where F : Fn ( & [ u8 ] ) + std:: panic:: RefUnwindSafe {
239
253
// get buffer from honggfuzz runtime
@@ -245,12 +259,8 @@ pub fn fuzz<F>(closure: F) where F: Fn(&[u8]) + std::panic::RefUnwindSafe {
245
259
buf = :: std:: slice:: from_raw_parts ( buf_ptr, len_ptr) ;
246
260
}
247
261
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 ) ;
254
264
255
265
// We still catch unwinding panics just in case the fuzzed code modifies
256
266
// the panic hook.
0 commit comments