From 04f5fdefa0e00293ce6e4c51e902939565d9e025 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sat, 22 Aug 2020 12:01:57 +0300 Subject: [PATCH] vm: properly unload context on exception Do not copy exception context on CALL*. --- pkg/vm/vm.go | 3 ++- pkg/vm/vm_test.go | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index ca68a2716..0a426fb6a 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -1476,6 +1476,7 @@ func (v *VM) Call(ctx *Context, offset int) { newCtx.CheckReturn = false newCtx.local = nil newCtx.arguments = nil + newCtx.tryStack = NewStack("exception") v.istack.PushVal(newCtx) v.Jump(newCtx, offset) } @@ -1517,7 +1518,7 @@ func (v *VM) handleException() { pop := 0 ictx := v.istack.Peek(0).Value().(*Context) for ictx != nil { - e := ictx.tryStack.Peek(pop) + e := ictx.tryStack.Peek(0) for e != nil { ectx := e.Value().(*exceptionHandlingContext) if ectx.State == eFinally || (ectx.State == eCatch && !ectx.HasFinally()) { diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 65c51e2d4..198d2183e 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -1336,6 +1336,12 @@ func TestTRY(t *testing.T) { checkVMFailed(t, vm) }) }) + t.Run("ThrowInCall", func(t *testing.T) { + catchP := []byte{byte(opcode.CALL), 2, byte(opcode.PUSH1), byte(opcode.ADD), byte(opcode.THROW), byte(opcode.RET)} + inner := getTRYProgram(throw, catchP, []byte{byte(opcode.PUSH2)}) + // add 5 to the exception, mul to the result of inner finally (2) + getTRYTestFunc(47, inner, append(add5, byte(opcode.MUL)), add9)(t) + }) } func TestMEMCPY(t *testing.T) {