Skip to content

Commit 4757a4a

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 53c03e2 commit 4757a4a

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
@@ -221,7 +221,7 @@ mod tests {
221221

222222
use super::*;
223223

224-
use crate::bitmap::tests::test_guest_memory_and_region;
224+
#[cfg(feature = "backend-bitmap")]
225225
use crate::bitmap::AtomicBitmap;
226226
use crate::{Bytes, GuestMemory, GuestMemoryError};
227227

@@ -634,8 +634,9 @@ mod tests {
634634
}
635635

636636
#[test]
637+
#[cfg(feature = "backend-bitmap")]
637638
fn test_dirty_tracking() {
638-
test_guest_memory_and_region(|| {
639+
crate::bitmap::tests::test_guest_memory_and_region(|| {
639640
crate::GuestMemoryMmap::<AtomicBitmap>::from_ranges(&[(GuestAddress(0), 0x1_0000)])
640641
.unwrap()
641642
});

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]
@@ -2080,6 +2085,7 @@ mod tests {
20802085
}
20812086

20822087
#[test]
2088+
#[cfg(feature = "backend-bitmap")]
20832089
fn test_volatile_slice_dirty_tracking() {
20842090
let val = 123u64;
20852091
let dirty_offset = 0x1000;
@@ -2170,6 +2176,7 @@ mod tests {
21702176
}
21712177

21722178
#[test]
2179+
#[cfg(feature = "backend-bitmap")]
21732180
fn test_volatile_ref_dirty_tracking() {
21742181
let val = 123u64;
21752182
let mut buf = vec![val];
@@ -2184,6 +2191,7 @@ mod tests {
21842191
assert!(range_is_dirty(vref.bitmap(), 0, vref.len()));
21852192
}
21862193

2194+
#[cfg(feature = "backend-bitmap")]
21872195
fn test_volatile_array_ref_copy_from_tracking<T>(
21882196
buf: &mut [T],
21892197
index: usize,
@@ -2210,6 +2218,7 @@ mod tests {
22102218
}
22112219

22122220
#[test]
2221+
#[cfg(feature = "backend-bitmap")]
22132222
fn test_volatile_array_ref_dirty_tracking() {
22142223
let val = 123u64;
22152224
let dirty_len = size_of_val(&val);

0 commit comments

Comments
 (0)