### Build all examples Source: https://github.com/hookforge/sighook/blob/main/examples/README.md Builds all examples from the repository root. ```bash cargo build --examples ``` -------------------------------- ### Run Instrumentation Example on Linux Source: https://github.com/hookforge/sighook/blob/main/examples/instrument_with_original/README.md Compile the target and run the instrumented application using LD_PRELOAD. ```bash cc -O0 -fno-inline examples/instrument_with_original/target.c -o examples/instrument_with_original/app cargo build --example instrument_with_original LD_PRELOAD="$PWD/target/debug/examples/libinstrument_with_original.so" examples/instrument_with_original/app ``` -------------------------------- ### Run example on macOS Source: https://github.com/hookforge/sighook/blob/main/examples/patch_asm_add_to_mul/README.md Compile the target C code and execute the application with the library injected via DYLD_INSERT_LIBRARIES. ```bash cc -O0 -fno-inline examples/patch_asm_add_to_mul/target.c -o examples/patch_asm_add_to_mul/app cargo build --example patch_asm_add_to_mul --features patch_asm DYLD_INSERT_LIBRARIES="$PWD/target/debug/examples/libpatch_asm_add_to_mul.dylib" examples/patch_asm_add_to_mul/app ``` -------------------------------- ### Build feature-gated asm example Source: https://github.com/hookforge/sighook/blob/main/examples/README.md Builds the patch_asm_add_to_mul example with the patch_asm feature enabled. ```bash cargo build --example patch_asm_add_to_mul --features patch_asm ``` -------------------------------- ### Run Instrumentation Example on macOS Source: https://github.com/hookforge/sighook/blob/main/examples/instrument_with_original/README.md Compile the target and run the instrumented application using DYLD_INSERT_LIBRARIES. ```bash cc -O0 -fno-inline examples/instrument_with_original/target.c -o examples/instrument_with_original/app cargo build --example instrument_with_original DYLD_INSERT_LIBRARIES="$PWD/target/debug/examples/libinstrument_with_original.dylib" examples/instrument_with_original/app ``` -------------------------------- ### Run instrumentation example on Linux Source: https://github.com/hookforge/sighook/blob/main/examples/instrument_unhook_restore/README.md Compiles the target and hook library, then executes the application with the library injected via LD_PRELOAD. ```bash cc -O0 -fno-inline -rdynamic examples/instrument_unhook_restore/target.c -o examples/instrument_unhook_restore/app cargo build --example instrument_unhook_restore LD_PRELOAD="$PWD/target/debug/examples/libinstrument_unhook_restore.so" examples/instrument_unhook_restore/app ``` -------------------------------- ### Build and Run AArch64 Instrumentation Example Source: https://github.com/hookforge/sighook/blob/main/examples/instrument_adrp_with_original/README.md Commands to compile the target application and the instrumentation library, then execute the instrumented binary. ```bash cc -O0 -fno-inline -rdynamic examples/instrument_adrp_with_original/target.c -o examples/instrument_adrp_with_original/app cargo build --example instrument_adrp_with_original LD_PRELOAD="$PWD/target/debug/examples/libinstrument_adrp_with_original.so" examples/instrument_adrp_with_original/app ``` -------------------------------- ### Run AArch64 semantic smoke examples Source: https://github.com/hookforge/sighook/blob/main/examples/README.md Executes the AArch64 semantic smoke examples on a real host process. ```bash ./scripts/run_aarch64_semantic_examples.sh ``` -------------------------------- ### Run example on Linux Source: https://github.com/hookforge/sighook/blob/main/examples/patch_asm_add_to_mul/README.md Compile the target C code with dynamic symbol support and execute the application with the library injected via LD_PRELOAD. ```bash cc -O0 -fno-inline -rdynamic examples/patch_asm_add_to_mul/target.c -o examples/patch_asm_add_to_mul/app cargo build --example patch_asm_add_to_mul --features patch_asm LD_PRELOAD="$PWD/target/debug/examples/libpatch_asm_add_to_mul.so" examples/patch_asm_add_to_mul/app ``` -------------------------------- ### Run patchcode example on Linux Source: https://github.com/hookforge/sighook/blob/main/examples/patchcode_add_to_mul/README.md Compiles the target and hook library, then executes the target with the library injected via LD_PRELOAD. ```bash cc -O0 -fno-inline examples/patchcode_add_to_mul/target.c -o examples/patchcode_add_to_mul/app cargo build --example patchcode_add_to_mul LD_PRELOAD="$PWD/target/debug/examples/libpatchcode_add_to_mul.so" examples/patchcode_add_to_mul/app ``` -------------------------------- ### Run patchcode example on macOS Source: https://github.com/hookforge/sighook/blob/main/examples/patchcode_add_to_mul/README.md Compiles the target and hook library, then executes the target with the library injected via DYLD_INSERT_LIBRARIES. ```bash cc -O0 -fno-inline examples/patchcode_add_to_mul/target.c -o examples/patchcode_add_to_mul/app cargo build --example patchcode_add_to_mul DYLD_INSERT_LIBRARIES="$PWD/target/debug/examples/libpatchcode_add_to_mul.dylib" examples/patchcode_add_to_mul/app ``` -------------------------------- ### Run instrumentation example on macOS Source: https://github.com/hookforge/sighook/blob/main/examples/instrument_unhook_restore/README.md Compiles the target and hook library, then executes the application with the library injected via DYLD_INSERT_LIBRARIES. ```bash cc -O0 -fno-inline examples/instrument_unhook_restore/target.c -o examples/instrument_unhook_restore/app cargo build --example instrument_unhook_restore DYLD_INSERT_LIBRARIES="$PWD/target/debug/examples/libinstrument_unhook_restore.dylib" examples/instrument_unhook_restore/app ``` -------------------------------- ### Build and Run ADRP Instrumentation Example Source: https://github.com/hookforge/sighook/blob/main/examples/instrument_adrp_no_original/README.md Commands to compile the target application, build the hook library, and execute the instrumented binary. ```bash cc -O0 -fno-inline -rdynamic examples/instrument_adrp_no_original/target.c -o examples/instrument_adrp_no_original/app cargo build --example instrument_adrp_no_original LD_PRELOAD="$PWD/target/debug/examples/libinstrument_adrp_no_original.so" examples/instrument_adrp_no_original/app ``` -------------------------------- ### Install sighook dependency Source: https://github.com/hookforge/sighook/blob/main/README.md Add the base sighook dependency to your Cargo.toml file. ```toml [dependencies] sighook = "0.10.0" ``` -------------------------------- ### Run inline hook signal example on Linux Source: https://github.com/hookforge/sighook/blob/main/examples/inline_hook_signal/README.md Compiles the target and library, then executes the application with the library injected via LD_PRELOAD. ```bash cc -O0 -fno-inline examples/inline_hook_signal/target.c -o examples/inline_hook_signal/app cargo build --example inline_hook_signal LD_PRELOAD="$PWD/target/debug/examples/libinline_hook_signal.so" examples/inline_hook_signal/app ``` -------------------------------- ### Install sighook with patch_asm feature Source: https://github.com/hookforge/sighook/blob/main/README.md Enable the patch_asm feature to support assembly-string patching, which includes the keystone-engine dependency. ```toml [dependencies] sighook = { version = "0.10.0", features = ["patch_asm"] } ``` -------------------------------- ### Run inline hook example on Linux Source: https://github.com/hookforge/sighook/blob/main/examples/inline_hook_far/README.md Compiles the target application and the hook library, then executes the app with the library injected via LD_PRELOAD. ```bash cc -O0 -fno-inline examples/inline_hook_far/target.c -o examples/inline_hook_far/app cargo build --example inline_hook_far LD_PRELOAD="$PWD/target/debug/examples/libinline_hook_far.so" examples/inline_hook_far/app ``` -------------------------------- ### Run inline hook signal example on macOS Source: https://github.com/hookforge/sighook/blob/main/examples/inline_hook_signal/README.md Compiles the target and library, then executes the application with the library injected via DYLD_INSERT_LIBRARIES. ```bash cc -O0 -fno-inline examples/inline_hook_signal/target.c -o examples/inline_hook_signal/app cargo build --example inline_hook_signal DYLD_INSERT_LIBRARIES="$PWD/target/debug/examples/libinline_hook_signal.dylib" examples/inline_hook_signal/app ``` -------------------------------- ### Run inline hook example on macOS Source: https://github.com/hookforge/sighook/blob/main/examples/inline_hook_far/README.md Compiles the target application and the hook library, then executes the app with the library injected via DYLD_INSERT_LIBRARIES. ```bash cc -O0 -fno-inline examples/inline_hook_far/target.c -o examples/inline_hook_far/app cargo build --example inline_hook_far DYLD_INSERT_LIBRARIES="$PWD/target/debug/examples/libinline_hook_far.dylib" examples/inline_hook_far/app ``` -------------------------------- ### Expected output for prepatched upgrade Source: https://github.com/hookforge/sighook/blob/main/examples/prepatched_upgrade_execute_original/README.md The expected output when the payload is successfully loaded and executed. ```text calc_prepatched() = 42 ``` -------------------------------- ### Build Commands for x86_64 Linux Source: https://github.com/hookforge/sighook/blob/main/examples/inline_hook_fpregs/README.md Compile the target application with AVX support and the hook library for x86_64 Linux. ```bash cc -O0 -fno-inline -mavx -rdynamic examples/inline_hook_fpregs/target.c -o examples/inline_hook_fpregs/app cargo build --example inline_hook_fpregs ``` -------------------------------- ### Run Hook on Linux Source: https://github.com/hookforge/sighook/blob/main/examples/inline_hook_fpregs/README.md Execute the target application with the hook library injected via LD_PRELOAD. ```bash LD_PRELOAD="$PWD/target/debug/examples/libinline_hook_fpregs.so" examples/inline_hook_fpregs/app ``` -------------------------------- ### Run instrument_no_original on Linux Source: https://github.com/hookforge/sighook/blob/main/examples/instrument_no_original/README.md Compiles the target and runs the instrumented application using LD_PRELOAD. ```bash cc -O0 -fno-inline examples/instrument_no_original/target.c -o examples/instrument_no_original/app cargo build --example instrument_no_original LD_PRELOAD="$PWD/target/debug/examples/libinstrument_no_original.so" examples/instrument_no_original/app ``` -------------------------------- ### Expected Output for x86_64 Source: https://github.com/hookforge/sighook/blob/main/examples/inline_hook_fpregs/README.md The expected console output after running the hook on x86_64. ```text target_vec_add = [42, 43, 44, 45, 46, 47, 48, 49] ``` -------------------------------- ### Build Commands for aarch64 Source: https://github.com/hookforge/sighook/blob/main/examples/inline_hook_fpregs/README.md Compile the target application and the hook library for aarch64 systems. ```bash cc -O0 -fno-inline examples/inline_hook_fpregs/target.c -o examples/inline_hook_fpregs/app cargo build --example inline_hook_fpregs ``` -------------------------------- ### Run Hook on macOS Source: https://github.com/hookforge/sighook/blob/main/examples/inline_hook_fpregs/README.md Execute the target application with the hook library injected via DYLD_INSERT_LIBRARIES. ```bash DYLD_INSERT_LIBRARIES="$PWD/target/debug/examples/libinline_hook_fpregs.dylib" examples/inline_hook_fpregs/app ``` -------------------------------- ### Expected Output for aarch64 Source: https://github.com/hookforge/sighook/blob/main/examples/inline_hook_fpregs/README.md The expected console output after running the hook on aarch64. ```text target_vec_add = [42, 43, 44, 45] ``` -------------------------------- ### Verify DT_NEEDED Dependencies Source: https://github.com/hookforge/sighook/blob/main/README.md Check the dynamic section of the target library to confirm the payload is correctly linked. ```bash readelf -d libtarget.so | grep NEEDED ``` -------------------------------- ### Expected output Source: https://github.com/hookforge/sighook/blob/main/examples/patchcode_add_to_mul/README.md The output observed after successfully patching the addition instruction to multiplication. ```text calc(6, 7) = 42 ``` -------------------------------- ### Expected runtime output Source: https://github.com/hookforge/sighook/blob/main/examples/instrument_unhook_restore/README.md The console output verifying that the function was hooked and subsequently restored. ```text hooked_calc(3, 4) = 123 calc(3, 4) = 7 ``` -------------------------------- ### Expected output for b.nv instruction Source: https://github.com/hookforge/sighook/blob/main/examples/instrument_b_cond_nv/README.md The expected console output when the payload is successfully loaded and executed. ```text branch_nv() = 2 ``` -------------------------------- ### Run project validation and release checks Source: https://github.com/hookforge/sighook/blob/main/AGENT.md Executes standard formatting, linting, and cross-compilation checks for various targets to ensure project stability. ```bash cargo fmt --all -- --check cargo check --all-targets cargo test --doc cargo clippy --all-targets -- -D warnings cargo check --all-targets --target aarch64-apple-darwin cargo check --all-targets --target aarch64-apple-ios cargo check --all-targets --target aarch64-linux-android cargo check --all-targets --target x86_64-unknown-linux-gnu # optional but recommended pre-release target checks cargo check --tests --target aarch64-apple-ios cargo check --tests --target aarch64-linux-android ``` ```bash cargo check --all-targets --target aarch64-unknown-linux-gnu cargo clippy --all-targets --target aarch64-unknown-linux-gnu -- -D warnings ``` ```bash cargo test --all-targets ``` -------------------------------- ### Perform BRK instrumentation with original opcode execution Source: https://github.com/hookforge/sighook/blob/main/README.md Use instrument to hook an instruction while allowing the original opcode to execute. ```rust use sighook::{instrument, HookContext}; extern "C" fn on_hit(_address: u64, ctx: *mut HookContext) { let _ = ctx; } let target_instruction = 0x1000_0000_u64; let _original = instrument(target_instruction, on_hit)?; # Ok::<(), sighook::SigHookError>(()) ``` -------------------------------- ### Patch instructions with assembly strings Source: https://github.com/hookforge/sighook/blob/main/README.md Use patch_asm to overwrite instructions with assembly strings, requiring the patch_asm feature. ```rust # #[cfg(feature = "patch_asm")] # { use sighook::patch_asm; let target_instruction = 0x1000_0000_u64; #[cfg(target_arch = "aarch64")] let _original = patch_asm(target_instruction, "mul w0, w8, w9")?; #[cfg(all(target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))] let _original = patch_asm(target_instruction, "imul %edx")?; # } # Ok::<(), sighook::SigHookError>(()) ``` -------------------------------- ### Expected Output for Literal Load Faults Source: https://github.com/hookforge/sighook/blob/main/examples/instrument_literal_fault_pc/README.md The expected output format when running on Linux aarch64 with the payload loaded. ```text fault_pc=0x... patchpoint=0x... match=1 ``` -------------------------------- ### Expected Output Source: https://github.com/hookforge/sighook/blob/main/examples/instrument_with_original/README.md The output observed after successful instrumentation. ```text calc(1, 2) = 42 ``` -------------------------------- ### Unhook and restore original instructions Source: https://github.com/hookforge/sighook/blob/main/README.md Use unhook to remove an existing hook and restore the original instruction state. ```rust use sighook::{instrument, unhook, HookContext}; extern "C" fn on_hit(_address: u64, _ctx: *mut HookContext) {} let target_instruction = 0x1000_0000_u64; let _ = instrument(target_instruction, on_hit)?; unhook(target_instruction)?; # Ok::<(), sighook::SigHookError>(()) ``` -------------------------------- ### Build Hook Payload for Android Source: https://github.com/hookforge/sighook/blob/main/README.md Compile the hook payload for the aarch64-linux-android target using Cargo. ```bash cargo build --release --target aarch64-linux-android ``` -------------------------------- ### Expected output for patch_race_stress Source: https://github.com/hookforge/sighook/blob/main/examples/patch_race_stress/README.md The expected console output when the payload is successfully loaded during the stress test. ```text stress ok ``` -------------------------------- ### Inject Payload into Target Library Source: https://github.com/hookforge/sighook/blob/main/README.md Use patchelf to add the hook payload as a dependency to the target shared object. ```bash patchelf --add-needed libsighook_payload.so libtarget.so ``` -------------------------------- ### Implement jump-based inline detour Source: https://github.com/hookforge/sighook/blob/main/README.md Use inline_hook_jump to redirect execution flow to a specific replacement address. ```rust use sighook::inline_hook_jump; extern "C" fn replacement() {} let function_entry = 0x1000_1000_u64; let replacement_addr = replacement as usize as u64; let _original = inline_hook_jump(function_entry, replacement_addr)?; # Ok::<(), sighook::SigHookError>(()) ``` -------------------------------- ### Expected output Source: https://github.com/hookforge/sighook/blob/main/examples/inline_hook_signal/README.md The output observed when the hook successfully intercepts and modifies the target function result. ```text target_add(6, 7) = 42 ``` -------------------------------- ### Perform BRK instrumentation without original opcode execution Source: https://github.com/hookforge/sighook/blob/main/README.md Use instrument_no_original to hook an instruction and replace the original logic entirely. ```rust use sighook::{instrument_no_original, HookContext}; extern "C" fn replace_logic(_address: u64, ctx: *mut HookContext) { let _ = ctx; } let target_instruction = 0x1000_0010_u64; let _original = instrument_no_original(target_instruction, replace_logic)?; # Ok::<(), sighook::SigHookError>(()) ``` -------------------------------- ### Implement signal-based inline entry hook Source: https://github.com/hookforge/sighook/blob/main/README.md Use inline_hook to intercept function entry and modify registers via the HookContext. ```rust use sighook::{inline_hook, HookContext}; extern "C" fn replacement(_address: u64, ctx: *mut HookContext) { unsafe { #[cfg(target_arch = "aarch64")] { (*ctx).regs.named.x0 = 42; } #[cfg(all(target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))] { (*ctx).rax = 42; } } } let function_entry = 0x1000_1000_u64; let _original = inline_hook(function_entry, replacement)?; # Ok::<(), sighook::SigHookError>(()) ``` -------------------------------- ### Validate AArch64 nop patchpoint output Source: https://github.com/hookforge/sighook/blob/main/examples/instrument_nop_x16/README.md Displays the expected output after the payload is loaded and the marker is read. ```text read_marker() = 4660 ``` -------------------------------- ### Expected output for blr instrumentation validation Source: https://github.com/hookforge/sighook/blob/main/examples/instrument_blr_lr/README.md The output format for verifying that the observed link register matches the expected value. ```text observe_lr() = 0x... expected = 0x... ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.