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