sh_stack now works if assert is disabled (stupid me), simple reentrancy test, META_IFACEPTR takes a type argument
2 #include "sourcehook_impl.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)));
70 bool TestThisPtrOffs(std::string &error)
72 SourceHook::CSourceHookImpl g_SHImpl;
86 // Get a callclass for pD
87 // Verify whether the this pointers are correct
88 // Also call them normally to make sure that we aren't messing it up ;)
89 SourceHook::CallClass<Derived> *pD_CC = SH_GET_CALLCLASS(pD);
91 SH_CALL(pD_CC, &Derived::Func1)();
92 SH_CALL(pD_CC, &Derived::Func2)();
93 SH_CALL(pD_CC, &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_CC, &Base1::Func1)();
108 SH_CALL(pD_CC, &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 SourceHook::CallClass<Base1> *pB1_CC = SH_GET_CALLCLASS(pB1);
119 SourceHook::CallClass<Base2> *pB2_CC = SH_GET_CALLCLASS(pB2);
121 SH_CALL(pB1_CC, &Base1::Func1)();
122 SH_CALL(pB2_CC, &Base2::Func2)();
124 CHECK_STATES((&g_States,
125 new State_Func1_Called(pB1),
126 new State_Func2_Called(pB2),
130 // 3) Add hooks on them (referring to them through pD1 / Derived)
131 // Check whether the hooks are called with the correct this pointer
133 SH_ADD_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
134 SH_ADD_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
135 SH_ADD_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
143 // The handlers should always be called with the pointer to Derived
144 CHECK_STATES((&g_States,
145 new State_Func1H_Called(pD),
146 new State_Func1_Called(pB1),
147 new State_Func2H_Called(pD),
148 new State_Func2_Called(pB2),
149 new State_Func3H_Called(pD),
150 new State_Func3_Called(pD),
151 new State_Func1H_Called(pD),
152 new State_Func1_Called(pB1),
153 new State_Func2H_Called(pD),
154 new State_Func2_Called(pB2),
157 SH_REMOVE_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
158 SH_REMOVE_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
159 SH_REMOVE_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
162 // Now add the hooks on Base1 and Base2 and check again
164 // Note that the new implicit_cast should convert the pD to Base1*/Base2* :)
165 SH_ADD_HOOK_STATICFUNC(Base1, Func1, pD, Handler_Func1, false);
166 SH_ADD_HOOK_STATICFUNC(Base2, Func2, pD, Handler_Func2, false);
167 SH_ADD_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
175 // This time, the handlers for Func1 should be called with pB1 and the handlers
176 // for Func2 should be called with pB2
177 CHECK_STATES((&g_States,
178 new State_Func1H_Called(pB1),
179 new State_Func1_Called(pB1),
180 new State_Func2H_Called(pB2),
181 new State_Func2_Called(pB2),
182 new State_Func3H_Called(pD),
183 new State_Func3_Called(pD),
184 new State_Func1H_Called(pB1),
185 new State_Func1_Called(pB1),
186 new State_Func2H_Called(pB2),
187 new State_Func2_Called(pB2),
190 SH_REMOVE_HOOK_STATICFUNC(Base1, Func1, pD, Handler_Func1, false);
191 SH_REMOVE_HOOK_STATICFUNC(Base2, Func2, pD, Handler_Func2, false);
192 SH_REMOVE_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
196 // Add some hooks, and use callclasses
198 // 5.1) First off, add all of them on pD
199 SH_ADD_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
200 SH_ADD_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
201 SH_ADD_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
207 CHECK_STATES((&g_States,
208 new State_Func1H_Called(pD),
209 new State_Func1_Called(pB1),
210 new State_Func2H_Called(pD),
211 new State_Func2_Called(pB2),
212 new State_Func3H_Called(pD),
213 new State_Func3_Called(pD),
216 SH_CALL(pD_CC, &Derived::Func1)();
217 SH_CALL(pD_CC, &Derived::Func2)();
218 SH_CALL(pD_CC, &Derived::Func3)();
219 SH_CALL(pB1_CC, &Base1::Func1)();
220 SH_CALL(pB2_CC, &Base2::Func2)();
222 CHECK_STATES((&g_States,
223 new State_Func1_Called(pB1),
224 new State_Func2_Called(pB2),
225 new State_Func3_Called(pD),
226 new State_Func1_Called(pB1),
227 new State_Func2_Called(pB2),
230 SH_REMOVE_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
231 SH_REMOVE_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
232 SH_REMOVE_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
234 SH_RELEASE_CALLCLASS(pB1_CC);
235 SH_RELEASE_CALLCLASS(pB2_CC);
236 SH_RELEASE_CALLCLASS(pD_CC);