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"
|
2021-09-08 14:27:11 +00:00
|
|
|
"encoding/json"
|
2019-10-03 13:54:14 +00:00
|
|
|
"errors"
|
2021-02-09 14:03:06 +00:00
|
|
|
"fmt"
|
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-12-29 10:45:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2021-01-19 08:23:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-07-08 17:49:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/invocations"
|
2020-03-03 14:21:42 +00:00
|
|
|
"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
|
|
|
|
2022-08-04 13:15:51 +00:00
|
|
|
// scriptContext is a part of the Context that is shared between multiple Contexts,
|
|
|
|
// it's created when a new script is loaded into the VM while regular
|
|
|
|
// CALL/CALLL/CALLA internal invocations reuse it.
|
|
|
|
type scriptContext struct {
|
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
|
|
|
|
|
2022-08-04 13:15:51 +00:00
|
|
|
static slot
|
2020-07-22 09:05:46 +00:00
|
|
|
|
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
|
|
|
|
|
2022-08-04 13:35:02 +00:00
|
|
|
// Caller's scriptContext, if not entry.
|
|
|
|
callingContext *scriptContext
|
|
|
|
|
2020-06-10 12:51:28 +00:00
|
|
|
// Call flags this context was created with.
|
2020-12-29 10:45:49 +00:00
|
|
|
callFlag callflag.CallFlag
|
2020-08-10 08:52:32 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NEF represents a NEF file for the current contract.
|
2021-01-19 08:23:39 +00:00
|
|
|
NEF *nef.File
|
2022-08-04 13:15:51 +00:00
|
|
|
// invTree is an invocation tree (or a branch of it) for this context.
|
2022-07-08 17:49:21 +00:00
|
|
|
invTree *invocations.Tree
|
2022-05-23 08:35:01 +00:00
|
|
|
// onUnload is a callback that should be called after current context unloading
|
|
|
|
// if no exception occurs.
|
|
|
|
onUnload ContextUnloadCallback
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 13:15:51 +00:00
|
|
|
// Context represents the current execution context of the VM.
|
|
|
|
type Context struct {
|
|
|
|
// Instruction pointer.
|
|
|
|
ip int
|
|
|
|
|
|
|
|
// The next instruction pointer.
|
|
|
|
nextip int
|
|
|
|
|
|
|
|
sc *scriptContext
|
|
|
|
|
|
|
|
local slot
|
|
|
|
arguments slot
|
|
|
|
|
|
|
|
// Exception context stack.
|
|
|
|
tryStack Stack
|
|
|
|
|
|
|
|
// retCount specifies the number of return values.
|
|
|
|
retCount int
|
|
|
|
}
|
|
|
|
|
2022-11-15 21:40:12 +00:00
|
|
|
type contextAux struct {
|
|
|
|
Script string
|
|
|
|
IP int
|
|
|
|
NextIP int
|
|
|
|
Caller string
|
|
|
|
}
|
|
|
|
|
2022-05-23 08:35:01 +00:00
|
|
|
// ContextUnloadCallback is a callback method used on context unloading from istack.
|
2022-09-29 13:10:47 +00:00
|
|
|
type ContextUnloadCallback func(v *VM, ctx *Context, commit bool) error
|
2022-05-23 08:35:01 +00:00
|
|
|
|
2019-12-13 16:31:13 +00:00
|
|
|
var errNoInstParam = errors.New("failed to read instruction parameter")
|
|
|
|
|
2022-09-29 13:10:47 +00:00
|
|
|
// ErrMultiRet is returned when caller does not expect multiple return values
|
|
|
|
// from callee.
|
|
|
|
var ErrMultiRet = errors.New("multiple return values in a cross-contract call")
|
|
|
|
|
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 {
|
2022-05-25 07:00:02 +00:00
|
|
|
return NewContextWithParams(b, -1, 0)
|
2020-12-29 10:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContextWithParams creates new Context objects using script, parameter count,
|
|
|
|
// return value count and initial position in script.
|
2022-05-25 07:00:02 +00:00
|
|
|
func NewContextWithParams(b []byte, rvcount int, pos int) *Context {
|
2018-03-30 16:15:06 +00:00
|
|
|
return &Context{
|
2022-08-04 13:15:51 +00:00
|
|
|
sc: &scriptContext{
|
|
|
|
prog: b,
|
|
|
|
},
|
2022-05-25 07:00:02 +00:00
|
|
|
retCount: rvcount,
|
|
|
|
nextip: pos,
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:02:21 +00:00
|
|
|
// Estack returns the evaluation stack of c.
|
|
|
|
func (c *Context) Estack() *Stack {
|
2022-08-04 13:15:51 +00:00
|
|
|
return c.sc.estack
|
2020-11-19 15:02:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NextIP returns the next instruction pointer.
|
2020-02-03 09:10:07 +00:00
|
|
|
func (c *Context) NextIP() int {
|
|
|
|
return c.nextip
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Jump unconditionally moves the next instruction pointer to the specified location.
|
2021-11-19 13:46:29 +00:00
|
|
|
func (c *Context) Jump(pos int) {
|
2022-08-04 13:15:51 +00:00
|
|
|
if pos < 0 || pos >= len(c.sc.prog) {
|
2022-05-11 10:47:26 +00:00
|
|
|
panic("instruction offset is out of range")
|
|
|
|
}
|
2021-11-19 13:46:29 +00:00
|
|
|
c.nextip = pos
|
|
|
|
}
|
|
|
|
|
2021-08-10 12:31:16 +00:00
|
|
|
// Next returns the next instruction to execute with its parameter if any.
|
2022-04-20 18:30:09 +00:00
|
|
|
// The parameter is not copied and shouldn't be written to. After its invocation,
|
|
|
|
// the instruction pointer points to the instruction 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
|
2022-08-04 13:15:51 +00:00
|
|
|
prog := c.sc.prog
|
|
|
|
if c.ip >= len(prog) {
|
2019-12-03 14:05:06 +00:00
|
|
|
return opcode.RET, nil, nil
|
2019-10-03 13:54:14 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 13:15:51 +00:00
|
|
|
var instrbyte = prog[c.ip]
|
2019-12-03 14:05:06 +00:00
|
|
|
instr := opcode.Opcode(instrbyte)
|
2021-02-09 14:03:06 +00:00
|
|
|
if !opcode.IsValid(instr) {
|
|
|
|
return instr, nil, fmt.Errorf("incorrect opcode %s", instr.String())
|
|
|
|
}
|
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:
|
2022-08-04 13:15:51 +00:00
|
|
|
if c.nextip >= len(prog) {
|
2019-12-13 16:31:13 +00:00
|
|
|
err = errNoInstParam
|
|
|
|
} else {
|
2022-08-04 13:15:51 +00:00
|
|
|
numtoread = int(prog[c.nextip])
|
2019-12-13 16:31:13 +00:00
|
|
|
c.nextip++
|
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.PUSHDATA2:
|
2022-08-04 13:15:51 +00:00
|
|
|
if c.nextip+1 >= len(prog) {
|
2019-12-13 16:31:13 +00:00
|
|
|
err = errNoInstParam
|
|
|
|
} else {
|
2022-08-04 13:15:51 +00:00
|
|
|
numtoread = int(binary.LittleEndian.Uint16(prog[c.nextip : c.nextip+2]))
|
2019-12-13 16:31:13 +00:00
|
|
|
c.nextip += 2
|
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.PUSHDATA4:
|
2022-08-04 13:15:51 +00:00
|
|
|
if c.nextip+3 >= len(prog) {
|
2019-12-13 16:31:13 +00:00
|
|
|
err = errNoInstParam
|
|
|
|
} else {
|
2022-08-04 13:15:51 +00:00
|
|
|
var n = binary.LittleEndian.Uint32(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-12-29 09:04:53 +00:00
|
|
|
case opcode.INITSLOT, opcode.TRY, opcode.CALLT:
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-08-04 13:15:51 +00:00
|
|
|
if c.nextip+numtoread-1 >= len(prog) {
|
2019-12-13 16:31:13 +00:00
|
|
|
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
|
|
|
|
}
|
2022-08-04 13:15:51 +00:00
|
|
|
parameter := 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
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// IP returns the 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 {
|
2022-08-04 13:15:51 +00:00
|
|
|
return len(c.sc.prog)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CurrInstr returns the current instruction and opcode.
|
2019-12-03 14:05:06 +00:00
|
|
|
func (c *Context) CurrInstr() (int, opcode.Opcode) {
|
2022-08-04 13:15:51 +00:00
|
|
|
return c.ip, opcode.Opcode(c.sc.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
|
2022-08-04 13:15:51 +00:00
|
|
|
if c.nextip < len(c.sc.prog) {
|
|
|
|
op = opcode.Opcode(c.sc.prog[c.nextip])
|
2020-12-01 13:53:38 +00:00
|
|
|
}
|
|
|
|
return c.nextip, op
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetCallFlags returns the calling flags which the context was created with.
|
2020-12-29 10:45:49 +00:00
|
|
|
func (c *Context) GetCallFlags() callflag.CallFlag {
|
2022-08-04 13:15:51 +00:00
|
|
|
return c.sc.callFlag
|
2020-06-10 14:21:26 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// Program returns the loaded program.
|
|
|
|
func (c *Context) Program() []byte {
|
2022-08-04 13:15:51 +00:00
|
|
|
return c.sc.prog
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-08-04 13:15:51 +00:00
|
|
|
if c.sc.scriptHash.Equals(util.Uint160{}) {
|
|
|
|
c.sc.scriptHash = hash.Hash160(c.sc.prog)
|
2019-12-13 14:05:03 +00:00
|
|
|
}
|
2022-08-04 13:15:51 +00:00
|
|
|
return c.sc.scriptHash
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNEF returns NEF structure used by this context if it's present.
|
|
|
|
func (c *Context) GetNEF() *nef.File {
|
|
|
|
return c.sc.NEF
|
2019-12-13 14:05:03 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 15:17:32 +00:00
|
|
|
// NumOfReturnVals returns the number of return values expected from this context.
|
|
|
|
func (c *Context) NumOfReturnVals() int {
|
|
|
|
return c.retCount
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
func (c *Context) atBreakPoint() bool {
|
2022-08-04 13:15:51 +00:00
|
|
|
for _, n := range c.sc.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
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// IsDeployed returns whether this context contains a deployed contract.
|
2020-11-26 20:02:00 +00:00
|
|
|
func (c *Context) IsDeployed() bool {
|
2022-08-04 13:15:51 +00:00
|
|
|
return c.sc.NEF != nil
|
2020-11-26 20:02:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 14:27:11 +00:00
|
|
|
// DumpStaticSlot returns json formatted representation of the given slot.
|
|
|
|
func (c *Context) DumpStaticSlot() string {
|
2022-08-04 13:15:51 +00:00
|
|
|
return dumpSlot(&c.sc.static)
|
2021-09-08 14:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DumpLocalSlot returns json formatted representation of the given slot.
|
|
|
|
func (c *Context) DumpLocalSlot() string {
|
2021-11-30 12:03:01 +00:00
|
|
|
return dumpSlot(&c.local)
|
2021-09-08 14:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DumpArgumentsSlot returns json formatted representation of the given slot.
|
|
|
|
func (c *Context) DumpArgumentsSlot() string {
|
2021-11-30 12:03:01 +00:00
|
|
|
return dumpSlot(&c.arguments)
|
2021-09-08 14:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// dumpSlot returns json formatted representation of the given slot.
|
2021-11-30 12:03:01 +00:00
|
|
|
func dumpSlot(s *slot) string {
|
|
|
|
if s == nil || *s == nil {
|
|
|
|
return "[]"
|
|
|
|
}
|
2021-09-08 14:27:11 +00:00
|
|
|
b, _ := json.MarshalIndent(s, "", " ")
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-11-17 19:06:49 +00:00
|
|
|
if len(v.istack) <= n {
|
2020-04-30 16:19:31 +00:00
|
|
|
return util.Uint160{}
|
|
|
|
}
|
2022-11-17 19:06:49 +00:00
|
|
|
return v.istack[len(v.istack)-1-n].ScriptHash()
|
2020-04-13 12:37:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 13:35:02 +00:00
|
|
|
// IsCalledByEntry checks parent script contexts and return true if the current one
|
|
|
|
// is an entry script (the first loaded into the VM) or one called by it.
|
|
|
|
func (c *Context) IsCalledByEntry() bool {
|
|
|
|
return c.sc.callingContext == nil || c.sc.callingContext.callingContext == nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// PushContextScriptHash pushes the script hash of the
|
|
|
|
// invocation stack element number n to the evaluation stack.
|
2020-04-13 12:37:44 +00:00
|
|
|
func (v *VM) PushContextScriptHash(n int) error {
|
2020-05-04 08:41:41 +00:00
|
|
|
h := v.getContextScriptHash(n)
|
2021-08-28 19:35:43 +00:00
|
|
|
v.Estack().PushItem(stackitem.NewByteArray(h.BytesBE()))
|
2020-04-13 12:37:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-05-25 07:00:02 +00:00
|
|
|
|
|
|
|
func (c *Context) HasTryBlock() bool {
|
|
|
|
for i := 0; i < c.tryStack.Len(); i++ {
|
|
|
|
eCtx := c.tryStack.Peek(i).Value().(*exceptionHandlingContext)
|
|
|
|
if eCtx.State == eTry {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-11-15 21:40:12 +00:00
|
|
|
|
|
|
|
// MarshalJSON implements the JSON marshalling interface.
|
|
|
|
func (c *Context) MarshalJSON() ([]byte, error) {
|
|
|
|
var aux = contextAux{
|
|
|
|
Script: c.ScriptHash().StringLE(),
|
|
|
|
IP: c.ip,
|
|
|
|
NextIP: c.nextip,
|
|
|
|
Caller: c.sc.callingScriptHash.StringLE(),
|
|
|
|
}
|
|
|
|
return json.Marshal(aux)
|
|
|
|
}
|
2022-09-29 13:10:47 +00:00
|
|
|
|
|
|
|
// DynamicOnUnload implements OnUnload script for dynamic calls, if no exception
|
|
|
|
// has occurred it checks that the context has exactly 0 (in which case a `Null`
|
|
|
|
// is pushed) or 1 returned value.
|
|
|
|
func DynamicOnUnload(v *VM, ctx *Context, commit bool) error {
|
|
|
|
if commit {
|
|
|
|
eLen := ctx.Estack().Len()
|
|
|
|
if eLen == 0 { // No return value, add one.
|
|
|
|
v.Context().Estack().PushItem(stackitem.Null{}) // Must use current context stack.
|
|
|
|
} else if eLen > 1 { // Only one can be returned.
|
|
|
|
return ErrMultiRet
|
|
|
|
} // One value returned, it's OK.
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|