github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/test/escape_param.go (about) 1 // errorcheck -0 -m -l 2 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. 6 7 // Test escape analysis for function parameters. 8 9 // In this test almost everything is BAD except the simplest cases 10 // where input directly flows to output. 11 12 package escape 13 14 var sink interface{} 15 16 // in -> out 17 func param0(p *int) *int { // ERROR "leaking param: p to result ~r1$" 18 return p 19 } 20 21 func caller0a() { 22 i := 0 23 _ = param0(&i) // ERROR "caller0a &i does not escape$" 24 } 25 26 func caller0b() { 27 i := 0 // ERROR "moved to heap: i$" 28 sink = param0(&i) // ERROR "&i escapes to heap$" "param0\(&i\) escapes to heap" 29 } 30 31 // in, in -> out, out 32 func param1(p1, p2 *int) (*int, *int) { // ERROR "leaking param: p1 to result ~r2$" "leaking param: p2 to result ~r3$" 33 return p1, p2 34 } 35 36 func caller1() { 37 i := 0 // ERROR "moved to heap: i$" 38 j := 0 39 sink, _ = param1(&i, &j) // ERROR "&i escapes to heap$" "caller1 &j does not escape$" 40 } 41 42 // in -> other in 43 func param2(p1 *int, p2 **int) { // ERROR "leaking param: p1$" "param2 p2 does not escape$" 44 *p2 = p1 45 } 46 47 func caller2a() { 48 i := 0 // ERROR "moved to heap: i$" 49 var p *int 50 param2(&i, &p) // ERROR "&i escapes to heap$" "caller2a &p does not escape$" 51 _ = p 52 } 53 54 func caller2b() { 55 i := 0 // ERROR "moved to heap: i$" 56 var p *int 57 param2(&i, &p) // ERROR "&i escapes to heap$" "caller2b &p does not escape$" 58 sink = p // ERROR "p escapes to heap$" 59 } 60 61 // in -> in 62 type Pair struct { 63 p1 *int 64 p2 *int 65 } 66 67 func param3(p *Pair) { // ERROR "leaking param: p$" 68 p.p1 = p.p2 69 } 70 71 func caller3a() { 72 i := 0 // ERROR "moved to heap: i$" 73 j := 0 // ERROR "moved to heap: j$" 74 p := Pair{&i, &j} // ERROR "&i escapes to heap$" "&j escapes to heap$" "moved to heap: p$" 75 param3(&p) // ERROR "&p escapes to heap$" 76 _ = p 77 } 78 79 func caller3b() { 80 i := 0 // ERROR "moved to heap: i$" 81 j := 0 // ERROR "moved to heap: j$" 82 p := Pair{&i, &j} // ERROR "&i escapes to heap$" "&j escapes to heap$" "moved to heap: p$" 83 param3(&p) // ERROR "&p escapes to heap$" 84 sink = p // ERROR "p escapes to heap$" 85 } 86 87 // in -> rcvr 88 func (p *Pair) param4(i *int) { // ERROR "\(\*Pair\).param4 p does not escape$" "leaking param: i$" 89 p.p1 = i 90 } 91 92 func caller4a() { 93 i := 0 // ERROR "moved to heap: i$" 94 p := Pair{} 95 p.param4(&i) // ERROR "&i escapes to heap$" "caller4a p does not escape$" 96 _ = p 97 } 98 99 func caller4b() { 100 i := 0 // ERROR "moved to heap: i$" 101 p := Pair{} 102 p.param4(&i) // ERROR "&i escapes to heap$" "caller4b p does not escape$" 103 sink = p // ERROR "p escapes to heap$" 104 } 105 106 // in -> heap 107 func param5(i *int) { // ERROR "leaking param: i$" 108 sink = i // ERROR "i escapes to heap$" 109 } 110 111 func caller5() { 112 i := 0 // ERROR "moved to heap: i$" 113 param5(&i) // ERROR "&i escapes to heap$" 114 } 115 116 // *in -> heap 117 func param6(i ***int) { // ERROR "leaking param: i$" 118 sink = *i // ERROR "\*i escapes to heap$" 119 } 120 121 func caller6a() { 122 i := 0 // ERROR "moved to heap: i$" 123 p := &i // ERROR "&i escapes to heap$" "moved to heap: p$" 124 p2 := &p // ERROR "&p escapes to heap$" "moved to heap: p2$" 125 param6(&p2) // ERROR "&p2 escapes to heap$" 126 } 127 128 // **in -> heap 129 func param7(i ***int) { // ERROR "leaking param: i$" 130 sink = **i // ERROR "\* \(\*i\) escapes to heap" 131 } 132 133 func caller7() { 134 i := 0 // ERROR "moved to heap: i$" 135 p := &i // ERROR "&i escapes to heap$" "moved to heap: p$" 136 p2 := &p // ERROR "&p escapes to heap$" "moved to heap: p2$" 137 param7(&p2) // ERROR "&p2 escapes to heap$" 138 } 139 140 // **in -> heap 141 func param8(i **int) { // ERROR "param8 i does not escape$" 142 sink = **i // ERROR "\* \(\*i\) escapes to heap" 143 } 144 145 func caller8() { 146 i := 0 147 p := &i // ERROR "caller8 &i does not escape$" 148 param8(&p) // ERROR "caller8 &p does not escape$" 149 } 150 151 // *in -> out 152 func param9(p ***int) **int { // ERROR "param9 leaking param p content to result ~r1$" 153 return *p 154 } 155 156 func caller9a() { 157 i := 0 // ERROR "moved to heap: i$" 158 p := &i // ERROR "&i escapes to heap$" "moved to heap: p$" 159 p2 := &p // ERROR "&p escapes to heap$" 160 _ = param9(&p2) // ERROR "caller9a &p2 does not escape$" 161 } 162 163 func caller9b() { 164 i := 0 // ERROR "moved to heap: i$" 165 p := &i // ERROR "&i escapes to heap$" "moved to heap: p$" 166 p2 := &p // ERROR "&p escapes to heap$" 167 sink = param9(&p2) // ERROR "caller9b &p2 does not escape$" "param9\(&p2\) escapes to heap" 168 } 169 170 // **in -> out 171 func param10(p ***int) *int { // ERROR "param10 leaking param p content to result ~r1$" 172 return **p 173 } 174 175 func caller10a() { 176 i := 0 // ERROR "moved to heap: i$" 177 p := &i // ERROR "&i escapes to heap$" "moved to heap: p$" 178 p2 := &p // ERROR "&p escapes to heap$" 179 _ = param10(&p2) // ERROR "caller10a &p2 does not escape$" 180 } 181 182 func caller10b() { 183 i := 0 // ERROR "moved to heap: i$" 184 p := &i // ERROR "&i escapes to heap$" "moved to heap: p$" 185 p2 := &p // ERROR "&p escapes to heap$" 186 sink = param10(&p2) // ERROR "caller10b &p2 does not escape$" "param10\(&p2\) escapes to heap" 187 } 188 189 // &in -> out 190 func param11(i **int) ***int { // ERROR "moved to heap: i$" 191 return &i // ERROR "&i escapes to heap$" 192 } 193 194 func caller11a() { 195 i := 0 // ERROR "moved to heap: i$" 196 p := &i // ERROR "&i escapes to heap$" "moved to heap: p$" 197 _ = param11(&p) // ERROR "&p escapes to heap$" 198 } 199 200 func caller11b() { 201 i := 0 // ERROR "moved to heap: i$" 202 p := &i // ERROR "&i escapes to heap$" "moved to heap: p$" 203 sink = param11(&p) // ERROR "&p escapes to heap$" "param11\(&p\) escapes to heap" 204 } 205 206 func caller11c() { 207 i := 0 // ERROR "moved to heap: i$" 208 p := &i // ERROR "&i escapes to heap$" "moved to heap: p$" 209 sink = *param11(&p) // ERROR "&p escapes to heap$" "\*param11\(&p\) escapes to heap" 210 } 211 212 // &in -> rcvr 213 type Indir struct { 214 p ***int 215 } 216 217 func (r *Indir) param12(i **int) { // ERROR "\(\*Indir\).param12 r does not escape$" "moved to heap: i$" 218 r.p = &i // ERROR "&i escapes to heap$" 219 } 220 221 func caller12a() { 222 i := 0 // ERROR "moved to heap: i$" 223 p := &i // ERROR "&i escapes to heap$" "moved to heap: p$" 224 var r Indir 225 r.param12(&p) // ERROR "&p escapes to heap$" "caller12a r does not escape$" 226 _ = r 227 } 228 229 func caller12b() { 230 i := 0 // ERROR "moved to heap: i$" 231 p := &i // ERROR "&i escapes to heap$" "moved to heap: p$" 232 r := &Indir{} // ERROR "caller12b &Indir literal does not escape$" 233 r.param12(&p) // ERROR "&p escapes to heap$" 234 _ = r 235 } 236 237 func caller12c() { 238 i := 0 // ERROR "moved to heap: i$" 239 p := &i // ERROR "&i escapes to heap$" "moved to heap: p$" 240 r := Indir{} 241 r.param12(&p) // ERROR "&p escapes to heap$" "caller12c r does not escape$" 242 sink = r // ERROR "r escapes to heap$" 243 } 244 245 func caller12d() { 246 i := 0 // ERROR "moved to heap: i$" 247 p := &i // ERROR "&i escapes to heap$" "moved to heap: p$" 248 r := Indir{} 249 r.param12(&p) // ERROR "&p escapes to heap$" "caller12d r does not escape$" 250 sink = **r.p // ERROR "\* \(\*r\.p\) escapes to heap" 251 } 252 253 // in -> value rcvr 254 type Val struct { 255 p **int 256 } 257 258 func (v Val) param13(i *int) { // ERROR "Val.param13 v does not escape$" "leaking param: i$" 259 *v.p = i 260 } 261 262 func caller13a() { 263 i := 0 // ERROR "moved to heap: i$" 264 var p *int 265 var v Val 266 v.p = &p // ERROR "caller13a &p does not escape$" 267 v.param13(&i) // ERROR "&i escapes to heap$" 268 _ = v 269 } 270 271 func caller13b() { 272 i := 0 // ERROR "moved to heap: i$" 273 var p *int 274 v := Val{&p} // ERROR "caller13b &p does not escape$" 275 v.param13(&i) // ERROR "&i escapes to heap$" 276 _ = v 277 } 278 279 func caller13c() { 280 i := 0 // ERROR "moved to heap: i$" 281 var p *int 282 v := &Val{&p} // ERROR "caller13c &Val literal does not escape$" "caller13c &p does not escape$" 283 v.param13(&i) // ERROR "&i escapes to heap$" 284 _ = v 285 } 286 287 func caller13d() { 288 i := 0 // ERROR "moved to heap: i$" 289 var p *int // ERROR "moved to heap: p$" 290 var v Val 291 v.p = &p // ERROR "&p escapes to heap$" 292 v.param13(&i) // ERROR "&i escapes to heap$" 293 sink = v // ERROR "v escapes to heap$" 294 } 295 296 func caller13e() { 297 i := 0 // ERROR "moved to heap: i$" 298 var p *int // ERROR "moved to heap: p$" 299 v := Val{&p} // ERROR "&p escapes to heap$" 300 v.param13(&i) // ERROR "&i escapes to heap$" 301 sink = v // ERROR "v escapes to heap$" 302 } 303 304 func caller13f() { 305 i := 0 // ERROR "moved to heap: i$" 306 var p *int // ERROR "moved to heap: p$" 307 v := &Val{&p} // ERROR "&Val literal escapes to heap$" "&p escapes to heap$" 308 v.param13(&i) // ERROR "&i escapes to heap$" 309 sink = v // ERROR "v escapes to heap$" 310 } 311 312 func caller13g() { 313 i := 0 // ERROR "moved to heap: i$" 314 var p *int 315 v := Val{&p} // ERROR "caller13g &p does not escape$" 316 v.param13(&i) // ERROR "&i escapes to heap$" 317 sink = *v.p // ERROR "\*v\.p escapes to heap" 318 } 319 320 func caller13h() { 321 i := 0 // ERROR "moved to heap: i$" 322 var p *int 323 v := &Val{&p} // ERROR "caller13h &Val literal does not escape$" "caller13h &p does not escape$" 324 v.param13(&i) // ERROR "&i escapes to heap$" 325 sink = **v.p // ERROR "\* \(\*v\.p\) escapes to heap" 326 }