Skip to content

Commit

Permalink
add option to enable "careful mode" inspired by cargo-careful
Browse files Browse the repository at this point in the history
* based on https://github.com/RalfJung/cargo-careful/
* disables default `-Zbuild-std` from the previous PR on `build-std`
  • Loading branch information
Michael Rodler committed May 4, 2023
1 parent 4c8061b commit 142a0f5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ pub struct BuildOptions {
/// Currently this conflicts with coverage instrumentation but -Zbuild-std enables detecting
/// more bugs so this option defaults to true, but when using `cargo fuzz coverage` it
/// defaults to false.
pub build_std: Option<bool>,
pub build_std: bool,

#[arg(short, long = "careful")]
/// enable "careful" mode: inspired by https://github.com/RalfJung/cargo-careful, this enables
/// building the fuzzing harness along with the standard library (implies --build-std) with
/// debug assertions and extra const UB and init checks.
pub careful_mode: bool,

#[arg(long = "target", default_value(crate::utils::default_target()))]
/// Target triple of the fuzz target
Expand Down Expand Up @@ -217,7 +223,8 @@ mod test {
no_default_features: false,
all_features: false,
features: None,
build_std: None,
build_std: false,
careful_mode: false,
sanitizer: Sanitizer::Address,
triple: String::from(crate::utils::default_target()),
unstable_flags: Vec::new(),
Expand Down
2 changes: 1 addition & 1 deletion src/options/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Coverage {

impl RunCommand for Coverage {
fn run_command(&mut self) -> Result<()> {
if self.build.build_std.unwrap_or(false) {
if self.build.build_std {
bail!(
"-Zbuild-std is currently incompatible with -Zinstrument-coverage, \
see https://github.com/rust-lang/wg-cargo-std-aware/issues/63"
Expand Down
13 changes: 9 additions & 4 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ impl FuzzProject {
for flag in &build.unstable_flags {
cmd.arg("-Z").arg(flag);
}
if let Sanitizer::Memory = build.sanitizer {
cmd.arg("-Z").arg("build-std");
} else if build.build_std.unwrap_or(true) && !build.coverage {

if (matches!(build.sanitizer, Sanitizer::Memory) || build.build_std || build.careful_mode)
&& !build.coverage
{
cmd.arg("-Z").arg("build-std");
}

Expand Down Expand Up @@ -196,10 +197,14 @@ impl FuzzProject {
sanitizer = build.sanitizer
)),
}

if build.careful_mode {
rustflags.push_str(" -Zextra-const-ub-checks -Zstrict-init-checks --cfg careful");
}
if build.triple.contains("-linux-") {
rustflags.push_str(" -Cllvm-args=-sanitizer-coverage-stack-depth");
}
if !build.release || build.debug_assertions {
if !build.release || build.debug_assertions || build.careful_mode {
rustflags.push_str(" -Cdebug-assertions");
}
if build.triple.contains("-msvc") {
Expand Down

0 comments on commit 142a0f5

Please sign in to comment.