sourcehook/generate/sh_memfuncinfo.hxx
author David Anderson <dvander@alliedmods.net>
Tue Sep 13 16:47:42 2005 +0000 (2005-09-13)
changeset 101 7d93aa4d2bf3
parent 62 90513b7e3d99
child 196 25d98645f668
permissions -rw-r--r--
No more STL! Imported TinyHash
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2005 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): Pavol "PM OnoTo" Marko
     8 * ============================
     9 */
    10 
    11 /**
    12  * @brief This file provides a way for getting information about a member function.
    13  * @file sh_memfuncinfo.h
    14  */
    15 
    16 #ifndef __SHINT_MEMFUNC_INFO_H__
    17 #define __SHINT_MEMFUNC_INFO_H__
    18 
    19 namespace SourceHook
    20 {
    21 
    22 	// Don Clugston:
    23 	//		implicit_cast< >
    24 	// I believe this was originally going to be in the C++ standard but 
    25 	// was left out by accident. It's even milder than static_cast.
    26 	// I use it instead of static_cast<> to emphasize that I'm not doing
    27 	// anything nasty. 
    28 	// Usage is identical to static_cast<>
    29 	template <class OutputClass, class InputClass>
    30 	inline OutputClass implicit_cast(InputClass input){
    31 		return input;
    32 	}
    33 
    34 
    35 	struct MemFuncInfo
    36 	{
    37 		bool isVirtual;		// Is the function virtual?
    38 		int thisptroffs;	// The this pointer the function expects to be called with
    39 							// If -1, you need to call the GetFuncInfo_GetThisPtr function
    40 		int vtblindex;		// The function's index in the vtable (0-based, 1=second entry, 2=third entry, ...)
    41 		int vtbloffs;		// The vtable pointer
    42 	};
    43 
    44 	// Ideas by Don Clugston.
    45 	// Check out his excellent paper: http://www.codeproject.com/cpp/FastDelegate.asp
    46 
    47 	template<int N> struct MFI_Impl
    48 	{
    49 		template<class MFP> static inline void GetFuncInfo(MFP *mfp, MemFuncInfo &out)
    50 		{
    51 			static char weird_memfunc_pointer_exclamation_mark_arrow_error[N-1000];
    52 		}
    53 	}; 
    54 
    55 # if SH_COMP == SH_COMP_GCC
    56 
    57 	template<> struct MFI_Impl<2*SH_PTRSIZE>   // All of these have size==8/16
    58 	{
    59 		struct GCC_MemFunPtr
    60 		{
    61 			union
    62 			{
    63 				void *funcadr;				// always even
    64 				intptr_t vtable_index_plus1;		//  = vindex+1, always odd
    65 			};
    66 			intptr_t delta;
    67 		};
    68 		template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
    69 		{
    70 			GCC_MemFunPtr *mfp_detail = (GCC_MemFunPtr*)&mfp;
    71 			out.thisptroffs = mfp_detail->delta;
    72 			if (mfp_detail->vtable_index_plus1 & 1)
    73 			{
    74 				out.vtblindex = (mfp_detail->vtable_index_plus1 - 1) / SH_PTRSIZE;
    75 				out.vtbloffs = 0;
    76 				out.isVirtual = true;
    77 			}
    78 			else
    79 				out.isVirtual = false;
    80 		}
    81 	}; 
    82 
    83 # elif SH_COMP == SH_COMP_MSVC
    84 
    85 	namespace
    86 	{
    87 		int MFI_GetVtblOffset(void *mfp)
    88 		{
    89 			unsigned char *addr = (unsigned char*)mfp;
    90 			if (*addr == 0xE9)		// Jmp
    91 			{
    92 				// May or may not be!
    93 				// Check where it'd jump
    94 				addr += 5 /*size of the instruction*/ + *(unsigned long*)(addr + 1);
    95 			}
    96 	
    97 			// Check whether it's a virtual function call
    98 			// They look like this:
    99 			// 004125A0 8B 01            mov         eax,dword ptr [ecx] 
   100 			// 004125A2 FF 60 04         jmp         dword ptr [eax+4]
   101 			//		==OR==
   102 			// 00411B80 8B 01            mov         eax,dword ptr [ecx] 
   103 			// 00411B82 FF A0 18 03 00 00 jmp         dword ptr [eax+318h]
   104 
   105 			// However, for vararg functions, they look like this:
   106 			// 0048F0B0 8B 44 24 04      mov         eax,dword ptr [esp+4]
   107 			// 0048F0B4 8B 00            mov         eax,dword ptr [eax]
   108 			// 0048F0B6 FF 60 08         jmp         dword ptr [eax+8]
   109 			//		==OR==
   110 			// 0048F0B0 8B 44 24 04      mov         eax,dword ptr [esp+4]
   111 			// 0048F0B4 8B 00            mov         eax,dword ptr [eax]
   112 			// 00411B82 FF A0 18 03 00 00 jmp         dword ptr [eax+318h]
   113 			
   114 			// With varargs, the this pointer is passed as if it was the first argument
   115 
   116 			bool ok = false;
   117 			if (addr[0] == 0x8B && addr[1] == 0x44 && addr[2] == 0x24 && addr[3] == 0x04 &&
   118 				addr[4] == 0x8B && addr[5] == 0x00)
   119 			{
   120 				addr += 6;
   121 				ok = true;
   122 			}
   123 			else if (addr[0] == 0x8B && addr[1] == 0x01)
   124 			{
   125 				addr += 2;
   126 				ok = true;
   127 			}
   128 			if (!ok)
   129 				return -1;
   130 
   131 			if (*addr++ == 0xFF)
   132 			{
   133 				if (*addr == 0x60)
   134 				{
   135 					return *++addr / 4;
   136 				}
   137 				else if (*addr == 0xA0)
   138 				{
   139 					return *((unsigned int*)++addr) / 4;
   140 				}
   141 				else if (*addr == 0x20)
   142 					return 0;
   143 				else
   144 					return -1;
   145 			}
   146 			return -1;
   147 		}
   148 	}
   149 
   150 	template<> struct MFI_Impl<1*SH_PTRSIZE>   // simple ones
   151 	{
   152 		template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
   153 		{
   154 			out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
   155 			out.isVirtual = out.vtblindex >= 0 ? true : false;
   156 			out.thisptroffs = 0;
   157 			out.vtbloffs = 0;
   158 		}
   159 	};
   160 
   161 	template<> struct MFI_Impl<2*SH_PTRSIZE>   // more complicated ones!
   162 	{
   163 		struct MSVC_MemFunPtr2
   164 		{
   165 			void *funcadr;
   166 			int delta;
   167 		};
   168 		template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
   169 		{
   170 			out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
   171 			out.isVirtual = out.vtblindex >= 0 ? true : false;
   172 			out.thisptroffs = reinterpret_cast<MSVC_MemFunPtr2*>(&mfp)->delta;
   173 			out.vtbloffs = 0;
   174 		}
   175 	};
   176 
   177 	// By Don Clugston, adapted
   178 	template<> struct MFI_Impl<3*SH_PTRSIZE>   // WOW IT"S GETTING BIGGER OMGOMOGMG
   179 	{
   180 		class __single_inheritance GenericClass;
   181 		class GenericClass {};
   182 
   183 		struct MicrosoftVirtualMFP {
   184 			void (GenericClass::*codeptr)(); // points to the actual member function
   185 			int delta;		// #bytes to be added to the 'this' pointer
   186 			int vtable_index; // or 0 if no virtual inheritance
   187 		};
   188 
   189 		struct GenericVirtualClass : virtual public GenericClass
   190 		{
   191 			typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)();
   192 			GenericVirtualClass * GetThis() { return this; }
   193 		};
   194 
   195 		template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
   196 		{
   197 			out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
   198 			out.isVirtual = out.vtblindex >= 0 ? true : false;
   199 			// This pointer
   200 			/*
   201 			union {
   202 				MFP func;
   203 				GenericClass* (T::*ProbeFunc)();
   204 				MicrosoftVirtualMFP s;
   205 			} u;
   206 			u.func = mfp;
   207 			union {
   208 				GenericVirtualClass::ProbePtrType virtfunc;
   209 				MicrosoftVirtualMFP s;
   210 			} u2;
   211 
   212 			// Check that the horrible_cast<>s will work
   213 			typedef int ERROR_CantUsehorrible_cast[sizeof(mfp)==sizeof(u.s)
   214 				&& sizeof(mfp)==sizeof(u.ProbeFunc)
   215 				&& sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1];
   216 			// Unfortunately, taking the address of a MF prevents it from being inlined, so 
   217 			// this next line can't be completely optimised away by the compiler.
   218 			u2.virtfunc = &GenericVirtualClass::GetThis;
   219 			u.s.codeptr = u2.s.codeptr;
   220 			out.thisptroffs = (reinterpret_cast<T*>(NULL)->*u.ProbeFunc)();
   221 			*/
   222 			out.thisptroffs = -1;
   223 			out.vtbloffs = 0;
   224 		}
   225 	};
   226 
   227 	// Don: Nasty hack for Microsoft and Intel (IA32 and Itanium)
   228 	// unknown_inheritance classes go here 
   229 	// This is probably the ugliest bit of code I've ever written. Look at the casts!
   230 	// There is a compiler bug in MSVC6 which prevents it from using this code.
   231 	template<> struct MFI_Impl<4*SH_PTRSIZE>   // THE BIGGEST ONE!!!1GABEN
   232 	{
   233 		template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
   234 		{
   235 			out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
   236 			out.isVirtual = out.vtblindex >= 0 ? true : false;
   237 
   238 			// The member function pointer is 16 bytes long. We can't use a normal cast, but
   239 			// we can use a union to do the conversion.
   240 			union {
   241 				MFP func;
   242 				// In VC++ and ICL, an unknown_inheritance member pointer 
   243 				// is internally defined as:
   244 				struct {
   245 					void *m_funcaddress; // points to the actual member function
   246 					int delta;		// #bytes to be added to the 'this' pointer
   247 					int vtordisp;		// #bytes to add to 'this' to find the vtable
   248 					int vtable_index; // or 0 if no virtual inheritance
   249 				} s;
   250 			} u;
   251 			// Check that the horrible_cast will work
   252 			typedef int ERROR_CantUsehorrible_cast[sizeof(u.func)==sizeof(u.s)? 1 : -1];
   253 			u.func = mfp;
   254 			int virtual_delta = 0;
   255 			if (u.s.vtable_index) { // Virtual inheritance is used
   256 				/*
   257 				// First, get to the vtable. 
   258 				// It is 'vtordisp' bytes from the start of the class.
   259 				int * vtable = *reinterpret_cast<int **>(
   260 					reinterpret_cast<char *>(thisptr) + u.s.vtordisp );
   261 
   262 				// 'vtable_index' tells us where in the table we should be looking.
   263 				virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>( 
   264 					reinterpret_cast<const char *>(vtable) + u.s.vtable_index);
   265 			// The int at 'virtual_delta' gives us the amount to add to 'this'.
   266 			// Finally we can add the three components together. Phew!
   267 			out.thisptr = reinterpret_cast<void *>(
   268 				reinterpret_cast<char *>(thisptr) + u.s.delta + virtual_delta);
   269 				*/
   270 				out.vtbloffs = u.s.vtordisp;
   271 				out.thisptroffs = -1;
   272 			}
   273 			else
   274 			{
   275 				out.vtbloffs = out.vtblindex < 0 ? 0 : u.s.delta;
   276 				out.thisptroffs = u.s.delta;
   277 			}
   278 		};
   279 	};
   280 # else
   281 #  error Unsupported compiler
   282 # endif
   283 
   284 	// This version does not take a this pointer
   285 	// Useful for hookdecls, as they ensure that mfp is correct through a static_cast
   286 	template<class X> inline void GetFuncInfo(X mfp, MemFuncInfo &out)
   287 	{
   288 		MFI_Impl<sizeof(mfp)>::GetFuncInfo(mfp, out);
   289 	}
   290 
   291 	// Versions which do take a this
   292 @VARARGS@
   293 	template<class X, class Y, class RetType@, @@class Param%%|, @>
   294 	inline void GetFuncInfo(Y *ptr, RetType(X::*mfp)(@Param%%|, @), MemFuncInfo &out)
   295 	{
   296 		RetType(Y::*mfp2)(@Param%%|, @) = mfp;
   297 		MFI_Impl<sizeof(mfp2)>::GetFuncInfo(mfp2, out);
   298 	}
   299 
   300 	template<class X, class Y, class RetType@, @@class Param%%|, @>
   301 		inline void GetFuncInfo(Y *ptr, RetType(X::*mfp)(@Param%%|, @) const, MemFuncInfo &out)
   302 	{
   303 		RetType(Y::*mfp2)(@Param%%|, @) const = mfp;
   304 		MFI_Impl<sizeof(mfp2)>::GetFuncInfo(mfp2, out);
   305 	}
   306 
   307 @ENDARGS@
   308 
   309 	// GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
   310 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
   311 
   312 @VARARGS@
   313 	template<class X, class Y, class RetType@, @@class Param%%|, @>
   314 	inline void GetFuncInfo(Y *ptr, RetType(X::*mfp)(@Param%%|, @@, @...), MemFuncInfo &out)
   315 	{
   316 		RetType(Y::*mfp2)(@Param%%|, @@, @...) = mfp;
   317 		MFI_Impl<sizeof(mfp2)>::GetFuncInfo(mfp2, out);
   318 	}
   319 
   320 	template<class X, class Y, class RetType@, @@class Param%%|, @>
   321 		inline void GetFuncInfo(Y *ptr, RetType(X::*mfp)(@Param%%|, @@, @...) const, MemFuncInfo &out)
   322 	{
   323 		RetType(Y::*mfp2)(@Param%%|, @@, @...) const = mfp;
   324 		MFI_Impl<sizeof(mfp2)>::GetFuncInfo(mfp2, out);
   325 	}
   326 
   327 @ENDARGS@
   328 
   329 #endif
   330 
   331 }
   332 
   333 #endif
   334