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