fuzz: report more events, and check more invariants in connman target #36786

Open
darosior wants to merge 3 commits from 2610_fuzz_sock_wait into master AGit
darosior commented 2026-11-05 20:02:16 +00:00 (Migrated from github.com)

On top of #36765, this extends FuzzedSock to report more events a real OS could report (see #36737 for some coverage of those), and improves the connman fuzz target to assert more socket handling invariants.

The first commit is also a fix to uniquely identify sockets, as currently each socket can only use one of two descriptor values, which means the fuzz target would act on one peer with the event of another.

See commit messages for details.

On top of #36765, this extends FuzzedSock to report more events a real OS could report (see #36737 for some coverage of those), and improves the connman fuzz target to assert more socket handling invariants. The first commit is also a fix to uniquely identify sockets, as currently each socket can only use one of two descriptor values, which means the fuzz target would act on one peer with the event of another. See commit messages for details.
DrahtBot commented 2026-11-05 20:02:31 +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
Concept ACK brunoerg, frankomosh

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/36786 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36786.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36786#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 | | ---- | --------- | | Concept ACK | [brunoerg](https://github.com/bitcoin/bitcoin/pull/36786#issuecomment-6754653195), [frankomosh](https://github.com/bitcoin/bitcoin/pull/36786#pullrequestreview-6034186624) | 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-->
First-time contributor

🔵 Suggestion · The reset guard permits descriptor reuse after socket creation

Although current callers reset first, CheckGlobals accepts a late ResetFuzzedSockets() call that can give two live sockets the same descriptor.

Location: src/test/fuzz/util/net.cpp:126 (head).

Review coverage was incomplete.


Reviewed: 75f6da8ea338 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:3705f06c6a27c475c77f2533c4d016ac03c829c839b7b538bba5e4768ccf54f1:1ed10c4979af6fc548d6b8d4534d505e02e84ac4b0aa4f8633555b2ba75119e8 --> <!-- ralph-base:079437cca7a8e2997a6dcd83ad38350014352544 --> <!-- ralph-head:75f6da8ea33861982cb0ad00984be48acf340e11 --> <!-- ralph-case:a5581c9728cf8f8f375c886a60740914bc0403812cc853d1545cb71272a21f43 --> **🔵 Suggestion · The reset guard permits descriptor reuse after socket creation** Although current callers reset first, `CheckGlobals` accepts a late `ResetFuzzedSockets()` call that can give two live sockets the same descriptor. Location: [`src/test/fuzz/util/net.cpp:126`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/75f6da8ea33861982cb0ad00984be48acf340e11/src/test/fuzz/util/net.cpp#L126>) (head). Review coverage was incomplete. <!-- Review tier: standard --> --- Reviewed: [`75f6da8ea338`](<https://git.fish.foo/bitcoin/bitcoin/commit/75f6da8ea33861982cb0ad00984be48acf340e11>) · [Review report](<https://review.fish.foo/report/36786-75f6da8ea33861982cb0ad00984be48acf340e11-2861-4.html>) · [Previous reports](<https://review.fish.foo/traces/36786/>) [permalink](<https://review.fish.foo/report/36786-75f6da8ea33861982cb0ad00984be48acf340e11-2861-4.html>)
DrahtBot commented 2026-11-05 21:12:45 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/42099583336/job/127498536190
LLM reason (✨ experimental): CI failed because the lint check detected a newly introduced circular dependency in test/fuzz utilities.

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.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `lint`: https://github.com/bitcoin/bitcoin/actions/runs/42099583336/job/127498536190</sub> <sub>LLM reason (✨ experimental): CI failed because the lint check detected a newly introduced circular dependency in `test/fuzz` utilities.</sub> <details><summary>Hints</summary> 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. </details>
fanquake commented 2026-11-06 11:02:25 +00:00 (Migrated from github.com)
> On top of https://github.com/bitcoin/bitcoin/pull/36765, That's been merged now.
maflcko (Migrated from github.com) reviewed 2026-11-06 13:50:53 +00:00
maflcko (Migrated from github.com) commented 2026-11-06 13:50:53 +00:00

style-nit: I guess this works, but design-wise this struct was more meant to be a no-op linter (only crash on a bug), not a behavior-changing cleanup utility 😅

Either:

  • Add a g_used_socket and g_reset_socket (and use it to lint and enforce a call to ResetFuzzedSockets in the affected fuzz targets)
  • Just add ResetFuzzedSockets in the affected fuzz targets and hope it is sufficient without a linter
  • Leave it as-is

Up to you.

style-nit: I guess this works, but design-wise this struct was more meant to be a no-op linter (only crash on a bug), not a behavior-changing cleanup utility 😅 Either: * Add a `g_used_socket` and `g_reset_socket` (and use it to lint and enforce a call to `ResetFuzzedSockets` in the affected fuzz targets) * Just add `ResetFuzzedSockets` in the affected fuzz targets and hope it is sufficient without a linter * Leave it as-is Up to you.
darosior (Migrated from github.com) reviewed 2026-11-06 16:04:35 +00:00
darosior (Migrated from github.com) commented 2026-11-06 16:04:35 +00:00

Happy to change this, but i'm not sure how CheckGlobalsImpl() is a no-op linter? It is already resetting state that fuzz targets depend on. Resetting the starting descriptor value for created sockets does not seem that different from resetting the mock time(s).

Happy to change this, but i'm not sure how CheckGlobalsImpl() is a no-op linter? It is already resetting state that fuzz targets depend on. Resetting the starting descriptor value for created sockets does not seem that different from resetting the mock time(s).
maflcko (Migrated from github.com) reviewed 2026-11-06 16:25:47 +00:00
maflcko (Migrated from github.com) commented 2026-11-06 16:25:47 +00:00

The idea is that this is resetting the mock time, so that system time is used. However, if system time was used, it would fail the fuzz run due to non-determinism.

Conversely, if the two time-clear toggles were removed, the fuzz run would just run normally and still deterministically, because all fuzz targets set their mock time explicitly.

This is just a style preference of mine to have each test case fully list all dependencies or requirements by itself, so that it is self-contained and doesn't depend on implicit global setup.

The idea is that this is resetting the mock time, so that system time is used. However, if system time was used, it would fail the fuzz run due to non-determinism. Conversely, if the two time-clear toggles were removed, the fuzz run would just run normally and still deterministically, because all fuzz targets set their mock time explicitly. This is just a style preference of mine to have each test case fully list all dependencies or requirements by itself, so that it is self-contained and doesn't depend on implicit global setup.
darosior (Migrated from github.com) reviewed 2026-11-06 16:58:31 +00:00
darosior (Migrated from github.com) commented 2026-11-06 16:58:31 +00:00

Make sense, i'll move the Reset to targets making use of fuzzed sockets then.

Make sense, i'll move the Reset to targets making use of fuzzed sockets then.
darosior (Migrated from github.com) reviewed 2026-11-06 17:53:34 +00:00
darosior (Migrated from github.com) commented 2026-11-06 17:53:34 +00:00

Done, along with a sanity check akin to that used for the global PRNG / mocked time states.

Done, along with a sanity check akin to that used for the global PRNG / mocked time states.
brunoerg (Migrated from github.com) reviewed 2026-11-09 23:49:53 +00:00
brunoerg (Migrated from github.com) commented 2026-11-09 23:49:53 +00:00

I think that the continue here is right for the fail-closed checks, but it also skips the readiness, m_recv_twice and m_send_after_short checks. Those describe what the handler did during the run, whether or not the socket survived.

The problem is that the bugs these checks look for usually end with the handler closing the socket. For example, a read without RecvEvent on a healthy FuzzedSock mostly returns 0 or an error, and the handler then calls CloseSocketDisconnect(). The node now has no socket, so the check never runs. The violation is only seen when the bad read happens to return data.

I tested this with a mutant that also reads on SendEvent:

diff --git a/src/net.cpp b/src/net.cpp
index f31604f6c5..b781371074 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -2218,7 +2218,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
             }
         }

-        if (recvSet || errorSet)
+        if (recvSet || errorSet || sendSet)
         {
             // typical socket buffer is 8K-64K
             uint8_t pchBuf[0x10000];

My clanker suggests to keep a reference to each socket before the handler runs, check behaviour on that reference, and only tie the fail-closed checks to whether the node still holds it.

I think that the `continue` here is right for the fail-closed checks, but it also skips the readiness, `m_recv_twice` and `m_send_after_short` checks. Those describe what the handler did during the run, whether or not the socket survived. The problem is that the bugs these checks look for usually end with the handler closing the socket. For example, a read without RecvEvent on a healthy FuzzedSock mostly returns 0 or an error, and the handler then calls CloseSocketDisconnect(). The node now has no socket, so the check never runs. The violation is only seen when the bad read happens to return data. I tested this with a mutant that also reads on SendEvent: ```diff diff --git a/src/net.cpp b/src/net.cpp index f31604f6c5..b781371074 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2218,7 +2218,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes, } } - if (recvSet || errorSet) + if (recvSet || errorSet || sendSet) { // typical socket buffer is 8K-64K uint8_t pchBuf[0x10000]; ``` My clanker suggests to keep a reference to each socket before the handler runs, check behaviour on that reference, and only tie the fail-closed checks to whether the node still holds it.
brunoerg commented 2026-11-09 23:50:38 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
DrahtBot commented 2026-11-10 13:48:42 +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).
darosior (Migrated from github.com) reviewed 2026-11-19 23:26:19 +00:00
darosior (Migrated from github.com) commented 2026-11-19 23:26:19 +00:00

Great point. I updated the approach to keep a vector of the test nodes, along with a copy of the socket shared_ptr and whether that peer was paused. The second loop now iterates through that vector and asserts invariants for all of them, including when the connection was closed.

Great point. I updated the approach to keep a vector of the test nodes, along with a copy of the socket shared_ptr and whether that peer was paused. The second loop now iterates through that vector and asserts invariants for all of them, including when the connection was closed.
darosior commented 2026-11-19 23:28:25 +00:00 (Migrated from github.com)

Rebased and updated the approach per @brunoerg's point about closed connections. Will give that new approach a fair amount of CPU cycles overnight.

Rebased and updated the approach per @brunoerg's point about closed connections. Will give that new approach a fair amount of CPU cycles overnight.
frankomosh (Migrated from github.com) reviewed 2026-11-24 06:24:11 +00:00
frankomosh (Migrated from github.com) left a comment

Concept ACK. Giving each FuzzedSock its own descriptor and checking more of what the socket handler does, both look useful.

Concept ACK. Giving each FuzzedSock its own descriptor and checking more of what the socket handler does, both look useful.
frankomosh (Migrated from github.com) commented 2026-11-24 05:31:27 +00:00

I counted locally, with help of a counter in SocketHandlerConnected(), how often the handler sends when replaying current qa-assets connman corpus:

  • master: a node had data to send 264,266 times, with 192 real sends
  • PR: 1 and 0

So existing corpus no longer reaches these checks on the send side. For example, Bruno's mutant that also reads on SendEvent survives the qa-assets corpus on this head, but a corpus I fuzzed locally catches it here (line 320).

I guess this is expected, since FuzzedSock now uses the input bytes differently, and the corpus will catch up eventually after merge. Noting it in case it's worth checking that the refreshed corpus reaches sends.

I counted locally, with help of a counter in `SocketHandlerConnected()`, how often the handler sends when replaying current qa-assets connman corpus: - master: a node had data to send 264,266 times, with 192 real sends - PR: 1 and 0 So existing corpus no longer reaches these checks on the send side. For example, Bruno's mutant that also reads on `SendEvent` survives the qa-assets corpus on this head, but a corpus I fuzzed locally catches it here (line 320). I guess this is expected, since FuzzedSock now uses the input bytes differently, and the corpus will catch up eventually after merge. Noting it in case it's worth checking that the refreshed corpus reaches sends.
frankomosh (Migrated from github.com) commented 2026-11-24 05:48:17 +00:00

nit. only three event bits exist, so maybe loop over them directly?, same bits in the same order, so it should consume the fuzz input the same way:

for (const Event e : {RecvEvent, SendEvent, ErrorEvent}) {
    if ((occurred & e) && m_fuzzed_data_provider.ConsumeBool()) {
        occurred &= ~e;
    }
}
nit. only three event bits exist, so maybe loop over them directly?, same bits in the same order, so it should consume the fuzz input the same way: ```cpp for (const Event e : {RecvEvent, SendEvent, ErrorEvent}) { if ((occurred & e) && m_fuzzed_data_provider.ConsumeBool()) { occurred &= ~e; } } ```
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!36786
No description provided.