aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/checkptr.go
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2019-10-17 14:29:16 -0700
committerMatthew Dempsky <mdempsky@google.com>2019-10-21 23:16:27 +0000
commit7b58581a232829f29a26d6ebee9f4c3ca59b4771 (patch)
tree99d236ae49eb9a2bad764a75eccbd5972f613309 /src/runtime/checkptr.go
parentdba19c65a78c017e4f3fab94c8288bb120203078 (diff)
downloadgo-7b58581a232829f29a26d6ebee9f4c3ca59b4771.tar.gz
go-7b58581a232829f29a26d6ebee9f4c3ca59b4771.zip
cmd/compile: recognize (*[Big]T)(ptr)[:n:m] pattern for -d=checkptr
A common idiom for turning an unsafe.Pointer into a slice is to write: s := (*[Big]T)(ptr)[:n:m] This technically violates Go's unsafe pointer rules (rule #1 says T2 can't be bigger than T1), but it's fairly common and not too difficult to recognize, so might as well allow it for now so we can make progress on #34972. This should be revisited if #19367 is accepted. Updates #22218. Updates #34972. Change-Id: Id824e2461904e770910b6e728b4234041d2cc8bc Reviewed-on: https://go-review.googlesource.com/c/go/+/201839 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/runtime/checkptr.go')
-rw-r--r--src/runtime/checkptr.go16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/runtime/checkptr.go b/src/runtime/checkptr.go
index a6d33c5af1..d1fc651509 100644
--- a/src/runtime/checkptr.go
+++ b/src/runtime/checkptr.go
@@ -9,18 +9,19 @@ import "unsafe"
type ptrAlign struct {
ptr unsafe.Pointer
elem *_type
+ n uintptr
}
-func checkptrAlignment(p unsafe.Pointer, elem *_type) {
- // Check that (*T)(p) is appropriately aligned.
+func checkptrAlignment(p unsafe.Pointer, elem *_type, n uintptr) {
+ // Check that (*[n]elem)(p) is appropriately aligned.
// TODO(mdempsky): What about fieldAlign?
if uintptr(p)&(uintptr(elem.align)-1) != 0 {
- panic(ptrAlign{p, elem})
+ panic(ptrAlign{p, elem, n})
}
- // Check that (*T)(p) doesn't straddle multiple heap objects.
- if elem.size != 1 && checkptrBase(p) != checkptrBase(add(p, elem.size-1)) {
- panic(ptrAlign{p, elem})
+ // Check that (*[n]elem)(p) doesn't straddle multiple heap objects.
+ if size := n * elem.size; size > 1 && checkptrBase(p) != checkptrBase(add(p, size-1)) {
+ panic(ptrAlign{p, elem, n})
}
}
@@ -34,6 +35,9 @@ func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) {
panic(ptrArith{p, originals})
}
+ // Check that if the computed pointer p points into a heap
+ // object, then one of the original pointers must have pointed
+ // into the same object.
base := checkptrBase(p)
if base == 0 {
return