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 <net.h>
7 : : #include <net_processing.h>
8 : : #include <protocol.h>
9 : : #include <sync.h>
10 : : #include <test/fuzz/FuzzedDataProvider.h>
11 : : #include <test/fuzz/fuzz.h>
12 : : #include <test/fuzz/util.h>
13 : : #include <test/fuzz/util/net.h>
14 : : #include <test/fuzz/util/reachability.h>
15 : : #include <test/util/net.h>
16 : : #include <test/util/setup_common.h>
17 : : #include <test/util/time.h>
18 : : #include <test/util/validation.h>
19 : : #include <util/time.h>
20 : : #include <validationinterface.h>
21 : :
22 : : #include <ios>
23 : : #include <utility>
24 : : #include <vector>
25 : :
26 : : namespace {
27 : : TestingSetup* g_setup;
28 : : constexpr ReachabilityGoal P2P_HANDSHAKE_COMPLETES{"p2p_handshake completes a handshake"};
29 : :
30 : 1 : void initialize()
31 : : {
32 : 1 : RegisterReachabilityGoal(P2P_HANDSHAKE_COMPLETES);
33 : 1 : static const auto testing_setup = MakeNoLogFileContext<TestingSetup>(
34 [ + - + - : 1 : /*chain_type=*/ChainType::REGTEST);
+ - ]
35 : 1 : g_setup = testing_setup.get();
36 : 1 : }
37 : : } // namespace
38 : :
39 [ + - ]: 2229 : FUZZ_TARGET(p2p_handshake, .init = ::initialize)
40 : : {
41 : 1753 : SeedRandomStateForTest(SeedRand::ZEROS);
42 : 1753 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
43 : :
44 : 1753 : auto& node{g_setup->m_node};
45 : 1753 : auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
46 : 1753 : connman.Reset();
47 : 1753 : auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
48 : 1753 : FakeNodeClock clock{1610000000s}; // 2021-01-07, arbitrary
49 [ + - ]: 1753 : FakeSteadyClock steady_clock;
50 [ + - ]: 1753 : chainman.ResetIbd();
51 : :
52 [ + + ]: 1753 : node.banman.reset();
53 [ + - ]: 1753 : node.addrman.reset();
54 [ + - ]: 1753 : node.peerman.reset();
55 : 3506 : node.addrman = std::make_unique<AddrMan>(
56 [ + - ]: 1753 : *node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
57 : 3506 : node.peerman = PeerManager::make(connman, *node.addrman,
58 : : /*banman=*/nullptr, chainman,
59 [ + - ]: 1753 : *node.mempool, *node.warnings,
60 : : PeerManager::Options{
61 : : .reconcile_txs = true,
62 : : .deterministic_rng = true,
63 : 1753 : });
64 [ + - ]: 1753 : connman.SetMsgProc(node.peerman.get());
65 [ + - ]: 1753 : connman.SetAddrman(*node.addrman);
66 : :
67 [ + - ]: 1753 : LOCK(NetEventsInterface::g_msgproc_mutex);
68 : :
69 : 1753 : std::vector<CNode*> peers;
70 : 1753 : const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
71 [ + + ]: 5221 : for (int i = 0; i < num_peers_to_add; ++i) {
72 [ + - ]: 3468 : peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, steady_clock, i).release());
73 [ + - ]: 3468 : connman.AddTestNode(*peers.back());
74 : 3468 : node.peerman->InitializeNode(
75 [ + - ]: 3468 : *peers.back(),
76 : 3468 : static_cast<ServiceFlags>(fuzzed_data_provider.ConsumeIntegral<uint64_t>()));
77 : : }
78 : :
79 : : // Toggle IBD from within the loop, so that some messages may be processed
80 : : // under IBD and the rest after leaving it. JumpOutOfIbd() latches, so guard
81 : : // it to call at most once.
82 : : bool jump_out_of_ibd{false};
83 [ + + + + ]: 26945 : LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 100) {
84 : 25192 : CNode& connection = *PickValue(fuzzed_data_provider, peers);
85 [ + + + + ]: 25192 : if (connection.fDisconnect || connection.fSuccessfullyConnected) {
86 : : // Skip if the connection was disconnected or if the version
87 : : // handshake was already completed.
88 : 1604 : continue;
89 : : }
90 : :
91 [ + + ]: 23588 : if (!jump_out_of_ibd) jump_out_of_ibd = fuzzed_data_provider.ConsumeBool();
92 [ + + + + : 23588 : if (jump_out_of_ibd && chainman.IsInitialBlockDownload()) chainman.JumpOutOfIbd();
+ - ]
93 : :
94 : 47176 : clock += std::chrono::seconds{
95 [ + - ]: 23588 : fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(
96 : : -std::chrono::seconds{10min}.count(), // Allow mocktime to go backwards slightly
97 : : std::chrono::seconds{TIMEOUT_INTERVAL}.count()),
98 [ + - ]: 23588 : };
99 : :
100 : 23588 : CSerializedNetMsg net_msg;
101 [ + - ]: 23588 : net_msg.m_type = PickValue(fuzzed_data_provider, ALL_NET_MESSAGE_TYPES);
102 : 23588 : net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
103 : :
104 [ + - ]: 23588 : connman.FlushSendBuffer(connection);
105 [ + - ]: 23588 : (void)connman.ReceiveMsgFrom(connection, std::move(net_msg));
106 : :
107 : : bool more_work{true};
108 [ + + ]: 48516 : while (more_work) {
109 [ + - ]: 24928 : connection.fPauseSend = false;
110 : :
111 : 24928 : try {
112 [ + - ]: 24928 : more_work = connman.ProcessMessagesOnce(connection);
113 [ - - ]: 0 : } catch (const std::ios_base::failure&) {
114 : 0 : }
115 [ + - ]: 24928 : node.peerman->SendMessages(connection);
116 : : }
117 : 23588 : }
118 : :
119 : 1753 : bool completed_handshake{false};
120 [ + + ]: 5221 : for (const CNode* peer : peers) {
121 : 3468 : completed_handshake |= peer->fSuccessfullyConnected;
122 : : }
123 [ + - ]: 1753 : ObserveReachabilityGoal(completed_handshake, P2P_HANDSHAKE_COMPLETES);
124 [ + - ]: 1753 : node.connman->StopNodes();
125 [ + - ]: 3506 : }
|