1.1 --- a/sourcehook/generate/sh_memfuncinfo.hxx Thu Oct 04 19:46:28 2007 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,332 +0,0 @@
1.4 -/* ======== SourceHook ========
1.5 -* Copyright (C) 2004-2007 Metamod:Source Development Team
1.6 -* No warranties of any kind
1.7 -*
1.8 -* License: zlib/libpng
1.9 -*
1.10 -* Author(s): Pavol "PM OnoTo" Marko
1.11 -* ============================
1.12 -*/
1.13 -
1.14 -/**
1.15 - * @brief This file provides a way for getting information about a member function.
1.16 - * @file sh_memfuncinfo.h
1.17 - */
1.18 -
1.19 -#ifndef __SHINT_MEMFUNC_INFO_H__
1.20 -#define __SHINT_MEMFUNC_INFO_H__
1.21 -
1.22 -namespace SourceHook
1.23 -{
1.24 -
1.25 - // Don Clugston:
1.26 - // implicit_cast< >
1.27 - // I believe this was originally going to be in the C++ standard but
1.28 - // was left out by accident. It's even milder than static_cast.
1.29 - // I use it instead of static_cast<> to emphasize that I'm not doing
1.30 - // anything nasty.
1.31 - // Usage is identical to static_cast<>
1.32 - template <class OutputClass, class InputClass>
1.33 - inline OutputClass implicit_cast(InputClass input){
1.34 - return input;
1.35 - }
1.36 -
1.37 -
1.38 - struct MemFuncInfo
1.39 - {
1.40 - bool isVirtual; // Is the function virtual?
1.41 - int thisptroffs; // The this pointer the function expects to be called with
1.42 - // If -1, you need to call the GetFuncInfo_GetThisPtr function
1.43 - int vtblindex; // The function's index in the vtable (0-based, 1=second entry, 2=third entry, ...)
1.44 - int vtbloffs; // The vtable pointer
1.45 - };
1.46 -
1.47 - // Ideas by Don Clugston.
1.48 - // Check out his excellent paper: http://www.codeproject.com/cpp/FastDelegate.asp
1.49 -
1.50 - template<int N> struct MFI_Impl
1.51 - {
1.52 - template<class MFP> static inline void GetFuncInfo(MFP *mfp, MemFuncInfo &out)
1.53 - {
1.54 - static char weird_memfunc_pointer_exclamation_mark_arrow_error[N-1000];
1.55 - }
1.56 - };
1.57 -
1.58 -# if SH_COMP == SH_COMP_GCC
1.59 -
1.60 - template<> struct MFI_Impl<2*SH_PTRSIZE> // All of these have size==8/16
1.61 - {
1.62 - struct GCC_MemFunPtr
1.63 - {
1.64 - union
1.65 - {
1.66 - void *funcadr; // always even
1.67 - intptr_t vtable_index_plus1; // = vindex+1, always odd
1.68 - };
1.69 - intptr_t delta;
1.70 - };
1.71 - template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
1.72 - {
1.73 - GCC_MemFunPtr *mfp_detail = (GCC_MemFunPtr*)&mfp;
1.74 - out.thisptroffs = mfp_detail->delta;
1.75 - if (mfp_detail->vtable_index_plus1 & 1)
1.76 - {
1.77 - out.vtblindex = (mfp_detail->vtable_index_plus1 - 1) / SH_PTRSIZE;
1.78 - out.vtbloffs = 0;
1.79 - out.isVirtual = true;
1.80 - }
1.81 - else
1.82 - out.isVirtual = false;
1.83 - }
1.84 - };
1.85 -
1.86 -# elif SH_COMP == SH_COMP_MSVC
1.87 -
1.88 - namespace
1.89 - {
1.90 - int MFI_GetVtblOffset(void *mfp)
1.91 - {
1.92 - unsigned char *addr = (unsigned char*)mfp;
1.93 - if (*addr == 0xE9) // Jmp
1.94 - {
1.95 - // May or may not be!
1.96 - // Check where it'd jump
1.97 - addr += 5 /*size of the instruction*/ + *(unsigned long*)(addr + 1);
1.98 - }
1.99 -
1.100 - // Check whether it's a virtual function call
1.101 - // They look like this:
1.102 - // 004125A0 8B 01 mov eax,dword ptr [ecx]
1.103 - // 004125A2 FF 60 04 jmp dword ptr [eax+4]
1.104 - // ==OR==
1.105 - // 00411B80 8B 01 mov eax,dword ptr [ecx]
1.106 - // 00411B82 FF A0 18 03 00 00 jmp dword ptr [eax+318h]
1.107 -
1.108 - // However, for vararg functions, they look like this:
1.109 - // 0048F0B0 8B 44 24 04 mov eax,dword ptr [esp+4]
1.110 - // 0048F0B4 8B 00 mov eax,dword ptr [eax]
1.111 - // 0048F0B6 FF 60 08 jmp dword ptr [eax+8]
1.112 - // ==OR==
1.113 - // 0048F0B0 8B 44 24 04 mov eax,dword ptr [esp+4]
1.114 - // 0048F0B4 8B 00 mov eax,dword ptr [eax]
1.115 - // 00411B82 FF A0 18 03 00 00 jmp dword ptr [eax+318h]
1.116 -
1.117 - // With varargs, the this pointer is passed as if it was the first argument
1.118 -
1.119 - bool ok = false;
1.120 - if (addr[0] == 0x8B && addr[1] == 0x44 && addr[2] == 0x24 && addr[3] == 0x04 &&
1.121 - addr[4] == 0x8B && addr[5] == 0x00)
1.122 - {
1.123 - addr += 6;
1.124 - ok = true;
1.125 - }
1.126 - else if (addr[0] == 0x8B && addr[1] == 0x01)
1.127 - {
1.128 - addr += 2;
1.129 - ok = true;
1.130 - }
1.131 - if (!ok)
1.132 - return -1;
1.133 -
1.134 - if (*addr++ == 0xFF)
1.135 - {
1.136 - if (*addr == 0x60)
1.137 - {
1.138 - return *++addr / 4;
1.139 - }
1.140 - else if (*addr == 0xA0)
1.141 - {
1.142 - return *((unsigned int*)++addr) / 4;
1.143 - }
1.144 - else if (*addr == 0x20)
1.145 - return 0;
1.146 - else
1.147 - return -1;
1.148 - }
1.149 - return -1;
1.150 - }
1.151 - }
1.152 -
1.153 - template<> struct MFI_Impl<1*SH_PTRSIZE> // simple ones
1.154 - {
1.155 - template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
1.156 - {
1.157 - out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
1.158 - out.isVirtual = out.vtblindex >= 0 ? true : false;
1.159 - out.thisptroffs = 0;
1.160 - out.vtbloffs = 0;
1.161 - }
1.162 - };
1.163 -
1.164 - template<> struct MFI_Impl<2*SH_PTRSIZE> // more complicated ones!
1.165 - {
1.166 - struct MSVC_MemFunPtr2
1.167 - {
1.168 - void *funcadr;
1.169 - int delta;
1.170 - };
1.171 - template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
1.172 - {
1.173 - out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
1.174 - out.isVirtual = out.vtblindex >= 0 ? true : false;
1.175 - out.thisptroffs = reinterpret_cast<MSVC_MemFunPtr2*>(&mfp)->delta;
1.176 - out.vtbloffs = 0;
1.177 - }
1.178 - };
1.179 -
1.180 - // By Don Clugston, adapted
1.181 - template<> struct MFI_Impl<3*SH_PTRSIZE> // WOW IT"S GETTING BIGGER OMGOMOGMG
1.182 - {
1.183 - class __single_inheritance GenericClass;
1.184 - class GenericClass {};
1.185 -
1.186 - struct MicrosoftVirtualMFP {
1.187 - void (GenericClass::*codeptr)(); // points to the actual member function
1.188 - int delta; // #bytes to be added to the 'this' pointer
1.189 - int vtable_index; // or 0 if no virtual inheritance
1.190 - };
1.191 -
1.192 - struct GenericVirtualClass : virtual public GenericClass
1.193 - {
1.194 - typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)();
1.195 - GenericVirtualClass * GetThis() { return this; }
1.196 - };
1.197 -
1.198 - template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
1.199 - {
1.200 - out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
1.201 - out.isVirtual = out.vtblindex >= 0 ? true : false;
1.202 - // This pointer
1.203 - /*
1.204 - union {
1.205 - MFP func;
1.206 - GenericClass* (T::*ProbeFunc)();
1.207 - MicrosoftVirtualMFP s;
1.208 - } u;
1.209 - u.func = mfp;
1.210 - union {
1.211 - GenericVirtualClass::ProbePtrType virtfunc;
1.212 - MicrosoftVirtualMFP s;
1.213 - } u2;
1.214 -
1.215 - // Check that the horrible_cast<>s will work
1.216 - typedef int ERROR_CantUsehorrible_cast[sizeof(mfp)==sizeof(u.s)
1.217 - && sizeof(mfp)==sizeof(u.ProbeFunc)
1.218 - && sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1];
1.219 - // Unfortunately, taking the address of a MF prevents it from being inlined, so
1.220 - // this next line can't be completely optimised away by the compiler.
1.221 - u2.virtfunc = &GenericVirtualClass::GetThis;
1.222 - u.s.codeptr = u2.s.codeptr;
1.223 - out.thisptroffs = (reinterpret_cast<T*>(NULL)->*u.ProbeFunc)();
1.224 - */
1.225 - out.thisptroffs = -1;
1.226 - out.vtbloffs = 0;
1.227 - }
1.228 - };
1.229 -
1.230 - // Don: Nasty hack for Microsoft and Intel (IA32 and Itanium)
1.231 - // unknown_inheritance classes go here
1.232 - // This is probably the ugliest bit of code I've ever written. Look at the casts!
1.233 - // There is a compiler bug in MSVC6 which prevents it from using this code.
1.234 - template<> struct MFI_Impl<4*SH_PTRSIZE> // THE BIGGEST ONE!!!1GABEN
1.235 - {
1.236 - template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
1.237 - {
1.238 - out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
1.239 - out.isVirtual = out.vtblindex >= 0 ? true : false;
1.240 -
1.241 - // The member function pointer is 16 bytes long. We can't use a normal cast, but
1.242 - // we can use a union to do the conversion.
1.243 - union {
1.244 - MFP func;
1.245 - // In VC++ and ICL, an unknown_inheritance member pointer
1.246 - // is internally defined as:
1.247 - struct {
1.248 - void *m_funcaddress; // points to the actual member function
1.249 - int delta; // #bytes to be added to the 'this' pointer
1.250 - int vtordisp; // #bytes to add to 'this' to find the vtable
1.251 - int vtable_index; // or 0 if no virtual inheritance
1.252 - } s;
1.253 - } u;
1.254 - // Check that the horrible_cast will work
1.255 - typedef int ERROR_CantUsehorrible_cast[sizeof(u.func)==sizeof(u.s)? 1 : -1];
1.256 - u.func = mfp;
1.257 - int virtual_delta = 0;
1.258 - if (u.s.vtable_index) { // Virtual inheritance is used
1.259 - /*
1.260 - // First, get to the vtable.
1.261 - // It is 'vtordisp' bytes from the start of the class.
1.262 - int * vtable = *reinterpret_cast<int **>(
1.263 - reinterpret_cast<char *>(thisptr) + u.s.vtordisp );
1.264 -
1.265 - // 'vtable_index' tells us where in the table we should be looking.
1.266 - virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>(
1.267 - reinterpret_cast<const char *>(vtable) + u.s.vtable_index);
1.268 - // The int at 'virtual_delta' gives us the amount to add to 'this'.
1.269 - // Finally we can add the three components together. Phew!
1.270 - out.thisptr = reinterpret_cast<void *>(
1.271 - reinterpret_cast<char *>(thisptr) + u.s.delta + virtual_delta);
1.272 - */
1.273 - out.vtbloffs = u.s.vtordisp;
1.274 - out.thisptroffs = -1;
1.275 - }
1.276 - else
1.277 - {
1.278 - out.vtbloffs = out.vtblindex < 0 ? 0 : u.s.delta;
1.279 - out.thisptroffs = u.s.delta;
1.280 - }
1.281 - };
1.282 - };
1.283 -# else
1.284 -# error Unsupported compiler
1.285 -# endif
1.286 -
1.287 - // This version does not take a this pointer
1.288 - // Useful for hookdecls, as they ensure that mfp is correct through a static_cast
1.289 - template<class X> inline void GetFuncInfo(X mfp, MemFuncInfo &out)
1.290 - {
1.291 - MFI_Impl<sizeof(mfp)>::GetFuncInfo(mfp, out);
1.292 - }
1.293 -
1.294 - // Versions which do take a this
1.295 -@[$1,0,$a:
1.296 - template<class X, class Y, class RetType@[$1!=0:, @]@[$2,1,$1|, :class Param$2@]>
1.297 - inline void GetFuncInfo(Y *ptr, RetType(X::*mfp)(@[$2,1,$1|, :Param$2@]), MemFuncInfo &out)
1.298 - {
1.299 - RetType(Y::*mfp2)(@[$2,1,$1|, :Param$2@]) = mfp;
1.300 - MFI_Impl<sizeof(mfp2)>::GetFuncInfo(mfp2, out);
1.301 - }
1.302 -
1.303 - template<class X, class Y, class RetType@[$1!=0:, @]@[$2,1,$1|, :class Param$2@]>
1.304 - inline void GetFuncInfo(Y *ptr, RetType(X::*mfp)(@[$2,1,$1|, :Param$2@]) const, MemFuncInfo &out)
1.305 - {
1.306 - RetType(Y::*mfp2)(@[$2,1,$1|, :Param$2@]) const = mfp;
1.307 - MFI_Impl<sizeof(mfp2)>::GetFuncInfo(mfp2, out);
1.308 - }
1.309 -@]
1.310 -
1.311 - // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1.312 -#if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1.313 -
1.314 -@[$1,0,$a:
1.315 - template<class X, class Y, class RetType@[$1!=0:, @]@[$2,1,$1|, :class Param$2@]>
1.316 - inline void GetFuncInfo(Y *ptr, RetType(X::*mfp)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...), MemFuncInfo &out)
1.317 - {
1.318 - RetType(Y::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...) = mfp;
1.319 - MFI_Impl<sizeof(mfp2)>::GetFuncInfo(mfp2, out);
1.320 - }
1.321 -
1.322 - template<class X, class Y, class RetType@[$1!=0:, @]@[$2,1,$1|, :class Param$2@]>
1.323 - inline void GetFuncInfo(Y *ptr, RetType(X::*mfp)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...) const, MemFuncInfo &out)
1.324 - {
1.325 - RetType(Y::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...) const = mfp;
1.326 - MFI_Impl<sizeof(mfp2)>::GetFuncInfo(mfp2, out);
1.327 - }
1.328 -
1.329 -@]
1.330 -
1.331 -#endif
1.332 -
1.333 -}
1.334 -
1.335 -#endif