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 <primitives/transaction.h>
11 : : #include <protocol.h>
12 : : #include <script/script.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/mining.h>
19 : : #include <test/util/net.h>
20 : : #include <test/util/setup_common.h>
21 : : #include <test/util/validation.h>
22 : : #include <util/check.h>
23 : : #include <util/time.h>
24 : : #include <validationinterface.h>
25 : :
26 : : #include <cstdlib>
27 : : #include <iostream>
28 : : #include <memory>
29 : : #include <string>
30 : : #include <string_view>
31 : : #include <vector>
32 : :
33 : : namespace {
34 : : TestingSetup* g_setup;
35 : : std::string_view LIMIT_TO_MESSAGE_TYPE{};
36 : :
37 : 67 : void ResetChainman(TestingSetup& setup)
38 : : {
39 : 67 : SetMockTime(setup.m_node.chainman->GetParams().GenesisBlock().Time());
40 [ + - ]: 67 : setup.m_node.chainman.reset();
41 : 67 : setup.m_make_chainman();
42 : 67 : setup.LoadVerifyActivateChainstate();
43 [ + + ]: 13467 : for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
44 [ + - ]: 26800 : MineBlock(setup.m_node, {});
45 : : }
46 : 67 : setup.m_node.validation_signals->SyncWithValidationInterfaceQueue();
47 [ + - + - ]: 26867 : }
48 : : } // namespace
49 : :
50 : 1 : void initialize_process_message()
51 : : {
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 : 1 : ResetChainman(*g_setup);
64 : 1 : }
65 : :
66 [ + - ]: 5811 : FUZZ_TARGET(process_message, .init = initialize_process_message)
67 : : {
68 : 5361 : SeedRandomStateForTest(SeedRand::ZEROS);
69 : 5361 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
70 : :
71 : 5361 : auto& node{g_setup->m_node};
72 : 5361 : auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
73 : 5361 : connman.ResetAddrCache();
74 : 5361 : connman.ResetMaxOutboundCycle();
75 : 5361 : auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
76 [ + - ]: 10722 : const auto block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
77 : 5361 : SetMockTime(1610000000); // any time to successfully reset ibd
78 : 5361 : chainman.ResetIbd();
79 : 5361 : chainman.DisableNextWrite();
80 : :
81 : : // Reset, so that dangling pointers can be detected by sanitizers.
82 [ + + ]: 5361 : node.banman.reset();
83 [ + - ]: 5361 : node.addrman.reset();
84 [ + - ]: 5361 : node.peerman.reset();
85 : 5361 : node.addrman = std::make_unique<AddrMan>(*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
86 : 10722 : node.peerman = PeerManager::make(connman, *node.addrman,
87 : : /*banman=*/nullptr, chainman,
88 : 5361 : *node.mempool, *node.warnings,
89 : : PeerManager::Options{
90 : : .reconcile_txs = true,
91 : : .deterministic_rng = true,
92 : 5361 : });
93 : :
94 [ + - ]: 5361 : connman.SetMsgProc(node.peerman.get());
95 : 5361 : connman.SetAddrman(*node.addrman);
96 : 5361 : LOCK(NetEventsInterface::g_msgproc_mutex);
97 : :
98 [ + - + - ]: 5361 : const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
99 [ - + - - ]: 5361 : if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
100 : 0 : return;
101 : : }
102 : 5361 : CNode& p2p_node = *ConsumeNodeAsUniquePtr(fuzzed_data_provider).release();
103 : :
104 [ + - ]: 5361 : connman.AddTestNode(p2p_node);
105 : 5361 : FillNode(fuzzed_data_provider, connman, p2p_node);
106 : :
107 : 5361 : const auto mock_time = ConsumeTime(fuzzed_data_provider);
108 [ + - ]: 5361 : SetMockTime(mock_time);
109 : :
110 [ + - ]: 5361 : CSerializedNetMsg net_msg;
111 [ + - ]: 5361 : net_msg.m_type = random_message_type;
112 : 5361 : net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
113 : :
114 [ + - ]: 5361 : connman.FlushSendBuffer(p2p_node);
115 [ + - ]: 5361 : (void)connman.ReceiveMsgFrom(p2p_node, std::move(net_msg));
116 : :
117 : : bool more_work{true};
118 [ + + ]: 566460 : while (more_work) {
119 [ + - ]: 561099 : p2p_node.fPauseSend = false;
120 : 561099 : try {
121 [ + - ]: 561099 : more_work = connman.ProcessMessagesOnce(p2p_node);
122 [ - - ]: 0 : } catch (const std::ios_base::failure&) {
123 : 0 : }
124 [ + - ]: 561099 : node.peerman->SendMessages(&p2p_node);
125 : : }
126 [ + - ]: 5361 : node.validation_signals->SyncWithValidationInterfaceQueue();
127 [ + - ]: 5361 : node.connman->StopNodes();
128 [ + - + + ]: 10722 : if (block_index_size != WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())) {
129 : : // Reuse the global chainman, but reset it when it is dirty
130 [ + - ]: 66 : ResetChainman(*g_setup);
131 : : }
132 [ - - + - ]: 10722 : }
|