1
0
mirror of https://github.com/alliedmodders/metamod-source.git synced 2024-11-28 10:24:20 +01:00
HLMetaModOfficial/core/sourcehook/sourcehook_hookmangen.h
David Anderson e19413dd5b Some reorganization.
--HG--
rename : sourcemm/IPluginManager.h => core/IPluginManager.h
rename : sourcemm/ISmmAPI.h => core/ISmmAPI.h
rename : sourcemm/ISmmPlugin.h => core/ISmmPlugin.h
rename : sourcemm/ISmmPluginExt.h => core/ISmmPluginExt.h
rename : sourcemm/LICENSE.txt => core/LICENSE.txt
rename : sourcemm/Makefile => core/Makefile
rename : sourcemm/changelog.txt => core/changelog.txt
rename : sourcemm/episode1/console.cpp => core/episode1/console.cpp
rename : sourcemm/episode1/console.h => core/episode1/console.h
rename : sourcemm/episode1/convar_smm.h => core/episode1/convar_smm.h
rename : sourcemm/episode1/msvc8/sourcemm.sln => core/episode1/msvc8/sourcemm.sln
rename : sourcemm/episode1/msvc8/sourcemm.vcproj => core/episode1/msvc8/sourcemm.vcproj
rename : sourcemm/episode1/provider_ep1.cpp => core/episode1/provider_ep1.cpp
rename : sourcemm/episode1/provider_ep1.h => core/episode1/provider_ep1.h
rename : sourcemm/episode1/vsp_listener.cpp => core/episode1/vsp_listener.cpp
rename : sourcemm/episode1/vsp_listener.h => core/episode1/vsp_listener.h
rename : sourcemm/metamod.cpp => core/metamod.cpp
rename : sourcemm/metamod.h => core/metamod.h
rename : sourcemm/metamod_console.cpp => core/metamod_console.cpp
rename : sourcemm/metamod_console.h => core/metamod_console.h
rename : sourcemm/metamod_oslink.cpp => core/metamod_oslink.cpp
rename : sourcemm/metamod_oslink.h => core/metamod_oslink.h
rename : sourcemm/metamod_plugins.cpp => core/metamod_plugins.cpp
rename : sourcemm/metamod_plugins.h => core/metamod_plugins.h
rename : sourcemm/metamod_provider.h => core/metamod_provider.h
rename : sourcemm/metamod_util.cpp => core/metamod_util.cpp
rename : sourcemm/metamod_util.h => core/metamod_util.h
rename : sourcemm/episode2/console.cpp => core/provider/console.cpp
rename : sourcemm/episode2/console.h => core/provider/console.h
rename : sourcemm/episode2/msvc8/sourcemm.sln => core/provider/msvc8/sourcemm.sln
rename : sourcemm/episode2/msvc8/sourcemm.vcproj => core/provider/msvc8/sourcemm.vcproj
rename : sourcemm/episode2/msvc9/sourcemm.sln => core/provider/msvc9/sourcemm.sln
rename : sourcemm/episode2/msvc9/sourcemm.vcproj => core/provider/msvc9/sourcemm.vcproj
rename : sourcemm/episode2/provider_ep2.cpp => core/provider/provider_ep2.cpp
rename : sourcemm/episode2/provider_ep2.h => core/provider/provider_ep2.h
rename : sourcemm/episode2/vsp_listener.cpp => core/provider/vsp_listener.cpp
rename : sourcemm/episode2/vsp_listener.h => core/provider/vsp_listener.h
rename : sourcehook/FastDelegate.h => core/sourcehook/FastDelegate.h
rename : sourcehook/generate/FastDelegate.h => core/sourcehook/generate/FastDelegate.h
rename : sourcehook/generate/FastDelegate.hxx => core/sourcehook/generate/FastDelegate.hxx
rename : sourcehook/generate/generate => core/sourcehook/generate/generate
rename : sourcehook/generate/generate.bat => core/sourcehook/generate/generate.bat
rename : sourcehook/generate/sh_memfuncinfo.h => core/sourcehook/generate/sh_memfuncinfo.h
rename : sourcehook/generate/sh_memfuncinfo.hxx => core/sourcehook/generate/sh_memfuncinfo.hxx
rename : sourcehook/generate/shworker.bin => core/sourcehook/generate/shworker.bin
rename : sourcehook/generate/shworker.exe => core/sourcehook/generate/shworker.exe
rename : sourcehook/generate/shworker/Makefile => core/sourcehook/generate/shworker/Makefile
rename : sourcehook/generate/shworker/fd_hopter.cpp => core/sourcehook/generate/shworker/fd_hopter.cpp
rename : sourcehook/generate/shworker/msvc7/shworker.vcproj => core/sourcehook/generate/shworker/msvc7/shworker.vcproj
rename : sourcehook/generate/shworker/msvc8/shworker.vcproj => core/sourcehook/generate/shworker/msvc8/shworker.vcproj
rename : sourcehook/generate/shworker/shworker.cpp => core/sourcehook/generate/shworker/shworker.cpp
rename : sourcehook/generate/sourcehook.h => core/sourcehook/generate/sourcehook.h
rename : sourcehook/generate/sourcehook.hxx => core/sourcehook/generate/sourcehook.hxx
rename : sourcehook/sh_list.h => core/sourcehook/sh_list.h
rename : sourcehook/sh_memfuncinfo.h => core/sourcehook/sh_memfuncinfo.h
rename : sourcehook/sh_memory.h => core/sourcehook/sh_memory.h
rename : sourcehook/sh_pagealloc.h => core/sourcehook/sh_pagealloc.h
rename : sourcehook/sh_stack.h => core/sourcehook/sh_stack.h
rename : sourcehook/sh_string.h => core/sourcehook/sh_string.h
rename : sourcehook/sh_tinyhash.h => core/sourcehook/sh_tinyhash.h
rename : sourcehook/sh_vector.h => core/sourcehook/sh_vector.h
rename : sourcehook/sourcehook.cpp => core/sourcehook/sourcehook.cpp
rename : sourcehook/sourcehook.h => core/sourcehook/sourcehook.h
rename : sourcehook/sourcehook_hookmangen.cpp => core/sourcehook/sourcehook_hookmangen.cpp
rename : sourcehook/sourcehook_hookmangen.h => core/sourcehook/sourcehook_hookmangen.h
rename : sourcehook/sourcehook_hookmangen_x86.h => core/sourcehook/sourcehook_hookmangen_x86.h
rename : sourcehook/sourcehook_impl.h => core/sourcehook/sourcehook_impl.h
rename : sourcehook/sourcehook_impl_chook.h => core/sourcehook/sourcehook_impl_chook.h
rename : sourcehook/sourcehook_impl_chookidman.h => core/sourcehook/sourcehook_impl_chookidman.h
rename : sourcehook/sourcehook_impl_chookmaninfo.h => core/sourcehook/sourcehook_impl_chookmaninfo.h
rename : sourcehook/sourcehook_impl_ciface.h => core/sourcehook/sourcehook_impl_ciface.h
rename : sourcehook/sourcehook_impl_cproto.h => core/sourcehook/sourcehook_impl_cproto.h
rename : sourcehook/sourcehook_impl_cvfnptr.h => core/sourcehook/sourcehook_impl_cvfnptr.h
rename : sourcehook/sourcehook_pibuilder.h => core/sourcehook/sourcehook_pibuilder.h
rename : sourcehook/test/Makefile => core/sourcehook/test/Makefile
rename : sourcehook/test/generate.bat => core/sourcehook/test/generate.bat
rename : sourcehook/test/main.cpp => core/sourcehook/test/main.cpp
rename : sourcehook/test/msvc7/test.vcproj => core/sourcehook/test/msvc7/test.vcproj
rename : sourcehook/test/msvc8/test.vcproj => core/sourcehook/test/msvc8/test.vcproj
rename : sourcehook/test/sourcehook_test.h => core/sourcehook/test/sourcehook_test.h
rename : sourcehook/test/test1.cpp => core/sourcehook/test/test1.cpp
rename : sourcehook/test/test2.cpp => core/sourcehook/test/test2.cpp
rename : sourcehook/test/test3.cpp => core/sourcehook/test/test3.cpp
rename : sourcehook/test/test4.cpp => core/sourcehook/test/test4.cpp
rename : sourcehook/test/testbail.cpp => core/sourcehook/test/testbail.cpp
rename : sourcehook/test/testbail.h => core/sourcehook/test/testbail.h
rename : sourcehook/test/testbail2.cpp => core/sourcehook/test/testbail2.cpp
rename : sourcehook/test/testevents.h => core/sourcehook/test/testevents.h
rename : sourcehook/test/testhookmangen.cpp => core/sourcehook/test/testhookmangen.cpp
rename : sourcehook/test/testhookmangen.h => core/sourcehook/test/testhookmangen.h
rename : sourcehook/test/testhookmangen.hxx => core/sourcehook/test/testhookmangen.hxx
rename : sourcehook/test/testlist.cpp => core/sourcehook/test/testlist.cpp
rename : sourcehook/test/testmanual.cpp => core/sourcehook/test/testmanual.cpp
rename : sourcehook/test/testmulti.cpp => core/sourcehook/test/testmulti.cpp
rename : sourcehook/test/testrecall.cpp => core/sourcehook/test/testrecall.cpp
rename : sourcehook/test/testreentr.cpp => core/sourcehook/test/testreentr.cpp
rename : sourcehook/test/testref.cpp => core/sourcehook/test/testref.cpp
rename : sourcehook/test/testrefret.cpp => core/sourcehook/test/testrefret.cpp
rename : sourcehook/test/testvphooks.cpp => core/sourcehook/test/testvphooks.cpp
rename : sourcemm/svn_version.h => core/svn_version.h
rename : sourcemm/svn_version.tpl => core/svn_version.tpl
rename : sourcemm/version.rc => core/version.rc
2008-11-14 04:04:02 -06:00

300 lines
8.7 KiB
C++

/* ======== SourceHook ========
* Copyright (C) 2004-2008 Metamod:Source Development Team
* No warranties of any kind
*
* License: zlib/libpng
*
* Author(s): Pavol "PM OnoTo" Marko
* ============================
*/
#ifndef __SOURCEHOOK_HOOKMANGEN_H__
#define __SOURCEHOOK_HOOKMANGEN_H__
#include "sh_pagealloc.h"
namespace SourceHook
{
namespace Impl
{
// Code gen stuff
#if SH_COMP == SH_COMP_GCC
#include <stdint.h>
typedef int8_t jit_int8_t;
typedef uint8_t jit_uint8_t;
typedef int32_t jit_int32_t;
typedef uint32_t jit_uint32_t;
typedef int64_t jit_int64_t;
typedef uint64_t jit_uint64_t;
#elif SH_COMP == SH_COMP_MSVC
typedef __int8 jit_int8_t;
typedef unsigned __int8 jit_uint8_t;
typedef __int32 jit_int32_t;
typedef unsigned __int32 jit_uint32_t;
typedef __int64 jit_int64_t;
typedef unsigned __int64 jit_uint64_t;
#endif
typedef unsigned int jitoffs_t;
typedef signed int jitrel_t;
class GenBuffer
{
static CPageAlloc ms_Allocator;
unsigned char *m_pData;
jitoffs_t m_Size;
jitoffs_t m_AllocatedSize;
public:
GenBuffer() : m_pData(NULL), m_Size(0), m_AllocatedSize(0)
{
}
~GenBuffer()
{
clear();
}
jitoffs_t GetSize()
{
return m_Size;
}
unsigned char *GetData()
{
return m_pData;
}
template <class PT> void push(PT what)
{
push((const unsigned char *)&what, sizeof(PT));
}
void push(const unsigned char *data, jitoffs_t size)
{
jitoffs_t newSize = m_Size + size;
if (newSize > m_AllocatedSize)
{
m_AllocatedSize = newSize > m_AllocatedSize*2 ? newSize : m_AllocatedSize*2;
if (m_AllocatedSize < 64)
m_AllocatedSize = 64;
unsigned char *newBuf;
newBuf = reinterpret_cast<unsigned char*>(ms_Allocator.Alloc(m_AllocatedSize));
ms_Allocator.SetRW(newBuf);
if (!newBuf)
{
SH_ASSERT(0, ("bad_alloc: couldn't allocate 0x%08X bytes of memory\n", m_AllocatedSize));
return;
}
memset((void*)newBuf, 0xCC, m_AllocatedSize); // :TODO: remove this !
memcpy((void*)newBuf, (const void*)m_pData, m_Size);
if (m_pData)
{
ms_Allocator.SetRE(reinterpret_cast<void*>(m_pData));
ms_Allocator.SetRW(newBuf);
ms_Allocator.Free(reinterpret_cast<void*>(m_pData));
}
m_pData = newBuf;
}
memcpy((void*)(m_pData + m_Size), (const void*)data, size);
m_Size = newSize;
}
template <class PT> void rewrite(jitoffs_t offset, PT what)
{
rewrite(offset, (const unsigned char *)&what, sizeof(PT));
}
void rewrite(jitoffs_t offset, const unsigned char *data, jitoffs_t size)
{
SH_ASSERT(offset + size <= m_AllocatedSize, ("rewrite too far"));
memcpy((void*)(m_pData + offset), (const void*)data, size);
}
void clear()
{
if (m_pData)
ms_Allocator.Free(reinterpret_cast<void*>(m_pData));
m_pData = NULL;
m_Size = 0;
m_AllocatedSize = 0;
}
void SetRE()
{
ms_Allocator.SetRE(reinterpret_cast<void*>(m_pData));
}
operator void *()
{
return reinterpret_cast<void*>(GetData());
}
void write_ubyte(jit_uint8_t x) { push(x); }
void write_byte(jit_uint8_t x) { push(x); }
void write_ushort(unsigned short x) { push(x); }
void write_short(signed short x) { push(x); }
void write_uint32(jit_uint32_t x) { push(x); }
void write_int32(jit_uint32_t x) { push(x); }
jitoffs_t get_outputpos()
{
return m_Size;
}
void start_count(jitoffs_t &offs)
{
offs = get_outputpos();
}
void end_count(jitoffs_t &offs)
{
offs = get_outputpos() - offs;
}
};
class GenContext
{
const static int SIZE_MWORD = 4;
const static int SIZE_PTR = sizeof(void*);
const static int PassFlag_ForcedByRef = (1<<30); // ByVal in source, but actually passed by reference (GCC) -> private pass, destruct
HookManagerPubFunc m_GeneratedPubFunc;
CProto m_OrigProto; // original passed-in prototype
CProto m_Proto;
int m_VtblOffs;
int m_VtblIdx;
ISourceHook *m_SHPtr;
GenBuffer m_HookFunc;
GenBuffer m_PubFunc;
ProtoInfo *m_BuiltPI;
PassInfo *m_BuiltPI_Params;
PassInfo::V2Info *m_BuiltPI_Params2;
// For hookfunc
void **m_pHI;
void **m_HookfuncVfnptr;
// Level 3 - Helpers
int m_RegCounter;
jit_int8_t NextRegEBX_ECX_EDX();
int m_BytesPushedAfterInitialAlignment;
enum AlignStackFlags
{
AlignStack_GCC_ThisOnStack = 1,
AlignStack_MSVC_ThisOnStack = 2,
AlignStack_MemRet = 4
};
jit_int32_t AlignStackBeforeCall(int paramsize, int flags);
void AlignStackAfterCall(jit_int32_t numofbytes);
void CheckAlignmentBeforeCall();
// size info
jit_int32_t GetRealSize(const IntPassInfo &info); // checks for reference
jit_int32_t AlignSize(jit_int32_t x, jit_int32_t boundary); // align a size
jit_int32_t GetParamStackSize(const IntPassInfo &info); // get the size of a param in the param stack
short GetParamsTotalStackSize(); // sum(GetParamStackSize(param[i]), 0 <= i < numOfParams)
// Helpers
void BitwiseCopy_Setup();
void BitwiseCopy_Do(size_t size);
// HookFunc frame
jit_int32_t m_HookFunc_FrameOffset;
jit_int32_t m_HookFunc_FrameVarsSize;
void ResetFrame(jit_int32_t startOffset);
jit_int32_t AddVarToFrame(jit_int32_t size);
jit_int32_t ComputeVarsSize();
// Param push
short GetForcedByRefParamsSize(); // sum(param[i] is forcedbyref ? GetStackSize(param[i]) : 0, 0 <= i < numOfParams)
short GetForcedByRefParamOffset(int p); // sum(param[i] is forcedbyref ? GetStackSize(param[i]) : 0, 0 <= i < p)
jit_int32_t PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to,
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
jit_int32_t PushRef(jit_int32_t param_offset, const IntPassInfo &pi);
jit_int32_t PushBasic(jit_int32_t param_offset, const IntPassInfo &pi);
jit_int32_t PushFloat(jit_int32_t param_offset, const IntPassInfo &pi);
jit_int32_t PushObject(jit_int32_t param_offset, const IntPassInfo &pi, jit_int32_t v_place_fbrr);
jit_int32_t PushMemRetPtr(jit_int32_t save_ret_to, jit_int32_t v_place_for_memret);
void DestroyParams(jit_int32_t fbrr_base);
// Ret val processing
void SaveRetVal(jit_int32_t v_where, jit_int32_t v_place_for_memret);
void ProcessPluginRetVal(jit_int32_t v_cur_res, jit_int32_t v_pContext, jit_int32_t v_plugin_ret);
void PrepareReturn(jit_int32_t v_status, jit_int32_t v_pContext, jit_int32_t v_retptr);
void DoReturn(jit_int32_t v_retptr, jit_int32_t v_memret_outaddr);
bool MemRetWithTempObj(); // do we do a memory return AND need a temporary place for it?
// Call hooks
void GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
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);
// Call orig
void GenerateCallOrig(int v_status, int v_pContext, int param_base_offs, int v_this,
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);
// Hook loop
void CallSetupHookLoop(int v_orig_ret, int v_override_ret,
int v_cur_res, int v_prev_res, int v_status, int v_vnfptr_origentry,
int v_this, int v_pContext);
void CallEndContext(int v_pContext);
// Level 2 -> called from Generate()
void AutoDetectRetType();
void AutoDetectParamFlags();
bool PassInfoSupported(const IntPassInfo &pi, bool is_ret);
void Clear();
void BuildProtoInfo();
void *GenerateHookFunc();
void *GeneratePubFunc();
HookManagerPubFunc Generate();
public:
// Level 1 -> Public interface
GenContext(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx, ISourceHook *pSHPtr);
~GenContext();
bool Equal(const CProto &proto, int vtbl_offs, int vtbl_idx);
bool Equal(HookManagerPubFunc other);
HookManagerPubFunc GetPubFunc();
};
class CHookManagerAutoGen : public IHookManagerAutoGen
{
struct StoredContext
{
int m_RefCnt;
GenContext *m_GenContext;
};
List<StoredContext> m_Contexts;
ISourceHook *m_pSHPtr;
public:
CHookManagerAutoGen(ISourceHook *pSHPtr);
~CHookManagerAutoGen();
int GetIfaceVersion();
int GetImplVersion();
HookManagerPubFunc MakeHookMan(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx);
void ReleaseHookMan(HookManagerPubFunc pubFunc);
};
}
}
#endif