sourcehook/test/testhookmangen.cpp
author Pavol Marko <pavolmarko@gmail.com>
Sat Nov 17 09:53:58 2007 +0000 (2007-11-17)
branchhookman_autogen
changeset 461 2fa049f36721
parent 459 53e363661d53
child 463 7a413a63426f
permissions -rw-r--r--
New hookfunc local vars code; first step to vafmt support
     1 #include <string>
     2 #include "sourcehook.h"
     3 #include "sourcehook_test.h"
     4 #include "testevents.h"
     5 #include "sh_memory.h"
     6 #include "sh_pagealloc.h"
     7 #include "sourcehook_pibuilder.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 	SourceHook::IHookManagerAutoGen *g_HMAGPtr;
    27 
    28 	// PtrBuf(ptr) gives ptrs unique numbers
    29 	// in the order they appear
    30 	SourceHook::List<const void*> g_PtrHash;
    31 
    32 	bool g_Inside_LeafFunc = false;			// inside a hook or a func
    33 
    34 
    35 	// POD / Object types
    36 	template <int MYSIZE>
    37 	struct POD
    38 	{
    39 		char x[MYSIZE];
    40 
    41 		bool operator==(const POD<MYSIZE> &other)
    42 		{
    43 			return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
    44 		}
    45 
    46 		bool operator==(char other)
    47 		{
    48 			for (int i = 0; i < MYSIZE; ++i)
    49 			{
    50 				if (x[i] != other)
    51 					return false;
    52 			}
    53 			return true;
    54 		}
    55 	};
    56 
    57 	template <int MYSIZE>
    58 		std::ostream& operator <<(std::ostream &os, const POD<MYSIZE> &obj)
    59 	{
    60 		os << "Some POD!";
    61 		return os;
    62 	}
    63 
    64 	MAKE_STATE_1(State_ObjOCtor_Called, int /*MYSIZE*/);
    65 	MAKE_STATE_1(State_ObjCCtor_Called, int /*MYSIZE*/);
    66 	MAKE_STATE_1(State_ObjODtor_Called, int /*MYSIZE*/);
    67 	MAKE_STATE_1(State_ObjAssignOp_Called, int /*MYSIZE*/);
    68 
    69 	template <int MYSIZE>
    70 	struct Object
    71 	{
    72 		char x[MYSIZE];
    73 
    74 		Object(char initch)
    75 		{
    76 			memset(reinterpret_cast<void*>(x), initch, MYSIZE);
    77 			if (!g_Inside_LeafFunc)
    78 				ADD_STATE(State_ObjOCtor_Called(MYSIZE));
    79 		}
    80 
    81 		Object()
    82 		{
    83 			if (!g_Inside_LeafFunc)
    84 				ADD_STATE(State_ObjOCtor_Called(MYSIZE));
    85 		}
    86 
    87 		Object(const Object<MYSIZE> & other)
    88 		{
    89 			memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
    90 			if (!g_Inside_LeafFunc)
    91 				ADD_STATE(State_ObjCCtor_Called(MYSIZE));
    92 		}
    93 
    94 		~Object()
    95 		{
    96 			if (!g_Inside_LeafFunc)
    97 				ADD_STATE(State_ObjODtor_Called(MYSIZE));
    98 		}
    99 
   100 		Object & operator = (const Object &other)
   101 		{
   102 			if (!g_Inside_LeafFunc)
   103 				ADD_STATE(State_ObjAssignOp_Called(MYSIZE));
   104 
   105 			memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
   106 
   107 			return *this;
   108 		}
   109 
   110 		bool operator==(const Object<MYSIZE> &other)
   111 		{
   112 			return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
   113 		}
   114 	};
   115 
   116 	template <int MYSIZE>
   117 	std::ostream& operator <<(std::ostream &os, const Object<MYSIZE> &obj)
   118 	{
   119 		os << "Some Obj" << static_cast<int>(obj.x[0]);
   120 		return os;
   121 	}
   122 
   123 	// Because of some weird bug in MSVC < 1400
   124 #define MAKE_PODRET(size) \
   125 	struct PodRet##size \
   126 	{ \
   127 		POD<size> actPod; \
   128 		bool operator==(const PodRet##size &other) { return actPod == other.actPod; } \
   129 		bool operator==(char other) { return actPod == other; } \
   130 	}; \
   131 	std::ostream& operator <<(std::ostream &os, const PodRet##size &obj) \
   132 	{ \
   133 		os << obj.actPod; \
   134 		return os; \
   135 	} \
   136 	template <> struct MakeRet< PodRet##size > \
   137 	{ \
   138 		static PodRet##size Do(int a) \
   139 		{ \
   140 			PodRet##size x; \
   141 			memset(reinterpret_cast<void*>(x.actPod.x), a, size); \
   142 			return x; \
   143 		} \
   144 	};
   145 
   146 #define MAKE_OBJRET(size) \
   147 	struct ObjRet##size \
   148 	{ \
   149 		Object<size> actObj; \
   150 		bool operator==(const ObjRet##size &other) { return actObj == other.actObj; } \
   151 		bool operator==(char other) { return actObj == other; } \
   152 	}; \
   153 	std::ostream& operator <<(std::ostream &os, const ObjRet##size &obj) \
   154 	{ \
   155 		os << obj.actObj; \
   156 		return os; \
   157 	} \
   158 	template <> struct MakeRet< ObjRet##size > \
   159 	{ \
   160 		static ObjRet##size Do(int a) NO_OPTIMIZE \
   161 		{ \
   162 			ObjRet##size *x = new ObjRet##size; /* ptr: otherwise gcc optimizes away the temp obj */ \
   163 			CAutoPtrDestruction< ObjRet##size > apd(x); \
   164 			memset(reinterpret_cast<void*>(x->actObj.x), a, size); \
   165 			return *x; \
   166 		} \
   167 	};
   168 
   169 
   170 
   171 	// "Increment" something:  (reproducible change for recall tests)
   172 	//  integer: ++
   173 	//  float: += 1.3
   174 	//  pod/object: x[i]++, 0 <= i < MYSIZE
   175 	template <class T>
   176 	struct Increment
   177 	{
   178 		static void Incr(T &what)	
   179 		{
   180 			++what;
   181 		}
   182 	};
   183 
   184 	template<>
   185 	struct Increment<float>
   186 	{
   187 		static void Incr(float &what)	
   188 		{
   189 			what += 1.3f;
   190 		}
   191 	};
   192 
   193 	template<>
   194 	struct Increment<double>
   195 	{
   196 		static void Incr(double &what)	
   197 		{
   198 			what += 1.3;
   199 		}
   200 	};
   201 
   202 	template<int MYSIZE>
   203 	struct Increment< POD<MYSIZE> >
   204 	{
   205 		static void Incr(POD<MYSIZE> &what)	
   206 		{
   207 			for (int i = 0; i < MYSIZE; ++i)
   208 				++ what.x[i];
   209 		}
   210 	};
   211 
   212 	template<int MYSIZE>
   213 	struct Increment< Object<MYSIZE> >
   214 	{
   215 		static void Incr(Object<MYSIZE> &what)	
   216 		{
   217 			for (int i = 0; i < MYSIZE; ++i)
   218 				++ what.x[i];
   219 		}
   220 	};
   221 
   222 	#include "testhookmangen.h"
   223 
   224 	template <class T>
   225 	int PtrBuf(T ptr)
   226 	{
   227 		int a = 0;
   228 
   229 		const void *vptr = reinterpret_cast<const void*>(ptr);
   230 		for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end(); ++iter)
   231 		{
   232 			if (*iter == vptr)
   233 				return a;
   234 			else
   235 				++a;
   236 		}
   237 		g_PtrHash.push_back(vptr);
   238 		return static_cast<int>(g_PtrHash.size()) - 1;
   239 	}
   240 
   241 	template <class T>
   242 	T PtrBufPtr(T ptr)
   243 	{
   244 		PtrBuf(ptr);
   245 		return ptr;
   246 	}
   247 
   248 	void PtrBuf_Clear(int leave_in = 0)
   249 	{
   250 		for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end();)
   251 		{
   252 			if (--leave_in < 0)
   253 				iter = g_PtrHash.erase(iter);
   254 			else
   255 				++iter;
   256 		}
   257 	}
   258 
   259 	// MyDelegate base class for other delegates
   260 	class MyDelegate : public SourceHook::ISHDelegate
   261 	{
   262 		// Unneeded
   263 		//  because we don't use old SH_REMOVE_HOOK syntax
   264 		virtual bool IsEqual(SourceHook::ISHDelegate *pOtherDeleg)
   265 		{
   266 			return false;
   267 		}
   268 
   269 		virtual void DeleteThis()
   270 		{
   271 			delete this;
   272 		}
   273 	};
   274 
   275 	THGM_MAKE_TEST0_void(0);
   276 	THGM_SETUP_PI0(0);
   277 
   278 	THGM_MAKE_TEST1_void(1, char);
   279 	THGM_SETUP_PI1(1, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   280 
   281 	THGM_MAKE_TEST1_void(2, short);
   282 	THGM_SETUP_PI1(2, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   283 
   284 	THGM_MAKE_TEST1_void(3, int);
   285 	THGM_SETUP_PI1(3, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   286 
   287 	THGM_MAKE_TEST1_void(4, float);
   288 	THGM_SETUP_PI1(4, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   289 
   290 	THGM_MAKE_TEST1_void(5, double);
   291 	THGM_SETUP_PI1(5, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   292 	
   293 	THGM_MAKE_TEST5_void(6, char, short, int, float, double);
   294 	THGM_SETUP_PI5(6,
   295 		char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   296 		short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   297 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   298 		float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal,
   299 		double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal
   300 		);
   301 
   302 	THGM_MAKE_TEST2_void(7, char&, double&);
   303 	THGM_SETUP_PI2(7,
   304 		char&, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByRef,
   305 		double&, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByRef
   306 		);
   307 
   308 	THGM_MAKE_TEST1_void(8, POD<7>);
   309 	THGM_SETUP_PI1(8, POD<7>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   310 
   311 	THGM_MAKE_TEST1_void(9, POD<600>);
   312 	THGM_SETUP_PI1(9, POD<600>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   313 
   314 	THGM_MAKE_TEST1_void(10, POD<600> &);
   315 	THGM_SETUP_PI1(10, POD<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef);
   316 
   317 	THGM_MAKE_TEST2_void(11, Object<3>, Object<600>&);
   318 	THGM_SETUP_PI2(11,
   319 		Object<3>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor,
   320 		Object<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor
   321 		);
   322 
   323 	THGM_MAKE_TEST0(101, char);
   324 	THGM_SETUP_PI0(101);
   325 	THGM_SETUP_RI(101, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   326 
   327 	THGM_MAKE_TEST0(102, short);
   328 	THGM_SETUP_PI0(102);
   329 	THGM_SETUP_RI(102, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   330 
   331 	THGM_MAKE_TEST0(103, int);
   332 	THGM_SETUP_PI0(103);
   333 	THGM_SETUP_RI(103, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   334 
   335 	THGM_MAKE_TEST0(104, float);
   336 	THGM_SETUP_PI0(104);
   337 	THGM_SETUP_RI(104, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   338 
   339 	THGM_MAKE_TEST0(105, double);
   340 	THGM_SETUP_PI0(105);
   341 	THGM_SETUP_RI(105, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   342 
   343 	// pod 1-13
   344 	MAKE_PODRET(1);
   345 	THGM_MAKE_TEST1(106, PodRet1, int);
   346 	THGM_SETUP_PI1(106,
   347 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   348 		);
   349 	THGM_SETUP_RI(106, PodRet1, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   350 
   351 	MAKE_PODRET(4);
   352 	THGM_MAKE_TEST1(107, PodRet4, int);
   353 	THGM_SETUP_PI1(107,
   354 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   355 		);
   356 	THGM_SETUP_RI(107, PodRet4, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   357 
   358 	MAKE_PODRET(8);
   359 	THGM_MAKE_TEST1(108, PodRet8, int);
   360 	THGM_SETUP_PI1(108,
   361 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   362 		);
   363 	THGM_SETUP_RI(108, PodRet8, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   364 
   365 	MAKE_PODRET(13);
   366 	THGM_MAKE_TEST1(109, PodRet13, int);
   367 	THGM_SETUP_PI1(109,
   368 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   369 		);
   370 	THGM_SETUP_RI(109, PodRet13, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   371 
   372 	MAKE_OBJRET(13);
   373 	THGM_MAKE_TEST1(110, ObjRet13, int);
   374 	THGM_SETUP_PI1(110,
   375 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   376 		);
   377 	THGM_SETUP_RI(110, ObjRet13, SourceHook::PassInfo::PassType_Object,
   378 		SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
   379 		SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
   380 
   381 	MAKE_OBJRET(111);
   382 	ObjRet111 g_O111_0;
   383 	ObjRet111 g_O111_1;
   384 	ObjRet111 g_O111_2;
   385 	ObjRet111 g_O111_3;
   386 	ObjRet111 g_O111_4;
   387 
   388 	template <>
   389 	struct MakeRet< ObjRet111& >
   390 	{
   391 		static ObjRet111 &Do(int a)
   392 		{
   393 			switch (a)
   394 			{
   395 			case 0:
   396 				return g_O111_0;
   397 			case 1:
   398 				return g_O111_1;
   399 			case 2:
   400 				return g_O111_2;
   401 			case 3:
   402 				return g_O111_3;
   403 			default:
   404 				return g_O111_4;
   405 			}
   406 		}
   407 	};
   408 
   409 	THGM_MAKE_TEST0(111, ObjRet111& );
   410 	THGM_SETUP_PI0(111);
   411 	THGM_SETUP_RI(111, ObjRet111& , SourceHook::PassInfo::PassType_Object,
   412 		SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
   413 		SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
   414 
   415 	MAKE_STATE(State_Hello_Func4_Called);
   416 	MAKE_STATE(State_Hello_Func79_Called);
   417 
   418 	MAKE_STATE(State_Hello_Func4_PreHook);
   419 	MAKE_STATE(State_Hello_Func79_PreHook);
   420 
   421 	// Test for larger vtable indices
   422 	class Hello
   423 	{
   424 	public:
   425 		virtual void Func0() {}
   426 		virtual void Func1() {}
   427 		virtual void Func2() {}
   428 		virtual void Func3() {}
   429 		virtual void Func4()
   430 		{
   431 			ADD_STATE(State_Hello_Func4_Called);
   432 		}
   433 		virtual void Func5() {}
   434 		virtual void Func6() {}
   435 		virtual void Func7() {}
   436 		virtual void Func8() {}
   437 		virtual void Func9() {}
   438 		virtual void Func10() {}
   439 		virtual void Func11() {}
   440 		virtual void Func12() {}
   441 		virtual void Func13() {}
   442 		virtual void Func14() {}
   443 		virtual void Func15() {}
   444 		virtual void Func16() {}
   445 		virtual void Func17() {}
   446 		virtual void Func18() {}
   447 		virtual void Func19() {}
   448 		virtual void Func20() {}
   449 		virtual void Func21() {}
   450 		virtual void Func22() {}
   451 		virtual void Func23() {}
   452 		virtual void Func24() {}
   453 		virtual void Func25() {}
   454 		virtual void Func26() {}
   455 		virtual void Func27() {}
   456 		virtual void Func28() {}
   457 		virtual void Func29() {}
   458 		virtual void Func30() {}
   459 		virtual void Func31() {}
   460 		virtual void Func32() {}
   461 		virtual void Func33() {}
   462 		virtual void Func34() {}
   463 		virtual void Func35() {}
   464 		virtual void Func36() {}
   465 		virtual void Func37() {}
   466 		virtual void Func38() {}
   467 		virtual void Func39() {}
   468 		virtual void Func40() {}
   469 		virtual void Func41() {}
   470 		virtual void Func42() {}
   471 		virtual void Func43() {}
   472 		virtual void Func44() {}
   473 		virtual void Func45() {}
   474 		virtual void Func46() {}
   475 		virtual void Func47() {}
   476 		virtual void Func48() {}
   477 		virtual void Func49() {}
   478 		virtual void Func50() {}
   479 		virtual void Func51() {}
   480 		virtual void Func52() {}
   481 		virtual void Func53() {}
   482 		virtual void Func54() {}
   483 		virtual void Func55() {}
   484 		virtual void Func56() {}
   485 		virtual void Func57() {}
   486 		virtual void Func58() {}
   487 		virtual void Func59() {}
   488 		virtual void Func60() {}
   489 		virtual void Func61() {}
   490 		virtual void Func62() {}
   491 		virtual void Func63() {}
   492 		virtual void Func64() {}
   493 		virtual void Func65() {}
   494 		virtual void Func66() {}
   495 		virtual void Func67() {}
   496 		virtual void Func68() {}
   497 		virtual void Func69() {}
   498 		virtual void Func70() {}
   499 		virtual void Func71() {}
   500 		virtual void Func72() {}
   501 		virtual void Func73() {}
   502 		virtual void Func74() {}
   503 		virtual void Func75() {}
   504 		virtual void Func76() {}
   505 		virtual void Func77() {}
   506 		virtual void Func78() {}
   507 		virtual void Func79()
   508 		{
   509 			ADD_STATE(State_Hello_Func79_Called);
   510 		}
   511 	};
   512 	class Hello_Func4_Deleg : public MyDelegate
   513 	{
   514 		virtual void Func()
   515 		{
   516 			ADD_STATE(State_Hello_Func4_PreHook);
   517 		}
   518 	};
   519 	class Hello_Func79_Deleg : public MyDelegate
   520 	{
   521 		int a;
   522 		virtual void Func()
   523 		{
   524 			ADD_STATE(State_Hello_Func79_PreHook);
   525 		}
   526 	};
   527 }
   528 
   529 bool TestHookManGen(std::string &error)
   530 {
   531 	GET_SHPTR(g_SHPtr);
   532 	GET_HMAG(g_HMAGPtr, g_SHPtr);
   533 	g_PLID = 1337;
   534 
   535 	// 5 Global constructors (g_O111_*)
   536 	CHECK_STATES((&g_States,
   537 		new State_ObjOCtor_Called(111),
   538 		new State_ObjOCtor_Called(111),
   539 		new State_ObjOCtor_Called(111),
   540 		new State_ObjOCtor_Called(111),
   541 		new State_ObjOCtor_Called(111),
   542 		NULL), "GlobCtors");
   543 
   544 	THGM_DO_TEST_void(0, ());
   545 
   546 	THGM_DO_TEST_void(1, (100));
   547 	
   548 	THGM_DO_TEST_void(2, (0x1F00));
   549 
   550 	THGM_DO_TEST_void(3, (0x1F000000));
   551 
   552 	THGM_DO_TEST_void(4, (0.5f));
   553 
   554 	THGM_DO_TEST_void(5, (5.5));
   555 
   556 	THGM_DO_TEST_void(6, (100, 0x1f00, 0x1f000000, 0.5f, 5.5));
   557 
   558 	char a = 5;
   559 	double b = 233.33;
   560 	THGM_DO_TEST_void(7, (a, b));
   561 
   562 	POD<7> pod7 = MakeRet< POD<7> >::Do(78);
   563 	THGM_DO_TEST_void(8, (pod7));
   564 
   565 	POD<600> pod600 = MakeRet< POD<600> >::Do(34);
   566 	THGM_DO_TEST_void(9, (pod600));
   567 	
   568 	THGM_DO_TEST_void(10, (pod600));
   569 
   570 	// Test11: Special: constructors/destructors
   571 	PtrBuf_Clear();
   572 	Object<3> obj3(33);
   573 	Object<600> obj600(21);
   574 
   575 	CHECK_STATES((&g_States,
   576 		new State_ObjOCtor_Called(3),
   577 		new State_ObjOCtor_Called(600),
   578 		NULL), "Test11 Part0");
   579 
   580 	setuppi_11();
   581 	SourceHook::HookManagerPubFunc myhookman11 = g_HMAGPtr->MakeHookMan(protoinfo_11, 0, 0); \
   582 	CAutoReleaseHookMan arhm_11(myhookman11); \
   583 	int hook1_11, hook2_11, hook3_11, hook4_11;
   584 	
   585 	TestClass11 *pTest11 = new TestClass11;
   586 	CAutoPtrDestruction<TestClass11> apd11(pTest11);
   587 	
   588 	/* no hooks - no hooks */
   589 	PtrBuf_Clear();
   590 	pTest11->Func(obj3, obj600);
   591 
   592 	g_Inside_LeafFunc = true;
   593 	CHECK_STATES((&g_States,
   594 		new State_ObjCCtor_Called(3),
   595 		new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
   596 		new State_ObjODtor_Called(3),
   597 		NULL), "Test" "11" " Part1");
   598 	g_Inside_LeafFunc = false;
   599 
   600 	/* hook1 - no hooks */
   601 	THGM_ADD_HOOK(11, 1);
   602 	
   603 	PtrBuf_Clear();
   604 	pTest11->Func(obj3, obj600);
   605 	g_Inside_LeafFunc = true;
   606 	CHECK_STATES((&g_States,
   607 		new State_ObjCCtor_Called(3),
   608 
   609 		new State_ObjCCtor_Called(3),
   610 		new State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
   611 		new State_ObjODtor_Called(3),
   612 
   613 		new State_ObjCCtor_Called(3),
   614 		new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
   615 		new State_ObjODtor_Called(3),
   616 
   617 		new State_ObjODtor_Called(3),
   618 		NULL), "Test" "11" " Part2");
   619 	g_Inside_LeafFunc = false;
   620 
   621 	THGM_REMOVE_HOOK(11, 1);
   622 
   623 	/* hook1, hook2 - hook3, hook4 */
   624 	THGM_ADD_HOOK(11, 1);
   625 	THGM_ADD_HOOK(11, 2);
   626 	THGM_ADD_HOOK(11, 3);
   627 	THGM_ADD_HOOK(11, 4);
   628 	PtrBuf_Clear();
   629 	pTest11->Func(obj3, obj600);
   630 	g_Inside_LeafFunc = true;
   631 	CHECK_STATES((&g_States,
   632 		new State_ObjCCtor_Called(3),
   633 
   634 		new State_ObjCCtor_Called(3),
   635 		new	State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
   636 		new State_ObjODtor_Called(3),
   637 
   638 		new State_ObjCCtor_Called(3),
   639 		new State_Deleg2_11(pTest11, 1, ParamState_m11 (obj3, obj600)),
   640 		new State_ObjODtor_Called(3),
   641 
   642 		new State_ObjCCtor_Called(3),
   643 		new State_Deleg3_11(pTest11, 2, ParamState_m11 (obj3, obj600)),
   644 		new State_ObjODtor_Called(3),
   645 
   646 		new State_ObjCCtor_Called(3),
   647 		new State_Deleg4_11(pTest11, 3, ParamState_m11 (obj3, obj600)),
   648 		new State_ObjODtor_Called(3),
   649 
   650 		new State_ObjODtor_Called(3),
   651 	NULL), "Test" "11" " Part3");
   652 	g_Inside_LeafFunc = false;
   653 
   654 	/* hook1 - hook3, hook4 WITH RECALLS */
   655 	THGM_REMOVE_HOOK(11, 2);
   656 	PtrBuf_Clear();
   657 	TestClass11::ms_DoRecall = true;
   658 	pTest11->Func(obj3, obj600);
   659 	g_Inside_LeafFunc = true;
   660 	CHECK_STATES((&g_States,
   661 		new State_ObjCCtor_Called(3),
   662 
   663 		new State_ObjCCtor_Called(3),
   664 		new	State_Deleg1_11(pTest11, 0 /* first deleg ptr */, ParamState_m11 (obj3, obj600)),
   665 
   666 		// recall !
   667 			// second hookfunc -> new copy
   668 			new State_ObjCCtor_Called(3),
   669 
   670 			// in second hookfunc now
   671 			//   -> calls orig func
   672 
   673 			new State_ObjCCtor_Called(3),
   674 			new State_Func11(pTest11, ParamState_m11 (obj3, obj600)(1)),
   675 			new State_ObjODtor_Called(3),
   676 
   677 			// calls first posthook
   678 			new State_ObjCCtor_Called(3),
   679 			new State_Deleg3_11(pTest11, 1 /* second deleg ptr */, ParamState_m11 (obj3, obj600)(1)),
   680 
   681 			// recall!
   682 				// third hookfunc -> new copy
   683 				new State_ObjCCtor_Called(3),
   684 
   685 				// calls second posthook
   686 
   687 				new State_ObjCCtor_Called(3),
   688 				new State_Deleg4_11(pTest11, 2 /* third deleg ptr */, ParamState_m11 (obj3, obj600)(2)),
   689 
   690 				// recall!
   691 					// fourth hookfunc -> new copy
   692 					new State_ObjCCtor_Called(3),
   693 
   694 					// has nothing to do though!
   695 
   696 					// fourth hookfunc done -> ret
   697 					new State_ObjODtor_Called(3),
   698 
   699 				// third hookfunc done -> ret
   700 				new State_ObjODtor_Called(3),
   701 				// ret from hookhandler which did the recall
   702 				new State_ObjODtor_Called(3),
   703 
   704 			// second hookfunc done -> ret
   705 			new State_ObjODtor_Called(3),
   706 			// ret from hookhandler which did the recall
   707 			new State_ObjODtor_Called(3),
   708 
   709 		// deleg1's instance
   710 		new State_ObjODtor_Called(3),
   711 		// first hookfunc done -> ret
   712 		new State_ObjODtor_Called(3),
   713 		NULL), "Test" "11" " Part4");
   714 	g_Inside_LeafFunc = false;
   715 
   716 	THGM_REMOVE_HOOK(11, 1);
   717 	THGM_REMOVE_HOOK(11, 3);
   718 	THGM_REMOVE_HOOK(11, 4);
   719 
   720 	THGM_DO_TEST(101, ());
   721 
   722 	THGM_DO_TEST(102, ());
   723 
   724 	THGM_DO_TEST(103, ());
   725 
   726 	THGM_DO_TEST(104, ());
   727 
   728 	THGM_DO_TEST(105, ());
   729 
   730 	// pod returns
   731 	THGM_DO_TEST(106, (5));
   732 	THGM_DO_TEST(107, (5));
   733 	THGM_DO_TEST(108, (5));
   734 	THGM_DO_TEST(109, (5));
   735 
   736 	// Test110: Special: constructors/destructors on return
   737 	PtrBuf_Clear();
   738 	ObjRet13 obj13;
   739 
   740 	CHECK_STATES((&g_States,
   741 		new State_ObjOCtor_Called(13),
   742 		NULL), "Test110 Part0");
   743 
   744 	setuppi_110();
   745 	setupri_110();
   746 
   747 	SourceHook::HookManagerPubFunc myhookman110 = g_HMAGPtr->MakeHookMan(protoinfo_110, 0, 0); \
   748 	CAutoReleaseHookMan arhm_110(myhookman110); \
   749 	int hook1_110, hook2_110, hook3_110, hook4_110;
   750 
   751 	TestClass110 *pTest110 = new TestClass110;
   752 	CAutoPtrDestruction<TestClass110> apd110(pTest110);
   753 
   754 	/* no hooks - no hooks */
   755 	PtrBuf_Clear();
   756 	obj13 = pTest110->Func(5);
   757 
   758 	g_Inside_LeafFunc = true;
   759 	CHECK_STATES((&g_States,
   760 		new State_Func110(pTest110, ParamState_m110 (5)),
   761 		new State_ObjOCtor_Called(13),			// MakeRet: Construction of x
   762 		new State_ObjCCtor_Called(13),			// Return from MakeRet	-> construct temporary object in our stack
   763 		new State_ObjODtor_Called(13),			// MakeRet: Destruction of x
   764 
   765 		new State_ObjAssignOp_Called(13),		// assign: obj13 = temporary object in our stack
   766 		new State_ObjODtor_Called(13),			// Func110: destruction of temporary object
   767 		NULL), "Test" "110" " Part1");
   768 	g_Inside_LeafFunc = false;
   769 
   770 	/* hook1 - no hooks */
   771 	THGM_ADD_HOOK(110, 1);
   772 
   773 	PtrBuf_Clear();
   774 	obj13 = pTest110->Func(5);
   775 	g_Inside_LeafFunc = true;
   776 	CHECK_STATES((&g_States,
   777 		// HookFunc: construct orig_ret/override_ret/plugin_ret objects
   778 		new State_ObjOCtor_Called(13),
   779 		new State_ObjOCtor_Called(13),
   780 		new State_ObjOCtor_Called(13),
   781 
   782 		// Calling delegate
   783 			new State_Deleg1_110(pTest110, 0, ParamState_m110 (5)),
   784 
   785 			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   786 			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   787 			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   788 
   789 		// back in hookfunc
   790 		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   791 		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   792 
   793 		// Calling orig function Func110
   794 			new State_Func110(pTest110, ParamState_m110 (5)),
   795 			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   796 			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   797 			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   798 
   799 		// back in hookfunc
   800 		new State_ObjAssignOp_Called(13),	// assign: orig_ret = temporary object in HookFunc's stack
   801 		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   802 
   803 		// hookfunc is returning:
   804 		new State_ObjCCtor_Called(13),		// copy to temp object in our stack
   805 
   806 		// hookfunc cleans up its stack -> destroys plugin_ret/override_ret/orig_ret
   807 		new State_ObjODtor_Called(13),
   808 		new State_ObjODtor_Called(13),
   809 		new State_ObjODtor_Called(13),
   810 		
   811 		// we are in our function: assign 
   812 		new State_ObjAssignOp_Called(13),	// assign: obj13 = temporary object in our stack
   813 		new State_ObjODtor_Called(13),		// Func110: destruction of temporary object
   814 
   815 		NULL), "Test" "11" " Part2");
   816 	
   817 	CHECK_COND(obj13 == 0, "Test" "11" " Part 2.1");
   818 	g_Inside_LeafFunc = false;
   819 
   820 	THGM_REMOVE_HOOK(11, 1);
   821 
   822 	/* hook1, hook2 - hook3, hook4 */
   823 	THGM_ADD_HOOK(110, 1);
   824 	THGM_ADD_HOOK(110, 2);
   825 	THGM_ADD_HOOK(110, 3);
   826 	THGM_ADD_HOOK(110, 4);
   827 
   828 	PtrBuf_Clear();
   829 	obj13 = pTest110->Func(5);
   830 	g_Inside_LeafFunc = true;
   831 	CHECK_STATES((&g_States,
   832 		// HookFunc: construct orig_ret/override_ret/plugin_ret objects
   833 		new State_ObjOCtor_Called(13),
   834 		new State_ObjOCtor_Called(13),
   835 		new State_ObjOCtor_Called(13),
   836 
   837 		// Calling delegate1
   838 			new State_Deleg1_110(pTest110, 0, ParamState_m110 (5)),
   839 
   840 			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   841 			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   842 			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   843 
   844 		// back in hookfunc
   845 		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   846 		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   847 
   848 
   849 		// Calling delegate2
   850 			new State_Deleg2_110(pTest110, 1, ParamState_m110 (5)),
   851 
   852 			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   853 			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   854 			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   855 
   856 		// back in hookfunc
   857 		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   858 		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   859 
   860 		// hookfunc finds out that the hook wanted to SUPERCEDE --> copy to override_Ret
   861 		new State_ObjAssignOp_Called(13),
   862 
   863 		// SUPERCEDE -> orig function is not called
   864 		// instead: orig_ret = override_ret
   865 		new State_ObjAssignOp_Called(13),
   866 
   867 		// Calling delegate3
   868 			new State_Deleg3_110(pTest110, 2, ParamState_m110 (5)),
   869 
   870 			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   871 			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   872 			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   873 
   874 		// back in hookfunc
   875 		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   876 		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   877 
   878 		// Calling delegate4
   879 			new State_Deleg4_110(pTest110, 3, ParamState_m110 (5)),
   880 
   881 			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   882 			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   883 			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   884 
   885 		// back in hookfunc
   886 		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   887 		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   888 
   889 		// hookfunc finds out that the hook wanted to SUPERCEDE --> copy to override_Ret (yes really, we overwrite the old value!)
   890 		new State_ObjAssignOp_Called(13),
   891 
   892 		// hookfunc is returning:
   893 		new State_ObjCCtor_Called(13),		// copy to temp object in our stack
   894 
   895 		// hookfunc cleans up its stack -> destroys plugin_ret/override_ret/orig_ret
   896 		new State_ObjODtor_Called(13),
   897 		new State_ObjODtor_Called(13),
   898 		new State_ObjODtor_Called(13),
   899 
   900 		// we are in our function: assign 
   901 		new State_ObjAssignOp_Called(13),	// assign: obj13 = temporary object in our stack
   902 		new State_ObjODtor_Called(13),		// Func110: destruction of temporary object
   903 
   904 		NULL), "Test" "11" " Part3");
   905 
   906 	CHECK_COND(obj13 == 4, "Test" "11" " Part 3.1");
   907 	g_Inside_LeafFunc = false;
   908 
   909 	THGM_REMOVE_HOOK(110, 1);
   910 	THGM_REMOVE_HOOK(110, 2);
   911 	THGM_REMOVE_HOOK(110, 3);
   912 	THGM_REMOVE_HOOK(110, 4);
   913 
   914 
   915 	// RefRet
   916 	THGM_DO_TEST(111, ());
   917 
   918 	// Test for lange vtable indices
   919 	Hello *pHello = new Hello;
   920 	SourceHook::CProtoInfoBuilder helloPi(SourceHook::ProtoInfo::CallConv_ThisCall);
   921 	SourceHook::HookManagerPubFunc helloHM_4 = g_HMAGPtr->MakeHookMan(helloPi, 0, 4);
   922 	SourceHook::HookManagerPubFunc helloHM_79 = g_HMAGPtr->MakeHookMan(helloPi, 0, 79);
   923 
   924 	pHello->Func4();
   925 	pHello->Func79();
   926 	SH_CALL(pHello, &Hello::Func4)();
   927 	SH_CALL(pHello, &Hello::Func79)();
   928 	CHECK_STATES((&g_States,
   929 		new State_Hello_Func4_Called,
   930 		new State_Hello_Func79_Called,
   931 		new State_Hello_Func4_Called,
   932 		new State_Hello_Func79_Called,
   933 		NULL), "Test" "Hello" " Part1");
   934 
   935 	int helloHook4 = g_SHPtr->AddHook(g_PLID, SourceHook::ISourceHook::Hook_Normal, reinterpret_cast<void*>(pHello),
   936 		0, helloHM_4, new Hello_Func4_Deleg, false);
   937 
   938 	int helloHook79 = g_SHPtr->AddHook(g_PLID, SourceHook::ISourceHook::Hook_Normal, reinterpret_cast<void*>(pHello),
   939 		0, helloHM_79, new Hello_Func79_Deleg, false);
   940 
   941 	pHello->Func4();
   942 	pHello->Func79();
   943 	SH_CALL(pHello, &Hello::Func4)();
   944 	SH_CALL(pHello, &Hello::Func79)();
   945 
   946 	CHECK_STATES((&g_States,
   947 		new State_Hello_Func4_PreHook,
   948 		new State_Hello_Func4_Called,
   949 		new State_Hello_Func79_PreHook,
   950 		new State_Hello_Func79_Called,
   951 		new State_Hello_Func4_Called,
   952 		new State_Hello_Func79_Called,
   953 		NULL), "Test" "Hello" " Part2");
   954 
   955 	g_SHPtr->RemoveHookByID(helloHook4);
   956 	g_SHPtr->RemoveHookByID(helloHook79);
   957 
   958 	g_HMAGPtr->ReleaseHookMan(helloHM_4);
   959 	g_HMAGPtr->ReleaseHookMan(helloHM_79);
   960 
   961 	// Shutdown now!
   962 	// If we don't SH will auto-shutdown _after_ genc's destructor is called
   963 	//  -> crash
   964 
   965 	Test_CompleteShutdown(g_SHPtr);
   966 
   967 	CHECK_COND(sizeof(PodRet1) == 1, "WTF!");
   968 	CHECK_COND(sizeof(PodRet4) == 4, "WTF!");
   969 	CHECK_COND(sizeof(PodRet8) == 8, "WTF!");
   970 	CHECK_COND(sizeof(PodRet13) == 13, "WTF!");
   971 
   972 	return true;
   973 }
   974 
   975 bool TestCPageAlloc(std::string &error)
   976 {
   977 	using namespace SourceHook;
   978 
   979 	CPageAlloc alloc;
   980 	int i;
   981 	size_t ps = alloc.GetPageSize();
   982 
   983 	char *test1[8];
   984 
   985 	for (i = 0; i < 8; ++i)
   986 		test1[i] = (char*) alloc.Alloc(ps / 4);
   987 
   988 	CHECK_COND(test1[1] == test1[0] + ps/4, "Part 1.1");
   989 	CHECK_COND(test1[2] == test1[1] + ps/4, "Part 1.2");
   990 	CHECK_COND(test1[3] == test1[2] + ps/4, "Part 1.3");
   991 
   992 	CHECK_COND(test1[5] == test1[4] + ps/4, "Part 1.4");
   993 	CHECK_COND(test1[6] == test1[5] + ps/4, "Part 1.5");
   994 	CHECK_COND(test1[7] == test1[6] + ps/4, "Part 1.6");
   995 
   996 	void *test2 = alloc.Alloc(ps * 3);
   997 
   998 	alloc.SetRW(test2);
   999 	memset(test2, 0, ps * 3);			// should not crash :)
  1000 	alloc.SetRE(test2);
  1001 
  1002 	alloc.Free(test2);
  1003 
  1004 	// Dealloc a ps/4 block and place two ps/8 blocks into it
  1005 	alloc.Free(test1[2]);
  1006 
  1007 	char *test3[2];
  1008 	test3[0] = (char*) alloc.Alloc(ps / 8);
  1009 	test3[1] = (char*) alloc.Alloc(ps / 8);
  1010 
  1011 	CHECK_COND(test3[0] == test1[2], "Part 2.1");
  1012 	CHECK_COND(test3[1] == test1[2] + ps/8, "Part 2.2");
  1013 
  1014 	// Isolated
  1015 	char *test4[8];
  1016 	for (i = 0; i < 8; ++i)
  1017 		test4[i] = (char*) alloc.AllocIsolated(ps / 4);
  1018 
  1019 	// -> The difference is at least one page
  1020 	CHECK_COND(static_cast<size_t>(abs(test4[1] - test4[0])) >= ps, "Part 3.1");
  1021 	CHECK_COND(static_cast<size_t>(abs(test4[2] - test4[1])) >= ps, "Part 3.2");
  1022 	CHECK_COND(static_cast<size_t>(abs(test4[3] - test4[2])) >= ps, "Part 3.3");
  1023 
  1024 	CHECK_COND(static_cast<size_t>(abs(test4[5] - test4[4])) >= ps, "Part 3.4");
  1025 	CHECK_COND(static_cast<size_t>(abs(test4[6] - test4[5])) >= ps, "Part 3.5");
  1026 	CHECK_COND(static_cast<size_t>(abs(test4[7] - test4[6])) >= ps, "Part 3.6");
  1027 
  1028 	// Thus i can set everything except for test4[2] to RE and still write to test4[2]
  1029 
  1030 	alloc.SetRW(test4[2]);
  1031 
  1032 	alloc.SetRE(test4[0]);
  1033 	alloc.SetRE(test4[1]);
  1034 	alloc.SetRE(test4[3]);
  1035 	alloc.SetRE(test4[4]);
  1036 	alloc.SetRE(test4[5]);
  1037 	alloc.SetRE(test4[6]);
  1038 	alloc.SetRE(test4[7]);
  1039 
  1040 	memset((void*)test4[2], 0, ps / 4);
  1041 
  1042 	return true;
  1043 }