LCOV - code coverage report
Current view: top level - src/util - asmap.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 148 166 89.2 %
Date: 2022-04-21 14:51:19 Functions: 8 8 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 66 102 64.7 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2019-2021 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, const std::vector<bool>::const_iterator& endpos, uint8_t minval, const std::vector<uint8_t> &bit_sizes)
#      22                 :     232504 : {
#      23                 :     232504 :     uint32_t val = minval;
#      24                 :     232504 :     bool bit;
#      25                 :     232504 :     for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
#      26         [ +  - ]:    1158942 :         bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
#      27         [ +  + ]:    1158942 :         if (bit_sizes_it + 1 != bit_sizes.end()) {
#      28         [ -  + ]:     958606 :             if (bitpos == endpos) break;
#      29                 :     958606 :             bit = *bitpos;
#      30                 :     958606 :             bitpos++;
#      31                 :     958606 :         } else {
#      32                 :     200336 :             bit = 0;
#      33                 :     200336 :         }
#      34         [ +  + ]:    1158942 :         if (bit) {
#      35                 :     926438 :             val += (1 << *bit_sizes_it);
#      36                 :     926438 :         } else {
#      37         [ +  + ]:    1302836 :             for (int b = 0; b < *bit_sizes_it; b++) {
#      38         [ -  + ]:    1070332 :                 if (bitpos == endpos) return INVALID; // Reached EOF in mantissa
#      39                 :    1070332 :                 bit = *bitpos;
#      40                 :    1070332 :                 bitpos++;
#      41                 :    1070332 :                 val += bit << (*bit_sizes_it - 1 - b);
#      42                 :    1070332 :             }
#      43                 :     232504 :             return val;
#      44                 :     232504 :         }
#      45                 :    1158942 :     }
#      46                 :          0 :     return INVALID; // Reached EOF in exponent
#      47                 :     232504 : }
#      48                 :            : 
#      49                 :            : enum class Instruction : uint32_t
#      50                 :            : {
#      51                 :            :     RETURN = 0,
#      52                 :            :     JUMP = 1,
#      53                 :            :     MATCH = 2,
#      54                 :            :     DEFAULT = 3,
#      55                 :            : };
#      56                 :            : 
#      57                 :            : const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
#      58                 :            : Instruction DecodeType(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
#      59                 :     116252 : {
#      60                 :     116252 :     return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES));
#      61                 :     116252 : }
#      62                 :            : 
#      63                 :            : const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
#      64                 :            : uint32_t DecodeASN(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
#      65                 :       7330 : {
#      66                 :       7330 :     return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES);
#      67                 :       7330 : }
#      68                 :            : 
#      69                 :            : 
#      70                 :            : const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
#      71                 :            : uint32_t DecodeMatch(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
#      72                 :     100436 : {
#      73                 :     100436 :     return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES);
#      74                 :     100436 : }
#      75                 :            : 
#      76                 :            : 
#      77                 :            : 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};
#      78                 :            : uint32_t DecodeJump(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos)
#      79                 :       8486 : {
#      80                 :       8486 :     return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES);
#      81                 :       8486 : }
#      82                 :            : 
#      83                 :            : }
#      84                 :            : 
#      85                 :            : uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
#      86                 :       8232 : {
#      87                 :       8232 :     std::vector<bool>::const_iterator pos = asmap.begin();
#      88                 :       8232 :     const std::vector<bool>::const_iterator endpos = asmap.end();
#      89                 :       8232 :     uint8_t bits = ip.size();
#      90                 :       8232 :     uint32_t default_asn = 0;
#      91                 :       8232 :     uint32_t jump, match, matchlen;
#      92                 :       8232 :     Instruction opcode;
#      93         [ +  - ]:     116066 :     while (pos != endpos) {
#      94                 :     116066 :         opcode = DecodeType(pos, endpos);
#      95         [ +  + ]:     116066 :         if (opcode == Instruction::RETURN) {
#      96                 :       7276 :             default_asn = DecodeASN(pos, endpos);
#      97         [ -  + ]:       7276 :             if (default_asn == INVALID) break; // ASN straddles EOF
#      98                 :       7276 :             return default_asn;
#      99         [ +  + ]:     108790 :         } else if (opcode == Instruction::JUMP) {
#     100                 :       8438 :             jump = DecodeJump(pos, endpos);
#     101         [ -  + ]:       8438 :             if (jump == INVALID) break; // Jump offset straddles EOF
#     102         [ -  + ]:       8438 :             if (bits == 0) break; // No input bits left
#     103         [ -  + ]:       8438 :             if (int64_t{jump} >= int64_t{endpos - pos}) break; // Jumping past EOF
#     104         [ +  + ]:       8438 :             if (ip[ip.size() - bits]) {
#     105                 :       7270 :                 pos += jump;
#     106                 :       7270 :             }
#     107                 :       8438 :             bits--;
#     108         [ +  - ]:     100352 :         } else if (opcode == Instruction::MATCH) {
#     109                 :     100352 :             match = DecodeMatch(pos, endpos);
#     110         [ -  + ]:     100352 :             if (match == INVALID) break; // Match bits straddle EOF
#     111                 :     100352 :             matchlen = CountBits(match) - 1;
#     112         [ -  + ]:     100352 :             if (bits < matchlen) break; // Not enough input bits
#     113         [ +  + ]:     898922 :             for (uint32_t bit = 0; bit < matchlen; bit++) {
#     114         [ +  + ]:     799526 :                 if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
#     115                 :        956 :                     return default_asn;
#     116                 :        956 :                 }
#     117                 :     798570 :                 bits--;
#     118                 :     798570 :             }
#     119         [ #  # ]:     100352 :         } else if (opcode == Instruction::DEFAULT) {
#     120                 :          0 :             default_asn = DecodeASN(pos, endpos);
#     121         [ #  # ]:          0 :             if (default_asn == INVALID) break; // ASN straddles EOF
#     122                 :          0 :         } else {
#     123                 :          0 :             break; // Instruction straddles EOF
#     124                 :          0 :         }
#     125                 :     116066 :     }
#     126                 :          0 :     assert(false); // Reached EOF without RETURN, or aborted (see any of the breaks above) - should have been caught by SanityCheckASMap below
#     127                 :          0 :     return 0; // 0 is not a valid ASN
#     128                 :       8232 : }
#     129                 :            : 
#     130                 :            : bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
#     131                 :          7 : {
#     132                 :          7 :     const std::vector<bool>::const_iterator begin = asmap.begin(), endpos = asmap.end();
#     133                 :          7 :     std::vector<bool>::const_iterator pos = begin;
#     134                 :          7 :     std::vector<std::pair<uint32_t, int>> jumps; // All future positions we may jump to (bit offset in asmap -> bits to consume left)
#     135                 :          7 :     jumps.reserve(bits);
#     136                 :          7 :     Instruction prevopcode = Instruction::JUMP;
#     137                 :          7 :     bool had_incomplete_match = false;
#     138         [ +  + ]:        187 :     while (pos != endpos) {
#     139                 :        186 :         uint32_t offset = pos - begin;
#     140 [ +  + ][ -  + ]:        186 :         if (!jumps.empty() && offset >= jumps.back().first) return false; // There was a jump into the middle of the previous instruction
#     141                 :        186 :         Instruction opcode = DecodeType(pos, endpos);
#     142         [ +  + ]:        186 :         if (opcode == Instruction::RETURN) {
#     143         [ -  + ]:         54 :             if (prevopcode == Instruction::DEFAULT) return false; // There should not be any RETURN immediately after a DEFAULT (could be combined into just RETURN)
#     144                 :         54 :             uint32_t asn = DecodeASN(pos, endpos);
#     145         [ -  + ]:         54 :             if (asn == INVALID) return false; // ASN straddles EOF
#     146         [ +  + ]:         54 :             if (jumps.empty()) {
#     147                 :            :                 // Nothing to execute anymore
#     148         [ -  + ]:          6 :                 if (endpos - pos > 7) return false; // Excessive padding
#     149         [ +  + ]:         18 :                 while (pos != endpos) {
#     150         [ -  + ]:         12 :                     if (*pos) return false; // Nonzero padding bit
#     151                 :         12 :                     ++pos;
#     152                 :         12 :                 }
#     153                 :          6 :                 return true; // Sanely reached EOF
#     154                 :         48 :             } else {
#     155                 :            :                 // Continue by pretending we jumped to the next instruction
#     156                 :         48 :                 offset = pos - begin;
#     157         [ -  + ]:         48 :                 if (offset != jumps.back().first) return false; // Unreachable code
#     158                 :         48 :                 bits = jumps.back().second; // Restore the number of bits we would have had left after this jump
#     159                 :         48 :                 jumps.pop_back();
#     160                 :         48 :                 prevopcode = Instruction::JUMP;
#     161                 :         48 :             }
#     162         [ +  + ]:        132 :         } else if (opcode == Instruction::JUMP) {
#     163                 :         48 :             uint32_t jump = DecodeJump(pos, endpos);
#     164         [ -  + ]:         48 :             if (jump == INVALID) return false; // Jump offset straddles EOF
#     165         [ -  + ]:         48 :             if (int64_t{jump} > int64_t{endpos - pos}) return false; // Jump out of range
#     166         [ -  + ]:         48 :             if (bits == 0) return false; // Consuming bits past the end of the input
#     167                 :         48 :             --bits;
#     168                 :         48 :             uint32_t jump_offset = pos - begin + jump;
#     169 [ +  + ][ -  + ]:         48 :             if (!jumps.empty() && jump_offset >= jumps.back().first) return false; // Intersecting jumps
#     170                 :         48 :             jumps.emplace_back(jump_offset, bits);
#     171                 :         48 :             prevopcode = Instruction::JUMP;
#     172         [ +  - ]:         84 :         } else if (opcode == Instruction::MATCH) {
#     173                 :         84 :             uint32_t match = DecodeMatch(pos, endpos);
#     174         [ -  + ]:         84 :             if (match == INVALID) return false; // Match bits straddle EOF
#     175                 :         84 :             int matchlen = CountBits(match) - 1;
#     176         [ +  + ]:         84 :             if (prevopcode != Instruction::MATCH) had_incomplete_match = false;
#     177 [ +  + ][ -  + ]:         84 :             if (matchlen < 8 && had_incomplete_match) return false; // Within a sequence of matches only at most one should be incomplete
#     178                 :         84 :             had_incomplete_match = (matchlen < 8);
#     179         [ -  + ]:         84 :             if (bits < matchlen) return false; // Consuming bits past the end of the input
#     180                 :         84 :             bits -= matchlen;
#     181                 :         84 :             prevopcode = Instruction::MATCH;
#     182         [ #  # ]:         84 :         } else if (opcode == Instruction::DEFAULT) {
#     183         [ #  # ]:          0 :             if (prevopcode == Instruction::DEFAULT) return false; // There should not be two successive DEFAULTs (they could be combined into one)
#     184                 :          0 :             uint32_t asn = DecodeASN(pos, endpos);
#     185         [ #  # ]:          0 :             if (asn == INVALID) return false; // ASN straddles EOF
#     186                 :          0 :             prevopcode = Instruction::DEFAULT;
#     187                 :          0 :         } else {
#     188                 :          0 :             return false; // Instruction straddles EOF
#     189                 :          0 :         }
#     190                 :        186 :     }
#     191                 :          1 :     return false; // Reached EOF without RETURN instruction
#     192                 :          7 : }
#     193                 :            : 
#     194                 :            : std::vector<bool> DecodeAsmap(fs::path path)
#     195                 :          7 : {
#     196                 :          7 :     std::vector<bool> bits;
#     197                 :          7 :     FILE *filestr = fsbridge::fopen(path, "rb");
#     198                 :          7 :     CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
#     199         [ -  + ]:          7 :     if (file.IsNull()) {
#     200                 :          0 :         LogPrintf("Failed to open asmap file from disk\n");
#     201                 :          0 :         return bits;
#     202                 :          0 :     }
#     203                 :          7 :     fseek(filestr, 0, SEEK_END);
#     204                 :          7 :     int length = ftell(filestr);
#     205                 :          7 :     LogPrintf("Opened asmap file %s (%d bytes) from disk\n", fs::quoted(fs::PathToString(path)), length);
#     206                 :          7 :     fseek(filestr, 0, SEEK_SET);
#     207                 :          7 :     uint8_t cur_byte;
#     208         [ +  + ]:        361 :     for (int i = 0; i < length; ++i) {
#     209                 :        354 :         file >> cur_byte;
#     210         [ +  + ]:       3186 :         for (int bit = 0; bit < 8; ++bit) {
#     211                 :       2832 :             bits.push_back((cur_byte >> bit) & 1);
#     212                 :       2832 :         }
#     213                 :        354 :     }
#     214         [ +  + ]:          7 :     if (!SanityCheckASMap(bits, 128)) {
#     215                 :          1 :         LogPrintf("Sanity check of asmap file %s failed\n", fs::quoted(fs::PathToString(path)));
#     216                 :          1 :         return {};
#     217                 :          1 :     }
#     218                 :          6 :     return bits;
#     219                 :          7 : }
#     220                 :            : 

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a