diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-09-02 22:35:18 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-02 22:35:18 +0200 |
| commit | 003ddec7a697b80a822a7af7074875f4746db0c9 (patch) | |
| tree | 24e27badb6d8d60e93d1aa6bb16a61423673b0cc | |
| parent | bd53aa3bf7a24a70d763182303bd75e5fc51a9af (diff) | |
| parent | 0e5628d7de217454052d5246e994590fa7b75a93 (diff) | |
| download | rust-003ddec7a697b80a822a7af7074875f4746db0c9.tar.gz rust-003ddec7a697b80a822a7af7074875f4746db0c9.zip | |
Rollup merge of #129748 - RalfJung:box-validity, r=workingjubilee
Box validity: update for new zero-sized rules Fixes https://github.com/rust-lang/unsafe-code-guidelines/issues/529 Cc `@joshlf` `@rust-lang/opsem`
| -rw-r--r-- | library/alloc/src/boxed.rs | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 38b1766c174..a924feaf15f 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -53,22 +53,20 @@ //! //! # Memory layout //! -//! For non-zero-sized values, a [`Box`] will use the [`Global`] allocator for -//! its allocation. It is valid to convert both ways between a [`Box`] and a -//! raw pointer allocated with the [`Global`] allocator, given that the -//! [`Layout`] used with the allocator is correct for the type. More precisely, -//! a `value: *mut T` that has been allocated with the [`Global`] allocator -//! with `Layout::for_value(&*value)` may be converted into a box using -//! [`Box::<T>::from_raw(value)`]. Conversely, the memory backing a `value: *mut -//! T` obtained from [`Box::<T>::into_raw`] may be deallocated using the -//! [`Global`] allocator with [`Layout::for_value(&*value)`]. +//! For non-zero-sized values, a [`Box`] will use the [`Global`] allocator for its allocation. It is +//! valid to convert both ways between a [`Box`] and a raw pointer allocated with the [`Global`] +//! allocator, given that the [`Layout`] used with the allocator is correct for the type and the raw +//! pointer points to a valid value of the right type. More precisely, a `value: *mut T` that has +//! been allocated with the [`Global`] allocator with `Layout::for_value(&*value)` may be converted +//! into a box using [`Box::<T>::from_raw(value)`]. Conversely, the memory backing a `value: *mut T` +//! obtained from [`Box::<T>::into_raw`] may be deallocated using the [`Global`] allocator with +//! [`Layout::for_value(&*value)`]. //! -//! For zero-sized values, the `Box` pointer still has to be [valid] for reads -//! and writes and sufficiently aligned. In particular, casting any aligned -//! non-zero integer literal to a raw pointer produces a valid pointer, but a -//! pointer pointing into previously allocated memory that since got freed is -//! not valid. The recommended way to build a Box to a ZST if `Box::new` cannot -//! be used is to use [`ptr::NonNull::dangling`]. +//! For zero-sized values, the `Box` pointer has to be non-null and sufficiently aligned. The +//! recommended way to build a Box to a ZST if `Box::new` cannot be used is to use +//! [`ptr::NonNull::dangling`]. +//! +//! On top of these basic layout requirements, a `Box<T>` must point to a valid value of `T`. //! //! So long as `T: Sized`, a `Box<T>` is guaranteed to be represented //! as a single pointer and is also ABI-compatible with C pointers |
