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]
Date:   Wed, 14 Jun 2023 19:42:55 +0100
From:   Gary Guo <gary@...yguo.net>
To:     Alice Ryhl <aliceryhl@...gle.com>
Cc:     rust-for-linux@...r.kernel.org, Miguel Ojeda <ojeda@...nel.org>,
        Wedson Almeida Filho <wedsonaf@...il.com>,
        Alex Gaynor <alex.gaynor@...il.com>,
        Boqun Feng <boqun.feng@...il.com>,
        "Björn Roy Baron" <bjorn3_gh@...tonmail.com>,
        Benno Lossin <benno.lossin@...ton.me>,
        linux-kernel@...r.kernel.org, patches@...ts.linux.dev
Subject: Re: [PATCH] rust: make `UnsafeCell` the outer type in `Opaque`

On Wed, 14 Jun 2023 11:53:28 +0000
Alice Ryhl <aliceryhl@...gle.com> wrote:

> When combining `UnsafeCell` with `MaybeUninit`, it is idiomatic to use
> `UnsafeCell` as the outer type. Intuitively, this is because a
> `MaybeUninit<T>` might not contain a `T`, but we always want the effect
> of the `UnsafeCell`, even if the inner value is uninitialized.
> 
> Now, strictly speaking, this doesn't really make a difference. The
> compiler will always apply the `UnsafeCell` effect even if the inner
> value is uninitialized. But I think we should follow the convention
> here.
> 
> Signed-off-by: Alice Ryhl <aliceryhl@...gle.com>

Reviewed-by: Gary Guo <gary@...yguo.net>

> ---
>  rust/kernel/types.rs | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> index 1e5380b16ed5..fb41635f1e1f 100644
> --- a/rust/kernel/types.rs
> +++ b/rust/kernel/types.rs
> @@ -224,17 +224,17 @@ fn drop(&mut self) {
>  ///
>  /// This is meant to be used with FFI objects that are never interpreted by Rust code.
>  #[repr(transparent)]
> -pub struct Opaque<T>(MaybeUninit<UnsafeCell<T>>);
> +pub struct Opaque<T>(UnsafeCell<MaybeUninit<T>>);
>  
>  impl<T> Opaque<T> {
>      /// Creates a new opaque value.
>      pub const fn new(value: T) -> Self {
> -        Self(MaybeUninit::new(UnsafeCell::new(value)))
> +        Self(UnsafeCell::new(MaybeUninit::new(value)))
>      }
>  
>      /// Creates an uninitialised value.
>      pub const fn uninit() -> Self {
> -        Self(MaybeUninit::uninit())
> +        Self(UnsafeCell::new(MaybeUninit::uninit()))
>      }
>  
>      /// Creates a pin-initializer from the given initializer closure.
> @@ -258,7 +258,7 @@ pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
>  
>      /// Returns a raw pointer to the opaque data.
>      pub fn get(&self) -> *mut T {
> -        UnsafeCell::raw_get(self.0.as_ptr())
> +        UnsafeCell::get(&self.0).cast::<T>()
>      }
>  
>      /// Gets the value behind `this`.
> @@ -266,7 +266,7 @@ pub fn get(&self) -> *mut T {
>      /// This function is useful to get access to the value without creating intermediate
>      /// references.
>      pub const fn raw_get(this: *const Self) -> *mut T {
> -        UnsafeCell::raw_get(this.cast::<UnsafeCell<T>>())
> +        UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()

This can just be `this.cast_mut().cast()` since all types involved are
transparent.

>      }
>  }
>  
> 
> base-commit: d2e3115d717197cb2bc020dd1f06b06538474ac3

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ