1 // $G $D/$F.go && $L $F.$A && ./$A.out
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.
11 // Having a big address space means that indexing
12 // at a 256 MB offset from a nil pointer might not
13 // cause a memory access fault. This test checks
14 // that Go is doing the correct explicit checks to catch
15 // these nil pointer accesses, not just relying on the hardware.
16 var dummy [512 << 20]byte // give us a big address space
19 // the test only tests what we intend to test
20 // if dummy starts in the first 256 MB of memory.
21 // otherwise there might not be anything mapped
22 // at the address that might be accidentally
23 // dereferenced below.
24 if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
25 panic("dummy too far out")
40 func shouldPanic(f func()) {
43 panic("memory reference did not panic")
51 var p *[1 << 30]byte = nil
52 println(p[256<<20]) // very likely to be inside dummy, but should panic
58 var p *[1 << 30]byte = nil
62 println(p[uintptr(unsafe.Pointer(&xb))]) // should panic
67 var p *[1 << 30]byte = nil
68 var x []byte = p[0:] // should panic
78 *y = q[0:] // should crash (uses arraytoslice runtime routine)
87 var p *[1 << 30]byte = nil
88 fb(p[0:]) // should crash
93 var p *[1 << 30]byte = nil
94 var _ []byte = p[10 : len(p)-10] // should crash
110 // Struct field access with large offset.
111 println(f().i) // should crash
115 // Struct field access with large offset.
116 println((*x).i) // should crash
120 // Struct field access with large offset.
122 println(&t.i) // should crash
126 // Struct field access with large offset.
128 println(t.i) // should crash