sourcehook/sourcehook_hookmangen.h
author Pavol Marko <pavolmarko@gmail.com>
Sun Nov 11 17:35:24 2007 +0000 (2007-11-11)
branchhookman_autogen
changeset 458 4a0c5136f5e1
parent 456 9f19985c76aa
child 461 2fa049f36721
permissions -rw-r--r--
Added CProtoInfoBuilder helper class (sourcehook_pibuilder.h)
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2007 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 #ifndef __SOURCEHOOK_HOOKMANGEN_H__
    12 #define __SOURCEHOOK_HOOKMANGEN_H__
    13 
    14 #include "sh_pagealloc.h"
    15 
    16 namespace SourceHook
    17 {
    18 	namespace Impl
    19 	{
    20 
    21 		// Code gen stuff
    22 #if SH_COMP == SH_COMP_GCC
    23 #include <stdint.h>
    24 		typedef int8_t jit_int8_t;
    25 		typedef uint8_t jit_uint8_t;
    26 		typedef int32_t jit_int32_t;
    27 		typedef uint32_t jit_uint32_t;
    28 		typedef int64_t jit_int64_t;
    29 		typedef uint64_t jit_uint64_t;
    30 #elif SH_COMP == SH_COMP_MSVC
    31 		typedef __int8 jit_int8_t;
    32 		typedef unsigned __int8 jit_uint8_t;
    33 		typedef __int32 jit_int32_t;
    34 		typedef unsigned __int32 jit_uint32_t;
    35 		typedef __int64 jit_int64_t;
    36 		typedef unsigned __int64 jit_uint64_t;
    37 #endif
    38 		typedef unsigned int jitoffs_t;
    39 		typedef signed int jitrel_t;
    40 
    41 
    42 		class GenBuffer
    43 		{
    44 			static CPageAlloc ms_Allocator;
    45 
    46 			unsigned char *m_pData;
    47 			jitoffs_t m_Size;
    48 			jitoffs_t m_AllocatedSize;
    49 
    50 		public:
    51 			GenBuffer() : m_pData(NULL), m_Size(0), m_AllocatedSize(0)
    52 			{
    53 			}
    54 			~GenBuffer()
    55 			{
    56 				clear();
    57 			}
    58 			jitoffs_t GetSize()
    59 			{
    60 				return m_Size;
    61 			}
    62 			unsigned char *GetData()
    63 			{
    64 				return m_pData;
    65 			}
    66 
    67 			template <class PT> void push(PT what)
    68 			{
    69 				push((const unsigned char *)&what, sizeof(PT));
    70 			}
    71 
    72 			void push(const unsigned char *data, jitoffs_t size)
    73 			{
    74 				jitoffs_t newSize = m_Size + size;
    75 				if (newSize > m_AllocatedSize)
    76 				{
    77 					m_AllocatedSize = newSize > m_AllocatedSize*2 ? newSize : m_AllocatedSize*2;
    78 					if (m_AllocatedSize < 64)
    79 						m_AllocatedSize = 64;
    80 
    81 					unsigned char *newBuf;
    82 					newBuf = reinterpret_cast<unsigned char*>(ms_Allocator.Alloc(m_AllocatedSize));
    83 					ms_Allocator.SetRW(newBuf);
    84 					if (!newBuf)
    85 					{
    86 						SH_ASSERT(0, ("bad_alloc: couldn't allocate 0x%08X bytes of memory\n", m_AllocatedSize));
    87 						return;
    88 					}
    89 					memset((void*)newBuf, 0xCC, m_AllocatedSize);			// :TODO: remove this !
    90 					memcpy((void*)newBuf, (const void*)m_pData, m_Size);
    91 					if (m_pData)
    92 						ms_Allocator.Free(reinterpret_cast<void*>(m_pData));
    93 					m_pData = newBuf;
    94 				}
    95 				memcpy((void*)(m_pData + m_Size), (const void*)data, size);
    96 				m_Size = newSize;
    97 			}
    98 
    99 			template <class PT> void rewrite(jitoffs_t offset, PT what)
   100 			{
   101 				rewrite(offset, (const unsigned char *)&what, sizeof(PT));
   102 			}
   103 
   104 			void rewrite(jitoffs_t offset, const unsigned char *data, jitoffs_t size)
   105 			{
   106 				SH_ASSERT(offset + size <= m_AllocatedSize, ("rewrite too far"));
   107 
   108 				memcpy((void*)(m_pData + offset), (const void*)data, size);
   109 			}
   110 
   111 			void clear()
   112 			{
   113 				if (m_pData)
   114 					ms_Allocator.Free(reinterpret_cast<void*>(m_pData));
   115 				m_pData = NULL;
   116 				m_Size = 0;
   117 				m_AllocatedSize = 0;
   118 			}
   119 
   120 			void SetRE()
   121 			{
   122 				ms_Allocator.SetRE(reinterpret_cast<void*>(m_pData));
   123 			}
   124 
   125 			operator void *()
   126 			{
   127 				return reinterpret_cast<void*>(GetData());
   128 			}
   129 
   130 			void write_ubyte(jit_uint8_t x)			{ push(x); }
   131 			void write_byte(jit_uint8_t x)			{ push(x); }
   132 			
   133 			void write_ushort(unsigned short x)		{ push(x); }
   134 			void write_short(signed short x)		{ push(x); }
   135 
   136 			void write_uint32(jit_uint32_t x)		{ push(x); }
   137 			void write_int32(jit_uint32_t x)		{ push(x); }
   138 
   139 			jitoffs_t get_outputpos()
   140 			{
   141 				return m_Size;
   142 			}
   143 
   144 			void start_count(jitoffs_t &offs)
   145 			{
   146 				offs = get_outputpos();
   147 			}
   148 			void end_count(jitoffs_t &offs)
   149 			{
   150 				offs = get_outputpos() - offs;
   151 			}
   152 		};
   153 
   154 		class GenContext
   155 		{
   156 			const static int SIZE_MWORD = 4;
   157 			const static int SIZE_PTR = sizeof(void*);
   158 			const static int PassFlag_ForcedByRef = (1<<30);   // ByVal in source, but actually passed by reference (GCC) -> private pass, destruct
   159 
   160 			HookManagerPubFunc m_GeneratedPubFunc;
   161 
   162 			CProto m_OrigProto;			// original passed-in prototype
   163 			CProto m_Proto;
   164 			int m_VtblOffs;
   165 			int m_VtblIdx;
   166 			ISourceHook *m_SHPtr;
   167 
   168 			GenBuffer m_HookFunc;
   169 			GenBuffer m_PubFunc;
   170 
   171 			ProtoInfo *m_BuiltPI;
   172 			PassInfo *m_BuiltPI_Params;
   173 			PassInfo::V2Info *m_BuiltPI_Params2;
   174 
   175 			// For hookfunc
   176 			void **m_pHI;
   177 			void **m_HookfuncVfnptr;
   178 
   179 			// Level 3 - Helpers
   180 			int m_RegCounter;
   181 			jit_int8_t NextRegEBX_ECX_EDX();
   182 
   183 			// size info
   184 			jit_int32_t GetRealSize(const IntPassInfo &info);
   185 			jit_int32_t GetStackSize(const IntPassInfo &info);
   186 			short GetParamsStackSize();		// sum(GetStackSize(param[i]), 0 <= i < numOfParams)
   187 
   188 			// Helpers
   189 			void BitwiseCopy_Setup();
   190 			void BitwiseCopy_Do(size_t size);
   191 
   192 			// Param push
   193 			short GetForcedByRefParamsSize();		// sum(param[i] is forcedbyref ? GetStackSize(param[i]) : 0, 0 <= i < numOfParams)
   194 			short GetForcedByRefParamOffset(int p);		// sum(param[i] is forcedbyref ? GetStackSize(param[i]) : 0, 0 <= i < p)
   195 			jit_int32_t PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to, 
   196 				jit_int32_t v_place_for_memret, jit_int32_t v_place_fbrr_base);		// save_ret_to and v_place_for_memret only used for memory returns
   197 			jit_int32_t PushRef(jit_int32_t param_offset, const IntPassInfo &pi);
   198 			jit_int32_t PushBasic(jit_int32_t param_offset, const IntPassInfo &pi);
   199 			jit_int32_t PushFloat(jit_int32_t param_offset, const IntPassInfo &pi);
   200 			jit_int32_t PushObject(jit_int32_t param_offset, const IntPassInfo &pi, jit_int32_t v_place_fbrr);
   201 			jit_int32_t PushMemRetPtr(jit_int32_t save_ret_to, jit_int32_t v_place_for_memret);
   202 			void DestroyParams(jit_int32_t fbrr_base);
   203 
   204 			// Ret val processing
   205 			void SaveRetVal(jit_int32_t v_where, jit_int32_t v_place_for_memret);
   206 			void ProcessPluginRetVal(jit_int32_t v_cur_res, jit_int32_t v_pContext, jit_int32_t v_plugin_ret);
   207 
   208 			void PrepareReturn(jit_int32_t v_status, jit_int32_t v_pContext, jit_int32_t v_retptr);
   209 			void DoReturn(jit_int32_t v_retptr, jit_int32_t v_memret_outaddr);
   210 
   211 			bool MemRetWithTempObj();			// do we do a memory return AND need a temporary place for it?
   212 
   213 			// Call hooks
   214 			void GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
   215 				int v_pContext, int base_param_offset, int v_plugin_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base);
   216 
   217 			// Call orig
   218 			void GenerateCallOrig(int v_status, int v_pContext, int param_base_offs, int v_this,
   219 				int v_vfnptr_origentry, int v_orig_ret, int v_override_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base);
   220 
   221 			// Hook loop
   222 			void CallSetupHookLoop(int v_orig_ret, int v_override_ret, 
   223 				int v_cur_res, int v_prev_res, int v_status, int v_vnfptr_origentry,
   224 				int v_this, int v_pContext);
   225 
   226 			void CallEndContext(int v_pContext);
   227 
   228 			// Level 2 -> called from Generate()
   229 			void AutoDetectRetType();
   230 			void AutoDetectParamFlags();
   231 			bool PassInfoSupported(const IntPassInfo &pi, bool is_ret);
   232 			void Clear();
   233 			void BuildProtoInfo();
   234 			void *GenerateHookFunc();
   235 			void *GeneratePubFunc();
   236 			
   237 			HookManagerPubFunc Generate();
   238 		public:
   239 			// Level 1 -> Public interface
   240 			GenContext(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx, ISourceHook *pSHPtr);
   241 			~GenContext();
   242 
   243 			bool Equal(const CProto &proto, int vtbl_offs, int vtbl_idx);
   244 			bool Equal(HookManagerPubFunc other);
   245 
   246 			HookManagerPubFunc GetPubFunc();
   247 		};
   248 
   249 		class CHookManagerAutoGen : public IHookManagerAutoGen 
   250 		{
   251 			struct StoredContext
   252 			{
   253 				int m_RefCnt;
   254 				GenContext *m_GenContext;
   255 			};
   256 			List<StoredContext> m_Contexts;
   257 			ISourceHook *m_pSHPtr;
   258 
   259 		public:
   260 			CHookManagerAutoGen(ISourceHook *pSHPtr);
   261 			~CHookManagerAutoGen();
   262 
   263 			int GetIfaceVersion();
   264 			int GetImplVersion();
   265 
   266 			HookManagerPubFunc MakeHookMan(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx);
   267 			void ReleaseHookMan(HookManagerPubFunc pubFunc);
   268 		};
   269 
   270 	}
   271 }
   272 
   273 
   274 #endif