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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
use core::{fmt, num::NonZero, slice};
use nix::sys::mman::{mmap_anonymous, munmap, MapFlags, ProtFlags};
use proptest::prelude::*;
use std::ptr::NonNull;
use vernos_buddy_allocator::BuddyAllocator;
use vernos_physmem_free_list::FreeListAllocator;
proptest! {
#![proptest_config(proptest::test_runner::Config {
failure_persistence: None,
..Default::default()
})]
#[test]
fn test_scenario(scenario in Scenario::any()) {
scenario.run(true)
}
}
#[test]
fn test_simple_scenario() {
let scenario = Scenario {
range_sizes: vec![5],
actions: vec![
Action::Debug,
Action::Alloc {
sentinel_value: 0xaa,
size_class: 0,
},
Action::Debug,
],
};
scenario.run(false)
}
const PAGE_SIZE: usize = 64;
const PAGE_SIZE_BITS: usize = PAGE_SIZE.trailing_zeros() as usize;
const SIZE_CLASS_COUNT: usize = 4;
/// A single action the property test should perform.
#[derive(Debug)]
enum Action {
/// Makes an allocation.
Alloc {
/// A sentinel value, which is expected to be present when the allocation is freed.
sentinel_value: u8,
/// The size class, which is constrained to be less than `SIZE_CLASS_COUNT`.
size_class: usize,
},
/// Deallocates an allocation.
Dealloc {
/// The index of the allocation in the set of live allocations, which is taken to be a
/// circular list (so this is always in-bounds if there are any allocations).
index: usize,
},
/// Prints the allocator and all live allocations, for debugging purposes. This is never
/// automatically generated.
Debug,
/// Overwrites an allocation, changing its sentinel.
Overwrite {
/// The index of the allocation in the set of live allocations, which is taken to be a
/// circular list (so this is always in-bounds if there are any allocations).
index: usize,
/// The new sentinel value.
sentinel_value: u8,
},
}
impl Action {
/// Generates a random action.
pub fn any() -> impl Strategy<Value = Action> {
prop_oneof![
(any::<u8>(), 0..SIZE_CLASS_COUNT).prop_map(|(sentinel_value, size_class)| {
Action::Alloc {
sentinel_value,
size_class,
}
}),
any::<usize>().prop_map(|index| { Action::Dealloc { index } }),
(any::<usize>(), any::<u8>()).prop_map(|(index, sentinel_value)| {
Action::Overwrite {
index,
sentinel_value,
}
})
]
}
/// Runs an action.
pub fn run(
&self,
buddy: &mut BuddyAllocator<PAGE_SIZE, PAGE_SIZE_BITS, SIZE_CLASS_COUNT>,
allocs: &mut Vec<Alloc>,
allow_errors: bool,
) {
match *self {
Action::Alloc {
sentinel_value,
size_class,
} => match buddy.alloc(size_class) {
Ok(ptr) => unsafe {
let slice = slice::from_raw_parts_mut(ptr.as_ptr(), PAGE_SIZE << size_class);
slice.fill(sentinel_value);
allocs.push(Alloc {
slice,
sentinel_value,
});
},
Err(err) => {
if !allow_errors {
Err(err).expect("failed to perform alloc action")
}
}
},
Action::Dealloc { index } => {
if allow_errors && allocs.is_empty() {
return;
}
let index = index % allocs.len();
let alloc = allocs.remove(index);
alloc.check_sentinel();
unsafe {
buddy.dealloc(
NonNull::from(alloc.slice.as_mut()).cast(),
alloc.slice.len().trailing_zeros() as usize - PAGE_SIZE_BITS,
);
}
}
Action::Debug => {
dbg!(buddy);
dbg!(allocs);
}
Action::Overwrite {
index,
sentinel_value,
} => {
if allow_errors && allocs.is_empty() {
return;
}
let index = index % allocs.len();
let alloc = &mut allocs[index];
alloc.slice.fill(sentinel_value);
alloc.sentinel_value = sentinel_value;
}
}
}
}
/// The entire series of actions to be performed.
#[derive(Debug)]
struct Scenario {
/// The sizes of the ranges the buddy allocator should be initialized with.
range_sizes: Vec<usize>,
/// Actions to perform after initializing the buddy allocator.
actions: Vec<Action>,
}
impl Scenario {
/// Generates a random scenario.
pub fn any() -> impl Strategy<Value = Scenario> {
(
prop::collection::vec(1usize..1 << (SIZE_CLASS_COUNT + 2), 0..4),
prop::collection::vec(Action::any(), 0..64),
)
.prop_map(|(range_sizes, actions)| Scenario {
range_sizes,
actions,
})
}
/// Runs the scenario.
pub fn run(&self, allow_errors: bool) {
// Allocate each of the page ranges.
let backing_mem = self
.range_sizes
.iter()
.map(|&size| {
Mmap::new(NonZero::new(size * PAGE_SIZE).unwrap())
.expect("failed to allocate memory")
})
.collect::<Vec<_>>();
// Create the free list allocator and move the pages there.
let mut free_list: FreeListAllocator<PAGE_SIZE> = FreeListAllocator::new();
for (&size, mmap) in self.range_sizes.iter().zip(backing_mem.iter()) {
unsafe {
free_list.add(mmap.ptr(), size);
}
}
// Create the buddy allocator and move the pages from the free list to there.
match BuddyAllocator::<PAGE_SIZE, PAGE_SIZE_BITS, SIZE_CLASS_COUNT>::new(free_list) {
Ok(mut buddy) => {
let mut allocs = Vec::new();
// Run each of the actions.
for action in &self.actions {
action.run(&mut buddy, &mut allocs, allow_errors);
// Check each allocation.
for alloc in &allocs {
alloc.check_sentinel();
}
}
}
Err(err) => {
if !allow_errors {
Err(err).expect("failed to make buddy allocator")
}
}
}
}
}
/// Information about an allocation we've made from the buddy allocator.
struct Alloc<'alloc> {
slice: &'alloc mut [u8],
sentinel_value: u8,
}
impl<'alloc> Alloc<'alloc> {
pub fn check_sentinel(&self) {
let s = self.sentinel_value;
for (i, &b) in self.slice.iter().enumerate() {
assert_eq!(b, s, "at index {i}, {b} != {s}",);
}
}
}
impl<'alloc> fmt::Debug for Alloc<'alloc> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Alloc")
.field("ptr", &self.slice.as_ptr())
.field("len", &self.slice.len())
.field("sentinel_value", &self.sentinel_value)
.finish()
}
}
/// A mmap-allocated region of memory.
struct Mmap {
ptr: NonNull<u8>,
len: NonZero<usize>,
}
impl Mmap {
pub fn new(len: NonZero<usize>) -> Result<Mmap, nix::Error> {
let ptr = unsafe {
mmap_anonymous(
None,
len,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_PRIVATE,
)?
.cast()
};
Ok(Mmap { ptr, len })
}
pub fn ptr<T>(&self) -> NonNull<T> {
self.ptr.cast()
}
}
impl Drop for Mmap {
fn drop(&mut self) {
unsafe {
munmap(self.ptr(), self.len.get()).expect("failed to free memory");
}
}
}
|