3 // Copyright 2018 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.
16 // Having a big address space means that indexing
17 // at a 1G + 256 MB offset from a nil pointer might not
18 // cause a memory access fault. This test checks
19 // that Go is doing the correct explicit checks to catch
20 // these nil pointer accesses, not just relying on the hardware.
21 // The reason of the 1G offset is because AIX addresses start after 1G.
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)) < 1<<32 {
31 panic("dummy not far enough")
52 func shouldPanic(f func()) {
55 panic("memory reference did not panic")
63 var p *[1 << 33]byte = nil
64 println(p[1<<32+256<<20]) // very likely to be inside dummy, but should panic
70 var p *[1 << 33]byte = nil
74 println(p[uintptr(unsafe.Pointer(&xb))]) // should panic
79 var p *[1 << 33]byte = nil
80 var x []byte = p[0:] // should panic
90 *y = q[0:] // should crash (uses arraytoslice runtime routine)
99 var p *[1 << 33]byte = nil
100 fb(p[0:]) // should crash
105 var p *[1 << 33]byte = nil
106 var _ []byte = p[10 : len(p)-10] // should crash
110 x [1<<32 + 256<<20]byte
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)[:] {