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