Skip to content

Commit a429926

Browse files
committed
cmd/compile: fix escape analysis of heap-allocated results
One of escape analysis's responsibilities is to summarize whether/how each function parameter flows to the heap so we can correctly incorporate those flows into callers' escape analysis data flow graphs. As an optimization, we separately record when parameters flow to result parameters, so that we can more precisely analyze parameter flows based on how the results are used at the call site. However, if a named result parameter itself needs to be heap allocated, this optimization isn't safe and the parameter needs to be recorded as flowing to heap rather than flowing to result. Escape analysis used to get this correct because it conservatively rewalked the data-flow graph multiple times. So even though it would incorrectly record the result parameter flow, it would separately find a flow to the heap. However, CL 196811 (specifically, case 3) optimized the walking logic to reduce unnecessary rewalks causing us to stop finding the extra heap flow. This CL fixes the issue by correcting location.leakTo to be sensitive to sink.escapes and not record result-flows when the result parameter escapes to the heap. Fixes #44614. Change-Id: I48742ed35a6cab591094e2d23a439e205bd65c50 Reviewed-on: https://go-review.googlesource.com/c/go/+/297289 Trust: Matthew Dempsky <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 998fe70 commit a429926

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,9 +1625,10 @@ func containsClosure(f, c *ir.Func) bool {
16251625

16261626
// leak records that parameter l leaks to sink.
16271627
func (l *location) leakTo(sink *location, derefs int) {
1628-
// If sink is a result parameter and we can fit return bits
1629-
// into the escape analysis tag, then record a return leak.
1630-
if sink.isName(ir.PPARAMOUT) && sink.curfn == l.curfn {
1628+
// If sink is a result parameter that doesn't escape (#44614)
1629+
// and we can fit return bits into the escape analysis tag,
1630+
// then record as a result leak.
1631+
if !sink.escapes && sink.isName(ir.PPARAMOUT) && sink.curfn == l.curfn {
16311632
ri := sink.resultIndex - 1
16321633
if ri < numEscResults {
16331634
// Leak to result parameter.

test/escape5.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,14 @@ func f28369(n int) int {
269269

270270
return 1 + f28369(n-1)
271271
}
272+
273+
// Issue 44614: parameters that flow to a heap-allocated result
274+
// parameter must be recorded as a heap-flow rather than a
275+
// result-flow.
276+
277+
// N.B., must match "leaking param: p",
278+
// but *not* "leaking param: p to result r level=0".
279+
func f(p *int) (r *int) { // ERROR "leaking param: p$" "moved to heap: r"
280+
sink4 = &r
281+
return p
282+
}

0 commit comments

Comments
 (0)