feat: add unhook() API with full restore and trampoline cleanup#2
Closed
feat: add unhook() API with full restore and trampoline cleanup#2
Conversation
Review and implement unhook code from codex/unhook-smoke-ci branch: 1. Add `unhook()` public API to restore instrument and inline_hook patches 2. Add `HookNotFound` error variant for unhook on unknown addresses 3. Add `InlinePatchSlot` storage in state.rs for inline_hook original bytes 4. Add `free_original_trampoline` to properly munmap trampoline pages 5. Refactor `inline_hook` to save original bytes before patching (enables restore) 6. Improve `instrument_internal` x86_64: pad full instruction with int3+NOPs, derive original_opcode from original_bytes instead of redundant read 7. Remove now-unused `patch_far_jump` and `patch_u8` from memory.rs 8. Make `read_bytes` available on all platforms (needed by aarch64 inline_hook) 9. Add instrument_unhook_restore example with CI smoke tests Co-authored-by: YinMo19 <144041694+YinMo19@users.noreply.github.com>
…e callback Co-authored-by: YinMo19 <144041694+YinMo19@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Review new branch submission for unhook code
feat: add unhook() API with full restore and trampoline cleanup
Feb 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Review and implement the unhook code from
codex/unhook-smoke-cibranch, with fixes identified during review.New public API
unhook(address) -> Result<(), SigHookError>— restores original bytes for hooks created byinstrument,instrument_no_original, orinline_hook. Frees trampoline pages and cleans up all internal state.SigHookError::HookNotFound— returned when unhooking an address with no active hook.Internal changes
state.rs: AddInlinePatchSlotarray to store full original bytes forinline_hookpatches (needed for restore). Addremove_slot,remove_inline_patch,remove_cached_original_opcodecleanup helpers.trampoline.rs: Addfree_original_trampolinetomunmaptrampoline pages on unhook.inline_hookrefactored: Now reads and caches original bytes before patching, enabling byte-exact restore. Includes rollback if patching fails.instrument_internalx86_64 improved: Pads full decoded instruction withint3 + NOP(was: singleint3byte). Derivesoriginal_opcodefromoriginal_bytesdirectly instead of a redundant 4-byte read.memory.rs:read_bytesungated from x86_64-only (aarch64inline_hooknow needs it). Removed deadpatch_far_jumpandpatch_u8.Review fixes vs upstream branch
read_bytes(address, 4)ininstrument_internal— opcode derived from already-readoriginal_bytesif step_len == 1special case —patch_bytes_publichandles single-byte patchesunsafeblocks per Rust 2024 edition semanticsExample & CI
instrument_unhook_restoreexample: hookscalc, verifies123, unhooks, verifies original7✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.