/root/bitcoin/src/test/fuzz/buffered_file.cpp
| Line | Count | Source | 
| 1 |  | // Copyright (c) 2020-2022 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 <span.h> | 
| 6 |  | #include <streams.h> | 
| 7 |  | #include <test/fuzz/fuzz.h> | 
| 8 |  | #include <test/fuzz/FuzzedDataProvider.h> | 
| 9 |  | #include <test/fuzz/util.h> | 
| 10 |  | #include <util/obfuscation.h> | 
| 11 |  |  | 
| 12 |  | #include <array> | 
| 13 |  | #include <cstddef> | 
| 14 |  | #include <cstdint> | 
| 15 |  | #include <iostream> | 
| 16 |  | #include <optional> | 
| 17 |  | #include <vector> | 
| 18 |  |  | 
| 19 |  | FUZZ_TARGET(buffered_file) | 
| 20 | 0 | { | 
| 21 | 0 |     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; | 
| 22 | 0 |     FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider}; | 
| 23 | 0 |     std::optional<BufferedFile> opt_buffered_file; | 
| 24 | 0 |     const auto key_bytes{ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, Obfuscation::KEY_SIZE)}; | 
| 25 | 0 |     AutoFile fuzzed_file{ | 
| 26 | 0 |         fuzzed_file_provider.open(), | 
| 27 | 0 |         Obfuscation{std::span{key_bytes}.first<Obfuscation::KEY_SIZE>()}, | 
| 28 | 0 |     }; | 
| 29 | 0 |     try { | 
| 30 | 0 |         auto n_buf_size = fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096); | 
| 31 | 0 |         auto n_rewind_in = fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096); | 
| 32 | 0 |         opt_buffered_file.emplace(fuzzed_file, n_buf_size, n_rewind_in); | 
| 33 | 0 |     } catch (const std::ios_base::failure&) { | 
| 34 | 0 |     } | 
| 35 | 0 |     if (opt_buffered_file && !fuzzed_file.IsNull()) { | 
| 36 | 0 |         bool setpos_fail = false; | 
| 37 | 0 |         LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100) | 
| 38 | 0 |         { | 
| 39 | 0 |             CallOneOf( | 
| 40 | 0 |                 fuzzed_data_provider, | 
| 41 | 0 |                 [&] { | 
| 42 | 0 |                     std::array<std::byte, 4096> arr{}; | 
| 43 | 0 |                     try { | 
| 44 | 0 |                         opt_buffered_file->read({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)}); | 
| 45 | 0 |                     } catch (const std::ios_base::failure&) { | 
| 46 | 0 |                     } | 
| 47 | 0 |                 }, | 
| 48 | 0 |                 [&] { | 
| 49 | 0 |                     opt_buffered_file->SetLimit(fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096)); | 
| 50 | 0 |                 }, | 
| 51 | 0 |                 [&] { | 
| 52 | 0 |                     if (!opt_buffered_file->SetPos(fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096))) { | 
| 53 | 0 |                         setpos_fail = true; | 
| 54 | 0 |                     } | 
| 55 | 0 |                 }, | 
| 56 | 0 |                 [&] { | 
| 57 | 0 |                     if (setpos_fail) { | 
| 58 |  |                         // Calling FindByte(...) after a failed SetPos(...) call may result in an infinite loop. | 
| 59 | 0 |                         return; | 
| 60 | 0 |                     } | 
| 61 | 0 |                     try { | 
| 62 | 0 |                         opt_buffered_file->FindByte(std::byte(fuzzed_data_provider.ConsumeIntegral<uint8_t>())); | 
| 63 | 0 |                     } catch (const std::ios_base::failure&) { | 
| 64 | 0 |                     } | 
| 65 | 0 |                 }, | 
| 66 | 0 |                 [&] { | 
| 67 | 0 |                     ReadFromStream(fuzzed_data_provider, *opt_buffered_file); | 
| 68 | 0 |                 }); | 
| 69 | 0 |         } | 
| 70 | 0 |         opt_buffered_file->GetPos(); | 
| 71 | 0 |     } | 
| 72 | 0 | } |