lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <D916LG7Z9Q31.5RVNMYM38E2D@proton.me>
Date: Tue, 08 Apr 2025 10:22:29 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Alexandre Courbot <acourbot@...dia.com>, Danilo Krummrich <dakr@...nel.org>, Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>, Andreas Hindborg <a.hindborg@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] rust: alloc: allow coercion from `Box<T>` to `Box<dyn U>` if T implements U

On Tue Apr 8, 2025 at 7:18 AM CEST, Alexandre Courbot wrote:
> This enables the creation of trait objects backed by a Box, similarly to
> what can be done with the standard library.
>
> Suggested-by: Benno Lossin <benno.lossin@...ton.me>
> Signed-off-by: Alexandre Courbot <acourbot@...dia.com>
> ---
> From this discussion on Zulip [1].
>
> Heavily inspired from the similar feature on `Arc`.
>
> [1] https://rust-for-linux.zulipchat.com/#narrow/channel/291565-Help/topic/Trait.20objects.3F/with/510689662
> ---
>  rust/kernel/alloc/kbox.rs | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs
> index b77d32f3a58bab5ec73c612bdaaba0d79bfdff65..969b9f9fd3149685e1d1ecdf1eed9c647c887397 100644
> --- a/rust/kernel/alloc/kbox.rs
> +++ b/rust/kernel/alloc/kbox.rs
> @@ -32,6 +32,8 @@
>  ///
>  /// When dropping a [`Box`], the value is also dropped and the heap memory is automatically freed.
>  ///
> +/// [`Box`]es can also be used to store trait objects by coercing their type.
> +///
>  /// # Examples
>  ///
>  /// ```
> @@ -62,7 +64,17 @@
>  /// `self.0` is always properly aligned and either points to memory allocated with `A` or, for
>  /// zero-sized types, is a dangling, well aligned pointer.
>  #[repr(transparent)]
> -pub struct Box<T: ?Sized, A: Allocator>(NonNull<T>, PhantomData<A>);
> +#[cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, derive(core::marker::CoercePointee))]
> +pub struct Box<#[pointee] T: ?Sized, A: Allocator>(NonNull<T>, PhantomData<A>);
> +
> +// This is to allow coercion from `Box<T>` to `Box<U>` if `T` can be converted to the
> +// dynamically-sized type (DST) `U`.
> +#[cfg(not(CONFIG_RUSTC_HAS_COERCE_POINTEE))]
> +impl<T: ?Sized + core::marker::Unsize<U>, U: ?Sized> core::ops::CoerceUnsized<Box<U>> for Box<T> {}

You forgot to add the `A: Allocator` generic here and in the impl below.

---
Cheers,
Benno

> +
> +// This is to allow `Box<U>` to be dispatched on when `Box<T>` can be coerced into `Box<U>`.
> +#[cfg(not(CONFIG_RUSTC_HAS_COERCE_POINTEE))]
> +impl<T: ?Sized + core::marker::Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<Box<U>> for Box<T> {}
>  
>  /// Type alias for [`Box`] with a [`Kmalloc`] allocator.
>  ///
>
> ---
> base-commit: a2cc6ff5ec8f91bc463fd3b0c26b61166a07eb11
> change-id: 20250408-box_trait_objs-02a700401f0b
>
> Best regards,



Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ