[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.
This commit is contained in:
John Newbery 2021-09-21 09:35:40 +01:00
parent 56303e382e
commit 491975c596

View File

@ -39,11 +39,8 @@ FUZZ_TARGET_INIT(data_stream_addr_man, initialize_addrman)
class AddrManDeterministic : public AddrMan
{
public:
FuzzedDataProvider& m_fuzzed_data_provider;
explicit AddrManDeterministic(std::vector<bool> asmap, FuzzedDataProvider& fuzzed_data_provider)
: AddrMan(std::move(asmap), /* deterministic */ true, /* consistency_check_ratio */ 0)
, m_fuzzed_data_provider(fuzzed_data_provider)
{
WITH_LOCK(m_impl->cs, m_impl->insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)});
}
@ -51,11 +48,12 @@ public:
/**
* Generate a random address. Always returns a valid address.
*/
CNetAddr RandAddr(FastRandomContext& fast_random_context) EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs)
CNetAddr RandAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext& fast_random_context)
EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs)
{
CNetAddr addr;
if (m_fuzzed_data_provider.remaining_bytes() > 1 && m_fuzzed_data_provider.ConsumeBool()) {
addr = ConsumeNetAddr(m_fuzzed_data_provider);
if (fuzzed_data_provider.remaining_bytes() > 1 && fuzzed_data_provider.ConsumeBool()) {
addr = ConsumeNetAddr(fuzzed_data_provider);
} else {
// The networks [1..6] correspond to CNetAddr::BIP155Network (private).
static const std::map<uint8_t, uint8_t> net_len_map = {{1, ADDR_IPV4_SIZE},
@ -89,27 +87,27 @@ public:
/**
* Fill this addrman with lots of addresses from lots of sources.
*/
void Fill()
void Fill(FuzzedDataProvider& fuzzed_data_provider)
{
LOCK(m_impl->cs);
// Add some of the addresses directly to the "tried" table.
// 0, 1, 2, 3 corresponding to 0%, 100%, 50%, 33%
const size_t n = m_fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 3);
const size_t n = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 3);
const size_t num_sources = m_fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
const size_t num_sources = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
CNetAddr prev_source;
// Generate a FastRandomContext seed to use inside the loops instead of
// m_fuzzed_data_provider. When m_fuzzed_data_provider is exhausted it
// fuzzed_data_provider. When fuzzed_data_provider is exhausted it
// just returns 0.
FastRandomContext fast_random_context{ConsumeUInt256(m_fuzzed_data_provider)};
FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
for (size_t i = 0; i < num_sources; ++i) {
const auto source = RandAddr(fast_random_context);
const auto source = RandAddr(fuzzed_data_provider, fast_random_context);
const size_t num_addresses = fast_random_context.randrange(500) + 1; // [1..500]
for (size_t j = 0; j < num_addresses; ++j) {
const auto addr = CAddress{CService{RandAddr(fast_random_context), 8333}, NODE_NETWORK};
const auto addr = CAddress{CService{RandAddr(fuzzed_data_provider, fast_random_context), 8333}, NODE_NETWORK};
const auto time_penalty = fast_random_context.randrange(100000001);
m_impl->Add_(addr, source, time_penalty);
@ -310,7 +308,7 @@ FUZZ_TARGET_INIT(addrman_serdeser, initialize_addrman)
CDataStream data_stream(SER_NETWORK, PROTOCOL_VERSION);
addr_man1.Fill();
addr_man1.Fill(fuzzed_data_provider);
data_stream << addr_man1;
data_stream >> addr_man2;
assert(addr_man1 == addr_man2);