Skip to content

Commit cac7a43

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 cac7a43

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ mod tests {
488488

489489
use super::*;
490490

491-
use crate::bitmap::tests::test_guest_memory_and_region;
491+
#[cfg(feature = "backend-bitmap")]
492492
use crate::bitmap::AtomicBitmap;
493493
use crate::GuestAddressSpace;
494494

@@ -1363,8 +1363,9 @@ mod tests {
13631363
}
13641364

13651365
#[test]
1366+
#[cfg(feature = "backend-bitmap")]
13661367
fn test_dirty_tracking() {
1367-
test_guest_memory_and_region(|| {
1368+
crate::bitmap::tests::test_guest_memory_and_region(|| {
13681369
crate::GuestMemoryMmap::<AtomicBitmap>::from_ranges(&[(GuestAddress(0), 0x1_0000)])
13691370
.unwrap()
13701371
});

src/mmap/unix.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,11 @@ mod tests {
438438
use super::*;
439439

440440
use std::io::Write;
441-
use std::num::NonZeroUsize;
442441
use std::slice;
443442
use std::sync::Arc;
444443
use vmm_sys_util::tempfile::TempFile;
445444

445+
#[cfg(feature = "backend-bitmap")]
446446
use crate::bitmap::AtomicBitmap;
447447

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

536536
#[test]
537537
#[cfg(not(miri))] // Miri cannot mmap files
538+
#[cfg(feature = "backend-bitmap")]
538539
fn test_mmap_region_build() {
539540
let a = Arc::new(TempFile::new().unwrap().into_file());
540541

@@ -586,7 +587,9 @@ mod tests {
586587
assert!(r.owned());
587588

588589
let region_size = 0x10_0000;
589-
let bitmap = AtomicBitmap::new(region_size, unsafe { NonZeroUsize::new_unchecked(0x1000) });
590+
let bitmap = AtomicBitmap::new(region_size, unsafe {
591+
std::num::NonZeroUsize::new_unchecked(0x1000)
592+
});
590593
let builder = MmapRegionBuilder::new_with_bitmap(region_size, bitmap)
591594
.with_hugetlbfs(true)
592595
.with_mmap_prot(libc::PROT_READ | libc::PROT_WRITE);
@@ -648,6 +651,7 @@ mod tests {
648651
}
649652

650653
#[test]
654+
#[cfg(feature = "backend-bitmap")]
651655
fn test_dirty_tracking() {
652656
// Using the `crate` prefix because we aliased `MmapRegion` to `MmapRegion<()>` for
653657
// the rest of the unit tests above.

src/volatile_memory.rs

Lines changed: 9 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;
@@ -1455,15 +1456,19 @@ mod tests {
14551456
use std::thread::spawn;
14561457

14571458
use matches::assert_matches;
1459+
#[cfg(feature = "backend-bitmap")]
14581460
use std::num::NonZeroUsize;
14591461
#[cfg(feature = "rawfd")]
14601462
use vmm_sys_util::tempfile::TempFile;
14611463

1464+
#[cfg(feature = "backend-bitmap")]
14621465
use crate::bitmap::tests::{
14631466
check_range, range_is_clean, range_is_dirty, test_bytes, test_volatile_memory,
14641467
};
1468+
#[cfg(feature = "backend-bitmap")]
14651469
use crate::bitmap::{AtomicBitmap, RefSlice};
14661470

1471+
#[cfg(feature = "backend-bitmap")]
14671472
const DEFAULT_PAGE_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(0x1000) };
14681473

14691474
#[test]
@@ -2132,6 +2137,7 @@ mod tests {
21322137
}
21332138

21342139
#[test]
2140+
#[cfg(feature = "backend-bitmap")]
21352141
fn test_volatile_slice_dirty_tracking() {
21362142
let val = 123u64;
21372143
let dirty_offset = 0x1000;
@@ -2222,6 +2228,7 @@ mod tests {
22222228
}
22232229

22242230
#[test]
2231+
#[cfg(feature = "backend-bitmap")]
22252232
fn test_volatile_ref_dirty_tracking() {
22262233
let val = 123u64;
22272234
let mut buf = vec![val];
@@ -2236,6 +2243,7 @@ mod tests {
22362243
assert!(range_is_dirty(vref.bitmap(), 0, vref.len()));
22372244
}
22382245

2246+
#[cfg(feature = "backend-bitmap")]
22392247
fn test_volatile_array_ref_copy_from_tracking<T>(
22402248
buf: &mut [T],
22412249
index: usize,
@@ -2262,6 +2270,7 @@ mod tests {
22622270
}
22632271

22642272
#[test]
2273+
#[cfg(feature = "backend-bitmap")]
22652274
fn test_volatile_array_ref_dirty_tracking() {
22662275
let val = 123u64;
22672276
let dirty_len = size_of_val(&val);

0 commit comments

Comments
 (0)