Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/conflicts/module_database_win.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 // Document the assumptions made on the ProcessType enum in order to convert | |
| 14 // them to bits. | |
| 15 static_assert(content::PROCESS_TYPE_UNKNOWN == 1, | |
| 16 "assumes unknown process type has value 1"); | |
| 17 static_assert(content::PROCESS_TYPE_BROWSER == 2, | |
| 18 "assumes browser process type has value 2"); | |
| 19 constexpr uint32_t kMinProcessType = content::PROCESS_TYPE_BROWSER; | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 ModuleDatabase::ModuleDatabase( | |
| 24 scoped_refptr<base::SequencedTaskRunner> task_runner) | |
| 25 : task_runner_(std::move(task_runner)), weak_ptr_factory_(this) {} | |
| 26 | |
| 27 ModuleDatabase::~ModuleDatabase() = default; | |
| 28 | |
| 29 void ModuleDatabase::OnProcessStarted(uint32_t process_id, | |
| 30 content::ProcessType process_type) { | |
| 31 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 32 CreateProcessInfo(process_id, process_type); | |
| 33 } | |
| 34 | |
| 35 void ModuleDatabase::OnModuleEvent(uint32_t process_id, | |
| 36 const ModuleWatcher::ModuleEvent& event) { | |
| 37 // Messages can arrive from any thread (UI thread for calls over IPC, and | |
| 38 // anywhere at all for calls from ModuleWatcher), so bounce if necessary. | |
| 39 if (!task_runner_->RunsTasksOnCurrentThread()) { | |
| 40 task_runner_->PostTask(FROM_HERE, base::Bind(&ModuleDatabase::OnModuleEvent, | |
| 41 weak_ptr_factory_.GetWeakPtr(), | |
| 42 process_id, event)); | |
| 43 return; | |
| 44 } | |
| 45 | |
| 46 // In theory this should always succeed. However, it is possible for a client | |
| 47 // to misbehave and send out-of-order messages. It is easy to be tolerant of | |
| 48 // this by simply not updating the process info in this case. It's not worth | |
| 49 // crashing if this data is slightly out of sync as this is purely | |
| 50 // informational. | |
| 51 auto* process_info = GetProcessInfo(process_id); | |
| 52 if (!process_info) | |
| 53 return; | |
| 54 | |
| 55 auto* module_info = FindOrCreateModuleInfo(event.module_path); | |
| 56 | |
| 57 // Update the list of process types that this module has been seen in. | |
| 58 module_info->process_types |= ProcessTypeToBit(process_info->process_type); | |
| 59 | |
| 60 uintptr_t load_address = | |
| 61 reinterpret_cast<uintptr_t>(event.module_load_address); | |
| 62 | |
| 63 // Update the module lists for this process. | |
| 64 switch (event.event_type) { | |
| 65 case mojom::ModuleEventType::MODULE_ALREADY_LOADED: | |
| 66 case mojom::ModuleEventType::MODULE_LOADED: | |
| 67 InsertLoadAddress(module_info->module_id, load_address, | |
| 68 &process_info->loaded_modules); | |
| 69 RemoveLoadAddressById(module_info->module_id, | |
| 70 &process_info->unloaded_modules); | |
| 71 break; | |
| 72 case mojom::ModuleEventType::MODULE_UNLOADED: | |
| 73 RemoveLoadAddressById(module_info->module_id, | |
| 74 &process_info->loaded_modules); | |
| 75 InsertLoadAddress(module_info->module_id, load_address, | |
| 76 &process_info->unloaded_modules); | |
| 77 break; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 void ModuleDatabase::OnModuleUnload(uint32_t process_id, | |
| 82 uintptr_t load_address) { | |
| 83 // Messages can arrive from any thread (UI thread for calls over IPC, and | |
| 84 // anywhere at all for calls from ModuleWatcher), so bounce if necessary. | |
| 85 if (!task_runner_->RunsTasksOnCurrentThread()) { | |
| 86 task_runner_->PostTask( | |
| 87 FROM_HERE, | |
| 88 base::Bind(&ModuleDatabase::OnModuleUnload, | |
| 89 weak_ptr_factory_.GetWeakPtr(), process_id, load_address)); | |
| 90 return; | |
| 91 } | |
| 92 | |
| 93 // See the long-winded comment in OnModuleEvent about reasons why this can | |
| 94 // fail (but shouldn't normally). | |
| 95 auto* process_info = GetProcessInfo(process_id); | |
| 96 if (!process_info) | |
| 97 return; | |
| 98 | |
| 99 // Find the module corresponding to this load address. | |
| 100 size_t i = | |
| 101 FindLoadAddressIndexByAddress(load_address, process_info->loaded_modules); | |
| 102 | |
| 103 // No such module found. This shouldn't happen either, unless messages are | |
| 104 // malformed or out of order. Gracefully fail in this case. | |
| 105 if (i == kInvalidIndex) | |
| 106 return; | |
| 107 | |
| 108 ModuleId module_id = process_info->loaded_modules[i].first; | |
| 109 | |
| 110 // Remove from the loaded module list and insert into the unloaded module | |
| 111 // list. | |
| 112 RemoveLoadAddressByIndex(i, &process_info->loaded_modules); | |
| 113 InsertLoadAddress(module_id, load_address, &process_info->unloaded_modules); | |
| 114 } | |
| 115 | |
| 116 void ModuleDatabase::OnProcessEnded(uint32_t process_id) { | |
| 117 // Messages can arrive from any thread (UI thread for calls over IPC, and | |
| 118 // anywhere at all for calls from ModuleWatcher), so bounce if necessary. | |
| 119 if (!task_runner_->RunsTasksOnCurrentThread()) { | |
| 120 task_runner_->PostTask( | |
| 121 FROM_HERE, base::Bind(&ModuleDatabase::OnProcessEnded, | |
| 122 weak_ptr_factory_.GetWeakPtr(), process_id)); | |
| 123 return; | |
| 124 } | |
| 125 | |
| 126 DeleteProcessInfo(process_id); | |
| 127 } | |
| 128 | |
| 129 // static | |
| 130 uint32_t ModuleDatabase::ProcessTypeToBit(content::ProcessType process_type) { | |
| 131 uint32_t bit_index = static_cast<uint32_t>(process_type) - kMinProcessType; | |
| 132 DCHECK_LE(0u, bit_index); | |
| 133 DCHECK_GE(31u, bit_index); | |
| 134 uint32_t bit = (1 << bit_index); | |
| 135 return bit; | |
| 136 } | |
| 137 | |
| 138 // static | |
| 139 content::ProcessType ModuleDatabase::BitIndexToProcessType(uint32_t bit_index) { | |
| 140 DCHECK_LE(0u, bit_index); | |
| 141 DCHECK_GE(31u, bit_index); | |
| 142 return static_cast<content::ProcessType>(bit_index + kMinProcessType); | |
| 143 } | |
| 144 | |
| 145 // static | |
| 146 size_t ModuleDatabase::FindLoadAddressIndexById( | |
| 147 ModuleId module_id, | |
| 148 const ModuleLoadAddresses& load_addresses) { | |
| 149 // Process elements in reverse order so that RemoveLoadAddressById can handle | |
| 150 // the more common case of removing the maximum element in O(1). | |
| 151 for (size_t i = load_addresses.size() - 1; i < load_addresses.size(); --i) { | |
| 152 if (load_addresses[i].first == module_id) | |
| 153 return i; | |
| 154 } | |
| 155 return kInvalidIndex; | |
| 156 } | |
| 157 | |
| 158 // static | |
| 159 size_t ModuleDatabase::FindLoadAddressIndexByAddress( | |
| 160 uintptr_t load_address, | |
| 161 const ModuleLoadAddresses& load_addresses) { | |
| 162 for (size_t i = 0; i < load_addresses.size(); ++i) { | |
| 163 if (load_addresses[i].second == load_address) | |
| 164 return i; | |
| 165 } | |
| 166 return kInvalidIndex; | |
| 167 } | |
| 168 | |
| 169 // static | |
| 170 void ModuleDatabase::InsertLoadAddress(ModuleId module_id, | |
| 171 uintptr_t load_address, | |
| 172 ModuleLoadAddresses* load_addresses) { | |
| 173 // A very small optimization: the largest module_id is always placed at the | |
| 174 // end of the array. This is the most common case, and allows O(1) | |
| 175 // determination that a |module_id| isn't present when it's bigger than the | |
| 176 // maximum already in the array. This keeps insertions to O(1) in the usual | |
| 177 // case. | |
| 178 if (load_addresses->empty() || module_id > load_addresses->back().first) { | |
| 179 load_addresses->emplace_back(module_id, load_address); | |
| 180 return; | |
| 181 } | |
| 182 | |
| 183 // If the module exists in the collection then update the load address and | |
| 184 // return. This should never really occur, unless the client is deliberately | |
| 185 // misbehaving or a race causes a reload event (at a different address) to be | |
| 186 // processed before the corresponding unload. This is very unlikely. | |
| 187 size_t i = FindLoadAddressIndexById(module_id, *load_addresses); | |
| 188 if (i != kInvalidIndex) { | |
| 189 (*load_addresses)[i].second = load_address; | |
| 190 return; | |
| 191 } | |
| 192 | |
| 193 // The module does not exist, and by definition is smaller in value than | |
| 194 // the largest module ID already present. Add it, ensuring that the largest | |
| 195 // module ID stays at the end. | |
| 196 load_addresses->emplace(--load_addresses->end(), module_id, load_address); | |
| 197 | |
| 198 return; | |
| 199 } | |
| 200 | |
| 201 // static | |
| 202 void ModuleDatabase::RemoveLoadAddressById( | |
| 203 ModuleId module_id, | |
| 204 ModuleLoadAddresses* load_addresses) { | |
| 205 if (load_addresses->empty()) | |
| 206 return; | |
| 207 | |
| 208 // This handles the special case of removing the max element in O(1), as | |
| 209 // FindLoadAddressIndexById processes the elements in reverse order. | |
| 210 size_t i = FindLoadAddressIndexById(module_id, *load_addresses); | |
| 211 RemoveLoadAddressByIndex(i, load_addresses); | |
| 212 } | |
| 213 | |
| 214 // static | |
| 215 void ModuleDatabase::RemoveLoadAddressByIndex( | |
| 216 size_t index, | |
| 217 ModuleLoadAddresses* load_addresses) { | |
| 218 DCHECK_LE(0u, index); | |
| 219 DCHECK_GT(load_addresses->size(), index); | |
| 220 | |
| 221 // Special case: removing the last module (with maximum id). Need to find the | |
| 222 // new maximum element and ensure it goes to the end. | |
| 223 if (load_addresses->size() > 2 && index + 1 == load_addresses->size()) { | |
| 224 // Find the index of the new maximum element. | |
| 225 ModuleId max_id = -1; // These start at zero. | |
| 226 int max_index = kInvalidIndex; | |
| 227 for (size_t i = 0; i < load_addresses->size() - 1; ++i) { | |
| 228 if ((*load_addresses)[i].first > max_id) { | |
| 229 max_id = (*load_addresses)[i].first; | |
| 230 max_index = i; | |
| 231 } | |
| 232 } | |
| 233 | |
| 234 // Remove the max element. | |
| 235 load_addresses->resize(load_addresses->size() - 1); | |
| 236 | |
| 237 // If the new max element isn't in the last position, then swap it so it is. | |
| 238 int last_index = load_addresses->size() - 1; | |
| 239 if (max_index != last_index) | |
| 240 std::swap((*load_addresses)[max_index], (*load_addresses)[last_index]); | |
| 241 | |
| 242 return; | |
| 243 } | |
| 244 | |
| 245 // If the element to be removed is second last then a single copy is | |
| 246 // sufficient. | |
| 247 if (index + 2 == load_addresses->size()) { | |
| 248 (*load_addresses)[index] = (*load_addresses)[index + 1]; | |
| 249 } else { | |
| 250 // In the general case two copies are necessary. | |
| 251 int max_index = load_addresses->size() - 1; | |
| 252 (*load_addresses)[index] = (*load_addresses)[max_index - 1]; | |
| 253 (*load_addresses)[max_index - 1] = (*load_addresses)[max_index]; | |
| 254 } | |
| 255 | |
| 256 // Remove the last element, which is now duplicated. | |
| 257 load_addresses->resize(load_addresses->size() - 1); | |
| 258 } | |
| 259 | |
| 260 ModuleDatabase::ModuleInfo* ModuleDatabase::FindOrCreateModuleInfo( | |
| 261 const base::FilePath& module_path) { | |
| 262 ModuleInfo key(module_path, modules_.size()); | |
| 263 auto result = modules_.insert(key); | |
| 264 // Cast away constness so that the non-key portions of the object can be | |
| 265 // modified. The key portions of the object are themselves marked const, so | |
| 266 // this causes no trouble with std::set. | |
| 267 return const_cast<ModuleInfo*>(&(*result.first)); | |
| 268 } | |
| 269 | |
| 270 ModuleDatabase::ProcessInfo* ModuleDatabase::GetProcessInfo( | |
| 271 uint32_t process_id) { | |
| 272 ProcessInfo key(process_id, content::PROCESS_TYPE_UNKNOWN); | |
| 273 auto it = processes_.find(key); | |
| 274 if (it == processes_.end()) | |
| 275 return nullptr; | |
| 276 // Cast away constness so that the non-key portions of the object can be | |
| 277 // modified. The key portions of the object are themselves marked const, so | |
| 278 // this causes no trouble with std::set. | |
| 279 return const_cast<ProcessInfo*>(&(*it)); | |
| 280 } | |
| 281 | |
| 282 void ModuleDatabase::CreateProcessInfo(uint32_t process_id, | |
| 283 content::ProcessType process_type) { | |
| 284 ProcessInfo key(process_id, process_type); | |
| 285 processes_.insert(key); | |
|
grt (UTC plus 2)
2016/12/20 21:09:53
https://youtu.be/Aharq3PFX54
processes_.emplace(
chrisha
2016/12/21 20:15:00
Done.
| |
| 286 } | |
| 287 | |
| 288 void ModuleDatabase::DeleteProcessInfo(uint32_t process_id) { | |
| 289 ProcessInfo key(process_id, content::PROCESS_TYPE_UNKNOWN); | |
| 290 auto it = processes_.find(key); | |
| 291 if (it == processes_.end()) | |
|
grt (UTC plus 2)
2016/12/20 21:09:53
nit: flip logic and move the "else" into the "if"
chrisha
2016/12/21 20:15:00
Done.
| |
| 292 return; | |
| 293 processes_.erase(it); | |
| 294 } | |
| 295 | |
| 296 // ModuleDatabase::ModuleInfo -------------------------------------------------- | |
| 297 | |
| 298 ModuleDatabase::ModuleInfo::ModuleInfo(const base::FilePath& module_path, | |
| 299 uint32_t module_id) | |
| 300 : module_path(module_path), module_id(module_id), process_types(0) {} | |
| 301 | |
| 302 bool ModuleDatabase::ModuleInfo::operator<(const ModuleInfo& mi) const { | |
| 303 return module_path < mi.module_path; | |
| 304 } | |
| 305 | |
| 306 // ModuleDatabase::ProcessInfo ------------------------------------------------- | |
| 307 | |
| 308 ModuleDatabase::ProcessInfo::ProcessInfo(uint32_t process_id, | |
| 309 content::ProcessType process_type) | |
| 310 : process_id(process_id), process_type(process_type) {} | |
| 311 | |
| 312 bool ModuleDatabase::ProcessInfo::operator<(const ProcessInfo& pi) const { | |
| 313 return process_id < pi.process_id; | |
| 314 } | |
| OLD | NEW |