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 : : #include <util/check.h>
12 : :
13 : : #include <limits>
14 : : #include <optional>
15 : : #include <vector>
16 : :
17 [ + - ]: 1280 : FUZZ_TARGET(bloom_filter)
18 : : {
19 : 804 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
20 : 804 : bool good_data{true};
21 : :
22 [ + + ]: 804 : const auto false_positive_rate{fuzzed_data_provider.ConsumeBool() ?
23 : 467 : fuzzed_data_provider.ConsumeProbability<double>() :
24 : 337 : fuzzed_data_provider.PickValueInArray({
25 : : 0.0,
26 : : std::numeric_limits<double>::denorm_min(),
27 : : std::numeric_limits<double>::min(),
28 : : std::numeric_limits<double>::epsilon(),
29 : : 1.0,
30 : : })};
31 : 804 : CBloomFilter bloom_filter{
32 : : fuzzed_data_provider.ConsumeIntegral<unsigned int>(),
33 : : false_positive_rate,
34 : : fuzzed_data_provider.ConsumeIntegral<unsigned int>(),
35 : 804 : static_cast<unsigned char>(fuzzed_data_provider.PickValueInArray({BLOOM_UPDATE_NONE, BLOOM_UPDATE_ALL, BLOOM_UPDATE_P2PUBKEY_ONLY, BLOOM_UPDATE_MASK}))};
36 [ + + + + : 56699 : LIMITED_WHILE (good_data && fuzzed_data_provider.remaining_bytes() > 0, 10'000) {
+ + ]
37 [ + - ]: 55895 : CallOneOf(
38 : : fuzzed_data_provider,
39 : 26355 : [&] {
40 : 26355 : const std::vector<unsigned char> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
41 [ - + + - ]: 26355 : (void)bloom_filter.contains(b);
42 [ - + + - ]: 26355 : bloom_filter.insert(b);
43 [ - + + - ]: 26355 : const bool present = bloom_filter.contains(b);
44 [ - + ]: 26355 : assert(present);
45 : 26355 : },
46 : 3018 : [&] {
47 : 3018 : const std::optional<COutPoint> out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
48 [ + + ]: 3018 : if (!out_point) {
49 : 39 : good_data = false;
50 : 39 : return;
51 : : }
52 : 2979 : (void)bloom_filter.contains(*out_point);
53 : 2979 : bloom_filter.insert(*out_point);
54 : 2979 : const bool present = bloom_filter.contains(*out_point);
55 [ - + ]: 2979 : assert(present);
56 : : },
57 : 1317 : [&] {
58 : 1317 : const std::optional<uint256> u256 = ConsumeDeserializable<uint256>(fuzzed_data_provider);
59 [ + + ]: 1317 : if (!u256) {
60 : 36 : good_data = false;
61 : 36 : return;
62 : : }
63 : 1281 : (void)bloom_filter.contains(*u256);
64 : 1281 : bloom_filter.insert(*u256);
65 : 1281 : const bool present = bloom_filter.contains(*u256);
66 [ - + ]: 1281 : assert(present);
67 : : },
68 : 25205 : [&] {
69 : 25205 : const std::optional<CMutableTransaction> mut_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
70 [ + + ]: 25205 : if (!mut_tx) {
71 : 389 : good_data = false;
72 [ - + ]: 389 : return;
73 : : }
74 [ + - ]: 24816 : const CTransaction tx{*mut_tx};
75 [ + - ]: 24816 : (void)bloom_filter.IsRelevantAndUpdate(tx);
76 [ + - ]: 50021 : });
77 [ + - ]: 55895 : (void)bloom_filter.IsWithinSizeConstraints();
78 : : }
79 : 804 : }
|