Skip to content

Commit d78c056

Browse files
committed
rust-lang#66219 documented unsafe in core::{alloc, hash}
1 parent 5797593 commit d78c056

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

src/libcore/alloc.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Memory allocation APIs
22
3-
// ignore-tidy-undocumented-unsafe
4-
53
#![stable(feature = "alloc_module", since = "1.28.0")]
64

75
use crate::cmp;
@@ -88,6 +86,7 @@ impl Layout {
8886
return Err(LayoutErr { private: () });
8987
}
9088

89+
// SAFETY: performed checks above
9190
unsafe {
9291
Ok(Layout::from_size_align_unchecked(size, align))
9392
}
@@ -120,11 +119,11 @@ impl Layout {
120119
#[inline]
121120
pub fn new<T>() -> Self {
122121
let (size, align) = size_align::<T>();
123-
// Note that the align is guaranteed by rustc to be a power of two and
122+
debug_assert!(Layout::from_size_align(size, align).is_ok());
123+
// SAFETY: Note that the align is guaranteed by rustc to be a power of two and
124124
// the size+align combo is guaranteed to fit in our address space. As a
125125
// result use the unchecked constructor here to avoid inserting code
126126
// that panics if it isn't optimized well enough.
127-
debug_assert!(Layout::from_size_align(size, align).is_ok());
128127
unsafe {
129128
Layout::from_size_align_unchecked(size, align)
130129
}
@@ -137,8 +136,8 @@ impl Layout {
137136
#[inline]
138137
pub fn for_value<T: ?Sized>(t: &T) -> Self {
139138
let (size, align) = (mem::size_of_val(t), mem::align_of_val(t));
140-
// See rationale in `new` for why this us using an unsafe variant below
141139
debug_assert!(Layout::from_size_align(size, align).is_ok());
140+
// SAFETY: See rationale in `new` for why this us using an unsafe variant below
142141
unsafe {
143142
Layout::from_size_align_unchecked(size, align)
144143
}
@@ -243,9 +242,9 @@ impl Layout {
243242
let alloc_size = padded_size.checked_mul(n)
244243
.ok_or(LayoutErr { private: () })?;
245244

245+
// SAFETY: self.align is already known to be valid and alloc_size has been
246+
// padded already.
246247
unsafe {
247-
// self.align is already known to be valid and alloc_size has been
248-
// padded already.
249248
Ok((Layout::from_size_align_unchecked(alloc_size, self.align()), padded_size))
250249
}
251250
}
@@ -1074,6 +1073,7 @@ pub unsafe trait Alloc {
10741073
{
10751074
let k = Layout::new::<T>();
10761075
if k.size() > 0 {
1076+
// SAFETY: layout has nonzero size
10771077
unsafe { self.alloc(k).map(|p| p.cast()) }
10781078
} else {
10791079
Err(AllocErr)
@@ -1143,6 +1143,7 @@ pub unsafe trait Alloc {
11431143
{
11441144
match Layout::array::<T>(n) {
11451145
Ok(ref layout) if layout.size() > 0 => {
1146+
// SAFETY: layout has nonzero size
11461147
unsafe {
11471148
self.alloc(layout.clone()).map(|p| p.cast())
11481149
}

src/libcore/hash/mod.rs

Lines changed: 4 additions & 4 deletions
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;
@@ -569,6 +567,8 @@ mod impls {
569567
fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
570568
let newlen = data.len() * mem::size_of::<$ty>();
571569
let ptr = data.as_ptr() as *const u8;
570+
// SAFETY: all of the requirements for from_raw_parts are guaranteed since
571+
// data is a slice
572572
state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
573573
}
574574
}
@@ -688,7 +688,7 @@ mod impls {
688688
// Thin pointer
689689
state.write_usize(*self as *const () as usize);
690690
} else {
691-
// Fat pointer
691+
// SAFETY: since it's not a thin pointer, it's a fat pointer
692692
let (a, b) = unsafe {
693693
*(self as *const Self as *const (usize, usize))
694694
};
@@ -705,7 +705,7 @@ mod impls {
705705
// Thin pointer
706706
state.write_usize(*self as *const () as usize);
707707
} else {
708-
// Fat pointer
708+
// SAFETY: since it's not a thin pointer, it's a fat pointer
709709
let (a, b) = unsafe {
710710
*(self as *const Self as *const (usize, usize))
711711
};

src/libcore/hash/sip.rs

Lines changed: 7 additions & 2 deletions
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::marker::PhantomData;
@@ -222,8 +220,10 @@ impl<S: Sip> Hasher<S> {
222220
let needed = 8 - self.ntail;
223221
let fill = cmp::min(length, needed);
224222
if fill == 8 {
223+
// SAFETY: msg has exactly sizeof(u64) == 8
225224
self.tail = unsafe { load_int_le!(msg, 0, u64) };
226225
} else {
226+
// SAFETY: fill < 7
227227
self.tail |= unsafe { u8to64_le(msg, 0, fill) } << (8 * self.ntail);
228228
if length < needed {
229229
self.ntail += length;
@@ -236,6 +236,7 @@ impl<S: Sip> Hasher<S> {
236236

237237
// Buffered tail is now flushed, process new input.
238238
self.ntail = length - needed;
239+
// SAFETY: self.ntail + needed - 1 = length - 1 < 8
239240
self.tail = unsafe { u8to64_le(msg, needed, self.ntail) };
240241
}
241242
}
@@ -270,6 +271,7 @@ impl<S: Sip> super::Hasher for Hasher<S> {
270271
// see short_write comment for explanation
271272
#[inline]
272273
fn write_usize(&mut self, i: usize) {
274+
// SAFETY: bytes leaves scope as i does
273275
let bytes = unsafe {
274276
crate::slice::from_raw_parts(&i as *const usize as *const u8, mem::size_of::<usize>())
275277
};
@@ -291,6 +293,7 @@ impl<S: Sip> super::Hasher for Hasher<S> {
291293

292294
if self.ntail != 0 {
293295
needed = 8 - self.ntail;
296+
// SAFETY: needed < 8 since self.ntail != 0
294297
self.tail |= unsafe { u8to64_le(msg, 0, cmp::min(length, needed)) } << 8 * self.ntail;
295298
if length < needed {
296299
self.ntail += length;
@@ -309,6 +312,7 @@ impl<S: Sip> super::Hasher for Hasher<S> {
309312

310313
let mut i = needed;
311314
while i < len - left {
315+
// SAFETY: i + 8 <= length
312316
let mi = unsafe { load_int_le!(msg, i, u64) };
313317

314318
self.state.v3 ^= mi;
@@ -318,6 +322,7 @@ impl<S: Sip> super::Hasher for Hasher<S> {
318322
i += 8;
319323
}
320324

325+
// SAFETY: left < 8
321326
self.tail = unsafe { u8to64_le(msg, i, left) };
322327
self.ntail = left;
323328
}

0 commit comments

Comments
 (0)