diff options
author | David Tolnay <dtolnay@chromium.org> | 2019-03-01 18:07:56 -0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2019-03-02 17:41:31 -0800 |
commit | c69f97542a6071f78d48a743ee94119a93bc9471 (patch) | |
tree | 69ee27de806bf460c108219d9d2c64bd633cf35d | |
parent | 5e1b46cbd8cb031a12c0fb20e94670f1007fd789 (diff) | |
download | crosvm-c69f97542a6071f78d48a743ee94119a93bc9471.tar crosvm-c69f97542a6071f78d48a743ee94119a93bc9471.tar.gz crosvm-c69f97542a6071f78d48a743ee94119a93bc9471.tar.bz2 crosvm-c69f97542a6071f78d48a743ee94119a93bc9471.tar.lz crosvm-c69f97542a6071f78d48a743ee94119a93bc9471.tar.xz crosvm-c69f97542a6071f78d48a743ee94119a93bc9471.tar.zst crosvm-c69f97542a6071f78d48a743ee94119a93bc9471.zip |
error: Consistently use Display instead of error description()
The description method is deprecated and its signature forces less helpful error messages than what Display can provide. BUG=none TEST=cargo check --all-features TEST=cargo check --target aarch64-unknown-linux-gnu Change-Id: I27fc99d59d0ef457c5273dc53e4c563ef439c2c0 Reviewed-on: https://chromium-review.googlesource.com/1497735 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Dylan Reid <dgreid@chromium.org>
-rw-r--r-- | aarch64/src/lib.rs | 45 | ||||
-rw-r--r-- | arch/src/fdt.rs | 52 | ||||
-rw-r--r-- | arch/src/lib.rs | 48 | ||||
-rw-r--r-- | data_model/src/volatile_memory.rs | 12 | ||||
-rw-r--r-- | devices/src/pci/ac97_bus_master.rs | 2 | ||||
-rw-r--r-- | devices/src/pit.rs | 10 | ||||
-rw-r--r-- | devices/src/proxy.rs | 11 | ||||
-rw-r--r-- | devices/src/virtio/p9.rs | 11 | ||||
-rw-r--r-- | devices/src/virtio/wl.rs | 51 | ||||
-rw-r--r-- | gpu_buffer/src/lib.rs | 22 | ||||
-rw-r--r-- | gpu_renderer/src/lib.rs | 9 | ||||
-rw-r--r-- | io_jail/src/lib.rs | 50 | ||||
-rw-r--r-- | kernel_cmdline/src/kernel_cmdline.rs | 24 | ||||
-rw-r--r-- | kernel_loader/src/lib.rs | 43 | ||||
-rw-r--r-- | net_util/src/lib.rs | 13 | ||||
-rw-r--r-- | src/argument.rs | 20 | ||||
-rw-r--r-- | src/linux.rs | 124 | ||||
-rw-r--r-- | src/plugin/mod.rs | 4 | ||||
-rw-r--r-- | sync/src/mutex.rs | 3 | ||||
-rw-r--r-- | sys_util/src/errno.rs | 7 | ||||
-rw-r--r-- | sys_util/src/guest_memory.rs | 17 | ||||
-rw-r--r-- | sys_util/src/syslog.rs | 26 | ||||
-rw-r--r-- | x86_64/src/cpuid.rs | 17 | ||||
-rw-r--r-- | x86_64/src/interrupts.rs | 17 | ||||
-rw-r--r-- | x86_64/src/lib.rs | 49 | ||||
-rw-r--r-- | x86_64/src/mptable.rs | 35 | ||||
-rw-r--r-- | x86_64/src/regs.rs | 35 |
27 files changed, 345 insertions, 412 deletions
diff --git a/aarch64/src/lib.rs b/aarch64/src/lib.rs index 17ec3d9..e891f58 100644 --- a/aarch64/src/lib.rs +++ b/aarch64/src/lib.rs @@ -14,7 +14,6 @@ extern crate resources; extern crate sync; extern crate sys_util; -use std::error::{self, Error as Aarch64Error}; use std::ffi::{CStr, CString}; use std::fmt::{self, Display}; use std::fs::File; @@ -133,7 +132,7 @@ pub enum Error { /// Unable to create Vcpu. CreateVcpu(sys_util::Error), /// FDT could not be created - FDTCreateFailure(Box<error::Error>), + FDTCreateFailure(Box<std::error::Error>), /// Kernel could not be loaded KernelLoadFailure(arch::LoadImageError), /// Initrd could not be loaded @@ -150,31 +149,29 @@ pub enum Error { VCPUSetRegFailure, } -impl error::Error for Error { - fn description(&self) -> &str { - match self { - &Error::CloneEventFd(_) => "Unable to clone an EventFd", - &Error::Cmdline(_) => "the given kernel command line was invalid", - &Error::CreateEventFd(_) => "Unable to make an EventFd", - &Error::CreateKvm(_) => "failed to open /dev/kvm", - &Error::CreatePciRoot(_) => "failed to create a PCI root hub", - &Error::CreateSocket(_) => "failed to create socket", - &Error::CreateVcpu(_) => "failed to create VCPU", - &Error::FDTCreateFailure(_) => "FDT could not be created", - &Error::KernelLoadFailure(_) => "Kernel cound not be loaded", - &Error::InitrdLoadFailure(_) => "initrd cound not be loaded", - &Error::CreateGICFailure(_) => "Failure to create GIC", - &Error::RegisterPci(_) => "error registering PCI bus", - &Error::RegisterVsock(_) => "error registering virtual socket device", - &Error::VCPUInitFailure => "Failed to initialize VCPU", - &Error::VCPUSetRegFailure => "Failed to set register", - } - } -} +impl std::error::Error for Error {} impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Aarch64 Error: {}", Error::description(self)) + use self::Error::*; + + match self { + CloneEventFd(e) => write!(f, "unable to clone an EventFd: {}", e), + Cmdline(e) => write!(f, "the given kernel command line was invalid: {}", e), + CreateEventFd(e) => write!(f, "unable to make an EventFd: {}", e), + CreateKvm(e) => write!(f, "failed to open /dev/kvm: {}", e), + CreatePciRoot(e) => write!(f, "failed to create a PCI root hub: {}", e), + CreateSocket(e) => write!(f, "failed to create socket: {}", e), + CreateVcpu(e) => write!(f, "failed to create VCPU: {}", e), + FDTCreateFailure(e) => write!(f, "FDT could not be created: {}", e), + KernelLoadFailure(e) => write!(f, "kernel cound not be loaded: {}", e), + InitrdLoadFailure(e) => write!(f, "initrd cound not be loaded: {}", e), + CreateGICFailure(e) => write!(f, "failed to create GIC: {}", e), + RegisterPci(e) => write!(f, "error registering PCI bus: {}", e), + RegisterVsock(e) => write!(f, "error registering virtual socket device: {}", e), + VCPUInitFailure => write!(f, "failed to initialize VCPU"), + VCPUSetRegFailure => write!(f, "failed to set register"), + } } } diff --git a/arch/src/fdt.rs b/arch/src/fdt.rs index 20a017b..5b4961d 100644 --- a/arch/src/fdt.rs +++ b/arch/src/fdt.rs @@ -4,9 +4,8 @@ use byteorder::{BigEndian, ByteOrder}; use libc::{c_char, c_int, c_void}; -use std::error::{self, Error as FdtError}; use std::ffi::{CStr, CString}; -use std::fmt; +use std::fmt::{self, Display}; use std::ptr::null; // This links to libfdt which handles the creation of the binary blob @@ -38,43 +37,24 @@ pub enum Error { FdtGuestMemoryWriteError, } -impl error::Error for Error { - fn description(&self) -> &str { - match self { - &Error::FdtCreateError(_) => "Error creating FDT", - &Error::FdtFinishReservemapError(_) => "Error finishing reserve map", - &Error::FdtBeginNodeError(_) => "Error beginning FDT node", - &Error::FdtPropertyError(_) => "Error adding FDT property", - &Error::FdtEndNodeError(_) => "Error ending FDT node", - &Error::FdtOpenIntoError(_) => "Error copying FDT to Guest", - &Error::FdtFinishError(_) => "Error performing FDT finish", - &Error::FdtPackError(_) => "Error packing FDT", - &Error::FdtGuestMemoryWriteError => "Error writing FDT to Guest Memory", - } - } -} +impl std::error::Error for Error {} -impl fmt::Display for Error { +impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let prefix = "Libfdt Error: "; + use self::Error::*; + + write!(f, "libfdt: ")?; + match self { - &Error::FdtCreateError(fdt_ret) - | &Error::FdtFinishReservemapError(fdt_ret) - | &Error::FdtBeginNodeError(fdt_ret) - | &Error::FdtPropertyError(fdt_ret) - | &Error::FdtEndNodeError(fdt_ret) - | &Error::FdtOpenIntoError(fdt_ret) - | &Error::FdtFinishError(fdt_ret) - | &Error::FdtPackError(fdt_ret) => write!( - f, - "{} {} code: {}", - prefix, - Error::description(self), - fdt_ret - ), - &Error::FdtGuestMemoryWriteError => { - write!(f, "{} {}", prefix, Error::description(self)) - } + FdtCreateError(ret) => write!(f, "error creating FDT, code={}", ret), + FdtFinishReservemapError(ret) => write!(f, "error finishing reserve map, code={}", ret), + FdtBeginNodeError(ret) => write!(f, "error beginning FDT node, code={}", ret), + FdtPropertyError(ret) => write!(f, "error adding FDT property, code={}", ret), + FdtEndNodeError(ret) => write!(f, "error ending FDT node, code={}", ret), + FdtOpenIntoError(ret) => write!(f, "error copying FDT to Guest, code={}", ret), + FdtFinishError(ret) => write!(f, "error performing FDT finish, code={}", ret), + FdtPackError(ret) => write!(f, "error packing FDT, code={}", ret), + FdtGuestMemoryWriteError => write!(f, "error writing FDT to guest memory"), } } } diff --git a/arch/src/lib.rs b/arch/src/lib.rs index 4596ee2..d725baa 100644 --- a/arch/src/lib.rs +++ b/arch/src/lib.rs @@ -15,7 +15,7 @@ extern crate sync; extern crate sys_util; use std::collections::BTreeMap; -use std::fmt; +use std::fmt::{self, Display}; use std::fs::File; use std::io::{Read, Seek, SeekFrom}; use std::os::unix::io::AsRawFd; @@ -118,37 +118,23 @@ pub enum DeviceRegistrationError { AddrsExhausted, } -impl fmt::Display for DeviceRegistrationError { +impl Display for DeviceRegistrationError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::DeviceRegistrationError::*; + match self { - DeviceRegistrationError::AllocateIoAddrs(e) => { - write!(f, "Allocating IO addresses: {}", e) - } - DeviceRegistrationError::AllocateDeviceAddrs(e) => { - write!(f, "Allocating device addresses: {:?}", e) - } - DeviceRegistrationError::AllocateIrq => write!(f, "Allocating IRQ number"), - DeviceRegistrationError::CreateMmioDevice(e) => { - write!(f, "failed to create mmio device: {}", e) - } - DeviceRegistrationError::Cmdline(e) => { - write!(f, "unable to add device to kernel command line: {}", e) - } - DeviceRegistrationError::EventFdCreate(e) => { - write!(f, "failed to create eventfd: {}", e) - } - DeviceRegistrationError::MmioInsert(e) => write!(f, "failed to add to mmio bus: {}", e), - DeviceRegistrationError::RegisterIoevent(e) => { - write!(f, "failed to register ioevent to VM: {}", e) - } - DeviceRegistrationError::RegisterIrqfd(e) => { - write!(f, "failed to register irq eventfd to VM: {}", e) - } - DeviceRegistrationError::ProxyDeviceCreation(e) => { - write!(f, "failed to create proxy device: {}", e) - } - DeviceRegistrationError::IrqsExhausted => write!(f, "no more IRQs are available"), - DeviceRegistrationError::AddrsExhausted => write!(f, "no more addresses are available"), + AllocateIoAddrs(e) => write!(f, "Allocating IO addresses: {}", e), + AllocateDeviceAddrs(e) => write!(f, "Allocating device addresses: {}", e), + AllocateIrq => write!(f, "Allocating IRQ number"), + CreateMmioDevice(e) => write!(f, "failed to create mmio device: {}", e), + Cmdline(e) => write!(f, "unable to add device to kernel command line: {}", e), + EventFdCreate(e) => write!(f, "failed to create eventfd: {}", e), + MmioInsert(e) => write!(f, "failed to add to mmio bus: {}", e), + RegisterIoevent(e) => write!(f, "failed to register ioevent to VM: {}", e), + RegisterIrqfd(e) => write!(f, "failed to register irq eventfd to VM: {}", e), + ProxyDeviceCreation(e) => write!(f, "failed to create proxy device: {}", e), + IrqsExhausted => write!(f, "no more IRQs are available"), + AddrsExhausted => write!(f, "no more addresses are available"), } } } @@ -233,7 +219,7 @@ pub enum LoadImageError { ReadToMemory(GuestMemoryError), } -impl fmt::Display for LoadImageError { +impl Display for LoadImageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use self::LoadImageError::*; diff --git a/data_model/src/volatile_memory.rs b/data_model/src/volatile_memory.rs index 450d92e..5a2c80a 100644 --- a/data_model/src/volatile_memory.rs +++ b/data_model/src/volatile_memory.rs @@ -20,7 +20,7 @@ //! not reordered or elided the access. use std::cmp::min; -use std::fmt; +use std::fmt::{self, Display}; use std::io::Result as IoResult; use std::io::{Read, Write}; use std::marker::PhantomData; @@ -41,13 +41,13 @@ pub enum VolatileMemoryError { Overflow { base: u64, offset: u64 }, } -impl fmt::Display for VolatileMemoryError { +impl Display for VolatileMemoryError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::VolatileMemoryError::*; + match self { - VolatileMemoryError::OutOfBounds { addr } => { - write!(f, "address 0x{:x} is out of bounds", addr) - } - VolatileMemoryError::Overflow { base, offset } => write!( + OutOfBounds { addr } => write!(f, "address 0x{:x} is out of bounds", addr), + Overflow { base, offset } => write!( f, "address 0x{:x} offset by 0x{:x} would overflow", base, offset diff --git a/devices/src/pci/ac97_bus_master.rs b/devices/src/pci/ac97_bus_master.rs index b67450e..4ea9097 100644 --- a/devices/src/pci/ac97_bus_master.rs +++ b/devices/src/pci/ac97_bus_master.rs @@ -78,7 +78,7 @@ enum PlaybackError { WritingOutput(std::io::Error), } -impl Error for PlaybackError {} +impl std::error::Error for PlaybackError {} impl Display for PlaybackError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/devices/src/pit.rs b/devices/src/pit.rs index 398ff86..6f508c7 100644 --- a/devices/src/pit.rs +++ b/devices/src/pit.rs @@ -3,7 +3,7 @@ // found in the LICENSE file. // Based heavily on GCE VMM's pit.cc. -use std::fmt; +use std::fmt::{self, Display}; use std::io::Error as IoError; use std::os::unix::io::AsRawFd; use std::sync::Arc; @@ -159,7 +159,7 @@ pub enum PitError { CloneEventFd(SysError), } -impl fmt::Display for PitError { +impl Display for PitError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use self::PitError::*; @@ -176,11 +176,7 @@ impl fmt::Display for PitError { } } -impl std::error::Error for PitError { - fn description(&self) -> &str { - "Pit failure" - } -} +impl std::error::Error for PitError {} type PitResult<T> = std::result::Result<T, PitError>; diff --git a/devices/src/proxy.rs b/devices/src/proxy.rs index a91d2ed..e7cc95f 100644 --- a/devices/src/proxy.rs +++ b/devices/src/proxy.rs @@ -6,10 +6,11 @@ use libc::pid_t; +use std::fmt::{self, Display}; use std::os::unix::io::{AsRawFd, RawFd}; use std::process; use std::time::Duration; -use std::{self, fmt, io}; +use std::{self, io}; use io_jail::{self, Minijail}; use msg_socket::{MsgOnSocket, MsgReceiver, MsgSender, MsgSocket}; @@ -25,11 +26,13 @@ pub enum Error { } pub type Result<T> = std::result::Result<T, Error>; -impl fmt::Display for Error { +impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Error::*; + match self { - Error::ForkingJail(_) => write!(f, "Failed to fork jail process"), - Error::Io(e) => write!(f, "IO error configuring proxy device {}.", e), + ForkingJail(e) => write!(f, "Failed to fork jail process: {}", e), + Io(e) => write!(f, "IO error configuring proxy device {}.", e), } } } diff --git a/devices/src/virtio/p9.rs b/devices/src/virtio/p9.rs index be461a0..636a1dc 100644 --- a/devices/src/virtio/p9.rs +++ b/devices/src/virtio/p9.rs @@ -3,8 +3,7 @@ // found in the LICENSE file. use std::cmp::min; -use std::error; -use std::fmt; +use std::fmt::{self, Display}; use std::io::{self, Read, Write}; use std::iter::Peekable; use std::mem; @@ -52,13 +51,9 @@ pub enum P9Error { Internal(io::Error), } -impl error::Error for P9Error { - fn description(&self) -> &str { - "An error occurred in the virtio 9P device" - } -} +impl std::error::Error for P9Error {} -impl fmt::Display for P9Error { +impl Display for P9Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use self::P9Error::*; diff --git a/devices/src/virtio/wl.rs b/devices/src/virtio/wl.rs index 0220d42..a9498fe 100644 --- a/devices/src/virtio/wl.rs +++ b/devices/src/virtio/wl.rs @@ -32,9 +32,8 @@ use std::cell::RefCell; use std::collections::btree_map::Entry; use std::collections::{BTreeMap as Map, BTreeSet as Set, VecDeque}; use std::convert::From; -use std::error::{self, Error as StdError}; use std::ffi::CStr; -use std::fmt; +use std::fmt::{self, Display}; use std::fs::File; use std::io::{self, Read, Seek, SeekFrom}; use std::mem::{size_of, size_of_val}; @@ -442,35 +441,33 @@ enum WlError { DmabufSync(io::Error), } -impl fmt::Display for WlError { +impl Display for WlError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.description()) - } -} - -impl error::Error for WlError { - fn description(&self) -> &str { - match *self { - WlError::NewAlloc(_) => "Failed to create shared memory allocation", - WlError::NewPipe(_) => "Failed to create pipe", - WlError::AllocSetSize(_) => "Failed to set size of shared memory", - WlError::SocketConnect(_) => "Failed to connect socket", - WlError::SocketNonBlock(_) => "Failed to set socket as non-blocking", - WlError::VmControl(_) => "Failed to control parent VM", - WlError::VmBadResponse => "Invalid response from parent VM", - WlError::CheckedOffset => "Overflow in calculation", - WlError::GuestMemory(_) => "Access violation in guest memory", - WlError::VolatileMemory(_) => "Access violating in guest volatile memory", - WlError::SendVfd(_) => "Failed to send on a socket", - WlError::WritePipe(_) => "Failed to write to a pipe", - WlError::RecvVfd(_) => "Failed to recv on a socket", - WlError::ReadPipe(_) => "Failed to read a pipe", - WlError::PollContextAdd(_) => "Failed to listen to FD on poll context", - WlError::DmabufSync(_) => "Failed to synchronize DMABuf access", + use self::WlError::*; + + match self { + NewAlloc(e) => write!(f, "failed to create shared memory allocation: {}", e), + NewPipe(e) => write!(f, "failed to create pipe: {}", e), + AllocSetSize(e) => write!(f, "failed to set size of shared memory: {}", e), + SocketConnect(e) => write!(f, "failed to connect socket: {}", e), + SocketNonBlock(e) => write!(f, "failed to set socket as non-blocking: {}", e), + VmControl(e) => write!(f, "failed to control parent VM: {}", e), + VmBadResponse => write!(f, "invalid response from parent VM"), + CheckedOffset => write!(f, "overflow in calculation"), + GuestMemory(e) => write!(f, "access violation in guest memory: {}", e), + VolatileMemory(e) => write!(f, "access violating in guest volatile memory: {}", e), + SendVfd(e) => write!(f, "failed to send on a socket: {}", e), + WritePipe(e) => write!(f, "failed to write to a pipe: {}", e), + RecvVfd(e) => write!(f, "failed to recv on a socket: {}", e), + ReadPipe(e) => write!(f, "failed to read a pipe: {}", e), + PollContextAdd(e) => write!(f, "failed to listen to FD on poll context: {}", e), + DmabufSync(e) => write!(f, "failed to synchronize DMABuf access: {}", e), } } } +impl std::error::Error for WlError {} + type WlResult<T> = result::Result<T, WlError>; impl From<GuestMemoryError> for WlError { @@ -648,7 +645,7 @@ enum WlResp<'a> { VfdHup { id: u32, }, - Err(Box<error::Error>), + Err(Box<std::error::Error>), OutOfMemory, InvalidId, InvalidType, diff --git a/gpu_buffer/src/lib.rs b/gpu_buffer/src/lib.rs index 112b086..ea77117 100644 --- a/gpu_buffer/src/lib.rs +++ b/gpu_buffer/src/lib.rs @@ -39,7 +39,7 @@ mod raw; pub mod rendernode; use std::cmp::min; -use std::fmt; +use std::fmt::{self, Display}; use std::fs::File; use std::isize; use std::os::raw::c_void; @@ -74,13 +74,15 @@ pub enum Error { Memcopy(VolatileMemoryError), } -impl fmt::Display for Error { +impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Error::GbmFailed => write!(f, "internal GBM failure"), - Error::ExportFailed(e) => write!(f, "export failed: {}", e), - Error::MapFailed => write!(f, "map failed"), - Error::CheckedArithmetic { + use self::Error::*; + + match self { + GbmFailed => write!(f, "internal GBM failure"), + ExportFailed(e) => write!(f, "export failed: {}", e), + MapFailed => write!(f, "map failed"), + CheckedArithmetic { field1: (label1, value1), field2: (label2, value2), op, @@ -89,7 +91,7 @@ impl fmt::Display for Error { "arithmetic failed: {}({}) {} {}({})", label1, value1, op, label2, value2 ), - Error::InvalidPrecondition { + InvalidPrecondition { field1: (label1, value1), field2: (label2, value2), op, @@ -98,8 +100,8 @@ impl fmt::Display for Error { "invalid precondition: {}({}) {} {}({})", label1, value1, op, label2, value2 ), - Error::UnknownFormat(format) => write!(f, "unknown format {:?}", format), - Error::Memcopy(ref e) => write!(f, "error copying memory: {}", e), + UnknownFormat(format) => write!(f, "unknown format {:?}", format), + Memcopy(e) => write!(f, "error copying memory: {}", e), } } } diff --git a/gpu_renderer/src/lib.rs b/gpu_renderer/src/lib.rs index 0dfd34c..81c99db 100644 --- a/gpu_renderer/src/lib.rs +++ b/gpu_renderer/src/lib.rs @@ -14,7 +14,7 @@ mod pipe_format_fourcc; use std::cell::RefCell; use std::ffi::CStr; -use std::fmt; +use std::fmt::{self, Display}; use std::fs::File; use std::marker::PhantomData; use std::mem::{size_of, transmute, uninitialized}; @@ -81,10 +81,11 @@ pub enum Error { InvalidCommandSize(usize), } -impl fmt::Display for Error { +impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use Error::*; - match *self { + use self::Error::*; + + match self { AlreadyInitialized => write!(f, "global gpu renderer was already initailized"), MissingEGLFunction(name) => write!(f, "egl function `{}` was missing", name), EGLGetDisplay => write!(f, "call to eglGetDisplay failed"), diff --git a/io_jail/src/lib.rs b/io_jail/src/lib.rs index ef0b935..26f96d7 100644 --- a/io_jail/src/lib.rs +++ b/io_jail/src/lib.rs @@ -12,7 +12,7 @@ mod libminijail; use libc::pid_t; use std::ffi::CString; -use std::fmt; +use std::fmt::{self, Display}; use std::fs; use std::io; use std::os::unix::io::{AsRawFd, RawFd}; @@ -70,17 +70,19 @@ pub enum Error { PreservingFd(i32), } -impl fmt::Display for Error { +impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Error::*; + match self { - Error::BindMount { src, dst, errno } => write!( + BindMount { src, dst, errno } => write!( f, "failed to accept bind mount {} -> {}: {}", src.display(), dst.display(), io::Error::from_raw_os_error(*errno), ), - Error::Mount { + Mount { errno, src, dest, @@ -98,57 +100,49 @@ impl fmt::Display for Error { data, io::Error::from_raw_os_error(*errno), ), - Error::CheckingMultiThreaded(e) => write!( + CheckingMultiThreaded(e) => write!( f, "Failed to count the number of threads from /proc/self/tasks {}", e ), - Error::CreatingMinijail => write!(f, "minjail_new failed due to an allocation failure"), - Error::ForkingMinijail(e) => write!(f, "minijail_fork failed with error {}", e), - Error::ForkingWhileMultiThreaded => { - write!(f, "Attempt to call fork() while multithreaded") - } - Error::SeccompPath(p) => write!(f, "missing seccomp policy path: {}", p.display()), - Error::StrToCString(s) => write!(f, "failed to convert string into CString: {}", s), - Error::PathToCString(s) => { - write!(f, "failed to convert path into CString: {}", s.display()) - } - Error::DupDevNull(errno) => write!( + CreatingMinijail => write!(f, "minjail_new failed due to an allocation failure"), + ForkingMinijail(e) => write!(f, "minijail_fork failed with error {}", e), + ForkingWhileMultiThreaded => write!(f, "Attempt to call fork() while multithreaded"), + SeccompPath(p) => write!(f, "missing seccomp policy path: {}", p.display()), + StrToCString(s) => write!(f, "failed to convert string into CString: {}", s), + PathToCString(s) => write!(f, "failed to convert path into CString: {}", s.display()), + DupDevNull(errno) => write!( f, "failed to call dup2 to set stdin, stdout, or stderr to /dev/null: {}", io::Error::from_raw_os_error(*errno), ), - Error::OpenDevNull(e) => write!( + OpenDevNull(e) => write!( f, "fail to open /dev/null for setting FDs 0, 1, or 2: {}", e, ), - Error::SetAltSyscallTable { name, errno } => write!( + SetAltSyscallTable { name, errno } => write!( f, "failed to set alt-syscall table {}: {}", name, io::Error::from_raw_os_error(*errno), ), - Error::SettingChrootDirectory(errno, p) => write!( + SettingChrootDirectory(errno, p) => write!( f, "failed to set chroot {}: {}", p.display(), io::Error::from_raw_os_error(*errno), ), - Error::SettingPivotRootDirectory(errno, p) => write!( + SettingPivotRootDirectory(errno, p) => write!( f, "failed to set pivot root {}: {}", p.display(), io::Error::from_raw_os_error(*errno), ), - Error::ReadFdDirEntry(e) => { - write!(f, "failed to read an entry in /proc/self/fd: {}", e) - } - Error::ReadFdDir(e) => write!(f, "failed to open /proc/self/fd: {}", e), - Error::ProcFd(s) => write!(f, "an entry in /proc/self/fd is not an integer: {}", s), - Error::PreservingFd(e) => { - write!(f, "fork failed in minijail_preserve_fd with error {}", e) - } + ReadFdDirEntry(e) => write!(f, "failed to read an entry in /proc/self/fd: {}", e), + ReadFdDir(e) => write!(f, "failed to open /proc/self/fd: {}", e), + ProcFd(s) => write!(f, "an entry in /proc/self/fd is not an integer: {}", s), + PreservingFd(e) => write!(f, "fork failed in minijail_preserve_fd with error {}", e), } } } diff --git a/kernel_cmdline/src/kernel_cmdline.rs b/kernel_cmdline/src/kernel_cmdline.rs index 1e1870e..7284004 100644 --- a/kernel_cmdline/src/kernel_cmdline.rs +++ b/kernel_cmdline/src/kernel_cmdline.rs @@ -4,7 +4,7 @@ //! Helper for creating valid kernel command line strings. -use std::fmt; +use std::fmt::{self, Display}; use std::result; /// The error type for command line building operations. @@ -20,18 +20,18 @@ pub enum Error { TooLarge, } -impl fmt::Display for Error { +impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{}", - match *self { - Error::InvalidAscii => "string contains non-printable ASCII character", - Error::HasSpace => "string contains a space", - Error::HasEquals => "string contains an equals sign", - Error::TooLarge => "inserting string would make command line too long", - } - ) + use self::Error::*; + + let description = match self { + InvalidAscii => "string contains non-printable ASCII character", + HasSpace => "string contains a space", + HasEquals => "string contains an equals sign", + TooLarge => "inserting string would make command line too long", + }; + + write!(f, "{}", description) } } diff --git a/kernel_loader/src/lib.rs b/kernel_loader/src/lib.rs index ecc5a34..15693a6 100644 --- a/kernel_loader/src/lib.rs +++ b/kernel_loader/src/lib.rs @@ -4,7 +4,6 @@ extern crate sys_util; -use std::error::{self, Error as KernelLoaderError}; use std::ffi::CStr; use std::fmt::{self, Display}; use std::io::{Read, Seek, SeekFrom}; @@ -37,31 +36,29 @@ pub enum Error { } pub type Result<T> = std::result::Result<T, Error>; -impl error::Error for Error { - fn description(&self) -> &str { - match self { - Error::BigEndianElfOnLittle => { - "Trying to load big-endian binary on little-endian machine" - } - Error::CommandLineCopy => "Failed writing command line to guest memory", - Error::CommandLineOverflow => "Command line overflowed guest memory", - Error::InvalidElfMagicNumber => "Invalid Elf magic number", - Error::InvalidProgramHeaderSize => "Invalid program header size", - Error::InvalidProgramHeaderOffset => "Invalid program header offset", - Error::InvalidProgramHeaderAddress => "Invalid Program Header Address", - Error::ReadElfHeader => "Unable to read elf header", - Error::ReadKernelImage => "Unable to read kernel image", - Error::ReadProgramHeader => "Unable to read program header", - Error::SeekKernelStart => "Unable to seek to kernel start", - Error::SeekElfStart => "Unable to seek to elf start", - Error::SeekProgramHeader => "Unable to seek to program header", - } - } -} +impl std::error::Error for Error {} impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Kernel Loader Error: {}", Error::description(self)) + use self::Error::*; + + let description = match self { + BigEndianElfOnLittle => "trying to load big-endian binary on little-endian machine", + CommandLineCopy => "failed writing command line to guest memory", + CommandLineOverflow => "command line overflowed guest memory", + InvalidElfMagicNumber => "invalid Elf magic number", + InvalidProgramHeaderSize => "invalid program header size", + InvalidProgramHeaderOffset => "invalid program header offset", + InvalidProgramHeaderAddress => "invalid Program Header Address", + ReadElfHeader => "unable to read elf header", + ReadKernelImage => "unable to read kernel image", + ReadProgramHeader => "unable to read program header", + SeekKernelStart => "unable to seek to kernel start", + SeekElfStart => "unable to seek to elf start", + SeekProgramHeader => "unable to seek to program header", + }; + + write!(f, "kernel loader: {}", description) } } diff --git a/net_util/src/lib.rs b/net_util/src/lib.rs index 790fbc3..066b7d8 100644 --- a/net_util/src/lib.rs +++ b/net_util/src/lib.rs @@ -101,14 +101,17 @@ pub enum MacAddressError { ParseOctet(ParseIntError), } -impl fmt::Display for MacAddressError { +impl Display for MacAddressError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - MacAddressError::InvalidNumOctets(n) => write!(f, "invalid number of octets: {}", n), - MacAddressError::ParseOctet(ref e) => write!(f, "failed to parse octet: {}", e), + use self::MacAddressError::*; + + match self { + InvalidNumOctets(n) => write!(f, "invalid number of octets: {}", n), + ParseOctet(e) => write!(f, "failed to parse octet: {}", e), } } } + /// An Ethernet mac address. This struct is compatible with the C `struct sockaddr`. #[repr(C)] #[derive(Clone, Copy)] @@ -147,7 +150,7 @@ impl FromStr for MacAddress { } } -impl fmt::Display for MacAddress { +impl Display for MacAddress { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, diff --git a/src/argument.rs b/src/argument.rs index baadb02..224a17c 100644 --- a/src/argument.rs +++ b/src/argument.rs @@ -40,7 +40,7 @@ //! } //! ``` -use std::fmt; +use std::fmt::{self, Display}; use std::result; /// An error with argument parsing. @@ -64,18 +64,20 @@ pub enum Error { PrintHelp, } -impl fmt::Display for Error { +impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Error::*; + match self { - Error::Syntax(s) => write!(f, "syntax error: {}", s), - Error::UnknownArgument(s) => write!(f, "unknown argument: {}", s), - Error::ExpectedArgument(s) => write!(f, "expected argument: {}", s), - Error::InvalidValue { value, expected } => { + Syntax(s) => write!(f, "syntax error: {}", s), + UnknownArgument(s) => write!(f, "unknown argument: {}", s), + ExpectedArgument(s) => write!(f, "expected argument: {}", s), + InvalidValue { value, expected } => { write!(f, "invalid value {:?}: {}", value, expected) } - Error::TooManyArguments(s) => write!(f, "too many arguments: {}", s), - Error::ExpectedValue(s) => write!(f, "expected parameter value: {}", s), - Error::PrintHelp => write!(f, "help was requested"), + TooManyArguments(s) => write!(f, "too many arguments: {}", s), + ExpectedValue(s) => write!(f, "expected parameter value: {}", s), + PrintHelp => write!(f, "help was requested"), } } } diff --git a/src/linux.rs b/src/linux.rs index a8f7ae9..ab83216 100644 --- a/src/linux.rs +++ b/src/linux.rs @@ -6,7 +6,7 @@ use std; use std::cmp::min; use std::error; use std::ffi::CStr; -use std::fmt; +use std::fmt::{self, Display}; use std::fs::{File, OpenOptions}; use std::io::{self, stdin, Read}; use std::mem; @@ -106,91 +106,81 @@ pub enum Error { LoadKernel(Box<error::Error>), } -impl fmt::Display for Error { +impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Error::*; + match self { - Error::BalloonDeviceNew(e) => write!(f, "failed to create balloon: {}", e), - Error::BlockDeviceNew(e) => write!(f, "failed to create block device: {}", e), - Error::BlockSignal(e) => write!(f, "failed to block signal: {}", e), - Error::BuildingVm(e) => write!(f, "The architecture failed to build the vm: {}", e), - Error::CloneEventFd(e) => write!(f, "failed to clone eventfd: {}", e), - Error::CreateEventFd(e) => write!(f, "failed to create eventfd: {}", e), - Error::CreatePollContext(e) => write!(f, "failed to create poll context: {}", e), - Error::CreateSignalFd(e) => write!(f, "failed to create signalfd: {}", e), - Error::CreateSocket(e) => write!(f, "failed to create socket: {}", e), - Error::CreateTapDevice(e) => write!(f, "failed to create tap device: {}", e), - Error::CreateTimerFd(e) => write!(f, "failed to create timerfd: {}", e), - Error::DetectImageType(e) => write!(f, "failed to detect disk image type: {}", e), - Error::DeviceJail(e) => write!(f, "failed to jail device: {}", e), - Error::DevicePivotRoot(e) => write!(f, "failed to pivot root device: {}", e), - Error::Disk(e) => write!(f, "failed to load disk image: {}", e), - Error::DiskImageLock(e) => write!(f, "failed to lock disk image: {}", e), - Error::InvalidFdPath => write!(f, "failed parsing a /proc/self/fd/*"), - Error::InvalidWaylandPath => { - write!(f, "wayland socket path has no parent or file name") - } - Error::NetDeviceNew(e) => write!(f, "failed to set up virtio networking: {}", e), - Error::PivotRootDoesntExist(p) => write!(f, "{} doesn't exist, can't jail devices.", p), - Error::OpenInitrd(p, e) => write!(f, "failed to open initrd {}: {}", p.display(), e), - Error::OpenKernel(p, e) => { - write!(f, "failed to open kernel image {}: {}", p.display(), e) - } - Error::OpenAndroidFstab(ref p, ref e) => write!( + BalloonDeviceNew(e) => write!(f, "failed to create balloon: {}", e), + BlockDeviceNew(e) => write!(f, "failed to create block device: {}", e), + BlockSignal(e) => write!(f, "failed to block signal: {}", e), + BuildingVm(e) => write!(f, "The architecture failed to build the vm: {}", e), + CloneEventFd(e) => write!(f, "failed to clone eventfd: {}", e), + CreateEventFd(e) => write!(f, "failed to create eventfd: {}", e), + CreatePollContext(e) => write!(f, "failed to create poll context: {}", e), + CreateSignalFd(e) => write!(f, "failed to create signalfd: {}", e), + CreateSocket(e) => write!(f, "failed to create socket: {}", e), + CreateTapDevice(e) => write!(f, "failed to create tap device: {}", e), + CreateTimerFd(e) => write!(f, "failed to create timerfd: {}", e), + DetectImageType(e) => write!(f, "failed to detect disk image type: {}", e), + DeviceJail(e) => write!(f, "failed to jail device: {}", e), + DevicePivotRoot(e) => write!(f, "failed to pivot root device: {}", e), + Disk(e) => write!(f, "failed to load disk image: {}", e), + DiskImageLock(e) => write!(f, "failed to lock disk image: {}", e), + InvalidFdPath => write!(f, "failed parsing a /proc/self/fd/*"), + InvalidWaylandPath => write!(f, "wayland socket path has no parent or file name"), + NetDeviceNew(e) => write!(f, "failed to set up virtio networking: {}", e), + PivotRootDoesntExist(p) => write!(f, "{} doesn't exist, can't jail devices.", p), + OpenInitrd(p, e) => write!(f, "failed to open initrd {}: {}", p.display(), e), + OpenKernel(p, e) => write!(f, "failed to open kernel image {}: {}", p.display(), e), + OpenAndroidFstab(ref p, ref e) => write!( f, "failed to open android fstab file {}: {}", p.display(), e ), - Error::P9DeviceNew(e) => write!(f, "failed to create 9p device: {}", e), - Error::PollContextAdd(e) => write!(f, "failed to add fd to poll context: {}", e), - Error::PollContextDelete(e) => { - write!(f, "failed to remove fd from poll context: {}", e) - } - Error::QcowDeviceCreate(e) => write!(f, "failed to read qcow formatted file {}", e), - Error::ReadLowmemAvailable(e) => write!( + P9DeviceNew(e) => write!(f, "failed to create 9p device: {}", e), + PollContextAdd(e) => write!(f, "failed to add fd to poll context: {}", e), + PollContextDelete(e) => write!(f, "failed to remove fd from poll context: {}", e), + QcowDeviceCreate(e) => write!(f, "failed to read qcow formatted file {}", e), + ReadLowmemAvailable(e) => write!( f, "failed to read /sys/kernel/mm/chromeos-low_mem/available: {}", e ), - Error::ReadLowmemMargin(e) => write!( + ReadLowmemMargin(e) => write!( f, "failed to read /sys/kernel/mm/chromeos-low_mem/margin: {}", e ), - Error::RegisterBalloon(e) => write!(f, "error registering balloon device: {}", e), - Error::RegisterBlock(e) => write!(f, "error registering block device: {}", e), - Error::RegisterGpu(e) => write!(f, "error registering gpu device: {}", e), - Error::RegisterNet(e) => write!(f, "error registering net device: {}", e), - Error::RegisterP9(e) => write!(f, "error registering 9p device: {}", e), - Error::RegisterRng(e) => write!(f, "error registering rng device: {}", e), - Error::RegisterSignalHandler(e) => write!(f, "error registering signal handler: {}", e), - Error::RegisterWayland(e) => write!(f, "error registering wayland device: {}", e), - Error::ResetTimerFd(e) => write!(f, "failed to reset timerfd: {}", e), - Error::RngDeviceNew(e) => write!(f, "failed to set up rng: {}", e), - Error::InputDeviceNew(ref e) => write!(f, "failed to set up input device: {}", e), - Error::InputEventsOpen(ref e) => write!(f, "failed to open event device: {}", e), - Error::SettingGidMap(e) => write!(f, "error setting GID map: {}", e), - Error::SettingUidMap(e) => write!(f, "error setting UID map: {}", e), - Error::SignalFd(e) => write!(f, "failed to read signal fd: {}", e), - Error::SpawnVcpu(e) => write!(f, "failed to spawn VCPU thread: {}", e), - Error::TimerFd(e) => write!(f, "failed to read timer fd: {}", e), - Error::ValidateRawFd(e) => write!(f, "failed to validate raw fd: {}", e), - Error::VhostNetDeviceNew(e) => write!(f, "failed to set up vhost networking: {}", e), - Error::VhostVsockDeviceNew(e) => { - write!(f, "failed to set up virtual socket device: {}", e) - } - Error::VirtioPciDev(e) => write!(f, "failed to create virtio pci dev: {}", e), - Error::WaylandDeviceNew(e) => write!(f, "failed to create wayland device: {}", e), - Error::LoadKernel(e) => write!(f, "failed to load kernel: {}", e), + RegisterBalloon(e) => write!(f, "error registering balloon device: {}", e), + RegisterBlock(e) => write!(f, "error registering block device: {}", e), + RegisterGpu(e) => write!(f, "error registering gpu device: {}", e), + RegisterNet(e) => write!(f, "error registering net device: {}", e), + RegisterP9(e) => write!(f, "error registering 9p device: {}", e), + RegisterRng(e) => write!(f, "error registering rng device: {}", e), + RegisterSignalHandler(e) => write!(f, "error registering signal handler: {}", e), + RegisterWayland(e) => write!(f, "error registering wayland device: {}", e), + ResetTimerFd(e) => write!(f, "failed to reset timerfd: {}", e), + RngDeviceNew(e) => write!(f, "failed to set up rng: {}", e), + InputDeviceNew(ref e) => write!(f, "failed to set up input device: {}", e), + InputEventsOpen(ref e) => write!(f, "failed to open event device: {}", e), + SettingGidMap(e) => write!(f, "error setting GID map: {}", e), + SettingUidMap(e) => write!(f, "error setting UID map: {}", e), + SignalFd(e) => write!(f, "failed to read signal fd: {}", e), + SpawnVcpu(e) => write!(f, "failed to spawn VCPU thread: {}", e), + TimerFd(e) => write!(f, "failed to read timer fd: {}", e), + ValidateRawFd(e) => write!(f, "failed to validate raw fd: {}", e), + VhostNetDeviceNew(e) => write!(f, "failed to set up vhost networking: {}", e), + VhostVsockDeviceNew(e) => write!(f, "failed to set up virtual socket device: {}", e), + VirtioPciDev(e) => write!(f, "failed to create virtio pci dev: {}", e), + WaylandDeviceNew(e) => write!(f, "failed to create wayland device: {}", e), + LoadKernel(e) => write!(f, "failed to load kernel: {}", e), } } } -impl std::error::Error for Error { - fn description(&self) -> &str { - "Some device failure" - } -} +impl std::error::Error for Error {} type Result<T> = std::result::Result<T, Error>; diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index 0736b1f..787c73f 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -5,7 +5,7 @@ mod process; mod vcpu; -use std::fmt; +use std::fmt::{self, Display}; use std::fs::File; use std::io; use std::os::unix::io::{FromRawFd, IntoRawFd}; @@ -103,7 +103,7 @@ pub enum Error { ValidateTapFd(SysError), } -impl fmt::Display for Error { +impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use self::Error::*; diff --git a/sync/src/mutex.rs b/sync/src/mutex.rs index 59c9f71..c479a2e 100644 --- a/sync/src/mutex.rs +++ b/sync/src/mutex.rs @@ -24,7 +24,6 @@ //! Developers should feel free to use sync::Mutex anywhere in crosvm that they //! would otherwise be using std::sync::Mutex. -use std::error::Error; use std::fmt::{self, Debug, Display}; use std::sync::{Mutex as StdMutex, MutexGuard, TryLockError}; @@ -119,4 +118,4 @@ impl Display for WouldBlock { } } -impl Error for WouldBlock {} +impl std::error::Error for WouldBlock {} diff --git a/sys_util/src/errno.rs b/sys_util/src/errno.rs index 023b162..4643484 100644 --- a/sys_util/src/errno.rs +++ b/sys_util/src/errno.rs @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -use std::error; use std::fmt::{self, Display}; use std::io; use std::result; @@ -41,11 +40,7 @@ impl From<io::Error> for Error { } } -impl error::Error for Error { - fn description(&self) -> &str { - "System returned an error code" - } -} +impl std::error::Error for Error {} impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/sys_util/src/guest_memory.rs b/sys_util/src/guest_memory.rs index 99902ba..9fa5fff 100644 --- a/sys_util/src/guest_memory.rs +++ b/sys_util/src/guest_memory.rs @@ -29,13 +29,16 @@ impl std::error::Error for Error {} impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Guest memory error: ")?; + use self::Error::*; + match self { - Error::InvalidGuestAddress(_) => write!(f, "Invalid Guest Address"), - Error::MemoryAccess(_, _) => write!(f, "Invalid Guest Memory Access"), - Error::MemoryMappingFailed(_) => write!(f, "Failed to map guest memory"), - Error::MemoryRegionOverlap => write!(f, "Memory regions overlap"), - Error::ShortWrite { + InvalidGuestAddress(addr) => write!(f, "invalid guest address {}", addr), + MemoryAccess(addr, e) => { + write!(f, "invalid guest memory access at addr={}: {}", addr, e) + } + MemoryMappingFailed(e) => write!(f, "failed to map guest memory: {}", e), + MemoryRegionOverlap => write!(f, "memory regions overlap"), + ShortWrite { expected, completed, } => write!( @@ -43,7 +46,7 @@ impl Display for Error { "incomplete write of {} instead of {} bytes", completed, expected, ), - Error::ShortRead { + ShortRead { expected, completed, } => write!( diff --git a/sys_util/src/syslog.rs b/sys_util/src/syslog.rs index f31ff11..2ea732b 100644 --- a/sys_util/src/syslog.rs +++ b/sys_util/src/syslog.rs @@ -64,18 +64,22 @@ pub enum Priority { Debug = 7, } -impl fmt::Display for Priority { +impl Display for Priority { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Priority::Emergency => write!(f, "EMERGENCY"), - Priority::Alert => write!(f, "ALERT"), - Priority::Critical => write!(f, "CRITICAL"), - Priority::Error => write!(f, "ERROR"), - Priority::Warning => write!(f, "WARNING"), - Priority::Notice => write!(f, "NOTICE"), - Priority::Info => write!(f, "INFO"), - Priority::Debug => write!(f, "DEBUG"), - } + use self::Priority::*; + + let string = match self { + Emergency => "EMERGENCY", + Alert => "ALERT", + Critical => "CRITICAL", + Error => "ERROR", + Warning => "WARNING", + Notice => "NOTICE", + Info => "INFO", + Debug => "DEBUG", + }; + + write!(f, "{}", string) } } diff --git a/x86_64/src/cpuid.rs b/x86_64/src/cpuid.rs index 6d875cb..1446d79 100644 --- a/x86_64/src/cpuid.rs +++ b/x86_64/src/cpuid.rs @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -use std::error::{self, Error as CpuidError}; use std::fmt::{self, Display}; use std::result; @@ -16,18 +15,16 @@ pub enum Error { } pub type Result<T> = result::Result<T, Error>; -impl error::Error for Error { - fn description(&self) -> &str { - match self { - Error::GetSupportedCpusFailed(_) => "GetSupportedCpus ioctl failed", - Error::SetSupportedCpusFailed(_) => "SetSupportedCpus ioctl failed", - } - } -} +impl std::error::Error for Error {} impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "CPUID Error: {}", Error::description(self)) + use self::Error::*; + + match self { + GetSupportedCpusFailed(e) => write!(f, "GetSupportedCpus ioctl failed: {}", e), + SetSupportedCpusFailed(e) => write!(f, "SetSupportedCpus ioctl failed: {}", e), + } } } diff --git a/x86_64/src/interrupts.rs b/x86_64/src/interrupts.rs index bcbb6f6..6c2ebfd 100644 --- a/x86_64/src/interrupts.rs +++ b/x86_64/src/interrupts.rs @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -use std::error::{self, Error as InterruptsError}; use std::fmt::{self, Display}; use std::io::Cursor; use std::mem; @@ -21,18 +20,16 @@ pub enum Error { } pub type Result<T> = result::Result<T, Error>; -impl error::Error for Error { - fn description(&self) -> &str { - match self { - Error::GetLapic(_) => "GetLapic ioctl failed", - Error::SetLapic(_) => "SetLapic ioctl failed", - } - } -} +impl std::error::Error for Error {} impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Interrupt Error: {}", Error::description(self)) + use self::Error::*; + + match self { + GetLapic(e) => write!(f, "GetLapic ioctl failed: {}", e), + SetLapic(e) => write!(f, "SetLapic ioctl failed: {}", e), + } } } diff --git a/x86_64/src/lib.rs b/x86_64/src/lib.rs index 01e8f6f..51c670c 100644 --- a/x86_64/src/lib.rs +++ b/x86_64/src/lib.rs @@ -66,7 +66,6 @@ mod interrupts; mod mptable; mod regs; -use std::error::{self, Error as X86Error}; use std::ffi::{CStr, CString}; use std::fmt::{self, Display}; use std::fs::File; @@ -122,34 +121,32 @@ pub enum Error { E820Configuration, } -impl error::Error for Error { - fn description(&self) -> &str { - match self { - Error::ConfigureSystem => "Error configuring the system", - Error::CloneEventFd(_) => "Unable to clone an EventFd", - Error::Cmdline(_) => "the given kernel command line was invalid", - Error::CreateEventFd(_) => "Unable to make an EventFd", - Error::CreatePit(_) => "Unable to make Pit device", - Error::CreateKvm(_) => "failed to open /dev/kvm", - Error::CreatePciRoot(_) => "failed to create a PCI root hub", - Error::CreateSocket(_) => "failed to create socket", - Error::CreateVcpu(_) => "failed to create VCPU", - Error::KernelOffsetPastEnd => "The kernel extends past the end of RAM", - Error::RegisterIrqfd(_) => "Error registering an IrqFd", - Error::RegisterVsock(_) => "error registering virtual socket device", - Error::LoadCmdline(_) => "Error Loading command line", - Error::LoadKernel(_) => "Error Loading Kernel", - Error::LoadInitrd(_) => "Error loading initrd", - Error::ZeroPageSetup => "Error writing the zero page of guest memory", - Error::ZeroPagePastRamEnd => "The zero page extends past the end of guest_mem", - Error::E820Configuration => "Invalid e820 setup params", - } - } -} +impl std::error::Error for Error {} impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "X86 Arch Error: {}", Error::description(self)) + use self::Error::*; + + match self { + ConfigureSystem => write!(f, "error configuring the system"), + CloneEventFd(e) => write!(f, "unable to clone an EventFd: {}", e), + Cmdline(e) => write!(f, "the given kernel command line was invalid: {}", e), + CreateEventFd(e) => write!(f, "unable to make an EventFd: {}", e), + CreatePit(e) => write!(f, "unable to make Pit device: {}", e), + CreateKvm(e) => write!(f, "failed to open /dev/kvm: {}", e), + CreatePciRoot(e) => write!(f, "failed to create a PCI root hub: {}", e), + CreateSocket(e) => write!(f, "failed to create socket: {}", e), + CreateVcpu(e) => write!(f, "failed to create VCPU: {}", e), + KernelOffsetPastEnd => write!(f, "the kernel extends past the end of RAM"), + RegisterIrqfd(e) => write!(f, "error registering an IrqFd: {}", e), + RegisterVsock(e) => write!(f, "error registering virtual socket device: {}", e), + LoadCmdline(e) => write!(f, "error Loading command line: {}", e), + LoadKernel(e) => write!(f, "error Loading Kernel: {}", e), + LoadInitrd(e) => write!(f, "error loading initrd: {}", e), + ZeroPageSetup => write!(f, "error writing the zero page of guest memory"), + ZeroPagePastRamEnd => write!(f, "the zero page extends past the end of guest_mem"), + E820Configuration => write!(f, "invalid e820 setup params"), + } } } diff --git a/x86_64/src/mptable.rs b/x86_64/src/mptable.rs index 4c3bae4..c2444d0 100644 --- a/x86_64/src/mptable.rs +++ b/x86_64/src/mptable.rs @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -use std::error::{self, Error as MptableError}; use std::fmt::{self, Display}; use std::io; use std::mem; @@ -40,26 +39,26 @@ pub enum Error { WriteMpcTable, } -impl error::Error for Error { - fn description(&self) -> &str { - match self { - Error::NotEnoughMemory => "There was too little guest memory to store the MP table", - Error::AddressOverflow => "The MP table has too little address space to be stored", - Error::Clear => "Failure while zeroing out the memory for the MP table", - Error::WriteMpfIntel => "Failure to write the MP floating pointer", - Error::WriteMpcCpu => "Failure to write MP CPU entry", - Error::WriteMpcIoapic => "Failure to write MP ioapic entry", - Error::WriteMpcBus => "Failure to write MP bus entry", - Error::WriteMpcIntsrc => "Failure to write MP interrupt source entry", - Error::WriteMpcLintsrc => "Failure to write MP local interrupt source entry", - Error::WriteMpcTable => "Failure to write MP table header", - } - } -} +impl std::error::Error for Error {} impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "MPTable Error: {}", Error::description(self)) + use self::Error::*; + + let description = match self { + NotEnoughMemory => "There was too little guest memory to store the MP table", + AddressOverflow => "The MP table has too little address space to be stored", + Clear => "Failure while zeroing out the memory for the MP table", + WriteMpfIntel => "Failure to write the MP floating pointer", + WriteMpcCpu => "Failure to write MP CPU entry", + WriteMpcIoapic => "Failure to write MP ioapic entry", + WriteMpcBus => "Failure to write MP bus entry", + WriteMpcIntsrc => "Failure to write MP interrupt source entry", + WriteMpcLintsrc => "Failure to write MP local interrupt source entry", + WriteMpcTable => "Failure to write MP table header", + }; + + write!(f, "MPTable error: {}", description) } } diff --git a/x86_64/src/regs.rs b/x86_64/src/regs.rs index 3dd4754..289d403 100644 --- a/x86_64/src/regs.rs +++ b/x86_64/src/regs.rs @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -use std::error::{self, Error as RegsError}; use std::fmt::{self, Display}; use std::{mem, result}; @@ -41,26 +40,26 @@ pub enum Error { } pub type Result<T> = result::Result<T, Error>; -impl error::Error for Error { - fn description(&self) -> &str { - match self { - Error::MsrIoctlFailed(_) => "Setting up msrs failed", - Error::FpuIoctlFailed(_) => "Failed to configure the FPU", - Error::GetSRegsIoctlFailed(_) => "Failed to get sregs for this cpu", - Error::SettingRegistersIoctl(_) => "Failed to set base registers for this cpu", - Error::SetSRegsIoctlFailed(_) => "Failed to set sregs for this cpu", - Error::WriteGDTFailure => "Writing the GDT to RAM failed", - Error::WriteIDTFailure => "Writing the IDT to RAM failed", - Error::WritePML4Address => "Writing PML4 to RAM failed", - Error::WritePDPTEAddress => "Writing PDPTE to RAM failed", - Error::WritePDEAddress => "Writing PDE to RAM failed", - } - } -} +impl std::error::Error for Error {} impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Interrupt Error: {}", Error::description(self)) + use self::Error::*; + + match self { + MsrIoctlFailed(e) => write!(f, "setting up msrs failed: {}", e), + FpuIoctlFailed(e) => write!(f, "failed to configure the FPU: {}", e), + GetSRegsIoctlFailed(e) => write!(f, "failed to get sregs for this cpu: {}", e), + SettingRegistersIoctl(e) => { + write!(f, "failed to set base registers for this cpu: {}", e) + } + SetSRegsIoctlFailed(e) => write!(f, "failed to set sregs for this cpu: {}", e), + WriteGDTFailure => write!(f, "writing the GDT to RAM failed"), + WriteIDTFailure => write!(f, "writing the IDT to RAM failed"), + WritePML4Address => write!(f, "writing PML4 to RAM failed"), + WritePDPTEAddress => write!(f, "writing PDPTE to RAM failed"), + WritePDEAddress => write!(f, "writing PDE to RAM failed"), + } } } |