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