blob: c070211eb238ad9000b386da6d4456f7e654ad44 [file] [log] [blame]
[email protected]f7817822009-09-24 05:11:581// Copyright (c) 2009 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#include "chrome_frame/vtable_patch_manager.h"
6
[email protected]3f55e872009-10-17 04:48:377#include <algorithm>
8
[email protected]f7817822009-09-24 05:11:589#include "base/logging.h"
[email protected]3f55e872009-10-17 04:48:3710#include "base/scoped_ptr.h"
[email protected]f7817822009-09-24 05:11:5811
12#include "chrome_frame/function_stub.h"
13
14namespace vtable_patch {
15
16// Convenient definition of a VTABLE
17typedef PROC* Vtable;
18
19// Returns a pointer to the VTable of a COM interface.
20// @param unknown [in] The pointer of the COM interface.
21inline Vtable GetIFVTable(void* unknown) {
22 return reinterpret_cast<Vtable>(*reinterpret_cast<void**>(unknown));
23}
24
25HRESULT PatchInterfaceMethods(void* unknown, MethodPatchInfo* patches) {
26 // Do some sanity checking of the input arguments.
27 if (NULL == unknown || NULL == patches) {
28 NOTREACHED();
29 return E_INVALIDARG;
30 }
31
32 Vtable vtable = GetIFVTable(unknown);
33 DCHECK(vtable);
34
35 for (MethodPatchInfo* it = patches; it->index_ != -1; ++it) {
[email protected]bc88e9d72009-09-25 16:04:4336 if (it->stub_ != NULL) {
37 // If this DCHECK fires it means that we are using the same VTable
38 // information to patch two different interfaces.
39 DCHECK(false);
40 DLOG(ERROR) << "Attempting to patch two different VTables with the "
41 << "same VTable information";
42 continue;
43 }
44
[email protected]f7817822009-09-24 05:11:5845 PROC original_fn = vtable[it->index_];
46 FunctionStub* stub = FunctionStub::FromCode(original_fn);
47 if (stub != NULL) {
48 DLOG(ERROR) << "attempt to patch a function that's already patched";
49 DCHECK(stub->absolute_target() ==
50 reinterpret_cast<uintptr_t>(it->method_)) <<
51 "patching the same method multiple times with different hooks?";
52 continue;
53 }
54
55 stub = FunctionStub::Create(reinterpret_cast<uintptr_t>(original_fn),
56 it->method_);
57 if (!stub) {
58 NOTREACHED();
59 return E_OUTOFMEMORY;
60 } else {
61 DWORD protect = 0;
62 if (::VirtualProtect(&vtable[it->index_], sizeof(PROC),
63 PAGE_EXECUTE_READWRITE, &protect)) {
64 it->stub_ = stub; // save the stub
65 vtable[it->index_] = stub->code();
66 ::VirtualProtect(&vtable[it->index_], sizeof(PROC), protect,
67 &protect);
68 } else {
69 NOTREACHED();
70 }
71 }
72 }
73
74 return S_OK;
75}
76
77HRESULT UnpatchInterfaceMethods(MethodPatchInfo* patches) {
78 for (MethodPatchInfo* it = patches; it->index_ != -1; ++it) {
79 if (it->stub_) {
[email protected]bc88e9d72009-09-25 16:04:4380 DCHECK(it->stub_->absolute_target() ==
[email protected]f7817822009-09-24 05:11:5881 reinterpret_cast<uintptr_t>(it->method_));
82 // Modify the stub to just jump directly to the original function.
83 it->stub_->BypassStub(reinterpret_cast<void*>(it->stub_->argument()));
84 it->stub_ = NULL;
85 // Leave the stub in memory so that we won't break any possible chains.
86 } else {
87 DLOG(WARNING) << "attempt to unpatch a function that wasn't patched";
88 }
89 }
90
91 return S_OK;
92}
93
[email protected]3f55e872009-10-17 04:48:3794// Disabled for now as we're not using it atm.
95#if 0
96
97DynamicPatchManager::DynamicPatchManager(const MethodPatchInfo* patch_prototype)
98 : patch_prototype_(patch_prototype) {
99 DCHECK(patch_prototype_);
100 DCHECK(patch_prototype_->stub_ == NULL);
101}
102
103DynamicPatchManager::~DynamicPatchManager() {
104 UnpatchAll();
105}
106
107HRESULT DynamicPatchManager::PatchObject(void* unknown) {
108 int patched_methods = 0;
109 for (; patch_prototype_[patched_methods].index_ != -1; patched_methods++) {
110 // If you hit this, then you are likely using the prototype instance for
111 // patching in _addition_ to this class. This is not a good idea :)
112 DCHECK(patch_prototype_[patched_methods].stub_ == NULL);
113 }
114
115 // Prepare a new patch object using the patch info from the prototype.
116 int mem_size = sizeof(PatchedObject) +
117 sizeof(MethodPatchInfo) * patched_methods;
118 PatchedObject* entry = reinterpret_cast<PatchedObject*>(new char[mem_size]);
119 entry->vtable_ = GetIFVTable(unknown);
120 memcpy(entry->patch_info_, patch_prototype_,
121 sizeof(MethodPatchInfo) * (patched_methods + 1));
122
123 patch_list_lock_.Acquire();
124
125 // See if we've already patched this vtable before.
126 // The search is done via the == operator of the PatchedObject class.
127 PatchList::const_iterator it = std::find(patch_list_.begin(),
128 patch_list_.end(), entry);
129 HRESULT hr;
130 if (it == patch_list_.end()) {
131 hr = PatchInterfaceMethods(unknown, entry->patch_info_);
132 if (SUCCEEDED(hr)) {
133 patch_list_.push_back(entry);
134 entry = NULL; // Ownership transferred to the array.
135 }
136 } else {
137 hr = S_FALSE;
138 }
139
140 patch_list_lock_.Release();
141
142 delete entry;
143
144 return hr;
145}
146
147bool DynamicPatchManager::UnpatchAll() {
148 patch_list_lock_.Acquire();
149 PatchList::iterator it;
150 for (it = patch_list_.begin(); it != patch_list_.end(); it++) {
151 UnpatchInterfaceMethods((*it)->patch_info_);
152 delete (*it);
153 }
154 patch_list_.clear();
155 patch_list_lock_.Release();
156
157 return true;
158}
159
160#endif // disabled DynamicPatchManager
161
[email protected]f7817822009-09-24 05:11:58162} // namespace vtable_patch