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