Merge pull request #3688 from nspcc-dev/vm-fix-popitem-refcounting

vm: fix incorrect refcounting in POPITEM
This commit is contained in:
Anna Shaleva 2024-11-18 11:32:57 +03:00 committed by GitHub
commit 990634a43a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 1 deletions

View file

@ -3,6 +3,7 @@ package vm
import ( import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -46,6 +47,19 @@ func TestRefCounter_Add(t *testing.T) {
require.Equal(t, 2, int(*r)) require.Equal(t, 2, int(*r))
} }
func TestRefCounterPopItem(t *testing.T) {
prog := makeProgram(opcode.POPITEM)
v := load(prog)
v.estack.PushVal(stackitem.NewArray([]stackitem.Item{stackitem.Make(42)}))
require.Equal(t, 2, int(v.refs))
runVM(t, v)
require.Equal(t, 1, v.estack.Len())
require.Equal(t, 1, int(v.refs))
_ = v.estack.Pop()
require.Equal(t, 0, v.estack.Len())
require.Equal(t, 0, int(v.refs))
}
func BenchmarkRefCounter_Add(b *testing.B) { func BenchmarkRefCounter_Add(b *testing.B) {
a := stackitem.NewArray(nil) a := stackitem.NewArray(nil)
rc := newRefCounter() rc := newRefCounter()

View file

@ -1459,7 +1459,6 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
case *stackitem.Struct: case *stackitem.Struct:
item.Remove(index) item.Remove(index)
} }
v.refs.Remove(elem)
case opcode.SIZE: case opcode.SIZE:
elem := v.estack.Pop() elem := v.estack.Pop()