Commit 07513d20 authored by Matthew Dempsky's avatar Matthew Dempsky Committed by David Chase

cmd/compile: fix -m=2 infinite loop in escape.go

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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: default avatarDavid Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent fc7ee0ba
...@@ -1226,8 +1226,17 @@ func (e *Escape) walkOne(root *EscLocation, walkgen uint32, enqueue func(*EscLoc ...@@ -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. // explainPath prints an explanation of how src flows to the walk root.
func (e *Escape) explainPath(root, src *EscLocation) { func (e *Escape) explainPath(root, src *EscLocation) {
visited := make(map[*EscLocation]bool)
pos := linestr(src.n.Pos) pos := linestr(src.n.Pos)
for { 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 dst := src.dst
edge := &dst.edges[src.dstEdgeIdx] edge := &dst.edges[src.dstEdgeIdx]
if edge.src != src { if edge.src != src {
......
// 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
}
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