Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate fuzz/fuzz_nohook redundancy #161

Merged
merged 2 commits into from
Jan 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 14 additions & 51 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,63 +199,26 @@ where
/// ```
#[macro_export]
macro_rules! fuzz {
(|$buf:ident| $body:block) => {
afl::fuzz(true, |$buf| $body);
};
(|$buf:ident: &[u8]| $body:block) => {
afl::fuzz(true, |$buf| $body);
};
(|$buf:ident: $dty: ty| $body:block) => {
afl::fuzz(true, |$buf| {
let $buf: $dty = {
use arbitrary::{Arbitrary, RingBuffer};
if let Ok(d) = RingBuffer::new($buf, $buf.len()).and_then(|mut b|{
Arbitrary::arbitrary(&mut b).map_err(|_| "")
}) {
d
} else {
return
}
};

$body
});
};
( $($x:tt)* ) => { __fuzz!(true, $($x)*) }
}

/// Fuzz a closure-like block of code by passing it an object of arbitrary type. Panics that are
/// caught inside the fuzzed code are not turned into crashes.
///
/// You can choose the type of the argument using the syntax as in the example below.
/// Please check out the `arbitrary` crate to see which types are available.
///
/// For performance reasons, it is recommended that you use the native type `&[u8]` when possible.
///
/// ```rust,no_run
/// # #[macro_use] extern crate afl;
/// # fn main() {
/// fuzz!(|data: &[u8]| {
/// if data.len() != 6 {return}
/// if data[0] != b'q' {return}
/// if data[1] != b'w' {return}
/// if data[2] != b'e' {return}
/// if data[3] != b'r' {return}
/// if data[4] != b't' {return}
/// if data[5] != b'y' {return}
/// panic!("BOOM")
/// });
/// # }
/// ```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happened to this doc comment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. It looks like I cut a little too much.

I added a comment explaining the difference between fuzz! and fuzz_nohook! (using the preexisting language).

/// Like `fuzz!` above, but panics that are caught inside the fuzzed code are not turned into
/// crashes.
#[macro_export]
macro_rules! fuzz_nohook {
(|$buf:ident| $body:block) => {
afl::fuzz(false, |$buf| $body);
( $($x:tt)* ) => { __fuzz!(false, $($x)*) }
}

#[macro_export]
macro_rules! __fuzz {
($hook:expr, |$buf:ident| $body:block) => {
afl::fuzz($hook, |$buf| $body);
};
(|$buf:ident: &[u8]| $body:block) => {
afl::fuzz(false, |$buf| $body);
($hook:expr, |$buf:ident: &[u8]| $body:block) => {
afl::fuzz($hook, |$buf| $body);
};
(|$buf:ident: $dty: ty| $body:block) => {
afl::fuzz(false, |$buf| {
($hook:expr, |$buf:ident: $dty: ty| $body:block) => {
afl::fuzz($hook, |$buf| {
let $buf: $dty = {
use arbitrary::{Arbitrary, RingBuffer};
if let Ok(d) = RingBuffer::new($buf, $buf.len())
Expand Down