|
| 1 | +# Nimbus |
| 2 | +# Copyright (c) 2023 Status Research & Development GmbH |
| 3 | +# Licensed under either of |
| 4 | +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0) |
| 6 | +# * MIT license ([LICENSE-MIT](LICENSE-MIT) or |
| 7 | +# http://opensource.org/licenses/MIT) |
| 8 | +# at your option. This file may not be copied, modified, or |
| 9 | +# distributed except according to those terms. |
| 10 | + |
| 11 | +import |
| 12 | + interpreter/op_codes |
| 13 | + |
| 14 | +const |
| 15 | + set2BitsMask = uint16(0b11) |
| 16 | + set3BitsMask = uint16(0b111) |
| 17 | + set4BitsMask = uint16(0b1111) |
| 18 | + set5BitsMask = uint16(0b1_1111) |
| 19 | + set6BitsMask = uint16(0b11_1111) |
| 20 | + set7BitsMask = uint16(0b111_1111) |
| 21 | + |
| 22 | +# bitvec is a bit vector which maps bytes in a program. |
| 23 | +# An unset bit means the byte is an opcode, a set bit means |
| 24 | +# it's data (i.e. argument of PUSHxx). |
| 25 | +type |
| 26 | + Bitvec* = seq[byte] |
| 27 | + |
| 28 | +proc set1(bits: var Bitvec, pos: int) = |
| 29 | + let x = bits[pos div 8] |
| 30 | + bits[pos div 8] = x or byte(1 shl (pos mod 8)) |
| 31 | + |
| 32 | +proc setN(bits: var Bitvec, flag: uint16, pos: int) = |
| 33 | + let z = pos div 8 |
| 34 | + let a = flag shl (pos mod 8) |
| 35 | + let x = bits[z] |
| 36 | + bits[z] = x or byte(a) |
| 37 | + let b = byte(a shr 8) |
| 38 | + if b != 0: |
| 39 | + bits[z+1] = b |
| 40 | + |
| 41 | +proc set8(bits: var Bitvec, pos: int) = |
| 42 | + let z = pos div 8 |
| 43 | + let a = byte(0xFF shl (pos mod 8)) |
| 44 | + bits[z] = bits[z] or a |
| 45 | + bits[z+1] = not a |
| 46 | + |
| 47 | +proc set16(bits: var Bitvec, pos: int) = |
| 48 | + let z = pos div 8 |
| 49 | + let a = byte(0xFF shl (pos mod 8)) |
| 50 | + bits[z] = bits[z] or a |
| 51 | + bits[z+1] = 0xFF |
| 52 | + bits[z+2] = not a |
| 53 | + |
| 54 | +# codeSegment checks if the position is in a code segment. |
| 55 | +proc codeSegment*(bits: Bitvec, pos: int): bool = |
| 56 | + ((bits[pos div 8] shr (pos mod 8)) and 1) == 0 |
| 57 | + |
| 58 | +# codeBitmapInternal is the internal implementation of codeBitmap. |
| 59 | +# It exists for the purpose of being able to run benchmark tests |
| 60 | +# without dynamic allocations affecting the results. |
| 61 | +proc codeBitmapInternal(bits: var Bitvec; code: openArray[byte]) = |
| 62 | + var pc = 0 |
| 63 | + while pc < code.len: |
| 64 | + let op = Op(code[pc]) |
| 65 | + inc pc |
| 66 | + |
| 67 | + if op < PUSH1: |
| 68 | + continue |
| 69 | + |
| 70 | + var numbits = op.int - PUSH1.int + 1 |
| 71 | + if numbits >= 8: |
| 72 | + while numbits >= 16: |
| 73 | + bits.set16(pc) |
| 74 | + pc += 16 |
| 75 | + numbits -= 16 |
| 76 | + |
| 77 | + while numbits >= 8: |
| 78 | + bits.set8(pc) |
| 79 | + pc += 8 |
| 80 | + numbits -= 8 |
| 81 | + |
| 82 | + case numbits |
| 83 | + of 1: bits.set1(pc) |
| 84 | + of 2: bits.setN(set2BitsMask, pc) |
| 85 | + of 3: bits.setN(set3BitsMask, pc) |
| 86 | + of 4: bits.setN(set4BitsMask, pc) |
| 87 | + of 5: bits.setN(set5BitsMask, pc) |
| 88 | + of 6: bits.setN(set6BitsMask, pc) |
| 89 | + of 7: bits.setN(set7BitsMask, pc) |
| 90 | + else: discard |
| 91 | + pc += numbits |
| 92 | + |
| 93 | +# codeBitmap collects data locations in code. |
| 94 | +proc codeBitmap*(code: openArray[byte]): Bitvec = |
| 95 | + # The bitmap is 4 bytes longer than necessary, in case the code |
| 96 | + # ends with a PUSH32, the algorithm will push zeroes onto the |
| 97 | + # bitvector outside the bounds of the actual code. |
| 98 | + let len = (code.len div 8)+1+4 |
| 99 | + result = newSeq[byte](len) |
| 100 | + result.codeBitmapInternal(code) |
| 101 | + |
| 102 | +# eofCodeBitmapInternal is the internal implementation of codeBitmap for EOF |
| 103 | +# code validation. |
| 104 | +proc eofCodeBitmapInternal(bits: var Bitvec; code: openArray[byte]) = |
| 105 | + var pc = 0 |
| 106 | + while pc < code.len: |
| 107 | + let op = Op(code[pc]) |
| 108 | + inc pc |
| 109 | + |
| 110 | + # RJUMP and RJUMPI always have 2 byte operand. |
| 111 | + if op == RJUMP or op == RJUMPI: |
| 112 | + bits.setN(set2BitsMask, pc) |
| 113 | + pc += 2 |
| 114 | + continue |
| 115 | + |
| 116 | + var numbits = 0 |
| 117 | + if op >= PUSH1 and op <= PUSH32: |
| 118 | + numbits = op.int - PUSH1.int + 1 |
| 119 | + elif op == RJUMPV: |
| 120 | + # RJUMPV is unique as it has a variable sized operand. |
| 121 | + # The total size is determined by the count byte which |
| 122 | + # immediate proceeds RJUMPV. Truncation will be caught |
| 123 | + # in other validation steps -- for now, just return a |
| 124 | + # valid bitmap for as much of the code as is |
| 125 | + # available. |
| 126 | + if pc >= code.len: |
| 127 | + # Count missing, no more bits to mark. |
| 128 | + return |
| 129 | + numbits = code[pc].int*2 + 1 |
| 130 | + if pc+numbits > code.len: |
| 131 | + # Jump table is truncated, mark as many bits |
| 132 | + # as possible. |
| 133 | + numbits = code.len - pc |
| 134 | + else: |
| 135 | + # If not PUSH (the int8(op) > int(PUSH32) is always false). |
| 136 | + continue |
| 137 | + |
| 138 | + if numbits >= 8: |
| 139 | + while numbits >= 16: |
| 140 | + bits.set16(pc) |
| 141 | + pc += 16 |
| 142 | + numbits -= 16 |
| 143 | + |
| 144 | + while numbits >= 8: |
| 145 | + bits.set8(pc) |
| 146 | + pc += 8 |
| 147 | + numbits -= 8 |
| 148 | + |
| 149 | + case numbits |
| 150 | + of 1: bits.set1(pc) |
| 151 | + of 2: bits.setN(set2BitsMask, pc) |
| 152 | + of 3: bits.setN(set3BitsMask, pc) |
| 153 | + of 4: bits.setN(set4BitsMask, pc) |
| 154 | + of 5: bits.setN(set5BitsMask, pc) |
| 155 | + of 6: bits.setN(set6BitsMask, pc) |
| 156 | + of 7: bits.setN(set7BitsMask, pc) |
| 157 | + else: discard |
| 158 | + pc += numbits |
| 159 | + |
| 160 | +# eofCodeBitmap collects data locations in code. |
| 161 | +proc eofCodeBitmap*(code: openArray[byte]): Bitvec = |
| 162 | + # The bitmap is 4 bytes longer than necessary, in case the code |
| 163 | + # ends with a PUSH32, the algorithm will push zeroes onto the |
| 164 | + # bitvector outside the bounds of the actual code. |
| 165 | + let len = (code.len div 8)+1+4 |
| 166 | + result = newSeq[byte](len) |
| 167 | + result.eofCodeBitmapInternal(code) |
0 commit comments