Ian Lance Taylor | e08008e | 2012-11-07 12:33:54 -0800 | [diff] [blame] | 1 | // skip |
Russ Cox | cd22afa | 2012-09-23 13:16:14 -0400 | [diff] [blame] | 2 | |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 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 | // Generate test of index and slice bounds checks. |
Ian Lance Taylor | e08008e | 2012-11-07 12:33:54 -0800 | [diff] [blame] | 8 | // The actual tests are index0.go, index1.go, index2.go. |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 9 | |
| 10 | package main |
| 11 | |
| 12 | import ( |
| 13 | "bufio" |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 14 | "fmt" |
| 15 | "os" |
Ian Lance Taylor | 3e50372 | 2012-11-08 15:43:28 -0800 | [diff] [blame] | 16 | "unsafe" |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 17 | ) |
| 18 | |
| 19 | const prolog = ` |
| 20 | |
| 21 | package main |
| 22 | |
| 23 | import ( |
| 24 | "runtime" |
| 25 | ) |
| 26 | |
| 27 | type quad struct { x, y, z, w int } |
| 28 | |
| 29 | const ( |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 30 | cj = 100011 |
| 31 | ci int = 100012 |
| 32 | ci8 int8 = 115 |
| 33 | ci16 int16 = 10016 |
| 34 | ci32 int32 = 100013 |
| 35 | ci64 int64 = 100014 |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 36 | ci64big int64 = 1<<31 |
| 37 | ci64bigger int64 = 1<<32 |
| 38 | chuge = 1<<100 |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 39 | cfgood = 2.0 |
| 40 | cfbad = 2.1 |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 41 | |
| 42 | cnj = -2 |
| 43 | cni int = -3 |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 44 | cni8 int8 = -6 |
| 45 | cni16 int16 = -7 |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 46 | cni32 int32 = -4 |
| 47 | cni64 int64 = -5 |
| 48 | cni64big int64 = -1<<31 |
| 49 | cni64bigger int64 = -1<<32 |
| 50 | cnhuge = -1<<100 |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 51 | cnfgood = -2.0 |
| 52 | cnfbad = -2.1 |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 53 | ) |
| 54 | |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 55 | var j int = 100020 |
| 56 | var i int = 100021 |
| 57 | var i8 int8 = 126 |
| 58 | var i16 int16 = 10025 |
| 59 | var i32 int32 = 100022 |
| 60 | var i64 int64 = 100023 |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 61 | var i64big int64 = 1<<31 |
| 62 | var i64bigger int64 = 1<<32 |
| 63 | var huge uint64 = 1<<64 - 1 |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 64 | var fgood float64 = 2.0 |
| 65 | var fbad float64 = 2.1 |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 66 | |
| 67 | var nj int = -10 |
| 68 | var ni int = -11 |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 69 | var ni8 int8 = -14 |
| 70 | var ni16 int16 = -15 |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 71 | var ni32 int32 = -12 |
| 72 | var ni64 int64 = -13 |
| 73 | var ni64big int64 = -1<<31 |
| 74 | var ni64bigger int64 = -1<<32 |
| 75 | var nhuge int64 = -1<<63 |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 76 | var nfgood float64 = -2.0 |
| 77 | var nfbad float64 = -2.1 |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 78 | |
| 79 | var si []int = make([]int, 10) |
| 80 | var ai [10]int |
| 81 | var pai *[10]int = &ai |
| 82 | |
| 83 | var sq []quad = make([]quad, 10) |
| 84 | var aq [10]quad |
| 85 | var paq *[10]quad = &aq |
| 86 | |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 87 | var sib []int = make([]int, 100000) |
| 88 | var aib [100000]int |
| 89 | var paib *[100000]int = &aib |
| 90 | |
| 91 | var sqb []quad = make([]quad, 100000) |
| 92 | var aqb [100000]quad |
| 93 | var paqb *[100000]quad = &aqb |
| 94 | |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 95 | type T struct { |
| 96 | si []int |
| 97 | ai [10]int |
| 98 | pai *[10]int |
| 99 | sq []quad |
| 100 | aq [10]quad |
| 101 | paq *[10]quad |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 102 | |
| 103 | sib []int |
| 104 | aib [100000]int |
| 105 | paib *[100000]int |
| 106 | sqb []quad |
| 107 | aqb [100000]quad |
| 108 | paqb *[100000]quad |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 111 | var t = T{si, ai, pai, sq, aq, paq, sib, aib, paib, sqb, aqb, paqb} |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 112 | |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 113 | var pt = &T{si, ai, pai, sq, aq, paq, sib, aib, paib, sqb, aqb, paqb} |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 114 | |
| 115 | // test that f panics |
| 116 | func test(f func(), s string) { |
| 117 | defer func() { |
| 118 | if err := recover(); err == nil { |
| 119 | _, file, line, _ := runtime.Caller(2) |
| 120 | bug() |
| 121 | print(file, ":", line, ": ", s, " did not panic\n") |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 122 | } else if !contains(err.(error).Error(), "out of range") { |
| 123 | _, file, line, _ := runtime.Caller(2) |
| 124 | bug() |
| 125 | print(file, ":", line, ": ", s, " unexpected panic: ", err.(error).Error(), "\n") |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 126 | } |
| 127 | }() |
| 128 | f() |
| 129 | } |
| 130 | |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 131 | func contains(x, y string) bool { |
| 132 | for i := 0; i+len(y) <= len(x); i++ { |
| 133 | if x[i:i+len(y)] == y { |
| 134 | return true |
| 135 | } |
| 136 | } |
| 137 | return false |
| 138 | } |
| 139 | |
| 140 | |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 141 | var X interface{} |
| 142 | func use(y interface{}) { |
| 143 | X = y |
| 144 | } |
| 145 | |
| 146 | var didBug = false |
| 147 | |
| 148 | func bug() { |
| 149 | if !didBug { |
| 150 | didBug = true |
| 151 | println("BUG") |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func main() { |
| 156 | ` |
| 157 | |
Ian Lance Taylor | e08008e | 2012-11-07 12:33:54 -0800 | [diff] [blame] | 158 | // pass variable set in index[012].go |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 159 | // 0 - dynamic checks |
| 160 | // 1 - static checks of invalid constants (cannot assign to types) |
| 161 | // 2 - static checks of array bounds |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 162 | |
| 163 | func testExpr(b *bufio.Writer, expr string) { |
Ian Lance Taylor | e08008e | 2012-11-07 12:33:54 -0800 | [diff] [blame] | 164 | if pass == 0 { |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 165 | fmt.Fprintf(b, "\ttest(func(){use(%s)}, %q)\n", expr, expr) |
| 166 | } else { |
Ian Lance Taylor | bdafe73 | 2013-09-27 20:38:52 -0700 | [diff] [blame] | 167 | fmt.Fprintf(b, "\tuse(%s) // ERROR \"index|overflow|truncated|must be integer\"\n", expr) |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | func main() { |
| 172 | b := bufio.NewWriter(os.Stdout) |
| 173 | |
Ian Lance Taylor | e08008e | 2012-11-07 12:33:54 -0800 | [diff] [blame] | 174 | if pass == 0 { |
| 175 | fmt.Fprint(b, "// run\n\n") |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 176 | } else { |
Ian Lance Taylor | e08008e | 2012-11-07 12:33:54 -0800 | [diff] [blame] | 177 | fmt.Fprint(b, "// errorcheck\n\n") |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 178 | } |
| 179 | fmt.Fprint(b, prolog) |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 180 | |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 181 | var choices = [][]string{ |
| 182 | // Direct value, fetch from struct, fetch from struct pointer. |
| 183 | // The last two cases get us to oindex_const_sudo in gsubr.c. |
| 184 | []string{"", "t.", "pt."}, |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 185 | |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 186 | // Array, pointer to array, slice. |
| 187 | []string{"a", "pa", "s"}, |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 188 | |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 189 | // Element is int, element is quad (struct). |
| 190 | // This controls whether we end up in gsubr.c (i) or cgen.c (q). |
| 191 | []string{"i", "q"}, |
| 192 | |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 193 | // Small or big len. |
| 194 | []string{"", "b"}, |
| 195 | |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 196 | // Variable or constant. |
| 197 | []string{"", "c"}, |
| 198 | |
| 199 | // Positive or negative. |
| 200 | []string{"", "n"}, |
| 201 | |
| 202 | // Size of index. |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 203 | []string{"j", "i", "i8", "i16", "i32", "i64", "i64big", "i64bigger", "huge", "fgood", "fbad"}, |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 204 | } |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 205 | |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 206 | forall(choices, func(x []string) { |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 207 | p, a, e, big, c, n, i := x[0], x[1], x[2], x[3], x[4], x[5], x[6] |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 208 | |
| 209 | // Pass: dynamic=0, static=1, 2. |
| 210 | // Which cases should be caught statically? |
| 211 | // Only constants, obviously. |
| 212 | // Beyond that, must be one of these: |
| 213 | // indexing into array or pointer to array |
| 214 | // negative constant |
| 215 | // large constant |
| 216 | thisPass := 0 |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 217 | if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge" || i == "fbad") { |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 218 | if i == "huge" { |
Aaron Jacobs | 8628688 | 2015-06-24 09:50:12 +1000 | [diff] [blame] | 219 | // Due to a detail of gc's internals, |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 220 | // the huge constant errors happen in an |
| 221 | // earlier pass than the others and inhibits |
| 222 | // the next pass from running. |
| 223 | // So run it as a separate check. |
| 224 | thisPass = 1 |
Ian Lance Taylor | 3e50372 | 2012-11-08 15:43:28 -0800 | [diff] [blame] | 225 | } else if a == "s" && n == "" && (i == "i64big" || i == "i64bigger") && unsafe.Sizeof(int(0)) > 4 { |
| 226 | // If int is 64 bits, these huge |
| 227 | // numbers do fit in an int, so they |
| 228 | // are not rejected at compile time. |
Russ Cox | 10ea651 | 2012-09-24 20:57:01 -0400 | [diff] [blame] | 229 | thisPass = 0 |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 230 | } else { |
| 231 | thisPass = 2 |
| 232 | } |
| 233 | } |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 234 | |
| 235 | pae := p + a + e + big |
| 236 | cni := c + n + i |
| 237 | |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 238 | // If we're using the big-len data, positive int8 and int16 cannot overflow. |
| 239 | if big == "b" && n == "" && (i == "i8" || i == "i16") { |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 240 | if pass == 0 { |
| 241 | fmt.Fprintf(b, "\tuse(%s[%s])\n", pae, cni) |
| 242 | fmt.Fprintf(b, "\tuse(%s[0:%s])\n", pae, cni) |
| 243 | fmt.Fprintf(b, "\tuse(%s[1:%s])\n", pae, cni) |
| 244 | fmt.Fprintf(b, "\tuse(%s[%s:])\n", pae, cni) |
| 245 | fmt.Fprintf(b, "\tuse(%s[%s:%s])\n", pae, cni, cni) |
| 246 | } |
| 247 | return |
| 248 | } |
| 249 | |
| 250 | // Float variables cannot be used as indices. |
| 251 | if c == "" && (i == "fgood" || i == "fbad") { |
| 252 | return |
| 253 | } |
| 254 | // Integral float constat is ok. |
| 255 | if c == "c" && n == "" && i == "fgood" { |
| 256 | if pass == 0 { |
| 257 | fmt.Fprintf(b, "\tuse(%s[%s])\n", pae, cni) |
| 258 | fmt.Fprintf(b, "\tuse(%s[0:%s])\n", pae, cni) |
| 259 | fmt.Fprintf(b, "\tuse(%s[1:%s])\n", pae, cni) |
| 260 | fmt.Fprintf(b, "\tuse(%s[%s:])\n", pae, cni) |
| 261 | fmt.Fprintf(b, "\tuse(%s[%s:%s])\n", pae, cni, cni) |
| 262 | } |
Russ Cox | c44768c | 2012-05-24 14:01:39 -0400 | [diff] [blame] | 263 | return |
| 264 | } |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 265 | |
| 266 | // Only print the test case if it is appropriate for this pass. |
Ian Lance Taylor | e08008e | 2012-11-07 12:33:54 -0800 | [diff] [blame] | 267 | if thisPass == pass { |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 268 | // Index operation |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 269 | testExpr(b, pae+"["+cni+"]") |
| 270 | |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 271 | // Slice operation. |
| 272 | // Low index 0 is a special case in ggen.c |
| 273 | // so test both 0 and 1. |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 274 | testExpr(b, pae+"[0:"+cni+"]") |
| 275 | testExpr(b, pae+"[1:"+cni+"]") |
| 276 | testExpr(b, pae+"["+cni+":]") |
| 277 | testExpr(b, pae+"["+cni+":"+cni+"]") |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 278 | } |
| 279 | }) |
| 280 | |
| 281 | fmt.Fprintln(b, "}") |
| 282 | b.Flush() |
| 283 | } |
| 284 | |
| 285 | func forall(choices [][]string, f func([]string)) { |
| 286 | x := make([]string, len(choices)) |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 287 | |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 288 | var recurse func(d int) |
| 289 | recurse = func(d int) { |
| 290 | if d >= len(choices) { |
| 291 | f(x) |
| 292 | return |
| 293 | } |
| 294 | for _, x[d] = range choices[d] { |
Rémy Oudompheng | 88b98ff | 2013-03-22 00:38:23 +0100 | [diff] [blame] | 295 | recurse(d + 1) |
Russ Cox | 9bac9d2 | 2010-08-03 00:26:02 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | recurse(0) |
| 299 | } |