diff options
| author | Ralf Jung <post@ralfj.de> | 2024-03-17 17:02:38 +0100 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2024-03-18 10:32:25 +0100 |
| commit | c96fa5e14345ca334073b32465e0ff3ef4b92ac5 (patch) | |
| tree | 17813962594b840b415e067392b057f6ee1ca405 /library/alloc | |
| parent | a42873e85bcdcc83c3161309e0408f2147778523 (diff) | |
| download | rust-c96fa5e14345ca334073b32465e0ff3ef4b92ac5.tar.gz rust-c96fa5e14345ca334073b32465e0ff3ef4b92ac5.zip | |
add_retag: ensure box-to-raw-ptr casts are preserved for Miri
Diffstat (limited to 'library/alloc')
| -rw-r--r-- | library/alloc/src/boxed.rs | 17 |
1 files changed, 6 insertions, 11 deletions
diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 304f607000b..f47fe560b07 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -155,7 +155,6 @@ use core::error::Error; use core::fmt; use core::future::Future; use core::hash::{Hash, Hasher}; -use core::intrinsics::retag_box_to_raw; use core::iter::FusedIterator; use core::marker::Tuple; use core::marker::Unsize; @@ -165,7 +164,7 @@ use core::ops::{ CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut, DispatchFromDyn, Receiver, }; use core::pin::Pin; -use core::ptr::{self, NonNull, Unique}; +use core::ptr::{self, addr_of_mut, NonNull, Unique}; use core::task::{Context, Poll}; #[cfg(not(no_global_oom_handling))] @@ -1111,16 +1110,12 @@ impl<T: ?Sized, A: Allocator> Box<T, A> { #[unstable(feature = "allocator_api", issue = "32838")] #[inline] pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) { - // This is the transition point from `Box` to raw pointers. For Stacked Borrows, these casts - // are relevant -- if this is a global allocator Box and we just get the pointer from `b.0`, - // it will have `Unique` permission, which is not what we want from a raw pointer. We could - // fix that by going through `&mut`, but then if this is *not* a global allocator Box, we'd - // be adding uniqueness assertions that we do not want. So for Miri's sake we pass this - // pointer through an intrinsic for box-to-raw casts, which can do the right thing wrt the - // aliasing model. - let b = mem::ManuallyDrop::new(b); + let mut b = mem::ManuallyDrop::new(b); + // We carefully get the raw pointer out in a way that Miri's aliasing model understands what + // is happening: using the primitive "deref" of `Box`. + let ptr = addr_of_mut!(**b); let alloc = unsafe { ptr::read(&b.1) }; - (unsafe { retag_box_to_raw::<T, A>(b.0.as_ptr()) }, alloc) + (ptr, alloc) } #[unstable( |
