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.
11 // +build !darwin !arm64
12 // Address space starts at 1<<32 on AIX and on darwin/arm64, so dummy is too far.
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
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")
53 func shouldPanic(f func()) {
56 panic("memory reference did not panic")
64 var p *[1 << 30]byte = nil
65 println(p[256<<20]) // very likely to be inside dummy, but should panic
71 var p *[1 << 30]byte = nil
75 println(p[uintptr(unsafe.Pointer(&xb))]) // should panic
80 var p *[1 << 30]byte = nil
81 var x []byte = p[0:] // should panic
91 *y = q[0:] // should crash (uses arraytoslice runtime routine)
100 var p *[1 << 30]byte = nil
101 fb(p[0:]) // should crash
106 var p *[1 << 30]byte = nil
107 var _ []byte = p[10 : len(p)-10] // should crash
123 // Struct field access with large offset.
124 println(f().i) // should crash
128 // Struct field access with large offset.
129 println((*x).i) // should crash
133 // Struct field access with large offset.
135 println(&t.i) // should crash
139 // Struct field access with large offset.
141 println(t.i) // should crash
158 // ADDR(DOT(IND(p))) needs a check also
161 println(*(&((*p).i)))
164 // Tests suggested in golang.org/issue/6080.
173 println((*[1]int)(nil)[:])
177 for i := range (*[1]int)(nil)[:] {
183 for i, v := range (*[1]int)(nil)[:] {