Commit 9f9e17a8 authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: fix ICE from go/defer call to variadic function

The special case logic for go/defer arguments in Escape.call was
scattered around a bit and was somewhat inconsistently handled across
different types of function calls and parameters. This CL pulls the
logic out into a separate callStmt method that's used uniformly for
all kinds of function calls and arguments.

Fixes #31573.

Change-Id: Icdcdf611754dc3fcf1af7cb52879fb4b73a7a31f
Reviewed-on: https://go-review.googlesource.com/c/go/+/173019
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarCherry Zhang <cherryyz@google.com>
parent 376ce8c8
...@@ -746,14 +746,7 @@ func (e *Escape) call(ks []EscHole, call, where *Node) { ...@@ -746,14 +746,7 @@ func (e *Escape) call(ks []EscHole, call, where *Node) {
var recvK EscHole var recvK EscHole
var paramKs []EscHole var paramKs []EscHole
if where != nil && !(where.Op == ODEFER && e.loopDepth == 1) { if static && fn.Name.Defn != nil && fn.Name.Defn.Esc < EscFuncTagged {
if recv != nil {
recvK = e.heapHole()
}
for range args {
paramKs = append(paramKs, e.heapHole())
}
} else if static && fn.Name.Defn != nil && fn.Name.Defn.Esc < EscFuncTagged {
// Static call to function in same mutually recursive // Static call to function in same mutually recursive
// group; incorporate into data flow graph. // group; incorporate into data flow graph.
...@@ -778,32 +771,25 @@ func (e *Escape) call(ks []EscHole, call, where *Node) { ...@@ -778,32 +771,25 @@ func (e *Escape) call(ks []EscHole, call, where *Node) {
// function. Setup flows to heap and/or ks according // function. Setup flows to heap and/or ks according
// to parameter tags. // to parameter tags.
if r := fntype.Recv(); r != nil { if r := fntype.Recv(); r != nil {
recvK = e.tagHole(ks, r, static, where) recvK = e.tagHole(ks, r, static)
} }
for _, param := range fntype.Params().FieldSlice() { for _, param := range fntype.Params().FieldSlice() {
paramKs = append(paramKs, e.tagHole(ks, param, static, where)) paramKs = append(paramKs, e.tagHole(ks, param, static))
} }
} else { } else {
// Handle escape analysis for builtins. // Handle escape analysis for builtins.
// By default, we just discard everything.
// By default, we just discard everything. However, if
// we're in a top-level defer statement, we can't
// allow transient values.
k := e.discardHole()
if where != nil {
k = e.newLoc(where, false).asHole()
}
for range args { for range args {
paramKs = append(paramKs, k) paramKs = append(paramKs, e.discardHole())
} }
switch call.Op { switch call.Op {
case OAPPEND: case OAPPEND:
// Appendee slice may flow directly to the // Appendee slice may flow directly to the
// result, if it has enough // result, if it has enough capacity.
// capacity. Alternatively, a new heap slice // Alternatively, a new heap slice might be
// might be allocated, and all slice elements // allocated, and all slice elements might
// might flow to heap. // flow to heap.
paramKs[0] = e.teeHole(paramKs[0], ks[0]) paramKs[0] = e.teeHole(paramKs[0], ks[0])
if types.Haspointers(args[0].Type.Elem()) { if types.Haspointers(args[0].Type.Elem()) {
paramKs[0] = e.teeHole(paramKs[0], e.heapHole().deref(call, "appendee slice")) paramKs[0] = e.teeHole(paramKs[0], e.heapHole().deref(call, "appendee slice"))
...@@ -829,6 +815,22 @@ func (e *Escape) call(ks []EscHole, call, where *Node) { ...@@ -829,6 +815,22 @@ func (e *Escape) call(ks []EscHole, call, where *Node) {
} }
} }
if call.Op == OCALLFUNC {
// Evaluate callee function expression.
e.expr(e.augmentParamHole(e.discardHole(), where), call.Left)
}
if recv != nil {
// TODO(mdempsky): Handle go:uintptrescapes here too?
e.expr(e.augmentParamHole(recvK, where), recv)
}
// Apply augmentParamHole before ODDDARG so that it affects
// the implicit slice allocation for variadic calls, if any.
for i, paramK := range paramKs {
paramKs[i] = e.augmentParamHole(paramK, where)
}
// TODO(mdempsky): Remove after early ddd-ification. // TODO(mdempsky): Remove after early ddd-ification.
if fntype != nil && fntype.IsVariadic() && !call.IsDDD() { if fntype != nil && fntype.IsVariadic() && !call.IsDDD() {
vi := fntype.NumParams() - 1 vi := fntype.NumParams() - 1
...@@ -849,24 +851,6 @@ func (e *Escape) call(ks []EscHole, call, where *Node) { ...@@ -849,24 +851,6 @@ func (e *Escape) call(ks []EscHole, call, where *Node) {
} }
} }
if call.Op == OCALLFUNC {
// Evaluate callee function expression.
k := e.discardHole()
if where != nil {
if where.Op == ODEFER && e.loopDepth == 1 {
k = e.newLoc(nil, false).asHole()
} else {
k = e.heapHole()
}
}
e.expr(k, call.Left)
}
if recv != nil {
// TODO(mdempsky): Handle go:uintptrescapes here too?
e.expr(recvK, recv)
}
for i, arg := range args { for i, arg := range args {
// For arguments to go:uintptrescapes, peel // For arguments to go:uintptrescapes, peel
// away an unsafe.Pointer->uintptr conversion, // away an unsafe.Pointer->uintptr conversion,
...@@ -881,15 +865,35 @@ func (e *Escape) call(ks []EscHole, call, where *Node) { ...@@ -881,15 +865,35 @@ func (e *Escape) call(ks []EscHole, call, where *Node) {
} }
} }
// no augmentParamHole here; handled in loop before ODDDARG
e.expr(paramKs[i], arg) e.expr(paramKs[i], arg)
} }
} }
// augmentParamHole augments parameter holes as necessary for use in
// go/defer statements.
func (e *Escape) augmentParamHole(k EscHole, where *Node) EscHole {
if where == nil {
return k
}
// Top level defers arguments don't escape to heap, but they
// do need to last until end of function. Tee with a
// non-transient location to avoid arguments from being
// transiently allocated.
if where.Op == ODEFER && e.loopDepth == 1 {
// TODO(mdempsky): Eliminate redundant EscLocation allocs.
return e.teeHole(k, e.newLoc(nil, false).asHole())
}
return e.heapHole()
}
// tagHole returns a hole for evaluating an argument passed to param. // tagHole returns a hole for evaluating an argument passed to param.
// ks should contain the holes representing where the function // ks should contain the holes representing where the function
// callee's results flows; static indicates whether this is a static // callee's results flows; static indicates whether this is a static
// call; where is the OGO/ODEFER context of the call, if any. // call.
func (e *Escape) tagHole(ks []EscHole, param *types.Field, static bool, where *Node) EscHole { func (e *Escape) tagHole(ks []EscHole, param *types.Field, static bool) EscHole {
// If this is a dynamic call, we can't rely on param.Note. // If this is a dynamic call, we can't rely on param.Note.
if !static { if !static {
return e.heapHole() return e.heapHole()
...@@ -902,10 +906,6 @@ func (e *Escape) tagHole(ks []EscHole, param *types.Field, static bool, where *N ...@@ -902,10 +906,6 @@ func (e *Escape) tagHole(ks []EscHole, param *types.Field, static bool, where *N
} }
var tagKs []EscHole var tagKs []EscHole
if where != nil {
tagKs = append(tagKs, e.newLoc(nil, false).asHole())
}
if esc&EscContentEscapes != 0 { if esc&EscContentEscapes != 0 {
tagKs = append(tagKs, e.heapHole().shift(1)) tagKs = append(tagKs, e.heapHole().shift(1))
} }
......
// errorcheck -0 -m
// 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.
package p
func f(...*int) {} // ERROR "can inline f$"
func g() {
defer f() // ERROR "... argument does not escape$"
defer f(new(int)) // ERROR "... argument does not escape$" "new\(int\) does not escape$"
defer f(new(int), new(int)) // ERROR "... argument does not escape$" "new\(int\) does not escape$"
defer f(nil...)
defer f([]*int{}...) // ERROR "\[\]\*int literal does not escape$"
defer f([]*int{new(int)}...) // ERROR "\[\]\*int literal does not escape$" "new\(int\) does not escape$"
defer f([]*int{new(int), new(int)}...) // ERROR "\[\]\*int literal does not escape$" "new\(int\) does not escape$"
go f() // ERROR "... argument escapes to heap$"
go f(new(int)) // ERROR "... argument escapes to heap$" "new\(int\) escapes to heap$"
go f(new(int), new(int)) // ERROR "... argument escapes to heap$" "new\(int\) escapes to heap$"
go f(nil...)
go f([]*int{}...) // ERROR "\[\]\*int literal escapes to heap$"
go f([]*int{new(int)}...) // ERROR "\[\]\*int literal escapes to heap$" "new\(int\) escapes to heap$"
go f([]*int{new(int), new(int)}...) // ERROR "\[\]\*int literal escapes to heap$" "new\(int\) escapes to heap$"
for {
defer f() // ERROR "... argument escapes to heap$"
defer f(new(int)) // ERROR "... argument escapes to heap$" "new\(int\) escapes to heap$"
defer f(new(int), new(int)) // ERROR "... argument escapes to heap$" "new\(int\) escapes to heap$"
defer f(nil...)
defer f([]*int{}...) // ERROR "\[\]\*int literal escapes to heap$"
defer f([]*int{new(int)}...) // ERROR "\[\]\*int literal escapes to heap$" "new\(int\) escapes to heap$"
defer f([]*int{new(int), new(int)}...) // ERROR "\[\]\*int literal escapes to heap$" "new\(int\) escapes to heap$"
go f() // ERROR "... argument escapes to heap$"
go f(new(int)) // ERROR "... argument escapes to heap$" "new\(int\) escapes to heap$"
go f(new(int), new(int)) // ERROR "... argument escapes to heap$" "new\(int\) escapes to heap$"
go f(nil...)
go f([]*int{}...) // ERROR "\[\]\*int literal escapes to heap$"
go f([]*int{new(int)}...) // ERROR "\[\]\*int literal escapes to heap$" "new\(int\) escapes to heap$"
go f([]*int{new(int), new(int)}...) // ERROR "\[\]\*int literal escapes to heap$" "new\(int\) escapes to heap$"
}
}
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