Skip to content

Commit fa7e931

Browse files
MarcoFalkehodlinator
andcommitted
contrib: Add optional parallelism to deterministic-fuzz-coverage
Co-Authored-By: Hodlinator <[email protected]>
1 parent a54baa8 commit fa7e931

File tree

1 file changed

+53
-15
lines changed
  • contrib/devtools/deterministic-fuzz-coverage/src

1 file changed

+53
-15
lines changed

contrib/devtools/deterministic-fuzz-coverage/src/main.rs

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or https://opensource.org/license/mit/.
44

5+
use std::collections::VecDeque;
56
use std::env;
6-
use std::fs::{read_dir, File};
7+
use std::fs::{read_dir, DirEntry, File};
78
use std::path::{Path, PathBuf};
89
use std::process::{Command, ExitCode};
910
use std::str;
11+
use std::thread;
1012

1113
/// A type for a complete and readable error message.
1214
type AppError = String;
@@ -16,12 +18,14 @@ const LLVM_PROFDATA: &str = "llvm-profdata";
1618
const LLVM_COV: &str = "llvm-cov";
1719
const GIT: &str = "git";
1820

21+
const DEFAULT_PAR: usize = 1;
22+
1923
fn exit_help(err: &str) -> AppError {
2024
format!(
2125
r#"
2226
Error: {err}
2327
24-
Usage: program ./build_dir ./qa-assets/fuzz_corpora fuzz_target_name
28+
Usage: program ./build_dir ./qa-assets/fuzz_corpora fuzz_target_name [parallelism={DEFAULT_PAR}]
2529
2630
Refer to the devtools/README.md for more details."#
2731
)
@@ -63,7 +67,14 @@ fn app() -> AppResult {
6367
// Require fuzz target for now. In the future it could be optional and the tool could
6468
// iterate over all compiled fuzz targets
6569
.ok_or(exit_help("Must set fuzz target"))?;
66-
if args.get(4).is_some() {
70+
let par = match args.get(4) {
71+
Some(s) => s
72+
.parse::<usize>()
73+
.map_err(|e| exit_help(&format!("Could not parse parallelism as usize ({s}): {e}")))?,
74+
None => DEFAULT_PAR,
75+
}
76+
.max(1);
77+
if args.get(5).is_some() {
6778
Err(exit_help("Too many args"))?;
6879
}
6980

@@ -73,7 +84,7 @@ fn app() -> AppResult {
7384

7485
sanity_check(corpora_dir, &fuzz_exe)?;
7586

76-
deterministic_coverage(build_dir, corpora_dir, &fuzz_exe, fuzz_target)
87+
deterministic_coverage(build_dir, corpora_dir, &fuzz_exe, fuzz_target, par)
7788
}
7889

7990
fn using_libfuzzer(fuzz_exe: &Path) -> Result<bool, AppError> {
@@ -94,10 +105,9 @@ fn deterministic_coverage(
94105
corpora_dir: &Path,
95106
fuzz_exe: &Path,
96107
fuzz_target: &str,
108+
par: usize,
97109
) -> AppResult {
98110
let using_libfuzzer = using_libfuzzer(fuzz_exe)?;
99-
let profraw_file = build_dir.join("fuzz_det_cov.profraw");
100-
let profdata_file = build_dir.join("fuzz_det_cov.profdata");
101111
let corpus_dir = corpora_dir.join(fuzz_target);
102112
let mut entries = read_dir(&corpus_dir)
103113
.map_err(|err| {
@@ -110,8 +120,10 @@ fn deterministic_coverage(
110120
.map(|entry| entry.expect("IO error"))
111121
.collect::<Vec<_>>();
112122
entries.sort_by_key(|entry| entry.file_name());
113-
let run_single = |run_id: u8, entry: &Path| -> Result<PathBuf, AppError> {
114-
let cov_txt_path = build_dir.join(format!("fuzz_det_cov.show.{run_id}.txt"));
123+
let run_single = |run_id: u8, entry: &Path, thread_id: usize| -> Result<PathBuf, AppError> {
124+
let cov_txt_path = build_dir.join(format!("fuzz_det_cov.show.t{thread_id}.r{run_id}.txt"));
125+
let profraw_file = build_dir.join(format!("fuzz_det_cov.t{thread_id}.r{run_id}.profraw"));
126+
let profdata_file = build_dir.join(format!("fuzz_det_cov.t{thread_id}.r{run_id}.profdata"));
115127
if !{
116128
{
117129
let mut cmd = Command::new(fuzz_exe);
@@ -187,20 +199,46 @@ The coverage was not deterministic between runs.
187199
//
188200
// Also, This can catch issues where several fuzz inputs are non-deterministic, but the sum of
189201
// their overall coverage trace remains the same across runs and thus remains undetected.
190-
println!("Check each fuzz input individually ...");
191-
for entry in entries {
202+
println!(
203+
"Check each fuzz input individually ... ({} inputs with parallelism {par})",
204+
entries.len()
205+
);
206+
let check_individual = |entry: &DirEntry, thread_id: usize| -> AppResult {
192207
let entry = entry.path();
193208
if !entry.is_file() {
194209
Err(format!("{} should be a file", entry.display()))?;
195210
}
196-
let cov_txt_base = run_single(0, &entry)?;
197-
let cov_txt_repeat = run_single(1, &entry)?;
211+
let cov_txt_base = run_single(0, &entry, thread_id)?;
212+
let cov_txt_repeat = run_single(1, &entry, thread_id)?;
198213
check_diff(
199214
&cov_txt_base,
200215
&cov_txt_repeat,
201216
&format!("The fuzz target input was {}.", entry.display()),
202217
)?;
203-
}
218+
Ok(())
219+
};
220+
thread::scope(|s| -> AppResult {
221+
let mut handles = VecDeque::with_capacity(par);
222+
let mut res = Ok(());
223+
for (i, entry) in entries.iter().enumerate() {
224+
println!("[{}/{}]", i + 1, entries.len());
225+
handles.push_back(s.spawn(move || check_individual(entry, i % par)));
226+
while handles.len() >= par || i == (entries.len() - 1) || res.is_err() {
227+
if let Some(th) = handles.pop_front() {
228+
let thread_result = match th.join() {
229+
Err(_e) => Err("A scoped thread panicked".to_string()),
230+
Ok(r) => r,
231+
};
232+
if thread_result.is_err() {
233+
res = thread_result;
234+
}
235+
} else {
236+
return res;
237+
}
238+
}
239+
}
240+
res
241+
})?;
204242
// Finally, check that running over all fuzz inputs in one process is deterministic as well.
205243
// This can catch issues where mutable global state is leaked from one fuzz input execution to
206244
// the next.
@@ -209,8 +247,8 @@ The coverage was not deterministic between runs.
209247
if !corpus_dir.is_dir() {
210248
Err(format!("{} should be a folder", corpus_dir.display()))?;
211249
}
212-
let cov_txt_base = run_single(0, &corpus_dir)?;
213-
let cov_txt_repeat = run_single(1, &corpus_dir)?;
250+
let cov_txt_base = run_single(0, &corpus_dir, 0)?;
251+
let cov_txt_repeat = run_single(1, &corpus_dir, 0)?;
214252
check_diff(
215253
&cov_txt_base,
216254
&cov_txt_repeat,

0 commit comments

Comments
 (0)