Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix clippy warnings #108

Merged
merged 1 commit into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/ioctls/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Kvm {
pub fn new() -> Result<Self> {
// Open `/dev/kvm` using `O_CLOEXEC` flag.
let fd = Self::open_with_cloexec(true)?;
// Safe because we verify that ret is valid and we own the fd.
// Safe because we verify that the fd is valid in `open_with_cloexec` and we own the fd.
Ok(unsafe { Self::new_with_fd_number(fd) })
}

Expand All @@ -54,6 +54,24 @@ impl Kvm {
///
/// * `fd` - File descriptor for `/dev/kvm`.
///
/// # Safety
///
/// This function is unsafe as the primitives currently returned have the contract that
/// they are the sole owner of the file descriptor they are wrapping. Usage of this function
/// could accidentally allow violating this contract which can cause memory unsafety in code
/// that relies on it being true.
///
/// The caller of this method must make sure the fd is valid and nothing else uses it.
///
/// # Example
///
/// ```
/// # use kvm_ioctls::Kvm;
/// let kvm_fd = Kvm::open_with_cloexec(true).unwrap();
/// // Safe because we verify that the fd is valid in `open_with_cloexec` and we own the fd.
/// let kvm = unsafe { Kvm::new_with_fd_number(kvm_fd) };
/// ```
///
pub unsafe fn new_with_fd_number(fd: RawFd) -> Self {
Kvm {
kvm: File::from_raw_fd(fd),
Expand Down Expand Up @@ -430,10 +448,32 @@ impl Kvm {

/// Creates a VmFd object from a VM RawFd.
///
/// # Arguments
///
/// * `fd` - the RawFd used for creating the VmFd object.
///
/// # Safety
///
/// This function is unsafe as the primitives currently returned have the contract that
/// they are the sole owner of the file descriptor they are wrapping. Usage of this function
/// could accidentally allow violating this contract which can cause memory unsafety in code
/// that relies on it being true.
///
/// The caller of this method must make sure the fd is valid and nothing else uses it.
///
/// # Example
///
/// ```rust
/// # extern crate kvm_ioctls;
/// # use std::os::unix::io::AsRawFd;
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let rawfd = unsafe { libc::dup(vm.as_raw_fd()) };
/// assert!(rawfd >= 0);
/// let vm = unsafe { kvm.create_vmfd_from_rawfd(rawfd).unwrap() };
/// ```
///
pub unsafe fn create_vmfd_from_rawfd(&self, fd: RawFd) -> Result<VmFd> {
let run_mmap_size = self.get_vcpu_mmap_size()?;
Ok(new_vmfd(File::from_raw_fd(fd), run_mmap_size))
Expand Down
10 changes: 9 additions & 1 deletion src/ioctls/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,17 +1040,25 @@ impl VmFd {

/// Creates a VcpuFd object from a vcpu RawFd.
///
/// # Arguments
///
/// * `fd` - the RawFd used for creating the VcpuFd object.
///
/// # Safety
///
/// This function is unsafe as the primitives currently returned have the contract that
/// they are the sole owner of the file descriptor they are wrapping. Usage of this function
/// could accidentally allow violating this contract which can cause memory unsafety in code
/// that relies on it being true.
///
/// The caller of this method must make sure the fd is valid and nothing else uses it.
///
/// # Example
///
/// ```rust
/// # extern crate kvm_ioctls;
/// # use std::os::unix::io::AsRawFd;
/// # use kvm_ioctls::{Kvm, VmFd, VcpuFd};
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// // Create one vCPU with the ID=0.
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
//! use kvm_bindings::KVM_MEM_LOG_DIRTY_PAGES;
//! use kvm_bindings::kvm_userspace_memory_region;
//!
//! let mem_size = 0x4000;
//! let guest_addr = 0x1000;
//! let asm_code: &[u8];
//! let mem_size = 0x4000;
//! let guest_addr = 0x1000;
//! let asm_code: &[u8];
//!
//! // Setting up architectural dependent values.
//! #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Expand Down