Skip to content

Commit e7b1f7a

Browse files
committed
refactor: Add GuestMemoryMmapError
Add a specific error type for the GuestMemoryMmap related functions in mmap_unix.rs, to remove them from the global GuestRegionError. This was the last blocker to Xen/Mmap coexistence. Signed-off-by: Patrick Roy <[email protected]>
1 parent 87ce16c commit e7b1f7a

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/mmap_unix.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,16 @@ impl<B> Drop for MmapRegion<B> {
438438
}
439439
}
440440

441+
#[derive(Debug, thiserror::Error)]
442+
pub enum GuestMemoryMmapError {
443+
/// Error creating a `MmapRegion` object.
444+
#[error("{0}")]
445+
MmapRegion(#[from] Error),
446+
/// Error when calling [`GuestRegionCollection`] APIs
447+
#[error("{0}")]
448+
GuestRegion(#[from] GuestRegionError),
449+
}
450+
441451
/// [`GuestMemoryRegion`](trait.GuestMemoryRegion.html) implementation that mmaps the guest's
442452
/// memory region in the current process.
443453
///
@@ -480,15 +490,14 @@ impl<B: NewBitmap> GuestRegionMmap<B> {
480490
addr: GuestAddress,
481491
size: usize,
482492
file: Option<FileOffset>,
483-
) -> result::Result<Self, GuestRegionError> {
493+
) -> result::Result<Self, GuestMemoryMmapError> {
484494
let region = if let Some(ref f_off) = file {
485-
MmapRegion::from_file(f_off.clone(), size)
495+
MmapRegion::from_file(f_off.clone(), size)?
486496
} else {
487-
MmapRegion::new(size)
488-
}
489-
.map_err(GuestRegionError::MmapRegion)?;
497+
MmapRegion::new(size)?
498+
};
490499

491-
Self::new(region, addr)
500+
Ok(Self::new(region, addr)?)
492501
}
493502
}
494503

@@ -550,15 +559,17 @@ impl<B: NewBitmap> GuestMemoryMmap<B> {
550559
/// Creates a container and allocates anonymous memory for guest memory regions.
551560
///
552561
/// Valid memory regions are specified as a slice of (Address, Size) tuples sorted by Address.
553-
pub fn from_ranges(ranges: &[(GuestAddress, usize)]) -> result::Result<Self, GuestRegionError> {
562+
pub fn from_ranges(
563+
ranges: &[(GuestAddress, usize)],
564+
) -> result::Result<Self, GuestMemoryMmapError> {
554565
Self::from_ranges_with_files(ranges.iter().map(|r| (r.0, r.1, None)))
555566
}
556567

557568
/// Creates a container and allocates anonymous memory for guest memory regions.
558569
///
559570
/// Valid memory regions are specified as a sequence of (Address, Size, [`Option<FileOffset>`])
560571
/// tuples sorted by Address.
561-
pub fn from_ranges_with_files<A, T>(ranges: T) -> result::Result<Self, GuestRegionError>
572+
pub fn from_ranges_with_files<A, T>(ranges: T) -> result::Result<Self, GuestMemoryMmapError>
562573
where
563574
A: Borrow<(GuestAddress, usize, Option<FileOffset>)>,
564575
T: IntoIterator<Item = A>,
@@ -571,6 +582,7 @@ impl<B: NewBitmap> GuestMemoryMmap<B> {
571582
})
572583
.collect::<result::Result<Vec<_>, _>>()?,
573584
)
585+
.map_err(GuestMemoryMmapError::GuestRegion)
574586
}
575587
}
576588

src/region.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,13 @@ pub trait GuestMemoryRegion: Bytes<MemoryRegionAddress, E = GuestMemoryError> {
142142
}
143143
}
144144

145-
/// Errors that can occur when dealing with [`GuestRegion`]s, or collections thereof
145+
/// Errors that can occur when dealing with [`GuestRegion`]s, and collections thereof
146146
#[derive(Debug, thiserror::Error)]
147147
pub enum GuestRegionError {
148148
/// Adding the guest base address to the length of the underlying mapping resulted
149149
/// in an overflow.
150150
#[error("Adding the guest base address to the length of the underlying mapping resulted in an overflow")]
151-
#[cfg(feature = "backend-mmap")]
152151
InvalidGuestRegion,
153-
/// Error creating a `MmapRegion` object.
154-
#[error("{0}")]
155-
#[cfg(all(feature = "backend-mmap", unix, not(feature = "xen")))]
156-
MmapRegion(crate::MmapRegionError),
157152
/// No memory region found.
158153
#[error("No memory region found")]
159154
NoMemoryRegion,

0 commit comments

Comments
 (0)