diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 52e1d7f79..738600b8e 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -1594,6 +1594,9 @@ func (v *VM) call(ctx *Context, offset int) { newCtx.retCount = -1 newCtx.local = nil newCtx.arguments = nil + // If memory for `elems` is reused, we can end up + // with incorrect exception context state in the caller. + newCtx.tryStack.elems = nil initStack(&newCtx.tryStack, "exception", nil) newCtx.NEF = ctx.NEF v.istack.PushItem(newCtx) diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 213a28b6a..6d28db7cf 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -1027,6 +1027,22 @@ func TestTRY(t *testing.T) { // add 5 to the exception, mul to the result of inner finally (2) getTRYTestFunc(47, inner, append(add5, byte(opcode.MUL)), add9)(t) }) + t.Run("nested, in throw and catch in call", func(t *testing.T) { + catchP := []byte{byte(opcode.PUSH10), byte(opcode.ADD)} + inner := getTRYProgram(throw, catchP, []byte{byte(opcode.PUSH2)}) + outer := getTRYProgram([]byte{byte(opcode.CALL), 0}, []byte{byte(opcode.PUSH3)}, []byte{byte(opcode.PUSH4)}) + outer = append(outer, byte(opcode.RET)) + outer[4] = byte(len(outer) - 3) // CALL argument at 3 (TRY) + 1 (CALL opcode) + outer = append(outer, inner...) + outer = append(outer, byte(opcode.RET)) + + v := load(outer) + runVM(t, v) + require.Equal(t, 3, v.Estack().Len()) + require.Equal(t, big.NewInt(4), v.Estack().Pop().Value()) // outer FINALLY + require.Equal(t, big.NewInt(2), v.Estack().Pop().Value()) // inner FINALLY + require.Equal(t, big.NewInt(23), v.Estack().Pop().Value()) // inner THROW + CATCH + }) } func TestMEMCPY(t *testing.T) {