Skip to content

Commit 9e056b1

Browse files
authored
chore: add in-house detours ffi bindings (#207)
This PR replaces the external `ms-detours` crate with our own FFI bindings to Microsoft's Detours library. The implementation: - Adds Microsoft Detours as a git submodule - Creates a new `fspy_detours_sys` crate with raw FFI bindings - Compiles Detours from source using the `cc` crate - Updates all imports to use the new bindings - Includes a bindgen test to verify/regenerate bindings This change improves built times and allows cross-compiling.
1 parent 4d4d4d9 commit 9e056b1

23 files changed

Lines changed: 637 additions & 49 deletions

File tree

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
4343
with:
4444
persist-credentials: false
45+
submodules: true
4546

4647
- uses: oxc-project/setup-rust@d286d43bc1f606abbd98096666ff8be68c8d5f57 # v1.0.0
4748
with:
@@ -61,6 +62,7 @@ jobs:
6162
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
6263
with:
6364
persist-credentials: false
65+
submodules: true
6466

6567
- uses: oxc-project/setup-rust@d286d43bc1f606abbd98096666ff8be68c8d5f57 # v1.0.0
6668
with:
@@ -89,6 +91,7 @@ jobs:
8991
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
9092
with:
9193
persist-credentials: false
94+
submodules: true
9295

9396
- uses: oxc-project/setup-rust@d286d43bc1f606abbd98096666ff8be68c8d5f57 # v1.0.0
9497
with:
@@ -126,6 +129,7 @@ jobs:
126129
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
127130
with:
128131
persist-credentials: false
132+
submodules: true
129133

130134
- run: |
131135
brew install rustup
@@ -161,6 +165,7 @@ jobs:
161165
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
162166
with:
163167
persist-credentials: false
168+
submodules: true
164169

165170
- uses: oxc-project/setup-rust@d286d43bc1f606abbd98096666ff8be68c8d5f57 # v1.0.0
166171
with:

.github/workflows/deny.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ on:
77
pull_request:
88
types: [opened, synchronize]
99
paths:
10-
- "Cargo.lock"
11-
- "deny.toml"
12-
- ".github/workflows/deny.yml"
10+
- 'Cargo.lock'
11+
- 'deny.toml'
12+
- '.github/workflows/deny.yml'
1313
push:
1414
branches:
1515
- main
1616
paths:
17-
- "Cargo.lock"
18-
- "deny.toml"
19-
- ".github/workflows/deny.yml"
17+
- 'Cargo.lock'
18+
- 'deny.toml'
19+
- '.github/workflows/deny.yml'
2020

2121
concurrency:
2222
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
@@ -30,6 +30,7 @@ jobs:
3030
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
3131
with:
3232
persist-credentials: false
33+
submodules: true
3334

3435
- uses: oxc-project/setup-rust@d286d43bc1f606abbd98096666ff8be68c8d5f57 # v1.0.0
3536
with:

.github/workflows/release.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2424
with:
2525
persist-credentials: false
26+
submodules: true
2627

2728
- uses: oxc-project/setup-rust@d286d43bc1f606abbd98096666ff8be68c8d5f57 # v1.0.2
2829
with:
@@ -93,6 +94,7 @@ jobs:
9394
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
9495
with:
9596
persist-credentials: false
97+
submodules: true
9698

9799
- uses: oxc-project/setup-node@fdbf0dfd334c4e6d56ceeb77d91c76339c2a0885 # v1.0.4
98100

@@ -120,8 +122,8 @@ jobs:
120122
- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
121123
with:
122124
node-version-file: .node-version
123-
registry-url: "https://npm.pkg.github.com"
124-
scope: "@voidzero-dev"
125+
registry-url: 'https://npm.pkg.github.com'
126+
scope: '@voidzero-dev'
125127
package-manager-cache: false
126128

127129
- run: npm install -g npm@latest # For trusted publishing support

.github/workflows/zizmor.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ on:
77
pull_request:
88
types: [opened, synchronize]
99
paths:
10-
- ".github/workflows/**"
10+
- '.github/workflows/**'
1111
push:
1212
branches:
1313
- main
14-
- "renovate/**"
14+
- 'renovate/**'
1515
paths:
16-
- ".github/workflows/**"
16+
- '.github/workflows/**'
1717

1818
jobs:
1919
zizmor:
@@ -26,6 +26,7 @@ jobs:
2626
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
2727
with:
2828
persist-credentials: false
29+
submodules: true
2930

3031
- uses: taiki-e/install-action@ae97ff9daf1cd2e216671a047d80ff48461e30bb # v2.49.1
3132
with:

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "crates/fspy_detours_sys/detours"]
2+
path = crates/fspy_detours_sys/detours
3+
url = https://github.com/microsoft/Detours

.typos.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ PUNICODE = "PUNICODE"
55
[files]
66
extend-exclude = [
77
"**/snap-tests/**/snap.txt",
8+
"crates/fspy_detours_sys/detours",
9+
"crates/fspy_detours_sys/src/generated_bindings.rs",
810
]

Cargo.lock

Lines changed: 17 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ backon = "1.3.0"
3737
bincode = "2.0.1"
3838
brush-parser = "0.2.18"
3939
bstr = "1.12.0"
40+
cc = "1.2.39"
4041
clap = "4.5.40"
4142
color-eyre = "0.6.5"
4243
compact_str = "0.9.0"
@@ -48,6 +49,7 @@ directories = "6.0.0"
4849
edit = "0.1.5"
4950
flate2 = "1.0.35"
5051
fspy = { path = "crates/fspy" }
52+
fspy_detours_sys = { path = "crates/fspy_detours_sys" }
5153
fspy_preload_unix = { path = "crates/fspy_preload_unix", artifact = "cdylib" }
5254
fspy_preload_windows = { path = "crates/fspy_preload_windows", artifact = "cdylib" }
5355
fspy_seccomp_unotify = { path = "crates/fspy_seccomp_unotify" }
@@ -102,6 +104,7 @@ vite_path = { path = "crates/vite_path" }
102104
vite_str = { path = "crates/vite_str" }
103105
vite_task = { path = "crates/vite_task" }
104106
wax = "0.6.0"
107+
winapi = "0.3.9"
105108

106109
napi = { version = "3.0.0", default-features = false, features = ["async", "error_anyhow"] }
107110
napi-build = "2"

crates/fspy/Cargo.toml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,17 @@ allocator-api2 = { version = "0.2.21", default-features = false, features = [
2727
tokio-seqpacket = "0.8.0"
2828
arrayvec = "0.7.6"
2929
nix = { version = "0.30.1", features = ["uio"] }
30-
fspy_seccomp_unotify = { workspace = true, features = ["supervisor"]}
31-
blink-alloc = { version = "0.3.1", features = ["sync"]}
30+
fspy_seccomp_unotify = { workspace = true, features = ["supervisor"] }
31+
blink-alloc = { version = "0.3.1", features = ["sync"] }
3232
thread_local = "1.1.9"
3333
tokio = { version = "1.44.2", features = ["bytes"] }
34-
syscalls = { version = "0.6.18", default-features = false, features = ["std"]}
34+
syscalls = { version = "0.6.18", default-features = false, features = ["std"] }
3535

3636
[target.'cfg(unix)'.dependencies]
3737
fspy_shared_unix = { workspace = true }
3838
fspy_preload_unix = { workspace = true }
3939
nix = { version = "0.30.1", features = ["fs", "process", "socket", "feature"] }
40-
passfd = { git = "https://github.com/polachok/passfd", features = [
41-
"async",
42-
] }
40+
passfd = { git = "https://github.com/polachok/passfd", features = ["async"] }
4341
memmap2 = "0.9.7"
4442
# asyncfd = "0.1.2"
4543

@@ -53,14 +51,14 @@ const_format = { version = "0.2.34", features = ["fmt"] }
5351

5452

5553
[target.'cfg(target_os = "windows")'.dependencies]
56-
ms-detours = "4.0.5"
5754
winsafe = { version = "0.0.24", features = ["kernel"] }
58-
winapi = { version = "0.3.9", features = [
55+
winapi = { workspace = true, features = [
5956
"winbase",
6057
"securitybaseapi",
6158
"handleapi",
6259
] }
6360
fspy_preload_windows = { workspace = true }
61+
fspy_detours_sys = { workspace = true }
6462

6563
[target.'cfg(target_os = "macos")'.dev-dependencies]
6664
tempfile = "3.19.1"

crates/fspy/src/windows/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use std::{
1010

1111
use bincode::borrow_decode_from_slice;
1212
use const_format::formatcp;
13+
use fspy_detours_sys::{DetourCopyPayloadToProcess, DetourUpdateProcessWithDll};
1314
use fspy_shared::{
1415
ipc::{BINCODE_CONFIG, PathAccess},
1516
windows::{PAYLOAD_ID, Payload},
1617
};
1718
use futures_util::FutureExt;
18-
use ms_detours::{DetourCopyPayloadToProcess, DetourUpdateProcessWithDll};
1919
use tokio::{
2020
io::AsyncReadExt,
2121
net::windows::named_pipe::{PipeMode, ServerOptions},

0 commit comments

Comments
 (0)