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