OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // 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 | 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 | 5 |
6 #ifndef CHROME_FRAME_FUNCTION_STUB_H_ | 6 #ifndef CHROME_FRAME_FUNCTION_STUB_H_ |
7 #define CHROME_FRAME_FUNCTION_STUB_H_ | 7 #define CHROME_FRAME_FUNCTION_STUB_H_ |
8 | 8 |
9 #include <windows.h> | 9 #include <windows.h> |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 | 11 |
12 // IMPORTANT: The struct below must be byte aligned. | 12 // IMPORTANT: The struct below must be byte aligned. |
13 #pragma pack(push) | 13 #pragma pack(push) |
14 #pragma pack(1) | 14 #pragma pack(1) |
15 | 15 |
| 16 struct FunctionStubAsm { |
| 17 // The stub always starts with an indirect jump, which starts out jumping |
| 18 // to the remainder of the stub. This means we can bypass the stub by |
| 19 // rewriting the jump destination, which is data, in a manner that does |
| 20 // not involve writing code, only writing data at a natural word boundary. |
| 21 uint16 jump_to_bypass_; // indirect jump |
| 22 uintptr_t bypass_target_addr_; // to the bypass target. |
| 23 uint8 pop_return_addr_; // pop eax |
| 24 uint16 push_; // push [arg] ; push... |
| 25 uintptr_t arg_addr_; // ; extra argument |
| 26 uint8 push_return_addr_; // push eax ; push the return address |
| 27 uint16 jump_to_target; // jmp [target] ; jump... |
| 28 uintptr_t target_addr_; // ; to the hook function |
| 29 }; |
| 30 |
| 31 #pragma pack(pop) |
| 32 |
| 33 |
16 #ifndef _M_IX86 | 34 #ifndef _M_IX86 |
17 #error Only x86 supported right now. | 35 #error Only x86 supported right now. |
18 #endif | 36 #endif |
19 | 37 |
20 extern "C" IMAGE_DOS_HEADER __ImageBase; | |
21 | |
22 // This struct is assembly code + signature. The purpose of the struct is to be | 38 // This struct is assembly code + signature. The purpose of the struct is to be |
23 // able to hook an existing function with our own and store information such | 39 // able to hook an existing function with our own and store information such |
24 // as the original function pointer with the code stub. Typically this is used | 40 // as the original function pointer with the code stub. Typically this is used |
25 // for patching entries of a vtable or e.g. a globally registered wndproc | 41 // for patching entries of a vtable or e.g. a globally registered wndproc |
26 // for a class as opposed to a window. | 42 // for a class as opposed to a window. |
27 // When unhooking, you can just call the BypassStub() function and leave the | 43 // When unhooking, you can just call the BypassStub() function and leave the |
28 // stub in memory. This unhooks your function while leaving the (potential) | 44 // stub in memory. This unhooks your function while leaving the (potential) |
29 // chain of patches intact. | 45 // chain of patches intact. |
30 // | 46 // |
31 // @note: This class is meant for __stdcall calling convention and | 47 // @note: This class is meant for __stdcall calling convention and |
32 // it uses eax as a temporary variable. The struct can | 48 // it uses eax as a temporary variable. The struct can |
33 // be improved in the future to save eax before the | 49 // be improved in the future to save eax before the |
34 // operation and then restore it. | 50 // operation and then restore it. |
35 // | 51 // |
36 // For instance if the function prototype is: | 52 // For instance if the function prototype is: |
37 // | 53 // |
38 // @code | 54 // @code |
39 // LRESULT WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam); | 55 // LRESULT WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam); |
40 // @endcode | 56 // @endcode |
41 // | 57 // |
42 // and we would like to add one static argument to make it, say: | 58 // and we would like to add one static argument to make it, say: |
43 // | 59 // |
44 // @code | 60 // @code |
45 // LRESULT MyNewWndProc(WNDPROC original, HWND hwnd, UINT msg, | 61 // LRESULT MyNewWndProc(WNDPROC original, HWND hwnd, UINT msg, |
46 // WPARAM wparam, LPARAM lparam); | 62 // WPARAM wparam, LPARAM lparam); |
47 // @endcode | 63 // @endcode |
48 // | 64 // |
49 // That can be achieved by wrapping the function up with a FunctionStub: | 65 // That can be achieved by wrapping the function up with a FunctionStub: |
50 // | 66 // |
51 // @code | 67 // @code |
52 // FunctionStub* stub = FunctionStub::Create(original_wndproc, MyNewWndProc); | 68 // FunctionStub* stub = FunctionStub::Create(original_wndproc, MyNewWndProc); |
53 // SetClassLongPtr(wnd, GCLP_WNDPROC, stub->code()); | 69 // SetClassLongPtr(wnd, GCLP_WNDPROC, stub->code()); |
54 // @endcode | 70 // @endcode |
55 struct FunctionStub { | 71 struct FunctionStub { |
56 private: | 72 public: |
57 typedef enum AsmConstants { | 73 // Neutralizes this stub and converts it to a direct jump to a new target. |
58 POP_EAX = 0x58, | 74 void BypassStub(void* new_target); |
59 PUSH = 0x68, | |
60 PUSH_EAX = 0x50, | |
61 JUMP_RELATIVE = 0xE9 | |
62 }; | |
63 | 75 |
64 FunctionStub(uintptr_t extra_argument, void* dest) | 76 inline bool is_bypassed() const { |
65 : signature_(reinterpret_cast<HMODULE>(&__ImageBase)) { | 77 return bypass_address_ != |
66 Opcodes::Hook& hook = code_.hook_; | 78 reinterpret_cast<uintptr_t>(&stub_.pop_return_addr_); |
67 hook.pop_return_addr_ = POP_EAX; | |
68 hook.push_ = PUSH; | |
69 hook.arg_ = extra_argument; | |
70 hook.push_return_addr_ = PUSH_EAX; | |
71 hook.jump_to_ = JUMP_RELATIVE; | |
72 | |
73 // Calculate the target jump to the destination function. | |
74 hook.target_ = CalculateRelativeJump(dest, &hook.jump_to_); | |
75 | |
76 // Allow the process to execute this struct as code. | |
77 DWORD old_protect = 0; | |
78 // Allow reads too since we want read-only member variable access at | |
79 // all times. | |
80 ::VirtualProtect(this, sizeof(FunctionStub), PAGE_EXECUTE_READ, | |
81 &old_protect); | |
82 } | 79 } |
83 | 80 |
84 ~FunctionStub() { | 81 // Returns true if the stub is valid and enabled. |
85 // No more execution rights. | 82 // Don't call this method after bypassing the stub. |
86 DWORD old_protect = 0; | 83 bool is_valid() const; |
87 ::VirtualProtect(this, sizeof(FunctionStub), PAGE_READWRITE, &old_protect); | 84 |
| 85 inline PROC code() const { |
| 86 return reinterpret_cast<PROC>(const_cast<FunctionStubAsm*>(&stub_)); |
88 } | 87 } |
89 | 88 |
90 // Calculates the target value for a relative jump. | 89 // Use to create a new function stub as shown above. |
91 // The function assumes that the size of the opcode is 1 byte. | 90 // @param extra_argument The static argument to pass to the function. |
92 inline uintptr_t CalculateRelativeJump(const void* absolute_to, | 91 // @param dest Target function to which the stub applies. |
93 const void* absolute_from) const { | 92 // @returns NULL if an error occurs, otherwise a pointer to the |
94 return (reinterpret_cast<uintptr_t>(absolute_to) - | 93 // function stub. |
95 reinterpret_cast<uintptr_t>(absolute_from)) - | 94 static FunctionStub* Create(uintptr_t extra_argument, void* dest); |
96 (sizeof(uint8) + sizeof(uintptr_t)); | 95 |
| 96 // Test whether address (likely) points to an existing function stub. |
| 97 // @returns NULL if address does not point to a function stub. |
| 98 // @note likely means approximately 1/2^48 here. |
| 99 static FunctionStub* FromCode(void* address); |
| 100 |
| 101 // Deallocates a FunctionStub. |
| 102 // The stub must not be in use on any thread! |
| 103 static bool Destroy(FunctionStub* stub); |
| 104 |
| 105 // Accessors. |
| 106 uintptr_t argument() const { return argument_; } |
| 107 void set_argument(uintptr_t argument) { argument_ = argument; } |
| 108 |
| 109 uintptr_t bypass_address() const { return bypass_address_; } |
| 110 void set_bypass_address(uintptr_t bypass_address) { |
| 111 bypass_address_ = bypass_address; |
97 } | 112 } |
98 | 113 |
99 // Does the opposite of what CalculateRelativeJump does, which is to | 114 uintptr_t destination_function() const { return destination_function_; } |
100 // calculate back the absolute address that previously was relative to | 115 void set_destination_function(uintptr_t destination_function) { |
101 // some other address. | 116 destination_function_ = destination_function; |
102 inline uintptr_t CalculateAbsoluteAddress(const void* relative_to, | |
103 uintptr_t relative_address) const { | |
104 return relative_address + sizeof(uint8) + sizeof(uintptr_t) + | |
105 reinterpret_cast<uintptr_t>(relative_to); | |
106 } | 117 } |
107 | 118 |
| 119 protected: |
| 120 // Protected for testing only. |
| 121 FunctionStub(uintptr_t extra_argument, void* dest); |
| 122 ~FunctionStub(); |
| 123 |
| 124 void Init(FunctionStubAsm* stub); |
| 125 |
| 126 FunctionStubAsm stub_; |
| 127 |
108 // Used to identify function stubs that belong to this module. | 128 // Used to identify function stubs that belong to this module. |
109 HMODULE signature_; | 129 HMODULE signature_; |
110 | 130 |
111 // IMPORTANT: Do not change the order of member variables | 131 // This is the argument value that gets passed to the destination_function_. |
112 union Opcodes { | 132 uintptr_t argument_; |
113 // Use this struct when the stub forwards the call to our hook function | 133 // Bypass address, if this is the address of the pop_return_addr_, the |
114 // providing an extra argument. | 134 // function stub is not bypassed. |
115 struct Hook { | 135 uintptr_t bypass_address_; |
116 uint8 pop_return_addr_; // pop eax | 136 // The destination function we dispatch to, not used if the stub |
117 uint8 push_; // push arg ; push... | 137 // is bypassed. |
118 uintptr_t arg_; // ; extra argument | 138 uintptr_t destination_function_; |
119 uint8 push_return_addr_; // push eax ; push the return address | |
120 uint8 jump_to_; // jmp ; jump... | |
121 uintptr_t target_; // ; to the hook function | |
122 } hook_; | |
123 // When the stub is bypassed, we jump directly to a given target, | |
124 // usually the originally hooked function. | |
125 struct Bypassed { | |
126 uint8 jump_to_; // jmp to | |
127 uintptr_t target_; // relative target. | |
128 } bypassed_; | |
129 }; | |
130 | |
131 Opcodes code_; | |
132 | |
133 public: | |
134 // Neutralizes this stub and converts it to a direct jump to a new target. | |
135 void BypassStub(void* new_target) { | |
136 DWORD old_protect = 0; | |
137 // Temporarily allow us to write to member variables | |
138 ::VirtualProtect(this, sizeof(FunctionStub), PAGE_EXECUTE_READWRITE, | |
139 &old_protect); | |
140 | |
141 // Now, just change the first 5 bytes to jump directly to the new target. | |
142 Opcodes::Bypassed& bypassed = code_.bypassed_; | |
143 bypassed.jump_to_ = JUMP_RELATIVE; | |
144 bypassed.target_ = CalculateRelativeJump(new_target, &bypassed.jump_to_); | |
145 | |
146 // Restore the previous protection flags. | |
147 ::VirtualProtect(this, sizeof(FunctionStub), old_protect, &old_protect); | |
148 | |
149 // Flush the instruction cache just in case. | |
150 ::FlushInstructionCache(::GetCurrentProcess(), this, sizeof(FunctionStub)); | |
151 } | |
152 | |
153 // @returns the argument supplied in the call to @ref Create | |
154 inline uintptr_t argument() const { | |
155 DCHECK(code_.hook_.pop_return_addr_ == POP_EAX) << "stub has been disabled"; | |
156 return code_.hook_.arg_; | |
157 } | |
158 | |
159 inline bool is_bypassed() const { | |
160 return code_.bypassed_.jump_to_ == JUMP_RELATIVE; | |
161 } | |
162 | |
163 inline uintptr_t absolute_target() const { | |
164 DCHECK(code_.hook_.pop_return_addr_ == POP_EAX) << "stub has been disabled"; | |
165 return CalculateAbsoluteAddress( | |
166 reinterpret_cast<const void*>(&code_.hook_.jump_to_), | |
167 code_.hook_.target_); | |
168 } | |
169 | |
170 // Returns true if the stub is valid and enabled. | |
171 // Don't call this method after bypassing the stub. | |
172 inline bool is_valid() const { | |
173 return signature_ == reinterpret_cast<HMODULE>(&__ImageBase) && | |
174 code_.hook_.pop_return_addr_ == POP_EAX; | |
175 } | |
176 | |
177 inline PROC code() const { | |
178 return reinterpret_cast<PROC>(const_cast<Opcodes*>(&code_)); | |
179 } | |
180 | |
181 // Use to create a new function stub as shown above. | |
182 // | |
183 // @param extra_argument The static argument to pass to the function. | |
184 // @param dest Target function to which the stub applies. | |
185 // @returns NULL if an error occurs, otherwise a pointer to the | |
186 // function stub. | |
187 // | |
188 // TODO(tommi): Change this so that VirtualAlloc isn't called for | |
189 // every stub. Instead we should re-use each allocation for | |
190 // multiple stubs. In practice I'm guessing that there would | |
191 // only be one allocation per process, since each allocation actually | |
192 // allocates at least one page of memory (4K). Size of FunctionStub | |
193 // is 12 bytes, so one page could house 341 function stubs. | |
194 // When stubs are created frequently, VirtualAlloc could fail | |
195 // and last error is ERROR_NOT_ENOUGH_MEMORY (8). | |
196 static FunctionStub* Create(uintptr_t extra_argument, void* dest) { | |
197 DCHECK(dest); | |
198 | |
199 // Use VirtualAlloc to get memory for the stub. This gives us a | |
200 // whole page that we don't share with anyone else. | |
201 // Initially the memory must be READWRITE to allow for construction | |
202 // PAGE_EXECUTE is set in constructor. | |
203 FunctionStub* ret = reinterpret_cast<FunctionStub*>(VirtualAlloc(NULL, | |
204 sizeof(FunctionStub), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | |
205 | |
206 if (!ret) { | |
207 NOTREACHED(); | |
208 } else { | |
209 // Construct | |
210 ret->FunctionStub::FunctionStub(extra_argument, dest); | |
211 } | |
212 | |
213 return ret; | |
214 } | |
215 | |
216 static FunctionStub* FromCode(void* address) { | |
217 Opcodes* code = reinterpret_cast<Opcodes*>(address); | |
218 if (code->hook_.pop_return_addr_ == POP_EAX) { | |
219 FunctionStub* stub = reinterpret_cast<FunctionStub*>( | |
220 reinterpret_cast<uint8*>(address) - sizeof(HMODULE)); | |
221 if (stub->is_valid()) | |
222 return stub; | |
223 } | |
224 | |
225 return NULL; | |
226 } | |
227 | |
228 // Deallocates a FunctionStub. The stub must not be in use on any thread! | |
229 static bool Destroy(FunctionStub* stub) { | |
230 DCHECK(stub != NULL); | |
231 FunctionStub* to_free = reinterpret_cast<FunctionStub*>(stub); | |
232 to_free->FunctionStub::~FunctionStub(); | |
233 BOOL success = VirtualFree(to_free, sizeof(FunctionStub), MEM_DECOMMIT); | |
234 DCHECK(success) << "VirtualFree"; | |
235 return success != FALSE; | |
236 } | |
237 }; | 139 }; |
238 | 140 |
239 #pragma pack(pop) | |
240 | |
241 #endif // CHROME_FRAME_FUNCTION_STUB_H_ | 141 #endif // CHROME_FRAME_FUNCTION_STUB_H_ |
OLD | NEW |