diff options
author | Sonny Rao <sonnyrao@chromium.org> | 2018-02-22 17:48:00 -0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-02-27 22:26:09 -0800 |
commit | d33407aa99fb798d6c3c9e5f4132f76354334c19 (patch) | |
tree | 3e53de640392847e4eb6f3846ed74c8b26273d39 /sys_util/src/guest_memory.rs | |
parent | 765deaafb45731060ec183688f727a74e1df0201 (diff) | |
download | crosvm-d33407aa99fb798d6c3c9e5f4132f76354334c19.tar crosvm-d33407aa99fb798d6c3c9e5f4132f76354334c19.tar.gz crosvm-d33407aa99fb798d6c3c9e5f4132f76354334c19.tar.bz2 crosvm-d33407aa99fb798d6c3c9e5f4132f76354334c19.tar.lz crosvm-d33407aa99fb798d6c3c9e5f4132f76354334c19.tar.xz crosvm-d33407aa99fb798d6c3c9e5f4132f76354334c19.tar.zst crosvm-d33407aa99fb798d6c3c9e5f4132f76354334c19.zip |
guest_memory: implement error trait
This implements the error trait so we can get more useful error information back on failures. BUG=chromium:797868 TEST=./build_test passes on all architectures TEST=crosvm runs on caroline Change-Id: Ia131b1eb14d24709c10f2ba752a962e822134a42 Reviewed-on: https://chromium-review.googlesource.com/938312 Commit-Ready: Sonny Rao <sonnyrao@chromium.org> Tested-by: Sonny Rao <sonnyrao@chromium.org> Reviewed-by: Sonny Rao <sonnyrao@chromium.org>
Diffstat (limited to 'sys_util/src/guest_memory.rs')
-rw-r--r-- | sys_util/src/guest_memory.rs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/sys_util/src/guest_memory.rs b/sys_util/src/guest_memory.rs index f9dc716..23c8a2f 100644 --- a/sys_util/src/guest_memory.rs +++ b/sys_util/src/guest_memory.rs @@ -7,6 +7,8 @@ use std::io::{Read, Write}; use std::result; use std::sync::Arc; +use std::error::{self, Error as GuestMemoryError}; +use std::fmt::{self, Display}; use data_model::DataInit; use data_model::volatile_memory::*; @@ -19,10 +21,30 @@ pub enum Error { MemoryAccess(GuestAddress, mmap::Error), MemoryMappingFailed(mmap::Error), MemoryRegionOverlap, - RegionOperationFailed, } pub type Result<T> = result::Result<T, Error>; +impl error::Error for Error { + fn description(&self) -> &str { + match self { + &Error::InvalidGuestAddress(_) => + "Invalid Guest Address", + &Error::MemoryAccess(_, _) => + "Invalid Guest Memory Access", + &Error::MemoryMappingFailed(_) => + "Failed to map guest memory", + &Error::MemoryRegionOverlap => + "Memory regions overlap", + } + } +} + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Guest memory error: {}", Error::description(self)) + } +} + struct MemoryRegion { mapping: MemoryMapping, guest_base: GuestAddress, |