Skip to content

Commit 46aa835

Browse files
committed
cmd/compile: only escape unsafe.Pointer conversions when -d=checkptr=2
Escaping all unsafe.Pointer conversions for -d=checkptr seems like it might be a little too aggressive to enable for -race/-msan mode, since at least some tests are written to expect unsafe.Pointer conversions to not affect escape analysis. So instead only enable that functionality behind -d=checkptr=2. Updates #22218. Updates #34959. Change-Id: I2f0a774ea5961dabec29bc5b8ebe387a1b90d27b Reviewed-on: https://go-review.googlesource.com/c/go/+/201840 Run-TryBot: Matthew Dempsky <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 5375c71 commit 46aa835

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,8 @@ func (e *Escape) exprSkipInit(k EscHole, n *Node) {
471471
e.discard(max)
472472

473473
case OCONV, OCONVNOP:
474-
if checkPtr(e.curfn) && n.Type.Etype == TUNSAFEPTR && n.Left.Type.IsPtr() {
475-
// When -d=checkptr is enabled, treat
474+
if checkPtr(e.curfn, 2) && n.Type.Etype == TUNSAFEPTR && n.Left.Type.IsPtr() {
475+
// When -d=checkptr=2 is enabled, treat
476476
// conversions to unsafe.Pointer as an
477477
// escaping operation. This allows better
478478
// runtime instrumentation, since we can more

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ const debugHelpHeader = `usage: -d arg[,arg]* and arg is <key>[=<value>]
9494
const debugHelpFooter = `
9595
<value> is key-specific.
9696
97+
Key "checkptr" supports values:
98+
"0": instrumentation disabled
99+
"1": conversions involving unsafe.Pointer are instrumented
100+
"2": conversions to unsafe.Pointer force heap allocation
101+
97102
Key "pctab" supports values:
98103
"pctospadj", "pctofile", "pctoline", "pctoinline", "pctopcdata"
99104
`

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ opswitch:
951951

952952
case OCONV, OCONVNOP:
953953
n.Left = walkexpr(n.Left, init)
954-
if n.Op == OCONVNOP && checkPtr(Curfn) {
954+
if n.Op == OCONVNOP && checkPtr(Curfn, 1) {
955955
if n.Type.IsPtr() && n.Left.Type.Etype == TUNSAFEPTR { // unsafe.Pointer to *T
956956
n = walkCheckPtrAlignment(n, init)
957957
break
@@ -3976,7 +3976,8 @@ func walkCheckPtrArithmetic(n *Node, init *Nodes) *Node {
39763976
}
39773977

39783978
// checkPtr reports whether pointer checking should be enabled for
3979-
// function fn.
3980-
func checkPtr(fn *Node) bool {
3981-
return Debug_checkptr != 0 && fn.Func.Pragma&NoCheckPtr == 0
3979+
// function fn at a given level. See debugHelpFooter for defined
3980+
// levels.
3981+
func checkPtr(fn *Node, level int) bool {
3982+
return Debug_checkptr >= level && fn.Func.Pragma&NoCheckPtr == 0
39823983
}

0 commit comments

Comments
 (0)