Branch data Line data Source code
1 : : // Copyright (c) 2019-present 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 <logging.h>
9 : : #include <serialize.h>
10 : : #include <streams.h>
11 : : #include <util/fs.h>
12 : :
13 : : #include <algorithm>
14 : : #include <bit>
15 : : #include <cassert>
16 : : #include <cstdio>
17 : : #include <utility>
18 : : #include <vector>
19 : :
20 : : namespace {
21 : :
22 : : constexpr uint32_t INVALID = 0xFFFFFFFF;
23 : :
24 : 67051108 : 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)
25 : : {
26 : 67051108 : uint32_t val = minval;
27 : 67051108 : bool bit;
28 : 67051108 : for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
29 [ + - ]: 159221911 : bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
30 [ + + ]: 159221911 : if (bit_sizes_it + 1 != bit_sizes.end()) {
31 [ - + ]: 142805967 : if (bitpos == endpos) break;
32 : 142782184 : bit = *bitpos;
33 : 142782184 : bitpos++;
34 : : } else {
35 : : bit = 0;
36 : : }
37 [ + + ]: 159198128 : if (bit) {
38 : 92170803 : val += (1 << *bit_sizes_it);
39 : : } else {
40 [ + + ]: 440269531 : for (int b = 0; b < *bit_sizes_it; b++) {
41 [ - + ]: 373304133 : if (bitpos == endpos) return INVALID; // Reached EOF in mantissa
42 : 373242206 : bit = *bitpos;
43 : 373242206 : bitpos++;
44 : 373242206 : val += bit << (*bit_sizes_it - 1 - b);
45 : : }
46 : : return val;
47 : : }
48 : : }
49 : : return INVALID; // Reached EOF in exponent
50 : : }
51 : :
52 : : enum class Instruction : uint32_t
53 : : {
54 : : RETURN = 0,
55 : : JUMP = 1,
56 : : MATCH = 2,
57 : : DEFAULT = 3,
58 : : };
59 : :
60 : : const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
61 : 33530594 : Instruction DecodeType(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
62 : : {
63 : 33530594 : return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES));
64 : : }
65 : :
66 : : const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
67 : 17070434 : uint32_t DecodeASN(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
68 : : {
69 : 17070434 : return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES);
70 : : }
71 : :
72 : :
73 : : const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
74 : 8360683 : uint32_t DecodeMatch(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
75 : : {
76 : 8360683 : return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES);
77 : : }
78 : :
79 : :
80 : : 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};
81 : 8089397 : uint32_t DecodeJump(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
82 : : {
83 : 8089397 : return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES);
84 : : }
85 : :
86 : : }
87 : :
88 : 13043090 : uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
89 : : {
90 : 13043090 : std::vector<bool>::const_iterator pos = asmap.begin();
91 : 13043090 : const std::vector<bool>::const_iterator endpos = asmap.end();
92 : 13043090 : uint8_t bits = ip.size();
93 : 13043090 : uint32_t default_asn = 0;
94 : 13043090 : uint32_t jump, match, matchlen;
95 : 13043090 : Instruction opcode;
96 [ - + ]: 25883746 : while (pos != endpos) {
97 : 25883746 : opcode = DecodeType(pos, endpos);
98 [ + + ]: 25883746 : if (opcode == Instruction::RETURN) {
99 : 8302670 : default_asn = DecodeASN(pos, endpos);
100 [ - + ]: 8302670 : if (default_asn == INVALID) break; // ASN straddles EOF
101 : : return default_asn;
102 [ + + ]: 17581076 : } else if (opcode == Instruction::JUMP) {
103 : 5797732 : jump = DecodeJump(pos, endpos);
104 [ + - ]: 5797732 : if (jump == INVALID) break; // Jump offset straddles EOF
105 [ + - ]: 5797732 : if (bits == 0) break; // No input bits left
106 [ - + + - ]: 11595464 : if (int64_t{jump} >= int64_t{endpos - pos}) break; // Jumping past EOF
107 [ + + ]: 5797732 : if (ip[ip.size() - bits]) {
108 : 2049238 : pos += jump;
109 : : }
110 : 5797732 : bits--;
111 [ + + ]: 11783344 : } else if (opcode == Instruction::MATCH) {
112 : 7220799 : match = DecodeMatch(pos, endpos);
113 [ + - ]: 7220799 : if (match == INVALID) break; // Match bits straddle EOF
114 [ + - ]: 7220799 : matchlen = std::bit_width(match) - 1;
115 [ + - ]: 7220799 : if (bits < matchlen) break; // Not enough input bits
116 [ + + ]: 14607402 : for (uint32_t bit = 0; bit < matchlen; bit++) {
117 [ + + ]: 12127023 : if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
118 : : return default_asn;
119 : : }
120 : 7386603 : bits--;
121 : : }
122 [ + - ]: 4562545 : } else if (opcode == Instruction::DEFAULT) {
123 : 4562545 : default_asn = DecodeASN(pos, endpos);
124 [ + - ]: 4562545 : if (default_asn == INVALID) break; // ASN straddles EOF
125 : : } else {
126 : : break; // Instruction straddles EOF
127 : : }
128 : : }
129 : 0 : assert(false); // Reached EOF without RETURN, or aborted (see any of the breaks above) - should have been caught by SanityCheckASMap below
130 : : return 0; // 0 is not a valid ASN
131 : : }
132 : :
133 : 137585 : bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
134 : : {
135 [ + - ]: 137585 : const std::vector<bool>::const_iterator begin = asmap.begin(), endpos = asmap.end();
136 : 137585 : std::vector<bool>::const_iterator pos = begin;
137 : 137585 : std::vector<std::pair<uint32_t, int>> jumps; // All future positions we may jump to (bit offset in asmap -> bits to consume left)
138 [ + - ]: 137585 : jumps.reserve(bits);
139 : : Instruction prevopcode = Instruction::JUMP;
140 : : bool had_incomplete_match = false;
141 [ - + ]: 7655624 : while (pos != endpos) {
142 [ + + ]: 7646903 : uint32_t offset = pos - begin;
143 [ + + + + ]: 7646903 : if (!jumps.empty() && offset >= jumps.back().first) return false; // There was a jump into the middle of the previous instruction
144 : 7646848 : Instruction opcode = DecodeType(pos, endpos);
145 [ + + ]: 7646848 : if (opcode == Instruction::RETURN) {
146 [ + + ]: 2250432 : if (prevopcode == Instruction::DEFAULT) return false; // There should not be any RETURN immediately after a DEFAULT (could be combined into just RETURN)
147 : 2249964 : uint32_t asn = DecodeASN(pos, endpos);
148 [ + + ]: 2249964 : if (asn == INVALID) return false; // ASN straddles EOF
149 [ + + ]: 2246862 : if (jumps.empty()) {
150 : : // Nothing to execute anymore
151 [ - + + + ]: 9416 : if (endpos - pos > 7) return false; // Excessive padding
152 : 137585 : while (pos != endpos) {
153 [ + + ]: 8843 : if (*pos) return false; // Nonzero padding bit
154 [ + + - + ]: 12144 : ++pos;
155 : : }
156 : : return true; // Sanely reached EOF
157 : : } else {
158 : : // Continue by pretending we jumped to the next instruction
159 [ - + ]: 2242154 : offset = pos - begin;
160 [ + + ]: 2242154 : if (offset != jumps.back().first) return false; // Unreachable code
161 : 2241984 : bits = jumps.back().second; // Restore the number of bits we would have had left after this jump
162 : 2241984 : jumps.pop_back();
163 : 2241984 : prevopcode = Instruction::JUMP;
164 : : }
165 [ + + ]: 5396416 : } else if (opcode == Instruction::JUMP) {
166 : 2291665 : uint32_t jump = DecodeJump(pos, endpos);
167 [ + + ]: 2291665 : if (jump == INVALID) return false; // Jump offset straddles EOF
168 [ - + + + ]: 4559896 : if (int64_t{jump} > int64_t{endpos - pos}) return false; // Jump out of range
169 [ + + ]: 2242719 : if (bits == 0) return false; // Consuming bits past the end of the input
170 : 2242700 : --bits;
171 [ + + ]: 2242700 : uint32_t jump_offset = pos - begin + jump;
172 [ + + + + ]: 2242700 : if (!jumps.empty() && jump_offset >= jumps.back().first) return false; // Intersecting jumps
173 [ + - ]: 2242553 : jumps.emplace_back(jump_offset, bits);
174 : : prevopcode = Instruction::JUMP;
175 [ + + ]: 3104751 : } else if (opcode == Instruction::MATCH) {
176 : 1139884 : uint32_t match = DecodeMatch(pos, endpos);
177 [ + + ]: 1139884 : if (match == INVALID) return false; // Match bits straddle EOF
178 [ + - ]: 1135000 : int matchlen = std::bit_width(match) - 1;
179 [ + + ]: 1135000 : if (prevopcode != Instruction::MATCH) had_incomplete_match = false;
180 [ + + ]: 1135000 : if (matchlen < 8 && had_incomplete_match) return false; // Within a sequence of matches only at most one should be incomplete
181 : 1134898 : had_incomplete_match = (matchlen < 8);
182 [ + + ]: 1134898 : if (bits < matchlen) return false; // Consuming bits past the end of the input
183 : 1134875 : bits -= matchlen;
184 : 1134875 : prevopcode = Instruction::MATCH;
185 [ + + ]: 1964867 : } else if (opcode == Instruction::DEFAULT) {
186 [ + + ]: 1955488 : if (prevopcode == Instruction::DEFAULT) return false; // There should not be two successive DEFAULTs (they could be combined into one)
187 : 1955255 : uint32_t asn = DecodeASN(pos, endpos);
188 [ + + ]: 1955255 : if (asn == INVALID) return false; // ASN straddles EOF
189 : : prevopcode = Instruction::DEFAULT;
190 : : } else {
191 : : return false; // Instruction straddles EOF
192 : : }
193 : : }
194 : : return false; // Reached EOF without RETURN instruction
195 : 137585 : }
196 : :
197 : 0 : std::vector<bool> DecodeAsmap(fs::path path)
198 : : {
199 : 0 : std::vector<bool> bits;
200 [ # # ]: 0 : FILE *filestr = fsbridge::fopen(path, "rb");
201 [ # # ]: 0 : AutoFile file{filestr};
202 [ # # ]: 0 : if (file.IsNull()) {
203 [ # # ]: 0 : LogWarning("Failed to open asmap file from disk");
204 : 0 : return bits;
205 : : }
206 [ # # ]: 0 : int64_t length{file.size()};
207 [ # # # # ]: 0 : LogInfo("Opened asmap file %s (%d bytes) from disk", fs::quoted(fs::PathToString(path)), length);
208 : 0 : uint8_t cur_byte;
209 [ # # ]: 0 : for (int i = 0; i < length; ++i) {
210 [ # # ]: 0 : file >> cur_byte;
211 [ # # ]: 0 : for (int bit = 0; bit < 8; ++bit) {
212 [ # # ]: 0 : bits.push_back((cur_byte >> bit) & 1);
213 : : }
214 : : }
215 [ # # # # ]: 0 : if (!SanityCheckASMap(bits, 128)) {
216 [ # # # # ]: 0 : LogWarning("Sanity check of asmap file %s failed", fs::quoted(fs::PathToString(path)));
217 : 0 : return {};
218 : : }
219 : 0 : return bits;
220 : 0 : }
|