Bitcoin ABC  0.26.3
P2P Digital Currency
asmap.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019 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,
22  const std::vector<bool>::const_iterator &endpos,
23  uint8_t minval, const std::vector<uint8_t> &bit_sizes) {
24  uint32_t val = minval;
25  bool bit;
26  for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
27  bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
28  if (bit_sizes_it + 1 != bit_sizes.end()) {
29  if (bitpos == endpos) {
30  break;
31  }
32  bit = *bitpos;
33  bitpos++;
34  } else {
35  bit = 0;
36  }
37  if (bit) {
38  val += (1 << *bit_sizes_it);
39  } else {
40  for (int b = 0; b < *bit_sizes_it; b++) {
41  if (bitpos == endpos) {
42  // Reached EOF in mantissa
43  return INVALID;
44  }
45  bit = *bitpos;
46  bitpos++;
47  val += bit << (*bit_sizes_it - 1 - b);
48  }
49  return val;
50  }
51  }
52  // Reached EOF in exponent
53  return INVALID;
54 }
55 
56 enum class Instruction : uint32_t {
57  RETURN = 0,
58  JUMP = 1,
59  MATCH = 2,
60  DEFAULT = 3,
61 };
62 
63 const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
64 Instruction DecodeType(std::vector<bool>::const_iterator &bitpos,
65  const std::vector<bool>::const_iterator &endpos) {
66  return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES));
67 }
68 
69 const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19,
70  20, 21, 22, 23, 24};
71 uint32_t DecodeASN(std::vector<bool>::const_iterator &bitpos,
72  const std::vector<bool>::const_iterator &endpos) {
73  return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES);
74 }
75 
76 const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
77 uint32_t DecodeMatch(std::vector<bool>::const_iterator &bitpos,
78  const std::vector<bool>::const_iterator &endpos) {
79  return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES);
80 }
81 
82 const std::vector<uint8_t> JUMP_BIT_SIZES{5, 6, 7, 8, 9, 10, 11, 12, 13,
83  14, 15, 16, 17, 18, 19, 20, 21, 22,
84  23, 24, 25, 26, 27, 28, 29, 30};
85 uint32_t DecodeJump(std::vector<bool>::const_iterator &bitpos,
86  const std::vector<bool>::const_iterator &endpos) {
87  return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES);
88 }
89 
90 } // namespace
91 
92 uint32_t Interpret(const std::vector<bool> &asmap,
93  const std::vector<bool> &ip) {
94  std::vector<bool>::const_iterator pos = asmap.begin();
95  const std::vector<bool>::const_iterator endpos = asmap.end();
96  uint8_t bits = ip.size();
97  uint32_t default_asn = 0;
98  uint32_t jump, match, matchlen;
99  Instruction opcode;
100  while (pos != endpos) {
101  opcode = DecodeType(pos, endpos);
102  if (opcode == Instruction::RETURN) {
103  default_asn = DecodeASN(pos, endpos);
104  if (default_asn == INVALID) {
105  // ASN straddles EOF
106  break;
107  }
108  return default_asn;
109  } else if (opcode == Instruction::JUMP) {
110  jump = DecodeJump(pos, endpos);
111  if (jump == INVALID) {
112  // Jump offset straddles EOF
113  break;
114  }
115  if (bits == 0) {
116  // No input bits left
117  break;
118  }
119  if (pos + jump < pos) {
120  // overflow
121  break;
122  }
123  if (pos + jump >= endpos) {
124  // Jumping past EOF
125  break;
126  }
127  if (ip[ip.size() - bits]) {
128  pos += jump;
129  }
130  bits--;
131  } else if (opcode == Instruction::MATCH) {
132  match = DecodeMatch(pos, endpos);
133  if (match == INVALID) {
134  // Match bits straddle EOF
135  break;
136  }
137  matchlen = CountBits(match) - 1;
138  if (bits < matchlen) {
139  // Not enough input bits
140  break;
141  }
142  for (uint32_t bit = 0; bit < matchlen; bit++) {
143  if ((ip[ip.size() - bits]) !=
144  ((match >> (matchlen - 1 - bit)) & 1)) {
145  return default_asn;
146  }
147  bits--;
148  }
149  } else if (opcode == Instruction::DEFAULT) {
150  default_asn = DecodeASN(pos, endpos);
151  if (default_asn == INVALID) {
152  // ASN straddles EOF
153  break;
154  }
155  } else {
156  // Instruction straddles EOF
157  break;
158  }
159  }
160 
161  // Reached EOF without RETURN, or aborted (see any of the breaks above) -
162  // should have been caught by SanityCheckASMap below
163  assert(false);
164 
165  // 0 is not a valid ASN
166  return 0;
167 }
168 
169 bool SanityCheckASMap(const std::vector<bool> &asmap, int bits) {
170  const std::vector<bool>::const_iterator begin = asmap.begin(),
171  endpos = asmap.end();
172  std::vector<bool>::const_iterator pos = begin;
173  // All future positions we may jump to (bit offset in asmap -> bits to
174  // consume left)
175  std::vector<std::pair<uint32_t, int>> jumps;
176  jumps.reserve(bits);
177  Instruction prevopcode = Instruction::JUMP;
178  bool had_incomplete_match = false;
179  while (pos != endpos) {
180  uint32_t offset = pos - begin;
181  if (!jumps.empty() && offset >= jumps.back().first) {
182  // There was a jump into the middle of the previous instruction
183  return false;
184  }
185  Instruction opcode = DecodeType(pos, endpos);
186  if (opcode == Instruction::RETURN) {
187  if (prevopcode == Instruction::DEFAULT) {
188  // There should not be any RETURN immediately after a DEFAULT
189  // (could be combined into just RETURN)
190  return false;
191  }
192  uint32_t asn = DecodeASN(pos, endpos);
193  if (asn == INVALID) {
194  // ASN straddles EOF
195  return false;
196  }
197  if (jumps.empty()) {
198  // Nothing to execute anymore
199  if (endpos - pos > 7) {
200  // Excessive padding
201  return false;
202  }
203  while (pos != endpos) {
204  if (*pos) {
205  // Nonzero padding bit
206  return false;
207  }
208  ++pos;
209  }
210  // Sanely reached EOF
211  return true;
212  } else {
213  // Continue by pretending we jumped to the next instruction
214  offset = pos - begin;
215  if (offset != jumps.back().first) {
216  // Unreachable code
217  return false;
218  }
219  // Restore the number of bits we would have had left after this
220  // jump
221  bits = jumps.back().second;
222  jumps.pop_back();
223  prevopcode = Instruction::JUMP;
224  }
225  } else if (opcode == Instruction::JUMP) {
226  uint32_t jump = DecodeJump(pos, endpos);
227  if (jump == INVALID) {
228  // Jump offset straddles EOF
229  return false;
230  }
231  if (pos + jump < pos) {
232  // overflow
233  return false;
234  }
235  if (pos + jump > endpos) {
236  // Jump out of range
237  return false;
238  }
239  if (bits == 0) {
240  // Consuming bits past the end of the input
241  return false;
242  }
243  --bits;
244  uint32_t jump_offset = pos - begin + jump;
245  if (!jumps.empty() && jump_offset >= jumps.back().first) {
246  // Intersecting jumps
247  return false;
248  }
249  jumps.emplace_back(jump_offset, bits);
250  prevopcode = Instruction::JUMP;
251  } else if (opcode == Instruction::MATCH) {
252  uint32_t match = DecodeMatch(pos, endpos);
253  if (match == INVALID) {
254  // Match bits straddle EOF
255  return false;
256  }
257  int matchlen = CountBits(match) - 1;
258  if (prevopcode != Instruction::MATCH) {
259  had_incomplete_match = false;
260  }
261  if (matchlen < 8 && had_incomplete_match) {
262  // Within a sequence of matches only at most one should be
263  // incomplete
264  return false;
265  }
266  had_incomplete_match = (matchlen < 8);
267  if (bits < matchlen) {
268  // Consuming bits past the end of the input
269  return false;
270  }
271  bits -= matchlen;
272  prevopcode = Instruction::MATCH;
273  } else if (opcode == Instruction::DEFAULT) {
274  if (prevopcode == Instruction::DEFAULT) {
275  // There should not be two successive DEFAULTs (they could be
276  // combined into one)
277  return false;
278  }
279  uint32_t asn = DecodeASN(pos, endpos);
280  if (asn == INVALID) {
281  // ASN straddles EOF
282  return false;
283  }
284  prevopcode = Instruction::DEFAULT;
285  } else {
286  // Instruction straddles EOF
287  return false;
288  }
289  }
290  // Reached EOF without RETURN instruction
291  return false;
292 }
293 
294 std::vector<bool> DecodeAsmap(fs::path path) {
295  std::vector<bool> bits;
296  FILE *filestr = fsbridge::fopen(path, "rb");
297  AutoFile file{filestr};
298  if (file.IsNull()) {
299  LogPrintf("Failed to open asmap file from disk\n");
300  return bits;
301  }
302  fseek(filestr, 0, SEEK_END);
303  int length = ftell(filestr);
304  LogPrintf("Opened asmap file %s (%d bytes) from disk\n",
305  fs::quoted(fs::PathToString(path)), length);
306  fseek(filestr, 0, SEEK_SET);
307  uint8_t cur_byte;
308  for (int i = 0; i < length; ++i) {
309  file >> cur_byte;
310  for (int bit = 0; bit < 8; ++bit) {
311  bits.push_back((cur_byte >> bit) & 1);
312  }
313  }
314  if (!SanityCheckASMap(bits, 128)) {
315  LogPrintf("Sanity check of asmap file %s failed\n",
317  return {};
318  }
319  return bits;
320 }
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
Definition: asmap.cpp:92
std::vector< bool > DecodeAsmap(fs::path path)
Read asmap from provided binary file.
Definition: asmap.cpp:294
bool SanityCheckASMap(const std::vector< bool > &asmap, int bits)
Definition: asmap.cpp:169
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:528
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
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:82
#define LogPrintf(...)
Definition: logging.h:206
static auto quoted(const std::string &s)
Definition: fs.h:107
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:142
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:28
assert(!tx.IsCoinBase())