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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/// Defines one or more static hooks.
///
/// A `static_hooks!` block can contain one or more hook definitions of the following forms:
///
/// ```ignore
/// // Creates a `StaticHookWithDefault`
/// #[ATTR]* pub? impl HOOK_VAR_NAME for PATH::TO::TARGET: FN_TYPE = FN_EXPR;
/// #[ATTR]* pub? impl HOOK_VAR_NAME for "FUNCTION" in "MODULE": FN_TYPE = FN_EXPR;
///
/// // Creates a `StaticHook`
/// #[ATTR]* pub? impl HOOK_VAR_NAME for PATH::TO::TARGET: FN_TYPE;
/// #[ATTR]* pub? impl HOOK_VAR_NAME for "FUNCTION" in "MODULE": FN_TYPE;
/// ```
///
/// All of the above definitions create a static variable with the specified name of
/// type `StaticHook` or `StaticHookWithDefault` for a target function of the given
/// type. If the function signature contains `extern`, any panics that happen inside of the
/// detour `Fn` are automatically caught before they can propagate across foreign code boundaries.
/// See the `panic` submodule for more information.
///
/// The first two forms create a static hook with a default detour `Fn`. This is useful if
/// the detour `Fn` is a closure that does not need to capture any local variables
/// or if the detour `Fn` is just a normal function. See `StaticHookWithDefault`.
///
/// The last two forms require a `Fn` to be supplied at the time of initialization of the
/// static hook. In this case a closure that captures local variables can be supplied.
/// See `StaticHook`.
///
/// The first and third forms are used for hooking functions by their compile-time identifier.
///
/// The second and fourth form will try to find the target function by name at initialization
/// instead of at compile time. These forms require the exported function symbol name and
/// its containing module's name to be supplied.
///
/// The optional `pub` keyword can be used to give the resulting hook variable public
/// visibility. Any attributes used on a hook definition will be applied to the resulting
/// hook variable.
#[macro_export]
#[cfg_attr(rustfmt, rustfmt_skip)]
macro_rules! static_hooks {
    // Step 1: parse attributes
    (@parse_attr ($($args:tt)*)
               | $(#[$var_attr:meta])* $next:tt $($rest:tt)*) => {
        static_hooks!(@parse_pub ($($args)* ($($var_attr)*)) | $next $($rest)*);
    };

    // Step 2: parse optional pub modifier
    (@parse_pub ($($args:tt)*)
              | pub impl $($rest:tt)*) =>
    {
        static_hooks!(@parse_mod ($($args)* (pub)) | $($rest)*);
    };
    (@parse_pub ($($args:tt)*)
              | impl $($rest:tt)*) =>
    {
        static_hooks!(@parse_mod ($($args)* ()) | $($rest)*);
    };

    // Step 3: parse optional mut or const modifier
    // (@parse_mod ($($args:tt)*)
    //           | mut $($rest:tt)*) =>
    // {
    //     static_hooks!(@parse_name_target ($($args)* (mut)) | $($rest)*);
    // };
    // (@parse_mod ($($args:tt)*)
    //           | const $($rest:tt)*) =>
    // {
    //     static_hooks!(@parse_name_target ($($args)* (const)) | $($rest)*);
    // };
    (@parse_mod ($($args:tt)*)
              | $($rest:tt)*) =>
    {
        static_hooks!(@parse_name_target ($($args)* ()) | $($rest)*);
    };

    // Step 4: parse name and target
    (@parse_name_target ($($args:tt)*)
                      | $var_name:ident for $target_fn_name:tt in $target_mod_name:tt : $($rest:tt)*) =>
    {
        static_hooks!(@parse_fn_unsafe ($($args)* ($var_name) ($crate::__StaticHookTarget::Dynamic($target_mod_name, $target_fn_name))) | $($rest)*);
    };
    (@parse_name_target ($($args:tt)*)
                      | $var_name:ident for $target_path:path : $($rest:tt)*) =>
    {
        static_hooks!(@parse_fn_unsafe ($($args)* ($var_name) ($crate::__StaticHookTarget::Static($target_path))) | $($rest)*);
    };

    // Step 5a: parse optional unsafe modifier
    (@parse_fn_unsafe ($($args:tt)*)
                    | unsafe $($rest:tt)*) =>
    {
        static_hooks!(@parse_fn_linkage ($($args)*) (unsafe) | $($rest)*);
    };
    (@parse_fn_unsafe ($($args:tt)*)
                    | $($rest:tt)*) => {
        static_hooks!(@parse_fn_linkage ($($args)*) () | $($rest)*);
    };

    // Step 5b: parse linkage
    (@parse_fn_linkage ($($args:tt)*) ($($fn_mod:tt)*)
                     | extern $linkage:tt fn $($rest:tt)*) =>
    {
        static_hooks!(@parse_fn_args ($($args)* ($($fn_mod)* extern $linkage) (GUARD)) | $($rest)*);
    };
    (@parse_fn_linkage ($($args:tt)*) ($($fn_mod:tt)*)
                     | extern fn $($rest:tt)*) =>
    {
        static_hooks!(@parse_fn_args ($($args)* ($($fn_mod)* extern) (GUARD)) | $($rest)*);
    };
    (@parse_fn_linkage ($($args:tt)*) ($($fn_mod:tt)*)
                     | fn $($rest:tt)*) =>
    {
        static_hooks!(@parse_fn_args ($($args)* ($($fn_mod)*) (NO_GUARD)) | $($rest)*);
    };

    // Step 5c: parse argument types and return type
    // Requires explicit look-ahead to satisfy rule for tokens following ty fragment specifier
    (@parse_fn_args ($($args:tt)*)
                  | ($($arg_type:ty),*) -> $return_type:ty = $($rest:tt)*) =>
    {
        static_hooks!(@parse_fn_value ($($args)* ($($arg_type)*) ($return_type)) | = $($rest)*);
    };
    (@parse_fn_args ($($args:tt)*)
                  | ($($arg_type:ty),*) -> $return_type:ty ; $($rest:tt)*) =>
    {
        static_hooks!(@parse_fn_value ($($args)* ($($arg_type)*) ($return_type)) | ; $($rest)*);
    };

    (@parse_fn_args ($($args:tt)*)
                  | ($($arg_type:ty),*) $($rest:tt)*) =>
    {
        static_hooks!(@parse_fn_value ($($args)* ($($arg_type)*) (())) | $($rest)*);
    };

    // Step 6: parse argument types and return type
    // Requires explicit look-ahead to satisfy rule for tokens following ty fragment specifier
    (@parse_fn_value ($($args:tt)*)
                   | = $value:expr ; $($rest:tt)*) =>
    {
        static_hooks!(@parse_rest ($($args)* ($value)) | $($rest)*);
    };
    (@parse_fn_value ($($args:tt)*)
                   | ; $($rest:tt)*) =>
    {
        static_hooks!(@parse_rest ($($args)* (!)) | $($rest)*);
    };

    // Step 6: parse rest and recurse
    (@parse_rest ($($args:tt)*)
               | $($rest:tt)+) =>
    {
        static_hooks!(@make $($args)*);
        static_hooks!($($rest)*);
    };
    (@parse_rest ($($args:tt)*)
               | ) =>
    {
        static_hooks!(@make $($args)*);
    };

    // Step 7: parse rest and recurse
    (@make ($($var_attr:meta)*) ($($var_mod:tt)*) ($($hook_mod:tt)*) ($var_name:ident) ($target:expr)
           ($($fn_mod:tt)*) ($guard:tt) ($($arg_type:ty)*) ($return_type:ty) ($value:tt)) =>
    {
        static_hooks!(@gen_arg_names (make_hook_var)
                                     (
                                         ($($var_attr)*) ($($var_mod)*) ($($hook_mod)*) ($var_name) ($target)
                                         ($($fn_mod)*) ($guard) ($($arg_type)*) ($return_type) ($value)
                                         ($($fn_mod)* fn ($($arg_type),*) -> $return_type)
                                     )
                                     ($($arg_type)*));
    };

    (@make_hook_var ($($arg_name:ident)*) ($($var_attr:meta)*) ($($var_mod:tt)*) ($($hook_mod:tt)*)
                    ($var_name:ident) ($target:expr) ($($fn_mod:tt)*) ($guard:tt)
                    ($($arg_type:ty)*) ($return_type:ty) (!) ($fn_type:ty)) =>
    {
        static_hooks!(@make_item
            #[allow(non_upper_case_globals)]
            $(#[$var_attr])*
            $($var_mod)* static $var_name: $crate::StaticHook<$fn_type> = {
                static __DATA: $crate::AtomicInitCell<$crate::__StaticHookInner<$fn_type>> = $crate::AtomicInitCell::new();

                static_hooks!(@make_detour ($guard) ($var_name) ($($fn_mod)*) ($($arg_name)*) ($($arg_type)*) ($return_type));

                $crate::StaticHook::<$fn_type>::__new(&__DATA, $target, __detour)
            };
        );
    };

    (@make_hook_var ($($arg_name:ident)*) ($($var_attr:meta)*) ($($var_mod:tt)*) ($($hook_mod:tt)*)
                    ($var_name:ident) ($target:expr) ($($fn_mod:tt)*) ($guard:tt)
                    ($($arg_type:ty)*) ($return_type:ty) ($value:tt) ($fn_type:ty)) =>
    {
        static_hooks!(@make_item
            #[allow(non_upper_case_globals)]
            $(#[$var_attr])*
            $($var_mod)* static $var_name: $crate::StaticHookWithDefault<$fn_type> = {
                static __DATA: $crate::AtomicInitCell<$crate::__StaticHookInner<$fn_type>> = $crate::AtomicInitCell::new();

                static_hooks!(@make_detour ($guard) ($var_name) ($($fn_mod)*) ($($arg_name)*) ($($arg_type)*) ($return_type));

                $crate::StaticHookWithDefault::<$fn_type>::__new(
                    $crate::StaticHook::__new(&__DATA, $target, __detour),
                    &$value)
            };
        );
    };

    (@make_detour (GUARD) ($var_name:ident) ($($fn_mod:tt)*) ($($arg_name:ident)*) ($($arg_type:ty)*) ($return_type:ty)) => {
        static_hooks!(@make_item
            #[inline(never)]
            $($fn_mod)* fn __detour($($arg_name: $arg_type),*) -> $return_type {
                ::std::panic::catch_unwind(|| {
                    let &$crate::__StaticHookInner(_, ref closure) = __DATA.get().unwrap();
                    closure($($arg_name),*)
                }).unwrap_or_else(|payload| $crate::panic::__handle(module_path!(), stringify!($var_name), payload))
            }
        );
    };

    (@make_detour (NO_GUARD) ($var_name:ident) ($($fn_mod:tt)*) ($($arg_name:ident)*) ($($arg_type:ty)*) ($return_type:ty)) => {
        static_hooks!(@make_item
            #[inline(never)]
            $($fn_mod)* fn __detour($($arg_name: $arg_type),*) -> $return_type {
                let &$crate::__StaticHookInner(_, ref closure) = __DATA.get().unwrap();
                closure($($arg_name),*)
            }
        );
    };



    // Makes sure items are interpreted correctly
    (@make_item $item:item) => {
        $item
    };

    // Generates a list of idents for each given token and invokes the macro by the given label passing through arguments
    (@gen_arg_names ($label:ident) ($($args:tt)*) ($($token:tt)*)) => {
        static_hooks!(@gen_arg_names ($label)
                                     ($($args)*)
                                     (
                                         __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 __arg_14 __arg_15
                                         __arg_16 __arg_17 __arg_18 __arg_19 __arg_20 __arg_21 __arg_22 __arg_23
                                         __arg_24 __arg_25
                                     )
                                     ($($token)*)
                                     ());
    };
    (@gen_arg_names ($label:ident) ($($args:tt)*) ($hd_name:tt $($tl_name:tt)*) ($hd:tt $($tl:tt)*) ($($acc:tt)*) ) => {
        static_hooks!(@gen_arg_names ($label) ($($args)*) ($($tl_name)*) ($($tl)*) ($($acc)* $hd_name));
    };
    (@gen_arg_names ($label:ident) ($($args:tt)*) ($($name:tt)*) () ($($acc:tt)*)) => {
        static_hooks!(@$label ($($acc)*) $($args)*);
    };

    // Step 0
    ($($t:tt)+) => {
        static_hooks!(@parse_attr () | $($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) ($unsafe_type));
        impl_hookable!(@impl_core ($($nm : $ty),*) ($unsafe_type) ($unsafe_type));

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

        impl_hookable!(@impl_safe ($($nm : $ty),*) ($safe_type));
        impl_hookable!(@impl_unsafe ($($nm : $ty),*) ($unsafe_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),*> Hook<$fn_type> {
            #[doc(hidden)]
            #[allow(too_many_arguments)]
            pub fn call_real(&self, $($nm : $ty),*) -> Ret {
                (self.trampoline)($($nm),*)
            }
        }
    };

    (@impl_unsafe ($($nm:ident : $ty:ident),*) ($fn_type:ty)) => {
        unsafe impl<Ret: 'static, $($ty: 'static),*> UnsafeFunction for $fn_type {}

        impl<Ret: 'static, $($ty: 'static),*> Hook<$fn_type> {
            #[doc(hidden)]
            #[allow(too_many_arguments)]
            pub unsafe fn call_real(&self, $($nm : $ty),*) -> Ret {
                (self.trampoline)($($nm),*)
            }
        }
    };

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

            const ARITY: usize = impl_hookable!(@count ($($ty)*));

            unsafe fn from_ptr(ptr: FnPointer) -> Self {
                mem::transmute(ptr.to_raw())
            }

            fn to_ptr(&self) -> FnPointer {
                unsafe { FnPointer::from_raw(*self as *mut c_void) }
            }

            #[allow(useless_transmute)]
            fn to_unsafe(&self) -> Self::Unsafe {
                unsafe { mem::transmute(*self) }
            }
        }
    };

    (@count ()) => {
        0
    };
    (@count ($hd:tt $($tl:tt)*)) => {
        1 + impl_hookable!(@count ($($tl)*))
    };

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