From bfc3f64598798e51a96a344fd74f72ba5c5cf6c7 Mon Sep 17 00:00:00 2001 From: Allen Webb Date: Tue, 5 Oct 2021 16:30:43 -0500 Subject: [PATCH] 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 Tested-by: kokoro Commit-Queue: Allen Webb Reviewed-by: Chirantan Ekbote --- sys_util/src/mmap.rs | 60 +++++++++++++------------------------------- 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/sys_util/src/mmap.rs b/sys_util/src/mmap.rs index 576ccb410a..bfd4a0311c 100644 --- a/sys_util/src/mmap.rs +++ b/sys_util/src/mmap.rs @@ -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 = std::result::Result; -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(); + ::msync(&m, 0, ps).unwrap(); + ::msync(&m, 0, size).unwrap(); + ::msync(&m, ps, size - ps).unwrap(); + let res = ::msync(&m, ps, size).unwrap_err(); match res { Error::InvalidAddress => {} e => panic!("unexpected error: {}", e),