1 // $G $D/$F.go && $L $F.$A &&
2 // ./$A.out -pass 0 >tmp.go && $G tmp.go && $L -o $A.out1 tmp.$A && ./$A.out1 &&
3 // ./$A.out -pass 1 >tmp.go && errchk $G -e tmp.go &&
4 // ./$A.out -pass 2 >tmp.go && errchk $G -e tmp.go
5 // rm -f tmp.go $A.out1
7 // NOTE: This test is not run by 'run.go' and so not run by all.bash.
8 // To run this test you must use the ./run shell script.
10 // Copyright 2010 The Go Authors. All rights reserved.
11 // Use of this source code is governed by a BSD-style
12 // license that can be found in the LICENSE file.
14 // Generate test of index and slice bounds checks.
15 // The output is compiled and run.
35 type quad struct { x, y, z, w int }
45 ci64bigger int64 = 1<<32
54 cni64big int64 = -1<<31
55 cni64bigger int64 = -1<<32
63 var i32 int32 = 100022
64 var i64 int64 = 100023
65 var i64big int64 = 1<<31
66 var i64bigger int64 = 1<<32
67 var huge uint64 = 1<<64 - 1
75 var ni64big int64 = -1<<31
76 var ni64bigger int64 = -1<<32
77 var nhuge int64 = -1<<63
79 var si []int = make([]int, 10)
81 var pai *[10]int = &ai
83 var sq []quad = make([]quad, 10)
85 var paq *[10]quad = &aq
87 var sib []int = make([]int, 100000)
89 var paib *[100000]int = &aib
91 var sqb []quad = make([]quad, 100000)
93 var paqb *[100000]quad = &aqb
111 var t = T{si, ai, pai, sq, aq, paq, sib, aib, paib, sqb, aqb, paqb}
113 var pt = &T{si, ai, pai, sq, aq, paq, sib, aib, paib, sqb, aqb, paqb}
115 // test that f panics
116 func test(f func(), s string) {
118 if err := recover(); err == nil {
119 _, file, line, _ := runtime.Caller(2)
121 print(file, ":", line, ": ", s, " did not panic\n")
122 } else if !contains(err.(error).Error(), "out of range") {
123 _, file, line, _ := runtime.Caller(2)
125 print(file, ":", line, ": ", s, " unexpected panic: ", err.(error).Error(), "\n")
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 {
142 func use(y interface{}) {
159 // 0 - dynamic checks
160 // 1 - static checks of invalid constants (cannot assign to types)
161 // 2 - static checks of array bounds
162 var pass = flag.Int("pass", 0, "which test (0,1,2)")
164 func testExpr(b *bufio.Writer, expr string) {
166 fmt.Fprintf(b, "\ttest(func(){use(%s)}, %q)\n", expr, expr)
168 fmt.Fprintf(b, "\tuse(%s) // ERROR \"index|overflow\"\n", expr)
173 b := bufio.NewWriter(os.Stdout)
178 fmt.Fprint(b, "// $G $D/$F.go && $L $F.$A && ./$A.out\n\n")
180 fmt.Fprint(b, "// errchk $G -e $D/$F.go\n\n")
182 fmt.Fprint(b, prolog)
184 var choices = [][]string{
185 // Direct value, fetch from struct, fetch from struct pointer.
186 // The last two cases get us to oindex_const_sudo in gsubr.c.
187 []string{"", "t.", "pt."},
189 // Array, pointer to array, slice.
190 []string{"a", "pa", "s"},
192 // Element is int, element is quad (struct).
193 // This controls whether we end up in gsubr.c (i) or cgen.c (q).
199 // Variable or constant.
202 // Positive or negative.
206 []string{"j", "i", "i8", "i16", "i32", "i64", "i64big", "i64bigger", "huge"},
209 forall(choices, func(x []string) {
210 p, a, e, big, c, n, i := x[0], x[1], x[2], x[3], x[4], x[5], x[6]
212 // Pass: dynamic=0, static=1, 2.
213 // Which cases should be caught statically?
214 // Only constants, obviously.
215 // Beyond that, must be one of these:
216 // indexing into array or pointer to array
220 if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge") {
222 // Due to a detail of 6g's internals,
223 // the huge constant errors happen in an
224 // earlier pass than the others and inhibits
225 // the next pass from running.
226 // So run it as a separate check.
228 } else if a == "s" && n == "" && (i == "i64big" || i == "i64bigger") && runtime.GOARCH == "amd64" {
229 // On amd64, these huge numbers do fit in an int, so they are not
230 // rejected at compile time.
237 // If we're using the big-len data, positive int8 and int16 cannot overflow.
238 if big == "b" && n == "" && (i == "i8" || i == "i16") {
242 // Only print the test case if it is appropriate for this pass.
243 if thisPass == *pass {
248 testExpr(b, pae + "[" + cni + "]")
251 // Low index 0 is a special case in ggen.c
252 // so test both 0 and 1.
253 testExpr(b, pae + "[0:" + cni + "]")
254 testExpr(b, pae + "[1:" + cni + "]")
255 testExpr(b, pae + "[" + cni + ":]")
256 testExpr(b, pae + "[" + cni + ":" + cni + "]")
264 func forall(choices [][]string, f func([]string)) {
265 x := make([]string, len(choices))
267 var recurse func(d int)
268 recurse = func(d int) {
269 if d >= len(choices) {
273 for _, x[d] = range choices[d] {