sourcemod/sourcepawn/jit/plugin-context.cpp

909 lines
19 KiB
C++
Raw Normal View History

2015-02-23 22:40:01 +01:00
// vim: set sts=2 ts=8 sw=2 tw=99 et:
//
// Copyright (C) 2006-2015 AlliedModders LLC
//
// This file is part of SourcePawn. SourcePawn is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// You should have received a copy of the GNU General Public License along with
// SourcePawn. If not, see http://www.gnu.org/licenses/.
//
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <limits.h>
#include <sp_vm_api.h>
#include "plugin-context.h"
#include "watchdog_timer.h"
#include "x86/jit_x86.h"
#include "interpreter.h"
#include "environment.h"
using namespace SourcePawn;
2015-02-23 22:40:01 +01:00
#define CELLBOUNDMAX (INT_MAX/sizeof(cell_t))
#define STACKMARGIN ((cell_t)(16*sizeof(cell_t)))
2015-02-24 21:50:09 +01:00
PluginContext::PluginContext(PluginRuntime *pRuntime)
{
2015-02-23 22:40:01 +01:00
m_pRuntime = pRuntime;
m_InExec = false;
m_CustomMsg = false;
/* Initialize the null references */
uint32_t index;
if (FindPubvarByName("NULL_VECTOR", &index) == SP_ERROR_NONE) {
sp_pubvar_t *pubvar;
GetPubvarByIndex(index, &pubvar);
m_pNullVec = pubvar->offs;
} else {
m_pNullVec = NULL;
}
if (FindPubvarByName("NULL_STRING", &index) == SP_ERROR_NONE) {
sp_pubvar_t *pubvar;
GetPubvarByIndex(index, &pubvar);
m_pNullString = pubvar->offs;
} else {
m_pNullString = NULL;
}
m_ctx.hp = m_pRuntime->plugin()->data_size;
sp_ = m_pRuntime->plugin()->mem_size - sizeof(cell_t);
frm_ = sp_;
rp_ = 0;
last_native_ = -1;
native_error_ = SP_ERROR_NONE;
2015-02-23 22:40:01 +01:00
tracker_.pBase = (ucell_t *)malloc(1024);
tracker_.pCur = tracker_.pBase;
tracker_.size = 1024 / sizeof(cell_t);
m_ctx.basecx = this;
m_ctx.plugin = const_cast<sp_plugin_t *>(pRuntime->plugin());
}
2015-02-24 21:50:09 +01:00
PluginContext::~PluginContext()
{
free(tracker_.pBase);
}
2015-02-23 22:40:01 +01:00
IVirtualMachine *
2015-02-24 21:50:09 +01:00
PluginContext::GetVirtualMachine()
{
2015-02-23 22:40:01 +01:00
return NULL;
}
2015-02-23 22:40:01 +01:00
sp_context_t *
2015-02-24 21:50:09 +01:00
PluginContext::GetContext()
{
2015-02-23 22:40:01 +01:00
return reinterpret_cast<sp_context_t *>((IPluginContext * )this);
}
2015-02-23 22:40:01 +01:00
sp_context_t *
2015-02-24 21:50:09 +01:00
PluginContext::GetCtx()
{
2015-02-23 22:40:01 +01:00
return &m_ctx;
}
2015-02-23 22:40:01 +01:00
bool
2015-02-24 21:50:09 +01:00
PluginContext::IsDebugging()
{
2015-02-23 22:40:01 +01:00
return true;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::SetDebugBreak(void *newpfn, void *oldpfn)
{
2015-02-23 22:40:01 +01:00
return SP_ERROR_ABORTED;
}
2015-02-23 22:40:01 +01:00
IPluginDebugInfo *
2015-02-24 21:50:09 +01:00
PluginContext::GetDebugInfo()
{
2015-02-23 22:40:01 +01:00
return NULL;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::Execute(uint32_t code_addr, cell_t *result)
{
2015-02-23 22:40:01 +01:00
return SP_ERROR_ABORTED;
}
2015-02-23 22:40:01 +01:00
void
2015-02-24 21:50:09 +01:00
PluginContext::SetErrorMessage(const char *msg, va_list ap)
{
2015-02-23 22:40:01 +01:00
m_CustomMsg = true;
2015-02-23 22:40:01 +01:00
vsnprintf(m_MsgCache, sizeof(m_MsgCache), msg, ap);
}
2015-02-23 22:40:01 +01:00
void
2015-02-24 21:50:09 +01:00
PluginContext::_SetErrorMessage(const char *msg, ...)
{
2015-02-23 22:40:01 +01:00
va_list ap;
va_start(ap, msg);
SetErrorMessage(msg, ap);
va_end(ap);
}
2015-02-23 22:40:01 +01:00
cell_t
2015-02-24 21:50:09 +01:00
PluginContext::ThrowNativeErrorEx(int error, const char *msg, ...)
{
2015-02-23 22:40:01 +01:00
if (!m_InExec)
return 0;
native_error_ = error;
2015-02-23 22:40:01 +01:00
if (msg) {
va_list ap;
va_start(ap, msg);
SetErrorMessage(msg, ap);
va_end(ap);
}
2015-02-23 22:40:01 +01:00
return 0;
}
2015-02-23 22:40:01 +01:00
cell_t
2015-02-24 21:50:09 +01:00
PluginContext::ThrowNativeError(const char *msg, ...)
{
2015-02-23 22:40:01 +01:00
if (!m_InExec)
return 0;
native_error_ = SP_ERROR_NATIVE;
2015-02-23 22:40:01 +01:00
if (msg) {
va_list ap;
va_start(ap, msg);
SetErrorMessage(msg, ap);
va_end(ap);
}
2015-02-23 22:40:01 +01:00
return 0;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::HeapAlloc(unsigned int cells, cell_t *local_addr, cell_t **phys_addr)
{
2015-02-23 22:40:01 +01:00
cell_t *addr;
ucell_t realmem;
#if 0
2015-02-23 22:40:01 +01:00
if (cells > CELLBOUNDMAX)
{
return SP_ERROR_ARAM;
}
#else
2015-02-23 22:40:01 +01:00
assert(cells < CELLBOUNDMAX);
#endif
2015-02-23 22:40:01 +01:00
realmem = cells * sizeof(cell_t);
2015-02-23 22:40:01 +01:00
/**
* Check if the space between the heap and stack is sufficient.
*/
if ((cell_t)(sp_ - m_ctx.hp - realmem) < STACKMARGIN)
2015-02-23 22:40:01 +01:00
return SP_ERROR_HEAPLOW;
2015-02-23 22:40:01 +01:00
addr = (cell_t *)(m_pRuntime->plugin()->memory + m_ctx.hp);
/* store size of allocation in cells */
*addr = (cell_t)cells;
addr++;
m_ctx.hp += sizeof(cell_t);
2015-02-23 22:40:01 +01:00
*local_addr = m_ctx.hp;
2015-02-23 22:40:01 +01:00
if (phys_addr)
*phys_addr = addr;
2015-02-23 22:40:01 +01:00
m_ctx.hp += realmem;
2015-02-23 22:40:01 +01:00
return SP_ERROR_NONE;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::HeapPop(cell_t local_addr)
{
2015-02-23 22:40:01 +01:00
cell_t cellcount;
cell_t *addr;
2015-02-23 22:40:01 +01:00
/* check the bounds of this address */
local_addr -= sizeof(cell_t);
if (local_addr < (cell_t)m_pRuntime->plugin()->data_size || local_addr >= sp_)
2015-02-23 22:40:01 +01:00
return SP_ERROR_INVALID_ADDRESS;
2015-02-23 22:40:01 +01:00
addr = (cell_t *)(m_pRuntime->plugin()->memory + local_addr);
cellcount = (*addr) * sizeof(cell_t);
/* check if this memory count looks valid */
if ((signed)(m_ctx.hp - cellcount - sizeof(cell_t)) != local_addr)
return SP_ERROR_INVALID_ADDRESS;
2015-02-23 22:40:01 +01:00
m_ctx.hp = local_addr;
2015-02-23 22:40:01 +01:00
return SP_ERROR_NONE;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::HeapRelease(cell_t local_addr)
{
2015-02-23 22:40:01 +01:00
if (local_addr < (cell_t)m_pRuntime->plugin()->data_size)
return SP_ERROR_INVALID_ADDRESS;
2015-02-23 22:40:01 +01:00
m_ctx.hp = local_addr - sizeof(cell_t);
2015-02-23 22:40:01 +01:00
return SP_ERROR_NONE;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::FindNativeByName(const char *name, uint32_t *index)
{
2015-02-23 22:40:01 +01:00
return m_pRuntime->FindNativeByName(name, index);
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::GetNativeByIndex(uint32_t index, sp_native_t **native)
{
2015-02-23 22:40:01 +01:00
return m_pRuntime->GetNativeByIndex(index, native);
}
2015-02-23 22:40:01 +01:00
uint32_t
2015-02-24 21:50:09 +01:00
PluginContext::GetNativesNum()
{
2015-02-23 22:40:01 +01:00
return m_pRuntime->GetNativesNum();
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::FindPublicByName(const char *name, uint32_t *index)
{
2015-02-23 22:40:01 +01:00
return m_pRuntime->FindPublicByName(name, index);
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::GetPublicByIndex(uint32_t index, sp_public_t **pblic)
{
2015-02-23 22:40:01 +01:00
return m_pRuntime->GetPublicByIndex(index, pblic);
}
2015-02-23 22:40:01 +01:00
uint32_t
2015-02-24 21:50:09 +01:00
PluginContext::GetPublicsNum()
{
2015-02-23 22:40:01 +01:00
return m_pRuntime->GetPublicsNum();
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::GetPubvarByIndex(uint32_t index, sp_pubvar_t **pubvar)
{
2015-02-23 22:40:01 +01:00
return m_pRuntime->GetPubvarByIndex(index, pubvar);
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::FindPubvarByName(const char *name, uint32_t *index)
{
2015-02-23 22:40:01 +01:00
return m_pRuntime->FindPubvarByName(name, index);
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::GetPubvarAddrs(uint32_t index, cell_t *local_addr, cell_t **phys_addr)
{
2015-02-23 22:40:01 +01:00
return m_pRuntime->GetPubvarAddrs(index, local_addr, phys_addr);
}
2015-02-23 22:40:01 +01:00
uint32_t
2015-02-24 21:50:09 +01:00
PluginContext::GetPubVarsNum()
{
2015-02-23 22:40:01 +01:00
return m_pRuntime->GetPubVarsNum();
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::BindNatives(const sp_nativeinfo_t *natives, unsigned int num, int overwrite)
{
2015-02-23 22:40:01 +01:00
return SP_ERROR_ABORTED;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::BindNative(const sp_nativeinfo_t *native)
{
2015-02-23 22:40:01 +01:00
return SP_ERROR_ABORTED;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::BindNativeToIndex(uint32_t index, SPVM_NATIVE_FUNC func)
{
2015-02-23 22:40:01 +01:00
return SP_ERROR_ABORTED;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::BindNativeToAny(SPVM_NATIVE_FUNC native)
{
2015-02-23 22:40:01 +01:00
return SP_ERROR_ABORTED;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::LocalToPhysAddr(cell_t local_addr, cell_t **phys_addr)
{
if (((local_addr >= m_ctx.hp) && (local_addr < sp_)) ||
2015-02-23 22:40:01 +01:00
(local_addr < 0) || ((ucell_t)local_addr >= m_pRuntime->plugin()->mem_size))
{
return SP_ERROR_INVALID_ADDRESS;
}
2015-02-23 22:40:01 +01:00
if (phys_addr)
*phys_addr = (cell_t *)(m_pRuntime->plugin()->memory + local_addr);
2015-02-23 22:40:01 +01:00
return SP_ERROR_NONE;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::PushCell(cell_t value)
{
2015-02-23 22:40:01 +01:00
return SP_ERROR_ABORTED;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::PushCellsFromArray(cell_t array[], unsigned int numcells)
{
2015-02-23 22:40:01 +01:00
return SP_ERROR_ABORTED;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::PushCellArray(cell_t *local_addr, cell_t **phys_addr, cell_t array[], unsigned int numcells)
{
2015-02-23 22:40:01 +01:00
return SP_ERROR_ABORTED;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::LocalToString(cell_t local_addr, char **addr)
{
if (((local_addr >= m_ctx.hp) && (local_addr < sp_)) ||
2015-02-23 22:40:01 +01:00
(local_addr < 0) || ((ucell_t)local_addr >= m_pRuntime->plugin()->mem_size))
{
return SP_ERROR_INVALID_ADDRESS;
}
*addr = (char *)(m_pRuntime->plugin()->memory + local_addr);
2015-02-23 22:40:01 +01:00
return SP_ERROR_NONE;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::PushString(cell_t *local_addr, char **phys_addr, const char *string)
{
2015-02-23 22:40:01 +01:00
return SP_ERROR_ABORTED;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::StringToLocal(cell_t local_addr, size_t bytes, const char *source)
{
2015-02-23 22:40:01 +01:00
char *dest;
size_t len;
if (((local_addr >= m_ctx.hp) && (local_addr < sp_)) ||
2015-02-23 22:40:01 +01:00
(local_addr < 0) || ((ucell_t)local_addr >= m_pRuntime->plugin()->mem_size))
{
return SP_ERROR_INVALID_ADDRESS;
}
2015-02-23 22:40:01 +01:00
if (bytes == 0)
return SP_ERROR_NONE;
2015-02-23 22:40:01 +01:00
len = strlen(source);
dest = (char *)(m_pRuntime->plugin()->memory + local_addr);
2015-02-23 22:40:01 +01:00
if (len >= bytes)
len = bytes - 1;
2015-02-23 22:40:01 +01:00
memmove(dest, source, len);
dest[len] = '\0';
2015-02-23 22:40:01 +01:00
return SP_ERROR_NONE;
}
2015-02-23 22:40:01 +01:00
static inline int
__CheckValidChar(char *c)
{
2015-02-23 22:40:01 +01:00
int count;
int bytecount = 0;
2015-02-23 22:40:01 +01:00
for (count=1; (*c & 0xC0) == 0x80; count++)
c--;
2015-02-23 22:40:01 +01:00
switch (*c & 0xF0)
{
case 0xC0:
case 0xD0:
{
bytecount = 2;
break;
}
case 0xE0:
{
bytecount = 3;
break;
}
case 0xF0:
{
bytecount = 4;
break;
}
}
2015-02-23 22:40:01 +01:00
if (bytecount != count)
return count;
2015-02-23 22:40:01 +01:00
return 0;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::StringToLocalUTF8(cell_t local_addr, size_t maxbytes, const char *source, size_t *wrtnbytes)
{
2015-02-23 22:40:01 +01:00
char *dest;
size_t len;
bool needtocheck = false;
if (((local_addr >= m_ctx.hp) && (local_addr < sp_)) ||
2015-02-23 22:40:01 +01:00
(local_addr < 0) ||
((ucell_t)local_addr >= m_pRuntime->plugin()->mem_size))
{
return SP_ERROR_INVALID_ADDRESS;
}
if (maxbytes == 0)
return SP_ERROR_NONE;
2015-02-23 22:40:01 +01:00
len = strlen(source);
dest = (char *)(m_pRuntime->plugin()->memory + local_addr);
2015-02-23 22:40:01 +01:00
if ((size_t)len >= maxbytes) {
len = maxbytes - 1;
needtocheck = true;
}
2015-02-23 22:40:01 +01:00
memmove(dest, source, len);
if ((dest[len-1] & 1<<7) && needtocheck)
len -= __CheckValidChar(dest+len-1);
dest[len] = '\0';
2015-02-23 22:40:01 +01:00
if (wrtnbytes)
*wrtnbytes = len;
2015-02-23 22:40:01 +01:00
return SP_ERROR_NONE;
}
2015-02-23 22:40:01 +01:00
IPluginFunction *
2015-02-24 21:50:09 +01:00
PluginContext::GetFunctionById(funcid_t func_id)
{
2015-02-23 22:40:01 +01:00
return m_pRuntime->GetFunctionById(func_id);
}
2015-02-23 22:40:01 +01:00
IPluginFunction *
2015-02-24 21:50:09 +01:00
PluginContext::GetFunctionByName(const char *public_name)
{
2015-02-23 22:40:01 +01:00
return m_pRuntime->GetFunctionByName(public_name);
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::LocalToStringNULL(cell_t local_addr, char **addr)
{
2015-02-23 22:40:01 +01:00
int err;
if ((err = LocalToString(local_addr, addr)) != SP_ERROR_NONE)
return err;
2015-02-23 22:40:01 +01:00
if ((cell_t *)*addr == m_pNullString)
*addr = NULL;
2015-02-23 22:40:01 +01:00
return SP_ERROR_NONE;
}
2015-02-23 22:40:01 +01:00
SourceMod::IdentityToken_t *
2015-02-24 21:50:09 +01:00
PluginContext::GetIdentity()
{
2015-02-23 22:40:01 +01:00
SourceMod::IdentityToken_t *tok;
2015-02-23 22:40:01 +01:00
if (GetKey(1, (void **)&tok))
return tok;
return NULL;
}
2015-02-23 22:40:01 +01:00
cell_t *
2015-02-24 21:50:09 +01:00
PluginContext::GetNullRef(SP_NULL_TYPE type)
{
2015-02-23 22:40:01 +01:00
if (type == SP_NULL_VECTOR)
return m_pNullVec;
2015-02-23 22:40:01 +01:00
return NULL;
}
2015-02-23 22:40:01 +01:00
bool
2015-02-24 21:50:09 +01:00
PluginContext::IsInExec()
{
2015-02-23 22:40:01 +01:00
return m_InExec;
}
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::Execute2(IPluginFunction *function, const cell_t *params, unsigned int num_params, cell_t *result)
{
2015-02-23 22:40:01 +01:00
int ir;
int serial;
cell_t *sp;
2015-02-24 01:27:57 +01:00
CompiledFunction *fn;
2015-02-23 22:40:01 +01:00
cell_t _ignore_result;
EnterProfileScope profileScope("SourcePawn", "EnterJIT");
2015-02-24 07:47:08 +01:00
if (!Environment::get()->watchdog()->HandleInterrupt())
2015-02-23 22:40:01 +01:00
return SP_ERROR_TIMEOUT;
2015-02-23 22:40:01 +01:00
funcid_t fnid = function->GetFunctionID();
if (!(fnid & 1))
return SP_ERROR_INVALID_ADDRESS;
2015-02-23 22:40:01 +01:00
unsigned public_id = fnid >> 1;
ScriptedInvoker *cfun = m_pRuntime->GetPublicFunction(public_id);
2015-02-23 22:40:01 +01:00
if (!cfun)
return SP_ERROR_NOT_FOUND;
2015-02-23 22:40:01 +01:00
if (m_pRuntime->IsPaused())
return SP_ERROR_NOT_RUNNABLE;
if ((cell_t)(m_ctx.hp + 16*sizeof(cell_t)) > (cell_t)(sp_ - (sizeof(cell_t) * (num_params + 1))))
2015-02-23 22:40:01 +01:00
return SP_ERROR_STACKLOW;
2015-02-23 22:40:01 +01:00
if (result == NULL)
result = &_ignore_result;
2015-02-23 22:40:01 +01:00
/* We got this far. It's time to start profiling. */
EnterProfileScope scriptScope("SourcePawn", cfun->FullName());
2015-02-23 22:40:01 +01:00
/* See if we have to compile the callee. */
if (Environment::get()->IsJitEnabled() &&
2015-02-23 22:40:01 +01:00
(fn = m_pRuntime->m_PubJitFuncs[public_id]) == NULL)
{
/* We might not have to - check pcode offset. */
fn = m_pRuntime->GetJittedFunctionByOffset(cfun->Public()->code_offs);
if (fn) {
m_pRuntime->m_PubJitFuncs[public_id] = fn;
} else {
2015-02-24 10:19:16 +01:00
if ((fn = CompileFunction(m_pRuntime, cfun->Public()->code_offs, &ir)) == NULL)
2015-02-23 22:40:01 +01:00
return ir;
m_pRuntime->m_PubJitFuncs[public_id] = fn;
}
}
2015-02-23 22:40:01 +01:00
/* Save our previous state. */
2015-02-23 22:40:01 +01:00
bool save_exec;
uint32_t save_n_idx;
cell_t save_sp, save_hp, save_rp, save_cip;
save_sp = sp_;
2015-02-23 22:40:01 +01:00
save_hp = m_ctx.hp;
save_exec = m_InExec;
save_n_idx = last_native_;
save_rp = rp_;
save_cip = cip_;
2015-02-23 22:40:01 +01:00
/* Push parameters */
sp_ -= sizeof(cell_t) * (num_params + 1);
sp = (cell_t *)(m_pRuntime->plugin()->memory + sp_);
2015-02-23 22:40:01 +01:00
sp[0] = num_params;
for (unsigned int i = 0; i < num_params; i++)
sp[i + 1] = params[i];
2015-02-23 22:40:01 +01:00
/* Clear internal state */
native_error_ = SP_ERROR_NONE;
last_native_ = -1;
2015-02-23 22:40:01 +01:00
m_MsgCache[0] = '\0';
m_CustomMsg = false;
m_InExec = true;
2015-02-24 10:12:23 +01:00
// Enter the execution engine.
Environment *env = Environment::get();
if (env->IsJitEnabled())
ir = env->Invoke(m_pRuntime, fn, result);
2015-02-23 22:40:01 +01:00
else
ir = Interpret(m_pRuntime, cfun->Public()->code_offs, result);
/* Restore some states, stop the frame tracer */
m_InExec = save_exec;
if (ir == SP_ERROR_NONE) {
native_error_ = SP_ERROR_NONE;
if (sp_ != save_sp) {
2015-02-23 22:40:01 +01:00
ir = SP_ERROR_STACKLEAK;
_SetErrorMessage("Stack leak detected: sp:%d should be %d!",
sp_,
2015-02-23 22:40:01 +01:00
save_sp);
}
if (m_ctx.hp != save_hp) {
ir = SP_ERROR_HEAPLEAK;
_SetErrorMessage("Heap leak detected: hp:%d should be %d!",
m_ctx.hp,
save_hp);
}
if (rp_ != save_rp) {
2015-02-23 22:40:01 +01:00
ir = SP_ERROR_STACKLEAK;
_SetErrorMessage("Return stack leak detected: rp:%d should be %d!",
rp_,
2015-02-23 22:40:01 +01:00
save_rp);
}
}
if (ir == SP_ERROR_TIMEOUT)
2015-02-24 07:47:08 +01:00
Environment::get()->watchdog()->NotifyTimeoutReceived();
2015-02-23 22:40:01 +01:00
if (ir != SP_ERROR_NONE)
Environment::get()->ReportError(m_pRuntime, ir, m_MsgCache, save_rp);
2015-02-23 22:40:01 +01:00
sp_ = save_sp;
2015-02-23 22:40:01 +01:00
m_ctx.hp = save_hp;
rp_ = save_rp;
2015-02-23 22:40:01 +01:00
cip_ = save_cip;
last_native_ = save_n_idx;
native_error_ = SP_ERROR_NONE;
2015-02-23 22:40:01 +01:00
m_MsgCache[0] = '\0';
m_CustomMsg = false;
return ir;
}
IPluginRuntime *
2015-02-24 21:50:09 +01:00
PluginContext::GetRuntime()
2015-02-23 22:40:01 +01:00
{
return m_pRuntime;
}
DebugInfo::DebugInfo(sp_plugin_t *plugin) : m_pPlugin(plugin)
{
}
#define USHR(x) ((unsigned int)(x)>>1)
2015-02-23 22:40:01 +01:00
int
DebugInfo::LookupFile(ucell_t addr, const char **filename)
{
int high, low, mid;
high = m_pPlugin->debug.files_num;
low = -1;
while (high - low > 1) {
mid = USHR(low + high);
if (m_pPlugin->debug.files[mid].addr <= addr)
low = mid;
else
high = mid;
}
if (low == -1)
return SP_ERROR_NOT_FOUND;
*filename = m_pPlugin->debug.stringbase + m_pPlugin->debug.files[low].name;
return SP_ERROR_NONE;
}
int
DebugInfo::LookupFunction(ucell_t addr, const char **name)
{
2015-02-23 22:40:01 +01:00
if (!m_pPlugin->debug.unpacked) {
uint32_t max, iter;
sp_fdbg_symbol_t *sym;
uint8_t *cursor = (uint8_t *)(m_pPlugin->debug.symbols);
max = m_pPlugin->debug.syms_num;
for (iter = 0; iter < max; iter++) {
sym = (sp_fdbg_symbol_t *)cursor;
if (sym->ident == sp::IDENT_FUNCTION &&
sym->codestart <= addr &&
sym->codeend > addr)
{
*name = m_pPlugin->debug.stringbase + sym->name;
return SP_ERROR_NONE;
}
if (sym->dimcount > 0) {
cursor += sizeof(sp_fdbg_symbol_t);
cursor += sizeof(sp_fdbg_arraydim_t) * sym->dimcount;
continue;
}
cursor += sizeof(sp_fdbg_symbol_t);
}
return SP_ERROR_NOT_FOUND;
} else {
uint32_t max, iter;
sp_u_fdbg_symbol_t *sym;
uint8_t *cursor = (uint8_t *)(m_pPlugin->debug.symbols);
max = m_pPlugin->debug.syms_num;
for (iter = 0; iter < max; iter++) {
sym = (sp_u_fdbg_symbol_t *)cursor;
if (sym->ident == sp::IDENT_FUNCTION &&
sym->codestart <= addr &&
sym->codeend > addr)
{
*name = m_pPlugin->debug.stringbase + sym->name;
return SP_ERROR_NONE;
}
if (sym->dimcount > 0) {
cursor += sizeof(sp_u_fdbg_symbol_t);
cursor += sizeof(sp_u_fdbg_arraydim_t) * sym->dimcount;
continue;
}
cursor += sizeof(sp_u_fdbg_symbol_t);
}
2015-02-23 22:40:01 +01:00
return SP_ERROR_NOT_FOUND;
}
}
int
DebugInfo::LookupLine(ucell_t addr, uint32_t *line)
{
int high, low, mid;
high = m_pPlugin->debug.lines_num;
low = -1;
while (high - low > 1) {
mid = USHR(low + high);
if (m_pPlugin->debug.lines[mid].addr <= addr)
low = mid;
else
high = mid;
}
if (low == -1)
return SP_ERROR_NOT_FOUND;
/* Since the CIP occurs BEFORE the line, we have to add one */
*line = m_pPlugin->debug.lines[low].line + 1;
return SP_ERROR_NONE;
}
#undef USHR
2015-02-23 22:40:01 +01:00
int
2015-02-24 21:50:09 +01:00
PluginContext::GetLastNativeError()
{
return native_error_;
}
2015-02-23 22:40:01 +01:00
cell_t *
2015-02-24 21:50:09 +01:00
PluginContext::GetLocalParams()
{
return (cell_t *)(m_pRuntime->plugin()->memory + frm_ + (2 * sizeof(cell_t)));
}
2015-02-23 22:40:01 +01:00
void
2015-02-24 21:50:09 +01:00
PluginContext::SetKey(int k, void *value)
{
2015-02-23 22:40:01 +01:00
if (k < 1 || k > 4)
return;
2015-02-23 22:40:01 +01:00
m_keys[k - 1] = value;
m_keys_set[k - 1] = true;
}
2015-02-23 22:40:01 +01:00
bool
2015-02-24 21:50:09 +01:00
PluginContext::GetKey(int k, void **value)
{
2015-02-23 22:40:01 +01:00
if (k < 1 || k > 4 || m_keys_set[k - 1] == false)
return false;
2015-02-23 22:40:01 +01:00
*value = m_keys[k - 1];
return true;
}
2015-02-23 22:40:01 +01:00
void
2015-02-24 21:50:09 +01:00
PluginContext::ClearLastNativeError()
{
native_error_ = SP_ERROR_NONE;
}
int
PluginContext::popTrackerAndSetHeap()
{
assert(tracker_.pCur > tracker_.pBase);
tracker_.pCur--;
if (tracker_.pCur < tracker_.pBase)
return SP_ERROR_TRACKER_BOUNDS;
ucell_t amt = *tracker_.pCur;
if (amt > (m_ctx.hp - m_pRuntime->plugin()->data_size))
return SP_ERROR_HEAPMIN;
m_ctx.hp -= amt;
return SP_ERROR_NONE;
}
int
PluginContext::pushTracker(uint32_t amount)
{
if ((size_t)(tracker_.pCur - tracker_.pBase) >= tracker_.size)
return SP_ERROR_TRACKER_BOUNDS;
if (tracker_.pCur + 1 - (tracker_.pBase + tracker_.size) == 0) {
size_t disp = tracker_.size - 1;
tracker_.size *= 2;
tracker_.pBase = (ucell_t *)realloc(tracker_.pBase, tracker_.size * sizeof(cell_t));
if (!tracker_.pBase)
return SP_ERROR_TRACKER_BOUNDS;
tracker_.pCur = tracker_.pBase + disp;
}
*tracker_.pCur++ = amount;
return SP_ERROR_NONE;
}
cell_t
PluginContext::invokeNative(ucell_t native_idx, cell_t *params)
{
cell_t save_sp = sp_;
cell_t save_hp = m_ctx.hp;
// Note: Invoke() saves the last native, so we don't need to here.
last_native_ = native_idx;
sp_native_t *native = &m_pRuntime->plugin()->natives[native_idx];
if (native->status == SP_NATIVE_UNBOUND) {
native_error_ = SP_ERROR_INVALID_NATIVE;
return 0;
}
cell_t result = native->pfn(m_ctx.basecx, params);
if (native_error_ != SP_ERROR_NONE)
return result;
if (save_sp != sp_) {
native_error_ = SP_ERROR_STACKLEAK;
return result;
}
if (save_hp != m_ctx.hp) {
native_error_ = SP_ERROR_HEAPLEAK;
return result;
}
return result;
}
cell_t
PluginContext::invokeBoundNative(SPVM_NATIVE_FUNC pfn, cell_t *params)
{
cell_t save_sp = sp_;
cell_t save_hp = m_ctx.hp;
cell_t result = pfn(this, params);
if (native_error_ != SP_ERROR_NONE)
return result;
if (save_sp != sp_) {
native_error_ = SP_ERROR_STACKLEAK;
return result;
}
if (save_hp != m_ctx.hp) {
native_error_ = SP_ERROR_HEAPLEAK;
return result;
}
return result;
}