Skip to content

Commit 13b055d

Browse files
committed
Rollup merge of rust-lang#58429 - RalfJung:box, r=TimNN
fix Box::into_unique effecitvely transmuting to a raw ptr Miri/Stacked Borrows treat `Box` specially: they assert that it is unique, and tag it appropriately. However, currently, `Box::into_inner` is not aware of that and returns a raw pointer (wrapped in a `Unique`) that carries the same tag as the box, meaning it carries a `Uniq` tag. This leads to all sorts of problems when people use the raw pointer they get out of the `Unique` type. In the future, it'd be interesting to make `Unique` also carry some kind of uniqueness. In that case, something like this would instead be needed whenever a raw pointer is extracted from a `Unique`. However, that is out-of-scope for the current version of Stacked Borrows. So until then, this changes `into_unique` to perform a proper reference-to-raw-ptr-cast, which clears the tag.
2 parents 092d191 + 719be24 commit 13b055d

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/liballoc/boxed.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,15 @@ impl<T: ?Sized> Box<T> {
202202
#[unstable(feature = "ptr_internals", issue = "0", reason = "use into_raw_non_null instead")]
203203
#[inline]
204204
#[doc(hidden)]
205-
pub fn into_unique(b: Box<T>) -> Unique<T> {
206-
let unique = b.0;
205+
pub fn into_unique(mut b: Box<T>) -> Unique<T> {
206+
// Box is kind-of a library type, but recognized as a "unique pointer" by
207+
// Stacked Borrows. This function here corresponds to "reborrowing to
208+
// a raw pointer", but there is no actual reborrow here -- so
209+
// without some care, the pointer we are returning here still carries
210+
// the `Uniq` tag. We round-trip through a mutable reference to avoid that.
211+
let unique = unsafe { b.0.as_mut() as *mut T };
207212
mem::forget(b);
208-
unique
213+
unsafe { Unique::new_unchecked(unique) }
209214
}
210215

211216
/// Consumes and leaks the `Box`, returning a mutable reference,

0 commit comments

Comments
 (0)