From 428e789ddc814696ed7e7b46750125be050d5ef8 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 31 Aug 2019 09:04:59 +0300 Subject: [PATCH 1/2] vm: produce better error for ROLL with wrong index Current VM implementation doesn't return errors for many operations, so the only way to handle it here is to check for NULL. Refs. #96. --- pkg/vm/vm.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 19914cce2..eff7dac51 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -342,7 +342,11 @@ func (v *VM) execute(ctx *Context, op Instruction) { panic("negative stack item returned") } if n > 0 { - v.estack.Push(v.estack.RemoveAt(n)) + e := v.estack.RemoveAt(n) + if e == nil { + panic("bad index") + } + v.estack.Push(e) } case DROP: From 42dfca47cfb8c212e5f099f8c2aae076801e2d33 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 31 Aug 2019 09:06:56 +0300 Subject: [PATCH 2/2] vm: fix double VM run from CLI Fixes one more instruction being ran when VM FAULTs: NEO-GO-VM > run NEO-GO-VM > error encountered at instruction 6 (ROLL) NEO-GO-VM > runtime error: invalid memory address or nil pointer dereference FAULT NEO-GO-VM > error encountered at instruction 7 (SETITEM) NEO-GO-VM > interface conversion: interface {} is []vm.StackItem, not []uint8 Refs. #96. --- pkg/vm/cli/cli.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/pkg/vm/cli/cli.go b/pkg/vm/cli/cli.go index b3d4efb36..84bc8ebcd 100644 --- a/pkg/vm/cli/cli.go +++ b/pkg/vm/cli/cli.go @@ -127,16 +127,13 @@ func (c *VMCLI) handleCommand(cmd string, args ...string) { fmt.Printf("READY: loaded %d instructions\n", c.vm.Context().LenInstr()) case "run": - var ( - method []byte - params []vm.StackItem - err error - start int - ) - - if len(args) == 0 { - c.vm.Run() - } else { + if len(args) != 0 { + var ( + method []byte + params []vm.StackItem + err error + start int + ) if isMethodArg(args[0]) { method = []byte(args[0]) start = 1 @@ -146,8 +143,8 @@ func (c *VMCLI) handleCommand(cmd string, args ...string) { fmt.Println(err) return } + c.vm.LoadArgs(method, params) } - c.vm.LoadArgs(method, params) c.vm.Run() case "cont":