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