Skip to content

fuzz: add ipc round-trip fuzz target - #35118

Merged
sedited merged 2 commits into
bitcoin:masterfrom
enochaz:fuzz-ipc-initial
Jul 2, 2026
Merged

sedited merged 2 commits into
bitcoin:masterfrom
enochaz:fuzz-ipc-initial

Conversation

@enochaz

@enochaz enochaz commented Apr 20, 2026 •

Copy link
Copy Markdown
Contributor

As discussed in #23015, this PR adds an IPC fuzz target to exercise the Cap'n Proto/libmultiprocess serialization bridge using an in-process two-way pipe and a reflected test interface.

It covers round-trip serialization for COutPoint, CScript, std::vector<uint8_t>, UniValue, and transactions, and exercises libmultiprocess proxy/server interaction. The target guarantees at least one IPC operation per input and is included by default when IPC is enabled in fuzz builds

Coverage report provided by marcofleon

@DrahtBot

DrahtBot commented Apr 20, 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/35118.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK marcofleon, sedited
Approach ACK dergoegge
Stale ACK ryanofsky

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.

@enochaz
enochaz force-pushed the fuzz-ipc-initial branch 2 times, most recently from c5a1192 to aa53fdf Compare April 20, 2026 08:48
@ViniciusCestarii

Copy link
Copy Markdown
Contributor

While testing this PR with cmake --preset=libfuzzer, I noticed the new ipc target never makes it into the fuzz binary (FUZZ=ipc build_fuzz/bin/fuzz reports No fuzz target compiled for ipc).

The cause is in the top-level CMakeLists.txt:234: when BUILD_FOR_FUZZING=ON, ENABLE_IPC is unconditionally forced OFF, which makes the if(ENABLE_IPC) guard in src/test/fuzz/CMakeLists.txt skip ipc.cpp. So the target added by this PR can't actually be exercised through the standard fuzz preset.

I believe the fix is to drop set(ENABLE_IPC OFF) from CMakeLists.txt:234.

@ViniciusCestarii

Copy link
Copy Markdown
Contributor

Three of the four methods (passOutPoint, passVectorUint8, passScript) are identity round-trips. add(a, b) is stronger because the server has to actually decode a and b to compute the sum and the client then checks against the original inputs.

With identity, the server never has to understand the value it received, so a broken deserialization can be re-serialized as the same garbage and still compare equal. A small deterministic transform on the server side would close that gap, e.g.:

  std::vector<uint8_t> passVectorUint8(std::vector<uint8_t> v) {
      std::reverse(v.begin(), v.end());
      return v;
  }
  CScript passScript(CScript s) { s << OP_NOP; return s; }
  COutPoint passOutPoint(COutPoint o) { return COutPoint{o.hash, o.n ^ 0xFFFFFFFFu}; }

and then comparing against the reproduced transform on the client just like add(a, b). This matches the "transform, reverse, or shift" idea from #23015.

@enochaz
enochaz force-pushed the fuzz-ipc-initial branch 2 times, most recently from a726d49 to fc26ea1 Compare April 21, 2026 03:56
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/24702970550/job/72250002067
LLM reason (✨ experimental): CI failed due to a lint error for trailing whitespace detected by the trailing_whitespace check.

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.

@enochaz
enochaz force-pushed the fuzz-ipc-initial branch 2 times, most recently from a04c31a to 580adc5 Compare April 21, 2026 04:34
@enochaz

enochaz commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review @ViniciusCestarii

While testing this PR with cmake --preset=libfuzzer, I noticed the new ipc target never makes it into the fuzz binary (FUZZ=ipc build_fuzz/bin/fuzz reports No fuzz target compiled for ipc).
I believe the fix is to drop set(ENABLE_IPC OFF) from CMakeLists.txt:234.

The fix is indeed to remove set(ENABLE_IPC OFF) from the BUILD_FOR_FUZZING block, that line was added in af4156a to work around missing capnp in fuzzing builds, but since add_libmultiprocess and add_subdirectory(ipc) are both gated behind ENABLE_IPC in src/CMakeLists.txt, users without capnp will still get ENABLE_IPC=OFF naturally with no configure error.

With identity, the server never has to understand the value it received, so a broken deserialization can be re-serialized as the same garbage and still compare equal. A small deterministic transform on the server side would close that gap, e.g.:

  std::vector<uint8_t> passVectorUint8(std::vector<uint8_t> v) {
      std::reverse(v.begin(), v.end());
      return v;
  }
  CScript passScript(CScript s) { s << OP_NOP; return s; }
  COutPoint passOutPoint(COutPoint o) { return COutPoint{o.hash, o.n ^ 0xFFFFFFFFu}; }

and then comparing against the reproduced transform on the client just like add(a, b). This matches the "transform, reverse, or shift" idea from #23015.

Good point. Updated the three identity methods to use deterministic transforms instead:

  • passOutPoint: XORs n with 0xFFFFFFFF
  • passVectorUint8: reverses the vector
  • passScript: appends OP_NOP

The client-side asserts now reproduce the same transform and compare against that

@enochaz
enochaz force-pushed the fuzz-ipc-initial branch 4 times, most recently from a46f70e to c1fcd53 Compare April 21, 2026 05:37
@enochaz enochaz changed the title fuzz: Add IPC round-trip fuzz target fuzz: add ipc round-trip fuzz target Apr 23, 2026
@enochaz

enochaz commented Apr 23, 2026

Copy link
Copy Markdown
Contributor Author

Updated the branch to keep the IPC fuzz target opt-in instead of enabling IPC for all fuzz builds.

The issue was that BUILD_FOR_FUZZING=ON intentionally disables IPC, so the new ipc target was not compiled even when using the standard libFuzzer preset. Simply dropping set(ENABLE_IPC OFF) would make cmake --preset=libfuzzer require IPC/libmultiprocess dependencies by default, which seems unnecessarily disruptive for existing fuzz builds.

The current approach preserves the existing default behavior:

  • cmake --preset=libfuzzer keeps IPC disabled.
  • cmake --preset=libfuzzer -DENABLE_IPC=ON builds the IPC fuzz target.

I also removed the unrelated fuzz target linking cleanup from this PR. The remaining diff is focused on making IPC fuzzing explicitly buildable and adding the initial ipc round-trip target.

@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task 32 bit ARM: https://github.com/bitcoin/bitcoin/actions/runs/24827161441/job/72666022727
LLM reason (✨ experimental): CI failed because the fuzz target build stopped with a missing include file (mp/proxy.capnp.h) causing a fatal compilation error.

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.

@DrahtBot
DrahtBot requested a review from ryanofsky June 27, 2026 10:49

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

Nice, good to see an IPC fuzz test. Here's the coverage after fuzzing for a while.

Left a question.

Comment thread src/ipc/test/fuzz/ipc.cpp
},
[&] {
std::vector<uint8_t> value = ConsumeRandomLengthByteVector<uint8_t>(fuzzed_data_provider, 512);
if (value.empty()) value.push_back(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.

I'm not quite seeing why we need this line. Is an empty vector not a case we should be covering?

@enochaz enochaz Jul 1, 2026 •

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 is a workaround to empty input, because of a libmultiprocess sanitizer issue

@enochaz enochaz Jul 1, 2026 •

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.

Removing this causes the UBSan job to find that the empty std::vector<uint8_t> case currently trips an invalid-null-argument in libmultiprocess' byte-span serializer.

For an empty vector, data() may be null, and libmultiprocess currently calls memcpy(result.begin(), data.data(), data.size()). Even with size 0, UBSan flags the null pointer argument.

So I restored the guard, but added a comment explaining why it is there. This is not because the IPC round-trip cannot logically handle empty data; it is to avoid a known sanitizer issue in libmultiprocess, which I do not think this PR should change.

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.

So it is real UB? This isn't a blocker, more just trying to understand. Would it make sense in a followup to address it (in type-data.h I'm assuming) and then remove this line at some point?

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.

IIUC this is a GCC bug, see #32016 (comment)

Maybe fixed in GCC-16?

@enochaz
enochaz force-pushed the fuzz-ipc-initial branch from e3a5012 to cf2c65e Compare July 1, 2026 19:06
@enochaz

enochaz commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Nice, good to see an IPC fuzz test. Here's the coverage after fuzzing for a while.

Thanks for giving this a look @marcofleon. Will update the PR description with this coverage report

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

ACK cf2c65e0cc3e75d7ba70e0b78a1515a48f39df76

@DrahtBot

DrahtBot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task fuzzer,address,undefined,integer: https://github.com/bitcoin/bitcoin/actions/runs/28541205184/job/84615183126
LLM reason (✨ experimental): Fuzz testing failed due to an AddressSanitizer UndefinedBehaviorSanitizer error: invalid-null-argument (null passed) in mp/type-data.h during IPC fuzzing.

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.

@enochaz
enochaz force-pushed the fuzz-ipc-initial branch from cf2c65e to 07221bd Compare July 1, 2026 20:27
Add an ipc fuzz target behind ENABLE_IPC.

Set up an in-process two-way pipe and use a small reflected interface
to exercise libmultiprocess client/server calls.

Round-trip COutPoint, CScript, std::vector<uint8_t>, UniValue, and
transactions.

Add bitcoin_ipc_fuzz static library and link it to the fuzz target
via a new src/ipc/test/fuzz/CMakeLists.txt.

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

ACK 037ad77

Non-blocking suggestion for a potential followup.

Comment thread src/ipc/test/fuzz/ipc.cpp Resolved

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

Re-ACK 037ad77

Comment thread src/ipc/test/fuzz/ipc.cpp Resolved
@enochaz

enochaz commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Opened #35916 to improve the fuzz coverage for this target

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.

10 participants