Skip to content

Commit b5aed96

Browse files
fix formatting of safety comments
All safety comments must start with "SAFETY:". We are not adding safety comments in tests and in the benchmarks. Signed-off-by: Andreea Florescu <[email protected]>
1 parent 668b723 commit b5aed96

File tree

9 files changed

+143
-124
lines changed

9 files changed

+143
-124
lines changed

benches/mmap/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
44
#![cfg(feature = "backend-mmap")]
5+
#![allow(clippy::undocumented_unsafe_blocks)]
56

67
extern crate criterion;
78
extern crate vm_memory;

src/atomic_integer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub unsafe trait AtomicInteger: Sync + Send {
2525

2626
macro_rules! impl_atomic_integer_ops {
2727
($T:path, $V:ty) => {
28+
// SAFETY: This is safe as long as T is an Atomic type.
29+
// This is a helper macro for generating the implementation for common
30+
// Atomic types.
2831
unsafe impl AtomicInteger for $T {
2932
type V = $V;
3033

src/bitmap/backend/atomic_bitmap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl NewBitmap for AtomicBitmap {
147147

148148
#[cfg(unix)]
149149
{
150-
// There's no unsafe potential in calling this function.
150+
// SAFETY: There's no unsafe potential in calling this function.
151151
page_size = unsafe { libc::sysconf(libc::_SC_PAGE_SIZE) };
152152
}
153153

src/bytes.rs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ pub unsafe trait ByteValued: Copy + Default + Send + Sync {
4444
return None;
4545
}
4646

47-
// Safe because the ByteValued trait asserts any data is valid for this type, and we ensured
48-
// the size of the pointer's buffer is the correct size. The `align_to` method ensures that
49-
// we don't have any unaligned references. This aliases a pointer, but because the pointer
50-
// is from a const slice reference, there are no mutable aliases. Finally, the reference
51-
// returned can not outlive data because they have equal implicit lifetime constraints.
47+
// SAFETY: Safe because the ByteValued trait asserts any data is valid for this type, and
48+
// we ensured the size of the pointer's buffer is the correct size. The `align_to` method
49+
// ensures that we don't have any unaligned references. This aliases a pointer, but because
50+
// the pointer is from a const slice reference, there are no mutable aliases. Finally, the
51+
// reference returned can not outlive data because they have equal implicit lifetime
52+
// constraints.
5253
match unsafe { data.align_to::<Self>() } {
5354
([], [mid], []) => Some(mid),
5455
_ => None,
@@ -69,12 +70,12 @@ pub unsafe trait ByteValued: Copy + Default + Send + Sync {
6970
return None;
7071
}
7172

72-
// Safe because the ByteValued trait asserts any data is valid for this type, and we ensured
73-
// the size of the pointer's buffer is the correct size. The `align_to` method ensures that
74-
// we don't have any unaligned references. This aliases a pointer, but because the pointer
75-
// is from a mut slice reference, we borrow the passed in mutable reference. Finally, the
76-
// reference returned can not outlive data because they have equal implicit lifetime
77-
// constraints.
73+
// SAFETY: Safe because the ByteValued trait asserts any data is valid for this type, and
74+
// we ensured the size of the pointer's buffer is the correct size. The `align_to` method
75+
// ensures that we don't have any unaligned references. This aliases a pointer, but because
76+
// the pointer is from a mut slice reference, we borrow the passed in mutable reference.
77+
// Finally, the reference returned can not outlive data because they have equal implicit
78+
// lifetime constraints.
7879
match unsafe { data.align_to_mut::<Self>() } {
7980
([], [mid], []) => Some(mid),
8081
_ => None,
@@ -87,9 +88,9 @@ pub unsafe trait ByteValued: Copy + Default + Send + Sync {
8788
/// The value of bytes in the returned slice will depend on the representation of the type in
8889
/// memory, and may change in an unstable fashion.
8990
fn as_slice(&self) -> &[u8] {
90-
// Safe because the entire size of self is accessible as bytes because the trait guarantees
91-
// it. The lifetime of the returned slice is the same as the passed reference, so that no
92-
// dangling pointers will result from this pointer alias.
91+
// SAFETY: Safe because the entire size of self is accessible as bytes because the trait
92+
// guarantees it. The lifetime of the returned slice is the same as the passed reference,
93+
// so that no dangling pointers will result from this pointer alias.
9394
unsafe { from_raw_parts(self as *const Self as *const u8, size_of::<Self>()) }
9495
}
9596

@@ -99,12 +100,12 @@ pub unsafe trait ByteValued: Copy + Default + Send + Sync {
99100
/// immediately reflected in `self`. The value of bytes in the returned slice will depend on
100101
/// the representation of the type in memory, and may change in an unstable fashion.
101102
fn as_mut_slice(&mut self) -> &mut [u8] {
102-
// Safe because the entire size of self is accessible as bytes because the trait guarantees
103-
// it. The trait also guarantees that any combination of bytes is valid for this type, so
104-
// modifying them in the form of a byte slice is valid. The lifetime of the returned slice
105-
// is the same as the passed reference, so that no dangling pointers will result from this
106-
// pointer alias. Although this does alias a mutable pointer, we do so by exclusively
107-
// borrowing the given mutable reference.
103+
// SAFETY: Safe because the entire size of self is accessible as bytes because the trait
104+
// guarantees it. The trait also guarantees that any combination of bytes is valid for this
105+
// type, so modifying them in the form of a byte slice is valid. The lifetime of the
106+
// returned slice is the same as the passed reference, so that no dangling pointers will
107+
// result from this pointer alias. Although this does alias a mutable pointer, we do so by
108+
// exclusively borrowing the given mutable reference.
108109
unsafe { from_raw_parts_mut(self as *mut Self as *mut u8, size_of::<Self>()) }
109110
}
110111

@@ -118,24 +119,25 @@ pub unsafe trait ByteValued: Copy + Default + Send + Sync {
118119
/// that all accesses to `self` use volatile accesses (because there can
119120
/// be no other accesses).
120121
fn as_bytes(&mut self) -> VolatileSlice {
121-
unsafe {
122-
// This is safe because the lifetime is the same as self
123-
VolatileSlice::new(self as *mut Self as usize as *mut _, size_of::<Self>())
124-
}
122+
// SAFETY: This is safe because the lifetime is the same as self
123+
unsafe { VolatileSlice::new(self as *mut Self as usize as *mut _, size_of::<Self>()) }
125124
}
126125
}
127126

128-
// All intrinsic types and arrays of intrinsic types are ByteValued. They are just numbers.
129127
macro_rules! byte_valued_array {
130128
($T:ty, $($N:expr)+) => {
131129
$(
130+
// SAFETY: All intrinsic types and arrays of intrinsic types are ByteValued.
131+
// They are just numbers.
132132
unsafe impl ByteValued for [$T; $N] {}
133133
)+
134134
}
135135
}
136136

137137
macro_rules! byte_valued_type {
138138
($T:ty) => {
139+
// SAFETY: Safe as long T is POD.
140+
// We are using this macro to generated the implementation for integer types below.
139141
unsafe impl ByteValued for $T {}
140142
byte_valued_array! {
141143
$T,
@@ -329,6 +331,7 @@ pub trait Bytes<A> {
329331

330332
#[cfg(test)]
331333
pub(crate) mod tests {
334+
#![allow(clippy::undocumented_unsafe_blocks)]
332335
use super::*;
333336

334337
use std::fmt::Debug;

src/endian.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ macro_rules! endian_type {
6363
}
6464
}
6565

66+
// SAFETY: Safe because we are using this for implementing ByteValued for endian types
67+
// which are POD.
6668
unsafe impl ByteValued for $new_type {}
6769

6870
impl PartialEq<$old_type> for $new_type {
@@ -102,6 +104,7 @@ endian_type!(usize, BeSize, to_be, from_be);
102104

103105
#[cfg(test)]
104106
mod tests {
107+
#![allow(clippy::undocumented_unsafe_blocks)]
105108
use super::*;
106109

107110
use std::convert::From;

src/guest_memory.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
848848
self.try_access(count, addr, |offset, len, caddr, region| -> Result<usize> {
849849
// Check if something bad happened before doing unsafe things.
850850
assert!(offset <= count);
851+
// SAFETY: Safe because we are checking the offset.
851852
if let Some(dst) = unsafe { region.as_mut_slice() } {
852853
// This is safe cause `start` and `len` are within the `region`, and we manually
853854
// record the dirty status of the written range below.
@@ -935,6 +936,7 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
935936
self.try_access(count, addr, |offset, len, caddr, region| -> Result<usize> {
936937
// Check if something bad happened before doing unsafe things.
937938
assert!(offset <= count);
939+
// SAFETY: Safe because we are checking the offset is valid.
938940
if let Some(src) = unsafe { region.as_slice() } {
939941
// This is safe cause `start` and `len` are within the `region`.
940942
let start = caddr.raw_value() as usize;
@@ -1024,6 +1026,7 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
10241026

10251027
#[cfg(test)]
10261028
mod tests {
1029+
#![allow(clippy::undocumented_unsafe_blocks)]
10271030
use super::*;
10281031
#[cfg(feature = "backend-mmap")]
10291032
use crate::bytes::ByteValued;

src/mmap.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ impl<B: Bitmap + 'static> GuestMemory for GuestMemoryMmap<B> {
670670

671671
#[cfg(test)]
672672
mod tests {
673+
#![allow(clippy::undocumented_unsafe_blocks)]
673674
extern crate vmm_sys_util;
674675

675676
use super::*;

src/mmap_unix.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ impl<B: Bitmap> MmapRegionBuilder<B> {
165165
(-1, 0)
166166
};
167167

168-
// This is safe because we're not allowing MAP_FIXED, and invalid parameters cannot break
169-
// Rust safety guarantees (things may change if we're mapping /dev/mem or some wacky file).
168+
// SAFETY: This is safe because we're not allowing MAP_FIXED, and invalid parameters
169+
// cannot break Rust safety guarantees (things may change if we're mapping /dev/mem or
170+
// some wacky file).
170171
let addr = unsafe {
171172
libc::mmap(
172173
null_mut(),
@@ -195,7 +196,8 @@ impl<B: Bitmap> MmapRegionBuilder<B> {
195196
}
196197

197198
fn build_raw(self) -> Result<MmapRegion<B>> {
198-
// Safe because this call just returns the page size and doesn't have any side effects.
199+
// SAFETY: Safe because this call just returns the page size and doesn't have any side
200+
// effects.
199201
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize;
200202
let addr = self.raw_ptr.unwrap();
201203

@@ -238,11 +240,12 @@ pub struct MmapRegion<B = ()> {
238240
hugetlbfs: Option<bool>,
239241
}
240242

241-
// Send and Sync aren't automatically inherited for the raw address pointer.
243+
// SAFETY: Send and Sync aren't automatically inherited for the raw address pointer.
242244
// Accessing that pointer is only done through the stateless interface which
243245
// allows the object to be shared by multiple threads without a decrease in
244246
// safety.
245247
unsafe impl<B: Send> Send for MmapRegion<B> {}
248+
// SAFETY: See comment above.
246249
unsafe impl<B: Sync> Sync for MmapRegion<B> {}
247250

248251
impl<B: NewBitmap> MmapRegion<B> {
@@ -399,16 +402,16 @@ impl<B: Bitmap> MmapRegion<B> {
399402
}
400403

401404
impl<B> AsSlice for MmapRegion<B> {
405+
// SAFETY: This is safe because we mapped the area at addr ourselves, so this slice will not
406+
// overflow. However, it is possible to alias.
402407
unsafe fn as_slice(&self) -> &[u8] {
403-
// This is safe because we mapped the area at addr ourselves, so this slice will not
404-
// overflow. However, it is possible to alias.
405408
std::slice::from_raw_parts(self.addr, self.size)
406409
}
407410

411+
// SAFETY: This is safe because we mapped the area at addr ourselves, so this slice will not
412+
// overflow. However, it is possible to alias.
408413
#[allow(clippy::mut_from_ref)]
409414
unsafe fn as_mut_slice(&self) -> &mut [u8] {
410-
// This is safe because we mapped the area at addr ourselves, so this slice will not
411-
// overflow. However, it is possible to alias.
412415
std::slice::from_raw_parts_mut(self.addr, self.size)
413416
}
414417
}
@@ -430,23 +433,25 @@ impl<B: Bitmap> VolatileMemory for MmapRegion<B> {
430433
return Err(volatile_memory::Error::OutOfBounds { addr: end });
431434
}
432435

433-
// Safe because we checked that offset + count was within our range and we only ever hand
434-
// out volatile accessors.
435-
Ok(unsafe {
436-
VolatileSlice::with_bitmap(
437-
(self.addr as usize + offset) as *mut _,
438-
count,
439-
self.bitmap.slice_at(offset),
440-
)
441-
})
436+
Ok(
437+
// SAFETY: Safe because we checked that offset + count was within our range and we only
438+
// ever hand out volatile accessors.
439+
unsafe {
440+
VolatileSlice::with_bitmap(
441+
(self.addr as usize + offset) as *mut _,
442+
count,
443+
self.bitmap.slice_at(offset),
444+
)
445+
},
446+
)
442447
}
443448
}
444449

445450
impl<B> Drop for MmapRegion<B> {
446451
fn drop(&mut self) {
447-
// This is safe because we mmap the area at addr ourselves, and nobody
448-
// else is holding a reference to it.
449452
if self.owned {
453+
// SAFETY: This is safe because we mmap the area at addr ourselves, and nobody
454+
// else is holding a reference to it.
450455
unsafe {
451456
libc::munmap(self.addr as *mut libc::c_void, self.size);
452457
}
@@ -456,6 +461,7 @@ impl<B> Drop for MmapRegion<B> {
456461

457462
#[cfg(test)]
458463
mod tests {
464+
#![allow(clippy::undocumented_unsafe_blocks)]
459465
use super::*;
460466

461467
use std::io::Write;

0 commit comments

Comments
 (0)