]> Cypherpunks.ru repositories - gostls13.git/blob - test/nilptr.go
[dev.fuzz] all: merge master into dev.fuzz
[gostls13.git] / test / nilptr.go
1 // run
2
3 // Copyright 2011 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 // Test that the implementation catches nil ptr indirection
8 // in a large address space.
9
10 // +build !aix
11 // +build !darwin !arm64
12 // Address space starts at 1<<32 on AIX and on darwin/arm64, so dummy is too far.
13
14 package main
15
16 import "unsafe"
17
18 // Having a big address space means that indexing
19 // at a 256 MB offset from a nil pointer might not
20 // cause a memory access fault. This test checks
21 // that Go is doing the correct explicit checks to catch
22 // these nil pointer accesses, not just relying on the hardware.
23 var dummy [256 << 20]byte // give us a big address space
24
25 func main() {
26         // the test only tests what we intend to test
27         // if dummy starts in the first 256 MB of memory.
28         // otherwise there might not be anything mapped
29         // at the address that might be accidentally
30         // dereferenced below.
31         if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
32                 panic("dummy too far out")
33         }
34
35         shouldPanic(p1)
36         shouldPanic(p2)
37         shouldPanic(p3)
38         shouldPanic(p4)
39         shouldPanic(p5)
40         shouldPanic(p6)
41         shouldPanic(p7)
42         shouldPanic(p8)
43         shouldPanic(p9)
44         shouldPanic(p10)
45         shouldPanic(p11)
46         shouldPanic(p12)
47         shouldPanic(p13)
48         shouldPanic(p14)
49         shouldPanic(p15)
50         shouldPanic(p16)
51 }
52
53 func shouldPanic(f func()) {
54         defer func() {
55                 if recover() == nil {
56                         panic("memory reference did not panic")
57                 }
58         }()
59         f()
60 }
61
62 func p1() {
63         // Array index.
64         var p *[1 << 30]byte = nil
65         println(p[256<<20]) // very likely to be inside dummy, but should panic
66 }
67
68 var xb byte
69
70 func p2() {
71         var p *[1 << 30]byte = nil
72         xb = 123
73
74         // Array index.
75         println(p[uintptr(unsafe.Pointer(&xb))]) // should panic
76 }
77
78 func p3() {
79         // Array to slice.
80         var p *[1 << 30]byte = nil
81         var x []byte = p[0:] // should panic
82         _ = x
83 }
84
85 var q *[1 << 30]byte
86
87 func p4() {
88         // Array to slice.
89         var x []byte
90         var y = &x
91         *y = q[0:] // should crash (uses arraytoslice runtime routine)
92 }
93
94 func fb([]byte) {
95         panic("unreachable")
96 }
97
98 func p5() {
99         // Array to slice.
100         var p *[1 << 30]byte = nil
101         fb(p[0:]) // should crash
102 }
103
104 func p6() {
105         // Array to slice.
106         var p *[1 << 30]byte = nil
107         var _ []byte = p[10 : len(p)-10] // should crash
108 }
109
110 type T struct {
111         x [256 << 20]byte
112         i int
113 }
114
115 func f() *T {
116         return nil
117 }
118
119 var y *T
120 var x = &y
121
122 func p7() {
123         // Struct field access with large offset.
124         println(f().i) // should crash
125 }
126
127 func p8() {
128         // Struct field access with large offset.
129         println((*x).i) // should crash
130 }
131
132 func p9() {
133         // Struct field access with large offset.
134         var t *T
135         println(&t.i) // should crash
136 }
137
138 func p10() {
139         // Struct field access with large offset.
140         var t *T
141         println(t.i) // should crash
142 }
143
144 type T1 struct {
145         T
146 }
147
148 type T2 struct {
149         *T1
150 }
151
152 func p11() {
153         t := &T2{}
154         p := &t.i
155         println(*p)
156 }
157
158 // ADDR(DOT(IND(p))) needs a check also
159 func p12() {
160         var p *T = nil
161         println(*(&((*p).i)))
162 }
163
164 // Tests suggested in golang.org/issue/6080.
165
166 func p13() {
167         var x *[10]int
168         y := x[:]
169         _ = y
170 }
171
172 func p14() {
173         println((*[1]int)(nil)[:])
174 }
175
176 func p15() {
177         for i := range (*[1]int)(nil)[:] {
178                 _ = i
179         }
180 }
181
182 func p16() {
183         for i, v := range (*[1]int)(nil)[:] {
184                 _ = i + v
185         }
186 }