vm: add instruction correctness check

See neo-project/neo-vm#392.
This commit is contained in:
Roman Khimov 2021-02-09 17:03:06 +03:00
parent a3abdbd7f0
commit b892db9976
3 changed files with 17 additions and 0 deletions

View file

@ -3,6 +3,7 @@ package vm
import (
"encoding/binary"
"errors"
"fmt"
"math/big"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
@ -109,6 +110,9 @@ func (c *Context) Next() (opcode.Opcode, []byte, error) {
var instrbyte = c.prog[c.ip]
instr := opcode.Opcode(instrbyte)
if !opcode.IsValid(instr) {
return instr, nil, fmt.Errorf("incorrect opcode %s", instr.String())
}
c.nextip++
var numtoread int

View file

@ -219,3 +219,9 @@ const (
ISTYPE Opcode = 0xD9
CONVERT Opcode = 0xDB
)
// IsValid returns true if the opcode passed is valid (defined in the VM).
func IsValid(op Opcode) bool {
_, ok := _Opcode_map[op] // We rely on stringer here, it has a map anyway.
return ok
}

View file

@ -28,3 +28,10 @@ func TestFromString(t *testing.T) {
require.NoError(t, err)
require.Equal(t, MUL, op)
}
func TestIsValid(t *testing.T) {
require.True(t, IsValid(ADD))
require.True(t, IsValid(CONVERT))
require.False(t, IsValid(0xff))
require.False(t, IsValid(0xa5))
}