Ethereum Virtual Machine (EVM) Runtime Operations

Introduction

Runtime related operations used while executing EVM code.

Module Contents

Functions

get_valid_jump_destinations

Analyze the evm code to obtain the set of valid jump destinations.

Module Details

get_valid_jump_destinations

get_valid_jump_destinations(code: bytes)Set[ethereum.base_types.Uint]

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 – The set of valid jump destinations in the code.

Return type

Set[Uint]

def get_valid_jump_destinations(code: bytes) -> Set[Uint]:
    valid_jump_destinations = set()
    pc = Uint(0)

    while pc < len(code):
        try:
            current_opcode = Ops(code[pc])
        except ValueError:
            # Skip invalid opcodes, as they don't affect the jumpdest
            # analysis. Nevertheless, such invalid opcodes would be caught
            # and raised when the interpreter runs.
            pc += 1
            continue

        if current_opcode == Ops.JUMPDEST:
            valid_jump_destinations.add(pc)
        elif Ops.PUSH1.value <= current_opcode.value <= Ops.PUSH32.value:
            # If PUSH-N opcodes are encountered, skip the current opcode along
            # with the trailing data segment corresponding to the PUSH-N
            # opcodes.
            push_data_size = current_opcode.value - Ops.PUSH1.value + 1
            pc += push_data_size

        pc += 1

    return valid_jump_destinations