-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathmocking.h
508 lines (436 loc) · 18 KB
/
mocking.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
505
506
507
508
#pragma once
#include <detours/detours.h>
#include <synchapi.h>
#include <wil/resource.h>
#include <wil/result_macros.h>
#include <wil/wistd_functional.h>
namespace witest
{
namespace details
{
__declspec(selectany) wil::srwlock s_detourLock;
template <typename FuncT>
inline HRESULT Register(FuncT** target, FuncT* detour) noexcept
{
RETURN_IF_WIN32_ERROR(::DetourTransactionBegin());
auto abortOnFailure = wil::scope_exit([&] {
LOG_IF_WIN32_ERROR(::DetourTransactionAbort());
});
RETURN_IF_WIN32_ERROR(::DetourUpdateThread(::GetCurrentThread()));
RETURN_IF_WIN32_ERROR(::DetourAttach(reinterpret_cast<void**>(target), reinterpret_cast<void*>(detour)));
RETURN_IF_WIN32_ERROR(::DetourTransactionCommit());
abortOnFailure.release();
return S_OK;
}
template <typename FuncT>
inline HRESULT Unregister(FuncT** target, FuncT* detour) noexcept
{
RETURN_IF_WIN32_ERROR(::DetourTransactionBegin());
auto abortOnFailure = wil::scope_exit([&] {
LOG_IF_WIN32_ERROR(::DetourTransactionAbort());
});
RETURN_IF_WIN32_ERROR(::DetourUpdateThread(::GetCurrentThread()));
RETURN_IF_WIN32_ERROR(::DetourDetach(reinterpret_cast<void**>(target), reinterpret_cast<void*>(detour)));
RETURN_IF_WIN32_ERROR(::DetourTransactionCommit());
abortOnFailure.release();
return S_OK;
}
template <typename DerivedT, typename FuncT>
struct detoured_global_function_base;
template <typename DerivedT, typename ReturnT, typename... ArgsT>
struct detoured_global_function_base<DerivedT, ReturnT(__stdcall*)(ArgsT...)>
{
protected:
using return_type = ReturnT;
using function_type = ReturnT(ArgsT...);
static constexpr bool is_noexcept = false;
static ReturnT __stdcall callback(ArgsT... args)
{
return DerivedT::invoke_callback(wistd::forward<ArgsT>(args)...);
}
};
template <typename DerivedT, typename ReturnT, typename... ArgsT>
struct detoured_global_function_base<DerivedT, ReturnT(__stdcall*)(ArgsT...) noexcept>
{
protected:
using return_type = ReturnT;
using function_type = ReturnT(ArgsT...);
static constexpr bool is_noexcept = true;
static ReturnT __stdcall callback(ArgsT... args) noexcept
{
return DerivedT::invoke_callback(wistd::forward<ArgsT>(args)...);
}
};
#if _M_IX86
template <typename DerivedT, typename ReturnT, typename... ArgsT>
struct detoured_global_function_base<DerivedT, ReturnT(__cdecl*)(ArgsT...)>
{
protected:
using return_type = ReturnT;
using function_type = ReturnT(ArgsT...);
static constexpr bool is_noexcept = false;
static ReturnT __cdecl callback(ArgsT... args)
{
return DerivedT::invoke_callback(wistd::forward<ArgsT>(args)...);
}
};
template <typename DerivedT, typename ReturnT, typename... ArgsT>
struct detoured_global_function_base<DerivedT, ReturnT(__cdecl*)(ArgsT...) noexcept>
{
protected:
using return_type = ReturnT;
using function_type = ReturnT(ArgsT...);
static constexpr bool is_noexcept = true;
static ReturnT __cdecl callback(ArgsT... args) noexcept
{
return DerivedT::invoke_callback(wistd::forward<ArgsT>(args)...);
}
};
#endif
template <typename DerivedT, typename FuncT>
struct detoured_thread_function_base;
template <typename DerivedT, typename ReturnT, typename... ArgsT>
struct detoured_thread_function_base<DerivedT, ReturnT(__stdcall*)(ArgsT...)>
{
protected:
using return_type = ReturnT;
using function_type = ReturnT(ArgsT...);
static constexpr bool is_noexcept = false;
static ReturnT __stdcall callback(ArgsT... args)
{
return DerivedT::invoke_callback(wistd::forward<ArgsT>(args)...);
}
};
template <typename DerivedT, typename ReturnT, typename... ArgsT>
struct detoured_thread_function_base<DerivedT, ReturnT(__stdcall*)(ArgsT...) noexcept>
{
protected:
using return_type = ReturnT;
using function_type = ReturnT(ArgsT...);
static constexpr bool is_noexcept = true;
static ReturnT __stdcall callback(ArgsT... args) noexcept
{
return DerivedT::invoke_callback(wistd::forward<ArgsT>(args)...);
}
};
#if _M_IX86
template <typename DerivedT, typename ReturnT, typename... ArgsT>
struct detoured_thread_function_base<DerivedT, ReturnT(__cdecl*)(ArgsT...)>
{
protected:
using return_type = ReturnT;
using function_type = ReturnT(ArgsT...);
static constexpr bool is_noexcept = false;
static ReturnT __cdecl callback(ArgsT... args)
{
return DerivedT::invoke_callback(wistd::forward<ArgsT>(args)...);
}
};
template <typename DerivedT, typename ReturnT, typename... ArgsT>
struct detoured_thread_function_base<DerivedT, ReturnT(__cdecl*)(ArgsT...) noexcept>
{
protected:
using return_type = ReturnT;
using function_type = ReturnT(ArgsT...);
static constexpr bool is_noexcept = true;
static ReturnT __cdecl callback(ArgsT... args) noexcept
{
return DerivedT::invoke_callback(wistd::forward<ArgsT>(args)...);
}
};
#endif
} // namespace details
//! An RAII type that manages the global detouring of a specific function.
//! This type is used to register a specific lambda (or function pointer) as the detour for a specific function that gets invoked
//! on all threads, not just the one that this object was created on.
template <auto TargetFn>
struct detoured_global_function : details::detoured_global_function_base<detoured_global_function<TargetFn>, decltype(TargetFn)>
{
private:
using base = details::detoured_global_function_base<detoured_global_function, decltype(TargetFn)>;
using return_type = typename base::return_type;
using function_type = typename base::function_type;
friend base;
public:
detoured_global_function() = default;
#ifdef WIL_ENABLE_EXCEPTIONS
template <typename Func>
detoured_global_function(Func&& func) noexcept(noexcept(reset(wistd::forward<Func>(func))))
{
THROW_IF_FAILED(reset(wistd::forward<Func>(func)));
}
#endif
detoured_global_function(const detoured_global_function&) = delete;
detoured_global_function& operator=(const detoured_global_function&) = delete;
// It's not safe to move construct/assign this type because it's not safe to move the function object while it's potentially
// being invoked by a different thread
~detoured_global_function()
{
WI_VERIFY_SUCCEEDED(reset());
}
HRESULT reset() noexcept
{
if (m_detour)
{
// The detour can be invoked from any thread, so we need to wait for any thread concurrently invoking the detour to
// complete before continuing with the reset
{
auto lock = details::s_detourLock.lock_exclusive();
m_removed = true;
while (m_entryCount > 0)
m_invokeComplete.wait(lock);
auto entryPtr = &s_globalInstance;
while (*entryPtr && (*entryPtr != this))
entryPtr = &(*entryPtr)->m_next;
// Failing this check likely means that there's either a memory corruption issue or an error in our logic
FAIL_FAST_IF_NULL(*entryPtr);
*entryPtr = m_next;
if (--s_refCount == 0)
{
RETURN_IF_FAILED(details::Unregister(&s_target, &base::callback));
}
}
m_detour = {};
}
return S_OK;
}
template <typename Func>
HRESULT reset(Func&& func) noexcept(
(wistd::is_reference_v<Func> || wistd::is_const_v<Func>) ? wistd::is_nothrow_copy_assignable_v<wistd::remove_reference_t<Func>>
: wistd::is_nothrow_move_assignable_v<Func>)
{
RETURN_IF_FAILED(reset());
// Once we release the lock below, we need to ensure that 'm_detour' is set because another thread may try and call into
// it. We can't do the assignment under the lock however, because the copy/move of 'func' may end up calling the function
// that we're detouring, leading to deadlock.
m_detour = wistd::forward<Func>(func);
auto resetOnExit = wil::scope_exit([&] {
m_detour = {};
});
{
auto lock = details::s_detourLock.lock_exclusive();
if (s_refCount == 0)
{
RETURN_IF_FAILED(details::Register(&s_target, &base::callback));
}
++s_refCount;
// 's_globalInstance' must be read/set under the lock
m_next = s_globalInstance;
s_globalInstance = this;
}
resetOnExit.release();
return S_OK;
}
private:
template <typename... ArgsT>
static return_type invoke_callback(ArgsT&&... args) noexcept(base::is_noexcept)
{
detoured_global_function* target = nullptr;
{
auto lock = details::s_detourLock.lock_exclusive();
if (s_invoking)
{
target = s_invoking->m_next;
}
else
{
target = s_globalInstance;
}
while (target && target->m_removed)
{
target = target->m_next;
}
if (target)
{
++target->m_entryCount;
}
}
if (target)
{
auto oldInvoking = s_invoking;
auto cleanup = wil::scope_exit([&] {
s_invoking = oldInvoking;
auto lock = details::s_detourLock.lock_exclusive();
if (--target->m_entryCount == 0)
{
target->m_invokeComplete.notify_all();
}
});
s_invoking = target;
return target->m_detour(wistd::forward<ArgsT>(args)...);
}
// NOTE: We specifically don't want to set 's_invoking' to null when forwarding to the implementation since we also
// use that to signal that the thread is not invoking any function.
// All registered functions have been called; forward to the implementation
return s_target(wistd::forward<ArgsT>(args)...);
}
detoured_global_function* m_next = nullptr; // Linked list of registrations; guarded by s_detourLock
wistd::function<function_type> m_detour;
int m_entryCount = 0; // Keep track of how many threads are invoking the detour; used to delay destruction
bool m_removed = false; // Marks the node as removed; not actually removed until all invocations complete
wil::condition_variable m_invokeComplete; // Used to delay destruction until all threads have finished invoking the detour
static inline detoured_global_function* s_globalInstance = nullptr; // Linked list head; guarded by s_detourLock
static inline thread_local detoured_global_function* s_invoking = nullptr; // Used for reentrancy
static inline auto s_target = TargetFn;
static inline int s_refCount = 0; // Guarded by details::s_detourLock
};
//! An RAII type that manages a thread-specific detouring of a specific function.
//! This type is used to register a specific lambda (or function pointer) as the detour for a specific function that only gets
//! invoked on the thread it was created on. This helps avoid issues such as the lambda being invoked on an unexpected thread or
//! having it get destroyed while being invoked on a different thread.
template <auto TargetFn>
struct detoured_thread_function : details::detoured_thread_function_base<detoured_thread_function<TargetFn>, decltype(TargetFn)>
{
private:
using base = details::detoured_thread_function_base<detoured_thread_function, decltype(TargetFn)>;
using return_type = typename base::return_type;
using function_type = typename base::function_type;
friend base;
public:
detoured_thread_function() = default;
#ifdef WIL_ENABLE_EXCEPTIONS
template <typename Func>
detoured_thread_function(Func&& func) noexcept(noexcept(reset(wistd::forward<Func>(func))))
{
THROW_IF_FAILED(reset(wistd::forward<Func>(func)));
}
#endif
detoured_thread_function(const detoured_thread_function&) = delete;
detoured_thread_function& operator=(const detoured_thread_function&) = delete;
detoured_thread_function(detoured_thread_function&& other) noexcept
{
swap(other);
}
detoured_thread_function& operator=(detoured_thread_function&& other) noexcept
{
swap(other);
return *this;
}
~detoured_thread_function()
{
WI_VERIFY_SUCCEEDED(reset());
}
void swap(detoured_thread_function& other) noexcept
{
wistd::swap_wil(m_detour, other.m_detour);
wistd::swap_wil(m_reentry, other.m_reentry);
// We need to swap positions in the linked list
detoured_thread_function** thisPos = nullptr;
detoured_thread_function** otherPos = nullptr;
for (auto ptr = &s_threadInstance; *ptr; ptr = &(*ptr)->m_next)
{
if (*ptr == this)
thisPos = ptr;
else if (*ptr == &other)
otherPos = ptr;
}
if (!thisPos)
{
// This not in the list; put it in other's position
if (otherPos)
{
*otherPos = this;
wistd::swap_wil(m_next, other.m_next);
}
}
else if (!otherPos)
{
// Other not in the list; put it in this's position
*thisPos = &other;
wistd::swap_wil(m_next, other.m_next);
}
// Otherwise, both are in the list
else if (m_next == &other)
{
// Special case; other needs to point to this now
WI_ASSERT(otherPos == &m_next);
*thisPos = &other;
m_next = wistd::exchange(other.m_next, this);
}
else if (other.m_next == this)
{
// Same as above, just swapped
WI_ASSERT(thisPos == &other.m_next);
*otherPos = this;
other.m_next = wistd::exchange(m_next, &other);
}
else
{
// General "easy" case; we can swap pointers
wistd::swap_wil(*thisPos, *otherPos);
wistd::swap_wil(m_next, other.m_next);
}
}
HRESULT reset() noexcept
{
if (m_detour)
{
m_detour = {};
detoured_thread_function** entryPtr = &s_threadInstance;
while (*entryPtr && (*entryPtr != this))
entryPtr = &(*entryPtr)->m_next;
// Faling this check would likely imply that this object is being destroyed on the wrong thread. No matter the reason,
// this should be considered a pretty fatal error
FAIL_FAST_IF_NULL(*entryPtr);
*entryPtr = m_next;
{
auto lock = details::s_detourLock.lock_exclusive();
if (--s_refCount == 0)
{
RETURN_IF_FAILED(details::Unregister(&s_target, &base::callback));
}
}
}
return S_OK;
}
template <typename Func>
HRESULT reset(Func&& func) noexcept(
(wistd::is_reference_v<Func> || wistd::is_const_v<Func>) ? wistd::is_nothrow_copy_assignable_v<wistd::remove_reference_t<Func>>
: wistd::is_nothrow_move_assignable_v<Func>)
{
RETURN_IF_FAILED(reset());
{
auto lock = details::s_detourLock.lock_exclusive();
if (s_refCount == 0)
{
RETURN_IF_FAILED(details::Register(&s_target, &base::callback));
}
++s_refCount;
}
m_detour = wistd::forward<Func>(func);
m_next = s_threadInstance;
s_threadInstance = this;
return S_OK;
}
private:
template <typename... ArgsT>
static return_type invoke_callback(ArgsT&&... args) noexcept(base::is_noexcept)
{
for (auto ptr = s_threadInstance; ptr; ptr = ptr->m_next)
{
if (!ptr->m_reentry)
{
return ptr->invoke(wistd::forward<ArgsT>(args)...);
}
}
// All registered functions have been called; forward to the implementation
return s_target(wistd::forward<ArgsT>(args)...);
}
template <typename... ArgsT>
return_type invoke(ArgsT&&... args) noexcept(base::is_noexcept)
{
WI_ASSERT(!m_reentry);
m_reentry = true;
auto resetOnExit = wil::scope_exit([&] { // No guarantee that 'return_type' is a movable type; NRVO is not guaranteed
m_reentry = false;
});
return m_detour(wistd::forward<ArgsT>(args)...);
}
detoured_thread_function* m_next = nullptr; // Linked list of registrations; support detouring more than once on same thread
wistd::function<function_type> m_detour;
bool m_reentry = false; // Used to handle forwarding to impl or the detour
static inline thread_local detoured_thread_function* s_threadInstance = nullptr; // Linked list head
static inline auto s_target = TargetFn;
static inline int s_refCount = 0; // Guarded by details::s_detourLock
};
} // namespace witest