sys_util: Convert mmap::Error over to thiserror::Error.

This conversion adds source attribution for wrapped errors and
implicitly implements std::error::Error for mmap::Error which was
missing before.

Also some compile time warnings were fixed for the mmap::arena_msync
test case.

BUG=None
TEST=cargo test

Change-Id: I13b6257692887bcd7229f1cb59c10edb850d0a65
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3207106
Auto-Submit: Allen Webb <allenwebb@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Allen Webb <allenwebb@google.com>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
This commit is contained in:
Allen Webb 2021-10-05 16:30:43 -05:00 committed by Commit Bot
parent f0dd0588ca
commit bfc3f64598

View file

@ -6,7 +6,6 @@
//! mmap object leaves scope.
use std::cmp::min;
use std::fmt::{self, Display};
use std::io;
use std::mem::size_of;
use std::os::unix::io::AsRawFd;
@ -19,54 +18,31 @@ use data_model::DataInit;
use crate::{errno, pagesize};
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// `add_fd_mapping` is not supported.
#[error("`add_fd_mapping` is unsupported")]
AddFdMappingIsUnsupported,
/// Requested memory out of range.
#[error("requested memory out of range")]
InvalidAddress,
/// Invalid argument provided when building mmap.
#[error("invalid argument provided when creating mapping")]
InvalidArgument,
/// Requested offset is out of range of `libc::off_t`.
#[error("requested offset is out of range of off_t")]
InvalidOffset,
/// Requested mapping is not page aligned
#[error("requested memory is not page aligned")]
NotPageAligned,
/// Requested memory range spans past the end of the region.
#[error("requested memory range spans past the end of the region: offset={0} count={1} region_size={2}")]
InvalidRange(usize, usize, usize),
/// `mmap` returned the given error.
SystemCallFailed(errno::Error),
/// Writing to memory failed
ReadToMemory(io::Error),
/// `remove_mapping` is not supported
#[error("mmap related system call failed: {0}")]
SystemCallFailed(#[source] errno::Error),
#[error("failed to read from file to memory: {0}")]
ReadToMemory(#[source] io::Error),
#[error("`remove_mapping` is unsupported")]
RemoveMappingIsUnsupported,
/// Reading from memory failed
WriteFromMemory(io::Error),
#[error("failed to write from memory to file: {0}")]
WriteFromMemory(#[source] io::Error),
}
pub type Result<T> = std::result::Result<T, Error>;
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;
match self {
AddFdMappingIsUnsupported => write!(f, "`add_fd_mapping` is unsupported"),
InvalidAddress => write!(f, "requested memory out of range"),
InvalidArgument => write!(f, "invalid argument provided when creating mapping"),
InvalidOffset => write!(f, "requested offset is out of range of off_t"),
NotPageAligned => write!(f, "requested memory is not page aligned"),
InvalidRange(offset, count, region_size) => write!(
f,
"requested memory range spans past the end of the region: offset={} count={} region_size={}",
offset, count, region_size,
),
SystemCallFailed(e) => write!(f, "mmap related system call failed: {}", e),
ReadToMemory(e) => write!(f, "failed to read from file to memory: {}", e),
RemoveMappingIsUnsupported => write!(f, "`remove_mapping` is unsupported"),
WriteFromMemory(e) => write!(f, "failed to write from memory to file: {}", e),
}
}
}
/// Memory access type for anonymous shared memory mapping.
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Protection(c_int);
@ -1154,10 +1130,10 @@ mod tests {
let size = 0x40000;
let m = MemoryMappingArena::new(size).unwrap();
let ps = pagesize();
MappedRegion::msync(&m, 0, ps).unwrap();
MappedRegion::msync(&m, 0, size).unwrap();
MappedRegion::msync(&m, ps, size - ps).unwrap();
let res = MappedRegion::msync(&m, ps, size).unwrap_err();
<dyn MappedRegion>::msync(&m, 0, ps).unwrap();
<dyn MappedRegion>::msync(&m, 0, size).unwrap();
<dyn MappedRegion>::msync(&m, ps, size - ps).unwrap();
let res = <dyn MappedRegion>::msync(&m, ps, size).unwrap_err();
match res {
Error::InvalidAddress => {}
e => panic!("unexpected error: {}", e),