2 #include "sourcehook.h"
3 #include "sourcehook_test.h"
4 #include "testevents.h"
7 // Test vfnptr-wide hooks
8 // Also contains a test for removing hooks on deleted instances (by id)
13 SourceHook::ISourceHook *g_SHPtr;
14 SourceHook::Plugin g_PLID;
18 MAKE_STATE_1(State_D1_Func1, IBase *);
19 MAKE_STATE_1(State_D2_Func1, IBase *);
20 MAKE_STATE_1(State_Func1_Pre, IBase *);
21 MAKE_STATE_1(State_Func1_Post, IBase *);
23 MAKE_STATE_1(State_D1_Func2, IBase *);
24 MAKE_STATE_1(State_D2_Func2, IBase *);
25 MAKE_STATE_1(State_Func2_Pre, IBase *);
26 MAKE_STATE_1(State_Func2_Post, IBase *);
28 MAKE_STATE_2(State_D1_Func3, IBase *, int);
29 MAKE_STATE_2(State_D2_Func3, IBase *, int);
30 MAKE_STATE_2(State_Func3_Pre, IBase *, int);
31 MAKE_STATE_2(State_Func3_Post, IBase *, int);
36 virtual void Func1() = 0;
37 virtual void Func2() = 0;
38 virtual void Func3(int x) = 0;
41 class CDerived1 : public IBase
46 ADD_STATE(State_D1_Func1(this));
50 ADD_STATE(State_D1_Func2(this));
52 virtual void Func3(int x)
54 ADD_STATE(State_D1_Func3(this, x));
58 class CDerived2 : public IBase
63 ADD_STATE(State_D2_Func1(this));
67 ADD_STATE(State_D2_Func2(this));
69 virtual void Func3(int x)
71 ADD_STATE(State_D2_Func3(this, x));
75 void Handler_Func1_Pre()
77 ADD_STATE(State_Func1_Pre(META_IFACEPTR(IBase)));
79 void Handler_Func1_Post()
81 ADD_STATE(State_Func1_Post(META_IFACEPTR(IBase)));
83 int g_F2_Pre_HookToRemove = 0;
84 void Handler_Func2_Pre()
86 ADD_STATE(State_Func2_Pre(META_IFACEPTR(IBase)));
87 SH_REMOVE_HOOK_ID(g_F2_Pre_HookToRemove);
89 void Handler_Func2_Post()
91 ADD_STATE(State_Func2_Post(META_IFACEPTR(IBase)));
95 void Handler_Func3_Pre(int x)
97 ADD_STATE(State_Func3_Pre(META_IFACEPTR(IBase), x));
99 RETURN_META_NEWPARAMS(MRES_IGNORED, &IBase::Func3, (x+1));
101 void Handler_Func3_Post(int x)
103 ADD_STATE(State_Func3_Post(META_IFACEPTR(IBase), x));
106 SH_DECL_HOOK0_void(IBase, Func1, SH_NOATTRIB, 0);
107 SH_DECL_HOOK0_void(IBase, Func2, SH_NOATTRIB, 0);
108 SH_DECL_HOOK1_void(IBase, Func3, SH_NOATTRIB, 0, int);
110 SH_DECL_MANUALHOOK1_void(IBase_Func3_Manual, 2, 0, 0, int);
113 bool TestVPHooks(std::string &error)
122 IBase *p_d1i1 = &d1i1;
123 IBase *p_d1i2 = &d1i2;
124 IBase *p_d2i1 = &d2i1;
126 int hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
132 CHECK_STATES((&g_States,
133 new State_Func1_Pre(p_d1i1),
134 new State_D1_Func1(p_d1i1),
136 new State_Func1_Pre(p_d1i2),
137 new State_D1_Func1(p_d1i2),
139 new State_D2_Func1(p_d2i1),
142 SH_CALL(p_d1i1, &IBase::Func1)();
143 SH_CALL(p_d1i2, &IBase::Func1)();
144 SH_CALL(p_d2i1, &IBase::Func1)();
146 CHECK_STATES((&g_States,
147 new State_D1_Func1(p_d1i1),
149 new State_D1_Func1(p_d1i2),
151 new State_D2_Func1(p_d2i1),
154 SH_REMOVE_HOOK_ID(hook1);
160 CHECK_STATES((&g_States,
161 new State_D1_Func1(p_d1i1),
163 new State_D1_Func1(p_d1i2),
165 new State_D2_Func1(p_d2i1),
170 // Normal hook, then vp hook
172 int hook2 = SH_ADD_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
173 hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
179 CHECK_STATES((&g_States,
180 new State_Func1_Pre(p_d1i1),
181 new State_Func1_Pre(p_d1i1),
182 new State_D1_Func1(p_d1i1),
184 new State_Func1_Pre(p_d1i2),
185 new State_D1_Func1(p_d1i2),
187 new State_D2_Func1(p_d2i1),
190 SH_REMOVE_HOOK_ID(hook1);
196 CHECK_STATES((&g_States,
197 new State_Func1_Pre(p_d1i1),
198 new State_D1_Func1(p_d1i1),
200 new State_D1_Func1(p_d1i2),
202 new State_D2_Func1(p_d2i1),
205 SH_REMOVE_HOOK_ID(hook2);
211 CHECK_STATES((&g_States,
212 new State_D1_Func1(p_d1i1),
214 new State_D1_Func1(p_d1i2),
216 new State_D2_Func1(p_d2i1),
220 // Normal hook AND vp hook on Func2
221 // Func2's pre handler removes the VP hook. It has to work anyway :)
223 hook1 = SH_ADD_VPHOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
224 hook2 = SH_ADD_HOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
226 g_F2_Pre_HookToRemove = hook1;
230 CHECK_STATES((&g_States,
231 new State_Func2_Pre(p_d1i1),
232 new State_Func2_Pre(p_d1i1),
233 new State_D1_Func2(p_d1i1),
235 new State_Func2_Pre(p_d1i1),
236 new State_D1_Func2(p_d1i1),
240 SH_REMOVE_HOOK_ID(hook1);
243 // Using manualhook, VP
244 hook1 = SH_ADD_MANUALVPHOOK(IBase_Func3_Manual, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
247 hook2 = SH_ADD_VPHOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
250 int hook3 = SH_ADD_HOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
254 CHECK_STATES((&g_States,
255 new State_Func3_Pre(p_d1i1, 1), // manual vp hook
256 new State_Func3_Pre(p_d1i1, 2), // normal vp hook
257 new State_Func3_Pre(p_d1i1, 3), // normal non-vp hook
259 new State_D1_Func3(p_d1i1, 4), // function
265 CHECK_STATES((&g_States,
266 new State_Func3_Pre(p_d1i2, 1), // manual vp hook
267 new State_Func3_Pre(p_d1i2, 2), // normal vp hook
269 new State_D1_Func3(p_d1i2, 3), // function
273 SH_REMOVE_HOOK_ID(hook1);
274 SH_REMOVE_HOOK_ID(hook2);
275 SH_REMOVE_HOOK_ID(hook3);
277 // Test direct VP hook
282 // get vtable manually
283 void *pVtable = *reinterpret_cast<void**>(p_d1i1);
285 hook1 = SH_ADD_DVPHOOK(IBase, Func1, pVtable, SH_STATIC(Handler_Func1_Pre), false);
290 CHECK_STATES((&g_States,
291 new State_D1_Func1(p_d1i1),
292 new State_D1_Func1(p_d1i2),
294 new State_Func1_Pre(p_d1i1),
295 new State_D1_Func1(p_d1i1),
296 new State_Func1_Pre(p_d1i2),
297 new State_D1_Func1(p_d1i2),
300 SH_REMOVE_HOOK_ID(hook1);
302 // Now try manual dvp hooks
306 hook1 = SH_ADD_MANUALDVPHOOK(IBase_Func3_Manual, pVtable, SH_STATIC(Handler_Func3_Pre), false);
311 CHECK_STATES((&g_States,
312 new State_D1_Func3(p_d1i1, 1),
313 new State_D1_Func3(p_d1i2, 1),
315 new State_Func3_Pre(p_d1i1, 1), // manual vp hook
316 new State_D1_Func3(p_d1i1, 2), // function
318 new State_Func3_Pre(p_d1i2, 1), // normal vp hook
319 new State_D1_Func3(p_d1i2, 2), // function
324 // Test removing normal hooks even though the instance is deleted
325 IBase *pOther = new CDerived1;
326 SH_ADD_HOOK(IBase, Func1, pOther, SH_STATIC(Handler_Func1_Pre), false);
328 // The following line may not crash!
329 SH_REMOVE_HOOK(IBase, Func1, pOther, SH_STATIC(Handler_Func1_Pre), false);
332 // Now test GetOrigVfnPtrEntry
333 void *origfuncptr = (*reinterpret_cast<void***>(p_d1i1))[0];
335 CHECK_COND(SH_GET_ORIG_VFNPTR_ENTRY(p_d1i1, &IBase::Func1) == origfuncptr, "Part 9.1");
337 SH_ADD_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
339 CHECK_COND(SH_GET_ORIG_VFNPTR_ENTRY(p_d1i1, &IBase::Func1) == origfuncptr, "Part 9.2");
341 SH_REMOVE_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
343 CHECK_COND(SH_GET_ORIG_VFNPTR_ENTRY(p_d1i1, &IBase::Func1) == origfuncptr, "Part 9.3");