2 #include "sourcehook.h"
3 #include "sourcehook_test.h"
4 #include "testevents.h"
6 #include "sh_pagealloc.h"
9 // Test automatic hookman generation
10 // Tests void and non-void functions
12 // integer-type, float-type, plain-old-data struct and objects with ctors/dtors
13 // both byval and byref
14 // also tests ignore/supercede
17 // :TODO: test override as well
22 SourceHook::ISourceHook *g_SHPtr;
23 SourceHook::Plugin g_PLID;
25 SourceHook::IHookManagerAutoGen *g_HMAGPtr;
27 // PtrBuf(ptr) gives ptrs unique numbers
28 // in the order they appear
29 SourceHook::List<const void*> g_PtrHash;
31 bool g_Inside_LeafFunc = false; // inside a hook or a func
40 bool operator==(const POD<MYSIZE> &other)
42 return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
45 bool operator==(char other)
47 for (int i = 0; i < MYSIZE; ++i)
57 std::ostream& operator <<(std::ostream &os, const POD<MYSIZE> &obj)
63 MAKE_STATE_1(State_ObjOCtor_Called, int /*MYSIZE*/);
64 MAKE_STATE_1(State_ObjCCtor_Called, int /*MYSIZE*/);
65 MAKE_STATE_1(State_ObjODtor_Called, int /*MYSIZE*/);
66 MAKE_STATE_1(State_ObjAssignOp_Called, int /*MYSIZE*/);
75 memset(reinterpret_cast<void*>(x), initch, MYSIZE);
76 if (!g_Inside_LeafFunc)
77 ADD_STATE(State_ObjOCtor_Called(MYSIZE));
82 if (!g_Inside_LeafFunc)
83 ADD_STATE(State_ObjOCtor_Called(MYSIZE));
86 Object(const Object<MYSIZE> & other)
88 memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
89 if (!g_Inside_LeafFunc)
90 ADD_STATE(State_ObjCCtor_Called(MYSIZE));
95 if (!g_Inside_LeafFunc)
96 ADD_STATE(State_ObjODtor_Called(MYSIZE));
99 Object & operator = (const Object &other)
101 if (!g_Inside_LeafFunc)
102 ADD_STATE(State_ObjAssignOp_Called(MYSIZE));
104 memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
109 bool operator==(const Object<MYSIZE> &other)
111 return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
115 template <int MYSIZE>
116 std::ostream& operator <<(std::ostream &os, const Object<MYSIZE> &obj)
118 os << "Some Obj" << static_cast<int>(obj.x[0]);
122 // Because of some weird bug in MSVC < 1400
123 #define MAKE_PODRET(size) \
124 struct PodRet##size \
127 bool operator==(const PodRet##size &other) { return actPod == other.actPod; } \
128 bool operator==(char other) { return actPod == other; } \
130 std::ostream& operator <<(std::ostream &os, const PodRet##size &obj) \
135 template <> struct MakeRet< PodRet##size > \
137 static PodRet##size Do(int a) \
140 memset(reinterpret_cast<void*>(x.actPod.x), a, size); \
145 #define MAKE_OBJRET(size) \
146 struct ObjRet##size \
148 Object<size> actObj; \
149 bool operator==(const ObjRet##size &other) { return actObj == other.actObj; } \
150 bool operator==(char other) { return actObj == other; } \
152 std::ostream& operator <<(std::ostream &os, const ObjRet##size &obj) \
157 template <> struct MakeRet< ObjRet##size > \
159 static ObjRet##size Do(int a) NO_OPTIMIZE \
161 ObjRet##size *x = new ObjRet##size; /* ptr: otherwise gcc optimizes away the temp obj */ \
162 CAutoPtrDestruction< ObjRet##size > apd(x); \
163 memset(reinterpret_cast<void*>(x->actObj.x), a, size); \
170 // "Increment" something: (reproducible change for recall tests)
173 // pod/object: x[i]++, 0 <= i < MYSIZE
177 static void Incr(T &what)
184 struct Increment<float>
186 static void Incr(float &what)
193 struct Increment<double>
195 static void Incr(double &what)
202 struct Increment< POD<MYSIZE> >
204 static void Incr(POD<MYSIZE> &what)
206 for (int i = 0; i < MYSIZE; ++i)
212 struct Increment< Object<MYSIZE> >
214 static void Incr(Object<MYSIZE> &what)
216 for (int i = 0; i < MYSIZE; ++i)
221 #include "testhookmangen.h"
228 const void *vptr = reinterpret_cast<const void*>(ptr);
229 for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end(); ++iter)
236 g_PtrHash.push_back(vptr);
237 return static_cast<int>(g_PtrHash.size()) - 1;
247 void PtrBuf_Clear(int leave_in = 0)
249 for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end();)
252 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)
418 GET_HMAG(g_HMAGPtr, g_SHPtr);
421 // 5 Global constructors (g_O111_*)
422 CHECK_STATES((&g_States,
423 new State_ObjOCtor_Called(111),
424 new State_ObjOCtor_Called(111),
425 new State_ObjOCtor_Called(111),
426 new State_ObjOCtor_Called(111),
427 new State_ObjOCtor_Called(111),
430 THGM_DO_TEST_void(0, ());
432 THGM_DO_TEST_void(1, (100));
434 THGM_DO_TEST_void(2, (0x1F00));
436 THGM_DO_TEST_void(3, (0x1F000000));
438 THGM_DO_TEST_void(4, (0.5f));
440 THGM_DO_TEST_void(5, (5.5));
442 THGM_DO_TEST_void(6, (100, 0x1f00, 0x1f000000, 0.5f, 5.5));
446 THGM_DO_TEST_void(7, (a, b));
448 POD<7> pod7 = MakeRet< POD<7> >::Do(78);
449 THGM_DO_TEST_void(8, (pod7));
451 POD<600> pod600 = MakeRet< POD<600> >::Do(34);
452 THGM_DO_TEST_void(9, (pod600));
454 THGM_DO_TEST_void(10, (pod600));
456 // Test11: Special: constructors/destructors
459 Object<600> obj600(21);
461 CHECK_STATES((&g_States,
462 new State_ObjOCtor_Called(3),
463 new State_ObjOCtor_Called(600),
464 NULL), "Test11 Part0");
467 SourceHook::HookManagerPubFunc myhookman11 = g_HMAGPtr->MakeHookMan(&protoinfo_11, 0, 0); \
468 CAutoReleaseHookMan arhm_11(myhookman11); \
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 SourceHook::HookManagerPubFunc myhookman110 = g_HMAGPtr->MakeHookMan(&protoinfo_110, 0, 0); \
634 CAutoReleaseHookMan arhm_110(myhookman110); \
635 int hook1_110, hook2_110, hook3_110, hook4_110;
637 TestClass110 *pTest110 = new TestClass110;
638 CAutoPtrDestruction<TestClass110> apd110(pTest110);
640 /* no hooks - no hooks */
642 obj13 = pTest110->Func(5);
644 g_Inside_LeafFunc = true;
645 CHECK_STATES((&g_States,
646 new State_Func110(pTest110, ParamState_m110 (5)),
647 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
648 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in our stack
649 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
651 new State_ObjAssignOp_Called(13), // assign: obj13 = temporary object in our stack
652 new State_ObjODtor_Called(13), // Func110: destruction of temporary object
653 NULL), "Test" "110" " Part1");
654 g_Inside_LeafFunc = false;
656 /* hook1 - no hooks */
657 THGM_ADD_HOOK(110, 1);
660 obj13 = pTest110->Func(5);
661 g_Inside_LeafFunc = true;
662 CHECK_STATES((&g_States,
663 // HookFunc: construct orig_ret/override_ret/plugin_ret objects
664 new State_ObjOCtor_Called(13),
665 new State_ObjOCtor_Called(13),
666 new State_ObjOCtor_Called(13),
669 new State_Deleg1_110(pTest110, 0, ParamState_m110 (5)),
671 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
672 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in HookFunc's stack
673 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
676 new State_ObjAssignOp_Called(13), // assign: plugin_ret = temporary object in HookFunc's stack
677 new State_ObjODtor_Called(13), // destruction of temporary object in HookFunc's stack
679 // Calling orig function Func110
680 new State_Func110(pTest110, ParamState_m110 (5)),
681 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
682 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in HookFunc's stack
683 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
686 new State_ObjAssignOp_Called(13), // assign: orig_ret = temporary object in HookFunc's stack
687 new State_ObjODtor_Called(13), // destruction of temporary object in HookFunc's stack
689 // hookfunc is returning:
690 new State_ObjCCtor_Called(13), // copy to temp object in our stack
692 // hookfunc cleans up its stack -> destroys plugin_ret/override_ret/orig_ret
693 new State_ObjODtor_Called(13),
694 new State_ObjODtor_Called(13),
695 new State_ObjODtor_Called(13),
697 // we are in our function: assign
698 new State_ObjAssignOp_Called(13), // assign: obj13 = temporary object in our stack
699 new State_ObjODtor_Called(13), // Func110: destruction of temporary object
701 NULL), "Test" "11" " Part2");
703 CHECK_COND(obj13 == 0, "Test" "11" " Part 2.1");
704 g_Inside_LeafFunc = false;
706 THGM_REMOVE_HOOK(11, 1);
708 /* hook1, hook2 - hook3, hook4 */
709 THGM_ADD_HOOK(110, 1);
710 THGM_ADD_HOOK(110, 2);
711 THGM_ADD_HOOK(110, 3);
712 THGM_ADD_HOOK(110, 4);
715 obj13 = pTest110->Func(5);
716 g_Inside_LeafFunc = true;
717 CHECK_STATES((&g_States,
718 // HookFunc: construct orig_ret/override_ret/plugin_ret objects
719 new State_ObjOCtor_Called(13),
720 new State_ObjOCtor_Called(13),
721 new State_ObjOCtor_Called(13),
724 new State_Deleg1_110(pTest110, 0, ParamState_m110 (5)),
726 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
727 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in HookFunc's stack
728 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
731 new State_ObjAssignOp_Called(13), // assign: plugin_ret = temporary object in HookFunc's stack
732 new State_ObjODtor_Called(13), // destruction of temporary object in HookFunc's stack
736 new State_Deleg2_110(pTest110, 1, ParamState_m110 (5)),
738 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
739 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in HookFunc's stack
740 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
743 new State_ObjAssignOp_Called(13), // assign: plugin_ret = temporary object in HookFunc's stack
744 new State_ObjODtor_Called(13), // destruction of temporary object in HookFunc's stack
746 // hookfunc finds out that the hook wanted to SUPERCEDE --> copy to override_Ret
747 new State_ObjAssignOp_Called(13),
749 // SUPERCEDE -> orig function is not called
750 // instead: orig_ret = override_ret
751 new State_ObjAssignOp_Called(13),
754 new State_Deleg3_110(pTest110, 2, ParamState_m110 (5)),
756 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
757 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in HookFunc's stack
758 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
761 new State_ObjAssignOp_Called(13), // assign: plugin_ret = temporary object in HookFunc's stack
762 new State_ObjODtor_Called(13), // destruction of temporary object in HookFunc's stack
765 new State_Deleg4_110(pTest110, 3, ParamState_m110 (5)),
767 new State_ObjOCtor_Called(13), // MakeRet: Construction of x
768 new State_ObjCCtor_Called(13), // Return from MakeRet -> construct temporary object in HookFunc's stack
769 new State_ObjODtor_Called(13), // MakeRet: Destruction of x
772 new State_ObjAssignOp_Called(13), // assign: plugin_ret = temporary object in HookFunc's stack
773 new State_ObjODtor_Called(13), // destruction of temporary object in HookFunc's stack
775 // hookfunc finds out that the hook wanted to SUPERCEDE --> copy to override_Ret (yes really, we overwrite the old value!)
776 new State_ObjAssignOp_Called(13),
778 // hookfunc is returning:
779 new State_ObjCCtor_Called(13), // copy to temp object in our stack
781 // hookfunc cleans up its stack -> destroys plugin_ret/override_ret/orig_ret
782 new State_ObjODtor_Called(13),
783 new State_ObjODtor_Called(13),
784 new State_ObjODtor_Called(13),
786 // we are in our function: assign
787 new State_ObjAssignOp_Called(13), // assign: obj13 = temporary object in our stack
788 new State_ObjODtor_Called(13), // Func110: destruction of temporary object
790 NULL), "Test" "11" " Part3");
792 CHECK_COND(obj13 == 4, "Test" "11" " Part 3.1");
793 g_Inside_LeafFunc = false;
795 THGM_REMOVE_HOOK(110, 1);
796 THGM_REMOVE_HOOK(110, 2);
797 THGM_REMOVE_HOOK(110, 3);
798 THGM_REMOVE_HOOK(110, 4);
802 THGM_DO_TEST(111, ());
805 // If we don't SH will auto-shutdown _after_ genc's destructor is called
808 Test_CompleteShutdown(g_SHPtr);
810 CHECK_COND(sizeof(PodRet1) == 1, "WTF!");
811 CHECK_COND(sizeof(PodRet4) == 4, "WTF!");
812 CHECK_COND(sizeof(PodRet8) == 8, "WTF!");
813 CHECK_COND(sizeof(PodRet13) == 13, "WTF!");
818 bool TestCPageAlloc(std::string &error)
820 using namespace SourceHook;
824 size_t ps = alloc.GetPageSize();
828 for (i = 0; i < 8; ++i)
829 test1[i] = (char*) alloc.Alloc(ps / 4);
831 CHECK_COND(test1[1] == test1[0] + ps/4, "Part 1.1");
832 CHECK_COND(test1[2] == test1[1] + ps/4, "Part 1.2");
833 CHECK_COND(test1[3] == test1[2] + ps/4, "Part 1.3");
835 CHECK_COND(test1[5] == test1[4] + ps/4, "Part 1.4");
836 CHECK_COND(test1[6] == test1[5] + ps/4, "Part 1.5");
837 CHECK_COND(test1[7] == test1[6] + ps/4, "Part 1.6");
839 void *test2 = alloc.Alloc(ps * 3);
842 memset(test2, 0, ps * 3); // should not crash :)
847 // Dealloc a ps/4 block and place two ps/8 blocks into it
848 alloc.Free(test1[2]);
851 test3[0] = (char*) alloc.Alloc(ps / 8);
852 test3[1] = (char*) alloc.Alloc(ps / 8);
854 CHECK_COND(test3[0] == test1[2], "Part 2.1");
855 CHECK_COND(test3[1] == test1[2] + ps/8, "Part 2.2");
859 for (i = 0; i < 8; ++i)
860 test4[i] = (char*) alloc.AllocIsolated(ps / 4);
862 // -> The difference is at least one page
863 CHECK_COND(static_cast<size_t>(abs(test4[1] - test4[0])) >= ps, "Part 3.1");
864 CHECK_COND(static_cast<size_t>(abs(test4[2] - test4[1])) >= ps, "Part 3.2");
865 CHECK_COND(static_cast<size_t>(abs(test4[3] - test4[2])) >= ps, "Part 3.3");
867 CHECK_COND(static_cast<size_t>(abs(test4[5] - test4[4])) >= ps, "Part 3.4");
868 CHECK_COND(static_cast<size_t>(abs(test4[6] - test4[5])) >= ps, "Part 3.5");
869 CHECK_COND(static_cast<size_t>(abs(test4[7] - test4[6])) >= ps, "Part 3.6");
871 // Thus i can set everything except for test4[2] to RE and still write to test4[2]
873 alloc.SetRW(test4[2]);
875 alloc.SetRE(test4[0]);
876 alloc.SetRE(test4[1]);
877 alloc.SetRE(test4[3]);
878 alloc.SetRE(test4[4]);
879 alloc.SetRE(test4[5]);
880 alloc.SetRE(test4[6]);
881 alloc.SetRE(test4[7]);
883 memset((void*)test4[2], 0, ps / 4);