[fuzz] Create a FastRandomContext in addrman fuzz tests

Don't reach inside the object-under-test to use its random context.
This commit is contained in:
John Newbery committed 2021-10-05 16:38:42 +01:00
1 parent 113b863f07
commit 56303e382e
1 file changed
+14 -11
+14 -11
View File
@@ -7,6 +7,7 @@
#include <addrman_impl.h> #include <addrman_impl.h>
#include <chainparams.h> #include <chainparams.h>
#include <merkleblock.h> #include <merkleblock.h>
#include <random.h>
#include <test/fuzz/FuzzedDataProvider.h> #include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h> #include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h> #include <test/fuzz/util.h>
@@ -50,7 +51,7 @@ public:
/** /**
* Generate a random address. Always returns a valid address. * Generate a random address. Always returns a valid address.
*/ */
CNetAddr RandAddr() EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs) CNetAddr RandAddr(FastRandomContext& fast_random_context) EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs)
{ {
CNetAddr addr; CNetAddr addr;
if (m_fuzzed_data_provider.remaining_bytes() > 1 && m_fuzzed_data_provider.ConsumeBool()) { if (m_fuzzed_data_provider.remaining_bytes() > 1 && m_fuzzed_data_provider.ConsumeBool()) {
@@ -62,7 +63,7 @@ public:
{4, ADDR_TORV3_SIZE}, {4, ADDR_TORV3_SIZE},
{5, ADDR_I2P_SIZE}, {5, ADDR_I2P_SIZE},
{6, ADDR_CJDNS_SIZE}}; {6, ADDR_CJDNS_SIZE}};
uint8_t net = m_impl->insecure_rand.randrange(5) + 1; // [1..5] uint8_t net = fast_random_context.randrange(5) + 1; // [1..5]
if (net == 3) { if (net == 3) {
net = 6; net = 6;
} }
@@ -70,7 +71,7 @@ public:
CDataStream s(SER_NETWORK, PROTOCOL_VERSION | ADDRV2_FORMAT); CDataStream s(SER_NETWORK, PROTOCOL_VERSION | ADDRV2_FORMAT);
s << net; s << net;
s << m_impl->insecure_rand.randbytes(net_len_map.at(net)); s << fast_random_context.randbytes(net_len_map.at(net));
s >> addr; s >> addr;
} }
@@ -99,15 +100,17 @@ public:
const size_t num_sources = m_fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50); const size_t num_sources = m_fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
CNetAddr prev_source; CNetAddr prev_source;
// Use insecure_rand inside the loops instead of m_fuzzed_data_provider because when // Generate a FastRandomContext seed to use inside the loops instead of
// the latter is exhausted it just returns 0. // m_fuzzed_data_provider. When m_fuzzed_data_provider is exhausted it
// just returns 0.
FastRandomContext fast_random_context{ConsumeUInt256(m_fuzzed_data_provider)};
for (size_t i = 0; i < num_sources; ++i) { for (size_t i = 0; i < num_sources; ++i) {
const auto source = RandAddr(); const auto source = RandAddr(fast_random_context);
const size_t num_addresses = m_impl->insecure_rand.randrange(500) + 1; // [1..500] const size_t num_addresses = fast_random_context.randrange(500) + 1; // [1..500]
for (size_t j = 0; j < num_addresses; ++j) { for (size_t j = 0; j < num_addresses; ++j) {
const auto addr = CAddress{CService{RandAddr(), 8333}, NODE_NETWORK}; const auto addr = CAddress{CService{RandAddr(fast_random_context), 8333}, NODE_NETWORK};
const auto time_penalty = m_impl->insecure_rand.randrange(100000001); const auto time_penalty = fast_random_context.randrange(100000001);
m_impl->Add_(addr, source, time_penalty); m_impl->Add_(addr, source, time_penalty);
if (n > 0 && m_impl->mapInfo.size() % n == 0) { if (n > 0 && m_impl->mapInfo.size() % n == 0) {
@@ -115,8 +118,8 @@ public:
} }
// Add 10% of the addresses from more than one source. // Add 10% of the addresses from more than one source.
if (m_impl->insecure_rand.randrange(10) == 0 && prev_source.IsValid()) { if (fast_random_context.randrange(10) == 0 && prev_source.IsValid()) {
m_impl->Add_({addr}, prev_source, time_penalty); m_impl->Add_(addr, prev_source, time_penalty);
} }
} }
prev_source = source; prev_source = source;