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 [ + - ]: 1163 : FUZZ_TARGET(bloom_filter)
18 : : {
19 : 721 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
20 : 721 : bool good_data{true};
21 : :
22 : 721 : CBloomFilter bloom_filter{
23 : : fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 10000000),
24 : 721 : 1.0 / fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, std::numeric_limits<unsigned int>::max()),
25 : : fuzzed_data_provider.ConsumeIntegral<unsigned int>(),
26 : 1442 : static_cast<unsigned char>(fuzzed_data_provider.PickValueInArray({BLOOM_UPDATE_NONE, BLOOM_UPDATE_ALL, BLOOM_UPDATE_P2PUBKEY_ONLY, BLOOM_UPDATE_MASK}))};
27 [ + + + + : 69668 : LIMITED_WHILE(good_data && fuzzed_data_provider.remaining_bytes() > 0, 10'000)
+ - ]
28 : : {
29 [ + - ]: 68947 : CallOneOf(
30 : : fuzzed_data_provider,
31 : 15959 : [&] {
32 : 15959 : const std::vector<unsigned char> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
33 [ + - ]: 15959 : (void)bloom_filter.contains(b);
34 [ + - ]: 15959 : bloom_filter.insert(b);
35 [ + - ]: 15959 : const bool present = bloom_filter.contains(b);
36 [ - + ]: 15959 : assert(present);
37 : 15959 : },
38 : 7319 : [&] {
39 : 7319 : const std::optional<COutPoint> out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
40 [ + + ]: 7319 : if (!out_point) {
41 : 21 : good_data = false;
42 : 21 : return;
43 : : }
44 : 7298 : (void)bloom_filter.contains(*out_point);
45 : 7298 : bloom_filter.insert(*out_point);
46 : 7298 : const bool present = bloom_filter.contains(*out_point);
47 [ - + ]: 7298 : assert(present);
48 : : },
49 : 4168 : [&] {
50 : 4168 : const std::optional<uint256> u256 = ConsumeDeserializable<uint256>(fuzzed_data_provider);
51 [ + + ]: 4168 : if (!u256) {
52 : 9 : good_data = false;
53 : 9 : return;
54 : : }
55 : 4159 : (void)bloom_filter.contains(*u256);
56 : 4159 : bloom_filter.insert(*u256);
57 : 4159 : const bool present = bloom_filter.contains(*u256);
58 [ - + ]: 4159 : assert(present);
59 : : },
60 : 41501 : [&] {
61 : 41501 : const std::optional<CMutableTransaction> mut_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
62 [ + + ]: 41501 : if (!mut_tx) {
63 : 229 : good_data = false;
64 [ - + ]: 229 : return;
65 : : }
66 [ + - ]: 41272 : const CTransaction tx{*mut_tx};
67 [ + - ]: 41272 : (void)bloom_filter.IsRelevantAndUpdate(tx);
68 [ + - ]: 82773 : });
69 [ + - ]: 68947 : (void)bloom_filter.IsWithinSizeConstraints();
70 : : }
71 : 721 : }
|