Skip to content

Commit e5fc8ea

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 15c3910 + 3b6f17a commit e5fc8ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1744
-1470
lines changed

.travis.yml

Lines changed: 0 additions & 59 deletions
This file was deleted.

Cargo.toml

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,34 @@ license = "BSD-2-Clause"
88
name = "detour"
99
readme = "README.md"
1010
repository = "https://github.com/darfink/detour-rs"
11-
version = "0.5.0"
11+
version = "0.8.0"
12+
edition = "2018"
1213

1314
[badges]
14-
15-
[badges.appveyor]
16-
repository = "darfink/detour-rs"
17-
18-
[badges.travis-ci]
19-
repository = "darfink/detour-rs"
15+
azure-devops = { project = "darfink/detour-rs", pipeline = "darfink.detour-rs" }
2016

2117
[dependencies]
22-
boolinator = "2.4.0"
23-
cfg-if = "0.1.1"
24-
failure = "0.1.1"
25-
generic-array = "0.10"
26-
lazy_static = "1.0"
27-
libc = "0.2"
28-
matches = "0.1.6"
29-
mmap-fixed = "0.1"
30-
region = "0.3"
31-
slice-pool = "0.3.4"
32-
tap = "0.2.1"
18+
cfg-if = "1.0.0"
19+
generic-array = "0.14.1"
20+
lazy_static = "1.2"
21+
libc = "0.2.45"
22+
mmap = { package = "mmap-fixed", version = "0.1.0" }
23+
region = "2.0.0"
24+
slice-pool = "0.4.1"
25+
26+
[dev-dependencies]
27+
matches = "0.1.8"
3328

3429
[features]
3530
default = ["nightly"]
3631
nightly = []
3732

38-
[target]
39-
40-
[target."cfg(any(target_arch = \"x86\", target_arch = \"x86_64\"))"]
33+
[[example]]
34+
name = "messageboxw_detour"
35+
crate-type = ["cdylib"]
4136

4237
[target."cfg(any(target_arch = \"x86\", target_arch = \"x86_64\"))".dependencies]
43-
libudis86-sys = "0.2.0"
38+
udis = { package = "libudis86-sys", version = "0.2.1" }
39+
40+
[target."cfg(windows)".dev-dependencies]
41+
winapi = { version = "0.3.7", features = ["minwindef", "windef", "winnt", "libloaderapi"] }

README.md

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
detour-rs
2-
=========
3-
[![Travis build status][travis-shield]][travis]
4-
[![Appveyor build status][appveyor-shield]][appveyor]
1+
<div align="center">
2+
3+
# `detour-rs`
4+
5+
[![Azure build Status][azure-shield]][azure]
56
[![crates.io version][crate-shield]][crate]
67
[![Documentation][docs-shield]][docs]
78
[![Language (Rust)][rust-shield]][rust]
89

10+
</div>
11+
912
This is a cross-platform detour library developed in Rust. Beyond the basic
1013
functionality, this library handles branch redirects, RIP-relative
1114
instructions, hot-patching, NOP-padded functions, and allows the original
@@ -16,68 +19,87 @@ maintain this feature, not all desired functionality can be supported due to
1619
lack of cross-platform APIs. Therefore [EIP relocation](#appendix) is not
1720
supported.
1821

19-
**NOTE**: Nightly is currently required for `static_detours!`.
22+
**NOTE**: Nightly is currently required for `static_detour!` and is enabled by
23+
default.
2024

2125
## Platforms
2226

23-
- `x86/x64`: Windows, Linux & macOS.
24-
- `ARM`: Not implemented, but foundation exists.
27+
This library provides CI for these targets:
28+
29+
- Linux
30+
* `i686-unknown-linux-gnu`
31+
* `x86_64-unknown-linux-gnu`
32+
* `x86_64-unknown-linux-musl`
33+
- Windows
34+
* `i686-pc-windows-gnu`
35+
* `i686-pc-windows-msvc`
36+
* `x86_64-pc-windows-gnu`
37+
* `x86_64-pc-windows-msvc`
38+
- macOS
39+
* ~~`i686-apple-darwin`~~
40+
* `x86_64-apple-darwin`
2541

2642
## Installation
2743

2844
Add this to your `Cargo.toml`:
2945

3046
```toml
3147
[dependencies]
32-
detour = "0.5.0"
33-
```
34-
35-
... and this to your crate root:
36-
37-
```rust
38-
#[macro_use]
39-
extern crate detour;
48+
detour = "0.8.0"
4049
```
4150

4251
## Example
4352

4453
- A static detour (one of *three* different detours):
4554

4655
```rust
47-
#[macro_use] extern crate detour;
56+
use std::error::Error;
57+
use detour::static_detour;
4858

49-
extern "C" fn add(x: i32, y: i32) -> i32 {
50-
x + y
59+
static_detour! {
60+
static Test: /* extern "X" */ fn(i32) -> i32;
5161
}
5262

53-
static_detours! {
54-
struct DetourAdd: extern "C" fn(i32, i32) -> i32;
63+
fn add5(val: i32) -> i32 {
64+
val + 5
5565
}
5666

57-
fn main() {
58-
// Replaces the 'add' function with a closure that subtracts
59-
let mut hook = unsafe { DetourAdd.initialize(add, |x, y| x - y).unwrap() };
67+
fn add10(val: i32) -> i32 {
68+
val + 10
69+
}
70+
71+
fn main() -> Result<(), Box<dyn Error>> {
72+
// Reroute the 'add5' function to 'add10' (can also be a closure)
73+
unsafe { Test.initialize(add5, add10)? };
74+
75+
assert_eq!(add5(1), 6);
76+
assert_eq!(Test.call(1), 6);
6077

61-
assert_eq!(add(1, 5), 6);
62-
assert_eq!(hook.is_enabled(), false);
78+
// Hooks must be enabled to take effect
79+
unsafe { Test.enable()? };
6380

64-
unsafe { hook.enable().unwrap(); }
81+
// The original function is detoured to 'add10'
82+
assert_eq!(add5(1), 11);
6583

66-
assert_eq!(add(1, 5), -4);
67-
assert_eq!(hook.call(1, 5), 6);
84+
// The original function can still be invoked using 'call'
85+
assert_eq!(Test.call(1), 6);
6886

69-
// Change the detour whilst hooked
70-
hook.set_detour(|x, y| x * y);
71-
assert_eq!(add(5, 5), 25);
87+
// It is also possible to change the detour whilst hooked
88+
Test.set_detour(|val| val - 5);
89+
assert_eq!(add5(5), 0);
7290

73-
unsafe { hook.disable().unwrap(); }
91+
unsafe { Test.disable()? };
7492

75-
assert_eq!(hook.is_enabled(), false);
76-
assert_eq!(hook.call(1, 5), 6);
77-
assert_eq!(add(1, 5), 6);
93+
assert_eq!(add5(1), 6);
94+
Ok(())
7895
}
7996
```
8097

98+
- A Windows API hooking example is available [here](./examples/messageboxw_detour.rs); build it by running:
99+
```
100+
$ cargo build --example messageboxw_detour
101+
```
102+
81103
## Mentions
82104

83105
Part of the library's external user interface was inspired by
@@ -90,7 +112,7 @@ derivative code of his work.
90112

91113
*Should be performed whenever a function's prolog instructions
92114
are being executed, simultaneously as the function itself is being
93-
detoured. This is done by halting all affected threads, copying the related
115+
detoured. This is done by halting all affected threads, copying the affected
94116
instructions and appending a `JMP` to return to the function. This is
95117
barely ever an issue, and never in single-threaded environments, but YMMV.*
96118

@@ -109,10 +131,8 @@ derivative code of his work.
109131
trailing `NOP` instructions will be replaced, to make room for the detour.*
110132
111133
<!-- Links -->
112-
[travis-shield]: https://img.shields.io/travis/darfink/detour-rs.svg?style=flat-square&label=travis
113-
[travis]: https://travis-ci.org/darfink/detour-rs
114-
[appveyor-shield]: https://img.shields.io/appveyor/ci/darfink/detour-rs/master.svg?style=flat-square&label=appveyor
115-
[appveyor]: https://ci.appveyor.com/project/darfink/detour-rs
134+
[azure-shield]: https://img.shields.io/azure-devops/build/darfink/detour-rs/2/master?label=Azure%20Pipelines&logo=azure-pipelines&style=flat-square
135+
[azure]: https://dev.azure.com/darfink/detour-rs/_build/latest?definitionId=1&branchName=master
116136
[crate-shield]: https://img.shields.io/crates/v/detour.svg?style=flat-square
117137
[crate]: https://crates.io/crates/detour
118138
[rust-shield]: https://img.shields.io/badge/powered%20by-rust-blue.svg?style=flat-square

appveyor.yml

Lines changed: 0 additions & 57 deletions
This file was deleted.

azure-pipelines.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
stages:
2+
- stage: test
3+
displayName: Test
4+
jobs:
5+
- template: ci/cargo-job-per-channel-target.yml
6+
parameters:
7+
channels: [nightly]
8+
targets:
9+
- target: 'i686-pc-windows-msvc'
10+
- target: 'i686-pc-windows-msvc'
11+
channels: [stable]
12+
cargoSteps:
13+
- bash: $CARGO test --target $TARGET --no-default-features
14+
displayName: Cargo test
15+
- target: 'x86_64-pc-windows-msvc'
16+
# - target: 'i686-pc-windows-gnu'
17+
- target: 'x86_64-pc-windows-gnu'
18+
# - target: 'i686-apple-darwin'
19+
- target: 'x86_64-apple-darwin'
20+
preSteps:
21+
- script: cp ./ci/macos/* ~/.cargo
22+
displayName: Configure custom linker
23+
- target: 'i686-unknown-linux-gnu'
24+
preSteps:
25+
- script: sudo apt-get update && sudo apt-get install gcc-multilib
26+
displayName: Install GCC 32-bit libs
27+
- target: 'x86_64-unknown-linux-gnu'
28+
- target: 'x86_64-unknown-linux-gnu'
29+
channels: [stable]
30+
cargoSteps:
31+
- bash: $CARGO test --target $TARGET --no-default-features
32+
displayName: Cargo test
33+
# - target: 'x86_64-unknown-linux-musl'
34+
# cross: true

ci/cargo-job-per-channel-target.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This template creates one job per channel-target.
2+
parameters:
3+
# The default rust channels to use ('stable', 'beta' and/or 'nightly').
4+
# This can also be overriden for each target.
5+
channels: [stable]
6+
# The target triples to use (e.g 'i686-apple-darwin')
7+
targets: []
8+
9+
jobs:
10+
- ${{ each target in parameters.targets }}:
11+
- ${{ if target.channels }}:
12+
- ${{ each channel in target.channels }}:
13+
- template: cargo-job.yml
14+
parameters:
15+
identifier: ${{ format('rust_{0}_{1}', channel, replace(target.target, '-', '_')) }}
16+
channel: ${{ channel }}
17+
${{ insert }}: ${{ target }}
18+
19+
- ${{ if not(target.channels) }}:
20+
- ${{ each channel in parameters.channels }}:
21+
- template: cargo-job.yml
22+
parameters:
23+
identifier: ${{ format('rust_{0}_{1}', channel, replace(target.target, '-', '_')) }}
24+
channel: ${{ channel }}
25+
${{ insert }}: ${{ target }}

0 commit comments

Comments
 (0)