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