Skip to content

Commit 8bd6f1a

Browse files
committed
Fix x86 trap padding semantics and robust unhook restore
1 parent 72b1b98 commit 8bd6f1a

3 files changed

Lines changed: 12 additions & 17 deletions

File tree

examples/instrument_unhook_restore/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,19 @@ extern "C" fn init() {
7272
}
7373
};
7474

75-
let _ = instrument(target_address, on_hit_should_not_run);
75+
if let Err(err) = instrument(target_address, on_hit_should_not_run) {
76+
println!("instrument failed: {err}");
77+
return;
78+
}
7679

7780
let hooked = calc_fn(3, 4);
7881
println!("hooked_calc(3, 4) = {hooked}");
7982
if hooked != 123 {
8083
return;
8184
}
8285

83-
let _ = unhook(target_address);
86+
if let Err(err) = unhook(target_address) {
87+
println!("unhook failed: {err}");
88+
}
8489
}
8590
}

src/lib.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,9 @@ fn instrument_internal(
291291
let original_bytes = memory::read_bytes(address, step_len as usize)?;
292292
let original4 = memory::read_bytes(address, 4)?;
293293

294-
if step_len == 1 {
295-
let _ = memory::patch_u8(address, memory::int3_opcode())?;
296-
} else {
297-
let mut trap_patch = vec![0x90u8; step_len as usize];
298-
trap_patch[0] = memory::int3_opcode();
299-
let _ = memory::patch_bytes_public(address, &trap_patch)?;
300-
}
294+
let mut trap_patch = vec![0x90u8; step_len as usize];
295+
trap_patch[0] = memory::int3_opcode();
296+
let _ = memory::patch_bytes_public(address, &trap_patch)?;
301297

302298
let mut opcode = [0u8; 4];
303299
opcode.copy_from_slice(&original4);
@@ -371,7 +367,7 @@ pub fn inline_hook(addr: u64, replace_fn: u64) -> Result<u32, SigHookError> {
371367
Err(err) => return Err(err),
372368
};
373369

374-
let original = memory::read_bytes(addr, patch.len())?;
370+
let original = memory::read_bytes(addr, 16)?;
375371
let inserted = unsafe { state::cache_inline_patch(addr, &original)? };
376372
if let Err(err) = memory::patch_bytes_public(addr, &patch) {
377373
if inserted {
@@ -403,7 +399,7 @@ pub fn inline_hook(addr: u64, replace_fn: u64) -> Result<u32, SigHookError> {
403399
memory::encode_absolute_jump(replace_fn).to_vec()
404400
};
405401

406-
let original = memory::read_bytes(addr, patch.len())?;
402+
let original = memory::read_bytes(addr, memory::encode_absolute_jump(0).len())?;
407403
let inserted = unsafe { state::cache_inline_patch(addr, &original)? };
408404
if let Err(err) = memory::patch_bytes_public(addr, &patch) {
409405
if inserted {

src/memory.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,6 @@ fn patch_bytes(address: u64, bytes: &[u8]) -> Result<Vec<u8>, SigHookError> {
248248
Ok(original)
249249
}
250250

251-
#[cfg(all(target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
252-
pub(crate) fn patch_u8(address: u64, new_opcode: u8) -> Result<u8, SigHookError> {
253-
let original = patch_bytes(address, &[new_opcode])?;
254-
Ok(original[0])
255-
}
256-
257251
pub(crate) fn read_bytes(address: u64, len: usize) -> Result<Vec<u8>, SigHookError> {
258252
if address == 0 || len == 0 {
259253
return Err(SigHookError::InvalidAddress);

0 commit comments

Comments
 (0)