LCOV - code coverage report
Current view: top level - src/test/fuzz - process_messages.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 96.9 % 64 62
Test Date: 2025-08-01 04:15:35 Functions: 100.0 % 6 6
Branches: 56.8 % 74 42

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

Generated by: LCOV version 2.0-1