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"
15 #include "sh_pagealloc.h"
23 #if SH_COMP == SH_COMP_GCC
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;
39 typedef unsigned int jitoffs_t;
40 typedef signed int jitrel_t;
45 static CPageAlloc ms_Allocator;
47 unsigned char *m_pData;
49 jitoffs_t m_AllocatedSize;
52 GenBuffer() : m_pData(NULL), m_Size(0), m_AllocatedSize(0)
63 unsigned char *GetData()
68 template <class PT> void push(PT what)
70 push((const unsigned char *)&what, sizeof(PT));
73 void push(const unsigned char *data, jitoffs_t size)
75 jitoffs_t newSize = m_Size + size;
76 if (newSize > m_AllocatedSize)
78 m_AllocatedSize = newSize > m_AllocatedSize*2 ? newSize : m_AllocatedSize*2;
79 if (m_AllocatedSize < 64)
82 unsigned char *newBuf;
83 newBuf = reinterpret_cast<unsigned char*>(ms_Allocator.Alloc(m_AllocatedSize));
84 ms_Allocator.SetRW(newBuf);
87 SH_ASSERT(0, ("bad_alloc: couldn't allocate 0x%08X bytes of memory\n", m_AllocatedSize));
90 memset((void*)newBuf, 0xCC, m_AllocatedSize); // :TODO: remove this !
91 memcpy((void*)newBuf, (const void*)m_pData, m_Size);
93 ms_Allocator.Free(reinterpret_cast<void*>(m_pData));
96 memcpy((void*)(m_pData + m_Size), (const void*)data, size);
100 template <class PT> void rewrite(jitoffs_t offset, PT what)
102 rewrite(offset, (const unsigned char *)&what, sizeof(PT));
105 void rewrite(jitoffs_t offset, const unsigned char *data, jitoffs_t size)
107 SH_ASSERT(offset + size <= m_AllocatedSize, ("rewrite too far"));
109 memcpy((void*)(m_pData + offset), (const void*)data, size);
115 ms_Allocator.Free(reinterpret_cast<void*>(m_pData));
123 ms_Allocator.SetRE(reinterpret_cast<void*>(m_pData));
128 return reinterpret_cast<void*>(GetData());
131 void write_ubyte(jit_uint8_t x) { push(x); }
132 void write_byte(jit_uint8_t x) { push(x); }
134 void write_ushort(unsigned short x) { push(x); }
135 void write_short(signed short x) { push(x); }
137 void write_uint32(jit_uint32_t x) { push(x); }
138 void write_int32(jit_uint32_t x) { push(x); }
140 jitoffs_t get_outputpos()
145 void start_count(jitoffs_t &offs)
147 offs = get_outputpos();
149 void end_count(jitoffs_t &offs)
151 offs = get_outputpos() - offs;
157 const static int SIZE_MWORD = 4;
158 const static int SIZE_PTR = sizeof(void*);
159 const static int PassFlag_ForcedByRef = (1<<30); // ByVal in source, but actually passed by reference (GCC) -> private pass, destruct
164 ISourceHook *m_SHPtr;
166 GenBuffer m_HookFunc;
169 ProtoInfo *m_BuiltPI;
170 PassInfo *m_BuiltPI_Params;
171 PassInfo::V2Info *m_BuiltPI_Params2;
175 void **m_HookfuncVfnptr;
179 jit_int8_t NextRegEBX_ECX_EDX();
182 jit_int32_t GetRealSize(const IntPassInfo &info);
183 jit_int32_t GetStackSize(const IntPassInfo &info);
184 short GetParamsStackSize(); // sum(GetStackSize(param[i]), 0 <= i < numOfParams)
187 void BitwiseCopy_Setup();
188 void BitwiseCopy_Do(size_t size);
191 short GetForcedByRefParamsSize(); // sum(param[i] is forcedbyref ? GetStackSize(param[i]) : 0, 0 <= i < numOfParams)
192 short GetForcedByRefParamOffset(int p); // sum(param[i] is forcedbyref ? GetStackSize(param[i]) : 0, 0 <= i < p)
193 jit_int32_t PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to,
194 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
195 jit_int32_t PushRef(jit_int32_t param_offset, const IntPassInfo &pi);
196 jit_int32_t PushBasic(jit_int32_t param_offset, const IntPassInfo &pi);
197 jit_int32_t PushFloat(jit_int32_t param_offset, const IntPassInfo &pi);
198 jit_int32_t PushObject(jit_int32_t param_offset, const IntPassInfo &pi, jit_int32_t v_place_fbrr);
199 void DestroyParams(jit_int32_t fbrr_base);
201 // Ret val processing
202 void SaveRetVal(jit_int32_t v_where, jit_int32_t v_place_for_memret);
203 void ProcessPluginRetVal(jit_int32_t v_cur_res, jit_int32_t v_pContext, jit_int32_t v_plugin_ret);
205 void PrepareReturn(jit_int32_t v_status, jit_int32_t v_pContext, jit_int32_t v_retptr);
206 void DoReturn(jit_int32_t v_retptr, jit_int32_t v_memret_outaddr);
208 bool MemRetWithTempObj(); // do we do a memory return AND need a temporary place for it?
211 void GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
212 int v_pContext, int base_param_offset, int v_plugin_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base);
215 void GenerateCallOrig(int v_status, int v_pContext, int param_base_offs, int v_this,
216 int v_vfnptr_origentry, int v_orig_ret, int v_override_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base);
219 void CallSetupHookLoop(int v_orig_ret, int v_override_ret,
220 int v_cur_res, int v_prev_res, int v_status, int v_vnfptr_origentry,
221 int v_this, int v_pContext);
223 void CallEndContext(int v_pContext);
225 // Level 2 -> called from Generate()
226 void AutoDetectRetType();
227 void AutoDetectParamFlags();
228 bool PassInfoSupported(const IntPassInfo &pi, bool is_ret);
230 void BuildProtoInfo();
231 void *GenerateHookFunc();
232 void *GeneratePubFunc();
234 // Level 1 -> Public interface
235 GenContext(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx, ISourceHook *pSHPtr);
238 HookManagerPubFunc Generate();