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
2 changes: 1 addition & 1 deletion library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1780,7 +1780,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
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::from(&mut *ptr), alloc) }
unsafe { (Unique::new_unchecked(ptr), alloc) }
}

/// Returns a raw mutable pointer to the `Box`'s contents.
Expand Down
14 changes: 6 additions & 8 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,16 +802,14 @@ impl<T, A: Allocator> Rc<T, A> {
{
// Construct the inner in the "uninitialized" state with a single
// weak reference.
let (uninit_raw_ptr, alloc) = Box::into_raw_with_allocator(Box::new_in(
let (uninit_ptr, alloc) = Box::into_non_null_with_allocator(Box::new_in(
RcInner {
strong: Cell::new(0),
weak: Cell::new(1),
value: mem::MaybeUninit::<T>::uninit(),
},
alloc,
));
// ignore-tidy-undocumented-unsafe
let uninit_ptr: NonNull<_> = (unsafe { &mut *uninit_raw_ptr }).into();
let init_ptr: NonNull<RcInner<T>> = uninit_ptr.cast();

let weak = Weak { ptr: init_ptr, alloc };
Expand Down Expand Up @@ -863,12 +861,12 @@ impl<T, A: Allocator> Rc<T, A> {
// pointers, which ensures that the weak destructor never frees
// the allocation while the strong destructor is running, even
// if the weak pointer is stored inside the strong one.
let (ptr, alloc) = Box::into_unique(Box::try_new_in(
let (ptr, alloc) = Box::into_non_null_with_allocator(Box::try_new_in(
RcInner { strong: Cell::new(1), weak: Cell::new(1), value },
alloc,
)?);
// ignore-tidy-undocumented-unsafe
Ok(unsafe { Self::from_inner_in(ptr.into(), alloc) })
// SAFETY: Pointer is valid.
Ok(unsafe { Self::from_inner_in(ptr, alloc) })
}

/// Constructs a new `Rc` with uninitialized contents, in the provided allocator, returning an
Expand Down Expand Up @@ -4340,7 +4338,7 @@ impl<T, A: Allocator> UniqueRc<T, A> {
#[must_use]
// #[unstable(feature = "allocator_api", issue = "32838")]
pub fn new_in(value: T, alloc: A) -> Self {
let (ptr, alloc) = Box::into_unique(Box::new_in(
let (ptr, alloc) = Box::into_non_null_with_allocator(Box::new_in(
RcInner {
strong: Cell::new(0),
// keep one weak reference so if all the weak pointers that are created are dropped
Expand All @@ -4350,7 +4348,7 @@ impl<T, A: Allocator> UniqueRc<T, A> {
},
alloc,
));
Self { ptr: ptr.into(), _marker: PhantomData, _marker2: PhantomData, alloc }
Self { ptr, _marker: PhantomData, _marker2: PhantomData, alloc }
}

#[cfg(not(no_global_oom_handling))]
Expand Down
16 changes: 7 additions & 9 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,9 @@ impl<T, A: Allocator> Arc<T, A> {
},
alloc,
);
let (ptr, alloc) = Box::into_unique(x);
let (ptr, alloc) = Box::into_non_null_with_allocator(x);
// SAFETY: Pointer is valid.
unsafe { Self::from_inner_in(ptr.into(), alloc) }
unsafe { Self::from_inner_in(ptr, alloc) }
}

/// Constructs a new `Arc` with uninitialized contents in the provided allocator.
Expand Down Expand Up @@ -834,16 +834,14 @@ impl<T, A: Allocator> Arc<T, A> {
{
// Construct the inner in the "uninitialized" state with a single
// weak reference.
let (uninit_raw_ptr, alloc) = Box::into_raw_with_allocator(Box::new_in(
let (uninit_ptr, alloc) = Box::into_non_null_with_allocator(Box::new_in(
ArcInner {
strong: atomic::AtomicUsize::new(0),
weak: atomic::AtomicUsize::new(1),
data: mem::MaybeUninit::<T>::uninit(),
},
alloc,
));
// SAFETY: Pointer is valid since we constructed it.
let uninit_ptr: NonNull<_> = (unsafe { &mut *uninit_raw_ptr }).into();
let init_ptr: NonNull<ArcInner<T>> = uninit_ptr.cast();

let weak = Weak { ptr: init_ptr, alloc };
Expand Down Expand Up @@ -939,9 +937,9 @@ impl<T, A: Allocator> Arc<T, A> {
},
alloc,
)?;
let (ptr, alloc) = Box::into_unique(x);
let (ptr, alloc) = Box::into_non_null_with_allocator(x);
// SAFETY: Pointer is valid since we created it.
Ok(unsafe { Self::from_inner_in(ptr.into(), alloc) })
Ok(unsafe { Self::from_inner_in(ptr, alloc) })
}

/// Constructs a new `Arc` with uninitialized contents, in the provided allocator, returning an
Expand Down Expand Up @@ -4817,7 +4815,7 @@ impl<T, A: Allocator> UniqueArc<T, A> {
#[must_use]
// #[unstable(feature = "allocator_api", issue = "32838")]
pub fn new_in(data: T, alloc: A) -> Self {
let (ptr, alloc) = Box::into_unique(Box::new_in(
let (ptr, alloc) = Box::into_non_null_with_allocator(Box::new_in(
ArcInner {
strong: atomic::AtomicUsize::new(0),
// keep one weak reference so if all the weak pointers that are created are dropped
Expand All @@ -4827,7 +4825,7 @@ impl<T, A: Allocator> UniqueArc<T, A> {
},
alloc,
));
Self { ptr: ptr.into(), _marker: PhantomData, _marker2: PhantomData, alloc }
Self { ptr, _marker: PhantomData, _marker2: PhantomData, alloc }
}

#[cfg(not(no_global_oom_handling))]
Expand Down
43 changes: 43 additions & 0 deletions library/alloctests/tests/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,46 @@ fn issue_158875_make_mut_dont_leak_allocator() {
fn new_uninit_slice_capacity_overflow() {
let _ = Arc::<[u8]>::new_uninit_slice(isize::MAX as usize);
}

mod arc_allocator_provenance {
//! Regression tests for issues where the pointer passed back to the allocator
//! only had the provenance of a reborrow, which is unsound with allocators
//! that store metadata next to the allocation.
//! Mainly meant to be run in Miri (but should also work outside it).

use std::alloc::{AllocError, Allocator, Global, Layout};
use std::ptr::NonNull;
use std::sync::Arc;

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> {
// imagine we are storing metadata in the extra bytes
let ptr = Global.allocate(widen(layout))?;
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 was
// returned by `allocate`, so it's okay to deallocate the full
// `layout.size() + 10` bytes through it.
unsafe { Global.deallocate(ptr, widen(layout)) }
}
}

#[test]
fn issue_162719() {
drop(Arc::<i32, _>::new_uninit_in(MyMetadataAlloc));
drop(Arc::new_in(1i32, MyMetadataAlloc));
}

#[test]
fn issue_162720() {
drop(Arc::new_cyclic_in(|_| 1i32, MyMetadataAlloc));
}
}
Loading