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