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 <cuckoocache.h>
6 : :
7 : : #include <script/sigcache.h>
8 : : #include <test/fuzz/FuzzedDataProvider.h>
9 : : #include <test/fuzz/fuzz.h>
10 : : #include <test/fuzz/util.h>
11 : : #include <test/util/setup_common.h>
12 : : #include <util/byte_units.h>
13 : : #include <util/check.h>
14 : :
15 : : #include <cstdint>
16 : : #include <string>
17 : : #include <vector>
18 : :
19 : : namespace {
20 : : FuzzedDataProvider* fuzzed_data_provider_ptr = nullptr;
21 : :
22 : : struct RandomHasher {
23 : : template <uint8_t>
24 : 1079152 : uint32_t operator()(const bool& /* unused */) const
25 : : {
26 [ - + ]: 1079152 : assert(fuzzed_data_provider_ptr != nullptr);
27 : 1079152 : return fuzzed_data_provider_ptr->ConsumeIntegral<uint32_t>();
28 : : }
29 : : };
30 : : } // namespace
31 : :
32 [ + - ]: 643 : FUZZ_TARGET(cuckoocache)
33 : : {
34 : 167 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
35 : 167 : fuzzed_data_provider_ptr = &fuzzed_data_provider;
36 : 167 : CuckooCache::cache<int, RandomHasher> cuckoo_cache{};
37 [ + + ]: 167 : if (fuzzed_data_provider.ConsumeBool()) {
38 : 31 : const size_t megabytes = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 16);
39 [ + - ]: 31 : cuckoo_cache.setup_bytes(megabytes * 1_MiB);
40 : : } else {
41 [ + - ]: 136 : cuckoo_cache.setup(fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, 4096));
42 : : }
43 [ + + + + ]: 106739 : LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {
44 [ + + ]: 106572 : if (fuzzed_data_provider.ConsumeBool()) {
45 : 102277 : cuckoo_cache.insert(fuzzed_data_provider.ConsumeBool());
46 : : } else {
47 : 4295 : auto e = fuzzed_data_provider.ConsumeBool();
48 : 4295 : auto erase = fuzzed_data_provider.ConsumeBool();
49 : 4295 : cuckoo_cache.contains(e, erase);
50 : : }
51 : : }
52 : 167 : fuzzed_data_provider_ptr = nullptr;
53 : 167 : }
|