Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrongly use reflect.growslice() #629

Merged
merged 7 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ast/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func quote(buf *[]byte, val string) {
}

// double buf size
*b = growslice(typeByte, *b, b.Cap*2)
*b = rt.GrowSlice(typeByte, *b, b.Cap*2)
// ret is the complement of consumed input
ret = ^ret
// update input buffer
Expand Down
6 changes: 0 additions & 6 deletions ast/stubs_go120.go → ast/stubs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build go1.20

/*
* Copyright 2021 ByteDance Inc.
*
Expand Down Expand Up @@ -34,10 +32,6 @@ func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)
//goland:noinspection GoUnusedParameter
func unsafe_NewArray(typ *rt.GoType, n int) unsafe.Pointer

//go:linkname growslice reflect.growslice
//goland:noinspection GoUnusedParameter
func growslice(et *rt.GoType, old rt.GoSlice, cap int) rt.GoSlice

//go:nosplit
func mem2ptr(s []byte) unsafe.Pointer {
return (*rt.GoSlice)(unsafe.Pointer(&s)).Ptr
Expand Down
55 changes: 0 additions & 55 deletions ast/stubs_go115.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/decoder/assembler_regabi_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ func (self *_Assembler) decode_dynamic(vt obj.Addr, vp obj.Addr) {
var (
_F_memequal = jit.Func(memequal)
_F_memmove = jit.Func(memmove)
_F_growslice = jit.Func(growslice)
_F_growslice = jit.Func(rt.GrowSlice)
_F_makeslice = jit.Func(makeslice)
_F_makemap_small = jit.Func(makemap_small)
_F_mapassign_fast64 = jit.Func(mapassign_fast64)
Expand Down
2 changes: 1 addition & 1 deletion internal/decoder/assembler_stkabi_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ func (self *_Assembler) decode_dynamic(vt obj.Addr, vp obj.Addr) {
var (
_F_memequal = jit.Func(memequal)
_F_memmove = jit.Func(memmove)
_F_growslice = jit.Func(growslice)
_F_growslice = jit.Func(rt.GrowSlice)
_F_makeslice = jit.Func(makeslice)
_F_makemap_small = jit.Func(makemap_small)
_F_mapassign_fast64 = jit.Func(mapassign_fast64)
Expand Down
5 changes: 0 additions & 5 deletions internal/decoder/stubs_go116.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ func mallocgc(size uintptr, typ *rt.GoType, needzero bool) unsafe.Pointer
//goland:noinspection GoUnusedParameter
func makeslice(et *rt.GoType, len int, cap int) unsafe.Pointer

//go:noescape
//go:linkname growslice runtime.growslice
//goland:noinspection GoUnusedParameter
func growslice(et *rt.GoType, old rt.GoSlice, cap int) rt.GoSlice

//go:linkname makemap_small runtime.makemap_small
func makemap_small() unsafe.Pointer

Expand Down
5 changes: 0 additions & 5 deletions internal/decoder/stubs_go120.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ func mallocgc(size uintptr, typ *rt.GoType, needzero bool) unsafe.Pointer
//goland:noinspection GoUnusedParameter
func makeslice(et *rt.GoType, len int, cap int) unsafe.Pointer

//go:noescape
//go:linkname growslice reflect.growslice
//goland:noinspection GoUnusedParameter
func growslice(et *rt.GoType, old rt.GoSlice, cap int) rt.GoSlice

//go:linkname makemap_small runtime.makemap_small
func makemap_small() unsafe.Pointer

Expand Down
2 changes: 1 addition & 1 deletion internal/encoder/assembler_regabi_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func (self *_Assembler) call_marshaler_v(fn obj.Addr, it *rt.GoType, vt reflect.

var (
_T_byte = jit.Type(byteType)
_F_growslice = jit.Func(growslice)
_F_growslice = jit.Func(rt.GrowSlice)
)

// AX must saving n
Expand Down
2 changes: 1 addition & 1 deletion internal/encoder/assembler_stkabi_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ func (self *_Assembler) call_marshaler_v(fn obj.Addr, it *rt.GoType, vt reflect.

var (
_T_byte = jit.Type(byteType)
_F_growslice = jit.Func(growslice)
_F_growslice = jit.Func(rt.GrowSlice)
)

func (self *_Assembler) more_space() {
Expand Down
2 changes: 1 addition & 1 deletion internal/encoder/mapiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func iteratorStart(t *rt.GoMapType, m *rt.GoMap, fv uint64) (*_MapIterator, erro

/* pre-allocate space if needed */
if m.Count > it.kv.Cap {
it.kv = growslice(iteratorPair, it.kv, m.Count)
it.kv = rt.GrowSlice(iteratorPair, it.kv, m.Count)
}

/* dump all the key-value pairs */
Expand Down
6 changes: 3 additions & 3 deletions internal/encoder/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func encodeString(buf *[]byte, val string) error {

/* not enough space, grow the slice and try again */
sidx += ^nb
*pbuf = growslice(rt.UnpackType(byteType), *pbuf, pbuf.Cap * 2)
*pbuf = rt.GrowSlice(rt.UnpackType(byteType), *pbuf, pbuf.Cap * 2)
}

/* closing quote */
Expand Down Expand Up @@ -122,7 +122,7 @@ func htmlEscape(dst []byte, src []byte) []byte {
/* grow dst if it is shorter */
if cap(dst) - len(dst) < len(src) + types.BufPaddingSize {
cap := len(src) * 3 / 2 + types.BufPaddingSize
*dbuf = growslice(typeByte, *dbuf, cap)
*dbuf = rt.GrowSlice(typeByte, *dbuf, cap)
}

for sidx < sbuf.Len {
Expand All @@ -140,7 +140,7 @@ func htmlEscape(dst []byte, src []byte) []byte {

/* not enough space, grow the slice and try again */
sidx += ^nb
*dbuf = growslice(typeByte, *dbuf, dbuf.Cap * 2)
*dbuf = rt.GrowSlice(typeByte, *dbuf, dbuf.Cap * 2)
}
return dst
}
Expand Down
4 changes: 0 additions & 4 deletions internal/encoder/stubs_go116.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ var _subr__b64encode uintptr
//goland:noinspection GoUnusedParameter
func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)

//go:linkname growslice runtime.growslice
//goland:noinspection GoUnusedParameter
func growslice(et *rt.GoType, old rt.GoSlice, cap int) rt.GoSlice

//go:linkname mapiternext runtime.mapiternext
//goland:noinspection GoUnusedParameter
func mapiternext(it *rt.GoMapIterator)
Expand Down
4 changes: 0 additions & 4 deletions internal/encoder/stubs_go117.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ var _subr__b64encode uintptr
//goland:noinspection GoUnusedParameter
func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)

//go:linkname growslice runtime.growslice
//goland:noinspection GoUnusedParameter
func growslice(et *rt.GoType, old rt.GoSlice, cap int) rt.GoSlice

//go:linkname mapiternext runtime.mapiternext
//goland:noinspection GoUnusedParameter
func mapiternext(it *rt.GoMapIterator)
Expand Down
4 changes: 0 additions & 4 deletions internal/encoder/stubs_go120.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ var _subr__b64encode uintptr
//goland:noinspection GoUnusedParameter
func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)

//go:linkname growslice reflect.growslice
//goland:noinspection GoUnusedParameter
func growslice(et *rt.GoType, old rt.GoSlice, cap int) rt.GoSlice

//go:linkname mapiternext runtime.mapiternext
//goland:noinspection GoUnusedParameter
func mapiternext(it *rt.GoMapIterator)
Expand Down
4 changes: 0 additions & 4 deletions internal/encoder/stubs_go121.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ var _subr__b64encode uintptr
//goland:noinspection GoUnusedParameter
func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)

//go:linkname growslice reflect.growslice
//goland:noinspection GoUnusedParameter
func growslice(et *rt.GoType, old rt.GoSlice, cap int) rt.GoSlice

//go:linkname mapiternext runtime.mapiternext
//goland:noinspection GoUnusedParameter
func mapiternext(it *rt.GoMapIterator)
Expand Down
2 changes: 1 addition & 1 deletion internal/rt/fastmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ func StrFrom(p unsafe.Pointer, n int64) (s string) {
func NoEscape(p unsafe.Pointer) unsafe.Pointer {
x := uintptr(p)
return unsafe.Pointer(x ^ 0)
}
}
2 changes: 1 addition & 1 deletion internal/rt/fastvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,4 @@ func GetFuncPC(fn interface{}) uintptr {
panic("not a function")
}
return *(*uintptr)(ft.Value)
}
}
2 changes: 1 addition & 1 deletion internal/rt/stackmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,4 @@ func (self *StackMapBuilder) AddFields(n int, ptr bool) {
} else {
self.b.AppendMany(n, 0)
}
}
}
23 changes: 23 additions & 0 deletions internal/rt/stub_go116.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// +build go1.16,!go1.20

// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rt

import _ "unsafe"

//go:linkname GrowSlice runtime.growslice
//goland:noinspection GoUnusedParameter
func GrowSlice(et *GoType, old GoSlice, cap int) GoSlice
29 changes: 29 additions & 0 deletions internal/rt/stub_go120.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// +build go1.20

// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rt

import "unsafe"

func GrowSlice(et *GoType, old GoSlice, cap int) GoSlice {
s := growslice(old.Ptr, cap, old.Cap, cap - old.Len, et)
s.Len = old.Len
return s
}

//go:linkname growslice runtime.growslice
//goland:noinspection GoUnusedParameter
func growslice(oldPtr unsafe.Pointer, newLen, oldCap, num int, et *GoType) GoSlice
32 changes: 32 additions & 0 deletions internal/rt/stub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rt

import (
"testing"
"unsafe"
)

func TestGrowSlic(t *testing.T) {
et := UnpackEface(byte(0)).Type
n := make([]byte, 1, 1024)
println("n len:", len(n), "cap:", cap(n))
v := GrowSlice(et, *(*GoSlice)(unsafe.Pointer(&n)), 1025)
println("v len:", v.Len, "cap:", v.Cap)
// according to go growslice rule, the next cap must be at most 1.5x of old.Cap
if v.Cap > 1536 {
t.Fatal(v.Cap)
}
}
Loading