Skip to content

Commit

Permalink
Merge bitcoin#23496: fuzz: Add minisketch fuzz test
Browse files Browse the repository at this point in the history
fa74d45 fuzz: Add minisketch fuzz test (MarcoFalke)

Pull request description:

ACKs for top commit:
  mjdietzx:
    re-ACK fa74d45
  sipa:
    utACK fa74d45

Tree-SHA512: 3d30095c85032139c37c7a2811dd417441a5105cb70af8250000d7b56aeda1e8fab5e65e683fb49d513ef40a81da3967a8a9a70caf40f56cef1dd96c6d4a05f6
  • Loading branch information
MarcoFalke authored and vijaydasmp committed Oct 3, 2024
1 parent 39951b4 commit 37a73ec
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ FUZZ_SUITE_LD_COMMON = \
$(LIBLEVELDB_SSE42) \
$(LIBMEMENV) \
$(LIBSECP256K1) \
$(MINISKETCH_LIBS) \
$(EVENT_LIBS) \
$(EVENT_PTHREADS_LIBS) \
$(GMP_LIBS) \
Expand Down Expand Up @@ -285,6 +286,7 @@ test_fuzz_fuzz_SOURCES = \
test/fuzz/locale.cpp \
test/fuzz/merkleblock.cpp \
test/fuzz/message.cpp \
test/fuzz/minisketch.cpp \
test/fuzz/muhash.cpp \
test/fuzz/multiplication_overflow.cpp \
test/fuzz/net.cpp \
Expand Down
64 changes: 64 additions & 0 deletions src/test/fuzz/minisketch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <minisketch.h>
#include <node/minisketchwrapper.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <util/check.h>

#include <map>
#include <numeric>

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))};

// Fill two sets and keep the difference in a map
std::map<uint32_t, bool> diff;
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
{
const auto entry{fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, std::numeric_limits<uint32_t>::max() - 1)};
const auto KeepDiff{[&] {
bool& mut{diff[entry]};
mut = !mut;
}};
CallOneOf(
fuzzed_data_provider,
[&] {
sketch_a.Add(entry);
KeepDiff();
},
[&] {
sketch_b.Add(entry);
KeepDiff();
},
[&] {
sketch_a.Add(entry);
sketch_b.Add(entry);
});
}
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)};
sketch_ar.Deserialize(sketch_a.Serialize());
sketch_br.Deserialize(sketch_b.Serialize());

Minisketch sketch_diff{std::move(fuzzed_data_provider.ConsumeBool() ? sketch_a : sketch_ar)};
sketch_diff.Merge(fuzzed_data_provider.ConsumeBool() ? sketch_b : sketch_br);

if (capacity >= num_diff) {
const auto max_elements{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(num_diff, capacity)};
const auto dec{*Assert(sketch_diff.Decode(max_elements))};
Assert(dec.size() == num_diff);
for (auto d : dec) {
Assert(diff.at(d));
}
}
}

0 comments on commit 37a73ec

Please sign in to comment.