Skip to content

Commit 491975c

Browse files
committed
[fuzz] Pass FuzzedDataProvider& into Fill() in addrman fuzz tests
Use a (reference) parameter instead of a data member of CAddrManDeterministic. This will allow us to make Fill() a free function in a later commit. Also remove CAddrManDeterministic.m_fuzzed_data_provider since it's no longer used.
1 parent 56303e3 commit 491975c

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/test/fuzz/addrman.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,21 @@ FUZZ_TARGET_INIT(data_stream_addr_man, initialize_addrman)
3939
class AddrManDeterministic : public AddrMan
4040
{
4141
public:
42-
FuzzedDataProvider& m_fuzzed_data_provider;
43-
4442
explicit AddrManDeterministic(std::vector<bool> asmap, FuzzedDataProvider& fuzzed_data_provider)
4543
: AddrMan(std::move(asmap), /* deterministic */ true, /* consistency_check_ratio */ 0)
46-
, m_fuzzed_data_provider(fuzzed_data_provider)
4744
{
4845
WITH_LOCK(m_impl->cs, m_impl->insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)});
4946
}
5047

5148
/**
5249
* Generate a random address. Always returns a valid address.
5350
*/
54-
CNetAddr RandAddr(FastRandomContext& fast_random_context) EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs)
51+
CNetAddr RandAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext& fast_random_context)
52+
EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs)
5553
{
5654
CNetAddr addr;
57-
if (m_fuzzed_data_provider.remaining_bytes() > 1 && m_fuzzed_data_provider.ConsumeBool()) {
58-
addr = ConsumeNetAddr(m_fuzzed_data_provider);
55+
if (fuzzed_data_provider.remaining_bytes() > 1 && fuzzed_data_provider.ConsumeBool()) {
56+
addr = ConsumeNetAddr(fuzzed_data_provider);
5957
} else {
6058
// The networks [1..6] correspond to CNetAddr::BIP155Network (private).
6159
static const std::map<uint8_t, uint8_t> net_len_map = {{1, ADDR_IPV4_SIZE},
@@ -89,27 +87,27 @@ class AddrManDeterministic : public AddrMan
8987
/**
9088
* Fill this addrman with lots of addresses from lots of sources.
9189
*/
92-
void Fill()
90+
void Fill(FuzzedDataProvider& fuzzed_data_provider)
9391
{
9492
LOCK(m_impl->cs);
9593

9694
// Add some of the addresses directly to the "tried" table.
9795

9896
// 0, 1, 2, 3 corresponding to 0%, 100%, 50%, 33%
99-
const size_t n = m_fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 3);
97+
const size_t n = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 3);
10098

101-
const size_t num_sources = m_fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
99+
const size_t num_sources = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
102100
CNetAddr prev_source;
103101
// Generate a FastRandomContext seed to use inside the loops instead of
104-
// m_fuzzed_data_provider. When m_fuzzed_data_provider is exhausted it
102+
// fuzzed_data_provider. When fuzzed_data_provider is exhausted it
105103
// just returns 0.
106-
FastRandomContext fast_random_context{ConsumeUInt256(m_fuzzed_data_provider)};
104+
FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
107105
for (size_t i = 0; i < num_sources; ++i) {
108-
const auto source = RandAddr(fast_random_context);
106+
const auto source = RandAddr(fuzzed_data_provider, fast_random_context);
109107
const size_t num_addresses = fast_random_context.randrange(500) + 1; // [1..500]
110108

111109
for (size_t j = 0; j < num_addresses; ++j) {
112-
const auto addr = CAddress{CService{RandAddr(fast_random_context), 8333}, NODE_NETWORK};
110+
const auto addr = CAddress{CService{RandAddr(fuzzed_data_provider, fast_random_context), 8333}, NODE_NETWORK};
113111
const auto time_penalty = fast_random_context.randrange(100000001);
114112
m_impl->Add_(addr, source, time_penalty);
115113

@@ -310,7 +308,7 @@ FUZZ_TARGET_INIT(addrman_serdeser, initialize_addrman)
310308

311309
CDataStream data_stream(SER_NETWORK, PROTOCOL_VERSION);
312310

313-
addr_man1.Fill();
311+
addr_man1.Fill(fuzzed_data_provider);
314312
data_stream << addr_man1;
315313
data_stream >> addr_man2;
316314
assert(addr_man1 == addr_man2);

0 commit comments

Comments
 (0)