mirror of
https://github.com/allankoechke/rust.git
synced 2026-12-03 09:32:42 +00:00
Rollup merge of #162732 - maxdexh:water-leak, r=Darksonn
Fix unsound dealloc of reborrows from custom allocators - Fixes rust-lang/rust#162719 - Fixes rust-lang/rust#162720
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -802,7 +802,7 @@ 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),
|
||||
@@ -810,8 +810,6 @@ impl<T, A: Allocator> Rc<T, A> {
|
||||
},
|
||||
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 };
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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))]
|
||||
|
||||
@@ -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.
|
||||
@@ -834,7 +834,7 @@ 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),
|
||||
@@ -842,8 +842,6 @@ impl<T, A: Allocator> Arc<T, A> {
|
||||
},
|
||||
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 };
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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))]
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user