]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: only escape unsafe.Pointer conversions when -d=checkptr=2
authorMatthew Dempsky <mdempsky@google.com>
Thu, 17 Oct 2019 23:31:19 +0000 (16:31 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Fri, 18 Oct 2019 23:33:48 +0000 (23:33 +0000)
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 <mdempsky@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/compile/internal/gc/escape.go
src/cmd/compile/internal/gc/main.go
src/cmd/compile/internal/gc/walk.go

index e25c79998cdbf90bc370c97ff5487a3c6694d44c..66440674d999a72b53b619f6053c91dc813dfbfd 100644 (file)
@@ -471,8 +471,8 @@ func (e *Escape) exprSkipInit(k EscHole, n *Node) {
                e.discard(max)
 
        case OCONV, OCONVNOP:
-               if checkPtr(e.curfn) && n.Type.Etype == TUNSAFEPTR && n.Left.Type.IsPtr() {
-                       // When -d=checkptr is enabled, treat
+               if checkPtr(e.curfn, 2) && n.Type.Etype == TUNSAFEPTR && n.Left.Type.IsPtr() {
+                       // When -d=checkptr=2 is enabled, treat
                        // conversions to unsafe.Pointer as an
                        // escaping operation. This allows better
                        // runtime instrumentation, since we can more
index e7131f10a2e6a69fb3deb5f8c4fa598ebce084f6..771b4fe973f22556af0990a87f341ddf95359acb 100644 (file)
@@ -94,6 +94,11 @@ const debugHelpHeader = `usage: -d arg[,arg]* and arg is <key>[=<value>]
 const debugHelpFooter = `
 <value> is key-specific.
 
+Key "checkptr" supports values:
+       "0": instrumentation disabled
+       "1": conversions involving unsafe.Pointer are instrumented
+       "2": conversions to unsafe.Pointer force heap allocation
+
 Key "pctab" supports values:
        "pctospadj", "pctofile", "pctoline", "pctoinline", "pctopcdata"
 `
index ebae392808bb015e4c54f988db11a1123a4c4d2d..4f5fa38a334cf8da1cb7b283585bb2c76bbd61f7 100644 (file)
@@ -951,7 +951,7 @@ opswitch:
 
        case OCONV, OCONVNOP:
                n.Left = walkexpr(n.Left, init)
-               if n.Op == OCONVNOP && checkPtr(Curfn) {
+               if n.Op == OCONVNOP && checkPtr(Curfn, 1) {
                        if n.Type.IsPtr() && n.Left.Type.Etype == TUNSAFEPTR { // unsafe.Pointer to *T
                                n = walkCheckPtrAlignment(n, init)
                                break
@@ -3976,7 +3976,8 @@ func walkCheckPtrArithmetic(n *Node, init *Nodes) *Node {
 }
 
 // checkPtr reports whether pointer checking should be enabled for
-// function fn.
-func checkPtr(fn *Node) bool {
-       return Debug_checkptr != 0 && fn.Func.Pragma&NoCheckPtr == 0
+// function fn at a given level. See debugHelpFooter for defined
+// levels.
+func checkPtr(fn *Node, level int) bool {
+       return Debug_checkptr >= level && fn.Func.Pragma&NoCheckPtr == 0
 }