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

run only 1 sanitized binary #116

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
54 changes: 38 additions & 16 deletions src/bin/cargo-ziggy/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,23 @@ impl Build {

// Add the --release argument if self.release is true
if self.release {
assert!(!self.release, "cannot use --release for ASAN builds");
assert!(!self.asan, "cannot use --release for ASAN builds");
afl_args.push("--release");
}

let opt_level = env::var("AFL_OPT_LEVEL").unwrap_or("0".to_string());
let mut rust_flags = env::var("RUSTFLAGS").unwrap_or_default();
let mut rust_doc_flags = env::var("RUSTDOCFLAGS").unwrap_or_default();
let asan_target_str = format!("--target={ASAN_TARGET}");
let opt_level_str = format!("-Copt-level={opt_level}");

if self.asan {
assert_eq!(opt_level, "0", "AFL_OPT_LEVEL must be 0 for ASAN builds");
afl_args.push(&asan_target_str);
afl_args.extend(["-Z", "build-std"]);
rust_flags.push_str(" -Zsanitizer=address ");
rust_flags.push_str(&opt_level_str);
rust_doc_flags.push_str(" -Zsanitizer=address ")
};

// First fuzzer we build: AFL++
let run = process::Command::new(cargo.clone())
.args(afl_args)
.args(&afl_args)
.env("AFL_QUIET", "1")
// need to specify for afl.rs so that we build with -Copt-level=0
.env("AFL_OPT_LEVEL", opt_level)
.env("AFL_OPT_LEVEL", &opt_level)
.env("AFL_LLVM_CMPLOG", "1") // for afl.rs feature "plugins"
.env("RUSTFLAGS", rust_flags)
.env("RUSTDOCFLAGS", rust_doc_flags)
.env("RUSTFLAGS", &rust_flags)
.env("RUSTDOCFLAGS", &rust_doc_flags)
.spawn()?
.wait()
.context("Error spawning afl build command")?;
Expand All @@ -69,6 +58,39 @@ impl Build {
));
}

let asan_target_str = format!("--target={ASAN_TARGET}");
let opt_level_str = format!("-Copt-level={opt_level}");

// If ASAN is enabled, build both a sanitized binary and a non-sanitized binary.
if self.asan {
eprintln!(" {} afl (ASan)", style("Building").red().bold());
assert_eq!(opt_level, "0", "AFL_OPT_LEVEL must be 0 for ASAN builds");
afl_args.push(&asan_target_str);
afl_args.extend(["-Z", "build-std"]);
rust_flags.push_str(" -Zsanitizer=address ");
rust_flags.push_str(&opt_level_str);
rust_doc_flags.push_str(" -Zsanitizer=address ");

let run = process::Command::new(cargo.clone())
.args(afl_args)
.env("AFL_QUIET", "1")
// need to specify for afl.rs so that we build with -Copt-level=0
.env("AFL_OPT_LEVEL", opt_level)
.env("AFL_LLVM_CMPLOG", "1") // for afl.rs feature "plugins"
.env("RUSTFLAGS", rust_flags)
.env("RUSTDOCFLAGS", rust_doc_flags)
.spawn()?
.wait()
.context("Error spawning afl build command")?;

if !run.success() {
return Err(anyhow!(
"Error building afl fuzzer: Exited with {:?}",
run.code()
));
}
};

eprintln!(" {} afl", style("Finished").cyan().bold());
}

Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo-ziggy/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ impl Fuzz {
false => {
if self.release {
format!("./target/afl/release/{}", self.target)
} else if self.asan {
} else if self.asan && job_num == 0 {
format!("./target/afl/{ASAN_TARGET}/debug/{}", self.target)
} else {
format!("./target/afl/debug/{}", self.target)
Expand Down
Loading