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 <cstdint> 14 : #include <optional> 15 : #include <string> 16 : #include <vector> 17 : 18 4 : FUZZ_TARGET(bloom_filter) 19 : { 20 0 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); 21 : 22 0 : CBloomFilter bloom_filter{ 23 0 : fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 10000000), 24 0 : 1.0 / fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, std::numeric_limits<unsigned int>::max()), 25 2 : fuzzed_data_provider.ConsumeIntegral<unsigned int>(), 26 0 : static_cast<unsigned char>(fuzzed_data_provider.PickValueInArray({BLOOM_UPDATE_NONE, BLOOM_UPDATE_ALL, BLOOM_UPDATE_P2PUBKEY_ONLY, BLOOM_UPDATE_MASK}))}; 27 0 : LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 10000) { 28 0 : CallOneOf( 29 : fuzzed_data_provider, 30 0 : [&] { 31 0 : const std::vector<unsigned char> b = ConsumeRandomLengthByteVector(fuzzed_data_provider); 32 0 : (void)bloom_filter.contains(b); 33 0 : bloom_filter.insert(b); 34 0 : const bool present = bloom_filter.contains(b); 35 0 : assert(present); 36 0 : }, 37 0 : [&] { 38 0 : const std::optional<COutPoint> out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider); 39 0 : if (!out_point) { 40 0 : return; 41 : } 42 0 : (void)bloom_filter.contains(*out_point); 43 0 : bloom_filter.insert(*out_point); 44 0 : const bool present = bloom_filter.contains(*out_point); 45 0 : assert(present); 46 0 : }, 47 0 : [&] { 48 0 : const std::optional<uint256> u256 = ConsumeDeserializable<uint256>(fuzzed_data_provider); 49 0 : if (!u256) { 50 0 : return; 51 : } 52 0 : (void)bloom_filter.contains(*u256); 53 0 : bloom_filter.insert(*u256); 54 0 : const bool present = bloom_filter.contains(*u256); 55 0 : assert(present); 56 0 : }, 57 0 : [&] { 58 0 : const std::optional<CMutableTransaction> mut_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); 59 0 : if (!mut_tx) { 60 0 : return; 61 : } 62 0 : const CTransaction tx{*mut_tx}; 63 0 : (void)bloom_filter.IsRelevantAndUpdate(tx); 64 0 : }); 65 0 : (void)bloom_filter.IsWithinSizeConstraints(); 66 0 : } 67 0 : }