-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoverread_test.go
76 lines (63 loc) · 1.8 KB
/
overread_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//go:build unix
// Copyright (c) 2024 Robert Clausecker <[email protected]>
package pospop
import (
"golang.org/x/sys/unix"
"testing"
)
// Allocate three pages of memory. Make the first and last page
// inaccessible. Return the full array as well as just the part
// in the middle (which is accessible).
func mapGuarded() (mapping []byte, slice []byte, err error) {
pagesize := unix.Getpagesize()
mapping, err = unix.Mmap(-1, 0, 3*pagesize, unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
if err != nil {
return nil, nil, err
}
slice = mapping[pagesize : 2*pagesize : 2*pagesize]
err = unix.Mprotect(slice, unix.PROT_READ|unix.PROT_WRITE)
if err != nil {
unix.Munmap(mapping)
return nil, nil, err
}
return
}
// Verify that our count functions only overread memory in benign ways,
// i.e. such that we never cross a page size boundary.
func TestOverread(t *testing.T) {
for i := range count8funcs {
t.Run(count8funcs[i].name, func(tt *testing.T) {
if !count8funcs[i].available {
tt.SkipNow()
}
testOverread(tt, count8funcs[i].count8)
})
}
}
func testOverread(t *testing.T, count8 func(*[8]int, []uint8)) {
var counters [8]int
mapping, slice, err := mapGuarded()
defer unix.Munmap(mapping)
if err != nil {
t.Log("Cannot allocate memory:", err)
t.SkipNow()
}
// test large slices that start/end right at the page boundary
for i := 0; i < 64; i++ {
for j := len(slice) - 64; j <= len(slice); j++ {
count8(&counters, slice[i:j])
}
}
// test small slices that start right after the page boundary
for i := 0; i < 64; i++ {
for j := i; j <= 64; j++ {
count8(&counters, slice[i:j])
}
}
// test small slices that end right before the page boundary
for i := len(slice) - 64; i <= len(slice); i++ {
for j := i; j <= len(slice); j++ {
count8(&counters, slice[i:j])
}
}
}