Branch data Line data Source code
1 : : // Copyright (c) 2020-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <common/bloom.h>
6 : : #include <primitives/transaction.h>
7 : : #include <test/fuzz/FuzzedDataProvider.h>
8 : : #include <test/fuzz/fuzz.h>
9 : : #include <test/fuzz/util.h>
10 : : #include <uint256.h>
11 : :
12 : : #include <cassert>
13 : : #include <limits>
14 : : #include <optional>
15 : : #include <vector>
16 : :
17 [ + - ]: 1690 : FUZZ_TARGET(bloom_filter)
18 : : {
19 : 1216 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
20 : 1216 : bool good_data{true};
21 : :
22 : 1216 : CBloomFilter bloom_filter{
23 : : fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 10000000),
24 : 1216 : 1.0 / fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, std::numeric_limits<unsigned int>::max()),
25 : : fuzzed_data_provider.ConsumeIntegral<unsigned int>(),
26 : 2432 : static_cast<unsigned char>(fuzzed_data_provider.PickValueInArray({BLOOM_UPDATE_NONE, BLOOM_UPDATE_ALL, BLOOM_UPDATE_P2PUBKEY_ONLY, BLOOM_UPDATE_MASK}))};
27 [ + + + + : 166656 : LIMITED_WHILE (good_data && fuzzed_data_provider.remaining_bytes() > 0, 10'000) {
+ + ]
28 [ + - ]: 165440 : CallOneOf(
29 : : fuzzed_data_provider,
30 : 58743 : [&] {
31 : 58743 : const std::vector<unsigned char> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
32 [ - + + - ]: 58743 : (void)bloom_filter.contains(b);
33 [ - + + - ]: 58743 : bloom_filter.insert(b);
34 [ - + + - ]: 58743 : const bool present = bloom_filter.contains(b);
35 [ - + ]: 58743 : assert(present);
36 : 58743 : },
37 : 10793 : [&] {
38 : 10793 : const std::optional<COutPoint> out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
39 [ + + ]: 10793 : if (!out_point) {
40 : 15 : good_data = false;
41 : 15 : return;
42 : : }
43 : 10778 : (void)bloom_filter.contains(*out_point);
44 : 10778 : bloom_filter.insert(*out_point);
45 : 10778 : const bool present = bloom_filter.contains(*out_point);
46 [ - + ]: 10778 : assert(present);
47 : : },
48 : 7164 : [&] {
49 : 7164 : const std::optional<uint256> u256 = ConsumeDeserializable<uint256>(fuzzed_data_provider);
50 [ + + ]: 7164 : if (!u256) {
51 : 15 : good_data = false;
52 : 15 : return;
53 : : }
54 : 7149 : (void)bloom_filter.contains(*u256);
55 : 7149 : bloom_filter.insert(*u256);
56 : 7149 : const bool present = bloom_filter.contains(*u256);
57 [ - + ]: 7149 : assert(present);
58 : : },
59 : 88740 : [&] {
60 : 88740 : const std::optional<CMutableTransaction> mut_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
61 [ + + ]: 88740 : if (!mut_tx) {
62 : 439 : good_data = false;
63 [ - + ]: 439 : return;
64 : : }
65 [ + - ]: 88301 : const CTransaction tx{*mut_tx};
66 [ + - ]: 88301 : (void)bloom_filter.IsRelevantAndUpdate(tx);
67 [ + - ]: 177041 : });
68 [ + - ]: 165440 : (void)bloom_filter.IsWithinSizeConstraints();
69 : : }
70 : 1216 : }
|