Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1770,19 +1770,6 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
unsafe { (NonNull::new_unchecked(ptr), alloc) }
}

#[unstable(
feature = "ptr_internals",
issue = "none",
reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead"
)]
#[inline]
#[doc(hidden)]
pub fn into_unique(b: Self) -> (Unique<T>, A) {
let (ptr, alloc) = Box::into_raw_with_allocator(b);
// SAFETY: Pointer is valid and unique.
unsafe { (Unique::new_unchecked(ptr), alloc) }
}

/// Returns a raw mutable pointer to the `Box`'s contents.
///
/// The caller must ensure that the `Box` outlives the pointer this
Expand Down
42 changes: 41 additions & 1 deletion src/tools/miri/tests/pass/box-custom-alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//@[tree]compile-flags: -Zmiri-tree-borrows
#![feature(allocator_api)]

use std::alloc::{AllocError, Allocator, Layout};
use std::alloc::{AllocError, Allocator, Global, Layout};
use std::cell::Cell;
use std::mem::MaybeUninit;
use std::ptr::{self, NonNull};
Expand Down Expand Up @@ -118,8 +118,48 @@ fn test3() {
foo(A, b1);
}

fn test_into_raw() {
Comment thread
maxdexh marked this conversation as resolved.
Resolved
struct MyMetadataAlloc;

fn widen(layout: Layout) -> Layout {
Layout::from_size_align(layout.size() + 10, layout.align())
.unwrap_or_else(|_| std::process::abort())
}
unsafe impl Allocator for MyMetadataAlloc {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
let ptr = Global.allocate(widen(layout))?;

// store some important metadata
unsafe { ptr.cast::<u8>().add(layout.size() + 5).write(42) }

Ok(NonNull::slice_from_raw_parts(ptr.cast(), layout.size()))
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
// SAFETY: we get back a pointer with the same provenance as
// returned by `allocate`, valid over the full `size() + 10` bytes.
unsafe {
assert_eq!(ptr.add(layout.size() + 5).replace(1), 42);
Global.deallocate(ptr, widen(layout))
}
}
}

unsafe {
let b = Box::new_in(1i32, MyMetadataAlloc);

let (r, alloc) = Box::into_raw_with_allocator(b);

// ptr is valid
assert_eq!(std::mem::replace(&mut *r, 2), 1);

// ptr can be deallocated again
drop(Box::from_raw_in(r, alloc));
}
}

fn main() {
test1();
test2();
test3();
test_into_raw();
}
16 changes: 0 additions & 16 deletions src/tools/miri/tests/pass/box.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//@compile-flags: -Zmiri-permissive-provenance
#![feature(ptr_internals)]

fn main() {
into_raw();
into_unique();
boxed_pair_to_vec();
}

Expand All @@ -22,20 +20,6 @@ fn into_raw() {
}
}

fn into_unique() {
unsafe {
let b = Box::new(4i32);
let u = Box::into_unique(b).0;

// "lose the tag"
let r = ((u.as_ptr() as usize) + 0) as *mut i32;
*(&mut *r) = 7;

// Use original ptr again.
drop(Box::from_raw(u.as_ptr()));
}
}

fn boxed_pair_to_vec() {
#[repr(C)]
#[derive(Debug)]
Expand Down
Loading