Commit f9226454 authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: fix -d=checkptr for named unsafe.Pointer types

We need to explicitly convert pointers to unsafe.Pointer before
passing to the runtime checkptr instrumentation in case the user
declared their own type with underlying type unsafe.Pointer.

Updates #22218.
Fixes #34966.

Change-Id: I3baa2809d77f8257167cd78f57156f819130baa8
Reviewed-on: https://go-review.googlesource.com/c/go/+/201782
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarKeith Randall <khr@golang.org>
parent 58e8f789
...@@ -2392,6 +2392,9 @@ func conv(n *Node, t *types.Type) *Node { ...@@ -2392,6 +2392,9 @@ func conv(n *Node, t *types.Type) *Node {
// convnop converts node n to type t using the OCONVNOP op // convnop converts node n to type t using the OCONVNOP op
// and typechecks the result with ctxExpr. // and typechecks the result with ctxExpr.
func convnop(n *Node, t *types.Type) *Node { func convnop(n *Node, t *types.Type) *Node {
if types.Identical(n.Type, t) {
return n
}
n = nod(OCONVNOP, n, nil) n = nod(OCONVNOP, n, nil)
n.Type = t n.Type = t
n = typecheck(n, ctxExpr) n = typecheck(n, ctxExpr)
...@@ -3915,7 +3918,7 @@ func walkCheckPtrAlignment(n *Node, init *Nodes) *Node { ...@@ -3915,7 +3918,7 @@ func walkCheckPtrAlignment(n *Node, init *Nodes) *Node {
} }
n.Left = cheapexpr(n.Left, init) n.Left = cheapexpr(n.Left, init)
init.Append(mkcall("checkptrAlignment", nil, init, n.Left, typename(n.Type.Elem()))) init.Append(mkcall("checkptrAlignment", nil, init, convnop(n.Left, types.Types[TUNSAFEPTR]), typename(n.Type.Elem())))
return n return n
} }
...@@ -3956,7 +3959,7 @@ func walkCheckPtrArithmetic(n *Node, init *Nodes) *Node { ...@@ -3956,7 +3959,7 @@ func walkCheckPtrArithmetic(n *Node, init *Nodes) *Node {
case OCONVNOP: case OCONVNOP:
if n.Left.Type.Etype == TUNSAFEPTR { if n.Left.Type.Etype == TUNSAFEPTR {
n.Left = cheapexpr(n.Left, init) n.Left = cheapexpr(n.Left, init)
originals = append(originals, n.Left) originals = append(originals, convnop(n.Left, types.Types[TUNSAFEPTR]))
} }
} }
} }
...@@ -3968,7 +3971,7 @@ func walkCheckPtrArithmetic(n *Node, init *Nodes) *Node { ...@@ -3968,7 +3971,7 @@ func walkCheckPtrArithmetic(n *Node, init *Nodes) *Node {
slice.Esc = EscNone slice.Esc = EscNone
slice.SetTransient(true) slice.SetTransient(true)
init.Append(mkcall("checkptrArithmetic", nil, init, n, slice)) init.Append(mkcall("checkptrArithmetic", nil, init, convnop(n, types.Types[TUNSAFEPTR]), slice))
return n return n
} }
......
// compile -d=checkptr
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p
import "unsafe"
type ptr unsafe.Pointer
func f(p ptr) *int { return (*int)(p) }
func g(p ptr) ptr { return ptr(uintptr(p) + 1) }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment