Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/conflicts/module_watcher_win.h" | 5 #include "chrome/common/conflicts/module_watcher_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <tlhelp32.h> | 8 #include <tlhelp32.h> |
| 9 #include <winternl.h> // For UNICODE_STRING. | 9 #include <winternl.h> // For UNICODE_STRING. |
| 10 | 10 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 // Global pointer to the singleton ModuleWatcher, if one exists. Under | 89 // Global pointer to the singleton ModuleWatcher, if one exists. Under |
| 90 // |module_watcher_lock|. | 90 // |module_watcher_lock|. |
| 91 ModuleWatcher* g_module_watcher_instance = nullptr; | 91 ModuleWatcher* g_module_watcher_instance = nullptr; |
| 92 | 92 |
| 93 // Names of the DLL notification registration functions. These are exported by | 93 // Names of the DLL notification registration functions. These are exported by |
| 94 // ntdll. | 94 // ntdll. |
| 95 constexpr wchar_t kNtDll[] = L"ntdll.dll"; | 95 constexpr wchar_t kNtDll[] = L"ntdll.dll"; |
| 96 constexpr char kLdrRegisterDllNotification[] = "LdrRegisterDllNotification"; | 96 constexpr char kLdrRegisterDllNotification[] = "LdrRegisterDllNotification"; |
| 97 constexpr char kLdrUnregisterDllNotification[] = "LdrUnregisterDllNotification"; | 97 constexpr char kLdrUnregisterDllNotification[] = "LdrUnregisterDllNotification"; |
| 98 | 98 |
| 99 // Helper function for converting a UNICODE_STRING to a UTF8 std::string. | 99 // Helper function for converting a UNICODE_STRING to a FilePath. |
| 100 std::string ToString(const UNICODE_STRING* str) { | 100 base::FilePath ToFilePath(const UNICODE_STRING* str) { |
| 101 std::string s; | 101 return base::FilePath( |
| 102 base::WideToUTF8(str->Buffer, str->Length / sizeof(wchar_t), &s); | 102 base::string16(str->Buffer, str->Length / sizeof(wchar_t))); |
|
grt (UTC plus 2)
2016/12/20 11:11:35
nit: base::StringPiece16(str->Buffer, str->Length
chrisha
2016/12/20 19:46:24
Done.
| |
| 103 return s; | |
| 104 } | 103 } |
| 105 | 104 |
| 106 template <typename NotificationDataType> | 105 template <typename NotificationDataType> |
| 107 void OnModuleEvent(mojom::ModuleEventType event_type, | 106 void OnModuleEvent(const mojom::ModuleEventType& event_type, |
|
grt (UTC plus 2)
2016/12/20 11:11:35
passing an enum by constref?
chrisha
2016/12/20 19:46:24
Errr.... oops?
Done.
| |
| 108 const NotificationDataType& notification_data, | 107 const NotificationDataType& notification_data, |
| 109 const ModuleWatcher::OnModuleEventCallback& callback) { | 108 const ModuleWatcher::OnModuleEventCallback& callback) { |
| 110 mojom::ModuleEvent event; | 109 ModuleWatcher::ModuleEvent event( |
| 111 event.event_type = event_type; | 110 event_type, ToFilePath(notification_data.FullDllName), |
| 112 event.module_path = ToString(notification_data.FullDllName); | 111 notification_data.DllBase, notification_data.SizeOfImage); |
| 113 event.load_address = reinterpret_cast<uintptr_t>(notification_data.DllBase); | |
| 114 event.size = notification_data.SizeOfImage; | |
| 115 callback.Run(event); | 112 callback.Run(event); |
| 116 } | 113 } |
| 117 | 114 |
| 118 } // namespace | 115 } // namespace |
| 119 | 116 |
| 117 ModuleWatcher::ModuleEvent::ModuleEvent(mojom::ModuleEventType event_type, | |
|
grt (UTC plus 2)
2016/12/20 11:11:35
is this sufficiently trivial that it could be inli
chrisha
2016/12/20 19:46:24
Done.
| |
| 118 const base::FilePath& module_path, | |
| 119 void* module_load_address, | |
| 120 size_t module_size) | |
| 121 : event_type(event_type), | |
| 122 module_path(module_path), | |
| 123 module_load_address(module_load_address), | |
| 124 module_size(module_size) {} | |
| 125 | |
| 120 // static | 126 // static |
| 121 std::unique_ptr<ModuleWatcher> ModuleWatcher::Create( | 127 std::unique_ptr<ModuleWatcher> ModuleWatcher::Create( |
| 122 const OnModuleEventCallback& callback) { | 128 const OnModuleEventCallback& callback) { |
| 123 // If a ModuleWatcher already exists then bail out. | 129 // If a ModuleWatcher already exists then bail out. |
| 124 base::AutoLock lock(g_module_watcher_lock.Get()); | 130 base::AutoLock lock(g_module_watcher_lock.Get()); |
| 125 if (g_module_watcher_instance) | 131 if (g_module_watcher_instance) |
| 126 return nullptr; | 132 return nullptr; |
| 127 | 133 |
| 128 // This thread acquired the right to create a ModuleWatcher, so do so. | 134 // This thread acquired the right to create a ModuleWatcher, so do so. |
| 129 g_module_watcher_instance = new ModuleWatcher(callback); | 135 g_module_watcher_instance = new ModuleWatcher(callback); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 171 } | 177 } |
| 172 if (!snap.IsValid()) | 178 if (!snap.IsValid()) |
| 173 return; | 179 return; |
| 174 | 180 |
| 175 // Walk the module list. | 181 // Walk the module list. |
| 176 MODULEENTRY32 module = {sizeof(module)}; | 182 MODULEENTRY32 module = {sizeof(module)}; |
| 177 std::string path; | 183 std::string path; |
| 178 for (BOOL result = ::Module32First(snap.Get(), &module); result != FALSE; | 184 for (BOOL result = ::Module32First(snap.Get(), &module); result != FALSE; |
| 179 result = ::Module32Next(snap.Get(), &module)) { | 185 result = ::Module32Next(snap.Get(), &module)) { |
| 180 base::WideToUTF8(module.szExePath, ::wcslen(module.szExePath), &path); | 186 base::WideToUTF8(module.szExePath, ::wcslen(module.szExePath), &path); |
| 181 mojom::ModuleEvent event; | 187 ModuleEvent event(mojom::ModuleEventType::MODULE_ALREADY_LOADED, |
| 182 event.event_type = mojom::ModuleEventType::MODULE_ALREADY_LOADED; | 188 base::FilePath(base::UTF8ToWide(path)), |
|
grt (UTC plus 2)
2016/12/20 11:11:35
while i loves me a good conversion, how about usin
chrisha
2016/12/20 19:46:24
Oops, remnants of when this was being stored as as
| |
| 183 event.module_path = path; | 189 module.modBaseAddr, module.modBaseSize); |
| 184 event.load_address = reinterpret_cast<uintptr_t>(module.modBaseAddr); | |
| 185 event.size = module.modBaseSize; | |
| 186 callback_.Run(event); | 190 callback_.Run(event); |
|
grt (UTC plus 2)
2016/12/20 11:11:35
nit: i'd be inclined to inline the ModuleEvent, bu
chrisha
2016/12/20 19:46:24
I lean towards verbosity, as I personally find it
grt (UTC plus 2)
2016/12/20 21:09:53
Acknowledged.
| |
| 187 } | 191 } |
| 188 | 192 |
| 189 return; | 193 return; |
| 190 } | 194 } |
| 191 | 195 |
| 192 // static | 196 // static |
| 193 ModuleWatcher::OnModuleEventCallback ModuleWatcher::GetCallbackForContext( | 197 ModuleWatcher::OnModuleEventCallback ModuleWatcher::GetCallbackForContext( |
| 194 void* context) { | 198 void* context) { |
| 195 base::AutoLock lock(g_module_watcher_lock.Get()); | 199 base::AutoLock lock(g_module_watcher_lock.Get()); |
| 196 if (context != g_module_watcher_instance) | 200 if (context != g_module_watcher_instance) |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 218 notification_data->Unloaded, callback); | 222 notification_data->Unloaded, callback); |
| 219 break; | 223 break; |
| 220 | 224 |
| 221 default: | 225 default: |
| 222 // This is unexpected, but not a reason to crash. | 226 // This is unexpected, but not a reason to crash. |
| 223 NOTREACHED() << "Unknown LDR_DLL_NOTIFICATION_REASON: " | 227 NOTREACHED() << "Unknown LDR_DLL_NOTIFICATION_REASON: " |
| 224 << notification_reason; | 228 << notification_reason; |
| 225 } | 229 } |
| 226 } | 230 } |
| 227 | 231 |
| 228 ModuleWatcher::ModuleWatcher(const OnModuleEventCallback& callback) | 232 ModuleWatcher::ModuleWatcher(const OnModuleEventCallback& callback) |
|
grt (UTC plus 2)
2016/12/20 11:11:35
i just recently read this guidance on callbacks "P
chrisha
2016/12/20 19:46:24
Interesting. In the context of ModuleWatcher owner
grt (UTC plus 2)
2016/12/20 21:09:53
Are you saying that ModuleWatcher takes ownership
chrisha
2016/12/21 20:15:00
Err, no. I was walking about ownership of the argu
| |
| 229 : callback_(callback) { | 233 : callback_(callback) { |
| 230 RegisterDllNotificationCallback(); | 234 RegisterDllNotificationCallback(); |
| 231 EnumerateAlreadyLoadedModules(); | 235 EnumerateAlreadyLoadedModules(); |
| 232 } | 236 } |
| OLD | NEW |