Skip to content

Commit 764fff4

Browse files
committed
add_retag: ensure box-to-raw-ptr casts are preserved for Miri
1 parent 0009e85 commit 764fff4

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

alloc/src/boxed.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ use core::error::Error;
155155
use core::fmt;
156156
use core::future::Future;
157157
use core::hash::{Hash, Hasher};
158-
use core::intrinsics::retag_box_to_raw;
159158
use core::iter::FusedIterator;
160159
use core::marker::Tuple;
161160
use core::marker::Unsize;
@@ -165,7 +164,7 @@ use core::ops::{
165164
CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut, DispatchFromDyn, Receiver,
166165
};
167166
use core::pin::Pin;
168-
use core::ptr::{self, NonNull, Unique};
167+
use core::ptr::{self, addr_of_mut, NonNull, Unique};
169168
use core::task::{Context, Poll};
170169

171170
#[cfg(not(no_global_oom_handling))]
@@ -1111,16 +1110,12 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11111110
#[unstable(feature = "allocator_api", issue = "32838")]
11121111
#[inline]
11131112
pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
1114-
// This is the transition point from `Box` to raw pointers. For Stacked Borrows, these casts
1115-
// are relevant -- if this is a global allocator Box and we just get the pointer from `b.0`,
1116-
// it will have `Unique` permission, which is not what we want from a raw pointer. We could
1117-
// fix that by going through `&mut`, but then if this is *not* a global allocator Box, we'd
1118-
// be adding uniqueness assertions that we do not want. So for Miri's sake we pass this
1119-
// pointer through an intrinsic for box-to-raw casts, which can do the right thing wrt the
1120-
// aliasing model.
1121-
let b = mem::ManuallyDrop::new(b);
1113+
let mut b = mem::ManuallyDrop::new(b);
1114+
// We carefully get the raw pointer out in a way that Miri's aliasing model understands what
1115+
// is happening: using the primitive "deref" of `Box`.
1116+
let ptr = addr_of_mut!(**b);
11221117
let alloc = unsafe { ptr::read(&b.1) };
1123-
(unsafe { retag_box_to_raw::<T, A>(b.0.as_ptr()) }, alloc)
1118+
(ptr, alloc)
11241119
}
11251120

11261121
#[unstable(

0 commit comments

Comments
 (0)