| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #ifndef CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ | 5 #ifndef CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ |
| 6 #define CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ | 6 #define CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
| 15 #include "content/public/common/process_type.h" | 15 #include "content/public/common/process_type.h" |
| 16 | 16 |
| 17 // A class that keeps track of all modules loaded across Chrome processes. | 17 // A class that keeps track of all modules loaded across Chrome processes. |
| 18 // Drives the chrome://conflicts UI. | 18 // Drives the chrome://conflicts UI. |
| 19 // | 19 // |
| 20 // This is effectively a singleton, but doesn't use base::Singleton. The intent | 20 // This is effectively a singleton, but doesn't use base::Singleton. The intent |
| 21 // is for the object to be created when Chrome is single-threaded, and for it | 21 // is for the object to be created when Chrome is single-threaded, and for it |
| 22 // be set as the process-wide singleton via SetInstance. | 22 // be set as the process-wide singleton via SetInstance. |
| 23 class ModuleDatabase { | 23 class ModuleDatabase { |
| 24 public: | 24 public: |
| 25 // Used as a unique identifier for a module in a ModuleSet. |
| 26 using ModuleId = int; |
| 27 |
| 28 // The type of certificate found for the module. |
| 29 enum CertificateType { |
| 30 // The module is not signed. |
| 31 NO_CERTIFICATE, |
| 32 // The module is signed and the certificate is in the module. |
| 33 CERTIFICATE_IN_FILE, |
| 34 // The module is signed and the certificate is in an external catalog. |
| 35 CERTIFICATE_IN_CATALOG, |
| 36 }; |
| 37 |
| 38 // Structures for maintaining information about modules. |
| 39 struct ModuleInfoKey; |
| 40 struct CertificateInfo; |
| 41 struct ModuleInfoData; |
| 42 using ModuleMap = std::map<ModuleInfoKey, ModuleInfoData>; |
| 43 using ModuleInfo = ModuleMap::value_type; |
| 44 |
| 45 // Used for maintaing a list of modules loaded in a process. Maps module IDs |
| 46 // to load addresses. |
| 47 using ModuleLoadAddresses = std::vector<std::pair<ModuleId, uintptr_t>>; |
| 48 |
| 49 // Structures for maintaining information about running processes. |
| 50 struct ProcessInfoKey; |
| 51 struct ProcessInfoData; |
| 52 using ProcessMap = std::map<ProcessInfoKey, ProcessInfoData>; |
| 53 using ProcessInfo = ProcessMap::value_type; |
| 54 |
| 25 // A ModuleDatabase is by default bound to a provided sequenced task runner. | 55 // A ModuleDatabase is by default bound to a provided sequenced task runner. |
| 26 // All calls must be made in the context of this task runner, unless | 56 // All calls must be made in the context of this task runner, unless |
| 27 // otherwise noted. For calls from other contexts this task runner is used to | 57 // otherwise noted. For calls from other contexts this task runner is used to |
| 28 // bounce the call when appropriate. | 58 // bounce the call when appropriate. |
| 29 explicit ModuleDatabase(scoped_refptr<base::SequencedTaskRunner> task_runner); | 59 explicit ModuleDatabase(scoped_refptr<base::SequencedTaskRunner> task_runner); |
| 30 ~ModuleDatabase(); | 60 ~ModuleDatabase(); |
| 31 | 61 |
| 32 // Retrieves the singleton global instance of the ModuleDatabase. | 62 // Retrieves the singleton global instance of the ModuleDatabase. |
| 33 static ModuleDatabase* GetInstance(); | 63 static ModuleDatabase* GetInstance(); |
| 34 | 64 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 | 102 |
| 73 private: | 103 private: |
| 74 friend class TestModuleDatabase; | 104 friend class TestModuleDatabase; |
| 75 friend class ModuleDatabaseTest; | 105 friend class ModuleDatabaseTest; |
| 76 friend class ModuleEventSinkImplTest; | 106 friend class ModuleEventSinkImplTest; |
| 77 | 107 |
| 78 // Used by the FindLoadAddress* functions to indicate a load address has not | 108 // Used by the FindLoadAddress* functions to indicate a load address has not |
| 79 // been found. | 109 // been found. |
| 80 static constexpr size_t kInvalidIndex = ~0u; | 110 static constexpr size_t kInvalidIndex = ~0u; |
| 81 | 111 |
| 82 // Used as a unique identifier for a module in a ModuleSet. | |
| 83 using ModuleId = int; | |
| 84 | |
| 85 // Structures for maintaining information about modules. | |
| 86 struct ModuleInfoKey; | |
| 87 struct ModuleInfoData; | |
| 88 using ModuleMap = std::map<ModuleInfoKey, ModuleInfoData>; | |
| 89 using ModuleInfo = ModuleMap::value_type; | |
| 90 | |
| 91 // Used for maintaing a list of modules loaded in a process. Maps module IDs | |
| 92 // to load addresses. | |
| 93 using ModuleLoadAddresses = std::vector<std::pair<ModuleId, uintptr_t>>; | |
| 94 | |
| 95 // Structures for maintaining information about running processes. | |
| 96 struct ProcessInfoKey; | |
| 97 struct ProcessInfoData; | |
| 98 using ProcessMap = std::map<ProcessInfoKey, ProcessInfoData>; | |
| 99 using ProcessInfo = ProcessMap::value_type; | |
| 100 | |
| 101 // Converts a valid |process_type| to a bit for use in a bitmask of process | 112 // Converts a valid |process_type| to a bit for use in a bitmask of process |
| 102 // values. Exposed in the header for testing. | 113 // values. Exposed in the header for testing. |
| 103 static uint32_t ProcessTypeToBit(content::ProcessType process_type); | 114 static uint32_t ProcessTypeToBit(content::ProcessType process_type); |
| 104 | 115 |
| 105 // Converts a |bit_index| (which maps to the bit 1 << bit_index) to the | 116 // Converts a |bit_index| (which maps to the bit 1 << bit_index) to the |
| 106 // corresponding process type. Exposed in the header for testing. | 117 // corresponding process type. Exposed in the header for testing. |
| 107 static content::ProcessType BitIndexToProcessType(uint32_t bit_index); | 118 static content::ProcessType BitIndexToProcessType(uint32_t bit_index); |
| 108 | 119 |
| 109 // Performs a linear scan to find the index of a |module_id| or |load_address| | 120 // Performs a linear scan to find the index of a |module_id| or |load_address| |
| 110 // in a collection of modules. Returns kInvalidIndex if the index is not | 121 // in a collection of modules. Returns kInvalidIndex if the index is not |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 // TimeDateStamp from the module's IMAGE_FILE_HEADER. | 199 // TimeDateStamp from the module's IMAGE_FILE_HEADER. |
| 189 uint32_t module_time_date_stamp; | 200 uint32_t module_time_date_stamp; |
| 190 | 201 |
| 191 // The ID of this module. This is a strictly incrementing value, and is used | 202 // The ID of this module. This is a strictly incrementing value, and is used |
| 192 // to tie a module to the list of running processes in which it is found. | 203 // to tie a module to the list of running processes in which it is found. |
| 193 // It is not part of the key for the module, but it is immutable. This is | 204 // It is not part of the key for the module, but it is immutable. This is |
| 194 // simply the index of the module in the insertion order. | 205 // simply the index of the module in the insertion order. |
| 195 ModuleId module_id; | 206 ModuleId module_id; |
| 196 }; | 207 }; |
| 197 | 208 |
| 209 // Information about the certificate of a file. |
| 210 struct ModuleDatabase::CertificateInfo { |
| 211 CertificateInfo(); |
| 212 |
| 213 // The type of signature encountered. |
| 214 CertificateType type; |
| 215 |
| 216 // Path to the file containing the certificate. Empty if |type| is |
| 217 // NO_CERTIFICATE. |
| 218 base::FilePath path; |
| 219 |
| 220 // The "Subject" name of the certificate. This is the signer (ie, |
| 221 // "Google Inc." or "Microsoft Inc."). |
| 222 base::string16 subject; |
| 223 }; |
| 224 |
| 198 // This is the mutable portion of the module information, and is the storage | 225 // This is the mutable portion of the module information, and is the storage |
| 199 // type in a std::map. | 226 // type in a std::map. |
| 200 struct ModuleDatabase::ModuleInfoData { | 227 struct ModuleDatabase::ModuleInfoData { |
| 201 ModuleInfoData(); | 228 ModuleInfoData(); |
| 229 ModuleInfoData(const ModuleInfoData& others); |
| 230 ~ModuleInfoData(); |
| 202 | 231 |
| 203 // Set of all process types in which this module has been seen (may not be | 232 // Set of all process types in which this module has been seen (may not be |
| 204 // currently present in a process of that type). This is a conversion of | 233 // currently present in a process of that type). This is a conversion of |
| 205 // ProcessType enumeration to a bitfield. See "ProcessTypeToBit" and | 234 // ProcessType enumeration to a bitfield. See "ProcessTypeToBit" and |
| 206 // "BitIndexToProcessType" for details. | 235 // "BitIndexToProcessType" for details. |
| 207 uint32_t process_types; | 236 uint32_t process_types; |
| 237 |
| 238 // The following pieces of information are determined via a detailed |
| 239 // inspection of the module. This is relatively expensive and uses blocking |
| 240 // IO, so is performed in a background task. |
| 241 |
| 242 // The module path, not including the basename. This is cleaned and normalized |
| 243 // so that common paths are converted to their environment variable mappings |
| 244 // (ie, %systemroot%). This makes i18n localized paths easily comparable. |
| 245 base::string16 location; |
| 246 |
| 247 // The basename of the module. |
| 248 base::string16 basename; |
| 249 |
| 250 // The name of the product the module belongs to. |
| 251 base::string16 product_name; |
| 252 |
| 253 // The module file description. |
| 254 base::string16 description; |
| 255 |
| 256 // The module version. This is usually in the form a.b.c.d (where a, b, c and |
| 257 // d are integers), but may also have fewer than 4 components. |
| 258 base::string16 version; |
| 259 |
| 260 // The certificate info for the module. |
| 261 CertificateInfo certificate_info; |
| 208 }; | 262 }; |
| 209 | 263 |
| 210 // Information about a running process. This ties modules in a ModuleSet to | 264 // Information about a running process. This ties modules in a ModuleSet to |
| 211 // processes in which they are (or have been) loaded. | 265 // processes in which they are (or have been) loaded. |
| 212 | 266 |
| 213 // This is the constant portion of the process information, and acts as the key | 267 // This is the constant portion of the process information, and acts as the key |
| 214 // in a std::map. | 268 // in a std::map. |
| 215 struct ModuleDatabase::ProcessInfoKey { | 269 struct ModuleDatabase::ProcessInfoKey { |
| 216 ProcessInfoKey(uint32_t process_id, | 270 ProcessInfoKey(uint32_t process_id, |
| 217 uint64_t creation_time, | 271 uint64_t creation_time, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 246 // | 300 // |
| 247 // These are modified by the various static *LoadAddress* helper functions in | 301 // These are modified by the various static *LoadAddress* helper functions in |
| 248 // ModuleDatabase. The vector maintains the invariant the element with maximum | 302 // ModuleDatabase. The vector maintains the invariant the element with maximum |
| 249 // module ID is always last. This ensures that the usual operation of loading | 303 // module ID is always last. This ensures that the usual operation of loading |
| 250 // a module is O(1). | 304 // a module is O(1). |
| 251 ModuleLoadAddresses loaded_modules; | 305 ModuleLoadAddresses loaded_modules; |
| 252 ModuleLoadAddresses unloaded_modules; | 306 ModuleLoadAddresses unloaded_modules; |
| 253 }; | 307 }; |
| 254 | 308 |
| 255 #endif // CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ | 309 #endif // CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ |
| OLD | NEW |