process_messages.cpp
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 <protocol.h> 9 #include <script/script.h> 10 #include <sync.h> 11 #include <test/fuzz/FuzzedDataProvider.h> 12 #include <test/fuzz/fuzz.h> 13 #include <test/fuzz/util.h> 14 #include <test/fuzz/util/net.h> 15 #include <test/util/mining.h> 16 #include <test/util/net.h> 17 #include <test/util/setup_common.h> 18 #include <test/util/validation.h> 19 #include <util/time.h> 20 #include <validationinterface.h> 21 22 #include <ios> 23 #include <string> 24 #include <utility> 25 #include <vector> 26 27 namespace { 28 const TestingSetup* g_setup; 29 } // namespace 30 31 void initialize_process_messages() 32 { 33 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>( 34 /*chain_type=*/ChainType::REGTEST, 35 {.extra_args = {"-txreconciliation"}}); 36 g_setup = testing_setup.get(); 37 for (int i = 0; i < 2 * COINBASE_MATURITY; i++) { 38 MineBlock(g_setup->m_node, {}); 39 } 40 g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue(); 41 } 42 43 FUZZ_TARGET(process_messages, .init = initialize_process_messages) 44 { 45 SeedRandomStateForTest(SeedRand::ZEROS); 46 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); 47 48 ConnmanTestMsg& connman = *static_cast<ConnmanTestMsg*>(g_setup->m_node.connman.get()); 49 auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman); 50 SetMockTime(1610000000); // any time to successfully reset ibd 51 chainman.ResetIbd(); 52 53 LOCK(NetEventsInterface::g_msgproc_mutex); 54 55 std::vector<CNode*> peers; 56 const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3); 57 for (int i = 0; i < num_peers_to_add; ++i) { 58 peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release()); 59 CNode& p2p_node = *peers.back(); 60 61 FillNode(fuzzed_data_provider, connman, p2p_node); 62 63 connman.AddTestNode(p2p_node); 64 } 65 66 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30) 67 { 68 const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()}; 69 70 const auto mock_time = ConsumeTime(fuzzed_data_provider); 71 SetMockTime(mock_time); 72 73 CSerializedNetMsg net_msg; 74 net_msg.m_type = random_message_type; 75 net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH); 76 77 CNode& random_node = *PickValue(fuzzed_data_provider, peers); 78 79 connman.FlushSendBuffer(random_node); 80 (void)connman.ReceiveMsgFrom(random_node, std::move(net_msg)); 81 82 bool more_work{true}; 83 while (more_work) { // Ensure that every message is eventually processed in some way or another 84 random_node.fPauseSend = false; 85 86 try { 87 more_work = connman.ProcessMessagesOnce(random_node); 88 } catch (const std::ios_base::failure&) { 89 } 90 g_setup->m_node.peerman->SendMessages(&random_node); 91 } 92 } 93 g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue(); 94 g_setup->m_node.connman->StopNodes(); 95 }