sourcehook/sh_memfuncinfo.h
author Pavol Marko <pavolmarko@gmail.com>
Thu Apr 14 12:54:13 2005 +0000 (2005-04-14)
changeset 1 586787053c73
child 3 5a9efd949f83
permissions -rw-r--r--
Initial import to CVS
     1 /* ======== SourceHook ========
     2 * By PM
     3 * No warranties of any kind
     4 *
     5 * This file provides a way for getting information about a member function.
     6 * ============================
     7 */
     8 
     9 
    10 
    11 #ifndef __SHINT_MEMFUNC_INFO_H__
    12 #define __SHINT_MEMFUNC_INFO_H__
    13 
    14 namespace SourceHook
    15 {
    16 
    17 	// Don Clugston:
    18 	//		implicit_cast< >
    19 	// I believe this was originally going to be in the C++ standard but 
    20 	// was left out by accident. It's even milder than static_cast.
    21 	// I use it instead of static_cast<> to emphasize that I'm not doing
    22 	// anything nasty. 
    23 	// Usage is identical to static_cast<>
    24 	template <class OutputClass, class InputClass>
    25 	inline OutputClass implicit_cast(InputClass input){
    26 		return input;
    27 	}
    28 
    29 
    30 	struct MemFuncInfo
    31 	{
    32 		bool isVirtual;		// Is the function virtual?
    33 		int thisptroffs;	// The this pointer the function expects to be called with
    34 							// If -1, you need to call the GetFuncInfo_GetThisPtr function
    35 		int vtblindex;		// The function's index in the vtable (0-based, 1=second entry, 2=third entry, ...)
    36 		int vtbloffs;		// The vtable pointer
    37 	};
    38 
    39 	// Ideas by Don Clugston.
    40 	// Check out his excellent paper: http://www.codeproject.com/cpp/FastDelegate.asp
    41 
    42 	template<int N> struct MFI_Impl
    43 	{
    44 		template<class MFP> static inline void GetFuncInfo(MFP *mfp, MemFuncInfo &out)
    45 		{
    46 			static char weird_memfunc_pointer_exclamation_mark_arrow_error[N-1000];
    47 		}
    48 	}; 
    49 
    50 # if SH_COMP == SH_COMP_GCC
    51 
    52 	template<> struct MFI_Impl<8>   // All of these have size==8
    53 	{
    54 		struct GCC_MemFunPtr
    55 		{
    56 			union
    57 			{
    58 				void *funcadr;				// always even
    59 				int vtable_index_plus1;		//  = vindex+1, always odd
    60 			};
    61 			int delta;
    62 		};
    63 		template<class MFP> static inline void GetFuncInfo(MFP *mfp, MemFuncInfo &out)
    64 		{
    65 			GCC_MemFunPtr *mfp_detail = (GCC_MemFunPtr*)mfp;
    66 			out.thisptroffs = mfp_detail->delta;
    67 			if (mfp_detail->vtable_index_plus1 & 1)
    68 			{
    69 				out.vtblindex = (mfp_detail->vtable_index_plus1 - 1) / 4;
    70 				out.vtbloffs = 0;
    71 				out.isVirtual = true;
    72 			}
    73 			else
    74 				out.isVirtual = false;
    75 		}
    76 	}; 
    77 
    78 # elif SH_COMP == SH_COMP_MSVC
    79 
    80 	namespace
    81 	{
    82 		int MFI_GetVtblOffset(void *mfp)
    83 		{
    84 			unsigned char *addr = (unsigned char*)mfp;
    85 			if (*addr == 0xE9)		// Jmp
    86 			{
    87 				// May or may not be!
    88 				// Check where it'd jump
    89 				addr += 5 /*size of the instruction*/ + *(unsigned long*)(addr + 1);
    90 			}
    91 	
    92 			// Check whether it's a virtual function call
    93 			// They look like this:
    94 			// 004125A0 8B 01            mov         eax,dword ptr [ecx] 
    95 			// 004125A2 FF 60 04         jmp         dword ptr [eax+4]
    96 			//		==OR==
    97 			// 00411B80 8B 01            mov         eax,dword ptr [ecx] 
    98 			// 00411B82 FF A0 18 03 00 00 jmp         dword ptr [eax+318h] 
    99 			if (*addr++ == 0x8B && *addr++ == 0x01 && *addr++ == 0xFF)
   100 			{
   101 				if (*addr == 0x60)
   102 				{
   103 					return *++addr / 4;
   104 				}
   105 				else if (*addr == 0xA0)
   106 				{
   107 					return *((unsigned int*)++addr) / 4;
   108 				}
   109 				else if (*addr == 0x20)
   110 					return 0;
   111 				else
   112 					return -1;
   113 			}
   114 			return -1;
   115 		}
   116 	}
   117 
   118 	template<> struct MFI_Impl<4>   // simple ones
   119 	{
   120 		template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
   121 		{
   122 			out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
   123 			out.isVirtual = out.vtblindex >= 0 ? true : false;
   124 			out.thisptroffs = 0;
   125 			out.vtbloffs = 0;
   126 		}
   127 	};
   128 
   129 	template<> struct MFI_Impl<8>   // more complicated ones!
   130 	{
   131 		struct MSVC_MemFunPtr2
   132 		{
   133 			void *funcadr;
   134 			int delta;
   135 		};
   136 		template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
   137 		{
   138 			out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
   139 			out.isVirtual = out.vtblindex >= 0 ? true : false;
   140 			out.thisptr = reinterpret_cast<MSVC_MemFunPtr2*>(&mfp)->delta;
   141 			out.vtbloffs = 0;
   142 		}
   143 	};
   144 
   145 	// By Don Clugston, adapted
   146 	template<> struct MFI_Impl<12>   // WOW IT"S GETTING BIGGER OMGOMOGMG
   147 	{
   148 		class __single_inheritance GenericClass;
   149 		class GenericClass {};
   150 
   151 		struct MicrosoftVirtualMFP {
   152 			void (GenericClass::*codeptr)(); // points to the actual member function
   153 			int delta;		// #bytes to be added to the 'this' pointer
   154 			int vtable_index; // or 0 if no virtual inheritance
   155 		};
   156 
   157 		struct GenericVirtualClass : virtual public GenericClass
   158 		{
   159 			typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)();
   160 			GenericVirtualClass * GetThis() { return this; }
   161 		};
   162 
   163 		template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
   164 		{
   165 			out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
   166 			out.isVirtual = out.vtblindex >= 0 ? true : false;
   167 			// This pointer
   168 			union {
   169 				MFP func;
   170 				GenericClass* (T::*ProbeFunc)();
   171 				MicrosoftVirtualMFP s;
   172 			} u;
   173 			u.func = mfp;
   174 			union {
   175 				GenericVirtualClass::ProbePtrType virtfunc;
   176 				MicrosoftVirtualMFP s;
   177 			} u2;
   178 
   179 			printf("%d %d", sizeof(mfp), sizeof(u.ProbeFunc));
   180 			// Check that the horrible_cast<>s will work
   181 			typedef int ERROR_CantUsehorrible_cast[sizeof(mfp)==sizeof(u.s)
   182 				&& sizeof(mfp)==sizeof(u.ProbeFunc)
   183 				&& sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1];
   184 			// Unfortunately, taking the address of a MF prevents it from being inlined, so 
   185 			// this next line can't be completely optimised away by the compiler.
   186 			u2.virtfunc = &GenericVirtualClass::GetThis;
   187 			u.s.codeptr = u2.s.codeptr;
   188 			out.thisptroffs = (reinterpret_cast<T*>(NULL)->*u.ProbeFunc)();
   189 			out.vtbloffs = 0;
   190 		}
   191 	};
   192 
   193 	// Don: Nasty hack for Microsoft and Intel (IA32 and Itanium)
   194 	// unknown_inheritance classes go here 
   195 	// This is probably the ugliest bit of code I've ever written. Look at the casts!
   196 	// There is a compiler bug in MSVC6 which prevents it from using this code.
   197 	template<> struct MFI_Impl<16>   // THE BIGGEST ONE!!!1GABEN
   198 	{
   199 		template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
   200 		{
   201 			out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
   202 			out.isVirtual = out.vtblindex >= 0 ? true : false;
   203 
   204 			// The member function pointer is 16 bytes long. We can't use a normal cast, but
   205 			// we can use a union to do the conversion.
   206 			union {
   207 				MFP func;
   208 				// In VC++ and ICL, an unknown_inheritance member pointer 
   209 				// is internally defined as:
   210 				struct {
   211 					void *m_funcaddress; // points to the actual member function
   212 					int delta;		// #bytes to be added to the 'this' pointer
   213 					int vtordisp;		// #bytes to add to 'this' to find the vtable
   214 					int vtable_index; // or 0 if no virtual inheritance
   215 				} s;
   216 			} u;
   217 			// Check that the horrible_cast will work
   218 			typedef int ERROR_CantUsehorrible_cast[sizeof(u.func)==sizeof(u.s)? 1 : -1];
   219 			u.func = mfp;
   220 			int virtual_delta = 0;
   221 			if (u.s.vtable_index) { // Virtual inheritance is used
   222 				/*
   223 				// First, get to the vtable. 
   224 				// It is 'vtordisp' bytes from the start of the class.
   225 				int * vtable = *reinterpret_cast<int **>(
   226 					reinterpret_cast<char *>(thisptr) + u.s.vtordisp );
   227 
   228 				// 'vtable_index' tells us where in the table we should be looking.
   229 				virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>( 
   230 					reinterpret_cast<const char *>(vtable) + u.s.vtable_index);
   231 			// The int at 'virtual_delta' gives us the amount to add to 'this'.
   232 			// Finally we can add the three components together. Phew!
   233 			out.thisptr = reinterpret_cast<void *>(
   234 				reinterpret_cast<char *>(thisptr) + u.s.delta + virtual_delta);
   235 				*/
   236 				out.vtbloffs = u.s.vtordisp;
   237 				out.thisptroffs = -1;
   238 			}
   239 			else
   240 			{
   241 				out.vtbloffs = out.vtblindex < 0 ? 0 : u.s.delta;
   242 				out.thisptroffs = u.s.delta;
   243 			}
   244 		};
   245 	};
   246 # else
   247 #  error Unsupported compiler
   248 # endif
   249 
   250 	template<class X> inline void GetFuncInfo(X mfp, MemFuncInfo &out)
   251 	{
   252 		MFI_Impl<sizeof(mfp)>::GetFuncInfo(mfp, out);
   253 	}
   254 
   255 }
   256 
   257 #endif