diff --git a/Cargo.toml b/Cargo.toml index fa4263c3..4c884563 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ bitflags = { version = "2.4.0", optional = true } thiserror = "1.0.40" vmm-sys-util = { version = "0.12.1", optional = true } -[target.'cfg(windows)'.dependencies.winapi] +[target.'cfg(target_family = "windows")'.dependencies.winapi] version = "0.3" features = ["errhandlingapi", "sysinfoapi"] diff --git a/coverage_config_aarch64.json b/coverage_config_aarch64.json index 3a28db2d..4aeb3711 100644 --- a/coverage_config_aarch64.json +++ b/coverage_config_aarch64.json @@ -1,5 +1,5 @@ { "coverage_score": 85.2, - "exclude_path": "mmap_windows.rs", + "exclude_path": "mmap/windows.rs", "crate_features": "backend-mmap,backend-atomic,backend-bitmap" } diff --git a/src/bitmap/backend/atomic_bitmap.rs b/src/bitmap/backend/atomic_bitmap.rs index 07e30986..9fed13b6 100644 --- a/src/bitmap/backend/atomic_bitmap.rs +++ b/src/bitmap/backend/atomic_bitmap.rs @@ -194,11 +194,11 @@ impl Default for AtomicBitmap { #[cfg(feature = "backend-mmap")] impl NewBitmap for AtomicBitmap { fn with_len(len: usize) -> Self { - #[cfg(unix)] + #[cfg(target_family = "unix")] // SAFETY: There's no unsafe potential in calling this function. let page_size = unsafe { libc::sysconf(libc::_SC_PAGE_SIZE) }; - #[cfg(windows)] + #[cfg(target_family = "windows")] let page_size = { use winapi::um::sysinfoapi::{GetSystemInfo, LPSYSTEM_INFO, SYSTEM_INFO}; let mut sysinfo = std::mem::MaybeUninit::zeroed(); diff --git a/src/bytes.rs b/src/bytes.rs index 253d368f..c755cfeb 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -340,7 +340,7 @@ pub trait Bytes { /// # let gm = GuestMemoryMmap::<()>::from_ranges(&vec![(start_addr, 0x400)]) /// # .expect("Could not create guest memory"); /// # let addr = GuestAddress(0x1010); - /// # let mut file = if cfg!(unix) { + /// # let mut file = if cfg!(target_family = "unix") { /// let mut file = File::open(Path::new("/dev/urandom")).expect("Could not open /dev/urandom"); /// # file /// # } else { diff --git a/src/lib.rs b/src/lib.rs index 6f87ce48..b8fe5f40 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,21 +53,12 @@ pub use guest_memory::{ pub mod io; pub use io::{ReadVolatile, WriteVolatile}; -#[cfg(all(feature = "backend-mmap", not(feature = "xen"), unix))] -mod mmap_unix; - -#[cfg(all(feature = "backend-mmap", feature = "xen", unix))] -mod mmap_xen; - -#[cfg(all(feature = "backend-mmap", windows))] -mod mmap_windows; - #[cfg(feature = "backend-mmap")] pub mod mmap; #[cfg(feature = "backend-mmap")] pub use mmap::{Error, GuestMemoryMmap, GuestRegionMmap, MmapRegion}; -#[cfg(all(feature = "backend-mmap", feature = "xen", unix))] +#[cfg(all(feature = "backend-mmap", feature = "xen", target_family = "unix"))] pub use mmap::{MmapRange, MmapXenFlags}; pub mod volatile_memory; diff --git a/src/mmap.rs b/src/mmap/mod.rs similarity index 98% rename from src/mmap.rs rename to src/mmap/mod.rs index a5c81f24..23cd8e33 100644 --- a/src/mmap.rs +++ b/src/mmap/mod.rs @@ -13,7 +13,7 @@ //! This implementation is mmap-ing the memory of the guest into the current process. use std::borrow::Borrow; -#[cfg(unix)] +#[cfg(target_family = "unix")] use std::io::{Seek, SeekFrom}; use std::ops::Deref; use std::result; @@ -28,16 +28,25 @@ use crate::guest_memory::{ use crate::volatile_memory::{VolatileMemory, VolatileSlice}; use crate::{AtomicAccess, Bytes, ReadVolatile, WriteVolatile}; -#[cfg(all(not(feature = "xen"), unix))] -pub use crate::mmap_unix::{Error as MmapRegionError, MmapRegion, MmapRegionBuilder}; +#[cfg(all(not(feature = "xen"), target_family = "unix"))] +mod unix; -#[cfg(all(feature = "xen", unix))] -pub use crate::mmap_xen::{Error as MmapRegionError, MmapRange, MmapRegion, MmapXenFlags}; +#[cfg(all(feature = "xen", target_family = "unix"))] +pub(crate) mod xen; -#[cfg(windows)] -pub use crate::mmap_windows::MmapRegion; -#[cfg(windows)] +#[cfg(target_family = "windows")] +mod windows; + +#[cfg(all(not(feature = "xen"), target_family = "unix"))] +pub use unix::{Error as MmapRegionError, MmapRegion, MmapRegionBuilder}; + +#[cfg(all(feature = "xen", target_family = "unix"))] +pub use xen::{Error as MmapRegionError, MmapRange, MmapRegion, MmapXenFlags}; + +#[cfg(target_family = "windows")] pub use std::io::Error as MmapRegionError; +#[cfg(target_family = "windows")] +pub use windows::MmapRegion; /// A `Bitmap` that can be created starting from an initial size. pub trait NewBitmap: Bitmap + Default { @@ -71,7 +80,7 @@ pub enum Error { } // TODO: use this for Windows as well after we redefine the Error type there. -#[cfg(unix)] +#[cfg(target_family = "unix")] /// Checks if a mapping of `size` bytes fits at the provided `file_offset`. /// /// For a borrowed `FileOffset` and size, this function checks whether the mapping does not @@ -1033,7 +1042,7 @@ mod tests { let gm_list = [gm, gm_backed_by_file]; for gm in gm_list.iter() { let addr = GuestAddress(0x1010); - let mut file = if cfg!(unix) { + let mut file = if cfg!(target_family = "unix") { File::open(Path::new("/dev/zero")).unwrap() } else { File::open(Path::new("c:\\Windows\\system32\\ntoskrnl.exe")).unwrap() @@ -1042,7 +1051,7 @@ mod tests { gm.read_exact_volatile_from(addr, &mut file, mem::size_of::()) .unwrap(); let value: u32 = gm.read_obj(addr).unwrap(); - if cfg!(unix) { + if cfg!(target_family = "unix") { assert_eq!(value, 0); } else { assert_eq!(value, 0x0090_5a4d); @@ -1051,7 +1060,7 @@ mod tests { let mut sink = vec![0; mem::size_of::()]; gm.write_all_volatile_to(addr, &mut sink.as_mut_slice(), mem::size_of::()) .unwrap(); - if cfg!(unix) { + if cfg!(target_family = "unix") { assert_eq!(sink, vec![0; mem::size_of::()]); } else { assert_eq!(sink, vec![0x4d, 0x5a, 0x90, 0x00]); @@ -1170,7 +1179,7 @@ mod tests { // used for the backing file. Refer to Microsoft docs here: // https://docs.microsoft.com/en-us/windows/desktop/api/memoryapi/nf-memoryapi-mapviewoffile #[test] - #[cfg(unix)] + #[cfg(target_family = "unix")] fn test_retrieve_offset_from_fd_backing_memory_region() { let f = TempFile::new().unwrap().into_file(); f.set_len(0x1400).unwrap(); diff --git a/src/mmap_unix.rs b/src/mmap/unix.rs similarity index 100% rename from src/mmap_unix.rs rename to src/mmap/unix.rs diff --git a/src/mmap_windows.rs b/src/mmap/windows.rs similarity index 99% rename from src/mmap_windows.rs rename to src/mmap/windows.rs index 0c7dbd99..36afdab4 100644 --- a/src/mmap_windows.rs +++ b/src/mmap/windows.rs @@ -248,7 +248,7 @@ mod tests { use crate::bitmap::AtomicBitmap; use crate::guest_memory::FileOffset; - use crate::mmap_windows::INVALID_HANDLE_VALUE; + use crate::mmap::windows::INVALID_HANDLE_VALUE; type MmapRegion = super::MmapRegion<()>; diff --git a/src/mmap_xen.rs b/src/mmap/xen.rs similarity index 100% rename from src/mmap_xen.rs rename to src/mmap/xen.rs diff --git a/src/volatile_memory.rs b/src/volatile_memory.rs index 486f5720..43c1d206 100644 --- a/src/volatile_memory.rs +++ b/src/volatile_memory.rs @@ -39,8 +39,8 @@ use crate::atomic_integer::AtomicInteger; use crate::bitmap::{Bitmap, BitmapSlice, BS}; use crate::{AtomicAccess, ByteValued, Bytes}; -#[cfg(all(feature = "backend-mmap", feature = "xen", unix))] -use crate::mmap_xen::{MmapXen as MmapInfo, MmapXenSlice}; +#[cfg(all(feature = "backend-mmap", feature = "xen", target_family = "unix"))] +use crate::mmap::xen::{MmapXen as MmapInfo, MmapXenSlice}; #[cfg(not(feature = "xen"))] type MmapInfo = std::marker::PhantomData<()>; @@ -322,7 +322,7 @@ pub struct PtrGuard { // This isn't used anymore, but it protects the slice from getting unmapped while in use. // Once this goes out of scope, the memory is unmapped automatically. - #[cfg(all(feature = "xen", unix))] + #[cfg(all(feature = "xen", target_family = "unix"))] _slice: MmapXenSlice, } @@ -330,7 +330,7 @@ pub struct PtrGuard { impl PtrGuard { #[allow(unused_variables)] fn new(mmap: Option<&MmapInfo>, addr: *mut u8, write: bool, len: usize) -> Self { - #[cfg(all(feature = "xen", unix))] + #[cfg(all(feature = "xen", target_family = "unix"))] let (addr, _slice) = { let prot = if write { libc::PROT_WRITE @@ -345,7 +345,7 @@ impl PtrGuard { addr, len, - #[cfg(all(feature = "xen", unix))] + #[cfg(all(feature = "xen", target_family = "unix"))] _slice, } } @@ -1952,7 +1952,7 @@ mod tests { let a = VolatileSlice::from(backing.as_mut_slice()); let s = a.as_volatile_slice(); assert!(s.write_obj(!0u32, 1).is_ok()); - let mut file = if cfg!(unix) { + let mut file = if cfg!(target_family = "unix") { File::open(Path::new("/dev/zero")).unwrap() } else { File::open(Path::new("c:\\Windows\\system32\\ntoskrnl.exe")).unwrap() @@ -1968,7 +1968,7 @@ mod tests { .is_err()); let value = s.read_obj::(1).unwrap(); - if cfg!(unix) { + if cfg!(target_family = "unix") { assert_eq!(value, 0); } else { assert_eq!(value, 0x0090_5a4d); @@ -1980,7 +1980,7 @@ mod tests { .write_all_volatile(&s.get_slice(1, size_of::()).unwrap()) .is_ok()); - if cfg!(unix) { + if cfg!(target_family = "unix") { assert_eq!(sink, vec![0; size_of::()]); } else { assert_eq!(sink, vec![0x4d, 0x5a, 0x90, 0x00]);