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