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-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
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
|
|
|
|
|
|
|
// Return value count, -1 is unspecified.
|
|
|
|
rvcount int
|
|
|
|
|
|
|
|
// Evaluation stack pointer.
|
|
|
|
estack *Stack
|
|
|
|
|
|
|
|
// Alt stack pointer.
|
|
|
|
astack *Stack
|
2019-12-13 14:05:03 +00:00
|
|
|
|
|
|
|
// Script hash of the prog.
|
|
|
|
scriptHash util.Uint160
|
2020-03-10 14:17:36 +00:00
|
|
|
|
|
|
|
// Whether it's allowed to make dynamic calls from this context.
|
|
|
|
hasDynamicInvoke bool
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
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{},
|
2019-10-25 14:25:46 +00:00
|
|
|
rvcount: -1,
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.PUSHDATA1, opcode.SYSCALL:
|
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])
|
|
|
|
if n > MaxItemSize {
|
|
|
|
return instr, nil, errors.New("parameter is too big")
|
|
|
|
}
|
|
|
|
numtoread = int(n)
|
|
|
|
c.nextip += 4
|
2019-10-17 14:10:00 +00:00
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.JMP, opcode.JMPIF, opcode.JMPIFNOT, opcode.CALL, opcode.CALLED, opcode.CALLEDT:
|
2019-10-03 13:54:14 +00:00
|
|
|
numtoread = 2
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.CALLI:
|
2019-10-25 14:25:46 +00:00
|
|
|
numtoread = 4
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.APPCALL, opcode.TAILCALL:
|
2019-10-03 13:54:14 +00:00
|
|
|
numtoread = 20
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.CALLE, opcode.CALLET:
|
2019-10-25 14:25:46 +00:00
|
|
|
numtoread = 22
|
2019-10-03 13:54:14 +00:00
|
|
|
default:
|
2019-12-03 14:05:06 +00:00
|
|
|
if instr >= opcode.PUSHBYTES1 && instr <= opcode.PUSHBYTES75 {
|
2019-10-03 13:54:14 +00:00
|
|
|
numtoread = int(instr)
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2018-04-04 19:41:19 +00:00
|
|
|
// IP returns the absolute instruction without taking 0 into account.
|
2018-03-30 16:15:06 +00:00
|
|
|
// If that program starts the ip = 0 but IP() will return 1, cause its
|
|
|
|
// the first instruction.
|
|
|
|
func (c *Context) IP() int {
|
|
|
|
return c.ip + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// Value implements StackItem interface.
|
|
|
|
func (c *Context) Value() interface{} {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-12-17 13:38:42 +00:00
|
|
|
// Dup implements StackItem interface.
|
|
|
|
func (c *Context) Dup() StackItem {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:04:28 +00:00
|
|
|
// TryBytes implements StackItem interface.
|
|
|
|
func (c *Context) TryBytes() ([]byte, error) {
|
|
|
|
return nil, errors.New("can't convert Context to ByteArray")
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:44:10 +00:00
|
|
|
// Equals implements StackItem interface.
|
|
|
|
func (c *Context) Equals(s StackItem) bool {
|
|
|
|
return c == s
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:56:28 +00:00
|
|
|
// ToContractParameter implements StackItem interface.
|
2020-03-03 09:52:56 +00:00
|
|
|
func (c *Context) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
|
2020-03-03 10:05:57 +00:00
|
|
|
return smartcontract.Parameter{
|
|
|
|
Type: smartcontract.StringType,
|
|
|
|
Value: c.String(),
|
|
|
|
}
|
2020-02-21 14:56:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
func (c *Context) atBreakPoint() bool {
|
|
|
|
for _, n := range c.breakPoints {
|
|
|
|
if n == c.ip {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) String() string {
|
|
|
|
return "execution context"
|
|
|
|
}
|