sourcehook/sourcehook_hookmangen.h
author Pavol Marko <pavolmarko@gmail.com>
Thu Nov 01 01:58:44 2007 +0000 (2007-11-01)
branchhookman_autogen
changeset 435 35296642e147
parent 432 f26dd3e3030b
child 436 41f21d77c3c1
permissions -rw-r--r--
sh_memory now has CPageAlloc, testhookmangen.cpp contains a small test for it, GenBuffer uses CPageAlloc
(with this simple change, thesthookmangen uses 124K less memory!)
     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 "windows.h"
    16 
    17 namespace SourceHook
    18 {
    19 	namespace Impl
    20 	{
    21 
    22 		// Code gen stuff
    23 #if defined HAVE_STDINT_H && !defined WIN32
    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 defined WIN32
    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 					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 unsigned char *()
   126 			{
   127 				return 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 
   159 			CProto m_Proto;
   160 			int m_VtblOffs;
   161 			int m_VtblIdx;
   162 			ISourceHook *m_SHPtr;
   163 
   164 			GenBuffer m_HookFunc;
   165 			GenBuffer m_PubFunc;
   166 
   167 			ProtoInfo *m_BuiltPI;
   168 			PassInfo *m_BuiltPI_Params;
   169 			PassInfo::V2Info *m_BuiltPI_Params2;
   170 
   171 			// For hookfunc
   172 			void **m_pHI;
   173 			void **m_HookfuncVfnptr;
   174 
   175 			// Level 3 - Helpers
   176 			int m_RegCounter;
   177 			jit_int8_t NextRegEBX_ECX_EDX();
   178 
   179 			// size info
   180 			jit_int32_t GetRealSize(const IntPassInfo &info);
   181 			jit_int32_t GetStackSize(const IntPassInfo &info);
   182 			short GetParamsStackSize();		// sum(GetStackSize(i), 0 <= i < numOfParams)
   183 
   184 
   185 			// Param push
   186 			jit_int32_t PushParams(jit_int32_t param_base_offset);
   187 			jit_int32_t PushRef(jit_int32_t param_offset, const IntPassInfo &pi);
   188 			jit_int32_t PushBasic(jit_int32_t param_offset, const IntPassInfo &pi);
   189 			jit_int32_t PushFloat(jit_int32_t param_offset, const IntPassInfo &pi);
   190 			jit_int32_t PushObject(jit_int32_t param_offset, const IntPassInfo &pi);
   191 
   192 			// Ret val processing
   193 			void SaveRetVal(int v_where);
   194 			void ProcessPluginRetVal(int v_cur_res, int v_pContext, int v_plugin_ret);
   195 
   196 			void PrepareReturn(int v_status, int v_pContext, int v_retptr);
   197 			void DoReturn(int v_retptr);
   198 
   199 			// Call hooks
   200 			void GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
   201 				int v_pContext, int base_param_offset, int v_plugin_ret);
   202 
   203 			// Call orig
   204 			void GenerateCallOrig(int v_status, int v_pContext,
   205 				int param_base_offs, int v_this, int v_vfnptr_origentry, int v_orig_ret, int v_override_ret);
   206 
   207 			// Hook loop
   208 			void CallSetupHookLoop(int v_orig_ret, int v_override_ret, 
   209 				int v_cur_res, int v_prev_res, int v_status, int v_vnfptr_origentry,
   210 				int v_this, int v_pContext);
   211 
   212 			void CallEndContext(int v_pContext);
   213 
   214 			// Level 2 -> called from Generate()
   215 			bool PassInfoSupported(const IntPassInfo &pi, bool is_ret);
   216 			void Clear();
   217 			void BuildProtoInfo();
   218 			unsigned char *GenerateHookFunc();
   219 			unsigned char *GeneratePubFunc();
   220 		public:
   221 			// Level 1 -> Public interface
   222 			GenContext(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx, ISourceHook *pSHPtr);
   223 			~GenContext();
   224 
   225 			HookManagerPubFunc Generate();
   226 		};
   227 	}
   228 }
   229 
   230 
   231 #endif