2019-07-25 19:54:03 +00:00
|
|
|
// errorcheck -0 -m -l
|
2015-02-19 12:57:03 +00:00
|
|
|
|
2016-04-10 21:32:26 +00:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
2015-02-19 12:57:03 +00:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Test escape analysis for closure arguments.
|
|
|
|
|
|
|
|
package escape
|
|
|
|
|
|
|
|
var sink interface{}
|
|
|
|
|
|
|
|
func ClosureCallArgs0() {
|
cmd/compile: update escape analysis tests for newescape
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>
2019-04-02 21:44:13 +00:00
|
|
|
x := 0
|
2015-02-19 12:57:03 +00:00
|
|
|
func(p *int) { // ERROR "p does not escape" "func literal does not escape"
|
|
|
|
*p = 1
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs1() {
|
cmd/compile: update escape analysis tests for newescape
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>
2019-04-02 21:44:13 +00:00
|
|
|
x := 0
|
2015-02-19 12:57:03 +00:00
|
|
|
for {
|
|
|
|
func(p *int) { // ERROR "p does not escape" "func literal does not escape"
|
|
|
|
*p = 1
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs2() {
|
|
|
|
for {
|
cmd/compile: update escape analysis tests for newescape
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>
2019-04-02 21:44:13 +00:00
|
|
|
x := 0
|
2015-02-19 12:57:03 +00:00
|
|
|
func(p *int) { // ERROR "p does not escape" "func literal does not escape"
|
|
|
|
*p = 1
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs3() {
|
|
|
|
x := 0 // ERROR "moved to heap: x"
|
|
|
|
func(p *int) { // ERROR "leaking param: p" "func literal does not escape"
|
cmd/compile: silence esc diagnostics about directiface OCONVIFACEs
In general, a conversion to interface type may require values to be
boxed, which in turn necessitates escape analysis to determine whether
the boxed representation can be stack allocated.
However, esc.go used to unconditionally print escape analysis
decisions about OCONVIFACE, even for conversions that don't require
boxing (e.g., pointers, channels, maps, functions).
For test compatibility with esc.go, escape.go similarly printed these
useless diagnostics. This CL removes the diagnostics, and updates test
expectations accordingly.
Change-Id: I97c57a4a08e44d265bba516c78426ff4f2bf1e12
Reviewed-on: https://go-review.googlesource.com/c/go/+/192697
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
2019-08-30 17:56:30 +00:00
|
|
|
sink = p
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs4() {
|
cmd/compile: update escape analysis tests for newescape
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>
2019-04-02 21:44:13 +00:00
|
|
|
x := 0
|
2021-05-26 20:54:31 +00:00
|
|
|
_ = func(p *int) *int { // ERROR "leaking param: p to result ~r0" "func literal does not escape"
|
2015-02-19 12:57:03 +00:00
|
|
|
return p
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs5() {
|
2020-09-22 09:12:03 +00:00
|
|
|
x := 0 // ERROR "moved to heap: x"
|
cmd/compile: update escape analysis tests for newescape
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>
2019-04-02 21:44:13 +00:00
|
|
|
// TODO(mdempsky): We get "leaking param: p" here because the new escape analysis pass
|
|
|
|
// can tell that p flows directly to sink, but it's a little weird. Re-evaluate.
|
cmd/compile: silence esc diagnostics about directiface OCONVIFACEs
In general, a conversion to interface type may require values to be
boxed, which in turn necessitates escape analysis to determine whether
the boxed representation can be stack allocated.
However, esc.go used to unconditionally print escape analysis
decisions about OCONVIFACE, even for conversions that don't require
boxing (e.g., pointers, channels, maps, functions).
For test compatibility with esc.go, escape.go similarly printed these
useless diagnostics. This CL removes the diagnostics, and updates test
expectations accordingly.
Change-Id: I97c57a4a08e44d265bba516c78426ff4f2bf1e12
Reviewed-on: https://go-review.googlesource.com/c/go/+/192697
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
2019-08-30 17:56:30 +00:00
|
|
|
sink = func(p *int) *int { // ERROR "leaking param: p" "func literal does not escape"
|
2015-02-19 12:57:03 +00:00
|
|
|
return p
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs6() {
|
|
|
|
x := 0 // ERROR "moved to heap: x"
|
|
|
|
func(p *int) { // ERROR "moved to heap: p" "func literal does not escape"
|
cmd/compile: silence esc diagnostics about directiface OCONVIFACEs
In general, a conversion to interface type may require values to be
boxed, which in turn necessitates escape analysis to determine whether
the boxed representation can be stack allocated.
However, esc.go used to unconditionally print escape analysis
decisions about OCONVIFACE, even for conversions that don't require
boxing (e.g., pointers, channels, maps, functions).
For test compatibility with esc.go, escape.go similarly printed these
useless diagnostics. This CL removes the diagnostics, and updates test
expectations accordingly.
Change-Id: I97c57a4a08e44d265bba516c78426ff4f2bf1e12
Reviewed-on: https://go-review.googlesource.com/c/go/+/192697
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
2019-08-30 17:56:30 +00:00
|
|
|
sink = &p
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs7() {
|
|
|
|
var pp *int
|
|
|
|
for {
|
|
|
|
x := 0 // ERROR "moved to heap: x"
|
|
|
|
func(p *int) { // ERROR "leaking param: p" "func literal does not escape"
|
|
|
|
pp = p
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
_ = pp
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs8() {
|
cmd/compile: update escape analysis tests for newescape
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>
2019-04-02 21:44:13 +00:00
|
|
|
x := 0
|
2015-02-19 12:57:03 +00:00
|
|
|
defer func(p *int) { // ERROR "p does not escape" "func literal does not escape"
|
|
|
|
*p = 1
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs9() {
|
|
|
|
// BAD: x should not leak
|
|
|
|
x := 0 // ERROR "moved to heap: x"
|
|
|
|
for {
|
|
|
|
defer func(p *int) { // ERROR "func literal escapes to heap" "p does not escape"
|
|
|
|
*p = 1
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs10() {
|
|
|
|
for {
|
|
|
|
x := 0 // ERROR "moved to heap: x"
|
|
|
|
defer func(p *int) { // ERROR "func literal escapes to heap" "p does not escape"
|
|
|
|
*p = 1
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs11() {
|
|
|
|
x := 0 // ERROR "moved to heap: x"
|
|
|
|
defer func(p *int) { // ERROR "leaking param: p" "func literal does not escape"
|
cmd/compile: silence esc diagnostics about directiface OCONVIFACEs
In general, a conversion to interface type may require values to be
boxed, which in turn necessitates escape analysis to determine whether
the boxed representation can be stack allocated.
However, esc.go used to unconditionally print escape analysis
decisions about OCONVIFACE, even for conversions that don't require
boxing (e.g., pointers, channels, maps, functions).
For test compatibility with esc.go, escape.go similarly printed these
useless diagnostics. This CL removes the diagnostics, and updates test
expectations accordingly.
Change-Id: I97c57a4a08e44d265bba516c78426ff4f2bf1e12
Reviewed-on: https://go-review.googlesource.com/c/go/+/192697
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
2019-08-30 17:56:30 +00:00
|
|
|
sink = p
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs12() {
|
cmd/compile: update escape analysis tests for newescape
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>
2019-04-02 21:44:13 +00:00
|
|
|
x := 0
|
2021-05-26 20:54:31 +00:00
|
|
|
defer func(p *int) *int { // ERROR "leaking param: p to result ~r0" "func literal does not escape"
|
2015-02-19 12:57:03 +00:00
|
|
|
return p
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs13() {
|
|
|
|
x := 0 // ERROR "moved to heap: x"
|
|
|
|
defer func(p *int) { // ERROR "moved to heap: p" "func literal does not escape"
|
cmd/compile: silence esc diagnostics about directiface OCONVIFACEs
In general, a conversion to interface type may require values to be
boxed, which in turn necessitates escape analysis to determine whether
the boxed representation can be stack allocated.
However, esc.go used to unconditionally print escape analysis
decisions about OCONVIFACE, even for conversions that don't require
boxing (e.g., pointers, channels, maps, functions).
For test compatibility with esc.go, escape.go similarly printed these
useless diagnostics. This CL removes the diagnostics, and updates test
expectations accordingly.
Change-Id: I97c57a4a08e44d265bba516c78426ff4f2bf1e12
Reviewed-on: https://go-review.googlesource.com/c/go/+/192697
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
2019-08-30 17:56:30 +00:00
|
|
|
sink = &p
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&x)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs14() {
|
cmd/compile: update escape analysis tests for newescape
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>
2019-04-02 21:44:13 +00:00
|
|
|
x := 0
|
|
|
|
p := &x
|
2021-05-26 20:54:31 +00:00
|
|
|
_ = func(p **int) *int { // ERROR "leaking param: p to result ~r0 level=1" "func literal does not escape"
|
2015-02-19 12:57:03 +00:00
|
|
|
return *p
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&p)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClosureCallArgs15() {
|
2020-09-22 09:12:03 +00:00
|
|
|
x := 0 // ERROR "moved to heap: x"
|
cmd/compile: update escape analysis tests for newescape
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>
2019-04-02 21:44:13 +00:00
|
|
|
p := &x
|
cmd/compile: silence esc diagnostics about directiface OCONVIFACEs
In general, a conversion to interface type may require values to be
boxed, which in turn necessitates escape analysis to determine whether
the boxed representation can be stack allocated.
However, esc.go used to unconditionally print escape analysis
decisions about OCONVIFACE, even for conversions that don't require
boxing (e.g., pointers, channels, maps, functions).
For test compatibility with esc.go, escape.go similarly printed these
useless diagnostics. This CL removes the diagnostics, and updates test
expectations accordingly.
Change-Id: I97c57a4a08e44d265bba516c78426ff4f2bf1e12
Reviewed-on: https://go-review.googlesource.com/c/go/+/192697
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
2019-08-30 17:56:30 +00:00
|
|
|
sink = func(p **int) *int { // ERROR "leaking param content: p" "func literal does not escape"
|
2015-02-19 12:57:03 +00:00
|
|
|
return *p
|
2019-04-01 18:58:33 +00:00
|
|
|
}(&p)
|
2015-02-19 12:57:03 +00:00
|
|
|
}
|
2016-03-01 21:53:37 +00:00
|
|
|
|
2019-09-12 17:18:03 +00:00
|
|
|
func ClosureLeak1(s string) string { // ERROR "s does not escape"
|
2016-03-01 21:53:37 +00:00
|
|
|
t := s + "YYYY" // ERROR "escapes to heap"
|
2019-09-12 17:18:03 +00:00
|
|
|
return ClosureLeak1a(t) // ERROR "... argument does not escape"
|
2016-03-01 21:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// See #14409 -- returning part of captured var leaks it.
|
2021-05-26 20:54:31 +00:00
|
|
|
func ClosureLeak1a(a ...string) string { // ERROR "leaking param: a to result ~r0 level=1$"
|
2019-09-12 17:18:03 +00:00
|
|
|
return func() string { // ERROR "func literal does not escape"
|
2016-03-01 21:53:37 +00:00
|
|
|
return a[0]
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-09-12 17:18:03 +00:00
|
|
|
func ClosureLeak2(s string) string { // ERROR "s does not escape"
|
2016-03-01 21:53:37 +00:00
|
|
|
t := s + "YYYY" // ERROR "escapes to heap"
|
2019-09-12 17:18:03 +00:00
|
|
|
c := ClosureLeak2a(t) // ERROR "... argument does not escape"
|
2016-03-01 21:53:37 +00:00
|
|
|
return c
|
|
|
|
}
|
cmd/compile: update escape analysis tests for newescape
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>
2019-04-02 21:44:13 +00:00
|
|
|
func ClosureLeak2a(a ...string) string { // ERROR "leaking param content: a"
|
2019-09-12 17:18:03 +00:00
|
|
|
return ClosureLeak2b(func() string { // ERROR "func literal does not escape"
|
2016-03-01 21:53:37 +00:00
|
|
|
return a[0]
|
|
|
|
})
|
|
|
|
}
|
2019-09-12 17:18:03 +00:00
|
|
|
func ClosureLeak2b(f func() string) string { // ERROR "f does not escape"
|
2016-03-01 21:53:37 +00:00
|
|
|
return f()
|
|
|
|
}
|
2020-09-22 09:12:03 +00:00
|
|
|
|
|
|
|
func ClosureIndirect() {
|
|
|
|
f := func(p *int) {} // ERROR "p does not escape" "func literal does not escape"
|
|
|
|
f(new(int)) // ERROR "new\(int\) does not escape"
|
|
|
|
|
|
|
|
g := f
|
|
|
|
g(new(int)) // ERROR "new\(int\) does not escape"
|
|
|
|
|
|
|
|
h := nopFunc
|
|
|
|
h(new(int)) // ERROR "new\(int\) does not escape"
|
|
|
|
}
|
|
|
|
|
|
|
|
func nopFunc(p *int) {} // ERROR "p does not escape"
|