Branch data Line data Source code
1 : : // Copyright (c) 2020-2021 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 [ + - + - ]: 1303 : FUZZ_TARGET(bloom_filter)
18 : : {
19 : 1300 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
20 : 1300 : bool good_data{true};
21 : :
22 : 2600 : CBloomFilter bloom_filter{
23 : 1300 : fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 10000000),
24 : 1300 : 1.0 / fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, std::numeric_limits<unsigned int>::max()),
25 : 1300 : fuzzed_data_provider.ConsumeIntegral<unsigned int>(),
26 : 1300 : static_cast<unsigned char>(fuzzed_data_provider.PickValueInArray({BLOOM_UPDATE_NONE, BLOOM_UPDATE_ALL, BLOOM_UPDATE_P2PUBKEY_ONLY, BLOOM_UPDATE_MASK}))};
27 [ + + + - : 120036 : LIMITED_WHILE(good_data && fuzzed_data_provider.remaining_bytes() > 0, 10'000)
+ + + + ]
28 : : {
29 [ + - ]: 118736 : CallOneOf(
30 : : fuzzed_data_provider,
31 : 149189 : [&] {
32 : 30453 : const std::vector<unsigned char> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
33 [ + - + - ]: 30453 : (void)bloom_filter.contains(b);
34 [ + - + - ]: 30453 : bloom_filter.insert(b);
35 [ + - + - ]: 30453 : const bool present = bloom_filter.contains(b);
36 [ + - ]: 30453 : assert(present);
37 : 30453 : },
38 : 135554 : [&] {
39 : 16818 : const std::optional<COutPoint> out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
40 [ + + ]: 16818 : if (!out_point) {
41 : 36 : good_data = false;
42 : 36 : return;
43 : : }
44 : 16782 : (void)bloom_filter.contains(*out_point);
45 : 16782 : bloom_filter.insert(*out_point);
46 : 16782 : const bool present = bloom_filter.contains(*out_point);
47 [ + - ]: 16782 : assert(present);
48 [ - + ]: 16818 : },
49 : 125667 : [&] {
50 : 6931 : const std::optional<uint256> u256 = ConsumeDeserializable<uint256>(fuzzed_data_provider);
51 [ + + ]: 6931 : if (!u256) {
52 : 12 : good_data = false;
53 : 12 : return;
54 : : }
55 : 6919 : (void)bloom_filter.contains(*u256);
56 : 6919 : bloom_filter.insert(*u256);
57 : 6919 : const bool present = bloom_filter.contains(*u256);
58 [ + - ]: 6919 : assert(present);
59 [ - + ]: 6931 : },
60 : 183270 : [&] {
61 : 64534 : const std::optional<CMutableTransaction> mut_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
62 [ + + ]: 64534 : if (!mut_tx) {
63 : 434 : good_data = false;
64 : 434 : return;
65 : : }
66 [ + - ]: 64100 : const CTransaction tx{*mut_tx};
67 [ + - ]: 64100 : (void)bloom_filter.IsRelevantAndUpdate(tx);
68 [ - + ]: 64534 : });
69 [ + - ]: 118736 : (void)bloom_filter.IsWithinSizeConstraints();
70 : 118736 : }
71 : 1300 : }
|