LdrWatcher.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "Common/LdrWatcher.h"
  4. #include <Windows.h>
  5. #include <TlHelp32.h>
  6. #include <string>
  7. #include <winternl.h>
  8. typedef struct _LDR_DLL_LOADED_NOTIFICATION_DATA
  9. {
  10. ULONG Flags; // Reserved.
  11. PCUNICODE_STRING FullDllName; // The full path name of the DLL module.
  12. PCUNICODE_STRING BaseDllName; // The base file name of the DLL module.
  13. PVOID DllBase; // A pointer to the base address for the DLL in memory.
  14. ULONG SizeOfImage; // The size of the DLL image, in bytes.
  15. } LDR_DLL_LOADED_NOTIFICATION_DATA, *PLDR_DLL_LOADED_NOTIFICATION_DATA;
  16. typedef struct _LDR_DLL_UNLOADED_NOTIFICATION_DATA
  17. {
  18. ULONG Flags; // Reserved.
  19. PCUNICODE_STRING FullDllName; // The full path name of the DLL module.
  20. PCUNICODE_STRING BaseDllName; // The base file name of the DLL module.
  21. PVOID DllBase; // A pointer to the base address for the DLL in memory.
  22. ULONG SizeOfImage; // The size of the DLL image, in bytes.
  23. } LDR_DLL_UNLOADED_NOTIFICATION_DATA, *PLDR_DLL_UNLOADED_NOTIFICATION_DATA;
  24. typedef union _LDR_DLL_NOTIFICATION_DATA
  25. {
  26. LDR_DLL_LOADED_NOTIFICATION_DATA Loaded;
  27. LDR_DLL_UNLOADED_NOTIFICATION_DATA Unloaded;
  28. } LDR_DLL_NOTIFICATION_DATA, *PLDR_DLL_NOTIFICATION_DATA;
  29. typedef const LDR_DLL_NOTIFICATION_DATA* PCLDR_DLL_NOTIFICATION_DATA;
  30. #define LDR_DLL_NOTIFICATION_REASON_LOADED (1)
  31. #define LDR_DLL_NOTIFICATION_REASON_UNLOADED (2)
  32. typedef VOID NTAPI LDR_DLL_NOTIFICATION_FUNCTION(_In_ ULONG NotificationReason,
  33. _In_ PCLDR_DLL_NOTIFICATION_DATA NotificationData,
  34. _In_opt_ PVOID Context);
  35. typedef LDR_DLL_NOTIFICATION_FUNCTION* PLDR_DLL_NOTIFICATION_FUNCTION;
  36. typedef NTSTATUS(NTAPI* LdrRegisterDllNotification_t)(
  37. _In_ ULONG Flags, _In_ PLDR_DLL_NOTIFICATION_FUNCTION NotificationFunction,
  38. _In_opt_ PVOID Context, _Out_ PVOID* Cookie);
  39. typedef NTSTATUS(NTAPI* LdrUnregisterDllNotification_t)(_In_ PVOID Cookie);
  40. static void LdrObserverRun(const LdrObserver& observer, PCUNICODE_STRING module_name,
  41. uintptr_t base_address)
  42. {
  43. for (auto& needle : observer.module_names)
  44. {
  45. // Like RtlCompareUnicodeString, but saves dynamically resolving it.
  46. // NOTE: Does not compare null terminator.
  47. auto compare_length = module_name->Length / sizeof(wchar_t);
  48. if (!_wcsnicmp(needle.c_str(), module_name->Buffer, compare_length))
  49. observer.action({needle, base_address});
  50. }
  51. }
  52. static VOID DllNotificationCallback(ULONG NotificationReason,
  53. PCLDR_DLL_NOTIFICATION_DATA NotificationData, PVOID Context)
  54. {
  55. if (NotificationReason != LDR_DLL_NOTIFICATION_REASON_LOADED)
  56. return;
  57. auto& data = NotificationData->Loaded;
  58. auto observer = static_cast<const LdrObserver*>(Context);
  59. LdrObserverRun(*observer, data.BaseDllName, reinterpret_cast<uintptr_t>(data.DllBase));
  60. }
  61. // This only works on Vista+. On lower platforms, it will be a no-op.
  62. class LdrDllNotifier
  63. {
  64. public:
  65. static LdrDllNotifier& GetInstance()
  66. {
  67. static LdrDllNotifier notifier;
  68. return notifier;
  69. }
  70. void Install(LdrObserver* observer);
  71. void Uninstall(LdrObserver* observer);
  72. private:
  73. LdrDllNotifier();
  74. bool Init();
  75. LdrRegisterDllNotification_t LdrRegisterDllNotification{};
  76. LdrUnregisterDllNotification_t LdrUnregisterDllNotification{};
  77. bool initialized{};
  78. };
  79. LdrDllNotifier::LdrDllNotifier()
  80. {
  81. initialized = Init();
  82. }
  83. bool LdrDllNotifier::Init()
  84. {
  85. auto ntdll = GetModuleHandleW(L"ntdll");
  86. if (!ntdll)
  87. return false;
  88. LdrRegisterDllNotification = reinterpret_cast<decltype(LdrRegisterDllNotification)>(
  89. GetProcAddress(ntdll, "LdrRegisterDllNotification"));
  90. if (!LdrRegisterDllNotification)
  91. return false;
  92. LdrUnregisterDllNotification = reinterpret_cast<decltype(LdrUnregisterDllNotification)>(
  93. GetProcAddress(ntdll, "LdrUnregisterDllNotification"));
  94. if (!LdrUnregisterDllNotification)
  95. return false;
  96. return true;
  97. }
  98. void LdrDllNotifier::Install(LdrObserver* observer)
  99. {
  100. if (!initialized)
  101. return;
  102. void* cookie{};
  103. if (!NT_SUCCESS(LdrRegisterDllNotification(0, DllNotificationCallback,
  104. static_cast<PVOID>(observer), &cookie)))
  105. cookie = {};
  106. observer->cookie = cookie;
  107. return;
  108. }
  109. void LdrDllNotifier::Uninstall(LdrObserver* observer)
  110. {
  111. if (!initialized)
  112. return;
  113. LdrUnregisterDllNotification(observer->cookie);
  114. observer->cookie = {};
  115. return;
  116. }
  117. LdrWatcher::~LdrWatcher()
  118. {
  119. UninstallAll();
  120. }
  121. // Needed for RtlInitUnicodeString
  122. #pragma comment(lib, "ntdll")
  123. bool LdrWatcher::InjectCurrentModules(const LdrObserver& observer)
  124. {
  125. // Use TlHelp32 instead of psapi functions to reduce dolphin's dependency on psapi
  126. // (revisit this when Win7 support is dropped).
  127. HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
  128. if (snapshot == INVALID_HANDLE_VALUE)
  129. return false;
  130. MODULEENTRY32 entry;
  131. entry.dwSize = sizeof(entry);
  132. for (BOOL rv = Module32First(snapshot, &entry); rv == TRUE; rv = Module32Next(snapshot, &entry))
  133. {
  134. UNICODE_STRING module_name;
  135. RtlInitUnicodeString(&module_name, entry.szModule);
  136. LdrObserverRun(observer, &module_name, reinterpret_cast<uintptr_t>(entry.modBaseAddr));
  137. }
  138. CloseHandle(snapshot);
  139. return true;
  140. }
  141. void LdrWatcher::Install(const LdrObserver& observer)
  142. {
  143. observers.emplace_back(observer);
  144. auto& new_observer = observers.back();
  145. // Register for notifications before looking at the list of current modules.
  146. // This ensures none are missed, but there is a tiny chance some will be seen twice.
  147. LdrDllNotifier::GetInstance().Install(&new_observer);
  148. InjectCurrentModules(new_observer);
  149. }
  150. void LdrWatcher::UninstallAll()
  151. {
  152. for (auto& observer : observers)
  153. LdrDllNotifier::GetInstance().Uninstall(&observer);
  154. observers.clear();
  155. }