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>
9#include <net.h>
10#include <net_processing.h>
11#include <node/mining_types.h>
12#include <primitives/block.h>
14#include <protocol.h>
15#include <sync.h>
17#include <test/fuzz/fuzz.h>
18#include <test/fuzz/util.h>
19#include <test/fuzz/util/net.h>
20#include <test/util/mining.h>
21#include <test/util/net.h>
22#include <test/util/random.h>
24#include <test/util/time.h>
26#include <util/check.h>
27#include <util/time.h>
28#include <validation.h>
29#include <validationinterface.h>
30
31#include <algorithm>
32#include <array>
33#include <cstdlib>
34#include <functional>
35#include <iostream>
36#include <memory>
37#include <optional>
38#include <string>
39#include <string_view>
40#include <utility>
41#include <vector>
42
43namespace {
45std::string_view LIMIT_TO_MESSAGE_TYPE{};
46
47void ResetChainman(TestingSetup& setup)
48{
49 SetMockTime(setup.m_node.chainman->GetParams().GenesisBlock().Time());
50 setup.m_node.chainman.reset();
51 setup.m_make_chainman();
52 setup.LoadVerifyActivateChainstate();
53 for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
55 MineBlock(setup.m_node, options);
56 }
57}
58} // namespace
59
61{
62 if (const auto val{std::getenv("LIMIT_TO_MESSAGE_TYPE")}) {
63 LIMIT_TO_MESSAGE_TYPE = val;
64 Assert(std::count(ALL_NET_MESSAGE_TYPES.begin(), ALL_NET_MESSAGE_TYPES.end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
65 }
66
67 static const auto testing_setup{
68 MakeNoLogFileContext<TestingSetup>(
69 /*chain_type=*/ChainType::REGTEST,
70 {}),
71 };
72 g_setup = testing_setup.get();
73 ResetChainman(*g_setup);
74}
75
77{
79 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
80
81 auto& node{g_setup->m_node};
82 auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
83 connman.Reset();
84 auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
85 const auto block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
86 NodeClockContext clock_ctx{1610000000s}; // any time to successfully reset ibd
87 chainman.ResetIbd();
88 chainman.DisableNextWrite();
89
90 // Reset, so that dangling pointers can be detected by sanitizers.
91 node.banman.reset();
92 node.addrman.reset();
93 node.peerman.reset();
94 node.addrman = std::make_unique<AddrMan>(*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
95 node.peerman = PeerManager::make(connman, *node.addrman,
96 /*banman=*/nullptr, chainman,
97 *node.mempool, *node.warnings,
99 .reconcile_txs = true,
100 .deterministic_rng = true,
101 });
102
103 connman.SetMsgProc(node.peerman.get());
104 connman.SetAddrman(*node.addrman);
106
107 const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
108 if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
109 return;
110 }
111
112 node.validation_signals->RegisterValidationInterface(node.peerman.get());
113
114 CNode& p2p_node = *ConsumeNodeAsUniquePtr(fuzzed_data_provider).release();
115
116 connman.AddTestNode(p2p_node);
117 FillNode(fuzzed_data_provider, connman, p2p_node);
118
119 clock_ctx.set(ConsumeTime(fuzzed_data_provider));
120
121 CSerializedNetMsg net_msg;
122 net_msg.m_type = random_message_type;
124
125 connman.FlushSendBuffer(p2p_node);
126 (void)connman.ReceiveMsgFrom(p2p_node, std::move(net_msg));
127
128 bool more_work{true};
129 while (more_work) {
130 p2p_node.fPauseSend = false;
131 try {
132 more_work = connman.ProcessMessagesOnce(p2p_node);
133 } catch (const std::ios_base::failure&) {
134 }
135 node.peerman->SendMessages(p2p_node);
136 }
137 node.validation_signals->SyncWithValidationInterfaceQueue();
138 node.validation_signals->UnregisterValidationInterface(node.peerman.get());
139 node.connman->StopNodes();
140 if (block_index_size != WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())) {
141 // Reuse the global chainman, but reset it when it is dirty
142 ResetChainman(*g_setup);
143 }
144}
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:680
std::atomic_bool fPauseSend
Definition: net.h:744
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:1030
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:40
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, node::Warnings &warnings, Options opts)
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
Definition: consensus.h:19
is used externally by mining IPC clients, so it should only declare simple data definitions.
Definition: basic.cpp:8
Definition: messages.h:21
static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH
Maximum length of incoming protocol messages (no message over 4 MB is currently acceptable).
Definition: net.h:65
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:270
node::NodeContext m_node
Definition: setup_common.h:58
std::string m_type
Definition: net.h:137
std::vector< unsigned char > data
Definition: net.h:136
void Reset()
Reset the internal state.
Definition: net.cpp:83
Testing setup that configures a complete environment.
Definition: setup_common.h:113
Block template creation options.
Definition: mining_types.h:33
#define LOCK(cs)
Definition: sync.h:268
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:299
void FillNode(FuzzedDataProvider &fuzzed_data_provider, ConnmanTestMsg &connman, CNode &node) noexcept
Definition: net.cpp:457
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, 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:59
COutPoint MineBlock(const NodeContext &node, const node::BlockCreateOptions &assembler_options)
Returns the generated coin.
Definition: mining.cpp:78
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
static int setup(void)
Definition: tests.c:8040
static int count
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:52
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:39