Skip to content

Commit 0b7a437

Browse files
bonziniroypat
authored andcommitted
make Read/WriteVolatile implementations optional
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 1755d1d commit 0b7a437

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Upcoming version
44

5+
### Added
6+
7+
- \[[#311](https://github.com/rust-vmm/vm-memory/pull/311)\] Allow compiling without the ReadVolatile and WriteVolatile implementations
8+
59
## \[v0.16.1\]
610

711
### Added

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ edition = "2021"
1212
autobenches = false
1313

1414
[features]
15-
default = []
15+
default = ["rawfd"]
1616
backend-bitmap = []
17-
backend-mmap = []
17+
backend-mmap = ["dep:libc"]
1818
backend-atomic = ["arc-swap"]
19+
rawfd = ["dep:libc"]
1920
xen = ["backend-mmap", "bitflags", "vmm-sys-util"]
2021

2122
[dependencies]
22-
libc = "0.2.39"
23+
libc = { version = "0.2.39", optional = true }
2324
arc-swap = { version = "1.0.0", optional = true }
2425
bitflags = { version = "2.4.0", optional = true }
2526
thiserror = "1.0.40"

src/guest_memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ pub trait GuestMemory {
650650
/// * Read bytes from /dev/urandom (uses the `backend-mmap` feature)
651651
///
652652
/// ```
653-
/// # #[cfg(feature = "backend-mmap")]
653+
/// # #[cfg(all(feature = "backend-mmap", feature = "rawfd"))]
654654
/// # {
655655
/// # use vm_memory::{Address, GuestMemory, Bytes, GuestAddress, GuestMemoryMmap};
656656
/// # use std::fs::File;

src/io.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
use crate::bitmap::BitmapSlice;
77
use crate::volatile_memory::copy_slice_impl::{copy_from_volatile_slice, copy_to_volatile_slice};
88
use crate::{VolatileMemoryError, VolatileSlice};
9-
use std::io::{Cursor, ErrorKind, Stdout};
9+
use std::io::{Cursor, ErrorKind};
10+
11+
#[cfg(feature = "rawfd")]
12+
use std::io::Stdout;
13+
14+
#[cfg(feature = "rawfd")]
1015
use std::os::fd::AsRawFd;
1116

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

115120
macro_rules! impl_read_write_volatile_for_raw_fd {
116121
($raw_fd_ty:ty) => {
122+
#[cfg(feature = "rawfd")]
117123
impl ReadVolatile for $raw_fd_ty {
118124
fn read_volatile<B: BitmapSlice>(
119125
&mut self,
@@ -123,6 +129,7 @@ macro_rules! impl_read_write_volatile_for_raw_fd {
123129
}
124130
}
125131

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

144+
#[cfg(feature = "rawfd")]
137145
impl WriteVolatile for Stdout {
138146
fn write_volatile<B: BitmapSlice>(
139147
&mut self,
@@ -153,6 +161,7 @@ impl_read_write_volatile_for_raw_fd!(std::os::fd::BorrowedFd<'_>);
153161
/// the given [`VolatileSlice`].
154162
///
155163
/// Returns the numbers of bytes read.
164+
#[cfg(feature = "rawfd")]
156165
fn read_volatile_raw_fd<Fd: AsRawFd>(
157166
raw_fd: &mut Fd,
158167
buf: &mut VolatileSlice<impl BitmapSlice>,
@@ -183,6 +192,7 @@ fn read_volatile_raw_fd<Fd: AsRawFd>(
183192
/// data stored in the given [`VolatileSlice`].
184193
///
185194
/// Returns the numbers of bytes written.
195+
#[cfg(feature = "rawfd")]
186196
fn write_volatile_raw_fd<Fd: AsRawFd>(
187197
raw_fd: &mut Fd,
188198
buf: &VolatileSlice<impl BitmapSlice>,
@@ -361,7 +371,10 @@ impl WriteVolatile for Cursor<&mut [u8]> {
361371
mod tests {
362372
use crate::io::{ReadVolatile, WriteVolatile};
363373
use crate::{VolatileMemoryError, VolatileSlice};
364-
use std::io::{Cursor, ErrorKind, Read, Seek, Write};
374+
use std::io::{Cursor, ErrorKind};
375+
#[cfg(feature = "rawfd")]
376+
use std::io::{Read, Seek, Write};
377+
#[cfg(feature = "rawfd")]
365378
use vmm_sys_util::tempfile::TempFile;
366379

367380
// ---- Test ReadVolatile for &[u8] ----
@@ -398,6 +411,7 @@ mod tests {
398411
}
399412

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

442456
for (input, output) in test_cases {
443457
read_4_bytes_to_5_byte_memory(input.clone(), output);
458+
#[cfg(feature = "rawfd")]
444459
read_4_bytes_from_file(input, output);
445460
}
446461
}
@@ -481,6 +496,7 @@ mod tests {
481496
}
482497

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

525541
for (input, output) in test_cases {
526542
write_4_bytes_to_5_byte_vec(input.clone(), output);
543+
#[cfg(feature = "rawfd")]
527544
write_5_bytes_to_file(input);
528545
}
529546
}

src/mmap.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,9 +645,9 @@ mod tests {
645645
use crate::bitmap::AtomicBitmap;
646646
use crate::GuestAddressSpace;
647647

648-
use std::fs::File;
649648
use std::mem;
650-
use std::path::Path;
649+
#[cfg(feature = "rawfd")]
650+
use std::{fs::File, path::Path};
651651
use vmm_sys_util::tempfile::TempFile;
652652

653653
type GuestMemoryMmap = super::GuestMemoryMmap<()>;
@@ -1141,6 +1141,7 @@ mod tests {
11411141
}
11421142

11431143
#[test]
1144+
#[cfg(feature = "rawfd")]
11441145
fn read_to_and_write_from_mem() {
11451146
let f = TempFile::new().unwrap().into_file();
11461147
f.set_len(0x400).unwrap();

src/volatile_memory.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,15 +1641,18 @@ mod tests {
16411641
use super::*;
16421642
use std::alloc::Layout;
16431643

1644+
#[cfg(feature = "rawfd")]
16441645
use std::fs::File;
16451646
use std::mem::size_of_val;
1647+
#[cfg(feature = "rawfd")]
16461648
use std::path::Path;
16471649
use std::sync::atomic::{AtomicUsize, Ordering};
16481650
use std::sync::{Arc, Barrier};
16491651
use std::thread::spawn;
16501652

16511653
use matches::assert_matches;
16521654
use std::num::NonZeroUsize;
1655+
#[cfg(feature = "rawfd")]
16531656
use vmm_sys_util::tempfile::TempFile;
16541657

16551658
use crate::bitmap::tests::{
@@ -2117,6 +2120,7 @@ mod tests {
21172120
}
21182121

21192122
#[test]
2123+
#[cfg(feature = "rawfd")]
21202124
fn mem_read_and_write() {
21212125
let mut backing = vec![0u8; 5];
21222126
let a = VolatileSlice::from(backing.as_mut_slice());

0 commit comments

Comments
 (0)