2018-03-30 16:15:06 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
2019-10-03 13:54:14 +00:00
|
|
|
"encoding/binary"
|
2018-03-30 16:15:06 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/big"
|
2018-04-02 15:04:42 +00:00
|
|
|
"os"
|
2019-08-20 16:46:52 +00:00
|
|
|
"reflect"
|
2019-09-09 08:23:27 +00:00
|
|
|
"text/tabwriter"
|
2019-10-03 13:56:48 +00:00
|
|
|
"unicode/utf8"
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-08-23 15:50:45 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
2019-09-23 16:54:06 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2018-03-30 16:15:06 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2019-12-03 14:05:06 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
2019-10-22 10:44:14 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-03-30 16:15:06 +00:00
|
|
|
)
|
|
|
|
|
2019-10-22 10:44:14 +00:00
|
|
|
type errorAtInstruct struct {
|
|
|
|
ip int
|
2019-12-03 14:05:06 +00:00
|
|
|
op opcode.Opcode
|
2019-10-22 10:44:14 +00:00
|
|
|
err interface{}
|
|
|
|
}
|
2018-04-02 15:04:42 +00:00
|
|
|
|
2019-10-22 10:44:14 +00:00
|
|
|
func (e *errorAtInstruct) Error() string {
|
|
|
|
return fmt.Sprintf("error encountered at instruction %d (%s): %s", e.ip, e.op, e.err)
|
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
func newError(ip int, op opcode.Opcode, err interface{}) *errorAtInstruct {
|
2019-10-22 10:44:14 +00:00
|
|
|
return &errorAtInstruct{ip: ip, op: op, err: err}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StateMessage is a vm state message which could be used as additional info for example by cli.
|
|
|
|
type StateMessage string
|
2018-04-02 15:04:42 +00:00
|
|
|
|
2019-09-18 11:35:29 +00:00
|
|
|
const (
|
2019-10-03 12:58:52 +00:00
|
|
|
// MaxArraySize is the maximum array size allowed in the VM.
|
|
|
|
MaxArraySize = 1024
|
2019-10-17 14:01:03 +00:00
|
|
|
|
|
|
|
// MaxItemSize is the maximum item size allowed in the VM.
|
|
|
|
MaxItemSize = 1024 * 1024
|
|
|
|
|
2019-10-29 08:01:06 +00:00
|
|
|
// MaxInvocationStackSize is the maximum size of an invocation stack.
|
|
|
|
MaxInvocationStackSize = 1024
|
|
|
|
|
2019-11-07 09:14:36 +00:00
|
|
|
// MaxBigIntegerSizeBits is the maximum size of BigInt item in bits.
|
|
|
|
MaxBigIntegerSizeBits = 32 * 8
|
|
|
|
|
2019-10-29 10:26:34 +00:00
|
|
|
// MaxStackSize is the maximum number of items allowed to be
|
|
|
|
// on all stacks at once.
|
|
|
|
MaxStackSize = 2 * 1024
|
|
|
|
|
2019-11-07 09:50:11 +00:00
|
|
|
maxSHLArg = MaxBigIntegerSizeBits
|
|
|
|
minSHLArg = -MaxBigIntegerSizeBits
|
2019-09-18 11:35:29 +00:00
|
|
|
)
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// VM represents the virtual machine.
|
|
|
|
type VM struct {
|
|
|
|
state State
|
|
|
|
|
2018-04-10 09:45:31 +00:00
|
|
|
// registered interop hooks.
|
2019-10-01 13:37:42 +00:00
|
|
|
interop map[string]InteropFuncPrice
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-09-30 16:52:16 +00:00
|
|
|
// callback to get scripts.
|
|
|
|
getScript func(util.Uint160) []byte
|
2018-03-30 16:15:06 +00:00
|
|
|
|
|
|
|
istack *Stack // invocation stack.
|
|
|
|
estack *Stack // execution stack.
|
|
|
|
astack *Stack // alt stack.
|
|
|
|
|
2019-09-23 16:54:06 +00:00
|
|
|
// Hash to verify in CHECKSIG/CHECKMULTISIG.
|
|
|
|
checkhash []byte
|
2019-10-29 10:26:34 +00:00
|
|
|
|
|
|
|
itemCount map[StackItem]int
|
|
|
|
size int
|
2019-12-10 16:13:29 +00:00
|
|
|
|
|
|
|
// Public keys cache.
|
|
|
|
keys map[string]*keys.PublicKey
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 13:37:42 +00:00
|
|
|
// InteropFuncPrice represents an interop function with a price.
|
|
|
|
type InteropFuncPrice struct {
|
|
|
|
Func InteropFunc
|
|
|
|
Price int
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// New returns a new VM object ready to load .avm bytecode scripts.
|
2019-10-22 10:44:14 +00:00
|
|
|
func New() *VM {
|
2018-04-02 15:04:42 +00:00
|
|
|
vm := &VM{
|
2019-10-01 13:37:42 +00:00
|
|
|
interop: make(map[string]InteropFuncPrice),
|
2019-09-30 16:52:16 +00:00
|
|
|
getScript: nil,
|
|
|
|
state: haltState,
|
|
|
|
istack: NewStack("invocation"),
|
2019-10-29 10:26:34 +00:00
|
|
|
|
|
|
|
itemCount: make(map[StackItem]int),
|
2019-12-10 16:13:29 +00:00
|
|
|
keys: make(map[string]*keys.PublicKey),
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
2018-04-10 09:45:31 +00:00
|
|
|
|
2019-10-29 10:26:34 +00:00
|
|
|
vm.estack = vm.newItemStack("evaluation")
|
|
|
|
vm.astack = vm.newItemStack("alt")
|
|
|
|
|
2018-04-10 09:45:31 +00:00
|
|
|
// Register native interop hooks.
|
2019-10-01 13:37:42 +00:00
|
|
|
vm.RegisterInteropFunc("Neo.Runtime.Log", runtimeLog, 1)
|
|
|
|
vm.RegisterInteropFunc("Neo.Runtime.Notify", runtimeNotify, 1)
|
2019-11-05 14:10:52 +00:00
|
|
|
vm.RegisterInteropFunc("Neo.Runtime.Serialize", RuntimeSerialize, 1)
|
|
|
|
vm.RegisterInteropFunc("System.Runtime.Serialize", RuntimeSerialize, 1)
|
|
|
|
vm.RegisterInteropFunc("Neo.Runtime.Deserialize", RuntimeDeserialize, 1)
|
|
|
|
vm.RegisterInteropFunc("System.Runtime.Deserialize", RuntimeDeserialize, 1)
|
2018-04-10 09:45:31 +00:00
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
return vm
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:26:34 +00:00
|
|
|
func (v *VM) newItemStack(n string) *Stack {
|
|
|
|
s := NewStack(n)
|
|
|
|
s.size = &v.size
|
|
|
|
s.itemCount = v.itemCount
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-10-18 15:28:08 +00:00
|
|
|
// RegisterInteropFunc registers the given InteropFunc to the VM.
|
2019-10-01 13:37:42 +00:00
|
|
|
func (v *VM) RegisterInteropFunc(name string, f InteropFunc, price int) {
|
|
|
|
v.interop[name] = InteropFuncPrice{f, price}
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 15:28:08 +00:00
|
|
|
// RegisterInteropFuncs registers all interop functions passed in a map in
|
2019-10-01 13:38:33 +00:00
|
|
|
// the VM. Effectively it's a batched version of RegisterInteropFunc.
|
|
|
|
func (v *VM) RegisterInteropFuncs(interops map[string]InteropFuncPrice) {
|
|
|
|
// We allow reregistration here.
|
2019-12-13 14:02:28 +00:00
|
|
|
for name := range interops {
|
|
|
|
v.interop[name] = interops[name]
|
2019-10-01 13:38:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 15:28:08 +00:00
|
|
|
// Estack returns the evaluation stack so interop hooks can utilize this.
|
2018-04-10 09:45:31 +00:00
|
|
|
func (v *VM) Estack() *Stack {
|
|
|
|
return v.estack
|
|
|
|
}
|
|
|
|
|
2019-10-18 15:28:08 +00:00
|
|
|
// Astack returns the alt stack so interop hooks can utilize this.
|
2018-04-10 09:45:31 +00:00
|
|
|
func (v *VM) Astack() *Stack {
|
|
|
|
return v.astack
|
|
|
|
}
|
|
|
|
|
2019-10-18 15:28:08 +00:00
|
|
|
// Istack returns the invocation stack so interop hooks can utilize this.
|
2018-04-10 09:45:31 +00:00
|
|
|
func (v *VM) Istack() *Stack {
|
|
|
|
return v.istack
|
|
|
|
}
|
|
|
|
|
2019-12-10 16:13:29 +00:00
|
|
|
// SetPublicKeys sets internal key cache to the specified value (note
|
|
|
|
// that it doesn't copy them).
|
|
|
|
func (v *VM) SetPublicKeys(keys map[string]*keys.PublicKey) {
|
|
|
|
v.keys = keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPublicKeys returns internal key cache (note that it doesn't copy it).
|
|
|
|
func (v *VM) GetPublicKeys() map[string]*keys.PublicKey {
|
|
|
|
return v.keys
|
|
|
|
}
|
|
|
|
|
2019-10-18 15:28:08 +00:00
|
|
|
// LoadArgs loads in the arguments used in the Mian entry point.
|
2018-04-10 09:45:31 +00:00
|
|
|
func (v *VM) LoadArgs(method []byte, args []StackItem) {
|
|
|
|
if len(args) > 0 {
|
|
|
|
v.estack.PushVal(args)
|
|
|
|
}
|
|
|
|
if method != nil {
|
2019-02-09 15:53:58 +00:00
|
|
|
v.estack.PushVal(method)
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 15:28:08 +00:00
|
|
|
// PrintOps prints the opcodes of the current loaded program to stdout.
|
2018-04-02 15:04:42 +00:00
|
|
|
func (v *VM) PrintOps() {
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
|
2019-10-03 13:56:48 +00:00
|
|
|
fmt.Fprintln(w, "INDEX\tOPCODE\tPARAMETER\t")
|
|
|
|
realctx := v.Context()
|
|
|
|
ctx := realctx.Copy()
|
|
|
|
ctx.ip = 0
|
|
|
|
ctx.nextip = 0
|
|
|
|
for {
|
|
|
|
cursor := ""
|
|
|
|
instr, parameter, err := ctx.Next()
|
|
|
|
if ctx.ip == realctx.ip {
|
2018-04-02 15:04:42 +00:00
|
|
|
cursor = "<<"
|
|
|
|
}
|
2019-10-03 13:56:48 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "%d\t%s\tERROR: %s\t%s\n", ctx.ip, instr, err, cursor)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
var desc = ""
|
|
|
|
if parameter != nil {
|
|
|
|
switch instr {
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.JMP, opcode.JMPIF, opcode.JMPIFNOT, opcode.CALL:
|
2019-10-03 13:56:48 +00:00
|
|
|
offset := int16(binary.LittleEndian.Uint16(parameter))
|
|
|
|
desc = fmt.Sprintf("%d (%d/%x)", ctx.ip+int(offset), offset, parameter)
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.SYSCALL:
|
2019-10-03 13:56:48 +00:00
|
|
|
desc = fmt.Sprintf("%q", parameter)
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.APPCALL, opcode.TAILCALL:
|
2019-10-03 13:56:48 +00:00
|
|
|
desc = fmt.Sprintf("%x", parameter)
|
|
|
|
default:
|
|
|
|
if utf8.Valid(parameter) {
|
|
|
|
desc = fmt.Sprintf("%x (%q)", parameter, parameter)
|
|
|
|
} else {
|
|
|
|
desc = fmt.Sprintf("%x", parameter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 15:04:42 +00:00
|
|
|
|
2019-10-03 13:56:48 +00:00
|
|
|
fmt.Fprintf(w, "%d\t%s\t%s\t%s\n", ctx.ip, instr, desc, cursor)
|
|
|
|
if ctx.nextip >= len(ctx.prog) {
|
|
|
|
break
|
|
|
|
}
|
2018-04-02 15:04:42 +00:00
|
|
|
}
|
|
|
|
w.Flush()
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddBreakPoint adds a breakpoint to the current context.
|
|
|
|
func (v *VM) AddBreakPoint(n int) {
|
|
|
|
ctx := v.Context()
|
|
|
|
ctx.breakPoints = append(ctx.breakPoints, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddBreakPointRel adds a breakpoint relative to the current
|
|
|
|
// instruction pointer.
|
|
|
|
func (v *VM) AddBreakPointRel(n int) {
|
|
|
|
ctx := v.Context()
|
|
|
|
v.AddBreakPoint(ctx.ip + n)
|
|
|
|
}
|
|
|
|
|
2019-10-18 15:28:08 +00:00
|
|
|
// LoadFile loads a program from the given path, ready to execute it.
|
2018-04-02 15:04:42 +00:00
|
|
|
func (v *VM) LoadFile(path string) error {
|
2018-03-30 16:15:06 +00:00
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-02 15:04:42 +00:00
|
|
|
v.Load(b)
|
2018-03-30 16:15:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// Load initializes the VM with the program given.
|
2018-04-02 15:04:42 +00:00
|
|
|
func (v *VM) Load(prog []byte) {
|
2019-10-29 09:53:09 +00:00
|
|
|
// Clear all stacks and state, it could be a reload.
|
2018-04-02 15:04:42 +00:00
|
|
|
v.istack.Clear()
|
|
|
|
v.estack.Clear()
|
|
|
|
v.astack.Clear()
|
2019-10-29 09:53:09 +00:00
|
|
|
v.state = noneState
|
2019-10-25 14:25:46 +00:00
|
|
|
v.LoadScript(prog)
|
2018-04-02 15:04:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 15:28:08 +00:00
|
|
|
// LoadScript loads a script from the internal script table. It
|
2019-02-09 15:53:58 +00:00
|
|
|
// will immediately push a new context created from this script to
|
2018-03-30 16:15:06 +00:00
|
|
|
// the invocation stack and starts executing it.
|
|
|
|
func (v *VM) LoadScript(b []byte) {
|
|
|
|
ctx := NewContext(b)
|
2019-10-25 14:25:46 +00:00
|
|
|
ctx.estack = v.estack
|
|
|
|
ctx.astack = v.astack
|
2018-03-30 16:15:06 +00:00
|
|
|
v.istack.PushVal(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context returns the current executed context. Nil if there is no context,
|
|
|
|
// which implies no program is loaded.
|
|
|
|
func (v *VM) Context() *Context {
|
|
|
|
if v.istack.Len() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-03 13:12:24 +00:00
|
|
|
return v.istack.Peek(0).Value().(*Context)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// PopResult is used to pop the first item of the evaluation stack. This allows
|
|
|
|
// us to test compiler and vm in a bi-directional way.
|
|
|
|
func (v *VM) PopResult() interface{} {
|
2019-10-03 13:02:15 +00:00
|
|
|
e := v.estack.Pop()
|
|
|
|
if e != nil {
|
|
|
|
return e.Value()
|
|
|
|
}
|
|
|
|
return nil
|
2018-04-02 15:04:42 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// Stack returns json formatted representation of the given stack.
|
|
|
|
func (v *VM) Stack(n string) string {
|
|
|
|
var s *Stack
|
|
|
|
if n == "astack" {
|
|
|
|
s = v.astack
|
|
|
|
}
|
|
|
|
if n == "istack" {
|
|
|
|
s = v.istack
|
|
|
|
}
|
|
|
|
if n == "estack" {
|
|
|
|
s = v.estack
|
|
|
|
}
|
|
|
|
return buildStackOutput(s)
|
|
|
|
}
|
|
|
|
|
2019-10-29 15:26:04 +00:00
|
|
|
// State returns string representation of the state for the VM.
|
|
|
|
func (v *VM) State() string {
|
|
|
|
return v.state.String()
|
|
|
|
}
|
|
|
|
|
2019-10-18 15:28:08 +00:00
|
|
|
// Ready returns true if the VM ready to execute the loaded program.
|
2018-03-30 16:15:06 +00:00
|
|
|
// Will return false if no program is loaded.
|
|
|
|
func (v *VM) Ready() bool {
|
|
|
|
return v.istack.Len() > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the execution of the loaded program.
|
2019-10-22 10:44:14 +00:00
|
|
|
func (v *VM) Run() error {
|
2018-03-30 16:15:06 +00:00
|
|
|
if !v.Ready() {
|
2019-10-22 10:44:14 +00:00
|
|
|
v.state = faultState
|
|
|
|
return errors.New("no program loaded")
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 12:16:29 +00:00
|
|
|
if v.state.HasFlag(faultState) {
|
|
|
|
// VM already ran something and failed, in general its state is
|
|
|
|
// undefined in this case so we can't run anything.
|
|
|
|
return errors.New("VM has failed")
|
|
|
|
}
|
|
|
|
// haltState (the default) or breakState are safe to continue.
|
2018-03-30 16:15:06 +00:00
|
|
|
v.state = noneState
|
|
|
|
for {
|
2019-10-03 13:54:14 +00:00
|
|
|
// check for breakpoint before executing the next instruction
|
|
|
|
ctx := v.Context()
|
|
|
|
if ctx != nil && ctx.atBreakPoint() {
|
|
|
|
v.state |= breakState
|
|
|
|
}
|
2019-02-19 11:47:25 +00:00
|
|
|
switch {
|
2019-10-22 12:16:29 +00:00
|
|
|
case v.state.HasFlag(faultState):
|
|
|
|
// Should be caught and reported already by the v.Step(),
|
|
|
|
// but we're checking here anyway just in case.
|
|
|
|
return errors.New("VM has failed")
|
|
|
|
case v.state.HasFlag(haltState), v.state.HasFlag(breakState):
|
|
|
|
// Normal exit from this loop.
|
|
|
|
return nil
|
2019-02-19 11:47:25 +00:00
|
|
|
case v.state == noneState:
|
2019-10-22 10:44:14 +00:00
|
|
|
if err := v.Step(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
v.state = faultState
|
|
|
|
return errors.New("unknown state")
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 1 instruction in the program.
|
2019-10-22 10:44:14 +00:00
|
|
|
func (v *VM) Step() error {
|
2018-03-30 16:15:06 +00:00
|
|
|
ctx := v.Context()
|
2019-10-03 13:54:14 +00:00
|
|
|
op, param, err := ctx.Next()
|
|
|
|
if err != nil {
|
|
|
|
v.state = faultState
|
2019-10-22 10:44:14 +00:00
|
|
|
return newError(ctx.ip, op, err)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
2019-10-22 10:44:14 +00:00
|
|
|
return v.execute(ctx, op, param)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 15:28:08 +00:00
|
|
|
// StepInto behaves the same as “step over” in case if the line does not contain a function. Otherwise
|
2019-10-14 15:37:11 +00:00
|
|
|
// the debugger will enter the called function and continue line-by-line debugging there.
|
2019-10-22 10:44:14 +00:00
|
|
|
func (v *VM) StepInto() error {
|
2019-10-14 15:37:11 +00:00
|
|
|
ctx := v.Context()
|
|
|
|
|
|
|
|
if ctx == nil {
|
|
|
|
v.state |= haltState
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.HasStopped() {
|
2019-10-22 10:44:14 +00:00
|
|
|
return nil
|
2019-10-14 15:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ctx != nil && ctx.prog != nil {
|
|
|
|
op, param, err := ctx.Next()
|
|
|
|
if err != nil {
|
|
|
|
v.state = faultState
|
2019-10-22 10:44:14 +00:00
|
|
|
return newError(ctx.ip, op, err)
|
|
|
|
}
|
|
|
|
vErr := v.execute(ctx, op, param)
|
|
|
|
if vErr != nil {
|
|
|
|
return vErr
|
2019-10-14 15:37:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cctx := v.Context()
|
|
|
|
if cctx != nil && cctx.atBreakPoint() {
|
|
|
|
v.state = breakState
|
|
|
|
}
|
2019-10-22 10:44:14 +00:00
|
|
|
return nil
|
2019-10-14 15:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StepOut takes the debugger to the line where the current function was called.
|
2019-10-22 10:44:14 +00:00
|
|
|
func (v *VM) StepOut() error {
|
|
|
|
var err error
|
2019-10-14 15:37:11 +00:00
|
|
|
if v.state == breakState {
|
|
|
|
v.state = noneState
|
|
|
|
} else {
|
|
|
|
v.state = breakState
|
|
|
|
}
|
|
|
|
|
|
|
|
expSize := v.istack.len
|
2019-11-06 09:24:02 +00:00
|
|
|
for v.state == noneState && v.istack.len >= expSize {
|
2019-10-22 10:44:14 +00:00
|
|
|
err = v.StepInto()
|
2019-10-14 15:37:11 +00:00
|
|
|
}
|
2019-10-22 10:44:14 +00:00
|
|
|
return err
|
2019-10-14 15:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StepOver takes the debugger to the line that will step over a given line.
|
|
|
|
// If the line contains a function the function will be executed and the result returned without debugging each line.
|
2019-10-22 10:44:14 +00:00
|
|
|
func (v *VM) StepOver() error {
|
|
|
|
var err error
|
2019-10-14 15:37:11 +00:00
|
|
|
if v.HasStopped() {
|
2019-10-22 10:44:14 +00:00
|
|
|
return err
|
2019-10-14 15:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if v.state == breakState {
|
|
|
|
v.state = noneState
|
|
|
|
} else {
|
|
|
|
v.state = breakState
|
|
|
|
}
|
|
|
|
|
|
|
|
expSize := v.istack.len
|
|
|
|
for {
|
2019-10-22 10:44:14 +00:00
|
|
|
err = v.StepInto()
|
2019-11-06 09:24:02 +00:00
|
|
|
if !(v.state == noneState && v.istack.len > expSize) {
|
2019-10-14 15:37:11 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 09:24:02 +00:00
|
|
|
|
|
|
|
if v.state == noneState {
|
|
|
|
v.state = breakState
|
|
|
|
}
|
|
|
|
|
2019-10-22 10:44:14 +00:00
|
|
|
return err
|
2019-10-14 15:37:11 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 15:46:47 +00:00
|
|
|
// HasFailed returns whether VM is in the failed state now. Usually used to
|
|
|
|
// check status after Run.
|
|
|
|
func (v *VM) HasFailed() bool {
|
|
|
|
return v.state.HasFlag(faultState)
|
|
|
|
}
|
|
|
|
|
2019-10-14 15:37:11 +00:00
|
|
|
// HasStopped returns whether VM is in Halt or Failed state.
|
|
|
|
func (v *VM) HasStopped() bool {
|
|
|
|
return v.state.HasFlag(haltState) || v.state.HasFlag(faultState)
|
|
|
|
}
|
|
|
|
|
2019-10-22 10:44:14 +00:00
|
|
|
// HasHalted returns whether VM is in Halt state.
|
|
|
|
func (v *VM) HasHalted() bool {
|
|
|
|
return v.state.HasFlag(haltState)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AtBreakpoint returns whether VM is at breakpoint.
|
|
|
|
func (v *VM) AtBreakpoint() bool {
|
|
|
|
return v.state.HasFlag(breakState)
|
|
|
|
}
|
|
|
|
|
2019-09-23 16:54:06 +00:00
|
|
|
// SetCheckedHash sets checked hash for CHECKSIG and CHECKMULTISIG instructions.
|
|
|
|
func (v *VM) SetCheckedHash(h []byte) {
|
|
|
|
v.checkhash = make([]byte, len(h))
|
|
|
|
copy(v.checkhash, h)
|
|
|
|
}
|
|
|
|
|
2019-09-30 16:52:16 +00:00
|
|
|
// SetScriptGetter sets the script getter for CALL instructions.
|
|
|
|
func (v *VM) SetScriptGetter(gs func(util.Uint160) []byte) {
|
|
|
|
v.getScript = gs
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// execute performs an instruction cycle in the VM. Acting on the instruction (opcode).
|
2019-12-03 14:05:06 +00:00
|
|
|
func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err error) {
|
2019-02-09 15:53:58 +00:00
|
|
|
// Instead of polluting the whole VM logic with error handling, we will recover
|
2019-10-22 10:44:14 +00:00
|
|
|
// each panic at a central point, putting the VM in a fault state and setting error.
|
2018-03-30 16:15:06 +00:00
|
|
|
defer func() {
|
2019-10-22 10:44:14 +00:00
|
|
|
if errRecover := recover(); errRecover != nil {
|
2018-03-30 16:15:06 +00:00
|
|
|
v.state = faultState
|
2019-10-22 10:44:14 +00:00
|
|
|
err = newError(ctx.ip, op, errRecover)
|
2019-10-29 10:26:34 +00:00
|
|
|
} else if v.size > MaxStackSize {
|
|
|
|
v.state = faultState
|
|
|
|
err = newError(ctx.ip, op, "stack is too big")
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
if op >= opcode.PUSHBYTES1 && op <= opcode.PUSHBYTES75 {
|
2019-10-03 13:54:14 +00:00
|
|
|
v.estack.PushVal(parameter)
|
2018-03-30 16:15:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch op {
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.PUSHM1, opcode.PUSH1, opcode.PUSH2, opcode.PUSH3,
|
|
|
|
opcode.PUSH4, opcode.PUSH5, opcode.PUSH6, opcode.PUSH7,
|
|
|
|
opcode.PUSH8, opcode.PUSH9, opcode.PUSH10, opcode.PUSH11,
|
|
|
|
opcode.PUSH12, opcode.PUSH13, opcode.PUSH14, opcode.PUSH15,
|
|
|
|
opcode.PUSH16:
|
|
|
|
val := int(op) - int(opcode.PUSH1) + 1
|
2018-03-30 16:15:06 +00:00
|
|
|
v.estack.PushVal(val)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.PUSH0:
|
2019-09-12 08:42:27 +00:00
|
|
|
v.estack.PushVal([]byte{})
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.PUSHDATA1, opcode.PUSHDATA2, opcode.PUSHDATA4:
|
2019-10-03 13:54:14 +00:00
|
|
|
v.estack.PushVal(parameter)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
|
|
|
// Stack operations.
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.TOALTSTACK:
|
2018-03-30 16:15:06 +00:00
|
|
|
v.astack.Push(v.estack.Pop())
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.FROMALTSTACK:
|
2018-03-30 16:15:06 +00:00
|
|
|
v.estack.Push(v.astack.Pop())
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.DUPFROMALTSTACK:
|
2018-03-30 16:15:06 +00:00
|
|
|
v.estack.Push(v.astack.Dup(0))
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.DUP:
|
2018-03-30 16:15:06 +00:00
|
|
|
v.estack.Push(v.estack.Dup(0))
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.SWAP:
|
2019-12-16 16:02:40 +00:00
|
|
|
err := v.estack.Swap(1, 0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.TUCK:
|
2019-09-05 13:34:35 +00:00
|
|
|
a := v.estack.Dup(0)
|
|
|
|
if a == nil {
|
|
|
|
panic("no top-level element found")
|
|
|
|
}
|
|
|
|
if v.estack.Len() < 2 {
|
|
|
|
panic("can't TUCK with a one-element stack")
|
|
|
|
}
|
|
|
|
v.estack.InsertAt(a, 2)
|
2019-10-18 15:28:08 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.CAT:
|
2019-09-06 06:12:19 +00:00
|
|
|
b := v.estack.Pop().Bytes()
|
|
|
|
a := v.estack.Pop().Bytes()
|
2019-10-17 14:01:03 +00:00
|
|
|
if l := len(a) + len(b); l > MaxItemSize {
|
|
|
|
panic(fmt.Sprintf("too big item: %d", l))
|
|
|
|
}
|
2019-09-06 06:12:19 +00:00
|
|
|
ab := append(a, b...)
|
|
|
|
v.estack.PushVal(ab)
|
2019-10-18 15:28:08 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.SUBSTR:
|
2019-09-06 06:12:19 +00:00
|
|
|
l := int(v.estack.Pop().BigInt().Int64())
|
2019-09-11 09:00:11 +00:00
|
|
|
if l < 0 {
|
|
|
|
panic("negative length")
|
|
|
|
}
|
2019-09-18 11:19:58 +00:00
|
|
|
o := int(v.estack.Pop().BigInt().Int64())
|
2019-09-11 09:00:11 +00:00
|
|
|
if o < 0 {
|
|
|
|
panic("negative index")
|
|
|
|
}
|
2019-09-18 11:19:58 +00:00
|
|
|
s := v.estack.Pop().Bytes()
|
|
|
|
if o > len(s) {
|
2019-11-06 09:15:55 +00:00
|
|
|
// panic("invalid offset")
|
|
|
|
// FIXME revert when NEO 3.0 https://github.com/nspcc-dev/neo-go/issues/477
|
|
|
|
v.estack.PushVal("")
|
|
|
|
break
|
2019-09-18 11:19:58 +00:00
|
|
|
}
|
|
|
|
last := l + o
|
|
|
|
if last > len(s) {
|
|
|
|
last = len(s)
|
2019-09-11 09:00:11 +00:00
|
|
|
}
|
2019-09-18 11:19:58 +00:00
|
|
|
v.estack.PushVal(s[o:last])
|
2019-10-18 15:28:08 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.LEFT:
|
2019-09-06 06:12:19 +00:00
|
|
|
l := int(v.estack.Pop().BigInt().Int64())
|
2019-09-11 09:03:43 +00:00
|
|
|
if l < 0 {
|
|
|
|
panic("negative length")
|
|
|
|
}
|
2019-09-06 06:12:19 +00:00
|
|
|
s := v.estack.Pop().Bytes()
|
2019-09-09 14:05:40 +00:00
|
|
|
if t := len(s); l > t {
|
|
|
|
l = t
|
|
|
|
}
|
2019-09-06 06:12:19 +00:00
|
|
|
v.estack.PushVal(s[:l])
|
2019-10-18 15:28:08 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.RIGHT:
|
2019-09-06 06:12:19 +00:00
|
|
|
l := int(v.estack.Pop().BigInt().Int64())
|
2019-09-11 09:03:43 +00:00
|
|
|
if l < 0 {
|
|
|
|
panic("negative length")
|
|
|
|
}
|
2019-09-06 06:12:19 +00:00
|
|
|
s := v.estack.Pop().Bytes()
|
|
|
|
v.estack.PushVal(s[len(s)-l:])
|
2019-10-18 15:28:08 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.XDROP:
|
2019-09-05 13:42:54 +00:00
|
|
|
n := int(v.estack.Pop().BigInt().Int64())
|
|
|
|
if n < 0 {
|
|
|
|
panic("invalid length")
|
|
|
|
}
|
|
|
|
e := v.estack.RemoveAt(n)
|
|
|
|
if e == nil {
|
|
|
|
panic("bad index")
|
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.XSWAP:
|
2018-03-30 16:15:06 +00:00
|
|
|
n := int(v.estack.Pop().BigInt().Int64())
|
2019-12-16 16:02:40 +00:00
|
|
|
err := v.estack.Swap(n, 0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.XTUCK:
|
2018-03-30 16:15:06 +00:00
|
|
|
n := int(v.estack.Pop().BigInt().Int64())
|
2019-09-12 07:39:54 +00:00
|
|
|
if n <= 0 {
|
2019-09-05 12:43:59 +00:00
|
|
|
panic("XTUCK: invalid length")
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
2019-09-05 12:43:59 +00:00
|
|
|
a := v.estack.Dup(0)
|
|
|
|
if a == nil {
|
|
|
|
panic("no top-level element found")
|
|
|
|
}
|
|
|
|
if n > v.estack.Len() {
|
|
|
|
panic("can't push to the position specified")
|
|
|
|
}
|
|
|
|
v.estack.InsertAt(a, n)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.ROT:
|
2019-12-16 16:53:21 +00:00
|
|
|
err := v.estack.Roll(2)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
2019-09-12 07:57:55 +00:00
|
|
|
}
|
2018-04-02 15:04:42 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.DEPTH:
|
2018-03-30 16:15:06 +00:00
|
|
|
v.estack.PushVal(v.estack.Len())
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.NIP:
|
2019-09-12 08:09:23 +00:00
|
|
|
elem := v.estack.RemoveAt(1)
|
|
|
|
if elem == nil {
|
|
|
|
panic("no second element found")
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.OVER:
|
2019-12-17 13:56:58 +00:00
|
|
|
a := v.estack.Dup(1)
|
2019-09-05 12:57:44 +00:00
|
|
|
if a == nil {
|
|
|
|
panic("no second element found")
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
v.estack.Push(a)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.PICK:
|
2019-09-05 12:18:04 +00:00
|
|
|
n := int(v.estack.Pop().BigInt().Int64())
|
|
|
|
if n < 0 {
|
|
|
|
panic("negative stack item returned")
|
|
|
|
}
|
2019-12-17 13:56:58 +00:00
|
|
|
a := v.estack.Dup(n)
|
2019-09-05 12:57:44 +00:00
|
|
|
if a == nil {
|
|
|
|
panic("no nth element found")
|
|
|
|
}
|
2019-09-05 12:18:04 +00:00
|
|
|
v.estack.Push(a)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.ROLL:
|
2018-03-30 16:15:06 +00:00
|
|
|
n := int(v.estack.Pop().BigInt().Int64())
|
2019-12-16 16:53:21 +00:00
|
|
|
err := v.estack.Roll(n)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.DROP:
|
2019-09-12 08:05:10 +00:00
|
|
|
if v.estack.Len() < 1 {
|
|
|
|
panic("stack is too small")
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
v.estack.Pop()
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.EQUAL:
|
2019-08-20 16:46:52 +00:00
|
|
|
b := v.estack.Pop()
|
2019-09-12 07:50:43 +00:00
|
|
|
if b == nil {
|
|
|
|
panic("no top-level element found")
|
|
|
|
}
|
2019-08-20 16:46:52 +00:00
|
|
|
a := v.estack.Pop()
|
2019-09-12 07:50:43 +00:00
|
|
|
if a == nil {
|
|
|
|
panic("no second-to-the-top element found")
|
|
|
|
}
|
2019-09-18 11:10:10 +00:00
|
|
|
if ta, ok := a.value.(*ArrayItem); ok {
|
|
|
|
if tb, ok := b.value.(*ArrayItem); ok {
|
|
|
|
v.estack.PushVal(ta == tb)
|
|
|
|
break
|
|
|
|
}
|
2019-09-24 13:45:41 +00:00
|
|
|
} else if ma, ok := a.value.(*MapItem); ok {
|
|
|
|
if mb, ok := b.value.(*MapItem); ok {
|
|
|
|
v.estack.PushVal(ma == mb)
|
|
|
|
break
|
|
|
|
}
|
2019-09-18 11:10:10 +00:00
|
|
|
}
|
2019-09-09 08:23:27 +00:00
|
|
|
v.estack.PushVal(reflect.DeepEqual(a, b))
|
2018-03-30 16:15:06 +00:00
|
|
|
|
|
|
|
// Bit operations.
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.INVERT:
|
2019-09-05 14:20:53 +00:00
|
|
|
// inplace
|
2019-12-17 11:01:15 +00:00
|
|
|
e := v.estack.Peek(0)
|
|
|
|
i := e.BigInt()
|
|
|
|
e.value = makeStackItem(i.Not(i))
|
2019-09-05 14:20:53 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.AND:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
|
|
|
a := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(new(big.Int).And(b, a))
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.OR:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
|
|
|
a := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(new(big.Int).Or(b, a))
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.XOR:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
|
|
|
a := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(new(big.Int).Xor(b, a))
|
|
|
|
|
|
|
|
// Numeric operations.
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.ADD:
|
2018-03-30 16:15:06 +00:00
|
|
|
a := v.estack.Pop().BigInt()
|
2019-11-07 09:14:36 +00:00
|
|
|
v.checkBigIntSize(a)
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
2019-11-07 09:14:36 +00:00
|
|
|
v.checkBigIntSize(b)
|
|
|
|
|
|
|
|
c := new(big.Int).Add(a, b)
|
|
|
|
v.checkBigIntSize(c)
|
|
|
|
v.estack.PushVal(c)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.SUB:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
2019-11-07 09:14:36 +00:00
|
|
|
v.checkBigIntSize(b)
|
2018-03-30 16:15:06 +00:00
|
|
|
a := v.estack.Pop().BigInt()
|
2019-11-07 09:14:36 +00:00
|
|
|
v.checkBigIntSize(a)
|
|
|
|
|
|
|
|
c := new(big.Int).Sub(a, b)
|
|
|
|
v.checkBigIntSize(c)
|
|
|
|
v.estack.PushVal(c)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.DIV:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
2019-11-07 09:14:36 +00:00
|
|
|
v.checkBigIntSize(b)
|
2018-03-30 16:15:06 +00:00
|
|
|
a := v.estack.Pop().BigInt()
|
2019-11-07 09:14:36 +00:00
|
|
|
v.checkBigIntSize(a)
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
v.estack.PushVal(new(big.Int).Div(a, b))
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.MUL:
|
2018-03-30 16:15:06 +00:00
|
|
|
a := v.estack.Pop().BigInt()
|
2019-11-07 09:14:36 +00:00
|
|
|
v.checkBigIntSize(a)
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
2019-11-07 09:14:36 +00:00
|
|
|
v.checkBigIntSize(b)
|
|
|
|
|
|
|
|
c := new(big.Int).Mul(a, b)
|
|
|
|
v.checkBigIntSize(c)
|
|
|
|
v.estack.PushVal(c)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.MOD:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
2019-11-07 09:14:36 +00:00
|
|
|
v.checkBigIntSize(b)
|
2018-03-30 16:15:06 +00:00
|
|
|
a := v.estack.Pop().BigInt()
|
2019-11-07 09:14:36 +00:00
|
|
|
v.checkBigIntSize(a)
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
v.estack.PushVal(new(big.Int).Mod(a, b))
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.SHL, opcode.SHR:
|
2019-09-18 11:35:29 +00:00
|
|
|
b := v.estack.Pop().BigInt().Int64()
|
|
|
|
if b == 0 {
|
2019-09-12 09:02:38 +00:00
|
|
|
return
|
2019-09-18 11:35:29 +00:00
|
|
|
} else if b < minSHLArg || b > maxSHLArg {
|
|
|
|
panic(fmt.Sprintf("operand must be between %d and %d", minSHLArg, maxSHLArg))
|
2019-09-12 09:02:38 +00:00
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
a := v.estack.Pop().BigInt()
|
2019-11-07 09:14:36 +00:00
|
|
|
v.checkBigIntSize(a)
|
|
|
|
|
|
|
|
var item big.Int
|
2019-12-03 14:05:06 +00:00
|
|
|
if op == opcode.SHL {
|
2019-11-07 09:14:36 +00:00
|
|
|
item.Lsh(a, uint(b))
|
2019-09-18 11:35:29 +00:00
|
|
|
} else {
|
2019-11-07 09:14:36 +00:00
|
|
|
item.Rsh(a, uint(b))
|
2019-09-12 09:02:38 +00:00
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-11-07 09:14:36 +00:00
|
|
|
v.checkBigIntSize(&item)
|
|
|
|
v.estack.PushVal(&item)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.BOOLAND:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().Bool()
|
|
|
|
a := v.estack.Pop().Bool()
|
|
|
|
v.estack.PushVal(a && b)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.BOOLOR:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().Bool()
|
|
|
|
a := v.estack.Pop().Bool()
|
|
|
|
v.estack.PushVal(a || b)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.NUMEQUAL:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
|
|
|
a := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(a.Cmp(b) == 0)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.NUMNOTEQUAL:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
|
|
|
a := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(a.Cmp(b) != 0)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.LT:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
|
|
|
a := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(a.Cmp(b) == -1)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.GT:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
|
|
|
a := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(a.Cmp(b) == 1)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.LTE:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
|
|
|
a := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(a.Cmp(b) <= 0)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.GTE:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
|
|
|
a := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(a.Cmp(b) >= 0)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.MIN:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
|
|
|
a := v.estack.Pop().BigInt()
|
|
|
|
val := a
|
|
|
|
if a.Cmp(b) == 1 {
|
|
|
|
val = b
|
|
|
|
}
|
|
|
|
v.estack.PushVal(val)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.MAX:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
|
|
|
a := v.estack.Pop().BigInt()
|
|
|
|
val := a
|
|
|
|
if a.Cmp(b) == -1 {
|
|
|
|
val = b
|
|
|
|
}
|
|
|
|
v.estack.PushVal(val)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.WITHIN:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().BigInt()
|
|
|
|
a := v.estack.Pop().BigInt()
|
|
|
|
x := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(a.Cmp(x) <= 0 && x.Cmp(b) == -1)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.INC:
|
2018-03-30 16:15:06 +00:00
|
|
|
x := v.estack.Pop().BigInt()
|
2019-11-07 09:14:36 +00:00
|
|
|
a := new(big.Int).Add(x, big.NewInt(1))
|
|
|
|
v.checkBigIntSize(a)
|
|
|
|
v.estack.PushVal(a)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.DEC:
|
2018-03-30 16:15:06 +00:00
|
|
|
x := v.estack.Pop().BigInt()
|
2019-11-07 09:14:36 +00:00
|
|
|
a := new(big.Int).Sub(x, big.NewInt(1))
|
|
|
|
v.checkBigIntSize(a)
|
|
|
|
v.estack.PushVal(a)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.SIGN:
|
2018-03-30 16:15:06 +00:00
|
|
|
x := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(x.Sign())
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.NEGATE:
|
2018-03-30 16:15:06 +00:00
|
|
|
x := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(x.Neg(x))
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.ABS:
|
2018-03-30 16:15:06 +00:00
|
|
|
x := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(x.Abs(x))
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.NOT:
|
2018-04-04 19:41:19 +00:00
|
|
|
x := v.estack.Pop().Bool()
|
|
|
|
v.estack.PushVal(!x)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.NZ:
|
2019-09-05 12:05:44 +00:00
|
|
|
x := v.estack.Pop().BigInt()
|
|
|
|
v.estack.PushVal(x.Cmp(big.NewInt(0)) != 0)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
|
|
|
// Object operations.
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.NEWARRAY:
|
2019-09-06 16:00:04 +00:00
|
|
|
item := v.estack.Pop()
|
|
|
|
switch t := item.value.(type) {
|
|
|
|
case *StructItem:
|
2019-10-18 10:01:51 +00:00
|
|
|
arr := make([]StackItem, len(t.value))
|
|
|
|
copy(arr, t.value)
|
|
|
|
v.estack.PushVal(&ArrayItem{arr})
|
2019-09-06 16:00:04 +00:00
|
|
|
case *ArrayItem:
|
|
|
|
v.estack.PushVal(t)
|
|
|
|
default:
|
2019-10-17 08:03:21 +00:00
|
|
|
n := item.BigInt().Int64()
|
|
|
|
if n > MaxArraySize {
|
|
|
|
panic("too long array")
|
|
|
|
}
|
|
|
|
items := makeArrayOfFalses(int(n))
|
2019-09-12 08:48:39 +00:00
|
|
|
v.estack.PushVal(&ArrayItem{items})
|
2019-09-06 16:00:04 +00:00
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.NEWSTRUCT:
|
2019-09-06 16:00:04 +00:00
|
|
|
item := v.estack.Pop()
|
|
|
|
switch t := item.value.(type) {
|
|
|
|
case *ArrayItem:
|
2019-10-18 10:01:51 +00:00
|
|
|
arr := make([]StackItem, len(t.value))
|
|
|
|
copy(arr, t.value)
|
|
|
|
v.estack.PushVal(&StructItem{arr})
|
2019-09-06 16:00:04 +00:00
|
|
|
case *StructItem:
|
|
|
|
v.estack.PushVal(t)
|
|
|
|
default:
|
2019-10-17 08:03:21 +00:00
|
|
|
n := item.BigInt().Int64()
|
|
|
|
if n > MaxArraySize {
|
|
|
|
panic("too long struct")
|
|
|
|
}
|
|
|
|
items := makeArrayOfFalses(int(n))
|
2019-09-12 08:48:39 +00:00
|
|
|
v.estack.PushVal(&StructItem{items})
|
2019-09-06 16:00:04 +00:00
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.APPEND:
|
2018-03-30 16:15:06 +00:00
|
|
|
itemElem := v.estack.Pop()
|
|
|
|
arrElem := v.estack.Pop()
|
|
|
|
|
2019-09-25 09:18:37 +00:00
|
|
|
val := cloneIfStruct(itemElem.value)
|
2019-09-18 11:03:15 +00:00
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
switch t := arrElem.value.(type) {
|
2019-09-11 14:05:56 +00:00
|
|
|
case *ArrayItem:
|
|
|
|
arr := t.Value().([]StackItem)
|
2019-10-17 08:03:35 +00:00
|
|
|
if len(arr) >= MaxArraySize {
|
|
|
|
panic("too long array")
|
|
|
|
}
|
2019-09-18 11:03:15 +00:00
|
|
|
arr = append(arr, val)
|
2019-09-11 14:05:56 +00:00
|
|
|
t.value = arr
|
|
|
|
case *StructItem:
|
2018-03-30 16:15:06 +00:00
|
|
|
arr := t.Value().([]StackItem)
|
2019-10-17 08:03:35 +00:00
|
|
|
if len(arr) >= MaxArraySize {
|
|
|
|
panic("too long struct")
|
|
|
|
}
|
2019-09-18 11:03:15 +00:00
|
|
|
arr = append(arr, val)
|
2019-09-11 14:05:56 +00:00
|
|
|
t.value = arr
|
2018-03-30 16:15:06 +00:00
|
|
|
default:
|
|
|
|
panic("APPEND: not of underlying type Array")
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:26:34 +00:00
|
|
|
v.estack.updateSizeAdd(val)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.PACK:
|
2018-03-30 16:15:06 +00:00
|
|
|
n := int(v.estack.Pop().BigInt().Int64())
|
2019-10-17 08:07:44 +00:00
|
|
|
if n < 0 || n > v.estack.Len() || n > MaxArraySize {
|
2018-03-30 16:15:06 +00:00
|
|
|
panic("OPACK: invalid length")
|
|
|
|
}
|
|
|
|
|
|
|
|
items := make([]StackItem, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
items[i] = v.estack.Pop().value
|
|
|
|
}
|
|
|
|
|
|
|
|
v.estack.PushVal(items)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.UNPACK:
|
2019-09-06 08:32:20 +00:00
|
|
|
a := v.estack.Pop().Array()
|
|
|
|
l := len(a)
|
|
|
|
for i := l - 1; i >= 0; i-- {
|
|
|
|
v.estack.PushVal(a[i])
|
|
|
|
}
|
|
|
|
v.estack.PushVal(l)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.PICKITEM:
|
2019-09-24 12:58:31 +00:00
|
|
|
key := v.estack.Pop()
|
|
|
|
validateMapKey(key)
|
|
|
|
|
|
|
|
obj := v.estack.Pop()
|
|
|
|
index := int(key.BigInt().Int64())
|
2018-03-30 16:15:06 +00:00
|
|
|
|
|
|
|
switch t := obj.value.(type) {
|
|
|
|
// Struct and Array items have their underlying value as []StackItem.
|
2018-04-02 15:04:42 +00:00
|
|
|
case *ArrayItem, *StructItem:
|
2018-03-30 16:15:06 +00:00
|
|
|
arr := t.Value().([]StackItem)
|
|
|
|
if index < 0 || index >= len(arr) {
|
|
|
|
panic("PICKITEM: invalid index")
|
|
|
|
}
|
2019-12-17 14:20:44 +00:00
|
|
|
item := arr[index].Dup()
|
2018-03-30 16:15:06 +00:00
|
|
|
v.estack.PushVal(item)
|
2019-09-24 12:58:31 +00:00
|
|
|
case *MapItem:
|
|
|
|
if !t.Has(key.value) {
|
|
|
|
panic("invalid key")
|
|
|
|
}
|
|
|
|
k := toMapKey(key.value)
|
2019-12-17 14:20:44 +00:00
|
|
|
v.estack.Push(&Element{value: t.value[k].Dup()})
|
2018-03-30 16:15:06 +00:00
|
|
|
default:
|
2019-09-12 08:19:25 +00:00
|
|
|
arr := obj.Bytes()
|
|
|
|
if index < 0 || index >= len(arr) {
|
|
|
|
panic("PICKITEM: invalid index")
|
|
|
|
}
|
|
|
|
item := arr[index]
|
|
|
|
v.estack.PushVal(int(item))
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.SETITEM:
|
2019-09-24 12:58:31 +00:00
|
|
|
item := v.estack.Pop().value
|
|
|
|
key := v.estack.Pop()
|
|
|
|
validateMapKey(key)
|
|
|
|
|
|
|
|
obj := v.estack.Pop()
|
2018-03-30 16:15:06 +00:00
|
|
|
|
|
|
|
switch t := obj.value.(type) {
|
|
|
|
// Struct and Array items have their underlying value as []StackItem.
|
2018-04-02 15:04:42 +00:00
|
|
|
case *ArrayItem, *StructItem:
|
2018-03-30 16:15:06 +00:00
|
|
|
arr := t.Value().([]StackItem)
|
2019-09-24 12:58:31 +00:00
|
|
|
index := int(key.BigInt().Int64())
|
2018-03-30 16:15:06 +00:00
|
|
|
if index < 0 || index >= len(arr) {
|
2018-04-02 15:04:42 +00:00
|
|
|
panic("SETITEM: invalid index")
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
2019-10-29 10:26:34 +00:00
|
|
|
v.estack.updateSizeRemove(arr[index])
|
2018-03-30 16:15:06 +00:00
|
|
|
arr[index] = item
|
2019-10-29 10:26:34 +00:00
|
|
|
v.estack.updateSizeAdd(arr[index])
|
2019-09-24 12:58:31 +00:00
|
|
|
case *MapItem:
|
2019-10-29 10:26:34 +00:00
|
|
|
if t.Has(key.value) {
|
|
|
|
v.estack.updateSizeRemove(item)
|
|
|
|
} else if len(t.value) >= MaxArraySize {
|
2019-10-17 08:18:15 +00:00
|
|
|
panic("too big map")
|
|
|
|
}
|
2019-09-24 12:58:31 +00:00
|
|
|
t.Add(key.value, item)
|
2019-10-29 10:26:34 +00:00
|
|
|
v.estack.updateSizeAdd(item)
|
2019-09-24 12:58:31 +00:00
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
default:
|
2018-04-02 15:04:42 +00:00
|
|
|
panic(fmt.Sprintf("SETITEM: invalid item type %s", t))
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.REVERSE:
|
2019-09-16 12:46:35 +00:00
|
|
|
a := v.estack.Pop().Array()
|
2019-09-06 08:32:20 +00:00
|
|
|
if len(a) > 1 {
|
|
|
|
for i, j := 0, len(a)-1; i <= j; i, j = i+1, j-1 {
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.REMOVE:
|
2019-09-24 13:16:24 +00:00
|
|
|
key := v.estack.Pop()
|
|
|
|
validateMapKey(key)
|
|
|
|
|
2019-09-12 08:32:09 +00:00
|
|
|
elem := v.estack.Pop()
|
|
|
|
switch t := elem.value.(type) {
|
|
|
|
case *ArrayItem:
|
|
|
|
a := t.value
|
2019-09-24 13:16:24 +00:00
|
|
|
k := int(key.BigInt().Int64())
|
|
|
|
if k < 0 || k >= len(a) {
|
2019-09-12 08:32:09 +00:00
|
|
|
panic("REMOVE: invalid index")
|
|
|
|
}
|
2019-10-29 10:26:34 +00:00
|
|
|
v.estack.updateSizeRemove(a[k])
|
2019-09-24 13:16:24 +00:00
|
|
|
a = append(a[:k], a[k+1:]...)
|
2019-09-12 08:32:09 +00:00
|
|
|
t.value = a
|
|
|
|
case *StructItem:
|
|
|
|
a := t.value
|
2019-09-24 13:16:24 +00:00
|
|
|
k := int(key.BigInt().Int64())
|
|
|
|
if k < 0 || k >= len(a) {
|
2019-09-12 08:32:09 +00:00
|
|
|
panic("REMOVE: invalid index")
|
|
|
|
}
|
2019-10-29 10:26:34 +00:00
|
|
|
v.estack.updateSizeRemove(a[k])
|
2019-09-24 13:16:24 +00:00
|
|
|
a = append(a[:k], a[k+1:]...)
|
2019-09-12 08:32:09 +00:00
|
|
|
t.value = a
|
2019-09-24 13:16:24 +00:00
|
|
|
case *MapItem:
|
|
|
|
m := t.value
|
|
|
|
k := toMapKey(key.value)
|
2019-10-29 10:26:34 +00:00
|
|
|
v.estack.updateSizeRemove(m[k])
|
2019-09-24 13:16:24 +00:00
|
|
|
delete(m, k)
|
2019-09-12 08:32:09 +00:00
|
|
|
default:
|
|
|
|
panic("REMOVE: invalid type")
|
|
|
|
}
|
2019-09-06 08:32:20 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.ARRAYSIZE:
|
2018-03-30 16:15:06 +00:00
|
|
|
elem := v.estack.Pop()
|
2018-04-22 18:11:37 +00:00
|
|
|
// Cause there is no native (byte) item type here, hence we need to check
|
|
|
|
// the type of the item for array size operations.
|
2019-10-03 13:12:24 +00:00
|
|
|
switch t := elem.Value().(type) {
|
2018-04-22 18:11:37 +00:00
|
|
|
case []StackItem:
|
|
|
|
v.estack.PushVal(len(t))
|
2019-09-24 13:50:37 +00:00
|
|
|
case map[interface{}]StackItem:
|
2018-04-22 18:11:37 +00:00
|
|
|
v.estack.PushVal(len(t))
|
|
|
|
default:
|
2019-09-24 13:50:37 +00:00
|
|
|
v.estack.PushVal(len(elem.Bytes()))
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.SIZE:
|
2018-04-04 19:41:19 +00:00
|
|
|
elem := v.estack.Pop()
|
2019-09-12 08:53:11 +00:00
|
|
|
arr := elem.Bytes()
|
2018-04-04 19:41:19 +00:00
|
|
|
v.estack.PushVal(len(arr))
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.JMP, opcode.JMPIF, opcode.JMPIFNOT:
|
2018-04-02 15:04:42 +00:00
|
|
|
var (
|
2019-10-03 13:54:14 +00:00
|
|
|
rOffset = int16(binary.LittleEndian.Uint16(parameter))
|
|
|
|
offset = ctx.ip + int(rOffset)
|
2018-04-02 15:04:42 +00:00
|
|
|
)
|
2018-03-30 16:15:06 +00:00
|
|
|
if offset < 0 || offset > len(ctx.prog) {
|
2018-04-02 15:04:42 +00:00
|
|
|
panic(fmt.Sprintf("JMP: invalid offset %d ip at %d", offset, ctx.ip))
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
cond := true
|
2019-12-03 14:05:06 +00:00
|
|
|
if op > opcode.JMP {
|
2018-03-30 16:15:06 +00:00
|
|
|
cond = v.estack.Pop().Bool()
|
2019-12-03 14:05:06 +00:00
|
|
|
if op == opcode.JMPIFNOT {
|
2018-03-30 16:15:06 +00:00
|
|
|
cond = !cond
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cond {
|
2019-10-03 13:54:14 +00:00
|
|
|
ctx.nextip = offset
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.CALL:
|
2019-10-29 08:01:06 +00:00
|
|
|
v.checkInvocationStackSize()
|
|
|
|
|
2019-10-25 14:25:46 +00:00
|
|
|
newCtx := ctx.Copy()
|
|
|
|
newCtx.rvcount = -1
|
|
|
|
v.istack.PushVal(newCtx)
|
2019-12-03 14:05:06 +00:00
|
|
|
err = v.execute(v.Context(), opcode.JMP, parameter)
|
2019-10-22 10:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.SYSCALL:
|
2019-10-03 13:54:14 +00:00
|
|
|
ifunc, ok := v.interop[string(parameter)]
|
2018-04-10 09:45:31 +00:00
|
|
|
if !ok {
|
2019-10-03 13:54:14 +00:00
|
|
|
panic(fmt.Sprintf("interop hook (%q) not registered", parameter))
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
2019-10-01 13:37:42 +00:00
|
|
|
if err := ifunc.Func(v); err != nil {
|
2018-03-30 16:15:06 +00:00
|
|
|
panic(fmt.Sprintf("failed to invoke syscall: %s", err))
|
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.APPCALL, opcode.TAILCALL:
|
2019-09-30 16:52:16 +00:00
|
|
|
if v.getScript == nil {
|
|
|
|
panic("no getScript callback is set up")
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
if op == opcode.APPCALL {
|
2019-10-29 08:01:06 +00:00
|
|
|
v.checkInvocationStackSize()
|
|
|
|
}
|
|
|
|
|
2019-11-27 09:20:31 +00:00
|
|
|
hash, err := util.Uint160DecodeBytesBE(parameter)
|
2018-03-30 16:15:06 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-09-30 16:52:16 +00:00
|
|
|
script := v.getScript(hash)
|
|
|
|
if script == nil {
|
2018-03-30 16:15:06 +00:00
|
|
|
panic("could not find script")
|
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
if op == opcode.TAILCALL {
|
2018-03-30 16:15:06 +00:00
|
|
|
_ = v.istack.Pop()
|
|
|
|
}
|
|
|
|
|
|
|
|
v.LoadScript(script)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.RET:
|
2019-10-25 14:25:46 +00:00
|
|
|
oldCtx := v.istack.Pop().Value().(*Context)
|
|
|
|
rvcount := oldCtx.rvcount
|
|
|
|
oldEstack := v.estack
|
|
|
|
|
|
|
|
if rvcount > 0 && oldEstack.Len() < rvcount {
|
|
|
|
panic("missing some return elements")
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
if v.istack.Len() == 0 {
|
|
|
|
v.state = haltState
|
2019-10-25 14:25:46 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
newEstack := v.Context().estack
|
|
|
|
if oldEstack != newEstack {
|
|
|
|
if rvcount < 0 {
|
|
|
|
rvcount = oldEstack.Len()
|
|
|
|
}
|
|
|
|
for i := rvcount; i > 0; i-- {
|
|
|
|
elem := oldEstack.RemoveAt(i - 1)
|
|
|
|
newEstack.Push(elem)
|
|
|
|
}
|
|
|
|
v.estack = newEstack
|
|
|
|
v.astack = v.Context().astack
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.CHECKSIG, opcode.VERIFY:
|
2019-09-23 16:54:06 +00:00
|
|
|
var hashToCheck []byte
|
|
|
|
|
|
|
|
keyb := v.estack.Pop().Bytes()
|
|
|
|
signature := v.estack.Pop().Bytes()
|
2019-12-03 14:05:06 +00:00
|
|
|
if op == opcode.CHECKSIG {
|
2019-09-23 16:54:06 +00:00
|
|
|
if v.checkhash == nil {
|
|
|
|
panic("VM is not set up properly for signature checks")
|
|
|
|
}
|
|
|
|
hashToCheck = v.checkhash
|
|
|
|
} else { // VERIFY
|
|
|
|
msg := v.estack.Pop().Bytes()
|
2019-11-27 09:23:18 +00:00
|
|
|
hashToCheck = hash.Sha256(msg).BytesBE()
|
2019-09-23 16:54:06 +00:00
|
|
|
}
|
2019-12-10 16:13:29 +00:00
|
|
|
pkey := v.bytesToPublicKey(keyb)
|
2019-09-23 16:54:06 +00:00
|
|
|
res := pkey.Verify(signature, hashToCheck)
|
|
|
|
v.estack.PushVal(res)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.CHECKMULTISIG:
|
2019-09-23 16:54:06 +00:00
|
|
|
pkeys, err := v.estack.popSigElements()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("wrong parameters: %s", err.Error()))
|
|
|
|
}
|
|
|
|
sigs, err := v.estack.popSigElements()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("wrong parameters: %s", err.Error()))
|
|
|
|
}
|
2019-09-30 13:52:35 +00:00
|
|
|
// It's ok to have more keys than there are signatures (it would
|
|
|
|
// just mean that some keys didn't sign), but not the other way around.
|
2019-09-23 16:54:06 +00:00
|
|
|
if len(pkeys) < len(sigs) {
|
|
|
|
panic("more signatures than there are keys")
|
|
|
|
}
|
|
|
|
if v.checkhash == nil {
|
|
|
|
panic("VM is not set up properly for signature checks")
|
|
|
|
}
|
|
|
|
sigok := true
|
2019-09-30 13:52:35 +00:00
|
|
|
// j counts keys and i counts signatures.
|
2019-09-23 16:54:06 +00:00
|
|
|
j := 0
|
2019-09-30 13:52:35 +00:00
|
|
|
for i := 0; sigok && j < len(pkeys) && i < len(sigs); {
|
2019-12-10 16:13:29 +00:00
|
|
|
pkey := v.bytesToPublicKey(pkeys[j])
|
2019-09-30 13:52:35 +00:00
|
|
|
// We only move to the next signature if the check was
|
|
|
|
// successful, but if it's not maybe the next key will
|
|
|
|
// fit, so we always move to the next key.
|
2019-09-23 16:54:06 +00:00
|
|
|
if pkey.Verify(sigs[i], v.checkhash) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
j++
|
2019-09-30 13:52:35 +00:00
|
|
|
// When there are more signatures left to check than
|
|
|
|
// there are keys the check won't successed for sure.
|
|
|
|
if len(sigs)-i > len(pkeys)-j {
|
2019-09-23 16:54:06 +00:00
|
|
|
sigok = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.estack.PushVal(sigok)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.NEWMAP:
|
2019-09-24 12:06:23 +00:00
|
|
|
v.estack.Push(&Element{value: NewMapItem()})
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.KEYS:
|
2019-09-24 12:41:39 +00:00
|
|
|
item := v.estack.Pop()
|
|
|
|
if item == nil {
|
|
|
|
panic("no argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
m, ok := item.value.(*MapItem)
|
|
|
|
if !ok {
|
|
|
|
panic("not a Map")
|
|
|
|
}
|
|
|
|
|
|
|
|
arr := make([]StackItem, 0, len(m.value))
|
|
|
|
for k := range m.value {
|
|
|
|
arr = append(arr, makeStackItem(k))
|
|
|
|
}
|
|
|
|
v.estack.PushVal(arr)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.VALUES:
|
2019-09-24 12:53:55 +00:00
|
|
|
item := v.estack.Pop()
|
|
|
|
if item == nil {
|
|
|
|
panic("no argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
var arr []StackItem
|
|
|
|
switch t := item.value.(type) {
|
|
|
|
case *ArrayItem, *StructItem:
|
|
|
|
src := t.Value().([]StackItem)
|
|
|
|
arr = make([]StackItem, len(src))
|
|
|
|
for i := range src {
|
|
|
|
arr[i] = cloneIfStruct(src[i])
|
|
|
|
}
|
|
|
|
case *MapItem:
|
|
|
|
arr = make([]StackItem, 0, len(t.value))
|
|
|
|
for k := range t.value {
|
|
|
|
arr = append(arr, cloneIfStruct(t.value[k]))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("not a Map, Array or Struct")
|
|
|
|
}
|
|
|
|
|
|
|
|
v.estack.PushVal(arr)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.HASKEY:
|
2019-09-24 12:25:57 +00:00
|
|
|
key := v.estack.Pop()
|
|
|
|
validateMapKey(key)
|
|
|
|
|
|
|
|
c := v.estack.Pop()
|
|
|
|
if c == nil {
|
|
|
|
panic("no value found")
|
|
|
|
}
|
|
|
|
switch t := c.value.(type) {
|
|
|
|
case *ArrayItem, *StructItem:
|
|
|
|
index := key.BigInt().Int64()
|
|
|
|
if index < 0 {
|
|
|
|
panic("negative index")
|
|
|
|
}
|
|
|
|
v.estack.PushVal(index < int64(len(c.Array())))
|
|
|
|
case *MapItem:
|
|
|
|
v.estack.PushVal(t.Has(key.value))
|
|
|
|
default:
|
|
|
|
panic("wrong collection type")
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// Cryptographic operations.
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.SHA1:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().Bytes()
|
|
|
|
sha := sha1.New()
|
|
|
|
sha.Write(b)
|
|
|
|
v.estack.PushVal(sha.Sum(nil))
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.SHA256:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().Bytes()
|
2019-11-27 09:23:18 +00:00
|
|
|
v.estack.PushVal(hash.Sha256(b).BytesBE())
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.HASH160:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().Bytes()
|
2019-11-27 09:20:31 +00:00
|
|
|
v.estack.PushVal(hash.Hash160(b).BytesBE())
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.HASH256:
|
2018-03-30 16:15:06 +00:00
|
|
|
b := v.estack.Pop().Bytes()
|
2019-11-27 09:23:18 +00:00
|
|
|
v.estack.PushVal(hash.DoubleSha256(b).BytesBE())
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.NOP:
|
2018-03-30 16:15:06 +00:00
|
|
|
// unlucky ^^
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.CALLI, opcode.CALLE, opcode.CALLED, opcode.CALLET, opcode.CALLEDT:
|
2019-10-25 14:25:46 +00:00
|
|
|
var (
|
2019-12-03 14:05:06 +00:00
|
|
|
tailCall = (op == opcode.CALLET || op == opcode.CALLEDT)
|
|
|
|
hashOnStack = (op == opcode.CALLED || op == opcode.CALLEDT)
|
2019-10-25 14:25:46 +00:00
|
|
|
addElement int
|
|
|
|
newCtx *Context
|
|
|
|
)
|
|
|
|
|
|
|
|
if hashOnStack {
|
|
|
|
addElement = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
rvcount := int(parameter[0])
|
|
|
|
pcount := int(parameter[1])
|
|
|
|
if v.estack.Len() < pcount+addElement {
|
|
|
|
panic("missing some parameters")
|
|
|
|
}
|
2019-10-29 08:01:06 +00:00
|
|
|
if tailCall {
|
|
|
|
if ctx.rvcount != rvcount {
|
|
|
|
panic("context and parameter rvcount mismatch")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
v.checkInvocationStackSize()
|
2019-10-25 14:25:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
if op == opcode.CALLI {
|
2019-10-25 14:25:46 +00:00
|
|
|
newCtx = ctx.Copy()
|
|
|
|
} else {
|
|
|
|
var hashBytes []byte
|
|
|
|
|
|
|
|
if hashOnStack {
|
|
|
|
hashBytes = v.estack.Pop().Bytes()
|
|
|
|
} else {
|
|
|
|
hashBytes = parameter[2:]
|
|
|
|
}
|
|
|
|
|
2019-11-27 09:20:31 +00:00
|
|
|
hash, err := util.Uint160DecodeBytesBE(hashBytes)
|
2019-10-25 14:25:46 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
script := v.getScript(hash)
|
|
|
|
if script == nil {
|
|
|
|
panic(fmt.Sprintf("could not find script %s", hash))
|
|
|
|
}
|
|
|
|
newCtx = NewContext(script)
|
|
|
|
}
|
|
|
|
newCtx.rvcount = rvcount
|
|
|
|
newCtx.estack = NewStack("evaluation")
|
|
|
|
newCtx.astack = NewStack("alt")
|
|
|
|
// Going backwards to naturally push things onto the new stack.
|
|
|
|
for i := pcount; i > 0; i-- {
|
|
|
|
elem := v.estack.RemoveAt(i - 1)
|
|
|
|
newCtx.estack.Push(elem)
|
|
|
|
}
|
|
|
|
if tailCall {
|
|
|
|
_ = v.istack.Pop()
|
|
|
|
}
|
|
|
|
v.istack.PushVal(newCtx)
|
|
|
|
v.estack = newCtx.estack
|
|
|
|
v.astack = newCtx.astack
|
2019-12-03 14:05:06 +00:00
|
|
|
if op == opcode.CALLI {
|
|
|
|
err = v.execute(v.Context(), opcode.JMP, parameter[2:])
|
2019-10-25 14:25:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.THROW:
|
2018-03-30 16:15:06 +00:00
|
|
|
panic("THROW")
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.THROWIFNOT:
|
2018-03-30 16:15:06 +00:00
|
|
|
if !v.estack.Pop().Bool() {
|
|
|
|
panic("THROWIFNOT")
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2019-02-09 15:53:58 +00:00
|
|
|
panic(fmt.Sprintf("unknown opcode %s", op.String()))
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
2019-10-22 10:44:14 +00:00
|
|
|
return
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 12:53:55 +00:00
|
|
|
func cloneIfStruct(item StackItem) StackItem {
|
|
|
|
switch it := item.(type) {
|
|
|
|
case *StructItem:
|
|
|
|
return it.Clone()
|
|
|
|
default:
|
|
|
|
return it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 08:24:10 +00:00
|
|
|
func makeArrayOfFalses(n int) []StackItem {
|
|
|
|
items := make([]StackItem, n)
|
|
|
|
for i := range items {
|
|
|
|
items[i] = &BoolItem{false}
|
|
|
|
}
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
2019-09-24 12:25:57 +00:00
|
|
|
func validateMapKey(key *Element) {
|
|
|
|
if key == nil {
|
|
|
|
panic("no key found")
|
|
|
|
}
|
|
|
|
switch key.value.(type) {
|
|
|
|
case *ArrayItem, *StructItem, *MapItem:
|
|
|
|
panic("key can't be a collection")
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 08:01:06 +00:00
|
|
|
|
|
|
|
func (v *VM) checkInvocationStackSize() {
|
|
|
|
if v.istack.len >= MaxInvocationStackSize {
|
|
|
|
panic("invocation stack is too big")
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 09:14:36 +00:00
|
|
|
|
|
|
|
func (v *VM) checkBigIntSize(a *big.Int) {
|
|
|
|
if a.BitLen() > MaxBigIntegerSizeBits {
|
|
|
|
panic("big integer is too big")
|
|
|
|
}
|
|
|
|
}
|
2019-12-10 16:13:29 +00:00
|
|
|
|
|
|
|
// bytesToPublicKey is a helper deserializing keys using cache and panicing on
|
|
|
|
// error.
|
|
|
|
func (v *VM) bytesToPublicKey(b []byte) *keys.PublicKey {
|
|
|
|
var pkey *keys.PublicKey
|
|
|
|
s := string(b)
|
|
|
|
if v.keys[s] != nil {
|
|
|
|
pkey = v.keys[s]
|
|
|
|
} else {
|
|
|
|
pkey = &keys.PublicKey{}
|
|
|
|
err := pkey.DecodeBytes(b)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
v.keys[s] = pkey
|
|
|
|
}
|
|
|
|
return pkey
|
|
|
|
}
|