Skip to content

Commit f922645

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 58e8f78 commit f922645

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,6 +2392,9 @@ func conv(n *Node, t *types.Type) *Node {
23922392
// convnop converts node n to type t using the OCONVNOP op
23932393
// and typechecks the result with ctxExpr.
23942394
func convnop(n *Node, t *types.Type) *Node {
2395+
if types.Identical(n.Type, t) {
2396+
return n
2397+
}
23952398
n = nod(OCONVNOP, n, nil)
23962399
n.Type = t
23972400
n = typecheck(n, ctxExpr)
@@ -3915,7 +3918,7 @@ func walkCheckPtrAlignment(n *Node, init *Nodes) *Node {
39153918
}
39163919

39173920
n.Left = cheapexpr(n.Left, init)
3918-
init.Append(mkcall("checkptrAlignment", nil, init, n.Left, typename(n.Type.Elem())))
3921+
init.Append(mkcall("checkptrAlignment", nil, init, convnop(n.Left, types.Types[TUNSAFEPTR]), typename(n.Type.Elem())))
39193922
return n
39203923
}
39213924

@@ -3956,7 +3959,7 @@ func walkCheckPtrArithmetic(n *Node, init *Nodes) *Node {
39563959
case OCONVNOP:
39573960
if n.Left.Type.Etype == TUNSAFEPTR {
39583961
n.Left = cheapexpr(n.Left, init)
3959-
originals = append(originals, n.Left)
3962+
originals = append(originals, convnop(n.Left, types.Types[TUNSAFEPTR]))
39603963
}
39613964
}
39623965
}
@@ -3968,7 +3971,7 @@ func walkCheckPtrArithmetic(n *Node, init *Nodes) *Node {
39683971
slice.Esc = EscNone
39693972
slice.SetTransient(true)
39703973

3971-
init.Append(mkcall("checkptrArithmetic", nil, init, n, slice))
3974+
init.Append(mkcall("checkptrArithmetic", nil, init, convnop(n, types.Types[TUNSAFEPTR]), slice))
39723975
return n
39733976
}
39743977

test/fixedbugs/issue34966.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// compile -d=checkptr
2+
3+
// Copyright 2019 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
import "unsafe"
10+
11+
type ptr unsafe.Pointer
12+
13+
func f(p ptr) *int { return (*int)(p) }
14+
func g(p ptr) ptr { return ptr(uintptr(p) + 1) }

0 commit comments

Comments
 (0)