Skip to content

Commit

Permalink
style: apply rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
darfink committed Jun 9, 2019
1 parent 6682e77 commit ab9f22a
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cfg-if = "0.1.1"
generic-array = "0.12.0"
lazy_static = "1.2"
libc = "0.2.45"
mmap-fixed = "0.1"
mmap = { package = "mmap-fixed", version = "0.1.0" }
region = "2.0.0"
slice-pool = "0.3.4"

Expand Down
2 changes: 0 additions & 2 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ fn_single_line = false
format_strings = true
match_block_trailing_comma = true
normalize_comments = true
reorder_extern_crates_in_group = true
reorder_imported_names = true
reorder_imports = true
tab_spaces = 2
use_field_init_shorthand = true
Expand Down
2 changes: 1 addition & 1 deletion src/alloc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::Result;
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Mutex};
use crate::error::Result;

mod proximity;
mod search;
Expand Down
14 changes: 8 additions & 6 deletions src/alloc/proximity.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::ops::Range;
use std::slice;

use mmap_fixed as mmap;
use slice_pool::{PoolVal, SlicePool};

use crate::error::{Result, Error};
use crate::util::RangeContains;
use super::search as region_search;
use crate::error::{Error, Result};
use crate::util::RangeContains;

/// Defines the allocation type.
pub type Allocation = PoolVal<u8>;
Expand Down Expand Up @@ -47,7 +46,8 @@ impl ProximityAllocator {

// Determine if this is the associated memory pool
(lower..upper).contains_(value.as_ptr() as usize)
}).expect("retrieving associated memory pool");
})
.expect("retrieving associated memory pool");

// Release the pool if the associated allocation is unique
if self.pools[index].allocations() == 1 {
Expand Down Expand Up @@ -97,7 +97,8 @@ impl ProximityAllocator {
.filter_map(|result| match result {
Ok(address) => Self::allocate_fixed_pool(address, size).map(Ok),
Err(error) => Some(Err(error)),
}).next()
})
.next()
.unwrap_or(Err(Error::OutOfMemory))
}

Expand All @@ -112,7 +113,8 @@ impl ProximityAllocator {
mmap::MapOption::MapExecutable,
mmap::MapOption::MapAddr(address as *const _),
],
).ok()
)
.ok()
.map(SliceableMemoryMap)
.map(SlicePool::new)
}
Expand Down
6 changes: 3 additions & 3 deletions src/alloc/search.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Range;
use crate::error::{Error, Result};
use crate::util::RangeContains;
use std::ops::Range;

/// Returns an iterator for free after the specified address.
pub fn after(origin: *const (), range: Option<Range<usize>>) -> FreeRegionIter {
Expand Down Expand Up @@ -50,7 +50,7 @@ impl Iterator for FreeRegionIter {
SearchDirection::Before => region.lower().saturating_sub(page_size),
SearchDirection::After => region.upper(),
}
}
},
Err(error) => {
// Check whether the region is free, otherwise return the error
let result = Some(match error {
Expand All @@ -65,7 +65,7 @@ impl Iterator for FreeRegionIter {
};

return result;
}
},
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/arch/detour.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{cell::{UnsafeCell, Cell}, fmt};
use super::memory;
use crate::error::{Error, Result};
use crate::{alloc, arch, util};
use super::memory;
use std::cell::{Cell, UnsafeCell};
use std::fmt;

/// An architecture-independent implementation of a base detour.
///
Expand Down Expand Up @@ -46,7 +47,11 @@ impl Detour {
.unwrap_or(detour);

Ok(Detour {
patcher: UnsafeCell::new(arch::Patcher::new(target, detour, trampoline.prolog_size())?),
patcher: UnsafeCell::new(arch::Patcher::new(
target,
detour,
trampoline.prolog_size(),
)?),
trampoline: memory::allocate_pic(&mut pool, trampoline.emitter(), target)?,
enabled: Cell::new(false),
relay,
Expand Down
4 changes: 2 additions & 2 deletions src/arch/memory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Mutex;
use crate::{alloc, arch, error::Result, pic};
use lazy_static::lazy_static;
use crate::{alloc, arch, pic, error::Result};
use std::sync::Mutex;

lazy_static! {
/// Shared allocator for all detours.
Expand Down
2 changes: 1 addition & 1 deletion src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// The current implementation requires a module to expose some functionality:
///
/// - A standalone `relay_builder` function.
/// This function creates a relay for targets with large displacement, that
/// This function creates a relay for targets with large displacement, that
/// requires special attention. An example would be detours further away than
/// 2GB on x64. A relative jump is not enough, so the `relay_builder`
/// generates an absolute jump that the relative jump can reach. If it's
Expand Down
2 changes: 1 addition & 1 deletion src/detours/generic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::marker::PhantomData;
use crate::arch::Detour;
use crate::error::Result;
use crate::{Function, HookableWith};
use std::marker::PhantomData;

/// A type-safe detour.
///
Expand Down
27 changes: 12 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#![recursion_limit = "1024"]
#![cfg_attr(
feature = "nightly",
feature(const_fn, unboxed_closures, abi_thiscall)
)]
#![cfg_attr(feature = "nightly", feature(const_fn, unboxed_closures, abi_thiscall))]
#![cfg_attr(
all(feature = "nightly", test),
feature(naked_functions, core_intrinsics, asm)
Expand Down Expand Up @@ -35,22 +32,22 @@
//! limited to only one active detour at a time.
//!
//! - [Generic](./struct.GenericDetour.html): A type-safe interface — the same
//! prototype is enforced for both the target and the detour.
//! It is also enforced when invoking the original target.
//! prototype is enforced for both the target and the detour. It is also
//! enforced when invoking the original target.
//!
//! - [Raw](./struct.RawDetour.html): The underlying building block that
//! the others types abstract upon. It has no type-safety and interacts with
//! raw pointers.
//! It should be avoided unless the types used aren't known until runtime.
//! - [Raw](./struct.RawDetour.html): The underlying building block that the
//! others types abstract upon. It has no type-safety and interacts with raw
//! pointers. It should be avoided unless the types used aren't known until
//! runtime.
//!
//! All detours dereferences to the [Detour](./struct.Detour.html) interface,
//! which exposes several methods, and enforces `Send + Sync`.
//!
//! ## Features
//!
//! - **nightly**: Enabled by default. Required for the static detours, due to
//! usage of *const_fn* & *unboxed_closures*.
//! The feature also enables a more extensive test suite.
//! usage of *const_fn* & *unboxed_closures*. The feature also enables a more
//! extensive test suite.
//!
//! ## Platforms
//!
Expand Down Expand Up @@ -98,7 +95,7 @@
// Re-exports
pub use detours::*;
pub use error::{Result, Error};
pub use error::{Error, Result};
pub use traits::{Function, HookableWith};

#[macro_use]
Expand All @@ -115,9 +112,9 @@ mod util;

#[cfg(test)]
mod tests {
use matches::assert_matches;
use crate::Result;
use super::*;
use crate::Result;
use matches::assert_matches;

#[test]
fn detours_share_target() -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/pic/thunk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use generic_array::{ArrayLength, GenericArray};
use super::Thunkable;
use generic_array::{ArrayLength, GenericArray};

/// A closure that generates a thunk.
pub struct FixedThunk<N: ArrayLength<u8>>(Box<Fn(usize) -> GenericArray<u8, N>>);
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::ops::Range;
use crate::error::Result;
use std::ops::Range;

/// Returns true if an address is executable.
pub fn is_executable_address(address: *const ()) -> Result<bool> {
Expand Down
2 changes: 1 addition & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg_attr(feature = "nightly", feature(const_fn))]
use std::mem;
use detour::Result;
use std::mem;

type FnAdd = extern "C" fn(i32, i32) -> i32;

Expand Down

0 comments on commit ab9f22a

Please sign in to comment.