core/sourcehook/test/testoddthunks.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Thu Apr 25 11:31:26 2013 -0400 (2013-04-25)
changeset 836 31afabdc2b32
parent 733 2365240861a2
permissions -rw-r--r--
Triggering build.
     1 #include <string>
     2 #include "sourcehook_test.h"
     3 #include "sh_pagealloc.h"
     4 #include "testevents.h"
     5 
     6 namespace
     7 {
     8 	StateList g_States;
     9 	SourceHook::ISourceHook *g_SHPtr;
    10 	SourceHook::Plugin g_PLID;
    11 
    12 	MAKE_STATE(State_Func_Called);
    13 	MAKE_STATE(State_Pre1_Called);
    14 	MAKE_STATE(State_Pre2_Called);
    15 
    16 	int g_CallNumber;
    17 
    18 	class Test
    19 	{
    20 	public:
    21 		virtual void Func()
    22 		{
    23 			ADD_STATE(State_Func_Called);
    24 		}
    25 	};
    26 
    27 	Test *g_pInst;
    28 
    29 	void Handler_Func_Pre1()
    30 	{
    31 		ADD_STATE(State_Pre1_Called);
    32 	}
    33 
    34 	SH_DECL_HOOK0_void(Test, Func, SH_NOATTRIB, 0);
    35 
    36 	void Handler_Func_Pre2()
    37 	{
    38 		ADD_STATE(State_Pre2_Called);
    39 
    40 		++g_CallNumber;
    41 		if (g_CallNumber == 2)
    42 		{
    43 			// REMOVE ourselves
    44 			SH_REMOVE_HOOK(Test, Func, g_pInst, SH_STATIC(Handler_Func_Pre1), false);
    45 			SH_REMOVE_HOOK(Test, Func, g_pInst, SH_STATIC(Handler_Func_Pre2), false);
    46 		}
    47 
    48 		// Call again.
    49 		g_pInst->Func();
    50 	}
    51 
    52 	Test *MyInstanceFactory()
    53 	{
    54 		return new Test;
    55 	}
    56 
    57 	SourceHook::CPageAlloc g_ThunkAllocator(2);
    58 	void *g_OddThunkMemory = NULL;
    59 	void *g_OddThunk = NULL;
    60 
    61 	// Guarantees that Func()
    62 	// has a vtable entry showing to an ODD address
    63 	void PatchFuncWithOddThunk()
    64 	{
    65 		g_OddThunkMemory = g_ThunkAllocator.Alloc(10);
    66 		unsigned char* base = reinterpret_cast<unsigned char*>(g_OddThunkMemory);
    67 
    68 		if (((ptrdiff_t)base) & 1)
    69 		{
    70 			// ODD, ok.
    71 		}
    72 		else
    73 		{
    74 			// EVEN. make odd.
    75 			base += 1;
    76 		}
    77 
    78 		// Get vtable entry pointer
    79 		SourceHook::MemFuncInfo info = {true, -1, 0, 0};
    80 		SourceHook::GetFuncInfo(g_pInst, &Test::Func, info);
    81 
    82 		void *adjustediface = NULL;
    83 		void **cur_vtptr = NULL;
    84 		void **cur_vfnptr = NULL;
    85 
    86 		adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(g_pInst) + info.thisptroffs);
    87 
    88 		cur_vtptr = *reinterpret_cast<void***>(
    89 			reinterpret_cast<char*>(adjustediface) + info.vtbloffs);
    90 		cur_vfnptr = reinterpret_cast<void**>(cur_vtptr + info.vtblindex);
    91 
    92 		
    93 		// Original function
    94 		void *origEntry = *cur_vfnptr;
    95 
    96 		// Now generate the jump code
    97 		g_ThunkAllocator.SetRW(g_OddThunkMemory);
    98 
    99 		*(base + 0) = 0xE9;		// offset jump, immediate operand
   100 		ptrdiff_t *offsetAddr = reinterpret_cast<ptrdiff_t*>(base + 1);
   101 				
   102 		// destination = src + offset + 5
   103 		// <=>  offset = destination - src - 5
   104 		*offsetAddr =
   105 			(reinterpret_cast<unsigned char*>(origEntry) - base) - 5;
   106 
   107 		g_ThunkAllocator.SetRE(g_OddThunkMemory);
   108 		
   109 		g_OddThunk = reinterpret_cast<void*>(base);
   110 
   111 		// Now set the odd thunk as new vtable entry
   112 		SourceHook::SetMemAccess((void*)cur_vfnptr, sizeof(void*), SH_MEM_READ | SH_MEM_WRITE);
   113 		*cur_vfnptr = g_OddThunk;
   114 	}
   115 
   116 	void FreeOddThunk()
   117 	{
   118 		g_ThunkAllocator.Free(g_OddThunkMemory);
   119 		g_OddThunk = NULL;
   120 	}
   121 }
   122 
   123 bool TestOddThunks(std::string &error)
   124 {
   125 	GET_SHPTR(g_SHPtr);
   126 	g_PLID = 1337;
   127 
   128 	g_CallNumber = 0;
   129 
   130 	g_pInst = MyInstanceFactory();
   131 
   132 	PatchFuncWithOddThunk();
   133 
   134 	SH_ADD_HOOK(Test, Func, g_pInst, SH_STATIC(Handler_Func_Pre1), false);
   135 	SH_ADD_HOOK(Test, Func, g_pInst, SH_STATIC(Handler_Func_Pre2), false);
   136 
   137 	g_pInst->Func();
   138 
   139 	CHECK_STATES((&g_States,
   140 		// FIRST func() call
   141 		new State_Pre1_Called(),
   142 		new State_Pre2_Called(),		// calls Func() again
   143 
   144 		// SECOND func() call
   145 		new State_Pre1_Called(),
   146 		new State_Pre2_Called(),		// removes hooks and calls Func() again
   147 
   148 		// THIRD func() call
   149 		new State_Func_Called(),
   150 
   151 		// SECOND func() call
   152 		new State_Func_Called(),
   153 
   154 		// FIRST func() call
   155 		new State_Func_Called(),
   156 		NULL), "Part 1");
   157 
   158 	delete g_pInst;
   159 	FreeOddThunk();
   160 
   161 	return true;
   162 }
   163