-
Notifications
You must be signed in to change notification settings - Fork 9
/
call_thunk.h
504 lines (407 loc) · 13.4 KB
/
call_thunk.h
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#ifndef _CALL_TRUNK_H_
#define _CALL_THUNK_H_
#include <stdlib.h>
#include <stdint.h>
#include <exception>
/*
call C++ member function as C functions's callback
*/
#if __cplusplus>=201103L || (defined(_MSC_VER) && _MSC_VER>=1800)
#ifndef USE_CPP11
#define USE_CPP11
#endif //USE_CPP11
#include <type_traits>
#include <tuple>
#endif //C++11
#if !(__cplusplus>=201103L || (defined(_MSC_VER) && _MSC_VER>=1900))
#define noexcept throw()
#endif //C++11
#if defined(_M_IX86) || defined(__i386__)
#if defined(_MSC_VER)
#define __FASTCALL__ __fastcall
#define __CDECL__ __cdecl
#define __STDCALL__ __stdcall
#define __THISCALL__ __thiscall
#elif defined(__GNUC__)
#define __FASTCALL__ __attribute__((__fastcall__))
#define __CDECL__ __attribute__((__cdecl__))
#define __STDCALL__ __attribute__((__stdcall__))
#define __THISCALL__ __attribute__((__thiscall__))
#endif
#elif defined(_M_X64) || defined(__x86_64__)
#define __FASTCALL__
#define __CDECL__
#define __STDCALL__
#define __THISCALL__
#endif
namespace call_thunk {
class bad_call : std::exception
{
public:
bad_call() noexcept { }
virtual ~bad_call() noexcept { }
virtual const char* what() const noexcept { return "call convention mismatch of function."; }
};
enum call_declare
{
cc_fastcall, //__FASTCALL__
#if defined(_M_IX86) || defined(__i386__)
cc_cdecl, //_cdecl
cc_stdcall, //__STDCALL__
cc_thiscall, //thiscall
default_caller = cc_cdecl,
#ifdef _WIN32
default_callee = cc_thiscall
#else
default_callee = cc_cdecl
#endif //_MSC_VER
#elif defined(_M_X64) || defined(__x86_64__)
default_caller = cc_fastcall,
default_callee = cc_fastcall
#endif
};
struct thunk_code;
struct argument_info
{
short _size;
bool _is_floating;
argument_info() : _size(0), _is_floating(false) { }
#ifdef USE_CPP11
template<typename T>
void init()
{
if (std::is_pointer<T>::value || std::is_reference<T>::value)
_size = sizeof(intptr_t);
else
_size = sizeof(T);
_is_floating = std::is_floating_point<T>::value;
}
#endif //C++11
bool as_integer() const;
bool as_floating() const;
short stack_size() const;
};
inline bool argument_info::as_integer() const
{
#if defined(_M_IX86) || defined(__i386__)
return stack_size() == sizeof(intptr_t);
#elif defined(_M_X64) || defined(__x86_64__)
#if defined(_WIN32)
return _size == sizeof(char) || _size == sizeof(short) ||
_size == sizeof(int) || _size == sizeof(intptr_t);
#else
return !_is_floating && stack_size() == sizeof(intptr_t);
#endif
#endif
}
inline bool argument_info::as_floating() const
{
return _is_floating;
}
inline short argument_info::stack_size() const
{
return (_size + sizeof(intptr_t) - 1) / sizeof(intptr_t) * sizeof(intptr_t);
}
class base_thunk
{
protected:
base_thunk() : _code(NULL), _thunk_size(0), _thunk(NULL) { }
~base_thunk() { destroy_code(); }
char* _code;
void init_code(call_declare caller, call_declare callee, size_t argc, const argument_info* arginfos=NULL) throw(bad_call);
void destroy_code();
void flush_cache();
void bind_impl(void* object, void* proc);
private:
size_t _thunk_size;
thunk_code* _thunk;
base_thunk(const base_thunk&);
base_thunk& operator=(const base_thunk&);
};
class unsafe_thunk : public base_thunk
{
public:
explicit unsafe_thunk(size_t argc, const argument_info* arginfos = NULL, call_declare caller = default_caller, call_declare callee = default_callee)
{
init_code(caller, callee, argc, arginfos);
}
template<typename T, typename PROC>
unsafe_thunk(T* object, PROC proc, size_t argc, const argument_info* arginfos = NULL, call_declare caller = default_caller, call_declare callee = default_callee)
{
init_code(caller, callee, argc, arginfos);
bind(object, proc);
}
template<typename T, typename PROC>
void bind(T& object, PROC proc)
{
bind_impl(static_cast<void*>(&object), *(void**)&proc);
flush_cache();
}
template<typename Callback>
operator Callback() const
{
return reinterpret_cast<Callback>(_code);
}
};
#ifdef USE_CPP11
template<typename Arg, typename... Others,
typename = typename std::enable_if<sizeof...(Others) != 0>::type>
inline size_t stat_param_bytes(size_t bytes)
{
bytes = stat_param_bytes<Arg>(bytes);
return stat_param_bytes<Others...>(bytes);
}
template<typename Arg>
inline size_t stat_param_bytes(size_t bytes)
{
if (std::is_pointer<Arg>::value || std::is_reference<Arg>::value)
bytes += sizeof(intptr_t);
else
bytes += (sizeof(Arg) + sizeof(intptr_t)-1) / sizeof(intptr_t) * sizeof(intptr_t);
return bytes;
}
template<typename CallerRet, typename... CallerArgs>
class thunk_impl : public base_thunk
{
public:
thunk_impl(call_declare caller = default_caller, call_declare callee = default_caller)
{
argument_info arginfos[sizeof...(CallerArgs)];
get_argument_infos(arginfos);
init_code(caller, callee, sizeof...(CallerArgs), arginfos);
}
~thunk_impl() { destroy_code(); }
private:
template<typename CalleeRet, typename... CalleeArgs>
struct check_call_impl : public std::integral_constant<bool,
std::is_same<CallerRet, CalleeRet>::value &&
std::is_same<std::tuple<CallerArgs...>, std::tuple<CalleeArgs...>>::value
> { };
template<typename>
struct check_call;
#define __CALL_THUNK_CHECK_CALL__(declare, ...) \
template<typename CalleeRet, typename CalleeClass, typename... CalleeArgs > \
struct check_call<CalleeRet(declare CalleeClass::*)(CalleeArgs...) __VA_ARGS__> \
: public check_call_impl<CalleeRet, CalleeArgs...> \
{ \
typedef CalleeClass class_type; \
typedef CalleeRet result_type; \
}
__CALL_THUNK_CHECK_CALL__(__FASTCALL__);
__CALL_THUNK_CHECK_CALL__(__FASTCALL__, const);
__CALL_THUNK_CHECK_CALL__(__FASTCALL__, volatile);
__CALL_THUNK_CHECK_CALL__(__FASTCALL__, const volatile);
#if defined(_M_IX86) || defined(__i386__)
__CALL_THUNK_CHECK_CALL__(__CDECL__);
__CALL_THUNK_CHECK_CALL__(__CDECL__, const);
__CALL_THUNK_CHECK_CALL__(__CDECL__, volatile);
__CALL_THUNK_CHECK_CALL__(__CDECL__, const volatile);
__CALL_THUNK_CHECK_CALL__(__STDCALL__);
__CALL_THUNK_CHECK_CALL__(__STDCALL__, const);
__CALL_THUNK_CHECK_CALL__(__STDCALL__, volatile);
__CALL_THUNK_CHECK_CALL__(__STDCALL__, const volatile);
__CALL_THUNK_CHECK_CALL__(__THISCALL__);
__CALL_THUNK_CHECK_CALL__(__THISCALL__, const);
__CALL_THUNK_CHECK_CALL__(__THISCALL__, volatile);
__CALL_THUNK_CHECK_CALL__(__THISCALL__, const volatile);
#endif //X86
template<size_t N>
inline void get_argument_infos(argument_info (&arginfos)[N])
{
check_argument_impl<0, N, CallerArgs...>(arginfos);
}
template<size_t I, size_t N, typename Arg, typename... Others, typename = typename std::enable_if< I != N-1 >::type>
inline void check_argument_impl(argument_info (&arginfos)[N])
{
argument_info& a = arginfos[I];
a.init<Arg>();
check_argument_impl<I + 1, N, Others...>(arginfos);
}
template<size_t I, size_t N, typename Arg>
inline void check_argument_impl(argument_info (&arginfos)[N])
{
argument_info& a = arginfos[I];
a.init<Arg>();
}
public:
template<typename CalleeClass, typename CalleeProc,
typename = typename std::enable_if<check_call<CalleeProc>::value>::type>
void bind(CalleeClass& object, CalleeProc proc)
{
bind_impl(static_cast<check_call<CalleeProc>::class_type*>(&object), *(void**)&proc);
flush_cache();
}
template<typename Callee,
typename = typename std::enable_if<check_call<decltype(&Callee::operator())>::value>::type>
void bind(Callee& callee)
{
auto proc = &Callee::operator();
bind_impl(&callee, *(void**)&proc);
flush_cache();
}
};
template<typename CallerRet>
class thunk_impl<CallerRet> : public base_thunk
{
public:
thunk_impl(call_declare caller = default_caller, call_declare callee = default_caller)
{
init_code(caller, callee, 0, NULL);
}
~thunk_impl() { destroy_code(); }
private:
template<typename CalleeRet>
struct check_call_impl : public std::integral_constant<bool,
std::is_same<CallerRet, CalleeRet>::value>
{ };
template<typename>
struct check_call;
#define __CALL_THUNK_CHECK_CALL__VOID(declare, ...) \
template<typename CalleeRet, typename CalleeClass> \
struct check_call<CalleeRet(declare CalleeClass::*)() __VA_ARGS__> \
: public check_call_impl<CalleeRet> \
{ \
typedef CalleeClass class_type; \
typedef CalleeRet result_type; \
}
__CALL_THUNK_CHECK_CALL__VOID(__FASTCALL__);
__CALL_THUNK_CHECK_CALL__VOID(__FASTCALL__, const);
__CALL_THUNK_CHECK_CALL__VOID(__FASTCALL__, volatile);
__CALL_THUNK_CHECK_CALL__VOID(__FASTCALL__, const volatile);
#if defined(_M_IX86) || defined(__i386__)
__CALL_THUNK_CHECK_CALL__VOID(__CDECL__);
__CALL_THUNK_CHECK_CALL__VOID(__CDECL__, const);
__CALL_THUNK_CHECK_CALL__VOID(__CDECL__, volatile);
__CALL_THUNK_CHECK_CALL__VOID(__CDECL__, const volatile);
__CALL_THUNK_CHECK_CALL__VOID(__STDCALL__);
__CALL_THUNK_CHECK_CALL__VOID(__STDCALL__, const);
__CALL_THUNK_CHECK_CALL__VOID(__STDCALL__, volatile);
__CALL_THUNK_CHECK_CALL__VOID(__STDCALL__, const volatile);
__CALL_THUNK_CHECK_CALL__VOID(__THISCALL__);
__CALL_THUNK_CHECK_CALL__VOID(__THISCALL__, const);
__CALL_THUNK_CHECK_CALL__VOID(__THISCALL__, volatile);
__CALL_THUNK_CHECK_CALL__VOID(__THISCALL__, const volatile);
#endif //X86
public:
template<typename CalleeClass, typename CalleeProc,
typename = typename std::enable_if<check_call<CalleeProc>::value>::type>
void bind(CalleeClass& object, CalleeProc proc)
{
bind_impl(static_cast<check_call<CalleeProc>::class_type*>(&object), *(void**)&proc);
flush_cache();
}
template<typename Callee,
typename = typename std::enable_if<check_call<decltype(&Callee::operator())>::value>::type>
void bind(Callee& callee)
{
auto proc = &Callee::operator();
bind_impl(&callee, *(void**)&proc);
flush_cache();
}
};
template<typename>
struct get_call_declare;
#define __CALL_THUNK_GET_CALL_DECLARE__(declare, value, ...) \
template<typename R, typename T, typename... Args> \
struct get_call_declare<R (declare T::*)(Args...) __VA_ARGS__> \
: std::integral_constant<call_declare, value> { }
__CALL_THUNK_GET_CALL_DECLARE__(__FASTCALL__, cc_fastcall);
__CALL_THUNK_GET_CALL_DECLARE__(__FASTCALL__, cc_fastcall, const);
__CALL_THUNK_GET_CALL_DECLARE__(__FASTCALL__, cc_fastcall, volatile);
__CALL_THUNK_GET_CALL_DECLARE__(__FASTCALL__, cc_fastcall, const volatile);
#if defined(_M_IX86) || defined(__i386__)
__CALL_THUNK_GET_CALL_DECLARE__(__CDECL__, cc_cdecl);
__CALL_THUNK_GET_CALL_DECLARE__(__CDECL__, cc_cdecl, const);
__CALL_THUNK_GET_CALL_DECLARE__(__CDECL__, cc_cdecl, volatile);
__CALL_THUNK_GET_CALL_DECLARE__(__CDECL__, cc_cdecl, const volatile);
__CALL_THUNK_GET_CALL_DECLARE__(__STDCALL__, cc_stdcall);
__CALL_THUNK_GET_CALL_DECLARE__(__STDCALL__, cc_stdcall, const);
__CALL_THUNK_GET_CALL_DECLARE__(__STDCALL__, cc_stdcall, volatile);
__CALL_THUNK_GET_CALL_DECLARE__(__STDCALL__, cc_stdcall, const volatile);
__CALL_THUNK_GET_CALL_DECLARE__(__THISCALL__, cc_thiscall);
__CALL_THUNK_GET_CALL_DECLARE__(__THISCALL__, cc_thiscall, const);
__CALL_THUNK_GET_CALL_DECLARE__(__THISCALL__, cc_thiscall, volatile);
__CALL_THUNK_GET_CALL_DECLARE__(__THISCALL__, cc_thiscall, const volatile);
#endif //X86
template<typename>
class thunk;
template<typename CallerRet, typename... CallerArgs>
class thunk<CallerRet(__FASTCALL__*)(CallerArgs...)> :
public thunk_impl<CallerRet, CallerArgs...>
{
public:
typedef thunk_impl<CallerRet, CallerArgs...> base_class;
typedef CallerRet(__FASTCALL__ *CallbackType)(CallerArgs...);
explicit thunk(call_declare callee = default_caller) : base_class(cc_fastcall, callee) { }
template<typename CalleeClass, typename CalleeProc>
thunk(CalleeClass& object, CalleeProc proc)
: base_class(cc_fastcall, get_call_declare<CalleeProc>::value)
{
this->bind(object, proc);
}
template<typename Callee>
thunk(Callee&& callee)
: base_class(cc_fastcall, get_call_declare<decltype(&Callee::operator())>::value)
{
this->bind(callee);
}
operator CallbackType() const
{
return reinterpret_cast<CallbackType>(this->_code);
}
};
#if defined(_M_IX86) || defined(__i386__)
template<typename CallerRet, typename... CallerArgs>
class thunk<CallerRet(__CDECL__*)(CallerArgs...)> :
public thunk_impl<CallerRet, CallerArgs...>
{
public:
typedef thunk_impl<CallerRet, CallerArgs...> base_class;
typedef CallerRet(__CDECL__ *CallbackType)(CallerArgs...);
explicit thunk(call_declare callee = default_caller) : base_class(cc_cdecl, callee) { }
template<typename CalleeClass, typename CalleeProc>
thunk(CalleeClass& object, CalleeProc proc)
: base_class(cc_cdecl, get_call_declare<CalleeProc>::value)
{
this->bind(object, proc);
}
template<typename Callee>
thunk(Callee&& callee)
: base_class(cc_fastcall, get_call_declare<decltype(&Callee::operator())>::value)
{
this->bind(callee);
}
operator CallbackType() const
{
return reinterpret_cast<CallbackType>(this->_code);
}
};
template<typename CallerRet, typename... CallerArgs>
class thunk<CallerRet(__STDCALL__*)(CallerArgs...)> :
public thunk_impl<CallerRet, CallerArgs...>
{
public:
typedef thunk_impl<CallerRet, CallerArgs...> base_class;
typedef CallerRet(__STDCALL__ *CallbackType)(CallerArgs...);
explicit thunk(call_declare callee = default_caller) : base_class(cc_stdcall, callee) { }
template<typename CalleeClass, typename CalleeProc>
thunk(CalleeClass& object, CalleeProc proc)
: base_class(cc_stdcall, get_call_declare<CalleeProc>::value)
{
this->bind(object, proc);
}
template<typename Callee>
thunk(Callee&& callee)
: base_class(cc_fastcall, get_call_declare<decltype(&Callee::operator())>::value)
{
this->bind(callee);
}
operator CallbackType() const
{
return reinterpret_cast<CallbackType>(this->_code);
}
};
#endif //X86
#endif //C++11
}
#endif //_CALL_TRUNK_H_