Skip to content

Commit c346b17

Browse files
lauraltandreeaflorescu
authored andcommitted
fix clippy warnings
A more recent clippy version requires the public unsafe functions to have a #Safety section in their documentation. Fixes: #101. Signed-off-by: Laura Loghin <[email protected]>
1 parent bc85674 commit c346b17

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

src/ioctls/system.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ 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 ret is valid and we own the fd.
44+
// Safe because we verify that the fd is valid in `open_with_cloexec` and we own the fd.
4545
Ok(unsafe { Self::new_with_fd_number(fd) })
4646
}
4747

@@ -54,6 +54,24 @@ impl Kvm {
5454
///
5555
/// * `fd` - File descriptor for `/dev/kvm`.
5656
///
57+
/// # Safety
58+
///
59+
/// This function is unsafe as the primitives currently returned have the contract that
60+
/// they are the sole owner of the file descriptor they are wrapping. Usage of this function
61+
/// could accidentally allow violating this contract which can cause memory unsafety in code
62+
/// that relies on it being true.
63+
///
64+
/// The caller of this method must make sure the fd is valid and nothing else uses it.
65+
///
66+
/// # Example
67+
///
68+
/// ```
69+
/// # use kvm_ioctls::Kvm;
70+
/// let kvm_fd = Kvm::open_with_cloexec(true).unwrap();
71+
/// // Safe because we verify that the fd is valid in `open_with_cloexec` and we own the fd.
72+
/// let kvm = unsafe { Kvm::new_with_fd_number(kvm_fd) };
73+
/// ```
74+
///
5775
pub unsafe fn new_with_fd_number(fd: RawFd) -> Self {
5876
Kvm {
5977
kvm: File::from_raw_fd(fd),
@@ -430,10 +448,32 @@ impl Kvm {
430448

431449
/// Creates a VmFd object from a VM RawFd.
432450
///
451+
/// # Arguments
452+
///
453+
/// * `fd` - the RawFd used for creating the VmFd object.
454+
///
455+
/// # Safety
456+
///
433457
/// This function is unsafe as the primitives currently returned have the contract that
434458
/// they are the sole owner of the file descriptor they are wrapping. Usage of this function
435459
/// could accidentally allow violating this contract which can cause memory unsafety in code
436460
/// that relies on it being true.
461+
///
462+
/// The caller of this method must make sure the fd is valid and nothing else uses it.
463+
///
464+
/// # Example
465+
///
466+
/// ```rust
467+
/// # extern crate kvm_ioctls;
468+
/// # use std::os::unix::io::AsRawFd;
469+
/// # use kvm_ioctls::Kvm;
470+
/// let kvm = Kvm::new().unwrap();
471+
/// let vm = kvm.create_vm().unwrap();
472+
/// let rawfd = unsafe { libc::dup(vm.as_raw_fd()) };
473+
/// assert!(rawfd >= 0);
474+
/// let vm = unsafe { kvm.create_vmfd_from_rawfd(rawfd).unwrap() };
475+
/// ```
476+
///
437477
pub unsafe fn create_vmfd_from_rawfd(&self, fd: RawFd) -> Result<VmFd> {
438478
let run_mmap_size = self.get_vcpu_mmap_size()?;
439479
Ok(new_vmfd(File::from_raw_fd(fd), run_mmap_size))

src/ioctls/vm.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,17 +1040,25 @@ impl VmFd {
10401040

10411041
/// Creates a VcpuFd object from a vcpu RawFd.
10421042
///
1043+
/// # Arguments
1044+
///
1045+
/// * `fd` - the RawFd used for creating the VcpuFd object.
1046+
///
1047+
/// # Safety
1048+
///
10431049
/// This function is unsafe as the primitives currently returned have the contract that
10441050
/// they are the sole owner of the file descriptor they are wrapping. Usage of this function
10451051
/// could accidentally allow violating this contract which can cause memory unsafety in code
10461052
/// that relies on it being true.
10471053
///
1054+
/// The caller of this method must make sure the fd is valid and nothing else uses it.
1055+
///
10481056
/// # Example
10491057
///
10501058
/// ```rust
10511059
/// # extern crate kvm_ioctls;
10521060
/// # use std::os::unix::io::AsRawFd;
1053-
/// # use kvm_ioctls::{Kvm, VmFd, VcpuFd};
1061+
/// # use kvm_ioctls::Kvm;
10541062
/// let kvm = Kvm::new().unwrap();
10551063
/// let vm = kvm.create_vm().unwrap();
10561064
/// // Create one vCPU with the ID=0.

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@
6161
//! use kvm_bindings::KVM_MEM_LOG_DIRTY_PAGES;
6262
//! use kvm_bindings::kvm_userspace_memory_region;
6363
//!
64-
//! let mem_size = 0x4000;
65-
//! let guest_addr = 0x1000;
66-
//! let asm_code: &[u8];
64+
//! let mem_size = 0x4000;
65+
//! let guest_addr = 0x1000;
66+
//! let asm_code: &[u8];
6767
//!
6868
//! // Setting up architectural dependent values.
6969
//! #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]

0 commit comments

Comments
 (0)