diff options
author | Ian Lance Taylor <iant@golang.org> | 2021-07-31 19:28:51 -0700 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2021-08-02 13:56:28 -0700 |
commit | 06d0437d4a5faca2b695918cbe1d54a61935c98b (patch) | |
tree | 81cc666b16c30384b39469f246c1ec304a4a4e72 /libgo | |
parent | 14d8a5ae472ca5743016f37da2dd4770d83dea21 (diff) | |
download | gcc-06d0437d4a5faca2b695918cbe1d54a61935c98b.zip gcc-06d0437d4a5faca2b695918cbe1d54a61935c98b.tar.gz gcc-06d0437d4a5faca2b695918cbe1d54a61935c98b.tar.bz2 |
compiler, runtime: support unsafe.Add and unsafe.Slice
For golang/go#19367
For golang/go#40481
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/338949
Diffstat (limited to 'libgo')
-rw-r--r-- | libgo/go/runtime/slice.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/libgo/go/runtime/slice.go b/libgo/go/runtime/slice.go index 27a9777..d4c0e90 100644 --- a/libgo/go/runtime/slice.go +++ b/libgo/go/runtime/slice.go @@ -18,6 +18,8 @@ import ( //go:linkname checkMakeSlice //go:linkname makeslice64 //go:linkname growslice +//go:linkname unsafeslice +//go:linkname unsafeslice64 type slice struct { array unsafe.Pointer @@ -127,6 +129,33 @@ func makeslice64(et *_type, len64, cap64 int64) unsafe.Pointer { return makeslice(et, len, cap) } +func unsafeslice(et *_type, ptr unsafe.Pointer, len int) { + if len == 0 { + return + } + + if ptr == nil { + panic(errorString("unsafe.Slice: ptr is nil and len is not zero")) + } + + mem, overflow := math.MulUintptr(et.size, uintptr(len)) + if overflow || mem > maxAlloc || len < 0 { + panicunsafeslicelen() + } +} + +func unsafeslice64(et *_type, ptr unsafe.Pointer, len64 int64) { + len := int(len64) + if int64(len) != len64 { + panicunsafeslicelen() + } + unsafeslice(et, ptr, len) +} + +func panicunsafeslicelen() { + panic(errorString("unsafe.Slice: len out of range")) +} + // growslice handles slice growth during append. // It is passed the slice element type, the old slice, and the desired new minimum capacity, // and it returns a new slice with at least that capacity, with the old data |