-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d123874
commit 0c99206
Showing
5 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
use inka::program; | ||
use inka::Hook; | ||
|
||
fn main() { | ||
dbg!(program()); | ||
unsafe extern "C" fn hook_me() { | ||
println!("Original"); | ||
} | ||
|
||
let _guard = (hook_me as unsafe extern "C" fn()).hook(|| println!("Hooked")); | ||
dbg!(_guard); | ||
|
||
unsafe { hook_me() }; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use core::any::Any; | ||
use sharded_slab::Slab; | ||
use std::{ | ||
ptr::NonNull, | ||
sync::{LazyLock, Mutex}, | ||
}; | ||
|
||
static HOOKS: LazyLock<Slab<Mutex<Box<dyn Any + Send>>>> = LazyLock::new(Slab::new); | ||
|
||
pub trait Hook<F>: Copy { | ||
fn hook(self, f: F) -> HookGuard; | ||
fn as_ptr_u8(self) -> NonNull<u8>; | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct HookGuard { | ||
ptr: NonNull<u8>, | ||
bytes: [u8; 10], // used to restore original function prologue | ||
index: usize, | ||
} | ||
|
||
impl HookGuard { | ||
pub fn unhook(&mut self) { | ||
HOOKS.remove(self.index); | ||
} | ||
} | ||
|
||
impl Drop for HookGuard { | ||
fn drop(&mut self) { | ||
self.unhook(); | ||
} | ||
} | ||
|
||
impl<F> Hook<F> for unsafe extern "C" fn() | ||
where | ||
F: FnMut() + 'static + Send, | ||
{ | ||
fn hook(self, f: F) -> HookGuard { | ||
let ptr = <unsafe extern "C" fn() as Hook<F>>::as_ptr_u8(self); | ||
|
||
let index = HOOKS | ||
.insert(Mutex::new(Box::new(f))) | ||
.expect("Max shards reached"); | ||
|
||
HookGuard { | ||
ptr, | ||
bytes: [0; 10], | ||
index, | ||
} | ||
} | ||
|
||
fn as_ptr_u8(self) -> NonNull<u8> { | ||
unsafe { NonNull::new_unchecked(self as *mut u8) } | ||
} | ||
} |
This file contains 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