2019-11-06 09:15:30 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
2020-04-15 13:24:50 +00:00
|
|
|
"encoding/binary"
|
2019-11-06 09:15:30 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-05-20 12:01:06 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2019-11-06 09:15:30 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2019-11-06 09:15:30 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
vmUT struct {
|
|
|
|
Category string `json:"category"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Tests []vmUTEntry `json:"tests"`
|
|
|
|
}
|
|
|
|
|
|
|
|
vmUTActionType string
|
|
|
|
|
|
|
|
vmUTEntry struct {
|
|
|
|
Name string
|
|
|
|
Script vmUTScript
|
|
|
|
Steps []vmUTStep
|
|
|
|
}
|
|
|
|
|
|
|
|
vmUTExecutionContextState struct {
|
|
|
|
Instruction string `json:"nextInstruction"`
|
|
|
|
InstructionPointer int `json:"instructionPointer"`
|
|
|
|
AStack []vmUTStackItem `json:"altStack"`
|
|
|
|
EStack []vmUTStackItem `json:"evaluationStack"`
|
|
|
|
}
|
|
|
|
|
|
|
|
vmUTExecutionEngineState struct {
|
2020-05-20 12:02:01 +00:00
|
|
|
State State `json:"state"`
|
2019-11-06 09:15:30 +00:00
|
|
|
ResultStack []vmUTStackItem `json:"resultStack"`
|
|
|
|
InvocationStack []vmUTExecutionContextState `json:"invocationStack"`
|
|
|
|
}
|
|
|
|
|
|
|
|
vmUTScript []byte
|
|
|
|
|
|
|
|
vmUTStackItem struct {
|
|
|
|
Type vmUTStackItemType
|
|
|
|
Value interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
vmUTStep struct {
|
|
|
|
Actions []vmUTActionType `json:"actions"`
|
|
|
|
Result vmUTExecutionEngineState `json:"result"`
|
|
|
|
}
|
|
|
|
|
|
|
|
vmUTStackItemType string
|
|
|
|
)
|
|
|
|
|
|
|
|
// stackItemAUX is used as an intermediate structure
|
|
|
|
// to conditionally unmarshal vmUTStackItem based
|
|
|
|
// on the value of Type field.
|
|
|
|
type stackItemAUX struct {
|
|
|
|
Type vmUTStackItemType `json:"type"`
|
|
|
|
Value json.RawMessage `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
vmExecute vmUTActionType = "Execute"
|
|
|
|
vmStepInto vmUTActionType = "StepInto"
|
|
|
|
vmStepOut vmUTActionType = "StepOut"
|
|
|
|
vmStepOver vmUTActionType = "StepOver"
|
|
|
|
|
|
|
|
typeArray vmUTStackItemType = "Array"
|
|
|
|
typeBoolean vmUTStackItemType = "Boolean"
|
|
|
|
typeByteArray vmUTStackItemType = "ByteArray"
|
|
|
|
typeInteger vmUTStackItemType = "Integer"
|
|
|
|
typeInterop vmUTStackItemType = "Interop"
|
|
|
|
typeMap vmUTStackItemType = "Map"
|
|
|
|
typeString vmUTStackItemType = "String"
|
|
|
|
typeStruct vmUTStackItemType = "Struct"
|
|
|
|
|
2019-12-04 17:30:27 +00:00
|
|
|
testsDir = "testdata/neo-vm/tests/neo-vm.Tests/Tests/"
|
2019-11-06 09:15:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUT(t *testing.T) {
|
2020-04-15 13:24:50 +00:00
|
|
|
t.Skip()
|
2019-12-04 17:30:27 +00:00
|
|
|
testsRan := false
|
2019-11-06 09:15:30 +00:00
|
|
|
err := filepath.Walk(testsDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if !strings.HasSuffix(path, ".json") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
testFile(t, path)
|
2019-12-04 17:30:27 +00:00
|
|
|
testsRan = true
|
2019-11-06 09:15:30 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
2019-12-04 17:30:27 +00:00
|
|
|
require.Equal(t, true, testsRan, "neo-vm tests should be available (check submodules)")
|
2019-11-06 09:15:30 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 16:49:56 +00:00
|
|
|
func getTestingInterop(id uint32) *InteropFuncPrice {
|
2020-04-15 13:24:50 +00:00
|
|
|
if id == binary.LittleEndian.Uint32([]byte{0x77, 0x77, 0x77, 0x77}) {
|
2019-12-18 16:49:56 +00:00
|
|
|
return &InteropFuncPrice{InteropFunc(func(v *VM) error {
|
|
|
|
v.estack.Push(&Element{value: (*InteropItem)(nil)})
|
|
|
|
return nil
|
|
|
|
}), 0}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-06 09:15:30 +00:00
|
|
|
func testFile(t *testing.T, filename string) {
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-06 09:15:30 +00:00
|
|
|
|
|
|
|
ut := new(vmUT)
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, json.Unmarshal(data, ut))
|
2019-11-06 09:15:30 +00:00
|
|
|
|
|
|
|
t.Run(ut.Category+":"+ut.Name, func(t *testing.T) {
|
|
|
|
for i := range ut.Tests {
|
|
|
|
test := ut.Tests[i]
|
|
|
|
t.Run(ut.Tests[i].Name, func(t *testing.T) {
|
|
|
|
prog := []byte(test.Script)
|
|
|
|
vm := load(prog)
|
|
|
|
vm.state = breakState
|
2019-12-18 16:49:56 +00:00
|
|
|
vm.RegisterInteropGetter(getTestingInterop)
|
2019-11-06 09:15:30 +00:00
|
|
|
|
|
|
|
for i := range test.Steps {
|
|
|
|
execStep(t, vm, test.Steps[i])
|
|
|
|
result := test.Steps[i].Result
|
2020-05-20 12:02:01 +00:00
|
|
|
require.Equal(t, result.State, vm.state)
|
|
|
|
if result.State == faultState { // do not compare stacks on fault
|
2019-11-06 09:15:30 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result.InvocationStack) > 0 {
|
|
|
|
for i, s := range result.InvocationStack {
|
|
|
|
ctx := vm.istack.Peek(i).Value().(*Context)
|
|
|
|
if ctx.nextip < len(ctx.prog) {
|
|
|
|
require.Equal(t, s.InstructionPointer, ctx.nextip)
|
2020-05-20 12:01:06 +00:00
|
|
|
op, err := opcode.FromString(s.Instruction)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, op, opcode.Opcode(ctx.prog[ctx.nextip]))
|
2019-11-06 09:15:30 +00:00
|
|
|
}
|
|
|
|
compareStacks(t, s.EStack, vm.estack)
|
|
|
|
compareStacks(t, s.AStack, vm.astack)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result.ResultStack) != 0 {
|
|
|
|
compareStacks(t, result.ResultStack, vm.estack)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareItems(t *testing.T, a, b StackItem) {
|
|
|
|
switch si := a.(type) {
|
|
|
|
case *BigIntegerItem:
|
|
|
|
val := si.value.Int64()
|
|
|
|
switch ac := b.(type) {
|
|
|
|
case *BigIntegerItem:
|
|
|
|
require.Equal(t, val, ac.value.Int64())
|
|
|
|
case *ByteArrayItem:
|
2020-02-03 15:05:13 +00:00
|
|
|
require.Equal(t, val, emit.BytesToInt(ac.value).Int64())
|
2019-11-06 09:15:30 +00:00
|
|
|
case *BoolItem:
|
|
|
|
if ac.value {
|
|
|
|
require.Equal(t, val, int64(1))
|
|
|
|
} else {
|
|
|
|
require.Equal(t, val, int64(0))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
require.Fail(t, "wrong type")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
require.Equal(t, a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareStacks(t *testing.T, expected []vmUTStackItem, actual *Stack) {
|
|
|
|
if expected == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, len(expected), actual.Len())
|
|
|
|
for i, item := range expected {
|
|
|
|
e := actual.Peek(i)
|
|
|
|
require.NotNil(t, e)
|
|
|
|
|
|
|
|
if item.Type == typeInterop {
|
|
|
|
require.IsType(t, (*InteropItem)(nil), e.value)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
compareItems(t, item.toStackItem(), e.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vmUTStackItem) toStackItem() StackItem {
|
|
|
|
switch v.Type {
|
|
|
|
case typeArray:
|
|
|
|
items := v.Value.([]vmUTStackItem)
|
|
|
|
result := make([]StackItem, len(items))
|
|
|
|
for i := range items {
|
|
|
|
result[i] = items[i].toStackItem()
|
|
|
|
}
|
|
|
|
return &ArrayItem{
|
|
|
|
value: result,
|
|
|
|
}
|
|
|
|
case typeString:
|
|
|
|
panic("not implemented")
|
|
|
|
case typeMap:
|
|
|
|
items := v.Value.(map[string]vmUTStackItem)
|
|
|
|
result := NewMapItem()
|
|
|
|
for k, v := range items {
|
|
|
|
var item vmUTStackItem
|
|
|
|
_ = json.Unmarshal([]byte(`"`+k+`"`), &item)
|
|
|
|
result.Add(item.toStackItem(), v.toStackItem())
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
case typeInterop:
|
|
|
|
panic("not implemented")
|
|
|
|
case typeByteArray:
|
|
|
|
return &ByteArrayItem{
|
|
|
|
v.Value.([]byte),
|
|
|
|
}
|
|
|
|
case typeBoolean:
|
|
|
|
return &BoolItem{
|
|
|
|
v.Value.(bool),
|
|
|
|
}
|
|
|
|
case typeInteger:
|
|
|
|
return &BigIntegerItem{
|
|
|
|
value: v.Value.(*big.Int),
|
|
|
|
}
|
|
|
|
case typeStruct:
|
|
|
|
items := v.Value.([]vmUTStackItem)
|
|
|
|
result := make([]StackItem, len(items))
|
|
|
|
for i := range items {
|
|
|
|
result[i] = items[i].toStackItem()
|
|
|
|
}
|
|
|
|
return &StructItem{
|
|
|
|
value: result,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("invalid type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func execStep(t *testing.T, v *VM, step vmUTStep) {
|
|
|
|
for i, a := range step.Actions {
|
|
|
|
var err error
|
|
|
|
switch a {
|
|
|
|
case vmExecute:
|
|
|
|
err = v.Run()
|
|
|
|
case vmStepInto:
|
|
|
|
err = v.StepInto()
|
|
|
|
case vmStepOut:
|
|
|
|
err = v.StepOut()
|
|
|
|
case vmStepOver:
|
|
|
|
err = v.StepOver()
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("invalid action: %s", a))
|
|
|
|
}
|
|
|
|
|
|
|
|
// only the last action is allowed to fail
|
|
|
|
if i+1 < len(step.Actions) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vmUTScript) UnmarshalJSON(data []byte) error {
|
2020-05-20 12:01:06 +00:00
|
|
|
var ops []string
|
|
|
|
if err := json.Unmarshal(data, &ops); err != nil {
|
2019-11-06 09:15:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:01:06 +00:00
|
|
|
var script []byte
|
|
|
|
for i := range ops {
|
|
|
|
if b, ok := decodeSingle(ops[i]); ok {
|
|
|
|
script = append(script, b...)
|
|
|
|
} else {
|
|
|
|
const regex = `(?P<hex>(?:0x)?[0-9a-zA-Z]+)\*(?P<num>[0-9]+)`
|
|
|
|
re := regexp.MustCompile(regex)
|
|
|
|
ss := re.FindStringSubmatch(ops[i])
|
|
|
|
if len(ss) != 3 {
|
|
|
|
return fmt.Errorf("invalid script part: %s", ops[i])
|
|
|
|
}
|
|
|
|
b, ok := decodeSingle(ss[1])
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid script part: %s", ops[i])
|
|
|
|
}
|
|
|
|
num, err := strconv.Atoi(ss[2])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid script part: %s", ops[i])
|
|
|
|
}
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
script = append(script, b...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*v = script
|
2019-11-06 09:15:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:01:06 +00:00
|
|
|
func decodeSingle(s string) ([]byte, bool) {
|
|
|
|
if op, err := opcode.FromString(s); err == nil {
|
|
|
|
return []byte{byte(op)}, true
|
|
|
|
}
|
|
|
|
b, err := decodeHex(s)
|
|
|
|
return b, err == nil
|
|
|
|
}
|
|
|
|
|
2019-11-06 09:15:30 +00:00
|
|
|
func (v *vmUTActionType) UnmarshalJSON(data []byte) error {
|
|
|
|
return json.Unmarshal(data, (*string)(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vmUTStackItem) UnmarshalJSON(data []byte) error {
|
|
|
|
var si stackItemAUX
|
|
|
|
if err := json.Unmarshal(data, &si); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Type = si.Type
|
|
|
|
|
|
|
|
switch si.Type {
|
|
|
|
case typeArray, typeStruct:
|
|
|
|
var a []vmUTStackItem
|
|
|
|
if err := json.Unmarshal(si.Value, &a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Value = a
|
|
|
|
case typeInteger:
|
|
|
|
num := new(big.Int)
|
|
|
|
var a int64
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(si.Value, &a); err == nil {
|
|
|
|
num.SetInt64(a)
|
|
|
|
} else if err := json.Unmarshal(si.Value, &s); err == nil {
|
|
|
|
num.SetString(s, 10)
|
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("invalid integer: %v", si.Value))
|
|
|
|
}
|
|
|
|
v.Value = num
|
|
|
|
case typeBoolean:
|
|
|
|
var b bool
|
|
|
|
if err := json.Unmarshal(si.Value, &b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Value = b
|
|
|
|
case typeByteArray:
|
|
|
|
b, err := decodeBytes(si.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Value = b
|
|
|
|
case typeInterop:
|
|
|
|
v.Value = nil
|
|
|
|
case typeMap:
|
|
|
|
var m map[string]vmUTStackItem
|
|
|
|
if err := json.Unmarshal(si.Value, &m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Value = m
|
|
|
|
case typeString:
|
|
|
|
panic("not implemented")
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown type: %s", si.Type))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// decodeBytes tries to decode bytes from string.
|
|
|
|
// It tries hex and base64 encodings.
|
|
|
|
func decodeBytes(data []byte) ([]byte, error) {
|
|
|
|
if len(data) == 2 {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:01:06 +00:00
|
|
|
data = data[1 : len(data)-1] // strip quotes
|
|
|
|
if b, err := decodeHex(string(data)); err == nil {
|
2019-11-06 09:15:30 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r := base64.NewDecoder(base64.StdEncoding, bytes.NewReader(data))
|
|
|
|
return ioutil.ReadAll(r)
|
|
|
|
}
|
2020-05-20 12:01:06 +00:00
|
|
|
|
|
|
|
func decodeHex(s string) ([]byte, error) {
|
|
|
|
if strings.HasPrefix(s, "0x") {
|
|
|
|
s = s[2:]
|
|
|
|
}
|
|
|
|
return hex.DecodeString(s)
|
|
|
|
}
|