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