Skip to content

Commit 3f01453

Browse files
committed
Use miri to test for leaks on init panic
1 parent faedd30 commit 3f01453

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

crates/slice-dst/tests/leak.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
use {
22
slice_dst::*,
33
std::{
4-
panic,
5-
sync::atomic::{AtomicUsize, Ordering::SeqCst},
4+
alloc::Layout,
5+
panic, ptr,
6+
rc::Rc,
7+
sync::{
8+
atomic::{AtomicUsize, Ordering::SeqCst},
9+
Arc,
10+
},
611
},
712
};
813

@@ -66,3 +71,33 @@ fn bad_exactsizeiterator() {
6671
});
6772
assert_eq!(*counter.get_mut(), 0);
6873
}
74+
75+
struct S(u8);
76+
77+
unsafe impl SliceDst for S {
78+
fn layout_for(_: usize) -> Layout {
79+
Layout::new::<S>()
80+
}
81+
82+
fn retype(ptr: ptr::NonNull<[()]>) -> ptr::NonNull<Self> {
83+
ptr.cast()
84+
}
85+
}
86+
87+
#[test]
88+
#[cfg_attr(
89+
all(miri, target_os = "windows"),
90+
ignore = "miri does not support panicking on windows rust-lang/miri#1059"
91+
)]
92+
fn panic_in_init() {
93+
// This relies on miri to catch leaks
94+
let _ = std::panic::catch_unwind(|| {
95+
let _: Box<S> = unsafe { AllocSliceDst::new_slice_dst(0, |_| panic!()) };
96+
});
97+
let _ = std::panic::catch_unwind(|| {
98+
let _: Arc<S> = unsafe { AllocSliceDst::new_slice_dst(0, |_| panic!()) };
99+
});
100+
let _ = std::panic::catch_unwind(|| {
101+
let _: Rc<S> = unsafe { AllocSliceDst::new_slice_dst(0, |_| panic!()) };
102+
});
103+
}

0 commit comments

Comments
 (0)