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 // +build !windows !arm64
13 // Address space starts at 1<<32 on AIX and on darwin/arm64 and on windows/arm64, so dummy is too far.
19 // Having a big address space means that indexing
20 // at a 256 MB offset from a nil pointer might not
21 // cause a memory access fault. This test checks
22 // that Go is doing the correct explicit checks to catch
23 // these nil pointer accesses, not just relying on the hardware.
24 var dummy [256 << 20]byte // give us a big address space
27 // the test only tests what we intend to test
28 // if dummy starts in the first 256 MB of memory.
29 // otherwise there might not be anything mapped
30 // at the address that might be accidentally
31 // dereferenced below.
32 if uintptr(unsafe.Pointer(&dummy)) > 256<<20 {
33 panic("dummy too far out")
54 func shouldPanic(f func()) {
57 panic("memory reference did not panic")
65 var p *[1 << 30]byte = nil
66 println(p[256<<20]) // very likely to be inside dummy, but should panic
72 var p *[1 << 30]byte = nil
76 println(p[uintptr(unsafe.Pointer(&xb))]) // should panic
81 var p *[1 << 30]byte = nil
82 var x []byte = p[0:] // should panic
92 *y = q[0:] // should crash (uses arraytoslice runtime routine)
101 var p *[1 << 30]byte = nil
102 fb(p[0:]) // should crash
107 var p *[1 << 30]byte = nil
108 var _ []byte = p[10 : len(p)-10] // should crash
124 // Struct field access with large offset.
125 println(f().i) // should crash
129 // Struct field access with large offset.
130 println((*x).i) // should crash
134 // Struct field access with large offset.
136 println(&t.i) // should crash
140 // Struct field access with large offset.
142 println(t.i) // should crash
159 // ADDR(DOT(IND(p))) needs a check also
162 println(*(&((*p).i)))
165 // Tests suggested in golang.org/issue/6080.
174 println((*[1]int)(nil)[:])
178 for i := range (*[1]int)(nil)[:] {
184 for i, v := range (*[1]int)(nil)[:] {