Skip to content

fuzz: add a custom mutator for PSBT - #36631

Open
brunoerg wants to merge 2 commits into
bitcoin:masterfrom
brunoerg:2026-10-fuzz-psbt-custom-mutator
Open

brunoerg wants to merge 2 commits into
bitcoin:masterfrom
brunoerg:2026-10-fuzz-psbt-custom-mutator

Conversation

@brunoerg

@brunoerg brunoerg commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

The partially_signed_transaction_deserialize target feeds raw bytes to the PSBT parser. Byte-level mutations rarely produce well-formed key-value maps with valid keydata (pubkeys, xpubs, control blocks, DER signatures, etc), so most inputs are rejected early and the deeper per-field parsing is rarely reached.

This PR adds a custom mutator for PSBT which is ported from bitcoinfuzz (with some changes). The mutator splits the input into global/input/output maps of key-value records, applies the mutations and serializes the result again. The first commit is a preparation to allow targets to provide a custom mutator by adding an optional mutator field to
FuzzTargetOptions.

note: LLM assisted on porting the custom mutator.

@DrahtBot

DrahtBot commented Oct 23, 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.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #35911 (Warn on and add missing [[noreturn]] by fanquake)

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.

Comment thread src/test/fuzz/fuzz.cpp Resolved
@brunoerg
brunoerg marked this pull request as draft October 23, 2026 19:18
@brunoerg
brunoerg force-pushed the 2026-10-fuzz-psbt-custom-mutator branch from b44c41b to 6e20db3 Compare October 23, 2026 19:23
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task NetBSD Cross: https://github.com/bitcoin/bitcoin/actions/runs/40009584622/job/120606204787
LLM reason (✨ experimental): CI failed during the fuzz-test build because g_custom_mutator in src/test/fuzz/fuzz.cpp is set but unused, triggering -Werror.

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-10-fuzz-psbt-custom-mutator branch from 6e20db3 to f34b7ff Compare October 23, 2026 19:57
@brunoerg
brunoerg marked this pull request as ready for review October 26, 2026 09:48
@brunoerg
brunoerg force-pushed the 2026-10-fuzz-psbt-custom-mutator branch from f34b7ff to 3f6be48 Compare October 28, 2026 17:10
@brunoerg

Copy link
Copy Markdown
Contributor Author

Rebased

Comment thread src/test/fuzz/fuzz.cpp Resolved
// Leave room to grow, so keydata of other lengths can be reached.
const size_t old_size{rec.key.size()};
rec.key.resize(old_size + 16);
rec.key.resize(FuzzDefaultMutate(rec.key.data(), old_size, rec.key.size()));

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.

The key gets erased if FuzzDefaultMutate returns 0 (which is the case for LLVMFuzzerMutate in afl++) . Is that expected to happen in this branch for key mutations?

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.

this would only happen with AFL++'s weak stub when PROVIDE_FUZZ_CUSTOM_MUTATOR is defined, isn't it?

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.

Ah yes, that is my understanding as well after seeing #if defined(PROVIDE_FUZZ_CUSTOM_MUTATOR) at fuzz.cpp:224.

@brunoerg
brunoerg marked this pull request as draft October 30, 2026 01:23
@brunoerg
brunoerg force-pushed the 2026-10-fuzz-psbt-custom-mutator branch from 3f6be48 to 7e7cbba Compare November 2, 2026 13:15
@brunoerg
brunoerg marked this pull request as ready for review November 2, 2026 13:16

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

Partial review. Left some nits and a remark wrt honggfuzz + suggestion from Claude. Will look into mutator effectiveness next.

Comment thread src/test/fuzz/fuzz.cpp Resolved
Comment thread CMakeLists.txt Resolved
Comment thread CMakeLists.txt Resolved
Add an optional `mutator` field to FuzzTargetOptions. When the fuzz
binary is linked against libFuzzer, LLVMFuzzerCustomMutator dispatches
to the selected target's mutator, falling back to LLVMFuzzerMutate for
targets that don't set one.

Custom mutators can call FuzzDefaultMutate() to apply libFuzzer's
default mutations to parts of the input.

libFuzzer disables -len_control by default whenever
LLVMFuzzerCustomMutator is linked in, which would otherwise affect every
target. Targets without a custom mutator get -len_control=100 restored,
unless it was set explicitly.

This is only enabled when the real libFuzzer runtime is linked, detected
at configure time, so other engines are unaffected.
Add a custom mutator for raw PSBTs, ported from bitcoinfuzz, and use it
for the partially_signed_transaction_deserialize target.
@brunoerg
brunoerg force-pushed the 2026-10-fuzz-psbt-custom-mutator branch from 7e7cbba to 93dc3cf Compare November 4, 2026 14:37
@brunoerg

brunoerg commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Force-pushed:

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