Skip to content

Commit 65e3279

Browse files
authored
Merge pull request #402 from 0xdeafbeef/0xdeafbeef/push-tqronnqkpknw
Add support for more llvm instrumentations
2 parents 95f7737 + 2e41070 commit 65e3279

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

src/options.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ pub struct BuildOptions {
137137
pub no_trace_compares: bool,
138138

139139
#[arg(long)]
140+
/// Enables `sanitizer-coverage-trace-divs` LLVM instrumentation
141+
///
142+
/// When set to `true`, the compiler will instrument integer division instructions
143+
/// to capture the right argument of division.
144+
pub trace_div: bool,
145+
146+
#[arg(long)]
147+
/// Enables `sanitizer-coverage-trace-geps` LLVM instrumentation
148+
///
149+
/// When set to `true`, instruments GetElementPtr (GEP) instructions to track
150+
/// pointer arithmetic operations to capture array indices.
151+
pub trace_gep: bool,
152+
153+
#[arg(long, default_value_t = true)]
140154
/// Disable transformation of if-statements into `cmov` instructions (when this
141155
/// happens, we get no coverage feedback for that branch). Default setting is true.
142156
/// This is done by setting the `-simplifycfg-branch-fold-threshold=0` LLVM arg.
@@ -165,7 +179,7 @@ pub struct BuildOptions {
165179
/// Note, that in the second program, there are now 2 new coverage feedback points,
166180
/// and the fuzzer can store an input to the corpus at each condition that it passes;
167181
/// giving it a better chance of producing an input that reaches `res = 2;`.
168-
pub disable_branch_folding: Option<bool>,
182+
pub disable_branch_folding: bool,
169183

170184
#[arg(long)]
171185
/// Disable the inclusion of the `/include:main` MSVC linker argument
@@ -279,7 +293,9 @@ mod test {
279293
strip_dead_code: true,
280294
no_cfg_fuzzing: false,
281295
no_trace_compares: false,
282-
disable_branch_folding: None,
296+
trace_div: false,
297+
trace_gep: false,
298+
disable_branch_folding: true,
283299
no_include_main_msvc: false,
284300
};
285301

src/project.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,12 @@ impl FuzzProject {
182182
rustflags.push_str(" -Cllvm-args=-sanitizer-coverage-trace-compares");
183183
}
184184

185-
if build.disable_branch_folding.unwrap_or(true) {
186-
rustflags.push_str(" -Cllvm-args=-simplifycfg-branch-fold-threshold=0");
185+
if build.trace_div {
186+
rustflags.push_str(" -Cllvm-args=-sanitizer-coverage-trace-divs");
187+
}
188+
189+
if build.trace_gep {
190+
rustflags.push_str(" -Cllvm-args=-sanitizer-coverage-trace-geps");
187191
}
188192

189193
if !build.no_cfg_fuzzing {
@@ -194,6 +198,10 @@ impl FuzzProject {
194198
rustflags.push_str(" -Clink-dead-code");
195199
}
196200

201+
if build.disable_branch_folding {
202+
rustflags.push_str(" -Cllvm-args=-simplifycfg-branch-fold-threshold=0");
203+
}
204+
197205
if build.coverage {
198206
rustflags.push_str(" -Cinstrument-coverage");
199207
}

tests/tests/main.rs

+29
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,35 @@ fn build_stripping_dead_code() {
915915
assert!(a_bin.is_file(), "Not a file: {}", a_bin.display());
916916
}
917917

918+
#[test]
919+
fn build_with_all_llvm_features() {
920+
let project = project("build_all_feats").with_fuzz().build();
921+
922+
// Create some targets.
923+
project
924+
.cargo_fuzz()
925+
.arg("add")
926+
.arg("build_strip_a")
927+
.assert()
928+
.success();
929+
930+
project
931+
.cargo_fuzz()
932+
.arg("build")
933+
.arg("--strip-dead-code")
934+
.arg("--dev")
935+
.arg("--trace-div")
936+
.arg("--trace-gep")
937+
.arg("--disable-branch-folding")
938+
.assert()
939+
.success();
940+
941+
let build_dir = project.fuzz_build_dir().join("debug");
942+
943+
let a_bin = build_dir.join("build_strip_a");
944+
assert!(a_bin.is_file(), "Not a file: {}", a_bin.display());
945+
}
946+
918947
#[test]
919948
fn run_with_different_fuzz_dir() {
920949
let (fuzz_dir, mut project_builder) = project_with_fuzz_dir(

0 commit comments

Comments
 (0)