Skip to content

Commit 5c7575e

Browse files
committed
Make Miri aware of leaks of Box/Vec/String
1 parent b605c65 commit 5c7575e

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

library/alloc/src/boxed.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,19 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
16611661
where
16621662
A: 'a,
16631663
{
1664-
unsafe { &mut *Box::into_raw(b) }
1664+
unsafe {
1665+
let ptr = Box::into_raw(b);
1666+
1667+
#[cfg(miri)]
1668+
{
1669+
extern "Rust" {
1670+
fn miri_static_root(ptr: *const u8);
1671+
}
1672+
miri_static_root(ptr.cast());
1673+
}
1674+
1675+
&mut *ptr
1676+
}
16651677
}
16661678

16671679
/// Converts a `Box<T>` into a `Pin<Box<T>>`. If `T` does not implement [`Unpin`], then

library/alloc/src/vec/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2842,7 +2842,20 @@ impl<T, A: Allocator> Vec<T, A> {
28422842
A: 'a,
28432843
{
28442844
let mut me = ManuallyDrop::new(self);
2845-
unsafe { slice::from_raw_parts_mut(me.as_mut_ptr(), me.len) }
2845+
2846+
unsafe {
2847+
let ptr = me.as_mut_ptr();
2848+
2849+
#[cfg(miri)]
2850+
{
2851+
extern "Rust" {
2852+
fn miri_static_root(ptr: *const u8);
2853+
}
2854+
miri_static_root(ptr.cast());
2855+
}
2856+
2857+
slice::from_raw_parts_mut(ptr, me.len)
2858+
}
28462859
}
28472860

28482861
/// Returns the remaining spare capacity of the vector as a slice of

0 commit comments

Comments
 (0)