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.
10 // Address space starts at 1<<32 on AIX and on darwin/arm64 and on windows/arm64, so dummy is too far.
11 //go:build !aix && (!darwin || !arm64) && (!windows || !arm64)
17 // Having a big address space means that indexing
18 // at a 256 MB offset from a nil pointer might not
19 // cause a memory access fault. This test checks
20 // that Go is doing the correct explicit checks to catch
21 // these nil pointer accesses, not just relying on the hardware.
22 var dummy [256 << 20]byte // give us a big address space
25 // the test only tests what we intend to test
26 // if dummy starts in the first 256 MB of memory.
27 // otherwise there might not be anything mapped
28 // at the address that might be accidentally
29 // dereferenced below.
30 if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
31 panic("dummy too far out")
52 func shouldPanic(f func()) {
55 panic("memory reference did not panic")
63 var p *[1 << 30]byte = nil
64 println(p[256<<20]) // very likely to be inside dummy, but should panic
70 var p *[1 << 30]byte = nil
74 println(p[uintptr(unsafe.Pointer(&xb))]) // should panic
79 var p *[1 << 30]byte = nil
80 var x []byte = p[0:] // should panic
90 *y = q[0:] // should crash (uses arraytoslice runtime routine)
99 var p *[1 << 30]byte = nil
100 fb(p[0:]) // should crash
105 var p *[1 << 30]byte = nil
106 var _ []byte = p[10 : len(p)-10] // should crash
122 // Struct field access with large offset.
123 println(f().i) // should crash
127 // Struct field access with large offset.
128 println((*x).i) // should crash
132 // Struct field access with large offset.
134 println(&t.i) // should crash
138 // Struct field access with large offset.
140 println(t.i) // should crash
157 // ADDR(DOT(IND(p))) needs a check also
160 println(*(&((*p).i)))
163 // Tests suggested in golang.org/issue/6080.
172 println((*[1]int)(nil)[:])
176 for i := range (*[1]int)(nil)[:] {
182 for i, v := range (*[1]int)(nil)[:] {