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

Add support for more llvm instrumentations #402

Open
wants to merge 1 commit 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
20 changes: 18 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ pub struct BuildOptions {
/// available.
pub no_trace_compares: bool,

#[arg(long)]
/// Enables `sanitizer-coverage-trace-divs` LLVM instrumentation
///
/// When set to `true`, the compiler will instrument integer division instructions
/// to capture the right argument of division.
pub trace_div: bool,

#[arg(long)]
/// Enables `sanitizer-coverage-trace-geps` LLVM instrumentation
///
/// When set to `true`, instruments GetElementPtr (GEP) instructions to track
/// pointer arithmetic operations to capture array indices.
pub trace_gep: bool,

#[arg(long)]
/// Disable transformation of if-statements into `cmov` instructions (when this
/// happens, we get no coverage feedback for that branch). Default setting is true.
Expand Down Expand Up @@ -165,7 +179,7 @@ pub struct BuildOptions {
/// Note, that in the second program, there are now 2 new coverage feedback points,
/// and the fuzzer can store an input to the corpus at each condition that it passes;
/// giving it a better chance of producing an input that reaches `res = 2;`.
pub disable_branch_folding: Option<bool>,
pub disable_branch_folding: bool,

Choose a reason for hiding this comment

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

This was an Option because the default value was "true", now the default is "false". Add this to set the default to "true":

    #[arg(long, default_value_t = true)]


#[arg(long)]
/// Disable the inclusion of the `/include:main` MSVC linker argument
Expand Down Expand Up @@ -279,7 +293,9 @@ mod test {
strip_dead_code: true,
no_cfg_fuzzing: false,
no_trace_compares: false,
disable_branch_folding: None,
trace_div: false,
trace_gep: false,
disable_branch_folding: false,
no_include_main_msvc: false,
};

Expand Down
12 changes: 10 additions & 2 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,12 @@ impl FuzzProject {
rustflags.push_str(" -Cllvm-args=-sanitizer-coverage-trace-compares");
}

if build.disable_branch_folding.unwrap_or(true) {
rustflags.push_str(" -Cllvm-args=-simplifycfg-branch-fold-threshold=0");
if build.trace_div {
rustflags.push_str(" -Cllvm-args=-sanitizer-coverage-trace-divs");
}

if build.trace_gep {
rustflags.push_str(" -Cllvm-args=-sanitizer-coverage-trace-geps");
}

if !build.no_cfg_fuzzing {
Expand All @@ -194,6 +198,10 @@ impl FuzzProject {
rustflags.push_str(" -Clink-dead-code");
}

if build.disable_branch_folding {
rustflags.push_str(" -Cllvm-args=-simplifycfg-branch-fold-threshold=0");
}

if build.coverage {
rustflags.push_str(" -Cinstrument-coverage");
}
Expand Down
29 changes: 29 additions & 0 deletions tests/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,35 @@ fn build_stripping_dead_code() {
assert!(a_bin.is_file(), "Not a file: {}", a_bin.display());
}

#[test]
fn build_with_all_llvm_features() {
let project = project("build_all_feats").with_fuzz().build();

// Create some targets.
project
.cargo_fuzz()
.arg("add")
.arg("build_strip_a")
.assert()
.success();

project
.cargo_fuzz()
.arg("build")
.arg("--strip-dead-code")
.arg("--dev")
.arg("--trace-div")
.arg("--trace-gep")
.arg("--disable-branch-folding")
.assert()
.success();

let build_dir = project.fuzz_build_dir().join("debug");

let a_bin = build_dir.join("build_strip_a");
assert!(a_bin.is_file(), "Not a file: {}", a_bin.display());
}

#[test]
fn run_with_different_fuzz_dir() {
let (fuzz_dir, mut project_builder) = project_with_fuzz_dir(
Expand Down
Loading