From 640476eb0e17fd4c64d4840ceab229642f1d79d9 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 21 Sep 2021 10:48:32 +0100 Subject: [PATCH] [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. --- src/test/fuzz/addrman.cpp | 78 +++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/src/test/fuzz/addrman.cpp b/src/test/fuzz/addrman.cpp index 7b88e9b6c2..297656ac3d 100644 --- a/src/test/fuzz/addrman.cpp +++ b/src/test/fuzz/addrman.cpp @@ -74,6 +74,42 @@ CNetAddr RandAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext& f return addr; } +/** Fill addrman with lots of addresses from lots of sources. */ +void FillAddrman(AddrMan& addrman, FuzzedDataProvider& fuzzed_data_provider) +{ + // Add some of the addresses directly to the "tried" table. + + // 0, 1, 2, 3 corresponding to 0%, 100%, 50%, 33% + const size_t n = fuzzed_data_provider.ConsumeIntegralInRange(0, 3); + + const size_t num_sources = fuzzed_data_provider.ConsumeIntegralInRange(1, 50); + CNetAddr prev_source; + // Generate a FastRandomContext seed to use inside the loops instead of + // fuzzed_data_provider. When fuzzed_data_provider is exhausted it + // just returns 0. + FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)}; + for (size_t i = 0; i < num_sources; ++i) { + 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(fuzzed_data_provider, fast_random_context), 8333}, NODE_NETWORK}; + const auto time_penalty = fast_random_context.randrange(100000001); + addrman.Add({addr}, source, time_penalty); + + if (n > 0 && addrman.size() % n == 0) { + addrman.Good(addr, GetTime()); + } + + // Add 10% of the addresses from more than one source. + if (fast_random_context.randrange(10) == 0 && prev_source.IsValid()) { + addrman.Add({addr}, prev_source, time_penalty); + } + } + prev_source = source; + } +} + class AddrManDeterministic : public AddrMan { public: @@ -83,46 +119,6 @@ public: WITH_LOCK(m_impl->cs, m_impl->insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)}); } - /** - * Fill this addrman with lots of addresses from lots of sources. - */ - 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 = fuzzed_data_provider.ConsumeIntegralInRange(0, 3); - - const size_t num_sources = fuzzed_data_provider.ConsumeIntegralInRange(1, 50); - CNetAddr prev_source; - // Generate a FastRandomContext seed to use inside the loops instead of - // fuzzed_data_provider. When fuzzed_data_provider is exhausted it - // just returns 0. - FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)}; - for (size_t i = 0; i < num_sources; ++i) { - 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(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); - - if (n > 0 && m_impl->mapInfo.size() % n == 0) { - m_impl->Good_(addr, false, GetTime()); - } - - // Add 10% of the addresses from more than one source. - if (fast_random_context.randrange(10) == 0 && prev_source.IsValid()) { - m_impl->Add_(addr, prev_source, time_penalty); - } - } - prev_source = source; - } - } - /** * Compare with another AddrMan. * This compares: @@ -307,7 +303,7 @@ FUZZ_TARGET_INIT(addrman_serdeser, initialize_addrman) CDataStream data_stream(SER_NETWORK, PROTOCOL_VERSION); - addr_man1.Fill(fuzzed_data_provider); + FillAddrman(addr_man1, fuzzed_data_provider); data_stream << addr_man1; data_stream >> addr_man2; assert(addr_man1 == addr_man2);