Skip to content

Commit d3d6253

Browse files
author
Paho Lurie-Gregg
committedApr 16, 2021
Allow stripping of dead code
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.
1 parent e4ad715 commit d3d6253

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed
 

Diff for: ‎src/options.rs

+6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ pub struct BuildOptions {
123123
/// The attribute takes a default value `false`, ensuring that by default,
124124
/// the coverage option will be disabled).
125125
pub coverage: bool,
126+
127+
/// Dead code is linked by default to prevent a potential error with some
128+
/// optimized targets. This flag allows you to opt out of it.
129+
#[structopt(long)]
130+
pub strip_dead_code: bool,
126131
}
127132

128133
impl stdfmt::Display for BuildOptions {
@@ -200,6 +205,7 @@ mod test {
200205
unstable_flags: Vec::new(),
201206
target_dir: None,
202207
coverage: false,
208+
strip_dead_code: false,
203209
};
204210

205211
let opts = vec![

Diff for: ‎src/project.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,13 @@ impl FuzzProject {
162162
-Cllvm-args=-sanitizer-coverage-level=4 \
163163
-Cllvm-args=-sanitizer-coverage-trace-compares \
164164
-Cllvm-args=-sanitizer-coverage-inline-8bit-counters \
165-
-Cllvm-args=-sanitizer-coverage-pc-table \
166-
-Clink-dead-code"
165+
-Cllvm-args=-sanitizer-coverage-pc-table"
167166
.to_owned();
168167

168+
if !build.strip_dead_code {
169+
rustflags.push_str(" -Clink-dead-code");
170+
}
171+
169172
if build.coverage {
170173
rustflags.push_str(" -Zinstrument-coverage");
171174
}

Diff for: ‎tests/tests/main.rs

+26
Original file line numberDiff line numberDiff line change
@@ -810,3 +810,29 @@ fn build_dev() {
810810
assert!(a_bin.is_file());
811811
assert!(b_bin.is_file());
812812
}
813+
814+
#[test]
815+
fn build_stripping_dead_code() {
816+
let project = project("build_strip").with_fuzz().build();
817+
818+
// Create some targets.
819+
project
820+
.cargo_fuzz()
821+
.arg("add")
822+
.arg("build_strip_a")
823+
.assert()
824+
.success();
825+
826+
project
827+
.cargo_fuzz()
828+
.arg("build")
829+
.arg("--strip-dead-code")
830+
.arg("--dev")
831+
.assert()
832+
.success();
833+
834+
let build_dir = project.fuzz_build_dir().join("debug");
835+
836+
let a_bin = build_dir.join("build_strip_a");
837+
assert!(a_bin.is_file(), "Not a file: {}", a_bin.display());
838+
}

0 commit comments

Comments
 (0)
Please sign in to comment.