2 #include "sourcehook_test.h"
3 #include "testevents.h"
6 // Tests with inheritance / thisptroffsets
11 SourceHook::ISourceHook *g_SHPtr;
12 SourceHook::Plugin g_PLID;
14 MAKE_STATE_1(State_Func1_Called, void*); // param1: This pointer
15 MAKE_STATE_1(State_Func2_Called, void*); // param1: This pointer
16 MAKE_STATE_1(State_Func3_Called, void*); // param1: This pointer
18 MAKE_STATE_1(State_Func1H_Called, void*);
19 MAKE_STATE_1(State_Func2H_Called, void*);
20 MAKE_STATE_1(State_Func3H_Called, void*);
28 ADD_STATE(State_Func1_Called(reinterpret_cast<void*>(this)));
37 ADD_STATE(State_Func2_Called(reinterpret_cast<void*>(this)));
40 class Derived : public Base1, public Base2
46 ADD_STATE(State_Func3_Called(reinterpret_cast<void*>(this)));
50 SH_DECL_HOOK0_void(Derived, Func1, SH_NOATTRIB, 0);
51 SH_DECL_HOOK0_void(Derived, Func2, SH_NOATTRIB, 0);
52 SH_DECL_HOOK0_void(Derived, Func3, SH_NOATTRIB, 0);
53 SH_DECL_HOOK0_void(Base1, Func1, SH_NOATTRIB, 0);
54 SH_DECL_HOOK0_void(Base2, Func2, SH_NOATTRIB, 0);
58 ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
62 ADD_STATE(State_Func2H_Called(META_IFACEPTR(void)));
66 ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
69 Derived *MyInstanceFactory()
75 bool TestThisPtrOffs(std::string &error)
80 Derived *pD = MyInstanceFactory();
84 CAutoPtrDestruction<Derived> apd(pD);
92 // Get a callclass for pD
93 // Verify whether the this pointers are correct
94 // Also call them normally to make sure that we aren't messing it up ;)
95 SourceHook::CallClass<Derived> *pD_CC = SH_GET_CALLCLASS(pD);
97 SH_CALL(pD_CC, &Derived::Func1)();
98 SH_CALL(pD_CC, &Derived::Func2)();
99 SH_CALL(pD_CC, &Derived::Func3)();
104 CHECK_STATES((&g_States,
105 new State_Func1_Called(pB1),
106 new State_Func2_Called(pB2),
107 new State_Func3_Called(pD),
108 new State_Func1_Called(pB1),
109 new State_Func2_Called(pB2),
110 new State_Func3_Called(pD),
113 SH_CALL(pD_CC, &Base1::Func1)();
114 SH_CALL(pD_CC, &Base2::Func2)();
116 CHECK_STATES((&g_States,
117 new State_Func1_Called(pB1),
118 new State_Func2_Called(pB2),
122 // Get callclasses for the other ones and verify it as well
124 SourceHook::CallClass<Base1> *pB1_CC = SH_GET_CALLCLASS(pB1);
125 SourceHook::CallClass<Base2> *pB2_CC = SH_GET_CALLCLASS(pB2);
127 SH_CALL(pB1_CC, &Base1::Func1)();
128 SH_CALL(pB2_CC, &Base2::Func2)();
130 CHECK_STATES((&g_States,
131 new State_Func1_Called(pB1),
132 new State_Func2_Called(pB2),
136 // 3) Add hooks on them (referring to them through pD1 / Derived)
137 // Check whether the hooks are called with the correct this pointer
139 SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
140 SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
141 SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
149 // The handlers should always be called with the pointer to Derived
150 CHECK_STATES((&g_States,
151 new State_Func1H_Called(pD),
152 new State_Func1_Called(pB1),
153 new State_Func2H_Called(pD),
154 new State_Func2_Called(pB2),
155 new State_Func3H_Called(pD),
156 new State_Func3_Called(pD),
157 new State_Func1H_Called(pD),
158 new State_Func1_Called(pB1),
159 new State_Func2H_Called(pD),
160 new State_Func2_Called(pB2),
163 SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
164 SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
165 SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
168 // Now add the hooks on Base1 and Base2 and check again
170 // Note that the new implicit_cast should convert the pD to Base1*/Base2* :)
171 SH_ADD_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
172 SH_ADD_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
173 SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
181 // This time, the handlers for Func1 should be called with pB1 and the handlers
182 // for Func2 should be called with pB2
183 CHECK_STATES((&g_States,
184 new State_Func1H_Called(pB1),
185 new State_Func1_Called(pB1),
186 new State_Func2H_Called(pB2),
187 new State_Func2_Called(pB2),
188 new State_Func3H_Called(pD),
189 new State_Func3_Called(pD),
190 new State_Func1H_Called(pB1),
191 new State_Func1_Called(pB1),
192 new State_Func2H_Called(pB2),
193 new State_Func2_Called(pB2),
196 SH_REMOVE_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
197 SH_REMOVE_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
198 SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
202 // Add some hooks, and use callclasses
204 // 5.1) First off, add all of them on pD
205 SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
206 SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
207 SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
213 CHECK_STATES((&g_States,
214 new State_Func1H_Called(pD),
215 new State_Func1_Called(pB1),
216 new State_Func2H_Called(pD),
217 new State_Func2_Called(pB2),
218 new State_Func3H_Called(pD),
219 new State_Func3_Called(pD),
222 SH_CALL(pD_CC, &Derived::Func1)();
223 SH_CALL(pD_CC, &Derived::Func2)();
224 SH_CALL(pD_CC, &Derived::Func3)();
225 SH_CALL(pB1_CC, &Base1::Func1)();
226 SH_CALL(pB2_CC, &Base2::Func2)();
228 CHECK_STATES((&g_States,
229 new State_Func1_Called(pB1),
230 new State_Func2_Called(pB2),
231 new State_Func3_Called(pD),
232 new State_Func1_Called(pB1),
233 new State_Func2_Called(pB2),
236 SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
237 SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
238 SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
240 SH_RELEASE_CALLCLASS(pB1_CC);
241 SH_RELEASE_CALLCLASS(pB2_CC);
242 SH_RELEASE_CALLCLASS(pD_CC);