|
| 1 | +#![unstable(feature = "maybe_dangling", issue = "118166")] |
| 2 | + |
| 3 | +use crate::{mem, ptr}; |
| 4 | + |
| 5 | +/// Allows wrapped [references] and [boxes] to dangle. |
| 6 | +/// |
| 7 | +/// That is, if a reference (or a `Box`) is wrapped in `MaybeDangling` (includding when in a |
| 8 | +/// (nested) field of a compound type wrapped in `MaybeDangling`), it does not have to follow |
| 9 | +/// pointer aliasing rules or be dereferenceable. |
| 10 | +/// |
| 11 | +/// This can be useful when the value can become dangling while the function holding it is still |
| 12 | +/// executing (particularly in concurent code). As a somewhat absurd example, consider this code: |
| 13 | +/// |
| 14 | +/// ```rust,no_run |
| 15 | +/// #![feature(box_as_ptr)] |
| 16 | +/// # use std::alloc::{dealloc, Layout}; |
| 17 | +/// # use std::mem; |
| 18 | +/// |
| 19 | +/// let mut boxed = Box::new(0_u32); |
| 20 | +/// let ptr = Box::as_mut_ptr(&mut boxed); |
| 21 | +/// |
| 22 | +/// // Safety: the pointer comes from a box and thus was allocated before; `box` is not used afterwards |
| 23 | +/// unsafe { dealloc(ptr.cast(), Layout::new::<u32>()) }; |
| 24 | +/// |
| 25 | +/// mem::forget(boxed); // <-- this is UB! |
| 26 | +/// ``` |
| 27 | +/// |
| 28 | +/// Even though the `Box`e's destructor is not run (and thus we don't have a double free bug), this |
| 29 | +/// code is still UB. This is because when moving `boxed` into `forget`, its validity invariants |
| 30 | +/// are asserted, causing UB since the `Box` is dangling. |
| 31 | +/// |
| 32 | +/// To fix this we could use `MaybeDangling`: |
| 33 | +/// |
| 34 | +/// ```rust |
| 35 | +/// #![feature(maybe_dangling, box_as_ptr)] |
| 36 | +/// # use std::alloc::{dealloc, Layout}; |
| 37 | +/// # use std::mem::{self, MaybeDangling}; |
| 38 | +/// |
| 39 | +/// let mut boxed = MaybeDangling::new(Box::new(0_u32)); |
| 40 | +/// let ptr = Box::as_mut_ptr(boxed.as_mut()); |
| 41 | +/// |
| 42 | +/// // Safety: the pointer comes from a box and thus was allocated before; `box` is not used afterwards |
| 43 | +/// unsafe { dealloc(ptr.cast(), Layout::new::<u32>()) }; |
| 44 | +/// |
| 45 | +/// mem::forget(boxed); // <-- this is OK! |
| 46 | +/// ``` |
| 47 | +/// |
| 48 | +/// Note that the bit pattern must still be valid for the wrapped type. That is [references] |
| 49 | +/// (and [boxes]) still must be aligned and non-null. |
| 50 | +/// |
| 51 | +/// Additionally note that safe code can still assume that the inner value in a `MaybeDangling` is |
| 52 | +/// **not** dangling -- functions like [`as_ref`] and [`into_inner`] are safe. It is not sound to |
| 53 | +/// return a dangling reference in a `MaybeDangling` to safe code. |
| 54 | +/// |
| 55 | +/// [references]: prim@reference |
| 56 | +/// [boxes]: ../../std/boxed/struct.Box.html |
| 57 | +/// [`into_inner`]: MaybeDangling::into_inner |
| 58 | +/// [`as_ref`]: MaybeDangling:as_ref |
| 59 | +#[repr(transparent)] |
| 60 | +#[rustc_pub_transparent] |
| 61 | +#[derive(Debug, Copy, Clone, Default)] |
| 62 | +pub struct MaybeDangling<P: ?Sized>(P); |
| 63 | + |
| 64 | +impl<P: ?Sized> MaybeDangling<P> { |
| 65 | + /// Wraps a value in a `MaybeDangling`, allowing it to dangle. |
| 66 | + pub const fn new(x: P) -> Self |
| 67 | + where |
| 68 | + P: Sized, |
| 69 | + { |
| 70 | + MaybeDangling(x) |
| 71 | + } |
| 72 | + |
| 73 | + /// Returns a reference to the inner value. |
| 74 | + /// |
| 75 | + /// Note that this is UB if the inner value is currently dangling. |
| 76 | + pub const fn as_ref(&self) -> &P { |
| 77 | + &self.0 |
| 78 | + } |
| 79 | + |
| 80 | + /// Returns a mutable reference to the inner value. |
| 81 | + /// |
| 82 | + /// Note that this is UB if the inner value is currently dangling. |
| 83 | + pub const fn as_mut(&mut self) -> &mut P { |
| 84 | + &mut self.0 |
| 85 | + } |
| 86 | + |
| 87 | + /// Extracts the value from the `MaybeDangling` container. |
| 88 | + /// |
| 89 | + /// Note that this is UB if the inner value is currently dangling. |
| 90 | + pub const fn into_inner(self) -> P |
| 91 | + where |
| 92 | + P: Sized, |
| 93 | + { |
| 94 | + // FIXME: replace this with `self.0` when const checker can figure out that `self` isn't actually dropped |
| 95 | + // SAFETY: this is equivalent to `self.0` |
| 96 | + let x = unsafe { ptr::read(&self.0) }; |
| 97 | + mem::forget(self); |
| 98 | + x |
| 99 | + } |
| 100 | +} |
0 commit comments