Skip to content

Commit 56303e3

Browse files
committed
[fuzz] Create a FastRandomContext in addrman fuzz tests
Don't reach inside the object-under-test to use its random context.
1 parent 113b863 commit 56303e3

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/test/fuzz/addrman.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <addrman_impl.h>
88
#include <chainparams.h>
99
#include <merkleblock.h>
10+
#include <random.h>
1011
#include <test/fuzz/FuzzedDataProvider.h>
1112
#include <test/fuzz/fuzz.h>
1213
#include <test/fuzz/util.h>
@@ -50,7 +51,7 @@ class AddrManDeterministic : public AddrMan
5051
/**
5152
* Generate a random address. Always returns a valid address.
5253
*/
53-
CNetAddr RandAddr() EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs)
54+
CNetAddr RandAddr(FastRandomContext& fast_random_context) EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs)
5455
{
5556
CNetAddr addr;
5657
if (m_fuzzed_data_provider.remaining_bytes() > 1 && m_fuzzed_data_provider.ConsumeBool()) {
@@ -62,15 +63,15 @@ class AddrManDeterministic : public AddrMan
6263
{4, ADDR_TORV3_SIZE},
6364
{5, ADDR_I2P_SIZE},
6465
{6, ADDR_CJDNS_SIZE}};
65-
uint8_t net = m_impl->insecure_rand.randrange(5) + 1; // [1..5]
66+
uint8_t net = fast_random_context.randrange(5) + 1; // [1..5]
6667
if (net == 3) {
6768
net = 6;
6869
}
6970

7071
CDataStream s(SER_NETWORK, PROTOCOL_VERSION | ADDRV2_FORMAT);
7172

7273
s << net;
73-
s << m_impl->insecure_rand.randbytes(net_len_map.at(net));
74+
s << fast_random_context.randbytes(net_len_map.at(net));
7475

7576
s >> addr;
7677
}
@@ -99,24 +100,26 @@ class AddrManDeterministic : public AddrMan
99100

100101
const size_t num_sources = m_fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
101102
CNetAddr prev_source;
102-
// Use insecure_rand inside the loops instead of m_fuzzed_data_provider because when
103-
// the latter is exhausted it just returns 0.
103+
// Generate a FastRandomContext seed to use inside the loops instead of
104+
// m_fuzzed_data_provider. When m_fuzzed_data_provider is exhausted it
105+
// just returns 0.
106+
FastRandomContext fast_random_context{ConsumeUInt256(m_fuzzed_data_provider)};
104107
for (size_t i = 0; i < num_sources; ++i) {
105-
const auto source = RandAddr();
106-
const size_t num_addresses = m_impl->insecure_rand.randrange(500) + 1; // [1..500]
108+
const auto source = RandAddr(fast_random_context);
109+
const size_t num_addresses = fast_random_context.randrange(500) + 1; // [1..500]
107110

108111
for (size_t j = 0; j < num_addresses; ++j) {
109-
const auto addr = CAddress{CService{RandAddr(), 8333}, NODE_NETWORK};
110-
const auto time_penalty = m_impl->insecure_rand.randrange(100000001);
112+
const auto addr = CAddress{CService{RandAddr(fast_random_context), 8333}, NODE_NETWORK};
113+
const auto time_penalty = fast_random_context.randrange(100000001);
111114
m_impl->Add_(addr, source, time_penalty);
112115

113116
if (n > 0 && m_impl->mapInfo.size() % n == 0) {
114117
m_impl->Good_(addr, false, GetTime());
115118
}
116119

117120
// Add 10% of the addresses from more than one source.
118-
if (m_impl->insecure_rand.randrange(10) == 0 && prev_source.IsValid()) {
119-
m_impl->Add_({addr}, prev_source, time_penalty);
121+
if (fast_random_context.randrange(10) == 0 && prev_source.IsValid()) {
122+
m_impl->Add_(addr, prev_source, time_penalty);
120123
}
121124
}
122125
prev_source = source;

0 commit comments

Comments
 (0)