OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome_frame/function_stub.h" |
| 6 |
| 7 #include "chrome_frame/test/chrome_frame_unittests.h" |
| 8 |
| 9 #define NO_INLINE __declspec(noinline) |
| 10 |
| 11 namespace { |
| 12 |
| 13 typedef int (__stdcall* FooPrototype)(); |
| 14 |
| 15 NO_INLINE int __stdcall Foo() { |
| 16 return 1; |
| 17 } |
| 18 |
| 19 NO_INLINE int __stdcall PatchedFoo(FooPrototype original) { |
| 20 return original() + 1; |
| 21 } |
| 22 |
| 23 } // end namespace |
| 24 |
| 25 TEST(PatchTests, FunctionStub) { |
| 26 EXPECT_EQ(Foo(), 1); |
| 27 // Create a function stub that calls PatchedFoo and supplies it with |
| 28 // a pointer to Foo. |
| 29 FunctionStub* stub = FunctionStub::Create(reinterpret_cast<uintptr_t>(&Foo), |
| 30 &PatchedFoo); |
| 31 EXPECT_TRUE(stub != NULL); |
| 32 // Call the stub as it were Foo(). The call should get forwarded to Foo(). |
| 33 FooPrototype patch = reinterpret_cast<FooPrototype>(stub->code()); |
| 34 EXPECT_EQ(patch(), 2); |
| 35 // Now neutralize the stub so that it calls Foo() directly without touching |
| 36 // PatchedFoo(). |
| 37 // stub->BypassStub(&Foo); |
| 38 stub->BypassStub(reinterpret_cast<void*>(stub->argument())); |
| 39 EXPECT_EQ(patch(), 1); |
| 40 // We're done with the stub. |
| 41 FunctionStub::Destroy(stub); |
| 42 } |
| 43 |
| 44 // Basic tests to check the validity of a stub. |
| 45 TEST(PatchTests, FunctionStubCompare) { |
| 46 EXPECT_EQ(Foo(), 1); |
| 47 |
| 48 // Detect the absence of a stub |
| 49 FunctionStub* stub = reinterpret_cast<FunctionStub*>(&Foo); |
| 50 EXPECT_FALSE(stub->is_valid()); |
| 51 |
| 52 stub = FunctionStub::Create(reinterpret_cast<uintptr_t>(&Foo), &PatchedFoo); |
| 53 EXPECT_TRUE(stub != NULL); |
| 54 EXPECT_TRUE(stub->is_valid()); |
| 55 |
| 56 FooPrototype patch = reinterpret_cast<FooPrototype>(stub->code()); |
| 57 EXPECT_EQ(patch(), 2); |
| 58 |
| 59 // See if we can get the correct absolute pointer to the hook function |
| 60 // back from the stub. |
| 61 EXPECT_EQ(stub->absolute_target(), reinterpret_cast<uintptr_t>(&PatchedFoo)); |
| 62 |
| 63 // Also verify that the argument being passed to the hook function is indeed |
| 64 // the pointer to the original function (again, absolute not relative). |
| 65 EXPECT_EQ(stub->argument(), reinterpret_cast<uintptr_t>(&Foo)); |
| 66 } |
OLD | NEW |