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/time.h>
22 : : #include <test/util/validation.h>
23 : : #include <util/check.h>
24 : : #include <util/time.h>
25 : : #include <validationinterface.h>
26 : :
27 : : #include <cstdlib>
28 : : #include <iostream>
29 : : #include <memory>
30 : : #include <string>
31 : : #include <string_view>
32 : : #include <vector>
33 : :
34 : : namespace {
35 : : TestingSetup* g_setup;
36 : : std::string_view LIMIT_TO_MESSAGE_TYPE{};
37 : :
38 : 74 : void ResetChainman(TestingSetup& setup)
39 : : {
40 : 74 : SetMockTime(setup.m_node.chainman->GetParams().GenesisBlock().Time());
41 [ + - ]: 74 : setup.m_node.chainman.reset();
42 : 74 : setup.m_make_chainman();
43 : 74 : setup.LoadVerifyActivateChainstate();
44 [ + + ]: 14874 : for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
45 : 14800 : node::BlockAssembler::Options options;
46 : 14800 : options.include_dummy_extranonce = true;
47 [ + - ]: 14800 : MineBlock(setup.m_node, options);
48 : 14800 : }
49 : 74 : }
50 : : } // namespace
51 : :
52 : 1 : void initialize_process_message()
53 : : {
54 [ - + ]: 1 : if (const auto val{std::getenv("LIMIT_TO_MESSAGE_TYPE")}) {
55 [ # # ]: 0 : LIMIT_TO_MESSAGE_TYPE = val;
56 [ # # ]: 0 : Assert(std::count(ALL_NET_MESSAGE_TYPES.begin(), ALL_NET_MESSAGE_TYPES.end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
57 : : }
58 : :
59 : 1 : static const auto testing_setup{
60 : : MakeNoLogFileContext<TestingSetup>(
61 : : /*chain_type=*/ChainType::REGTEST,
62 : : {}),
63 [ + - + - : 1 : };
+ - ]
64 : 1 : g_setup = testing_setup.get();
65 : 1 : ResetChainman(*g_setup);
66 : 1 : }
67 : :
68 [ + - ]: 1993 : FUZZ_TARGET(process_message, .init = initialize_process_message)
69 : : {
70 : 1535 : SeedRandomStateForTest(SeedRand::ZEROS);
71 : 1535 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
72 : :
73 : 1535 : auto& node{g_setup->m_node};
74 : 1535 : auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
75 : 1535 : connman.Reset();
76 : 1535 : auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
77 [ + - ]: 3070 : const auto block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
78 : 1535 : NodeClockContext clock_ctx{1610000000s}; // any time to successfully reset ibd
79 [ + - ]: 1535 : chainman.ResetIbd();
80 [ + - ]: 1535 : chainman.DisableNextWrite();
81 : :
82 : : // Reset, so that dangling pointers can be detected by sanitizers.
83 [ + + ]: 1535 : node.banman.reset();
84 [ + - ]: 1535 : node.addrman.reset();
85 [ + - ]: 1535 : node.peerman.reset();
86 [ + - ]: 1535 : node.addrman = std::make_unique<AddrMan>(*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
87 : 3070 : node.peerman = PeerManager::make(connman, *node.addrman,
88 : : /*banman=*/nullptr, chainman,
89 [ + - ]: 1535 : *node.mempool, *node.warnings,
90 : : PeerManager::Options{
91 : : .reconcile_txs = true,
92 : : .deterministic_rng = true,
93 : 1535 : });
94 : :
95 [ + - ]: 1535 : connman.SetMsgProc(node.peerman.get());
96 [ + - ]: 1535 : connman.SetAddrman(*node.addrman);
97 [ + - ]: 1535 : LOCK(NetEventsInterface::g_msgproc_mutex);
98 : :
99 [ + - + - ]: 1535 : const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
100 [ - + - - ]: 1535 : if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
101 : 0 : return;
102 : : }
103 : :
104 [ + - ]: 1535 : node.validation_signals->RegisterValidationInterface(node.peerman.get());
105 : :
106 : 1535 : CNode& p2p_node = *ConsumeNodeAsUniquePtr(fuzzed_data_provider).release();
107 : :
108 [ + - ]: 1535 : connman.AddTestNode(p2p_node);
109 : 1535 : FillNode(fuzzed_data_provider, connman, p2p_node);
110 : :
111 [ + - ]: 1535 : clock_ctx.set(ConsumeTime(fuzzed_data_provider));
112 : :
113 [ + - ]: 1535 : CSerializedNetMsg net_msg;
114 [ + - ]: 1535 : net_msg.m_type = random_message_type;
115 : 1535 : net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
116 : :
117 [ + - ]: 1535 : connman.FlushSendBuffer(p2p_node);
118 [ + - ]: 1535 : (void)connman.ReceiveMsgFrom(p2p_node, std::move(net_msg));
119 : :
120 : : bool more_work{true};
121 [ + + ]: 623189 : while (more_work) {
122 [ + - ]: 621654 : p2p_node.fPauseSend = false;
123 : 621654 : try {
124 [ + - ]: 621654 : more_work = connman.ProcessMessagesOnce(p2p_node);
125 [ - - ]: 0 : } catch (const std::ios_base::failure&) {
126 : 0 : }
127 [ + - ]: 621654 : node.peerman->SendMessages(p2p_node);
128 : : }
129 [ + - ]: 1535 : node.validation_signals->SyncWithValidationInterfaceQueue();
130 [ + - ]: 1535 : node.validation_signals->UnregisterValidationInterface(node.peerman.get());
131 [ + - ]: 1535 : node.connman->StopNodes();
132 [ + - + + ]: 3070 : if (block_index_size != WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())) {
133 : : // Reuse the global chainman, but reset it when it is dirty
134 [ + - ]: 73 : ResetChainman(*g_setup);
135 : : }
136 [ - - + - ]: 3070 : }
|