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")
49 func shouldPanic(f func()) {
52 panic("memory reference did not panic")
60 var p *[1 << 30]byte = nil
61 println(p[256<<20]) // very likely to be inside dummy, but should panic
67 var p *[1 << 30]byte = nil
71 println(p[uintptr(unsafe.Pointer(&xb))]) // should panic
76 var p *[1 << 30]byte = nil
77 var x []byte = p[0:] // should panic
87 *y = q[0:] // should crash (uses arraytoslice runtime routine)
96 var p *[1 << 30]byte = nil
97 fb(p[0:]) // should crash
102 var p *[1 << 30]byte = nil
103 var _ []byte = p[10 : len(p)-10] // should crash
119 // Struct field access with large offset.
120 println(f().i) // should crash
124 // Struct field access with large offset.
125 println((*x).i) // should crash
129 // Struct field access with large offset.
131 println(&t.i) // should crash
135 // Struct field access with large offset.
137 println(t.i) // should crash
154 // ADDR(DOT(IND(p))) needs a check also
157 println(*(&((*p).i)))
160 // Tests suggested in golang.org/issue/6080.
169 println((*[1]int)(nil)[:])
173 for i := range (*[1]int)(nil)[:] {
179 for i, v := range (*[1]int)(nil)[:] {