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-01-28 04:17:28 Functions: 100.0 % 6 6
Branches: 58.1 % 74 43

             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 <banman.h>
       6                 :             : #include <consensus/consensus.h>
       7                 :             : #include <net.h>
       8                 :             : #include <net_processing.h>
       9                 :             : #include <node/warnings.h>
      10                 :             : #include <protocol.h>
      11                 :             : #include <script/script.h>
      12                 :             : #include <sync.h>
      13                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      14                 :             : #include <test/fuzz/fuzz.h>
      15                 :             : #include <test/fuzz/util.h>
      16                 :             : #include <test/fuzz/util/net.h>
      17                 :             : #include <test/util/mining.h>
      18                 :             : #include <test/util/net.h>
      19                 :             : #include <test/util/setup_common.h>
      20                 :             : #include <test/util/validation.h>
      21                 :             : #include <util/time.h>
      22                 :             : #include <validationinterface.h>
      23                 :             : 
      24                 :             : #include <ios>
      25                 :             : #include <string>
      26                 :             : #include <utility>
      27                 :             : #include <vector>
      28                 :             : 
      29                 :             : namespace {
      30                 :             : TestingSetup* g_setup;
      31                 :             : 
      32                 :          67 : void ResetChainman(TestingSetup& setup)
      33                 :             : {
      34                 :          67 :     SetMockTime(setup.m_node.chainman->GetParams().GenesisBlock().Time());
      35         [ +  - ]:          67 :     setup.m_node.chainman.reset();
      36                 :          67 :     setup.m_make_chainman();
      37                 :          67 :     setup.LoadVerifyActivateChainstate();
      38         [ +  + ]:       13467 :     for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
      39         [ +  - ]:       26800 :         MineBlock(setup.m_node, {});
      40                 :             :     }
      41                 :          67 :     setup.m_node.validation_signals->SyncWithValidationInterfaceQueue();
      42   [ +  -  +  - ]:       26867 : }
      43                 :             : } // namespace
      44                 :             : 
      45                 :           1 : void initialize_process_messages()
      46                 :             : {
      47                 :           1 :     static const auto testing_setup{
      48                 :             :         MakeNoLogFileContext<TestingSetup>(
      49                 :             :             /*chain_type=*/ChainType::REGTEST,
      50                 :             :             {}),
      51   [ +  -  +  -  :           1 :     };
                   +  - ]
      52                 :           1 :     g_setup = testing_setup.get();
      53                 :           1 :     ResetChainman(*g_setup);
      54                 :           1 : }
      55                 :             : 
      56         [ +  - ]:        7129 : FUZZ_TARGET(process_messages, .init = initialize_process_messages)
      57                 :             : {
      58                 :        6679 :     SeedRandomStateForTest(SeedRand::ZEROS);
      59                 :        6679 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      60                 :             : 
      61                 :        6679 :     auto& node{g_setup->m_node};
      62                 :        6679 :     auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
      63                 :        6679 :     connman.ResetAddrCache();
      64                 :        6679 :     connman.ResetMaxOutboundCycle();
      65                 :        6679 :     auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
      66         [ +  - ]:       13358 :     const auto block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
      67                 :        6679 :     SetMockTime(1610000000); // any time to successfully reset ibd
      68                 :        6679 :     chainman.ResetIbd();
      69                 :        6679 :     chainman.DisableNextWrite();
      70                 :             : 
      71                 :             :     // Reset, so that dangling pointers can be detected by sanitizers.
      72         [ +  + ]:        6679 :     node.banman.reset();
      73         [ +  - ]:        6679 :     node.addrman.reset();
      74         [ +  - ]:        6679 :     node.peerman.reset();
      75                 :        6679 :     node.addrman = std::make_unique<AddrMan>(*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
      76                 :       13358 :     node.peerman = PeerManager::make(connman, *node.addrman,
      77                 :             :                                      /*banman=*/nullptr, chainman,
      78                 :        6679 :                                      *node.mempool, *node.warnings,
      79                 :             :                                      PeerManager::Options{
      80                 :             :                                          .reconcile_txs = true,
      81                 :             :                                          .deterministic_rng = true,
      82                 :        6679 :                                      });
      83         [ +  - ]:        6679 :     connman.SetMsgProc(node.peerman.get());
      84                 :        6679 :     connman.SetAddrman(*node.addrman);
      85                 :             : 
      86                 :        6679 :     LOCK(NetEventsInterface::g_msgproc_mutex);
      87                 :             : 
      88                 :        6679 :     std::vector<CNode*> peers;
      89                 :        6679 :     const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
      90         [ +  + ]:       17900 :     for (int i = 0; i < num_peers_to_add; ++i) {
      91         [ +  - ]:       11221 :         peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
      92                 :       11221 :         CNode& p2p_node = *peers.back();
      93                 :             : 
      94                 :       11221 :         FillNode(fuzzed_data_provider, connman, p2p_node);
      95                 :             : 
      96         [ +  - ]:       11221 :         connman.AddTestNode(p2p_node);
      97                 :             :     }
      98                 :             : 
      99   [ +  +  +  + ]:       93422 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30)
     100                 :             :     {
     101   [ +  -  +  - ]:       86743 :         const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
     102                 :             : 
     103                 :       86743 :         const auto mock_time = ConsumeTime(fuzzed_data_provider);
     104         [ +  - ]:       86743 :         SetMockTime(mock_time);
     105                 :             : 
     106         [ +  - ]:       86743 :         CSerializedNetMsg net_msg;
     107         [ +  - ]:       86743 :         net_msg.m_type = random_message_type;
     108                 :       86743 :         net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
     109                 :             : 
     110                 :       86743 :         CNode& random_node = *PickValue(fuzzed_data_provider, peers);
     111                 :             : 
     112         [ +  - ]:       86743 :         connman.FlushSendBuffer(random_node);
     113         [ +  - ]:       86743 :         (void)connman.ReceiveMsgFrom(random_node, std::move(net_msg));
     114                 :             : 
     115                 :             :         bool more_work{true};
     116         [ +  + ]:      209294 :         while (more_work) { // Ensure that every message is eventually processed in some way or another
     117         [ +  - ]:      122551 :             random_node.fPauseSend = false;
     118                 :             : 
     119                 :      122551 :             try {
     120         [ +  - ]:      122551 :                 more_work = connman.ProcessMessagesOnce(random_node);
     121         [ -  - ]:           0 :             } catch (const std::ios_base::failure&) {
     122                 :           0 :             }
     123         [ +  - ]:      122551 :             node.peerman->SendMessages(&random_node);
     124                 :             :         }
     125                 :       86743 :     }
     126         [ +  - ]:        6679 :     node.validation_signals->SyncWithValidationInterfaceQueue();
     127         [ +  - ]:        6679 :     node.connman->StopNodes();
     128   [ +  -  +  + ]:       13358 :     if (block_index_size != WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())) {
     129                 :             :         // Reuse the global chainman, but reset it when it is dirty
     130         [ +  - ]:          66 :         ResetChainman(*g_setup);
     131                 :             :     }
     132         [ +  - ]:       13358 : }
        

Generated by: LCOV version 2.0-1