Skip to content

Commit

Permalink
Merge pull request rust-fuzz#264 from c410-f3r/stuff
Browse files Browse the repository at this point in the history
[`FuzzProject::exec_fuzz`] Take `fuzz_dir` into consideration
  • Loading branch information
fitzgen authored May 25, 2021
2 parents 2e496ca + 076cd62 commit ede133e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
16 changes: 14 additions & 2 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,14 +459,22 @@ impl FuzzProject {
eprintln!();
}

let fuzz_dir = if self.fuzz_dir_is_default_path() {
String::new()
} else {
format!(" --fuzz-dir {}", self.fuzz_dir().display())
};

eprintln!(
"Reproduce with:\n\n\tcargo fuzz run{options} {target} {artifact}\n",
"Reproduce with:\n\n\tcargo fuzz run{fuzz_dir}{options} {target} {artifact}\n",
fuzz_dir = &fuzz_dir,
options = &run.build,
target = &run.target,
artifact = artifact.display()
);
eprintln!(
"Minimize test case with:\n\n\tcargo fuzz tmin{options} {target} {artifact}\n",
"Minimize test case with:\n\n\tcargo fuzz tmin{fuzz_dir}{options} {target} {artifact}\n",
fuzz_dir = &fuzz_dir,
options = &run.build,
target = &run.target,
artifact = artifact.display()
Expand Down Expand Up @@ -822,6 +830,10 @@ impl FuzzProject {
targets: Vec::new(),
})
}

fn fuzz_dir_is_default_path(&self) -> bool {
self.fuzz_dir.ends_with(DEFAULT_FUZZ_DIR)
}
}

fn collect_targets(value: &toml::Value) -> Vec<String> {
Expand Down
64 changes: 58 additions & 6 deletions tests/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,12 +839,11 @@ fn build_stripping_dead_code() {

#[test]
fn run_with_different_fuzz_dir() {
const FUZZ_DIR_NAME: &str = "dir_likes_to_move_it_move_it";

let next_root = next_root();
let fuzz_dir = next_root.join(FUZZ_DIR_NAME);

let project = project_with_params("project_likes_to_move_it", next_root, fuzz_dir.clone())
let (fuzz_dir, mut project_builder) = project_with_fuzz_dir(
"project_likes_to_move_it",
Some("dir_likes_to_move_it_move_it"),
);
let project = project_builder
.with_fuzz()
.fuzz_target(
"you_like_to_move_it",
Expand All @@ -870,3 +869,56 @@ fn run_with_different_fuzz_dir() {
.stderr(predicate::str::contains("Done 2 runs"))
.success();
}

#[test]
fn run_diagnostic_contains_fuzz_dir() {
let (fuzz_dir, mut project_builder) = project_with_fuzz_dir("run_with_crash", None);
let project = project_builder
.with_fuzz()
.fuzz_target(
"yes_crash",
r#"
#![no_main]
use libfuzzer_sys::fuzz_target;
fuzz_target!(|data: &[u8]| {
run_with_crash::fail_fuzzing(data);
});
"#,
)
.build();

let run = format!(
"cargo fuzz run --fuzz-dir {} yes_crash custom_dir/artifacts/yes_crash",
&fuzz_dir
);

let tmin = format!(
"cargo fuzz tmin --fuzz-dir {} yes_crash custom_dir/artifacts/yes_crash",
&fuzz_dir
);

project
.cargo_fuzz()
.arg("run")
.arg("--fuzz-dir")
.arg(fuzz_dir)
.arg("yes_crash")
.arg("--")
.arg("-runs=1000")
.assert()
.stderr(predicates::str::contains(run).and(predicate::str::contains(tmin)))
.failure();
}

fn project_with_fuzz_dir(
project_name: &str,
fuzz_dir_opt: Option<&str>,
) -> (String, ProjectBuilder) {
let fuzz_dir = fuzz_dir_opt.unwrap_or("custom_dir");
let next_root = next_root();
let fuzz_dir_pb = next_root.join(fuzz_dir);
let fuzz_dir_sting = fuzz_dir_pb.display().to_string();
let pb = project_with_params(project_name, next_root, fuzz_dir_pb);
(fuzz_dir_sting, pb)
}

0 comments on commit ede133e

Please sign in to comment.