1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2007 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): Pavol "PM OnoTo" Marko
8 * ============================
11 #ifndef __SOURCEHOOK_HOOKMANGEN_H__
12 #define __SOURCEHOOK_HOOKMANGEN_H__
14 #include "sourcehook_impl.h"
22 #if SH_COMP == SH_COMP_GCC
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;
38 typedef unsigned int jitoffs_t;
39 typedef signed int jitrel_t;
44 static CPageAlloc ms_Allocator;
46 unsigned char *m_pData;
48 jitoffs_t m_AllocatedSize;
51 GenBuffer() : m_pData(NULL), m_Size(0), m_AllocatedSize(0)
62 unsigned char *GetData()
67 template <class PT> void push(PT what)
69 push((const unsigned char *)&what, sizeof(PT));
72 void push(const unsigned char *data, jitoffs_t size)
74 jitoffs_t newSize = m_Size + size;
75 if (newSize > m_AllocatedSize)
77 m_AllocatedSize = newSize > m_AllocatedSize*2 ? newSize : m_AllocatedSize*2;
78 if (m_AllocatedSize < 64)
81 unsigned char *newBuf;
82 newBuf = reinterpret_cast<unsigned char*>(ms_Allocator.Alloc(m_AllocatedSize));
83 ms_Allocator.SetRW(newBuf);
86 SH_ASSERT(0, ("bad_alloc: couldn't allocate 0x%08X bytes of memory\n", m_AllocatedSize));
89 memset((void*)newBuf, 0xCC, m_AllocatedSize); // :TODO: remove this !
90 memcpy((void*)newBuf, (const void*)m_pData, m_Size);
92 ms_Allocator.Free(reinterpret_cast<void*>(m_pData));
95 memcpy((void*)(m_pData + m_Size), (const void*)data, size);
99 template <class PT> void rewrite(jitoffs_t offset, PT what)
101 rewrite(offset, (const unsigned char *)&what, sizeof(PT));
104 void rewrite(jitoffs_t offset, const unsigned char *data, jitoffs_t size)
106 SH_ASSERT(offset + size <= m_AllocatedSize, ("rewrite too far"));
108 memcpy((void*)(m_pData + offset), (const void*)data, size);
114 ms_Allocator.Free(reinterpret_cast<void*>(m_pData));
122 ms_Allocator.SetRE(reinterpret_cast<void*>(m_pData));
127 return reinterpret_cast<void*>(GetData());
130 void write_ubyte(jit_uint8_t x) { push(x); }
131 void write_byte(jit_uint8_t x) { push(x); }
133 void write_ushort(unsigned short x) { push(x); }
134 void write_short(signed short x) { push(x); }
136 void write_uint32(jit_uint32_t x) { push(x); }
137 void write_int32(jit_uint32_t x) { push(x); }
139 jitoffs_t get_outputpos()
144 void start_count(jitoffs_t &offs)
146 offs = get_outputpos();
148 void end_count(jitoffs_t &offs)
150 offs = get_outputpos() - offs;
156 const static int SIZE_MWORD = 4;
157 const static int SIZE_PTR = sizeof(void*);
162 ISourceHook *m_SHPtr;
164 GenBuffer m_HookFunc;
167 ProtoInfo *m_BuiltPI;
168 PassInfo *m_BuiltPI_Params;
169 PassInfo::V2Info *m_BuiltPI_Params2;
173 void **m_HookfuncVfnptr;
177 jit_int8_t NextRegEBX_ECX_EDX();
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)
185 void BitwiseCopy_Setup();
186 void BitwiseCopy_Do(size_t size);
189 jit_int32_t PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to,
190 jit_int32_t v_place_for_memret); // save_ret_to and v_place_for_memret only used for memory returns
191 jit_int32_t PushRef(jit_int32_t param_offset, const IntPassInfo &pi);
192 jit_int32_t PushBasic(jit_int32_t param_offset, const IntPassInfo &pi);
193 jit_int32_t PushFloat(jit_int32_t param_offset, const IntPassInfo &pi);
194 jit_int32_t PushObject(jit_int32_t param_offset, const IntPassInfo &pi);
196 // Ret val processing
197 void SaveRetVal(jit_int32_t v_where, jit_int32_t v_place_for_memret);
198 void ProcessPluginRetVal(jit_int32_t v_cur_res, jit_int32_t v_pContext, jit_int32_t v_plugin_ret);
200 void PrepareReturn(jit_int32_t v_status, jit_int32_t v_pContext, jit_int32_t v_retptr);
201 void DoReturn(jit_int32_t v_retptr, jit_int32_t v_memret_outaddr);
203 bool MemRetWithTempObj(); // do we do a memory return AND need a temporary place for it?
206 void GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
207 int v_pContext, int base_param_offset, int v_plugin_ret, int v_place_for_memret);
210 void GenerateCallOrig(int v_status, int v_pContext, int param_base_offs, int v_this,
211 int v_vfnptr_origentry, int v_orig_ret, int v_override_ret, int v_place_for_memret);
214 void CallSetupHookLoop(int v_orig_ret, int v_override_ret,
215 int v_cur_res, int v_prev_res, int v_status, int v_vnfptr_origentry,
216 int v_this, int v_pContext);
218 void CallEndContext(int v_pContext);
220 // Level 2 -> called from Generate()
221 void AutoDetectRetType();
222 bool PassInfoSupported(const IntPassInfo &pi, bool is_ret);
224 void BuildProtoInfo();
225 void *GenerateHookFunc();
226 void *GeneratePubFunc();
228 // Level 1 -> Public interface
229 GenContext(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx, ISourceHook *pSHPtr);
232 HookManagerPubFunc Generate();