| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/win/iat_patch_function.h" | 5 #include "base/win/iat_patch_function.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/win/pe_image.h" | 8 #include "base/win/pe_image.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 if (NULL != intercept_function_) { | 228 if (NULL != intercept_function_) { |
| 229 DWORD error = Unpatch(); | 229 DWORD error = Unpatch(); |
| 230 DCHECK_EQ(static_cast<DWORD>(NO_ERROR), error); | 230 DCHECK_EQ(static_cast<DWORD>(NO_ERROR), error); |
| 231 } | 231 } |
| 232 } | 232 } |
| 233 | 233 |
| 234 DWORD IATPatchFunction::Patch(const wchar_t* module, | 234 DWORD IATPatchFunction::Patch(const wchar_t* module, |
| 235 const char* imported_from_module, | 235 const char* imported_from_module, |
| 236 const char* function_name, | 236 const char* function_name, |
| 237 void* new_function) { | 237 void* new_function) { |
| 238 DCHECK_EQ(static_cast<void*>(NULL), original_function_); | |
| 239 DCHECK_EQ(static_cast<IMAGE_THUNK_DATA*>(NULL), iat_thunk_); | |
| 240 DCHECK_EQ(static_cast<void*>(NULL), intercept_function_); | |
| 241 | |
| 242 HMODULE module_handle = LoadLibraryW(module); | 238 HMODULE module_handle = LoadLibraryW(module); |
| 243 | |
| 244 if (module_handle == NULL) { | 239 if (module_handle == NULL) { |
| 245 NOTREACHED(); | 240 NOTREACHED(); |
| 246 return GetLastError(); | 241 return GetLastError(); |
| 247 } | 242 } |
| 248 | 243 |
| 249 DWORD error = InterceptImportedFunction(module_handle, | 244 DWORD error = PatchFromModule(module_handle, imported_from_module, |
| 245 function_name, new_function); |
| 246 if (NO_ERROR == error) { |
| 247 module_handle_ = module_handle; |
| 248 } else { |
| 249 FreeLibrary(module_handle); |
| 250 } |
| 251 |
| 252 return error; |
| 253 } |
| 254 |
| 255 DWORD IATPatchFunction::PatchFromModule(HMODULE module, |
| 256 const char* imported_from_module, |
| 257 const char* function_name, |
| 258 void* new_function) { |
| 259 DCHECK_EQ(static_cast<void*>(NULL), original_function_); |
| 260 DCHECK_EQ(static_cast<IMAGE_THUNK_DATA*>(NULL), iat_thunk_); |
| 261 DCHECK_EQ(static_cast<void*>(NULL), intercept_function_); |
| 262 DCHECK(module); |
| 263 |
| 264 DWORD error = InterceptImportedFunction(module, |
| 250 imported_from_module, | 265 imported_from_module, |
| 251 function_name, | 266 function_name, |
| 252 new_function, | 267 new_function, |
| 253 &original_function_, | 268 &original_function_, |
| 254 &iat_thunk_); | 269 &iat_thunk_); |
| 255 | 270 |
| 256 if (NO_ERROR == error) { | 271 if (NO_ERROR == error) { |
| 257 DCHECK_NE(original_function_, intercept_function_); | 272 DCHECK_NE(original_function_, intercept_function_); |
| 258 module_handle_ = module_handle; | |
| 259 intercept_function_ = new_function; | 273 intercept_function_ = new_function; |
| 260 } else { | |
| 261 FreeLibrary(module_handle); | |
| 262 } | 274 } |
| 263 | 275 |
| 264 return error; | 276 return error; |
| 265 } | 277 } |
| 266 | 278 |
| 267 DWORD IATPatchFunction::Unpatch() { | 279 DWORD IATPatchFunction::Unpatch() { |
| 268 DWORD error = RestoreImportedFunction(intercept_function_, | 280 DWORD error = RestoreImportedFunction(intercept_function_, |
| 269 original_function_, | 281 original_function_, |
| 270 iat_thunk_); | 282 iat_thunk_); |
| 271 DCHECK_EQ(static_cast<DWORD>(NO_ERROR), error); | 283 DCHECK_EQ(static_cast<DWORD>(NO_ERROR), error); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 286 return error; | 298 return error; |
| 287 } | 299 } |
| 288 | 300 |
| 289 void* IATPatchFunction::original_function() const { | 301 void* IATPatchFunction::original_function() const { |
| 290 DCHECK(is_patched()); | 302 DCHECK(is_patched()); |
| 291 return original_function_; | 303 return original_function_; |
| 292 } | 304 } |
| 293 | 305 |
| 294 } // namespace win | 306 } // namespace win |
| 295 } // namespace base | 307 } // namespace base |
| OLD | NEW |