]> Cypherpunks.ru repositories - gostls13.git/blob - test/escape2n.go
cmd/gc: allocate stack buffer for ORUNESTR
[gostls13.git] / test / escape2n.go
1 // errorcheck -0 -N -m -l
2
3 // Copyright 2010 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, using compiler diagnostic flags, that the escape analysis is working.
8 // Compiles but does not run.  Inlining is disabled.
9 // Registerization is disabled too (-N), which should
10 // have no effect on escape analysis.
11
12 package foo
13
14 import (
15         "fmt"
16         "unsafe"
17 )
18
19 var gxx *int
20
21 func foo1(x int) { // ERROR "moved to heap: x"
22         gxx = &x // ERROR "&x escapes to heap"
23 }
24
25 func foo2(yy *int) { // ERROR "leaking param: yy"
26         gxx = yy
27 }
28
29 func foo3(x int) *int { // ERROR "moved to heap: x"
30         return &x // ERROR "&x escapes to heap"
31 }
32
33 type T *T
34
35 func foo3b(t T) { // ERROR "leaking param: t"
36         *t = t
37 }
38
39 // xx isn't going anywhere, so use of yy is ok
40 func foo4(xx, yy *int) { // ERROR "xx does not escape" "yy does not escape"
41         xx = yy
42 }
43
44 // xx isn't going anywhere, so taking address of yy is ok
45 func foo5(xx **int, yy *int) { // ERROR "xx does not escape" "yy does not escape"
46         xx = &yy // ERROR "&yy does not escape"
47 }
48
49 func foo6(xx **int, yy *int) { // ERROR "xx does not escape" "leaking param: yy"
50         *xx = yy
51 }
52
53 func foo7(xx **int, yy *int) { // ERROR "xx does not escape" "yy does not escape"
54         **xx = *yy
55 }
56
57 func foo8(xx, yy *int) int { // ERROR "xx does not escape" "yy does not escape"
58         xx = yy
59         return *xx
60 }
61
62 func foo9(xx, yy *int) *int { // ERROR "leaking param: xx" "leaking param: yy"
63         xx = yy
64         return xx
65 }
66
67 func foo10(xx, yy *int) { // ERROR "xx does not escape" "yy does not escape"
68         *xx = *yy
69 }
70
71 func foo11() int {
72         x, y := 0, 42
73         xx := &x // ERROR "&x does not escape"
74         yy := &y // ERROR "&y does not escape"
75         *xx = *yy
76         return x
77 }
78
79 var xxx **int
80
81 func foo12(yyy **int) { // ERROR "leaking param: yyy"
82         xxx = yyy
83 }
84
85 // Must treat yyy as leaking because *yyy leaks, and the escape analysis
86 // summaries in exported metadata do not distinguish these two cases.
87 func foo13(yyy **int) { // ERROR "leaking param: yyy"
88         *xxx = *yyy
89 }
90
91 func foo14(yyy **int) { // ERROR "yyy does not escape"
92         **xxx = **yyy
93 }
94
95 func foo15(yy *int) { // ERROR "moved to heap: yy"
96         xxx = &yy // ERROR "&yy escapes to heap"
97 }
98
99 func foo16(yy *int) { // ERROR "leaking param: yy"
100         *xxx = yy
101 }
102
103 func foo17(yy *int) { // ERROR "yy does not escape"
104         **xxx = *yy
105 }
106
107 func foo18(y int) { // ERROR "moved to heap: "y"
108         *xxx = &y // ERROR "&y escapes to heap"
109 }
110
111 func foo19(y int) {
112         **xxx = y
113 }
114
115 type Bar struct {
116         i  int
117         ii *int
118 }
119
120 func NewBar() *Bar {
121         return &Bar{42, nil} // ERROR "&Bar literal escapes to heap"
122 }
123
124 func NewBarp(x *int) *Bar { // ERROR "leaking param: x"
125         return &Bar{42, x} // ERROR "&Bar literal escapes to heap"
126 }
127
128 func NewBarp2(x *int) *Bar { // ERROR "x does not escape"
129         return &Bar{*x, nil} // ERROR "&Bar literal escapes to heap"
130 }
131
132 func (b *Bar) NoLeak() int { // ERROR "b does not escape"
133         return *(b.ii)
134 }
135
136 func (b *Bar) Leak() *int { // ERROR "leaking param: b"
137         return &b.i // ERROR "&b.i escapes to heap"
138 }
139
140 func (b *Bar) AlsoNoLeak() *int { // ERROR "leaking param b content to result ~r0"
141         return b.ii
142 }
143
144 func (b Bar) AlsoLeak() *int { // ERROR "leaking param: b"
145         return b.ii
146 }
147
148 func (b Bar) LeaksToo() *int { // ERROR "leaking param: b"
149         v := 0    // ERROR "moved to heap: v"
150         b.ii = &v // ERROR "&v escapes"
151         return b.ii
152 }
153
154 func (b *Bar) LeaksABit() *int { // ERROR "leaking param b content to result ~r0"
155         v := 0    // ERROR "moved to heap: v"
156         b.ii = &v // ERROR "&v escapes"
157         return b.ii
158 }
159
160 func (b Bar) StillNoLeak() int { // ERROR "b does not escape"
161         v := 0
162         b.ii = &v // ERROR "&v does not escape"
163         return b.i
164 }
165
166 func goLeak(b *Bar) { // ERROR "leaking param: b"
167         go b.NoLeak()
168 }
169
170 type Bar2 struct {
171         i  [12]int
172         ii []int
173 }
174
175 func NewBar2() *Bar2 {
176         return &Bar2{[12]int{42}, nil} // ERROR "&Bar2 literal escapes to heap"
177 }
178
179 func (b *Bar2) NoLeak() int { // ERROR "b does not escape"
180         return b.i[0]
181 }
182
183 func (b *Bar2) Leak() []int { // ERROR "leaking param: b"
184         return b.i[:] // ERROR "b.i escapes to heap"
185 }
186
187 func (b *Bar2) AlsoNoLeak() []int { // ERROR "leaking param b content to result ~r0"
188         return b.ii[0:1]
189 }
190
191 func (b Bar2) AgainNoLeak() [12]int { // ERROR "b does not escape"
192         return b.i
193 }
194
195 func (b *Bar2) LeakSelf() { // ERROR "leaking param: b"
196         b.ii = b.i[0:4] // ERROR "b.i escapes to heap"
197 }
198
199 func (b *Bar2) LeakSelf2() { // ERROR "leaking param: b"
200         var buf []int
201         buf = b.i[0:] // ERROR "b.i escapes to heap"
202         b.ii = buf
203 }
204
205 func foo21() func() int {
206         x := 42             // ERROR "moved to heap: x"
207         return func() int { // ERROR "func literal escapes to heap"
208                 return x // ERROR "&x escapes to heap"
209         }
210 }
211
212 func foo22() int {
213         x := 42
214         return func() int { // ERROR "func literal does not escape"
215                 return x
216         }()
217 }
218
219 func foo23(x int) func() int { // ERROR "moved to heap: x"
220         return func() int { // ERROR "func literal escapes to heap"
221                 return x // ERROR "&x escapes to heap"
222         }
223 }
224
225 func foo23a(x int) func() int { // ERROR "moved to heap: x"
226         f := func() int { // ERROR "func literal escapes to heap"
227                 return x // ERROR "&x escapes to heap"
228         }
229         return f
230 }
231
232 func foo23b(x int) *(func() int) { // ERROR "moved to heap: x"
233         f := func() int { return x } // ERROR "moved to heap: f" "func literal escapes to heap" "&x escapes to heap"
234         return &f                    // ERROR "&f escapes to heap"
235 }
236
237 func foo24(x int) int {
238         return func() int { // ERROR "func literal does not escape"
239                 return x
240         }()
241 }
242
243 var x *int
244
245 func fooleak(xx *int) int { // ERROR "leaking param: xx"
246         x = xx
247         return *x
248 }
249
250 func foonoleak(xx *int) int { // ERROR "xx does not escape"
251         return *x + *xx
252 }
253
254 func foo31(x int) int { // ERROR "moved to heap: x"
255         return fooleak(&x) // ERROR "&x escapes to heap"
256 }
257
258 func foo32(x int) int {
259         return foonoleak(&x) // ERROR "&x does not escape"
260 }
261
262 type Foo struct {
263         xx *int
264         x  int
265 }
266
267 var F Foo
268 var pf *Foo
269
270 func (f *Foo) fooleak() { // ERROR "leaking param: f"
271         pf = f
272 }
273
274 func (f *Foo) foonoleak() { // ERROR "f does not escape"
275         F.x = f.x
276 }
277
278 func (f *Foo) Leak() { // ERROR "leaking param: f"
279         f.fooleak()
280 }
281
282 func (f *Foo) NoLeak() { // ERROR "f does not escape"
283         f.foonoleak()
284 }
285
286 func foo41(x int) { // ERROR "moved to heap: x"
287         F.xx = &x // ERROR "&x escapes to heap"
288 }
289
290 func (f *Foo) foo42(x int) { // ERROR "f does not escape" "moved to heap: x"
291         f.xx = &x // ERROR "&x escapes to heap"
292 }
293
294 func foo43(f *Foo, x int) { // ERROR "f does not escape" "moved to heap: x"
295         f.xx = &x // ERROR "&x escapes to heap"
296 }
297
298 func foo44(yy *int) { // ERROR "leaking param: yy"
299         F.xx = yy
300 }
301
302 func (f *Foo) foo45() { // ERROR "f does not escape"
303         F.x = f.x
304 }
305
306 // See foo13 above for explanation of why f leaks.
307 func (f *Foo) foo46() { // ERROR "leaking param: f"
308         F.xx = f.xx
309 }
310
311 func (f *Foo) foo47() { // ERROR "leaking param: f"
312         f.xx = &f.x // ERROR "&f.x escapes to heap"
313 }
314
315 var ptrSlice []*int
316
317 func foo50(i *int) { // ERROR "leaking param: i"
318         ptrSlice[0] = i
319 }
320
321 var ptrMap map[*int]*int
322
323 func foo51(i *int) { // ERROR "leaking param: i"
324         ptrMap[i] = i
325 }
326
327 func indaddr1(x int) *int { // ERROR "moved to heap: x"
328         return &x // ERROR "&x escapes to heap"
329 }
330
331 func indaddr2(x *int) *int { // ERROR "leaking param: x"
332         return *&x // ERROR "&x does not escape"
333 }
334
335 func indaddr3(x *int32) *int { // ERROR "leaking param: x"
336         return *(**int)(unsafe.Pointer(&x)) // ERROR "&x does not escape"
337 }
338
339 // From package math:
340
341 func Float32bits(f float32) uint32 {
342         return *(*uint32)(unsafe.Pointer(&f)) // ERROR "&f does not escape"
343 }
344
345 func Float32frombits(b uint32) float32 {
346         return *(*float32)(unsafe.Pointer(&b)) // ERROR "&b does not escape"
347 }
348
349 func Float64bits(f float64) uint64 {
350         return *(*uint64)(unsafe.Pointer(&f)) // ERROR "&f does not escape"
351 }
352
353 func Float64frombits(b uint64) float64 {
354         return *(*float64)(unsafe.Pointer(&b)) // ERROR "&b does not escape"
355 }
356
357 // contrast with
358 func float64bitsptr(f float64) *uint64 { // ERROR "moved to heap: f"
359         return (*uint64)(unsafe.Pointer(&f)) // ERROR "&f escapes to heap"
360 }
361
362 func float64ptrbitsptr(f *float64) *uint64 { // ERROR "leaking param: f"
363         return (*uint64)(unsafe.Pointer(f))
364 }
365
366 func typesw(i interface{}) *int { // ERROR "leaking param: i"
367         switch val := i.(type) {
368         case *int:
369                 return val
370         case *int8:
371                 v := int(*val) // ERROR "moved to heap: v"
372                 return &v      // ERROR "&v escapes to heap"
373         }
374         return nil
375 }
376
377 func exprsw(i *int) *int { // ERROR "leaking param: i"
378         switch j := i; *j + 110 {
379         case 12:
380                 return j
381         case 42:
382                 return nil
383         }
384         return nil
385
386 }
387
388 // assigning to an array element is like assigning to the array
389 func foo60(i *int) *int { // ERROR "leaking param: i"
390         var a [12]*int
391         a[0] = i
392         return a[1]
393 }
394
395 func foo60a(i *int) *int { // ERROR "i does not escape"
396         var a [12]*int
397         a[0] = i
398         return nil
399 }
400
401 // assigning to a struct field  is like assigning to the struct
402 func foo61(i *int) *int { // ERROR "leaking param: i"
403         type S struct {
404                 a, b *int
405         }
406         var s S
407         s.a = i
408         return s.b
409 }
410
411 func foo61a(i *int) *int { // ERROR "i does not escape"
412         type S struct {
413                 a, b *int
414         }
415         var s S
416         s.a = i
417         return nil
418 }
419
420 // assigning to a struct field is like assigning to the struct but
421 // here this subtlety is lost, since s.a counts as an assignment to a
422 // track-losing dereference.
423 func foo62(i *int) *int { // ERROR "leaking param: i"
424         type S struct {
425                 a, b *int
426         }
427         s := new(S) // ERROR "new[(]S[)] does not escape"
428         s.a = i
429         return nil // s.b
430 }
431
432 type M interface {
433         M()
434 }
435
436 func foo63(m M) { // ERROR "m does not escape"
437 }
438
439 func foo64(m M) { // ERROR "leaking param: m"
440         m.M()
441 }
442
443 func foo64b(m M) { // ERROR "leaking param: m"
444         defer m.M()
445 }
446
447 type MV int
448
449 func (MV) M() {}
450
451 func foo65() {
452         var mv MV
453         foo63(&mv) // ERROR "&mv does not escape"
454 }
455
456 func foo66() {
457         var mv MV  // ERROR "moved to heap: mv"
458         foo64(&mv) // ERROR "&mv escapes to heap"
459 }
460
461 func foo67() {
462         var mv MV
463         foo63(mv)
464 }
465
466 func foo68() {
467         var mv MV
468         foo64(mv) // escapes but it's an int so irrelevant
469 }
470
471 func foo69(m M) { // ERROR "leaking param: m"
472         foo64(m)
473 }
474
475 func foo70(mv1 *MV, m M) { // ERROR "leaking param: mv1" "leaking param: m"
476         m = mv1
477         foo64(m)
478 }
479
480 func foo71(x *int) []*int { // ERROR "leaking param: x"
481         var y []*int
482         y = append(y, x)
483         return y
484 }
485
486 func foo71a(x int) []*int { // ERROR "moved to heap: x"
487         var y []*int
488         y = append(y, &x) // ERROR "&x escapes to heap"
489         return y
490 }
491
492 func foo72() {
493         var x int
494         var y [1]*int
495         y[0] = &x // ERROR "&x does not escape"
496 }
497
498 func foo72aa() [10]*int {
499         var x int // ERROR "moved to heap: x"
500         var y [10]*int
501         y[0] = &x // ERROR "&x escapes to heap"
502         return y
503 }
504
505 func foo72a() {
506         var y [10]*int
507         for i := 0; i < 10; i++ {
508                 // escapes its scope
509                 x := i    // ERROR "moved to heap: x"
510                 y[i] = &x // ERROR "&x escapes to heap"
511         }
512         return
513 }
514
515 func foo72b() [10]*int {
516         var y [10]*int
517         for i := 0; i < 10; i++ {
518                 x := i    // ERROR "moved to heap: x"
519                 y[i] = &x // ERROR "&x escapes to heap"
520         }
521         return y
522 }
523
524 // issue 2145
525 func foo73() {
526         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
527         for _, v := range s {
528                 vv := v // ERROR "moved to heap: vv"
529                 // actually just escapes its scope
530                 defer func() { // ERROR "func literal escapes to heap"
531                         println(vv) // ERROR "&vv escapes to heap"
532                 }()
533         }
534 }
535
536 func foo74() {
537         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
538         for _, v := range s {
539                 vv := v // ERROR "moved to heap: vv"
540                 // actually just escapes its scope
541                 fn := func() { // ERROR "func literal escapes to heap"
542                         println(vv) // ERROR "&vv escapes to heap"
543                 }
544                 defer fn()
545         }
546 }
547
548 // issue 3975
549 func foo74b() {
550         var array [3]func()
551         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
552         for i, v := range s {
553                 vv := v // ERROR "moved to heap: vv"
554                 // actually just escapes its scope
555                 array[i] = func() { // ERROR "func literal escapes to heap"
556                         println(vv) // ERROR "&vv escapes to heap"
557                 }
558         }
559 }
560
561 func myprint(y *int, x ...interface{}) *int { // ERROR "x does not escape" "leaking param: y"
562         return y
563 }
564
565 func myprint1(y *int, x ...interface{}) *interface{} { // ERROR "y does not escape" "leaking param: x"
566         return &x[0] // ERROR "&x.0. escapes to heap"
567 }
568
569 func foo75(z *int) { // ERROR "z does not escape"
570         myprint(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
571 }
572
573 func foo75a(z *int) { // ERROR "z does not escape"
574         myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
575 }
576
577 func foo75esc(z *int) { // ERROR "leaking param: z"
578         gxx = myprint(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
579 }
580
581 func foo75aesc(z *int) { // ERROR "z does not escape"
582         var ppi **interface{}       // assignments to pointer dereferences lose track
583         *ppi = myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
584 }
585
586 func foo76(z *int) { // ERROR "leaking param: z"
587         myprint(nil, z) // ERROR "[.][.][.] argument does not escape"
588 }
589
590 func foo76a(z *int) { // ERROR "leaking param: z"
591         myprint1(nil, z) // ERROR "[.][.][.] argument does not escape"
592 }
593
594 func foo76b() {
595         myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
596 }
597
598 func foo76c() {
599         myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
600 }
601
602 func foo76d() {
603         defer myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
604 }
605
606 func foo76e() {
607         defer myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
608 }
609
610 func foo76f() {
611         for {
612                 // TODO: This one really only escapes its scope, but we don't distinguish yet.
613                 defer myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
614         }
615 }
616
617 func foo76g() {
618         for {
619                 defer myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
620         }
621 }
622
623 func foo77(z []interface{}) { // ERROR "z does not escape"
624         myprint(nil, z...) // z does not escape
625 }
626
627 func foo77a(z []interface{}) { // ERROR "z does not escape"
628         myprint1(nil, z...)
629 }
630
631 func foo77b(z []interface{}) { // ERROR "leaking param: z"
632         var ppi **interface{}
633         *ppi = myprint1(nil, z...)
634 }
635
636 func foo78(z int) *int { // ERROR "moved to heap: z"
637         return &z // ERROR "&z escapes to heap"
638 }
639
640 func foo78a(z int) *int { // ERROR "moved to heap: z"
641         y := &z   // ERROR "&z escapes to heap"
642         x := &y   // ERROR "&y does not escape"
643         return *x // really return y
644 }
645
646 func foo79() *int {
647         return new(int) // ERROR "new[(]int[)] escapes to heap"
648 }
649
650 func foo80() *int {
651         var z *int
652         for {
653                 // Really just escapes its scope but we don't distinguish
654                 z = new(int) // ERROR "new[(]int[)] escapes to heap"
655         }
656         _ = z
657         return nil
658 }
659
660 func foo81() *int {
661         for {
662                 z := new(int) // ERROR "new[(]int[)] does not escape"
663                 _ = z
664         }
665         return nil
666 }
667
668 func tee(p *int) (x, y *int) { return p, p } // ERROR "leaking param"
669
670 func noop(x, y *int) {} // ERROR "does not escape"
671
672 func foo82() {
673         var x, y, z int  // ERROR "moved to heap"
674         go noop(tee(&z)) // ERROR "&z escapes to heap"
675         go noop(&x, &y)  // ERROR "escapes to heap"
676         for {
677                 var u, v, w int     // ERROR "moved to heap"
678                 defer noop(tee(&u)) // ERROR "&u escapes to heap"
679                 defer noop(&v, &w)  // ERROR "escapes to heap"
680         }
681 }
682
683 type Fooer interface {
684         Foo()
685 }
686
687 type LimitedFooer struct {
688         Fooer
689         N int64
690 }
691
692 func LimitFooer(r Fooer, n int64) Fooer { // ERROR "leaking param: r"
693         return &LimitedFooer{r, n} // ERROR "&LimitedFooer literal escapes to heap"
694 }
695
696 func foo90(x *int) map[*int]*int { // ERROR "leaking param: x"
697         return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int literal escapes to heap"
698 }
699
700 func foo91(x *int) map[*int]*int { // ERROR "leaking param: x"
701         return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int literal escapes to heap"
702 }
703
704 func foo92(x *int) [2]*int { // ERROR "leaking param: x"
705         return [2]*int{x, nil}
706 }
707
708 // does not leak c
709 func foo93(c chan *int) *int { // ERROR "c does not escape"
710         for v := range c {
711                 return v
712         }
713         return nil
714 }
715
716 // does not leak m
717 func foo94(m map[*int]*int, b bool) *int { // ERROR "m does not escape"
718         for k, v := range m {
719                 if b {
720                         return k
721                 }
722                 return v
723         }
724         return nil
725 }
726
727 // does leak x
728 func foo95(m map[*int]*int, x *int) { // ERROR "m does not escape" "leaking param: x"
729         m[x] = x
730 }
731
732 // does not leak m
733 func foo96(m []*int) *int { // ERROR "m does not escape"
734         return m[0]
735 }
736
737 // does leak m
738 func foo97(m [1]*int) *int { // ERROR "leaking param: m"
739         return m[0]
740 }
741
742 // does not leak m
743 func foo98(m map[int]*int) *int { // ERROR "m does not escape"
744         return m[0]
745 }
746
747 // does leak m
748 func foo99(m *[1]*int) []*int { // ERROR "leaking param: m"
749         return m[:]
750 }
751
752 // does not leak m
753 func foo100(m []*int) *int { // ERROR "m does not escape"
754         for _, v := range m {
755                 return v
756         }
757         return nil
758 }
759
760 // does leak m
761 func foo101(m [1]*int) *int { // ERROR "leaking param: m"
762         for _, v := range m {
763                 return v
764         }
765         return nil
766 }
767
768 // does not leak m
769 func foo101a(m [1]*int) *int { // ERROR "m does not escape"
770         for i := range m { // ERROR "moved to heap: i"
771                 return &i // ERROR "&i escapes to heap"
772         }
773         return nil
774 }
775
776 // does leak x
777 func foo102(m []*int, x *int) { // ERROR "m does not escape" "leaking param: x"
778         m[0] = x
779 }
780
781 // does not leak x
782 func foo103(m [1]*int, x *int) { // ERROR "m does not escape" "x does not escape"
783         m[0] = x
784 }
785
786 var y []*int
787
788 // does not leak x
789 func foo104(x []*int) { // ERROR "x does not escape"
790         copy(y, x)
791 }
792
793 // does not leak x
794 func foo105(x []*int) { // ERROR "x does not escape"
795         _ = append(y, x...)
796 }
797
798 // does leak x
799 func foo106(x *int) { // ERROR "leaking param: x"
800         _ = append(y, x)
801 }
802
803 func foo107(x *int) map[*int]*int { // ERROR "leaking param: x"
804         return map[*int]*int{x: nil} // ERROR "map.* literal escapes to heap"
805 }
806
807 func foo108(x *int) map[*int]*int { // ERROR "leaking param: x"
808         return map[*int]*int{nil: x} // ERROR "map.* literal escapes to heap"
809 }
810
811 func foo109(x *int) *int { // ERROR "leaking param: x"
812         m := map[*int]*int{x: nil} // ERROR "map.* literal does not escape"
813         for k, _ := range m {
814                 return k
815         }
816         return nil
817 }
818
819 func foo110(x *int) *int { // ERROR "leaking param: x"
820         m := map[*int]*int{nil: x} // ERROR "map.* literal does not escape"
821         return m[nil]
822 }
823
824 func foo111(x *int) *int { // ERROR "leaking param: x"
825         m := []*int{x} // ERROR "\[\]\*int literal does not escape"
826         return m[0]
827 }
828
829 func foo112(x *int) *int { // ERROR "leaking param: x"
830         m := [1]*int{x}
831         return m[0]
832 }
833
834 func foo113(x *int) *int { // ERROR "leaking param: x"
835         m := Bar{ii: x}
836         return m.ii
837 }
838
839 func foo114(x *int) *int { // ERROR "leaking param: x"
840         m := &Bar{ii: x} // ERROR "&Bar literal does not escape"
841         return m.ii
842 }
843
844 func foo115(x *int) *int { // ERROR "leaking param: x"
845         return (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + 1))
846 }
847
848 func foo116(b bool) *int {
849         if b {
850                 x := 1    // ERROR "moved to heap: x"
851                 return &x // ERROR "&x escapes to heap"
852         } else {
853                 y := 1    // ERROR "moved to heap: y"
854                 return &y // ERROR "&y escapes to heap"
855         }
856         return nil
857 }
858
859 func foo117(unknown func(interface{})) { // ERROR "unknown does not escape"
860         x := 1      // ERROR "moved to heap: x"
861         unknown(&x) // ERROR "&x escapes to heap"
862 }
863
864 func foo118(unknown func(*int)) { // ERROR "unknown does not escape"
865         x := 1      // ERROR "moved to heap: x"
866         unknown(&x) // ERROR "&x escapes to heap"
867 }
868
869 func external(*int)
870
871 func foo119(x *int) { // ERROR "leaking param: x"
872         external(x)
873 }
874
875 func foo120() {
876         // formerly exponential time analysis
877 L1:
878 L2:
879 L3:
880 L4:
881 L5:
882 L6:
883 L7:
884 L8:
885 L9:
886 L10:
887 L11:
888 L12:
889 L13:
890 L14:
891 L15:
892 L16:
893 L17:
894 L18:
895 L19:
896 L20:
897 L21:
898 L22:
899 L23:
900 L24:
901 L25:
902 L26:
903 L27:
904 L28:
905 L29:
906 L30:
907 L31:
908 L32:
909 L33:
910 L34:
911 L35:
912 L36:
913 L37:
914 L38:
915 L39:
916 L40:
917 L41:
918 L42:
919 L43:
920 L44:
921 L45:
922 L46:
923 L47:
924 L48:
925 L49:
926 L50:
927 L51:
928 L52:
929 L53:
930 L54:
931 L55:
932 L56:
933 L57:
934 L58:
935 L59:
936 L60:
937 L61:
938 L62:
939 L63:
940 L64:
941 L65:
942 L66:
943 L67:
944 L68:
945 L69:
946 L70:
947 L71:
948 L72:
949 L73:
950 L74:
951 L75:
952 L76:
953 L77:
954 L78:
955 L79:
956 L80:
957 L81:
958 L82:
959 L83:
960 L84:
961 L85:
962 L86:
963 L87:
964 L88:
965 L89:
966 L90:
967 L91:
968 L92:
969 L93:
970 L94:
971 L95:
972 L96:
973 L97:
974 L98:
975 L99:
976 L100:
977         // use the labels to silence compiler errors
978         goto L1
979         goto L2
980         goto L3
981         goto L4
982         goto L5
983         goto L6
984         goto L7
985         goto L8
986         goto L9
987         goto L10
988         goto L11
989         goto L12
990         goto L13
991         goto L14
992         goto L15
993         goto L16
994         goto L17
995         goto L18
996         goto L19
997         goto L20
998         goto L21
999         goto L22
1000         goto L23
1001         goto L24
1002         goto L25
1003         goto L26
1004         goto L27
1005         goto L28
1006         goto L29
1007         goto L30
1008         goto L31
1009         goto L32
1010         goto L33
1011         goto L34
1012         goto L35
1013         goto L36
1014         goto L37
1015         goto L38
1016         goto L39
1017         goto L40
1018         goto L41
1019         goto L42
1020         goto L43
1021         goto L44
1022         goto L45
1023         goto L46
1024         goto L47
1025         goto L48
1026         goto L49
1027         goto L50
1028         goto L51
1029         goto L52
1030         goto L53
1031         goto L54
1032         goto L55
1033         goto L56
1034         goto L57
1035         goto L58
1036         goto L59
1037         goto L60
1038         goto L61
1039         goto L62
1040         goto L63
1041         goto L64
1042         goto L65
1043         goto L66
1044         goto L67
1045         goto L68
1046         goto L69
1047         goto L70
1048         goto L71
1049         goto L72
1050         goto L73
1051         goto L74
1052         goto L75
1053         goto L76
1054         goto L77
1055         goto L78
1056         goto L79
1057         goto L80
1058         goto L81
1059         goto L82
1060         goto L83
1061         goto L84
1062         goto L85
1063         goto L86
1064         goto L87
1065         goto L88
1066         goto L89
1067         goto L90
1068         goto L91
1069         goto L92
1070         goto L93
1071         goto L94
1072         goto L95
1073         goto L96
1074         goto L97
1075         goto L98
1076         goto L99
1077         goto L100
1078 }
1079
1080 func foo121() {
1081         for i := 0; i < 10; i++ {
1082                 defer myprint(nil, i) // ERROR "[.][.][.] argument escapes to heap"
1083                 go myprint(nil, i)    // ERROR "[.][.][.] argument escapes to heap"
1084         }
1085 }
1086
1087 // same as foo121 but check across import
1088 func foo121b() {
1089         for i := 0; i < 10; i++ {
1090                 defer fmt.Printf("%d", i) // ERROR "[.][.][.] argument escapes to heap"
1091                 go fmt.Printf("%d", i)    // ERROR "[.][.][.] argument escapes to heap"
1092         }
1093 }
1094
1095 // a harmless forward jump
1096 func foo122() {
1097         var i *int
1098
1099         goto L1
1100 L1:
1101         i = new(int) // ERROR "new.int. does not escape"
1102         _ = i
1103 }
1104
1105 // a backward jump, increases loopdepth
1106 func foo123() {
1107         var i *int
1108
1109 L1:
1110         i = new(int) // ERROR "new.int. escapes to heap"
1111
1112         goto L1
1113         _ = i
1114 }
1115
1116 func foo124(x **int) { // ERROR "x does not escape"
1117         var i int // ERROR "moved to heap: i"
1118         p := &i   // ERROR "&i escapes"
1119         func() {  // ERROR "func literal does not escape"
1120                 *x = p // ERROR "leaking closure reference p"
1121         }()
1122 }
1123
1124 func foo125(ch chan *int) { // ERROR "does not escape"
1125         var i int // ERROR "moved to heap"
1126         p := &i   // ERROR "&i escapes to heap"
1127         func() {  // ERROR "func literal does not escape"
1128                 ch <- p // ERROR "leaking closure reference p"
1129         }()
1130 }
1131
1132 func foo126() {
1133         var px *int // loopdepth 0
1134         for {
1135                 // loopdepth 1
1136                 var i int // ERROR "moved to heap"
1137                 func() {  // ERROR "func literal does not escape"
1138                         px = &i // ERROR "&i escapes"
1139                 }()
1140         }
1141         _ = px
1142 }
1143
1144 var px *int
1145
1146 func foo127() {
1147         var i int // ERROR "moved to heap: i"
1148         p := &i   // ERROR "&i escapes to heap"
1149         q := p
1150         px = q
1151 }
1152
1153 func foo128() {
1154         var i int
1155         p := &i // ERROR "&i does not escape"
1156         q := p
1157         _ = q
1158 }
1159
1160 func foo129() {
1161         var i int // ERROR "moved to heap: i"
1162         p := &i   // ERROR "&i escapes to heap"
1163         func() {  // ERROR "func literal does not escape"
1164                 q := p   // ERROR "leaking closure reference p"
1165                 func() { // ERROR "func literal does not escape"
1166                         r := q // ERROR "leaking closure reference q"
1167                         px = r
1168                 }()
1169         }()
1170 }
1171
1172 func foo130() {
1173         for {
1174                 var i int // ERROR "moved to heap"
1175                 func() {  // ERROR "func literal does not escape"
1176                         px = &i // ERROR "&i escapes" "leaking closure reference i"
1177                 }()
1178         }
1179 }
1180
1181 func foo131() {
1182         var i int // ERROR "moved to heap"
1183         func() {  // ERROR "func literal does not escape"
1184                 px = &i // ERROR "&i escapes" "leaking closure reference i"
1185         }()
1186 }
1187
1188 func foo132() {
1189         var i int   // ERROR "moved to heap"
1190         go func() { // ERROR "func literal escapes to heap"
1191                 px = &i // ERROR "&i escapes" "leaking closure reference i"
1192         }()
1193 }
1194
1195 func foo133() {
1196         var i int      // ERROR "moved to heap"
1197         defer func() { // ERROR "func literal does not escape"
1198                 px = &i // ERROR "&i escapes" "leaking closure reference i"
1199         }()
1200 }
1201
1202 func foo134() {
1203         var i int
1204         p := &i  // ERROR "&i does not escape"
1205         func() { // ERROR "func literal does not escape"
1206                 q := p
1207                 func() { // ERROR "func literal does not escape"
1208                         r := q
1209                         _ = r
1210                 }()
1211         }()
1212 }
1213
1214 func foo135() {
1215         var i int   // ERROR "moved to heap: i"
1216         p := &i     // ERROR "&i escapes to heap" "moved to heap: p"
1217         go func() { // ERROR "func literal escapes to heap"
1218                 q := p   // ERROR "&p escapes to heap"
1219                 func() { // ERROR "func literal does not escape"
1220                         r := q
1221                         _ = r
1222                 }()
1223         }()
1224 }
1225
1226 func foo136() {
1227         var i int   // ERROR "moved to heap: i"
1228         p := &i     // ERROR "&i escapes to heap" "moved to heap: p"
1229         go func() { // ERROR "func literal escapes to heap"
1230                 q := p   // ERROR "&p escapes to heap" "leaking closure reference p"
1231                 func() { // ERROR "func literal does not escape"
1232                         r := q // ERROR "leaking closure reference q"
1233                         px = r
1234                 }()
1235         }()
1236 }
1237
1238 func foo137() {
1239         var i int // ERROR "moved to heap: i"
1240         p := &i   // ERROR "&i escapes to heap"
1241         func() {  // ERROR "func literal does not escape"
1242                 q := p      // ERROR "leaking closure reference p" "moved to heap: q"
1243                 go func() { // ERROR "func literal escapes to heap"
1244                         r := q // ERROR "&q escapes to heap"
1245                         _ = r
1246                 }()
1247         }()
1248 }
1249
1250 func foo138() *byte {
1251         type T struct {
1252                 x [1]byte
1253         }
1254         t := new(T)    // ERROR "new.T. escapes to heap"
1255         return &t.x[0] // ERROR "&t.x.0. escapes to heap"
1256 }
1257
1258 func foo139() *byte {
1259         type T struct {
1260                 x struct {
1261                         y byte
1262                 }
1263         }
1264         t := new(T)   // ERROR "new.T. escapes to heap"
1265         return &t.x.y // ERROR "&t.x.y escapes to heap"
1266 }
1267
1268 // issue 4751
1269 func foo140() interface{} {
1270         type T struct {
1271                 X string
1272         }
1273         type U struct {
1274                 X string
1275                 T *T
1276         }
1277         t := &T{} // ERROR "&T literal escapes to heap"
1278         return U{
1279                 X: t.X,
1280                 T: t,
1281         }
1282 }
1283
1284 //go:noescape
1285
1286 func F1([]byte)
1287
1288 func F2([]byte)
1289
1290 //go:noescape
1291
1292 func F3(x []byte) // ERROR "F3 x does not escape"
1293
1294 func F4(x []byte)
1295
1296 func G() {
1297         var buf1 [10]byte
1298         F1(buf1[:]) // ERROR "buf1 does not escape"
1299
1300         var buf2 [10]byte // ERROR "moved to heap: buf2"
1301         F2(buf2[:])       // ERROR "buf2 escapes to heap"
1302
1303         var buf3 [10]byte
1304         F3(buf3[:]) // ERROR "buf3 does not escape"
1305
1306         var buf4 [10]byte // ERROR "moved to heap: buf4"
1307         F4(buf4[:])       // ERROR "buf4 escapes to heap"
1308 }
1309
1310 type Tm struct {
1311         x int
1312 }
1313
1314 func (t *Tm) M() { // ERROR "t does not escape"
1315 }
1316
1317 func foo141() {
1318         var f func()
1319
1320         t := new(Tm) // ERROR "escapes to heap"
1321         f = t.M      // ERROR "t.M does not escape"
1322         _ = f
1323 }
1324
1325 var gf func()
1326
1327 func foo142() {
1328         t := new(Tm) // ERROR "escapes to heap"
1329         gf = t.M     // ERROR "t.M escapes to heap"
1330 }
1331
1332 // issue 3888.
1333 func foo143() {
1334         for i := 0; i < 1000; i++ {
1335                 func() { // ERROR "func literal does not escape"
1336                         for i := 0; i < 1; i++ {
1337                                 var t Tm
1338                                 t.M() // ERROR "t does not escape"
1339                         }
1340                 }()
1341         }
1342 }
1343
1344 // issue 5773
1345 // Check that annotations take effect regardless of whether they
1346 // are before or after the use in the source code.
1347
1348 //go:noescape
1349
1350 func foo144a(*int)
1351
1352 func foo144() {
1353         var x int
1354         foo144a(&x) // ERROR "&x does not escape"
1355         var y int
1356         foo144b(&y) // ERROR "&y does not escape"
1357 }
1358
1359 //go:noescape
1360
1361 func foo144b(*int)
1362
1363 // issue 7313: for loop init should not be treated as "in loop"
1364
1365 type List struct {
1366         Next *List
1367 }
1368
1369 func foo145(l List) { // ERROR "l does not escape"
1370         var p *List
1371         for p = &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1372         }
1373 }
1374
1375 func foo146(l List) { // ERROR "l does not escape"
1376         var p *List
1377         p = &l // ERROR "&l does not escape"
1378         for ; p.Next != nil; p = p.Next {
1379         }
1380 }
1381
1382 func foo147(l List) { // ERROR "l does not escape"
1383         var p *List
1384         p = &l // ERROR "&l does not escape"
1385         for p.Next != nil {
1386                 p = p.Next
1387         }
1388 }
1389
1390 func foo148(l List) { // ERROR " l does not escape"
1391         for p := &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1392         }
1393 }
1394
1395 // related: address of variable should have depth of variable, not of loop
1396
1397 func foo149(l List) { // ERROR " l does not escape"
1398         var p *List
1399         for {
1400                 for p = &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1401                 }
1402         }
1403 }
1404
1405 // issue 7934: missed ... if element type had no pointers
1406
1407 var save150 []byte
1408
1409 func foo150(x ...byte) { // ERROR "leaking param: x"
1410         save150 = x
1411 }
1412
1413 func bar150() {
1414         foo150(1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
1415 }
1416
1417 // issue 7931: bad handling of slice of array
1418
1419 var save151 *int
1420
1421 func foo151(x *int) { // ERROR "leaking param: x"
1422         save151 = x
1423 }
1424
1425 func bar151() {
1426         var a [64]int // ERROR "moved to heap: a"
1427         a[4] = 101
1428         foo151(&(&a)[4:8][0]) // ERROR "&\(&a\)\[4:8\]\[0\] escapes to heap" "&a escapes to heap"
1429 }
1430
1431 func bar151b() {
1432         var a [10]int      // ERROR "moved to heap: a"
1433         b := a[:]          // ERROR "a escapes to heap"
1434         foo151(&b[4:8][0]) // ERROR "&b\[4:8\]\[0\] escapes to heap"
1435 }
1436
1437 func bar151c() {
1438         var a [64]int // ERROR "moved to heap: a"
1439         a[4] = 101
1440         foo151(&(&a)[4:8:8][0]) // ERROR "&\(&a\)\[4:8:8\]\[0\] escapes to heap" "&a escapes to heap"
1441 }
1442
1443 func bar151d() {
1444         var a [10]int        // ERROR "moved to heap: a"
1445         b := a[:]            // ERROR "a escapes to heap"
1446         foo151(&b[4:8:8][0]) // ERROR "&b\[4:8:8\]\[0\] escapes to heap"
1447 }
1448
1449 // issue 8120
1450
1451 type U struct {
1452         s *string
1453 }
1454
1455 func (u *U) String() *string { // ERROR "leaking param u content to result ~r0"
1456         return u.s
1457 }
1458
1459 type V struct {
1460         s *string
1461 }
1462
1463 func NewV(u U) *V { // ERROR "leaking param: u"
1464         return &V{u.String()} // ERROR "&V literal escapes to heap" "u does not escape"
1465 }
1466
1467 func foo152() {
1468         a := "a"   // ERROR "moved to heap: a"
1469         u := U{&a} // ERROR "&a escapes to heap"
1470         v := NewV(u)
1471         println(v)
1472 }
1473
1474 // issue 8176 - &x in type switch body not marked as escaping
1475
1476 func foo153(v interface{}) *int { // ERROR "leaking param: v"
1477         switch x := v.(type) {
1478         case int: // ERROR "moved to heap: x"
1479                 return &x // ERROR "&x escapes to heap"
1480         }
1481         panic(0)
1482 }
1483
1484 // issue 8185 - &result escaping into result
1485
1486 func f() (x int, y *int) { // ERROR "moved to heap: x"
1487         y = &x // ERROR "&x escapes to heap"
1488         return
1489 }
1490
1491 func g() (x interface{}) { // ERROR "moved to heap: x"
1492         x = &x // ERROR "&x escapes to heap"
1493         return
1494 }
1495
1496 var sink interface{}
1497
1498 type Lit struct {
1499         p *int
1500 }
1501
1502 func ptrlitNoescape() {
1503         // Both literal and element do not escape.
1504         i := 0
1505         x := &Lit{&i} // ERROR "&Lit literal does not escape" "&i does not escape"
1506         _ = x
1507 }
1508
1509 func ptrlitNoEscape2() {
1510         // Literal does not escape, but element does.
1511         i := 0 // ERROR "moved to heap: i"
1512         x := &Lit{&i} // ERROR "&Lit literal does not escape" "&i escapes to heap"
1513         sink = *x
1514 }
1515
1516 func ptrlitEscape() {
1517         // Both literal and element escape.
1518         i := 0 // ERROR "moved to heap: i"
1519         x := &Lit{&i} // ERROR "&Lit literal escapes to heap" "&i escapes to heap"
1520         sink = x
1521 }
1522
1523 // self-assignments
1524
1525 type Buffer struct {
1526         arr  [64]byte
1527         buf1 []byte
1528         buf2 []byte
1529         str1 string
1530         str2 string
1531 }
1532
1533 func (b *Buffer) foo() { // ERROR "b does not escape"
1534         b.buf1 = b.buf1[1:2]   // ERROR "ignoring self-assignment to b.buf1"
1535         b.buf1 = b.buf1[1:2:3] // ERROR "ignoring self-assignment to b.buf1"
1536         b.buf1 = b.buf2[1:2]   // ERROR "ignoring self-assignment to b.buf1"
1537         b.buf1 = b.buf2[1:2:3] // ERROR "ignoring self-assignment to b.buf1"
1538 }
1539
1540 func (b *Buffer) bar() { // ERROR "leaking param: b"
1541         b.buf1 = b.arr[1:2] // ERROR "b.arr escapes to heap"
1542 }
1543
1544 func (b *Buffer) baz() { // ERROR "b does not escape"
1545         b.str1 = b.str1[1:2] // ERROR "ignoring self-assignment to b.str1"
1546         b.str1 = b.str2[1:2] // ERROR "ignoring self-assignment to b.str1"
1547 }
1548
1549 func (b *Buffer) bat() { // ERROR "leaking param: b"
1550         o := new(Buffer) // ERROR "new\(Buffer\) escapes to heap"
1551         o.buf1 = b.buf1[1:2]
1552         sink = o
1553 }
1554
1555 func quux(sp *string, bp *[]byte) { // ERROR "sp does not escape" "bp does not escape"
1556         *sp = (*sp)[1:2] // ERROR "quux ignoring self-assignment to \*sp"
1557         *bp = (*bp)[1:2] // ERROR "quux ignoring self-assignment to \*bp"
1558 }
1559
1560 type StructWithString struct {
1561         p *int
1562         s string
1563 }
1564
1565 // This is escape analysis false negative.
1566 // We assign the pointer to x.p but leak x.s. Escape analysis coarsens flows
1567 // to just x, and thus &i looks escaping.
1568 func fieldFlowTracking() {
1569         var x StructWithString
1570         i := 0 // ERROR "moved to heap: i"
1571         x.p = &i // ERROR "&i escapes to heap"
1572         sink = x.s
1573 }
1574
1575 // String operations.
1576
1577 func slicebytetostring0() {
1578         b := make([]byte, 20) // ERROR "does not escape"
1579         s := string(b)        // ERROR "string\(b\) does not escape"
1580         _ = s
1581 }
1582
1583 func slicebytetostring1() {
1584         b := make([]byte, 20) // ERROR "does not escape"
1585         s := string(b)        // ERROR "string\(b\) does not escape"
1586         s1 := s[0:1]
1587         _ = s1
1588 }
1589
1590 func slicebytetostring2() {
1591         b := make([]byte, 20) // ERROR "does not escape"
1592         s := string(b)        // ERROR "string\(b\) escapes to heap"
1593         s1 := s[0:1]          // ERROR "moved to heap: s1"
1594         sink = &s1            // ERROR "&s1 escapes to heap"
1595 }
1596
1597 func slicebytetostring3() {
1598         b := make([]byte, 20) // ERROR "does not escape"
1599         s := string(b)        // ERROR "string\(b\) escapes to heap"
1600         s1 := s[0:1]
1601         sink = s1
1602 }
1603
1604 func addstr0() {
1605         s0 := "a"
1606         s1 := "b"
1607         s := s0 + s1 // ERROR "s0 \+ s1 does not escape"
1608         _ = s
1609 }
1610
1611 func addstr1() {
1612         s0 := "a"
1613         s1 := "b"
1614         s := "c"
1615         s += s0 + s1 // ERROR "s0 \+ s1 does not escape"
1616         _ = s
1617 }
1618
1619 func addstr2() {
1620         b := make([]byte, 20) // ERROR "does not escape"
1621         s0 := "a"
1622         s := string(b) + s0 // ERROR "string\(b\) does not escape" "string\(b\) \+ s0 does not escape"
1623         _ = s
1624 }
1625
1626 func addstr3() {
1627         s0 := "a"
1628         s1 := "b"
1629         s := s0 + s1 // ERROR "s0 \+ s1 escapes to heap"
1630         s2 := s[0:1]
1631         sink = s2
1632 }
1633
1634 func intstring0() bool {
1635         // string does not escape
1636         x := '0'
1637         s := string(x) // ERROR "string\(x\) does not escape"
1638         return s == "0"
1639 }
1640
1641 func intstring1() string {
1642         // string does not escape, but the buffer does
1643         x := '0'
1644         s := string(x) // ERROR "string\(x\) escapes to heap"
1645         return s
1646 }
1647
1648 func intstring2() {
1649         // string escapes to heap
1650         x := '0'
1651         s := string(x) // ERROR "string\(x\) escapes to heap" "moved to heap: s"
1652         sink = &s      // ERROR "&s escapes to heap"
1653 }