Commit 7fbb1b36 authored by David Chase's avatar David Chase

cmd/internal/gc: improve flow of input params to output params

This includes the following information in the per-function summary:

outK = paramJ   encoded in outK bits for paramJ
outK = *paramJ  encoded in outK bits for paramJ
heap = paramJ   EscHeap
heap = *paramJ  EscContentEscapes

Note that (currently) if the address of a parameter is taken and
returned, necessarily a heap allocation occurred to contain that
reference, and the heap can never refer to stack, therefore the
parameter and everything downstream from it escapes to the heap.

The per-function summary information now has a tuneable number of bits
(2 is probably noticeably better than 1, 3 is likely overkill, but it
is now easy to check and the -m debugging output includes information
that allows you to figure out if more would be better.)

A new test was  added to check pointer flow through struct-typed and
*struct-typed parameters and returns; some of these are sensitive to
the number of summary bits, and ought to yield better results with a
more competent escape analysis algorithm.  Another new test checks
(some) correctness with array parameters, results, and operations.

The old analysis inferred a piece of plan9 runtime was non-escaping by
counteracting overconservative analysis with buggy analysis; with the
bug fixed, the result was too conservative (and it's not easy to fix
in this framework) so the source code was tweaked to get the desired
result.  A test was added against the discovered bug.

The escape analysis was further improved splitting the "level" into
3 parts, one tracking the conventional "level" and the other two
computing the highest-level-suffix-from-copy, which is used to
generally model the cancelling effect of indirection applied to
address-of.

With the improved escape analysis enabled, it was necessary to
modify one of the runtime tests because it now attempts to allocate
too much on the (small, fixed-size) G0 (system) stack and this
failed the test.

Compiling src/std after touching src/runtime/*.go with -m logging
turned on shows 420 fewer heap allocation sites (10538 vs 10968).

Profiling allocations in src/html/template with
for i in {1..5} ;
  do go tool 6g -memprofile=mastx.${i}.prof  -memprofilerate=1 *.go;
  go tool pprof -alloc_objects -text  mastx.${i}.prof ;
done

showed a 15% reduction in allocations performed by the compiler.

Update #3753
Update #4720
Fixes #10466

Change-Id: I0fd97d5f5ac527b45f49e2218d158a6e89951432
Reviewed-on: https://go-review.googlesource.com/8202
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent 4044aded
This diff is collapsed.
......@@ -23,11 +23,10 @@ func Sysfunc(name string) *Node {
return n
}
/*
* the address of n has been taken and might be used after
* the current function returns. mark any local vars
* as needing to move to the heap.
*/
// addrescapes tags node n as having had its address taken
// by "increasing" the "value" of n.Esc to EscHeap.
// Storage is allocated as necessary to allow the address
// to be taken.
func addrescapes(n *Node) {
switch n.Op {
// probably a type error already.
......@@ -50,7 +49,7 @@ func addrescapes(n *Node) {
case PPARAMREF:
addrescapes(n.Defn)
// if func param, need separate temporary
// if func param, need separate temporary
// to hold heap pointer.
// the function type has already been checked
// (we're in the function body)
......@@ -93,12 +92,12 @@ func addrescapes(n *Node) {
case OIND, ODOTPTR:
break
// ODOTPTR has already been introduced,
// ODOTPTR has already been introduced,
// so these are the non-pointer ODOT and OINDEX.
// In &x[0], if x is a slice, then x does not
// escape--the pointer inside x does, but that
// is always a heap pointer anyway.
case ODOT, OINDEX:
case ODOT, OINDEX, OPAREN, OCONVNOP:
if !Isslice(n.Left.Type) {
addrescapes(n.Left)
}
......
......@@ -214,19 +214,6 @@ type InitPlan struct {
E []InitEntry
}
const (
EscUnknown = iota
EscHeap
EscScope
EscNone
EscReturn
EscNever
EscBits = 3
EscMask = (1 << EscBits) - 1
EscContentEscapes = 1 << EscBits // value obtained by indirect of parameter escapes to some returned result
EscReturnBits = EscBits + 1
)
const (
SymExport = 1 << 0 // to be exported
SymPackage = 1 << 1
......
......@@ -44,15 +44,15 @@ type Node struct {
Isddd bool // is the argument variadic
Readonly bool
Implicit bool
Addrtaken bool // address taken, even if not moved to heap
Assigned bool // is the variable ever assigned to
Captured bool // is the variable captured by a closure
Byval bool // is the variable captured by value or by reference
Reslice bool // this is a reslice x = x[0:y] or x = append(x, ...)
Likely int8 // likeliness of if statement
Hasbreak bool // has break statement
Needzero bool // if it contains pointers, needs to be zeroed on function entry
Esc uint8 // EscXXX
Addrtaken bool // address taken, even if not moved to heap
Assigned bool // is the variable ever assigned to
Captured bool // is the variable captured by a closure
Byval bool // is the variable captured by value or by reference
Reslice bool // this is a reslice x = x[0:y] or x = append(x, ...)
Likely int8 // likeliness of if statement
Hasbreak bool // has break statement
Needzero bool // if it contains pointers, needs to be zeroed on function entry
Esc uint16 // EscXXX
Funcdepth int32
// most nodes
......@@ -103,14 +103,14 @@ type Node struct {
Escloopdepth int // -1: global, 0: return variables, 1:function top level, increased inside function for every loop or label to mark scopes
Sym *Sym // various
Vargen int32 // unique name for OTYPE/ONAME
Vargen int32 // unique name for OTYPE/ONAME within a function. Function outputs are numbered starting at one.
Lineno int32
Xoffset int64
Stkdelta int64 // offset added by stack frame compaction phase.
Ostk int32 // 6g only
Iota int32
Walkgen uint32
Esclevel int32
Esclevel Level
Opt interface{} // for optimization passes
}
......
......@@ -1777,7 +1777,7 @@ func ascompatet(op int, nl *NodeList, nr **Type, fp int, init **NodeList) *NodeL
* package all the arguments that match a ... T parameter into a []T.
*/
func mkdotargslice(lr0 *NodeList, nn *NodeList, l *Type, fp int, init **NodeList, ddd *Node) *NodeList {
esc := uint8(EscUnknown)
esc := uint16(EscUnknown)
if ddd != nil {
esc = ddd.Esc
}
......
......@@ -3531,9 +3531,13 @@ func testSchedLocalQueue() {
}
}
var pSink *p
func testSchedLocalQueueSteal() {
p1 := new(p)
p2 := new(p)
pSink = p1 // Force to heap, too large to allocate on system stack ("G0 stack")
pSink = p2 // Force to heap, too large to allocate on system stack ("G0 stack")
gs := make([]g, len(p1.runq))
for i := 0; i < len(p1.runq); i++ {
for j := 0; j < i; j++ {
......
......@@ -35,10 +35,8 @@ var maxstring uintptr = 256 // a hint for print
//go:nosplit
func gostringnocopy(str *byte) string {
var s string
sp := (*stringStruct)(unsafe.Pointer(&s))
sp.str = unsafe.Pointer(str)
sp.len = findnull(str)
ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
s := *(*string)(unsafe.Pointer(&ss))
for {
ms := maxstring
if uintptr(len(s)) <= ms || casuintptr(&maxstring, ms, uintptr(len(s))) {
......
This diff is collapsed.
This diff is collapsed.
// errorcheck -0 -m -l
// 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 for function parameters.
// In this test almost everything is BAD except the simplest cases
// where input directly flows to output.
package foo
var Ssink *string
type U [2]*string
func bar(a, b *string) U { // ERROR "leaking param: a to result ~r2 level=0$" "leaking param: b to result ~r2 level=0$"
return U{a, b}
}
func foo(x U) U { // ERROR "leaking param: x to result ~r1 level=0$"
return U{x[1], x[0]}
}
func bff(a, b *string) U { // ERROR "leaking param: a to result ~r2 level=0$" "leaking param: b to result ~r2 level=0$"
return foo(foo(bar(a, b)))
}
func tbff1() *string {
a := "cat"
b := "dog" // ERROR "moved to heap: b$"
u := bff(&a, &b) // ERROR "tbff1 &a does not escape$" "tbff1 &b does not escape$"
_ = u[0]
return &b // ERROR "&b escapes to heap$"
}
// BAD: need fine-grained analysis to track u[0] and u[1] differently.
func tbff2() *string {
a := "cat" // ERROR "moved to heap: a$"
b := "dog" // ERROR "moved to heap: b$"
u := bff(&a, &b) // ERROR "&a escapes to heap$" "&b escapes to heap$"
_ = u[0]
return u[1]
}
func car(x U) *string { // ERROR "leaking param: x to result ~r1 level=0$"
return x[0]
}
// BAD: need fine-grained analysis to track x[0] and x[1] differently.
func fun(x U, y *string) *string { // ERROR "leaking param: x to result ~r2 level=0$" "leaking param: y to result ~r2 level=0$"
x[0] = y
return x[1]
}
func fup(x *U, y *string) *string { // ERROR "leaking param: x to result ~r2 level=1$" "leaking param: y$"
x[0] = y // leaking y to heap is intended
return x[1]
}
// BAD: would be nice to record that *y (content) is what leaks, not y itself
func fum(x *U, y **string) *string { // ERROR "leaking param: x to result ~r2 level=1$" "leaking param content: y$"
x[0] = *y
return x[1]
}
// BAD: would be nice to record that y[0] (content) is what leaks, not y itself
func fuo(x *U, y *U) *string { // ERROR "leaking param: x to result ~r2 level=1$" "leaking param content: y$"
x[0] = y[0]
return x[1]
}
// errorcheck -0 -m -l
// 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 for function parameters.
// In this test almost everything is BAD except the simplest cases
// where input directly flows to output.
package foo
func f(buf []byte) []byte { // ERROR "leaking param: buf to result ~r1 level=0$"
return buf
}
func g(*byte) string
func h(e int) {
var x [32]byte // ERROR "moved to heap: x$"
g(&f(x[:])[0]) // ERROR "&f\(x\[:\]\)\[0\] escapes to heap$" "x escapes to heap$"
}
type Node struct {
s string
left, right *Node
}
func walk(np **Node) int { // ERROR "leaking param content: np"
n := *np
w := len(n.s)
if n == nil {
return 0
}
wl := walk(&n.left) // ERROR "walk &n.left does not escape"
wr := walk(&n.right) // ERROR "walk &n.right does not escape"
if wl < wr {
n.left, n.right = n.right, n.left
wl, wr = wr, wl
}
*np = n
return w + wl + wr
}
......@@ -131,7 +131,7 @@ func ClosureCallArgs14() {
x := 0 // ERROR "moved to heap: x"
// BAD: &x should not escape here
p := &x // ERROR "moved to heap: p" "&x escapes to heap"
_ = func(p **int) *int { // ERROR "leaking param p content to result ~r1" "func literal does not escape"
_ = func(p **int) *int { // ERROR "leaking param: p to result ~r1 level=1" "func literal does not escape"
return *p
// BAD: p should not escape here
}(&p) // ERROR "&p escapes to heap"
......@@ -140,7 +140,7 @@ func ClosureCallArgs14() {
func ClosureCallArgs15() {
x := 0 // ERROR "moved to heap: x"
p := &x // ERROR "moved to heap: p" "&x escapes to heap"
sink = func(p **int) *int { // ERROR "leaking param p content to result ~r1" "func literal does not escape"
sink = func(p **int) *int { // ERROR "leaking param: p to result ~r1 level=1" "func literal does not escape"
return *p
// BAD: p should not escape here
}(&p) // ERROR "&p escapes to heap" "\(func literal\)\(&p\) escapes to heap"
......
......@@ -23,7 +23,7 @@ type Y struct {
func field0() {
i := 0 // ERROR "moved to heap: i$"
var x X
x.p1 = &i // ERROR "&i escapes to heap$"
x.p1 = &i // ERROR "&i escapes to heap$"
sink = x.p1 // ERROR "x\.p1 escapes to heap"
}
......@@ -31,7 +31,7 @@ func field1() {
i := 0 // ERROR "moved to heap: i$"
var x X
// BAD: &i should not escape
x.p1 = &i // ERROR "&i escapes to heap$"
x.p1 = &i // ERROR "&i escapes to heap$"
sink = x.p2 // ERROR "x\.p2 escapes to heap"
}
......@@ -39,7 +39,7 @@ func field3() {
i := 0 // ERROR "moved to heap: i$"
var x X
x.p1 = &i // ERROR "&i escapes to heap$"
sink = x // ERROR "x escapes to heap"
sink = x // ERROR "x escapes to heap"
}
func field4() {
......@@ -54,22 +54,21 @@ func field5() {
i := 0 // ERROR "moved to heap: i$"
var x X
// BAD: &i should not escape here
x.a[0] = &i // ERROR "&i escapes to heap$"
x.a[0] = &i // ERROR "&i escapes to heap$"
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: x$"
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 // ERROR "moved to heap: x$"
i := 0 // ERROR "moved to heap: i$"
var x X
// BAD: &i should not escape
x.p1 = &i // ERROR "&i escapes to heap$"
// BAD: &x should not escape
field6(&x) // ERROR "&x escapes to heap$"
x.p1 = &i // ERROR "&i escapes to heap$"
field6(&x) // ERROR "field6a &x does not escape"
}
func field7() {
......@@ -116,40 +115,40 @@ func field10() {
func field11() {
i := 0 // ERROR "moved to heap: i$"
x := X{p1: &i} // ERROR "&i escapes to heap$"
sink = x.p1 // ERROR "x\.p1 escapes to heap"
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} // ERROR "&i escapes to heap$"
sink = x.p2 // ERROR "x\.p2 escapes to heap"
sink = x.p2 // ERROR "x\.p2 escapes to heap"
}
func field13() {
i := 0 // ERROR "moved to heap: i$"
x := &X{p1: &i} // ERROR "&i escapes to heap$" "field13 &X literal does not escape$"
sink = x.p1 // ERROR "x\.p1 escapes to heap"
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 "&i escapes to heap$" "field14 &X literal does not escape$"
sink = x.p2 // ERROR "x\.p2 escapes to heap"
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$" "&i escapes to heap$"
sink = x // ERROR "x 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 // ERROR "&i escapes to heap$"
x.p1 = &i // ERROR "&i escapes to heap$"
var iface interface{} = x // ERROR "x escapes to heap"
x1 := iface.(X)
sink = x1.p2 // ERROR "x1\.p2 escapes to heap"
......@@ -158,7 +157,7 @@ func field16() {
func field17() {
i := 0 // ERROR "moved to heap: i$"
var x X
x.p1 = &i // ERROR "&i escapes to heap$"
x.p1 = &i // ERROR "&i escapes to heap$"
var iface interface{} = x // ERROR "x escapes to heap"
x1 := iface.(X)
sink = x1.p1 // ERROR "x1\.p1 escapes to heap"
......@@ -168,8 +167,8 @@ func field18() {
i := 0 // ERROR "moved to heap: i$"
var x X
// BAD: &i should not escape
x.p1 = &i // ERROR "&i escapes to heap$"
x.p1 = &i // ERROR "&i escapes to heap$"
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"
y, _ := iface.(Y) // Put X, but extracted Y. The cast will fail, so y is zero initialized.
sink = y // ERROR "y escapes to heap"
}
......@@ -54,14 +54,14 @@ func constptr1() {
i := 0 // ERROR "moved to heap: i"
x := &ConstPtr{} // ERROR "&ConstPtr literal escapes to heap"
x.p = &i // ERROR "&i escapes to heap"
sink = x // ERROR "x escapes to heap"
sink = x // ERROR "x escapes to heap"
}
func constptr2() {
i := 0 // ERROR "moved to heap: i"
x := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
x.p = &i // ERROR "&i escapes to heap"
sink = *x// ERROR "\*x escapes to heap"
sink = *x // ERROR "\*x escapes to heap"
}
func constptr4() *ConstPtr {
......@@ -78,7 +78,7 @@ func constptr5() *ConstPtr {
}
// BAD: p should not escape here
func constptr6(p *ConstPtr) { // ERROR "leaking param: p"
func constptr6(p *ConstPtr) { // ERROR "leaking param content: p"
p1 := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
*p1 = *p
_ = p1
......@@ -151,3 +151,10 @@ func foo2() {
i := 0 // ERROR "moved to heap: i"
*p = &i // ERROR "&i escapes to heap"
}
var global *byte
func f() {
var x byte // ERROR "moved to heap: x"
global = &*&x // ERROR "&\(\*\(&x\)\) escapes to heap" "&x escapes to heap"
}
......@@ -27,18 +27,18 @@ func level1() {
}
func level2() {
i := 0 // ERROR "moved to heap: i"
p0 := &i // ERROR "moved to heap: p0" "&i escapes to heap"
p1 := &p0 // ERROR "&p0 escapes to heap"
p2 := &p1 // ERROR "&p1 does not escape"
i := 0 // ERROR "moved to heap: i"
p0 := &i // ERROR "moved to heap: p0" "&i escapes to heap"
p1 := &p0 // ERROR "&p0 escapes to heap"
p2 := &p1 // ERROR "&p1 does not escape"
sink = *p2 // ERROR "\*p2 escapes to heap"
}
func level3() {
i := 0 // ERROR "moved to heap: i"
p0 := &i // ERROR "&i escapes to heap"
p1 := &p0 // ERROR "&p0 does not escape"
p2 := &p1 // ERROR "&p1 does not escape"
i := 0 // ERROR "moved to heap: i"
p0 := &i // ERROR "&i escapes to heap"
p1 := &p0 // ERROR "&p0 does not escape"
p2 := &p1 // ERROR "&p1 does not escape"
sink = **p2 // ERROR "\* \(\*p2\) escapes to heap"
}
......@@ -67,10 +67,10 @@ func level6() {
}
func level7() {
i := 0 // ERROR "moved to heap: i"
p0 := &i // ERROR "moved to heap: p0" "&i escapes to heap"
// BAD: p0 should not escape here
p1 := &p0 // ERROR "&p0 escapes to heap"
i := 0 // ERROR "moved to heap: i"
p0 := &i // ERROR "&i escapes to heap"
p1 := &p0 // ERROR "&p0 does not escape"
// note *p1 == &i
p2 := *p1 // ERROR "moved to heap: p2"
sink = &p2 // ERROR "&p2 escapes to heap"
}
......@@ -95,7 +95,7 @@ func level10() {
i := 0
p0 := &i // ERROR "&i does not escape"
p1 := *p0
p2 := &p1 // ERROR "&p1 does not escape"
p2 := &p1 // ERROR "&p1 does not escape"
sink = *p2 // ERROR "\*p2 escapes to heap"
}
......
......@@ -14,7 +14,7 @@ package escape
var sink interface{}
// in -> out
func param0(p *int) *int { // ERROR "leaking param: p to result ~r1$"
func param0(p *int) *int { // ERROR "leaking param: p to result ~r1"
return p
}
......@@ -29,7 +29,7 @@ func caller0b() {
}
// in, in -> out, out
func param1(p1, p2 *int) (*int, *int) { // ERROR "leaking param: p1 to result ~r2$" "leaking param: p2 to result ~r3$"
func param1(p1, p2 *int) (*int, *int) { // ERROR "leaking param: p1 to result ~r2" "leaking param: p2 to result ~r3"
return p1, p2
}
......@@ -64,23 +64,23 @@ type Pair struct {
p2 *int
}
func param3(p *Pair) { // ERROR "leaking param: p$"
func param3(p *Pair) { // ERROR "leaking param content: p$"
p.p1 = p.p2
}
func caller3a() {
i := 0 // ERROR "moved to heap: i$"
j := 0 // ERROR "moved to heap: j$"
p := Pair{&i, &j} // ERROR "&i escapes to heap$" "&j escapes to heap$" "moved to heap: p$"
param3(&p) // ERROR "&p escapes to heap$"
p := Pair{&i, &j} // ERROR "&i escapes to heap$" "&j escapes to heap$"
param3(&p) // ERROR "caller3a &p does not escape"
_ = p
}
func caller3b() {
i := 0 // ERROR "moved to heap: i$"
j := 0 // ERROR "moved to heap: j$"
p := Pair{&i, &j} // ERROR "&i escapes to heap$" "&j escapes to heap$" "moved to heap: p$"
param3(&p) // ERROR "&p escapes to heap$"
p := Pair{&i, &j} // ERROR "&i escapes to heap$" "&j escapes to heap$"
param3(&p) // ERROR "caller3b &p does not escape"
sink = p // ERROR "p escapes to heap$"
}
......@@ -114,27 +114,27 @@ func caller5() {
}
// *in -> heap
func param6(i ***int) { // ERROR "leaking param: i$"
func param6(i ***int) { // ERROR "leaking param content: i$"
sink = *i // ERROR "\*i escapes to heap$"
}
func caller6a() {
i := 0 // ERROR "moved to heap: i$"
p := &i // ERROR "&i escapes to heap$" "moved to heap: p$"
p2 := &p // ERROR "&p escapes to heap$" "moved to heap: p2$"
param6(&p2) // ERROR "&p2 escapes to heap$"
p2 := &p // ERROR "&p escapes to heap$"
param6(&p2) // ERROR "caller6a &p2 does not escape"
}
// **in -> heap
func param7(i ***int) { // ERROR "leaking param: i$"
func param7(i ***int) { // ERROR "leaking param content: i$"
sink = **i // ERROR "\* \(\*i\) escapes to heap"
}
func caller7() {
i := 0 // ERROR "moved to heap: i$"
p := &i // ERROR "&i escapes to heap$" "moved to heap: p$"
p2 := &p // ERROR "&p escapes to heap$" "moved to heap: p2$"
param7(&p2) // ERROR "&p2 escapes to heap$"
p2 := &p // ERROR "&p escapes to heap$"
param7(&p2) // ERROR "caller7 &p2 does not escape"
}
// **in -> heap
......@@ -149,14 +149,14 @@ func caller8() {
}
// *in -> out
func param9(p ***int) **int { // ERROR "param9 leaking param p content to result ~r1$"
func param9(p ***int) **int { // ERROR "leaking param: p to result ~r1 level=1"
return *p
}
func caller9a() {
i := 0 // ERROR "moved to heap: i$"
p := &i // ERROR "&i escapes to heap$" "moved to heap: p$"
p2 := &p // ERROR "&p escapes to heap$"
i := 0
p := &i // ERROR "caller9a &i does not escape"
p2 := &p // ERROR "caller9a &p does not escape"
_ = param9(&p2) // ERROR "caller9a &p2 does not escape$"
}
......@@ -168,33 +168,33 @@ func caller9b() {
}
// **in -> out
func param10(p ***int) *int { // ERROR "param10 leaking param p content to result ~r1$"
func param10(p ***int) *int { // ERROR "leaking param: p to result ~r1 level=2"
return **p
}
func caller10a() {
i := 0 // ERROR "moved to heap: i$"
p := &i // ERROR "&i escapes to heap$" "moved to heap: p$"
p2 := &p // ERROR "&p escapes to heap$"
i := 0
p := &i // ERROR "caller10a &i does not escape"
p2 := &p // ERROR "caller10a &p does not escape"
_ = param10(&p2) // ERROR "caller10a &p2 does not escape$"
}
func caller10b() {
i := 0 // ERROR "moved to heap: i$"
p := &i // ERROR "&i escapes to heap$" "moved to heap: p$"
p2 := &p // ERROR "&p escapes to heap$"
p := &i // ERROR "&i escapes to heap$"
p2 := &p // ERROR "caller10b &p does not escape$"
sink = param10(&p2) // ERROR "caller10b &p2 does not escape$" "param10\(&p2\) escapes to heap"
}
// &in -> out
// in escapes to heap (address of param taken and returned)
func param11(i **int) ***int { // ERROR "moved to heap: i$"
return &i // ERROR "&i escapes to heap$"
}
func caller11a() {
i := 0 // ERROR "moved to heap: i$"
p := &i // ERROR "&i escapes to heap$" "moved to heap: p$"
_ = param11(&p) // ERROR "&p escapes to heap$"
i := 0 // ERROR "moved to heap: i"
p := &i // ERROR "moved to heap: p" "&i escapes to heap"
_ = param11(&p) // ERROR "&p escapes to heap"
}
func caller11b() {
......@@ -203,10 +203,17 @@ func caller11b() {
sink = param11(&p) // ERROR "&p escapes to heap$" "param11\(&p\) escapes to heap"
}
func caller11c() {
func caller11c() { // GOOD
i := 0 // ERROR "moved to heap: i$"
p := &i // ERROR "&i escapes to heap$" "moved to heap: p$"
sink = *param11(&p) // ERROR "&p escapes to heap$" "\*param11\(&p\) escapes to heap"
p := &i // ERROR "moved to heap: p" "&i escapes to heap"
sink = *param11(&p) // ERROR "&p escapes to heap" "\*param11\(&p\) escapes to heap"
}
func caller11d() {
i := 0 // ERROR "moved to heap: i$"
p := &i // ERROR "&i escapes to heap" "moved to heap: p"
p2 := &p // ERROR "&p escapes to heap"
sink = param11(p2) // ERROR "param11\(p2\) escapes to heap"
}
// &in -> rcvr
......@@ -324,3 +331,23 @@ func caller13h() {
v.param13(&i) // ERROR "&i escapes to heap$"
sink = **v.p // ERROR "\* \(\*v\.p\) escapes to heap"
}
type Node struct {
p *Node
}
var Sink *Node
func f(x *Node) { // ERROR "leaking param content: x"
Sink = &Node{x.p} // ERROR "&Node literal escapes to heap"
}
func g(x *Node) *Node { // ERROR "leaking param: x to result ~r1 level=0"
return &Node{x.p} // ERROR "&Node literal escapes to heap"
}
func h(x *Node) { // ERROR "leaking param: x"
y := &Node{x} // ERROR "h &Node literal does not escape"
Sink = g(y)
f(y)
}
This diff is collapsed.
This diff is collapsed.
// errorcheck -0 -m -l
// 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 for function parameters.
package foo
var Ssink *string
type U struct {
_sp *string
_spp **string
}
func A(sp *string, spp **string) U { // ERROR "leaking param: sp to result ~r2 level=0$" "leaking param: spp to result ~r2 level=0$"
return U{sp, spp}
}
func B(spp **string) U { // ERROR "leaking param: spp to result ~r1 level=0$" "leaking param: spp to result ~r1 level=1$"
return U{*spp, spp}
}
func tA1() {
s := "cat"
sp := &s // ERROR "tA1 &s does not escape$"
spp := &sp // ERROR "tA1 &sp does not escape$"
u := A(sp, spp)
_ = u
println(s)
}
func tA2() {
s := "cat"
sp := &s // ERROR "tA2 &s does not escape$"
spp := &sp // ERROR "tA2 &sp does not escape$"
u := A(sp, spp)
println(*u._sp)
}
func tA3() {
s := "cat"
sp := &s // ERROR "tA3 &s does not escape$"
spp := &sp // ERROR "tA3 &sp does not escape$"
u := A(sp, spp)
println(**u._spp)
}
func tB1() {
s := "cat"
sp := &s // ERROR "tB1 &s does not escape$"
spp := &sp // ERROR "tB1 &sp does not escape$"
u := B(spp)
_ = u
println(s)
}
func tB2() {
s := "cat"
sp := &s // ERROR "tB2 &s does not escape$"
spp := &sp // ERROR "tB2 &sp does not escape$"
u := B(spp)
println(*u._sp)
}
func tB3() {
s := "cat"
sp := &s // ERROR "tB3 &s does not escape$"
spp := &sp // ERROR "tB3 &sp does not escape$"
u := B(spp)
println(**u._spp)
}
......@@ -45,5 +45,5 @@ func growstack(x int) {
if x == 0 {
return
}
growstack(x-1)
growstack(x - 1)
}
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