summaryrefslogtreecommitdiff
path: root/test/escape_struct_param1.go
blob: 496172c166ac1bc9c5c9cd96f859e342d3ca9afc (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// 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 *struct function parameters.
// Note companion strict_param2 checks struct function parameters with similar tests.

package notmain

var Ssink *string

type U struct {
	_sp  *string
	_spp **string
}

type V struct {
	_u   U
	_up  *U
	_upp **U
}

func (u *U) SP() *string { // ERROR "leaking param: u to result ~r0 level=1$"
	return u._sp
}

func (u *U) SPP() **string { // ERROR "leaking param: u to result ~r0 level=1$"
	return u._spp
}

func (u *U) SPPi() *string { // ERROR "leaking param: u to result ~r0 level=2$"
	return *u._spp
}

func tSPPi() {
	s := "cat" // ERROR "moved to heap: s$"
	ps := &s
	pps := &ps
	pu := &U{ps, pps} // ERROR "&U{...} does not escape$"
	Ssink = pu.SPPi()
}

func tiSPP() {
	s := "cat" // ERROR "moved to heap: s$"
	ps := &s
	pps := &ps
	pu := &U{ps, pps} // ERROR "&U{...} does not escape$"
	Ssink = *pu.SPP()
}

// BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of ps
func tSP() {
	s := "cat" // ERROR "moved to heap: s$"
	ps := &s   // ERROR "moved to heap: ps$"
	pps := &ps
	pu := &U{ps, pps} // ERROR "&U{...} does not escape$"
	Ssink = pu.SP()
}

func (v *V) u() U { // ERROR "leaking param: v to result ~r0 level=1$"
	return v._u
}

func (v *V) UP() *U { // ERROR "leaking param: v to result ~r0 level=1$"
	return v._up
}

func (v *V) UPP() **U { // ERROR "leaking param: v to result ~r0 level=1$"
	return v._upp
}

func (v *V) UPPia() *U { // ERROR "leaking param: v to result ~r0 level=2$"
	return *v._upp
}

func (v *V) UPPib() *U { // ERROR "leaking param: v to result ~r0 level=2$"
	return *v.UPP()
}

func (v *V) USPa() *string { // ERROR "leaking param: v to result ~r0 level=1$"
	return v._u._sp
}

func (v *V) USPb() *string { // ERROR "leaking param: v to result ~r0 level=1$"
	return v.u()._sp
}

func (v *V) USPPia() *string { // ERROR "leaking param: v to result ~r0 level=2$"
	return *v._u._spp
}

func (v *V) USPPib() *string { // ERROR "leaking param: v to result ~r0 level=2$"
	return v._u.SPPi()
}

func (v *V) UPiSPa() *string { // ERROR "leaking param: v to result ~r0 level=2$"
	return v._up._sp
}

func (v *V) UPiSPb() *string { // ERROR "leaking param: v to result ~r0 level=2$"
	return v._up.SP()
}

func (v *V) UPiSPc() *string { // ERROR "leaking param: v to result ~r0 level=2$"
	return v.UP()._sp
}

func (v *V) UPiSPd() *string { // ERROR "leaking param: v to result ~r0 level=2$"
	return v.UP().SP()
}

// BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s3
func tUPiSPa() {
	s1 := "ant"
	s2 := "bat" // ERROR "moved to heap: s2$"
	s3 := "cat" // ERROR "moved to heap: s3$"
	s4 := "dog" // ERROR "moved to heap: s4$"
	s5 := "emu" // ERROR "moved to heap: s5$"
	s6 := "fox" // ERROR "moved to heap: s6$"
	ps2 := &s2
	ps4 := &s4 // ERROR "moved to heap: ps4$"
	ps6 := &s6 // ERROR "moved to heap: ps6$"
	u1 := U{&s1, &ps2}
	u2 := &U{&s3, &ps4}  // ERROR "&U{...} does not escape$"
	u3 := &U{&s5, &ps6}  // ERROR "&U{...} escapes to heap$"
	v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
	Ssink = v.UPiSPa()   // Ssink = &s3 (only &s3 really escapes)
}

// BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s3
func tUPiSPb() {
	s1 := "ant"
	s2 := "bat" // ERROR "moved to heap: s2$"
	s3 := "cat" // ERROR "moved to heap: s3$"
	s4 := "dog" // ERROR "moved to heap: s4$"
	s5 := "emu" // ERROR "moved to heap: s5$"
	s6 := "fox" // ERROR "moved to heap: s6$"
	ps2 := &s2
	ps4 := &s4 // ERROR "moved to heap: ps4$"
	ps6 := &s6 // ERROR "moved to heap: ps6$"
	u1 := U{&s1, &ps2}
	u2 := &U{&s3, &ps4}  // ERROR "&U{...} does not escape$"
	u3 := &U{&s5, &ps6}  // ERROR "&U{...} escapes to heap$"
	v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
	Ssink = v.UPiSPb()   // Ssink = &s3 (only &s3 really escapes)
}

// BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s3
func tUPiSPc() {
	s1 := "ant"
	s2 := "bat" // ERROR "moved to heap: s2$"
	s3 := "cat" // ERROR "moved to heap: s3$"
	s4 := "dog" // ERROR "moved to heap: s4$"
	s5 := "emu" // ERROR "moved to heap: s5$"
	s6 := "fox" // ERROR "moved to heap: s6$"
	ps2 := &s2
	ps4 := &s4 // ERROR "moved to heap: ps4$"
	ps6 := &s6 // ERROR "moved to heap: ps6$"
	u1 := U{&s1, &ps2}
	u2 := &U{&s3, &ps4}  // ERROR "&U{...} does not escape$"
	u3 := &U{&s5, &ps6}  // ERROR "&U{...} escapes to heap$"
	v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
	Ssink = v.UPiSPc()   // Ssink = &s3 (only &s3 really escapes)
}

// BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s3
func tUPiSPd() {
	s1 := "ant"
	s2 := "bat" // ERROR "moved to heap: s2$"
	s3 := "cat" // ERROR "moved to heap: s3$"
	s4 := "dog" // ERROR "moved to heap: s4$"
	s5 := "emu" // ERROR "moved to heap: s5$"
	s6 := "fox" // ERROR "moved to heap: s6$"
	ps2 := &s2
	ps4 := &s4 // ERROR "moved to heap: ps4$"
	ps6 := &s6 // ERROR "moved to heap: ps6$"
	u1 := U{&s1, &ps2}
	u2 := &U{&s3, &ps4}  // ERROR "&U{...} does not escape$"
	u3 := &U{&s5, &ps6}  // ERROR "&U{...} escapes to heap$"
	v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
	Ssink = v.UPiSPd()   // Ssink = &s3 (only &s3 really escapes)
}

func (v V) UPiSPPia() *string { // ERROR "leaking param: v to result ~r0 level=2$"
	return *v._up._spp
}

func (v V) UPiSPPib() *string { // ERROR "leaking param: v to result ~r0 level=2$"
	return v._up.SPPi()
}

func (v V) UPiSPPic() *string { // ERROR "leaking param: v to result ~r0 level=2$"
	return *v.UP()._spp
}

func (v V) UPiSPPid() *string { // ERROR "leaking param: v to result ~r0 level=2$"
	return v.UP().SPPi()
}

// BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s4
func tUPiSPPia() {
	s1 := "ant"
	s2 := "bat"
	s3 := "cat"
	s4 := "dog" // ERROR "moved to heap: s4$"
	s5 := "emu" // ERROR "moved to heap: s5$"
	s6 := "fox" // ERROR "moved to heap: s6$"
	ps2 := &s2
	ps4 := &s4
	ps6 := &s6 // ERROR "moved to heap: ps6$"
	u1 := U{&s1, &ps2}
	u2 := &U{&s3, &ps4}  // ERROR "&U{...} does not escape$"
	u3 := &U{&s5, &ps6}  // ERROR "&U{...} does not escape$"
	v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
	Ssink = v.UPiSPPia() // Ssink = *&ps4 = &s4 (only &s4 really escapes)
}

// BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s4
func tUPiSPPib() {
	s1 := "ant"
	s2 := "bat"
	s3 := "cat"
	s4 := "dog" // ERROR "moved to heap: s4$"
	s5 := "emu" // ERROR "moved to heap: s5$"
	s6 := "fox" // ERROR "moved to heap: s6$"
	ps2 := &s2
	ps4 := &s4
	ps6 := &s6 // ERROR "moved to heap: ps6$"
	u1 := U{&s1, &ps2}
	u2 := &U{&s3, &ps4}  // ERROR "&U{...} does not escape$"
	u3 := &U{&s5, &ps6}  // ERROR "&U{...} does not escape$"
	v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
	Ssink = v.UPiSPPib() // Ssink = *&ps4 = &s4 (only &s4 really escapes)
}

// BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s4
func tUPiSPPic() {
	s1 := "ant"
	s2 := "bat"
	s3 := "cat"
	s4 := "dog" // ERROR "moved to heap: s4$"
	s5 := "emu" // ERROR "moved to heap: s5$"
	s6 := "fox" // ERROR "moved to heap: s6$"
	ps2 := &s2
	ps4 := &s4
	ps6 := &s6 // ERROR "moved to heap: ps6$"
	u1 := U{&s1, &ps2}
	u2 := &U{&s3, &ps4}  // ERROR "&U{...} does not escape$"
	u3 := &U{&s5, &ps6}  // ERROR "&U{...} does not escape$"
	v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
	Ssink = v.UPiSPPic() // Ssink = *&ps4 = &s4 (only &s4 really escapes)
}

// BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s4
func tUPiSPPid() {
	s1 := "ant"
	s2 := "bat"
	s3 := "cat"
	s4 := "dog" // ERROR "moved to heap: s4$"
	s5 := "emu" // ERROR "moved to heap: s5$"
	s6 := "fox" // ERROR "moved to heap: s6$"
	ps2 := &s2
	ps4 := &s4
	ps6 := &s6 // ERROR "moved to heap: ps6$"
	u1 := U{&s1, &ps2}
	u2 := &U{&s3, &ps4}  // ERROR "&U{...} does not escape$"
	u3 := &U{&s5, &ps6}  // ERROR "&U{...} does not escape$"
	v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
	Ssink = v.UPiSPPid() // Ssink = *&ps4 = &s4 (only &s4 really escapes)
}

func (v *V) UPPiSPPia() *string { // ERROR "leaking param: v to result ~r0 level=4$"
	return *(*v._upp)._spp
}

// This test isolates the one value that needs to escape, not because
// it distinguishes fields but because it knows that &s6 is the only
// value reachable by two indirects from v.
// The test depends on the level cap in the escape analysis tags
// being able to encode that fact.
func tUPPiSPPia() {
	s1 := "ant"
	s2 := "bat"
	s3 := "cat"
	s4 := "dog"
	s5 := "emu"
	s6 := "fox" // ERROR "moved to heap: s6$"
	ps2 := &s2
	ps4 := &s4
	ps6 := &s6
	u1 := U{&s1, &ps2}
	u2 := &U{&s3, &ps4}   // ERROR "&U{...} does not escape$"
	u3 := &U{&s5, &ps6}   // ERROR "&U{...} does not escape$"
	v := &V{u1, u2, &u3}  // ERROR "&V{...} does not escape$"
	Ssink = v.UPPiSPPia() // Ssink = *&ps6 = &s6 (only &s6 really escapes)
}