Bitcoin Core  22.99.0
P2P Digital Currency
asmap.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-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 <util/asmap.h>
6 
7 #include <clientversion.h>
8 #include <crypto/common.h>
9 #include <fs.h>
10 #include <logging.h>
11 #include <streams.h>
12 
13 #include <cassert>
14 #include <map>
15 #include <vector>
16 
17 namespace {
18 
19 constexpr uint32_t INVALID = 0xFFFFFFFF;
20 
21 uint32_t DecodeBits(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos, uint8_t minval, const std::vector<uint8_t> &bit_sizes)
22 {
23  uint32_t val = minval;
24  bool bit;
25  for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
26  bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
27  if (bit_sizes_it + 1 != bit_sizes.end()) {
28  if (bitpos == endpos) break;
29  bit = *bitpos;
30  bitpos++;
31  } else {
32  bit = 0;
33  }
34  if (bit) {
35  val += (1 << *bit_sizes_it);
36  } else {
37  for (int b = 0; b < *bit_sizes_it; b++) {
38  if (bitpos == endpos) return INVALID; // Reached EOF in mantissa
39  bit = *bitpos;
40  bitpos++;
41  val += bit << (*bit_sizes_it - 1 - b);
42  }
43  return val;
44  }
45  }
46  return INVALID; // Reached EOF in exponent
47 }
48 
49 enum class Instruction : uint32_t
50 {
51  RETURN = 0,
52  JUMP = 1,
53  MATCH = 2,
54  DEFAULT = 3,
55 };
56 
57 const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
58 Instruction DecodeType(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
59 {
60  return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES));
61 }
62 
63 const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
64 uint32_t DecodeASN(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
65 {
66  return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES);
67 }
68 
69 
70 const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
71 uint32_t DecodeMatch(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
72 {
73  return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES);
74 }
75 
76 
77 const std::vector<uint8_t> JUMP_BIT_SIZES{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
78 uint32_t DecodeJump(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
79 {
80  return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES);
81 }
82 
83 }
84 
85 uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
86 {
87  std::vector<bool>::const_iterator pos = asmap.begin();
88  const std::vector<bool>::const_iterator endpos = asmap.end();
89  uint8_t bits = ip.size();
90  uint32_t default_asn = 0;
91  uint32_t jump, match, matchlen;
92  Instruction opcode;
93  while (pos != endpos) {
94  opcode = DecodeType(pos, endpos);
95  if (opcode == Instruction::RETURN) {
96  default_asn = DecodeASN(pos, endpos);
97  if (default_asn == INVALID) break; // ASN straddles EOF
98  return default_asn;
99  } else if (opcode == Instruction::JUMP) {
100  jump = DecodeJump(pos, endpos);
101  if (jump == INVALID) break; // Jump offset straddles EOF
102  if (bits == 0) break; // No input bits left
103  if (int64_t{jump} >= int64_t{endpos - pos}) break; // Jumping past EOF
104  if (ip[ip.size() - bits]) {
105  pos += jump;
106  }
107  bits--;
108  } else if (opcode == Instruction::MATCH) {
109  match = DecodeMatch(pos, endpos);
110  if (match == INVALID) break; // Match bits straddle EOF
111  matchlen = CountBits(match) - 1;
112  if (bits < matchlen) break; // Not enough input bits
113  for (uint32_t bit = 0; bit < matchlen; bit++) {
114  if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
115  return default_asn;
116  }
117  bits--;
118  }
119  } else if (opcode == Instruction::DEFAULT) {
120  default_asn = DecodeASN(pos, endpos);
121  if (default_asn == INVALID) break; // ASN straddles EOF
122  } else {
123  break; // Instruction straddles EOF
124  }
125  }
126  assert(false); // Reached EOF without RETURN, or aborted (see any of the breaks above) - should have been caught by SanityCheckASMap below
127  return 0; // 0 is not a valid ASN
128 }
129 
130 bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
131 {
132  const std::vector<bool>::const_iterator begin = asmap.begin(), endpos = asmap.end();
133  std::vector<bool>::const_iterator pos = begin;
134  std::vector<std::pair<uint32_t, int>> jumps; // All future positions we may jump to (bit offset in asmap -> bits to consume left)
135  jumps.reserve(bits);
136  Instruction prevopcode = Instruction::JUMP;
137  bool had_incomplete_match = false;
138  while (pos != endpos) {
139  uint32_t offset = pos - begin;
140  if (!jumps.empty() && offset >= jumps.back().first) return false; // There was a jump into the middle of the previous instruction
141  Instruction opcode = DecodeType(pos, endpos);
142  if (opcode == Instruction::RETURN) {
143  if (prevopcode == Instruction::DEFAULT) return false; // There should not be any RETURN immediately after a DEFAULT (could be combined into just RETURN)
144  uint32_t asn = DecodeASN(pos, endpos);
145  if (asn == INVALID) return false; // ASN straddles EOF
146  if (jumps.empty()) {
147  // Nothing to execute anymore
148  if (endpos - pos > 7) return false; // Excessive padding
149  while (pos != endpos) {
150  if (*pos) return false; // Nonzero padding bit
151  ++pos;
152  }
153  return true; // Sanely reached EOF
154  } else {
155  // Continue by pretending we jumped to the next instruction
156  offset = pos - begin;
157  if (offset != jumps.back().first) return false; // Unreachable code
158  bits = jumps.back().second; // Restore the number of bits we would have had left after this jump
159  jumps.pop_back();
160  prevopcode = Instruction::JUMP;
161  }
162  } else if (opcode == Instruction::JUMP) {
163  uint32_t jump = DecodeJump(pos, endpos);
164  if (jump == INVALID) return false; // Jump offset straddles EOF
165  if (int64_t{jump} > int64_t{endpos - pos}) return false; // Jump out of range
166  if (bits == 0) return false; // Consuming bits past the end of the input
167  --bits;
168  uint32_t jump_offset = pos - begin + jump;
169  if (!jumps.empty() && jump_offset >= jumps.back().first) return false; // Intersecting jumps
170  jumps.emplace_back(jump_offset, bits);
171  prevopcode = Instruction::JUMP;
172  } else if (opcode == Instruction::MATCH) {
173  uint32_t match = DecodeMatch(pos, endpos);
174  if (match == INVALID) return false; // Match bits straddle EOF
175  int matchlen = CountBits(match) - 1;
176  if (prevopcode != Instruction::MATCH) had_incomplete_match = false;
177  if (matchlen < 8 && had_incomplete_match) return false; // Within a sequence of matches only at most one should be incomplete
178  had_incomplete_match = (matchlen < 8);
179  if (bits < matchlen) return false; // Consuming bits past the end of the input
180  bits -= matchlen;
181  prevopcode = Instruction::MATCH;
182  } else if (opcode == Instruction::DEFAULT) {
183  if (prevopcode == Instruction::DEFAULT) return false; // There should not be two successive DEFAULTs (they could be combined into one)
184  uint32_t asn = DecodeASN(pos, endpos);
185  if (asn == INVALID) return false; // ASN straddles EOF
186  prevopcode = Instruction::DEFAULT;
187  } else {
188  return false; // Instruction straddles EOF
189  }
190  }
191  return false; // Reached EOF without RETURN instruction
192 }
193 
194 std::vector<bool> DecodeAsmap(fs::path path)
195 {
196  std::vector<bool> bits;
197  FILE *filestr = fsbridge::fopen(path, "rb");
198  CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
199  if (file.IsNull()) {
200  LogPrintf("Failed to open asmap file from disk\n");
201  return bits;
202  }
203  fseek(filestr, 0, SEEK_END);
204  int length = ftell(filestr);
205  LogPrintf("Opened asmap file %s (%d bytes) from disk\n", fs::quoted(fs::PathToString(path)), length);
206  fseek(filestr, 0, SEEK_SET);
207  uint8_t cur_byte;
208  for (int i = 0; i < length; ++i) {
209  file >> cur_byte;
210  for (int bit = 0; bit < 8; ++bit) {
211  bits.push_back((cur_byte >> bit) & 1);
212  }
213  }
214  if (!SanityCheckASMap(bits, 128)) {
215  LogPrintf("Sanity check of asmap file %s failed\n", fs::quoted(fs::PathToString(path)));
216  return {};
217  }
218  return bits;
219 }
220 
assert
assert(!tx.IsCoinBase())
SER_DISK
@ SER_DISK
Definition: serialize.h:132
fs.h
fsbridge::fopen
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:27
asmap.h
streams.h
fs::quoted
static auto quoted(const std::string &s)
Definition: fs.h:75
clientversion.h
fs::PathToString
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:112
common.h
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:478
bech32::Encoding::INVALID
@ INVALID
Failed decoding.
DecodeAsmap
std::vector< bool > DecodeAsmap(fs::path path)
Read asmap from provided binary file.
Definition: asmap.cpp:194
ip
static CService ip(uint32_t i)
Definition: denialofservice_tests.cpp:30
fs::path
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:29
LogPrintf
#define LogPrintf(...)
Definition: logging.h:188
CAutoFile::IsNull
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:523
Interpret
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
Definition: asmap.cpp:85
CLIENT_VERSION
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
VarIntMode::DEFAULT
@ DEFAULT
SanityCheckASMap
bool SanityCheckASMap(const std::vector< bool > &asmap, int bits)
Definition: asmap.cpp:130
CountBits
static uint64_t CountBits(uint64_t x)
Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set.
Definition: common.h:90