blob: 38aa33dd305927ae8d658db75a0295cec827c824 [file] [log] [blame]
Russ Cox9bac9d22010-08-03 00:26:02 -07001// $G $D/$F.go && $L $F.$A &&
Russ Cox33c4ff02010-09-19 23:28:32 -04002// ./$A.out -pass 0 >tmp.go && $G tmp.go && $L -o $A.out1 tmp.$A && ./$A.out1 &&
Russ Cox9bac9d22010-08-03 00:26:02 -07003// ./$A.out -pass 1 >tmp.go && errchk $G -e tmp.go &&
4// ./$A.out -pass 2 >tmp.go && errchk $G -e tmp.go
Russ Cox33c4ff02010-09-19 23:28:32 -04005// rm -f tmp.go $A.out1
Russ Cox9bac9d22010-08-03 00:26:02 -07006
7// Copyright 2010 The Go Authors. All rights reserved.
8// Use of this source code is governed by a BSD-style
9// license that can be found in the LICENSE file.
10
11// Generate test of index and slice bounds checks.
12
13package main
14
15import (
16 "bufio"
17 "flag"
18 "fmt"
19 "os"
20)
21
22const prolog = `
23
24package main
25
26import (
27 "runtime"
28)
29
30type quad struct { x, y, z, w int }
31
32const (
33 cj = 11
34 ci int = 12
35 ci32 int32 = 13
36 ci64 int64 = 14
37 ci64big int64 = 1<<31
38 ci64bigger int64 = 1<<32
39 chuge = 1<<100
40
41 cnj = -2
42 cni int = -3
43 cni32 int32 = -4
44 cni64 int64 = -5
45 cni64big int64 = -1<<31
46 cni64bigger int64 = -1<<32
47 cnhuge = -1<<100
48)
49
50var j int = 20
51var i int = 21
52var i32 int32 = 22
53var i64 int64 = 23
54var i64big int64 = 1<<31
55var i64bigger int64 = 1<<32
56var huge uint64 = 1<<64 - 1
57
58var nj int = -10
59var ni int = -11
60var ni32 int32 = -12
61var ni64 int64 = -13
62var ni64big int64 = -1<<31
63var ni64bigger int64 = -1<<32
64var nhuge int64 = -1<<63
65
66var si []int = make([]int, 10)
67var ai [10]int
68var pai *[10]int = &ai
69
70var sq []quad = make([]quad, 10)
71var aq [10]quad
72var paq *[10]quad = &aq
73
74type T struct {
75 si []int
76 ai [10]int
77 pai *[10]int
78 sq []quad
79 aq [10]quad
80 paq *[10]quad
81}
82
83var t = T{si, ai, pai, sq, aq, paq}
84
85var pt = &T{si, ai, pai, sq, aq, paq}
86
87// test that f panics
88func test(f func(), s string) {
89 defer func() {
90 if err := recover(); err == nil {
91 _, file, line, _ := runtime.Caller(2)
92 bug()
93 print(file, ":", line, ": ", s, " did not panic\n")
94 }
95 }()
96 f()
97}
98
99var X interface{}
100func use(y interface{}) {
101 X = y
102}
103
104var didBug = false
105
106func bug() {
107 if !didBug {
108 didBug = true
109 println("BUG")
110 }
111}
112
113func main() {
114`
115
116// Passes:
117// 0 - dynamic checks
118// 1 - static checks of invalid constants (cannot assign to types)
119// 2 - static checks of array bounds
120var pass = flag.Int("pass", 0, "which test (0,1,2)")
121
122func testExpr(b *bufio.Writer, expr string) {
123 if *pass == 0 {
124 fmt.Fprintf(b, "\ttest(func(){use(%s)}, %q)\n", expr, expr)
125 } else {
126 fmt.Fprintf(b, "\tuse(%s) // ERROR \"index|overflow\"\n", expr)
127 }
128}
129
130func main() {
131 b := bufio.NewWriter(os.Stdout)
132
133 flag.Parse()
134
135 if *pass == 0 {
136 fmt.Fprint(b, "// $G $D/$F.go && $L $F.$A && ./$A.out\n\n")
137 } else {
138 fmt.Fprint(b, "// errchk $G -e $D/$F.go\n\n")
139 }
140 fmt.Fprint(b, prolog)
141
142 var choices = [][]string{
143 // Direct value, fetch from struct, fetch from struct pointer.
144 // The last two cases get us to oindex_const_sudo in gsubr.c.
145 []string{"", "t.", "pt."},
146
147 // Array, pointer to array, slice.
148 []string{"a", "pa", "s"},
149
150 // Element is int, element is quad (struct).
151 // This controls whether we end up in gsubr.c (i) or cgen.c (q).
152 []string{"i", "q"},
153
154 // Variable or constant.
155 []string{"", "c"},
156
157 // Positive or negative.
158 []string{"", "n"},
159
160 // Size of index.
161 []string{"j", "i", "i32", "i64", "i64big", "i64bigger", "huge"},
162 }
163
164 forall(choices, func(x []string) {
165 p, a, e, c, n, i := x[0], x[1], x[2], x[3], x[4], x[5]
166
167 // Pass: dynamic=0, static=1, 2.
168 // Which cases should be caught statically?
169 // Only constants, obviously.
170 // Beyond that, must be one of these:
171 // indexing into array or pointer to array
172 // negative constant
173 // large constant
174 thisPass := 0
175 if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge") {
176 if i == "huge" {
177 // Due to a detail of 6g's internals,
178 // the huge constant errors happen in an
179 // earlier pass than the others and inhibits
180 // the next pass from running.
181 // So run it as a separate check.
182 thisPass = 1
183 } else {
184 thisPass = 2
185 }
186 }
187
188 // Only print the test case if it is appropriate for this pass.
189 if thisPass == *pass {
190 pae := p+a+e
191 cni := c+n+i
192
193 // Index operation
194 testExpr(b, pae + "[" + cni + "]")
195
196 // Slice operation.
197 // Low index 0 is a special case in ggen.c
198 // so test both 0 and 1.
199 testExpr(b, pae + "[0:" + cni + "]")
200 testExpr(b, pae + "[1:" + cni + "]")
201 testExpr(b, pae + "[" + cni + ":]")
202 testExpr(b, pae + "[" + cni + ":" + cni + "]")
203 }
204 })
205
206 fmt.Fprintln(b, "}")
207 b.Flush()
208}
209
210func forall(choices [][]string, f func([]string)) {
211 x := make([]string, len(choices))
212
213 var recurse func(d int)
214 recurse = func(d int) {
215 if d >= len(choices) {
216 f(x)
217 return
218 }
219 for _, x[d] = range choices[d] {
220 recurse(d+1)
221 }
222 }
223 recurse(0)
224}