Skip to content

Commit ba461bd

Browse files
committed
refactor(mmap): Add Error type for check_file_offset
This avoids duplicating all the variants across the unix/xen specific error types. Signed-off-by: Patrick Roy <[email protected]>
1 parent 5e4e1d4 commit ba461bd

File tree

3 files changed

+48
-37
lines changed

3 files changed

+48
-37
lines changed

src/mmap.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ impl NewBitmap for () {
4646
fn with_len(_len: usize) -> Self {}
4747
}
4848

49+
/// Errors that can occur during [`check_file_offset`]
50+
#[derive(Debug, thiserror::Error)]
51+
pub enum CheckFileOffsetError {
52+
/// Seeking the end of the file returned an error.
53+
#[error("Error seeking the end of the file: {0}")]
54+
SeekEnd(std::io::Error),
55+
/// Seeking the start of the file returned an error.
56+
#[error("Error seeking the start of the file: {0}")]
57+
SeekStart(std::io::Error),
58+
/// A mapping with offset + length > EOF was attempted.
59+
#[error("The specified file offset and length is greater then file length")]
60+
MappingPastEof,
61+
/// The specified file offset and length cause overflow when added.
62+
#[error("The specified file offset and length cause overflow when added")]
63+
InvalidOffsetLength,
64+
}
65+
4966
// TODO: use this for Windows as well after we redefine the Error type there.
5067
#[cfg(unix)]
5168
/// Checks if a mapping of `size` bytes fits at the provided `file_offset`.
@@ -55,20 +72,20 @@ impl NewBitmap for () {
5572
pub fn check_file_offset(
5673
file_offset: &FileOffset,
5774
size: usize,
58-
) -> result::Result<(), MmapRegionError> {
75+
) -> result::Result<(), CheckFileOffsetError> {
5976
let mut file = file_offset.file();
6077
let start = file_offset.start();
6178

6279
if let Some(end) = start.checked_add(size as u64) {
6380
let filesize = file
6481
.seek(SeekFrom::End(0))
65-
.map_err(MmapRegionError::SeekEnd)?;
66-
file.rewind().map_err(MmapRegionError::SeekStart)?;
82+
.map_err(CheckFileOffsetError::SeekEnd)?;
83+
file.rewind().map_err(CheckFileOffsetError::SeekStart)?;
6784
if filesize < end {
68-
return Err(MmapRegionError::MappingPastEof);
85+
return Err(CheckFileOffsetError::MappingPastEof);
6986
}
7087
} else {
71-
return Err(MmapRegionError::InvalidOffsetLength);
88+
return Err(CheckFileOffsetError::InvalidOffsetLength);
7289
}
7390

7491
Ok(())

src/mmap_unix.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ use std::result;
1717

1818
use crate::bitmap::{Bitmap, BS};
1919
use crate::guest_memory::FileOffset;
20-
use crate::mmap::{check_file_offset, NewBitmap};
20+
use crate::mmap::{check_file_offset, CheckFileOffsetError, NewBitmap};
2121
use crate::volatile_memory::{self, VolatileMemory, VolatileSlice};
2222

2323
/// Error conditions that may arise when creating a new `MmapRegion` object.
2424
#[derive(Debug, thiserror::Error)]
2525
pub enum Error {
26-
/// The specified file offset and length cause overflow when added.
27-
#[error("The specified file offset and length cause overflow when added")]
28-
InvalidOffsetLength,
2926
/// The specified pointer to the mapping is not page-aligned.
3027
#[error("The specified pointer to the mapping is not page-aligned")]
3128
InvalidPointer,
@@ -35,18 +32,12 @@ pub enum Error {
3532
/// Mappings using the same fd overlap in terms of file offset and length.
3633
#[error("Mappings using the same fd overlap in terms of file offset and length")]
3734
MappingOverlap,
38-
/// A mapping with offset + length > EOF was attempted.
39-
#[error("The specified file offset and length is greater then file length")]
40-
MappingPastEof,
4135
/// The `mmap` call returned an error.
4236
#[error("{0}")]
4337
Mmap(io::Error),
44-
/// Seeking the end of the file returned an error.
45-
#[error("Error seeking the end of the file: {0}")]
46-
SeekEnd(io::Error),
47-
/// Seeking the start of the file returned an error.
48-
#[error("Error seeking the start of the file: {0}")]
49-
SeekStart(io::Error),
38+
/// Error validating a [`FileOffset`]
39+
#[error("{0}")]
40+
ValidateFile(#[from] CheckFileOffsetError),
5041
}
5142

5243
pub type Result<T> = result::Result<T, Error>;
@@ -558,7 +549,10 @@ mod tests {
558549
prot,
559550
flags,
560551
);
561-
assert!(matches!(r.unwrap_err(), Error::InvalidOffsetLength));
552+
assert!(matches!(
553+
r.unwrap_err(),
554+
Error::ValidateFile(CheckFileOffsetError::InvalidOffsetLength)
555+
));
562556

563557
// Offset + size is greater than the size of the file (which is 0 at this point).
564558
let r = MmapRegion::build(
@@ -567,7 +561,10 @@ mod tests {
567561
prot,
568562
flags,
569563
);
570-
assert!(matches!(r.unwrap_err(), Error::MappingPastEof));
564+
assert!(matches!(
565+
r.unwrap_err(),
566+
Error::ValidateFile(CheckFileOffsetError::MappingPastEof)
567+
));
571568

572569
// MAP_FIXED was specified among the flags.
573570
let r = MmapRegion::build(

src/mmap_xen.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,18 @@ use tests::ioctl_with_ref;
2626

2727
use crate::bitmap::{Bitmap, BS};
2828
use crate::guest_memory::{FileOffset, GuestAddress};
29-
use crate::mmap::{check_file_offset, NewBitmap};
29+
use crate::mmap::{check_file_offset, CheckFileOffsetError, NewBitmap};
3030
use crate::volatile_memory::{self, VolatileMemory, VolatileSlice};
3131

3232
/// Error conditions that may arise when creating a new `MmapRegion` object.
3333
#[derive(Debug, thiserror::Error)]
3434
pub enum Error {
35-
/// The specified file offset and length cause overflow when added.
36-
#[error("The specified file offset and length cause overflow when added")]
37-
InvalidOffsetLength,
3835
/// The forbidden `MAP_FIXED` flag was specified.
3936
#[error("The forbidden `MAP_FIXED` flag was specified")]
4037
MapFixed,
41-
/// A mapping with offset + length > EOF was attempted.
42-
#[error("The specified file offset and length is greater then file length")]
43-
MappingPastEof,
4438
/// The `mmap` call returned an error.
4539
#[error("{0}")]
4640
Mmap(io::Error),
47-
/// Seeking the end of the file returned an error.
48-
#[error("Error seeking the end of the file: {0}")]
49-
SeekEnd(io::Error),
50-
/// Seeking the start of the file returned an error.
51-
#[error("Error seeking the start of the file: {0}")]
52-
SeekStart(io::Error),
5341
/// Invalid file offset.
5442
#[error("Invalid file offset")]
5543
InvalidFileOffset,
@@ -65,6 +53,9 @@ pub enum Error {
6553
/// Unexpected error.
6654
#[error("Unexpected error")]
6755
UnexpectedError,
56+
/// Error validating a [`FileOffset`]
57+
#[error("{0}")]
58+
ValidateFile(#[from] CheckFileOffsetError),
6859
}
6960

7061
type Result<T> = result::Result<T, Error>;
@@ -497,15 +488,15 @@ fn pages(size: usize) -> (usize, usize) {
497488
fn validate_file(file_offset: &Option<FileOffset>) -> Result<(i32, u64)> {
498489
let file_offset = match file_offset {
499490
Some(f) => f,
500-
None => return Err(Error::InvalidFileOffset),
491+
None => return Err(CheckFileOffsetError::InvalidOffsetLength.into()),
501492
};
502493

503494
let fd = file_offset.file().as_raw_fd();
504495
let f_offset = file_offset.start();
505496

506497
// We don't allow file offsets with Xen foreign mappings.
507498
if f_offset != 0 {
508-
return Err(Error::InvalidOffsetLength);
499+
return Err(CheckFileOffsetError::InvalidOffsetLength.into());
509500
}
510501

511502
Ok((fd, f_offset))
@@ -1134,7 +1125,10 @@ mod tests {
11341125
let mut range = MmapRange::initialized(true);
11351126
range.file_offset = Some(FileOffset::new(TempFile::new().unwrap().into_file(), 1));
11361127
let r = MmapXenForeign::new(&range);
1137-
assert!(matches!(r.unwrap_err(), Error::InvalidOffsetLength));
1128+
assert!(matches!(
1129+
r.unwrap_err(),
1130+
Error::ValidateFile(CheckFileOffsetError::InvalidOffsetLength)
1131+
));
11381132

11391133
let mut range = MmapRange::initialized(true);
11401134
range.size = 0;
@@ -1171,7 +1165,10 @@ mod tests {
11711165
let mut range = MmapRange::initialized(true);
11721166
range.file_offset = Some(FileOffset::new(TempFile::new().unwrap().into_file(), 1));
11731167
let r = MmapXenGrant::new(&range, MmapXenFlags::NO_ADVANCE_MAP);
1174-
assert!(matches!(r.unwrap_err(), Error::InvalidOffsetLength));
1168+
assert!(matches!(
1169+
r.unwrap_err(),
1170+
Error::ValidateFile(CheckFileOffsetError::InvalidOffsetLength)
1171+
));
11751172

11761173
let mut range = MmapRange::initialized(true);
11771174
range.size = 0;

0 commit comments

Comments
 (0)