Skip to content

Commit 201d80b

Browse files
Use panic::set_hook() to abort process
See rationnal here: rust-fuzz/honggfuzz-rs@abe2b4c closes #134
1 parent 02348dc commit 201d80b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ pub fn fuzz<F>(closure: F) where F: Fn(&[u8]) + std::panic::RefUnwindSafe {
9898
unsafe{std::ptr::read_volatile(&PERSIST_MARKER)}; // hack used in https://github.com/bluss/bencher for black_box()
9999
// unsafe { asm!("" : : "r"(&PERSIST_MARKER)) }; // hack used in nightly's back_box(), requires feature asm
100100

101+
// sets panic hook to abort
102+
std::panic::set_hook(Box::new(|_| {
103+
std::process::abort();
104+
}));
105+
101106
let mut input = vec![];
102107

103108
while unsafe{__afl_persistent_loop(1000)} != 0 {
@@ -107,11 +112,17 @@ pub fn fuzz<F>(closure: F) where F: Fn(&[u8]) + std::panic::RefUnwindSafe {
107112
return;
108113
}
109114

115+
// We still catch unwinding panics just in case the fuzzed code modifies
116+
// the panic hook.
117+
// If so, the fuzzer will be unable to tell different bugs appart and you will
118+
// only be able to find one bug at a time before fixing it to then find a new one.
110119
let did_panic = std::panic::catch_unwind(|| {
111120
closure(&input);
112121
}).is_err();
113122

114123
if did_panic {
124+
// hopefully the custom panic hook will be called before and abort the
125+
// process before the stack frames are unwinded.
115126
std::process::abort();
116127
}
117128
input.clear();

0 commit comments

Comments
 (0)