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