Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: chrome/common/conflicts/module_watcher_win.cc

Issue 2576843002: [win] Create ModuleDatabase and ModuleEventSinkImpl. (Closed)
Patch Set: Fix small bug. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
11 #include <string> 11 #include <string>
12 #include <utility>
12 13
13 #include "base/lazy_instance.h" 14 #include "base/lazy_instance.h"
14 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/strings/string_piece.h"
15 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
16 #include "base/synchronization/lock.h" 18 #include "base/synchronization/lock.h"
17 #include "base/win/scoped_handle.h" 19 #include "base/win/scoped_handle.h"
18 20
19 // These structures and functions are documented in MSDN, see 21 // These structures and functions are documented in MSDN, see
20 // http://msdn.microsoft.com/en-us/library/gg547638(v=vs.85).aspx 22 // http://msdn.microsoft.com/en-us/library/gg547638(v=vs.85).aspx
21 // there are however no headers or import libraries available in the 23 // there are however no headers or import libraries available in the
22 // Platform SDK. They are declared outside of the anonymous namespace to 24 // Platform SDK. They are declared outside of the anonymous namespace to
23 // allow them to be forward declared in the header file. 25 // allow them to be forward declared in the header file.
24 enum { 26 enum {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 // Global pointer to the singleton ModuleWatcher, if one exists. Under 91 // Global pointer to the singleton ModuleWatcher, if one exists. Under
90 // |module_watcher_lock|. 92 // |module_watcher_lock|.
91 ModuleWatcher* g_module_watcher_instance = nullptr; 93 ModuleWatcher* g_module_watcher_instance = nullptr;
92 94
93 // Names of the DLL notification registration functions. These are exported by 95 // Names of the DLL notification registration functions. These are exported by
94 // ntdll. 96 // ntdll.
95 constexpr wchar_t kNtDll[] = L"ntdll.dll"; 97 constexpr wchar_t kNtDll[] = L"ntdll.dll";
96 constexpr char kLdrRegisterDllNotification[] = "LdrRegisterDllNotification"; 98 constexpr char kLdrRegisterDllNotification[] = "LdrRegisterDllNotification";
97 constexpr char kLdrUnregisterDllNotification[] = "LdrUnregisterDllNotification"; 99 constexpr char kLdrUnregisterDllNotification[] = "LdrUnregisterDllNotification";
98 100
99 // Helper function for converting a UNICODE_STRING to a UTF8 std::string. 101 // Helper function for converting a UNICODE_STRING to a FilePath.
100 std::string ToString(const UNICODE_STRING* str) { 102 base::FilePath ToFilePath(const UNICODE_STRING* str) {
101 std::string s; 103 return base::FilePath(
102 base::WideToUTF8(str->Buffer, str->Length / sizeof(wchar_t), &s); 104 base::StringPiece16(str->Buffer, str->Length / sizeof(wchar_t)));
103 return s;
104 } 105 }
105 106
106 template <typename NotificationDataType> 107 template <typename NotificationDataType>
107 void OnModuleEvent(mojom::ModuleEventType event_type, 108 void OnModuleEvent(mojom::ModuleEventType event_type,
108 const NotificationDataType& notification_data, 109 const NotificationDataType& notification_data,
109 const ModuleWatcher::OnModuleEventCallback& callback) { 110 const ModuleWatcher::OnModuleEventCallback& callback) {
110 mojom::ModuleEvent event; 111 ModuleWatcher::ModuleEvent event(
111 event.event_type = event_type; 112 event_type, ToFilePath(notification_data.FullDllName),
112 event.module_path = ToString(notification_data.FullDllName); 113 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); 114 callback.Run(event);
116 } 115 }
117 116
118 } // namespace 117 } // namespace
119 118
120 // static 119 // static
121 std::unique_ptr<ModuleWatcher> ModuleWatcher::Create( 120 std::unique_ptr<ModuleWatcher> ModuleWatcher::Create(
122 const OnModuleEventCallback& callback) { 121 OnModuleEventCallback callback) {
123 // If a ModuleWatcher already exists then bail out. 122 // If a ModuleWatcher already exists then bail out.
124 base::AutoLock lock(g_module_watcher_lock.Get()); 123 base::AutoLock lock(g_module_watcher_lock.Get());
125 if (g_module_watcher_instance) 124 if (g_module_watcher_instance)
126 return nullptr; 125 return nullptr;
127 126
128 // This thread acquired the right to create a ModuleWatcher, so do so. 127 // This thread acquired the right to create a ModuleWatcher, so do so.
129 g_module_watcher_instance = new ModuleWatcher(callback); 128 g_module_watcher_instance = new ModuleWatcher(std::move(callback));
130 return base::WrapUnique(g_module_watcher_instance); 129 return base::WrapUnique(g_module_watcher_instance);
131 } 130 }
132 131
133 ModuleWatcher::~ModuleWatcher() { 132 ModuleWatcher::~ModuleWatcher() {
134 // As soon as |g_module_watcher_instance| is null any dispatched callbacks 133 // As soon as |g_module_watcher_instance| is null any dispatched callbacks
135 // will be silently absorbed by LoaderNotificationCallback. 134 // will be silently absorbed by LoaderNotificationCallback.
136 base::AutoLock lock(g_module_watcher_lock.Get()); 135 base::AutoLock lock(g_module_watcher_lock.Get());
137 DCHECK_EQ(g_module_watcher_instance, this); 136 DCHECK_EQ(g_module_watcher_instance, this);
138 g_module_watcher_instance = nullptr; 137 g_module_watcher_instance = nullptr;
139 UnregisterDllNotificationCallback(); 138 UnregisterDllNotificationCallback();
(...skipping 27 matching lines...) Expand all
167 if (snap.IsValid()) 166 if (snap.IsValid())
168 break; 167 break;
169 if (::GetLastError() != ERROR_BAD_LENGTH) 168 if (::GetLastError() != ERROR_BAD_LENGTH)
170 return; 169 return;
171 } 170 }
172 if (!snap.IsValid()) 171 if (!snap.IsValid())
173 return; 172 return;
174 173
175 // Walk the module list. 174 // Walk the module list.
176 MODULEENTRY32 module = {sizeof(module)}; 175 MODULEENTRY32 module = {sizeof(module)};
177 std::string path;
178 for (BOOL result = ::Module32First(snap.Get(), &module); result != FALSE; 176 for (BOOL result = ::Module32First(snap.Get(), &module); result != FALSE;
179 result = ::Module32Next(snap.Get(), &module)) { 177 result = ::Module32Next(snap.Get(), &module)) {
180 base::WideToUTF8(module.szExePath, ::wcslen(module.szExePath), &path); 178 ModuleEvent event(mojom::ModuleEventType::MODULE_ALREADY_LOADED,
181 mojom::ModuleEvent event; 179 base::FilePath(module.szExePath), module.modBaseAddr,
182 event.event_type = mojom::ModuleEventType::MODULE_ALREADY_LOADED; 180 module.modBaseSize);
183 event.module_path = path;
184 event.load_address = reinterpret_cast<uintptr_t>(module.modBaseAddr);
185 event.size = module.modBaseSize;
186 callback_.Run(event); 181 callback_.Run(event);
187 } 182 }
188 183
189 return; 184 return;
190 } 185 }
191 186
192 // static 187 // static
193 ModuleWatcher::OnModuleEventCallback ModuleWatcher::GetCallbackForContext( 188 ModuleWatcher::OnModuleEventCallback ModuleWatcher::GetCallbackForContext(
194 void* context) { 189 void* context) {
195 base::AutoLock lock(g_module_watcher_lock.Get()); 190 base::AutoLock lock(g_module_watcher_lock.Get());
(...skipping 22 matching lines...) Expand all
218 notification_data->Unloaded, callback); 213 notification_data->Unloaded, callback);
219 break; 214 break;
220 215
221 default: 216 default:
222 // This is unexpected, but not a reason to crash. 217 // This is unexpected, but not a reason to crash.
223 NOTREACHED() << "Unknown LDR_DLL_NOTIFICATION_REASON: " 218 NOTREACHED() << "Unknown LDR_DLL_NOTIFICATION_REASON: "
224 << notification_reason; 219 << notification_reason;
225 } 220 }
226 } 221 }
227 222
228 ModuleWatcher::ModuleWatcher(const OnModuleEventCallback& callback) 223 ModuleWatcher::ModuleWatcher(OnModuleEventCallback callback)
229 : callback_(callback) { 224 : callback_(std::move(callback)) {
230 RegisterDllNotificationCallback(); 225 RegisterDllNotificationCallback();
231 EnumerateAlreadyLoadedModules(); 226 EnumerateAlreadyLoadedModules();
232 } 227 }
OLDNEW
« no previous file with comments | « chrome/common/conflicts/module_watcher_win.h ('k') | chrome/common/conflicts/module_watcher_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698