Skip to content

fuzz: Corpus sharding in parallel runs - #36121

Closed
maflcko wants to merge 2 commits into
bitcoin:masterfrom
maflcko:2608-fuzz-sharding
Closed

maflcko wants to merge 2 commits into
bitcoin:masterfrom
maflcko:2608-fuzz-sharding

Conversation

@maflcko

@maflcko maflcko commented Aug 29, 2026

Copy link
Copy Markdown
Member

Currently, the fuzz runner accepts a --par option to schedule fuzz runs in parallel. This is fine. However, when only a single target is selected, --par will not speed up the run. Moreover, when multiple targets are selected, the run-time of the longest target dominates.

Fix both issues by splitting the corpus to into --par equal-sized shards by default. The sharding can be disabled, if needed.

Can be tested e.g. by running a single target:

time ./bld-cmake/test/fuzz/test_runner.py --par 1  -l DEBUG ./qa-assets/fuzz_corpora/ utxo_snapshot  # slow
time ./bld-cmake/test/fuzz/test_runner.py --par 99 -l DEBUG ./qa-assets/fuzz_corpora/ utxo_snapshot  # fast

@DrahtBot

DrahtBot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

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

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36121.

Reviews

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

Type Reviewers
Concept ACK jeanpablojp

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36308 (test: Set tsan enable_adaptive_delay=1 by default by maflcko)
  • #35417 (ci: isolate container test networks by willcl-ark)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@maflcko

maflcko commented Aug 29, 2026 •

Copy link
Copy Markdown
Member Author

@jeanpablojp jeanpablojp left a comment

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.

Concept ACK

I had a look at the logs. Looks like on macOS the pool was already saturated. Summing the process times and dividing by --par gives 613s there, against 200s for the slowest single target, so there was no tail to cut. On Windows and MSan it's the other way round, and that is where the win comes from.

Almost all of the macOS side comes from one target. utxo_total_supply builds a ChainTestingSetup inside the target body, so a datadir per input, and three shards of 680 take around 240s each against 200s for all 2040 in one process. Same on Windows.

Is it worth gating this on something, or does the win on the other jobs already outweigh it?

Comment thread test/fuzz/test_runner.py
help='How many targets to merge or execute in parallel.',
)
parser.add_argument(
'--corpus-shards',

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.

nit: --help ends up with two defaults, the one in the help string and a (default: None) that ArgumentDefaultsHelpFormatter appends.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, saw that too. Not sure how to fix. I guess it is fine to leave as-is.

Comment thread test/fuzz/test_runner.py Outdated
if using_libfuzzer:
if empty_min_time and empty_dir:
args += [f"-max_total_time={empty_min_time}"]
with tempfile.TemporaryDirectory(dir=corpus.parent, prefix='.fuzz-shards-') as shard_root:

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.

This gets created before we know whether there will be any sharding, so with a read-only parent the run dies in mkdtemp, and --corpus-shards 1 doesn't avoid it. It also survives a SIGTERM, left next to the corpus.

And since the with wraps the collection loop too, the removal only happens after the last job, with everything else idle. 31s on macOS.

@maflcko maflcko Sep 1, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

read-only parent should be rare, but I think I fixed this in the latest push by having a temp dir for each job.

SIGTERM will probably still leave them around.

The creation/removal is now interleaved, but I don't expect this to improve the runtime, unless there is over-subscription, but then there could be even more storage thrashing...

Comment thread test/fuzz/test_runner.py Outdated
corpus_files = sorted(path for path in corpus_path.iterdir() if path.is_file())
empty_dir = not corpus_files

if corpus_shards > 1 and len(corpus_files) > 1:

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.

This hits every target that has a corpus, none has fewer than three inputs. cmpctblock sometimes does the same kind of per-input work through ResetChainmanAndMempool.

The hard links are one per corpus input, so that part doesn't shrink with more workers. On macOS that takes 155s before the first job gets collected.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm, I guess this is a combination of some OSs not having a tmpfs at all and the GHA VMs having a slow storage. :(

Comment thread test/fuzz/test_runner.py Outdated
logging.info(e.stdout)
if e.stderr:
logging.info(e.stderr)
logging.info(f"⚠️ Failure generated from target with exit code {e.returncode}: {result.args}")

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.

nit: this path is already gone by the time anyone reads the log, it's inside the shard dir. The file name survives, the path doesn't.

And with the sys.exit(1) in here, the dir disappears under the shards that are still running, so on the standalone builds they die on the read_file Assert instead of on the input that actually broke.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm, the file disappearing should be fine, because the basename of the file (sha1 hash) should be important/sufficient.

The read_file assertions aren't great. I guess this is fixed by having per-job tmp dirs for the shards.

Comment thread test/fuzz/test_runner.py Outdated
if using_libfuzzer:
done_stat = [l for l in output.splitlines() if "DONE" in l]
assert len(done_stat) == 1
stats.append((target, done_stat[0]))

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.

Summary goes from 237 lines to 3754 on your asan run, and cov: becomes per process. coins_view_db reports sixteen values between 12033 and 13709 where master reports 14509, and since it's a union there's no way to put it back together. So you can't compare coverage across a change any more. Could the shards be folded back into one line per target?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think it is possible to recover the coverage from the shards. I can only see compiling with coverage instrumentation and then using that. This could possibly also help with bitcoin-core/qa-assets#166

To better utilize the given --par option, split each corpus into shards by default.

The diff can be reviewed via --ignore-all-space
@maflcko

maflcko commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

I had a look at the logs. Looks like on macOS the pool was already saturated. Summing the process times and dividing by --par gives 613s there, against 200s for the slowest single target, so there was no tail to cut.

Hmm, if the slowest single target happened to be scheduled toward the end, there would be a tail to cut, no?

Almost all of the macOS side comes from one target. utxo_total_supply builds a ChainTestingSetup inside the target body, so a datadir per input, and three shards of 680 take around 240s each against 200s for all 2040 in one process. Same on Windows.

Hmm, I guess this means the bottleneck is storage IO and not CPU, so this pull request won't help and may even make it worse due to storage thrashing.

In theory, all of the fuzzing should be in a tmpfs (default on modern Linux), but neither macOS nor Windows have that option :(

Is it worth gating this on something, or does the win on the other jobs already outweigh it?

in theory, the macOS config (and possibly Windows) could disable it, but if this gets too complicated, I may close this pull.

@maflcko
maflcko marked this pull request as draft September 1, 2026 10:11
@maflcko

maflcko commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

Ok, so tmpfs helped a bit on macos, but still not enough. On Linux, it didn't really make a difference, it seems.

@jeanpablojp

jeanpablojp commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Hmm, if the slowest single target happened to be scheduled toward the end, there would be a tail to cut, no?

You're right. utxo_total_supply is the last to finish in all 20 master runs I checked, and it runs for the last 86 to 214s of them, so my sentence was wrong.

Ok, so tmpfs helped a bit on macos, but still not enough.

macOS on master runs anywhere from 632 to 976s and this head is 767s, so it is inside what master does on its own. I guess two runs can't really separate that.

@maflcko

maflcko commented Oct 1, 2026

Copy link
Copy Markdown
Member Author

Closing for now. Not sure if this is worth it. IIRC I created this after @willcl-ark contemplated how the MSan fuzz CI could be speed up.

It would be a bit faster with this pull, but I am not sure if the code review and complexity is worth it

@maflcko maflcko closed this Oct 1, 2026
@maflcko
maflcko deleted the 2608-fuzz-sharding branch October 1, 2026 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants