/
Hook.h
306 lines (269 loc) · 17.9 KB
/
Hook.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
#pragma once
#include <atomic>
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
#include "ll/api/base/Concepts.h" // IWYU pragma: keep
#include "ll/api/base/FixedString.h"
#include "ll/api/base/Macro.h"
#include "ll/api/memory/Memory.h"
#include "ll/api/reflection/TypeName.h"
#include "ll/api/thread/GlobalThreadPauser.h"
namespace ll::memory {
template <class T>
struct IsConstMemberFun : std::false_type {};
template <class T, class Ret, class... Args>
struct IsConstMemberFun<Ret (T::*)(Args...) const> : std::true_type {};
template <class T>
constexpr bool IsConstMemberFunV = IsConstMemberFun<T>::value;
template <class T>
struct AddConstAtMemberFun {
using type = T;
};
template <class T, class Ret, class... Args>
struct AddConstAtMemberFun<Ret (T::*)(Args...)> {
using type = Ret (T::*)(Args...) const;
};
template <class T>
using AddConstAtMemberFunT = typename AddConstAtMemberFun<T>::type;
/**
* @brief Hook priority enum.
* @details The higher priority, the hook will be executed earlier
*/
enum class HookPriority : int {
Highest = 0,
High = 100,
Normal = 200,
Low = 300,
Lowest = 400,
};
LLAPI int
hook(FuncPtr target, FuncPtr detour, FuncPtr* originalFunc, HookPriority priority, bool suspendThreads = true);
LLAPI bool unhook(FuncPtr target, FuncPtr detour, bool suspendThreads = true);
/**
* @brief Get the pointer of a function by identifier.
*
* @param identifier symbol or signature
* @return FuncPtr
*/
LLNDAPI FuncPtr resolveIdentifier(std::string_view identifier, bool disableErrorOutput = false);
template <class T>
concept FuncPtrType = std::is_function_v<std::remove_pointer_t<T>> || std::is_member_function_pointer_v<T>;
template <FuncPtrType T>
constexpr FuncPtr resolveIdentifier(T identifier) {
return toFuncPtr(identifier);
}
// redirect to resolveIdentifier(std::string_view)
template <class T>
constexpr FuncPtr resolveIdentifier(std::string_view identifier) {
return resolveIdentifier(identifier);
}
template <class T>
constexpr FuncPtr resolveIdentifier(uintptr_t address) {
return toFuncPtr(address);
}
template <class T>
constexpr FuncPtr resolveIdentifier(void* address) {
return address;
}
template <class, FixedString>
consteval bool virtualDetector() noexcept {
return false;
}
template <class T, T f>
consteval bool virtualDetector() noexcept {
return reflection::getRawName<f>().find("::`vcall'{") != std::string::npos;
}
template <class... Ts>
class HookRegistrar {
public:
static void hook() {
thread::GlobalThreadPauser pauser;
(((++Ts::_AutoHookCount == 1) ? Ts::hook(false) : 0), ...);
}
static void unhook() {
thread::GlobalThreadPauser pauser;
(((--Ts::_AutoHookCount == 0) ? Ts::unhook(false) : 0), ...);
}
HookRegistrar() noexcept { hook(); }
~HookRegistrar() noexcept { unhook(); }
HookRegistrar(HookRegistrar const&) noexcept { ((++Ts::_AutoHookCount), ...); }
HookRegistrar& operator=(HookRegistrar const& other) noexcept {
if (this != std::addressof(other)) {
((++Ts::_AutoHookCount), ...);
}
return *this;
}
HookRegistrar(HookRegistrar&&) noexcept = default;
HookRegistrar& operator=(HookRegistrar&&) noexcept = default;
};
struct __declspec(empty_bases) Hook {};
} // namespace ll::memory
#define LL_HOOK_IMPL(REGISTER, FUNC_PTR, STATIC, CALL, DEF_TYPE, TYPE, PRIORITY, IDENTIFIER, RET_TYPE, ...) \
struct DEF_TYPE : public TYPE { \
using _FuncPtr = ::ll::memory::FuncPtr; \
using HookPriority = ::ll::memory::HookPriority; \
using _RawFuncType = RET_TYPE FUNC_PTR(__VA_ARGS__); \
using _RawConstFuncType = ::ll::memory::AddConstAtMemberFunT<_RawFuncType>; \
\
template <class T> \
struct _ConstDetector { \
[[maybe_unused]] static constexpr bool value = false; \
explicit constexpr _ConstDetector(T) {} \
}; \
template <class T> \
[[maybe_unused]] _ConstDetector(T) -> _ConstDetector<T>; \
[[maybe_unused]] _ConstDetector(_RawFuncType) -> _ConstDetector<_RawFuncType>; \
template <> \
struct _ConstDetector<_RawConstFuncType> { \
[[maybe_unused]] static constexpr bool value = true; \
explicit constexpr _ConstDetector(_RawConstFuncType) {} \
}; \
template <class T = _RawFuncType, std::enable_if_t<std::is_member_function_pointer_v<T>, int> = 0> \
[[maybe_unused]] _ConstDetector(_RawConstFuncType) -> _ConstDetector<_RawConstFuncType>; \
\
static constexpr bool _IsConstMemberFunction = decltype(_ConstDetector{IDENTIFIER})::value; \
\
using _OriginFuncType = std::conditional_t<_IsConstMemberFunction, _RawConstFuncType, _RawFuncType>; \
\
inline static _FuncPtr _HookTarget{}; \
inline static _OriginFuncType _OriginalFunc{}; \
\
inline static std::atomic_uint _AutoHookCount{}; \
\
template <class T> \
static consteval void detector() { \
if constexpr (requires { ::ll::memory::virtualDetector<T, IDENTIFIER>(); }) { \
if constexpr (::ll::memory::virtualDetector<T, IDENTIFIER>()) { \
static_assert( \
::ll::concepts::always_false<T>, \
#IDENTIFIER " is a virtual function, you need use prefix $ workaround to hook it." \
); \
} \
} \
} \
\
template <class... Args> \
STATIC RET_TYPE origin(Args&&... params) { \
return CALL(std::forward<Args>(params)...); \
} \
\
STATIC RET_TYPE detour(__VA_ARGS__); \
\
static int hook(bool suspendThreads = true) { \
detector<_OriginFuncType>(); \
_HookTarget = ::ll::memory::resolveIdentifier<_OriginFuncType>(IDENTIFIER); \
if (_HookTarget == nullptr) { \
return -1; \
} \
return ::ll::memory::hook( \
_HookTarget, \
::ll::memory::toFuncPtr(&DEF_TYPE::detour), \
reinterpret_cast<_FuncPtr*>(&_OriginalFunc), \
PRIORITY, \
suspendThreads \
); \
} \
\
static bool unhook(bool suspendThreads = true) { \
return ::ll::memory::unhook(_HookTarget, ::ll::memory::toFuncPtr(&DEF_TYPE::detour), suspendThreads); \
} \
}; \
REGISTER; \
RET_TYPE DEF_TYPE::detour(__VA_ARGS__)
#define LL_AUTO_REG_HOOK_IMPL(FUNC_PTR, STATIC, CALL, DEF_TYPE, ...) \
LL_VA_EXPAND(LL_HOOK_IMPL( \
inline ::ll::memory::HookRegistrar<DEF_TYPE> DEF_TYPE##AutoRegister, \
FUNC_PTR, \
STATIC, \
CALL, \
DEF_TYPE, \
__VA_ARGS__ \
))
#define LL_MANUAL_REG_HOOK_IMPL(...) LL_VA_EXPAND(LL_HOOK_IMPL(, __VA_ARGS__))
#define LL_STATIC_HOOK_IMPL(...) LL_VA_EXPAND(LL_MANUAL_REG_HOOK_IMPL((*), static, _OriginalFunc, __VA_ARGS__))
#define LL_AUTO_STATIC_HOOK_IMPL(...) LL_VA_EXPAND(LL_AUTO_REG_HOOK_IMPL((*), static, _OriginalFunc, __VA_ARGS__))
#define LL_INSTANCE_HOOK_IMPL(DEF_TYPE, TYPE, ...) \
LL_VA_EXPAND(LL_MANUAL_REG_HOOK_IMPL((TYPE::*), , (this->*_OriginalFunc), DEF_TYPE, TYPE, __VA_ARGS__))
#define LL_AUTO_INSTANCE_HOOK_IMPL(DEF_TYPE, TYPE, ...) \
LL_VA_EXPAND(LL_AUTO_REG_HOOK_IMPL((TYPE::*), , (this->*_OriginalFunc), DEF_TYPE, TYPE, __VA_ARGS__))
/**
* @brief Register a hook for a typed static function.
* @param DEF_TYPE The name of the hook definition.
* @param PRIORITY ll::memory::HookPriority The priority of the hook.
* @param TYPE The type which the function belongs to.
* @param IDENTIFIER The identifier of the hook. It can be a function pointer, symbol, address or a signature.
* @param RET_TYPE The return type of the hook.
* @param ... The parameters of the hook.
*
* @note register or unregister by calling DEF_TYPE::hook() and DEF_TYPE::unhook().
*/
#define LL_TYPE_STATIC_HOOK(DEF_TYPE, PRIORITY, TYPE, IDENTIFIER, RET_TYPE, ...) \
LL_VA_EXPAND(LL_STATIC_HOOK_IMPL(DEF_TYPE, TYPE, PRIORITY, IDENTIFIER, RET_TYPE, __VA_ARGS__))
/**
* @brief Register a hook for a static function.
* @param DEF_TYPE The name of the hook definition.
* @param PRIORITY ll::memory::HookPriority The priority of the hook.
* @param IDENTIFIER The identifier of the hook. It can be a function pointer, symbol, address or a signature.
* @param RET_TYPE The return type of the hook.
* @param ... The parameters of the hook.
*
* @note register or unregister by calling DEF_TYPE::hook() and DEF_TYPE::unhook().
*/
#define LL_STATIC_HOOK(DEF_TYPE, PRIORITY, IDENTIFIER, RET_TYPE, ...) \
LL_VA_EXPAND(LL_STATIC_HOOK_IMPL(DEF_TYPE, ::ll::memory::Hook, PRIORITY, IDENTIFIER, RET_TYPE, __VA_ARGS__))
/**
* @brief Register a hook for a typed static function.
* @details The hook will be automatically registered and unregistered.
* @see LL_TYPE_STATIC_HOOK for usage.
*/
#define LL_AUTO_TYPE_STATIC_HOOK(DEF_TYPE, PRIORITY, TYPE, IDENTIFIER, RET_TYPE, ...) \
LL_VA_EXPAND(LL_AUTO_STATIC_HOOK_IMPL(DEF_TYPE, TYPE, PRIORITY, IDENTIFIER, RET_TYPE, __VA_ARGS__))
/**
* @brief Register a hook for a static function.
* @details The hook will be automatically registered and unregistered.
* @see LL_STATIC_HOOK for usage.
*/
#define LL_AUTO_STATIC_HOOK(DEF_TYPE, PRIORITY, IDENTIFIER, RET_TYPE, ...) \
LL_VA_EXPAND(LL_AUTO_STATIC_HOOK_IMPL(DEF_TYPE, ::ll::memory::Hook, PRIORITY, IDENTIFIER, RET_TYPE, __VA_ARGS__))
/**
* @brief Register a hook for a typed instance function.
* @param DEF_TYPE The name of the hook definition.
* @param PRIORITY ll::memory::HookPriority The priority of the hook.
* @param TYPE The type which the function belongs to.
* @param IDENTIFIER The identifier of the hook. It can be a function pointer, symbol, address or a signature.
* @param RET_TYPE The return type of the hook.
* @param ... The parameters of the hook.
*
* @note register or unregister by calling DEF_TYPE::hook() and DEF_TYPE::unhook().
*/
#define LL_TYPE_INSTANCE_HOOK(DEF_TYPE, PRIORITY, TYPE, IDENTIFIER, RET_TYPE, ...) \
LL_VA_EXPAND(LL_INSTANCE_HOOK_IMPL(DEF_TYPE, TYPE, PRIORITY, IDENTIFIER, RET_TYPE, __VA_ARGS__))
/**
* @brief Register a hook for a instance function.
* @param DEF_TYPE The name of the hook definition.
* @param PRIORITY ll::memory::HookPriority The priority of the hook.
* @param IDENTIFIER The identifier of the hook. It can be a function pointer, symbol, address or a signature.
* @param RET_TYPE The return type of the hook.
* @param ... The parameters of the hook.
*
* @note register or unregister by calling DEF_TYPE::hook() and DEF_TYPE::unhook().
*/
#define LL_INSTANCE_HOOK(DEF_TYPE, PRIORITY, IDENTIFIER, RET_TYPE, ...) \
LL_VA_EXPAND(LL_INSTANCE_HOOK_IMPL(DEF_TYPE, ::ll::memory::Hook, PRIORITY, IDENTIFIER, RET_TYPE, __VA_ARGS__))
/**
* @brief Register a hook for a typed instance function.
* @details The hook will be automatically registered and unregistered.
* @see LL_TYPE_INSTANCE_HOOK for usage.
*/
#define LL_AUTO_TYPE_INSTANCE_HOOK(DEF_TYPE, PRIORITY, TYPE, IDENTIFIER, RET_TYPE, ...) \
LL_VA_EXPAND(LL_AUTO_INSTANCE_HOOK_IMPL(DEF_TYPE, TYPE, PRIORITY, IDENTIFIER, RET_TYPE, __VA_ARGS__))
/**
* @brief Register a hook for a instance function.
* @details The hook will be automatically registered and unregistered.
* @see LL_INSTANCE_HOOK for usage.
*/
#define LL_AUTO_INSTANCE_HOOK(DEF_TYPE, PRIORITY, IDENTIFIER, RET_TYPE, ...) \
LL_VA_EXPAND(LL_AUTO_INSTANCE_HOOK_IMPL(DEF_TYPE, ::ll::memory::Hook, PRIORITY, IDENTIFIER, RET_TYPE, __VA_ARGS__))