1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/// A macro for defining type-safe detours.
///
/// This macro creates a `StaticDetourController`, which returns a `StaticDetour`
/// upon initialization. It can accept both functions and closures as its second
/// argument. Due to the requirements of the implementation, *const_fn* is needed
/// if the macro is to be used.
///
/// A static detour may only have one active detour at a time. Therefore another
/// `initialize` call can only be done once the previous instance has been
/// dropped. This is because the closures are being stored as static variables.
///
/// # Example
///
/// ```rust
/// #![feature(const_fn, const_atomic_ptr_new, const_ptr_null_mut)]
/// #[macro_use] extern crate detour;
///
/// use detour::Detour;
///
/// static_detours! {
///     struct Test: /* extern "X" */ fn(i32) -> i32;
/// }
///
/// fn add5(val: i32) -> i32 { val + 5 }
/// fn add10(val: i32) -> i32 { val + 10 }
///
/// fn main() {
///     let mut hook = unsafe { Test.initialize(add5, add10).unwrap() };
///
///     assert_eq!(add5(1), 6);
///     assert_eq!(hook.call(1), 6);
///
///     unsafe { hook.enable().unwrap(); }
///
///     assert_eq!(add5(1), 11);
///     assert_eq!(hook.call(1), 6);
///
///     // You can also call using the static object
///     assert_eq!(unsafe { Test.get().unwrap().call(1) }, 6);
///
///     // ... and change the detour whilst hooked
///     hook.set_detour(|val| val - 5);
///     assert_eq!(add5(5), 0);
///
///     unsafe { hook.disable().unwrap() };
///
///     assert_eq!(add5(1), 6);
/// }
/// ```
///
/// Any type of function is supported, and *extern* is optional.
#[cfg(feature = "static")]
#[macro_export]
// Inspired by: https://github.com/Jascha-N/minhook-rs
macro_rules! static_detours {
    // 1 — meta attributes
    // See: https://github.com/rust-lang/rust/issues/24189
    (@parse_attributes ($($input:tt)*) | $(#[$attribute:meta])* $next:tt $($rest:tt)*) => {
        static_detours!(@parse_access_modifier ($($input)* ($($attribute)*)) | $next $($rest)*);
    };

    // 2 — pub modifier (yes/no)
    (@parse_access_modifier ($($input:tt)*) | pub struct $($rest:tt)*) => {
        static_detours!(@parse_name ($($input)* (pub)) | $($rest)*);
    };
    (@parse_access_modifier ($($input:tt)*) | struct $($rest:tt)*) => {
        static_detours!(@parse_name ($($input)* ()) | $($rest)*);
    };

    // 3 — detour name
    (@parse_name ($($input:tt)*) | $name:ident : $($rest:tt)*) => {
        static_detours!(@parse_unsafe ($($input)* ($name)) | $($rest)*);
    };

    // 4 — unsafe modifier (yes/no)
    (@parse_unsafe ($($input:tt)*) | unsafe $($rest:tt)*) => {
        static_detours!(@parse_calling_convention ($($input)*) (unsafe) | $($rest)*);
    };
    (@parse_unsafe ($($input:tt)*) | $($rest:tt)*) => {
        static_detours!(@parse_calling_convention ($($input)*) () | $($rest)*);
    };

    // 5 — calling convention (extern "XXX"/extern/-)
    (@parse_calling_convention ($($input:tt)*) ($($modifier:tt)*) | extern $cc:tt fn $($rest:tt)*) => {
        static_detours!(@parse_prototype ($($input)* ($($modifier)* extern $cc)) | $($rest)*);
    };
    (@parse_calling_convention ($($input:tt)*) ($($modifier:tt)*) | extern fn $($rest:tt)*) => {
        static_detours!(@parse_prototype ($($input)* ($($modifier)* extern)) | $($rest)*);
    };
    (@parse_calling_convention ($($input:tt)*) ($($modifier:tt)*) | fn $($rest:tt)*) => {
        static_detours!(@parse_prototype ($($input)* ($($modifier)*)) | $($rest)*);
    };

    // 6 — argument and return type (return/void)
    (@parse_prototype ($($input:tt)*) | ($($argument_type:ty),*) -> $return_type:ty ; $($rest:tt)*) => {
        static_detours!(@parse_terminator ($($input)* ($($argument_type)*) ($return_type)) | ; $($rest)*);
    };
    (@parse_prototype ($($input:tt)*) | ($($argument_type:ty),*) $($rest:tt)*) => {
        static_detours!(@parse_terminator ($($input)* ($($argument_type)*) (())) | $($rest)*);
    };

    // 7 — semicolon terminator
    (@parse_terminator ($($input:tt)*) | ; $($rest:tt)*) => {
        static_detours!(@parse_entries ($($input)*) | $($rest)*);
    };

    // 8 - additional detours (multiple/single)
    (@parse_entries ($($input:tt)*) | $($rest:tt)+) => {
        static_detours!(@aggregate $($input)*);
        static_detours!($($rest)*);
    };
    (@parse_entries ($($input:tt)*) | ) => {
        static_detours!(@aggregate $($input)*);
    };

    // 9 - aggregate data for the generate function
    (@aggregate ($($attribute:meta)*) ($($visibility:tt)*) ($name:ident)
                ($($modifier:tt)*) ($($argument_type:ty)*) ($return_type:ty)) => {
        static_detours!(@argument_names (create_detour)(
            ($($attribute)*) ($($visibility)*) ($name)
            ($($modifier)*) ($($argument_type)*) ($return_type)
            ($($modifier)* fn ($($argument_type),*) -> $return_type)
        )($($argument_type)*));
    };

    // 10 - detour type implementation
    // TODO: Support access to detour in closure
    (@create_detour ($($argument_name:ident)*) ($($attribute:meta)*) ($($visibility:tt)*)
                    ($name:ident) ($($modifier:tt)*) ($($argument_type:ty)*)
                    ($return_type:ty) ($fn_type:ty)) => {
        static_detours!(@generate
            #[allow(non_upper_case_globals)]
            $(#[$attribute])*
            $($visibility)* static $name: $crate::StaticDetourController<$fn_type> = {
                use std::sync::atomic::{AtomicPtr, Ordering};
                use std::ptr;

                static DATA: AtomicPtr<$crate::__StaticDetourInner<$fn_type>> =
                    AtomicPtr::new(ptr::null_mut());

                #[inline(never)]
                $($modifier) * fn __ffi_detour($($argument_name: $argument_type),*) -> $return_type {
                    let data = unsafe { DATA.load(Ordering::SeqCst).as_ref().unwrap() };
                    (data.closure)($($argument_name),*)
                }

                $crate::StaticDetourController::__new(&DATA, __ffi_detour)
            };
        );
    };

    // Associates each argument type with a dummy name.
    (@argument_names ($label:ident) ($($input:tt)*) ($($token:tt)*)) => {
        static_detours!(@argument_names ($label) ($($input)*)(
            __arg_0  __arg_1  __arg_2  __arg_3  __arg_4  __arg_5  __arg_6
            __arg_7  __arg_8  __arg_9  __arg_10 __arg_11 __arg_12 __arg_13
        )($($token)*)());
    };
    (@argument_names ($label:ident) ($($input:tt)*) ($hd_name:tt $($tl_name:tt)*) ($hd:tt $($tl:tt)*) ($($acc:tt)*) ) => {
        static_detours!(@argument_names ($label) ($($input)*) ($($tl_name)*) ($($tl)*) ($($acc)* $hd_name));
    };
    (@argument_names ($label:ident) ($($input:tt)*) ($($name:tt)*) () ($($acc:tt)*)) => {
        static_detours!(@$label ($($acc)*) $($input)*);
    };

    (@generate $item:item) => { $item };

    // Bootstrapper
    ($($t:tt)+) => {
        static_detours!(@parse_attributes () | $($t)+);
    };
}

macro_rules! impl_hookable {
    (@recurse () ($($nm:ident : $ty:ident),*)) => {
        impl_hookable!(@impl_all ($($nm : $ty),*));
    };
    (@recurse ($hd_nm:ident : $hd_ty:ident $(, $tl_nm:ident : $tl_ty:ident)*) ($($nm:ident : $ty:ident),*)) => {
        impl_hookable!(@impl_all ($($nm : $ty),*));
        impl_hookable!(@recurse ($($tl_nm : $tl_ty),*) ($($nm : $ty,)* $hd_nm : $hd_ty));
    };

    (@impl_all ($($nm:ident : $ty:ident),*)) => {
        impl_hookable!(@impl_pair ($($nm : $ty),*) (                  fn($($ty),*) -> Ret));
        impl_hookable!(@impl_pair ($($nm : $ty),*) (extern "cdecl"    fn($($ty),*) -> Ret));
        impl_hookable!(@impl_pair ($($nm : $ty),*) (extern "stdcall"  fn($($ty),*) -> Ret));
        impl_hookable!(@impl_pair ($($nm : $ty),*) (extern "fastcall" fn($($ty),*) -> Ret));
        impl_hookable!(@impl_pair ($($nm : $ty),*) (extern "win64"    fn($($ty),*) -> Ret));
        impl_hookable!(@impl_pair ($($nm : $ty),*) (extern "C"        fn($($ty),*) -> Ret));
        impl_hookable!(@impl_pair ($($nm : $ty),*) (extern "system"   fn($($ty),*) -> Ret));
    };

    (@impl_pair ($($nm:ident : $ty:ident),*) ($($fn_t:tt)*)) => {
        impl_hookable!(@impl_fun ($($nm : $ty),*) ($($fn_t)*) (unsafe $($fn_t)*));
    };

    (@impl_fun ($($nm:ident : $ty:ident),*) ($safe_type:ty) ($unsafe_type:ty)) => {
        impl_hookable!(@impl_core ($($nm : $ty),*) ($safe_type));
        impl_hookable!(@impl_core ($($nm : $ty),*) ($unsafe_type));

        impl_hookable!(@impl_hookable_with ($($nm : $ty),*) ($unsafe_type) ($safe_type));
        impl_hookable!(@impl_safe ($($nm : $ty),*) ($safe_type));
    };

    (@impl_hookable_with ($($nm:ident : $ty:ident),*) ($target:ty) ($detour:ty)) => {
        unsafe impl<Ret: 'static, $($ty: 'static),*> HookableWith<$detour> for $target {}
    };

    (@impl_safe ($($nm:ident : $ty:ident),*) ($fn_type:ty)) => {
        impl<Ret: 'static, $($ty: 'static),*> $crate::GenericDetour<$fn_type> {
            #[doc(hidden)]
            pub fn call(&self, $($nm : $ty),*) -> Ret {
                unsafe {
                    let original: $fn_type = ::std::mem::transmute(self.trampoline());
                    original($($nm),*)
                }
            }
        }
    };

    (@impl_core ($($nm:ident : $ty:ident),*) ($fn_type:ty)) => {
        unsafe impl<Ret: 'static, $($ty: 'static),*> Function for $fn_type {
            type Arguments = ($($ty,)*);
            type Output = Ret;

            unsafe fn from_ptr(ptr: *const ()) -> Self {
                ::std::mem::transmute(ptr)
            }

            fn to_ptr(&self) -> *const () {
                unsafe { ::std::mem::transmute(*self) }
            }
        }
    };

    ($($nm:ident : $ty:ident),*) => {
        impl_hookable!(@recurse ($($nm : $ty),*) ());
    };
}