vm: properly clear try stack in CALL

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2022-02-04 10:39:38 +03:00
parent 01022c78e4
commit 10d006195a
2 changed files with 19 additions and 0 deletions

View file

@ -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)

View file

@ -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) {