46constexpr uint32_t
INVALID = 0xFFFFFFFF;
52inline bool ConsumeBitLE(
size_t& bitpos, std::span<const std::byte> bytes)
noexcept
54 const bool bit = (std::to_integer<uint8_t>(bytes[bitpos / 8]) >> (bitpos % 8)) & 1;
63inline bool ConsumeBitBE(uint8_t& bitpos, std::span<const std::byte> bytes)
noexcept
65 const bool bit = (std::to_integer<uint8_t>(bytes[bitpos / 8]) >> (7 - (bitpos % 8))) & 1;
85uint32_t DecodeBits(
size_t& bitpos,
const std::span<const std::byte>
data, uint8_t minval,
const std::span<const uint8_t> bit_sizes)
87 uint32_t val = minval;
89 for (
auto bit_sizes_it = bit_sizes.begin(); bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
91 if (bit_sizes_it + 1 != bit_sizes.end()) {
92 if (bitpos >=
data.size() * 8)
break;
93 bit = ConsumeBitLE(bitpos,
data);
100 val += (1 << *bit_sizes_it);
103 for (
int b = 0; b < *bit_sizes_it; b++) {
105 bit = ConsumeBitLE(bitpos,
data);
106 val += bit << (*bit_sizes_it - 1 - b);
121enum class Instruction : uint32_t
140constexpr uint8_t TYPE_BIT_SIZES[]{0, 0, 1};
141Instruction DecodeType(
size_t& bitpos,
const std::span<const std::byte>
data)
143 return Instruction(DecodeBits(bitpos,
data, 0, TYPE_BIT_SIZES));
149constexpr uint8_t ASN_BIT_SIZES[]{15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
150uint32_t DecodeASN(
size_t& bitpos,
const std::span<const std::byte>
data)
152 return DecodeBits(bitpos,
data, 1, ASN_BIT_SIZES);
157constexpr uint8_t MATCH_BIT_SIZES[]{1, 2, 3, 4, 5, 6, 7, 8};
158uint32_t DecodeMatch(
size_t& bitpos,
const std::span<const std::byte>
data)
160 return DecodeBits(bitpos,
data, 2, MATCH_BIT_SIZES);
165constexpr 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};
166uint32_t DecodeJump(
size_t& bitpos,
const std::span<const std::byte>
data)
168 return DecodeBits(bitpos,
data, 17, JUMP_BIT_SIZES);
180uint32_t
Interpret(
const std::span<const std::byte> asmap,
const std::span<const std::byte>
ip)
183 const size_t endpos{asmap.size() * 8};
185 const uint8_t ip_bits_end =
ip.size() * 8;
186 uint32_t default_asn = 0;
187 while (pos < endpos) {
188 Instruction opcode = DecodeType(pos, asmap);
189 if (opcode == Instruction::RETURN) {
191 uint32_t asn = DecodeASN(pos, asmap);
194 }
else if (opcode == Instruction::JUMP) {
196 uint32_t jump = DecodeJump(pos, asmap);
198 if (ip_bit == ip_bits_end)
break;
199 if (int64_t{jump} >=
static_cast<int64_t
>(endpos - pos))
break;
200 if (ConsumeBitBE(ip_bit,
ip)) {
204 }
else if (opcode == Instruction::MATCH) {
209 uint32_t match = DecodeMatch(pos, asmap);
211 int matchlen = std::bit_width(match) - 1;
212 if ((ip_bits_end - ip_bit) < matchlen)
break;
213 for (
int bit = 0; bit < matchlen; bit++) {
214 if (ConsumeBitBE(ip_bit,
ip) != ((match >> (matchlen - 1 - bit)) & 1)) {
219 }
else if (opcode == Instruction::DEFAULT) {
221 default_asn = DecodeASN(pos, asmap);
222 if (default_asn ==
INVALID)
break;
240 const size_t endpos{asmap.size() * 8};
241 std::vector<std::pair<uint32_t, int>> jumps;
243 Instruction prevopcode = Instruction::JUMP;
244 bool had_incomplete_match =
false;
246 while (pos != endpos) {
248 if (!jumps.empty() && pos >= jumps.back().first)
return false;
250 Instruction opcode = DecodeType(pos, asmap);
251 if (opcode == Instruction::RETURN) {
253 if (prevopcode == Instruction::DEFAULT)
return false;
254 uint32_t asn = DecodeASN(pos, asmap);
255 if (asn ==
INVALID)
return false;
258 if (endpos - pos > 7)
return false;
259 while (pos != endpos) {
260 if (ConsumeBitLE(pos, asmap))
return false;
265 if (pos != jumps.back().first)
return false;
266 bits = jumps.back().second;
268 prevopcode = Instruction::JUMP;
270 }
else if (opcode == Instruction::JUMP) {
271 uint32_t jump = DecodeJump(pos, asmap);
272 if (jump ==
INVALID)
return false;
273 if (int64_t{jump} >
static_cast<int64_t
>(endpos - pos))
return false;
274 if (bits == 0)
return false;
276 uint32_t jump_offset = pos + jump;
277 if (!jumps.empty() && jump_offset >= jumps.back().first)
return false;
278 jumps.emplace_back(jump_offset, bits);
279 prevopcode = Instruction::JUMP;
280 }
else if (opcode == Instruction::MATCH) {
281 uint32_t match = DecodeMatch(pos, asmap);
282 if (match ==
INVALID)
return false;
283 int matchlen = std::bit_width(match) - 1;
284 if (prevopcode != Instruction::MATCH) had_incomplete_match =
false;
286 if (matchlen < 8 && had_incomplete_match)
return false;
287 had_incomplete_match = (matchlen < 8);
288 if (bits < matchlen)
return false;
290 prevopcode = Instruction::MATCH;
291 }
else if (opcode == Instruction::DEFAULT) {
293 if (prevopcode == Instruction::DEFAULT)
return false;
294 uint32_t asn = DecodeASN(pos, asmap);
295 if (asn ==
INVALID)
return false;
296 prevopcode = Instruction::DEFAULT;
311 LogWarning(
"Sanity check of asmap data failed\n");
325 LogWarning(
"Failed to open asmap file from disk");
328 int64_t length{file.size()};
332 std::vector<std::byte> buffer(length);
348 if (
data.empty())
return {};
351 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.