Skip to content

Commit 640476e

Browse files
committed
[fuzz] Make Fill() a free function in fuzz/addrman.cpp
Also rename it to FillAddrman and pass an addrman reference as an argument. Change FillAddrman to only use addrman's public interface methods.
1 parent 90ad8ad commit 640476e

File tree

1 file changed

+37
-41
lines changed

1 file changed

+37
-41
lines changed

src/test/fuzz/addrman.cpp

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,42 @@ CNetAddr RandAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext& f
7474
return addr;
7575
}
7676

77+
/** Fill addrman with lots of addresses from lots of sources. */
78+
void FillAddrman(AddrMan& addrman, FuzzedDataProvider& fuzzed_data_provider)
79+
{
80+
// Add some of the addresses directly to the "tried" table.
81+
82+
// 0, 1, 2, 3 corresponding to 0%, 100%, 50%, 33%
83+
const size_t n = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 3);
84+
85+
const size_t num_sources = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
86+
CNetAddr prev_source;
87+
// Generate a FastRandomContext seed to use inside the loops instead of
88+
// fuzzed_data_provider. When fuzzed_data_provider is exhausted it
89+
// just returns 0.
90+
FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
91+
for (size_t i = 0; i < num_sources; ++i) {
92+
const auto source = RandAddr(fuzzed_data_provider, fast_random_context);
93+
const size_t num_addresses = fast_random_context.randrange(500) + 1; // [1..500]
94+
95+
for (size_t j = 0; j < num_addresses; ++j) {
96+
const auto addr = CAddress{CService{RandAddr(fuzzed_data_provider, fast_random_context), 8333}, NODE_NETWORK};
97+
const auto time_penalty = fast_random_context.randrange(100000001);
98+
addrman.Add({addr}, source, time_penalty);
99+
100+
if (n > 0 && addrman.size() % n == 0) {
101+
addrman.Good(addr, GetTime());
102+
}
103+
104+
// Add 10% of the addresses from more than one source.
105+
if (fast_random_context.randrange(10) == 0 && prev_source.IsValid()) {
106+
addrman.Add({addr}, prev_source, time_penalty);
107+
}
108+
}
109+
prev_source = source;
110+
}
111+
}
112+
77113
class AddrManDeterministic : public AddrMan
78114
{
79115
public:
@@ -83,46 +119,6 @@ class AddrManDeterministic : public AddrMan
83119
WITH_LOCK(m_impl->cs, m_impl->insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)});
84120
}
85121

86-
/**
87-
* Fill this addrman with lots of addresses from lots of sources.
88-
*/
89-
void Fill(FuzzedDataProvider& fuzzed_data_provider)
90-
{
91-
LOCK(m_impl->cs);
92-
93-
// Add some of the addresses directly to the "tried" table.
94-
95-
// 0, 1, 2, 3 corresponding to 0%, 100%, 50%, 33%
96-
const size_t n = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 3);
97-
98-
const size_t num_sources = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
99-
CNetAddr prev_source;
100-
// Generate a FastRandomContext seed to use inside the loops instead of
101-
// fuzzed_data_provider. When fuzzed_data_provider is exhausted it
102-
// just returns 0.
103-
FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
104-
for (size_t i = 0; i < num_sources; ++i) {
105-
const auto source = RandAddr(fuzzed_data_provider, fast_random_context);
106-
const size_t num_addresses = fast_random_context.randrange(500) + 1; // [1..500]
107-
108-
for (size_t j = 0; j < num_addresses; ++j) {
109-
const auto addr = CAddress{CService{RandAddr(fuzzed_data_provider, fast_random_context), 8333}, NODE_NETWORK};
110-
const auto time_penalty = fast_random_context.randrange(100000001);
111-
m_impl->Add_(addr, source, time_penalty);
112-
113-
if (n > 0 && m_impl->mapInfo.size() % n == 0) {
114-
m_impl->Good_(addr, false, GetTime());
115-
}
116-
117-
// Add 10% of the addresses from more than one source.
118-
if (fast_random_context.randrange(10) == 0 && prev_source.IsValid()) {
119-
m_impl->Add_(addr, prev_source, time_penalty);
120-
}
121-
}
122-
prev_source = source;
123-
}
124-
}
125-
126122
/**
127123
* Compare with another AddrMan.
128124
* This compares:
@@ -307,7 +303,7 @@ FUZZ_TARGET_INIT(addrman_serdeser, initialize_addrman)
307303

308304
CDataStream data_stream(SER_NETWORK, PROTOCOL_VERSION);
309305

310-
addr_man1.Fill(fuzzed_data_provider);
306+
FillAddrman(addr_man1, fuzzed_data_provider);
311307
data_stream << addr_man1;
312308
data_stream >> addr_man2;
313309
assert(addr_man1 == addr_man2);

0 commit comments

Comments
 (0)