Skip to content

Commit 5f3ba04

Browse files
committed
Revert leaking, track POINTER instead of REFERENCE for leaking
1 parent 3a5e38f commit 5f3ba04

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
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/test.rs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ use std::{
1010

1111
#[repr(align(128))]
1212
struct Chonk<const N: usize> {
13-
data: [MaybeUninit<u8>; N],
13+
data: MaybeUninit<[u8; N]>,
1414
}
1515

1616
impl<const N: usize> Chonk<N> {
1717
pub fn new() -> Self {
1818
Self {
19-
data: [MaybeUninit::uninit(); N],
19+
data: MaybeUninit::uninit(),
2020
}
2121
}
2222
}
@@ -42,45 +42,56 @@ impl<F> DerefMut for OwnedHeap<F> {
4242

4343
pub fn new_heap() -> OwnedHeap<impl Sized> {
4444
const HEAP_SIZE: usize = 1000;
45-
let mut heap_space = Box::new(Chonk::<HEAP_SIZE>::new());
46-
let data = &mut Box::leak(heap_space).data;
47-
let assumed_location = data.as_mut_ptr().cast();
45+
let heap_space_ptr: *mut Chonk<HEAP_SIZE> = {
46+
let owned_box = Box::new(Chonk::<HEAP_SIZE>::new());
47+
let mutref = Box::leak(owned_box);
48+
mutref
49+
};
50+
let data_ptr: *mut u8 = unsafe { core::ptr::addr_of_mut!((*heap_space_ptr).data).cast() };
4851

49-
let heap = unsafe { Heap::new(data.as_mut_ptr().cast(), data.len()) };
50-
assert_eq!(heap.bottom(), assumed_location);
52+
let heap = unsafe { Heap::new(data_ptr, HEAP_SIZE) };
53+
assert_eq!(heap.bottom(), data_ptr);
5154
assert_eq!(heap.size(), align_down_size(HEAP_SIZE, size_of::<usize>()));
5255
let drop = move || {
53-
// let _ = heap_space;
56+
let _ = unsafe { Box::from_raw(heap_space_ptr) };
5457
};
5558
OwnedHeap { heap, _drop: drop }
5659
}
5760

5861
fn new_max_heap() -> OwnedHeap<impl Sized> {
5962
const HEAP_SIZE: usize = 1024;
6063
const HEAP_SIZE_MAX: usize = 2048;
61-
let mut heap_space = Box::new(Chonk::<HEAP_SIZE_MAX>::new());
62-
let data = &mut Box::leak(heap_space).data;
63-
let start_ptr = data.as_mut_ptr().cast();
64+
let heap_space_ptr: *mut Chonk<HEAP_SIZE_MAX> = {
65+
let owned_box = Box::new(Chonk::<HEAP_SIZE_MAX>::new());
66+
let mutref = Box::leak(owned_box);
67+
mutref
68+
};
69+
let data_ptr: *mut u8 = unsafe { core::ptr::addr_of_mut!((*heap_space_ptr).data).cast() };
6470

6571
// Unsafe so that we have provenance over the whole allocation.
66-
let heap = unsafe { Heap::new(start_ptr, HEAP_SIZE) };
67-
assert_eq!(heap.bottom(), start_ptr);
72+
let heap = unsafe { Heap::new(data_ptr, HEAP_SIZE) };
73+
assert_eq!(heap.bottom(), data_ptr);
6874
assert_eq!(heap.size(), HEAP_SIZE);
6975

7076
let drop = move || {
71-
// let _ = heap_space;
77+
let _ = unsafe { Box::from_raw(heap_space_ptr) };
7278
};
7379
OwnedHeap { heap, _drop: drop }
7480
}
7581

7682
fn new_heap_skip(ct: usize) -> OwnedHeap<impl Sized> {
7783
const HEAP_SIZE: usize = 1000;
78-
let mut heap_space = Box::new(Chonk::<HEAP_SIZE>::new());
79-
let data = &mut Box::leak(heap_space).data[ct..];
80-
let heap = unsafe { Heap::new(data.as_mut_ptr().cast(), data.len()) };
84+
let heap_space_ptr: *mut Chonk<HEAP_SIZE> = {
85+
let owned_box = Box::new(Chonk::<HEAP_SIZE>::new());
86+
let mutref = Box::leak(owned_box);
87+
mutref
88+
};
89+
let data_ptr: *mut u8 = unsafe { core::ptr::addr_of_mut!((*heap_space_ptr).data).cast() };
90+
91+
let heap = unsafe { Heap::new(data_ptr.add(ct), HEAP_SIZE - ct) };
8192

8293
let drop = move || {
83-
// let _ = heap_space;
94+
let _ = unsafe { Box::from_raw(heap_space_ptr) };
8495
};
8596
OwnedHeap { heap, _drop: drop }
8697
}
@@ -95,17 +106,23 @@ fn empty() {
95106
#[test]
96107
fn oom() {
97108
const HEAP_SIZE: usize = 1000;
98-
let mut heap_space = Box::new(Chonk::<HEAP_SIZE>::new());
99-
let data = &mut heap_space.data;
100-
let assumed_location = data.as_mut_ptr().cast();
109+
let heap_space_ptr: *mut Chonk<HEAP_SIZE> = {
110+
let owned_box = Box::new(Chonk::<HEAP_SIZE>::new());
111+
let mutref = Box::leak(owned_box);
112+
mutref
113+
};
114+
let data_ptr: *mut u8 = unsafe { core::ptr::addr_of_mut!((*heap_space_ptr).data).cast() };
101115

102-
let mut heap = unsafe { Heap::new(data.as_mut_ptr().cast(), data.len()) };
103-
assert_eq!(heap.bottom(), assumed_location);
116+
let mut heap = unsafe { Heap::new(data_ptr, HEAP_SIZE) };
117+
assert_eq!(heap.bottom(), data_ptr);
104118
assert_eq!(heap.size(), align_down_size(HEAP_SIZE, size_of::<usize>()));
105119

106120
let layout = Layout::from_size_align(heap.size() + 1, align_of::<usize>());
107121
let addr = heap.allocate_first_fit(layout.unwrap());
108122
assert!(addr.is_err());
123+
124+
// Explicitly unleak the heap allocation
125+
let _ = unsafe { Box::from_raw(heap_space_ptr) };
109126
}
110127

111128
#[test]

0 commit comments

Comments
 (0)