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 <banman.h>
6 : : #include <consensus/consensus.h>
7 : : #include <net.h>
8 : : #include <net_processing.h>
9 : : #include <node/warnings.h>
10 : : #include <protocol.h>
11 : : #include <script/script.h>
12 : : #include <sync.h>
13 : : #include <test/fuzz/FuzzedDataProvider.h>
14 : : #include <test/fuzz/fuzz.h>
15 : : #include <test/fuzz/util.h>
16 : : #include <test/fuzz/util/net.h>
17 : : #include <test/util/mining.h>
18 : : #include <test/util/net.h>
19 : : #include <test/util/setup_common.h>
20 : : #include <test/util/time.h>
21 : : #include <test/util/validation.h>
22 : : #include <util/time.h>
23 : : #include <validationinterface.h>
24 : :
25 : : #include <ios>
26 : : #include <string>
27 : : #include <utility>
28 : : #include <vector>
29 : :
30 : : namespace {
31 : : TestingSetup* g_setup;
32 : :
33 : 151 : void ResetChainman(TestingSetup& setup)
34 : : {
35 : 151 : SetMockTime(setup.m_node.chainman->GetParams().GenesisBlock().Time());
36 [ + - ]: 151 : setup.m_node.chainman.reset();
37 : 151 : setup.m_make_chainman();
38 : 151 : setup.LoadVerifyActivateChainstate();
39 : 151 : node::BlockAssembler::Options options;
40 [ + + ]: 30351 : for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
41 [ + - ]: 30200 : MineBlock(setup.m_node, options);
42 : : }
43 : 151 : }
44 : : } // namespace
45 : :
46 : 1 : void initialize_process_messages()
47 : : {
48 : 1 : static const auto testing_setup{
49 : : MakeNoLogFileContext<TestingSetup>(
50 : : /*chain_type=*/ChainType::REGTEST,
51 : : {}),
52 [ + - + - : 1 : };
+ - ]
53 : 1 : g_setup = testing_setup.get();
54 : 1 : ResetChainman(*g_setup);
55 : 1 : }
56 : :
57 [ + - ]: 4243 : FUZZ_TARGET(process_messages, .init = initialize_process_messages)
58 : : {
59 : 3783 : SeedRandomStateForTest(SeedRand::ZEROS);
60 : 3783 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
61 : :
62 : 3783 : auto& node{g_setup->m_node};
63 : 3783 : auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
64 : 3783 : connman.Reset();
65 : 3783 : auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
66 [ + - ]: 7566 : const auto block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
67 : 3783 : NodeClockContext clock_ctx{1610000000s}; // any time to successfully reset ibd
68 [ + - ]: 3783 : chainman.ResetIbd();
69 [ + - ]: 3783 : chainman.DisableNextWrite();
70 : :
71 : : // Reset, so that dangling pointers can be detected by sanitizers.
72 [ + + ]: 3783 : node.banman.reset();
73 [ + - ]: 3783 : node.addrman.reset();
74 [ + - ]: 3783 : node.peerman.reset();
75 [ + - ]: 3783 : node.addrman = std::make_unique<AddrMan>(*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
76 : 7566 : node.peerman = PeerManager::make(connman, *node.addrman,
77 : : /*banman=*/nullptr, chainman,
78 [ + - ]: 3783 : *node.mempool, *node.warnings,
79 : : PeerManager::Options{
80 : : .reconcile_txs = true,
81 : : .deterministic_rng = true,
82 : 3783 : });
83 [ + - ]: 3783 : connman.SetMsgProc(node.peerman.get());
84 [ + - ]: 3783 : connman.SetAddrman(*node.addrman);
85 : :
86 [ + - ]: 3783 : node.validation_signals->RegisterValidationInterface(node.peerman.get());
87 : :
88 [ + - ]: 3783 : LOCK(NetEventsInterface::g_msgproc_mutex);
89 : :
90 : 3783 : std::vector<CNode*> peers;
91 : 3783 : const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
92 [ + + ]: 10303 : for (int i = 0; i < num_peers_to_add; ++i) {
93 [ + - ]: 6520 : peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
94 : 6520 : CNode& p2p_node = *peers.back();
95 : :
96 : 6520 : FillNode(fuzzed_data_provider, connman, p2p_node);
97 : :
98 [ + - ]: 6520 : connman.AddTestNode(p2p_node);
99 : : }
100 : :
101 [ + + + + ]: 51945 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30)
102 : : {
103 [ + - + - ]: 48162 : const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
104 : :
105 [ + - ]: 48162 : clock_ctx.set(ConsumeTime(fuzzed_data_provider));
106 : :
107 [ + - ]: 48162 : CSerializedNetMsg net_msg;
108 [ + - ]: 48162 : net_msg.m_type = random_message_type;
109 : 48162 : net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
110 : :
111 : 48162 : CNode& random_node = *PickValue(fuzzed_data_provider, peers);
112 : :
113 [ + - ]: 48162 : connman.FlushSendBuffer(random_node);
114 [ + - ]: 48162 : (void)connman.ReceiveMsgFrom(random_node, std::move(net_msg));
115 : :
116 : : bool more_work{true};
117 [ + + ]: 301150 : while (more_work) { // Ensure that every message is eventually processed in some way or another
118 [ + - ]: 252988 : random_node.fPauseSend = false;
119 : :
120 : 252988 : try {
121 [ + - ]: 252988 : more_work = connman.ProcessMessagesOnce(random_node);
122 [ - - ]: 0 : } catch (const std::ios_base::failure&) {
123 : 0 : }
124 [ + - ]: 252988 : node.peerman->SendMessages(random_node);
125 : : }
126 : 48162 : }
127 [ + - ]: 3783 : node.validation_signals->SyncWithValidationInterfaceQueue();
128 [ + - ]: 3783 : node.validation_signals->UnregisterValidationInterface(node.peerman.get());
129 [ + - ]: 3783 : node.connman->StopNodes();
130 [ + - + + ]: 7566 : if (block_index_size != WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())) {
131 : : // Reuse the global chainman, but reset it when it is dirty
132 [ + - ]: 150 : ResetChainman(*g_setup);
133 : : }
134 [ + - ]: 7566 : }
|