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) \
161 memset(reinterpret_cast<void*>(x.actObj.x), a, size); \
168 // "Increment" something: (reproducible change for recall tests)
171 // pod/object: x[i]++, 0 <= i < MYSIZE
175 static void Incr(T &what)
182 struct Increment<float>
184 static void Incr(float &what)
191 struct Increment<double>
193 static void Incr(double &what)
200 struct Increment< POD<MYSIZE> >
202 static void Incr(POD<MYSIZE> &what)
204 for (int i = 0; i < MYSIZE; ++i)
210 struct Increment< Object<MYSIZE> >
212 static void Incr(Object<MYSIZE> &what)
214 for (int i = 0; i < MYSIZE; ++i)
219 #include "testhookmangen.h"
226 const void *vptr = reinterpret_cast<const void*>(ptr);
227 for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end(); ++iter)
234 g_PtrHash.push_back(vptr);
235 return static_cast<int>(g_PtrHash.size()) - 1;
245 void PtrBuf_Clear(int leave_in = 0)
247 for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end();)
250 iter = g_PtrHash.erase(iter);
257 // MyDelegate base class for other delegates
258 class MyDelegate : public SourceHook::ISHDelegate
261 // because we don't use old SH_REMOVE_HOOK syntax
262 virtual bool IsEqual(SourceHook::ISHDelegate *pOtherDeleg)
267 virtual void DeleteThis()
273 THGM_MAKE_TEST0_void(0);
276 THGM_MAKE_TEST1_void(1, char);
277 THGM_SETUP_PI1(1, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
279 THGM_MAKE_TEST1_void(2, short);
280 THGM_SETUP_PI1(2, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
282 THGM_MAKE_TEST1_void(3, int);
283 THGM_SETUP_PI1(3, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
285 THGM_MAKE_TEST1_void(4, float);
286 THGM_SETUP_PI1(4, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
288 THGM_MAKE_TEST1_void(5, double);
289 THGM_SETUP_PI1(5, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
291 THGM_MAKE_TEST5_void(6, char, short, int, float, double);
293 char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
294 short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
295 int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
296 float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal,
297 double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal
300 THGM_MAKE_TEST2_void(7, char&, double&);
302 char&, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByRef,
303 double&, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByRef
306 THGM_MAKE_TEST1_void(8, POD<7>);
307 THGM_SETUP_PI1(8, POD<7>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
309 THGM_MAKE_TEST1_void(9, POD<600>);
310 THGM_SETUP_PI1(9, POD<600>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
312 THGM_MAKE_TEST1_void(10, POD<600> &);
313 THGM_SETUP_PI1(10, POD<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef);
315 THGM_MAKE_TEST2_void(11, Object<3>, Object<600>&);
317 Object<3>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor,
318 Object<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor
321 THGM_MAKE_TEST0(101, char);
323 THGM_SETUP_RI(101, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
325 THGM_MAKE_TEST0(102, short);
327 THGM_SETUP_RI(102, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
329 THGM_MAKE_TEST0(103, int);
331 THGM_SETUP_RI(103, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
333 THGM_MAKE_TEST0(104, float);
335 THGM_SETUP_RI(104, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
337 THGM_MAKE_TEST0(105, double);
339 THGM_SETUP_RI(105, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
343 THGM_MAKE_TEST1(106, PodRet1, int);
345 int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
347 THGM_SETUP_RI(106, PodRet1, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
350 THGM_MAKE_TEST1(107, PodRet4, int);
352 int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
354 THGM_SETUP_RI(107, PodRet4, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
357 THGM_MAKE_TEST1(108, PodRet8, int);
359 int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
361 THGM_SETUP_RI(108, PodRet8, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
364 THGM_MAKE_TEST1(109, PodRet13, int);
366 int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
368 THGM_SETUP_RI(109, PodRet13, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
371 THGM_MAKE_TEST1(110, ObjRet13, int);
373 int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
375 THGM_SETUP_RI(110, ObjRet13, SourceHook::PassInfo::PassType_Object,
376 SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
377 SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
387 struct MakeRet< ObjRet111& >
389 static ObjRet111 &Do(int a)
407 THGM_MAKE_TEST0(111, ObjRet111& );
409 THGM_SETUP_RI(111, ObjRet111& , SourceHook::PassInfo::PassType_Object,
410 SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
411 SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
414 bool TestHookManGen(std::string &error)
419 // 5 Global constructors (g_O111_*)
420 CHECK_STATES((&g_States,
421 new State_ObjOCtor_Called(111),
422 new State_ObjOCtor_Called(111),
423 new State_ObjOCtor_Called(111),
424 new State_ObjOCtor_Called(111),
425 new State_ObjOCtor_Called(111),
428 THGM_DO_TEST_void(0, ());
430 THGM_DO_TEST_void(1, (100));
432 THGM_DO_TEST_void(2, (0x1F00));
434 THGM_DO_TEST_void(3, (0x1F000000));
436 THGM_DO_TEST_void(4, (0.5f));
438 THGM_DO_TEST_void(5, (5.5));
440 THGM_DO_TEST_void(6, (100, 0x1f00, 0x1f000000, 0.5f, 5.5));
444 THGM_DO_TEST_void(7, (a, b));
446 POD<7> pod7 = MakeRet< POD<7> >::Do(78);
447 THGM_DO_TEST_void(8, (pod7));
449 POD<600> pod600 = MakeRet< POD<600> >::Do(34);
450 THGM_DO_TEST_void(9, (pod600));
452 THGM_DO_TEST_void(10, (pod600));
454 // Test11: Special: constructors/destructors
457 Object<600> obj600(21);
459 CHECK_STATES((&g_States,
460 new State_ObjOCtor_Called(3),
461 new State_ObjOCtor_Called(600),
462 NULL), "Test11 Part0");
465 g_Genc11 = new SourceHook::Impl::GenContext(&protoinfo_11, 0, 0, g_SHPtr);
466 g_Genc_ad11.set(g_Genc11);
467 SourceHook::HookManagerPubFunc myhookman11 = g_Genc11->Generate();
468 int hook1_11, hook2_11, hook3_11, hook4_11;
470 TestClass11 *pTest11 = new TestClass11;
471 CAutoPtrDestruction<TestClass11> apd11(pTest11);
473 /* no hooks - no hooks */
475 pTest11->Func(obj3, obj600);
477 g_Inside_LeafFunc = true;
478 CHECK_STATES((&g_States,
479 new State_ObjCCtor_Called(3),
480 new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
481 new State_ObjODtor_Called(3),
482 NULL), "Test" "11" " Part1");
483 g_Inside_LeafFunc = false;
485 /* hook1 - no hooks */
486 THGM_ADD_HOOK(11, 1);
489 pTest11->Func(obj3, obj600);
490 g_Inside_LeafFunc = true;
491 CHECK_STATES((&g_States,
492 new State_ObjCCtor_Called(3),
494 new State_ObjCCtor_Called(3),
495 new State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
496 new State_ObjODtor_Called(3),
498 new State_ObjCCtor_Called(3),
499 new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
500 new State_ObjODtor_Called(3),
502 new State_ObjODtor_Called(3),
503 NULL), "Test" "11" " Part2");
504 g_Inside_LeafFunc = false;
506 THGM_REMOVE_HOOK(11, 1);
508 /* hook1, hook2 - hook3, hook4 */
509 THGM_ADD_HOOK(11, 1);
510 THGM_ADD_HOOK(11, 2);
511 THGM_ADD_HOOK(11, 3);
512 THGM_ADD_HOOK(11, 4);
514 pTest11->Func(obj3, obj600);
515 g_Inside_LeafFunc = true;
516 CHECK_STATES((&g_States,
517 new State_ObjCCtor_Called(3),
519 new State_ObjCCtor_Called(3),
520 new State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
521 new State_ObjODtor_Called(3),
523 new State_ObjCCtor_Called(3),
524 new State_Deleg2_11(pTest11, 1, ParamState_m11 (obj3, obj600)),
525 new State_ObjODtor_Called(3),
527 new State_ObjCCtor_Called(3),
528 new State_Deleg3_11(pTest11, 2, ParamState_m11 (obj3, obj600)),
529 new State_ObjODtor_Called(3),
531 new State_ObjCCtor_Called(3),
532 new State_Deleg4_11(pTest11, 3, ParamState_m11 (obj3, obj600)),
533 new State_ObjODtor_Called(3),
535 new State_ObjODtor_Called(3),
536 NULL), "Test" "11" " Part3");
537 g_Inside_LeafFunc = false;
539 /* hook1 - hook3, hook4 WITH RECALLS */
540 THGM_REMOVE_HOOK(11, 2);
542 TestClass11::ms_DoRecall = true;
543 pTest11->Func(obj3, obj600);
544 g_Inside_LeafFunc = true;
545 CHECK_STATES((&g_States,
546 new State_ObjCCtor_Called(3),
548 new State_ObjCCtor_Called(3),
549 new State_Deleg1_11(pTest11, 0 /* first deleg ptr */, ParamState_m11 (obj3, obj600)),
552 // second hookfunc -> new copy
553 new State_ObjCCtor_Called(3),
555 // in second hookfunc now
556 // -> calls orig func
558 new State_ObjCCtor_Called(3),
559 new State_Func11(pTest11, ParamState_m11 (obj3, obj600)(1)),
560 new State_ObjODtor_Called(3),
562 // calls first posthook
563 new State_ObjCCtor_Called(3),
564 new State_Deleg3_11(pTest11, 1 /* second deleg ptr */, ParamState_m11 (obj3, obj600)(1)),
567 // third hookfunc -> new copy
568 new State_ObjCCtor_Called(3),
570 // calls second posthook
572 new State_ObjCCtor_Called(3),
573 new State_Deleg4_11(pTest11, 2 /* third deleg ptr */, ParamState_m11 (obj3, obj600)(2)),
576 // fourth hookfunc -> new copy
577 new State_ObjCCtor_Called(3),
579 // has nothing to do though!
581 // fourth hookfunc done -> ret
582 new State_ObjODtor_Called(3),
584 // third hookfunc done -> ret
585 new State_ObjODtor_Called(3),
586 // ret from hookhandler which did the recall
587 new State_ObjODtor_Called(3),
589 // second hookfunc done -> ret
590 new State_ObjODtor_Called(3),
591 // ret from hookhandler which did the recall
592 new State_ObjODtor_Called(3),
595 new State_ObjODtor_Called(3),
596 // first hookfunc done -> ret
597 new State_ObjODtor_Called(3),
598 NULL), "Test" "11" " Part4");
599 g_Inside_LeafFunc = false;
601 THGM_REMOVE_HOOK(11, 1);
602 THGM_REMOVE_HOOK(11, 3);
603 THGM_REMOVE_HOOK(11, 4);
605 THGM_DO_TEST(101, ());
607 THGM_DO_TEST(102, ());
609 THGM_DO_TEST(103, ());
611 THGM_DO_TEST(104, ());
613 THGM_DO_TEST(105, ());
616 THGM_DO_TEST(106, (5));
617 THGM_DO_TEST(107, (5));
618 THGM_DO_TEST(108, (5));
619 THGM_DO_TEST(109, (5));
621 // Test110: Special: constructors/destructors on return
625 CHECK_STATES((&g_States,
626 new State_ObjOCtor_Called(13),
627 NULL), "Test110 Part0");
632 g_Genc110 = new SourceHook::Impl::GenContext(&protoinfo_110, 0, 0, g_SHPtr);
633 g_Genc_ad110.set(g_Genc110);
634 SourceHook::HookManagerPubFunc myhookman110 = g_Genc110->Generate();
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);