Skip to content

Commit

Permalink
vm: properly clear try stack in CALL
Browse files Browse the repository at this point in the history
Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
  • Loading branch information
fyrchik committed Feb 4, 2022
1 parent 01022c7 commit 10d0061
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/vm/vm.go
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions pkg/vm/vm_test.go
Expand Up @@ -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) {
Expand Down

0 comments on commit 10d0061

Please sign in to comment.