1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
// errorcheck -0 -m -l
// Copyright 2012 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, using compiler diagnostic flags, that the escape analysis is working.
// Compiles but does not run. Inlining is disabled.
package foo
import (
"runtime"
"unsafe"
)
func noleak(p *int) int { // ERROR "p does not escape"
return *p
}
func leaktoret(p *int) *int { // ERROR "leaking param: p to result"
return p
}
func leaktoret2(p *int) (*int, *int) { // ERROR "leaking param: p to result ~r1" "leaking param: p to result ~r2"
return p, p
}
func leaktoret22(p, q *int) (*int, *int) { // ERROR "leaking param: p to result ~r2" "leaking param: q to result ~r3"
return p, q
}
func leaktoret22b(p, q *int) (*int, *int) { // ERROR "leaking param: p to result ~r3" "leaking param: q to result ~r2"
return leaktoret22(q, p)
}
func leaktoret22c(p, q *int) (*int, *int) { // ERROR "leaking param: p to result ~r3" "leaking param: q to result ~r2"
r, s := leaktoret22(q, p)
return r, s
}
func leaktoret22d(p, q *int) (r, s *int) { // ERROR "leaking param: p to result s" "leaking param: q to result r"
r, s = leaktoret22(q, p)
return
}
func leaktoret22e(p, q *int) (r, s *int) { // ERROR "leaking param: p to result s" "leaking param: q to result r"
r, s = leaktoret22(q, p)
return r, s
}
func leaktoret22f(p, q *int) (r, s *int) { // ERROR "leaking param: p to result s" "leaking param: q to result r"
rr, ss := leaktoret22(q, p)
return rr, ss
}
var gp *int
func leaktosink(p *int) *int { // ERROR "leaking param: p"
gp = p
return p
}
func f1() {
var x int
p := noleak(&x)
_ = p
}
func f2() {
var x int
p := leaktoret(&x)
_ = p
}
func f3() {
var x int // ERROR "moved to heap: x"
p := leaktoret(&x)
gp = p
}
func f4() {
var x int // ERROR "moved to heap: x"
p, q := leaktoret2(&x)
gp = p
gp = q
}
func f5() {
var x int
leaktoret22(leaktoret2(&x))
}
func f6() {
var x int // ERROR "moved to heap: x"
px1, px2 := leaktoret22(leaktoret2(&x))
gp = px1
_ = px2
}
type T struct{ x int }
func (t *T) Foo(u int) (*T, bool) { // ERROR "leaking param: t to result"
t.x += u
return t, true
}
func f7() *T {
r, _ := new(T).Foo(42) // ERROR "new.T. escapes to heap"
return r
}
func leakrecursive1(p, q *int) (*int, *int) { // ERROR "leaking param: p" "leaking param: q"
return leakrecursive2(q, p)
}
func leakrecursive2(p, q *int) (*int, *int) { // ERROR "leaking param: p" "leaking param: q"
if *p > *q {
return leakrecursive1(q, p)
}
// without this, leakrecursive? are safe for p and q, b/c in fact their graph does not have leaking edges.
return p, q
}
var global interface{}
type T1 struct {
X *int
}
type T2 struct {
Y *T1
}
func f8(p *T1) (k T2) { // ERROR "leaking param: p$"
if p == nil {
k = T2{}
return
}
// should make p leak always
global = p
return T2{p}
}
func f9() {
var j T1 // ERROR "moved to heap: j"
f8(&j)
}
func f10() {
// These don't escape but are too big for the stack
var x [1 << 30]byte // ERROR "moved to heap: x"
var y = make([]byte, 1<<30) // ERROR "make\(\[\]byte, 1 << 30\) escapes to heap"
_ = x[0] + y[0]
}
// Test for issue 19687 (passing to unnamed parameters does not escape).
func f11(**int) {
}
func f12(_ **int) {
}
func f13() {
var x *int
f11(&x)
f12(&x)
runtime.KeepAlive(&x)
}
// Test for issue 24305 (passing to unnamed receivers does not escape).
type U int
func (*U) M() {}
func (_ *U) N() {}
func _() {
var u U
u.M()
u.N()
}
func fbad24305() {
// BAD u should not be heap allocated
var u U // ERROR "moved to heap: u"
(*U).M(&u)
(*U).N(&u)
}
// Issue 24730: taking address in a loop causes unnecessary escape
type T24730 struct {
x [64]byte
}
func (t *T24730) g() { // ERROR "t does not escape"
y := t.x[:]
for i := range t.x[:] {
y = t.x[:]
y[i] = 1
}
var z *byte
for i := range t.x[:] {
z = &t.x[i]
*z = 2
}
}
// Issue 15730: copy causes unnecessary escape
var sink []byte
var sink2 []int
var sink3 []*int
func f15730a(args ...interface{}) { // ERROR "args does not escape"
for _, arg := range args {
switch a := arg.(type) {
case string:
copy(sink, a)
}
}
}
func f15730b(args ...interface{}) { // ERROR "args does not escape"
for _, arg := range args {
switch a := arg.(type) {
case []int:
copy(sink2, a)
}
}
}
func f15730c(args ...interface{}) { // ERROR "leaking param content: args"
for _, arg := range args {
switch a := arg.(type) {
case []*int:
// copy pointerful data should cause escape
copy(sink3, a)
}
}
}
// Issue 29000: unnamed parameter is not handled correctly
var sink4 interface{}
var alwaysFalse = false
func f29000(_ int, x interface{}) { // ERROR "leaking param: x"
sink4 = x
if alwaysFalse {
g29000()
}
}
func g29000() {
x := 1
f29000(2, x) // ERROR "x escapes to heap"
}
// Issue 28369: taking an address of a parameter and converting it into a uintptr causes an
// unnecessary escape.
var sink28369 uintptr
func f28369(n int) int {
if n == 0 {
sink28369 = uintptr(unsafe.Pointer(&n))
return n
}
return 1 + f28369(n-1)
}
// Issue 44614: parameters that flow to a heap-allocated result
// parameter must be recorded as a heap-flow rather than a
// result-flow.
// N.B., must match "leaking param: p",
// but *not* "leaking param: p to result r level=0".
func f(p *int) (r *int) { // ERROR "leaking param: p$" "moved to heap: r"
sink4 = &r
return p
}
|