Skip to content

Commit 8770a67

Browse files
committed
Strengthen unhook smoke with pre/post verification
1 parent a665e55 commit 8770a67

4 files changed

Lines changed: 30 additions & 5 deletions

File tree

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ jobs:
6363
DYLIB="$PWD/target/aarch64-apple-darwin/debug/examples/libinstrument_unhook_restore.dylib"
6464
OUT=$(DYLD_INSERT_LIBRARIES="$DYLIB" examples/instrument_unhook_restore/app)
6565
echo "$OUT"
66+
[[ "$OUT" == *"hooked_calc(3, 4) = 123"* ]]
6667
[[ "$OUT" == *"calc(3, 4) = 7"* ]]
6768
6869
cc -O0 -fno-inline examples/inline_hook_far/target.c -o examples/inline_hook_far/app
@@ -137,6 +138,7 @@ jobs:
137138
DYLIB="$PWD/target/x86_64-apple-darwin/debug/examples/libinstrument_unhook_restore.dylib"
138139
OUT=$(DYLD_INSERT_LIBRARIES="$DYLIB" examples/instrument_unhook_restore/app)
139140
echo "$OUT"
141+
[[ "$OUT" == *"hooked_calc(3, 4) = 123"* ]]
140142
[[ "$OUT" == *"calc(3, 4) = 7"* ]]
141143
142144
cc -O0 -fno-inline examples/inline_hook_far/target.c -o examples/inline_hook_far/app
@@ -201,6 +203,13 @@ jobs:
201203
echo "$OUT"
202204
[[ "$OUT" == *"calc(4, 5) = 99"* ]]
203205
206+
cc -O0 -fno-inline -rdynamic examples/instrument_unhook_restore/target.c -o examples/instrument_unhook_restore/app
207+
SO="$PWD/target/x86_64-unknown-linux-gnu/debug/examples/libinstrument_unhook_restore.so"
208+
OUT=$(LD_PRELOAD="$SO" examples/instrument_unhook_restore/app)
209+
echo "$OUT"
210+
[[ "$OUT" == *"hooked_calc(3, 4) = 123"* ]]
211+
[[ "$OUT" == *"calc(3, 4) = 7"* ]]
212+
204213
cc -O0 -fno-inline -rdynamic examples/inline_hook_far/target.c -o examples/inline_hook_far/app
205214
SO="$PWD/target/x86_64-unknown-linux-gnu/debug/examples/libinline_hook_far.so"
206215
OUT=$(LD_PRELOAD="$SO" examples/inline_hook_far/app)
@@ -273,6 +282,7 @@ jobs:
273282
SO="$PWD/target/aarch64-unknown-linux-gnu/debug/examples/libinstrument_unhook_restore.so"
274283
OUT=$(LD_PRELOAD="$SO" examples/instrument_unhook_restore/app)
275284
echo "$OUT"
285+
[[ "$OUT" == *"hooked_calc(3, 4) = 123"* ]]
276286
[[ "$OUT" == *"calc(3, 4) = 7"* ]]
277287
278288
cc -O0 -fno-inline -rdynamic examples/instrument_adrp_no_original/target.c -o examples/instrument_adrp_no_original/app

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Available examples:
4040
- `x86_64-apple-darwin`: compile coverage for all 4 examples, plus optional `patch_asm` build
4141
- `aarch64-unknown-linux-gnu`: runtime smoke coverage for all 6 core examples (CI, includes `instrument_adrp_no_original`)
4242
- `aarch64-unknown-linux-gnu`: plus optional `patch_asm` smoke (`--features patch_asm`)
43-
- `x86_64-unknown-linux-gnu`: runtime smoke coverage for 4 base examples (CI), plus optional `patch_asm` smoke (`--features patch_asm`)
43+
- `x86_64-unknown-linux-gnu`: runtime smoke coverage for 5 base examples (CI), plus optional `patch_asm` smoke (`--features patch_asm`)
4444

4545
## Notes by architecture
4646

examples/instrument_unhook_restore/README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ Demonstrates `sighook::instrument` + `sighook::unhook`.
55
Flow:
66

77
1. install an instruction hook
8-
2. immediately call `unhook` on the same patchpoint
9-
3. execute target function and verify original behavior is restored
8+
2. call `calc(3, 4)` once while hook is active and verify hooked value `123`
9+
3. call `unhook` on the same patchpoint
10+
4. execute target function again and verify original behavior is restored (`7`)
1011

1112
The callback would force result to `123` if triggered:
1213

1314
- `aarch64`: set `x8 = 120`, `x9 = 3`, then original `add w0, w8, w9` runs
1415
- `linux x86_64`: set `rax = 123`
1516

16-
Expected runtime output proves callback is not reached after unhook.
17+
Expected runtime output proves both stages:
18+
19+
- callback is reached before unhook (`hooked_calc(3, 4) = 123`)
20+
- original behavior is restored after unhook (`calc(3, 4) = 7`)
1721

1822
## Run (from repository root)
1923

@@ -36,5 +40,6 @@ LD_PRELOAD="$PWD/target/debug/examples/libinstrument_unhook_restore.so" examples
3640
Expected output:
3741

3842
```text
43+
hooked_calc(3, 4) = 123
3944
calc(3, 4) = 7
4045
```

examples/instrument_unhook_restore/main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ static INIT_ARRAY: extern "C" fn() = init;
3434

3535
extern "C" fn init() {
3636
unsafe {
37+
let calc_symbol = libc::dlsym(libc::RTLD_DEFAULT, c"calc".as_ptr());
38+
if calc_symbol.is_null() {
39+
return;
40+
}
41+
let calc_fn: extern "C" fn(i32, i32) -> i32 = std::mem::transmute(calc_symbol);
42+
3743
let target_address = {
3844
#[cfg(all(
3945
any(target_os = "linux", target_os = "android"),
@@ -66,7 +72,11 @@ extern "C" fn init() {
6672
}
6773
};
6874

69-
if instrument(target_address, on_hit_should_not_run).is_err() {
75+
let _ = instrument(target_address, on_hit_should_not_run);
76+
77+
let hooked = calc_fn(3, 4);
78+
println!("hooked_calc(3, 4) = {hooked}");
79+
if hooked != 123 {
7080
return;
7181
}
7282

0 commit comments

Comments
 (0)