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 <netaddress.h> 6 : #include <util/asmap.h> 7 : #include <test/fuzz/fuzz.h> 8 : 9 : #include <cstdint> 10 : #include <optional> 11 : #include <vector> 12 : 13 : #include <assert.h> 14 : 15 4 : FUZZ_TARGET(asmap_direct) 16 : { 17 : // Encoding: [asmap using 1 bit / byte] 0xFF [addr using 1 bit / byte] 18 0 : std::optional<size_t> sep_pos_opt; 19 0 : for (size_t pos = 0; pos < buffer.size(); ++pos) { 20 0 : uint8_t x = buffer[pos]; 21 0 : if ((x & 0xFE) == 0) continue; 22 0 : if (x == 0xFF) { 23 0 : if (sep_pos_opt) return; 24 0 : sep_pos_opt = pos; 25 2 : } else { 26 0 : return; 27 : } 28 0 : } 29 0 : if (!sep_pos_opt) return; // Needs exactly 1 separator 30 0 : const size_t sep_pos{sep_pos_opt.value()}; 31 0 : if (buffer.size() - sep_pos - 1 > 128) return; // At most 128 bits in IP address 32 : 33 : // Checks on asmap 34 0 : std::vector<bool> asmap(buffer.begin(), buffer.begin() + sep_pos); 35 0 : if (SanityCheckASMap(asmap, buffer.size() - 1 - sep_pos)) { 36 : // Verify that for valid asmaps, no prefix (except up to 7 zero padding bits) is valid. 37 0 : std::vector<bool> asmap_prefix = asmap; 38 0 : while (!asmap_prefix.empty() && asmap_prefix.size() + 7 > asmap.size() && asmap_prefix.back() == false) { 39 0 : asmap_prefix.pop_back(); 40 : } 41 0 : while (!asmap_prefix.empty()) { 42 0 : asmap_prefix.pop_back(); 43 0 : assert(!SanityCheckASMap(asmap_prefix, buffer.size() - 1 - sep_pos)); 44 : } 45 : // No address input should trigger assertions in interpreter 46 0 : std::vector<bool> addr(buffer.begin() + sep_pos + 1, buffer.end()); 47 0 : (void)Interpret(asmap, addr); 48 0 : } 49 0 : }