2018-03-30 16:15:06 +00:00
|
|
|
package vm
|
|
|
|
|
2018-04-10 09:45:31 +00:00
|
|
|
import (
|
2019-11-05 08:36:13 +00:00
|
|
|
"errors"
|
2018-04-10 09:45:31 +00:00
|
|
|
"fmt"
|
2019-12-18 16:49:56 +00:00
|
|
|
"sort"
|
2020-04-15 14:13:50 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2018-04-10 09:45:31 +00:00
|
|
|
)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
|
|
|
// InteropFunc allows to hook into the VM.
|
|
|
|
type InteropFunc func(vm *VM) error
|
|
|
|
|
2019-12-18 16:49:56 +00:00
|
|
|
// InteropFuncPrice represents an interop function with a price.
|
|
|
|
type InteropFuncPrice struct {
|
|
|
|
Func InteropFunc
|
|
|
|
Price int
|
|
|
|
}
|
|
|
|
|
|
|
|
// interopIDFuncPrice adds an ID to the InteropFuncPrice.
|
|
|
|
type interopIDFuncPrice struct {
|
|
|
|
ID uint32
|
|
|
|
InteropFuncPrice
|
|
|
|
}
|
|
|
|
|
|
|
|
// InteropGetterFunc is a function that returns an interop function-price
|
|
|
|
// structure by the given interop ID.
|
|
|
|
type InteropGetterFunc func(uint32) *InteropFuncPrice
|
|
|
|
|
|
|
|
var defaultVMInterops = []interopIDFuncPrice{
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("Neo.Runtime.Log")),
|
2019-12-18 16:49:56 +00:00
|
|
|
InteropFuncPrice{runtimeLog, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("Neo.Runtime.Notify")),
|
2019-12-18 16:49:56 +00:00
|
|
|
InteropFuncPrice{runtimeNotify, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("Neo.Runtime.Serialize")),
|
2019-12-18 16:49:56 +00:00
|
|
|
InteropFuncPrice{RuntimeSerialize, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("System.Runtime.Serialize")),
|
2019-12-18 16:49:56 +00:00
|
|
|
InteropFuncPrice{RuntimeSerialize, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("Neo.Runtime.Deserialize")),
|
2019-12-18 16:49:56 +00:00
|
|
|
InteropFuncPrice{RuntimeDeserialize, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("System.Runtime.Deserialize")),
|
2019-12-18 16:49:56 +00:00
|
|
|
InteropFuncPrice{RuntimeDeserialize, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("Neo.Enumerator.Create")),
|
2019-11-13 11:34:03 +00:00
|
|
|
InteropFuncPrice{EnumeratorCreate, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("Neo.Enumerator.Next")),
|
2019-11-13 11:34:03 +00:00
|
|
|
InteropFuncPrice{EnumeratorNext, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("Neo.Enumerator.Concat")),
|
2019-11-13 11:34:03 +00:00
|
|
|
InteropFuncPrice{EnumeratorConcat, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("Neo.Enumerator.Value")),
|
2019-11-13 11:34:03 +00:00
|
|
|
InteropFuncPrice{EnumeratorValue, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("Neo.Iterator.Create")),
|
2019-11-13 12:29:27 +00:00
|
|
|
InteropFuncPrice{IteratorCreate, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("Neo.Iterator.Concat")),
|
2019-11-13 12:29:27 +00:00
|
|
|
InteropFuncPrice{IteratorConcat, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("Neo.Iterator.Key")),
|
2019-11-13 12:29:27 +00:00
|
|
|
InteropFuncPrice{IteratorKey, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("Neo.Iterator.Keys")),
|
2019-11-13 12:29:27 +00:00
|
|
|
InteropFuncPrice{IteratorKeys, 1}},
|
2020-04-15 14:13:50 +00:00
|
|
|
{emit.InteropNameToID([]byte("Neo.Iterator.Values")),
|
2019-11-13 12:29:27 +00:00
|
|
|
InteropFuncPrice{IteratorValues, 1}},
|
2019-12-18 16:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getDefaultVMInterop(id uint32) *InteropFuncPrice {
|
|
|
|
n := sort.Search(len(defaultVMInterops), func(i int) bool {
|
|
|
|
return defaultVMInterops[i].ID >= id
|
|
|
|
})
|
|
|
|
if n < len(defaultVMInterops) && defaultVMInterops[n].ID == id {
|
|
|
|
return &defaultVMInterops[n].InteropFuncPrice
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// runtimeLog handles the syscall "Neo.Runtime.Log" for printing and logging stuff.
|
2018-04-10 09:45:31 +00:00
|
|
|
func runtimeLog(vm *VM) error {
|
|
|
|
item := vm.Estack().Pop()
|
2019-10-03 13:12:24 +00:00
|
|
|
fmt.Printf("NEO-GO-VM (log) > %s\n", item.Value())
|
2018-04-10 09:45:31 +00:00
|
|
|
return nil
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// runtimeNotify handles the syscall "Neo.Runtime.Notify" for printing and logging stuff.
|
2018-04-10 09:45:31 +00:00
|
|
|
func runtimeNotify(vm *VM) error {
|
|
|
|
item := vm.Estack().Pop()
|
2019-10-03 13:12:24 +00:00
|
|
|
fmt.Printf("NEO-GO-VM (notify) > %s\n", item.Value())
|
2018-04-10 09:45:31 +00:00
|
|
|
return nil
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
2019-11-05 08:36:13 +00:00
|
|
|
|
2019-11-05 14:10:52 +00:00
|
|
|
// RuntimeSerialize handles syscalls System.Runtime.Serialize and Neo.Runtime.Serialize.
|
|
|
|
func RuntimeSerialize(vm *VM) error {
|
2019-11-05 08:36:13 +00:00
|
|
|
item := vm.Estack().Pop()
|
2020-06-03 12:55:06 +00:00
|
|
|
data, err := stackitem.SerializeItem(item.value)
|
2019-11-05 08:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(data) > MaxItemSize {
|
|
|
|
return errors.New("too big item")
|
|
|
|
}
|
|
|
|
|
|
|
|
vm.Estack().PushVal(data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 14:10:52 +00:00
|
|
|
// RuntimeDeserialize handles syscalls System.Runtime.Deserialize and Neo.Runtime.Deserialize.
|
|
|
|
func RuntimeDeserialize(vm *VM) error {
|
2019-11-05 08:36:13 +00:00
|
|
|
data := vm.Estack().Pop().Bytes()
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
item, err := stackitem.DeserializeItem(data)
|
2019-11-05 08:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vm.Estack().Push(&Element{value: item})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-18 16:49:56 +00:00
|
|
|
|
|
|
|
// init sorts the global defaultVMInterops value.
|
|
|
|
func init() {
|
|
|
|
sort.Slice(defaultVMInterops, func(i, j int) bool {
|
|
|
|
return defaultVMInterops[i].ID < defaultVMInterops[j].ID
|
|
|
|
})
|
|
|
|
}
|
2019-11-13 11:34:03 +00:00
|
|
|
|
|
|
|
// EnumeratorCreate handles syscall Neo.Enumerator.Create.
|
|
|
|
func EnumeratorCreate(v *VM) error {
|
|
|
|
data := v.Estack().Pop().Array()
|
|
|
|
v.Estack().Push(&Element{
|
2020-06-03 12:55:06 +00:00
|
|
|
value: stackitem.NewInterop(&arrayWrapper{
|
2019-11-13 11:34:03 +00:00
|
|
|
index: -1,
|
|
|
|
value: data,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnumeratorNext handles syscall Neo.Enumerator.Next.
|
|
|
|
func EnumeratorNext(v *VM) error {
|
|
|
|
iop := v.Estack().Pop().Interop()
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := iop.Value().(enumerator)
|
2019-11-13 11:34:03 +00:00
|
|
|
v.Estack().PushVal(arr.Next())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnumeratorValue handles syscall Neo.Enumerator.Value.
|
|
|
|
func EnumeratorValue(v *VM) error {
|
|
|
|
iop := v.Estack().Pop().Interop()
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := iop.Value().(enumerator)
|
2019-11-13 11:34:03 +00:00
|
|
|
v.Estack().Push(&Element{value: arr.Value()})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnumeratorConcat handles syscall Neo.Enumerator.Concat.
|
|
|
|
func EnumeratorConcat(v *VM) error {
|
|
|
|
iop1 := v.Estack().Pop().Interop()
|
2020-06-03 12:55:06 +00:00
|
|
|
arr1 := iop1.Value().(enumerator)
|
2019-11-13 11:34:03 +00:00
|
|
|
iop2 := v.Estack().Pop().Interop()
|
2020-06-03 12:55:06 +00:00
|
|
|
arr2 := iop2.Value().(enumerator)
|
2019-11-13 11:34:03 +00:00
|
|
|
|
|
|
|
v.Estack().Push(&Element{
|
2020-06-03 12:55:06 +00:00
|
|
|
value: stackitem.NewInterop(&concatEnum{
|
2019-11-13 11:34:03 +00:00
|
|
|
current: arr1,
|
|
|
|
second: arr2,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-13 12:29:27 +00:00
|
|
|
|
|
|
|
// IteratorCreate handles syscall Neo.Iterator.Create.
|
|
|
|
func IteratorCreate(v *VM) error {
|
|
|
|
data := v.Estack().Pop()
|
2020-06-03 12:55:06 +00:00
|
|
|
var item stackitem.Item
|
2019-11-13 12:29:27 +00:00
|
|
|
switch t := data.value.(type) {
|
2020-06-03 12:55:06 +00:00
|
|
|
case *stackitem.Array, *stackitem.Struct:
|
|
|
|
item = stackitem.NewInterop(&arrayWrapper{
|
2019-11-13 12:29:27 +00:00
|
|
|
index: -1,
|
2020-06-03 12:55:06 +00:00
|
|
|
value: t.Value().([]stackitem.Item),
|
2019-12-26 11:34:15 +00:00
|
|
|
})
|
2020-06-03 12:55:06 +00:00
|
|
|
case *stackitem.Map:
|
2020-03-31 10:30:38 +00:00
|
|
|
item = NewMapIterator(t)
|
2019-11-13 12:29:27 +00:00
|
|
|
default:
|
|
|
|
return errors.New("non-iterable type")
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:34:15 +00:00
|
|
|
v.Estack().Push(&Element{value: item})
|
2019-11-13 12:29:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:34:15 +00:00
|
|
|
// NewMapIterator returns new interop item containing iterator over m.
|
2020-06-03 12:55:06 +00:00
|
|
|
func NewMapIterator(m *stackitem.Map) *stackitem.Interop {
|
|
|
|
return stackitem.NewInterop(&mapWrapper{
|
2019-12-26 11:34:15 +00:00
|
|
|
index: -1,
|
2020-06-03 12:55:06 +00:00
|
|
|
m: m.Value().([]stackitem.MapElement),
|
2019-12-26 11:34:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-13 12:29:27 +00:00
|
|
|
// IteratorConcat handles syscall Neo.Iterator.Concat.
|
|
|
|
func IteratorConcat(v *VM) error {
|
|
|
|
iop1 := v.Estack().Pop().Interop()
|
2020-06-03 12:55:06 +00:00
|
|
|
iter1 := iop1.Value().(iterator)
|
2019-11-13 12:29:27 +00:00
|
|
|
iop2 := v.Estack().Pop().Interop()
|
2020-06-03 12:55:06 +00:00
|
|
|
iter2 := iop2.Value().(iterator)
|
2019-11-13 12:29:27 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
v.Estack().Push(&Element{value: stackitem.NewInterop(
|
2019-11-13 12:29:27 +00:00
|
|
|
&concatIter{
|
|
|
|
current: iter1,
|
|
|
|
second: iter2,
|
|
|
|
},
|
|
|
|
)})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IteratorKey handles syscall Neo.Iterator.Key.
|
|
|
|
func IteratorKey(v *VM) error {
|
|
|
|
iop := v.estack.Pop().Interop()
|
2020-06-03 12:55:06 +00:00
|
|
|
iter := iop.Value().(iterator)
|
2019-11-13 12:29:27 +00:00
|
|
|
v.Estack().Push(&Element{value: iter.Key()})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IteratorKeys handles syscall Neo.Iterator.Keys.
|
|
|
|
func IteratorKeys(v *VM) error {
|
|
|
|
iop := v.estack.Pop().Interop()
|
2020-06-03 12:55:06 +00:00
|
|
|
iter := iop.Value().(iterator)
|
|
|
|
v.Estack().Push(&Element{value: stackitem.NewInterop(
|
2019-11-13 12:29:27 +00:00
|
|
|
&keysWrapper{iter},
|
|
|
|
)})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IteratorValues handles syscall Neo.Iterator.Values.
|
|
|
|
func IteratorValues(v *VM) error {
|
|
|
|
iop := v.estack.Pop().Interop()
|
2020-06-03 12:55:06 +00:00
|
|
|
iter := iop.Value().(iterator)
|
|
|
|
v.Estack().Push(&Element{value: stackitem.NewInterop(
|
2019-11-13 12:29:27 +00:00
|
|
|
&valuesWrapper{iter},
|
|
|
|
)})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|