extensions/bintools/CallWrapper.cpp
author Scott Ehlert <ds@alliedmods.net>
Sat Apr 23 21:53:53 2011 -0500 (2011-04-23)
changeset 3261 e4359292e59a
parent 2714 f9b9ae8234f0
permissions -rw-r--r--
Added detection for clang compiler and fixed various warnings triggered by it (bug 4878, r=dvander).
faluco@739
     1
/**
faluco@739
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
ds@757
     4
 * SourceMod BinTools Extension
ds@1947
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
faluco@739
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1226
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1226
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1226
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
faluco@739
    28
 *
faluco@739
    29
 * Version: $Id$
faluco@739
    30
 */
faluco@739
    31
faluco@739
    32
#include "extension.h"
faluco@739
    33
#include "CallWrapper.h"
pred@2504
    34
#include "CallMaker.h"
faluco@739
    35
pred@2504
    36
CallWrapper::CallWrapper(const SourceHook::ProtoInfo *protoInfo)
faluco@739
    37
{
pred@2504
    38
	m_AddrCodeBase = NULL;
pred@2504
    39
	m_AddrCallee = NULL;
pred@2504
    40
pred@2504
    41
	unsigned int argnum = protoInfo->numOfParams;
faluco@739
    42
pred@2504
    43
	m_Info = *protoInfo;
pred@2504
    44
	m_Info.paramsPassInfo = new SourceHook::PassInfo[argnum + 1];
pred@2504
    45
	memcpy((void *)m_Info.paramsPassInfo, protoInfo->paramsPassInfo, sizeof(SourceHook::PassInfo) * (argnum+1));
pred@2504
    46
pred@2504
    47
	if (argnum)
faluco@739
    48
	{
pred@2504
    49
		m_Params = new PassEncode[argnum];
pred@2504
    50
		for (size_t i=0; i<argnum; i++)
faluco@739
    51
		{
pred@2504
    52
			GetSMPassInfo(&(m_Params[i].info), &(m_Info.paramsPassInfo[i+1]));
faluco@739
    53
		}
dvander@948
    54
	} else {
dvander@948
    55
		m_Params = NULL;
faluco@739
    56
	}
faluco@739
    57
pred@2504
    58
	if (m_Info.retPassInfo.size != 0)
faluco@739
    59
	{
faluco@739
    60
		m_RetParam = new PassInfo;
pred@2504
    61
		GetSMPassInfo(m_RetParam, &(m_Info.retPassInfo));
faluco@739
    62
	} else {
faluco@739
    63
		m_RetParam = NULL;
faluco@739
    64
	}
faluco@739
    65
faluco@739
    66
	/* Calculate virtual stack offsets for each parameter */
faluco@739
    67
	size_t offs = 0;
faluco@739
    68
pred@2504
    69
	if (m_Info.convention == SourceHook::ProtoInfo::CallConv_ThisCall)
faluco@739
    70
	{
faluco@739
    71
		offs += sizeof(void *);
faluco@739
    72
	}
pred@2504
    73
	for (size_t i=0; i<argnum; i++)
faluco@739
    74
	{
faluco@739
    75
		m_Params[i].offset = offs;
faluco@739
    76
		offs += m_Params[i].info.size;
faluco@739
    77
	}
faluco@739
    78
}
faluco@739
    79
faluco@739
    80
CallWrapper::~CallWrapper()
faluco@739
    81
{
faluco@739
    82
	delete [] m_Params;
faluco@739
    83
	delete m_RetParam;
pred@2504
    84
	delete [] m_Info.paramsPassInfo;
faluco@739
    85
}
faluco@739
    86
dvander@942
    87
void CallWrapper::Destroy()
dvander@942
    88
{
pred@2504
    89
	if (m_AddrCodeBase != NULL)
pred@2504
    90
	{
pred@2504
    91
		g_SPEngine->FreePageMemory(m_AddrCodeBase);
pred@2504
    92
		m_AddrCodeBase = NULL;
pred@2504
    93
	}
pred@2504
    94
dvander@942
    95
	delete this;
dvander@942
    96
}
dvander@942
    97
faluco@739
    98
CallConvention CallWrapper::GetCallConvention()
faluco@739
    99
{
pred@2504
   100
	/* Need to convert to a SourceMod convention for bcompat */
pred@2504
   101
	return GetSMCallConvention((SourceHook::ProtoInfo::CallConvention)m_Info.convention);
faluco@739
   102
}
faluco@739
   103
faluco@739
   104
const PassEncode *CallWrapper::GetParamInfo(unsigned int num)
faluco@739
   105
{
ds@3261
   106
	if (num + 1 > GetParamCount())
pred@2504
   107
	{
pred@2504
   108
		return NULL;
pred@2504
   109
	}
pred@2504
   110
pred@2504
   111
	return &m_Params[num];
faluco@739
   112
}
faluco@739
   113
faluco@739
   114
const PassInfo *CallWrapper::GetReturnInfo()
faluco@739
   115
{
faluco@739
   116
	return m_RetParam;
faluco@739
   117
}
faluco@739
   118
faluco@739
   119
unsigned int CallWrapper::GetParamCount()
faluco@739
   120
{
pred@2504
   121
	return m_Info.numOfParams;
faluco@739
   122
}
faluco@739
   123
faluco@739
   124
void CallWrapper::Execute(void *vParamStack, void *retBuffer)
faluco@739
   125
{
faluco@739
   126
	typedef void (*CALL_EXECUTE)(void *, void *);
pred@2504
   127
	CALL_EXECUTE fn = (CALL_EXECUTE)GetCodeBaseAddr();
faluco@739
   128
	fn(vParamStack, retBuffer);
faluco@739
   129
}
pred@2504
   130
pred@2504
   131
void CallWrapper::SetMemFuncInfo(const SourceHook::MemFuncInfo *funcInfo)
pred@2504
   132
{
pred@2504
   133
	m_FuncInfo = *funcInfo;
pred@2504
   134
}
pred@2504
   135
pred@2504
   136
SourceHook::MemFuncInfo *CallWrapper::GetMemFuncInfo()
pred@2504
   137
{
pred@2504
   138
	return &m_FuncInfo;
pred@2504
   139
}
pred@2504
   140
pred@2504
   141
void CallWrapper::SetCalleeAddr(void *addr)
pred@2504
   142
{
pred@2504
   143
	m_AddrCallee = addr;
pred@2504
   144
}
pred@2504
   145
pred@2504
   146
void CallWrapper::SetCodeBaseAddr(void *addr)
pred@2504
   147
{
pred@2504
   148
	m_AddrCodeBase = addr;
pred@2504
   149
}
pred@2504
   150
pred@2504
   151
void *CallWrapper::GetCalleeAddr()
pred@2504
   152
{
pred@2504
   153
	return m_AddrCallee;
pred@2504
   154
}
pred@2504
   155
pred@2504
   156
void *CallWrapper::GetCodeBaseAddr()
pred@2504
   157
{
pred@2504
   158
	return m_AddrCodeBase;
pred@2504
   159
}
pred@2504
   160
pred@2504
   161
const SourceHook::PassInfo *CallWrapper::GetSHReturnInfo()
pred@2504
   162
{
pred@2504
   163
	return &(m_Info.retPassInfo);
pred@2504
   164
}
pred@2504
   165
pred@2504
   166
SourceHook::ProtoInfo::CallConvention CallWrapper::GetSHCallConvention()
pred@2504
   167
{
pred@2504
   168
	return (SourceHook::ProtoInfo::CallConvention)m_Info.convention;
pred@2504
   169
}
pred@2504
   170
pred@2504
   171
const SourceHook::PassInfo * CallWrapper::GetSHParamInfo(unsigned int num)
pred@2504
   172
{
ds@3261
   173
	if (num + 1 > GetParamCount())
pred@2504
   174
	{
pred@2504
   175
		return NULL;
pred@2504
   176
	}
pred@2504
   177
pred@2504
   178
	return &(m_Info.paramsPassInfo[num+1]);
pred@2504
   179
}
pred@2504
   180
pred@2504
   181
unsigned int CallWrapper::GetParamOffset(unsigned int num)
pred@2504
   182
{
fyrenmoo@2714
   183
	assert(num < GetParamCount() && num >= 0);
pred@2504
   184
pred@2504
   185
	return m_Params[num].offset;
pred@2504
   186
}