Skip to content

Commit

Permalink
make Read/WriteVolatile implementations optional
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
bonzini authored and roypat committed Jan 30, 2025
1 parent 1755d1d commit 0b7a437
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Upcoming version

### Added

- \[[#311](https://github.com/rust-vmm/vm-memory/pull/311)\] Allow compiling without the ReadVolatile and WriteVolatile implementations

## \[v0.16.1\]

### Added
Expand Down
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ edition = "2021"
autobenches = false

[features]
default = []
default = ["rawfd"]
backend-bitmap = []
backend-mmap = []
backend-mmap = ["dep:libc"]
backend-atomic = ["arc-swap"]
rawfd = ["dep:libc"]
xen = ["backend-mmap", "bitflags", "vmm-sys-util"]

[dependencies]
libc = "0.2.39"
libc = { version = "0.2.39", optional = true }
arc-swap = { version = "1.0.0", optional = true }
bitflags = { version = "2.4.0", optional = true }
thiserror = "1.0.40"
Expand Down
2 changes: 1 addition & 1 deletion src/guest_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ pub trait GuestMemory {
/// * Read bytes from /dev/urandom (uses the `backend-mmap` feature)
///
/// ```
/// # #[cfg(feature = "backend-mmap")]
/// # #[cfg(all(feature = "backend-mmap", feature = "rawfd"))]
/// # {
/// # use vm_memory::{Address, GuestMemory, Bytes, GuestAddress, GuestMemoryMmap};
/// # use std::fs::File;
Expand Down
21 changes: 19 additions & 2 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
use crate::bitmap::BitmapSlice;
use crate::volatile_memory::copy_slice_impl::{copy_from_volatile_slice, copy_to_volatile_slice};
use crate::{VolatileMemoryError, VolatileSlice};
use std::io::{Cursor, ErrorKind, Stdout};
use std::io::{Cursor, ErrorKind};

#[cfg(feature = "rawfd")]
use std::io::Stdout;

#[cfg(feature = "rawfd")]
use std::os::fd::AsRawFd;

/// A version of the standard library's [`Read`](std::io::Read) trait that operates on volatile
Expand Down Expand Up @@ -114,6 +119,7 @@ pub trait WriteVolatile {

macro_rules! impl_read_write_volatile_for_raw_fd {
($raw_fd_ty:ty) => {
#[cfg(feature = "rawfd")]
impl ReadVolatile for $raw_fd_ty {
fn read_volatile<B: BitmapSlice>(
&mut self,
Expand All @@ -123,6 +129,7 @@ macro_rules! impl_read_write_volatile_for_raw_fd {
}
}

#[cfg(feature = "rawfd")]
impl WriteVolatile for $raw_fd_ty {
fn write_volatile<B: BitmapSlice>(
&mut self,
Expand All @@ -134,6 +141,7 @@ macro_rules! impl_read_write_volatile_for_raw_fd {
};
}

#[cfg(feature = "rawfd")]
impl WriteVolatile for Stdout {
fn write_volatile<B: BitmapSlice>(
&mut self,
Expand All @@ -153,6 +161,7 @@ impl_read_write_volatile_for_raw_fd!(std::os::fd::BorrowedFd<'_>);
/// the given [`VolatileSlice`].
///
/// Returns the numbers of bytes read.
#[cfg(feature = "rawfd")]
fn read_volatile_raw_fd<Fd: AsRawFd>(
raw_fd: &mut Fd,
buf: &mut VolatileSlice<impl BitmapSlice>,
Expand Down Expand Up @@ -183,6 +192,7 @@ fn read_volatile_raw_fd<Fd: AsRawFd>(
/// data stored in the given [`VolatileSlice`].
///
/// Returns the numbers of bytes written.
#[cfg(feature = "rawfd")]
fn write_volatile_raw_fd<Fd: AsRawFd>(
raw_fd: &mut Fd,
buf: &VolatileSlice<impl BitmapSlice>,
Expand Down Expand Up @@ -361,7 +371,10 @@ impl WriteVolatile for Cursor<&mut [u8]> {
mod tests {
use crate::io::{ReadVolatile, WriteVolatile};
use crate::{VolatileMemoryError, VolatileSlice};
use std::io::{Cursor, ErrorKind, Read, Seek, Write};
use std::io::{Cursor, ErrorKind};
#[cfg(feature = "rawfd")]
use std::io::{Read, Seek, Write};
#[cfg(feature = "rawfd")]
use vmm_sys_util::tempfile::TempFile;

// ---- Test ReadVolatile for &[u8] ----
Expand Down Expand Up @@ -398,6 +411,7 @@ mod tests {
}

// ---- Test ReadVolatile for File ----
#[cfg(feature = "rawfd")]
fn read_4_bytes_from_file(source: Vec<u8>, expected_output: [u8; 5]) {
let mut temp_file = TempFile::new().unwrap().into_file();
temp_file.write_all(source.as_ref()).unwrap();
Expand Down Expand Up @@ -441,6 +455,7 @@ mod tests {

for (input, output) in test_cases {
read_4_bytes_to_5_byte_memory(input.clone(), output);
#[cfg(feature = "rawfd")]
read_4_bytes_from_file(input, output);
}
}
Expand Down Expand Up @@ -481,6 +496,7 @@ mod tests {
}

// ---- Test ẂriteVolatile for File works ----
#[cfg(feature = "rawfd")]
fn write_5_bytes_to_file(mut source: Vec<u8>) {
// Test write_volatile for File works
let mut temp_file = TempFile::new().unwrap().into_file();
Expand Down Expand Up @@ -524,6 +540,7 @@ mod tests {

for (input, output) in test_cases {
write_4_bytes_to_5_byte_vec(input.clone(), output);
#[cfg(feature = "rawfd")]
write_5_bytes_to_file(input);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,9 +645,9 @@ mod tests {
use crate::bitmap::AtomicBitmap;
use crate::GuestAddressSpace;

use std::fs::File;
use std::mem;
use std::path::Path;
#[cfg(feature = "rawfd")]
use std::{fs::File, path::Path};
use vmm_sys_util::tempfile::TempFile;

type GuestMemoryMmap = super::GuestMemoryMmap<()>;
Expand Down Expand Up @@ -1141,6 +1141,7 @@ mod tests {
}

#[test]
#[cfg(feature = "rawfd")]
fn read_to_and_write_from_mem() {
let f = TempFile::new().unwrap().into_file();
f.set_len(0x400).unwrap();
Expand Down
4 changes: 4 additions & 0 deletions src/volatile_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1641,15 +1641,18 @@ mod tests {
use super::*;
use std::alloc::Layout;

#[cfg(feature = "rawfd")]
use std::fs::File;
use std::mem::size_of_val;
#[cfg(feature = "rawfd")]
use std::path::Path;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Barrier};
use std::thread::spawn;

use matches::assert_matches;
use std::num::NonZeroUsize;
#[cfg(feature = "rawfd")]
use vmm_sys_util::tempfile::TempFile;

use crate::bitmap::tests::{
Expand Down Expand Up @@ -2117,6 +2120,7 @@ mod tests {
}

#[test]
#[cfg(feature = "rawfd")]
fn mem_read_and_write() {
let mut backing = vec![0u8; 5];
let a = VolatileSlice::from(backing.as_mut_slice());
Expand Down

0 comments on commit 0b7a437

Please sign in to comment.