Skip to content

contrib: Skip llvm-cov rendering for deterministic fuzz inputs - #35608

Open
HowHsu wants to merge 1 commit into
bitcoin:masterfrom
HowHsu:det-fuzz-cov-profdata-fastpath
Open

HowHsu wants to merge 1 commit into
bitcoin:masterfrom
HowHsu:det-fuzz-cov-profdata-fastpath

Conversation

@HowHsu

@HowHsu HowHsu commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Summary

deterministic-fuzz-coverage rendered a full llvm-cov show report for every
fuzz input on both runs and diffed the two reports, even though almost all
inputs are deterministic. llvm-cov show is by far the most expensive step, so
the vast majority of that work produced two identical reports that were
immediately discarded.

Compare the merged .profdata first: identical counters are a sufficient
condition for deterministic coverage, so when the two profdata files match the
llvm-cov show rendering is skipped entirely. Only when they differ does it
fall back to rendering and diffing the reports to confirm and locate the
difference.

The pass/fail result is unchanged: identical profdata implies identical
counters, which implies identical llvm-cov show output (the report is a
deterministic function of the profdata and the binary). Any profdata difference
falls back to the exact same llvm-cov diff as before.

Benchmark

standalone coverage build (-DBUILD_FOR_FUZZING=ON +
-fprofile-instr-generate -fcoverage-mapping, no sanitizer), par=4,
qa-assets corpora:

target inputs before after speedup
addition_overflow 112 32s 10s 3.2x
process_message 2386 719s 247s 2.9x

On process_message every input is deterministic, so all 2386 take the
profdata fast path and zero llvm-cov show reports are rendered, down from
2*2386 + 2 = 4774.

@DrahtBot

DrahtBot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Stale ACK jeanpablojp

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

No conflicts as of last run.

@HowHsu
HowHsu force-pushed the det-fuzz-cov-profdata-fastpath branch from f0bf343 to 87ecdf2 Compare June 26, 2026 12:16
@sedited
sedited requested a review from maflcko July 24, 2026 10:56
@jeanpablojp

Copy link
Copy Markdown
Contributor

tACK 87ecdf2

Ran both versions on qa-assets with the coverage build left over from my #35809 review with par=4. addition_overflow went from 5m49s to 31s and process_message from 1h50m to 25m48s, same PASS on both, and the new flow rendered zero reports. Identical .profdata would mean an identical report anyway; if they differ it falls back to the usual render and diff.

@sedited
sedited requested a review from marcofleon September 1, 2026 11:17

@maflcko maflcko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, left a nit to slim this down a bit

Comment on lines +298 to +302
let profdata_base = run_profile('a', &corpus_dir, 0)?;
let profdata_repeat = run_profile('b', &corpus_dir, 0)?;
if !profdata_eq(&profdata_base, &profdata_repeat)? {
let cov_txt_base = render_cov(&profdata_base, 'a', 0)?;
let cov_txt_repeat = render_cov(&profdata_repeat, 'b', 0)?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: There's now a lot of 'a' and 'b'. Maybe check_diff can just accept the &Path and then do everything itself. This would de-dupl a bunch of code. Something like this (untested):

 contrib/devtools/deterministic-fuzz-coverage/src/main.rs | 48 +++++++++++++++++++++---------------------------
 1 file changed, 21 insertions(+), 27 deletions(-)

diff --git a/contrib/devtools/deterministic-fuzz-coverage/src/main.rs b/contrib/devtools/deterministic-fuzz-coverage/src/main.rs
index d536cf1..5859d7e 100644
--- a/contrib/devtools/deterministic-fuzz-coverage/src/main.rs
+++ b/contrib/devtools/deterministic-fuzz-coverage/src/main.rs
@@ -216,7 +216,18 @@ fn deterministic_coverage(
         };
-    let check_diff = |a: &Path, b: &Path, err: &str| -> AppResult {
+    let check_diff = |entry: &Path, thread_id: usize, err: &str| -> AppResult {
+        let profdata_base = run_profile('a', entry, thread_id)?;
+        let profdata_repeat = run_profile('b', entry, thread_id)?;
+        // Fast path: identical counters mean the coverage is deterministic, so
+        // skip the expensive llvm-cov rendering entirely.
+        if profdata_eq(&profdata_base, &profdata_repeat)? {
+            return Ok(());
+        }
+        // Slow path: the profiles differ, so render both into human-readable
+        // reports and diff them to confirm and locate the difference.
+        let cov_txt_base = render_cov(&profdata_base, 'a', thread_id)?;
+        let cov_txt_repeat = render_cov(&profdata_repeat, 'b', thread_id)?;
         let same = Command::new(GIT)
             .args(["--no-pager", "diff", "--no-index"])
-            .arg(a)
-            .arg(b)
+            .arg(&cov_txt_base)
+            .arg(&cov_txt_repeat)
             .status()
@@ -249,16 +260,5 @@ The coverage was not deterministic between runs.
         }
-        let profdata_base = run_profile('a', &entry, thread_id)?;
-        let profdata_repeat = run_profile('b', &entry, thread_id)?;
-        // Fast path: identical counters mean the coverage is deterministic, so
-        // skip the expensive llvm-cov rendering entirely.
-        if profdata_eq(&profdata_base, &profdata_repeat)? {
-            return Ok(());
-        }
-        // Slow path: the profiles differ, so render both into human-readable
-        // reports and diff them to confirm and locate the difference.
-        let cov_txt_base = render_cov(&profdata_base, 'a', thread_id)?;
-        let cov_txt_repeat = render_cov(&profdata_repeat, 'b', thread_id)?;
         check_diff(
-            &cov_txt_base,
-            &cov_txt_repeat,
+            &entry,
+            thread_id,
             &format!("The fuzz target input was {}.", entry.display()),
@@ -297,13 +297,7 @@ The coverage was not deterministic between runs.
         }
-        let profdata_base = run_profile('a', &corpus_dir, 0)?;
-        let profdata_repeat = run_profile('b', &corpus_dir, 0)?;
-        if !profdata_eq(&profdata_base, &profdata_repeat)? {
-            let cov_txt_base = render_cov(&profdata_base, 'a', 0)?;
-            let cov_txt_repeat = render_cov(&profdata_repeat, 'b', 0)?;
-            check_diff(
-                &cov_txt_base,
-                &cov_txt_repeat,
-                &format!("All fuzz inputs in {} were used.", corpus_dir.display()),
-            )?;
-        }
+        check_diff(
+            &corpus_dir,
+            0,
+            &format!("All fuzz inputs in {} were used.", corpus_dir.display()),
+        )?;
     }

Possibly could also inline profdata_eq and only keep a small comment as it is only called once, to save some more lines?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: A build with -fprofile-instr-generate but no -fcoverage-mapping now prints Coverage test passed where master dies with llvm-cov show failed. Determinism is still checked either way, only the broken build slips by. Under your version, worth one run_profile and render_cov before the loop?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a dev-only script. If there is a "broken build" (because the dev ignored the docs), I think it is fine to not hold their hands with extra logic here. If needed, the error message can be adjusted, but other than that, I'd say the logic shouldn't be modified.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thanks. Kept the profdata_eq, since I feel it is very good for beginners (like me) to understand the code logic.

@HowHsu
HowHsu force-pushed the det-fuzz-cov-profdata-fastpath branch from 87ecdf2 to c237c9f Compare September 3, 2026 13:38

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Needs rebase anyway, and while touching this, could make it a proper named arg in a new commit:

diff --git a/contrib/devtools/README.md b/contrib/devtools/README.md
index 2fb3ba1..35d749d 100644
--- a/contrib/devtools/README.md
+++ b/contrib/devtools/README.md
@@ -25 +25 @@ before running the tool:
-cargo run --manifest-path ./contrib/devtools/deterministic-fuzz-coverage/Cargo.toml -- $PWD/build_dir $PWD/qa-assets/fuzz_corpora fuzz_target_name [parallelism] [--mode=<both|single|combined>]
+cargo run --manifest-path ./contrib/devtools/deterministic-fuzz-coverage/Cargo.toml -- $PWD/build_dir $PWD/qa-assets/fuzz_corpora fuzz_target_name [--parallelism=<parallelism>] [--mode=<both|single|combined>]
diff --git a/contrib/devtools/deterministic-fuzz-coverage/src/main.rs b/contrib/devtools/deterministic-fuzz-coverage/src/main.rs
index 9d06364..5685334 100644
--- a/contrib/devtools/deterministic-fuzz-coverage/src/main.rs
+++ b/contrib/devtools/deterministic-fuzz-coverage/src/main.rs
@@ -28 +28 @@ Error: {err}
-Usage: program ./build_dir ./qa-assets/fuzz_corpora fuzz_target_name [parallelism={DEFAULT_PAR}] [--mode=both]
+Usage: program ./build_dir ./qa-assets/fuzz_corpora fuzz_target_name [--parallelism={DEFAULT_PAR}] [--mode=both]
@@ -75,7 +75 @@ fn app() -> AppResult {
-    let par = match args.get(4) {
-        Some(s) => s
-            .parse::<usize>()
-            .map_err(|e| exit_help(&format!("Could not parse parallelism as usize ({s}): {e}")))?,
-        None => DEFAULT_PAR,
-    }
-    .max(1);
+    let mut par = DEFAULT_PAR;
@@ -85,2 +79,11 @@ fn app() -> AppResult {
-    for arg in args.iter().skip(5) {
-        if let Some(value) = arg.strip_prefix("--mode=") {
+    for arg in args.iter().skip(4) {
+        if let Some(value) = arg.strip_prefix("--parallelism=") {
+            par = value
+                .parse::<usize>()
+                .map_err(|e| {
+                    exit_help(&format!(
+                        "Could not parse parallelism as usize ({value}): {e}"
+                    ))
+                })?
+                .max(1);
+        } else if let Some(value) = arg.strip_prefix("--mode=") {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@sedited

sedited commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

@HowHsu are you still working on this?

deterministic-fuzz-coverage rendered a full `llvm-cov show` report for
every input on both runs and diffed the two reports. `llvm-cov show`
is by far the most expensive step, so almost all of that work produced
two identical reports that were immediately thrown away.

Compare the merged profdata first instead: identical counters are a
sufficient condition for deterministic coverage, so when the two
profdata files match we skip rendering entirely. Only when they differ
do we fall back to rendering and diffing the `llvm-cov show` reports to
render and locate the difference.

Co-authored-by: maflcko <[email protected]>
@HowHsu
HowHsu force-pushed the det-fuzz-cov-profdata-fastpath branch from c237c9f to 02275f6 Compare November 21, 2026 06:11
@HowHsu

HowHsu commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

@HowHsu are you still working on this?

Sorry for delay, it's updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants