2018-03-30 16:15:06 +00:00
|
|
|
package vm
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
import (
|
2019-12-13 16:31:13 +00:00
|
|
|
"encoding/binary"
|
2019-10-03 13:54:14 +00:00
|
|
|
"errors"
|
2020-03-19 15:21:56 +00:00
|
|
|
"math/big"
|
2019-10-03 13:54:14 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-06-10 12:51:28 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2018-04-02 15:04:42 +00:00
|
|
|
)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Context represents the current execution context of the VM.
|
2018-03-30 16:15:06 +00:00
|
|
|
type Context struct {
|
|
|
|
// Instruction pointer.
|
|
|
|
ip int
|
|
|
|
|
2019-10-03 13:54:14 +00:00
|
|
|
// The next instruction pointer.
|
|
|
|
nextip int
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// The raw program script.
|
|
|
|
prog []byte
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Breakpoints.
|
2018-03-30 16:15:06 +00:00
|
|
|
breakPoints []int
|
2019-10-25 14:25:46 +00:00
|
|
|
|
|
|
|
// Evaluation stack pointer.
|
|
|
|
estack *Stack
|
|
|
|
|
2020-07-09 12:00:49 +00:00
|
|
|
static *Slot
|
2020-05-07 08:54:35 +00:00
|
|
|
local *Slot
|
|
|
|
arguments *Slot
|
|
|
|
|
2020-07-22 09:05:46 +00:00
|
|
|
// Exception context stack pointer.
|
|
|
|
tryStack *Stack
|
|
|
|
|
2019-12-13 14:05:03 +00:00
|
|
|
// Script hash of the prog.
|
|
|
|
scriptHash util.Uint160
|
2020-06-10 12:51:28 +00:00
|
|
|
|
2020-06-23 18:39:26 +00:00
|
|
|
// Caller's contract script hash.
|
|
|
|
callingScriptHash util.Uint160
|
|
|
|
|
2020-11-26 20:02:00 +00:00
|
|
|
// Set to true when running deployed contracts.
|
|
|
|
isDeployed bool
|
|
|
|
|
2020-06-10 12:51:28 +00:00
|
|
|
// Call flags this context was created with.
|
|
|
|
callFlag smartcontract.CallFlag
|
2020-08-10 08:52:32 +00:00
|
|
|
|
2020-11-24 12:45:14 +00:00
|
|
|
// InvocationState contains expected return type and actions to be performed on context unload.
|
|
|
|
InvocationState
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvocationState contains return convention and callback to be executed on context unload.
|
|
|
|
type InvocationState struct {
|
|
|
|
// Callback is executed on context unload.
|
|
|
|
Callback func(ctx *Context)
|
2020-08-10 08:52:32 +00:00
|
|
|
// CheckReturn specifies if amount of return values needs to be checked.
|
2020-10-12 11:32:27 +00:00
|
|
|
CheckReturn CheckReturnState
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 11:32:27 +00:00
|
|
|
// CheckReturnState represents possible states of stack after opcode.RET was processed.
|
|
|
|
type CheckReturnState byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
// NoCheck performs no return values check.
|
|
|
|
NoCheck CheckReturnState = 0
|
|
|
|
// EnsureIsEmpty checks that stack is empty and panics if not.
|
|
|
|
EnsureIsEmpty CheckReturnState = 1
|
|
|
|
// EnsureNotEmpty checks that stack contains not more than 1 element and panics if not.
|
|
|
|
// It pushes stackitem.Null on stack in case if there's no elements.
|
|
|
|
EnsureNotEmpty CheckReturnState = 2
|
|
|
|
)
|
|
|
|
|
2019-12-13 16:31:13 +00:00
|
|
|
var errNoInstParam = errors.New("failed to read instruction parameter")
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// NewContext returns a new Context object.
|
2018-03-30 16:15:06 +00:00
|
|
|
func NewContext(b []byte) *Context {
|
|
|
|
return &Context{
|
|
|
|
prog: b,
|
|
|
|
breakPoints: []int{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:02:21 +00:00
|
|
|
// Estack returns the evaluation stack of c.
|
|
|
|
func (c *Context) Estack() *Stack {
|
|
|
|
return c.estack
|
|
|
|
}
|
|
|
|
|
2020-02-03 09:10:07 +00:00
|
|
|
// NextIP returns next instruction pointer.
|
|
|
|
func (c *Context) NextIP() int {
|
|
|
|
return c.nextip
|
|
|
|
}
|
|
|
|
|
2019-10-03 13:54:14 +00:00
|
|
|
// Next returns the next instruction to execute with its parameter if any. After
|
|
|
|
// its invocation the instruction pointer points to the instruction being
|
|
|
|
// returned.
|
2019-12-03 14:05:06 +00:00
|
|
|
func (c *Context) Next() (opcode.Opcode, []byte, error) {
|
2019-12-13 16:31:13 +00:00
|
|
|
var err error
|
|
|
|
|
2019-10-03 13:54:14 +00:00
|
|
|
c.ip = c.nextip
|
2018-04-04 19:41:19 +00:00
|
|
|
if c.ip >= len(c.prog) {
|
2019-12-03 14:05:06 +00:00
|
|
|
return opcode.RET, nil, nil
|
2019-10-03 13:54:14 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 16:31:13 +00:00
|
|
|
var instrbyte = c.prog[c.ip]
|
2019-12-03 14:05:06 +00:00
|
|
|
instr := opcode.Opcode(instrbyte)
|
2019-10-03 13:54:14 +00:00
|
|
|
c.nextip++
|
|
|
|
|
|
|
|
var numtoread int
|
|
|
|
switch instr {
|
2020-04-15 14:40:05 +00:00
|
|
|
case opcode.PUSHDATA1:
|
2019-12-13 16:31:13 +00:00
|
|
|
if c.nextip >= len(c.prog) {
|
|
|
|
err = errNoInstParam
|
|
|
|
} else {
|
|
|
|
numtoread = int(c.prog[c.nextip])
|
|
|
|
c.nextip++
|
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.PUSHDATA2:
|
2019-12-13 16:31:13 +00:00
|
|
|
if c.nextip+1 >= len(c.prog) {
|
|
|
|
err = errNoInstParam
|
|
|
|
} else {
|
|
|
|
numtoread = int(binary.LittleEndian.Uint16(c.prog[c.nextip : c.nextip+2]))
|
|
|
|
c.nextip += 2
|
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.PUSHDATA4:
|
2019-12-13 16:31:13 +00:00
|
|
|
if c.nextip+3 >= len(c.prog) {
|
|
|
|
err = errNoInstParam
|
|
|
|
} else {
|
|
|
|
var n = binary.LittleEndian.Uint32(c.prog[c.nextip : c.nextip+4])
|
2020-06-11 13:31:31 +00:00
|
|
|
if n > stackitem.MaxSize {
|
2019-12-13 16:31:13 +00:00
|
|
|
return instr, nil, errors.New("parameter is too big")
|
|
|
|
}
|
|
|
|
numtoread = int(n)
|
|
|
|
c.nextip += 4
|
2019-10-17 14:10:00 +00:00
|
|
|
}
|
2020-04-23 08:56:36 +00:00
|
|
|
case opcode.JMP, opcode.JMPIF, opcode.JMPIFNOT, opcode.JMPEQ, opcode.JMPNE,
|
|
|
|
opcode.JMPGT, opcode.JMPGE, opcode.JMPLT, opcode.JMPLE,
|
2020-05-07 08:54:35 +00:00
|
|
|
opcode.CALL, opcode.ISTYPE, opcode.CONVERT, opcode.NEWARRAYT,
|
2020-07-22 09:05:46 +00:00
|
|
|
opcode.ENDTRY,
|
2020-05-07 08:54:35 +00:00
|
|
|
opcode.INITSSLOT, opcode.LDSFLD, opcode.STSFLD, opcode.LDARG, opcode.STARG, opcode.LDLOC, opcode.STLOC:
|
2020-04-23 08:56:36 +00:00
|
|
|
numtoread = 1
|
2020-07-22 09:05:46 +00:00
|
|
|
case opcode.INITSLOT, opcode.TRY:
|
2020-05-07 08:54:35 +00:00
|
|
|
numtoread = 2
|
2020-04-23 08:56:36 +00:00
|
|
|
case opcode.JMPL, opcode.JMPIFL, opcode.JMPIFNOTL, opcode.JMPEQL, opcode.JMPNEL,
|
|
|
|
opcode.JMPGTL, opcode.JMPGEL, opcode.JMPLTL, opcode.JMPLEL,
|
2020-07-22 09:05:46 +00:00
|
|
|
opcode.ENDTRYL,
|
2020-05-06 13:55:30 +00:00
|
|
|
opcode.CALLL, opcode.SYSCALL, opcode.PUSHA:
|
2019-10-25 14:25:46 +00:00
|
|
|
numtoread = 4
|
2020-07-22 09:05:46 +00:00
|
|
|
case opcode.TRYL:
|
|
|
|
numtoread = 8
|
2019-10-03 13:54:14 +00:00
|
|
|
default:
|
2020-04-21 13:45:48 +00:00
|
|
|
if instr <= opcode.PUSHINT256 {
|
|
|
|
numtoread = 1 << instr
|
2019-10-03 13:54:14 +00:00
|
|
|
} else {
|
|
|
|
// No parameters, can just return.
|
|
|
|
return instr, nil, nil
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 16:31:13 +00:00
|
|
|
if c.nextip+numtoread-1 >= len(c.prog) {
|
|
|
|
err = errNoInstParam
|
2018-04-04 19:41:19 +00:00
|
|
|
}
|
2019-12-13 16:31:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return instr, nil, err
|
|
|
|
}
|
|
|
|
parameter := make([]byte, numtoread)
|
|
|
|
copy(parameter, c.prog[c.nextip:c.nextip+numtoread])
|
2019-10-03 13:54:14 +00:00
|
|
|
c.nextip += numtoread
|
|
|
|
return instr, parameter, nil
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 09:37:31 +00:00
|
|
|
// IP returns current instruction offset in the context script.
|
2018-03-30 16:15:06 +00:00
|
|
|
func (c *Context) IP() int {
|
2020-08-19 09:37:31 +00:00
|
|
|
return c.ip
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LenInstr returns the number of instructions loaded.
|
|
|
|
func (c *Context) LenInstr() int {
|
|
|
|
return len(c.prog)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrInstr returns the current instruction and opcode.
|
2019-12-03 14:05:06 +00:00
|
|
|
func (c *Context) CurrInstr() (int, opcode.Opcode) {
|
|
|
|
return c.ip, opcode.Opcode(c.prog[c.ip])
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:53:38 +00:00
|
|
|
// NextInstr returns the next instruction and opcode.
|
|
|
|
func (c *Context) NextInstr() (int, opcode.Opcode) {
|
|
|
|
op := opcode.RET
|
|
|
|
if c.nextip < len(c.prog) {
|
|
|
|
op = opcode.Opcode(c.prog[c.nextip])
|
|
|
|
}
|
|
|
|
return c.nextip, op
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// Copy returns an new exact copy of c.
|
|
|
|
func (c *Context) Copy() *Context {
|
2019-10-03 13:54:14 +00:00
|
|
|
ctx := new(Context)
|
|
|
|
*ctx = *c
|
|
|
|
return ctx
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 14:21:26 +00:00
|
|
|
// GetCallFlags returns calling flags context was created with.
|
|
|
|
func (c *Context) GetCallFlags() smartcontract.CallFlag {
|
|
|
|
return c.callFlag
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// Program returns the loaded program.
|
|
|
|
func (c *Context) Program() []byte {
|
|
|
|
return c.prog
|
|
|
|
}
|
|
|
|
|
2019-12-13 14:05:03 +00:00
|
|
|
// ScriptHash returns a hash of the script in the current context.
|
|
|
|
func (c *Context) ScriptHash() util.Uint160 {
|
|
|
|
if c.scriptHash.Equals(util.Uint160{}) {
|
|
|
|
c.scriptHash = hash.Hash160(c.prog)
|
|
|
|
}
|
|
|
|
return c.scriptHash
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
// Value implements stackitem.Item interface.
|
2018-03-30 16:15:06 +00:00
|
|
|
func (c *Context) Value() interface{} {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
// Dup implements stackitem.Item interface.
|
|
|
|
func (c *Context) Dup() stackitem.Item {
|
2019-12-17 13:38:42 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-08-21 17:55:20 +00:00
|
|
|
// TryBool implements stackitem.Item interface.
|
|
|
|
func (c *Context) TryBool() (bool, error) { panic("can't convert Context to Bool") }
|
2020-04-28 08:23:58 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
// TryBytes implements stackitem.Item interface.
|
2020-03-11 13:04:28 +00:00
|
|
|
func (c *Context) TryBytes() ([]byte, error) {
|
|
|
|
return nil, errors.New("can't convert Context to ByteArray")
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
// TryInteger implements stackitem.Item interface.
|
2020-03-19 15:21:56 +00:00
|
|
|
func (c *Context) TryInteger() (*big.Int, error) {
|
|
|
|
return nil, errors.New("can't convert Context to Integer")
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
// Type implements stackitem.Item interface.
|
|
|
|
func (c *Context) Type() stackitem.Type { panic("Context cannot appear on evaluation stack") }
|
2020-04-24 10:46:46 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
// Convert implements stackitem.Item interface.
|
|
|
|
func (c *Context) Convert(_ stackitem.Type) (stackitem.Item, error) {
|
2020-04-28 08:24:02 +00:00
|
|
|
panic("Context cannot be converted to anything")
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
// Equals implements stackitem.Item interface.
|
|
|
|
func (c *Context) Equals(s stackitem.Item) bool {
|
2020-03-11 13:44:10 +00:00
|
|
|
return c == s
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
func (c *Context) atBreakPoint() bool {
|
|
|
|
for _, n := range c.breakPoints {
|
2020-08-18 08:13:09 +00:00
|
|
|
if n == c.nextip {
|
2018-03-30 16:15:06 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) String() string {
|
|
|
|
return "execution context"
|
|
|
|
}
|
2020-04-13 12:37:44 +00:00
|
|
|
|
2020-11-26 20:02:00 +00:00
|
|
|
// IsDeployed returns whether this context contains deployed contract.
|
|
|
|
func (c *Context) IsDeployed() bool {
|
|
|
|
return c.isDeployed
|
|
|
|
}
|
|
|
|
|
2020-05-04 08:41:41 +00:00
|
|
|
// getContextScriptHash returns script hash of the invocation stack element
|
2020-04-13 12:37:44 +00:00
|
|
|
// number n.
|
2020-05-04 08:41:41 +00:00
|
|
|
func (v *VM) getContextScriptHash(n int) util.Uint160 {
|
2020-04-30 16:19:31 +00:00
|
|
|
element := v.Istack().Peek(n)
|
|
|
|
if element == nil {
|
|
|
|
return util.Uint160{}
|
|
|
|
}
|
|
|
|
ctxIface := element.Value()
|
2020-04-13 12:37:44 +00:00
|
|
|
ctx := ctxIface.(*Context)
|
|
|
|
return ctx.ScriptHash()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushContextScriptHash pushes to evaluation stack the script hash of the
|
|
|
|
// invocation stack element number n.
|
|
|
|
func (v *VM) PushContextScriptHash(n int) error {
|
2020-05-04 08:41:41 +00:00
|
|
|
h := v.getContextScriptHash(n)
|
2020-04-13 12:37:44 +00:00
|
|
|
v.Estack().PushVal(h.BytesBE())
|
|
|
|
return nil
|
|
|
|
}
|