extensions/bintools/HookWrapper.cpp
author Matt Woodrow <pred@alliedmods.net>
Thu Jan 08 18:17:17 2009 +1300 (2009-01-08)
changeset 2504 74fea7db93e4
child 3104 b15fb3b476f1
permissions -rw-r--r--
Added Dynamic Hooking to BinTools (bug 2616 r=dvander,DS)
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod BinTools Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id: CallMaker.h 1964 2008-03-27 04:54:56Z damagedsoul $
    30  */
    31 
    32 #if defined HOOKING_ENABLED
    33 
    34 #include "HookWrapper.h"
    35 #include "jit_compile.h"
    36 
    37 /******************************
    38  * HookWrapper Implementation *
    39  ******************************/
    40 
    41 HookWrapper::HookWrapper(SourceHook::ISourceHook *pSH, 
    42 						 const SourceHook::ProtoInfo *proto, 
    43 						 SourceHook::MemFuncInfo *memfunc, 
    44 						 void *addr) : m_ParamOffs(NULL), m_ParamSize(0), m_RetSize(0)
    45 {
    46 	unsigned int argnum = proto->numOfParams;
    47 
    48 	m_pSH = pSH;
    49 	m_ProtoInfo = *proto;
    50 	m_ProtoInfo.paramsPassInfo = new SourceHook::PassInfo[argnum + 1];
    51 	memcpy((void *)m_ProtoInfo.paramsPassInfo, proto->paramsPassInfo, sizeof(SourceHook::PassInfo) * (argnum+1));
    52 	m_MemFuncInfo = *memfunc;
    53 	m_FuncAddr = addr;
    54 	m_HookWrapper = NULL;
    55 
    56 	/* If the function has parameters calculate the parameter buffer size and offsets */
    57 	if (argnum > 0)
    58 	{
    59 		const SourceHook::PassInfo *info = m_ProtoInfo.paramsPassInfo;
    60 		m_ParamOffs = new unsigned int[argnum];
    61 		m_ParamOffs[0] = 0;
    62 
    63 		for (unsigned int i=1; i<=argnum; i++)
    64 		{
    65 			/* The stack should be at least aligned to a 4 byte boundary 
    66 			* and byref params have the size of pointers 
    67 			*/
    68 			if (((info[i].type == SourceHook::PassInfo::PassType_Basic) &&
    69 				(info[i].flags & SourceHook::PassInfo::PassFlag_ByVal) && 
    70 				(info[i].size < sizeof(void *))) ||
    71 				(info[i].flags & SourceHook::PassInfo::PassFlag_ByRef))
    72 			{
    73 				m_ParamSize += sizeof(void *);
    74 			}
    75 			else
    76 			{
    77 				m_ParamSize += info[i].size;
    78 			}
    79 
    80 			//:TODO: fix this!
    81 			if (i < argnum)
    82 			{
    83 				m_ParamOffs[i] = m_ParamSize;
    84 			}
    85 		}
    86 	}
    87 
    88 	/* Finally calculate the retval size if any */
    89 	size_t retsize = m_ProtoInfo.retPassInfo.size;
    90 	if (retsize != 0)
    91 	{
    92 		if (m_ProtoInfo.retPassInfo.flags & SourceHook::PassInfo::PassFlag_ByRef)
    93 		{
    94 			m_RetSize = sizeof(void *);
    95 		}
    96 		else
    97 		{
    98 			m_RetSize = (retsize < sizeof(void *)) ? sizeof(void *) : retsize;
    99 		}
   100 	}
   101 }
   102 
   103 ISMDelegate *HookWrapper::CreateDelegate(void *data)
   104 {
   105 	SMDelegate *pDeleg = new SMDelegate(data);
   106 	pDeleg->PatchVtable(m_HookWrapper);
   107 
   108 	return pDeleg;
   109 }
   110 
   111 unsigned int HookWrapper::GetParamCount()
   112 {
   113 	return m_ProtoInfo.numOfParams;
   114 }
   115 
   116 unsigned int HookWrapper::GetParamOffset(unsigned int argnum, unsigned int *size)
   117 {
   118 	assert(m_ParamOffs && argnum < (unsigned int)m_ProtoInfo.numOfParams);
   119 
   120 	if (size)
   121 	{
   122 		*size = m_ProtoInfo.paramsPassInfo[argnum+1].size;
   123 	}
   124 
   125 	return m_ParamOffs[argnum];
   126 }
   127 
   128 void HookWrapper::PerformRecall(void *params, void *retval)
   129 {
   130 	/* Notify SourceHook of the upcoming recall */
   131 	SH_GLOB_SHPTR->DoRecall();
   132 
   133 	/* Add thisptr into params buffer */
   134 	unsigned char *newparams = new unsigned char[sizeof(void *) + m_ParamSize];
   135 	*(void **)newparams = META_IFACEPTR(void);
   136 	memcpy(newparams + sizeof(void *), params, m_ParamSize);
   137 
   138 	/* Execute the call */
   139 	m_CallWrapper->Execute(newparams, retval);
   140 
   141 	SET_META_RESULT(MRES_SUPERCEDE);
   142 
   143 	return;
   144 }
   145 
   146 void HookWrapper::Destroy()
   147 {
   148 	if (m_HookWrapper != NULL)
   149 	{
   150 		JIT_FreeHook(m_HookWrapper);
   151 	}
   152 
   153 	if (m_CallWrapper != NULL)
   154 	{
   155 		m_CallWrapper->Destroy();
   156 	}
   157 
   158 	delete this;
   159 }
   160 
   161 unsigned int HookWrapper::GetParamSize()
   162 {
   163 	return m_ParamSize;
   164 }
   165 
   166 SourceHook::ProtoInfo * HookWrapper::GetProtoInfo()
   167 {
   168 	return &m_ProtoInfo;
   169 }
   170 
   171 unsigned int HookWrapper::GetRetSize()
   172 {
   173 	return m_RetSize;
   174 }
   175 
   176 void * HookWrapper::GetHandlerAddr()
   177 {
   178 	return m_FuncAddr;
   179 }
   180 
   181 void HookWrapper::SetCallWrapperAddr( ICallWrapper *wrap )
   182 {
   183 	m_CallWrapper = wrap;
   184 }
   185 
   186 void HookWrapper::SetHookWrpAddr( void *addr )
   187 {
   188 	m_HookWrapper = addr;
   189 }
   190 
   191 HookWrapper::~HookWrapper()
   192 {
   193 	delete [] m_ProtoInfo.paramsPassInfo;
   194 	delete [] m_ParamOffs;
   195 }
   196 /*****************************
   197  * SMDelegate Implementation *
   198  *****************************/
   199 
   200 SMDelegate::SMDelegate(void *data)
   201 {
   202 	m_Data = data;
   203 }
   204 
   205 bool SMDelegate::IsEqual(ISHDelegate *pOtherDeleg)
   206 {
   207 	return false;
   208 }
   209 
   210 void SMDelegate::DeleteThis()
   211 {
   212 	/* Remove our allocated vtable */
   213 	delete [] *reinterpret_cast<void ***>(this);
   214 
   215 	delete this;
   216 }
   217 
   218 void SMDelegate::Call()
   219 {
   220 }
   221 
   222 void *SMDelegate::GetUserData()
   223 {
   224 	return m_Data;
   225 }
   226 
   227 /** 
   228  * This duplicates the vtable (so each instance has a unique table) and patches SMDelgate::Call to point to our allocated function 
   229  */
   230 void SMDelegate::PatchVtable(void *addr)
   231 {
   232 	void **new_vtptr = new void *[4];
   233 	void **cur_vtptr = *reinterpret_cast<void ***>(this);
   234 	memcpy(new_vtptr, cur_vtptr, sizeof(void *)*4);
   235 
   236 	*reinterpret_cast<void ***>(this) = new_vtptr;
   237 
   238 	void *cur_vfnptr = reinterpret_cast<void *>(new_vtptr + VTABLE_PATCH_OFFS);
   239 
   240 	*reinterpret_cast<void **>(cur_vfnptr) = addr;
   241 }
   242 
   243 #endif