From: Matthew Dempsky Date: Tue, 12 Oct 2021 22:31:25 +0000 (-0700) Subject: unsafe: allow unsafe.Slice up to end of address space X-Git-Tag: go1.18beta1~914 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4efa216c9d753c0853aa96a8c54ed5014fbc12e6;p=gostls13.git unsafe: allow unsafe.Slice up to end of address space Allow the user to construct slices that are larger than the Go heap as long as they don't overflow the address space. Updates #48798. Change-Id: I659c8334d04676e1f253b9c3cd499eab9b9f989a Reviewed-on: https://go-review.googlesource.com/c/go/+/355489 Trust: Matthew Dempsky Run-TryBot: Matthew Dempsky Reviewed-by: Ian Lance Taylor --- diff --git a/src/runtime/slice.go b/src/runtime/slice.go index cfa862e047..66bb7d9d93 100644 --- a/src/runtime/slice.go +++ b/src/runtime/slice.go @@ -124,7 +124,7 @@ func unsafeslice(et *_type, ptr unsafe.Pointer, len int) { } mem, overflow := math.MulUintptr(et.size, uintptr(len)) - if overflow || mem > maxAlloc || len < 0 { + if overflow || mem > -uintptr(ptr) || len < 0 { panicunsafeslicelen() } } diff --git a/test/unsafebuiltins.go b/test/unsafebuiltins.go index 4c940aa855..d04bcbdc7d 100644 --- a/test/unsafebuiltins.go +++ b/test/unsafebuiltins.go @@ -47,6 +47,11 @@ func main() { // size overflows address space mustPanic(func() { _ = unsafe.Slice(new(uint64), maxUintptr/8) }) mustPanic(func() { _ = unsafe.Slice(new(uint64), maxUintptr/8+1) }) + + // sliced memory overflows address space + last := (*byte)(unsafe.Pointer(^uintptr(0))) + _ = unsafe.Slice(last, 1) + mustPanic(func() { _ = unsafe.Slice(last, 2) }) } }