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 <addrman.h>
6 : : #include <banman.h>
7 : : #include <kernel/chainparams.h>
8 : : #include <net.h>
9 : : #include <net_processing.h>
10 : : #include <primitives/block.h>
11 : : #include <primitives/transaction.h>
12 : : #include <protocol.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/net.h>
19 : : #include <test/util/random.h>
20 : : #include <test/util/setup_common.h>
21 : : #include <test/util/time.h>
22 : : #include <test/util/validation.h>
23 : : #include <uint256.h>
24 : : #include <util/check.h>
25 : : #include <util/time.h>
26 : : #include <validation.h>
27 : : #include <validationinterface.h>
28 : :
29 : : #include <functional>
30 : : #include <ios>
31 : : #include <memory>
32 : : #include <optional>
33 : : #include <string>
34 : : #include <utility>
35 : : #include <vector>
36 : :
37 : : namespace {
38 : : TestingSetup* g_setup;
39 : :
40 : : } // namespace
41 : :
42 : : extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
43 : :
44 : 1 : void initialize_process_messages()
45 : : {
46 : 1 : static const auto testing_setup{
47 : : MakeNoLogFileContext<TestingSetup>(
48 : : /*chain_type=*/ChainType::REGTEST,
49 : : {}),
50 [ + - + - : 2 : };
+ - ]
51 : 1 : g_setup = testing_setup.get();
52 : 1 : ResetChainmanAndMempool(*g_setup);
53 : 1 : }
54 : :
55 [ + - ]: 5845 : FUZZ_TARGET(process_messages, .init = initialize_process_messages)
56 : : {
57 : 5371 : SeedRandomStateForTest(SeedRand::ZEROS);
58 : 5371 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
59 : :
60 : 5371 : auto& node{g_setup->m_node};
61 : 5371 : auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
62 : 5371 : connman.Reset();
63 : 5371 : auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
64 [ + - ]: 10742 : const auto block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
65 [ + - ]: 10742 : const auto initial_sequence{WITH_LOCK(node.mempool->cs, return node.mempool->GetSequence())};
66 : 5371 : GetFakeNodeClock().set(1610000000s); // 2021-01-07, arbitrary
67 : 5371 : FakeSteadyClock steady_clock;
68 [ + - ]: 5371 : chainman.ResetIbd();
69 [ + - ]: 5371 : chainman.DisableNextWrite();
70 : :
71 : : // Reset, so that dangling pointers can be detected by sanitizers.
72 [ + + ]: 5371 : node.banman.reset();
73 [ + - ]: 5371 : node.addrman.reset();
74 [ + - ]: 5371 : node.peerman.reset();
75 [ + - ]: 5371 : node.addrman = std::make_unique<AddrMan>(*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
76 : 10742 : node.peerman = PeerManager::make(connman, *node.addrman,
77 : : /*banman=*/nullptr, chainman,
78 [ + - ]: 5371 : *node.mempool, *node.warnings,
79 : : PeerManager::Options{
80 : : .reconcile_txs = true,
81 : : .deterministic_rng = true,
82 : 5371 : });
83 [ + - ]: 5371 : connman.SetMsgProc(node.peerman.get());
84 [ + - ]: 5371 : connman.SetAddrman(*node.addrman);
85 : :
86 [ + - ]: 5371 : node.validation_signals->RegisterValidationInterface(node.peerman.get());
87 : :
88 [ + - ]: 5371 : LOCK(NetEventsInterface::g_msgproc_mutex);
89 : :
90 : 5371 : std::vector<CNode*> peers;
91 : 5371 : const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
92 [ + + ]: 14558 : for (int i = 0; i < num_peers_to_add; ++i) {
93 [ + - ]: 9187 : peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, steady_clock, i).release());
94 : 9187 : CNode& p2p_node = *peers.back();
95 : :
96 : 9187 : FillNode(fuzzed_data_provider, connman, p2p_node);
97 : :
98 [ + - ]: 9187 : connman.AddTestNode(p2p_node);
99 : : }
100 : :
101 : : // Toggle IBD from within the loop, so that some messages may be processed
102 : : // under IBD and the rest after leaving it. JumpOutOfIbd() latches, so guard
103 : : // it to call at most once.
104 : : bool jump_out_of_ibd{false};
105 [ + + + + ]: 68456 : LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 30) {
106 [ + + ]: 63085 : if (!jump_out_of_ibd) jump_out_of_ibd = fuzzed_data_provider.ConsumeBool();
107 [ + + + + : 63085 : if (jump_out_of_ibd && chainman.IsInitialBlockDownload()) chainman.JumpOutOfIbd();
+ - ]
108 [ + - + - ]: 63085 : const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
109 : :
110 [ + - + - ]: 63085 : GetFakeNodeClock().set(ConsumeTime(fuzzed_data_provider));
111 : :
112 [ + - ]: 63085 : CSerializedNetMsg net_msg;
113 [ + - ]: 63085 : net_msg.m_type = random_message_type;
114 : 63085 : net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
115 : :
116 : 63085 : CNode& random_node = *PickValue(fuzzed_data_provider, peers);
117 : :
118 [ + - ]: 63085 : connman.FlushSendBuffer(random_node);
119 [ + - ]: 63085 : (void)connman.ReceiveMsgFrom(random_node, std::move(net_msg));
120 : :
121 : : bool more_work{true};
122 [ + + ]: 367350 : while (more_work) { // Ensure that every message is eventually processed in some way or another
123 [ + - ]: 304265 : random_node.fPauseSend = false;
124 : :
125 : 304265 : try {
126 [ + - ]: 304265 : more_work = connman.ProcessMessagesOnce(random_node);
127 [ - - ]: 0 : } catch (const std::ios_base::failure&) {
128 : 0 : }
129 [ + - ]: 304265 : node.peerman->SendMessages(random_node);
130 : : }
131 : 63085 : }
132 [ + - ]: 5371 : node.validation_signals->SyncWithValidationInterfaceQueue();
133 [ + - ]: 5371 : node.validation_signals->UnregisterValidationInterface(node.peerman.get());
134 [ + - ]: 5371 : node.connman->StopNodes();
135 [ + - ][ + - ]: 10742 : const auto end_sequence{WITH_LOCK(node.mempool->cs, return node.mempool->GetSequence())};
136 [ + - ][ + - : 10742 : if (block_index_size != WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size()) || initial_sequence != end_sequence) {
+ + - + ]
137 : : // Reuse the global chainman and mempool, but reset them when dirty.
138 : 185 : MakeRandDeterministicDANGEROUS(uint256::ZERO);
139 [ + - ]: 370 : ResetChainmanAndMempool(*g_setup);
140 : : }
141 [ + - ]: 10742 : }
|