Skip to content

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

Open
darosior wants to merge 3 commits into
bitcoin:masterfrom
darosior:2610_fuzz_sock_wait
Open

darosior wants to merge 3 commits into
bitcoin:masterfrom
darosior:2610_fuzz_sock_wait

Conversation

@darosior

@darosior darosior commented Nov 5, 2026

Copy link
Copy Markdown
Member

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

DrahtBot commented Nov 5, 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
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.

@DrahtBot

DrahtBot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

🚧 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.

@fanquake

fanquake commented Nov 6, 2026

Copy link
Copy Markdown
Member

On top of #36765,

That's been merged now.

Comment thread src/test/fuzz/util/check_globals.cpp Resolved
Comment thread src/test/fuzz/connman.cpp
std::unordered_set<const CNode*> paused;
for (CNode* node : connman.TestNodes()) {
LOCK(node->m_sock_mutex);
if (!node->m_sock) continue;

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

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.

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.

@brunoerg

brunoerg commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

Prior to this commit, every FuzzedSock had one of two socket values and
CConnman::SocketHandler() merged the entries of different peers in a
single wait, which led the connman fuzz target to act on one peer with
the events of another.

This fixes it by using a different descriptor value for each socket,
like the OS does for real sockets. This is achieved by numbering the
sockets down from a global counter, restarted before each fuzz input to
keep each run deterministic.

A sanity check is added to CheckGlobalsImpl, in the same fashion as the
it's done for the global PRNG and mocked time states.
FuzzedSock::Wait() and WaitMany() reported either nothing or exactly the
requested events. The real OS reports more than that, and the fuzz
targets never exercised the callers on it:
- only some of the requested events, e.g. SendEvent alone on an idle
  connection;
- ErrorEvent, with or without RecvEvent, to a socket that requested
  RecvEvent, as select() does for urgent data;
- a failed connection, e.g. a reset one: poll() reports it as
  ErrorEvent whatever was requested, even nothing.

Model these. A failed connection stays failed: waits keep reporting
it, Recv() returns at most what was buffered before (a bounded amount
picked when it fails) and then fails for good, and Send() fails. So a
socket that did not request RecvEvent can be read from only once its
connection failed, and only for a bounded amount, as on the real OS.

WaitMany() now picks the outcome per socket, so that each socket keeps
its own state. As before, the requested events occur when the fuzz
input is exhausted, so that waits do not go on forever.
@darosior
darosior force-pushed the 2610_fuzz_sock_wait branch from 18b72e7 to 75f6da8 Compare November 19, 2026 23:25
Flag a number of events in the FuzzedSock and assert corresponding
invariants after running the handler.
@darosior

Copy link
Copy Markdown
Member Author

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 frankomosh 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. Giving each FuzzedSock its own descriptor and checking more of what the socket handler does, both look useful.

Comment thread src/test/fuzz/connman.cpp
// Check we only ever read (/send) data when Recv (/send) was reported. The converse
// does not always hold (for instance we may skip a recv() when we have too much to
// send).
Assert(!sock->m_last_recv || (sock->m_last_occurred & (Sock::RecvEvent | Sock::ErrorEvent)));

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

// Mask using ConsumeBool() rather than ConsumeIntegral() because fuzzers generally
// operate at the byte level.
Event occurred{requested};
for (size_t bit{0}; bit < sizeof(requested) * CHAR_BIT; ++bit) {

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. 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;
    }
}

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