LCOV - code coverage report
Current view: top level - src/test/fuzz - process_message.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 92.4 % 66 61
Test Date: 2026-05-23 06:32:53 Functions: 100.0 % 6 6
Branches: 48.9 % 90 44

             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 <primitives/transaction.h>
      11                 :             : #include <protocol.h>
      12                 :             : #include <script/script.h>
      13                 :             : #include <sync.h>
      14                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      15                 :             : #include <test/fuzz/fuzz.h>
      16                 :             : #include <test/fuzz/util.h>
      17                 :             : #include <test/fuzz/util/net.h>
      18                 :             : #include <test/util/mining.h>
      19                 :             : #include <test/util/net.h>
      20                 :             : #include <test/util/setup_common.h>
      21                 :             : #include <test/util/time.h>
      22                 :             : #include <test/util/validation.h>
      23                 :             : #include <util/check.h>
      24                 :             : #include <util/time.h>
      25                 :             : #include <validationinterface.h>
      26                 :             : 
      27                 :             : #include <cstdlib>
      28                 :             : #include <iostream>
      29                 :             : #include <memory>
      30                 :             : #include <string>
      31                 :             : #include <string_view>
      32                 :             : #include <vector>
      33                 :             : 
      34                 :             : namespace {
      35                 :             : TestingSetup* g_setup;
      36                 :             : std::string_view LIMIT_TO_MESSAGE_TYPE{};
      37                 :             : 
      38                 :          77 : void ResetChainman(TestingSetup& setup)
      39                 :             : {
      40                 :          77 :     SetMockTime(setup.m_node.chainman->GetParams().GenesisBlock().Time());
      41         [ +  - ]:          77 :     setup.m_node.chainman.reset();
      42                 :          77 :     setup.m_make_chainman();
      43                 :          77 :     setup.LoadVerifyActivateChainstate();
      44         [ +  + ]:       15477 :     for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
      45                 :       15400 :         node::BlockAssembler::Options options;
      46         [ +  - ]:       15400 :         MineBlock(setup.m_node, options);
      47                 :       15400 :     }
      48                 :          77 : }
      49                 :             : } // namespace
      50                 :             : 
      51                 :           1 : void initialize_process_message()
      52                 :             : {
      53         [ -  + ]:           1 :     if (const auto val{std::getenv("LIMIT_TO_MESSAGE_TYPE")}) {
      54         [ #  # ]:           0 :         LIMIT_TO_MESSAGE_TYPE = val;
      55         [ #  # ]:           0 :         Assert(std::count(ALL_NET_MESSAGE_TYPES.begin(), ALL_NET_MESSAGE_TYPES.end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
      56                 :             :     }
      57                 :             : 
      58                 :           1 :     static const auto testing_setup{
      59                 :             :         MakeNoLogFileContext<TestingSetup>(
      60                 :             :             /*chain_type=*/ChainType::REGTEST,
      61                 :             :             {}),
      62   [ +  -  +  -  :           1 :     };
                   +  - ]
      63                 :           1 :     g_setup = testing_setup.get();
      64                 :           1 :     ResetChainman(*g_setup);
      65                 :           1 : }
      66                 :             : 
      67         [ +  - ]:        2850 : FUZZ_TARGET(process_message, .init = initialize_process_message)
      68                 :             : {
      69                 :        2386 :     SeedRandomStateForTest(SeedRand::ZEROS);
      70                 :        2386 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      71                 :             : 
      72                 :        2386 :     auto& node{g_setup->m_node};
      73                 :        2386 :     auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
      74                 :        2386 :     connman.Reset();
      75                 :        2386 :     auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
      76         [ +  - ]:        4772 :     const auto block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
      77                 :        2386 :     NodeClockContext clock_ctx{1610000000s}; // any time to successfully reset ibd
      78         [ +  - ]:        2386 :     chainman.ResetIbd();
      79         [ +  - ]:        2386 :     chainman.DisableNextWrite();
      80                 :             : 
      81                 :             :     // Reset, so that dangling pointers can be detected by sanitizers.
      82         [ +  + ]:        2386 :     node.banman.reset();
      83         [ +  - ]:        2386 :     node.addrman.reset();
      84         [ +  - ]:        2386 :     node.peerman.reset();
      85         [ +  - ]:        2386 :     node.addrman = std::make_unique<AddrMan>(*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
      86                 :        4772 :     node.peerman = PeerManager::make(connman, *node.addrman,
      87                 :             :                                      /*banman=*/nullptr, chainman,
      88         [ +  - ]:        2386 :                                      *node.mempool, *node.warnings,
      89                 :             :                                      PeerManager::Options{
      90                 :             :                                          .reconcile_txs = true,
      91                 :             :                                          .deterministic_rng = true,
      92                 :        2386 :                                      });
      93                 :             : 
      94         [ +  - ]:        2386 :     connman.SetMsgProc(node.peerman.get());
      95         [ +  - ]:        2386 :     connman.SetAddrman(*node.addrman);
      96         [ +  - ]:        2386 :     LOCK(NetEventsInterface::g_msgproc_mutex);
      97                 :             : 
      98   [ +  -  +  - ]:        2386 :     const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
      99   [ -  +  -  - ]:        2386 :     if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
     100                 :           0 :         return;
     101                 :             :     }
     102                 :             : 
     103         [ +  - ]:        2386 :     node.validation_signals->RegisterValidationInterface(node.peerman.get());
     104                 :             : 
     105                 :        2386 :     CNode& p2p_node = *ConsumeNodeAsUniquePtr(fuzzed_data_provider).release();
     106                 :             : 
     107         [ +  - ]:        2386 :     connman.AddTestNode(p2p_node);
     108                 :        2386 :     FillNode(fuzzed_data_provider, connman, p2p_node);
     109                 :             : 
     110         [ +  - ]:        2386 :     clock_ctx.set(ConsumeTime(fuzzed_data_provider));
     111                 :             : 
     112         [ +  - ]:        2386 :     CSerializedNetMsg net_msg;
     113         [ +  - ]:        2386 :     net_msg.m_type = random_message_type;
     114                 :        2386 :     net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
     115                 :             : 
     116         [ +  - ]:        2386 :     connman.FlushSendBuffer(p2p_node);
     117         [ +  - ]:        2386 :     (void)connman.ReceiveMsgFrom(p2p_node, std::move(net_msg));
     118                 :             : 
     119                 :             :     bool more_work{true};
     120         [ +  + ]:     1046350 :     while (more_work) {
     121         [ +  - ]:     1043964 :         p2p_node.fPauseSend = false;
     122                 :     1043964 :         try {
     123         [ +  - ]:     1043964 :             more_work = connman.ProcessMessagesOnce(p2p_node);
     124         [ -  - ]:           0 :         } catch (const std::ios_base::failure&) {
     125                 :           0 :         }
     126         [ +  - ]:     1043964 :         node.peerman->SendMessages(p2p_node);
     127                 :             :     }
     128         [ +  - ]:        2386 :     node.validation_signals->SyncWithValidationInterfaceQueue();
     129         [ +  - ]:        2386 :     node.validation_signals->UnregisterValidationInterface(node.peerman.get());
     130         [ +  - ]:        2386 :     node.connman->StopNodes();
     131   [ +  -  +  + ]:        4772 :     if (block_index_size != WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())) {
     132                 :             :         // Reuse the global chainman, but reset it when it is dirty
     133         [ +  - ]:          76 :         ResetChainman(*g_setup);
     134                 :             :     }
     135   [ -  -  +  - ]:        4772 : }
        

Generated by: LCOV version 2.0-1