// Copyright 2017 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . // Provides support for dealing with EVM assembly instructions (e.g., disassembling them). package asm import ( "encoding/hex" "fmt" "github.com/ethereum/go-ethereum/core/vm" ) // Apply function to each disassembled EVM instruction. func ForEachDisassembledInstruction(script []byte, fun func(uint64, vm.OpCode, []byte)) error { for pc := uint64(0); pc < uint64(len(script)); pc++ { op := vm.OpCode(script[pc]) if op.IsPush() { a := uint64(op) - uint64(vm.PUSH1) + 1 u := pc + 1 + a if uint64(len(script)) <= pc || uint64(len(script)) < u { return fmt.Errorf("incomplete push instruction at %v", pc) } fun(pc, op, script[pc+1:u]) pc += a } else { fun(pc, op, make([]byte, 0)) } } return nil } // Pretty-print all disassembled EVM instructions to stdout. func PrintDisassembled(code string) error { script, err := hex.DecodeString(code) if err != nil { return err } return ForEachDisassembledInstruction(script, func(pc uint64, op vm.OpCode, args []byte) { if args != nil && 0 < len(args) { fmt.Printf("%06v: %v 0x%x\n", pc, op, args) } else { fmt.Printf("%06v: %v\n", pc, op) } }) } // Return all disassembled EVM instructions in human-readable format. func Disassemble(script []byte) ([]string, error) { instrs := make([]string, 0) err := ForEachDisassembledInstruction(script, func(pc uint64, op vm.OpCode, args []byte) { if args != nil && 0 < len(args) { instrs = append(instrs, fmt.Sprintf("%06v: %v 0x%x\n", pc, op, args)) } else { instrs = append(instrs, fmt.Sprintf("%06v: %v\n", pc, op)) } }) if err != nil { return nil, err } return instrs, nil }