2 #include "sourcehook.h"
3 #include "sourcehook_test.h"
4 #include "testevents.h"
5 #include "sourcehook_hookmangen.h"
7 #include "sh_pagealloc.h"
10 // Test automatic hookman generation
11 // Tests void and non-void functions
13 // integer-type, float-type, plain-old-data struct and objects with ctors/dtors
14 // both byval and byref
15 // also tests ignore/supercede
18 // :TODO: test override as well
23 SourceHook::ISourceHook *g_SHPtr;
24 SourceHook::Plugin g_PLID;
26 // PtrBuf(ptr) gives ptrs unique numbers
27 // in the order they appear
28 SourceHook::List<const void*> g_PtrHash;
30 bool g_Inside_LeafFunc = false; // inside a hook or a func
39 bool operator==(const POD<MYSIZE> &other)
41 return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
44 bool operator==(char other)
46 for (int i = 0; i < MYSIZE; ++i)
56 std::ostream& operator <<(std::ostream &os, const POD<MYSIZE> &obj)
62 MAKE_STATE_1(State_ObjOCtor_Called, int /*MYSIZE*/);
63 MAKE_STATE_1(State_ObjCCtor_Called, int /*MYSIZE*/);
64 MAKE_STATE_1(State_ObjODtor_Called, int /*MYSIZE*/);
65 MAKE_STATE_1(State_ObjAssignOp_Called, int /*MYSIZE*/);
74 memset(reinterpret_cast<void*>(x), initch, MYSIZE);
75 if (!g_Inside_LeafFunc)
76 ADD_STATE(State_ObjOCtor_Called(MYSIZE));
81 if (!g_Inside_LeafFunc)
82 ADD_STATE(State_ObjOCtor_Called(MYSIZE));
85 Object(const Object<MYSIZE> & other)
87 memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
88 if (!g_Inside_LeafFunc)
89 ADD_STATE(State_ObjCCtor_Called(MYSIZE));
94 if (!g_Inside_LeafFunc)
95 ADD_STATE(State_ObjODtor_Called(MYSIZE));
98 Object & operator = (const Object &other)
100 if (!g_Inside_LeafFunc)
101 ADD_STATE(State_ObjAssignOp_Called(MYSIZE));
103 memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
108 bool operator==(const Object<MYSIZE> &other)
110 return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
114 template <int MYSIZE>
115 std::ostream& operator <<(std::ostream &os, const Object<MYSIZE> &obj)
117 os << "Some Obj" << static_cast<int>(obj.x[0]);
121 // Because of some weird bug in MSVC < 1400
122 #define MAKE_PODRET(size) \
123 struct PodRet##size \
126 bool operator==(const PodRet##size &other) { return actPod == other.actPod; } \
127 bool operator==(char other) { return actPod == other; } \
129 std::ostream& operator <<(std::ostream &os, const PodRet##size &obj) \
134 template <> struct MakeRet< PodRet##size > \
136 static PodRet##size Do(int a) \
139 memset(reinterpret_cast<void*>(x.actPod.x), a, size); \
144 #define MAKE_OBJRET(size) \
145 struct ObjRet##size \
147 Object<size> actObj; \
148 bool operator==(const ObjRet##size &other) { return actObj == other.actObj; } \
149 bool operator==(char other) { return actObj == other; } \
151 std::ostream& operator <<(std::ostream &os, const ObjRet##size &obj) \
156 template <> struct MakeRet< ObjRet##size > \
158 static ObjRet##size Do(int a) NO_OPTIMIZE \
160 ObjRet##size *x = new ObjRet##size; /* ptr: otherwise gcc optimizes away the temp obj */ \
161 CAutoPtrDestruction< ObjRet##size > apd(x); \
162 memset(reinterpret_cast<void*>(x->actObj.x), a, size); \
169 // "Increment" something: (reproducible change for recall tests)
172 // pod/object: x[i]++, 0 <= i < MYSIZE
176 static void Incr(T &what)
183 struct Increment<float>
185 static void Incr(float &what)
192 struct Increment<double>
194 static void Incr(double &what)
201 struct Increment< POD<MYSIZE> >
203 static void Incr(POD<MYSIZE> &what)
205 for (int i = 0; i < MYSIZE; ++i)
211 struct Increment< Object<MYSIZE> >
213 static void Incr(Object<MYSIZE> &what)
215 for (int i = 0; i < MYSIZE; ++i)
220 #include "testhookmangen.h"
227 const void *vptr = reinterpret_cast<const void*>(ptr);
228 for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end(); ++iter)
235 g_PtrHash.push_back(vptr);
236 return static_cast<int>(g_PtrHash.size()) - 1;
246 void PtrBuf_Clear(int leave_in = 0)
248 for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end();)
251 iter = g_PtrHash.erase(iter);
258 // MyDelegate base class for other delegates
259 class MyDelegate : public SourceHook::ISHDelegate
262 // because we don't use old SH_REMOVE_HOOK syntax
263 virtual bool IsEqual(SourceHook::ISHDelegate *pOtherDeleg)
268 virtual void DeleteThis()
274 THGM_MAKE_TEST0_void(0);
277 THGM_MAKE_TEST1_void(1, char);
278 THGM_SETUP_PI1(1, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
280 THGM_MAKE_TEST1_void(2, short);
281 THGM_SETUP_PI1(2, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
283 THGM_MAKE_TEST1_void(3, int);
284 THGM_SETUP_PI1(3, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
286 THGM_MAKE_TEST1_void(4, float);
287 THGM_SETUP_PI1(4, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
289 THGM_MAKE_TEST1_void(5, double);
290 THGM_SETUP_PI1(5, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
292 THGM_MAKE_TEST5_void(6, char, short, int, float, double);
294 char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
295 short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
296 int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
297 float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal,
298 double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal
301 THGM_MAKE_TEST2_void(7, char&, double&);
303 char&, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByRef,
304 double&, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByRef
307 THGM_MAKE_TEST1_void(8, POD<7>);
308 THGM_SETUP_PI1(8, POD<7>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
310 THGM_MAKE_TEST1_void(9, POD<600>);
311 THGM_SETUP_PI1(9, POD<600>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
313 THGM_MAKE_TEST1_void(10, POD<600> &);
314 THGM_SETUP_PI1(10, POD<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef);
316 THGM_MAKE_TEST2_void(11, Object<3>, Object<600>&);
318 Object<3>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor,
319 Object<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor
322 THGM_MAKE_TEST0(101, char);
324 THGM_SETUP_RI(101, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
326 THGM_MAKE_TEST0(102, short);
328 THGM_SETUP_RI(102, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
330 THGM_MAKE_TEST0(103, int);
332 THGM_SETUP_RI(103, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
334 THGM_MAKE_TEST0(104, float);
336 THGM_SETUP_RI(104, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
338 THGM_MAKE_TEST0(105, double);
340 THGM_SETUP_RI(105, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
344 THGM_MAKE_TEST1(106, PodRet1, int);
346 int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
348 THGM_SETUP_RI(106, PodRet1, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
351 THGM_MAKE_TEST1(107, PodRet4, int);
353 int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
355 THGM_SETUP_RI(107, PodRet4, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
358 THGM_MAKE_TEST1(108, PodRet8, int);
360 int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
362 THGM_SETUP_RI(108, PodRet8, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
365 THGM_MAKE_TEST1(109, PodRet13, int);
367 int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
369 THGM_SETUP_RI(109, PodRet13, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
372 THGM_MAKE_TEST1(110, ObjRet13, int);
374 int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
376 THGM_SETUP_RI(110, ObjRet13, SourceHook::PassInfo::PassType_Object,
377 SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
378 SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
388 struct MakeRet< ObjRet111& >
390 static ObjRet111 &Do(int a)
408 THGM_MAKE_TEST0(111, ObjRet111& );
410 THGM_SETUP_RI(111, ObjRet111& , SourceHook::PassInfo::PassType_Object,
411 SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
412 SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
415 bool TestHookManGen(std::string &error)
420 // 5 Global constructors (g_O111_*)
421 CHECK_STATES((&g_States,
422 new State_ObjOCtor_Called(111),
423 new State_ObjOCtor_Called(111),
424 new State_ObjOCtor_Called(111),
425 new State_ObjOCtor_Called(111),
426 new State_ObjOCtor_Called(111),
429 THGM_DO_TEST_void(0, ());
431 THGM_DO_TEST_void(1, (100));
433 THGM_DO_TEST_void(2, (0x1F00));
435 THGM_DO_TEST_void(3, (0x1F000000));
437 THGM_DO_TEST_void(4, (0.5f));
439 THGM_DO_TEST_void(5, (5.5));
441 THGM_DO_TEST_void(6, (100, 0x1f00, 0x1f000000, 0.5f, 5.5));
445 THGM_DO_TEST_void(7, (a, b));
447 POD<7> pod7 = MakeRet< POD<7> >::Do(78);
448 THGM_DO_TEST_void(8, (pod7));
450 POD<600> pod600 = MakeRet< POD<600> >::Do(34);
451 THGM_DO_TEST_void(9, (pod600));
453 THGM_DO_TEST_void(10, (pod600));
455 // Test11: Special: constructors/destructors
458 Object<600> obj600(21);
460 CHECK_STATES((&g_States,
461 new State_ObjOCtor_Called(3),
462 new State_ObjOCtor_Called(600),
463 NULL), "Test11 Part0");
466 g_Genc11 = new SourceHook::Impl::GenContext(&protoinfo_11, 0, 0, g_SHPtr);
467 g_Genc_ad11.set(g_Genc11);
468 SourceHook::HookManagerPubFunc myhookman11 = g_Genc11->Generate();
469 int hook1_11, hook2_11, hook3_11, hook4_11;
471 TestClass11 *pTest11 = new TestClass11;
472 CAutoPtrDestruction<TestClass11> apd11(pTest11);
474 /* no hooks - no hooks */
476 pTest11->Func(obj3, obj600);
478 g_Inside_LeafFunc = true;
479 CHECK_STATES((&g_States,
480 new State_ObjCCtor_Called(3),
481 new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
482 new State_ObjODtor_Called(3),
483 NULL), "Test" "11" " Part1");
484 g_Inside_LeafFunc = false;
486 /* hook1 - no hooks */
487 THGM_ADD_HOOK(11, 1);
490 pTest11->Func(obj3, obj600);
491 g_Inside_LeafFunc = true;
492 CHECK_STATES((&g_States,
493 new State_ObjCCtor_Called(3),
495 new State_ObjCCtor_Called(3),
496 new State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
497 new State_ObjODtor_Called(3),
499 new State_ObjCCtor_Called(3),
500 new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
501 new State_ObjODtor_Called(3),
503 new State_ObjODtor_Called(3),
504 NULL), "Test" "11" " Part2");
505 g_Inside_LeafFunc = false;
507 THGM_REMOVE_HOOK(11, 1);
509 /* hook1, hook2 - hook3, hook4 */
510 THGM_ADD_HOOK(11, 1);
511 THGM_ADD_HOOK(11, 2);
512 THGM_ADD_HOOK(11, 3);
513 THGM_ADD_HOOK(11, 4);
515 pTest11->Func(obj3, obj600);
516 g_Inside_LeafFunc = true;
517 CHECK_STATES((&g_States,
518 new State_ObjCCtor_Called(3),
520 new State_ObjCCtor_Called(3),
521 new State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
522 new State_ObjODtor_Called(3),
524 new State_ObjCCtor_Called(3),
525 new State_Deleg2_11(pTest11, 1, ParamState_m11 (obj3, obj600)),
526 new State_ObjODtor_Called(3),
528 new State_ObjCCtor_Called(3),
529 new State_Deleg3_11(pTest11, 2, ParamState_m11 (obj3, obj600)),
530 new State_ObjODtor_Called(3),
532 new State_ObjCCtor_Called(3),
533 new State_Deleg4_11(pTest11, 3, ParamState_m11 (obj3, obj600)),
534 new State_ObjODtor_Called(3),
536 new State_ObjODtor_Called(3),
537 NULL), "Test" "11" " Part3");
538 g_Inside_LeafFunc = false;
540 /* hook1 - hook3, hook4 WITH RECALLS */
541 THGM_REMOVE_HOOK(11, 2);
543 TestClass11::ms_DoRecall = true;
544 pTest11->Func(obj3, obj600);
545 g_Inside_LeafFunc = true;
546 CHECK_STATES((&g_States,
547 new State_ObjCCtor_Called(3),
549 new State_ObjCCtor_Called(3),
550 new State_Deleg1_11(pTest11, 0 /* first deleg ptr */, ParamState_m11 (obj3, obj600)),
553 // second hookfunc -> new copy
554 new State_ObjCCtor_Called(3),
556 // in second hookfunc now
557 // -> calls orig func
559 new State_ObjCCtor_Called(3),
560 new State_Func11(pTest11, ParamState_m11 (obj3, obj600)(1)),
561 new State_ObjODtor_Called(3),
563 // calls first posthook
564 new State_ObjCCtor_Called(3),
565 new State_Deleg3_11(pTest11, 1 /* second deleg ptr */, ParamState_m11 (obj3, obj600)(1)),
568 // third hookfunc -> new copy
569 new State_ObjCCtor_Called(3),
571 // calls second posthook
573 new State_ObjCCtor_Called(3),
574 new State_Deleg4_11(pTest11, 2 /* third deleg ptr */, ParamState_m11 (obj3, obj600)(2)),
577 // fourth hookfunc -> new copy
578 new State_ObjCCtor_Called(3),
580 // has nothing to do though!
582 // fourth hookfunc done -> ret
583 new State_ObjODtor_Called(3),
585 // third hookfunc done -> ret
586 new State_ObjODtor_Called(3),
587 // ret from hookhandler which did the recall
588 new State_ObjODtor_Called(3),
590 // second hookfunc done -> ret
591 new State_ObjODtor_Called(3),
592 // ret from hookhandler which did the recall
593 new State_ObjODtor_Called(3),
596 new State_ObjODtor_Called(3),
597 // first hookfunc done -> ret
598 new State_ObjODtor_Called(3),
599 NULL), "Test" "11" " Part4");
600 g_Inside_LeafFunc = false;
602 THGM_REMOVE_HOOK(11, 1);
603 THGM_REMOVE_HOOK(11, 3);
604 THGM_REMOVE_HOOK(11, 4);
606 THGM_DO_TEST(101, ());
608 THGM_DO_TEST(102, ());
610 THGM_DO_TEST(103, ());
612 THGM_DO_TEST(104, ());
614 THGM_DO_TEST(105, ());
617 THGM_DO_TEST(106, (5));
618 THGM_DO_TEST(107, (5));
619 THGM_DO_TEST(108, (5));
620 THGM_DO_TEST(109, (5));
622 // Test110: Special: constructors/destructors on return
626 CHECK_STATES((&g_States,
627 new State_ObjOCtor_Called(13),
628 NULL), "Test110 Part0");
633 g_Genc110 = new SourceHook::Impl::GenContext(&protoinfo_110, 0, 0, g_SHPtr);
634 g_Genc_ad110.set(g_Genc110);
635 SourceHook::HookManagerPubFunc myhookman110 = g_Genc110->Generate();
636 int hook1_110, hook2_110, hook3_110, hook4_110;
638 TestClass110 *pTest110 = new TestClass110;
639 CAutoPtrDestruction<TestClass110> apd110(pTest110);
641 /* no hooks - no hooks */
643 obj13 = pTest110->Func(5);
645 g_Inside_LeafFunc = true;
646 CHECK_STATES((&g_States,
647 new State_Func110(pTest110, ParamState_m110 (5)),
648 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
649 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in our stack
650 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
652 new State_ObjAssignOp_Called(13), // assign: obj13 = temporary object in our stack
653 new State_ObjODtor_Called(13), // Func110: destruction of temporary object
654 NULL), "Test" "110" " Part1");
655 g_Inside_LeafFunc = false;
657 /* hook1 - no hooks */
658 THGM_ADD_HOOK(110, 1);
661 obj13 = pTest110->Func(5);
662 g_Inside_LeafFunc = true;
663 CHECK_STATES((&g_States,
664 // HookFunc: construct orig_ret/override_ret/plugin_ret objects
665 new State_ObjOCtor_Called(13),
666 new State_ObjOCtor_Called(13),
667 new State_ObjOCtor_Called(13),
670 new State_Deleg1_110(pTest110, 0, ParamState_m110 (5)),
672 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
673 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in HookFunc's stack
674 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
677 new State_ObjAssignOp_Called(13), // assign: plugin_ret = temporary object in HookFunc's stack
678 new State_ObjODtor_Called(13), // destruction of temporary object in HookFunc's stack
680 // Calling orig function Func110
681 new State_Func110(pTest110, ParamState_m110 (5)),
682 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
683 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in HookFunc's stack
684 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
687 new State_ObjAssignOp_Called(13), // assign: orig_ret = temporary object in HookFunc's stack
688 new State_ObjODtor_Called(13), // destruction of temporary object in HookFunc's stack
690 // hookfunc is returning:
691 new State_ObjCCtor_Called(13), // copy to temp object in our stack
693 // hookfunc cleans up its stack -> destroys plugin_ret/override_ret/orig_ret
694 new State_ObjODtor_Called(13),
695 new State_ObjODtor_Called(13),
696 new State_ObjODtor_Called(13),
698 // we are in our function: assign
699 new State_ObjAssignOp_Called(13), // assign: obj13 = temporary object in our stack
700 new State_ObjODtor_Called(13), // Func110: destruction of temporary object
702 NULL), "Test" "11" " Part2");
704 CHECK_COND(obj13 == 0, "Test" "11" " Part 2.1");
705 g_Inside_LeafFunc = false;
707 THGM_REMOVE_HOOK(11, 1);
709 /* hook1, hook2 - hook3, hook4 */
710 THGM_ADD_HOOK(110, 1);
711 THGM_ADD_HOOK(110, 2);
712 THGM_ADD_HOOK(110, 3);
713 THGM_ADD_HOOK(110, 4);
716 obj13 = pTest110->Func(5);
717 g_Inside_LeafFunc = true;
718 CHECK_STATES((&g_States,
719 // HookFunc: construct orig_ret/override_ret/plugin_ret objects
720 new State_ObjOCtor_Called(13),
721 new State_ObjOCtor_Called(13),
722 new State_ObjOCtor_Called(13),
725 new State_Deleg1_110(pTest110, 0, ParamState_m110 (5)),
727 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
728 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in HookFunc's stack
729 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
732 new State_ObjAssignOp_Called(13), // assign: plugin_ret = temporary object in HookFunc's stack
733 new State_ObjODtor_Called(13), // destruction of temporary object in HookFunc's stack
737 new State_Deleg2_110(pTest110, 1, ParamState_m110 (5)),
739 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
740 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in HookFunc's stack
741 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
744 new State_ObjAssignOp_Called(13), // assign: plugin_ret = temporary object in HookFunc's stack
745 new State_ObjODtor_Called(13), // destruction of temporary object in HookFunc's stack
747 // hookfunc finds out that the hook wanted to SUPERCEDE --> copy to override_Ret
748 new State_ObjAssignOp_Called(13),
750 // SUPERCEDE -> orig function is not called
751 // instead: orig_ret = override_ret
752 new State_ObjAssignOp_Called(13),
755 new State_Deleg3_110(pTest110, 2, ParamState_m110 (5)),
757 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
758 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in HookFunc's stack
759 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
762 new State_ObjAssignOp_Called(13), // assign: plugin_ret = temporary object in HookFunc's stack
763 new State_ObjODtor_Called(13), // destruction of temporary object in HookFunc's stack
766 new State_Deleg4_110(pTest110, 3, ParamState_m110 (5)),
768 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
769 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in HookFunc's stack
770 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
773 new State_ObjAssignOp_Called(13), // assign: plugin_ret = temporary object in HookFunc's stack
774 new State_ObjODtor_Called(13), // destruction of temporary object in HookFunc's stack
776 // hookfunc finds out that the hook wanted to SUPERCEDE --> copy to override_Ret (yes really, we overwrite the old value!)
777 new State_ObjAssignOp_Called(13),
779 // hookfunc is returning:
780 new State_ObjCCtor_Called(13), // copy to temp object in our stack
782 // hookfunc cleans up its stack -> destroys plugin_ret/override_ret/orig_ret
783 new State_ObjODtor_Called(13),
784 new State_ObjODtor_Called(13),
785 new State_ObjODtor_Called(13),
787 // we are in our function: assign
788 new State_ObjAssignOp_Called(13), // assign: obj13 = temporary object in our stack
789 new State_ObjODtor_Called(13), // Func110: destruction of temporary object
791 NULL), "Test" "11" " Part3");
793 CHECK_COND(obj13 == 4, "Test" "11" " Part 3.1");
794 g_Inside_LeafFunc = false;
796 THGM_REMOVE_HOOK(110, 1);
797 THGM_REMOVE_HOOK(110, 2);
798 THGM_REMOVE_HOOK(110, 3);
799 THGM_REMOVE_HOOK(110, 4);
803 THGM_DO_TEST(111, ());
806 // If we don't SH will auto-shutdown _after_ genc's destructor is called
809 Test_CompleteShutdown(g_SHPtr);
811 CHECK_COND(sizeof(PodRet1) == 1, "WTF!");
812 CHECK_COND(sizeof(PodRet4) == 4, "WTF!");
813 CHECK_COND(sizeof(PodRet8) == 8, "WTF!");
814 CHECK_COND(sizeof(PodRet13) == 13, "WTF!");
819 bool TestCPageAlloc(std::string &error)
821 using namespace SourceHook;
825 size_t ps = alloc.GetPageSize();
829 for (i = 0; i < 8; ++i)
830 test1[i] = (char*) alloc.Alloc(ps / 4);
832 CHECK_COND(test1[1] == test1[0] + ps/4, "Part 1.1");
833 CHECK_COND(test1[2] == test1[1] + ps/4, "Part 1.2");
834 CHECK_COND(test1[3] == test1[2] + ps/4, "Part 1.3");
836 CHECK_COND(test1[5] == test1[4] + ps/4, "Part 1.4");
837 CHECK_COND(test1[6] == test1[5] + ps/4, "Part 1.5");
838 CHECK_COND(test1[7] == test1[6] + ps/4, "Part 1.6");
840 void *test2 = alloc.Alloc(ps * 3);
843 memset(test2, 0, ps * 3); // should not crash :)
848 // Dealloc a ps/4 block and place two ps/8 blocks into it
849 alloc.Free(test1[2]);
852 test3[0] = (char*) alloc.Alloc(ps / 8);
853 test3[1] = (char*) alloc.Alloc(ps / 8);
855 CHECK_COND(test3[0] == test1[2], "Part 2.1");
856 CHECK_COND(test3[1] == test1[2] + ps/8, "Part 2.2");
860 for (i = 0; i < 8; ++i)
861 test4[i] = (char*) alloc.AllocIsolated(ps / 4);
863 // -> The difference is at least one page
864 CHECK_COND(static_cast<size_t>(abs(test4[1] - test4[0])) >= ps, "Part 3.1");
865 CHECK_COND(static_cast<size_t>(abs(test4[2] - test4[1])) >= ps, "Part 3.2");
866 CHECK_COND(static_cast<size_t>(abs(test4[3] - test4[2])) >= ps, "Part 3.3");
868 CHECK_COND(static_cast<size_t>(abs(test4[5] - test4[4])) >= ps, "Part 3.4");
869 CHECK_COND(static_cast<size_t>(abs(test4[6] - test4[5])) >= ps, "Part 3.5");
870 CHECK_COND(static_cast<size_t>(abs(test4[7] - test4[6])) >= ps, "Part 3.6");
872 // Thus i can set everything except for test4[2] to RE and still write to test4[2]
874 alloc.SetRW(test4[2]);
876 alloc.SetRE(test4[0]);
877 alloc.SetRE(test4[1]);
878 alloc.SetRE(test4[3]);
879 alloc.SetRE(test4[4]);
880 alloc.SetRE(test4[5]);
881 alloc.SetRE(test4[6]);
882 alloc.SetRE(test4[7]);
884 memset((void*)test4[2], 0, ps / 4);