3 * =============================================================================
4 * SourceMod BinTools Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
29 * Version: $Id: CallMaker.h 1964 2008-03-27 04:54:56Z damagedsoul $
32 #if defined HOOKING_ENABLED
34 #include "HookWrapper.h"
35 #include "jit_compile.h"
37 /******************************
38 * HookWrapper Implementation *
39 ******************************/
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)
46 unsigned int argnum = proto->numOfParams;
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;
56 /* If the function has parameters calculate the parameter buffer size and offsets */
59 const SourceHook::PassInfo *info = m_ProtoInfo.paramsPassInfo;
60 m_ParamOffs = new unsigned int[argnum];
63 for (unsigned int i=1; i<=argnum; i++)
65 /* The stack should be at least aligned to a 4 byte boundary
66 * and byref params have the size of pointers
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))
73 m_ParamSize += sizeof(void *);
77 m_ParamSize += info[i].size;
83 m_ParamOffs[i] = m_ParamSize;
88 /* Finally calculate the retval size if any */
89 size_t retsize = m_ProtoInfo.retPassInfo.size;
92 if (m_ProtoInfo.retPassInfo.flags & SourceHook::PassInfo::PassFlag_ByRef)
94 m_RetSize = sizeof(void *);
98 m_RetSize = (retsize < sizeof(void *)) ? sizeof(void *) : retsize;
103 ISMDelegate *HookWrapper::CreateDelegate(void *data)
105 SMDelegate *pDeleg = new SMDelegate(data);
106 pDeleg->PatchVtable(m_HookWrapper);
111 unsigned int HookWrapper::GetParamCount()
113 return m_ProtoInfo.numOfParams;
116 unsigned int HookWrapper::GetParamOffset(unsigned int argnum, unsigned int *size)
118 assert(m_ParamOffs && argnum < (unsigned int)m_ProtoInfo.numOfParams);
122 *size = m_ProtoInfo.paramsPassInfo[argnum+1].size;
125 return m_ParamOffs[argnum];
128 void HookWrapper::PerformRecall(void *params, void *retval)
130 /* Notify SourceHook of the upcoming recall */
131 SH_GLOB_SHPTR->DoRecall();
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);
138 /* Execute the call */
139 m_CallWrapper->Execute(newparams, retval);
141 SET_META_RESULT(MRES_SUPERCEDE);
146 void HookWrapper::Destroy()
148 if (m_HookWrapper != NULL)
150 JIT_FreeHook(m_HookWrapper);
153 if (m_CallWrapper != NULL)
155 m_CallWrapper->Destroy();
161 unsigned int HookWrapper::GetParamSize()
166 SourceHook::ProtoInfo * HookWrapper::GetProtoInfo()
171 unsigned int HookWrapper::GetRetSize()
176 void * HookWrapper::GetHandlerAddr()
181 void HookWrapper::SetCallWrapperAddr( ICallWrapper *wrap )
183 m_CallWrapper = wrap;
186 void HookWrapper::SetHookWrpAddr( void *addr )
188 m_HookWrapper = addr;
191 HookWrapper::~HookWrapper()
193 delete [] m_ProtoInfo.paramsPassInfo;
194 delete [] m_ParamOffs;
196 /*****************************
197 * SMDelegate Implementation *
198 *****************************/
200 SMDelegate::SMDelegate(void *data)
205 bool SMDelegate::IsEqual(ISHDelegate *pOtherDeleg)
210 void SMDelegate::DeleteThis()
212 /* Remove our allocated vtable */
213 delete [] *reinterpret_cast<void ***>(this);
218 void SMDelegate::Call()
222 void *SMDelegate::GetUserData()
228 * This duplicates the vtable (so each instance has a unique table) and patches SMDelgate::Call to point to our allocated function
230 void SMDelegate::PatchVtable(void *addr)
232 void **new_vtptr = new void *[4];
233 void **cur_vtptr = *reinterpret_cast<void ***>(this);
234 memcpy(new_vtptr, cur_vtptr, sizeof(void *)*4);
236 *reinterpret_cast<void ***>(this) = new_vtptr;
238 void *cur_vfnptr = reinterpret_cast<void *>(new_vtptr + VTABLE_PATCH_OFFS);
240 *reinterpret_cast<void **>(cur_vfnptr) = addr;