blob: 252a1e59cc80b55a96e7f36fd45591100a11bbd7 [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// run
Russ Cox391425a2009-01-29 17:38:58 -08002
3// Copyright 2009 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
7package main
8
Rob Pike83976e32012-02-19 14:28:53 +11009// Test for correct heap-moving of escaped variables.
10// It is hard to check for the allocations, but it is easy
Russ Cox391425a2009-01-29 17:38:58 -080011// to check that if you call the function twice at the
12// same stack level, the pointers returned should be
13// different.
14
15var bad = false
16
Rob Pike4f61fc92010-09-04 10:36:13 +100017var allptr = make([]*int, 0, 100)
Russ Cox391425a2009-01-29 17:38:58 -080018
19func noalias(p, q *int, s string) {
Rob Pike4f61fc92010-09-04 10:36:13 +100020 n := len(allptr)
Rémy Oudomphengba97d522012-08-31 22:23:37 +020021 *p = -(n + 1)
22 *q = -(n + 2)
23 allptr = allptr[0 : n+2]
Rob Pike4f61fc92010-09-04 10:36:13 +100024 allptr[n] = p
25 allptr[n+1] = q
26 n += 2
Russ Cox391425a2009-01-29 17:38:58 -080027 for i := 0; i < n; i++ {
28 if allptr[i] != nil && *allptr[i] != -(i+1) {
Rémy Oudomphengba97d522012-08-31 22:23:37 +020029 println("aliased pointers", -(i + 1), *allptr[i], "after", s)
Rob Pike4f61fc92010-09-04 10:36:13 +100030 allptr[i] = nil
31 bad = true
Russ Cox391425a2009-01-29 17:38:58 -080032 }
33 }
34}
35
36func val(p, q *int, v int, s string) {
37 if *p != v {
Rob Pike4f61fc92010-09-04 10:36:13 +100038 println("wrong value want", v, "got", *p, "after", s)
39 bad = true
Russ Cox391425a2009-01-29 17:38:58 -080040 }
41 if *q != v+1 {
Rob Pike4f61fc92010-09-04 10:36:13 +100042 println("wrong value want", v+1, "got", *q, "after", s)
43 bad = true
Russ Cox391425a2009-01-29 17:38:58 -080044 }
45}
46
47func chk(p, q *int, v int, s string) {
Rob Pike4f61fc92010-09-04 10:36:13 +100048 val(p, q, v, s)
49 noalias(p, q, s)
Russ Cox391425a2009-01-29 17:38:58 -080050}
51
52func chkalias(p, q *int, v int, s string) {
53 if p != q {
Rob Pike4f61fc92010-09-04 10:36:13 +100054 println("want aliased pointers but got different after", s)
Alan Donovan052c9422013-02-12 13:17:49 -050055 bad = true
Russ Cox391425a2009-01-29 17:38:58 -080056 }
57 if *q != v+1 {
Rob Pike4f61fc92010-09-04 10:36:13 +100058 println("wrong value want", v+1, "got", *q, "after", s)
Alan Donovan052c9422013-02-12 13:17:49 -050059 bad = true
Russ Cox391425a2009-01-29 17:38:58 -080060 }
61}
62
63func i_escapes(x int) *int {
Rob Pike4f61fc92010-09-04 10:36:13 +100064 var i int
65 i = x
66 return &i
Russ Cox391425a2009-01-29 17:38:58 -080067}
68
69func j_escapes(x int) *int {
Rob Pike4f61fc92010-09-04 10:36:13 +100070 var j int = x
71 j = x
72 return &j
Russ Cox391425a2009-01-29 17:38:58 -080073}
74
75func k_escapes(x int) *int {
Rob Pike4f61fc92010-09-04 10:36:13 +100076 k := x
77 return &k
Russ Cox391425a2009-01-29 17:38:58 -080078}
79
80func in_escapes(x int) *int {
Rob Pike4f61fc92010-09-04 10:36:13 +100081 return &x
Russ Cox391425a2009-01-29 17:38:58 -080082}
83
84func send(c chan int, x int) {
Rob Pike4f61fc92010-09-04 10:36:13 +100085 c <- x
Russ Cox391425a2009-01-29 17:38:58 -080086}
87
88func select_escapes(x int) *int {
Rob Pike4f61fc92010-09-04 10:36:13 +100089 c := make(chan int)
90 go send(c, x)
Russ Cox391425a2009-01-29 17:38:58 -080091 select {
92 case req := <-c:
Rob Pike4f61fc92010-09-04 10:36:13 +100093 return &req
Russ Cox391425a2009-01-29 17:38:58 -080094 }
Rob Pike4f61fc92010-09-04 10:36:13 +100095 return nil
Russ Cox391425a2009-01-29 17:38:58 -080096}
97
98func select_escapes1(x int, y int) (*int, *int) {
Rob Pike4f61fc92010-09-04 10:36:13 +100099 c := make(chan int)
100 var a [2]int
101 var p [2]*int
102 a[0] = x
103 a[1] = y
Russ Cox391425a2009-01-29 17:38:58 -0800104 for i := 0; i < 2; i++ {
Rob Pike4f61fc92010-09-04 10:36:13 +1000105 go send(c, a[i])
Russ Cox391425a2009-01-29 17:38:58 -0800106 select {
107 case req := <-c:
Rob Pike4f61fc92010-09-04 10:36:13 +1000108 p[i] = &req
Russ Cox391425a2009-01-29 17:38:58 -0800109 }
110 }
111 return p[0], p[1]
112}
113
114func range_escapes(x int) *int {
Rob Pike4f61fc92010-09-04 10:36:13 +1000115 var a [1]int
116 a[0] = x
Russ Coxae54cf72009-09-15 12:42:24 -0700117 for _, v := range a {
Rob Pike4f61fc92010-09-04 10:36:13 +1000118 return &v
Russ Cox391425a2009-01-29 17:38:58 -0800119 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000120 return nil
Russ Cox391425a2009-01-29 17:38:58 -0800121}
122
123// *is* aliased
124func range_escapes2(x, y int) (*int, *int) {
Rob Pike4f61fc92010-09-04 10:36:13 +1000125 var a [2]int
126 var p [2]*int
127 a[0] = x
128 a[1] = y
Russ Cox391425a2009-01-29 17:38:58 -0800129 for k, v := range a {
Rob Pike4f61fc92010-09-04 10:36:13 +1000130 p[k] = &v
Russ Cox391425a2009-01-29 17:38:58 -0800131 }
132 return p[0], p[1]
133}
134
135// *is* aliased
136func for_escapes2(x int, y int) (*int, *int) {
Rob Pike4f61fc92010-09-04 10:36:13 +1000137 var p [2]*int
138 n := 0
Russ Cox391425a2009-01-29 17:38:58 -0800139 for i := x; n < 2; i = y {
Rob Pike4f61fc92010-09-04 10:36:13 +1000140 p[n] = &i
141 n++
Russ Cox391425a2009-01-29 17:38:58 -0800142 }
143 return p[0], p[1]
144}
145
Rémy Oudomphengba97d522012-08-31 22:23:37 +0200146func for_escapes3(x int, y int) (*int, *int) {
147 var f [2]func() *int
148 n := 0
149 for i := x; n < 2; i = y {
150 p := new(int)
151 *p = i
152 f[n] = func() *int { return p }
153 n++
154 }
155 return f[0](), f[1]()
156}
157
Russ Cox97d0e8f2010-03-26 18:01:02 -0700158func out_escapes(i int) (x int, p *int) {
159 x = i
Rémy Oudomphengba97d522012-08-31 22:23:37 +0200160 p = &x // ERROR "address of out parameter"
Rob Pike4f61fc92010-09-04 10:36:13 +1000161 return
Russ Cox97d0e8f2010-03-26 18:01:02 -0700162}
163
164func out_escapes_2(i int) (x int, p *int) {
165 x = i
Rémy Oudomphengba97d522012-08-31 22:23:37 +0200166 return x, &x // ERROR "address of out parameter"
Russ Cox97d0e8f2010-03-26 18:01:02 -0700167}
168
169func defer1(i int) (x int) {
170 c := make(chan int)
171 go func() { x = i; c <- 1 }()
172 <-c
173 return
174}
175
Russ Cox391425a2009-01-29 17:38:58 -0800176func main() {
Rob Pike4f61fc92010-09-04 10:36:13 +1000177 p, q := i_escapes(1), i_escapes(2)
178 chk(p, q, 1, "i_escapes")
Russ Cox391425a2009-01-29 17:38:58 -0800179
Rob Pike4f61fc92010-09-04 10:36:13 +1000180 p, q = j_escapes(3), j_escapes(4)
181 chk(p, q, 3, "j_escapes")
Russ Cox391425a2009-01-29 17:38:58 -0800182
Rob Pike4f61fc92010-09-04 10:36:13 +1000183 p, q = k_escapes(5), k_escapes(6)
184 chk(p, q, 5, "k_escapes")
Russ Cox391425a2009-01-29 17:38:58 -0800185
Rob Pike4f61fc92010-09-04 10:36:13 +1000186 p, q = in_escapes(7), in_escapes(8)
187 chk(p, q, 7, "in_escapes")
Russ Cox391425a2009-01-29 17:38:58 -0800188
Rob Pike4f61fc92010-09-04 10:36:13 +1000189 p, q = select_escapes(9), select_escapes(10)
190 chk(p, q, 9, "select_escapes")
Russ Cox391425a2009-01-29 17:38:58 -0800191
Rob Pike4f61fc92010-09-04 10:36:13 +1000192 p, q = select_escapes1(11, 12)
193 chk(p, q, 11, "select_escapes1")
Russ Cox391425a2009-01-29 17:38:58 -0800194
Rob Pike4f61fc92010-09-04 10:36:13 +1000195 p, q = range_escapes(13), range_escapes(14)
196 chk(p, q, 13, "range_escapes")
Russ Cox391425a2009-01-29 17:38:58 -0800197
Rob Pike4f61fc92010-09-04 10:36:13 +1000198 p, q = range_escapes2(101, 102)
199 chkalias(p, q, 101, "range_escapes2")
Russ Cox391425a2009-01-29 17:38:58 -0800200
Rob Pike4f61fc92010-09-04 10:36:13 +1000201 p, q = for_escapes2(103, 104)
202 chkalias(p, q, 103, "for_escapes2")
Russ Cox391425a2009-01-29 17:38:58 -0800203
Rémy Oudomphengba97d522012-08-31 22:23:37 +0200204 p, q = for_escapes3(105, 106)
205 chk(p, q, 105, "for_escapes3")
206
Russ Cox97d0e8f2010-03-26 18:01:02 -0700207 _, p = out_escapes(15)
Rob Pike4f61fc92010-09-04 10:36:13 +1000208 _, q = out_escapes(16)
209 chk(p, q, 15, "out_escapes")
Russ Cox97d0e8f2010-03-26 18:01:02 -0700210
211 _, p = out_escapes_2(17)
Rob Pike4f61fc92010-09-04 10:36:13 +1000212 _, q = out_escapes_2(18)
213 chk(p, q, 17, "out_escapes_2")
Russ Cox97d0e8f2010-03-26 18:01:02 -0700214
215 x := defer1(20)
216 if x != 20 {
217 println("defer failed", x)
218 bad = true
219 }
220
Russ Cox391425a2009-01-29 17:38:58 -0800221 if bad {
Rob Pike4f61fc92010-09-04 10:36:13 +1000222 panic("BUG: no escape")
Russ Cox391425a2009-01-29 17:38:58 -0800223 }
224}