sourcehook/test/testhookmangen.cpp
author Pavol Marko <pavolmarko@gmail.com>
Fri Nov 09 21:14:37 2007 +0000 (2007-11-09)
branchhookman_autogen
changeset 452 2efe13dab607
parent 451 a39d347d9991
child 455 deec66cea8cc
permissions -rw-r--r--
- Removed unnecessary definitions from sh_pagealloc.h
- Added CallConv_StdCall
- Added CallConv_HasVarArgs and CallConv_HasVafmt
     1 #include <string>
     2 #include "sourcehook.h"
     3 #include "sourcehook_test.h"
     4 #include "testevents.h"
     5 #include "sourcehook_hookmangen.h"
     6 #include "sh_memory.h"
     7 #include "sh_pagealloc.h"
     8 
     9 // TESTHOOKMANGEN
    10 // Test automatic hookman generation
    11 //  Tests void and non-void functions
    12 //  0 to 6 params:
    13 //   integer-type, float-type, plain-old-data struct and objects with ctors/dtors
    14 //   both byval and byref
    15 //  also tests ignore/supercede
    16 //  also tests recalls
    17 
    18 // :TODO: test override as well
    19 
    20 namespace
    21 {
    22 	StateList g_States;
    23 	SourceHook::ISourceHook *g_SHPtr;
    24 	SourceHook::Plugin g_PLID;
    25 
    26 	// PtrBuf(ptr) gives ptrs unique numbers
    27 	// in the order they appear
    28 	SourceHook::List<const void*> g_PtrHash;
    29 
    30 	bool g_Inside_LeafFunc = false;			// inside a hook or a func
    31 
    32 
    33 	// POD / Object types
    34 	template <int MYSIZE>
    35 	struct POD
    36 	{
    37 		char x[MYSIZE];
    38 
    39 		bool operator==(const POD<MYSIZE> &other)
    40 		{
    41 			return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
    42 		}
    43 
    44 		bool operator==(char other)
    45 		{
    46 			for (int i = 0; i < MYSIZE; ++i)
    47 			{
    48 				if (x[i] != other)
    49 					return false;
    50 			}
    51 			return true;
    52 		}
    53 	};
    54 
    55 	template <int MYSIZE>
    56 		std::ostream& operator <<(std::ostream &os, const POD<MYSIZE> &obj)
    57 	{
    58 		os << "Some POD!";
    59 		return os;
    60 	}
    61 
    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*/);
    66 
    67 	template <int MYSIZE>
    68 	struct Object
    69 	{
    70 		char x[MYSIZE];
    71 
    72 		Object(char initch)
    73 		{
    74 			memset(reinterpret_cast<void*>(x), initch, MYSIZE);
    75 			if (!g_Inside_LeafFunc)
    76 				ADD_STATE(State_ObjOCtor_Called(MYSIZE));
    77 		}
    78 
    79 		Object()
    80 		{
    81 			if (!g_Inside_LeafFunc)
    82 				ADD_STATE(State_ObjOCtor_Called(MYSIZE));
    83 		}
    84 
    85 		Object(const Object<MYSIZE> & other)
    86 		{
    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));
    90 		}
    91 
    92 		~Object()
    93 		{
    94 			if (!g_Inside_LeafFunc)
    95 				ADD_STATE(State_ObjODtor_Called(MYSIZE));
    96 		}
    97 
    98 		Object & operator = (const Object &other)
    99 		{
   100 			if (!g_Inside_LeafFunc)
   101 				ADD_STATE(State_ObjAssignOp_Called(MYSIZE));
   102 
   103 			memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
   104 
   105 			return *this;
   106 		}
   107 
   108 		bool operator==(const Object<MYSIZE> &other)
   109 		{
   110 			return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
   111 		}
   112 	};
   113 
   114 	template <int MYSIZE>
   115 	std::ostream& operator <<(std::ostream &os, const Object<MYSIZE> &obj)
   116 	{
   117 		os << "Some Obj" << static_cast<int>(obj.x[0]);
   118 		return os;
   119 	}
   120 
   121 	// Because of some weird bug in MSVC < 1400
   122 #define MAKE_PODRET(size) \
   123 	struct PodRet##size \
   124 	{ \
   125 		POD<size> actPod; \
   126 		bool operator==(const PodRet##size &other) { return actPod == other.actPod; } \
   127 		bool operator==(char other) { return actPod == other; } \
   128 	}; \
   129 	std::ostream& operator <<(std::ostream &os, const PodRet##size &obj) \
   130 	{ \
   131 		os << obj.actPod; \
   132 		return os; \
   133 	} \
   134 	template <> struct MakeRet< PodRet##size > \
   135 	{ \
   136 		static PodRet##size Do(int a) \
   137 		{ \
   138 			PodRet##size x; \
   139 			memset(reinterpret_cast<void*>(x.actPod.x), a, size); \
   140 			return x; \
   141 		} \
   142 	};
   143 
   144 #define MAKE_OBJRET(size) \
   145 	struct ObjRet##size \
   146 	{ \
   147 		Object<size> actObj; \
   148 		bool operator==(const ObjRet##size &other) { return actObj == other.actObj; } \
   149 		bool operator==(char other) { return actObj == other; } \
   150 	}; \
   151 	std::ostream& operator <<(std::ostream &os, const ObjRet##size &obj) \
   152 	{ \
   153 		os << obj.actObj; \
   154 		return os; \
   155 	} \
   156 	template <> struct MakeRet< ObjRet##size > \
   157 	{ \
   158 		static ObjRet##size Do(int a) NO_OPTIMIZE \
   159 		{ \
   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); \
   163 			return *x; \
   164 		} \
   165 	};
   166 
   167 
   168 
   169 	// "Increment" something:  (reproducible change for recall tests)
   170 	//  integer: ++
   171 	//  float: += 1.3
   172 	//  pod/object: x[i]++, 0 <= i < MYSIZE
   173 	template <class T>
   174 	struct Increment
   175 	{
   176 		static void Incr(T &what)	
   177 		{
   178 			++what;
   179 		}
   180 	};
   181 
   182 	template<>
   183 	struct Increment<float>
   184 	{
   185 		static void Incr(float &what)	
   186 		{
   187 			what += 1.3f;
   188 		}
   189 	};
   190 
   191 	template<>
   192 	struct Increment<double>
   193 	{
   194 		static void Incr(double &what)	
   195 		{
   196 			what += 1.3;
   197 		}
   198 	};
   199 
   200 	template<int MYSIZE>
   201 	struct Increment< POD<MYSIZE> >
   202 	{
   203 		static void Incr(POD<MYSIZE> &what)	
   204 		{
   205 			for (int i = 0; i < MYSIZE; ++i)
   206 				++ what.x[i];
   207 		}
   208 	};
   209 
   210 	template<int MYSIZE>
   211 	struct Increment< Object<MYSIZE> >
   212 	{
   213 		static void Incr(Object<MYSIZE> &what)	
   214 		{
   215 			for (int i = 0; i < MYSIZE; ++i)
   216 				++ what.x[i];
   217 		}
   218 	};
   219 
   220 	#include "testhookmangen.h"
   221 
   222 	template <class T>
   223 	int PtrBuf(T ptr)
   224 	{
   225 		int a = 0;
   226 
   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)
   229 		{
   230 			if (*iter == vptr)
   231 				return a;
   232 			else
   233 				++a;
   234 		}
   235 		g_PtrHash.push_back(vptr);
   236 		return static_cast<int>(g_PtrHash.size()) - 1;
   237 	}
   238 
   239 	template <class T>
   240 	T PtrBufPtr(T ptr)
   241 	{
   242 		PtrBuf(ptr);
   243 		return ptr;
   244 	}
   245 
   246 	void PtrBuf_Clear(int leave_in = 0)
   247 	{
   248 		for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end();)
   249 		{
   250 			if (--leave_in < 0)
   251 				iter = g_PtrHash.erase(iter);
   252 			else
   253 				++iter;
   254 		}
   255 	}
   256 
   257 
   258 	// MyDelegate base class for other delegates
   259 	class MyDelegate : public SourceHook::ISHDelegate
   260 	{
   261 		// Unneeded
   262 		//  because we don't use old SH_REMOVE_HOOK syntax
   263 		virtual bool IsEqual(SourceHook::ISHDelegate *pOtherDeleg)
   264 		{
   265 			return false;
   266 		}
   267 
   268 		virtual void DeleteThis()
   269 		{
   270 			delete this;
   271 		}
   272 	};
   273 
   274 	THGM_MAKE_TEST0_void(0);
   275 	THGM_SETUP_PI0(0);
   276 
   277 	THGM_MAKE_TEST1_void(1, char);
   278 	THGM_SETUP_PI1(1, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   279 
   280 	THGM_MAKE_TEST1_void(2, short);
   281 	THGM_SETUP_PI1(2, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   282 
   283 	THGM_MAKE_TEST1_void(3, int);
   284 	THGM_SETUP_PI1(3, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   285 
   286 	THGM_MAKE_TEST1_void(4, float);
   287 	THGM_SETUP_PI1(4, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   288 
   289 	THGM_MAKE_TEST1_void(5, double);
   290 	THGM_SETUP_PI1(5, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   291 	
   292 	THGM_MAKE_TEST5_void(6, char, short, int, float, double);
   293 	THGM_SETUP_PI5(6,
   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
   299 		);
   300 
   301 	THGM_MAKE_TEST2_void(7, char&, double&);
   302 	THGM_SETUP_PI2(7,
   303 		char&, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByRef,
   304 		double&, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByRef
   305 		);
   306 
   307 	THGM_MAKE_TEST1_void(8, POD<7>);
   308 	THGM_SETUP_PI1(8, POD<7>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   309 
   310 	THGM_MAKE_TEST1_void(9, POD<600>);
   311 	THGM_SETUP_PI1(9, POD<600>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   312 
   313 	THGM_MAKE_TEST1_void(10, POD<600> &);
   314 	THGM_SETUP_PI1(10, POD<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef);
   315 
   316 	THGM_MAKE_TEST2_void(11, Object<3>, Object<600>&);
   317 	THGM_SETUP_PI2(11,
   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
   320 		);
   321 
   322 	THGM_MAKE_TEST0(101, char);
   323 	THGM_SETUP_PI0(101);
   324 	THGM_SETUP_RI(101, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   325 
   326 	THGM_MAKE_TEST0(102, short);
   327 	THGM_SETUP_PI0(102);
   328 	THGM_SETUP_RI(102, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   329 
   330 	THGM_MAKE_TEST0(103, int);
   331 	THGM_SETUP_PI0(103);
   332 	THGM_SETUP_RI(103, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   333 
   334 	THGM_MAKE_TEST0(104, float);
   335 	THGM_SETUP_PI0(104);
   336 	THGM_SETUP_RI(104, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   337 
   338 	THGM_MAKE_TEST0(105, double);
   339 	THGM_SETUP_PI0(105);
   340 	THGM_SETUP_RI(105, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   341 
   342 	// pod 1-13
   343 	MAKE_PODRET(1);
   344 	THGM_MAKE_TEST1(106, PodRet1, int);
   345 	THGM_SETUP_PI1(106,
   346 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   347 		);
   348 	THGM_SETUP_RI(106, PodRet1, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   349 
   350 	MAKE_PODRET(4);
   351 	THGM_MAKE_TEST1(107, PodRet4, int);
   352 	THGM_SETUP_PI1(107,
   353 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   354 		);
   355 	THGM_SETUP_RI(107, PodRet4, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   356 
   357 	MAKE_PODRET(8);
   358 	THGM_MAKE_TEST1(108, PodRet8, int);
   359 	THGM_SETUP_PI1(108,
   360 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   361 		);
   362 	THGM_SETUP_RI(108, PodRet8, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   363 
   364 	MAKE_PODRET(13);
   365 	THGM_MAKE_TEST1(109, PodRet13, int);
   366 	THGM_SETUP_PI1(109,
   367 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   368 		);
   369 	THGM_SETUP_RI(109, PodRet13, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   370 
   371 	MAKE_OBJRET(13);
   372 	THGM_MAKE_TEST1(110, ObjRet13, int);
   373 	THGM_SETUP_PI1(110,
   374 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   375 		);
   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);
   379 
   380 	MAKE_OBJRET(111);
   381 	ObjRet111 g_O111_0;
   382 	ObjRet111 g_O111_1;
   383 	ObjRet111 g_O111_2;
   384 	ObjRet111 g_O111_3;
   385 	ObjRet111 g_O111_4;
   386 
   387 	template <>
   388 	struct MakeRet< ObjRet111& >
   389 	{
   390 		static ObjRet111 &Do(int a)
   391 		{
   392 			switch (a)
   393 			{
   394 			case 0:
   395 				return g_O111_0;
   396 			case 1:
   397 				return g_O111_1;
   398 			case 2:
   399 				return g_O111_2;
   400 			case 3:
   401 				return g_O111_3;
   402 			default:
   403 				return g_O111_4;
   404 			}
   405 		}
   406 	};
   407 
   408 	THGM_MAKE_TEST0(111, ObjRet111& );
   409 	THGM_SETUP_PI0(111);
   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);
   413 }
   414 
   415 bool TestHookManGen(std::string &error)
   416 {
   417 	GET_SHPTR(g_SHPtr);
   418 	g_PLID = 1337;
   419 
   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),
   427 		NULL), "GlobCtors");
   428 
   429 	THGM_DO_TEST_void(0, ());
   430 
   431 	THGM_DO_TEST_void(1, (100));
   432 	
   433 	THGM_DO_TEST_void(2, (0x1F00));
   434 
   435 	THGM_DO_TEST_void(3, (0x1F000000));
   436 
   437 	THGM_DO_TEST_void(4, (0.5f));
   438 
   439 	THGM_DO_TEST_void(5, (5.5));
   440 
   441 	THGM_DO_TEST_void(6, (100, 0x1f00, 0x1f000000, 0.5f, 5.5));
   442 
   443 	char a = 5;
   444 	double b = 233.33;
   445 	THGM_DO_TEST_void(7, (a, b));
   446 
   447 	POD<7> pod7 = MakeRet< POD<7> >::Do(78);
   448 	THGM_DO_TEST_void(8, (pod7));
   449 
   450 	POD<600> pod600 = MakeRet< POD<600> >::Do(34);
   451 	THGM_DO_TEST_void(9, (pod600));
   452 	
   453 	THGM_DO_TEST_void(10, (pod600));
   454 
   455 	// Test11: Special: constructors/destructors
   456 	PtrBuf_Clear();
   457 	Object<3> obj3(33);
   458 	Object<600> obj600(21);
   459 
   460 	CHECK_STATES((&g_States,
   461 		new State_ObjOCtor_Called(3),
   462 		new State_ObjOCtor_Called(600),
   463 		NULL), "Test11 Part0");
   464 
   465 	setuppi_11();
   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;
   470 	
   471 	TestClass11 *pTest11 = new TestClass11;
   472 	CAutoPtrDestruction<TestClass11> apd11(pTest11);
   473 	
   474 	/* no hooks - no hooks */
   475 	PtrBuf_Clear();
   476 	pTest11->Func(obj3, obj600);
   477 
   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;
   485 
   486 	/* hook1 - no hooks */
   487 	THGM_ADD_HOOK(11, 1);
   488 	
   489 	PtrBuf_Clear();
   490 	pTest11->Func(obj3, obj600);
   491 	g_Inside_LeafFunc = true;
   492 	CHECK_STATES((&g_States,
   493 		new State_ObjCCtor_Called(3),
   494 
   495 		new State_ObjCCtor_Called(3),
   496 		new State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
   497 		new State_ObjODtor_Called(3),
   498 
   499 		new State_ObjCCtor_Called(3),
   500 		new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
   501 		new State_ObjODtor_Called(3),
   502 
   503 		new State_ObjODtor_Called(3),
   504 		NULL), "Test" "11" " Part2");
   505 	g_Inside_LeafFunc = false;
   506 
   507 	THGM_REMOVE_HOOK(11, 1);
   508 
   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);
   514 	PtrBuf_Clear();
   515 	pTest11->Func(obj3, obj600);
   516 	g_Inside_LeafFunc = true;
   517 	CHECK_STATES((&g_States,
   518 		new State_ObjCCtor_Called(3),
   519 
   520 		new State_ObjCCtor_Called(3),
   521 		new	State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
   522 		new State_ObjODtor_Called(3),
   523 
   524 		new State_ObjCCtor_Called(3),
   525 		new State_Deleg2_11(pTest11, 1, ParamState_m11 (obj3, obj600)),
   526 		new State_ObjODtor_Called(3),
   527 
   528 		new State_ObjCCtor_Called(3),
   529 		new State_Deleg3_11(pTest11, 2, ParamState_m11 (obj3, obj600)),
   530 		new State_ObjODtor_Called(3),
   531 
   532 		new State_ObjCCtor_Called(3),
   533 		new State_Deleg4_11(pTest11, 3, ParamState_m11 (obj3, obj600)),
   534 		new State_ObjODtor_Called(3),
   535 
   536 		new State_ObjODtor_Called(3),
   537 	NULL), "Test" "11" " Part3");
   538 	g_Inside_LeafFunc = false;
   539 
   540 	/* hook1 - hook3, hook4 WITH RECALLS */
   541 	THGM_REMOVE_HOOK(11, 2);
   542 	PtrBuf_Clear();
   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),
   548 
   549 		new State_ObjCCtor_Called(3),
   550 		new	State_Deleg1_11(pTest11, 0 /* first deleg ptr */, ParamState_m11 (obj3, obj600)),
   551 
   552 		// recall !
   553 			// second hookfunc -> new copy
   554 			new State_ObjCCtor_Called(3),
   555 
   556 			// in second hookfunc now
   557 			//   -> calls orig func
   558 
   559 			new State_ObjCCtor_Called(3),
   560 			new State_Func11(pTest11, ParamState_m11 (obj3, obj600)(1)),
   561 			new State_ObjODtor_Called(3),
   562 
   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)),
   566 
   567 			// recall!
   568 				// third hookfunc -> new copy
   569 				new State_ObjCCtor_Called(3),
   570 
   571 				// calls second posthook
   572 
   573 				new State_ObjCCtor_Called(3),
   574 				new State_Deleg4_11(pTest11, 2 /* third deleg ptr */, ParamState_m11 (obj3, obj600)(2)),
   575 
   576 				// recall!
   577 					// fourth hookfunc -> new copy
   578 					new State_ObjCCtor_Called(3),
   579 
   580 					// has nothing to do though!
   581 
   582 					// fourth hookfunc done -> ret
   583 					new State_ObjODtor_Called(3),
   584 
   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),
   589 
   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),
   594 
   595 		// deleg1's instance
   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;
   601 
   602 	THGM_REMOVE_HOOK(11, 1);
   603 	THGM_REMOVE_HOOK(11, 3);
   604 	THGM_REMOVE_HOOK(11, 4);
   605 
   606 	THGM_DO_TEST(101, ());
   607 
   608 	THGM_DO_TEST(102, ());
   609 
   610 	THGM_DO_TEST(103, ());
   611 
   612 	THGM_DO_TEST(104, ());
   613 
   614 	THGM_DO_TEST(105, ());
   615 
   616 	// pod returns
   617 	THGM_DO_TEST(106, (5));
   618 	THGM_DO_TEST(107, (5));
   619 	THGM_DO_TEST(108, (5));
   620 	THGM_DO_TEST(109, (5));
   621 
   622 	// Test110: Special: constructors/destructors on return
   623 	PtrBuf_Clear();
   624 	ObjRet13 obj13;
   625 
   626 	CHECK_STATES((&g_States,
   627 		new State_ObjOCtor_Called(13),
   628 		NULL), "Test110 Part0");
   629 
   630 	setuppi_110();
   631 	setupri_110();
   632 
   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;
   637 
   638 	TestClass110 *pTest110 = new TestClass110;
   639 	CAutoPtrDestruction<TestClass110> apd110(pTest110);
   640 
   641 	/* no hooks - no hooks */
   642 	PtrBuf_Clear();
   643 	obj13 = pTest110->Func(5);
   644 
   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
   651 
   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;
   656 
   657 	/* hook1 - no hooks */
   658 	THGM_ADD_HOOK(110, 1);
   659 
   660 	PtrBuf_Clear();
   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),
   668 
   669 		// Calling delegate
   670 			new State_Deleg1_110(pTest110, 0, ParamState_m110 (5)),
   671 
   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
   675 
   676 		// back in hookfunc
   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
   679 
   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
   685 
   686 		// back in hookfunc
   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
   689 
   690 		// hookfunc is returning:
   691 		new State_ObjCCtor_Called(13),		// copy to temp object in our stack
   692 
   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),
   697 		
   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
   701 
   702 		NULL), "Test" "11" " Part2");
   703 	
   704 	CHECK_COND(obj13 == 0, "Test" "11" " Part 2.1");
   705 	g_Inside_LeafFunc = false;
   706 
   707 	THGM_REMOVE_HOOK(11, 1);
   708 
   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);
   714 
   715 	PtrBuf_Clear();
   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),
   723 
   724 		// Calling delegate1
   725 			new State_Deleg1_110(pTest110, 0, ParamState_m110 (5)),
   726 
   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
   730 
   731 		// back in hookfunc
   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
   734 
   735 
   736 		// Calling delegate2
   737 			new State_Deleg2_110(pTest110, 1, ParamState_m110 (5)),
   738 
   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
   742 
   743 		// back in hookfunc
   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
   746 
   747 		// hookfunc finds out that the hook wanted to SUPERCEDE --> copy to override_Ret
   748 		new State_ObjAssignOp_Called(13),
   749 
   750 		// SUPERCEDE -> orig function is not called
   751 		// instead: orig_ret = override_ret
   752 		new State_ObjAssignOp_Called(13),
   753 
   754 		// Calling delegate3
   755 			new State_Deleg3_110(pTest110, 2, ParamState_m110 (5)),
   756 
   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
   760 
   761 		// back in hookfunc
   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
   764 
   765 		// Calling delegate4
   766 			new State_Deleg4_110(pTest110, 3, ParamState_m110 (5)),
   767 
   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
   771 
   772 		// back in hookfunc
   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
   775 
   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),
   778 
   779 		// hookfunc is returning:
   780 		new State_ObjCCtor_Called(13),		// copy to temp object in our stack
   781 
   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),
   786 
   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
   790 
   791 		NULL), "Test" "11" " Part3");
   792 
   793 	CHECK_COND(obj13 == 4, "Test" "11" " Part 3.1");
   794 	g_Inside_LeafFunc = false;
   795 
   796 	THGM_REMOVE_HOOK(110, 1);
   797 	THGM_REMOVE_HOOK(110, 2);
   798 	THGM_REMOVE_HOOK(110, 3);
   799 	THGM_REMOVE_HOOK(110, 4);
   800 
   801 
   802 	// RefRet
   803 	THGM_DO_TEST(111, ());
   804 
   805 	// Shutdown now!
   806 	// If we don't SH will auto-shutdown _after_ genc's destructor is called
   807 	//  -> crash
   808 
   809 	Test_CompleteShutdown(g_SHPtr);
   810 
   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!");
   815 
   816 	return true;
   817 }
   818 
   819 bool TestCPageAlloc(std::string &error)
   820 {
   821 	using namespace SourceHook;
   822 
   823 	CPageAlloc alloc;
   824 	int i;
   825 	size_t ps = alloc.GetPageSize();
   826 
   827 	char *test1[8];
   828 
   829 	for (i = 0; i < 8; ++i)
   830 		test1[i] = (char*) alloc.Alloc(ps / 4);
   831 
   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");
   835 
   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");
   839 
   840 	void *test2 = alloc.Alloc(ps * 3);
   841 
   842 	alloc.SetRW(test2);
   843 	memset(test2, 0, ps * 3);			// should not crash :)
   844 	alloc.SetRE(test2);
   845 
   846 	alloc.Free(test2);
   847 
   848 	// Dealloc a ps/4 block and place two ps/8 blocks into it
   849 	alloc.Free(test1[2]);
   850 
   851 	char *test3[2];
   852 	test3[0] = (char*) alloc.Alloc(ps / 8);
   853 	test3[1] = (char*) alloc.Alloc(ps / 8);
   854 
   855 	CHECK_COND(test3[0] == test1[2], "Part 2.1");
   856 	CHECK_COND(test3[1] == test1[2] + ps/8, "Part 2.2");
   857 
   858 	// Isolated
   859 	char *test4[8];
   860 	for (i = 0; i < 8; ++i)
   861 		test4[i] = (char*) alloc.AllocIsolated(ps / 4);
   862 
   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");
   867 
   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");
   871 
   872 	// Thus i can set everything except for test4[2] to RE and still write to test4[2]
   873 
   874 	alloc.SetRW(test4[2]);
   875 
   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]);
   883 
   884 	memset((void*)test4[2], 0, ps / 4);
   885 
   886 	return true;
   887 }