ethereum.forks.dao_fork.vm.runtimeethereum.forks.tangerine_whistle.vm.runtime
Ethereum Virtual Machine (EVM) Runtime Operations.
.. contents:: Table of Contents :backlinks: none :local:
Introduction
Runtime related operations used while executing EVM code.
get_valid_jump_destinations ¶
Analyze the EVM code to obtain the set of valid jump destinations.
Valid jump destinations are defined as follows:
* The jump destination is less than the length of the code.
* The jump destination should have the JUMPDEST opcode (0x5B).
* The jump destination shouldn't be part of the data corresponding to
PUSH-N opcodes.
Note - Jump destinations are 0-indexed.
Parameters
code : The EVM code which is to be executed.
Returns
valid_jump_destinations: Set[Uint]
The set of valid jump destinations in the code.
def get_valid_jump_destinations(code: Bytes) -> Set[Uint]:
| 23 | <snip> |
|---|---|
| 45 | valid_jump_destinations = set() |
| 46 | pc = Uint(0) |
| 47 | |
| 48 | while pc < ulen(code): |
| 49 | try: |
| 50 | current_opcode = Ops(code[pc]) |
| 51 | except ValueError: |
| 52 | # Skip invalid opcodes, as they don't affect the jumpdest |
| 53 | # analysis. Nevertheless, such invalid opcodes would be caught |
| 54 | # and raised when the interpreter runs. |
| 55 | pc += Uint(1) |
| 56 | continue |
| 57 | |
| 58 | if current_opcode == Ops.JUMPDEST: |
| 59 | valid_jump_destinations.add(pc) |
| 60 | elif Ops.PUSH1.value <= current_opcode.value <= Ops.PUSH32.value: |
| 61 | # If PUSH-N opcodes are encountered, skip the current opcode along |
| 62 | # with the trailing data segment corresponding to the PUSH-N |
| 63 | # opcodes. |
| 64 | push_data_size = current_opcode.value - Ops.PUSH1.value + 1 |
| 65 | pc += Uint(push_data_size) |
| 66 | |
| 67 | pc += Uint(1) |
| 68 | |
| 69 | return valid_jump_destinations |