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

Open
HowHsu wants to merge 1 commit from det-fuzz-cov-profdata-fastpath into master AGit
HowHsu commented 2026-06-26 11:55:11 +00:00 (Migrated from github.com)

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.

## 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 commented 2026-06-26 11:55:18 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/35608 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35608.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35608#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Stale ACK | [jeanpablojp](https://github.com/bitcoin/bitcoin/pull/35608#issuecomment-5340967884) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
jeanpablojp commented 2026-08-19 10:39:52 +00:00 (Migrated from github.com)

tACK 87ecdf2cf0bd3aa222b744a59ceaa62c2240b725

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.

tACK 87ecdf2cf0bd3aa222b744a59ceaa62c2240b725 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.
maflcko (Migrated from github.com) approved these changes 2026-09-01 19:24:44 +00:00
maflcko (Migrated from github.com) left a comment

lgtm, left a nit to slim this down a bit

lgtm, left a nit to slim this down a bit
maflcko (Migrated from github.com) commented 2026-09-01 19:24:30 +00:00

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?

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): ```diff 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?
jeanpablojp (Migrated from github.com) reviewed 2026-09-02 09:04:25 +00:00
jeanpablojp (Migrated from github.com) commented 2026-09-02 09:04:25 +00:00

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?

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?
maflcko (Migrated from github.com) reviewed 2026-09-02 09:57:02 +00:00
maflcko (Migrated from github.com) commented 2026-09-02 09:57:02 +00:00

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.

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.
HowHsu (Migrated from github.com) reviewed 2026-09-03 13:39:53 +00:00
HowHsu (Migrated from github.com) commented 2026-09-03 13:39:53 +00:00

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

Updated, thanks. Kept the `profdata_eq`, since I feel it is very good for beginners (like me) to understand the code logic.
DrahtBot commented 2026-09-23 18:37:20 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
maflcko (Migrated from github.com) reviewed 2026-09-24 11:57:59 +00:00
maflcko (Migrated from github.com) commented 2026-09-24 11:57:59 +00:00

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=") {
nit: Needs rebase anyway, and while touching this, could make it a proper named arg in a new commit: ```diff 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=") {
sedited commented 2026-11-05 17:07:52 +00:00 (Migrated from github.com)

@HowHsu are you still working on this?

@HowHsu are you still working on this?
HowHsu (Migrated from github.com) reviewed 2026-11-21 06:12:20 +00:00
HowHsu (Migrated from github.com) commented 2026-11-21 06:12:20 +00:00

Updated

Updated
First-time contributor

🔵 Suggestion · The fast path also skips llvm-cov compatibility validation

When all profile comparisons match, success no longer establishes that llvm-cov show can render the coverage, although no configuration where merging succeeds but rendering fails was reproduced.

Location: contrib/devtools/deterministic-fuzz-coverage/src/main.rs:257 (head).

Review coverage was incomplete.


Reviewed: 02275f6a7070 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:3705f06c6a27c475c77f2533c4d016ac03c829c839b7b538bba5e4768ccf54f1:1ed10c4979af6fc548d6b8d4534d505e02e84ac4b0aa4f8633555b2ba75119e8 --> <!-- ralph-base:004cfb21a51db563b92cde968f249086ef58df7c --> <!-- ralph-head:02275f6a7070e5d4c52a9ce198ce02208903d9dd --> <!-- ralph-case:a547c99dae01a6d349a14b490c43096903503bf5a61a4ca37795d02e992f3884 --> **🔵 Suggestion · The fast path also skips `llvm-cov` compatibility validation** When all profile comparisons match, success no longer establishes that `llvm-cov show` can render the coverage, although no configuration where merging succeeds but rendering fails was reproduced. Location: [`contrib/devtools/deterministic-fuzz-coverage/src/main.rs:257`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/02275f6a7070e5d4c52a9ce198ce02208903d9dd/contrib/devtools/deterministic-fuzz-coverage/src/main.rs#L257>) (head). Review coverage was incomplete. <!-- Review tier: standard --> --- Reviewed: [`02275f6a7070`](<https://git.fish.foo/bitcoin/bitcoin/commit/02275f6a7070e5d4c52a9ce198ce02208903d9dd>) · [Review report](<https://review.fish.foo/report/35608-02275f6a7070e5d4c52a9ce198ce02208903d9dd-2912-1.html>) · [Previous reports](<https://review.fish.foo/traces/35608/>) [permalink](<https://review.fish.foo/report/35608-02275f6a7070e5d4c52a9ce198ce02208903d9dd-2912-1.html>)
HowHsu commented 2026-11-21 06:51:44 +00:00 (Migrated from github.com)

@HowHsu are you still working on this?

Sorry for delay, it's updated.

> @HowHsu are you still working on this? Sorry for delay, it's updated.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!35608
No description provided.