From c47230fbc789b19751b6de2558fbe404e547c50e Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Fri, 13 Dec 2024 14:25:52 +0200 Subject: [PATCH 1/3] Move mmap_* modules under src/mmap/ No functional changes, other than the module paths of mmap related types. Signed-off-by: Manos Pitsidianakis --- coverage_config_aarch64.json | 2 +- src/lib.rs | 9 --------- src/mmap.rs | 15 ++++++++++++--- src/{mmap_unix.rs => mmap/unix.rs} | 0 src/{mmap_windows.rs => mmap/windows.rs} | 2 +- src/{mmap_xen.rs => mmap/xen.rs} | 0 src/volatile_memory.rs | 2 +- 7 files changed, 15 insertions(+), 15 deletions(-) rename src/{mmap_unix.rs => mmap/unix.rs} (100%) rename src/{mmap_windows.rs => mmap/windows.rs} (99%) rename src/{mmap_xen.rs => mmap/xen.rs} (100%) 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/lib.rs b/src/lib.rs index 6f87ce48..de4984c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,15 +53,6 @@ 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; diff --git a/src/mmap.rs b/src/mmap.rs index a5c81f24..29519f5d 100644 --- a/src/mmap.rs +++ b/src/mmap.rs @@ -29,15 +29,24 @@ 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}; +mod unix; #[cfg(all(feature = "xen", unix))] -pub use crate::mmap_xen::{Error as MmapRegionError, MmapRange, MmapRegion, MmapXenFlags}; +pub(crate) mod xen; #[cfg(windows)] -pub use crate::mmap_windows::MmapRegion; +mod windows; + +#[cfg(all(not(feature = "xen"), unix))] +pub use unix::{Error as MmapRegionError, MmapRegion, MmapRegionBuilder}; + +#[cfg(all(feature = "xen", unix))] +pub use xen::{Error as MmapRegionError, MmapRange, MmapRegion, MmapXenFlags}; + #[cfg(windows)] pub use std::io::Error as MmapRegionError; +#[cfg(windows)] +pub use windows::MmapRegion; /// A `Bitmap` that can be created starting from an initial size. pub trait NewBitmap: Bitmap + Default { 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..76f828b4 100644 --- a/src/volatile_memory.rs +++ b/src/volatile_memory.rs @@ -40,7 +40,7 @@ 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}; +use crate::mmap::xen::{MmapXen as MmapInfo, MmapXenSlice}; #[cfg(not(feature = "xen"))] type MmapInfo = std::marker::PhantomData<()>; From c321f0f57b4f5c61ccfeed5ab8f5697cfc68a6a4 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Fri, 13 Dec 2024 14:34:52 +0200 Subject: [PATCH 2/3] Replace cfg unix/windows with target_family cfg flags "unix" and "windows" are now aliases to target_family = "unix" and target_family = "windows" respectively for backwards compatibility, so use the non-legacy way of targeting those OS families. Signed-off-by: Manos Pitsidianakis --- Cargo.toml | 2 +- src/bitmap/backend/atomic_bitmap.rs | 4 ++-- src/bytes.rs | 2 +- src/lib.rs | 2 +- src/mmap.rs | 26 +++++++++++++------------- src/volatile_memory.rs | 14 +++++++------- 6 files changed, 25 insertions(+), 25 deletions(-) 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/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 de4984c3..b8fe5f40 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,7 +58,7 @@ 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.rs index 29519f5d..23cd8e33 100644 --- a/src/mmap.rs +++ b/src/mmap.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,24 +28,24 @@ use crate::guest_memory::{ use crate::volatile_memory::{VolatileMemory, VolatileSlice}; use crate::{AtomicAccess, Bytes, ReadVolatile, WriteVolatile}; -#[cfg(all(not(feature = "xen"), unix))] +#[cfg(all(not(feature = "xen"), target_family = "unix"))] mod unix; -#[cfg(all(feature = "xen", unix))] +#[cfg(all(feature = "xen", target_family = "unix"))] pub(crate) mod xen; -#[cfg(windows)] +#[cfg(target_family = "windows")] mod windows; -#[cfg(all(not(feature = "xen"), unix))] +#[cfg(all(not(feature = "xen"), target_family = "unix"))] pub use unix::{Error as MmapRegionError, MmapRegion, MmapRegionBuilder}; -#[cfg(all(feature = "xen", unix))] +#[cfg(all(feature = "xen", target_family = "unix"))] pub use xen::{Error as MmapRegionError, MmapRange, MmapRegion, MmapXenFlags}; -#[cfg(windows)] +#[cfg(target_family = "windows")] pub use std::io::Error as MmapRegionError; -#[cfg(windows)] +#[cfg(target_family = "windows")] pub use windows::MmapRegion; /// A `Bitmap` that can be created starting from an initial size. @@ -80,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 @@ -1042,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() @@ -1051,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); @@ -1060,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]); @@ -1179,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/volatile_memory.rs b/src/volatile_memory.rs index 76f828b4..43c1d206 100644 --- a/src/volatile_memory.rs +++ b/src/volatile_memory.rs @@ -39,7 +39,7 @@ use crate::atomic_integer::AtomicInteger; use crate::bitmap::{Bitmap, BitmapSlice, BS}; use crate::{AtomicAccess, ByteValued, Bytes}; -#[cfg(all(feature = "backend-mmap", feature = "xen", unix))] +#[cfg(all(feature = "backend-mmap", feature = "xen", target_family = "unix"))] use crate::mmap::xen::{MmapXen as MmapInfo, MmapXenSlice}; #[cfg(not(feature = "xen"))] @@ -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]); From 592031ec63334c915763723d8f8677eb41cf7ba7 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Mon, 24 Mar 2025 11:27:19 +0200 Subject: [PATCH 3/3] Rename mmap.rs to mmap/mod.rs for consistency Other modules with children modules in this crate use the /mod.rs structure except for mmap, fix the discrepancy. No functional changes. Signed-off-by: Manos Pitsidianakis --- src/{mmap.rs => mmap/mod.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{mmap.rs => mmap/mod.rs} (100%) diff --git a/src/mmap.rs b/src/mmap/mod.rs similarity index 100% rename from src/mmap.rs rename to src/mmap/mod.rs