Skip to content

Commit c24a6b9

Browse files
committed
Ensure that memory gets freed again in tests
1 parent 7cca404 commit c24a6b9

File tree

3 files changed

+59
-44
lines changed

3 files changed

+59
-44
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ jobs:
135135
name: "Miri tests"
136136
runs-on: ubuntu-latest
137137
env:
138-
MIRIFLAGS: "-Zmiri-disable-isolation -Zmiri-strict-provenance -Zmiri-tag-raw-pointers -Zmiri-ignore-leaks"
138+
MIRIFLAGS: "-Zmiri-disable-isolation -Zmiri-strict-provenance -Zmiri-tag-raw-pointers"
139139
steps:
140140
- uses: actions/checkout@v1
141141
- run: rustup toolchain install nightly --profile minimal --component rust-src miri

src/hole.rs

+2-27
Original file line numberDiff line numberDiff line change
@@ -697,34 +697,9 @@ fn deallocate(list: &mut HoleList, addr: *mut u8, size: usize) {
697697
#[cfg(test)]
698698
pub mod test {
699699
use super::HoleList;
700-
use crate::{align_down_size, Heap};
700+
use crate::{align_down_size, test::new_heap};
701701
use core::mem::size_of;
702-
use std::{alloc::Layout, convert::TryInto, mem::MaybeUninit, prelude::v1::*, ptr::NonNull};
703-
704-
#[repr(align(128))]
705-
struct Chonk<const N: usize> {
706-
data: [MaybeUninit<u8>; N],
707-
}
708-
709-
impl<const N: usize> Chonk<N> {
710-
pub fn new() -> Self {
711-
Self {
712-
data: [MaybeUninit::uninit(); N],
713-
}
714-
}
715-
}
716-
717-
fn new_heap() -> Heap {
718-
const HEAP_SIZE: usize = 1000;
719-
let heap_space = Box::leak(Box::new(Chonk::<HEAP_SIZE>::new()));
720-
let data = &mut heap_space.data;
721-
let assumed_location = data.as_mut_ptr().cast();
722-
723-
let heap = Heap::from_slice(data);
724-
assert_eq!(heap.bottom(), assumed_location);
725-
assert_eq!(heap.size(), align_down_size(HEAP_SIZE, size_of::<usize>()));
726-
heap
727-
}
702+
use std::{alloc::Layout, convert::TryInto, prelude::v1::*, ptr::NonNull};
728703

729704
#[test]
730705
fn cursor() {

src/test.rs

+56-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::*;
22
use core::alloc::Layout;
3+
use core::ops::DerefMut;
34
use std::mem::{align_of, size_of, MaybeUninit};
45
use std::prelude::v1::*;
56

@@ -16,30 +17,69 @@ impl<const N: usize> Chonk<N> {
1617
}
1718
}
1819

19-
fn new_heap() -> Heap {
20+
pub struct OwnedHeap<F> {
21+
heap: Heap,
22+
_drop: F,
23+
}
24+
25+
impl<F> Deref for OwnedHeap<F> {
26+
type Target = Heap;
27+
28+
fn deref(&self) -> &Self::Target {
29+
&self.heap
30+
}
31+
}
32+
33+
impl<F> DerefMut for OwnedHeap<F> {
34+
fn deref_mut(&mut self) -> &mut Self::Target {
35+
&mut self.heap
36+
}
37+
}
38+
39+
pub fn new_heap() -> OwnedHeap<impl Sized> {
2040
const HEAP_SIZE: usize = 1000;
21-
let heap_space = Box::leak(Box::new(Chonk::<HEAP_SIZE>::new()));
41+
let mut heap_space = Box::new(Chonk::<HEAP_SIZE>::new());
2242
let data = &mut heap_space.data;
2343
let assumed_location = data.as_mut_ptr().cast();
2444

25-
let heap = Heap::from_slice(data);
45+
let heap = unsafe { Heap::new(data.as_mut_ptr().cast(), data.len()) };
2646
assert_eq!(heap.bottom(), assumed_location);
2747
assert_eq!(heap.size(), align_down_size(HEAP_SIZE, size_of::<usize>()));
28-
heap
48+
49+
let drop = move || {
50+
let _ = heap_space;
51+
};
52+
OwnedHeap { heap, _drop: drop }
2953
}
3054

31-
fn new_max_heap() -> Heap {
55+
fn new_max_heap() -> OwnedHeap<impl Sized> {
3256
const HEAP_SIZE: usize = 1024;
3357
const HEAP_SIZE_MAX: usize = 2048;
34-
let heap_space = Box::leak(Box::new(Chonk::<HEAP_SIZE_MAX>::new()));
58+
let mut heap_space = Box::new(Chonk::<HEAP_SIZE_MAX>::new());
3559
let data = &mut heap_space.data;
3660
let start_ptr = data.as_mut_ptr().cast();
3761

3862
// Unsafe so that we have provenance over the whole allocation.
3963
let heap = unsafe { Heap::new(start_ptr, HEAP_SIZE) };
4064
assert_eq!(heap.bottom(), start_ptr);
4165
assert_eq!(heap.size(), HEAP_SIZE);
42-
heap
66+
67+
let drop = move || {
68+
let _ = heap_space;
69+
};
70+
OwnedHeap { heap, _drop: drop }
71+
}
72+
73+
fn new_heap_skip(ct: usize) -> OwnedHeap<impl Sized> {
74+
const HEAP_SIZE: usize = 1000;
75+
let mut heap_space = Box::new(Chonk::<HEAP_SIZE>::new());
76+
let data = &mut heap_space.data[ct..];
77+
let heap = unsafe { Heap::new(data.as_mut_ptr().cast(), data.len()) };
78+
79+
let drop = move || {
80+
let _ = heap_space;
81+
};
82+
OwnedHeap { heap, _drop: drop }
4383
}
4484

4585
#[test]
@@ -51,7 +91,15 @@ fn empty() {
5191

5292
#[test]
5393
fn oom() {
54-
let mut heap = new_heap();
94+
const HEAP_SIZE: usize = 1000;
95+
let mut heap_space = Box::new(Chonk::<HEAP_SIZE>::new());
96+
let data = &mut heap_space.data;
97+
let assumed_location = data.as_mut_ptr().cast();
98+
99+
let mut heap = unsafe { Heap::new(data.as_mut_ptr().cast(), data.len()) };
100+
assert_eq!(heap.bottom(), assumed_location);
101+
assert_eq!(heap.size(), align_down_size(HEAP_SIZE, size_of::<usize>()));
102+
55103
let layout = Layout::from_size_align(heap.size() + 1, align_of::<usize>());
56104
let addr = heap.allocate_first_fit(layout.unwrap());
57105
assert!(addr.is_err());
@@ -388,14 +436,6 @@ fn allocate_multiple_unaligned() {
388436
}
389437
}
390438

391-
fn new_heap_skip(ct: usize) -> Heap {
392-
const HEAP_SIZE: usize = 1000;
393-
let heap_space = Box::leak(Box::new(Chonk::<HEAP_SIZE>::new()));
394-
let data = &mut heap_space.data[ct..];
395-
let heap = Heap::from_slice(data);
396-
heap
397-
}
398-
399439
#[test]
400440
fn allocate_usize() {
401441
let mut heap = new_heap();

0 commit comments

Comments
 (0)