Re: [PATCH] rust: alloc: Fix `ArrayLayout` allocations

From: Boqun Feng
Date: Mon Nov 25 2024 - 11:20:33 EST


On Sat, Nov 23, 2024 at 06:39:23PM +0100, Miguel Ojeda wrote:
> On Sat, Nov 23, 2024 at 11:30 AM Asahi Lina <lina@xxxxxxxxxxxxx> wrote:
> >
> > We were accidentally allocating a layout for the *square* of the object
> > size due to a variable shadowing mishap.
>
> Good catch, thanks! (Square?)
>

While we are at it, I think it'll be good to add some example/tests for
those functions of ArrayLayout, for example, the below will catch this:

I will open a good-first-issue.

Regards,
Boqun

------------------------>8
diff --git a/rust/kernel/alloc/layout.rs b/rust/kernel/alloc/layout.rs
index 7e0c2f46157b..bb3ce3b2218b 100644
--- a/rust/kernel/alloc/layout.rs
+++ b/rust/kernel/alloc/layout.rs
@@ -7,6 +7,7 @@
use core::{alloc::Layout, marker::PhantomData};

/// Error when constructing an [`ArrayLayout`].
+#[derive(Debug)]
pub struct LayoutError;

/// A layout for an array `[T; n]`.
@@ -43,6 +44,20 @@ pub const fn empty() -> Self {
/// # Errors
///
/// When `len * size_of::<T>()` overflows or when `len * size_of::<T>() > isize::MAX`.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use kernel::alloc::layout::ArrayLayout;
+ ///
+ /// // No overflow.
+ /// let layout = ArrayLayout::<i32>::new(12);
+ /// assert_eq!(layout.expect("sizeof(i32) * 12 is 48, not overflow").len(), 12);
+ ///
+ /// // Overflow, should return `Err`.
+ /// let layout = ArrayLayout::<i32>::new(isize::MAX as usize);
+ /// assert!(layout.is_err());
+ /// ```
pub const fn new(len: usize) -> Result<Self, LayoutError> {
match len.checked_mul(core::mem::size_of::<T>()) {
Some(len) if len <= ISIZE_MAX => {