1 // State built in a frame and read after that frame has died.
2 //
3 // Every W31-class defect - a returned pointer whose pointee owned slices or
4 // func values, a struct whose field was a stack alloca, a constructor whose
5 // result aliased its own frame - looks like this: correct on the first read,
6 // garbage or a SIGSEGV once something else has reused the memory. So each case
7 // here builds its value in a function, returns it, churns 8 KiB of stack and
8 // arena, and only then reads it back.
9 //
10 // The shapes are the ones the arena model has to get right: a slice of
11 // pointers, a pushed slice of pointers, a map with slice values, a struct
12 // with an embedded slice, a nested pointer slice built one frame deeper, and a
13 // func value in a returned struct.
14 //
15 // usage: escape_shapes exit 0 when every shape survives the frame
16 package main
17 18 type node struct {
19 n int64
20 name []byte
21 }
22 23 type withMap struct {
24 m map[int32]int64
25 s []byte
26 }
27 28 type withFunc struct {
29 f func(x int32) (r int32)
30 n int64
31 }
32 33 type big struct {
34 a, b, c, d int64
35 name []byte
36 }
37 38 type holder struct {
39 items []*node
40 bigs []big
41 f func(x int32) (r int32)
42 }
43 44 type reg struct {
45 m map[int32][]byte
46 }
47 48 type refs struct {
49 items []*node
50 }
51 52 func double(x int32) (r int32) { return x * 2 }
53 54 func mkPushed(k int64) (s []*node) {
55 s = []*node{}
56 s = push(s, &node{n: k, name: "a"})
57 s = push(s, &node{n: k + 1, name: "b"})
58 return
59 }
60 61 func mkMap(k int64) (w withMap) {
62 w = withMap{m: map[int32]int64{}, s: "str"}
63 w.m[1] = k
64 w.m[2] = k + 1
65 return
66 }
67 68 func mkBig(k int64) (b big) {
69 b = big{a: k, b: k + 1, c: k + 2, d: k + 3, name: "big"}
70 return
71 }
72 73 func mkHolder(k int64) (h *holder) {
74 h = &holder{
75 items: []*node{{n: k, name: "x"}, {n: k + 1, name: "y"}},
76 bigs: []big{{a: k, b: k + 1, c: k + 2, d: k + 3, name: "one"}},
77 f: double,
78 }
79 return
80 }
81 82 func mkRegDirect() (r *reg) {
83 r = ®{m: map[int32][]byte{1: "one", 2: "two"}}
84 return
85 }
86 87 func mkRegInline() (r *reg) {
88 m := map[int32][]byte{}
89 b := []byte("inline")
90 m[1] = b
91 c := []byte("second")
92 m[2] = c
93 r = ®{m: m}
94 return
95 }
96 97 func mkNodes() (s []*node) {
98 s = []*node{{n: 41, name: "h"}, {n: 42, name: "i"}}
99 return
100 }
101 102 func mkRefsNested() (r *refs) {
103 r = &refs{items: mkNodes()}
104 return
105 }
106 107 // churn moves the stack and the arenas before the values are read.
108 func churn() (n int64) {
109 g := []byte{:8192}
110 for i := int32(0); i < 8192; i++ {
111 g[i] = byte(i)
112 }
113 return int64(g[8191])
114 }
115 116 func main() {
117 // Termination: a batch program must still be stoppable, so the entry
118 // handles the channel the runtime closes on SIGINT/SIGTERM.
119 select {
120 case <-shutdown:
121 return
122 default:
123 }
124 125 p := mkPushed(5)
126 _ = churn()
127 if p[0].n != 5 || p[1].n != 6 || !sameText(p[0].name, "a") {
128 panic("pushed slice of pointers lost its state")
129 }
130 131 w := mkMap(10)
132 _ = churn()
133 if w.m[1] != 10 || w.m[2] != 11 || !sameText(w.s, "str") {
134 panic("map-valued struct lost its state")
135 }
136 137 b := mkBig(20)
138 _ = churn()
139 if b.a != 20 || b.b != 21 || b.c != 22 || b.d != 23 || !sameText(b.name, "big") {
140 panic("wide struct lost its state")
141 }
142 143 h := mkHolder(30)
144 _ = churn()
145 if h.items[0].n != 30 || h.items[1].n != 31 || h.bigs[0].a != 30 ||
146 h.bigs[0].d != 33 || h.f(3) != 6 {
147 panic("holder lost its state")
148 }
149 150 d := mkRegDirect()
151 _ = churn()
152 if !sameText(d.m[1], "one") || !sameText(d.m[2], "two") {
153 panic("map literal with text values lost its state")
154 }
155 156 i := mkRegInline()
157 _ = churn()
158 if !sameText(i.m[1], "inline") || !sameText(i.m[2], "second") {
159 panic("map whose values were frame slices lost its state")
160 }
161 162 n := mkRefsNested()
163 _ = churn()
164 if n.items[0].n != 41 || n.items[1].n != 42 {
165 panic("pointer slice built one frame deeper lost its state")
166 }
167 168 println("escape shapes ok")
169 }
170 171 func sameText(a []byte, b []byte) (ok bool) {
172 if len(a) != len(b) {
173 return false
174 }
175 for i := int32(0); i < len(a); i++ {
176 if a[i] != b[i] {
177 return false
178 }
179 }
180 return true
181 }
182