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.
7 // Generate test of index and slice bounds checks.
8 // The actual tests are index0.go, index1.go, index2.go.
27 type quad struct { x, y, z, w int }
37 ci64bigger int64 = 1<<32
46 cni64big int64 = -1<<31
47 cni64bigger int64 = -1<<32
55 var i32 int32 = 100022
56 var i64 int64 = 100023
57 var i64big int64 = 1<<31
58 var i64bigger int64 = 1<<32
59 var huge uint64 = 1<<64 - 1
67 var ni64big int64 = -1<<31
68 var ni64bigger int64 = -1<<32
69 var nhuge int64 = -1<<63
71 var si []int = make([]int, 10)
73 var pai *[10]int = &ai
75 var sq []quad = make([]quad, 10)
77 var paq *[10]quad = &aq
79 var sib []int = make([]int, 100000)
81 var paib *[100000]int = &aib
83 var sqb []quad = make([]quad, 100000)
85 var paqb *[100000]quad = &aqb
103 var t = T{si, ai, pai, sq, aq, paq, sib, aib, paib, sqb, aqb, paqb}
105 var pt = &T{si, ai, pai, sq, aq, paq, sib, aib, paib, sqb, aqb, paqb}
107 // test that f panics
108 func test(f func(), s string) {
110 if err := recover(); err == nil {
111 _, file, line, _ := runtime.Caller(2)
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)
117 print(file, ":", line, ": ", s, " unexpected panic: ", err.(error).Error(), "\n")
123 func contains(x, y string) bool {
124 for i := 0; i+len(y) <= len(x); i++ {
125 if x[i:i+len(y)] == y {
134 func use(y interface{}) {
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
155 func testExpr(b *bufio.Writer, expr string) {
157 fmt.Fprintf(b, "\ttest(func(){use(%s)}, %q)\n", expr, expr)
159 fmt.Fprintf(b, "\tuse(%s) // ERROR \"index|overflow\"\n", expr)
164 b := bufio.NewWriter(os.Stdout)
167 fmt.Fprint(b, "// run\n\n")
169 fmt.Fprint(b, "// errorcheck\n\n")
171 fmt.Fprint(b, prolog)
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."},
178 // Array, pointer to array, slice.
179 []string{"a", "pa", "s"},
181 // Element is int, element is quad (struct).
182 // This controls whether we end up in gsubr.c (i) or cgen.c (q).
188 // Variable or constant.
191 // Positive or negative.
195 []string{"j", "i", "i8", "i16", "i32", "i64", "i64big", "i64bigger", "huge"},
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]
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
209 if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || 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.
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.
227 // If we're using the big-len data, positive int8 and int16 cannot overflow.
228 if big == "b" && n == "" && (i == "i8" || i == "i16") {
232 // Only print the test case if it is appropriate for this pass.
233 if thisPass == pass {
238 testExpr(b, pae + "[" + cni + "]")
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 + "]")
254 func forall(choices [][]string, f func([]string)) {
255 x := make([]string, len(choices))
257 var recurse func(d int)
258 recurse = func(d int) {
259 if d >= len(choices) {
263 for _, x[d] = range choices[d] {