Skip to content

Commit 8558ccd

Browse files
committed
safety-ptr: Add SAFETY on some unsafe blocks from libcore/ptr
Add documentation example to slice_from_raw_parts_mut() Add SAFETY explanations to some unsafe blocks in libcore/ptr * libcore/ptr/mod.rs * libcore/ptr/unique.rs * libcore/ptr/non_null.rs safety-mod.rs: Add SAFETY to slice_from_raw_parts(), slice_from_raw_parts_mut() slice_from_raw_parts_mut: Add documentation example safety-ptr-unique.rs: Add SAFETY to new() and cast() safety-ptr-non_null.rs: Add SAFETY to new() safety-ptr-non_null.rs: Add SAFETY to cast() safety-ptr-non_null.rs: Add SAFETY to from() impls safety-ptr-unique.rs: Add SAFETY to from() impls safety-ptr-non_null.rs: Add SAFETY to new() safety-ptr-unique.rs: Add SAFETY to new() safety-ptr-mod.rs: Fix safety explanation #71507 (comment) safety-prt-non_null.rs: Fix SAFETY comment syntax safety-ptr-unique.rs: Fix syntax for empty() safety-ptr-non_null.rs: Fix misuse of non-null for align_of() safety-ptr-non_null.rs: Remove incorrect SAFETY comment safety-ptr-unique.rs: Remove unsound SAFETY comment safety-ptr-mod.rs: Add std comment on slice_from_raw_parts guarantee safety-ptr-unique.rs: Remove incorrect safety comment Creating a Unique from a NonNull has strict guarantees that the current implementation does not guarantee #71507 (comment) safety-ptr: Re-adding ignore-tidy directive
1 parent b4a11d4 commit 8558ccd

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

src/libcore/ptr/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ pub(crate) struct FatPtr<T> {
254254
#[stable(feature = "slice_from_raw_parts", since = "1.42.0")]
255255
#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
256256
pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
257+
// SAFETY: Accessing the value from the `Repr` union is safe since *const [T]
258+
// and FatPtr have the same memory layouts. Only std can make this
259+
// guarantee.
257260
unsafe { Repr { raw: FatPtr { data, len } }.rust }
258261
}
259262

@@ -267,10 +270,28 @@ pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
267270
///
268271
/// [`slice_from_raw_parts`]: fn.slice_from_raw_parts.html
269272
/// [`from_raw_parts_mut`]: ../../std/slice/fn.from_raw_parts_mut.html
273+
///
274+
/// # Examples
275+
///
276+
/// ```rust
277+
/// use std::ptr;
278+
///
279+
/// let x = &mut [5, 6, 7];
280+
/// let raw_pointer = x.as_mut_ptr();
281+
/// let slice = ptr::slice_from_raw_parts_mut(raw_pointer, 3);
282+
///
283+
/// unsafe {
284+
/// (*slice)[2] = 99; // assign a value at an index in the slice
285+
/// };
286+
///
287+
/// assert_eq!(unsafe { &*slice }[2], 99);
288+
/// ```
270289
#[inline]
271290
#[stable(feature = "slice_from_raw_parts", since = "1.42.0")]
272291
#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
273292
pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
293+
// SAFETY: Accessing the value from the `Repr` union is safe since *mut [T]
294+
// and FatPtr have the same memory layouts
274295
unsafe { Repr { raw: FatPtr { data, len } }.rust_mut }
275296
}
276297

src/libcore/ptr/non_null.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use crate::mem;
77
use crate::ops::{CoerceUnsized, DispatchFromDyn};
88
use crate::ptr::Unique;
99

10-
// ignore-tidy-undocumented-unsafe
11-
1210
/// `*mut T` but non-zero and covariant.
1311
///
1412
/// This is often the correct thing to use when building data structures using
@@ -69,6 +67,9 @@ impl<T: Sized> NonNull<T> {
6967
#[rustc_const_stable(feature = "const_nonnull_dangling", since = "1.32.0")]
7068
#[inline]
7169
pub const fn dangling() -> Self {
70+
// SAFETY: mem::align_of() returns a non-zero usize which is then casted
71+
// to a *mut T. Therefore, `ptr` is not null and the conditions for
72+
// calling new_unchecked() are respected.
7273
unsafe {
7374
let ptr = mem::align_of::<T>() as *mut T;
7475
NonNull::new_unchecked(ptr)
@@ -93,7 +94,12 @@ impl<T: ?Sized> NonNull<T> {
9394
#[stable(feature = "nonnull", since = "1.25.0")]
9495
#[inline]
9596
pub fn new(ptr: *mut T) -> Option<Self> {
96-
if !ptr.is_null() { Some(unsafe { Self::new_unchecked(ptr) }) } else { None }
97+
if !ptr.is_null() {
98+
// SAFETY: The pointer is already checked and is not null
99+
Some(unsafe { Self::new_unchecked(ptr) })
100+
} else {
101+
None
102+
}
97103
}
98104

99105
/// Acquires the underlying `*mut` pointer.
@@ -131,6 +137,7 @@ impl<T: ?Sized> NonNull<T> {
131137
#[rustc_const_stable(feature = "const_nonnull_cast", since = "1.32.0")]
132138
#[inline]
133139
pub const fn cast<U>(self) -> NonNull<U> {
140+
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
134141
unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) }
135142
}
136143
}
@@ -205,6 +212,8 @@ impl<T: ?Sized> hash::Hash for NonNull<T> {
205212
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
206213
#[inline]
207214
fn from(unique: Unique<T>) -> Self {
215+
// SAFETY: A Unique pointer cannot be null, so the conditions for
216+
// new_unchecked() are respected.
208217
unsafe { NonNull::new_unchecked(unique.as_ptr()) }
209218
}
210219
}
@@ -213,6 +222,7 @@ impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
213222
impl<T: ?Sized> From<&mut T> for NonNull<T> {
214223
#[inline]
215224
fn from(reference: &mut T) -> Self {
225+
// SAFETY: A mutable reference cannot be null.
216226
unsafe { NonNull { pointer: reference as *mut T } }
217227
}
218228
}
@@ -221,6 +231,8 @@ impl<T: ?Sized> From<&mut T> for NonNull<T> {
221231
impl<T: ?Sized> From<&T> for NonNull<T> {
222232
#[inline]
223233
fn from(reference: &T) -> Self {
234+
// SAFETY: A reference cannot be null, so the conditions for
235+
// new_unchecked() are respected.
224236
unsafe { NonNull { pointer: reference as *const T } }
225237
}
226238
}

src/libcore/ptr/unique.rs

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ impl<T: Sized> Unique<T> {
7474
// FIXME: rename to dangling() to match NonNull?
7575
#[inline]
7676
pub const fn empty() -> Self {
77+
// SAFETY: mem::align_of() returns a valid, non-null pointer. The
78+
// conditions to call new_unchecked() are thus respected.
7779
unsafe { Unique::new_unchecked(mem::align_of::<T>() as *mut T) }
7880
}
7981
}
@@ -94,6 +96,7 @@ impl<T: ?Sized> Unique<T> {
9496
#[inline]
9597
pub fn new(ptr: *mut T) -> Option<Self> {
9698
if !ptr.is_null() {
99+
// SAFETY: The pointer has already been checked and is not null.
97100
Some(unsafe { Unique { pointer: ptr as _, _marker: PhantomData } })
98101
} else {
99102
None
@@ -129,6 +132,9 @@ impl<T: ?Sized> Unique<T> {
129132
/// Casts to a pointer of another type.
130133
#[inline]
131134
pub const fn cast<U>(self) -> Unique<U> {
135+
// SAFETY: Unique::new_unchecked() creates a new unique and needs
136+
// the given pointer to not be null.
137+
// Since we are passing self as a pointer, it cannot be null.
132138
unsafe { Unique::new_unchecked(self.as_ptr() as *mut U) }
133139
}
134140
}
@@ -168,6 +174,7 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
168174
impl<T: ?Sized> From<&mut T> for Unique<T> {
169175
#[inline]
170176
fn from(reference: &mut T) -> Self {
177+
// SAFETY: A mutable reference cannot be null
171178
unsafe { Unique { pointer: reference as *mut T, _marker: PhantomData } }
172179
}
173180
}

0 commit comments

Comments
 (0)