sourcepawn/jit/sp_vm_function.cpp
author David Anderson <dvander@alliedmods.net>
Sun Oct 26 23:50:54 2008 -0500 (2008-10-26)
changeset 2377 6ee3bbd6fbdf
parent 2226 d10b38524be3
child 2825 811ec7a539c3
permissions -rw-r--r--
Fixed leaking relocation table memory in JIT.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourcePawn
     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$
    30  */
    31 
    32 #include <stdio.h>
    33 #include <string.h>
    34 #include "sp_vm_function.h"
    35 #include "BaseRuntime.h"
    36 
    37 /********************
    38 * FUNCTION CALLING *
    39 ********************/
    40 
    41 void CFunction::Set(BaseRuntime *runtime, funcid_t fnid, uint32_t pub_id)
    42 {
    43 	m_pRuntime = runtime;
    44 	m_curparam = 0;
    45 	m_errorstate = SP_ERROR_NONE;
    46 	m_FnId = fnid;
    47 }
    48 
    49 bool CFunction::IsRunnable()
    50 {
    51 	return !m_pRuntime->IsPaused();
    52 }
    53 
    54 int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
    55 {
    56 	return CallFunction2(m_pRuntime->GetDefaultContext(), params, num_params, result);
    57 }
    58 
    59 int CFunction::CallFunction2(IPluginContext *pContext, const cell_t *params, unsigned int num_params, cell_t *result)
    60 {
    61 	return pContext->Execute2(this, params, num_params, result);
    62 }
    63 
    64 IPluginContext *CFunction::GetParentContext()
    65 {
    66 	return m_pRuntime->GetDefaultContext();
    67 }
    68 
    69 CFunction::CFunction(BaseRuntime *runtime, funcid_t id, uint32_t pub_id) : 
    70 	m_curparam(0), m_errorstate(SP_ERROR_NONE), m_FnId(id)
    71 {
    72 	m_pRuntime = runtime;
    73 }
    74 
    75 int CFunction::PushCell(cell_t cell)
    76 {
    77 	if (m_curparam >= SP_MAX_EXEC_PARAMS)
    78 	{
    79 		return SetError(SP_ERROR_PARAMS_MAX);
    80 	}
    81 
    82 	m_info[m_curparam].marked = false;
    83 	m_params[m_curparam] = cell;
    84 	m_curparam++;
    85 
    86 	return SP_ERROR_NONE;
    87 }
    88 
    89 int CFunction::PushCellByRef(cell_t *cell, int flags)
    90 {
    91 	return PushArray(cell, 1, flags);
    92 }
    93 
    94 int CFunction::PushFloat(float number)
    95 {
    96 	cell_t val = *(cell_t *)&number;
    97 
    98 	return PushCell(val);
    99 }
   100 
   101 int CFunction::PushFloatByRef(float *number, int flags)
   102 {
   103 	return PushCellByRef((cell_t *)number, flags);
   104 }
   105 
   106 int CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback)
   107 {
   108 	if (m_curparam >= SP_MAX_EXEC_PARAMS)
   109 	{
   110 		return SetError(SP_ERROR_PARAMS_MAX);
   111 	}
   112 
   113 	ParamInfo *info = &m_info[m_curparam];
   114 
   115 	info->flags = inarray ? copyback : 0;
   116 	info->marked = true;
   117 	info->size = cells;
   118 	info->str.is_sz = false;
   119 	info->orig_addr = inarray;
   120 
   121 	m_curparam++;
   122 
   123 	return SP_ERROR_NONE;
   124 }
   125 
   126 int CFunction::PushString(const char *string)
   127 {
   128 	return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
   129 }
   130 
   131 int CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
   132 {
   133 	return _PushString(buffer, sz_flags, cp_flags, length);
   134 }
   135 
   136 int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
   137 {
   138 	if (m_curparam >= SP_MAX_EXEC_PARAMS)
   139 	{
   140 		return SetError(SP_ERROR_PARAMS_MAX);
   141 	}
   142 
   143 	ParamInfo *info = &m_info[m_curparam];
   144 
   145 	info->marked = true;
   146 	info->orig_addr = (cell_t *)string;
   147 	info->flags = cp_flags;
   148 	info->size = len;
   149 	info->str.sz_flags = sz_flags;
   150 	info->str.is_sz = true;
   151 
   152 	m_curparam++;
   153 
   154 	return SP_ERROR_NONE;
   155 }
   156 
   157 void CFunction::Cancel()
   158 {
   159 	if (!m_curparam)
   160 	{
   161 		return;
   162 	}
   163 
   164 	m_errorstate = SP_ERROR_NONE;
   165 	m_curparam = 0;
   166 }
   167 
   168 int CFunction::Execute(cell_t *result)
   169 {
   170 	return Execute2(m_pRuntime->GetDefaultContext(), result);
   171 }
   172 
   173 int CFunction::Execute2(IPluginContext *ctx, cell_t *result)
   174 {
   175 	int err = SP_ERROR_NONE;
   176 
   177 	if (!IsRunnable())
   178 	{
   179 		m_errorstate = SP_ERROR_NOT_RUNNABLE;
   180 	}
   181 
   182 	if (m_errorstate != SP_ERROR_NONE)
   183 	{
   184 		err = m_errorstate;
   185 		Cancel();
   186 		return err;
   187 	}
   188 
   189 	//This is for re-entrancy!
   190 	cell_t temp_params[SP_MAX_EXEC_PARAMS];
   191 	ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
   192 	unsigned int numparams = m_curparam;
   193 	unsigned int i;
   194 	bool docopies = true;
   195 
   196 	if (numparams)
   197 	{
   198 		//Save the info locally, then reset it for re-entrant calls.
   199 		memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
   200 	}
   201 	m_curparam = 0;
   202 
   203 	/* Browse the parameters and build arrays */
   204 	for (i=0; i<numparams; i++)
   205 	{
   206 		/* Is this marked as an array? */
   207 		if (temp_info[i].marked)
   208 		{
   209 			if (!temp_info[i].str.is_sz)
   210 			{
   211 				/* Allocate a normal/generic array */
   212 				if ((err=ctx->HeapAlloc(temp_info[i].size, 
   213 										   &(temp_info[i].local_addr),
   214 										   &(temp_info[i].phys_addr)))
   215 					!= SP_ERROR_NONE)
   216 				{
   217 					break;
   218 				}
   219 				if (temp_info[i].orig_addr)
   220 				{
   221 					memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
   222 				}
   223 			}
   224 			else
   225 			{
   226 				/* Calculate cells required for the string */
   227 				size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
   228 
   229 				/* Allocate the buffer */
   230 				if ((err=ctx->HeapAlloc(cells,
   231 										&(temp_info[i].local_addr),
   232 										&(temp_info[i].phys_addr)))
   233 					!= SP_ERROR_NONE)
   234 				{
   235 					break;
   236 				}
   237 				/* Copy original string if necessary */
   238 				if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL))
   239 				{
   240 					/* Cut off UTF-8 properly */
   241 					if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
   242 					{
   243 						if ((err=ctx->StringToLocalUTF8(temp_info[i].local_addr, 
   244 															temp_info[i].size, 
   245 															(const char *)temp_info[i].orig_addr,
   246 															NULL))
   247 							!= SP_ERROR_NONE)
   248 						{
   249 							break;
   250 						}
   251 					}
   252 					/* Copy a binary blob */
   253 					else if (temp_info[i].str.sz_flags & SM_PARAM_STRING_BINARY)
   254 					{
   255 						memmove(temp_info[i].phys_addr, temp_info[i].orig_addr, temp_info[i].size);
   256 					}
   257 					/* Copy ASCII characters */
   258 					else
   259 					{
   260 						if ((err=ctx->StringToLocal(temp_info[i].local_addr,
   261 														temp_info[i].size,
   262 														(const char *)temp_info[i].orig_addr))
   263 							!= SP_ERROR_NONE)
   264 						{
   265 							break;
   266 						}
   267 					}
   268 				}
   269 			} /* End array/string calculation */
   270 			/* Update the pushed parameter with the byref local address */
   271 			temp_params[i] = temp_info[i].local_addr;
   272 		}
   273 		else
   274 		{
   275 			/* Just copy the value normally */
   276 			temp_params[i] = m_params[i];
   277 		}
   278 	}
   279 
   280 	/* Make the call if we can */
   281 	if (err == SP_ERROR_NONE)
   282 	{
   283 		if ((err = CallFunction2(ctx, temp_params, numparams, result)) != SP_ERROR_NONE)
   284 		{
   285 			docopies = false;
   286 		}
   287 	}
   288 	else
   289 	{
   290 		docopies = false;
   291 	}
   292 
   293 	/* i should be equal to the last valid parameter + 1 */
   294 	while (i--)
   295 	{
   296 		if (!temp_info[i].marked)
   297 		{
   298 			continue;
   299 		}
   300 
   301 		if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK))
   302 		{
   303 			if (temp_info[i].orig_addr)
   304 			{
   305 				if (temp_info[i].str.is_sz)
   306 				{
   307 					memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
   308 				
   309 				}
   310 				else
   311 				{
   312 					if (temp_info[i].size == 1)
   313 					{
   314 						*temp_info[i].orig_addr = *(temp_info[i].phys_addr);
   315 					}
   316 					else
   317 					{
   318 						memcpy(temp_info[i].orig_addr, 
   319 								temp_info[i].phys_addr, 
   320 								temp_info[i].size * sizeof(cell_t));
   321 					}
   322 				}
   323 			}
   324 		}
   325 
   326 		if ((err=ctx->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)
   327 		{
   328 			return err;
   329 		}
   330 	}
   331 
   332 	return err;
   333 }
   334 
   335 funcid_t CFunction::GetFunctionID()
   336 {
   337 	return m_FnId;
   338 }
   339 
   340 int CFunction::SetError(int err)
   341 {
   342 	m_errorstate = err;
   343 
   344 	return err;
   345 }
   346