|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! This file provides a implementation of a subset of the upstream rust `UnsafePinned` type |
| 4 | +//! for rust versions that don't include this type. |
| 5 | +
|
| 6 | +use core::{cell::UnsafeCell, marker::PhantomPinned}; |
| 7 | + |
| 8 | +/// This type provides a way to opt-out of typical aliasing rules; |
| 9 | +/// specifically, `&mut UnsafePinned<T>` is not guaranteed to be a unique pointer. |
| 10 | +/// |
| 11 | +/// However, even if you define your type like `pub struct Wrapper(UnsafePinned<...>)`, it is still |
| 12 | +/// very risky to have an `&mut Wrapper` that aliases anything else. Many functions that work |
| 13 | +/// generically on `&mut T` assume that the memory that stores `T` is uniquely owned (such as |
| 14 | +/// `mem::swap`). In other words, while having aliasing with `&mut Wrapper` is not immediate |
| 15 | +/// Undefined Behavior, it is still unsound to expose such a mutable reference to code you do not |
| 16 | +/// control! Techniques such as pinning via [`Pin`](core::pin::Pin) are needed to ensure soundness. |
| 17 | +/// |
| 18 | +/// Similar to [`UnsafeCell`], [`UnsafePinned`] will not usually show up in |
| 19 | +/// the public API of a library. It is an internal implementation detail of libraries that need to |
| 20 | +/// support aliasing mutable references. |
| 21 | +/// |
| 22 | +/// Further note that this does *not* lift the requirement that shared references must be read-only! |
| 23 | +/// Use [`UnsafeCell`] for that. |
| 24 | +/// |
| 25 | +/// This type blocks niches the same way [`UnsafeCell`] does. |
| 26 | +// |
| 27 | +// As opposed to the upstream Rust type this contains a `PhantomPinned`` and `UnsafeCell<T>` |
| 28 | +// - `PhantomPinned` to avoid needing a `impl<T> !Unpin for UnsafePinned<T>` |
| 29 | +// - `UnsafeCell<T>` instead of T to disallow niche optimizations, |
| 30 | +// which is handled in the compiler in upstream Rust |
| 31 | +#[repr(transparent)] |
| 32 | +pub struct UnsafePinned<T: ?Sized> { |
| 33 | + _ph: PhantomPinned, |
| 34 | + value: UnsafeCell<T>, |
| 35 | +} |
| 36 | + |
| 37 | +impl<T> UnsafePinned<T> { |
| 38 | + /// Constructs a new instance of [`UnsafePinned`] which will wrap the specified value. |
| 39 | + /// |
| 40 | + /// All access to the inner value through `&UnsafePinned<T>` or `&mut UnsafePinned<T>` or |
| 41 | + /// `Pin<&mut UnsafePinned<T>>` requires `unsafe` code. |
| 42 | + #[inline(always)] |
| 43 | + #[must_use] |
| 44 | + pub const fn new(value: T) -> Self { |
| 45 | + UnsafePinned { |
| 46 | + value: UnsafeCell::new(value), |
| 47 | + _ph: PhantomPinned, |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | +impl<T: ?Sized> UnsafePinned<T> { |
| 52 | + /// Get read-only access to the contents of a shared `UnsafePinned`. |
| 53 | + /// |
| 54 | + /// Note that `&UnsafePinned<T>` is read-only if `&T` is read-only. This means that if there is |
| 55 | + /// mutation of the `T`, future reads from the `*const T` returned here are UB! Use |
| 56 | + /// [`UnsafeCell`] if you also need interior mutability. |
| 57 | + /// |
| 58 | + /// [`UnsafeCell`]: core::cell::UnsafeCell |
| 59 | + /// |
| 60 | + /// ```rust,no_build |
| 61 | + /// use kernel::types::UnsafePinned; |
| 62 | + /// |
| 63 | + /// unsafe { |
| 64 | + /// let mut x = UnsafePinned::new(0); |
| 65 | + /// let ptr = x.get(); // read-only pointer, assumes immutability |
| 66 | + /// x.get_mut_unchecked().write(1); |
| 67 | + /// ptr.read(); // UB! |
| 68 | + /// } |
| 69 | + /// ``` |
| 70 | + /// |
| 71 | + /// Note that the `get_mut_unchecked` function used by this example is |
| 72 | + /// currently not implemented in the kernel implementation. |
| 73 | + #[inline(always)] |
| 74 | + #[must_use] |
| 75 | + pub const fn get(&self) -> *const T { |
| 76 | + self.value.get() |
| 77 | + } |
| 78 | + |
| 79 | + /// Gets a mutable pointer to the wrapped value. |
| 80 | + /// |
| 81 | + /// The difference from `get_mut_pinned` and `get_mut_unchecked` is that this function |
| 82 | + /// accepts a raw pointer, which is useful to avoid the creation of temporary references. |
| 83 | + /// |
| 84 | + /// These functions mentioned here are currently not implemented in the kernel. |
| 85 | + #[inline(always)] |
| 86 | + #[must_use] |
| 87 | + pub const fn raw_get_mut(this: *mut Self) -> *mut T { |
| 88 | + this as *mut T |
| 89 | + } |
| 90 | +} |
0 commit comments