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

fix: patch libfuzzer to support jobs argument #176

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
48 changes: 48 additions & 0 deletions lib/bolero-libfuzzer/bolero-fixes.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
diff -ruN libfuzzer/FuzzerCommand.h b/libfuzzer/FuzzerCommand.h
index f653fe3..678573e 100644
--- libfuzzer/FuzzerCommand.h
+++ b/libfuzzer/FuzzerCommand.h
@@ -139,16 +139,35 @@ public:
// be the equivalent command line.
std::string toString() const {
std::stringstream SS;
- for (auto arg : getArguments())
- SS << arg << " ";
+ auto test_name = std::getenv("BOLERO_TEST_NAME");
+ auto libtest_harness = std::getenv("BOLERO_LIBTEST_HARNESS");
+ auto args = getArguments();
+
+ SS << "env";
+
+ if (libtest_harness) {
+ SS << " BOLERO_LIBTEST_HARNESS=1 BOLERO_TEST_NAME=\"" << test_name << "\"";
+ }
+
+ SS << " BOLERO_LIBFUZZER_ARGS=\"";
+ if (args.size() > 1)
+ SS << args[1];
+ for (unsigned i = 2; i < args.size(); ++i)
+ SS << " " << args[i];
+ SS << "\"";
+
+ SS << " " << args[0];
+
+ if (libtest_harness) {
+ SS << " " << test_name << " --exact --nocapture --quiet --test-threads 1";
+ }
+
if (hasOutputFile())
- SS << ">" << getOutputFile() << " ";
+ SS << " >" << getOutputFile();
if (isOutAndErrCombined())
- SS << "2>&1 ";
- std::string result = SS.str();
- if (!result.empty())
- result = result.substr(0, result.length() - 1);
- return result;
+ SS << " 2>&1";
+
+ return SS.str();
}

private:
1 change: 1 addition & 0 deletions lib/bolero-libfuzzer/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate cc;
fn main() {
println!("cargo:rerun-if-env-changed=BOLERO_FUZZER");
println!("cargo:rerun-if-env-changed=CARGO_CFG_FUZZING_LIBFUZZER");
println!("cargo:rerun-if-changed=libfuzzer/");

if std::env::var("CARGO_CFG_FUZZING_LIBFUZZER").is_ok() {
let mut build = cc::Build::new();
Expand Down
35 changes: 27 additions & 8 deletions lib/bolero-libfuzzer/libfuzzer/FuzzerCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,35 @@ class Command final {
// be the equivalent command line.
std::string toString() const {
std::stringstream SS;
for (auto arg : getArguments())
SS << arg << " ";
auto test_name = std::getenv("BOLERO_TEST_NAME");
auto libtest_harness = std::getenv("BOLERO_LIBTEST_HARNESS");
auto args = getArguments();

SS << "env";

if (libtest_harness) {
SS << " BOLERO_LIBTEST_HARNESS=1 BOLERO_TEST_NAME=\"" << test_name << "\"";
}

SS << " BOLERO_LIBFUZZER_ARGS=\"";
if (args.size() > 1)
SS << args[1];
for (unsigned i = 2; i < args.size(); ++i)
SS << " " << args[i];
SS << "\"";

SS << " " << args[0];

if (libtest_harness) {
SS << " " << test_name << " --exact --nocapture --quiet --test-threads 1";
}

if (hasOutputFile())
SS << ">" << getOutputFile() << " ";
SS << " >" << getOutputFile();
if (isOutAndErrCombined())
SS << "2>&1 ";
std::string result = SS.str();
if (!result.empty())
result = result.substr(0, result.length() - 1);
return result;
SS << " 2>&1";

return SS.str();
}

private:
Expand Down
15 changes: 13 additions & 2 deletions lib/bolero-libfuzzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,23 @@ pub mod fuzzer {
));
}

if std::env::var("__BOLERO_LIBFUZZER_RECURSE").is_ok() {
// Libfuzzer can generate multiple jobs that can make the binary recurse.
// Still, let’s limit recursion depth to some reasonable amount.
let recursion_level = std::env::var("__BOLERO_LIBFUZZER_RECURSE")
.as_deref()
.unwrap_or("0")
.parse()
.unwrap_or(usize::MAX);

if recursion_level > 10 {
eprintln!("LOOPING BINARY");
std::process::exit(1);
}

std::env::set_var("__BOLERO_LIBFUZZER_RECURSE", "1");
std::env::set_var(
"__BOLERO_LIBFUZZER_RECURSE",
(recursion_level + 1).to_string(),
);

// create a vector of NULL terminated strings
let args = std::env::args()
Expand Down
1 change: 1 addition & 0 deletions lib/bolero-libfuzzer/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ git clone --depth 1 --single-branch --branch $version https://github.com/llvm/ll
rm -rf "$project_dir/libfuzzer/"
mv "$tmp_dir/compiler-rt/lib/fuzzer/" "$project_dir/libfuzzer"
mv "$tmp_dir/compiler-rt/LICENSE.TXT" "$project_dir/libfuzzer/LICENSE.TXT"
patch -p0 < bolero-fixes.patch
29 changes: 26 additions & 3 deletions tests/src/engines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,30 @@ pub fn test() -> Result {
supports_env: engine != "kani",
test_crashes: engine != "afl", // TODO fix this
runs: 10_000,
jobs: None,
};

test.run()?;

if ["libfuzzer", "hongfuzz"].contains(&engine) {
if ["libfuzzer", "honggfuzz"].contains(&engine) {
Test {
sanitizer: "address".to_string(),
rustc_bootstrap: !is_nightly,
..test.clone()
}
.run()?;
}

// libfuzzer supports multiple jobs
if ["libfuzzer"].contains(&engine) {
Test {
sanitizer: "address".to_string(),
rustc_bootstrap: !is_nightly,
jobs: Some(2),
..test.clone()
}
.run()?;
}
}

Ok(())
Expand All @@ -48,6 +60,7 @@ struct Test {
supports_env: bool,
test_crashes: bool,
runs: u32,
jobs: Option<u32>,
}

impl Test {
Expand Down Expand Up @@ -77,6 +90,12 @@ impl Test {
args.push("--rustc-bootstrap".to_string());
}

let jobs = &if let Some(jobs) = self.jobs {
vec!["--jobs".to_string(), jobs.to_string()]
} else {
vec![]
};

let args = &args;

for test in [
Expand All @@ -92,7 +111,11 @@ impl Test {
continue;
}

cmd!(sh, "{cargo_bolero} test {test} --runs {runs} {args...}").run()?;
cmd!(
sh,
"{cargo_bolero} test {test} --runs {runs} {jobs...} {args...}"
)
.run()?;

if self.reduce {
cmd!(sh, "{cargo_bolero} reduce {test} {args...}").run()?;
Expand All @@ -117,7 +140,7 @@ impl Test {
let _ = sh.remove_path(format!("src/__fuzz__/{test}"));
}

let res = cmd!(sh, "{cargo_bolero} test {test} {args...}").run();
let res = cmd!(sh, "{cargo_bolero} test {test} {args...} {jobs...}").run();

assert!(res.is_err(), "test {test} should catch a panic");
}
Expand Down
Loading