mirror of
https://github.com/golang/go
synced 2024-11-23 11:10:04 -07:00
a9831633be
The new escape analysis implementation tries to emit debugging diagnostics that are compatible with the existing implementation, but there's a handful of cases that are easier to handle by updating the test expectations instead. For regress tests that need updating, the original file is copied to oldescapeXXX.go.go with -newescape=false added to the //errorcheck line, while the file is updated in place with -newescape=true and new test requirements. Notable test changes: 1) escape_because.go looks for a lot of detailed internal debugging messages that are fairly particular to how esc.go works and that I haven't attempted to port over to escape.go yet. 2) There are a lot of "leaking param: x to result ~r1 level=-1" messages for code like func(p *int) *T { return &T{p} } that were simply wrong. Here &T must be heap allocated unconditionally (because it's being returned); and since p is stored into it, p escapes unconditionally too. esc.go incorrectly reports that p escapes conditionally only if the returned pointer escaped. 3) esc.go used to print each "leaking param" analysis result as it discovered them, which could lead to redundant messages (e.g., that a param leaks at level=0 and level=1). escape.go instead prints everything at the end, once it knows the shortest path to each sink. 4) esc.go didn't precisely model direct-interface types, resulting in some values unnecessarily escaping to the heap when stored into non-escaping interface values. 5) For functions written in assembly, esc.go only printed "does not escape" messages, whereas escape.go prints "does not escape" or "leaking param" as appropriate, consistent with the behavior for functions written in Go. 6) 12 tests included "BAD" annotations identifying cases where esc.go was unnecessarily heap allocating something. These are all fixed by escape.go. Updates #23109. Change-Id: Iabc9eb14c94c9cadde3b183478d1fd54f013502f Reviewed-on: https://go-review.googlesource.com/c/go/+/170447 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com>
175 lines
3.4 KiB
Go
175 lines
3.4 KiB
Go
// errorcheck -0 -m -l -newescape=false
|
|
|
|
// Copyright 2015 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.
|
|
|
|
// Test escape analysis with respect to field assignments.
|
|
|
|
package escape
|
|
|
|
var sink interface{}
|
|
|
|
type X struct {
|
|
p1 *int
|
|
p2 *int
|
|
a [2]*int
|
|
}
|
|
|
|
type Y struct {
|
|
x X
|
|
}
|
|
|
|
func field0() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
var x X
|
|
x.p1 = &i
|
|
sink = x.p1 // ERROR "x\.p1 escapes to heap"
|
|
}
|
|
|
|
func field1() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
var x X
|
|
// BAD: &i should not escape
|
|
x.p1 = &i
|
|
sink = x.p2 // ERROR "x\.p2 escapes to heap"
|
|
}
|
|
|
|
func field3() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
var x X
|
|
x.p1 = &i
|
|
sink = x // ERROR "x escapes to heap"
|
|
}
|
|
|
|
func field4() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
var y Y
|
|
y.x.p1 = &i
|
|
x := y.x
|
|
sink = x // ERROR "x escapes to heap"
|
|
}
|
|
|
|
func field5() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
var x X
|
|
// BAD: &i should not escape here
|
|
x.a[0] = &i
|
|
sink = x.a[1] // ERROR "x\.a\[1\] escapes to heap"
|
|
}
|
|
|
|
// BAD: we are not leaking param x, only x.p2
|
|
func field6(x *X) { // ERROR "leaking param content: x$"
|
|
sink = x.p2 // ERROR "x\.p2 escapes to heap"
|
|
}
|
|
|
|
func field6a() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
var x X
|
|
// BAD: &i should not escape
|
|
x.p1 = &i
|
|
field6(&x)
|
|
}
|
|
|
|
func field7() {
|
|
i := 0
|
|
var y Y
|
|
y.x.p1 = &i
|
|
x := y.x
|
|
var y1 Y
|
|
y1.x = x
|
|
_ = y1.x.p1
|
|
}
|
|
|
|
func field8() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
var y Y
|
|
y.x.p1 = &i
|
|
x := y.x
|
|
var y1 Y
|
|
y1.x = x
|
|
sink = y1.x.p1 // ERROR "y1\.x\.p1 escapes to heap"
|
|
}
|
|
|
|
func field9() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
var y Y
|
|
y.x.p1 = &i
|
|
x := y.x
|
|
var y1 Y
|
|
y1.x = x
|
|
sink = y1.x // ERROR "y1\.x escapes to heap"
|
|
}
|
|
|
|
func field10() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
var y Y
|
|
// BAD: &i should not escape
|
|
y.x.p1 = &i
|
|
x := y.x
|
|
var y1 Y
|
|
y1.x = x
|
|
sink = y1.x.p2 // ERROR "y1\.x\.p2 escapes to heap"
|
|
}
|
|
|
|
func field11() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
x := X{p1: &i}
|
|
sink = x.p1 // ERROR "x\.p1 escapes to heap"
|
|
}
|
|
|
|
func field12() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
// BAD: &i should not escape
|
|
x := X{p1: &i}
|
|
sink = x.p2 // ERROR "x\.p2 escapes to heap"
|
|
}
|
|
|
|
func field13() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
x := &X{p1: &i} // ERROR "field13 &X literal does not escape$"
|
|
sink = x.p1 // ERROR "x\.p1 escapes to heap"
|
|
}
|
|
|
|
func field14() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
// BAD: &i should not escape
|
|
x := &X{p1: &i} // ERROR "field14 &X literal does not escape$"
|
|
sink = x.p2 // ERROR "x\.p2 escapes to heap"
|
|
}
|
|
|
|
func field15() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
x := &X{p1: &i} // ERROR "&X literal escapes to heap$"
|
|
sink = x // ERROR "x escapes to heap"
|
|
}
|
|
|
|
func field16() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
var x X
|
|
// BAD: &i should not escape
|
|
x.p1 = &i
|
|
var iface interface{} = x // ERROR "x escapes to heap"
|
|
x1 := iface.(X)
|
|
sink = x1.p2 // ERROR "x1\.p2 escapes to heap"
|
|
}
|
|
|
|
func field17() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
var x X
|
|
x.p1 = &i
|
|
var iface interface{} = x // ERROR "x escapes to heap"
|
|
x1 := iface.(X)
|
|
sink = x1.p1 // ERROR "x1\.p1 escapes to heap"
|
|
}
|
|
|
|
func field18() {
|
|
i := 0 // ERROR "moved to heap: i$"
|
|
var x X
|
|
// BAD: &i should not escape
|
|
x.p1 = &i
|
|
var iface interface{} = x // ERROR "x escapes to heap"
|
|
y, _ := iface.(Y) // Put X, but extracted Y. The cast will fail, so y is zero initialized.
|
|
sink = y // ERROR "y escapes to heap"
|
|
}
|