LCOV - code coverage report
Current view: top level - src/test/fuzz - process_messages.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 97.0 % 67 65
Test Date: 2026-06-01 07:10:42 Functions: 100.0 % 6 6
Branches: 57.0 % 86 49

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <addrman.h>
       6                 :             : #include <banman.h>
       7                 :             : #include <consensus/consensus.h>
       8                 :             : #include <kernel/chainparams.h>
       9                 :             : #include <net.h>
      10                 :             : #include <net_processing.h>
      11                 :             : #include <node/mining_types.h>
      12                 :             : #include <primitives/block.h>
      13                 :             : #include <primitives/transaction.h>
      14                 :             : #include <protocol.h>
      15                 :             : #include <sync.h>
      16                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      17                 :             : #include <test/fuzz/fuzz.h>
      18                 :             : #include <test/fuzz/util.h>
      19                 :             : #include <test/fuzz/util/net.h>
      20                 :             : #include <test/util/mining.h>
      21                 :             : #include <test/util/net.h>
      22                 :             : #include <test/util/random.h>
      23                 :             : #include <test/util/setup_common.h>
      24                 :             : #include <test/util/time.h>
      25                 :             : #include <test/util/validation.h>
      26                 :             : #include <util/time.h>
      27                 :             : #include <validation.h>
      28                 :             : #include <validationinterface.h>
      29                 :             : 
      30                 :             : #include <functional>
      31                 :             : #include <ios>
      32                 :             : #include <memory>
      33                 :             : #include <optional>
      34                 :             : #include <string>
      35                 :             : #include <utility>
      36                 :             : #include <vector>
      37                 :             : 
      38                 :             : namespace {
      39                 :             : TestingSetup* g_setup;
      40                 :             : 
      41                 :         151 : void ResetChainman(TestingSetup& setup)
      42                 :             : {
      43                 :         151 :     SetMockTime(setup.m_node.chainman->GetParams().GenesisBlock().Time());
      44         [ +  - ]:         151 :     setup.m_node.chainman.reset();
      45                 :         151 :     setup.m_make_chainman();
      46                 :         151 :     setup.LoadVerifyActivateChainstate();
      47                 :         151 :     node::BlockCreateOptions options;
      48         [ +  + ]:       30351 :     for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
      49         [ +  - ]:       30200 :         MineBlock(setup.m_node, options);
      50                 :             :     }
      51                 :         151 : }
      52                 :             : } // namespace
      53                 :             : 
      54                 :           1 : void initialize_process_messages()
      55                 :             : {
      56                 :           1 :     static const auto testing_setup{
      57                 :             :         MakeNoLogFileContext<TestingSetup>(
      58                 :             :             /*chain_type=*/ChainType::REGTEST,
      59                 :             :             {}),
      60   [ +  -  +  -  :           1 :     };
                   +  - ]
      61                 :           1 :     g_setup = testing_setup.get();
      62                 :           1 :     ResetChainman(*g_setup);
      63                 :           1 : }
      64                 :             : 
      65         [ +  - ]:        4247 : FUZZ_TARGET(process_messages, .init = initialize_process_messages)
      66                 :             : {
      67                 :        3783 :     SeedRandomStateForTest(SeedRand::ZEROS);
      68                 :        3783 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      69                 :             : 
      70                 :        3783 :     auto& node{g_setup->m_node};
      71                 :        3783 :     auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
      72                 :        3783 :     connman.Reset();
      73                 :        3783 :     auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
      74         [ +  - ]:        7566 :     const auto block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
      75                 :        3783 :     NodeClockContext clock_ctx{1610000000s}; // any time to successfully reset ibd
      76         [ +  - ]:        3783 :     chainman.ResetIbd();
      77         [ +  - ]:        3783 :     chainman.DisableNextWrite();
      78                 :             : 
      79                 :             :     // Reset, so that dangling pointers can be detected by sanitizers.
      80         [ +  + ]:        3783 :     node.banman.reset();
      81         [ +  - ]:        3783 :     node.addrman.reset();
      82         [ +  - ]:        3783 :     node.peerman.reset();
      83         [ +  - ]:        3783 :     node.addrman = std::make_unique<AddrMan>(*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
      84                 :        7566 :     node.peerman = PeerManager::make(connman, *node.addrman,
      85                 :             :                                      /*banman=*/nullptr, chainman,
      86         [ +  - ]:        3783 :                                      *node.mempool, *node.warnings,
      87                 :             :                                      PeerManager::Options{
      88                 :             :                                          .reconcile_txs = true,
      89                 :             :                                          .deterministic_rng = true,
      90                 :        3783 :                                      });
      91         [ +  - ]:        3783 :     connman.SetMsgProc(node.peerman.get());
      92         [ +  - ]:        3783 :     connman.SetAddrman(*node.addrman);
      93                 :             : 
      94         [ +  - ]:        3783 :     node.validation_signals->RegisterValidationInterface(node.peerman.get());
      95                 :             : 
      96         [ +  - ]:        3783 :     LOCK(NetEventsInterface::g_msgproc_mutex);
      97                 :             : 
      98                 :        3783 :     std::vector<CNode*> peers;
      99                 :        3783 :     const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
     100         [ +  + ]:       10303 :     for (int i = 0; i < num_peers_to_add; ++i) {
     101         [ +  - ]:        6520 :         peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
     102                 :        6520 :         CNode& p2p_node = *peers.back();
     103                 :             : 
     104                 :        6520 :         FillNode(fuzzed_data_provider, connman, p2p_node);
     105                 :             : 
     106         [ +  - ]:        6520 :         connman.AddTestNode(p2p_node);
     107                 :             :     }
     108                 :             : 
     109   [ +  +  +  + ]:       51945 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30)
     110                 :             :     {
     111   [ +  -  +  - ]:       48162 :         const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
     112                 :             : 
     113         [ +  - ]:       48162 :         clock_ctx.set(ConsumeTime(fuzzed_data_provider));
     114                 :             : 
     115         [ +  - ]:       48162 :         CSerializedNetMsg net_msg;
     116         [ +  - ]:       48162 :         net_msg.m_type = random_message_type;
     117                 :       48162 :         net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
     118                 :             : 
     119                 :       48162 :         CNode& random_node = *PickValue(fuzzed_data_provider, peers);
     120                 :             : 
     121         [ +  - ]:       48162 :         connman.FlushSendBuffer(random_node);
     122         [ +  - ]:       48162 :         (void)connman.ReceiveMsgFrom(random_node, std::move(net_msg));
     123                 :             : 
     124                 :             :         bool more_work{true};
     125         [ +  + ]:      301150 :         while (more_work) { // Ensure that every message is eventually processed in some way or another
     126         [ +  - ]:      252988 :             random_node.fPauseSend = false;
     127                 :             : 
     128                 :      252988 :             try {
     129         [ +  - ]:      252988 :                 more_work = connman.ProcessMessagesOnce(random_node);
     130         [ -  - ]:           0 :             } catch (const std::ios_base::failure&) {
     131                 :           0 :             }
     132         [ +  - ]:      252988 :             node.peerman->SendMessages(random_node);
     133                 :             :         }
     134                 :       48162 :     }
     135         [ +  - ]:        3783 :     node.validation_signals->SyncWithValidationInterfaceQueue();
     136         [ +  - ]:        3783 :     node.validation_signals->UnregisterValidationInterface(node.peerman.get());
     137         [ +  - ]:        3783 :     node.connman->StopNodes();
     138   [ +  -  +  + ]:        7566 :     if (block_index_size != WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())) {
     139                 :             :         // Reuse the global chainman, but reset it when it is dirty
     140         [ +  - ]:         150 :         ResetChainman(*g_setup);
     141                 :             :     }
     142         [ +  - ]:        7566 : }
        

Generated by: LCOV version 2.0-1