sourcehook/sh_memfuncinfo.h
author David Anderson <dvander@alliedmods.net>
Fri Apr 22 14:29:25 2005 +0000 (2005-04-22)
changeset 34 11e62a912700
parent 10 406d8e5ddfc4
child 37 f4d069676dc2
permissions -rw-r--r--
Cleaned up files for linux
Disabled SH_CCC_Makegate call for linux builds
     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<8>   // All of these have size==8
    58 	{
    59 		struct GCC_MemFunPtr
    60 		{
    61 			union
    62 			{
    63 				void *funcadr;				// always even
    64 				int vtable_index_plus1;		//  = vindex+1, always odd
    65 			};
    66 			int 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) / 4;
    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 			if (*addr++ == 0x8B && *addr++ == 0x01 && *addr++ == 0xFF)
   105 			{
   106 				if (*addr == 0x60)
   107 				{
   108 					return *++addr / 4;
   109 				}
   110 				else if (*addr == 0xA0)
   111 				{
   112 					return *((unsigned int*)++addr) / 4;
   113 				}
   114 				else if (*addr == 0x20)
   115 					return 0;
   116 				else
   117 					return -1;
   118 			}
   119 			return -1;
   120 		}
   121 	}
   122 
   123 	template<> struct MFI_Impl<4>   // simple ones
   124 	{
   125 		template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
   126 		{
   127 			out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
   128 			out.isVirtual = out.vtblindex >= 0 ? true : false;
   129 			out.thisptroffs = 0;
   130 			out.vtbloffs = 0;
   131 		}
   132 	};
   133 
   134 	template<> struct MFI_Impl<8>   // more complicated ones!
   135 	{
   136 		struct MSVC_MemFunPtr2
   137 		{
   138 			void *funcadr;
   139 			int delta;
   140 		};
   141 		template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
   142 		{
   143 			out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
   144 			out.isVirtual = out.vtblindex >= 0 ? true : false;
   145 			out.thisptroffs = reinterpret_cast<MSVC_MemFunPtr2*>(&mfp)->delta;
   146 			out.vtbloffs = 0;
   147 		}
   148 	};
   149 
   150 	// By Don Clugston, adapted
   151 	template<> struct MFI_Impl<12>   // WOW IT"S GETTING BIGGER OMGOMOGMG
   152 	{
   153 		class __single_inheritance GenericClass;
   154 		class GenericClass {};
   155 
   156 		struct MicrosoftVirtualMFP {
   157 			void (GenericClass::*codeptr)(); // points to the actual member function
   158 			int delta;		// #bytes to be added to the 'this' pointer
   159 			int vtable_index; // or 0 if no virtual inheritance
   160 		};
   161 
   162 		struct GenericVirtualClass : virtual public GenericClass
   163 		{
   164 			typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)();
   165 			GenericVirtualClass * GetThis() { return this; }
   166 		};
   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 			// This pointer
   173 			/*
   174 			union {
   175 				MFP func;
   176 				GenericClass* (T::*ProbeFunc)();
   177 				MicrosoftVirtualMFP s;
   178 			} u;
   179 			u.func = mfp;
   180 			union {
   181 				GenericVirtualClass::ProbePtrType virtfunc;
   182 				MicrosoftVirtualMFP s;
   183 			} u2;
   184 
   185 			// Check that the horrible_cast<>s will work
   186 			typedef int ERROR_CantUsehorrible_cast[sizeof(mfp)==sizeof(u.s)
   187 				&& sizeof(mfp)==sizeof(u.ProbeFunc)
   188 				&& sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1];
   189 			// Unfortunately, taking the address of a MF prevents it from being inlined, so 
   190 			// this next line can't be completely optimised away by the compiler.
   191 			u2.virtfunc = &GenericVirtualClass::GetThis;
   192 			u.s.codeptr = u2.s.codeptr;
   193 			out.thisptroffs = (reinterpret_cast<T*>(NULL)->*u.ProbeFunc)();
   194 			*/
   195 			out.thisptroffs = -1;
   196 			out.vtbloffs = 0;
   197 		}
   198 	};
   199 
   200 	// Don: Nasty hack for Microsoft and Intel (IA32 and Itanium)
   201 	// unknown_inheritance classes go here 
   202 	// This is probably the ugliest bit of code I've ever written. Look at the casts!
   203 	// There is a compiler bug in MSVC6 which prevents it from using this code.
   204 	template<> struct MFI_Impl<16>   // THE BIGGEST ONE!!!1GABEN
   205 	{
   206 		template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
   207 		{
   208 			out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
   209 			out.isVirtual = out.vtblindex >= 0 ? true : false;
   210 
   211 			// The member function pointer is 16 bytes long. We can't use a normal cast, but
   212 			// we can use a union to do the conversion.
   213 			union {
   214 				MFP func;
   215 				// In VC++ and ICL, an unknown_inheritance member pointer 
   216 				// is internally defined as:
   217 				struct {
   218 					void *m_funcaddress; // points to the actual member function
   219 					int delta;		// #bytes to be added to the 'this' pointer
   220 					int vtordisp;		// #bytes to add to 'this' to find the vtable
   221 					int vtable_index; // or 0 if no virtual inheritance
   222 				} s;
   223 			} u;
   224 			// Check that the horrible_cast will work
   225 			typedef int ERROR_CantUsehorrible_cast[sizeof(u.func)==sizeof(u.s)? 1 : -1];
   226 			u.func = mfp;
   227 			int virtual_delta = 0;
   228 			if (u.s.vtable_index) { // Virtual inheritance is used
   229 				/*
   230 				// First, get to the vtable. 
   231 				// It is 'vtordisp' bytes from the start of the class.
   232 				int * vtable = *reinterpret_cast<int **>(
   233 					reinterpret_cast<char *>(thisptr) + u.s.vtordisp );
   234 
   235 				// 'vtable_index' tells us where in the table we should be looking.
   236 				virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>( 
   237 					reinterpret_cast<const char *>(vtable) + u.s.vtable_index);
   238 			// The int at 'virtual_delta' gives us the amount to add to 'this'.
   239 			// Finally we can add the three components together. Phew!
   240 			out.thisptr = reinterpret_cast<void *>(
   241 				reinterpret_cast<char *>(thisptr) + u.s.delta + virtual_delta);
   242 				*/
   243 				out.vtbloffs = u.s.vtordisp;
   244 				out.thisptroffs = -1;
   245 			}
   246 			else
   247 			{
   248 				out.vtbloffs = out.vtblindex < 0 ? 0 : u.s.delta;
   249 				out.thisptroffs = u.s.delta;
   250 			}
   251 		};
   252 	};
   253 # else
   254 #  error Unsupported compiler
   255 # endif
   256 
   257 	template<class X> inline void GetFuncInfo(X mfp, MemFuncInfo &out)
   258 	{
   259 		MFI_Impl<sizeof(mfp)>::GetFuncInfo(mfp, out);
   260 	}
   261 
   262 }
   263 
   264 #endif
   265