GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 75.0% 12 / 0 / 16
Functions: 100.0% 2 / 0 / 2
Branches: 50.0% 3 / 0 / 6

src/platform.hpp
Line Branch Exec Source
1 #ifndef DETOURMODKIT_PLATFORM_HPP
2 #define DETOURMODKIT_PLATFORM_HPP
3
4 #include <windows.h>
5
6 namespace DetourModKit::detail
7 {
8 /**
9 * @brief Checks if the current thread holds the Windows loader lock.
10 * @details Uses the PEB LoaderLock critical section at a well-known offset
11 * that has been stable across all Windows versions from XP through 11.
12 * Thread joins are unsafe while the loader lock is held (DllMain context).
13 */
14 579 inline bool is_loader_lock_held() noexcept
15 {
16 #ifdef _WIN64
17 579 auto *peb = reinterpret_cast<char *>(__readgsqword(0x60));
18 579 constexpr size_t kLoaderLockOffset = 0x110;
19 #else
20 auto *peb = reinterpret_cast<char *>(__readfsdword(0x30));
21 constexpr size_t kLoaderLockOffset = 0xA0;
22 #endif
23
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 579 times.
579 if (!peb)
24 return false;
25
26 579 auto *cs = *reinterpret_cast<PCRITICAL_SECTION *>(peb + kLoaderLockOffset);
27
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 579 times.
579 if (!cs)
28 return false;
29
30 1158 return cs->OwningThread ==
31 579 reinterpret_cast<HANDLE>(static_cast<uintptr_t>(GetCurrentThreadId()));
32 }
33
34 /**
35 * @brief Pins the current module to prevent unload while detached threads
36 * are still running.
37 * @details Uses GET_MODULE_HANDLE_EX_FLAG_PIN to permanently increment the
38 * module reference count. Safe to call from DllMain. This ensures
39 * that code pages and static data remain valid for detached threads
40 * during the FreeLibrary path.
41 * @return true if the module was successfully pinned, false on failure.
42 */
43 8 inline bool pin_current_module() noexcept
44 {
45 8 HMODULE pin = nullptr;
46
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 8 times.
8 if (!GetModuleHandleExW(
47 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
48 reinterpret_cast<LPCWSTR>(&pin_current_module),
49 &pin))
50 {
51 OutputDebugStringA("DetourModKit: pin_current_module failed; "
52 "detached threads may access unmapped code.\n");
53 return false;
54 }
55 8 return true;
56 }
57
58 } // namespace DetourModKit::detail
59
60 #endif // DETOURMODKIT_PLATFORM_HPP
61