core/vm/sp_vm_function.cpp
author David Anderson <dvander@alliedmods.net>
Sat Mar 10 21:02:40 2007 +0000 (2007-03-10)
changeset 589 5c1dd0ca6191
parent 586 1e73cb333b19
child 596 20a01c05806d
permissions -rw-r--r--
IPluginFunction implementation is re-entrant across native calls, as heap allocations are delayed until execution
removed ICallable::GetAddressOfPushedParam
removed phys_addr from ICallable::PushArray
fixed a bug where sp_context_t::n_idx was overwritten upon re-entrant calls
     1 /**
     2  * ================================================================
     3  * SourcePawn (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ================================================================
     5  *
     6  * This file is not open source and may not be copied without explicit
     7  * written permission of AlliedModders LLC.  This file may not be redistributed 
     8  * in whole or significant part.
     9  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #include <stdio.h>
    15 #include <string.h>
    16 #include "PluginSys.h"
    17 
    18 /********************
    19 * FUNCTION CALLING *
    20 ********************/
    21 
    22 void CFunction::Set(uint32_t code_addr, IPluginContext *plugin)
    23 {
    24 	m_codeaddr = code_addr;
    25 	m_pContext = plugin;
    26 	m_curparam = 0;
    27 	m_errorstate = SP_ERROR_NONE;
    28 	m_Invalid = false;
    29 	m_pCtx = plugin ? plugin->GetContext() : NULL;
    30 }
    31 
    32 bool CFunction::IsRunnable()
    33 {
    34 	return ((m_pCtx->flags & SPFLAG_PLUGIN_PAUSED) != SPFLAG_PLUGIN_PAUSED);
    35 }
    36 
    37 int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
    38 {
    39 	if (!IsRunnable())
    40 	{
    41 		return SP_ERROR_NOT_RUNNABLE;
    42 	}
    43 
    44 	while (num_params--)
    45 	{
    46 		m_pContext->PushCell(params[num_params]);
    47 	}
    48 
    49 	return m_pContext->Execute(m_codeaddr, result);
    50 }
    51 
    52 IPluginContext *CFunction::GetParentContext()
    53 {
    54 	return m_pContext;
    55 }
    56 
    57 CFunction::CFunction(uint32_t code_addr, IPluginContext *plugin) : 
    58 	m_codeaddr(code_addr), m_pContext(plugin), m_curparam(0), 
    59 	m_errorstate(SP_ERROR_NONE)
    60 {
    61 	m_Invalid = false;
    62 	if (plugin)
    63 	{
    64 		m_pCtx = plugin->GetContext();
    65 	}
    66 }
    67 
    68 int CFunction::PushCell(cell_t cell)
    69 {
    70 	if (m_curparam >= SP_MAX_EXEC_PARAMS)
    71 	{
    72 		return SetError(SP_ERROR_PARAMS_MAX);
    73 	}
    74 
    75 	m_info[m_curparam].marked = false;
    76 	m_params[m_curparam] = cell;
    77 	m_curparam++;
    78 
    79 	return SP_ERROR_NONE;
    80 }
    81 
    82 int CFunction::PushCellByRef(cell_t *cell, int flags)
    83 {
    84 	if (m_curparam >= SP_MAX_EXEC_PARAMS)
    85 	{
    86 		return SetError(SP_ERROR_PARAMS_MAX);
    87 	}
    88 
    89 	return PushArray(cell, 1, flags);
    90 }
    91 
    92 int CFunction::PushFloat(float number)
    93 {
    94 	cell_t val = *(cell_t *)&number;
    95 
    96 	return PushCell(val);
    97 }
    98 
    99 int CFunction::PushFloatByRef(float *number, int flags)
   100 {
   101 	return PushCellByRef((cell_t *)number, flags);
   102 }
   103 
   104 int CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback)
   105 {
   106 	if (m_curparam >= SP_MAX_EXEC_PARAMS)
   107 	{
   108 		return SetError(SP_ERROR_PARAMS_MAX);
   109 	}
   110 
   111 	ParamInfo *info = &m_info[m_curparam];
   112 
   113 	info->flags = inarray ? copyback : 0;
   114 	info->marked = true;
   115 	info->size = cells;
   116 	info->str.is_sz = false;
   117 	info->orig_addr = inarray;
   118 
   119 	m_curparam++;
   120 
   121 	return SP_ERROR_NONE;
   122 }
   123 
   124 int CFunction::PushString(const char *string)
   125 {
   126 	return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
   127 }
   128 
   129 int CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
   130 {
   131 	return _PushString(buffer, sz_flags, cp_flags, length);
   132 }
   133 
   134 int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
   135 {
   136 	if (m_curparam >= SP_MAX_EXEC_PARAMS)
   137 	{
   138 		return SetError(SP_ERROR_PARAMS_MAX);
   139 	}
   140 
   141 	ParamInfo *info = &m_info[m_curparam];
   142 	size_t cells = (len + sizeof(cell_t) - 1) / sizeof(cell_t);
   143 
   144 	info->marked = true;
   145 	info->orig_addr = (cell_t *)string;
   146 	info->flags = cp_flags;
   147 	info->size = len;
   148 	info->str.sz_flags = sz_flags;
   149 	info->str.is_sz = true;
   150 
   151 	m_curparam++;
   152 
   153 	return SP_ERROR_NONE;
   154 }
   155 
   156 void CFunction::Cancel()
   157 {
   158 	if (!m_curparam)
   159 	{
   160 		return;
   161 	}
   162 
   163 	m_errorstate = SP_ERROR_NONE;
   164 	m_curparam = 0;
   165 }
   166 
   167 int CFunction::Execute(cell_t *result)
   168 {
   169 	int err = SP_ERROR_NONE;
   170 	if (!IsRunnable())
   171 	{
   172 		m_errorstate = SP_ERROR_NOT_RUNNABLE;
   173 	}
   174 	if (m_errorstate != SP_ERROR_NONE)
   175 	{
   176 		err = m_errorstate;
   177 		Cancel();
   178 		return err;
   179 	}
   180 
   181 	//This is for re-entrancy!
   182 	cell_t temp_params[SP_MAX_EXEC_PARAMS];
   183 	ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
   184 	unsigned int numparams = m_curparam;
   185 	unsigned int i;
   186 	bool docopies = true;
   187 
   188 	if (numparams)
   189 	{
   190 		//Save the info locally, then reset it for re-entrant calls.
   191 		memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
   192 	}
   193 	m_curparam = 0;
   194 
   195 	/* Browse the parameters and build arrays */
   196 	for (i=0; i<numparams; i++)
   197 	{
   198 		/* Is this marked as an array? */
   199 		if (temp_info[i].marked)
   200 		{
   201 			if (!temp_info[i].str.is_sz)
   202 			{
   203 				/* Allocate a normal/generic array */
   204 				if ((err=m_pContext->HeapAlloc(temp_info[i].size, 
   205 											   &(temp_info[i].local_addr),
   206 											   &(temp_info[i].phys_addr)))
   207 					!= SP_ERROR_NONE)
   208 				{
   209 					break;
   210 				}
   211 				if (temp_info[i].orig_addr)
   212 				{
   213 					memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
   214 				}
   215 			} else {
   216 				/* Calculate cells required for the string */
   217 				size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
   218 
   219 				/* Allocate the buffer */
   220 				if ((err=m_pContext->HeapAlloc(cells,
   221 												&(temp_info[i].local_addr),
   222 												&(temp_info[i].phys_addr)))
   223 					!= SP_ERROR_NONE)
   224 				{
   225 					break;
   226 				}
   227 				/* Copy original string if necessary */
   228 				if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL))
   229 				{
   230 					if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
   231 					{
   232 						if ((err=m_pContext->StringToLocalUTF8(temp_info[i].local_addr, 
   233 																temp_info[i].size, 
   234 																(const char *)temp_info[i].orig_addr,
   235 																NULL))
   236 							!= SP_ERROR_NONE)
   237 						{
   238 							break;
   239 						}
   240 					} else {
   241 						if ((err=m_pContext->StringToLocal(temp_info[i].local_addr,
   242 															temp_info[i].size,
   243 															(const char *)temp_info[i].orig_addr))
   244 							!= SP_ERROR_NONE)
   245 						{
   246 							break;
   247 						}
   248 					}
   249 				}
   250 			} /* End array/string calculation */
   251 			/* Update the pushed parameter with the byref local address */
   252 			temp_params[i] = temp_info[i].local_addr;
   253 		} else {
   254 			/* Just copy the value normally */
   255 			temp_params[i] = m_params[i];
   256 		}
   257 	}
   258 
   259 	/* Make the call if we can */
   260 	if (err == SP_ERROR_NONE)
   261 	{
   262 		if ((err = CallFunction(temp_params, numparams, result)) != SP_ERROR_NONE)
   263 		{
   264 			docopies = false;
   265 		}
   266 	} else {
   267 		docopies = false;
   268 	}
   269 
   270 	/* i should be equal to the last valid parameter + 1 */
   271 	while (i--)
   272 	{
   273 		if (!temp_info[i].marked)
   274 		{
   275 			continue;
   276 		}
   277 		if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK))
   278 		{
   279 			if (temp_info[i].orig_addr)
   280 			{
   281 				if (temp_info[i].str.is_sz)
   282 				{
   283 					memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
   284 				} else {
   285 					if (temp_info[i].size == 1)
   286 					{
   287 						*temp_info[i].orig_addr = *(temp_info[i].phys_addr);
   288 					} else {
   289 						memcpy(temp_info[i].orig_addr, 
   290 								temp_info[i].phys_addr, 
   291 								temp_info[i].size * sizeof(cell_t));
   292 					}
   293 				}
   294 			}
   295 		}
   296 		if ((err=m_pContext->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)
   297 		{
   298 			return err;
   299 		}
   300 	}
   301 
   302 	return err;
   303 }