Fixed sample plugin makefiles for L4D Linux builds (
bug 4680, r=dvander).
2 #include "sourcehook_test.h"
3 #include "sh_pagealloc.h"
4 #include "testevents.h"
9 SourceHook::ISourceHook *g_SHPtr;
10 SourceHook::Plugin g_PLID;
12 MAKE_STATE(State_Func_Called);
13 MAKE_STATE(State_Pre1_Called);
14 MAKE_STATE(State_Pre2_Called);
23 ADD_STATE(State_Func_Called);
29 void Handler_Func_Pre1()
31 ADD_STATE(State_Pre1_Called);
34 SH_DECL_HOOK0_void(Test, Func, SH_NOATTRIB, 0);
36 void Handler_Func_Pre2()
38 ADD_STATE(State_Pre2_Called);
41 if (g_CallNumber == 2)
44 SH_REMOVE_HOOK(Test, Func, g_pInst, SH_STATIC(Handler_Func_Pre1), false);
45 SH_REMOVE_HOOK(Test, Func, g_pInst, SH_STATIC(Handler_Func_Pre2), false);
52 Test *MyInstanceFactory()
57 SourceHook::CPageAlloc g_ThunkAllocator(2);
58 void *g_OddThunkMemory = NULL;
59 void *g_OddThunk = NULL;
61 // Guarantees that Func()
62 // has a vtable entry showing to an ODD address
63 void PatchFuncWithOddThunk()
65 g_OddThunkMemory = g_ThunkAllocator.Alloc(10);
66 unsigned char* base = reinterpret_cast<unsigned char*>(g_OddThunkMemory);
68 if (((ptrdiff_t)base) & 1)
78 // Get vtable entry pointer
79 SourceHook::MemFuncInfo info = {true, -1, 0, 0};
80 SourceHook::GetFuncInfo(g_pInst, &Test::Func, info);
82 void *adjustediface = NULL;
83 void **cur_vtptr = NULL;
84 void **cur_vfnptr = NULL;
86 adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(g_pInst) + info.thisptroffs);
88 cur_vtptr = *reinterpret_cast<void***>(
89 reinterpret_cast<char*>(adjustediface) + info.vtbloffs);
90 cur_vfnptr = reinterpret_cast<void**>(cur_vtptr + info.vtblindex);
94 void *origEntry = *cur_vfnptr;
96 // Now generate the jump code
97 g_ThunkAllocator.SetRW(g_OddThunkMemory);
99 *(base + 0) = 0xE9; // offset jump, immediate operand
100 ptrdiff_t *offsetAddr = reinterpret_cast<ptrdiff_t*>(base + 1);
102 // destination = src + offset + 5
103 // <=> offset = destination - src - 5
105 (reinterpret_cast<unsigned char*>(origEntry) - base) - 5;
107 g_ThunkAllocator.SetRE(g_OddThunkMemory);
109 g_OddThunk = reinterpret_cast<void*>(base);
111 // Now set the odd thunk as new vtable entry
112 SourceHook::SetMemAccess((void*)cur_vfnptr, sizeof(void*), SH_MEM_READ | SH_MEM_WRITE);
113 *cur_vfnptr = g_OddThunk;
118 g_ThunkAllocator.Free(g_OddThunkMemory);
123 bool TestOddThunks(std::string &error)
130 g_pInst = MyInstanceFactory();
132 PatchFuncWithOddThunk();
134 SH_ADD_HOOK(Test, Func, g_pInst, SH_STATIC(Handler_Func_Pre1), false);
135 SH_ADD_HOOK(Test, Func, g_pInst, SH_STATIC(Handler_Func_Pre2), false);
139 CHECK_STATES((&g_States,
141 new State_Pre1_Called(),
142 new State_Pre2_Called(), // calls Func() again
144 // SECOND func() call
145 new State_Pre1_Called(),
146 new State_Pre2_Called(), // removes hooks and calls Func() again
149 new State_Func_Called(),
151 // SECOND func() call
152 new State_Func_Called(),
155 new State_Func_Called(),