Branch data Line data Source code
# 1 : : // Copyright (c) 2019-2020 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 <map>
# 6 : : #include <vector>
# 7 : : #include <assert.h>
# 8 : : #include <crypto/common.h>
# 9 : :
# 10 : : namespace {
# 11 : :
# 12 : : constexpr uint32_t INVALID = 0xFFFFFFFF;
# 13 : :
# 14 : : 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)
# 15 : 361448 : {
# 16 : 361448 : uint32_t val = minval;
# 17 : 361448 : bool bit;
# 18 : 361448 : for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
# 19 [ + - ]: 1802536 : bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
# 20 [ + + ]: 1802536 : if (bit_sizes_it + 1 != bit_sizes.end()) {
# 21 [ - + ]: 1490872 : if (bitpos == endpos) break;
# 22 : 1490872 : bit = *bitpos;
# 23 : 1490872 : bitpos++;
# 24 : 1490872 : } else {
# 25 : 311664 : bit = 0;
# 26 : 311664 : }
# 27 [ + + ]: 1802536 : if (bit) {
# 28 : 1441088 : val += (1 << *bit_sizes_it);
# 29 : 1441088 : } else {
# 30 [ + + ]: 2026224 : for (int b = 0; b < *bit_sizes_it; b++) {
# 31 [ - + ]: 1664776 : if (bitpos == endpos) return INVALID; // Reached EOF in mantissa
# 32 : 1664776 : bit = *bitpos;
# 33 : 1664776 : bitpos++;
# 34 : 1664776 : val += bit << (*bit_sizes_it - 1 - b);
# 35 : 1664776 : }
# 36 : 361448 : return val;
# 37 : 361448 : }
# 38 : 1802536 : }
# 39 : 361448 : return INVALID; // Reached EOF in exponent
# 40 : 361448 : }
# 41 : :
# 42 : : enum class Instruction : uint32_t
# 43 : : {
# 44 : : RETURN = 0,
# 45 : : JUMP = 1,
# 46 : : MATCH = 2,
# 47 : : DEFAULT = 3,
# 48 : : };
# 49 : :
# 50 : : const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
# 51 : : Instruction DecodeType(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
# 52 : 180724 : {
# 53 : 180724 : return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES));
# 54 : 180724 : }
# 55 : :
# 56 : : const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
# 57 : : uint32_t DecodeASN(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
# 58 : 11424 : {
# 59 : 11424 : return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES);
# 60 : 11424 : }
# 61 : :
# 62 : :
# 63 : : const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
# 64 : : uint32_t DecodeMatch(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
# 65 : 156218 : {
# 66 : 156218 : return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES);
# 67 : 156218 : }
# 68 : :
# 69 : :
# 70 : : 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};
# 71 : : uint32_t DecodeJump(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
# 72 : 13082 : {
# 73 : 13082 : return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES);
# 74 : 13082 : }
# 75 : :
# 76 : : }
# 77 : :
# 78 : : uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
# 79 : 12822 : {
# 80 : 12822 : std::vector<bool>::const_iterator pos = asmap.begin();
# 81 : 12822 : const std::vector<bool>::const_iterator endpos = asmap.end();
# 82 : 12822 : uint8_t bits = ip.size();
# 83 : 12822 : uint32_t default_asn = 0;
# 84 : 12822 : uint32_t jump, match, matchlen;
# 85 : 12822 : Instruction opcode;
# 86 [ + - ]: 180600 : while (pos != endpos) {
# 87 : 180600 : opcode = DecodeType(pos, endpos);
# 88 [ + + ]: 180600 : if (opcode == Instruction::RETURN) {
# 89 : 11388 : default_asn = DecodeASN(pos, endpos);
# 90 [ - + ]: 11388 : if (default_asn == INVALID) break; // ASN straddles EOF
# 91 : 11388 : return default_asn;
# 92 [ + + ]: 169212 : } else if (opcode == Instruction::JUMP) {
# 93 : 13050 : jump = DecodeJump(pos, endpos);
# 94 [ - + ]: 13050 : if (jump == INVALID) break; // Jump offset straddles EOF
# 95 [ - + ]: 13050 : if (bits == 0) break; // No input bits left
# 96 [ - + ]: 13050 : if (int64_t{jump} >= int64_t{endpos - pos}) break; // Jumping past EOF
# 97 [ + + ]: 13050 : if (ip[ip.size() - bits]) {
# 98 : 11406 : pos += jump;
# 99 : 11406 : }
# 100 : 13050 : bits--;
# 101 [ + - ]: 156162 : } else if (opcode == Instruction::MATCH) {
# 102 : 156162 : match = DecodeMatch(pos, endpos);
# 103 [ - + ]: 156162 : if (match == INVALID) break; // Match bits straddle EOF
# 104 : 156162 : matchlen = CountBits(match) - 1;
# 105 [ - + ]: 156162 : if (bits < matchlen) break; // Not enough input bits
# 106 [ + + ]: 1399224 : for (uint32_t bit = 0; bit < matchlen; bit++) {
# 107 [ + + ]: 1244496 : if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
# 108 : 1434 : return default_asn;
# 109 : 1434 : }
# 110 : 1243062 : bits--;
# 111 : 1243062 : }
# 112 [ # # ]: 156162 : } else if (opcode == Instruction::DEFAULT) {
# 113 : 0 : default_asn = DecodeASN(pos, endpos);
# 114 [ # # ]: 0 : if (default_asn == INVALID) break; // ASN straddles EOF
# 115 : 0 : } else {
# 116 : 0 : break; // Instruction straddles EOF
# 117 : 0 : }
# 118 : 180600 : }
# 119 : 12822 : assert(false); // Reached EOF without RETURN, or aborted (see any of the breaks above) - should have been caught by SanityCheckASMap below
# 120 : 0 : return 0; // 0 is not a valid ASN
# 121 : 12822 : }
# 122 : :
# 123 : : bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
# 124 : 5 : {
# 125 : 5 : const std::vector<bool>::const_iterator begin = asmap.begin(), endpos = asmap.end();
# 126 : 5 : std::vector<bool>::const_iterator pos = begin;
# 127 : 5 : std::vector<std::pair<uint32_t, int>> jumps; // All future positions we may jump to (bit offset in asmap -> bits to consume left)
# 128 : 5 : jumps.reserve(bits);
# 129 : 5 : Instruction prevopcode = Instruction::JUMP;
# 130 : 5 : bool had_incomplete_match = false;
# 131 [ + + ]: 125 : while (pos != endpos) {
# 132 : 124 : uint32_t offset = pos - begin;
# 133 [ + + ][ - + ]: 124 : if (!jumps.empty() && offset >= jumps.back().first) return false; // There was a jump into the middle of the previous instruction
# 134 : 124 : Instruction opcode = DecodeType(pos, endpos);
# 135 [ + + ]: 124 : if (opcode == Instruction::RETURN) {
# 136 [ - + ]: 36 : if (prevopcode == Instruction::DEFAULT) return false; // There should not be any RETURN immediately after a DEFAULT (could be combined into just RETURN)
# 137 : 36 : uint32_t asn = DecodeASN(pos, endpos);
# 138 [ - + ]: 36 : if (asn == INVALID) return false; // ASN straddles EOF
# 139 [ + + ]: 36 : if (jumps.empty()) {
# 140 : : // Nothing to execute anymore
# 141 [ - + ]: 4 : if (endpos - pos > 7) return false; // Excessive padding
# 142 [ + + ]: 12 : while (pos != endpos) {
# 143 [ - + ]: 8 : if (*pos) return false; // Nonzero padding bit
# 144 : 8 : ++pos;
# 145 : 8 : }
# 146 : 4 : return true; // Sanely reached EOF
# 147 : 32 : } else {
# 148 : : // Continue by pretending we jumped to the next instruction
# 149 : 32 : offset = pos - begin;
# 150 [ - + ]: 32 : if (offset != jumps.back().first) return false; // Unreachable code
# 151 : 32 : bits = jumps.back().second; // Restore the number of bits we would have had left after this jump
# 152 : 32 : jumps.pop_back();
# 153 : 32 : prevopcode = Instruction::JUMP;
# 154 : 32 : }
# 155 [ + + ]: 88 : } else if (opcode == Instruction::JUMP) {
# 156 : 32 : uint32_t jump = DecodeJump(pos, endpos);
# 157 [ - + ]: 32 : if (jump == INVALID) return false; // Jump offset straddles EOF
# 158 [ - + ]: 32 : if (int64_t{jump} > int64_t{endpos - pos}) return false; // Jump out of range
# 159 [ - + ]: 32 : if (bits == 0) return false; // Consuming bits past the end of the input
# 160 : 32 : --bits;
# 161 : 32 : uint32_t jump_offset = pos - begin + jump;
# 162 [ + + ][ - + ]: 32 : if (!jumps.empty() && jump_offset >= jumps.back().first) return false; // Intersecting jumps
# 163 : 32 : jumps.emplace_back(jump_offset, bits);
# 164 : 32 : prevopcode = Instruction::JUMP;
# 165 [ + - ]: 56 : } else if (opcode == Instruction::MATCH) {
# 166 : 56 : uint32_t match = DecodeMatch(pos, endpos);
# 167 [ - + ]: 56 : if (match == INVALID) return false; // Match bits straddle EOF
# 168 : 56 : int matchlen = CountBits(match) - 1;
# 169 [ + + ]: 56 : if (prevopcode != Instruction::MATCH) had_incomplete_match = false;
# 170 [ + + ][ - + ]: 56 : if (matchlen < 8 && had_incomplete_match) return false; // Within a sequence of matches only at most one should be incomplete
# 171 : 56 : had_incomplete_match = (matchlen < 8);
# 172 [ - + ]: 56 : if (bits < matchlen) return false; // Consuming bits past the end of the input
# 173 : 56 : bits -= matchlen;
# 174 : 56 : prevopcode = Instruction::MATCH;
# 175 [ # # ]: 56 : } else if (opcode == Instruction::DEFAULT) {
# 176 [ # # ]: 0 : if (prevopcode == Instruction::DEFAULT) return false; // There should not be two successive DEFAULTs (they could be combined into one)
# 177 : 0 : uint32_t asn = DecodeASN(pos, endpos);
# 178 [ # # ]: 0 : if (asn == INVALID) return false; // ASN straddles EOF
# 179 : 0 : prevopcode = Instruction::DEFAULT;
# 180 : 0 : } else {
# 181 : 0 : return false; // Instruction straddles EOF
# 182 : 0 : }
# 183 : 124 : }
# 184 : 5 : return false; // Reached EOF without RETURN instruction
# 185 : 5 : }
|