Skip to content
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 8 additions & 0 deletions library/alloc/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub struct Global;
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
// SAFETY: Untriaged.
unsafe {
// Make sure we don't accidentally allow omitting the allocator shim in
// stable code until it is actually stabilized.
Expand Down Expand Up @@ -159,13 +160,15 @@ pub unsafe fn alloc(layout: Layout) -> *mut u8 {
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
// SAFETY: Untriaged.
unsafe { dealloc_nonnull(NonNull::new_unchecked(ptr), layout) }
}

/// Same as [`dealloc`] but when you already have a non-null pointer
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
unsafe fn dealloc_nonnull(ptr: NonNull<u8>, layout: Layout) {
// SAFETY: Untriaged.
unsafe { __rust_dealloc(ptr, layout.size(), layout.alignment()) }
}

Expand Down Expand Up @@ -212,13 +215,15 @@ unsafe fn dealloc_nonnull(ptr: NonNull<u8>, layout: Layout) {
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
// SAFETY: Untriaged.
unsafe { realloc_nonnull(NonNull::new_unchecked(ptr), layout, new_size) }
}

/// Same as [`realloc`] but when you already have a non-null pointer
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
unsafe fn realloc_nonnull(ptr: NonNull<u8>, layout: Layout, new_size: usize) -> *mut u8 {
// SAFETY: Untriaged.
unsafe { __rust_realloc(ptr, layout.size(), layout.alignment(), new_size) }
}

Expand Down Expand Up @@ -276,6 +281,7 @@ unsafe fn realloc_nonnull(ptr: NonNull<u8>, layout: Layout, new_size: usize) ->
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
// SAFETY: Untriaged.
unsafe {
// Make sure we don't accidentally allow omitting the allocator shim in
// stable code until it is actually stabilized.
Expand Down Expand Up @@ -519,6 +525,7 @@ impl Global {
cmp::min(old_layout.size(), new_layout.size()),
);
}
// SAFETY: Untriaged.
unsafe {
self.deallocate_impl(ptr, old_layout);
}
Expand Down Expand Up @@ -633,6 +640,7 @@ pub const fn handle_alloc_error(layout: Layout) -> ! {

#[inline]
fn rt_error(layout: Layout) -> ! {
// SAFETY: Untriaged.
unsafe {
__rust_alloc_error_handler(layout.size(), layout.align());
}
Expand Down
33 changes: 33 additions & 0 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ const fn box_new_uninit(layout: Layout) -> *mut u8 {
pub const fn box_assume_init_into_vec_unsafe<T, const N: usize>(
b: Box<MaybeUninit<[T; N]>>,
) -> crate::vec::Vec<T> {
// SAFETY: Untriaged.
unsafe { (b.assume_init() as Box<[T]>).into_vec() }
}

Expand Down Expand Up @@ -450,6 +451,7 @@ impl<T> Box<T> {
if size_of::<T>() == size_of::<U>() && align_of::<T>() == align_of::<U>() {
let (value, allocation) = Box::take(this);
Box::write(
// SAFETY: Untriaged.
unsafe { mem::transmute::<Box<MaybeUninit<T>>, Box<MaybeUninit<U>>>(allocation) },
f(value),
)
Expand Down Expand Up @@ -490,6 +492,7 @@ impl<T> Box<T> {
let (value, allocation) = Box::take(this);
try {
Box::write(
// SAFETY: Untriaged.
unsafe {
mem::transmute::<Box<MaybeUninit<T>>, Box<MaybeUninit<R::Output>>>(
allocation,
Expand Down Expand Up @@ -528,6 +531,7 @@ impl<T, A: Allocator> Box<T, A> {
{
let mut boxed = Self::new_uninit_in(alloc);
boxed.write(x);
// SAFETY: Untriaged.
unsafe { boxed.assume_init() }
}

Expand All @@ -554,6 +558,7 @@ impl<T, A: Allocator> Box<T, A> {
{
let mut boxed = Self::try_new_uninit_in(alloc)?;
boxed.write(x);
// SAFETY: Untriaged.
unsafe { Ok(boxed.assume_init()) }
}

Expand Down Expand Up @@ -618,6 +623,7 @@ impl<T, A: Allocator> Box<T, A> {
let layout = Layout::new::<mem::MaybeUninit<T>>();
alloc.allocate(layout)?.cast()
};
// SAFETY: Untriaged.
unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) }
}

Expand Down Expand Up @@ -690,6 +696,7 @@ impl<T, A: Allocator> Box<T, A> {
let layout = Layout::new::<mem::MaybeUninit<T>>();
alloc.allocate_zeroed(layout)?.cast()
};
// SAFETY: Untriaged.
unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) }
}

Expand Down Expand Up @@ -726,6 +733,7 @@ impl<T, A: Allocator> Box<T, A> {
#[unstable(feature = "box_into_boxed_slice", issue = "71582")]
pub fn into_boxed_slice(boxed: Self) -> Box<[T], A> {
let (raw, alloc) = Box::into_raw_with_allocator(boxed);
// SAFETY: Untriaged.
unsafe { Box::from_raw_in(raw as *mut [T; 1], alloc) }
}

Expand Down Expand Up @@ -769,6 +777,7 @@ impl<T, A: Allocator> Box<T, A> {
/// ```
#[unstable(feature = "box_take", issue = "147212")]
pub fn take(boxed: Self) -> (T, Box<mem::MaybeUninit<T>, A>) {
// SAFETY: Untriaged.
unsafe {
let (raw, alloc) = Box::into_non_null_with_allocator(boxed);
let value = raw.read();
Expand Down Expand Up @@ -873,6 +882,7 @@ impl<T: ?Sized + CloneToUninit, A: Allocator> Box<T, A> {
fn drop(&mut self) {
let &mut DeallocDropGuard(layout, alloc, ptr) = self;
// Safety: `ptr` was allocated by `*alloc` with layout `layout`
// SAFETY: Untriaged.
unsafe {
alloc.deallocate(ptr, layout);
}
Expand All @@ -890,12 +900,14 @@ impl<T: ?Sized + CloneToUninit, A: Allocator> Box<T, A> {
// Safety: `*ptr` is newly allocated, correctly aligned to `align_of_val(src)`,
// and is valid for writes for `size_of_val(src)`.
// If this panics, then `guard` will deallocate for us (if allocation occuured)
// SAFETY: Untriaged.
unsafe {
<T as CloneToUninit>::clone_to_uninit(src, ptr);
}
// Defuse the deallocate guard
core::mem::forget(guard);
// Safety: We just initialized `*ptr` as a clone of `src`
// SAFETY: Untriaged.
Ok(unsafe { Box::from_raw_in(ptr.with_metadata_of(src), alloc) })
}
}
Expand All @@ -919,6 +931,7 @@ impl<T> Box<[T]> {
#[stable(feature = "new_uninit", since = "1.82.0")]
#[must_use]
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
// SAFETY: Untriaged.
unsafe { RawVec::with_capacity(len).into_box(len) }
}

Expand All @@ -942,6 +955,7 @@ impl<T> Box<[T]> {
#[stable(feature = "new_zeroed_alloc", since = "1.92.0")]
#[must_use]
pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
// SAFETY: Untriaged.
unsafe { RawVec::with_capacity_zeroed(len).into_box(len) }
}

Expand Down Expand Up @@ -975,6 +989,7 @@ impl<T> Box<[T]> {
};
Global.allocate(layout)?.cast()
};
// SAFETY: Untriaged.
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) }
}

Expand Down Expand Up @@ -1009,6 +1024,7 @@ impl<T> Box<[T]> {
};
Global.allocate_zeroed(layout)?.cast()
};
// SAFETY: Untriaged.
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) }
}
}
Expand Down Expand Up @@ -1036,6 +1052,7 @@ impl<T, A: Allocator> Box<[T], A> {
#[unstable(feature = "allocator_api", issue = "32838")]
#[must_use]
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
// SAFETY: Untriaged.
unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) }
}

Expand Down Expand Up @@ -1063,6 +1080,7 @@ impl<T, A: Allocator> Box<[T], A> {
#[unstable(feature = "allocator_api", issue = "32838")]
#[must_use]
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
// SAFETY: Untriaged.
unsafe { RawVec::with_capacity_zeroed_in(len, alloc).into_box(len) }
}

Expand Down Expand Up @@ -1101,6 +1119,7 @@ impl<T, A: Allocator> Box<[T], A> {
};
alloc.allocate(layout)?.cast()
};
// SAFETY: Untriaged.
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, alloc).into_box(len)) }
}

Expand Down Expand Up @@ -1140,6 +1159,7 @@ impl<T, A: Allocator> Box<[T], A> {
};
alloc.allocate_zeroed(layout)?.cast()
};
// SAFETY: Untriaged.
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, alloc).into_box(len)) }
}

Expand Down Expand Up @@ -1237,6 +1257,7 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
#[stable(feature = "box_uninit_write", since = "1.87.0")]
#[inline]
pub fn write(mut boxed: Self, value: T) -> Box<T, A> {
// SAFETY: Untriaged.
unsafe {
(*boxed).write(value);
boxed.assume_init()
Expand Down Expand Up @@ -1273,6 +1294,7 @@ impl<T, A: Allocator> Box<[mem::MaybeUninit<T>], A> {
#[inline]
pub unsafe fn assume_init(self) -> Box<[T], A> {
let (raw, alloc) = Box::into_raw_with_allocator(self);
// SAFETY: Untriaged.
unsafe { Box::from_raw_in(raw as *mut [T], alloc) }
}
}
Expand Down Expand Up @@ -1326,6 +1348,7 @@ impl<T: ?Sized> Box<T> {
#[inline]
#[must_use = "call `drop(Box::from_raw(ptr))` if you intend to drop the `Box`"]
pub unsafe fn from_raw(raw: *mut T) -> Self {
// SAFETY: Untriaged.
unsafe { Self::from_raw_in(raw, Global) }
}

Expand Down Expand Up @@ -1378,6 +1401,7 @@ impl<T: ?Sized> Box<T> {
#[inline]
#[must_use = "call `drop(Box::from_non_null(ptr))` if you intend to drop the `Box`"]
pub unsafe fn from_non_null(ptr: NonNull<T>) -> Self {
// SAFETY: Untriaged.
unsafe { Self::from_raw(ptr.as_ptr()) }
}

Expand Down Expand Up @@ -1557,6 +1581,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
#[unstable(feature = "allocator_api", issue = "32838")]
#[inline]
pub unsafe fn from_raw_in(raw: *mut T, alloc: A) -> Self {
// SAFETY: Untriaged.
Box(unsafe { Unique::new_unchecked(raw) }, alloc)
}

Expand Down Expand Up @@ -1675,6 +1700,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
// In case `A` *is* `Global`, this does not quite have the right behavior; `into_raw`
// works around that.
let ptr = &raw mut **b;
// SAFETY: Untriaged.
let alloc = unsafe { ptr::read(&b.1) };
(ptr, alloc)
}
Expand Down Expand Up @@ -1742,6 +1768,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
#[doc(hidden)]
pub fn into_unique(b: Self) -> (Unique<T>, A) {
let (ptr, alloc) = Box::into_raw_with_allocator(b);
// SAFETY: Untriaged.
unsafe { (Unique::from(&mut *ptr), alloc) }
}

Expand Down Expand Up @@ -1940,6 +1967,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
{
let (ptr, alloc) = Box::into_raw_with_allocator(b);
mem::forget(alloc);
// SAFETY: Untriaged.
unsafe { &mut *ptr }
}

Expand Down Expand Up @@ -1981,6 +2009,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
// It's not possible to move or replace the insides of a `Pin<Box<T>>`
// when `T: !Unpin`, so it's safe to pin it directly without any
// additional requirements.
// SAFETY: Untriaged.
unsafe { Pin::new_unchecked(boxed) }
}
}
Expand All @@ -1993,6 +2022,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Box<T, A> {

let ptr = self.0;

// SAFETY: Untriaged.
unsafe {
let layout = Layout::for_value_raw(ptr.as_ptr());
if layout.size() != 0 {
Expand All @@ -2009,6 +2039,7 @@ impl<T: Default> Default for Box<T> {
#[inline]
fn default() -> Self {
let mut x: Box<mem::MaybeUninit<T>> = Box::new_uninit();
// SAFETY: Untriaged.
unsafe {
// SAFETY: `x` is valid for writing and has the same layout as `T`.
// If `T::default()` panics, dropping `x` will just deallocate the Box as `MaybeUninit<T>`
Expand Down Expand Up @@ -2084,6 +2115,7 @@ impl<T: Clone, A: Allocator + Clone> Clone for Box<T, A> {
fn clone(&self) -> Self {
// Pre-allocate memory to allow writing the cloned value directly.
let mut boxed = Self::new_uninit_in(self.1.clone());
// SAFETY: Untriaged.
unsafe {
(**self).clone_to_uninit(boxed.as_mut_ptr().cast());
boxed.assume_init()
Expand Down Expand Up @@ -2154,6 +2186,7 @@ impl Clone for Box<str> {
fn clone(&self) -> Self {
// this makes a copy of the data
let buf: Box<[u8]> = self.as_bytes().into();
// SAFETY: Untriaged.
unsafe { from_boxed_utf8_unchecked(buf) }
}
}
Expand Down
Loading
Loading