ethereum.spurious_dragon.vm.runtimeethereum.byzantium.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]:
22
    """
23
    Analyze the evm code to obtain the set of valid jump destinations.
24
25
    Valid jump destinations are defined as follows:
26
        * The jump destination is less than the length of the code.
27
        * The jump destination should have the `JUMPDEST` opcode (0x5B).
28
        * The jump destination shouldn't be part of the data corresponding to
29
          `PUSH-N` opcodes.
30
31
    Note - Jump destinations are 0-indexed.
32
33
    Parameters
34
    ----------
35
    code :
36
        The EVM code which is to be executed.
37
38
    Returns
39
    -------
40
    valid_jump_destinations: `Set[Uint]`
41
        The set of valid jump destinations in the code.
42
    """
43
    valid_jump_destinations = set()
44
    pc = Uint(0)
45
46
    while pc < ulen(code):
47
        try:
48
            current_opcode = Ops(code[pc])
49
        except ValueError:
50
            # Skip invalid opcodes, as they don't affect the jumpdest
51
            # analysis. Nevertheless, such invalid opcodes would be caught
52
            # and raised when the interpreter runs.
53
            pc += Uint(1)
54
            continue
55
56
        if current_opcode == Ops.JUMPDEST:
57
            valid_jump_destinations.add(pc)
58
        elif Ops.PUSH1.value <= current_opcode.value <= Ops.PUSH32.value:
59
            # If PUSH-N opcodes are encountered, skip the current opcode along
60
            # with the trailing data segment corresponding to the PUSH-N
61
            # opcodes.
62
            push_data_size = current_opcode.value - Ops.PUSH1.value + 1
63
            pc += Uint(push_data_size)
64
65
        pc += Uint(1)
66
67
    return valid_jump_destinations