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.
7 // Test that the implementation catches nil ptr indirection
8 // in a large address space.
14 // Having a big address space means that indexing
15 // at a 256 MB offset from a nil pointer might not
16 // cause a memory access fault. This test checks
17 // that Go is doing the correct explicit checks to catch
18 // these nil pointer accesses, not just relying on the hardware.
19 var dummy [256 << 20]byte // give us a big address space
22 // the test only tests what we intend to test
23 // if dummy starts in the first 256 MB of memory.
24 // otherwise there might not be anything mapped
25 // at the address that might be accidentally
26 // dereferenced below.
27 if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
28 panic("dummy too far out")
43 func shouldPanic(f func()) {
46 panic("memory reference did not panic")
54 var p *[1 << 30]byte = nil
55 println(p[256<<20]) // very likely to be inside dummy, but should panic
61 var p *[1 << 30]byte = nil
65 println(p[uintptr(unsafe.Pointer(&xb))]) // should panic
70 var p *[1 << 30]byte = nil
71 var x []byte = p[0:] // should panic
81 *y = q[0:] // should crash (uses arraytoslice runtime routine)
90 var p *[1 << 30]byte = nil
91 fb(p[0:]) // should crash
96 var p *[1 << 30]byte = nil
97 var _ []byte = p[10 : len(p)-10] // should crash
113 // Struct field access with large offset.
114 println(f().i) // should crash
118 // Struct field access with large offset.
119 println((*x).i) // should crash
123 // Struct field access with large offset.
125 println(&t.i) // should crash
129 // Struct field access with large offset.
131 println(t.i) // should crash