Skip to content

Commit

Permalink
Merge pull request #120 from srlabs/arg
Browse files Browse the repository at this point in the history
Implements file tests for afl fuzz target
  • Loading branch information
vanhauser-thc authored Jan 30, 2025
2 parents 684ef75 + 916ccda commit 03b4788
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use honggfuzz::fuzz as honggfuzz_fuzz;
// This is our inner harness handler function for the runner.
// We open the input file and feed the data to the harness closure.
#[doc(hidden)]
#[cfg(not(any(feature = "afl", feature = "honggfuzz", feature = "coverage")))]
#[cfg(not(feature = "coverage"))]
pub fn read_file_and_fuzz<F>(mut closure: F, file: String)
where
F: FnMut(&[u8]),
Expand Down Expand Up @@ -85,7 +85,6 @@ where
// We read input files and directories from the command line and run the inner harness `fuzz`.
#[doc(hidden)]
#[macro_export]
#[cfg(not(any(feature = "afl", feature = "honggfuzz")))]
macro_rules! read_args_and_fuzz {
( |$buf:ident| $body:block ) => {
use std::{env, fs};
Expand Down Expand Up @@ -134,8 +133,7 @@ macro_rules! read_args_and_fuzz {
/// # }
/// ```
#[macro_export]
#[cfg(not(any(feature = "afl", feature = "honggfuzz")))]
macro_rules! fuzz {
macro_rules! inner_fuzz {
(|$buf:ident| $body:block) => {
$crate::read_args_and_fuzz!(|$buf| $body);
};
Expand All @@ -157,11 +155,25 @@ macro_rules! fuzz {
};
}

/// We need this wrapper
#[macro_export]
#[cfg(feature = "afl")]
#[cfg(not(any(feature = "afl", feature = "honggfuzz")))]
macro_rules! fuzz {
( $($x:tt)* ) => {
$crate::afl_fuzz!($($x)*);
$crate::inner_fuzz!($($x)*);
}
}

#[macro_export]
#[cfg(feature = "afl")]
macro_rules! fuzz {
( $($x:tt)* ) => {
static USE_ARGS: std::sync::LazyLock<bool> = std::sync::LazyLock::new(|| std::env::args().len() > 1);
if *USE_ARGS {
$crate::inner_fuzz!($($x)*);
} else {
$crate::afl_fuzz!($($x)*);
}
};
}

Expand Down

0 comments on commit 03b4788

Please sign in to comment.