Skip to content

Commit ba1c9cd

Browse files
committed
Move Zydis bridge into external package
1 parent b6602aa commit ba1c9cd

11 files changed

Lines changed: 123 additions & 67 deletions

File tree

.github/workflows/ci.yml

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ jobs:
4444
- name: Zig test
4545
run: zig build test
4646

47+
- name: Resolve Zydis bridge path
48+
run: echo "ZYDIS_BRIDGE_C=$(./scripts/zydis-package-path.sh bridge-c)" >> "$GITHUB_ENV"
49+
4750
- name: Cross compile Linux AArch64 core and payload
4851
run: |
4952
set -euo pipefail
@@ -63,13 +66,11 @@ jobs:
6366
set -euo pipefail
6467
zig build-lib -dynamic -target x86_64-macos -OReleaseFast \
6568
-femit-bin=/tmp/libzighook_macos_x86_64.dylib \
66-
src/root.zig c_deps/x86_64/decoder_zydis.c \
67-
-I c_deps/zydis \
69+
src/root.zig "$ZYDIS_BRIDGE_C" \
6870
-lc
6971
zig build-lib -dynamic -target x86_64-macos -OReleaseFast \
7072
-femit-bin=/tmp/inline_hook_signal_macos_x86_64.dylib \
71-
c_deps/x86_64/decoder_zydis.c \
72-
-I c_deps/zydis \
73+
"$ZYDIS_BRIDGE_C" \
7374
--dep zighook \
7475
-Mroot=examples/inline_hook_signal/hook.zig \
7576
-Mzighook=src/root.zig \
@@ -193,6 +194,9 @@ jobs:
193194
- name: Zig test
194195
run: zig build test
195196

197+
- name: Resolve Zydis bridge path
198+
run: echo "ZYDIS_BRIDGE_C=$(./scripts/zydis-package-path.sh bridge-c)" >> "$GITHUB_ENV"
199+
196200
- name: Example inline_hook_signal
197201
working-directory: examples/inline_hook_signal
198202
run: |
@@ -300,8 +304,7 @@ jobs:
300304
set -euo pipefail
301305
cc -O3 -DNDEBUG -rdynamic -o target target.c -ldl
302306
zig build-lib -dynamic -OReleaseFast -femit-bin=hook.so \
303-
../../c_deps/x86_64/decoder_zydis.c \
304-
-I ../../c_deps/zydis \
307+
"$ZYDIS_BRIDGE_C" \
305308
--dep zighook \
306309
-Mroot=hook.zig \
307310
-Mzighook=../../src/root.zig \
@@ -315,8 +318,7 @@ jobs:
315318
set -euo pipefail
316319
cc -O3 -DNDEBUG -rdynamic -o target target.c -ldl
317320
zig build-lib -dynamic -OReleaseFast -femit-bin=hook.so \
318-
../../c_deps/x86_64/decoder_zydis.c \
319-
-I ../../c_deps/zydis \
321+
"$ZYDIS_BRIDGE_C" \
320322
--dep zighook \
321323
-Mroot=hook.zig \
322324
-Mzighook=../../src/root.zig \
@@ -330,8 +332,7 @@ jobs:
330332
set -euo pipefail
331333
cc -O3 -DNDEBUG -rdynamic -o target target.c -ldl
332334
zig build-lib -dynamic -OReleaseFast -femit-bin=hook.so \
333-
../../c_deps/x86_64/decoder_zydis.c \
334-
-I ../../c_deps/zydis \
335+
"$ZYDIS_BRIDGE_C" \
335336
--dep zighook \
336337
-Mroot=hook.zig \
337338
-Mzighook=../../src/root.zig \
@@ -345,8 +346,7 @@ jobs:
345346
set -euo pipefail
346347
cc -O3 -DNDEBUG -rdynamic -o target target.c -ldl
347348
zig build-lib -dynamic -OReleaseFast -femit-bin=hook.so \
348-
../../c_deps/x86_64/decoder_zydis.c \
349-
-I ../../c_deps/zydis \
349+
"$ZYDIS_BRIDGE_C" \
350350
--dep zighook \
351351
-Mroot=hook.zig \
352352
-Mzighook=../../src/root.zig \
@@ -361,8 +361,7 @@ jobs:
361361
set -euo pipefail
362362
cc -O3 -DNDEBUG -rdynamic -o target target.c -ldl
363363
zig build-lib -dynamic -OReleaseFast -femit-bin=hook.so \
364-
../../c_deps/x86_64/decoder_zydis.c \
365-
-I ../../c_deps/zydis \
364+
"$ZYDIS_BRIDGE_C" \
366365
--dep zighook \
367366
-Mroot=hook.zig \
368367
-Mzighook=../../src/root.zig \

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,15 @@ zig build-lib -dynamic -OReleaseFast -femit-bin=hook.dylib \
141141
DYLD_INSERT_LIBRARIES=$PWD/hook.dylib ./target
142142
```
143143

144-
When you build a hook payload for `x86_64` directly with `zig build-lib`,
145-
include the vendored Zydis shim C file as well:
144+
When you build a hook payload for `x86_64` directly with `zig build-lib`, fetch
145+
the pinned `zydis-zig` package once and then compile its bridge C file:
146146

147147
```bash
148+
(cd ../.. && zig build --fetch)
149+
ZYDIS_BRIDGE_C="$(../../scripts/zydis-package-path.sh bridge-c)"
150+
148151
zig build-lib -dynamic -OReleaseFast -femit-bin=hook.so \
149-
../../c_deps/x86_64/decoder_zydis.c \
150-
-I ../../c_deps/zydis \
152+
"$ZYDIS_BRIDGE_C" \
151153
--dep zighook \
152154
-Mroot=hook.zig \
153155
-Mzighook=../../src/root.zig \

build.zig

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
const std = @import("std");
22

3-
fn addX86DecoderIfNeeded(module: *std.Build.Module, b: *std.Build, target: std.Build.ResolvedTarget) void {
3+
fn addX86DecoderIfNeeded(
4+
module: *std.Build.Module,
5+
b: *std.Build,
6+
target: std.Build.ResolvedTarget,
7+
optimize: std.builtin.OptimizeMode,
8+
) void {
49
if (target.result.cpu.arch != .x86_64) return;
510

6-
module.addIncludePath(b.path("c_deps/zydis"));
11+
const zydis_dep = b.dependency("zydis_zig", .{
12+
.target = target,
13+
.optimize = optimize,
14+
});
715
module.addCSourceFile(.{
8-
.file = b.path("c_deps/x86_64/decoder_zydis.c"),
16+
.file = zydis_dep.path("c/x86_64/decoder_zydis.c"),
917
.flags = &.{"-std=c99"},
1018
});
1119
}
@@ -20,7 +28,7 @@ pub fn build(b: *std.Build) void {
2028
.optimize = optimize,
2129
.link_libc = true,
2230
});
23-
addX86DecoderIfNeeded(zighook_mod, b, target);
31+
addX86DecoderIfNeeded(zighook_mod, b, target, optimize);
2432

2533
const lib = b.addLibrary(.{
2634
.name = "zighook",

build.zig.zon

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,10 @@
3232
// Once all dependencies are fetched, `zig build` no longer requires
3333
// internet connectivity.
3434
.dependencies = .{
35-
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
36-
//.example = .{
37-
// // When updating this field to a new URL, be sure to delete the corresponding
38-
// // `hash`, otherwise you are communicating that you expect to find the old hash at
39-
// // the new URL. If the contents of a URL change this will result in a hash mismatch
40-
// // which will prevent zig from using it.
41-
// .url = "https://example.com/foo.tar.gz",
42-
//
43-
// // This is computed from the file contents of the directory of files that is
44-
// // obtained after fetching `url` and applying the inclusion rules given by
45-
// // `paths`.
46-
// //
47-
// // This field is the source of truth; packages do not come from a `url`; they
48-
// // come from a `hash`. `url` is just one of many possible mirrors for how to
49-
// // obtain a package matching this `hash`.
50-
// //
51-
// // Uses the [multihash](https://multiformats.io/multihash/) format.
52-
// .hash = "...",
53-
//
54-
// // When this is provided, the package is found in a directory relative to the
55-
// // build root. In this case the package's hash is irrelevant and therefore not
56-
// // computed. This field and `url` are mutually exclusive.
57-
// .path = "foo",
58-
//
59-
// // When this is set to `true`, a package is declared to be lazily
60-
// // fetched. This makes the dependency only get fetched if it is
61-
// // actually used.
62-
// .lazy = false,
63-
//},
35+
.zydis_zig = .{
36+
.url = "https://github.com/hookforge/zydis-zig/archive/4fdf1bb02f916e86d7af0f3b0db1caf0b62b1220.tar.gz",
37+
.hash = "zydis_zig-0.1.0-QlrbL2VevAAxCMrxvOKC7hfaWBvKJOyxQW8VYA77FbEJ",
38+
},
6439
},
6540
// Specifies the set of files and directories that are included in this package.
6641
// Only files and directories listed here are included in the `hash` that
@@ -76,8 +51,8 @@
7651
"README.md",
7752
"CHANGELOG.md",
7853
"LICENSE",
54+
"scripts",
7955
"src",
80-
"c_deps",
8156
"tests",
8257
"examples",
8358
"docs",

examples/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ DYLD_INSERT_LIBRARIES=$PWD/hook.dylib ./target
3939

4040
That exact command sequence is still the canonical **macOS runtime smoke** for
4141
AArch64. On Linux, CI runs both AArch64 and x86_64 runtime smokes. Direct
42-
`x86_64` CLI builds also add the vendored Zydis shim:
42+
`x86_64` CLI builds first fetch the pinned `zydis-zig` package and then compile
43+
its bridge C source:
4344

4445
```bash
46+
(cd ../.. && zig build --fetch)
47+
ZYDIS_BRIDGE_C="$(../../scripts/zydis-package-path.sh bridge-c)"
48+
4549
zig build-lib -dynamic -OReleaseFast -femit-bin=hook.so \
46-
../../c_deps/x86_64/decoder_zydis.c \
47-
-I ../../c_deps/zydis \
50+
"$ZYDIS_BRIDGE_C" \
4851
--dep zighook \
4952
-Mroot=hook.zig \
5053
-Mzighook=../../src/root.zig \

examples/inline_hook_signal/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ zig build-lib -dynamic -OReleaseFast -femit-bin=hook.dylib \
3434
-lc
3535
```
3636

37-
Linux x86_64 uses the Zydis shim C source as well:
37+
Linux x86_64 fetches the pinned `zydis-zig` package and compiles its bridge C
38+
source as well:
3839

3940
```bash
41+
(cd ../.. && zig build --fetch)
42+
ZYDIS_BRIDGE_C="$(../../scripts/zydis-package-path.sh bridge-c)"
43+
4044
zig build-lib -dynamic -OReleaseFast -femit-bin=hook.so \
41-
../../c_deps/x86_64/decoder_zydis.c \
42-
-I ../../c_deps/zydis \
45+
"$ZYDIS_BRIDGE_C" \
4346
--dep zighook \
4447
-Mroot=hook.zig \
4548
-Mzighook=../../src/root.zig \

examples/instrument_no_original/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ cc -O3 -DNDEBUG -rdynamic -o target target.c -ldl
3030
```
3131

3232
```bash
33+
(cd ../.. && zig build --fetch)
34+
ZYDIS_BRIDGE_C="$(../../scripts/zydis-package-path.sh bridge-c)"
35+
3336
zig build-lib -dynamic -OReleaseFast -femit-bin=hook.so \
34-
../../c_deps/x86_64/decoder_zydis.c \
35-
-I ../../c_deps/zydis \
37+
"$ZYDIS_BRIDGE_C" \
3638
--dep zighook \
3739
-Mroot=hook.zig \
3840
-Mzighook=../../src/root.zig \

examples/instrument_unhook_restore/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ cc -O3 -DNDEBUG -rdynamic -o target target.c -ldl
3131
```
3232

3333
```bash
34+
(cd ../.. && zig build --fetch)
35+
ZYDIS_BRIDGE_C="$(../../scripts/zydis-package-path.sh bridge-c)"
36+
3437
zig build-lib -dynamic -OReleaseFast -femit-bin=hook.so \
35-
../../c_deps/x86_64/decoder_zydis.c \
36-
-I ../../c_deps/zydis \
38+
"$ZYDIS_BRIDGE_C" \
3739
--dep zighook \
3840
-Mroot=hook.zig \
3941
-Mzighook=../../src/root.zig \

examples/instrument_with_original/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ cc -O3 -DNDEBUG -rdynamic -o target target.c -ldl
2929
```
3030

3131
```bash
32+
(cd ../.. && zig build --fetch)
33+
ZYDIS_BRIDGE_C="$(../../scripts/zydis-package-path.sh bridge-c)"
34+
3235
zig build-lib -dynamic -OReleaseFast -femit-bin=hook.so \
33-
../../c_deps/x86_64/decoder_zydis.c \
34-
-I ../../c_deps/zydis \
36+
"$ZYDIS_BRIDGE_C" \
3537
--dep zighook \
3638
-Mroot=hook.zig \
3739
-Mzighook=../../src/root.zig \

examples/prepatched_inline_hook/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ zig build-lib -dynamic -OReleaseFast -femit-bin=hook.dylib \
3737
-lc
3838
```
3939

40-
Linux x86_64 uses the Zydis shim C source as well:
40+
Linux x86_64 fetches the pinned `zydis-zig` package and compiles its bridge C
41+
source as well:
4142

4243
```bash
44+
(cd ../.. && zig build --fetch)
45+
ZYDIS_BRIDGE_C="$(../../scripts/zydis-package-path.sh bridge-c)"
46+
4347
zig build-lib -dynamic -OReleaseFast -femit-bin=hook.so \
44-
../../c_deps/x86_64/decoder_zydis.c \
45-
-I ../../c_deps/zydis \
48+
"$ZYDIS_BRIDGE_C" \
4649
--dep zighook \
4750
-Mroot=hook.zig \
4851
-Mzighook=../../src/root.zig \

0 commit comments

Comments
 (0)