]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: detect unsafe conversions from smaller to larger types
authorMatthew Dempsky <mdempsky@google.com>
Thu, 17 Oct 2019 19:06:53 +0000 (12:06 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 17 Oct 2019 19:29:20 +0000 (19:29 +0000)
This CL extends the runtime instrumentation for (*T)(ptr) to also
check that the first and last bytes of *(*T)(ptr) are part of the same
heap object.

Updates #22218.
Updates #34959.

Change-Id: I2c8063fe1b7fe6e6145e41c5654cb64dd1c9dd41
Reviewed-on: https://go-review.googlesource.com/c/go/+/201778
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/walk.go
src/runtime/checkptr.go

index d8fc0abf3f58be7688bdfb8d2607b4667bede1cc..a9628096e7fa15f22c9c3aee5820693a3b3bc264 100644 (file)
@@ -3910,7 +3910,7 @@ func isRuneCount(n *Node) bool {
 }
 
 func walkCheckPtrAlignment(n *Node, init *Nodes) *Node {
-       if n.Type.Elem().Alignment() == 1 {
+       if n.Type.Elem().Alignment() == 1 && n.Type.Elem().Size() == 1 {
                return n
        }
 
index 040a19a39c1c37a2c4cdc1a8a6364ce9831771e3..a6d33c5af1024bc4c93369fb2e241731b754764c 100644 (file)
@@ -7,14 +7,20 @@ package runtime
 import "unsafe"
 
 type ptrAlign struct {
-       ptr   unsafe.Pointer
-       align uintptr
+       ptr  unsafe.Pointer
+       elem *_type
 }
 
 func checkptrAlignment(p unsafe.Pointer, elem *_type) {
+       // Check that (*T)(p) is appropriately aligned.
        // TODO(mdempsky): What about fieldAlign?
        if uintptr(p)&(uintptr(elem.align)-1) != 0 {
-               panic(ptrAlign{p, uintptr(elem.align)})
+               panic(ptrAlign{p, elem})
+       }
+
+       // 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})
        }
 }