Skip to content

fuzz: several improvements to scriptpubkeyman harness - #34969

Open
brunoerg wants to merge 7 commits into
bitcoin:masterfrom
brunoerg:2026-03-fuzz-spkm-improvements
Open

brunoerg wants to merge 7 commits into
bitcoin:masterfrom
brunoerg:2026-03-fuzz-spkm-improvements

Conversation

@brunoerg

Copy link
Copy Markdown
Contributor

The scriptpubkeyman harness is slow - I'm getting under 40 exec/s on my personal server. This PR solves the following issues that may be affecting the performance:

  • There are redundant IsMine calls
  • ConsumeCoins always create up to 10'000 coins which may cause slowness.
  • There are some expensive ops in a loop - e.g. One of the slow units I got on my server is about a repeated sequence of GetNewDestination calls (see flamegraph below).
  • We're always calling GetDescriptorString unconditionally. It looks like an inofensive function but this is extremely costly due to calls to ToNormalizedString/ToPrivateString functions that touch key operations.
  • Not related to slowness, but there is a bug on harness because we are aborting the execution when we cannot update the wallet descriptor, we can just skip and continue.

spkm-updated

@DrahtBot

DrahtBot commented Mar 31, 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
ACK pablomartin4btc

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:

  • #35444 (wallet: make descriptor SPKM mutex non-recursive by w0xlt)

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.

@brunoerg brunoerg mentioned this pull request Mar 31, 2026
11 of 14 tasks
@andrewtoth

Copy link
Copy Markdown
Contributor

Getting decent throughput when running on my laptop:

$ FUZZ=scriptpubkeyman ./build_fuzz_nosan/bin/fuzz ../qa-assets/fuzz_corpora/scriptpubkeyman/
...
#3326974	REDUCE cov: 14175 ft: 84867 corp: 5289/37Mb lim: 998620 exec/s: 1319 rss: 262Mb L: 15776/882699 MS: 1 EraseBytes-
#3327186	REDUCE cov: 14175 ft: 84867 corp: 5289/37Mb lim: 998620 exec/s: 1319 rss: 262Mb L: 961/882699 MS: 2 ChangeBit-EraseBytes-
#3328687	REDUCE cov: 14175 ft: 84867 corp: 5289/37Mb lim: 998620 exec/s: 1319 rss: 262Mb L: 1249/882699 MS: 1 EraseBytes-
...

How is your server configured? Do you see a big speedup after applying this patch?

@brunoerg

brunoerg commented Apr 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Getting decent throughput when running on my laptop:

What are the specs? How did you build it? Building with --preset=libfuzzer on my Ubuntu machine (AMD Ryzen 9 7900 / 32GB RAM), I get around 20 exec/s. With this PR, on same machine, I could reach similar coverage from master with an avg of 150 exec/s.

@andrewtoth

Copy link
Copy Markdown
Contributor

I'm on a i9-14900 with 96 GB RAM.

I was running with only fuzzer sanitizer. When --preset=libfuzzer that uses undefined,address,fuzzer. With those I get a lot slower. It started at at 39 but after a few minutes was up to 128 exec/s. I checked out this PR and then it started at 128 and after a few minutes was up to 385 exec/s. Not bad!

Running with only fuzzer sanitizer on this branch however didn't seem to speed up it was around 1100 exec/s.

@brunoerg

brunoerg commented Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

I'm on a i9-14900 with 96 GB RAM.

I was running with only fuzzer sanitizer. When --preset=libfuzzer that uses undefined,address,fuzzer. With those I get a lot slower. It started at at 39 but after a few minutes was up to 128 exec/s. I checked out this PR and then it started at 128 and after a few minutes was up to 385 exec/s. Not bad!

Running with only fuzzer sanitizer on this branch however didn't seem to speed up it was around 1100 exec/s.

Ah, got it. This is expected, because ASan instruments most memory accesses in instrumented code and tracks heap allocations, while UBSan adds runtime checks for selected undefined behaviors. So operations like GetScriptPubKeys(), ConsumeCoins(), and some string operations, such as GetDescriptorString() can become more expensive under these sanitizers.

@brunoerg
brunoerg force-pushed the 2026-03-fuzz-spkm-improvements branch from 883cf5a to 32b717e Compare April 13, 2026 13:46
@brunoerg
brunoerg force-pushed the 2026-03-fuzz-spkm-improvements branch from 32b717e to 3964b48 Compare April 30, 2026 18:43
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/25183113515/job/73833297823
LLM reason (✨ experimental): CI failed because the IWYU include/format check for src/util/feefrac.h generated a failure (^^^ ⚠️ Failure generated from IWYU).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@brunoerg
brunoerg force-pushed the 2026-03-fuzz-spkm-improvements branch from 3964b48 to 5aabf8b Compare April 30, 2026 20:03
@brunoerg
brunoerg force-pushed the 2026-03-fuzz-spkm-improvements branch from 5aabf8b to 40786bb Compare May 5, 2026 14:42
@brunoerg

brunoerg commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

Rebased

@ekzyis

ekzyis commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

Building with --preset=libfuzzer on my Ubuntu machine (AMD Ryzen 9 7900 / 32GB RAM), I get around 20 exec/s

I get around 2000 exec/s on master (4c99ed1) and 40786bb with --preset=libfuzzer.

I also fuzzed the scriptpubkeyman target for 10 minutes on both commits, and got a similar amount of runs:

$ tail -1 fuzz-scriptpubkeyman-4c99ed1076.txt
Done 1191929 runs in 601 second(s)
$ tail -1 fuzz-scriptpubkeyman-40786bb2f8.txt
Done 1195257 runs in 601 second(s)

Intel(R) Xeon(R) CPU E3-1275 v6 @ 4.20GHz with 64GB RAM

I thought maybe the latest changes to the harness since your comments changed performance:

$ git log --oneline --since=2026-04-01 master -- src/wallet/test/fuzz/scriptpubkeyman.cpp
c6f225c757c Merge bitcoin/bitcoin#28333: wallet: Construct ScriptPubKeyMans with all data rather than loaded progressively
c568624ff29 psbt: Return std::optional from PrecomputePSBTData
c01c7f068c5 psbt: Remove default constructor
6538f691357 fuzz: Skip adding descriptor to wallet if it cannot be expanded
0301c758ea0 wallet migration, fuzz: Migrate hd seed once
dc4a5d1270f refactor: use PSBTFillOptions for filling and signing

I checked out c6f225c and c6f225c^. Same performance with around 2000 exec/s:

$ tail -1 fuzz-scriptpubkeyman-5486ef8cc2.txt
Done 1198053 runs in 601 second(s)
$ tail -1 fuzz-scriptpubkeyman-c6f225c757.txt
Done 1124156 runs in 601 second(s)

So not sure where the low exec/s on your machines are coming from.

@brunoerg

Copy link
Copy Markdown
Contributor Author

I get around 2000 exec/s on master (4c99ed1) and 40786bb with --preset=libfuzzer.

Running from our corpus?

@ekzyis

ekzyis commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Running from our corpus?

Oh, no. With the scriptpubkeyman corpus from qa-assets, I get about 200–300 exec/s on the same machine. Thanks, this clears up my confusion! Sorry for the noise

@brunoerg
brunoerg force-pushed the 2026-03-fuzz-spkm-improvements branch from 40786bb to 08499b1 Compare July 15, 2026 15:38
@brunoerg

Copy link
Copy Markdown
Contributor Author

Rebased

@pablomartin4btc pablomartin4btc 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.

Concept ACK

A real improvement — the early return bug fix and moving expensive ops (GetDescriptorString, GetNewDestination) out of the hot path are the right calls.

Left a comment inline on the assertion coverage (not blocking).

Avoid calling IsMine for every single spkm from
GetScriptPubKeys(). Also, expands `MarkUnusedAddresses`
which covers it. In a later commit, we introduce the
assertion back in a better way.
This new parameter is used to control the max number of coins
this function might create. It allows us to create less than
10'000 coins and then reduce the cost.
GetDescriptorString calls ToNormalizedString or ToPrivateString,
both of which rebuild the full descriptor string. This is expensive.
@brunoerg
brunoerg force-pushed the 2026-03-fuzz-spkm-improvements branch from 08499b1 to 37c2304 Compare August 19, 2026 13:03
Assert both directions of the invariant, using the `spks` set that is
now computed only once:

- every script from GetScriptPubKeys() is IsMine;
- every IsMine script is in GetScriptPubKeys().

The latter is asserted before MarkUnusedAddresses(), which may TopUp()
and add scripts that are not part of the already computed `spks` set.

@pablomartin4btc pablomartin4btc 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.

ACK 0b0df80

Since my last review, only a rebase plus the last commit, which now asserts the full IsMine/GetScriptPubKeys invariant in both directions using the precomputed spks set, thanks for taking that! Also nice catch on asserting the inverse direction before MarkUnusedAddresses(), since it may TopUp().

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.

6 participants