From a19ed7f68fe355eb119231a515c7d76fa6c1e764 Mon Sep 17 00:00:00 2001 From: "liuqiang.06" Date: Tue, 16 Jul 2024 21:52:17 +0800 Subject: [PATCH] fix(arm): grow slice not clear mem --- internal/rt/growslice.go | 4 ++-- internal/rt/rt_stubs_go116.go | 45 ----------------------------------- internal/rt/rt_stubs_go120.go | 36 ---------------------------- internal/rt/stubs.go | 36 ++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 83 deletions(-) delete mode 100644 internal/rt/rt_stubs_go116.go delete mode 100644 internal/rt/rt_stubs_go120.go diff --git a/internal/rt/growslice.go b/internal/rt/growslice.go index ef1856948..0c46529b3 100644 --- a/internal/rt/growslice.go +++ b/internal/rt/growslice.go @@ -20,8 +20,8 @@ package rt import "unsafe" -func GrowSlice(et *GoType, old GoSlice, cap int) GoSlice { - s := growslice(old.Ptr, cap, old.Cap, cap - old.Len, et) +func GrowSlice(et *GoType, old GoSlice, newLen int) GoSlice { + s := growslice(old.Ptr, newLen, old.Cap, newLen - old.Len, et) s.Len = old.Len return s } diff --git a/internal/rt/rt_stubs_go116.go b/internal/rt/rt_stubs_go116.go deleted file mode 100644 index 2c60e164c..000000000 --- a/internal/rt/rt_stubs_go116.go +++ /dev/null @@ -1,45 +0,0 @@ -//go:build go1.17 && !go1.20 -// +build go1.17,!go1.20 - -package rt - -import ( - "unsafe" -) - - -//go:linkname makeslice runtime.makeslice -//goland:noinspection GoUnusedParameter -func makeslice(et *GoType, len int, cap int) unsafe.Pointer - -func MakeSlice(oldPtr unsafe.Pointer, et *GoType, newLen int) *GoSlice { - if newLen == 0 { - return &EmptySlice - } - - if *(*unsafe.Pointer)(oldPtr) == nil { - return &GoSlice{ - Ptr: makeslice(et, newLen, newLen), - Len: newLen, - Cap: newLen, - } - } - - old := (*GoSlice)(oldPtr) - if old.Cap >= newLen { - old.Len = newLen - return old - } - - new := GrowSlice(et, *old, newLen) - - // we sould clear the memory from [oldLen:newLen] - if et.PtrData == 0 { - oldlenmem := uintptr(old.Len) * et.Size - newlenmem := uintptr(newLen) * et.Size - MemclrNoHeapPointers(add(new.Ptr, oldlenmem), newlenmem-oldlenmem) - } - - new.Len = newLen - return &new -} diff --git a/internal/rt/rt_stubs_go120.go b/internal/rt/rt_stubs_go120.go deleted file mode 100644 index 735be152d..000000000 --- a/internal/rt/rt_stubs_go120.go +++ /dev/null @@ -1,36 +0,0 @@ -//go:build go1.20 && !go1.23 -// +build go1.20,!go1.23 - -package rt - -import ( - "unsafe" -) - -//go:linkname makeslice runtime.makeslice -//goland:noinspection GoUnusedParameter -func makeslice(et *GoType, len int, cap int) unsafe.Pointer - -func MakeSlice(oldPtr unsafe.Pointer, et *GoType, newLen int) *GoSlice { - if newLen == 0 { - return &EmptySlice - } - - if *(*unsafe.Pointer)(oldPtr) == nil { - return &GoSlice{ - Ptr: makeslice(et, newLen, newLen), - Len: newLen, - Cap: newLen, - } - } - - old := (*GoSlice)(oldPtr) - if old.Cap >= newLen { - old.Len = newLen - return old - } - - new := GrowSlice(et, *old, newLen-old.Len) - new.Len = newLen - return &new -} diff --git a/internal/rt/stubs.go b/internal/rt/stubs.go index 36e9a808e..5104a0793 100644 --- a/internal/rt/stubs.go +++ b/internal/rt/stubs.go @@ -127,3 +127,39 @@ func GetMap64Assign(vt reflect.Type) Map64Assign { var emptyBytes = make([]byte, 0, 0) var EmptySlice = *(*GoSlice)(unsafe.Pointer(&emptyBytes)) + +//go:linkname makeslice runtime.makeslice +//goland:noinspection GoUnusedParameter +func makeslice(et *GoType, len int, cap int) unsafe.Pointer + +func MakeSlice(oldPtr unsafe.Pointer, et *GoType, newLen int) *GoSlice { + if newLen == 0 { + return &EmptySlice + } + + if *(*unsafe.Pointer)(oldPtr) == nil { + return &GoSlice{ + Ptr: makeslice(et, newLen, newLen), + Len: newLen, + Cap: newLen, + } + } + + old := (*GoSlice)(oldPtr) + if old.Cap >= newLen { + old.Len = newLen + return old + } + + new := GrowSlice(et, *old, newLen) + + // we sould clear the memory from [oldLen:newLen] + if et.PtrData == 0 { + oldlenmem := uintptr(old.Len) * et.Size + newlenmem := uintptr(newLen) * et.Size + MemclrNoHeapPointers(add(new.Ptr, oldlenmem), newlenmem-oldlenmem) + } + + new.Len = newLen + return &new +}