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