we validate each subroutine individually, as if at top level - PC is the offset in the code to start validating at - RP is the top PC on return stack that RETURNSUB returns to - SP = FP at the top level, so the stack size is also the frame size
| 61 | // - RP is the top PC on return stack that RETURNSUB returns to |
| 62 | // - SP = FP at the top level, so the stack size is also the frame size |
| 63 | void VM::validateSubroutine(uint64_t _PC, uint64_t* _RP, u256* _SP) |
| 64 | { |
| 65 | // set current interpreter state |
| 66 | m_PC = _PC, m_RP = _RP, m_SP = _SP; |
| 67 | |
| 68 | INIT_CASES |
| 69 | DO_CASES |
| 70 | { |
| 71 | CASE(JUMPDEST) |
| 72 | { |
| 73 | // if frame size is set then we have been here before |
| 74 | ptrdiff_t frameSize = m_frameSize[m_PC]; |
| 75 | if (0 <= frameSize) |
| 76 | { |
| 77 | // check for constant frame size |
| 78 | if (stackSize() != frameSize) |
| 79 | throwBadStack(stackSize(), frameSize, 0); |
| 80 | |
| 81 | // return to break cycle in control flow graph |
| 82 | return; |
| 83 | } |
| 84 | // set frame size to check later |
| 85 | m_frameSize[m_PC] = stackSize(); |
| 86 | ++m_PC; |
| 87 | } |
| 88 | NEXT |
| 89 | |
| 90 | CASE(JUMPTO) |
| 91 | { |
| 92 | // extract jump destination from bytecode |
| 93 | m_PC = decodeJumpDest(m_code, m_PC); |
| 94 | } |
| 95 | NEXT |
| 96 | |
| 97 | CASE(JUMPIF) |
| 98 | { |
| 99 | // recurse to validate code to jump to, saving and restoring |
| 100 | // interpreter state around call |
| 101 | _PC = m_PC, _RP = m_RP, _SP = m_SP; |
| 102 | validateSubroutine(decodeJumpvDest(m_code, m_PC, m_SP), _RP, _SP); |
| 103 | m_PC = _PC, m_RP = _RP, m_SP = _SP; |
| 104 | ++m_PC; |
| 105 | } |
| 106 | NEXT |
| 107 | |
| 108 | CASE(JUMPV) |
| 109 | { |
| 110 | // for every jump destination in jump vector |
| 111 | for (size_t dest = 0, nDests = m_code[m_PC+1]; dest < nDests; ++dest) |
| 112 | { |
| 113 | // recurse to validate code to jump to, saving and |
| 114 | // restoring interpreter state around call |
| 115 | _PC = m_PC, _RP = m_RP, _SP = m_SP; |
| 116 | validateSubroutine(decodeJumpDest(m_code, m_PC), _RP, _SP); |
| 117 | m_PC = _PC, m_RP = _RP, m_SP = _SP; |
| 118 | } |
| 119 | } |
| 120 | RETURN |