Corrected an API design flaw with pausing. Contexts can now be flagged as paused, and IsRunnable() is moved from IBaseContext to IPluginFunction. While this allows for per-function pausing, it is not intended that way.
2 * ================================================================
3 * SourcePawn (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ================================================================
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
18 #include "sp_vm_api.h"
19 #include "sp_vm_basecontext.h"
20 #include "sp_vm_engine.h"
22 using namespace SourcePawn;
24 extern SourcePawnEngine g_SourcePawn;
26 #define CELLBOUNDMAX (INT_MAX/sizeof(cell_t))
27 #define STACKMARGIN ((cell_t)(16*sizeof(cell_t)))
29 int GlobalDebugBreak(sp_context_t *ctx, uint32_t frm, uint32_t cip)
31 g_SourcePawn.RunTracer(ctx, frm, cip);
36 BaseContext::BaseContext(sp_context_t *_ctx)
40 ctx->dbreak = GlobalDebugBreak;
43 m_funcsnum = ctx->vmbase->FunctionCount(ctx);
48 * Note: Since the m_plugin member will never change,
49 * it is safe to assume the function count will never change
51 if (m_funcsnum && m_priv_funcs == NULL)
53 m_priv_funcs = new CFunction *[m_funcsnum];
54 memset(m_priv_funcs, 0, sizeof(CFunction *) * m_funcsnum);
59 if (ctx->plugin->info.publics_num && m_pub_funcs == NULL)
61 m_pub_funcs = new CFunction *[ctx->plugin->info.publics_num];
62 memset(m_pub_funcs, 0, sizeof(CFunction *) * ctx->plugin->info.publics_num);
68 void BaseContext::FlushFunctionCache(bool remove)
72 for (uint32_t i=0; i<ctx->plugin->info.publics_num; i++)
76 delete m_pub_funcs[i];
77 m_pub_funcs[i] = NULL;
78 } else if (m_pub_funcs[i]) {
79 m_pub_funcs[i]->Invalidate();
86 for (unsigned int i=0; i<m_funcsnum; i++)
90 delete m_priv_funcs[i];
91 m_priv_funcs[i] = NULL;
92 } else if (m_priv_funcs[i]) {
93 m_priv_funcs[i]->Invalidate();
99 BaseContext::~BaseContext()
101 FlushFunctionCache(true);
102 delete [] m_pub_funcs;
104 delete [] m_priv_funcs;
108 void BaseContext::SetContext(sp_context_t *_ctx)
116 ctx->dbreak = GlobalDebugBreak;
117 FlushFunctionCache(false);
120 IVirtualMachine *BaseContext::GetVirtualMachine()
122 return (IVirtualMachine *)ctx->vmbase;
125 sp_context_t *BaseContext::GetContext()
130 bool BaseContext::IsDebugging()
132 return (ctx->flags & SPFLAG_PLUGIN_DEBUG);
135 int BaseContext::SetDebugBreak(SPVM_DEBUGBREAK newpfn, SPVM_DEBUGBREAK *oldpfn)
139 return SP_ERROR_NOTDEBUGGING;
142 *oldpfn = ctx->dbreak;
143 ctx->dbreak = newpfn;
145 return SP_ERROR_NONE;
148 IPluginDebugInfo *BaseContext::GetDebugInfo()
153 int BaseContext::Execute(uint32_t code_addr, cell_t *result)
155 if ((ctx->flags & SPFLAG_PLUGIN_PAUSED) == SPFLAG_PLUGIN_PAUSED)
157 return SP_ERROR_NOT_RUNNABLE;
160 IVirtualMachine *vm = (IVirtualMachine *)ctx->vmbase;
162 uint32_t pushcount = ctx->pushcount;
165 PushCell(pushcount++);
168 cell_t save_sp = ctx->sp;
169 cell_t save_hp = ctx->hp;
171 bool wasExec = m_InExec;
173 /* Clear the error state, if any */
174 ctx->n_err = SP_ERROR_NONE;
177 m_MsgCache[0] = '\0';
180 g_SourcePawn.PushTracer(ctx);
182 err = vm->ContextExecute(ctx, code_addr, result);
187 * :TODO: Calling from a plugin in here will erase the cached message...
188 * Should that be documented?
190 g_SourcePawn.PopTracer(err, m_CustomMsg ? m_MsgCache : NULL);
193 if (err == SP_ERROR_NONE)
195 assert(ctx->sp - pushcount * sizeof(cell_t) == save_sp);
196 assert(ctx->hp == save_hp);
199 if (err != SP_ERROR_NONE)
208 void BaseContext::SetErrorMessage(const char *msg, va_list ap)
212 vsnprintf(m_MsgCache, sizeof(m_MsgCache), msg, ap);
215 void BaseContext::ThrowNativeErrorEx(int error, const char *msg, ...)
228 SetErrorMessage(msg, ap);
233 cell_t BaseContext::ThrowNativeError(const char *msg, ...)
240 ctx->n_err = SP_ERROR_NATIVE;
246 SetErrorMessage(msg, ap);
253 int BaseContext::HeapAlloc(unsigned int cells, cell_t *local_addr, cell_t **phys_addr)
259 if (cells > CELLBOUNDMAX)
261 return SP_ERROR_ARAM;
264 assert(cells < CELLBOUNDMAX);
267 realmem = cells * sizeof(cell_t);
270 * Check if the space between the heap and stack is sufficient.
272 if ((cell_t)(ctx->sp - ctx->hp - realmem) < STACKMARGIN)
274 return SP_ERROR_HEAPLOW;
277 addr = (cell_t *)(ctx->memory + ctx->hp);
278 /* store size of allocation in cells */
279 *addr = (cell_t)cells;
281 ctx->hp += sizeof(cell_t);
283 *local_addr = ctx->hp;
292 return SP_ERROR_NONE;
295 int BaseContext::HeapPop(cell_t local_addr)
300 /* check the bounds of this address */
301 local_addr -= sizeof(cell_t);
302 if (local_addr < ctx->heap_base || local_addr >= ctx->sp)
304 return SP_ERROR_INVALID_ADDRESS;
307 addr = (cell_t *)(ctx->memory + local_addr);
308 cellcount = (*addr) * sizeof(cell_t);
309 /* check if this memory count looks valid */
310 if ((signed)(ctx->hp - cellcount - sizeof(cell_t)) != local_addr)
312 return SP_ERROR_INVALID_ADDRESS;
315 ctx->hp = local_addr;
317 return SP_ERROR_NONE;
321 int BaseContext::HeapRelease(cell_t local_addr)
323 if (local_addr < ctx->heap_base)
325 return SP_ERROR_INVALID_ADDRESS;
328 ctx->hp = local_addr - sizeof(cell_t);
330 return SP_ERROR_NONE;
333 int BaseContext::FindNativeByName(const char *name, uint32_t *index)
337 high = ctx->plugin->info.natives_num - 1;
339 for (uint32_t i=0; i<ctx->plugin->info.natives_num; i++)
341 if (strcmp(ctx->natives[i].name, name) == 0)
347 return SP_ERROR_NONE;
351 return SP_ERROR_NOT_FOUND;
354 int BaseContext::GetNativeByIndex(uint32_t index, sp_native_t **native)
356 if (index >= ctx->plugin->info.natives_num)
358 return SP_ERROR_INDEX;
363 *native = &(ctx->natives[index]);
366 return SP_ERROR_NONE;
370 uint32_t BaseContext::GetNativesNum()
372 return ctx->plugin->info.natives_num;
375 int BaseContext::FindPublicByName(const char *name, uint32_t *index)
380 high = ctx->plugin->info.publics_num - 1;
385 mid = (low + high) / 2;
386 diff = strcmp(ctx->publics[mid].name, name);
393 return SP_ERROR_NONE;
394 } else if (diff < 0) {
401 return SP_ERROR_NOT_FOUND;
404 int BaseContext::GetPublicByIndex(uint32_t index, sp_public_t **pblic)
406 if (index >= ctx->plugin->info.publics_num)
408 return SP_ERROR_INDEX;
413 *pblic = &(ctx->publics[index]);
416 return SP_ERROR_NONE;
419 uint32_t BaseContext::GetPublicsNum()
421 return ctx->plugin->info.publics_num;
424 int BaseContext::GetPubvarByIndex(uint32_t index, sp_pubvar_t **pubvar)
426 if (index >= ctx->plugin->info.pubvars_num)
428 return SP_ERROR_INDEX;
433 *pubvar = &(ctx->pubvars[index]);
436 return SP_ERROR_NONE;
439 int BaseContext::FindPubvarByName(const char *name, uint32_t *index)
444 high = ctx->plugin->info.pubvars_num - 1;
449 mid = (low + high) / 2;
450 diff = strcmp(ctx->pubvars[mid].name, name);
457 return SP_ERROR_NONE;
458 } else if (diff < 0) {
465 return SP_ERROR_NOT_FOUND;
468 int BaseContext::GetPubvarAddrs(uint32_t index, cell_t *local_addr, cell_t **phys_addr)
470 if (index >= ctx->plugin->info.pubvars_num)
472 return SP_ERROR_INDEX;
475 *local_addr = ctx->plugin->info.pubvars[index].address;
476 *phys_addr = ctx->pubvars[index].offs;
478 return SP_ERROR_NONE;
481 uint32_t BaseContext::GetPubVarsNum()
483 return ctx->plugin->info.pubvars_num;
486 int BaseContext::BindNatives(const sp_nativeinfo_t *natives, unsigned int num, int overwrite)
490 max = ctx->plugin->info.natives_num;
492 for (i=0; i<max; i++)
494 if ((ctx->natives[i].status == SP_NATIVE_BOUND) && !overwrite)
499 for (j=0; (natives[j].name) && (!num || j<num); j++)
501 if (!strcmp(ctx->natives[i].name, natives[j].name))
503 ctx->natives[i].pfn = natives[j].func;
504 ctx->natives[i].status = SP_NATIVE_BOUND;
509 return SP_ERROR_NONE;
512 int BaseContext::BindNative(const sp_nativeinfo_t *native)
517 if ((err = FindNativeByName(native->name, &index)) != SP_ERROR_NONE)
522 ctx->natives[index].pfn = native->func;
523 ctx->natives[index].status = SP_NATIVE_BOUND;
525 return SP_ERROR_NONE;
528 int BaseContext::BindNativeToAny(SPVM_NATIVE_FUNC native)
530 uint32_t nativesnum, i;
532 nativesnum = ctx->plugin->info.natives_num;
534 for (i=0; i<nativesnum; i++)
536 if (ctx->natives[i].status == SP_NATIVE_UNBOUND)
538 ctx->natives[i].pfn = native;
539 ctx->natives[i].status = SP_NATIVE_BOUND;
543 return SP_ERROR_NONE;
546 int BaseContext::LocalToPhysAddr(cell_t local_addr, cell_t **phys_addr)
548 if (((local_addr >= ctx->hp) && (local_addr < ctx->sp)) || (local_addr < 0) || ((ucell_t)local_addr >= ctx->mem_size))
550 return SP_ERROR_INVALID_ADDRESS;
555 *phys_addr = (cell_t *)(ctx->memory + local_addr);
558 return SP_ERROR_NONE;
561 int BaseContext::PushCell(cell_t value)
563 if ((ctx->hp + STACKMARGIN) > (cell_t)(ctx->sp - sizeof(cell_t)))
565 return SP_ERROR_STACKLOW;
568 ctx->sp -= sizeof(cell_t);
569 *(cell_t *)(ctx->memory + ctx->sp) = value;
572 return SP_ERROR_NONE;
575 int BaseContext::PushCellsFromArray(cell_t array[], unsigned int numcells)
580 for (i=0; i<numcells; i++)
582 if ((err = PushCell(array[i])) != SP_ERROR_NONE)
584 ctx->sp += (cell_t)(i * sizeof(cell_t));
590 return SP_ERROR_NONE;
593 int BaseContext::PushCellArray(cell_t *local_addr, cell_t **phys_addr, cell_t array[], unsigned int numcells)
598 if ((err = HeapAlloc(numcells, local_addr, &ph_addr)) != SP_ERROR_NONE)
603 memcpy(ph_addr, array, numcells * sizeof(cell_t));
605 if ((err = PushCell(*local_addr)) != SP_ERROR_NONE)
607 HeapRelease(*local_addr);
613 *phys_addr = ph_addr;
616 return SP_ERROR_NONE;
619 int BaseContext::LocalToString(cell_t local_addr, char **addr)
621 if (((local_addr >= ctx->hp) && (local_addr < ctx->sp)) || (local_addr < 0) || ((ucell_t)local_addr >= ctx->mem_size))
623 return SP_ERROR_INVALID_ADDRESS;
625 *addr = (char *)(ctx->memory + local_addr);
627 return SP_ERROR_NONE;
630 int BaseContext::PushString(cell_t *local_addr, char **phys_addr, const char *string)
634 unsigned int len, numcells = ((len=strlen(string)) + sizeof(cell_t)) / sizeof(cell_t);
636 if ((err = HeapAlloc(numcells, local_addr, (cell_t **)&ph_addr)) != SP_ERROR_NONE)
641 memcpy(ph_addr, string, len);
644 if ((err = PushCell(*local_addr)) != SP_ERROR_NONE)
646 HeapRelease(*local_addr);
652 *phys_addr = ph_addr;
655 return SP_ERROR_NONE;
658 int BaseContext::StringToLocal(cell_t local_addr, size_t chars, const char *source)
663 if (((local_addr >= ctx->hp) && (local_addr < ctx->sp)) || (local_addr < 0) || ((ucell_t)local_addr >= ctx->mem_size))
665 return SP_ERROR_INVALID_ADDRESS;
668 len = strlen(source);
669 dest = (char *)(ctx->memory + local_addr);
671 if ((size_t)len >= chars)
677 return SP_ERROR_NONE;
680 memcpy(dest, source, len);
683 return SP_ERROR_NONE;
686 inline int __CheckValidChar(char *c)
691 for (count=1; (*c & 0xC0) == 0x80; count++)
716 if (bytecount != count)
724 int BaseContext::StringToLocalUTF8(cell_t local_addr, size_t maxbytes, const char *source, size_t *wrtnbytes)
728 bool needtocheck = false;
730 if (((local_addr >= ctx->hp) && (local_addr < ctx->sp)) || (local_addr < 0) || ((ucell_t)local_addr >= ctx->mem_size))
732 return SP_ERROR_INVALID_ADDRESS;
735 len = strlen(source);
736 dest = (char *)(ctx->memory + local_addr);
738 if ((size_t)len >= maxbytes)
745 return SP_ERROR_NONE;
748 memcpy(dest, source, len);
749 if ((dest[len-1] & 1<<7) && needtocheck)
751 len -= __CheckValidChar(dest+len-1);
760 return SP_ERROR_NONE;
763 #define USHR(x) ((unsigned int)(x)>>1)
765 int BaseContext::LookupFile(ucell_t addr, const char **filename)
769 high = ctx->plugin->debug.files_num;
772 while (high - low > 1)
774 mid = USHR(low + high);
775 if (ctx->files[mid].addr <= addr)
785 return SP_ERROR_NOT_FOUND;
788 *filename = ctx->files[low].name;
790 return SP_ERROR_NONE;
793 int BaseContext::LookupFunction(ucell_t addr, const char **name)
795 uint32_t iter, max = ctx->plugin->debug.syms_num;
797 for (iter=0; iter<max; iter++)
799 if ((ctx->symbols[iter].sym->ident == SP_SYM_FUNCTION)
800 && (ctx->symbols[iter].codestart <= addr)
801 && (ctx->symbols[iter].codeend > addr))
809 return SP_ERROR_NOT_FOUND;
812 *name = ctx->symbols[iter].name;
814 return SP_ERROR_NONE;
817 int BaseContext::LookupLine(ucell_t addr, uint32_t *line)
821 high = ctx->plugin->debug.lines_num;
824 while (high - low > 1)
826 mid = USHR(low + high);
827 if (ctx->lines[mid].addr <= addr)
837 return SP_ERROR_NOT_FOUND;
840 /* Since the CIP occurs BEFORE the line, we have to add one */
841 *line = ctx->lines[low].line + 1;
843 return SP_ERROR_NONE;
846 IPluginFunction *BaseContext::GetFunctionById(funcid_t func_id)
848 CFunction *pFunc = NULL;
853 if (func_id >= ctx->plugin->info.publics_num)
857 pFunc = m_pub_funcs[func_id];
860 m_pub_funcs[func_id] = new CFunction(ctx->publics[func_id].code_offs, this);
861 pFunc = m_pub_funcs[func_id];
862 } else if (pFunc->IsInvalidated()) {
863 pFunc->Set(ctx->publics[func_id].code_offs, this);
866 /* :TODO: currently not used */
870 if (!g_pVM->FunctionLookup(ctx, func_id, &index))
874 pFunc = m_priv_funcs[func_id];
877 m_priv_funcs[func_id] = new CFunction(save, this);
878 pFunc = m_priv_funcs[func_id];
887 IPluginFunction *BaseContext::GetFunctionByName(const char *public_name)
891 if (FindPublicByName(public_name, &index) != SP_ERROR_NONE)
896 CFunction *pFunc = m_pub_funcs[index];
899 sp_public_t *pub = NULL;
900 GetPublicByIndex(index, &pub);
903 m_pub_funcs[index] = new CFunction(pub->code_offs, this);
905 pFunc = m_pub_funcs[index];
906 } else if (pFunc->IsInvalidated()) {
907 sp_public_t *pub = NULL;
908 GetPublicByIndex(index, &pub);
911 pFunc->Set(pub->code_offs, this);
920 #if defined SOURCEMOD_BUILD
921 SourceMod::IdentityToken_t *BaseContext::GetIdentity()
926 void BaseContext::SetIdentity(SourceMod::IdentityToken_t *token)