Skip to content

Commit d8c235d

Browse files
authored
Rollup merge of rust-lang#71063 - LeSeulArtichaut:document-unsafe, r=Mark-Simulacrum
Document unsafety in core::{option, hash} Helps with rust-lang#66219. I think that the part that will need reviewing the most is the `hash/sip.rs` file. r? @LukasKalbertodt (or someone else from the libs team)
2 parents 7a282eb + a694315 commit d8c235d

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/libcore/hash/mod.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@
7979
//! }
8080
//! ```
8181
82-
// ignore-tidy-undocumented-unsafe
83-
8482
#![stable(feature = "rust1", since = "1.0.0")]
8583

8684
use crate::fmt;
@@ -572,6 +570,10 @@ mod impls {
572570
fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
573571
let newlen = data.len() * mem::size_of::<$ty>();
574572
let ptr = data.as_ptr() as *const u8;
573+
// SAFETY: `ptr` is valid and aligned, as this macro is only used
574+
// for numeric primitives which have no padding. The new slice only
575+
// spans across `data` and is never mutated, and its total size is the
576+
// same as the original `data` so it can't be over `isize::MAX`.
575577
state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
576578
}
577579
}
@@ -691,6 +693,11 @@ mod impls {
691693
state.write_usize(*self as *const () as usize);
692694
} else {
693695
// Fat pointer
696+
// SAFETY: we are accessing the memory occupied by `self`
697+
// which is guaranteed to be valid.
698+
// This assumes a fat pointer can be represented by a `(usize, usize)`,
699+
// which is safe to do in `std` because it is shipped and kept in sync
700+
// with the implementation of fat pointers in `rustc`.
694701
let (a, b) = unsafe { *(self as *const Self as *const (usize, usize)) };
695702
state.write_usize(a);
696703
state.write_usize(b);
@@ -706,6 +713,11 @@ mod impls {
706713
state.write_usize(*self as *const () as usize);
707714
} else {
708715
// Fat pointer
716+
// SAFETY: we are accessing the memory occupied by `self`
717+
// which is guaranteed to be valid.
718+
// This assumes a fat pointer can be represented by a `(usize, usize)`,
719+
// which is safe to do in `std` because it is shipped and kept in sync
720+
// with the implementation of fat pointers in `rustc`.
709721
let (a, b) = unsafe { *(self as *const Self as *const (usize, usize)) };
710722
state.write_usize(a);
711723
state.write_usize(b);

src/libcore/hash/sip.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! An implementation of SipHash.
22
3-
// ignore-tidy-undocumented-unsafe
4-
53
#![allow(deprecated)] // the types in this module are deprecated
64

75
use crate::cmp;
@@ -265,6 +263,7 @@ impl<S: Sip> super::Hasher for Hasher<S> {
265263

266264
if self.ntail != 0 {
267265
needed = 8 - self.ntail;
266+
// SAFETY: `cmp::min(length, needed)` is guaranteed to not be over `length`
268267
self.tail |= unsafe { u8to64_le(msg, 0, cmp::min(length, needed)) } << (8 * self.ntail);
269268
if length < needed {
270269
self.ntail += length;
@@ -279,10 +278,13 @@ impl<S: Sip> super::Hasher for Hasher<S> {
279278

280279
// Buffered tail is now flushed, process new input.
281280
let len = length - needed;
282-
let left = len & 0x7;
281+
let left = len & 0x7; // len % 8
283282

284283
let mut i = needed;
285284
while i < len - left {
285+
// SAFETY: because `len - left` is the biggest multiple of 8 under
286+
// `len`, and because `i` starts at `needed` where `len` is `length - needed`,
287+
// `i + 8` is guaranteed to be less than or equal to `length`.
286288
let mi = unsafe { load_int_le!(msg, i, u64) };
287289

288290
self.state.v3 ^= mi;
@@ -292,6 +294,9 @@ impl<S: Sip> super::Hasher for Hasher<S> {
292294
i += 8;
293295
}
294296

297+
// SAFETY: `i` is now `needed + len.div_euclid(8) * 8`,
298+
// so `i + left` = `needed + len` = `length`, which is by
299+
// definition equal to `msg.len()`.
295300
self.tail = unsafe { u8to64_le(msg, i, left) };
296301
self.ntail = left;
297302
}

src/libcore/option.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@
133133
//! [`Box<T>`]: ../../std/boxed/struct.Box.html
134134
//! [`i32`]: ../../std/primitive.i32.html
135135
136-
// ignore-tidy-undocumented-unsafe
137-
138136
#![stable(feature = "rust1", since = "1.0.0")]
139137

140138
use crate::iter::{FromIterator, FusedIterator, TrustedLen};
@@ -301,6 +299,8 @@ impl<T> Option<T> {
301299
#[inline]
302300
#[stable(feature = "pin", since = "1.33.0")]
303301
pub fn as_pin_ref(self: Pin<&Self>) -> Option<Pin<&T>> {
302+
// SAFETY: `x` is guaranteed to be pinned because it comes from `self`
303+
// which is pinned.
304304
unsafe { Pin::get_ref(self).as_ref().map(|x| Pin::new_unchecked(x)) }
305305
}
306306

@@ -310,6 +310,8 @@ impl<T> Option<T> {
310310
#[inline]
311311
#[stable(feature = "pin", since = "1.33.0")]
312312
pub fn as_pin_mut(self: Pin<&mut Self>) -> Option<Pin<&mut T>> {
313+
// SAFETY: `get_unchecked_mut` is never used to move the `Option` inside `self`.
314+
// `x` is guaranteed to be pinned because it comes from `self` which is pinned.
313315
unsafe { Pin::get_unchecked_mut(self).as_mut().map(|x| Pin::new_unchecked(x)) }
314316
}
315317

@@ -858,6 +860,8 @@ impl<T> Option<T> {
858860

859861
match *self {
860862
Some(ref mut v) => v,
863+
// SAFETY: a `None` variant for `self` would have been replaced by a `Some`
864+
// variant in the code above.
861865
None => unsafe { hint::unreachable_unchecked() },
862866
}
863867
}

0 commit comments

Comments
 (0)