Skip to content

Commit 772996a

Browse files
committed
Merge bitcoin#32158: fuzz: Make partially_downloaded_block more deterministic
fa51310 contrib: Warn about using libFuzzer for coverage check (MarcoFalke) fa17cdb test: Avoid script check worker threads while fuzzing (MarcoFalke) fa900bb contrib: Only print fuzz output on failure (MarcoFalke) fa82fe2 contrib: Use -Xdemangler=llvm-cxxfilt in deterministic-*-coverage (MarcoFalke) fa7e931 contrib: Add optional parallelism to deterministic-fuzz-coverage (MarcoFalke) Pull request description: This should make the `partially_downloaded_block` fuzz target even more deterministic. Follow-up to bitcoin#31841. Tracking issue: bitcoin#29018. This bundles several changes: * First, speed up the `deterministic-fuzz-coverage` helper by introducing parallelism. * Then, a fix to remove spawned test threads or spawn them deterministically. (While testing this, high parallelism and thread contention may be needed) ### Testing It can be tested via (setting 32 parallel threads): ``` cargo run --manifest-path ./contrib/devtools/deterministic-fuzz-coverage/Cargo.toml -- $PWD/bld-cmake/ $PWD/../b-c-qa-assets/fuzz_corpora/ partially_downloaded_block 32 ``` Locally, on a failure, the output would look like: ```diff .... - 150| 0| m_worker_threads.emplace_back([this, n]() { - 151| 0| util::ThreadRename(strprintf("scriptch.%i", n)); + 150| 1| m_worker_threads.emplace_back([this, n]() { + 151| 1| util::ThreadRename(strprintf("scriptch.%i", n)); ... ``` This excerpt likely indicates that the script threads were started after the fuzz init function returned. Similarly, for the scheduler thread, it would look like: ```diff ... 227| 0| m_node.scheduler = std::make_unique<CScheduler>(); - 228| 1| m_node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { m_node.scheduler->serviceQueue(); }); + 228| 0| m_node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { m_node.scheduler->serviceQueue(); }); 229| 0| m_node.validation_signals = ... ``` ACKs for top commit: Prabhat1308: re-ACK [`fa51310`](bitcoin@fa51310) hodlinator: re-ACK fa51310 janb84: Re-ACK [fa51310](bitcoin@fa51310) Tree-SHA512: 1a935eb19da98c7c3810b8bcc5287e5649ffb55bf50ab78c414a424fef8e703839291bb24040a552c49274a4a0292910a00359bdff72fa29a4f53ad36d7a8720
2 parents 40de191 + fa51310 commit 772996a

File tree

3 files changed

+89
-30
lines changed

3 files changed

+89
-30
lines changed

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

Lines changed: 73 additions & 24 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,14 @@ 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");
111+
if using_libfuzzer {
112+
println!("Warning: The fuzz executable was compiled with libFuzzer as sanitizer.");
113+
println!("This tool may be tripped by libFuzzer misbehavior.");
114+
println!("It is recommended to compile without libFuzzer.");
115+
}
101116
let corpus_dir = corpora_dir.join(fuzz_target);
102117
let mut entries = read_dir(&corpus_dir)
103118
.map_err(|err| {
@@ -110,10 +125,12 @@ fn deterministic_coverage(
110125
.map(|entry| entry.expect("IO error"))
111126
.collect::<Vec<_>>();
112127
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"));
115-
if !{
116-
{
128+
let run_single = |run_id: char, entry: &Path, thread_id: usize| -> Result<PathBuf, AppError> {
129+
let cov_txt_path = build_dir.join(format!("fuzz_det_cov.show.t{thread_id}.{run_id}.txt"));
130+
let profraw_file = build_dir.join(format!("fuzz_det_cov.t{thread_id}.{run_id}.profraw"));
131+
let profdata_file = build_dir.join(format!("fuzz_det_cov.t{thread_id}.{run_id}.profdata"));
132+
{
133+
let output = {
117134
let mut cmd = Command::new(fuzz_exe);
118135
if using_libfuzzer {
119136
cmd.arg("-runs=1");
@@ -123,11 +140,15 @@ fn deterministic_coverage(
123140
.env("LLVM_PROFILE_FILE", &profraw_file)
124141
.env("FUZZ", fuzz_target)
125142
.arg(entry)
126-
.status()
127-
.map_err(|e| format!("fuzz failed with {e}"))?
128-
.success()
129-
} {
130-
Err("fuzz failed".to_string())?;
143+
.output()
144+
.map_err(|e| format!("fuzz failed: {e}"))?;
145+
if !output.status.success() {
146+
Err(format!(
147+
"fuzz failed!\nstdout:\n{}\nstderr:\n{}\n",
148+
String::from_utf8_lossy(&output.stdout),
149+
String::from_utf8_lossy(&output.stderr)
150+
))?;
151+
}
131152
}
132153
if !Command::new(LLVM_PROFDATA)
133154
.arg("merge")
@@ -149,6 +170,8 @@ fn deterministic_coverage(
149170
"--show-line-counts-or-regions",
150171
"--show-branches=count",
151172
"--show-expansions",
173+
"--show-instantiation-summary",
174+
"-Xdemangler=llvm-cxxfilt",
152175
&format!("--instr-profile={}", profdata_file.display()),
153176
])
154177
.arg(fuzz_exe)
@@ -187,20 +210,46 @@ The coverage was not deterministic between runs.
187210
//
188211
// Also, This can catch issues where several fuzz inputs are non-deterministic, but the sum of
189212
// 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 {
213+
println!(
214+
"Check each fuzz input individually ... ({} inputs with parallelism {par})",
215+
entries.len()
216+
);
217+
let check_individual = |entry: &DirEntry, thread_id: usize| -> AppResult {
192218
let entry = entry.path();
193219
if !entry.is_file() {
194220
Err(format!("{} should be a file", entry.display()))?;
195221
}
196-
let cov_txt_base = run_single(0, &entry)?;
197-
let cov_txt_repeat = run_single(1, &entry)?;
222+
let cov_txt_base = run_single('a', &entry, thread_id)?;
223+
let cov_txt_repeat = run_single('b', &entry, thread_id)?;
198224
check_diff(
199225
&cov_txt_base,
200226
&cov_txt_repeat,
201227
&format!("The fuzz target input was {}.", entry.display()),
202228
)?;
203-
}
229+
Ok(())
230+
};
231+
thread::scope(|s| -> AppResult {
232+
let mut handles = VecDeque::with_capacity(par);
233+
let mut res = Ok(());
234+
for (i, entry) in entries.iter().enumerate() {
235+
println!("[{}/{}]", i + 1, entries.len());
236+
handles.push_back(s.spawn(move || check_individual(entry, i % par)));
237+
while handles.len() >= par || i == (entries.len() - 1) || res.is_err() {
238+
if let Some(th) = handles.pop_front() {
239+
let thread_result = match th.join() {
240+
Err(_e) => Err("A scoped thread panicked".to_string()),
241+
Ok(r) => r,
242+
};
243+
if thread_result.is_err() {
244+
res = thread_result;
245+
}
246+
} else {
247+
return res;
248+
}
249+
}
250+
}
251+
res
252+
})?;
204253
// Finally, check that running over all fuzz inputs in one process is deterministic as well.
205254
// This can catch issues where mutable global state is leaked from one fuzz input execution to
206255
// the next.
@@ -209,23 +258,23 @@ The coverage was not deterministic between runs.
209258
if !corpus_dir.is_dir() {
210259
Err(format!("{} should be a folder", corpus_dir.display()))?;
211260
}
212-
let cov_txt_base = run_single(0, &corpus_dir)?;
213-
let cov_txt_repeat = run_single(1, &corpus_dir)?;
261+
let cov_txt_base = run_single('a', &corpus_dir, 0)?;
262+
let cov_txt_repeat = run_single('b', &corpus_dir, 0)?;
214263
check_diff(
215264
&cov_txt_base,
216265
&cov_txt_repeat,
217266
&format!("All fuzz inputs in {} were used.", corpus_dir.display()),
218267
)?;
219268
}
220-
println!("Coverage test passed for {fuzz_target}.");
269+
println!("Coverage test passed for {fuzz_target}.");
221270
Ok(())
222271
}
223272

224273
fn main() -> ExitCode {
225274
match app() {
226275
Ok(()) => ExitCode::SUCCESS,
227276
Err(err) => {
228-
eprintln!("{}", err);
277+
eprintln!("⚠️\n{}", err);
229278
ExitCode::FAILURE
230279
}
231280
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn app() -> AppResult {
7171
fn deterministic_coverage(build_dir: &Path, test_exe: &Path, filter: &str) -> AppResult {
7272
let profraw_file = build_dir.join("test_det_cov.profraw");
7373
let profdata_file = build_dir.join("test_det_cov.profdata");
74-
let run_single = |run_id: u8| -> Result<PathBuf, AppError> {
74+
let run_single = |run_id: char| -> Result<PathBuf, AppError> {
7575
println!("Run with id {run_id}");
7676
let cov_txt_path = build_dir.join(format!("test_det_cov.show.{run_id}.txt"));
7777
if !Command::new(test_exe)
@@ -104,6 +104,8 @@ fn deterministic_coverage(build_dir: &Path, test_exe: &Path, filter: &str) -> Ap
104104
"--show-line-counts-or-regions",
105105
"--show-branches=count",
106106
"--show-expansions",
107+
"--show-instantiation-summary",
108+
"-Xdemangler=llvm-cxxfilt",
107109
&format!("--instr-profile={}", profdata_file.display()),
108110
])
109111
.arg(test_exe)
@@ -129,18 +131,18 @@ fn deterministic_coverage(build_dir: &Path, test_exe: &Path, filter: &str) -> Ap
129131
}
130132
Ok(())
131133
};
132-
let r0 = run_single(0)?;
133-
let r1 = run_single(1)?;
134+
let r0 = run_single('a')?;
135+
let r1 = run_single('b')?;
134136
check_diff(&r0, &r1)?;
135-
println!("The coverage was deterministic across two runs.");
137+
println!("The coverage was deterministic across two runs.");
136138
Ok(())
137139
}
138140

139141
fn main() -> ExitCode {
140142
match app() {
141143
Ok(()) => ExitCode::SUCCESS,
142144
Err(err) => {
143-
eprintln!("{}", err);
145+
eprintln!("⚠️\n{}", err);
144146
ExitCode::FAILURE
145147
}
146148
}

src/test/util/setup_common.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include <walletinitinterface.h>
6161

6262
#include <algorithm>
63+
#include <future>
6364
#include <functional>
6465
#include <stdexcept>
6566

@@ -230,6 +231,12 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, TestOpts opts)
230231
// Use synchronous task runner while fuzzing to avoid non-determinism
231232
G_FUZZING ? std::make_unique<ValidationSignals>(std::make_unique<util::ImmediateTaskRunner>()) :
232233
std::make_unique<ValidationSignals>(std::make_unique<SerialTaskRunner>(*m_node.scheduler));
234+
{
235+
// Ensure deterministic coverage by waiting for m_service_thread to be running
236+
std::promise<void> promise;
237+
m_node.scheduler->scheduleFromNow([&promise] { promise.set_value(); }, 0ms);
238+
promise.get_future().wait();
239+
}
233240
}
234241

235242
bilingual_str error{};
@@ -247,7 +254,8 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, TestOpts opts)
247254
.check_block_index = 1,
248255
.notifications = *m_node.notifications,
249256
.signals = m_node.validation_signals.get(),
250-
.worker_threads_num = 2,
257+
// Use no worker threads while fuzzing to avoid non-determinism
258+
.worker_threads_num = G_FUZZING ? 0 : 2,
251259
};
252260
if (opts.min_validation_cache) {
253261
chainman_opts.script_execution_cache_bytes = 0;

0 commit comments

Comments
 (0)