From ecb9d87d20ed7c69fa22972e96aef4a51aa21cb5 Mon Sep 17 00:00:00 2001 From: Elliott Linder Date: Fri, 4 May 2018 20:58:18 +0200 Subject: [PATCH] fix: use local Error type instead of failure --- Cargo.toml | 2 +- src/alloc/proximity.rs | 4 ++-- src/alloc/search.rs | 6 +++--- src/arch/x86/mod.rs | 2 +- src/arch/x86/patcher.rs | 2 +- src/arch/x86/trampoline/mod.rs | 2 +- src/error.rs | 10 ++++++++-- src/lib.rs | 2 +- 8 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8493fc7c..c086e1e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ lazy_static = "1.0" libc = "0.2" matches = "0.1.6" mmap-fixed = "0.1" -region = "0.1" +region = "0.3" slice-pool = "0.3.4" tap = "0.2.1" diff --git a/src/alloc/proximity.rs b/src/alloc/proximity.rs index d1f51b8c..f10dbec8 100644 --- a/src/alloc/proximity.rs +++ b/src/alloc/proximity.rs @@ -72,7 +72,7 @@ impl ProximityAllocator { .iter_mut() .filter_map(|pool| is_pool_in_range(pool).and_option_from(|| pool.allocate(size))) .next() - .ok_or(Error::OutOfMemory.into()) + .ok_or(Error::OutOfMemory) } /// Allocates a new pool close to `origin`. @@ -95,7 +95,7 @@ impl ProximityAllocator { Err(error) => Some(Err(error)), }) .next() - .unwrap_or(Err(Error::OutOfMemory.into())) + .unwrap_or(Err(Error::OutOfMemory)) } /// Tries to allocate fixed memory at the specified address. diff --git a/src/alloc/search.rs b/src/alloc/search.rs index 0af4d1d1..2cf32618 100644 --- a/src/alloc/search.rs +++ b/src/alloc/search.rs @@ -42,7 +42,7 @@ impl Iterator for RegionIter { /// Returns the closest free region for the current address. fn next(&mut self) -> Option { - let page_size = region::page::page_size(); + let page_size = region::page::size(); while self.current > 0 && self.range.contains_(self.current) { match region::query(self.current as *const _) { @@ -54,9 +54,9 @@ impl Iterator for RegionIter { }, Err(error) => { // Check whether the region is free, otherwise return the error - let result = Some(match error.downcast().expect("downcasting region error") { + let result = Some(match error { region::Error::Free => Ok(self.current as *const _), - inner @ _ => Err(Error::RegionFailure(inner).into()), + inner => Err(Error::RegionFailure(inner)), }); // Adjust the offset for repeated calls. diff --git a/src/arch/x86/mod.rs b/src/arch/x86/mod.rs index 98fd691b..70c29ce2 100644 --- a/src/arch/x86/mod.rs +++ b/src/arch/x86/mod.rs @@ -54,7 +54,7 @@ mod tests { funcs::external_loop as *const (), funcs::ret10 as *const ()) }.unwrap_err(); - assert_matches!(error.downcast(), Ok(Error::UnsupportedInstruction)); + assert_matches!(error, Error::UnsupportedInstruction); } #[test] diff --git a/src/arch/x86/patcher.rs b/src/arch/x86/patcher.rs index fb4f1748..d07ca3ab 100644 --- a/src/arch/x86/patcher.rs +++ b/src/arch/x86/patcher.rs @@ -71,7 +71,7 @@ impl Patcher { let patch_size = jump_rel32_size + jump_rel08_size; Ok(slice::from_raw_parts_mut(hot_patch as *mut u8, patch_size)) } else { - Err(Error::NoPatchArea.into()) + Err(Error::NoPatchArea) } } else { // The range is from the start of the function to the end of the jump diff --git a/src/arch/x86/trampoline/mod.rs b/src/arch/x86/trampoline/mod.rs index 729b0fdb..b1e81d2d 100644 --- a/src/arch/x86/trampoline/mod.rs +++ b/src/arch/x86/trampoline/mod.rs @@ -195,7 +195,7 @@ impl Builder { Ok(Box::new(instruction.as_slice().to_vec())) } else if instruction.is_loop() { // Loops (e.g 'loopnz', 'jecxz') to the outside are not supported - Err(Error::UnsupportedInstruction.into()) + Err(Error::UnsupportedInstruction) } else if instruction.is_unconditional_jump() { // If the function is not in a branch, and it unconditionally jumps // a distance larger than the prolog, it's the same as if it terminates. diff --git a/src/error.rs b/src/error.rs index 3adc377b..48c02c8e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,9 +1,9 @@ //! Error types and utilities. -use {failure, region}; +use region; /// The result of a detour operation. -pub type Result = ::std::result::Result; +pub type Result = ::std::result::Result; #[derive(Fail, Debug)] pub enum Error { @@ -32,3 +32,9 @@ pub enum Error { #[fail(display = "{}", _0)] RegionFailure(#[cause] region::Error), } + +impl From for Error { + fn from(error: region::Error) -> Self { + Error::RegionFailure(error) + } +} diff --git a/src/lib.rs b/src/lib.rs index 7542a3a1..b878f77e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -167,6 +167,6 @@ mod tests { } let err = unsafe { RawDetour::new(add as *const (), add as *const ()).unwrap_err() }; - assert_matches!(err.downcast(), Ok(Error::SameAddress)); + assert_matches!(err, Error::SameAddress); } }