1 // Element literals in a pointer-element aggregate literal must be allocated in
2 // the arena, not on the stack.
3 //
4 // `[]*T{{n: 1}, {n: 2}, {n: 3}}` means three `&T{...}` values. The address of
5 // each pointee is stored into the slice's backing array, which outlives the
6 // frame whenever the slice is returned or stored in a receiver - so the pointee
7 // escapes. stage4 built it with a stack alloca and never called
8 // markAddrEscaping, while the explicit `&T{...}` path always did, so the
9 // returned slice carried pointers into a dead frame:
10 //
11 // func makeThree(k int64) (s []*T) {
12 // s = []*T{{n: k}, {n: k + 1}, {n: k + 2}}
13 // return
14 // }
15 // makeThree(9) -> 9 10 7 (legacy: 9 10 11)
16 //
17 // The last element's stores were also dropped from the emitted IR, so it read
18 // back whatever the recycled slot held. The same shape is what crashed
19 // `idna.Lookup().ToUnicode` on its second call (a func-value field of a
20 // dangling pointee was executed); with the escape mark those calls are clean.
21 //
22 // usage: ptr_elem_lit_escape exit 0 when every element survives the return
23 package main
24 25 type node struct {
26 n int64
27 name []byte
28 }
29 30 type hook struct {
31 info func(x int32) (r int32)
32 n int64
33 }
34 35 func double(x int32) (r int32) { return x * 2 }
36 func triple(x int32) (r int32) { return x * 3 }
37 38 func makeNodes(k int64) (s []*node) {
39 s = []*node{
40 {n: k, name: "a"},
41 {n: k + 1, name: "b"},
42 {n: k + 2, name: "c"},
43 }
44 return
45 }
46 47 func makeHooks() (s []*hook) {
48 s = []*hook{
49 {info: double, n: 1},
50 {info: triple, n: 2},
51 {info: double, n: 3},
52 }
53 return
54 }
55 56 func makeHookArray() (a [3]*hook) {
57 a = [3]*hook{
58 {info: double, n: 11},
59 {info: triple, n: 22},
60 {info: double, n: 33},
61 }
62 return
63 }
64 65 func makeHookMap() (m map[int32]*hook) {
66 m = map[int32]*hook{
67 0: {info: double, n: 5},
68 1: {info: triple, n: 6},
69 }
70 return
71 }
72 73 // churn moves the stack and the arenas before the values are read again.
74 func churn() (n int64) {
75 big := []byte{:4096}
76 for i := int32(0); i < 4096; i++ {
77 big[i] = byte(i)
78 }
79 return int64(big[4095])
80 }
81 82 func main() {
83 // Termination: a batch program must still be stoppable, so the entry
84 // handles the channel the runtime closes on SIGINT/SIGTERM.
85 select {
86 case <-shutdown:
87 return
88 default:
89 }
90 91 nodes := makeNodes(9)
92 _ = churn()
93 if nodes[0].n != 9 || nodes[1].n != 10 || nodes[2].n != 11 {
94 panic("returned []*T lost an element after the frame died")
95 }
96 if nodes[2].name != nil && !bytesEqual(nodes[2].name, "c") {
97 panic("returned []*T lost the last element's slice field")
98 }
99 100 hooks := makeHooks()
101 _ = churn()
102 if hooks[2].n != 3 || hooks[2].info(2) != 4 {
103 panic("returned []*T lost the last element's func field")
104 }
105 if hooks[0].info(3) != 6 || hooks[1].info(3) != 9 {
106 panic("returned []*T has the wrong func field in an earlier element")
107 }
108 109 arr := makeHookArray()
110 _ = churn()
111 if arr[2].n != 33 || arr[2].info(2) != 4 || arr[0].info(2) != 4 {
112 panic("[3]*T literal lost an element")
113 }
114 115 m := makeHookMap()
116 _ = churn()
117 if m[0].n != 5 || m[1].n != 6 || m[1].info(2) != 6 {
118 panic("map[K]*T literal lost an element")
119 }
120 121 println("ptr elem lit escape ok")
122 }
123 124 func bytesEqual(a []byte, b []byte) (ok bool) {
125 if len(a) != len(b) {
126 return false
127 }
128 for i := int32(0); i < len(a); i++ {
129 if a[i] != b[i] {
130 return false
131 }
132 }
133 return true
134 }
135