| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome_frame/module_utils.h" | 5 #include "chrome_frame/module_utils.h" |
| 6 | 6 |
| 7 #include <atlbase.h> | 7 #include <atlbase.h> |
| 8 #include <TlHelp32.h> | 8 #include <TlHelp32.h> |
| 9 | 9 |
| 10 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
| 11 #include "base/file_version_info.h" | 11 #include "base/file_version_info.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/scoped_handle.h" | 13 #include "base/scoped_handle.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "base/version.h" | 15 #include "base/version.h" |
| 16 #include "chrome_frame/exception_barrier.h" |
| 16 | 17 |
| 17 DllRedirector::DllRedirector() : dcgo_ptr_(NULL), initialized_(false), | 18 DllRedirector::DllRedirector() : dcgo_ptr_(NULL), initialized_(false), |
| 18 module_handle_(NULL) {} | 19 module_handle_(NULL) {} |
| 19 | 20 |
| 20 DllRedirector::~DllRedirector() { | 21 DllRedirector::~DllRedirector() { |
| 21 if (module_handle_) { | 22 if (module_handle_) { |
| 22 FreeLibrary(module_handle_); | 23 FreeLibrary(module_handle_); |
| 23 module_handle_ = NULL; | 24 module_handle_ = NULL; |
| 24 } | 25 } |
| 25 } | 26 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 REFCLSID clsid, | 66 REFCLSID clsid, |
| 66 HMODULE* oldest_module_handle) { | 67 HMODULE* oldest_module_handle) { |
| 67 DCHECK(oldest_module_handle); | 68 DCHECK(oldest_module_handle); |
| 68 | 69 |
| 69 ScopedHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 0)); | 70 ScopedHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 0)); |
| 70 if (snapshot == INVALID_HANDLE_VALUE) { | 71 if (snapshot == INVALID_HANDLE_VALUE) { |
| 71 LOG(ERROR) << "Could not create module snapshot!"; | 72 LOG(ERROR) << "Could not create module snapshot!"; |
| 72 return false; | 73 return false; |
| 73 } | 74 } |
| 74 | 75 |
| 76 |
| 75 bool success = false; | 77 bool success = false; |
| 76 PathToHModuleMap map; | 78 PathToHModuleMap map; |
| 77 | 79 |
| 78 // First get the list of module paths, and save the full path to base address | 80 { |
| 79 // mapping. | 81 // Here we add an SEH to the chain to prevent our VEH from picking up on any |
| 80 MODULEENTRY32W module_entry; | 82 // exceptions thrown in DLLs who hook some of the below api calls. We will |
| 81 module_entry.dwSize = sizeof(module_entry); | 83 // still report the exceptions if they make our way back to us, the hope is |
| 82 BOOL cont = Module32FirstW(snapshot, &module_entry); | 84 // that they will not. |
| 83 while (cont) { | 85 ExceptionBarrier exception_barrier; |
| 84 if (!lstrcmpi(module_entry.szModule, module_name.c_str())) { | 86 |
| 85 std::wstring full_path(module_entry.szExePath); | 87 // First get the list of module paths, and save the full path to base |
| 86 map[full_path] = module_entry.hModule; | 88 // address mapping. |
| 89 MODULEENTRY32W module_entry = {0}; |
| 90 module_entry.dwSize = sizeof(module_entry); |
| 91 BOOL cont = Module32FirstW(snapshot, &module_entry); |
| 92 while (cont) { |
| 93 if (!lstrcmpi(module_entry.szModule, module_name.c_str())) { |
| 94 std::wstring full_path(module_entry.szExePath); |
| 95 map[full_path] = module_entry.hModule; |
| 96 } |
| 97 SecureZeroMemory(&module_entry, sizeof(MODULEENTRY32W)); |
| 98 module_entry.dwSize = sizeof(module_entry); |
| 99 cont = Module32NextW(snapshot, &module_entry); |
| 87 } | 100 } |
| 88 cont = Module32NextW(snapshot, &module_entry); | |
| 89 } | 101 } |
| 90 | 102 |
| 91 // Next, enumerate the map and find the oldest version of the module. | 103 // Next, enumerate the map and find the oldest version of the module. |
| 92 // (check if the map is of size 1 first) | 104 // (check if the map is of size 1 first) |
| 93 if (!map.empty()) { | 105 if (!map.empty()) { |
| 94 if (map.size() == 1) { | 106 if (map.size() == 1) { |
| 95 *oldest_module_handle = map.begin()->second; | 107 *oldest_module_handle = map.begin()->second; |
| 96 } else { | 108 } else { |
| 97 *oldest_module_handle = GetHandleOfOldestModule(map, clsid); | 109 *oldest_module_handle = GetHandleOfOldestModule(map, clsid); |
| 98 } | 110 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 LOG(ERROR) << "Module Scan: Couldn't get address of " | 178 LOG(ERROR) << "Module Scan: Couldn't get address of " |
| 167 << "DllGetClassObject: " | 179 << "DllGetClassObject: " |
| 168 << GetLastError(); | 180 << GetLastError(); |
| 169 } | 181 } |
| 170 } else { | 182 } else { |
| 171 LOG(ERROR) << "Module Scan: Could not increment module count: " | 183 LOG(ERROR) << "Module Scan: Could not increment module count: " |
| 172 << GetLastError(); | 184 << GetLastError(); |
| 173 } | 185 } |
| 174 return proc_ptr; | 186 return proc_ptr; |
| 175 } | 187 } |
| OLD | NEW |