Bitcoin Core 31.99.0
P2P Digital Currency
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>
18#include <test/util/net.h>
19#include <test/util/random.h>
21#include <test/util/time.h>
23#include <uint256.h>
24#include <util/check.h>
25#include <util/time.h>
26#include <validation.h>
27#include <validationinterface.h>
28
29#include <algorithm>
30#include <array>
31#include <cstdlib>
32#include <functional>
33#include <iostream>
34#include <memory>
35#include <optional>
36#include <string>
37#include <string_view>
38#include <utility>
39#include <vector>
40
41namespace {
43std::string_view LIMIT_TO_MESSAGE_TYPE{};
44
45} // namespace
46
47extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
48
50{
51 FakeNodeClock init_clock{}; // Uses the existing mock time
52 if (const auto val{std::getenv("LIMIT_TO_MESSAGE_TYPE")}) {
53 LIMIT_TO_MESSAGE_TYPE = val;
54 Assert(std::count(ALL_NET_MESSAGE_TYPES.begin(), ALL_NET_MESSAGE_TYPES.end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
55 }
56
57 static const auto testing_setup{
58 MakeNoLogFileContext<TestingSetup>(
59 /*chain_type=*/ChainType::REGTEST,
60 {}),
61 };
62 g_setup = testing_setup.get();
63 ResetChainmanAndMempool(*g_setup, init_clock);
64}
65
67{
69 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
70
71 auto& node{g_setup->m_node};
72 auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
73 connman.Reset();
74 auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
75 const auto block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
76 const auto initial_sequence{WITH_LOCK(node.mempool->cs, return node.mempool->GetSequence())};
77 FakeNodeClock node_clock{1610000000s}; // 2021-01-07, arbitrary
78 FakeSteadyClock steady_clock;
79 chainman.ResetIbd();
80 chainman.DisableNextWrite();
81
82 // Reset, so that dangling pointers can be detected by sanitizers.
83 node.banman.reset();
84 node.addrman.reset();
85 node.peerman.reset();
86 node.addrman = std::make_unique<AddrMan>(*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
87 node.peerman = PeerManager::make(connman, *node.addrman,
88 /*banman=*/nullptr, chainman,
89 *node.mempool, *node.warnings,
91 .reconcile_txs = true,
92 .deterministic_rng = true,
93 });
94
95 connman.SetMsgProc(node.peerman.get());
96 connman.SetAddrman(*node.addrman);
98
99 const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
100 if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
101 return;
102 }
103
104 node.validation_signals->RegisterValidationInterface(node.peerman.get());
105
106 CNode& p2p_node = *ConsumeNodeAsUniquePtr(fuzzed_data_provider, steady_clock).release();
107
108 connman.AddTestNode(p2p_node);
109 FillNode(fuzzed_data_provider, connman, p2p_node);
110
111 node_clock.set(ConsumeTime(fuzzed_data_provider));
112
113 CSerializedNetMsg net_msg;
114 net_msg.m_type = random_message_type;
116
117 connman.FlushSendBuffer(p2p_node);
118 (void)connman.ReceiveMsgFrom(p2p_node, std::move(net_msg));
119
121 chainman.JumpOutOfIbd();
122 }
123
124 bool more_work{true};
125 while (more_work) {
126 p2p_node.fPauseSend = false;
127 try {
128 more_work = connman.ProcessMessagesOnce(p2p_node);
129 } catch (const std::ios_base::failure&) {
130 }
131 node.peerman->SendMessages(p2p_node);
132 }
133 node.validation_signals->SyncWithValidationInterfaceQueue();
134 node.validation_signals->UnregisterValidationInterface(node.peerman.get());
135 node.connman->StopNodes();
136 const auto end_sequence{WITH_LOCK(node.mempool->cs, return node.mempool->GetSequence())};
137 if (block_index_size != WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size()) || initial_sequence != end_sequence) {
138 // Reuse the global chainman and mempool, but reset them when dirty.
140 ResetChainmanAndMempool(*g_setup, node_clock);
141 }
142}
const TestingSetup * g_setup
#define Assert(val)
Identity function.
Definition: check.h:116
static constexpr size_t MESSAGE_TYPE_SIZE
Definition: protocol.h:31
Information about a peer.
Definition: net.h:683
std::atomic_bool fPauseSend
Definition: net.h:751
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:1044
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
Definition: basic.cpp:11
Definition: messages.h:21
constexpr unsigned int MAX_PROTOCOL_MESSAGE_LENGTH
Maximum length of incoming protocol messages (no message over 4 MB is currently acceptable).
Definition: net.h:65
void MakeRandDeterministicDANGEROUS(const uint256 &seed) noexcept
Internal function to set g_determinstic_rng.
Definition: random.cpp:595
FUZZ_TARGET(process_message,.init=initialize_process_message)
void initialize_process_message()
const std::array ALL_NET_MESSAGE_TYPES
All known message types (see above).
Definition: protocol.h:274
node::NodeContext m_node
Definition: setup_common.h:60
std::string m_type
Definition: net.h:139
std::vector< unsigned char > data
Definition: net.h:138
void Reset()
Reset the internal state.
Definition: net.cpp:83
Testing setup that configures a complete environment.
Definition: setup_common.h:115
#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)
Definition: validation.cpp:108
static int count
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:45