Skip to content

Commit

Permalink
split and fix rustflags adding and separately build afl runtime and l…
Browse files Browse the repository at this point in the history
…lvm plugins
  • Loading branch information
brunoproduit committed Oct 10, 2023
1 parent 1bffcf8 commit b5eeedb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
13 changes: 12 additions & 1 deletion cargo-afl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ fn main() {
}

build_afl(&work_dir, base, llvm_config);
build_afl_llvm_runtime(&work_dir, base);

if cfg!(feature = "cmplog") {
build_afl_llvm_runtime(&work_dir, base);
build_afl_llvm_plugins(&work_dir, base);
}
}

Expand Down Expand Up @@ -91,6 +92,16 @@ fn build_afl_llvm_runtime(work_dir: &Path, base: Option<&Path>) {
)
.expect("Couldn't copy object file");

let status = Command::new(AR_CMD)
.arg("r")
.arg(common::archive_file_path(base))
.arg(common::object_file_path(base))
.status()
.expect("could not run 'ar'");
assert!(status.success());
}

fn build_afl_llvm_plugins(work_dir: &Path, base: Option<&Path>) {
let shared_libraries = [
"afl-llvm-dict2file.so",
"afl-llvm-pass.so",
Expand Down
37 changes: 19 additions & 18 deletions cargo-afl/src/bin/cargo-afl.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clap::crate_version;

use std::collections::HashMap;
use std::env;
use std::ffi::{OsStr, OsString};
use std::process::{self, Command, Stdio};
use std::collections::HashMap;

#[path = "../common.rs"]
mod common;
Expand Down Expand Up @@ -302,8 +302,15 @@ where
let binding = common::afl_llvm_dir(None);
let p = binding.display();

let mut rustflags = String::new();
let mut environment_variables= HashMap::<String, String>::new();
let mut rustflags = format!(
"-C debug-assertions \
-C overflow_checks \
-C passes={passes} \
-C codegen-units=1 \
-C opt-level=3 \
-C target-cpu=native "
);
let mut environment_variables = HashMap::<String, String>::new();
environment_variables.insert("ASAN_OPTIONS".to_string(), asan_options);
environment_variables.insert("TSAN_OPTIONS".to_string(), tsan_options);

Expand All @@ -325,29 +332,23 @@ where
assert!(status.success());

rustflags.push_str(&format!(
"-C debug-assertions \
-C overflow_checks \
-C passes={passes} \
-C codegen-units=1 \
-Z llvm-plugins={p}/cmplog-instructions-pass.so \
-Z llvm-plugins={p}/cmplog-routines-pass.so \
-Z llvm-plugins={p}/cmplog-switches-pass.so \
-Z llvm-plugins={p}/SanitizerCoveragePCGUARD.so \
-C opt-level=3 \
-C target-cpu=native "
"-Z llvm-plugins={p}/cmplog-instructions-pass.so \
-Z llvm-plugins={p}/cmplog-routines-pass.so \
-Z llvm-plugins={p}/cmplog-switches-pass.so \
-Z llvm-plugins={p}/SanitizerCoveragePCGUARD.so
"
));

environment_variables.insert("AFL_LLVM_INSTRUMENT".to_string(), "PCGUARD".to_string());
environment_variables.insert("AFL_LLVM_CMPLOG".to_string(), "1".to_string());
environment_variables.insert("AFL_QUIET".to_string(), "1".to_string());

} else {
rustflags.push_str(
"-C llvm-args=-sanitizer-coverage-level=3 \
-C llvm-args=-sanitizer-coverage-trace-pc-guard \
-C llvm-args=-sanitizer-coverage-prune-blocks=0 \
-C llvm-args=-sanitizer-coverage-trace-compares
",
-C llvm-args=-sanitizer-coverage-trace-pc-guard \
-C llvm-args=-sanitizer-coverage-prune-blocks=0 \
-C llvm-args=-sanitizer-coverage-trace-compares
",
);
}

Expand Down

0 comments on commit b5eeedb

Please sign in to comment.