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

pull darosior wants to merge 3 commits into bitcoin:master from darosior:2610_fuzz_sock_wait changing 14 files +268 −10
  1. darosior commented at 8:02 PM on November 5, 2026: 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.

  2. DrahtBot added the label Fuzzing on Nov 5, 2026
  3. DrahtBot commented at 8:02 PM on November 5, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    Concept ACK brunoerg

    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

    Reviewers, this pull request conflicts with the following ones:

    • #36808 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36808.svg"></sub> (iwyu: Fix warnings in src/test/fuzz and treat them as errors by hebasto)
    • #36074 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36074.svg"></sub> (scripted-diff: [test] Add util/check.h includes for assertions by maflcko)

    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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. DrahtBot added the label CI failed on Nov 5, 2026
  5. DrahtBot commented at 9:12 PM on November 5, 2026: contributor

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

  6. fanquake commented at 11:02 AM on November 6, 2026: member

    On top of #36765,

    That's been merged now.

  7. in src/test/fuzz/util/check_globals.cpp:24 in 111a2f7794 outdated
      20 | @@ -20,6 +21,7 @@ struct CheckGlobalsImpl {
      21 |          g_used_system_time = false;
      22 |          SetMockTime(0s);
      23 |          MockableSteadyClock::ClearMockTime();
      24 | +        ResetFuzzedSockets();
    


    maflcko commented at 1:50 PM on November 6, 2026:

    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 commented at 4:04 PM on November 6, 2026:

    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 commented at 4:25 PM on November 6, 2026:

    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 commented at 4:58 PM on November 6, 2026:

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


    darosior commented at 5:53 PM on November 6, 2026:

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

  8. darosior force-pushed on Nov 6, 2026
  9. DrahtBot removed the label CI failed on Nov 6, 2026
  10. darosior force-pushed on Nov 6, 2026
  11. fuzz: give each FuzzedSock its own socket
    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.
    fc3532c50c
  12. fuzz: let FuzzedSock waits report what the real OS can report
    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.
    32632c556a
  13. fuzz: assert more invariants in the connman target
    Flag a number of events in the FuzzedSock and assert corresponding
    invariants after running the handler.
    18b72e7481
  14. in src/test/fuzz/connman.cpp:295 in 18b72e7481
     287 | @@ -285,7 +288,56 @@ FUZZ_TARGET(connman, .init = initialize_connman)
     288 |                  connman.InitBindsPublic(options);
     289 |              },
     290 |              [&] {
     291 | +                // Initialize state to accurately flag events during the SocketHandler run.
     292 | +                std::unordered_set<const CNode*> paused;
     293 | +                for (CNode* node : connman.TestNodes()) {
     294 | +                    LOCK(node->m_sock_mutex);
     295 | +                    if (!node->m_sock) continue;
    


    brunoerg commented at 11:49 PM on November 9, 2026:

    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.

  15. brunoerg commented at 11:50 PM on November 9, 2026: contributor

    Concept ACK

  16. fanquake requested review from Crypt-iQ on Nov 10, 2026
  17. fanquake requested review from marcofleon on Nov 10, 2026
  18. DrahtBot added the label Needs rebase on Nov 10, 2026
  19. DrahtBot commented at 1:48 PM on November 10, 2026: contributor

    <!--cf906140f33d8803c4a75a2196329ecb-->

    🐙 This pull request conflicts with the target branch and needs rebase.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-18 16:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me