core/sourcehook/test/test3.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Dec 10 00:59:50 2009 -0600 (2009-12-10)
changeset 689 4065ba5e9155
parent 539 db914129ae01
permissions -rw-r--r--
Rewrote user message caching code to always use IServerGameDLL::GetUserMessageInfo (bug 4117, r=dvander).

Games where the above function can crash are handled by detouring tier0's Error() function and jumping back to our own code. So in theory this should work on all games now.
     1 #include <string>
     2 #include "sourcehook_test.h"
     3 #include "testevents.h"
     4 
     5 // TEST3
     6 // Tests with inheritance / thisptroffsets
     7 
     8 namespace
     9 {
    10 	StateList g_States;
    11 	SourceHook::ISourceHook *g_SHPtr;
    12 	SourceHook::Plugin g_PLID;
    13 
    14 	MAKE_STATE_1(State_Func1_Called, void*);			// param1: This pointer
    15 	MAKE_STATE_1(State_Func2_Called, void*);			// param1: This pointer
    16 	MAKE_STATE_1(State_Func3_Called, void*);			// param1: This pointer
    17 
    18 	MAKE_STATE_1(State_Func1H_Called, void*);
    19 	MAKE_STATE_1(State_Func2H_Called, void*);
    20 	MAKE_STATE_1(State_Func3H_Called, void*);
    21 
    22 	class Base1
    23 	{
    24 		int a;
    25 	public:
    26 		virtual void Func1()
    27 		{
    28 			ADD_STATE(State_Func1_Called(reinterpret_cast<void*>(this)));
    29 		}
    30 	};
    31 	class Base2
    32 	{
    33 		int b;
    34 	public:
    35 		virtual void Func2()
    36 		{
    37 			ADD_STATE(State_Func2_Called(reinterpret_cast<void*>(this)));
    38 		}
    39 	};
    40 	class Derived : public Base1, public Base2
    41 	{
    42 		int c;
    43 	public:
    44 		virtual void Func3()
    45 		{
    46 			ADD_STATE(State_Func3_Called(reinterpret_cast<void*>(this)));
    47 		}
    48 	};
    49 
    50 	SH_DECL_HOOK0_void(Derived, Func1, SH_NOATTRIB, 0);
    51 	SH_DECL_HOOK0_void(Derived, Func2, SH_NOATTRIB, 0);
    52 	SH_DECL_HOOK0_void(Derived, Func3, SH_NOATTRIB, 0);
    53 	SH_DECL_HOOK0_void(Base1, Func1, SH_NOATTRIB, 0);
    54 	SH_DECL_HOOK0_void(Base2, Func2, SH_NOATTRIB, 0);
    55 
    56 	void Handler_Func1()
    57 	{
    58 		ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
    59 	}
    60 	void Handler_Func2()
    61 	{
    62 		ADD_STATE(State_Func2H_Called(META_IFACEPTR(void)));
    63 	}
    64 	void Handler_Func3()
    65 	{
    66 		ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
    67 	}
    68 
    69 	Derived *MyInstanceFactory()
    70 	{
    71 		return new Derived;
    72 	}
    73 }
    74 
    75 bool TestThisPtrOffs(std::string &error)
    76 {
    77 	GET_SHPTR(g_SHPtr);
    78 	g_PLID = 1337;
    79 
    80 	Derived *pD = MyInstanceFactory();
    81 	Base1 *pB1 = pD;
    82 	Base2 *pB2 = pD;
    83 
    84 	CAutoPtrDestruction<Derived> apd(pD);
    85 
    86 
    87 	// It should be:
    88 	// pB1 = pD
    89 	// pB2 > pB1
    90 
    91 	// 1)
    92 	//  Get a callclass for pD
    93 	//  Verify whether the this pointers are correct
    94 	//  Also call them normally to make sure that we aren't messing it up ;)
    95 	SourceHook::CallClass<Derived> *pD_CC = SH_GET_CALLCLASS(pD);
    96 
    97 	SH_CALL(pD_CC, &Derived::Func1)();
    98 	SH_CALL(pD_CC, &Derived::Func2)();
    99 	SH_CALL(pD_CC, &Derived::Func3)();
   100 	pD->Func1();
   101 	pD->Func2();
   102 	pD->Func3();
   103 
   104 	CHECK_STATES((&g_States,
   105 		new State_Func1_Called(pB1),
   106 		new State_Func2_Called(pB2),
   107 		new State_Func3_Called(pD),
   108 		new State_Func1_Called(pB1),
   109 		new State_Func2_Called(pB2),
   110 		new State_Func3_Called(pD),
   111 		NULL), "Part 1");
   112 
   113 	SH_CALL(pD_CC, &Base1::Func1)();
   114 	SH_CALL(pD_CC, &Base2::Func2)();
   115 
   116 	CHECK_STATES((&g_States,
   117 		new State_Func1_Called(pB1),
   118 		new State_Func2_Called(pB2),
   119 		NULL), "Part 1.1");
   120 
   121 	// 2)
   122 	//   Get callclasses for the other ones and verify it as well
   123 
   124 	SourceHook::CallClass<Base1> *pB1_CC = SH_GET_CALLCLASS(pB1);
   125 	SourceHook::CallClass<Base2> *pB2_CC = SH_GET_CALLCLASS(pB2);
   126 
   127 	SH_CALL(pB1_CC, &Base1::Func1)();
   128 	SH_CALL(pB2_CC, &Base2::Func2)();
   129 
   130 	CHECK_STATES((&g_States,
   131 		new State_Func1_Called(pB1),
   132 		new State_Func2_Called(pB2),
   133 		NULL), "Part 2");
   134 
   135 
   136 	// 3) Add hooks on them (referring to them through pD1 / Derived)
   137 	//   Check whether the hooks are called with the correct this pointer
   138 
   139 	SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   140 	SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   141 	SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   142 
   143 	pD->Func1();
   144 	pD->Func2();
   145 	pD->Func3();
   146 	pB1->Func1();
   147 	pB2->Func2();
   148 
   149 	// The handlers should always be called with the pointer to Derived
   150 	CHECK_STATES((&g_States,
   151 		new State_Func1H_Called(pD),
   152 		new State_Func1_Called(pB1),
   153 		new State_Func2H_Called(pD),
   154 		new State_Func2_Called(pB2),
   155 		new State_Func3H_Called(pD),
   156 		new State_Func3_Called(pD),
   157 		new State_Func1H_Called(pD),
   158 		new State_Func1_Called(pB1),
   159 		new State_Func2H_Called(pD),
   160 		new State_Func2_Called(pB2),
   161 		NULL), "Part 3");
   162 
   163 	SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   164 	SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   165 	SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   166 
   167 	// 4)
   168 	//   Now add the hooks on Base1 and Base2 and check again
   169 
   170 	// Note that the new implicit_cast should convert the pD to Base1*/Base2* :)
   171 	SH_ADD_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
   172 	SH_ADD_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
   173 	SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   174 
   175 	pD->Func1();
   176 	pD->Func2();
   177 	pD->Func3();
   178 	pB1->Func1();
   179 	pB2->Func2();
   180 
   181 	// This time, the handlers for Func1 should be called with pB1 and the handlers
   182 	// for Func2 should be called with pB2
   183 	CHECK_STATES((&g_States,
   184 		new State_Func1H_Called(pB1),
   185 		new State_Func1_Called(pB1),
   186 		new State_Func2H_Called(pB2),
   187 		new State_Func2_Called(pB2),
   188 		new State_Func3H_Called(pD),
   189 		new State_Func3_Called(pD),
   190 		new State_Func1H_Called(pB1),
   191 		new State_Func1_Called(pB1),
   192 		new State_Func2H_Called(pB2),
   193 		new State_Func2_Called(pB2),
   194 		NULL), "Part 4");
   195 
   196 	SH_REMOVE_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
   197 	SH_REMOVE_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
   198 	SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   199 
   200 
   201 	// 5)
   202 	//   Add some hooks, and use callclasses
   203 
   204 	// 5.1) First off, add all of them on pD
   205 	SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   206 	SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   207 	SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   208 
   209 	pD->Func1();
   210 	pD->Func2();
   211 	pD->Func3();
   212 
   213 	CHECK_STATES((&g_States,
   214 		new State_Func1H_Called(pD),
   215 		new State_Func1_Called(pB1),
   216 		new State_Func2H_Called(pD),
   217 		new State_Func2_Called(pB2),
   218 		new State_Func3H_Called(pD),
   219 		new State_Func3_Called(pD),
   220 		NULL), "Part 5.1");
   221 
   222 	SH_CALL(pD_CC, &Derived::Func1)();
   223 	SH_CALL(pD_CC, &Derived::Func2)();
   224 	SH_CALL(pD_CC, &Derived::Func3)();
   225 	SH_CALL(pB1_CC, &Base1::Func1)();
   226 	SH_CALL(pB2_CC, &Base2::Func2)();
   227 
   228 	CHECK_STATES((&g_States,
   229 		new State_Func1_Called(pB1),
   230 		new State_Func2_Called(pB2),
   231 		new State_Func3_Called(pD),
   232 		new State_Func1_Called(pB1),
   233 		new State_Func2_Called(pB2),
   234 		NULL), "Part 5.2");
   235 
   236 	SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   237 	SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   238 	SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   239 
   240 	SH_RELEASE_CALLCLASS(pB1_CC);
   241 	SH_RELEASE_CALLCLASS(pB2_CC);
   242 	SH_RELEASE_CALLCLASS(pD_CC);
   243 
   244 	return true;
   245 }