Skip to content

Commit

Permalink
feat: replace the --release flag with a generic --profile flag (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekleog-NEAR authored Sep 5, 2023
1 parent b1f2d9f commit d2cb031
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 36 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ members = [
"cargo-bolero",
]

[profile.fuzz]
inherits = "dev"
opt-level = 3
incremental = false
codegen-units = 1

[profile.release]
lto = true
codegen-units = 1
Expand Down
15 changes: 0 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ libfuzzer honggfuzz:
--manifest-path examples/basic/Cargo.toml \
--runs 100000 \
--engine $@ \
--release \
--sanitizer $(SANITIZER)
@cargo run \
--features $@ \
reduce \
fuzz_bytes \
--manifest-path examples/basic/Cargo.toml \
--engine $@ \
--release \
--sanitizer $(SANITIZER)
@cargo run \
--features $@ \
Expand All @@ -78,15 +76,13 @@ libfuzzer honggfuzz:
--manifest-path examples/basic/Cargo.toml \
--runs 100000 \
--engine $@ \
--release true \
--sanitizer $(SANITIZER)
@cargo run \
--features $@ \
reduce \
fuzz_generator \
--manifest-path examples/basic/Cargo.toml \
--engine $@ \
--release \
--sanitizer $(SANITIZER)
@cargo run \
--features $@ \
Expand All @@ -95,15 +91,13 @@ libfuzzer honggfuzz:
--manifest-path examples/basic/Cargo.toml \
--runs 1000 \
--engine $@ \
--release \
--sanitizer $(SANITIZER)
@cargo run \
--features $@ \
reduce \
fuzz_operations \
--manifest-path examples/basic/Cargo.toml \
--engine $@ \
--release \
--sanitizer $(SANITIZER)
@SHOULD_PANIC=1 cargo run \
--features $@ \
Expand All @@ -112,7 +106,6 @@ libfuzzer honggfuzz:
--manifest-path examples/basic/Cargo.toml \
--runs 100000 \
--engine $@ \
--release \
--sanitizer $(SANITIZER) \
|| true # TODO make this consistent
@SHOULD_PANIC=1 cargo run \
Expand All @@ -121,7 +114,6 @@ libfuzzer honggfuzz:
tests::add_test \
--manifest-path examples/basic/Cargo.toml \
--engine $@ \
--release \
--sanitizer $(SANITIZER) \
|| true # TODO make this consistent
@SHOULD_PANIC=1 cargo run \
Expand All @@ -131,7 +123,6 @@ libfuzzer honggfuzz:
--manifest-path examples/basic/Cargo.toml \
--runs 100000 \
--engine $@ \
--release \
--sanitizer $(SANITIZER) \
|| true # TODO make this consistent
@SHOULD_PANIC=1 cargo run \
Expand All @@ -140,7 +131,6 @@ libfuzzer honggfuzz:
tests::other_test \
--manifest-path examples/basic/Cargo.toml \
--engine $@ \
--release \
--sanitizer $(SANITIZER) \
|| true # TODO make this consistent
@SHOULD_PANIC=1 cargo run \
Expand All @@ -150,7 +140,6 @@ libfuzzer honggfuzz:
--manifest-path examples/basic/Cargo.toml \
--runs 1000 \
--engine $@ \
--release \
--sanitizer $(SANITIZER) \
|| true # TODO make this consistent

Expand All @@ -162,7 +151,6 @@ afl:
--manifest-path examples/basic/Cargo.toml \
--runs 100000 \
--engine $@ \
--release \
--sanitizer $(SANITIZER)
@cargo run \
--features $@ \
Expand All @@ -171,7 +159,6 @@ afl:
--manifest-path examples/basic/Cargo.toml \
--runs 100000 \
--engine $@ \
--release \
--sanitizer $(SANITIZER)
@rm -rf examples/basic/src/__fuzz__
@SHOULD_PANIC=1 cargo run \
Expand All @@ -181,7 +168,6 @@ afl:
--manifest-path examples/basic/Cargo.toml \
--runs 100000 \
--engine $@ \
--release \
--sanitizer $(SANITIZER) \
&& exit 1 || true
@rm -rf examples/basic/src/__fuzz__
Expand All @@ -192,7 +178,6 @@ afl:
--manifest-path examples/basic/Cargo.toml \
--runs 100000 \
--engine $@ \
--release \
--sanitizer $(SANITIZER) \
&& exit 1 || true

Expand Down
14 changes: 14 additions & 0 deletions book/src/library-installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ bolero = "0.9"
```
to `Cargo.toml`.

Then, create the `fuzz` profile: (Note that LTO is not well-supported for the fuzzing profile)
```toml
[profile.fuzz]
inherits = "dev"
opt-level = 3
incremental = false
codegen-units = 1
```

If you forget adding the profile, then you will get the following error:
```
error: profile `fuzz` is not defined
```

## Structured Test Generation

If your crate wishes to implement structured test generation on public data structures, `bolero-generator` can be added to the main dependencies:
Expand Down
27 changes: 6 additions & 21 deletions cargo-bolero/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ pub struct Project {
#[structopt(long)]
all_features: bool,

/// Build artifacts in release mode, with optimizations [default: true]
#[structopt(long)]
release: Option<Option<bool>>,
/// Build artifacts in release mode, with optimizations [default: "fuzz"]
///
/// Note that if you do not have `codegen-units = 1` in the profile, there are known compilation bugs
#[structopt(long, default_value = "fuzz")]
profile: String,

/// Do not activate the `default` feature
#[structopt(long)]
Expand Down Expand Up @@ -88,10 +90,7 @@ impl Project {
let mut cmd = self.cargo();

cmd.arg(call).arg("--target").arg(&self.target);

if self.release() {
cmd.arg("--release");
}
cmd.arg("--profile").arg(&self.profile);

if self.no_default_features {
cmd.arg("--no-default-features");
Expand Down Expand Up @@ -187,12 +186,6 @@ impl Project {
} else {
None
})
// https://github.com/rust-lang/rust/issues/47071
.chain(if self.release() {
Some("-Ccodegen-units=1")
} else {
None
})
.map(String::from)
.chain(self.sanitizer_flags())
.chain(std::env::var(inherits).ok())
Expand All @@ -212,12 +205,4 @@ impl Project {
self.sanitizers()
.map(|sanitizer| format!("-Zsanitizer={}", sanitizer))
}

fn release(&self) -> bool {
match self.release {
None => true,
Some(None) => true,
Some(Some(v)) => v,
}
}
}
6 changes: 6 additions & 0 deletions examples/basic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ harness = false

[profile.bench]
debug = true

[profile.fuzz]
inherits = "dev"
opt-level = 3
incremental = false
codegen-units = 1
6 changes: 6 additions & 0 deletions examples/workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ members = ["crate_a", "crate_b"]

[profile.bench]
debug = true

[profile.fuzz]
inherits = "dev"
opt-level = 3
incremental = false
codegen-units = 1

0 comments on commit d2cb031

Please sign in to comment.