48constexpr uint32_t
INVALID = 0xFFFFFFFF;
54inline bool ConsumeBitLE(
size_t& bitpos, std::span<const std::byte> bytes)
noexcept
56 const bool bit = (std::to_integer<uint8_t>(bytes[bitpos / 8]) >> (bitpos % 8)) & 1;
65inline bool ConsumeBitBE(uint8_t& bitpos, std::span<const std::byte> bytes)
noexcept
67 const bool bit = (std::to_integer<uint8_t>(bytes[bitpos / 8]) >> (7 - (bitpos % 8))) & 1;
87uint32_t DecodeBits(
size_t& bitpos,
const std::span<const std::byte>
data, uint8_t minval,
const std::span<const uint8_t> bit_sizes)
89 uint32_t val = minval;
91 for (
auto bit_sizes_it = bit_sizes.begin(); bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
93 if (bit_sizes_it + 1 != bit_sizes.end()) {
94 if (bitpos >=
data.size() * 8)
break;
95 bit = ConsumeBitLE(bitpos,
data);
102 val += (1 << *bit_sizes_it);
105 for (
int b = 0; b < *bit_sizes_it; b++) {
107 bit = ConsumeBitLE(bitpos,
data);
108 val += bit << (*bit_sizes_it - 1 - b);
123enum class Instruction : uint32_t
142constexpr uint8_t TYPE_BIT_SIZES[]{0, 0, 1};
143Instruction DecodeType(
size_t& bitpos,
const std::span<const std::byte>
data)
145 return Instruction(DecodeBits(bitpos,
data, 0, TYPE_BIT_SIZES));
151constexpr uint8_t ASN_BIT_SIZES[]{15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
152uint32_t DecodeASN(
size_t& bitpos,
const std::span<const std::byte>
data)
154 return DecodeBits(bitpos,
data, 1, ASN_BIT_SIZES);
159constexpr uint8_t MATCH_BIT_SIZES[]{1, 2, 3, 4, 5, 6, 7, 8};
160uint32_t DecodeMatch(
size_t& bitpos,
const std::span<const std::byte>
data)
162 return DecodeBits(bitpos,
data, 2, MATCH_BIT_SIZES);
167constexpr 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};
168uint32_t DecodeJump(
size_t& bitpos,
const std::span<const std::byte>
data)
170 return DecodeBits(bitpos,
data, 17, JUMP_BIT_SIZES);
182uint32_t
Interpret(
const std::span<const std::byte> asmap,
const std::span<const std::byte>
ip)
185 const size_t endpos{asmap.size() * 8};
187 const uint8_t ip_bits_end =
ip.size() * 8;
188 uint32_t default_asn = 0;
189 while (pos < endpos) {
190 Instruction opcode = DecodeType(pos, asmap);
191 if (opcode == Instruction::RETURN) {
193 uint32_t asn = DecodeASN(pos, asmap);
196 }
else if (opcode == Instruction::JUMP) {
198 uint32_t jump = DecodeJump(pos, asmap);
200 if (ip_bit == ip_bits_end)
break;
201 if (int64_t{jump} >=
static_cast<int64_t
>(endpos - pos))
break;
202 if (ConsumeBitBE(ip_bit,
ip)) {
206 }
else if (opcode == Instruction::MATCH) {
211 uint32_t match = DecodeMatch(pos, asmap);
213 int matchlen = std::bit_width(match) - 1;
214 if ((ip_bits_end - ip_bit) < matchlen)
break;
215 for (
int bit = 0; bit < matchlen; bit++) {
216 if (ConsumeBitBE(ip_bit,
ip) != ((match >> (matchlen - 1 - bit)) & 1)) {
221 }
else if (opcode == Instruction::DEFAULT) {
223 default_asn = DecodeASN(pos, asmap);
224 if (default_asn ==
INVALID)
break;
242 const size_t endpos{asmap.size() * 8};
243 std::vector<std::pair<uint32_t, int>> jumps;
245 Instruction prevopcode = Instruction::JUMP;
246 bool had_incomplete_match =
false;
248 while (pos != endpos) {
250 if (!jumps.empty() && pos >= jumps.back().first)
return false;
252 Instruction opcode = DecodeType(pos, asmap);
253 if (opcode == Instruction::RETURN) {
255 if (prevopcode == Instruction::DEFAULT)
return false;
256 uint32_t asn = DecodeASN(pos, asmap);
257 if (asn ==
INVALID)
return false;
260 if (endpos - pos > 7)
return false;
261 while (pos != endpos) {
262 if (ConsumeBitLE(pos, asmap))
return false;
267 if (pos != jumps.back().first)
return false;
268 bits = jumps.back().second;
270 prevopcode = Instruction::JUMP;
272 }
else if (opcode == Instruction::JUMP) {
273 uint32_t jump = DecodeJump(pos, asmap);
274 if (jump ==
INVALID)
return false;
275 if (int64_t{jump} >
static_cast<int64_t
>(endpos - pos))
return false;
276 if (bits == 0)
return false;
278 uint32_t jump_offset = pos + jump;
279 if (!jumps.empty() && jump_offset >= jumps.back().first)
return false;
280 jumps.emplace_back(jump_offset, bits);
281 prevopcode = Instruction::JUMP;
282 }
else if (opcode == Instruction::MATCH) {
283 uint32_t match = DecodeMatch(pos, asmap);
284 if (match ==
INVALID)
return false;
285 int matchlen = std::bit_width(match) - 1;
286 if (prevopcode != Instruction::MATCH) had_incomplete_match =
false;
288 if (matchlen < 8 && had_incomplete_match)
return false;
289 had_incomplete_match = (matchlen < 8);
290 if (bits < matchlen)
return false;
292 prevopcode = Instruction::MATCH;
293 }
else if (opcode == Instruction::DEFAULT) {
295 if (prevopcode == Instruction::DEFAULT)
return false;
296 uint32_t asn = DecodeASN(pos, asmap);
297 if (asn ==
INVALID)
return false;
298 prevopcode = Instruction::DEFAULT;
313 LogWarning(
"Sanity check of asmap data failed\n");
327 LogWarning(
"Failed to open asmap file from disk");
330 int64_t length{file.size()};
334 std::vector<std::byte> buffer(length);
350 if (
data.empty())
return {};
353 asmap_hasher <<
data;
Non-refcounted RAII wrapper for FILE*.
A writer stream (for serialization) that computes a 256-bit hash.
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
static CService ip(uint32_t i)
static auto quoted(const std::string &s)
static std::string PathToString(const path &path)
Convert path object to a byte string.
FILE * fopen(const fs::path &p, const char *mode)
static const auto INVALID
A stack representing the lack of any (dis)satisfactions.
bool SanityCheckAsmap(const std::span< const std::byte > asmap, int bits)
Validates ASMap structure by simulating all possible execution paths.
bool CheckStandardAsmap(const std::span< const std::byte > data)
Provides a safe interface for validating ASMap data before use.
std::vector< std::byte > DecodeAsmap(fs::path path)
Loads an ASMap file from disk and validates it.
uint32_t Interpret(const std::span< const std::byte > asmap, const std::span< const std::byte > ip)
Execute the ASMap bytecode to find the ASN for an IP.
uint256 AsmapVersion(const std::span< const std::byte > data)
Computes SHA256 hash of ASMap data for versioning and consistency checks.