Skip to content

Commit fa11eea

Browse files
author
MarcoFalke
committed
fuzz: Avoid non-determinism in process_message(s) target (PeerMan)
The PeerManager has several members, such as the FastRandomContext, which need to be reset before every run to avoid leaking state from one run into the next. Also, style fixups in p2p_handshake.cpp, where this code is copied from.
1 parent 9f713b8 commit fa11eea

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/test/fuzz/p2p_handshake.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ FUZZ_TARGET(p2p_handshake, .init = ::initialize)
4242
SeedRandomStateForTest(SeedRand::ZEROS);
4343
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
4444

45-
ConnmanTestMsg& connman = static_cast<ConnmanTestMsg&>(*g_setup->m_node.connman);
45+
auto& connman = static_cast<ConnmanTestMsg&>(*g_setup->m_node.connman);
4646
auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman);
4747
SetMockTime(1610000000); // any time to successfully reset ibd
4848
chainman.ResetIbd();
4949

5050
node::Warnings warnings{};
5151
NetGroupManager netgroupman{{}};
52-
AddrMan addrman{netgroupman, /*deterministic=*/true, 0};
52+
AddrMan addrman{netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0};
5353
auto peerman = PeerManager::make(connman, addrman,
5454
/*banman=*/nullptr, chainman,
5555
*g_setup->m_node.mempool, warnings,

src/test/fuzz/process_message.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <consensus/consensus.h>
66
#include <net.h>
77
#include <net_processing.h>
8+
#include <node/warnings.h>
89
#include <primitives/transaction.h>
910
#include <protocol.h>
1011
#include <script/script.h>
@@ -40,9 +41,11 @@ void initialize_process_message()
4041
Assert(std::count(ALL_NET_MESSAGE_TYPES.begin(), ALL_NET_MESSAGE_TYPES.end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
4142
}
4243

43-
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(
44+
static const auto testing_setup{
45+
MakeNoLogFileContext<TestingSetup>(
4446
/*chain_type=*/ChainType::REGTEST,
45-
{.extra_args = {"-txreconciliation"}});
47+
{}),
48+
};
4649
g_setup = testing_setup.get();
4750
SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
4851
for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
@@ -56,11 +59,23 @@ FUZZ_TARGET(process_message, .init = initialize_process_message)
5659
SeedRandomStateForTest(SeedRand::ZEROS);
5760
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
5861

59-
ConnmanTestMsg& connman = *static_cast<ConnmanTestMsg*>(g_setup->m_node.connman.get());
62+
auto& connman = static_cast<ConnmanTestMsg&>(*g_setup->m_node.connman);
6063
auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman);
6164
SetMockTime(1610000000); // any time to successfully reset ibd
6265
chainman.ResetIbd();
6366

67+
node::Warnings warnings{};
68+
NetGroupManager netgroupman{{}};
69+
AddrMan addrman{netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0};
70+
auto peerman = PeerManager::make(connman, addrman,
71+
/*banman=*/nullptr, chainman,
72+
*g_setup->m_node.mempool, warnings,
73+
PeerManager::Options{
74+
.reconcile_txs = true,
75+
.deterministic_rng = true,
76+
});
77+
78+
connman.SetMsgProc(peerman.get());
6479
LOCK(NetEventsInterface::g_msgproc_mutex);
6580

6681
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};

src/test/fuzz/process_messages.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <consensus/consensus.h>
66
#include <net.h>
77
#include <net_processing.h>
8+
#include <node/warnings.h>
89
#include <protocol.h>
910
#include <script/script.h>
1011
#include <sync.h>
@@ -30,9 +31,11 @@ const TestingSetup* g_setup;
3031

3132
void initialize_process_messages()
3233
{
33-
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(
34+
static const auto testing_setup{
35+
MakeNoLogFileContext<TestingSetup>(
3436
/*chain_type=*/ChainType::REGTEST,
35-
{.extra_args = {"-txreconciliation"}});
37+
{}),
38+
};
3639
g_setup = testing_setup.get();
3740
SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
3841
for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
@@ -46,11 +49,23 @@ FUZZ_TARGET(process_messages, .init = initialize_process_messages)
4649
SeedRandomStateForTest(SeedRand::ZEROS);
4750
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
4851

49-
ConnmanTestMsg& connman = *static_cast<ConnmanTestMsg*>(g_setup->m_node.connman.get());
52+
auto& connman = static_cast<ConnmanTestMsg&>(*g_setup->m_node.connman);
5053
auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman);
5154
SetMockTime(1610000000); // any time to successfully reset ibd
5255
chainman.ResetIbd();
5356

57+
node::Warnings warnings{};
58+
NetGroupManager netgroupman{{}};
59+
AddrMan addrman{netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0};
60+
auto peerman = PeerManager::make(connman, addrman,
61+
/*banman=*/nullptr, chainman,
62+
*g_setup->m_node.mempool, warnings,
63+
PeerManager::Options{
64+
.reconcile_txs = true,
65+
.deterministic_rng = true,
66+
});
67+
connman.SetMsgProc(peerman.get());
68+
5469
LOCK(NetEventsInterface::g_msgproc_mutex);
5570

5671
std::vector<CNode*> peers;

0 commit comments

Comments
 (0)