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 #ifndef CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ | |
| 6 #define CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 #include <utility> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/sequenced_task_runner.h" | |
| 15 #include "content/public/common/process_type.h" | |
| 16 | |
| 17 // A class that keeps track of all modules loaded across Chrome processes. | |
| 18 // Drives the chrome://conflicts UI. | |
| 19 class ModuleDatabase { | |
| 20 public: | |
| 21 // A ModuleDatabase is by default bound to a provided sequenced task runner. | |
| 22 // All calls must be made in the context of this task runner, unless | |
| 23 // otherwise noted. For calls from other contexts this task runner is used to | |
| 24 // bounce the call when appropriate. | |
| 25 explicit ModuleDatabase(scoped_refptr<base::SequencedTaskRunner> task_runner); | |
| 26 ~ModuleDatabase(); | |
| 27 | |
| 28 // Indicates that process with the given type has started. This must be called | |
| 29 // before any calls to OnModuleEvent or OnModuleUnload. Must be called on the | |
| 30 // same thread as |task_runner_|. | |
|
grt (UTC plus 2)
2016/12/22 14:19:06
nit: "on the same thread" -> "in the same sequence
chrisha
2017/01/03 21:34:47
Done.
| |
| 31 void OnProcessStarted(uint32_t process_id, | |
| 32 uint64_t creation_time, | |
| 33 content::ProcessType process_type); | |
| 34 | |
| 35 // Indicates that a module has been loaded. The data passed to this function | |
| 36 // is taken as gospel, so if it originates from a remote process it should be | |
| 37 // independently validated first. (In practice, see ModuleEventSinkImpl for | |
| 38 // details of where this happens.) | |
| 39 void OnModuleLoad(uint32_t process_id, | |
| 40 uint64_t creation_time, | |
| 41 const base::FilePath& module_path, | |
| 42 uint32_t module_size, | |
| 43 uint32_t module_time_date_stamp, | |
| 44 uintptr_t module_load_address); | |
| 45 | |
| 46 // Indicates that the module at the given |load_address| in the specified | |
| 47 // process is being unloaded. This need not be trusted data, as it will be | |
| 48 // validated by the ModuleDatabase directly. | |
| 49 void OnModuleUnload(uint32_t process_id, | |
| 50 uint64_t creation_time, | |
| 51 uintptr_t module_load_address); | |
| 52 | |
| 53 // Indicates that the given process has ended. This can be called from any | |
| 54 // thread and will be bounced to the |task_runner|. In practice it will be | |
|
grt (UTC plus 2)
2016/12/22 14:19:06
nit: |task_runner_| for consistency with comment a
chrisha
2017/01/03 21:34:48
Done.
| |
| 55 // invoked from the UI thread as the Mojo channel is torn down. | |
| 56 void OnProcessEnded(uint32_t process_id, uint64_t creation_time); | |
| 57 | |
| 58 // TODO(chrisha): Module analysis code, and various accessors for use by | |
| 59 // chrome://conflicts. | |
| 60 | |
| 61 private: | |
| 62 friend class TestModuleDatabase; | |
| 63 friend class ModuleDatabaseTest; | |
| 64 friend class ModuleEventSinkImplTest; | |
| 65 | |
| 66 static constexpr size_t kInvalidIndex = ~0u; | |
| 67 | |
| 68 // Used as a unique identifier for a module in a ModuleSet. | |
| 69 using ModuleId = int; | |
| 70 | |
| 71 // Structures for maintaining information about modules. | |
| 72 struct ModuleInfo; | |
| 73 using ModuleSet = std::set<ModuleInfo>; | |
| 74 using ModuleLoadAddresses = std::vector<std::pair<ModuleId, uintptr_t>>; | |
| 75 | |
| 76 // Structures for maintaining information about running processes. | |
| 77 struct ProcessInfo; | |
| 78 using ProcessSet = std::set<ProcessInfo>; | |
| 79 | |
| 80 // Converts a valid |process_type| to a bit for use in a bitmask of process | |
| 81 // values. Exposed in the header for testing. | |
| 82 static uint32_t ProcessTypeToBit(content::ProcessType process_type); | |
| 83 | |
| 84 // Converts a |bit_index| (which maps to the bit 1 << bit_index) to the | |
| 85 // corresponding process type. Exposed in the header for testing. | |
| 86 static content::ProcessType BitIndexToProcessType(uint32_t bit_index); | |
| 87 | |
| 88 // Performs a linear scan to find the index of a |module_id| or |load_address| | |
| 89 // in a collection of modules. Returns kInvalidIndex if the index is not | |
| 90 // found. | |
| 91 static size_t FindLoadAddressIndexById( | |
| 92 ModuleId module_id, | |
| 93 const ModuleLoadAddresses& load_addresses); | |
| 94 static size_t FindLoadAddressIndexByAddress( | |
| 95 uintptr_t load_address, | |
| 96 const ModuleLoadAddresses& load_addresses); | |
| 97 | |
| 98 // Inserts a module into a ModuleLoadAddress object. | |
| 99 static void InsertLoadAddress(ModuleId module_id, | |
| 100 uintptr_t load_address, | |
| 101 ModuleLoadAddresses* load_addresses); | |
| 102 | |
| 103 // Removes a module from a ModuleLoadAddress object, either by the | |
| 104 // |module_id| or the |index| in the collection. | |
| 105 static void RemoveLoadAddressById(ModuleId module_id, | |
| 106 ModuleLoadAddresses* load_addresses); | |
| 107 static void RemoveLoadAddressByIndex(size_t index, | |
| 108 ModuleLoadAddresses* load_addresses); | |
| 109 | |
| 110 // Finds or creates a mutable ModuleInfo entry. | |
| 111 ModuleInfo* FindOrCreateModuleInfo(const base::FilePath& module_path, | |
| 112 uint32_t module_size, | |
| 113 uint32_t module_time_date_stamp); | |
| 114 | |
| 115 // Finds a process info entry. Returns nullptr if none is found. | |
| 116 ProcessInfo* GetProcessInfo(uint32_t process_id, uint64_t creation_time); | |
| 117 | |
| 118 // Creates a process info entry. | |
| 119 void CreateProcessInfo(uint32_t process_id, | |
| 120 uint64_t creation_time, | |
| 121 content::ProcessType process_type); | |
| 122 | |
| 123 // Deletes a process info entry. | |
| 124 void DeleteProcessInfo(uint32_t process_id, uint64_t creation_time); | |
| 125 | |
| 126 // The task runner to which this object is bound. | |
| 127 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 128 | |
| 129 // A map of all known modules. | |
| 130 ModuleSet modules_; | |
| 131 | |
| 132 // The set of all known running processes, and modules loaded/unloaded in | |
| 133 // them. | |
| 134 ProcessSet processes_; | |
| 135 | |
| 136 // Weak pointer factory for this object. This is used when bouncing | |
| 137 // incoming events to |task_runner_|. | |
| 138 base::WeakPtrFactory<ModuleDatabase> weak_ptr_factory_; | |
| 139 | |
| 140 DISALLOW_COPY_AND_ASSIGN(ModuleDatabase); | |
| 141 }; | |
| 142 | |
| 143 // Maintains information about a module. Modules are permanent once added to | |
| 144 // the ModuleSet, so this structure grows monotonically. In practice this is | |
| 145 // not an issue as the modules themselves are vastly bigger than the minor | |
| 146 // amount of metadata tracked here. | |
| 147 struct ModuleDatabase::ModuleInfo { | |
| 148 ModuleInfo(const base::FilePath& module_path, | |
| 149 uint32_t module_size, | |
| 150 uint32_t module_time_date_stamp, | |
| 151 uint32_t module_id); | |
| 152 | |
| 153 // Less-than operator allowing this object to be used in std::set. | |
| 154 bool operator<(const ModuleInfo& mi) const; | |
| 155 | |
| 156 // Full path to the module on disk. Part of the key for a ModuleInfo. | |
| 157 const base::FilePath module_path; | |
| 158 | |
| 159 // The module size. Part of the key for a ModuleInfo. | |
| 160 const uint32_t module_size; | |
|
grt (UTC plus 2)
2016/12/22 14:19:06
maybe mention that this is SizeOfImage from the mo
chrisha
2017/01/03 21:34:47
Done.
| |
| 161 | |
| 162 // The module time date stamp. Part of the key for a ModuleInfo. | |
| 163 const uint32_t module_time_date_stamp; | |
|
grt (UTC plus 2)
2016/12/22 14:19:06
maybe mention that this is TimeDateStamp from the
chrisha
2017/01/03 21:34:47
Done.
| |
| 164 | |
| 165 // The ID of this module. This is a strictly incrementing value, and is used | |
| 166 // to tie a module to the list of running processes in which it is found. | |
| 167 // It is not part of the key for the module, but it is immutable. This is | |
| 168 // simply the index of the module in the insertion order. | |
| 169 const ModuleId module_id; | |
| 170 | |
| 171 // Everything below this point is implicitly mutable data. Const versions of | |
| 172 // this struct will have their constness casted away at runtime, allowing | |
| 173 // map-like behaviour for a set without duplicating the key externally. | |
| 174 | |
| 175 // Set of all process types in which this module has been seen (may not be | |
| 176 // currently present in a process of that type). This is a conversion of | |
| 177 // ProcessType enumeration to a bitfield. See "ProcessTypeToBit" and | |
| 178 // "BitIndexToProcessType" for details. | |
| 179 uint32_t process_types; | |
| 180 }; | |
| 181 | |
| 182 // Information about a running process. This ties modules in a ModuleSet to | |
| 183 // processes in which they are (or have been) loaded. | |
| 184 struct ModuleDatabase::ProcessInfo { | |
| 185 ProcessInfo(uint32_t process_id, | |
| 186 uint64_t creation_time, | |
| 187 content::ProcessType process_type); | |
| 188 | |
| 189 // Less-than operator allowing this object to be used in std::set. | |
| 190 bool operator<(const ProcessInfo& pi) const; | |
| 191 | |
| 192 const uint32_t process_id; | |
| 193 const uint64_t creation_time; | |
| 194 const content::ProcessType process_type; | |
| 195 | |
| 196 // Everything below this point is implicitly mutable data. Const versions of | |
| 197 // this struct will have their constness casted away at runtime, allowing | |
| 198 // map-like behaviour for a set without duplicating the key externally. | |
| 199 | |
| 200 // The set of modules that are loaded/unloaded in this process, by ID. This | |
| 201 // is typically a small list so a linear cost is okay to pay for | |
| 202 // lookup/deletion. | |
| 203 // | |
| 204 // These are modified by the various static *LoadAddress* helper functions in | |
| 205 // ModuleDatabase. The vector maintains the invariant the element with maximum | |
| 206 // module ID is always last. This ensures that the usual operation of loading | |
| 207 // a module is O(1). | |
| 208 ModuleLoadAddresses loaded_modules; | |
| 209 ModuleLoadAddresses unloaded_modules; | |
| 210 }; | |
| 211 | |
| 212 #endif // CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ | |
| OLD | NEW |