summaryrefslogtreecommitdiff
path: root/crates/buddy_allocator/tests/hosted_test.rs
blob: d13895c7b16b5e27eeaf8486923583149303bc62 (plain)
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
use core::{fmt, num::NonZero, slice};
use nix::sys::mman::{mmap_anonymous, munmap, MapFlags, ProtFlags};
use proptest::prelude::*;
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![4],
        actions: vec![
            Action::Debug,
            Action::Alloc {
                sentinel_value: 0xaa,
                size_class: 0,
            },
            Action::Debug,
        ],
    };
    scenario.run(false)
}

const PAGE_SIZE: usize = (size_of::<*const ()>() * 4).next_power_of_two();
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).unwrap()
                    }
                }
            },
            Action::Dealloc { index } => {
                if allow_errors && allocs.is_empty() {
                    return;
                }

                let index = index % allocs.len();
                let alloc = allocs.remove(index);
                alloc.check_sentinel();
                todo!("{alloc:?}")
            }
            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| unsafe {
                mmap_anonymous(
                    None,
                    NonZero::new(size * PAGE_SIZE).unwrap(),
                    ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
                    MapFlags::MAP_PRIVATE,
                )
                .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, &ptr) in self.range_sizes.iter().zip(backing_mem.iter()) {
            unsafe {
                free_list.add(ptr.cast(), 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).unwrap()
                }
            }
        }

        // Free each of the page ranges.
        for (&size, ptr) in self.range_sizes.iter().zip(backing_mem) {
            unsafe {
                munmap(ptr, size * PAGE_SIZE).expect("failed to free memory");
            }
        }
    }
}

/// 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()
    }
}