Line data Source code
1 : // Copyright (c) 2020-2022 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 <protocol.h> 9 : #include <test/fuzz/FuzzedDataProvider.h> 10 : #include <test/fuzz/fuzz.h> 11 : #include <test/fuzz/util.h> 12 : #include <test/fuzz/util/net.h> 13 : #include <test/util/mining.h> 14 : #include <test/util/net.h> 15 : #include <test/util/setup_common.h> 16 : #include <test/util/validation.h> 17 2 : #include <validation.h> 18 2 : #include <validationinterface.h> 19 : 20 : namespace { 21 : const TestingSetup* g_setup; 22 : } // namespace 23 : 24 0 : void initialize_process_messages() 25 : { 26 0 : static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>( 27 : /*chain_type=*/ChainType::REGTEST, 28 0 : /*extra_args=*/{"-txreconciliation"}); 29 0 : g_setup = testing_setup.get(); 30 0 : for (int i = 0; i < 2 * COINBASE_MATURITY; i++) { 31 0 : MineBlock(g_setup->m_node, CScript() << OP_TRUE); 32 0 : } 33 0 : SyncWithValidationInterfaceQueue(); 34 0 : } 35 : 36 4 : FUZZ_TARGET(process_messages, .init = initialize_process_messages) 37 : { 38 0 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); 39 : 40 0 : ConnmanTestMsg& connman = *static_cast<ConnmanTestMsg*>(g_setup->m_node.connman.get()); 41 0 : auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman); 42 0 : SetMockTime(1610000000); // any time to successfully reset ibd 43 0 : chainman.ResetIbd(); 44 : 45 0 : LOCK(NetEventsInterface::g_msgproc_mutex); 46 : 47 0 : std::vector<CNode*> peers; 48 0 : const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3); 49 0 : for (int i = 0; i < num_peers_to_add; ++i) { 50 0 : peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release()); 51 0 : CNode& p2p_node = *peers.back(); 52 : 53 0 : FillNode(fuzzed_data_provider, connman, p2p_node); 54 : 55 0 : connman.AddTestNode(p2p_node); 56 0 : } 57 : 58 0 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) { 59 0 : const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()}; 60 : 61 0 : const auto mock_time = ConsumeTime(fuzzed_data_provider); 62 0 : SetMockTime(mock_time); 63 : 64 0 : CSerializedNetMsg net_msg; 65 0 : net_msg.m_type = random_message_type; 66 0 : net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider); 67 : 68 0 : CNode& random_node = *PickValue(fuzzed_data_provider, peers); 69 : 70 0 : connman.FlushSendBuffer(random_node); 71 0 : (void)connman.ReceiveMsgFrom(random_node, std::move(net_msg)); 72 0 : random_node.fPauseSend = false; 73 : 74 2 : try { 75 0 : connman.ProcessMessagesOnce(random_node); 76 0 : } catch (const std::ios_base::failure&) { 77 0 : } 78 0 : g_setup->m_node.peerman->SendMessages(&random_node); 79 0 : } 80 0 : SyncWithValidationInterfaceQueue(); 81 0 : g_setup->m_node.connman->StopNodes(); 82 0 : }