5 // Copyright 2016 The Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file.
16 // Use global variables so the compiler
17 // doesn't know that they are constants.
18 var p = syscall.Getpagesize()
23 // Allocate 2 pages of memory.
24 b, err := syscall.Mmap(-1, 0, 2*p, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
28 // Mark the second page as faulting.
29 err = syscall.Mprotect(b[p:], syscall.PROT_NONE)
33 // Get a slice pointing to the last byte of the good page.
44 func test16(x []byte) uint16 {
48 panic("no fault or bounds check failure happened")
50 s := fmt.Sprintf("%s", r)
51 if s != "runtime error: index out of range" {
52 panic("bad panic: " + s)
55 // Try to read 2 bytes from x.
56 return uint16(x[0]) | uint16(x[1])<<8
58 // We expect to get an "index out of range" error from x[1].
59 // If we promote the first load to a 2-byte load, it will segfault, which we don't want.
62 func test16i(x []byte, i int) uint16 {
66 panic("no fault or bounds check failure happened")
68 s := fmt.Sprintf("%s", r)
69 if s != "runtime error: index out of range" {
70 panic("bad panic: " + s)
73 return uint16(x[i]) | uint16(x[i+1])<<8
76 func test32(x []byte) uint32 {
80 panic("no fault or bounds check failure happened")
82 s := fmt.Sprintf("%s", r)
83 if s != "runtime error: index out of range" {
84 panic("bad panic: " + s)
87 return uint32(x[0]) | uint32(x[1])<<8 | uint32(x[2])<<16 | uint32(x[3])<<24
90 func test32i(x []byte, i int) uint32 {
94 panic("no fault or bounds check failure happened")
96 s := fmt.Sprintf("%s", r)
97 if s != "runtime error: index out of range" {
98 panic("bad panic: " + s)
101 return uint32(x[i]) | uint32(x[i+1])<<8 | uint32(x[i+2])<<16 | uint32(x[i+3])<<24
104 func test64(x []byte) uint64 {
108 panic("no fault or bounds check failure happened")
110 s := fmt.Sprintf("%s", r)
111 if s != "runtime error: index out of range" {
112 panic("bad panic: " + s)
115 return uint64(x[0]) | uint64(x[1])<<8 | uint64(x[2])<<16 | uint64(x[3])<<24 |
116 uint64(x[4])<<32 | uint64(x[5])<<40 | uint64(x[6])<<48 | uint64(x[7])<<56
119 func test64i(x []byte, i int) uint64 {
123 panic("no fault or bounds check failure happened")
125 s := fmt.Sprintf("%s", r)
126 if s != "runtime error: index out of range" {
127 panic("bad panic: " + s)
130 return uint64(x[i+0]) | uint64(x[i+1])<<8 | uint64(x[i+2])<<16 | uint64(x[i+3])<<24 |
131 uint64(x[i+4])<<32 | uint64(x[i+5])<<40 | uint64(x[i+6])<<48 | uint64(x[i+7])<<56