Skip to main content

Try it Live

Run Opcode examples in the interactive playground

Opcode.jumpDests()

Find all valid JUMPDEST positions in bytecode.
import { jumpDests } from 'tevm/Opcode'

const dests = jumpDests(bytecode)
console.log(Array(dests))  // [3]

Parameters

  • bytecode: Uint8Array - Contract bytecode

Returns

Set<number> - Set of valid JUMPDEST offsets

Behavior

  • Correctly skips PUSH data: JUMPDEST inside PUSH immediate bytes is NOT valid
  • Only JUMPDEST opcodes: Only returns positions where opcode is 0x5B
  • Empty if none found: Returns empty Set if no JUMPDESTs

Use Cases

Build Jump Table

function buildJumpTable(bytecode: Uint8Array): Map<number, string> {
  const dests = Opcode.jumpDests(bytecode)
  const table = new Map<number, string>()

  for (const offset of dests) {
    table.set(offset, `JUMPDEST_${offset.toString(16)}`)
  }

  return table
}

Validate Jump Target

function isValidJumpTarget(bytecode: Uint8Array, target: number): boolean {
  const validDests = Opcode.jumpDests(bytecode)
  return validDests.has(target)
}

Count Jump Destinations

function countJumpDests(bytecode: Uint8Array): number {
  return Opcode.jumpDests(bytecode).size
}