From 5c7575e31534cd90469cbe6c71ba523ef82f080b Mon Sep 17 00:00:00 2001 From: Nikolai Vazquez Date: Tue, 21 Jan 2025 10:28:52 +0100 Subject: [PATCH] Make Miri aware of leaks of `Box`/`Vec`/`String` --- library/alloc/src/boxed.rs | 14 +++++++++++++- library/alloc/src/vec/mod.rs | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 1b5e44a913467..a2b5b81d9ba3c 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -1661,7 +1661,19 @@ impl Box { where A: 'a, { - unsafe { &mut *Box::into_raw(b) } + unsafe { + let ptr = Box::into_raw(b); + + #[cfg(miri)] + { + extern "Rust" { + fn miri_static_root(ptr: *const u8); + } + miri_static_root(ptr.cast()); + } + + &mut *ptr + } } /// Converts a `Box` into a `Pin>`. If `T` does not implement [`Unpin`], then diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index cd2afd7a47319..4728ca05beeb7 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2842,7 +2842,20 @@ impl Vec { A: 'a, { let mut me = ManuallyDrop::new(self); - unsafe { slice::from_raw_parts_mut(me.as_mut_ptr(), me.len) } + + unsafe { + let ptr = me.as_mut_ptr(); + + #[cfg(miri)] + { + extern "Rust" { + fn miri_static_root(ptr: *const u8); + } + miri_static_root(ptr.cast()); + } + + slice::from_raw_parts_mut(ptr, me.len) + } } /// Returns the remaining spare capacity of the vector as a slice of