Skip to content

Commit

Permalink
Allow stripping of dead code
Browse files Browse the repository at this point in the history
The `-Clink-dead-code` flag was added to fix an error with certain
targets.

However, it can also cause problems.

Example: You depend on crate A which links to library B. A includes an
unused reference to `B::foo`. If the specific version of B we have is
missing `foo`, we now get a linker error whereas we would not if
stripping dead code.
  • Loading branch information
Paho Lurie-Gregg committed Apr 16, 2021
1 parent e4ad715 commit d3d6253
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ pub struct BuildOptions {
/// The attribute takes a default value `false`, ensuring that by default,
/// the coverage option will be disabled).
pub coverage: bool,

/// Dead code is linked by default to prevent a potential error with some
/// optimized targets. This flag allows you to opt out of it.
#[structopt(long)]
pub strip_dead_code: bool,
}

impl stdfmt::Display for BuildOptions {
Expand Down Expand Up @@ -200,6 +205,7 @@ mod test {
unstable_flags: Vec::new(),
target_dir: None,
coverage: false,
strip_dead_code: false,
};

let opts = vec![
Expand Down
7 changes: 5 additions & 2 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,13 @@ impl FuzzProject {
-Cllvm-args=-sanitizer-coverage-level=4 \
-Cllvm-args=-sanitizer-coverage-trace-compares \
-Cllvm-args=-sanitizer-coverage-inline-8bit-counters \
-Cllvm-args=-sanitizer-coverage-pc-table \
-Clink-dead-code"
-Cllvm-args=-sanitizer-coverage-pc-table"
.to_owned();

if !build.strip_dead_code {
rustflags.push_str(" -Clink-dead-code");
}

if build.coverage {
rustflags.push_str(" -Zinstrument-coverage");
}
Expand Down
26 changes: 26 additions & 0 deletions tests/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,3 +810,29 @@ fn build_dev() {
assert!(a_bin.is_file());
assert!(b_bin.is_file());
}

#[test]
fn build_stripping_dead_code() {
let project = project("build_strip").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")
.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());
}

0 comments on commit d3d6253

Please sign in to comment.