Skip to contentSkip to conversation

rust/alloc: mention layout in Box::from_raw()

PATCH In review David Rheinsberg 2 participants 2 messages Apr 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust/alloc: mention layout in Box::from_raw()

Extend the safety requirements of Box::from_raw() to mention that the layout of the allocation must match exactly. Even though the underlying allocators maintain allocation layout information to some degree, the Rust abstraction strictly requires the layout to match exactly.

---
 rust/kernel/alloc/kbox.rs | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
rust/kernel/alloc/kbox.rs +5 -4
index 622b3529edfc..1b30c51f87ab 100644
@@ -170,15 +170,16 @@ impl<T, A> Box<T, A>
     ///
     /// # Safety
     ///
-    /// For non-ZSTs, `raw` must point at an allocation allocated with `A` that is sufficiently
-    /// aligned for and holds a valid `T`. The caller passes ownership of the allocation to the
-    /// `Box`.
+    /// For non-ZSTs, `raw` must point at an allocation allocated with `A` with a layout
+    /// of `Layout::for_value::<T>()`. The caller passes ownership of the allocation
+    /// to the `Box`.
     ///
     /// For ZSTs, `raw` must be a dangling, well aligned pointer.
     #[inline]
     pub const unsafe fn from_raw(raw: *mut T) -> Self {
         // INVARIANT: Validity of `raw` is guaranteed by the safety preconditions of this function.
-        // SAFETY: By the safety preconditions of this function, `raw` is not a NULL pointer.
+        // SAFETY: By the safety preconditions of this function, `raw` is not a NULL pointer and
+        // was allocated via `A` for `Layout::for_value::<T>()`.
         Self(unsafe { NonNull::new_unchecked(raw) }, PhantomData)
     }
 
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 4 lines · David Rheinsberg

Extend the safety requirements of Box::from_raw() to mention that the layout of the allocation must match exactly. Even though the underlying allocators maintain allocation layout information to some degree, the Rust abstraction strictly requires the layout to match exactly.

One additional general note on this. I've always been more against this additional requirement of Allocator::free() compared to our existing kernel allocators, but eventually agreed as there may also be advantages.

This is the first time it gets a little bit in our way, since technically Vec::into_boxed_slice() only needs to call realloc() to fulfill the safety requirement that comes from Allocator::free().

I will keep monitoring this, and if it turns out to have more disadvantages we might want to change it.

Quoted 1 line

This should be:

Please also add a corresponding Fixes: tag.

Quoted 19 lines
---
 rust/kernel/alloc/kbox.rs | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
rust/kernel/alloc/kbox.rs +3 -3
index 622b3529edfc..1b30c51f87ab 100644
@@ -170,15 +170,16 @@ impl<T, A> Box<T, A>
     ///
     /// # Safety
     ///
-    /// For non-ZSTs, `raw` must point at an allocation allocated with `A` that is sufficiently
-    /// aligned for and holds a valid `T`. The caller passes ownership of the allocation to the
-    /// `Box`.
+    /// For non-ZSTs, `raw` must point at an allocation allocated with `A` with a layout
+    /// of `Layout::for_value::<T>()`. The caller passes ownership of the allocation
+    /// to the `Box`.

rust/kernel/alloc/kbox.rs impl<T, A> Box<T, A> · line 184

Quoted 10 lines
rust/kernel/alloc/kbox.rs +2 -1
     ///
     /// For ZSTs, `raw` must be a dangling, well aligned pointer.
     #[inline]
     pub const unsafe fn from_raw(raw: *mut T) -> Self {
         // INVARIANT: Validity of `raw` is guaranteed by the safety preconditions of this function.
-        // SAFETY: By the safety preconditions of this function, `raw` is not a NULL pointer.
+        // SAFETY: By the safety preconditions of this function, `raw` is not a NULL pointer and
+        // was allocated via `A` for `Layout::for_value::<T>()`.
         Self(unsafe { NonNull::new_unchecked(raw) }, PhantomData)
     }

This looks good, but I think we also need this as a type invariant, can you please add one?