Skip to content

Commit

Permalink
vm: add instruction correctness check
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-khimov committed Feb 9, 2021
1 parent a3abdbd commit b892db9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/vm/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vm
import (
"encoding/binary"
"errors"
"fmt"
"math/big"

"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/vm/opcode/opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions pkg/vm/opcode/opcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit b892db9

Please sign in to comment.