Skip to content

Commit

Permalink
Merge bitcoin#29064: fuzz: Improve fuzzing stability for minisketch h…
Browse files Browse the repository at this point in the history
…arness

b2fc7a2 [fuzz] Improve fuzzing stability for minisketch harness (dergoegge)

Pull request description:

  The `minisketch` harness has low stability due to:
  * Rng internal to minisketch
  * Benchmarkning for the best minisketch impl

  Fix this by seeding the rng and letting the fuzzer choose the impl.

  Also see bitcoin#29018.

ACKs for top commit:
  maflcko:
    review ACK b2fc7a2

Tree-SHA512: 3d81414299c6803c34e928a53bcf843722fa8c38e1d3676cde7fa80923f9058b1ad4b9a2941f718303a6641b17eeb28b4a22eda09678102e9fb7c4e31d06f8f2
  • Loading branch information
fanquake committed Dec 18, 2023
2 parents c840dea + b2fc7a2 commit eef19c4
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/test/fuzz/minisketch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,27 @@
#include <map>
#include <numeric>

using node::MakeMinisketch32;
namespace {

Minisketch MakeFuzzMinisketch32(size_t capacity, uint32_t impl)
{
return Assert(Minisketch(32, impl, capacity));
}

} // namespace

FUZZ_TARGET(minisketch)
{
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};

const auto capacity{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 200)};
Minisketch sketch_a{Assert(MakeMinisketch32(capacity))};
Minisketch sketch_b{Assert(MakeMinisketch32(capacity))};
const uint32_t impl{fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, Minisketch::MaxImplementation())};
if (!Minisketch::ImplementationSupported(32, impl)) return;

Minisketch sketch_a{MakeFuzzMinisketch32(capacity, impl)};
Minisketch sketch_b{MakeFuzzMinisketch32(capacity, impl)};
sketch_a.SetSeed(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
sketch_b.SetSeed(fuzzed_data_provider.ConsumeIntegral<uint64_t>());

// Fill two sets and keep the difference in a map
std::map<uint32_t, bool> diff;
Expand Down Expand Up @@ -47,8 +60,11 @@ FUZZ_TARGET(minisketch)
}
const auto num_diff{std::accumulate(diff.begin(), diff.end(), size_t{0}, [](auto n, const auto& e) { return n + e.second; })};

Minisketch sketch_ar{MakeMinisketch32(capacity)};
Minisketch sketch_br{MakeMinisketch32(capacity)};
Minisketch sketch_ar{MakeFuzzMinisketch32(capacity, impl)};
Minisketch sketch_br{MakeFuzzMinisketch32(capacity, impl)};
sketch_ar.SetSeed(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
sketch_br.SetSeed(fuzzed_data_provider.ConsumeIntegral<uint64_t>());

sketch_ar.Deserialize(sketch_a.Serialize());
sketch_br.Deserialize(sketch_b.Serialize());

Expand Down

0 comments on commit eef19c4

Please sign in to comment.