Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
process_message.cpp
Go to the documentation of this file.
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>
8#include <net.h>
9#include <net_processing.h>
10#include <primitives/block.h>
12#include <protocol.h>
13#include <sync.h>
15#include <test/fuzz/fuzz.h>
16#include <test/fuzz/util.h>
17#include <test/fuzz/util/net.h>
19#include <test/util/net.h>
20#include <test/util/random.h>
22#include <test/util/time.h>
24#include <uint256.h>
25#include <util/check.h>
26#include <util/time.h>
27#include <validation.h>
28#include <validationinterface.h>
29
30#include <algorithm>
31#include <array>
32#include <cstdlib>
33#include <functional>
34#include <iostream>
35#include <memory>
36#include <optional>
37#include <string>
38#include <string_view>
39#include <utility>
40#include <vector>
41
42namespace {
44std::string_view LIMIT_TO_MESSAGE_TYPE{};
45constexpr ReachabilityGoal PROCESS_MESSAGE_CHANGES_MEMPOOL{"process_message changes the mempool"};
46constexpr ReachabilityGoal PROCESS_MESSAGE_CHANGES_BLOCK_INDEX{"process_message changes the block index"};
47
48} // namespace
49
50extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
51
53{
56 FakeNodeClock init_clock{}; // Uses the existing mock time
57 if (const auto val{std::getenv("LIMIT_TO_MESSAGE_TYPE")}) {
59 Assert(std::count(ALL_NET_MESSAGE_TYPES.begin(), ALL_NET_MESSAGE_TYPES.end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
60 }
61
62 static const auto testing_setup{
64 /*chain_type=*/ChainType::REGTEST,
65 {}),
66 };
67 g_setup = testing_setup.get();
68 ResetChainmanAndMempool(*g_setup, init_clock, {.init_block_filter_basic = true});
69}
70
72{
74 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
75
76 auto& node{g_setup->m_node};
77 auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
78 connman.Reset();
79 auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
80 const auto initial_block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
81 const auto initial_sequence{WITH_LOCK(node.mempool->cs, return node.mempool->GetSequence())};
82 FakeNodeClock node_clock{1610000000s}; // 2021-01-07, arbitrary
83 FakeSteadyClock steady_clock;
84 chainman.ResetIbd();
85 chainman.DisableNextWrite();
86
87 // Reset, so that dangling pointers can be detected by sanitizers.
88 node.banman.reset();
89 node.addrman.reset();
90 node.peerman.reset();
91 node.addrman = std::make_unique<AddrMan>(*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
92 node.peerman = PeerManager::make(connman, *node.addrman,
93 /*banman=*/nullptr, chainman,
94 *node.mempool, *node.warnings,
96 .reconcile_txs = true,
97 .deterministic_rng = true,
98 });
99
100 connman.SetMsgProc(node.peerman.get());
101 connman.SetAddrman(*node.addrman);
103
106 return;
107 }
108
109 node.validation_signals->RegisterValidationInterface(node.peerman.get());
110
111 CNode& p2p_node = *ConsumeNodeAsUniquePtr(fuzzed_data_provider, steady_clock).release();
112
113 connman.AddTestNode(p2p_node);
115
117
121
122 connman.FlushSendBuffer(p2p_node);
123 (void)connman.ReceiveMsgFrom(p2p_node, std::move(net_msg));
124
126 chainman.JumpOutOfIbd();
127 }
128
129 bool more_work{true};
130 while (more_work) {
131 p2p_node.fPauseSend = false;
132 try {
133 more_work = connman.ProcessMessagesOnce(p2p_node);
134 } catch (const std::ios_base::failure&) {
135 }
136 node.peerman->SendMessages(p2p_node);
137 }
138 node.validation_signals->SyncWithValidationInterfaceQueue();
139 node.validation_signals->UnregisterValidationInterface(node.peerman.get());
140 node.connman->StopNodes();
141 const auto end_sequence{WITH_LOCK(node.mempool->cs, return node.mempool->GetSequence())};
142 const auto end_block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
146 // Reuse the global chainman and mempool, but reset them when dirty.
148 ResetChainmanAndMempool(*g_setup, node_clock, {.init_block_filter_basic = true});
149 }
150}
const TestingSetup * g_setup
#define Assert(val)
Identity function.
Definition check.h:119
static constexpr size_t MESSAGE_TYPE_SIZE
Definition protocol.h:31
Information about a peer.
Definition net.h:686
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition time.h:54
Helper to initialize the global MockableSteadyClock, let a duration elapse, and reset it after use in...
Definition time.h:29
std::string ConsumeBytesAsString(size_t num_bytes)
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
Definition net.h:1049
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, node::Warnings &warnings, Options opts)
256-bit opaque blob.
Definition uint256.h:196
static const uint256 ZERO
Definition uint256.h:204
#define FUZZ_TARGET(...)
Definition fuzz.h:35
Definition basic.cpp:11
constexpr unsigned int MAX_PROTOCOL_MESSAGE_LENGTH
Maximum length of incoming protocol messages (no message over 4 MB is currently acceptable).
Definition net.h:66
void MakeRandDeterministicDANGEROUS(const uint256 &seed) noexcept
Internal function to set g_determinstic_rng.
Definition random.cpp:595
void initialize_process_message()
const std::array ALL_NET_MESSAGE_TYPES
All known message types (see above).
Definition protocol.h:274
void ObserveReachabilityGoal(bool reached, const ReachabilityGoal &goal)
Record whether a registered goal is satisfied by at least one fuzz input.
void RegisterReachabilityGoal(const ReachabilityGoal &goal)
Register a goal before executing any fuzz inputs.
node::NodeContext m_node
std::string m_type
Definition net.h:142
void Reset()
Reset the internal state.
Definition net.cpp:83
A reachability condition explicitly registered by a fuzz target.
Testing setup that configures a complete environment.
#define LOCK(cs)
Definition sync.h:268
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition sync.h:299
SeedRandomStateForTest(SeedRand::ZEROS)
void FillNode(FuzzedDataProvider &fuzzed_data_provider, ConnmanTestMsg &connman, CNode &node) noexcept
Definition net.cpp:450
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, FakeSteadyClock &clock, const std::optional< NodeId > &node_id_in=std::nullopt)
Definition net.h:310
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition util.cpp:34
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition util.h:63
@ ZEROS
Seed with a compile time constant of zeros.
std::vector< std::pair< COutPoint, CAmount > > ResetChainmanAndMempool(TestingSetup &setup, FakeNodeClock &node_clock, const ResetOptions &opts)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
FuzzedDataProvider & fuzzed_data_provider
Definition fees.cpp:46