Bitcoin Core  27.99.0
P2P Digital Currency
asmap_direct.cpp
Go to the documentation of this file.
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 FUZZ_TARGET(asmap_direct)
16 {
17  // Encoding: [asmap using 1 bit / byte] 0xFF [addr using 1 bit / byte]
18  std::optional<size_t> sep_pos_opt;
19  for (size_t pos = 0; pos < buffer.size(); ++pos) {
20  uint8_t x = buffer[pos];
21  if ((x & 0xFE) == 0) continue;
22  if (x == 0xFF) {
23  if (sep_pos_opt) return;
24  sep_pos_opt = pos;
25  } else {
26  return;
27  }
28  }
29  if (!sep_pos_opt) return; // Needs exactly 1 separator
30  const size_t sep_pos{sep_pos_opt.value()};
31  if (buffer.size() - sep_pos - 1 > 128) return; // At most 128 bits in IP address
32 
33  // Checks on asmap
34  std::vector<bool> asmap(buffer.begin(), buffer.begin() + sep_pos);
35  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  std::vector<bool> asmap_prefix = asmap;
38  while (!asmap_prefix.empty() && asmap_prefix.size() + 7 > asmap.size() && asmap_prefix.back() == false) {
39  asmap_prefix.pop_back();
40  }
41  while (!asmap_prefix.empty()) {
42  asmap_prefix.pop_back();
43  assert(!SanityCheckASMap(asmap_prefix, buffer.size() - 1 - sep_pos));
44  }
45  // No address input should trigger assertions in interpreter
46  std::vector<bool> addr(buffer.begin() + sep_pos + 1, buffer.end());
47  (void)Interpret(asmap, addr);
48  }
49 }
FUZZ_TARGET(asmap_direct)
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
Definition: asmap.cpp:88
bool SanityCheckASMap(const std::vector< bool > &asmap, int bits)
Definition: asmap.cpp:133
assert(!tx.IsCoinBase())