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 class DerivedDerived : public Derived { };
52 SH_DECL_HOOK0_void(Derived, Func1, SH_NOATTRIB, 0);
53 SH_DECL_HOOK0_void(Derived, Func2, SH_NOATTRIB, 0);
54 SH_DECL_HOOK0_void(Derived, Func3, SH_NOATTRIB, 0);
55 SH_DECL_HOOK0_void(Base1, Func1, SH_NOATTRIB, 0);
56 SH_DECL_HOOK0_void(Base2, Func2, SH_NOATTRIB, 0);
60 ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
64 ADD_STATE(State_Func2H_Called(META_IFACEPTR(void)));
68 ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
72 bool TestThisPtrOffs(std::string &error)
87 // Get a callclass for pD
88 // Verify whether the this pointers are correct
89 // Also call them normally to make sure that we aren't messing it up ;)
91 SH_CALL(pD, &Derived::Func1)();
92 SH_CALL(pD, &Derived::Func2)();
93 SH_CALL(pD, &Derived::Func3)();
98 CHECK_STATES((&g_States,
99 new State_Func1_Called(pB1),
100 new State_Func2_Called(pB2),
101 new State_Func3_Called(pD),
102 new State_Func1_Called(pB1),
103 new State_Func2_Called(pB2),
104 new State_Func3_Called(pD),
107 SH_CALL(pD, &Base1::Func1)();
108 SH_CALL(pD, &Base2::Func2)();
110 CHECK_STATES((&g_States,
111 new State_Func1_Called(pB1),
112 new State_Func2_Called(pB2),
116 // Get callclasses for the other ones and verify it as well
118 SH_CALL(pB1, &Base1::Func1)();
119 SH_CALL(pB2, &Base2::Func2)();
121 CHECK_STATES((&g_States,
122 new State_Func1_Called(pB1),
123 new State_Func2_Called(pB2),
127 // 3) Add hooks on them (referring to them through pD1 / Derived)
128 // Check whether the hooks are called with the correct this pointer
130 SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
131 SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
132 SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
140 // The handlers should always be called with the pointer to Derived
141 CHECK_STATES((&g_States,
142 new State_Func1H_Called(pD),
143 new State_Func1_Called(pB1),
144 new State_Func2H_Called(pD),
145 new State_Func2_Called(pB2),
146 new State_Func3H_Called(pD),
147 new State_Func3_Called(pD),
148 new State_Func1H_Called(pD),
149 new State_Func1_Called(pB1),
150 new State_Func2H_Called(pD),
151 new State_Func2_Called(pB2),
154 SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
155 SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
156 SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
159 // Now add the hooks on Base1 and Base2 and check again
161 // Note that the new implicit_cast should convert the pD to Base1*/Base2* :)
162 SH_ADD_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
163 SH_ADD_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
164 SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
172 // This time, the handlers for Func1 should be called with pB1 and the handlers
173 // for Func2 should be called with pB2
174 CHECK_STATES((&g_States,
175 new State_Func1H_Called(pB1),
176 new State_Func1_Called(pB1),
177 new State_Func2H_Called(pB2),
178 new State_Func2_Called(pB2),
179 new State_Func3H_Called(pD),
180 new State_Func3_Called(pD),
181 new State_Func1H_Called(pB1),
182 new State_Func1_Called(pB1),
183 new State_Func2H_Called(pB2),
184 new State_Func2_Called(pB2),
187 SH_REMOVE_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
188 SH_REMOVE_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
189 SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
193 // Add some hooks, and use callclasses
195 // 5.1) First off, add all of them on pD
196 SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
197 SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
198 SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
204 CHECK_STATES((&g_States,
205 new State_Func1H_Called(pD),
206 new State_Func1_Called(pB1),
207 new State_Func2H_Called(pD),
208 new State_Func2_Called(pB2),
209 new State_Func3H_Called(pD),
210 new State_Func3_Called(pD),
213 SH_CALL(pD, &Derived::Func1)();
214 SH_CALL(pD, &Derived::Func2)();
215 SH_CALL(pD, &Derived::Func3)();
216 SH_CALL(pB1, &Base1::Func1)();
217 SH_CALL(pB2, &Base2::Func2)();
219 CHECK_STATES((&g_States,
220 new State_Func1_Called(pB1),
221 new State_Func2_Called(pB2),
222 new State_Func3_Called(pD),
223 new State_Func1_Called(pB1),
224 new State_Func2_Called(pB2),
227 SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
228 SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
229 SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);