finalized new structure and imported newly proposed plugin system API
added zlib to source tree added VM API to source tree --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40164
This commit is contained in:
@@ -0,0 +1,617 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include "sp_vm_api.h"
|
||||
#include "sp_vm_basecontext.h"
|
||||
|
||||
using namespace SourcePawn;
|
||||
|
||||
#define CELLBOUNDMAX (INT_MAX/sizeof(cell_t))
|
||||
#define STACKMARGIN ((cell_t)(16*sizeof(cell_t)))
|
||||
|
||||
BaseContext::BaseContext(sp_context_t *_ctx)
|
||||
{
|
||||
ctx = _ctx;
|
||||
ctx->context = this;
|
||||
}
|
||||
|
||||
IVirtualMachine *BaseContext::GetVirtualMachine()
|
||||
{
|
||||
return (IVirtualMachine *)ctx->vmbase;
|
||||
}
|
||||
|
||||
sp_context_t *BaseContext::GetContext()
|
||||
{
|
||||
return ctx;
|
||||
}
|
||||
|
||||
bool BaseContext::IsDebugging()
|
||||
{
|
||||
return (ctx->flags & SPFLAG_PLUGIN_DEBUG);
|
||||
}
|
||||
|
||||
int BaseContext::SetDebugBreak(SPVM_DEBUGBREAK newpfn, SPVM_DEBUGBREAK *oldpfn)
|
||||
{
|
||||
if (!IsDebugging())
|
||||
{
|
||||
return SP_ERROR_NOTDEBUGGING;
|
||||
}
|
||||
|
||||
*oldpfn = ctx->dbreak;
|
||||
ctx->dbreak = newpfn;
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
IPluginDebugInfo *BaseContext::GetDebugInfo()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
int BaseContext::Execute(uint32_t public_func, cell_t *result)
|
||||
{
|
||||
IVirtualMachine *vm = (IVirtualMachine *)ctx->vmbase;
|
||||
|
||||
uint32_t pushcount = ctx->pushcount;
|
||||
|
||||
int err;
|
||||
sp_public_t *pubfunc;
|
||||
if ((err=GetPublicByIndex(public_func, &pubfunc)) != SP_ERROR_NONE)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
PushCell(pushcount++);
|
||||
ctx->pushcount = 0;
|
||||
|
||||
cell_t save_sp = ctx->sp;
|
||||
cell_t save_hp = ctx->hp;
|
||||
|
||||
err = vm->ContextExecute(ctx, pubfunc->offs, result);
|
||||
|
||||
/**
|
||||
* :TODO: turn this into an error check
|
||||
*/
|
||||
|
||||
#if defined _DEBUG
|
||||
if (err == SP_ERROR_NONE)
|
||||
{
|
||||
assert(ctx->sp - pushcount * sizeof(cell_t) == save_sp);
|
||||
assert(ctx->hp == save_hp);
|
||||
}
|
||||
#endif
|
||||
if (err != SP_ERROR_NONE)
|
||||
{
|
||||
ctx->sp = save_sp;
|
||||
ctx->hp = save_hp;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int BaseContext::HeapAlloc(unsigned int cells, cell_t *local_addr, cell_t **phys_addr)
|
||||
{
|
||||
cell_t *addr;
|
||||
ucell_t realmem;
|
||||
|
||||
#if 0
|
||||
if (cells > CELLBOUNDMAX)
|
||||
{
|
||||
return SP_ERROR_ARAM;
|
||||
}
|
||||
#else
|
||||
assert(cells < CELLBOUNDMAX);
|
||||
#endif
|
||||
|
||||
realmem = cells * sizeof(cell_t);
|
||||
|
||||
/**
|
||||
* Check if the space between the heap and stack is sufficient.
|
||||
*/
|
||||
if ((cell_t)(ctx->sp - ctx->hp - realmem) < STACKMARGIN)
|
||||
{
|
||||
return SP_ERROR_HEAPLOW;
|
||||
}
|
||||
|
||||
addr = (cell_t *)(ctx->memory + ctx->hp);
|
||||
/* store size of allocation in cells */
|
||||
*addr = (cell_t)cells;
|
||||
addr++;
|
||||
ctx->hp += sizeof(cell_t);
|
||||
|
||||
*local_addr = ctx->hp;
|
||||
|
||||
if (phys_addr)
|
||||
{
|
||||
*phys_addr = addr;
|
||||
}
|
||||
|
||||
ctx->hp += realmem;
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::HeapPop(cell_t local_addr)
|
||||
{
|
||||
cell_t cellcount;
|
||||
cell_t *addr;
|
||||
|
||||
/* check the bounds of this address */
|
||||
local_addr -= sizeof(cell_t);
|
||||
if (local_addr < ctx->heap_base || local_addr >= ctx->sp)
|
||||
{
|
||||
return SP_ERROR_INVALID_ADDRESS;
|
||||
}
|
||||
|
||||
addr = (cell_t *)(ctx->memory + local_addr);
|
||||
cellcount = (*addr) * sizeof(cell_t);
|
||||
/* check if this memory count looks valid */
|
||||
if (ctx->hp - cellcount - sizeof(cell_t) != local_addr)
|
||||
{
|
||||
return SP_ERROR_INVALID_ADDRESS;
|
||||
}
|
||||
|
||||
ctx->hp = local_addr;
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
|
||||
int BaseContext::HeapRelease(cell_t local_addr)
|
||||
{
|
||||
if (local_addr < ctx->heap_base)
|
||||
{
|
||||
return SP_ERROR_INVALID_ADDRESS;
|
||||
}
|
||||
|
||||
ctx->hp = local_addr - sizeof(cell_t);
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::FindNativeByName(const char *name, uint32_t *index)
|
||||
{
|
||||
int diff, high, low;
|
||||
uint32_t mid;
|
||||
|
||||
high = ctx->plugin->info.natives_num - 1;
|
||||
low = 0;
|
||||
|
||||
while (low <= high)
|
||||
{
|
||||
mid = (low + high) / 2;
|
||||
diff = strcmp(ctx->natives[mid].name, name);
|
||||
if (diff == 0)
|
||||
{
|
||||
if (index)
|
||||
{
|
||||
*index = mid;
|
||||
}
|
||||
return SP_ERROR_NONE;
|
||||
} else if (diff < 0) {
|
||||
low = mid + 1;
|
||||
} else {
|
||||
high = mid - 1;
|
||||
}
|
||||
}
|
||||
|
||||
return SP_ERROR_NOT_FOUND;
|
||||
}
|
||||
|
||||
int BaseContext::GetNativeByIndex(uint32_t index, sp_native_t **native)
|
||||
{
|
||||
if (index >= ctx->plugin->info.natives_num)
|
||||
{
|
||||
return SP_ERROR_INDEX;
|
||||
}
|
||||
|
||||
if (native)
|
||||
{
|
||||
*native = &(ctx->natives[index]);
|
||||
}
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
|
||||
uint32_t BaseContext::GetNativesNum()
|
||||
{
|
||||
return ctx->plugin->info.natives_num;
|
||||
}
|
||||
|
||||
int BaseContext::FindPublicByName(const char *name, uint32_t *index)
|
||||
{
|
||||
int diff, high, low;
|
||||
uint32_t mid;
|
||||
|
||||
high = ctx->plugin->info.publics_num - 1;
|
||||
low = 0;
|
||||
|
||||
while (low <= high)
|
||||
{
|
||||
mid = (low + high) / 2;
|
||||
diff = strcmp(ctx->publics[mid].name, name);
|
||||
if (diff == 0)
|
||||
{
|
||||
if (index)
|
||||
{
|
||||
*index = mid;
|
||||
}
|
||||
return SP_ERROR_NONE;
|
||||
} else if (diff < 0) {
|
||||
low = mid + 1;
|
||||
} else {
|
||||
high = mid - 1;
|
||||
}
|
||||
}
|
||||
|
||||
return SP_ERROR_NOT_FOUND;
|
||||
}
|
||||
|
||||
int BaseContext::GetPublicByIndex(uint32_t index, sp_public_t **pblic)
|
||||
{
|
||||
if (index >= ctx->plugin->info.publics_num)
|
||||
{
|
||||
return SP_ERROR_INDEX;
|
||||
}
|
||||
|
||||
if (pblic)
|
||||
{
|
||||
*pblic = &(ctx->publics[index]);
|
||||
}
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
uint32_t BaseContext::GetPublicsNum()
|
||||
{
|
||||
return ctx->plugin->info.publics_num;
|
||||
}
|
||||
|
||||
int BaseContext::GetPubvarByIndex(uint32_t index, sp_pubvar_t **pubvar)
|
||||
{
|
||||
if (index >= ctx->plugin->info.pubvars_num)
|
||||
{
|
||||
return SP_ERROR_INDEX;
|
||||
}
|
||||
|
||||
if (pubvar)
|
||||
{
|
||||
*pubvar = &(ctx->pubvars[index]);
|
||||
}
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::FindPubvarByName(const char *name, uint32_t *index)
|
||||
{
|
||||
int diff, high, low;
|
||||
uint32_t mid;
|
||||
|
||||
high = ctx->plugin->info.pubvars_num - 1;
|
||||
low = 0;
|
||||
|
||||
while (low <= high)
|
||||
{
|
||||
mid = (low + high) / 2;
|
||||
diff = strcmp(ctx->pubvars[mid].name, name);
|
||||
if (diff == 0)
|
||||
{
|
||||
if (index)
|
||||
{
|
||||
*index = mid;
|
||||
}
|
||||
return SP_ERROR_NONE;
|
||||
} else if (diff < 0) {
|
||||
low = mid + 1;
|
||||
} else {
|
||||
high = mid - 1;
|
||||
}
|
||||
}
|
||||
|
||||
return SP_ERROR_NOT_FOUND;
|
||||
}
|
||||
|
||||
int BaseContext::GetPubvarAddrs(uint32_t index, cell_t *local_addr, cell_t **phys_addr)
|
||||
{
|
||||
if (index >= ctx->plugin->info.pubvars_num)
|
||||
{
|
||||
return SP_ERROR_INDEX;
|
||||
}
|
||||
|
||||
*local_addr = ctx->plugin->info.pubvars[index].address;
|
||||
*phys_addr = ctx->pubvars[index].offs;
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
uint32_t BaseContext::GetPubVarsNum()
|
||||
{
|
||||
return ctx->plugin->info.pubvars_num;
|
||||
}
|
||||
|
||||
int BaseContext::BindNatives(sp_nativeinfo_t *natives, unsigned int num, int overwrite)
|
||||
{
|
||||
uint32_t i, j, max;
|
||||
|
||||
max = ctx->plugin->info.natives_num;
|
||||
|
||||
for (i=0; i<max; i++)
|
||||
{
|
||||
if ((ctx->natives[i].status == SP_NATIVE_OKAY) && !overwrite)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for (j=0; (natives[j].name) && (!num || j<num); j++)
|
||||
{
|
||||
if (!strcmp(ctx->natives[i].name, natives[j].name))
|
||||
{
|
||||
ctx->natives[i].pfn = natives[j].func;
|
||||
ctx->natives[i].status = SP_NATIVE_OKAY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::BindNative(sp_nativeinfo_t *native, uint32_t status)
|
||||
{
|
||||
uint32_t index;
|
||||
int err;
|
||||
|
||||
if ((err = FindNativeByName(native->name, &index)) != SP_ERROR_NONE)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
ctx->natives[index].pfn = native->func;
|
||||
ctx->natives[index].status = status;
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::BindNativeToAny(SPVM_NATIVE_FUNC native)
|
||||
{
|
||||
uint32_t nativesnum, i;
|
||||
|
||||
nativesnum = ctx->plugin->info.natives_num;
|
||||
|
||||
for (i=0; i<nativesnum; i++)
|
||||
{
|
||||
if (ctx->natives[i].status != SP_NATIVE_OKAY)
|
||||
{
|
||||
ctx->natives[i].pfn = native;
|
||||
ctx->natives[i].status = SP_NATIVE_PENDING;
|
||||
}
|
||||
}
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::LocalToPhysAddr(cell_t local_addr, cell_t **phys_addr)
|
||||
{
|
||||
if (((local_addr >= ctx->hp) && (local_addr < ctx->sp)) || (local_addr < 0) || ((ucell_t)local_addr >= ctx->mem_size))
|
||||
{
|
||||
return SP_ERROR_INVALID_ADDRESS;
|
||||
}
|
||||
|
||||
if (phys_addr)
|
||||
{
|
||||
*phys_addr = (cell_t *)(ctx->memory + local_addr);
|
||||
}
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::PushCell(cell_t value)
|
||||
{
|
||||
if ((ctx->hp + STACKMARGIN) > (cell_t)(ctx->sp - sizeof(cell_t)))
|
||||
{
|
||||
return SP_ERROR_STACKLOW;
|
||||
}
|
||||
|
||||
ctx->sp -= sizeof(cell_t);
|
||||
*(cell_t *)(ctx->memory + ctx->sp) = value;
|
||||
ctx->pushcount++;
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::PushCellsFromArray(cell_t array[], unsigned int numcells)
|
||||
{
|
||||
unsigned int i;
|
||||
int err;
|
||||
|
||||
for (i=0; i<numcells; i++)
|
||||
{
|
||||
if ((err = PushCell(array[i])) != SP_ERROR_NONE)
|
||||
{
|
||||
ctx->sp += (cell_t)(i * sizeof(cell_t));
|
||||
ctx->pushcount -= i;
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::PushCellArray(cell_t *local_addr, cell_t **phys_addr, cell_t array[], unsigned int numcells)
|
||||
{
|
||||
cell_t *ph_addr;
|
||||
int err;
|
||||
|
||||
if ((err = HeapAlloc(numcells, local_addr, &ph_addr)) != SP_ERROR_NONE)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
memcpy(ph_addr, array, numcells * sizeof(cell_t));
|
||||
|
||||
if ((err = PushCell(*local_addr)) != SP_ERROR_NONE)
|
||||
{
|
||||
HeapRelease(*local_addr);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (phys_addr)
|
||||
{
|
||||
*phys_addr = ph_addr;
|
||||
}
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::LocalToString(cell_t local_addr, char **addr)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
if (((local_addr >= ctx->hp) && (local_addr < ctx->sp)) || (local_addr < 0) || ((ucell_t)local_addr >= ctx->mem_size))
|
||||
{
|
||||
return SP_ERROR_INVALID_ADDRESS;
|
||||
}
|
||||
*addr = (char *)(ctx->memory + local_addr);
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::PushString(cell_t *local_addr, cell_t **phys_addr, const char *string)
|
||||
{
|
||||
cell_t *ph_addr;
|
||||
int err;
|
||||
unsigned int i, numcells = strlen(string);
|
||||
|
||||
if ((err = HeapAlloc(numcells+1, local_addr, &ph_addr)) != SP_ERROR_NONE)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
for (i=0; i<numcells; i++)
|
||||
{
|
||||
ph_addr[i] = (cell_t)string[i];
|
||||
}
|
||||
ph_addr[numcells] = '\0';
|
||||
|
||||
if ((err = PushCell(*local_addr)) != SP_ERROR_NONE)
|
||||
{
|
||||
HeapRelease(*local_addr);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (phys_addr)
|
||||
{
|
||||
*phys_addr = ph_addr;
|
||||
}
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::StringToLocal(cell_t local_addr, size_t chars, const char *source)
|
||||
{
|
||||
cell_t *dest;
|
||||
int i, len;
|
||||
|
||||
if (((local_addr >= ctx->hp) && (local_addr < ctx->sp)) || (local_addr < 0) || ((ucell_t)local_addr >= ctx->mem_size))
|
||||
{
|
||||
return SP_ERROR_INVALID_ADDRESS;
|
||||
}
|
||||
|
||||
len = strlen(source);
|
||||
dest = (cell_t *)(ctx->memory + local_addr);
|
||||
|
||||
if ((size_t)len >= chars)
|
||||
{
|
||||
len = chars - 1;
|
||||
}
|
||||
|
||||
for (i=0; i<len; i++)
|
||||
{
|
||||
dest[i] = (cell_t)source[i];
|
||||
}
|
||||
dest[len] = '\0';
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
#define USHR(x) ((unsigned int)(x)>>1)
|
||||
|
||||
int BaseContext::LookupFile(ucell_t addr, const char **filename)
|
||||
{
|
||||
int high, low, mid;
|
||||
|
||||
high = ctx->plugin->debug.files_num;
|
||||
low = -1;
|
||||
|
||||
while (high - low > 1)
|
||||
{
|
||||
mid = USHR(low + high);
|
||||
if (ctx->files[mid].addr <= addr)
|
||||
{
|
||||
low = mid;
|
||||
} else {
|
||||
high = mid;
|
||||
}
|
||||
}
|
||||
|
||||
if (low == -1)
|
||||
{
|
||||
return SP_ERROR_NOT_FOUND;
|
||||
}
|
||||
|
||||
*filename = ctx->files[low].name;
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::LookupFunction(ucell_t addr, const char **name)
|
||||
{
|
||||
uint32_t iter, max = ctx->plugin->debug.syms_num;
|
||||
|
||||
for (iter=0; iter<max; iter++)
|
||||
{
|
||||
if ((ctx->symbols[iter].sym->ident == SP_SYM_FUNCTION)
|
||||
&& (ctx->symbols[iter].codestart <= addr)
|
||||
&& (ctx->symbols[iter].codeend > addr))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (iter >= max)
|
||||
{
|
||||
return SP_ERROR_NOT_FOUND;
|
||||
}
|
||||
|
||||
*name = ctx->symbols[iter].name;
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
int BaseContext::LookupLine(ucell_t addr, uint32_t *line)
|
||||
{
|
||||
int high, low, mid;
|
||||
|
||||
high = ctx->plugin->debug.lines_num;
|
||||
low = -1;
|
||||
|
||||
while (high - low > 1)
|
||||
{
|
||||
mid = USHR(low + high);
|
||||
if (ctx->lines[mid].addr <= addr)
|
||||
{
|
||||
low = mid;
|
||||
} else {
|
||||
high = mid;
|
||||
}
|
||||
}
|
||||
|
||||
if (low == -1)
|
||||
{
|
||||
return SP_ERROR_NOT_FOUND;
|
||||
}
|
||||
|
||||
*line = ctx->lines[low].line;
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef _INCLUDE_SOURCEPAWN_BASECONTEXT_H_
|
||||
#define _INCLUDE_SOURCEPAWN_BASECONTEXT_H_
|
||||
|
||||
#include "sp_vm_api.h"
|
||||
|
||||
namespace SourcePawn
|
||||
{
|
||||
class BaseContext :
|
||||
public IPluginContext,
|
||||
public IPluginDebugInfo
|
||||
{
|
||||
public:
|
||||
BaseContext(sp_context_t *ctx);
|
||||
public: //IPluginContext
|
||||
IVirtualMachine *GetVirtualMachine();
|
||||
sp_context_t *GetContext();
|
||||
bool IsDebugging();
|
||||
int SetDebugBreak(SPVM_DEBUGBREAK newpfn, SPVM_DEBUGBREAK *oldpfn);
|
||||
IPluginDebugInfo *GetDebugInfo();
|
||||
virtual int HeapAlloc(unsigned int cells, cell_t *local_addr, cell_t **phys_addr);
|
||||
virtual int HeapPop(cell_t local_addr);
|
||||
virtual int HeapRelease(cell_t local_addr);
|
||||
virtual int FindNativeByName(const char *name, uint32_t *index);
|
||||
virtual int GetNativeByIndex(uint32_t index, sp_native_t **native);
|
||||
virtual uint32_t GetNativesNum();
|
||||
virtual int FindPublicByName(const char *name, uint32_t *index);
|
||||
virtual int GetPublicByIndex(uint32_t index, sp_public_t **publicptr);
|
||||
virtual uint32_t GetPublicsNum();
|
||||
virtual int GetPubvarByIndex(uint32_t index, sp_pubvar_t **pubvar);
|
||||
virtual int FindPubvarByName(const char *name, uint32_t *index);
|
||||
virtual int GetPubvarAddrs(uint32_t index, cell_t *local_addr, cell_t **phys_addr);
|
||||
virtual uint32_t GetPubVarsNum();
|
||||
virtual int LocalToPhysAddr(cell_t local_addr, cell_t **phys_addr);
|
||||
virtual int LocalToString(cell_t local_addr, char **addr);
|
||||
virtual int StringToLocal(cell_t local_addr, size_t chars, const char *source);
|
||||
virtual int PushCell(cell_t value);
|
||||
virtual int PushCellArray(cell_t *local_addr, cell_t **phys_addr, cell_t array[], unsigned int numcells);
|
||||
virtual int PushString(cell_t *local_addr, cell_t **phys_addr, const char *string);
|
||||
virtual int PushCellsFromArray(cell_t array[], unsigned int numcells);
|
||||
virtual int BindNatives(sp_nativeinfo_t *natives, unsigned int num, int overwrite);
|
||||
virtual int BindNative(sp_nativeinfo_t *native, uint32_t status);
|
||||
virtual int BindNativeToAny(SPVM_NATIVE_FUNC native);
|
||||
virtual int Execute(uint32_t public_func, cell_t *result);
|
||||
public: //IPluginDebugInfo
|
||||
virtual int LookupFile(ucell_t addr, const char **filename);
|
||||
virtual int LookupFunction(ucell_t addr, const char **name);
|
||||
virtual int LookupLine(ucell_t addr, uint32_t *line);
|
||||
private:
|
||||
sp_context_t *ctx;
|
||||
};
|
||||
};
|
||||
|
||||
#endif //_INCLUDE_SOURCEPAWN_BASECONTEXT_H_
|
||||
@@ -0,0 +1,309 @@
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include "sp_file_headers.h"
|
||||
#include "sp_vm_types.h"
|
||||
#include "sp_vm_engine.h"
|
||||
#include "zlib/zlib.h"
|
||||
#include "sp_vm_basecontext.h"
|
||||
|
||||
#if defined WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#else if defined __GNUC__
|
||||
#include <sys/mman.h>
|
||||
#endif
|
||||
|
||||
using namespace SourcePawn;
|
||||
|
||||
void *SourcePawnEngine::ExecAlloc(size_t size)
|
||||
{
|
||||
#if defined WIN32
|
||||
return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
||||
#else if defined __GNUC__
|
||||
void *base = memalign(sysconf(_SC_PAGESIZE), size);
|
||||
if (mprotect(base, size, PROT_READ|PROT_WRITE|PROT_EXEC) != 0)
|
||||
{
|
||||
free(base);
|
||||
return NULL;
|
||||
}
|
||||
return base;
|
||||
#endif
|
||||
}
|
||||
|
||||
void SourcePawnEngine::ExecFree(void *address)
|
||||
{
|
||||
#if defined WIN32
|
||||
VirtualFree(address, 0, MEM_RELEASE);
|
||||
#else if defined __GNUC__
|
||||
free(address);
|
||||
#endif
|
||||
}
|
||||
|
||||
void *SourcePawnEngine::BaseAlloc(size_t size)
|
||||
{
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
void SourcePawnEngine::BaseFree(void *memory)
|
||||
{
|
||||
free(memory);
|
||||
}
|
||||
|
||||
IPluginContext *SourcePawnEngine::CreateBaseContext(sp_context_t *ctx)
|
||||
{
|
||||
return new BaseContext(ctx);
|
||||
}
|
||||
|
||||
void SourcePawnEngine::FreeBaseContext(IPluginContext *ctx)
|
||||
{
|
||||
sp_context_t *_ctx = ctx->GetContext();
|
||||
IVirtualMachine *vm = ctx->GetVirtualMachine();
|
||||
|
||||
vm->FreeContext(_ctx);
|
||||
|
||||
delete ctx;
|
||||
}
|
||||
|
||||
sp_plugin_t *_ReadPlugin(sp_file_hdr_t *hdr, uint8_t *base, sp_plugin_t *plugin, int *err)
|
||||
{
|
||||
char *nameptr;
|
||||
uint8_t sectnum = 0;
|
||||
sp_file_section_t *secptr = (sp_file_section_t *)(base + sizeof(sp_file_hdr_t));
|
||||
|
||||
memset(plugin, 0, sizeof(sp_plugin_t));
|
||||
|
||||
plugin->base = base;
|
||||
|
||||
while (sectnum < hdr->sections)
|
||||
{
|
||||
nameptr = (char *)(base + hdr->stringtab + secptr->nameoffs);
|
||||
|
||||
if (!(plugin->pcode) && !strcmp(nameptr, ".code"))
|
||||
{
|
||||
sp_file_code_t *cod = (sp_file_code_t *)(base + secptr->dataoffs);
|
||||
plugin->pcode = base + secptr->dataoffs + cod->code;
|
||||
plugin->pcode_size = cod->codesize;
|
||||
plugin->flags = cod->flags;
|
||||
}
|
||||
else if (!(plugin->data) && !strcmp(nameptr, ".data"))
|
||||
{
|
||||
sp_file_data_t *dat = (sp_file_data_t *)(base + secptr->dataoffs);
|
||||
plugin->data = base + secptr->dataoffs + dat->data;
|
||||
plugin->data_size = dat->datasize;
|
||||
plugin->memory = dat->memsize;
|
||||
}
|
||||
else if (!(plugin->info.publics) && !strcmp(nameptr, ".publics"))
|
||||
{
|
||||
plugin->info.publics_num = secptr->size / sizeof(sp_file_publics_t);
|
||||
plugin->info.publics = (sp_file_publics_t *)(base + secptr->dataoffs);
|
||||
}
|
||||
else if (!(plugin->info.pubvars) && !strcmp(nameptr, ".pubvars"))
|
||||
{
|
||||
plugin->info.pubvars_num = secptr->size / sizeof(sp_file_pubvars_t);
|
||||
plugin->info.pubvars = (sp_file_pubvars_t *)(base + secptr->dataoffs);
|
||||
}
|
||||
else if (!(plugin->info.natives) && !strcmp(nameptr, ".natives"))
|
||||
{
|
||||
plugin->info.natives_num = secptr->size / sizeof(sp_file_natives_t);
|
||||
plugin->info.natives = (sp_file_natives_t *)(base + secptr->dataoffs);
|
||||
}
|
||||
else if (!(plugin->info.lib) && !strcmp(nameptr, ".libraries"))
|
||||
{
|
||||
plugin->info.libraries_num = secptr->size / sizeof(sp_file_libraries_t);
|
||||
plugin->info.lib = (sp_file_libraries_t *)(base + secptr->dataoffs);
|
||||
}
|
||||
else if (!(plugin->info.stringbase) && !strcmp(nameptr, ".names"))
|
||||
{
|
||||
plugin->info.stringbase = (const char *)(base + secptr->dataoffs);
|
||||
}
|
||||
else if (!(plugin->debug.files) && !strcmp(nameptr, ".dbg.files"))
|
||||
{
|
||||
plugin->debug.files = (sp_fdbg_file_t *)(base + secptr->dataoffs);
|
||||
}
|
||||
else if (!(plugin->debug.lines) && !strcmp(nameptr, ".dbg.lines"))
|
||||
{
|
||||
plugin->debug.lines = (sp_fdbg_line_t *)(base + secptr->dataoffs);
|
||||
}
|
||||
else if (!(plugin->debug.symbols) && !strcmp(nameptr, ".dbg.symbols"))
|
||||
{
|
||||
plugin->debug.symbols = (sp_fdbg_symbol_t *)(base + secptr->dataoffs);
|
||||
}
|
||||
else if (!(plugin->debug.lines_num) && !strcmp(nameptr, ".dbg.info"))
|
||||
{
|
||||
sp_fdbg_info_t *inf = (sp_fdbg_info_t *)(base + secptr->dataoffs);
|
||||
plugin->debug.files_num = inf->num_files;
|
||||
plugin->debug.lines_num = inf->num_lines;
|
||||
plugin->debug.syms_num = inf->num_syms;
|
||||
}
|
||||
else if (!(plugin->debug.stringbase) && !strcmp(nameptr, ".dbg.strings"))
|
||||
{
|
||||
plugin->debug.stringbase = (const char *)(base + secptr->dataoffs);
|
||||
}
|
||||
|
||||
secptr++;
|
||||
sectnum++;
|
||||
}
|
||||
|
||||
if (!(plugin->pcode) || !(plugin->data) || !(plugin->info.stringbase))
|
||||
{
|
||||
goto return_error;
|
||||
}
|
||||
|
||||
if ((plugin->flags & SP_FLAG_DEBUG) && (!(plugin->debug.files) || !(plugin->debug.lines) || !(plugin->debug.symbols)))
|
||||
{
|
||||
goto return_error;
|
||||
}
|
||||
|
||||
if (err)
|
||||
{
|
||||
*err = SP_ERROR_NONE;
|
||||
}
|
||||
|
||||
return plugin;
|
||||
|
||||
return_error:
|
||||
if (err)
|
||||
{
|
||||
*err = SP_ERROR_FILE_FORMAT;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sp_plugin_t *SourcePawnEngine::LoadFromFilePointer(FILE *fp, int *err)
|
||||
{
|
||||
sp_file_hdr_t hdr;
|
||||
sp_plugin_t *plugin;
|
||||
uint8_t *base;
|
||||
int z_result;
|
||||
int error;
|
||||
|
||||
if (!fp)
|
||||
{
|
||||
error = SP_ERROR_NOT_FOUND;
|
||||
goto return_error;
|
||||
}
|
||||
|
||||
/* Rewind for safety */
|
||||
rewind(fp);
|
||||
fread(&hdr, sizeof(sp_file_hdr_t), 1, fp);
|
||||
|
||||
if (hdr.magic != SPFILE_MAGIC)
|
||||
{
|
||||
error = SP_ERROR_FILE_FORMAT;
|
||||
goto return_error;
|
||||
}
|
||||
|
||||
switch (hdr.compression)
|
||||
{
|
||||
case SPFILE_COMPRESSION_GZ:
|
||||
{
|
||||
uint32_t uncompsize = hdr.imagesize - hdr.dataoffs;
|
||||
uint32_t compsize = hdr.disksize - hdr.dataoffs;
|
||||
uint32_t sectsize = hdr.dataoffs - sizeof(sp_file_hdr_t);
|
||||
uLongf destlen = uncompsize;
|
||||
|
||||
char *tempbuf = (char *)malloc(compsize);
|
||||
void *uncompdata = malloc(uncompsize);
|
||||
void *sectheader = malloc(sectsize);
|
||||
|
||||
fread(sectheader, sectsize, 1, fp);
|
||||
fread(tempbuf, compsize, 1, fp);
|
||||
|
||||
z_result = uncompress((Bytef *)uncompdata, &destlen, (Bytef *)tempbuf, compsize);
|
||||
free(tempbuf);
|
||||
if (z_result != Z_OK)
|
||||
{
|
||||
free(sectheader);
|
||||
free(uncompdata);
|
||||
error = SP_ERROR_DECOMPRESSOR;
|
||||
goto return_error;
|
||||
}
|
||||
|
||||
base = (uint8_t *)malloc(hdr.imagesize);
|
||||
memcpy(base, &hdr, sizeof(sp_file_hdr_t));
|
||||
memcpy(base + sizeof(sp_file_hdr_t), sectheader, sectsize);
|
||||
free(sectheader);
|
||||
memcpy(base + hdr.dataoffs, uncompdata, uncompsize);
|
||||
free(uncompdata);
|
||||
break;
|
||||
}
|
||||
case SPFILE_COMPRESSION_NONE:
|
||||
{
|
||||
base = (uint8_t *)malloc(hdr.imagesize);
|
||||
rewind(fp);
|
||||
fread(base, hdr.imagesize, 1, fp);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
error = SP_ERROR_DECOMPRESSOR;
|
||||
goto return_error;
|
||||
}
|
||||
}
|
||||
|
||||
plugin = (sp_plugin_t *)malloc(sizeof(sp_plugin_t));
|
||||
if (!_ReadPlugin(&hdr, base, plugin, err))
|
||||
{
|
||||
free(plugin);
|
||||
free(base);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
plugin->allocflags = 0;
|
||||
|
||||
return plugin;
|
||||
|
||||
return_error:
|
||||
if (err)
|
||||
{
|
||||
*err = error;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sp_plugin_t *SourcePawnEngine::LoadFromMemory(void *base, sp_plugin_t *plugin, int *err)
|
||||
{
|
||||
sp_file_hdr_t hdr;
|
||||
uint8_t noptr = 0;
|
||||
|
||||
memcpy(&hdr, base, sizeof(sp_file_hdr_t));
|
||||
|
||||
if (!plugin)
|
||||
{
|
||||
plugin = (sp_plugin_t *)malloc(sizeof(sp_plugin_t));
|
||||
noptr = 1;
|
||||
}
|
||||
|
||||
if (!_ReadPlugin(&hdr, (uint8_t *)base, plugin, err))
|
||||
{
|
||||
if (noptr)
|
||||
{
|
||||
free(plugin);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!noptr)
|
||||
{
|
||||
plugin->allocflags |= SP_FA_SELF_EXTERNAL;
|
||||
}
|
||||
plugin->allocflags |= SP_FA_BASE_EXTERNAL;
|
||||
|
||||
return plugin;
|
||||
}
|
||||
|
||||
int SourcePawnEngine::FreeFromMemory(sp_plugin_t *plugin)
|
||||
{
|
||||
if (!(plugin->allocflags & SP_FA_BASE_EXTERNAL))
|
||||
{
|
||||
free(plugin->base);
|
||||
plugin->base = NULL;
|
||||
}
|
||||
if (!(plugin->allocflags & SP_FA_SELF_EXTERNAL))
|
||||
{
|
||||
free(plugin);
|
||||
}
|
||||
|
||||
return SP_ERROR_NONE;
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
#ifndef _INCLUDE_SOURCEPAWN_VM_ENGINE_H_
|
||||
#define _INCLUDE_SOURCEPAWN_VM_ENGINE_H_
|
||||
|
||||
#include "sp_vm_api.h"
|
||||
|
||||
namespace SourcePawn
|
||||
{
|
||||
class SourcePawnEngine : public ISourcePawnEngine
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Loads a named file from a file pointer.
|
||||
* Using this means base memory will be allocated by the VM.
|
||||
* Note: The file handle position may be undefined on entry, and is
|
||||
* always undefined on conclusion.
|
||||
*
|
||||
* @param fp File pointer. May be at any offset. Not closed on return.
|
||||
* @param err Optional error code pointer.
|
||||
* @return A new plugin structure.
|
||||
*/
|
||||
sp_plugin_t *LoadFromFilePointer(FILE *fp, int *err);
|
||||
|
||||
/**
|
||||
* Loads a file from a base memory address.
|
||||
*
|
||||
* @param base Base address of the plugin's memory region.
|
||||
* @param plugin If NULL, a new plugin pointer is returned.
|
||||
* Otherwise, the passed pointer is used.
|
||||
* @param err Optional error code pointer.
|
||||
* @return The resulting plugin pointer.
|
||||
*/
|
||||
sp_plugin_t *LoadFromMemory(void *base, sp_plugin_t *plugin, int *err);
|
||||
|
||||
/**
|
||||
* Frees all of the memory associated with a plugin file.
|
||||
* If allocated using SP_LoadFromMemory, the base and plugin pointer
|
||||
* itself are not freed (so this may end up doing nothing).
|
||||
*/
|
||||
int FreeFromMemory(sp_plugin_t *plugin);
|
||||
|
||||
/**
|
||||
* Creates a new IContext from a context handle.
|
||||
*
|
||||
* @param ctx Context to use as a basis for the IPluginContext.
|
||||
* @return New IPluginContext handle.
|
||||
*/
|
||||
IPluginContext *CreateBaseContext(sp_context_t *ctx);
|
||||
|
||||
/**
|
||||
* Frees a context.
|
||||
*
|
||||
* @param ctx Context pointer to free.
|
||||
*/
|
||||
void FreeBaseContext(IPluginContext *ctx);
|
||||
|
||||
/**
|
||||
* Allocates memory.
|
||||
*
|
||||
* @param size Size of memory to allocate.
|
||||
* @return Pointer to memory, NULL if allocation failed.
|
||||
*/
|
||||
void *BaseAlloc(size_t size);
|
||||
|
||||
/**
|
||||
* Frees memory allocated with BaseAlloc.
|
||||
*
|
||||
* @param memory Memory address to free.
|
||||
*/
|
||||
void BaseFree(void *memory);
|
||||
|
||||
/**
|
||||
* Allocates executable memory.
|
||||
*
|
||||
* @param size Size of memory to allocate.
|
||||
* @return Pointer to memory, NULL if allocation failed.
|
||||
*/
|
||||
void *ExecAlloc(size_t size);
|
||||
|
||||
/**
|
||||
* Frees executable memory.
|
||||
*
|
||||
* @param address Address to free.
|
||||
*/
|
||||
void ExecFree(void *address);
|
||||
};
|
||||
};
|
||||
|
||||
#endif //_INCLUDE_SOURCEPAWN_VM_ENGINE_H_
|
||||
Reference in New Issue
Block a user