From d559cfd34554f0bf1bc4eeaaf64fad59157e1b24 Mon Sep 17 00:00:00 2001 From: Max Dexheimer Date: Sun, 13 Sep 2026 18:53:12 +0200 Subject: [PATCH 1/2] Fix unsound reborrows of raw ptrs from custom allocators --- library/alloc/src/boxed.rs | 2 +- library/alloc/src/rc.rs | 14 ++++++-------- library/alloc/src/sync.rs | 16 +++++++--------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 07228664d65ef..bf77b477eef70 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -1780,7 +1780,7 @@ impl Box { pub fn into_unique(b: Self) -> (Unique, 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. diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index b2afe0b464eb6..0a37489474511 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -802,7 +802,7 @@ impl Rc { { // 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 Rc { }, alloc, )); - // ignore-tidy-undocumented-unsafe - let uninit_ptr: NonNull<_> = (unsafe { &mut *uninit_raw_ptr }).into(); let init_ptr: NonNull> = uninit_ptr.cast(); let weak = Weak { ptr: init_ptr, alloc }; @@ -863,12 +861,12 @@ impl Rc { // 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 UniqueRc { #[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 UniqueRc { }, alloc, )); - Self { ptr: ptr.into(), _marker: PhantomData, _marker2: PhantomData, alloc } + Self { ptr, _marker: PhantomData, _marker2: PhantomData, alloc } } #[cfg(not(no_global_oom_handling))] diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 09a371f94bbb9..3f5e8614987aa 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -714,9 +714,9 @@ impl Arc { }, 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 Arc { { // 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 Arc { }, alloc, )); - // SAFETY: Pointer is valid since we constructed it. - let uninit_ptr: NonNull<_> = (unsafe { &mut *uninit_raw_ptr }).into(); let init_ptr: NonNull> = uninit_ptr.cast(); let weak = Weak { ptr: init_ptr, alloc }; @@ -939,9 +937,9 @@ impl Arc { }, 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 UniqueArc { #[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 UniqueArc { }, alloc, )); - Self { ptr: ptr.into(), _marker: PhantomData, _marker2: PhantomData, alloc } + Self { ptr, _marker: PhantomData, _marker2: PhantomData, alloc } } #[cfg(not(no_global_oom_handling))] From d9e19459af23e53c76a55ca49554794b45299311 Mon Sep 17 00:00:00 2001 From: Max Dexheimer Date: Sun, 13 Sep 2026 19:32:06 +0200 Subject: [PATCH 2/2] add test --- library/alloctests/tests/arc.rs | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/library/alloctests/tests/arc.rs b/library/alloctests/tests/arc.rs index 4299fea4120d8..31a98fc6a6fa2 100644 --- a/library/alloctests/tests/arc.rs +++ b/library/alloctests/tests/arc.rs @@ -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, 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, 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::::new_uninit_in(MyMetadataAlloc)); + drop(Arc::new_in(1i32, MyMetadataAlloc)); + } + + #[test] + fn issue_162720() { + drop(Arc::new_cyclic_in(|_| 1i32, MyMetadataAlloc)); + } +}