]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix -m=2 infinite loop in escape.go
authorMatthew Dempsky <mdempsky@google.com>
Tue, 12 Nov 2019 00:45:34 +0000 (16:45 -0800)
committerDavid Chase <drchase@google.com>
Tue, 12 Nov 2019 17:14:12 +0000 (17:14 +0000)
This CL detects infinite loops due to negative dereference cycles
during escape analysis, and terminates the loop gracefully. We still
fail to print a complete explanation of the escape path, but esc.go
didn't print *any* explanation for these test cases, so the release
blocking issue here is simply that we don't infinite loop.

Updates #35518.

Change-Id: I39beed036e5a685706248852f1fa619af3b7abbc
Reviewed-on: https://go-review.googlesource.com/c/go/+/206619
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/escape.go
test/fixedbugs/issue35518.go [new file with mode: 0644]

index 0f71f9990bc3ad77562ff06bcc065ae62ea65a14..76c91ba2d2ff823a09dadf306e9f42793607a408 100644 (file)
@@ -1226,8 +1226,17 @@ func (e *Escape) walkOne(root *EscLocation, walkgen uint32, enqueue func(*EscLoc
 
 // explainPath prints an explanation of how src flows to the walk root.
 func (e *Escape) explainPath(root, src *EscLocation) {
+       visited := make(map[*EscLocation]bool)
+
        pos := linestr(src.n.Pos)
        for {
+               // Prevent infinite loop.
+               if visited[src] {
+                       fmt.Printf("%s:   warning: truncated explanation due to assignment cycle; see golang.org/issue/35518\n", pos)
+                       break
+               }
+               visited[src] = true
+
                dst := src.dst
                edge := &dst.edges[src.dstEdgeIdx]
                if edge.src != src {
diff --git a/test/fixedbugs/issue35518.go b/test/fixedbugs/issue35518.go
new file mode 100644 (file)
index 0000000..18a02d4
--- /dev/null
@@ -0,0 +1,36 @@
+// errorcheck -0 -l -m=2
+
+// 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.
+
+// This test makes sure that -m=2's escape analysis diagnostics don't
+// go into an infinite loop when handling negative dereference
+// cycles. The critical thing being tested here is that compilation
+// succeeds ("errorcheck -0"), not any particular diagnostic output,
+// hence the very lax ERROR patterns below.
+
+package p
+
+type Node struct {
+       Orig *Node
+}
+
+var sink *Node
+
+func f1() {
+       var n Node // ERROR "."
+       n.Orig = &n
+
+       m := n // ERROR "."
+       sink = &m
+}
+
+func f2() {
+       var n1, n2 Node // ERROR "."
+       n1.Orig = &n2
+       n2 = n1
+
+       m := n2 // ERROR "."
+       sink = &m
+}