Skip to content

Commit

Permalink
Apply review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
sharnoff committed Dec 14, 2022
1 parent 5b365b6 commit ccd42f5
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,36 +266,27 @@ impl FuzzProject {
Ok(cmd)
}

fn target_dir(&self, build: &options::BuildOptions) -> Result<PathBuf> {
if build.coverage {
Ok(env::current_dir()?
.join("target")
.join(default_target())
.join("coverage"))
// note: never returns Ok(None) if build.coverage is true
fn target_dir(&self, build: &options::BuildOptions) -> Result<Option<PathBuf>> {
// Use the user-provided target directory, if provided. Otherwise if building for coverage,
// use the coverage directory
if let Some(target_dir) = build.target_dir.as_ref() {
return Ok(Some(PathBuf::from(target_dir)));
} else if build.coverage {
// To ensure that fuzzing and coverage-output generation can run in parallel, we
// produce a separate binary for the coverage command.
let current_dir = env::current_dir()?;
Ok(Some(
current_dir
.join("target")
.join(default_target())
.join("coverage"),
))
} else {
Ok(build
.target_dir
.as_ref()
.map(PathBuf::from)
.unwrap_or_else(|| self.project_dir.join("target")))
Ok(None)
}
}

fn cargo_build_bin_path(
&self,
build: &options::BuildOptions,
fuzz_target: &str,
) -> Result<PathBuf> {
let profile_subdir = if build.dev { "debug" } else { "release" };

let bin_path = self
.target_dir(&build)?
.join(&build.triple)
.join(profile_subdir)
.join(fuzz_target);
Ok(bin_path)
}

pub fn exec_build(
&self,
mode: options::BuildMode,
Expand All @@ -314,8 +305,7 @@ impl FuzzProject {
cmd.arg("--bins");
}

if build.target_dir.is_some() || build.coverage {
let target_dir = self.target_dir(&build)?;
if let Some(target_dir) = self.target_dir(&build)? {
cmd.arg("--target-dir").arg(target_dir);
}

Expand Down Expand Up @@ -710,7 +700,23 @@ impl FuzzProject {
coverage_dir: &Path,
input_file: &Path,
) -> Result<(Command, String)> {
let mut cmd = Command::new(self.cargo_build_bin_path(&coverage.build, &coverage.target)?);
let bin_path = {
let profile_subdir = if coverage.build.dev {
"debug"
} else {
"release"
};

let target_dir = self
.target_dir(&coverage.build)?
.expect("target dir for coverage command should never be None");
target_dir
.join(&coverage.build.triple)
.join(profile_subdir)
.join(&coverage.target)
};

let mut cmd = Command::new(bin_path);

// Raw coverage data will be saved in `coverage/<target>` directory.
let input_file_name = input_file
Expand Down

0 comments on commit ccd42f5

Please sign in to comment.