Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
KiraCoding committed Aug 27, 2024
1 parent 314ed7b commit 3d1ac81
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/hook.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
use core::sync::atomic::{AtomicPtr, Ordering};
use core::ptr::null_mut;

pub trait Hook<F>: Copy {

fn hook(&self) -> HookGuard {
let self_ptr = self.as_ptr_u8();

HookGuard { bytes: [0; 16] }
}

fn as_ptr_u8(self) -> *mut u8;
fn trampoline(f: F) -> Closure<F>;
}
Expand All @@ -11,9 +21,8 @@ impl HookGuard {
pub fn unhook(&self) {}
}


pub struct Closure<F> {
inner: Box<ClosureInner<F>>
inner: Box<ClosureInner<F>>,
}

#[repr(C)]
Expand All @@ -22,3 +31,30 @@ struct ClosureInner<F> {
data: F,
}

static STATIC_CONTEXT: AtomicPtr<()> = AtomicPtr::new(null_mut());

impl<F, R> Hook<F> for unsafe extern "C" fn() -> R
where
F: FnMut() + 'static,
{
fn as_ptr_u8(self) -> *mut u8 {
self as *mut u8
}

fn trampoline(f: F) -> Closure<F> {
unsafe extern "C" fn thunk<F, R>()
where
F: FnMut(),
{
let p = STATIC_CONTEXT.swap(null_mut(), Ordering::Relaxed) as *mut ClosureInner<F>;
((*p).data)();
}

Closure {
inner: Box::new(ClosureInner {
ptr: thunk::<F, R> as *const (),
data: f,
}),
}
}
}

0 comments on commit 3d1ac81

Please sign in to comment.