asmap_direct.cpp raw
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 <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 <cassert>
14
15 std::vector<std::byte> BitsToBytes(std::span<const uint8_t> bits) noexcept
16 {
17 std::vector<std::byte> ret;
18 uint8_t next_byte{0};
19 int next_byte_bits{0};
20 for (uint8_t val : bits) {
21 next_byte |= (val & 1) << (next_byte_bits++);
22 if (next_byte_bits == 8) {
23 ret.push_back(std::byte(next_byte));
24 next_byte = 0;
25 next_byte_bits = 0;
26 }
27 }
28 if (next_byte_bits) ret.push_back(std::byte(next_byte));
29
30 return ret;
31 }
32
33 FUZZ_TARGET(asmap_direct)
34 {
35 // Encoding: [asmap using 1 bit / byte] 0xFF [addr using 1 bit / byte]
36 std::optional<size_t> sep_pos_opt;
37 for (size_t pos = 0; pos < buffer.size(); ++pos) {
38 uint8_t x = buffer[pos];
39 if ((x & 0xFE) == 0) continue;
40 if (x == 0xFF) {
41 if (sep_pos_opt) return;
42 sep_pos_opt = pos;
43 } else {
44 return;
45 }
46 }
47 if (!sep_pos_opt) return; // Needs exactly 1 separator
48 const size_t sep_pos{sep_pos_opt.value()};
49 const size_t ip_len{buffer.size() - sep_pos - 1};
50 if (ip_len > 128) return; // At most 128 bits in IP address
51
52 // Checks on asmap
53 auto asmap = BitsToBytes(buffer.first(sep_pos));
54 if (SanityCheckAsmap(asmap, ip_len)) {
55 // Verify that for valid asmaps, no prefix (except up to 7 zero padding bits) is valid.
56 for (size_t prefix_len = sep_pos - 1; prefix_len > 0; --prefix_len) {
57 auto prefix = BitsToBytes(buffer.first(prefix_len));
58 // We have to skip the prefixes of the same length as the original
59 // asmap, since they will contain some zero padding bits in the last
60 // byte.
61 if (prefix.size() == asmap.size()) continue;
62 assert(!SanityCheckAsmap(prefix, ip_len));
63 }
64
65 // No address input should trigger assertions in interpreter
66 auto addr = BitsToBytes(buffer.subspan(sep_pos + 1));
67 (void)Interpret(asmap, addr);
68 }
69 }
70