vm: add some Fuzz tests

Both `IsScriptCorrect` and `VM.Run` should never panic.

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2022-03-18 15:15:54 +03:00
parent 4869049965
commit 3f65473f64
2 changed files with 68 additions and 0 deletions

14
.gitignore vendored
View file

@ -42,3 +42,17 @@ coverage.html
# Compiler output
examples/*/*.nef
examples/*/*.json
# Fuzzing testdata.
testdata/
!cli/testdata
!pkg/compiler/testdata
!pkg/config/testdata
!pkg/consensus/testdata
!pkg/rpc/server/testdata
!pkg/services/notary/testdata
!pkg/services/oracle/testdata
!pkg/smartcontract/testdata
pkg/vm/testdata/fuzz
!pkg/vm/testdata
!pkg/wallet/testdata

54
pkg/vm/fuzz_test.go Normal file
View file

@ -0,0 +1,54 @@
//go:build go1.18
// +build go1.18
package vm
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/stretchr/testify/require"
)
var fuzzSeedValidScripts = [][]byte{
makeProgram(opcode.PUSH1, opcode.PUSH10, opcode.ADD),
makeProgram(opcode.PUSH10, opcode.JMP, 3, opcode.ABORT, opcode.RET),
makeProgram(opcode.PUSHINT16, 1, 2, opcode.PUSHINT32, 3, 4, opcode.DROP),
makeProgram(opcode.PUSH2, opcode.NEWARRAY, opcode.DUP, opcode.PUSH0, opcode.PUSH1, opcode.SETITEM, opcode.VALUES),
append([]byte{byte(opcode.PUSHDATA1), 10}, randomBytes(10)...),
append([]byte{byte(opcode.PUSHDATA1), 100}, randomBytes(100)...),
}
func FuzzIsScriptCorrect(f *testing.F) {
for _, s := range fuzzSeedValidScripts {
f.Add(s)
}
f.Fuzz(func(t *testing.T, script []byte) {
require.NotPanics(t, func() {
_ = IsScriptCorrect(script, nil)
})
})
}
func FuzzVMDontPanic(f *testing.F) {
for _, s := range fuzzSeedValidScripts {
f.Add(s)
}
f.Fuzz(func(t *testing.T, script []byte) {
if IsScriptCorrect(script, nil) != nil {
return
}
v := load(script)
// Prevent infinite loops from being reported as fail.
v.GasLimit = 1000
v.getPrice = func(opcode.Opcode, []byte) int64 {
return 1
}
require.NotPanics(t, func() {
_ = v.Run()
})
})
}