forked from tailscale/wf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
malloc_test.go
127 lines (103 loc) · 2.94 KB
/
malloc_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (C) 2020 Huntress Labs, Inc.
//
// This file is part of Huntress. Unauthorized copying of this file, via any medium is
// strictly prohibited without the express written consent of Huntress Labs, Inc.
package wf
import (
"testing"
"unsafe"
)
func TestMemoryAllocation(t *testing.T) {
var a arena
defer a.Dispose()
// This should have an alignment of 8-bytes
// ref: https://go.dev/ref/spec#Size_and_alignment_guarantees
var ip *int64
// allocate from 8 to 15 bytes. Each allocation should end up on the boundary size
for i := 0; i < byteBoundary; i++ {
p := a.Alloc(unsafe.Sizeof(ip) + uintptr(i))
if uintptr(p)%byteBoundary != 0 {
t.Fatalf("allocation not on %v-byte boundary", byteBoundary)
}
ip = (*int64)(p)
// verify we can write to the pointer
*ip = 0
}
}
func TestMemoryAllocationSpanSlabsWithRounding(t *testing.T) {
var a arena
defer a.Dispose()
// allocate almost a full slab size
// This will likely round to the end of the slab
size := slabSize - (byteBoundary / 2)
p := a.Alloc(uintptr(size))
ip := (*int64)(p)
*ip = 0
// This should force us over the current slab of memory.
p = a.Alloc(byteBoundary)
ip = (*int64)(p)
*ip = 0
}
func TestMemoryAllocationSpanSlabsWithRoundingSmaller(t *testing.T) {
var a arena
defer a.Dispose()
// allocate almost a full slab size
// This will likely round to the end of the slab
size := slabSize - (byteBoundary / 2)
p := a.Alloc(uintptr(size))
ip := (*int64)(p)
*ip = 0
// allocate a small amount
p = a.Alloc(uintptr(1))
if p == nil {
t.Fatal("invalid address returned")
}
// This should force us over the current slab of memory.
p = a.Alloc(byteBoundary)
ip = (*int64)(p)
*ip = 0
}
func TestMemoryAllocationSpanSlabsWithRoundingSmallerWithSlab(t *testing.T) {
var a arena
defer a.Dispose()
// allocate almost a full slab size
// This will likely round to the end of the slab
size := slabSize - (byteBoundary / 2)
p := a.Alloc(uintptr(size))
ip := (*int64)(p)
*ip = 0
// allocate a small amount
p = a.Alloc(uintptr(1))
if p == nil {
t.Fatal("invalid address returned")
}
// This should force us over the current slab of memory.
p = a.Alloc(byteBoundary)
ip = (*int64)(p)
*ip = 0
// Try one more allocation with a slab size
p = a.Alloc(slabSize)
ip = (*int64)(p)
*ip = 0
// allocate one more small amount
p = a.Alloc(uintptr(1))
if p == nil {
t.Fatal("invalid address returned")
}
}
func TestMemoryAllocationSpanSlabWithoutRounding(t *testing.T) {
var a arena
defer a.Dispose()
// allocate almost a full slab size, but back off an amount that is
// bigger than our boundary size so that we know that
// we would not have rounded to the end of the slab
size := slabSize - (byteBoundary * 2)
p := a.Alloc(uintptr(size))
ip := (*int64)(p)
*ip = 0
// Allocate more than we backed off of so we know that our allocation
// will go over amount we have left in our slab
p = a.Alloc(uintptr(byteBoundary * 3))
ip = (*int64)(p)
*ip = 0
}