1.1 --- a/sourcehook/sourcehook_hookmangen.h Sun Nov 25 01:22:41 2007 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,299 +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 -#ifndef __SOURCEHOOK_HOOKMANGEN_H__
1.15 -#define __SOURCEHOOK_HOOKMANGEN_H__
1.16 -
1.17 -#include "sh_pagealloc.h"
1.18 -
1.19 -namespace SourceHook
1.20 -{
1.21 - namespace Impl
1.22 - {
1.23 -
1.24 - // Code gen stuff
1.25 -#if SH_COMP == SH_COMP_GCC
1.26 -#include <stdint.h>
1.27 - typedef int8_t jit_int8_t;
1.28 - typedef uint8_t jit_uint8_t;
1.29 - typedef int32_t jit_int32_t;
1.30 - typedef uint32_t jit_uint32_t;
1.31 - typedef int64_t jit_int64_t;
1.32 - typedef uint64_t jit_uint64_t;
1.33 -#elif SH_COMP == SH_COMP_MSVC
1.34 - typedef __int8 jit_int8_t;
1.35 - typedef unsigned __int8 jit_uint8_t;
1.36 - typedef __int32 jit_int32_t;
1.37 - typedef unsigned __int32 jit_uint32_t;
1.38 - typedef __int64 jit_int64_t;
1.39 - typedef unsigned __int64 jit_uint64_t;
1.40 -#endif
1.41 - typedef unsigned int jitoffs_t;
1.42 - typedef signed int jitrel_t;
1.43 -
1.44 -
1.45 - class GenBuffer
1.46 - {
1.47 - static CPageAlloc ms_Allocator;
1.48 -
1.49 - unsigned char *m_pData;
1.50 - jitoffs_t m_Size;
1.51 - jitoffs_t m_AllocatedSize;
1.52 -
1.53 - public:
1.54 - GenBuffer() : m_pData(NULL), m_Size(0), m_AllocatedSize(0)
1.55 - {
1.56 - }
1.57 - ~GenBuffer()
1.58 - {
1.59 - clear();
1.60 - }
1.61 - jitoffs_t GetSize()
1.62 - {
1.63 - return m_Size;
1.64 - }
1.65 - unsigned char *GetData()
1.66 - {
1.67 - return m_pData;
1.68 - }
1.69 -
1.70 - template <class PT> void push(PT what)
1.71 - {
1.72 - push((const unsigned char *)&what, sizeof(PT));
1.73 - }
1.74 -
1.75 - void push(const unsigned char *data, jitoffs_t size)
1.76 - {
1.77 - jitoffs_t newSize = m_Size + size;
1.78 - if (newSize > m_AllocatedSize)
1.79 - {
1.80 - m_AllocatedSize = newSize > m_AllocatedSize*2 ? newSize : m_AllocatedSize*2;
1.81 - if (m_AllocatedSize < 64)
1.82 - m_AllocatedSize = 64;
1.83 -
1.84 - unsigned char *newBuf;
1.85 - newBuf = reinterpret_cast<unsigned char*>(ms_Allocator.Alloc(m_AllocatedSize));
1.86 - ms_Allocator.SetRW(newBuf);
1.87 - if (!newBuf)
1.88 - {
1.89 - SH_ASSERT(0, ("bad_alloc: couldn't allocate 0x%08X bytes of memory\n", m_AllocatedSize));
1.90 - return;
1.91 - }
1.92 - memset((void*)newBuf, 0xCC, m_AllocatedSize); // :TODO: remove this !
1.93 - memcpy((void*)newBuf, (const void*)m_pData, m_Size);
1.94 - if (m_pData)
1.95 - {
1.96 - ms_Allocator.SetRE(reinterpret_cast<void*>(m_pData));
1.97 - ms_Allocator.SetRW(newBuf);
1.98 - ms_Allocator.Free(reinterpret_cast<void*>(m_pData));
1.99 - }
1.100 - m_pData = newBuf;
1.101 - }
1.102 - memcpy((void*)(m_pData + m_Size), (const void*)data, size);
1.103 - m_Size = newSize;
1.104 - }
1.105 -
1.106 - template <class PT> void rewrite(jitoffs_t offset, PT what)
1.107 - {
1.108 - rewrite(offset, (const unsigned char *)&what, sizeof(PT));
1.109 - }
1.110 -
1.111 - void rewrite(jitoffs_t offset, const unsigned char *data, jitoffs_t size)
1.112 - {
1.113 - SH_ASSERT(offset + size <= m_AllocatedSize, ("rewrite too far"));
1.114 -
1.115 - memcpy((void*)(m_pData + offset), (const void*)data, size);
1.116 - }
1.117 -
1.118 - void clear()
1.119 - {
1.120 - if (m_pData)
1.121 - ms_Allocator.Free(reinterpret_cast<void*>(m_pData));
1.122 - m_pData = NULL;
1.123 - m_Size = 0;
1.124 - m_AllocatedSize = 0;
1.125 - }
1.126 -
1.127 - void SetRE()
1.128 - {
1.129 - ms_Allocator.SetRE(reinterpret_cast<void*>(m_pData));
1.130 - }
1.131 -
1.132 - operator void *()
1.133 - {
1.134 - return reinterpret_cast<void*>(GetData());
1.135 - }
1.136 -
1.137 - void write_ubyte(jit_uint8_t x) { push(x); }
1.138 - void write_byte(jit_uint8_t x) { push(x); }
1.139 -
1.140 - void write_ushort(unsigned short x) { push(x); }
1.141 - void write_short(signed short x) { push(x); }
1.142 -
1.143 - void write_uint32(jit_uint32_t x) { push(x); }
1.144 - void write_int32(jit_uint32_t x) { push(x); }
1.145 -
1.146 - jitoffs_t get_outputpos()
1.147 - {
1.148 - return m_Size;
1.149 - }
1.150 -
1.151 - void start_count(jitoffs_t &offs)
1.152 - {
1.153 - offs = get_outputpos();
1.154 - }
1.155 - void end_count(jitoffs_t &offs)
1.156 - {
1.157 - offs = get_outputpos() - offs;
1.158 - }
1.159 - };
1.160 -
1.161 - class GenContext
1.162 - {
1.163 - const static int SIZE_MWORD = 4;
1.164 - const static int SIZE_PTR = sizeof(void*);
1.165 - const static int PassFlag_ForcedByRef = (1<<30); // ByVal in source, but actually passed by reference (GCC) -> private pass, destruct
1.166 -
1.167 - HookManagerPubFunc m_GeneratedPubFunc;
1.168 -
1.169 - CProto m_OrigProto; // original passed-in prototype
1.170 - CProto m_Proto;
1.171 - int m_VtblOffs;
1.172 - int m_VtblIdx;
1.173 - ISourceHook *m_SHPtr;
1.174 -
1.175 - GenBuffer m_HookFunc;
1.176 - GenBuffer m_PubFunc;
1.177 -
1.178 - ProtoInfo *m_BuiltPI;
1.179 - PassInfo *m_BuiltPI_Params;
1.180 - PassInfo::V2Info *m_BuiltPI_Params2;
1.181 -
1.182 - // For hookfunc
1.183 - void **m_pHI;
1.184 - void **m_HookfuncVfnptr;
1.185 -
1.186 - // Level 3 - Helpers
1.187 - int m_RegCounter;
1.188 - jit_int8_t NextRegEBX_ECX_EDX();
1.189 -
1.190 - int m_BytesPushedAfterInitialAlignment;
1.191 - enum AlignStackFlags
1.192 - {
1.193 - AlignStack_GCC_ThisOnStack = 1,
1.194 - AlignStack_MSVC_ThisOnStack = 2,
1.195 - AlignStack_MemRet = 4
1.196 - };
1.197 - jit_int32_t AlignStackBeforeCall(int paramsize, int flags);
1.198 - void AlignStackAfterCall(jit_int32_t numofbytes);
1.199 - void CheckAlignmentBeforeCall();
1.200 -
1.201 - // size info
1.202 - jit_int32_t GetRealSize(const IntPassInfo &info); // checks for reference
1.203 - jit_int32_t AlignSize(jit_int32_t x, jit_int32_t boundary); // align a size
1.204 - jit_int32_t GetParamStackSize(const IntPassInfo &info); // get the size of a param in the param stack
1.205 - short GetParamsTotalStackSize(); // sum(GetParamStackSize(param[i]), 0 <= i < numOfParams)
1.206 -
1.207 - // Helpers
1.208 - void BitwiseCopy_Setup();
1.209 - void BitwiseCopy_Do(size_t size);
1.210 -
1.211 -
1.212 - // HookFunc frame
1.213 - jit_int32_t m_HookFunc_FrameOffset;
1.214 - jit_int32_t m_HookFunc_FrameVarsSize;
1.215 -
1.216 - void ResetFrame(jit_int32_t startOffset);
1.217 - jit_int32_t AddVarToFrame(jit_int32_t size);
1.218 - jit_int32_t ComputeVarsSize();
1.219 -
1.220 - // Param push
1.221 - short GetForcedByRefParamsSize(); // sum(param[i] is forcedbyref ? GetStackSize(param[i]) : 0, 0 <= i < numOfParams)
1.222 - short GetForcedByRefParamOffset(int p); // sum(param[i] is forcedbyref ? GetStackSize(param[i]) : 0, 0 <= i < p)
1.223 - jit_int32_t PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to,
1.224 - 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
1.225 - jit_int32_t PushRef(jit_int32_t param_offset, const IntPassInfo &pi);
1.226 - jit_int32_t PushBasic(jit_int32_t param_offset, const IntPassInfo &pi);
1.227 - jit_int32_t PushFloat(jit_int32_t param_offset, const IntPassInfo &pi);
1.228 - jit_int32_t PushObject(jit_int32_t param_offset, const IntPassInfo &pi, jit_int32_t v_place_fbrr);
1.229 - jit_int32_t PushMemRetPtr(jit_int32_t save_ret_to, jit_int32_t v_place_for_memret);
1.230 - void DestroyParams(jit_int32_t fbrr_base);
1.231 -
1.232 - // Ret val processing
1.233 - void SaveRetVal(jit_int32_t v_where, jit_int32_t v_place_for_memret);
1.234 - void ProcessPluginRetVal(jit_int32_t v_cur_res, jit_int32_t v_pContext, jit_int32_t v_plugin_ret);
1.235 -
1.236 - void PrepareReturn(jit_int32_t v_status, jit_int32_t v_pContext, jit_int32_t v_retptr);
1.237 - void DoReturn(jit_int32_t v_retptr, jit_int32_t v_memret_outaddr);
1.238 -
1.239 - bool MemRetWithTempObj(); // do we do a memory return AND need a temporary place for it?
1.240 -
1.241 - // Call hooks
1.242 - void GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
1.243 - int v_pContext, int base_param_offset, int v_plugin_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base, jit_int32_t v_va_buf);
1.244 -
1.245 - // Call orig
1.246 - void GenerateCallOrig(int v_status, int v_pContext, int param_base_offs, int v_this,
1.247 - int v_vfnptr_origentry, int v_orig_ret, int v_override_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base, jit_int32_t v_va_buf);
1.248 -
1.249 - // Hook loop
1.250 - void CallSetupHookLoop(int v_orig_ret, int v_override_ret,
1.251 - int v_cur_res, int v_prev_res, int v_status, int v_vnfptr_origentry,
1.252 - int v_this, int v_pContext);
1.253 -
1.254 - void CallEndContext(int v_pContext);
1.255 -
1.256 - // Level 2 -> called from Generate()
1.257 - void AutoDetectRetType();
1.258 - void AutoDetectParamFlags();
1.259 - bool PassInfoSupported(const IntPassInfo &pi, bool is_ret);
1.260 - void Clear();
1.261 - void BuildProtoInfo();
1.262 - void *GenerateHookFunc();
1.263 - void *GeneratePubFunc();
1.264 -
1.265 - HookManagerPubFunc Generate();
1.266 - public:
1.267 - // Level 1 -> Public interface
1.268 - GenContext(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx, ISourceHook *pSHPtr);
1.269 - ~GenContext();
1.270 -
1.271 - bool Equal(const CProto &proto, int vtbl_offs, int vtbl_idx);
1.272 - bool Equal(HookManagerPubFunc other);
1.273 -
1.274 - HookManagerPubFunc GetPubFunc();
1.275 - };
1.276 -
1.277 - class CHookManagerAutoGen : public IHookManagerAutoGen
1.278 - {
1.279 - struct StoredContext
1.280 - {
1.281 - int m_RefCnt;
1.282 - GenContext *m_GenContext;
1.283 - };
1.284 - List<StoredContext> m_Contexts;
1.285 - ISourceHook *m_pSHPtr;
1.286 -
1.287 - public:
1.288 - CHookManagerAutoGen(ISourceHook *pSHPtr);
1.289 - ~CHookManagerAutoGen();
1.290 -
1.291 - int GetIfaceVersion();
1.292 - int GetImplVersion();
1.293 -
1.294 - HookManagerPubFunc MakeHookMan(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx);
1.295 - void ReleaseHookMan(HookManagerPubFunc pubFunc);
1.296 - };
1.297 -
1.298 - }
1.299 -}
1.300 -
1.301 -
1.302 -#endif