1.1 --- a/sourcehook/test/testvphooks.cpp Sun Oct 07 03:12:19 2007 +0000
1.2 +++ b/sourcehook/test/testvphooks.cpp Sun Oct 07 22:10:07 2007 +0000
1.3 @@ -5,7 +5,6 @@
1.4
1.5 // TEST VP HOOKS
1.6 // Test vfnptr-wide hooks
1.7 -// Also contains a test for removing hooks on deleted instances (by id)
1.8
1.9 namespace
1.10 {
1.11 @@ -31,76 +30,76 @@
1.12 MAKE_STATE_2(State_Func3_Post, IBase *, int);
1.13
1.14 class IBase
1.15 - {
1.16 - public:
1.17 - virtual void Func1() = 0;
1.18 - virtual void Func2() = 0;
1.19 - virtual void Func3(int x) = 0;
1.20 + {
1.21 + public:
1.22 + virtual void Func1() = 0;
1.23 + virtual void Func2() = 0;
1.24 + virtual void Func3(int x) = 0;
1.25 };
1.26
1.27 class CDerived1 : public IBase
1.28 - {
1.29 - public:
1.30 - virtual void Func1()
1.31 - {
1.32 - ADD_STATE(State_D1_Func1(this));
1.33 - }
1.34 - virtual void Func2()
1.35 - {
1.36 - ADD_STATE(State_D1_Func2(this));
1.37 - }
1.38 - virtual void Func3(int x)
1.39 - {
1.40 - ADD_STATE(State_D1_Func3(this, x));
1.41 - }
1.42 + {
1.43 + public:
1.44 + virtual void Func1()
1.45 + {
1.46 + ADD_STATE(State_D1_Func1(this));
1.47 + }
1.48 + virtual void Func2()
1.49 + {
1.50 + ADD_STATE(State_D1_Func2(this));
1.51 + }
1.52 + virtual void Func3(int x)
1.53 + {
1.54 + ADD_STATE(State_D1_Func3(this, x));
1.55 + }
1.56 };
1.57
1.58 class CDerived2 : public IBase
1.59 - {
1.60 - public:
1.61 - virtual void Func1()
1.62 - {
1.63 - ADD_STATE(State_D2_Func1(this));
1.64 - }
1.65 - virtual void Func2()
1.66 - {
1.67 - ADD_STATE(State_D2_Func2(this));
1.68 - }
1.69 - virtual void Func3(int x)
1.70 - {
1.71 - ADD_STATE(State_D2_Func3(this, x));
1.72 - }
1.73 + {
1.74 + public:
1.75 + virtual void Func1()
1.76 + {
1.77 + ADD_STATE(State_D2_Func1(this));
1.78 + }
1.79 + virtual void Func2()
1.80 + {
1.81 + ADD_STATE(State_D2_Func2(this));
1.82 + }
1.83 + virtual void Func3(int x)
1.84 + {
1.85 + ADD_STATE(State_D2_Func3(this, x));
1.86 + }
1.87 };
1.88
1.89 void Handler_Func1_Pre()
1.90 - {
1.91 - ADD_STATE(State_Func1_Pre(META_IFACEPTR(IBase)));
1.92 + {
1.93 + ADD_STATE(State_Func1_Pre(META_IFACEPTR(IBase)));
1.94 }
1.95 void Handler_Func1_Post()
1.96 - {
1.97 - ADD_STATE(State_Func1_Post(META_IFACEPTR(IBase)));
1.98 + {
1.99 + ADD_STATE(State_Func1_Post(META_IFACEPTR(IBase)));
1.100 }
1.101 int g_F2_Pre_HookToRemove = 0;
1.102 void Handler_Func2_Pre()
1.103 - {
1.104 - ADD_STATE(State_Func2_Pre(META_IFACEPTR(IBase)));
1.105 - SH_REMOVE_HOOK_ID(g_F2_Pre_HookToRemove);
1.106 + {
1.107 + ADD_STATE(State_Func2_Pre(META_IFACEPTR(IBase)));
1.108 + SH_REMOVE_HOOK_ID(g_F2_Pre_HookToRemove);
1.109 }
1.110 void Handler_Func2_Post()
1.111 - {
1.112 - ADD_STATE(State_Func2_Post(META_IFACEPTR(IBase)));
1.113 + {
1.114 + ADD_STATE(State_Func2_Post(META_IFACEPTR(IBase)));
1.115 }
1.116
1.117
1.118 void Handler_Func3_Pre(int x)
1.119 - {
1.120 - ADD_STATE(State_Func3_Pre(META_IFACEPTR(IBase), x));
1.121 -
1.122 - RETURN_META_NEWPARAMS(MRES_IGNORED, &IBase::Func3, (x+1));
1.123 + {
1.124 + ADD_STATE(State_Func3_Pre(META_IFACEPTR(IBase), x));
1.125 +
1.126 + RETURN_META_NEWPARAMS(MRES_IGNORED, &IBase::Func3, (x+1));
1.127 }
1.128 void Handler_Func3_Post(int x)
1.129 - {
1.130 - ADD_STATE(State_Func3_Post(META_IFACEPTR(IBase), x));
1.131 + {
1.132 + ADD_STATE(State_Func3_Post(META_IFACEPTR(IBase), x));
1.133 }
1.134
1.135 SH_DECL_HOOK0_void(IBase, Func1, SH_NOATTRIB, 0);
1.136 @@ -111,24 +110,24 @@
1.137 }
1.138
1.139 bool TestVPHooks(std::string &error)
1.140 -{
1.141 +{
1.142 GET_SHPTR(g_SHPtr);
1.143 - g_PLID = 1337;
1.144 -
1.145 - CDerived1 d1i1;
1.146 - CDerived1 d1i2;
1.147 - CDerived2 d2i1;
1.148 -
1.149 - IBase *p_d1i1 = &d1i1;
1.150 - IBase *p_d1i2 = &d1i2;
1.151 - IBase *p_d2i1 = &d2i1;
1.152 -
1.153 - int hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.154 -
1.155 - p_d1i1->Func1();
1.156 - p_d1i2->Func1();
1.157 - p_d2i1->Func1();
1.158 -
1.159 + g_PLID = 1337;
1.160 +
1.161 + CDerived1 d1i1;
1.162 + CDerived1 d1i2;
1.163 + CDerived2 d2i1;
1.164 +
1.165 + IBase *p_d1i1 = &d1i1;
1.166 + IBase *p_d1i2 = &d1i2;
1.167 + IBase *p_d2i1 = &d2i1;
1.168 +
1.169 + int hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.170 +
1.171 + p_d1i1->Func1();
1.172 + p_d1i2->Func1();
1.173 + p_d2i1->Func1();
1.174 +
1.175 CHECK_STATES((&g_States,
1.176 new State_Func1_Pre(p_d1i1),
1.177 new State_D1_Func1(p_d1i1),
1.178 @@ -137,45 +136,33 @@
1.179 new State_D1_Func1(p_d1i2),
1.180
1.181 new State_D2_Func1(p_d2i1),
1.182 - NULL), "Part 1");
1.183 -
1.184 - SH_CALL(p_d1i1, &IBase::Func1)();
1.185 - SH_CALL(p_d1i2, &IBase::Func1)();
1.186 - SH_CALL(p_d2i1, &IBase::Func1)();
1.187 -
1.188 + NULL), "Part 1");
1.189 +
1.190 + SH_REMOVE_HOOK_ID(hook1);
1.191 +
1.192 + p_d1i1->Func1();
1.193 + p_d1i2->Func1();
1.194 + p_d2i1->Func1();
1.195 +
1.196 CHECK_STATES((&g_States,
1.197 new State_D1_Func1(p_d1i1),
1.198
1.199 new State_D1_Func1(p_d1i2),
1.200
1.201 new State_D2_Func1(p_d2i1),
1.202 - NULL), "Part 1.1");
1.203 -
1.204 - SH_REMOVE_HOOK_ID(hook1);
1.205 -
1.206 - p_d1i1->Func1();
1.207 - p_d1i2->Func1();
1.208 - p_d2i1->Func1();
1.209 -
1.210 - CHECK_STATES((&g_States,
1.211 - new State_D1_Func1(p_d1i1),
1.212 -
1.213 - new State_D1_Func1(p_d1i2),
1.214 -
1.215 - new State_D2_Func1(p_d2i1),
1.216 - NULL), "Part 2");
1.217 -
1.218 -
1.219 -
1.220 - // Normal hook, then vp hook
1.221 -
1.222 - int hook2 = SH_ADD_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.223 - hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.224 -
1.225 - p_d1i1->Func1();
1.226 - p_d1i2->Func1();
1.227 - p_d2i1->Func1();
1.228 -
1.229 + NULL), "Part 2");
1.230 +
1.231 +
1.232 +
1.233 + // Normal hook, then vp hook
1.234 +
1.235 + int hook2 = SH_ADD_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.236 + hook1 = SH_ADD_VPHOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.237 +
1.238 + p_d1i1->Func1();
1.239 + p_d1i2->Func1();
1.240 + p_d2i1->Func1();
1.241 +
1.242 CHECK_STATES((&g_States,
1.243 new State_Func1_Pre(p_d1i1),
1.244 new State_Func1_Pre(p_d1i1),
1.245 @@ -185,14 +172,14 @@
1.246 new State_D1_Func1(p_d1i2),
1.247
1.248 new State_D2_Func1(p_d2i1),
1.249 - NULL), "Part 3");
1.250 -
1.251 - SH_REMOVE_HOOK_ID(hook1);
1.252 -
1.253 - p_d1i1->Func1();
1.254 - p_d1i2->Func1();
1.255 - p_d2i1->Func1();
1.256 -
1.257 + NULL), "Part 3");
1.258 +
1.259 + SH_REMOVE_HOOK_ID(hook1);
1.260 +
1.261 + p_d1i1->Func1();
1.262 + p_d1i2->Func1();
1.263 + p_d2i1->Func1();
1.264 +
1.265 CHECK_STATES((&g_States,
1.266 new State_Func1_Pre(p_d1i1),
1.267 new State_D1_Func1(p_d1i1),
1.268 @@ -200,57 +187,56 @@
1.269 new State_D1_Func1(p_d1i2),
1.270
1.271 new State_D2_Func1(p_d2i1),
1.272 - NULL), "Part 4");
1.273 -
1.274 - SH_REMOVE_HOOK_ID(hook2);
1.275 -
1.276 - p_d1i1->Func1();
1.277 - p_d1i2->Func1();
1.278 - p_d2i1->Func1();
1.279 -
1.280 + NULL), "Part 4");
1.281 +
1.282 + SH_REMOVE_HOOK_ID(hook2);
1.283 +
1.284 + p_d1i1->Func1();
1.285 + p_d1i2->Func1();
1.286 + p_d2i1->Func1();
1.287 +
1.288 CHECK_STATES((&g_States,
1.289 new State_D1_Func1(p_d1i1),
1.290
1.291 new State_D1_Func1(p_d1i2),
1.292
1.293 new State_D2_Func1(p_d2i1),
1.294 - NULL), "Part 5");
1.295 -
1.296 - // Test this:
1.297 - // Normal hook AND vp hook on Func2
1.298 - // Func2's pre handler removes the VP hook. It has to work anyway :)
1.299 -
1.300 - hook1 = SH_ADD_VPHOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
1.301 - hook2 = SH_ADD_HOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
1.302 -
1.303 - g_F2_Pre_HookToRemove = hook1;
1.304 - p_d1i1->Func2();
1.305 - p_d1i1->Func2();
1.306 -
1.307 + NULL), "Part 5");
1.308 +
1.309 + // Test this:
1.310 + // Normal hook AND vp hook on Func2
1.311 + // Func2's pre handler removes the VP hook.
1.312 +
1.313 + hook1 = SH_ADD_VPHOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
1.314 + hook2 = SH_ADD_HOOK(IBase, Func2, p_d1i1, SH_STATIC(Handler_Func2_Pre), false);
1.315 +
1.316 + g_F2_Pre_HookToRemove = hook1;
1.317 + p_d1i1->Func2();
1.318 + p_d1i1->Func2();
1.319 +
1.320 CHECK_STATES((&g_States,
1.321 new State_Func2_Pre(p_d1i1),
1.322 - new State_Func2_Pre(p_d1i1),
1.323 new State_D1_Func2(p_d1i1),
1.324
1.325 new State_Func2_Pre(p_d1i1),
1.326 new State_D1_Func2(p_d1i1),
1.327
1.328 - NULL), "Part 6");
1.329 -
1.330 - SH_REMOVE_HOOK_ID(hook1);
1.331 -
1.332 - // Hook function 3:
1.333 - // Using manualhook, VP
1.334 - hook1 = SH_ADD_MANUALVPHOOK(IBase_Func3_Manual, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
1.335 -
1.336 - // Normally, VP
1.337 - hook2 = SH_ADD_VPHOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
1.338 -
1.339 - // Normally, no VP
1.340 - int hook3 = SH_ADD_HOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
1.341 -
1.342 - p_d1i1->Func3(1);
1.343 -
1.344 + NULL), "Part 6");
1.345 +
1.346 + SH_REMOVE_HOOK_ID(hook1);
1.347 +
1.348 + // Hook function 3:
1.349 + // Using manualhook, VP
1.350 + hook1 = SH_ADD_MANUALVPHOOK(IBase_Func3_Manual, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
1.351 +
1.352 + // Normally, VP
1.353 + hook2 = SH_ADD_VPHOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
1.354 +
1.355 + // Normally, no VP
1.356 + int hook3 = SH_ADD_HOOK(IBase, Func3, p_d1i1, SH_STATIC(Handler_Func3_Pre), false);
1.357 +
1.358 + p_d1i1->Func3(1);
1.359 +
1.360 CHECK_STATES((&g_States,
1.361 new State_Func3_Pre(p_d1i1, 1), // manual vp hook
1.362 new State_Func3_Pre(p_d1i1, 2), // normal vp hook
1.363 @@ -258,90 +244,17 @@
1.364
1.365 new State_D1_Func3(p_d1i1, 4), // function
1.366
1.367 - NULL), "Part 7.1");
1.368 -
1.369 - p_d1i2->Func3(1);
1.370 -
1.371 + NULL), "Part 7.1");
1.372 +
1.373 + p_d1i2->Func3(1);
1.374 +
1.375 CHECK_STATES((&g_States,
1.376 new State_Func3_Pre(p_d1i2, 1), // manual vp hook
1.377 new State_Func3_Pre(p_d1i2, 2), // normal vp hook
1.378
1.379 new State_D1_Func3(p_d1i2, 3), // function
1.380
1.381 - NULL), "Part 7.2");
1.382 -
1.383 - SH_REMOVE_HOOK_ID(hook1);
1.384 - SH_REMOVE_HOOK_ID(hook2);
1.385 - SH_REMOVE_HOOK_ID(hook3);
1.386 -
1.387 - // Test direct VP hook
1.388 -
1.389 - p_d1i1->Func1();
1.390 - p_d1i2->Func1();
1.391 -
1.392 - // get vtable manually
1.393 - void *pVtable = *reinterpret_cast<void**>(p_d1i1);
1.394 -
1.395 - hook1 = SH_ADD_DVPHOOK(IBase, Func1, pVtable, SH_STATIC(Handler_Func1_Pre), false);
1.396 -
1.397 - p_d1i1->Func1();
1.398 - p_d1i2->Func1();
1.399 -
1.400 - CHECK_STATES((&g_States,
1.401 - new State_D1_Func1(p_d1i1),
1.402 - new State_D1_Func1(p_d1i2),
1.403 -
1.404 - new State_Func1_Pre(p_d1i1),
1.405 - new State_D1_Func1(p_d1i1),
1.406 - new State_Func1_Pre(p_d1i2),
1.407 - new State_D1_Func1(p_d1i2),
1.408 - NULL), "Part 8.1");
1.409 -
1.410 - SH_REMOVE_HOOK_ID(hook1);
1.411 -
1.412 - // Now try manual dvp hooks
1.413 - p_d1i1->Func3(1);
1.414 - p_d1i2->Func3(1);
1.415 -
1.416 - hook1 = SH_ADD_MANUALDVPHOOK(IBase_Func3_Manual, pVtable, SH_STATIC(Handler_Func3_Pre), false);
1.417 -
1.418 - p_d1i1->Func3(1);
1.419 - p_d1i2->Func3(1);
1.420 -
1.421 - CHECK_STATES((&g_States,
1.422 - new State_D1_Func3(p_d1i1, 1),
1.423 - new State_D1_Func3(p_d1i2, 1),
1.424 -
1.425 - new State_Func3_Pre(p_d1i1, 1), // manual vp hook
1.426 - new State_D1_Func3(p_d1i1, 2), // function
1.427 -
1.428 - new State_Func3_Pre(p_d1i2, 1), // normal vp hook
1.429 - new State_D1_Func3(p_d1i2, 2), // function
1.430 -
1.431 - NULL), "Part 8.2");
1.432 -
1.433 -
1.434 - // Test removing normal hooks even though the instance is deleted
1.435 - IBase *pOther = new CDerived1;
1.436 - SH_ADD_HOOK(IBase, Func1, pOther, SH_STATIC(Handler_Func1_Pre), false);
1.437 - delete pOther;
1.438 - // The following line may not crash!
1.439 - SH_REMOVE_HOOK(IBase, Func1, pOther, SH_STATIC(Handler_Func1_Pre), false);
1.440 -
1.441 -
1.442 - // Now test GetOrigVfnPtrEntry
1.443 - void *origfuncptr = (*reinterpret_cast<void***>(p_d1i1))[0];
1.444 -
1.445 - CHECK_COND(SH_GET_ORIG_VFNPTR_ENTRY(p_d1i1, &IBase::Func1) == origfuncptr, "Part 9.1");
1.446 -
1.447 - SH_ADD_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.448 -
1.449 - CHECK_COND(SH_GET_ORIG_VFNPTR_ENTRY(p_d1i1, &IBase::Func1) == origfuncptr, "Part 9.2");
1.450 -
1.451 - SH_REMOVE_HOOK(IBase, Func1, p_d1i1, SH_STATIC(Handler_Func1_Pre), false);
1.452 -
1.453 - CHECK_COND(SH_GET_ORIG_VFNPTR_ENTRY(p_d1i1, &IBase::Func1) == origfuncptr, "Part 9.3");
1.454 -
1.455 - return true;
1.456 -}
1.457 -
1.458 + NULL), "Part 7.2");
1.459 +
1.460 + return true;
1.461 +}
1.462 \ No newline at end of file