1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/sourcehook/test/testoddthunks.cpp Sat Oct 16 23:21:58 2010 -0700
1.3 @@ -0,0 +1,163 @@
1.4 +#include <string>
1.5 +#include "sourcehook_test.h"
1.6 +#include "sh_pagealloc.h"
1.7 +#include "testevents.h"
1.8 +
1.9 +namespace
1.10 +{
1.11 + StateList g_States;
1.12 + SourceHook::ISourceHook *g_SHPtr;
1.13 + SourceHook::Plugin g_PLID;
1.14 +
1.15 + MAKE_STATE(State_Func_Called);
1.16 + MAKE_STATE(State_Pre1_Called);
1.17 + MAKE_STATE(State_Pre2_Called);
1.18 +
1.19 + int g_CallNumber;
1.20 +
1.21 + class Test
1.22 + {
1.23 + public:
1.24 + virtual void Func()
1.25 + {
1.26 + ADD_STATE(State_Func_Called);
1.27 + }
1.28 + };
1.29 +
1.30 + Test *g_pInst;
1.31 +
1.32 + void Handler_Func_Pre1()
1.33 + {
1.34 + ADD_STATE(State_Pre1_Called);
1.35 + }
1.36 +
1.37 + SH_DECL_HOOK0_void(Test, Func, SH_NOATTRIB, 0);
1.38 +
1.39 + void Handler_Func_Pre2()
1.40 + {
1.41 + ADD_STATE(State_Pre2_Called);
1.42 +
1.43 + ++g_CallNumber;
1.44 + if (g_CallNumber == 2)
1.45 + {
1.46 + // REMOVE ourselves
1.47 + SH_REMOVE_HOOK(Test, Func, g_pInst, SH_STATIC(Handler_Func_Pre1), false);
1.48 + SH_REMOVE_HOOK(Test, Func, g_pInst, SH_STATIC(Handler_Func_Pre2), false);
1.49 + }
1.50 +
1.51 + // Call again.
1.52 + g_pInst->Func();
1.53 + }
1.54 +
1.55 + Test *MyInstanceFactory()
1.56 + {
1.57 + return new Test;
1.58 + }
1.59 +
1.60 + SourceHook::CPageAlloc g_ThunkAllocator(2);
1.61 + void *g_OddThunkMemory = NULL;
1.62 + void *g_OddThunk = NULL;
1.63 +
1.64 + // Guarantees that Func()
1.65 + // has a vtable entry showing to an ODD address
1.66 + void PatchFuncWithOddThunk()
1.67 + {
1.68 + g_OddThunkMemory = g_ThunkAllocator.Alloc(10);
1.69 + unsigned char* base = reinterpret_cast<unsigned char*>(g_OddThunkMemory);
1.70 +
1.71 + if (((ptrdiff_t)base) & 1)
1.72 + {
1.73 + // ODD, ok.
1.74 + }
1.75 + else
1.76 + {
1.77 + // EVEN. make odd.
1.78 + base += 1;
1.79 + }
1.80 +
1.81 + // Get vtable entry pointer
1.82 + SourceHook::MemFuncInfo info = {true, -1, 0, 0};
1.83 + SourceHook::GetFuncInfo(g_pInst, &Test::Func, info);
1.84 +
1.85 + void *adjustediface = NULL;
1.86 + void **cur_vtptr = NULL;
1.87 + void **cur_vfnptr = NULL;
1.88 +
1.89 + adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(g_pInst) + info.thisptroffs);
1.90 +
1.91 + cur_vtptr = *reinterpret_cast<void***>(
1.92 + reinterpret_cast<char*>(adjustediface) + info.vtbloffs);
1.93 + cur_vfnptr = reinterpret_cast<void**>(cur_vtptr + info.vtblindex);
1.94 +
1.95 +
1.96 + // Original function
1.97 + void *origEntry = *cur_vfnptr;
1.98 +
1.99 + // Now generate the jump code
1.100 + g_ThunkAllocator.SetRW(g_OddThunkMemory);
1.101 +
1.102 + *(base + 0) = 0xE9; // offset jump, immediate operand
1.103 + ptrdiff_t *offsetAddr = reinterpret_cast<ptrdiff_t*>(base + 1);
1.104 +
1.105 + // destination = src + offset + 5
1.106 + // <=> offset = destination - src - 5
1.107 + *offsetAddr =
1.108 + (reinterpret_cast<unsigned char*>(origEntry) - base) - 5;
1.109 +
1.110 + g_ThunkAllocator.SetRE(g_OddThunkMemory);
1.111 +
1.112 + g_OddThunk = reinterpret_cast<void*>(base);
1.113 +
1.114 + // Now set the odd thunk as new vtable entry
1.115 + SourceHook::SetMemAccess((void*)cur_vfnptr, sizeof(void*), SH_MEM_READ | SH_MEM_WRITE);
1.116 + *cur_vfnptr = g_OddThunk;
1.117 + }
1.118 +
1.119 + void FreeOddThunk()
1.120 + {
1.121 + g_ThunkAllocator.Free(g_OddThunkMemory);
1.122 + g_OddThunk = NULL;
1.123 + }
1.124 +}
1.125 +
1.126 +bool TestOddThunks(std::string &error)
1.127 +{
1.128 + GET_SHPTR(g_SHPtr);
1.129 + g_PLID = 1337;
1.130 +
1.131 + g_CallNumber = 0;
1.132 +
1.133 + g_pInst = MyInstanceFactory();
1.134 +
1.135 + PatchFuncWithOddThunk();
1.136 +
1.137 + SH_ADD_HOOK(Test, Func, g_pInst, SH_STATIC(Handler_Func_Pre1), false);
1.138 + SH_ADD_HOOK(Test, Func, g_pInst, SH_STATIC(Handler_Func_Pre2), false);
1.139 +
1.140 + g_pInst->Func();
1.141 +
1.142 + CHECK_STATES((&g_States,
1.143 + // FIRST func() call
1.144 + new State_Pre1_Called(),
1.145 + new State_Pre2_Called(), // calls Func() again
1.146 +
1.147 + // SECOND func() call
1.148 + new State_Pre1_Called(),
1.149 + new State_Pre2_Called(), // removes hooks and calls Func() again
1.150 +
1.151 + // THIRD func() call
1.152 + new State_Func_Called(),
1.153 +
1.154 + // SECOND func() call
1.155 + new State_Func_Called(),
1.156 +
1.157 + // FIRST func() call
1.158 + new State_Func_Called(),
1.159 + NULL), "Part 1");
1.160 +
1.161 + delete g_pInst;
1.162 + FreeOddThunk();
1.163 +
1.164 + return true;
1.165 +}
1.166 +