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 <algorithm>
33 : : #include <array>
34 : : #include <cstdlib>
35 : : #include <functional>
36 : : #include <iostream>
37 : : #include <memory>
38 : : #include <optional>
39 : : #include <string>
40 : : #include <string_view>
41 : : #include <utility>
42 : : #include <vector>
43 : :
44 : : namespace {
45 : : TestingSetup* g_setup;
46 : : std::string_view LIMIT_TO_MESSAGE_TYPE{};
47 : : constexpr ReachabilityGoal PROCESS_MESSAGE_CHANGES_MEMPOOL{"process_message changes the mempool"};
48 : : constexpr ReachabilityGoal PROCESS_MESSAGE_CHANGES_BLOCK_INDEX{"process_message changes the block index"};
49 : :
50 : : } // namespace
51 : :
52 : : extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
53 : :
54 : 1 : void initialize_process_message()
55 : : {
56 : 1 : RegisterReachabilityGoal(PROCESS_MESSAGE_CHANGES_MEMPOOL);
57 : 1 : RegisterReachabilityGoal(PROCESS_MESSAGE_CHANGES_BLOCK_INDEX);
58 : 1 : FakeNodeClock init_clock{}; // Uses the existing mock time
59 [ - + ]: 1 : if (const auto val{std::getenv("LIMIT_TO_MESSAGE_TYPE")}) {
60 [ # # ]: 0 : LIMIT_TO_MESSAGE_TYPE = val;
61 [ # # ]: 0 : Assert(std::count(ALL_NET_MESSAGE_TYPES.begin(), ALL_NET_MESSAGE_TYPES.end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
62 : : }
63 : :
64 : 1 : static const auto testing_setup{
65 : : MakeNoLogFileContext<TestingSetup>(
66 : : /*chain_type=*/ChainType::REGTEST,
67 : : {}),
68 [ + - + - : 1 : };
+ - ]
69 [ + - ]: 1 : g_setup = testing_setup.get();
70 [ + - ]: 2 : ResetChainmanAndMempool(*g_setup, init_clock, {.init_block_filter_basic = true});
71 : 1 : }
72 : :
73 [ + - ]: 3828 : FUZZ_TARGET(process_message, .init = initialize_process_message)
74 : : {
75 : 3352 : SeedRandomStateForTest(SeedRand::ZEROS);
76 : 3352 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
77 : :
78 : 3352 : auto& node{g_setup->m_node};
79 : 3352 : auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
80 : 3352 : connman.Reset();
81 : 3352 : auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
82 [ + - ]: 6704 : const auto initial_block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
83 [ + - ]: 6704 : const auto initial_sequence{WITH_LOCK(node.mempool->cs, return node.mempool->GetSequence())};
84 : 3352 : FakeNodeClock node_clock{1610000000s}; // 2021-01-07, arbitrary
85 [ + - ]: 3352 : FakeSteadyClock steady_clock;
86 [ + - ]: 3352 : chainman.ResetIbd();
87 [ + - ]: 3352 : chainman.DisableNextWrite();
88 : :
89 : : // Reset, so that dangling pointers can be detected by sanitizers.
90 [ + + ]: 3352 : node.banman.reset();
91 [ + - ]: 3352 : node.addrman.reset();
92 [ + - ]: 3352 : node.peerman.reset();
93 [ + - ]: 3352 : node.addrman = std::make_unique<AddrMan>(*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
94 : 3352 : node.peerman = PeerManager::make(connman, *node.addrman,
95 : : /*banman=*/nullptr, chainman,
96 [ + - ]: 3352 : *node.mempool, *node.warnings,
97 : 0 : PeerManager::Options{
98 : : .reconcile_txs = true,
99 : : .deterministic_rng = true,
100 [ + - ]: 3354 : .get_block_filter_index = [&node](BlockFilterType filter_type) {
101 : 2 : return node::GetBlockFilterIndex(node, filter_type);
102 : : },
103 : 3352 : });
104 : :
105 [ + - ]: 3352 : connman.SetMsgProc(node.peerman.get());
106 [ + - ]: 3352 : connman.SetAddrman(*node.addrman);
107 [ + - ]: 3352 : LOCK(NetEventsInterface::g_msgproc_mutex);
108 : :
109 [ + - + - ]: 3352 : const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
110 [ - + - - ]: 3352 : if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
111 : 0 : return;
112 : : }
113 : :
114 [ + - ]: 3352 : node.validation_signals->RegisterValidationInterface(node.peerman.get());
115 : :
116 : 3352 : CNode& p2p_node = *ConsumeNodeAsUniquePtr(fuzzed_data_provider, steady_clock).release();
117 : :
118 [ + - ]: 3352 : connman.AddTestNode(p2p_node);
119 : 3352 : FillNode(fuzzed_data_provider, connman, p2p_node);
120 : :
121 [ + - ]: 3352 : node_clock.set(ConsumeTime(fuzzed_data_provider));
122 : :
123 [ + - ]: 3352 : CSerializedNetMsg net_msg;
124 [ + - ]: 3352 : net_msg.m_type = random_message_type;
125 : 3352 : net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
126 : :
127 [ + - ]: 3352 : connman.FlushSendBuffer(p2p_node);
128 [ + - ]: 3352 : (void)connman.ReceiveMsgFrom(p2p_node, std::move(net_msg));
129 : :
130 [ + + ]: 3352 : if (fuzzed_data_provider.ConsumeBool()) {
131 [ + - ]: 912 : chainman.JumpOutOfIbd();
132 : : }
133 : :
134 : : bool more_work{true};
135 [ + + ]: 888092 : while (more_work) {
136 [ + - ]: 884740 : p2p_node.fPauseSend = false;
137 : 884740 : try {
138 [ + - ]: 884740 : more_work = connman.ProcessMessagesOnce(p2p_node);
139 [ - - ]: 0 : } catch (const std::ios_base::failure&) {
140 : 0 : }
141 [ + - ]: 884740 : node.peerman->SendMessages(p2p_node);
142 : : }
143 [ + - ]: 3352 : node.validation_signals->SyncWithValidationInterfaceQueue();
144 [ + - ]: 3352 : node.validation_signals->UnregisterValidationInterface(node.peerman.get());
145 [ + - ]: 3352 : node.connman->StopNodes();
146 [ + - ][ + - ]: 6704 : const auto end_sequence{WITH_LOCK(node.mempool->cs, return node.mempool->GetSequence())};
147 [ + - ][ + - ]: 6704 : const auto end_block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
148 [ + - ]: 3352 : ObserveReachabilityGoal(initial_sequence != end_sequence, PROCESS_MESSAGE_CHANGES_MEMPOOL);
149 [ + - ]: 3352 : ObserveReachabilityGoal(initial_block_index_size != end_block_index_size, PROCESS_MESSAGE_CHANGES_BLOCK_INDEX);
150 [ + + ]: 3352 : if (initial_block_index_size != end_block_index_size || initial_sequence != end_sequence) {
151 : : // Reuse the global chainman and mempool, but reset them when dirty.
152 : 138 : MakeRandDeterministicDANGEROUS(uint256::ZERO);
153 [ + - ]: 276 : ResetChainmanAndMempool(*g_setup, node_clock, {.init_block_filter_basic = true});
154 : : }
155 [ - - + - ]: 6704 : }
|