Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // run |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 2 | |
| 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 | |
| 7 | package main |
| 8 | |
Rob Pike | 83976e3 | 2012-02-19 14:28:53 +1100 | [diff] [blame] | 9 | // Test for correct heap-moving of escaped variables. |
| 10 | // It is hard to check for the allocations, but it is easy |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 11 | // to check that if you call the function twice at the |
| 12 | // same stack level, the pointers returned should be |
| 13 | // different. |
| 14 | |
| 15 | var bad = false |
| 16 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 17 | var allptr = make([]*int, 0, 100) |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 18 | |
| 19 | func noalias(p, q *int, s string) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 20 | n := len(allptr) |
Rémy Oudompheng | ba97d52 | 2012-08-31 22:23:37 +0200 | [diff] [blame] | 21 | *p = -(n + 1) |
| 22 | *q = -(n + 2) |
| 23 | allptr = allptr[0 : n+2] |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 24 | allptr[n] = p |
| 25 | allptr[n+1] = q |
| 26 | n += 2 |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 27 | for i := 0; i < n; i++ { |
| 28 | if allptr[i] != nil && *allptr[i] != -(i+1) { |
Rémy Oudompheng | ba97d52 | 2012-08-31 22:23:37 +0200 | [diff] [blame] | 29 | println("aliased pointers", -(i + 1), *allptr[i], "after", s) |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 30 | allptr[i] = nil |
| 31 | bad = true |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func val(p, q *int, v int, s string) { |
| 37 | if *p != v { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 38 | println("wrong value want", v, "got", *p, "after", s) |
| 39 | bad = true |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 40 | } |
| 41 | if *q != v+1 { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 42 | println("wrong value want", v+1, "got", *q, "after", s) |
| 43 | bad = true |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
| 47 | func chk(p, q *int, v int, s string) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 48 | val(p, q, v, s) |
| 49 | noalias(p, q, s) |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | func chkalias(p, q *int, v int, s string) { |
| 53 | if p != q { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 54 | println("want aliased pointers but got different after", s) |
Alan Donovan | 052c942 | 2013-02-12 13:17:49 -0500 | [diff] [blame] | 55 | bad = true |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 56 | } |
| 57 | if *q != v+1 { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 58 | println("wrong value want", v+1, "got", *q, "after", s) |
Alan Donovan | 052c942 | 2013-02-12 13:17:49 -0500 | [diff] [blame] | 59 | bad = true |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | func i_escapes(x int) *int { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 64 | var i int |
| 65 | i = x |
| 66 | return &i |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | func j_escapes(x int) *int { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 70 | var j int = x |
| 71 | j = x |
| 72 | return &j |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | func k_escapes(x int) *int { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 76 | k := x |
| 77 | return &k |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | func in_escapes(x int) *int { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 81 | return &x |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | func send(c chan int, x int) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 85 | c <- x |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | func select_escapes(x int) *int { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 89 | c := make(chan int) |
| 90 | go send(c, x) |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 91 | select { |
| 92 | case req := <-c: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 93 | return &req |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 94 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 95 | return nil |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | func select_escapes1(x int, y int) (*int, *int) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 99 | c := make(chan int) |
| 100 | var a [2]int |
| 101 | var p [2]*int |
| 102 | a[0] = x |
| 103 | a[1] = y |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 104 | for i := 0; i < 2; i++ { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 105 | go send(c, a[i]) |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 106 | select { |
| 107 | case req := <-c: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 108 | p[i] = &req |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | return p[0], p[1] |
| 112 | } |
| 113 | |
| 114 | func range_escapes(x int) *int { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 115 | var a [1]int |
| 116 | a[0] = x |
Russ Cox | ae54cf7 | 2009-09-15 12:42:24 -0700 | [diff] [blame] | 117 | for _, v := range a { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 118 | return &v |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 119 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 120 | return nil |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // *is* aliased |
| 124 | func range_escapes2(x, y int) (*int, *int) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 125 | var a [2]int |
| 126 | var p [2]*int |
| 127 | a[0] = x |
| 128 | a[1] = y |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 129 | for k, v := range a { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 130 | p[k] = &v |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 131 | } |
| 132 | return p[0], p[1] |
| 133 | } |
| 134 | |
| 135 | // *is* aliased |
| 136 | func for_escapes2(x int, y int) (*int, *int) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 137 | var p [2]*int |
| 138 | n := 0 |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 139 | for i := x; n < 2; i = y { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 140 | p[n] = &i |
| 141 | n++ |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 142 | } |
| 143 | return p[0], p[1] |
| 144 | } |
| 145 | |
Rémy Oudompheng | ba97d52 | 2012-08-31 22:23:37 +0200 | [diff] [blame] | 146 | func 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 Cox | 97d0e8f | 2010-03-26 18:01:02 -0700 | [diff] [blame] | 158 | func out_escapes(i int) (x int, p *int) { |
| 159 | x = i |
Rémy Oudompheng | ba97d52 | 2012-08-31 22:23:37 +0200 | [diff] [blame] | 160 | p = &x // ERROR "address of out parameter" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 161 | return |
Russ Cox | 97d0e8f | 2010-03-26 18:01:02 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | func out_escapes_2(i int) (x int, p *int) { |
| 165 | x = i |
Rémy Oudompheng | ba97d52 | 2012-08-31 22:23:37 +0200 | [diff] [blame] | 166 | return x, &x // ERROR "address of out parameter" |
Russ Cox | 97d0e8f | 2010-03-26 18:01:02 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | func defer1(i int) (x int) { |
| 170 | c := make(chan int) |
| 171 | go func() { x = i; c <- 1 }() |
| 172 | <-c |
| 173 | return |
| 174 | } |
| 175 | |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 176 | func main() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 177 | p, q := i_escapes(1), i_escapes(2) |
| 178 | chk(p, q, 1, "i_escapes") |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 179 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 180 | p, q = j_escapes(3), j_escapes(4) |
| 181 | chk(p, q, 3, "j_escapes") |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 182 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 183 | p, q = k_escapes(5), k_escapes(6) |
| 184 | chk(p, q, 5, "k_escapes") |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 185 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 186 | p, q = in_escapes(7), in_escapes(8) |
| 187 | chk(p, q, 7, "in_escapes") |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 188 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 189 | p, q = select_escapes(9), select_escapes(10) |
| 190 | chk(p, q, 9, "select_escapes") |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 191 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 192 | p, q = select_escapes1(11, 12) |
| 193 | chk(p, q, 11, "select_escapes1") |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 194 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 195 | p, q = range_escapes(13), range_escapes(14) |
| 196 | chk(p, q, 13, "range_escapes") |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 197 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 198 | p, q = range_escapes2(101, 102) |
| 199 | chkalias(p, q, 101, "range_escapes2") |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 200 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 201 | p, q = for_escapes2(103, 104) |
| 202 | chkalias(p, q, 103, "for_escapes2") |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 203 | |
Rémy Oudompheng | ba97d52 | 2012-08-31 22:23:37 +0200 | [diff] [blame] | 204 | p, q = for_escapes3(105, 106) |
| 205 | chk(p, q, 105, "for_escapes3") |
| 206 | |
Russ Cox | 97d0e8f | 2010-03-26 18:01:02 -0700 | [diff] [blame] | 207 | _, p = out_escapes(15) |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 208 | _, q = out_escapes(16) |
| 209 | chk(p, q, 15, "out_escapes") |
Russ Cox | 97d0e8f | 2010-03-26 18:01:02 -0700 | [diff] [blame] | 210 | |
| 211 | _, p = out_escapes_2(17) |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 212 | _, q = out_escapes_2(18) |
| 213 | chk(p, q, 17, "out_escapes_2") |
Russ Cox | 97d0e8f | 2010-03-26 18:01:02 -0700 | [diff] [blame] | 214 | |
| 215 | x := defer1(20) |
| 216 | if x != 20 { |
| 217 | println("defer failed", x) |
| 218 | bad = true |
| 219 | } |
| 220 | |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 221 | if bad { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 222 | panic("BUG: no escape") |
Russ Cox | 391425a | 2009-01-29 17:38:58 -0800 | [diff] [blame] | 223 | } |
| 224 | } |