Skip to content

Commit 2305012

Browse files
committed
docs: transmute<&mut T, &mut MaybeUninit<T>> is unsound when exposed to safe code
In the playground the example program terminates with an unpredictable exit code. The undefined behavior is also detected by miri: error: Undefined Behavior: using uninitialized data
1 parent 8a1f803 commit 2305012

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Diff for: library/core/src/mem/maybe_uninit.rs

+20
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,26 @@ use crate::{fmt, intrinsics, ptr, slice};
232232
/// remain `#[repr(transparent)]`. That said, `MaybeUninit<T>` will *always* guarantee that it has
233233
/// the same size, alignment, and ABI as `T`; it's just that the way `MaybeUninit` implements that
234234
/// guarantee may evolve.
235+
///
236+
/// Note that even though `T` and `MaybeUninit<T>` are ABI compatible it is still unsound to
237+
/// transmute `&mut T` to `&mut MaybeUninit<T>` and expose that to safe code because it would allow
238+
/// safe code to access uninitialized memory:
239+
///
240+
/// ```rust,no_run
241+
/// use core::mem::MaybeUninit;
242+
///
243+
/// fn unsound_transmute<T>(val: &mut T) -> &mut MaybeUninit<T> {
244+
/// unsafe { core::mem::transmute(val) }
245+
/// }
246+
///
247+
/// fn main() {
248+
/// let mut code = 0;
249+
/// let code = &mut code;
250+
/// let code2 = unsound_transmute(code);
251+
/// *code2 = MaybeUninit::uninit();
252+
/// std::process::exit(*code); // UB! Accessing uninitialized memory.
253+
/// }
254+
/// ```
235255
#[stable(feature = "maybe_uninit", since = "1.36.0")]
236256
// Lang item so we can wrap other types in it. This is useful for coroutines.
237257
#[lang = "maybe_uninit"]

0 commit comments

Comments
 (0)