Skip to content

Commit d50c1cd

Browse files
committed
do not backdoor-enable backend-bitmap in unittests
The bitmap backend implementation in vm_memory::bitmap::backend is only exported if the backend-bitmap feature is enabled. However, compiling the crate in test mode (e.g. `cargo test`), also unconditionally enables it. This is weird, and actually causes problems: The bitmap backend code depends on the libc crate, and this weird "enable unconditionally for tests" behavior means we have to go through more trouble in Cargo.toml to get the code to compile in some feature combinations, as not only do we need to add the libc crate as a dependency of the backend-bitmap feature, we also have to add it as a dev-dependency. Add a #[cfg(feature = "backend-bitmap")] to the test module in src/bitmap/mod.rs, as otherwise some of the functions will be warned about being unused in some feature configurations. Signed-off-by: Patrick Roy <[email protected]>
1 parent 92874cc commit d50c1cd

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

src/bitmap/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
//! `GuestMemoryRegion` object, and the resulting bitmaps can then be aggregated to build the
77
//! global view for an entire `GuestMemory` object.
88
9-
#[cfg(any(test, feature = "backend-bitmap"))]
9+
#[cfg(feature = "backend-bitmap")]
1010
mod backend;
1111

1212
use std::fmt::Debug;
1313

1414
use crate::{GuestMemory, GuestMemoryRegion};
1515

16-
#[cfg(any(test, feature = "backend-bitmap"))]
16+
#[cfg(feature = "backend-bitmap")]
1717
pub use backend::{ArcSlice, AtomicBitmap, RefSlice};
1818

1919
/// Trait implemented by types that support creating `BitmapSlice` objects.
@@ -117,6 +117,7 @@ pub type BS<'a, B> = <B as WithBitmapSlice<'a>>::S;
117117
pub type MS<'a, M> = BS<'a, <<M as GuestMemory>::R as GuestMemoryRegion>::B>;
118118

119119
#[cfg(test)]
120+
#[cfg(feature = "backend-bitmap")]
120121
pub(crate) mod tests {
121122
use super::*;
122123

src/mmap/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ mod tests {
489489
use super::*;
490490

491491
use crate::bitmap::tests::test_guest_memory_and_region;
492+
#[cfg(feature = "backend-bitmap")]
492493
use crate::bitmap::AtomicBitmap;
493494
use crate::GuestAddressSpace;
494495

@@ -1363,6 +1364,7 @@ mod tests {
13631364
}
13641365

13651366
#[test]
1367+
#[cfg(feature = "backend-bitmap")]
13661368
fn test_dirty_tracking() {
13671369
test_guest_memory_and_region(|| {
13681370
crate::GuestMemoryMmap::<AtomicBitmap>::from_ranges(&[(GuestAddress(0), 0x1_0000)])

src/mmap/unix.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ mod tests {
443443
use std::sync::Arc;
444444
use vmm_sys_util::tempfile::TempFile;
445445

446+
#[cfg(feature = "backend-bitmap")]
446447
use crate::bitmap::AtomicBitmap;
447448

448449
type MmapRegion = super::MmapRegion<()>;
@@ -535,6 +536,7 @@ mod tests {
535536

536537
#[test]
537538
#[cfg(not(miri))] // Miri cannot mmap files
539+
#[cfg(feature = "backend-bitmap")]
538540
fn test_mmap_region_build() {
539541
let a = Arc::new(TempFile::new().unwrap().into_file());
540542

@@ -648,6 +650,7 @@ mod tests {
648650
}
649651

650652
#[test]
653+
#[cfg(feature = "backend-bitmap")]
651654
fn test_dirty_tracking() {
652655
// Using the `crate` prefix because we aliased `MmapRegion` to `MmapRegion<()>` for
653656
// the rest of the unit tests above.

src/volatile_memory.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,7 @@ mod tests {
14471447

14481448
#[cfg(feature = "rawfd")]
14491449
use std::fs::File;
1450+
#[cfg(feature = "backend-bitmap")]
14501451
use std::mem::size_of_val;
14511452
#[cfg(feature = "rawfd")]
14521453
use std::path::Path;
@@ -1459,9 +1460,11 @@ mod tests {
14591460
#[cfg(feature = "rawfd")]
14601461
use vmm_sys_util::tempfile::TempFile;
14611462

1463+
#[cfg(feature = "backend-bitmap")]
14621464
use crate::bitmap::tests::{
14631465
check_range, range_is_clean, range_is_dirty, test_bytes, test_volatile_memory,
14641466
};
1467+
#[cfg(feature = "backend-bitmap")]
14651468
use crate::bitmap::{AtomicBitmap, RefSlice};
14661469

14671470
const DEFAULT_PAGE_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(0x1000) };
@@ -2132,6 +2135,7 @@ mod tests {
21322135
}
21332136

21342137
#[test]
2138+
#[cfg(feature = "backend-bitmap")]
21352139
fn test_volatile_slice_dirty_tracking() {
21362140
let val = 123u64;
21372141
let dirty_offset = 0x1000;
@@ -2222,6 +2226,7 @@ mod tests {
22222226
}
22232227

22242228
#[test]
2229+
#[cfg(feature = "backend-bitmap")]
22252230
fn test_volatile_ref_dirty_tracking() {
22262231
let val = 123u64;
22272232
let mut buf = vec![val];
@@ -2236,6 +2241,7 @@ mod tests {
22362241
assert!(range_is_dirty(vref.bitmap(), 0, vref.len()));
22372242
}
22382243

2244+
#[cfg(feature = "backend-bitmap")]
22392245
fn test_volatile_array_ref_copy_from_tracking<T>(
22402246
buf: &mut [T],
22412247
index: usize,
@@ -2262,6 +2268,7 @@ mod tests {
22622268
}
22632269

22642270
#[test]
2271+
#[cfg(feature = "backend-bitmap")]
22652272
fn test_volatile_array_ref_dirty_tracking() {
22662273
let val = 123u64;
22672274
let dirty_len = size_of_val(&val);

0 commit comments

Comments
 (0)