Skip to content

Commit 21b9ed1

Browse files
andreeaflorescujiangliu
authored andcommitted
add safety suffix to safety comments
Also added safety comments where none were already added. For tests, we ignore this requirement as in tests we might be doing unsafe things on purpose and it just adds significant overhead to comment all the blocks. Signed-off-by: Andreea Florescu <[email protected]>
1 parent b609232 commit 21b9ed1

File tree

6 files changed

+137
-119
lines changed

6 files changed

+137
-119
lines changed

src/ioctls/device.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl DeviceFd {
2424
///
2525
/// * `device_attr` - The device attribute to be tested. `addr` field is ignored.
2626
pub fn has_device_attr(&self, device_attr: &kvm_device_attr) -> Result<()> {
27+
// SAFETY: We are calling this function with a Device fd, and we trust the kernel.
2728
let ret = unsafe { ioctl_with_ref(self, KVM_HAS_DEVICE_ATTR(), device_attr) };
2829
if ret != 0 {
2930
return Err(errno::Error::last());
@@ -73,6 +74,7 @@ impl DeviceFd {
7374
/// }
7475
/// ```
7576
pub fn set_device_attr(&self, device_attr: &kvm_device_attr) -> Result<()> {
77+
// SAFETY: We are calling this function with a Device fd, and we trust the kernel.
7678
let ret = unsafe { ioctl_with_ref(self, KVM_SET_DEVICE_ATTR(), device_attr) };
7779
if ret != 0 {
7880
return Err(errno::Error::last());
@@ -142,6 +144,7 @@ impl DeviceFd {
142144
/// }
143145
/// ```
144146
pub fn get_device_attr(&self, device_attr: &mut kvm_device_attr) -> Result<()> {
147+
// SAFETY: We are calling this function with a Device fd, and we trust the kernel.
145148
let ret = unsafe { ioctl_with_mut_ref(self, KVM_GET_DEVICE_ATTR(), device_attr) };
146149
if ret != 0 {
147150
return Err(errno::Error::last());
@@ -177,6 +180,7 @@ impl FromRawFd for DeviceFd {
177180

178181
#[cfg(test)]
179182
mod tests {
183+
#![allow(clippy::undocumented_unsafe_blocks)]
180184
use super::*;
181185
use crate::ioctls::system::Kvm;
182186
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]

src/ioctls/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ pub struct KvmRunWrapper {
3737
mmap_size: usize,
3838
}
3939

40-
// Send and Sync aren't automatically inherited for the raw address pointer.
40+
// SAFETY: Send and Sync aren't automatically inherited for the raw address pointer.
4141
// Accessing that pointer is only done through the stateless interface which
4242
// allows the object to be shared by multiple threads without a decrease in
4343
// safety.
4444
unsafe impl Send for KvmRunWrapper {}
45+
// SAFETY: See above.
4546
unsafe impl Sync for KvmRunWrapper {}
4647

4748
impl KvmRunWrapper {
@@ -51,8 +52,8 @@ impl KvmRunWrapper {
5152
/// * `fd` - File descriptor to mmap from.
5253
/// * `size` - Size of memory region in bytes.
5354
pub fn mmap_from_fd(fd: &dyn AsRawFd, size: usize) -> Result<KvmRunWrapper> {
54-
// This is safe because we are creating a mapping in a place not already used by any other
55-
// area in this process.
55+
// SAFETY: This is safe because we are creating a mapping in a place not already used by
56+
// any other area in this process.
5657
let addr = unsafe {
5758
libc::mmap(
5859
null_mut(),
@@ -76,9 +77,9 @@ impl KvmRunWrapper {
7677
/// Returns a mutable reference to `kvm_run`.
7778
#[allow(clippy::mut_from_ref)]
7879
pub fn as_mut_ref(&self) -> &mut kvm_run {
79-
// Safe because we know we mapped enough memory to hold the kvm_run struct because the
80-
// kernel told us how large it was.
8180
#[allow(clippy::cast_ptr_alignment)]
81+
// SAFETY: Safe because we know we mapped enough memory to hold the kvm_run struct because
82+
// the kernel told us how large it was.
8283
unsafe {
8384
&mut *(self.kvm_run_ptr as *mut kvm_run)
8485
}
@@ -87,7 +88,7 @@ impl KvmRunWrapper {
8788

8889
impl Drop for KvmRunWrapper {
8990
fn drop(&mut self) {
90-
// This is safe because we mmap the area at kvm_run_ptr ourselves,
91+
// SAFETY: This is safe because we mmap the area at kvm_run_ptr ourselves,
9192
// and nobody else is holding a reference to it.
9293
unsafe {
9394
libc::munmap(self.kvm_run_ptr as *mut libc::c_void, self.mmap_size);

src/ioctls/system.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ impl Kvm {
4141
pub fn new() -> Result<Self> {
4242
// Open `/dev/kvm` using `O_CLOEXEC` flag.
4343
let fd = Self::open_with_cloexec(true)?;
44-
// Safe because we verify that the fd is valid in `open_with_cloexec` and we own the fd.
44+
// SAFETY: Safe because we verify that the fd is valid in `open_with_cloexec` and we own
45+
// the fd.
4546
Ok(unsafe { Self::from_raw_fd(fd) })
4647
}
4748

@@ -68,7 +69,7 @@ impl Kvm {
6869
/// ```
6970
pub fn open_with_cloexec(close_on_exec: bool) -> Result<RawFd> {
7071
let open_flags = O_RDWR | if close_on_exec { O_CLOEXEC } else { 0 };
71-
// Safe because we give a constant nul-terminated string and verify the result.
72+
// SAFETY: Safe because we give a constant nul-terminated string and verify the result.
7273
let ret = unsafe { open("/dev/kvm\0".as_ptr() as *const c_char, open_flags) };
7374
if ret < 0 {
7475
Err(errno::Error::last())
@@ -89,8 +90,8 @@ impl Kvm {
8990
/// assert_eq!(kvm.get_api_version(), 12);
9091
/// ```
9192
pub fn get_api_version(&self) -> i32 {
92-
// Safe because we know that our file is a KVM fd and that the request is one of the ones
93-
// defined by kernel.
93+
// SAFETY: Safe because we know that our file is a KVM fd and that the request is one of
94+
// the ones defined by kernel.
9495
unsafe { ioctl(self, KVM_GET_API_VERSION()) }
9596
}
9697

@@ -137,8 +138,8 @@ impl Kvm {
137138
/// assert!(kvm.check_extension_int(Cap::MaxVcpuId) > 0);
138139
/// ```
139140
pub fn check_extension_int(&self, c: Cap) -> i32 {
140-
// Safe because we know that our file is a KVM fd and that the extension is one of the ones
141-
// defined by kernel.
141+
// SAFETY: Safe because we know that our file is a KVM fd and that the extension is one of
142+
// the ones defined by kernel.
142143
unsafe { ioctl_with_val(self, KVM_CHECK_EXTENSION(), c as c_ulong) }
143144
}
144145

@@ -177,7 +178,7 @@ impl Kvm {
177178
/// assert!(kvm.get_vcpu_mmap_size().unwrap() > 0);
178179
/// ```
179180
pub fn get_vcpu_mmap_size(&self) -> Result<usize> {
180-
// Safe because we know that our file is a KVM fd and we verify the return result.
181+
// SAFETY: Safe because we know that our file is a KVM fd and we verify the return result.
181182
let res = unsafe { ioctl(self, KVM_GET_VCPU_MMAP_SIZE()) };
182183
if res > 0 {
183184
Ok(res as usize)
@@ -279,11 +280,10 @@ impl Kvm {
279280
}
280281

281282
let mut cpuid = CpuId::new(num_entries).map_err(|_| errno::Error::new(libc::ENOMEM))?;
282-
283+
// SAFETY: The kernel is trusted not to write beyond the bounds of the memory
284+
// allocated for the struct. The limit is read from nent, which is set to the allocated
285+
// size(num_entries) above.
283286
let ret = unsafe {
284-
// ioctl is unsafe. The kernel is trusted not to write beyond the bounds of the memory
285-
// allocated for the struct. The limit is read from nent, which is set to the allocated
286-
// size(num_entries) above.
287287
ioctl_with_mut_ptr(self, kind, cpuid.as_mut_fam_struct_ptr())
288288
};
289289
if ret < 0 {
@@ -368,10 +368,10 @@ impl Kvm {
368368
let mut msr_list =
369369
MsrList::new(KVM_MAX_MSR_ENTRIES).map_err(|_| errno::Error::new(libc::ENOMEM))?;
370370

371+
// SAFETY: The kernel is trusted not to write beyond the bounds of the memory
372+
// allocated for the struct. The limit is read from nmsrs, which is set to the allocated
373+
// size (MAX_KVM_MSR_ENTRIES) above.
371374
let ret = unsafe {
372-
// ioctl is unsafe. The kernel is trusted not to write beyond the bounds of the memory
373-
// allocated for the struct. The limit is read from nmsrs, which is set to the allocated
374-
// size (MAX_KVM_MSR_ENTRIES) above.
375375
ioctl_with_mut_ptr(
376376
self,
377377
KVM_GET_MSR_INDEX_LIST(),
@@ -483,11 +483,11 @@ impl Kvm {
483483
/// assert!(vm.run_size() == kvm.get_vcpu_mmap_size().unwrap());
484484
/// ```
485485
pub fn create_vm_with_type(&self, vm_type: u64) -> Result<VmFd> {
486-
// Safe because we know `self.kvm` is a real KVM fd as this module is the only one that
487-
// create Kvm objects.
486+
// SAFETY: Safe because we know `self.kvm` is a real KVM fd as this module is the only one
487+
// that create Kvm objects.
488488
let ret = unsafe { ioctl_with_val(&self.kvm, KVM_CREATE_VM(), vm_type) };
489489
if ret >= 0 {
490-
// Safe because we verify the value of ret and we are the owners of the fd.
490+
// SAFETY: Safe because we verify the value of ret and we are the owners of the fd.
491491
let vm_file = unsafe { File::from_raw_fd(ret) };
492492
let run_mmap_size = self.get_vcpu_mmap_size()?;
493493
Ok(new_vmfd(vm_file, run_mmap_size))
@@ -572,6 +572,7 @@ impl FromRawFd for Kvm {
572572

573573
#[cfg(test)]
574574
mod tests {
575+
#![allow(clippy::undocumented_unsafe_blocks)]
575576
use super::*;
576577
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
577578
use kvm_bindings::KVM_MAX_CPUID_ENTRIES;

0 commit comments

Comments
 (0)