| 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 in the |
| 30 // same sequence as |task_runner_|. |
| 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 |
| 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. This is taken from |
| 160 // SizeOfImage from the module's IMAGE_OPTIONAL_HEADER. |
| 161 const uint32_t module_size; |
| 162 |
| 163 // The module time date stamp. Part of the key for a ModuleInfo. Taken from |
| 164 // TimeDateStamp from the module's IMAGE_FILE_HEADER. |
| 165 const uint32_t module_time_date_stamp; |
| 166 |
| 167 // The ID of this module. This is a strictly incrementing value, and is used |
| 168 // to tie a module to the list of running processes in which it is found. |
| 169 // It is not part of the key for the module, but it is immutable. This is |
| 170 // simply the index of the module in the insertion order. |
| 171 const ModuleId module_id; |
| 172 |
| 173 // Everything below this point is implicitly mutable data. Const versions of |
| 174 // this struct will have their constness casted away at runtime, allowing |
| 175 // map-like behaviour for a set without duplicating the key externally. |
| 176 |
| 177 // Set of all process types in which this module has been seen (may not be |
| 178 // currently present in a process of that type). This is a conversion of |
| 179 // ProcessType enumeration to a bitfield. See "ProcessTypeToBit" and |
| 180 // "BitIndexToProcessType" for details. |
| 181 uint32_t process_types; |
| 182 }; |
| 183 |
| 184 // Information about a running process. This ties modules in a ModuleSet to |
| 185 // processes in which they are (or have been) loaded. |
| 186 struct ModuleDatabase::ProcessInfo { |
| 187 ProcessInfo(uint32_t process_id, |
| 188 uint64_t creation_time, |
| 189 content::ProcessType process_type); |
| 190 |
| 191 // Less-than operator allowing this object to be used in std::set. |
| 192 bool operator<(const ProcessInfo& pi) const; |
| 193 |
| 194 const uint32_t process_id; |
| 195 const uint64_t creation_time; |
| 196 const content::ProcessType process_type; |
| 197 |
| 198 // Everything below this point is implicitly mutable data. Const versions of |
| 199 // this struct will have their constness casted away at runtime, allowing |
| 200 // map-like behaviour for a set without duplicating the key externally. |
| 201 |
| 202 // The set of modules that are loaded/unloaded in this process, by ID. This |
| 203 // is typically a small list so a linear cost is okay to pay for |
| 204 // lookup/deletion. |
| 205 // |
| 206 // These are modified by the various static *LoadAddress* helper functions in |
| 207 // ModuleDatabase. The vector maintains the invariant the element with maximum |
| 208 // module ID is always last. This ensures that the usual operation of loading |
| 209 // a module is O(1). |
| 210 ModuleLoadAddresses loaded_modules; |
| 211 ModuleLoadAddresses unloaded_modules; |
| 212 }; |
| 213 |
| 214 #endif // CHROME_BROWSER_CONFLICTS_MODULE_DATABASE_WIN_H_ |
| OLD | NEW |