3 // Copyright 2015 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Test escape analysis for *struct function parameters.
8 // Note companion strict_param2 checks struct function parameters with similar tests.
25 func (u *U) SP() *string { // ERROR "leaking param: u to result ~r0 level=1$"
29 func (u *U) SPP() **string { // ERROR "leaking param: u to result ~r0 level=1$"
33 func (u *U) SPPi() *string { // ERROR "leaking param: u to result ~r0 level=2$"
38 s := "cat" // ERROR "moved to heap: s$"
41 pu := &U{ps, pps} // ERROR "&U{...} does not escape$"
46 s := "cat" // ERROR "moved to heap: s$"
49 pu := &U{ps, pps} // ERROR "&U{...} does not escape$"
53 // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of ps
55 s := "cat" // ERROR "moved to heap: s$"
56 ps := &s // ERROR "moved to heap: ps$"
58 pu := &U{ps, pps} // ERROR "&U{...} does not escape$"
62 func (v *V) u() U { // ERROR "leaking param: v to result ~r0 level=1$"
66 func (v *V) UP() *U { // ERROR "leaking param: v to result ~r0 level=1$"
70 func (v *V) UPP() **U { // ERROR "leaking param: v to result ~r0 level=1$"
74 func (v *V) UPPia() *U { // ERROR "leaking param: v to result ~r0 level=2$"
78 func (v *V) UPPib() *U { // ERROR "leaking param: v to result ~r0 level=2$"
82 func (v *V) USPa() *string { // ERROR "leaking param: v to result ~r0 level=1$"
86 func (v *V) USPb() *string { // ERROR "leaking param: v to result ~r0 level=1$"
90 func (v *V) USPPia() *string { // ERROR "leaking param: v to result ~r0 level=2$"
94 func (v *V) USPPib() *string { // ERROR "leaking param: v to result ~r0 level=2$"
98 func (v *V) UPiSPa() *string { // ERROR "leaking param: v to result ~r0 level=2$"
102 func (v *V) UPiSPb() *string { // ERROR "leaking param: v to result ~r0 level=2$"
106 func (v *V) UPiSPc() *string { // ERROR "leaking param: v to result ~r0 level=2$"
110 func (v *V) UPiSPd() *string { // ERROR "leaking param: v to result ~r0 level=2$"
114 // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s3
117 s2 := "bat" // ERROR "moved to heap: s2$"
118 s3 := "cat" // ERROR "moved to heap: s3$"
119 s4 := "dog" // ERROR "moved to heap: s4$"
120 s5 := "emu" // ERROR "moved to heap: s5$"
121 s6 := "fox" // ERROR "moved to heap: s6$"
123 ps4 := &s4 // ERROR "moved to heap: ps4$"
124 ps6 := &s6 // ERROR "moved to heap: ps6$"
126 u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$"
127 u3 := &U{&s5, &ps6} // ERROR "&U{...} escapes to heap$"
128 v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
129 Ssink = v.UPiSPa() // Ssink = &s3 (only &s3 really escapes)
132 // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s3
135 s2 := "bat" // ERROR "moved to heap: s2$"
136 s3 := "cat" // ERROR "moved to heap: s3$"
137 s4 := "dog" // ERROR "moved to heap: s4$"
138 s5 := "emu" // ERROR "moved to heap: s5$"
139 s6 := "fox" // ERROR "moved to heap: s6$"
141 ps4 := &s4 // ERROR "moved to heap: ps4$"
142 ps6 := &s6 // ERROR "moved to heap: ps6$"
144 u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$"
145 u3 := &U{&s5, &ps6} // ERROR "&U{...} escapes to heap$"
146 v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
147 Ssink = v.UPiSPb() // Ssink = &s3 (only &s3 really escapes)
150 // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s3
153 s2 := "bat" // ERROR "moved to heap: s2$"
154 s3 := "cat" // ERROR "moved to heap: s3$"
155 s4 := "dog" // ERROR "moved to heap: s4$"
156 s5 := "emu" // ERROR "moved to heap: s5$"
157 s6 := "fox" // ERROR "moved to heap: s6$"
159 ps4 := &s4 // ERROR "moved to heap: ps4$"
160 ps6 := &s6 // ERROR "moved to heap: ps6$"
162 u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$"
163 u3 := &U{&s5, &ps6} // ERROR "&U{...} escapes to heap$"
164 v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
165 Ssink = v.UPiSPc() // Ssink = &s3 (only &s3 really escapes)
168 // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s3
171 s2 := "bat" // ERROR "moved to heap: s2$"
172 s3 := "cat" // ERROR "moved to heap: s3$"
173 s4 := "dog" // ERROR "moved to heap: s4$"
174 s5 := "emu" // ERROR "moved to heap: s5$"
175 s6 := "fox" // ERROR "moved to heap: s6$"
177 ps4 := &s4 // ERROR "moved to heap: ps4$"
178 ps6 := &s6 // ERROR "moved to heap: ps6$"
180 u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$"
181 u3 := &U{&s5, &ps6} // ERROR "&U{...} escapes to heap$"
182 v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
183 Ssink = v.UPiSPd() // Ssink = &s3 (only &s3 really escapes)
186 func (v V) UPiSPPia() *string { // ERROR "leaking param: v to result ~r0 level=2$"
190 func (v V) UPiSPPib() *string { // ERROR "leaking param: v to result ~r0 level=2$"
194 func (v V) UPiSPPic() *string { // ERROR "leaking param: v to result ~r0 level=2$"
198 func (v V) UPiSPPid() *string { // ERROR "leaking param: v to result ~r0 level=2$"
202 // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s4
207 s4 := "dog" // ERROR "moved to heap: s4$"
208 s5 := "emu" // ERROR "moved to heap: s5$"
209 s6 := "fox" // ERROR "moved to heap: s6$"
212 ps6 := &s6 // ERROR "moved to heap: ps6$"
214 u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$"
215 u3 := &U{&s5, &ps6} // ERROR "&U{...} does not escape$"
216 v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
217 Ssink = v.UPiSPPia() // Ssink = *&ps4 = &s4 (only &s4 really escapes)
220 // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s4
225 s4 := "dog" // ERROR "moved to heap: s4$"
226 s5 := "emu" // ERROR "moved to heap: s5$"
227 s6 := "fox" // ERROR "moved to heap: s6$"
230 ps6 := &s6 // ERROR "moved to heap: ps6$"
232 u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$"
233 u3 := &U{&s5, &ps6} // ERROR "&U{...} does not escape$"
234 v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
235 Ssink = v.UPiSPPib() // Ssink = *&ps4 = &s4 (only &s4 really escapes)
238 // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s4
243 s4 := "dog" // ERROR "moved to heap: s4$"
244 s5 := "emu" // ERROR "moved to heap: s5$"
245 s6 := "fox" // ERROR "moved to heap: s6$"
248 ps6 := &s6 // ERROR "moved to heap: ps6$"
250 u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$"
251 u3 := &U{&s5, &ps6} // ERROR "&U{...} does not escape$"
252 v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
253 Ssink = v.UPiSPPic() // Ssink = *&ps4 = &s4 (only &s4 really escapes)
256 // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s4
261 s4 := "dog" // ERROR "moved to heap: s4$"
262 s5 := "emu" // ERROR "moved to heap: s5$"
263 s6 := "fox" // ERROR "moved to heap: s6$"
266 ps6 := &s6 // ERROR "moved to heap: ps6$"
268 u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$"
269 u3 := &U{&s5, &ps6} // ERROR "&U{...} does not escape$"
270 v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
271 Ssink = v.UPiSPPid() // Ssink = *&ps4 = &s4 (only &s4 really escapes)
274 func (v *V) UPPiSPPia() *string { // ERROR "leaking param: v to result ~r0 level=4$"
275 return *(*v._upp)._spp
278 // This test isolates the one value that needs to escape, not because
279 // it distinguishes fields but because it knows that &s6 is the only
280 // value reachable by two indirects from v.
281 // The test depends on the level cap in the escape analysis tags
282 // being able to encode that fact.
289 s6 := "fox" // ERROR "moved to heap: s6$"
294 u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$"
295 u3 := &U{&s5, &ps6} // ERROR "&U{...} does not escape$"
296 v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$"
297 Ssink = v.UPPiSPPia() // Ssink = *&ps6 = &s6 (only &s6 really escapes)