sourcehook/sourcehook_hookmangen.h
author Pavol Marko <pavolmarko@gmail.com>
Sun Oct 21 21:55:17 2007 +0000 (2007-10-21)
branchhookman_autogen
changeset 433 a2d986ddd522
parent 432 f26dd3e3030b
child 435 35296642e147
permissions -rw-r--r--
Changed license in sourcehook_hookmangen_x86.h (agreed to by BAILOPAN and faluco)
Updated MSVC7 proj file
     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 			unsigned char *m_pData;
    46 			jitoffs_t m_Size;
    47 			jitoffs_t m_AllocatedSize;
    48 
    49 		public:
    50 			GenBuffer() : m_pData(NULL), m_Size(0), m_AllocatedSize(0)
    51 			{
    52 			}
    53 			~GenBuffer()
    54 			{
    55 				clear();
    56 			}
    57 			jitoffs_t GetSize()
    58 			{
    59 				return m_Size;
    60 			}
    61 			unsigned char *GetData()
    62 			{
    63 				return m_pData;
    64 			}
    65 
    66 			template <class PT> void push(PT what)
    67 			{
    68 				push((const unsigned char *)&what, sizeof(PT));
    69 			}
    70 
    71 			void push(const unsigned char *data, jitoffs_t size)
    72 			{
    73 				jitoffs_t newSize = m_Size + size;
    74 				if (newSize > m_AllocatedSize)
    75 				{
    76 					m_AllocatedSize = newSize > m_AllocatedSize*2 ? newSize : m_AllocatedSize*2;
    77 					if (m_AllocatedSize < 64)
    78 						m_AllocatedSize = 64;
    79 
    80 					unsigned char *newBuf;
    81 					try
    82 					{
    83 						//!!!! Better use of pages! or something!
    84 						newBuf = reinterpret_cast<unsigned char*>(VirtualAlloc(NULL, m_AllocatedSize, MEM_COMMIT, PAGE_READWRITE));
    85 					}
    86 					catch (std::bad_alloc)
    87 					{
    88 						newBuf = NULL;
    89 					}
    90 					if (!newBuf)
    91 					{
    92 						SH_ASSERT(0, ("bad_alloc: couldn't allocate 0x%08X bytes of memory\n", m_AllocatedSize));
    93 						return;
    94 					}
    95 					memcpy((void*)newBuf, (const void*)m_pData, m_Size);
    96 					if (m_pData)
    97 						VirtualFree(m_pData, 0, MEM_RELEASE);
    98 					m_pData = newBuf;
    99 				}
   100 				memcpy((void*)(m_pData + m_Size), (const void*)data, size);
   101 				m_Size = newSize;
   102 			}
   103 
   104 			template <class PT> void rewrite(jitoffs_t offset, PT what)
   105 			{
   106 				rewrite(offset, (const unsigned char *)&what, sizeof(PT));
   107 			}
   108 
   109 			void rewrite(jitoffs_t offset, const unsigned char *data, jitoffs_t size)
   110 			{
   111 				SH_ASSERT(offset + size <= m_AllocatedSize, ("rewrite too far"));
   112 
   113 				memcpy((void*)(m_pData + offset), (const void*)data, size);
   114 			}
   115 
   116 			void clear()
   117 			{
   118 				if (m_pData)
   119 					VirtualFree(m_pData, 0, MEM_RELEASE);
   120 				m_pData = NULL;
   121 				m_Size = 0;
   122 				m_AllocatedSize = 0;
   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 			// :TODO: real buffer which uses virtualalloc correctly	
   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 
   186 			// Param push
   187 			jit_int32_t PushParams(jit_int32_t param_base_offset);
   188 			jit_int32_t PushRef(jit_int32_t param_offset, const IntPassInfo &pi);
   189 			jit_int32_t PushBasic(jit_int32_t param_offset, const IntPassInfo &pi);
   190 			jit_int32_t PushFloat(jit_int32_t param_offset, const IntPassInfo &pi);
   191 			jit_int32_t PushObject(jit_int32_t param_offset, const IntPassInfo &pi);
   192 
   193 			// Ret val processing
   194 			void SaveRetVal(int v_where);
   195 			void ProcessPluginRetVal(int v_cur_res, int v_pContext, int v_plugin_ret);
   196 
   197 			void PrepareReturn(int v_status, int v_pContext, int v_retptr);
   198 			void DoReturn(int v_retptr);
   199 
   200 			// Call hooks
   201 			void GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
   202 				int v_pContext, int base_param_offset, int v_plugin_ret);
   203 
   204 			// Call orig
   205 			void GenerateCallOrig(int v_status, int v_pContext,
   206 				int param_base_offs, int v_this, int v_vfnptr_origentry, int v_orig_ret, int v_override_ret);
   207 
   208 			// Hook loop
   209 			void CallSetupHookLoop(int v_orig_ret, int v_override_ret, 
   210 				int v_cur_res, int v_prev_res, int v_status, int v_vnfptr_origentry,
   211 				int v_this, int v_pContext);
   212 
   213 			void CallEndContext(int v_pContext);
   214 
   215 			// Level 2 -> called from Generate()
   216 			bool PassInfoSupported(const IntPassInfo &pi, bool is_ret);
   217 			void Clear();
   218 			void BuildProtoInfo();
   219 			unsigned char *GenerateHookFunc();
   220 			unsigned char *GeneratePubFunc();
   221 		public:
   222 			// Level 1 -> Public interface
   223 			GenContext(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx, ISourceHook *pSHPtr);
   224 			~GenContext();
   225 
   226 			HookManagerPubFunc Generate();
   227 		};
   228 	}
   229 }
   230 
   231 
   232 #endif