Move n_err from sp_context_t to PluginContext.

This commit is contained in:
David Anderson 2015-02-24 20:16:13 -08:00
parent 9c104ef310
commit 97dbc7ff07
6 changed files with 28 additions and 25 deletions

View File

@ -105,7 +105,7 @@ CContextTrace::GetTraceInfo(CallStackInfo *trace)
const char * const char *
CContextTrace::GetLastNative(uint32_t *index) CContextTrace::GetLastNative(uint32_t *index)
{ {
if (m_ctx->n_err == SP_ERROR_NONE) if (context_->GetLastNativeError() == SP_ERROR_NONE)
return NULL; return NULL;
int lastNative = context_->lastNative(); int lastNative = context_->lastNative();

View File

@ -842,8 +842,8 @@ Interpret(PluginRuntime *rt, uint32_t aCodeStart, cell_t *rval)
ctx->sp = uintptr_t(stk) - uintptr_t(plugin->memory); ctx->sp = uintptr_t(stk) - uintptr_t(plugin->memory);
pri = cx->invokeNative(native_index, stk); pri = cx->invokeNative(native_index, stk);
if (ctx->n_err != SP_ERROR_NONE) { if (cx->GetLastNativeError() != SP_ERROR_NONE) {
ctx->err = ctx->n_err; ctx->err = cx->GetLastNativeError();
goto error; goto error;
} }

View File

@ -78,7 +78,6 @@ typedef struct sp_context_s
cell_t rval; /**< Return value from InvokeFunction() */ cell_t rval; /**< Return value from InvokeFunction() */
int32_t cip; /**< Code pointer last error occurred in */ int32_t cip; /**< Code pointer last error occurred in */
int32_t err; /**< Error last set by interpreter */ int32_t err; /**< Error last set by interpreter */
int32_t n_err; /**< Error code set by a native */
sp_plugin_t *plugin; sp_plugin_t *plugin;
PluginContext *basecx; PluginContext *basecx;
} sp_context_t; } sp_context_t;

View File

@ -54,9 +54,9 @@ PluginContext::PluginContext(PluginRuntime *pRuntime)
m_ctx.hp = m_pRuntime->plugin()->data_size; m_ctx.hp = m_pRuntime->plugin()->data_size;
m_ctx.sp = m_pRuntime->plugin()->mem_size - sizeof(cell_t); m_ctx.sp = m_pRuntime->plugin()->mem_size - sizeof(cell_t);
m_ctx.frm = m_ctx.sp; m_ctx.frm = m_ctx.sp;
m_ctx.n_err = SP_ERROR_NONE;
rp_ = 0; rp_ = 0;
last_native_ = -1; last_native_ = -1;
native_error_ = SP_ERROR_NONE;
tracker_.pBase = (ucell_t *)malloc(1024); tracker_.pBase = (ucell_t *)malloc(1024);
tracker_.pCur = tracker_.pBase; tracker_.pCur = tracker_.pBase;
@ -135,7 +135,7 @@ PluginContext::ThrowNativeErrorEx(int error, const char *msg, ...)
if (!m_InExec) if (!m_InExec)
return 0; return 0;
m_ctx.n_err = error; native_error_ = error;
if (msg) { if (msg) {
va_list ap; va_list ap;
@ -153,7 +153,7 @@ PluginContext::ThrowNativeError(const char *msg, ...)
if (!m_InExec) if (!m_InExec)
return 0; return 0;
m_ctx.n_err = SP_ERROR_NATIVE; native_error_ = SP_ERROR_NATIVE;
if (msg) { if (msg) {
va_list ap; va_list ap;
@ -595,7 +595,7 @@ PluginContext::Execute2(IPluginFunction *function, const cell_t *params, unsigne
sp[i + 1] = params[i]; sp[i + 1] = params[i];
/* Clear internal state */ /* Clear internal state */
m_ctx.n_err = SP_ERROR_NONE; native_error_ = SP_ERROR_NONE;
last_native_ = -1; last_native_ = -1;
m_MsgCache[0] = '\0'; m_MsgCache[0] = '\0';
m_CustomMsg = false; m_CustomMsg = false;
@ -613,7 +613,7 @@ PluginContext::Execute2(IPluginFunction *function, const cell_t *params, unsigne
m_InExec = save_exec; m_InExec = save_exec;
if (ir == SP_ERROR_NONE) { if (ir == SP_ERROR_NONE) {
m_ctx.n_err = SP_ERROR_NONE; native_error_ = SP_ERROR_NONE;
if (m_ctx.sp != save_sp) { if (m_ctx.sp != save_sp) {
ir = SP_ERROR_STACKLEAK; ir = SP_ERROR_STACKLEAK;
_SetErrorMessage("Stack leak detected: sp:%d should be %d!", _SetErrorMessage("Stack leak detected: sp:%d should be %d!",
@ -646,7 +646,7 @@ PluginContext::Execute2(IPluginFunction *function, const cell_t *params, unsigne
m_ctx.cip = save_cip; m_ctx.cip = save_cip;
last_native_ = save_n_idx; last_native_ = save_n_idx;
m_ctx.n_err = SP_ERROR_NONE; native_error_ = SP_ERROR_NONE;
m_MsgCache[0] = '\0'; m_MsgCache[0] = '\0';
m_CustomMsg = false; m_CustomMsg = false;
@ -778,7 +778,7 @@ DebugInfo::LookupLine(ucell_t addr, uint32_t *line)
int int
PluginContext::GetLastNativeError() PluginContext::GetLastNativeError()
{ {
return m_ctx.n_err; return native_error_;
} }
cell_t * cell_t *
@ -810,7 +810,7 @@ PluginContext::GetKey(int k, void **value)
void void
PluginContext::ClearLastNativeError() PluginContext::ClearLastNativeError()
{ {
m_ctx.n_err = SP_ERROR_NONE; native_error_ = SP_ERROR_NONE;
} }
int int
@ -863,21 +863,21 @@ PluginContext::invokeNative(ucell_t native_idx, cell_t *params)
sp_native_t *native = &m_pRuntime->plugin()->natives[native_idx]; sp_native_t *native = &m_pRuntime->plugin()->natives[native_idx];
if (native->status == SP_NATIVE_UNBOUND) { if (native->status == SP_NATIVE_UNBOUND) {
m_ctx.n_err = SP_ERROR_INVALID_NATIVE; native_error_ = SP_ERROR_INVALID_NATIVE;
return 0; return 0;
} }
cell_t result = native->pfn(m_ctx.basecx, params); cell_t result = native->pfn(m_ctx.basecx, params);
if (m_ctx.n_err != SP_ERROR_NONE) if (native_error_ != SP_ERROR_NONE)
return result; return result;
if (save_sp != m_ctx.sp) { if (save_sp != m_ctx.sp) {
m_ctx.n_err = SP_ERROR_STACKLEAK; native_error_ = SP_ERROR_STACKLEAK;
return result; return result;
} }
if (save_hp != m_ctx.hp) { if (save_hp != m_ctx.hp) {
m_ctx.n_err = SP_ERROR_HEAPLEAK; native_error_ = SP_ERROR_HEAPLEAK;
return result; return result;
} }
@ -892,15 +892,15 @@ PluginContext::invokeBoundNative(SPVM_NATIVE_FUNC pfn, cell_t *params)
cell_t result = pfn(this, params); cell_t result = pfn(this, params);
if (m_ctx.n_err != SP_ERROR_NONE) if (native_error_ != SP_ERROR_NONE)
return result; return result;
if (save_sp != m_ctx.sp) { if (save_sp != m_ctx.sp) {
m_ctx.n_err = SP_ERROR_STACKLEAK; native_error_ = SP_ERROR_STACKLEAK;
return result; return result;
} }
if (save_hp != m_ctx.hp) { if (save_hp != m_ctx.hp) {
m_ctx.n_err = SP_ERROR_HEAPLEAK; native_error_ = SP_ERROR_HEAPLEAK;
return result; return result;
} }

View File

@ -100,6 +100,9 @@ class PluginContext : public IPluginContext
static inline size_t offsetOfLastNative() { static inline size_t offsetOfLastNative() {
return offsetof(PluginContext, last_native_); return offsetof(PluginContext, last_native_);
} }
static inline size_t offsetOfNativeError() {
return offsetof(PluginContext, native_error_);
}
// Return stack logic. // Return stack logic.
bool pushReturnCip(cell_t cip) { bool pushReturnCip(cell_t cip) {
@ -151,8 +154,9 @@ class PluginContext : public IPluginContext
cell_t rp_; cell_t rp_;
cell_t rstk_cips_[SP_MAX_RETURN_STACK]; cell_t rstk_cips_[SP_MAX_RETURN_STACK];
// Track the currently executing native index. // Track the currently executing native index, and any error it throws.
uint32_t last_native_; int32_t last_native_;
int native_error_;
}; };
#endif //_INCLUDE_SOURCEPAWN_BASECONTEXT_H_ #endif //_INCLUDE_SOURCEPAWN_BASECONTEXT_H_

View File

@ -1632,8 +1632,8 @@ Compiler::emitNativeCall(OPCODE op)
} }
// Check for errors. // Check for errors.
__ movl(ecx, intptr_t(rt_->GetBaseContext()->GetCtx())); __ movl(ecx, intptr_t(rt_->GetBaseContext()));
__ movl(ecx, Operand(ecx, offsetof(sp_context_t, n_err))); __ movl(ecx, Operand(ecx, PluginContext::offsetOfNativeError()));
__ testl(ecx, ecx); __ testl(ecx, ecx);
__ j(not_zero, &extern_error_); __ j(not_zero, &extern_error_);
@ -1824,8 +1824,8 @@ Compiler::emitErrorPaths()
if (extern_error_.used()) { if (extern_error_.used()) {
__ bind(&extern_error_); __ bind(&extern_error_);
__ movl(eax, intptr_t(rt_->GetBaseContext()->GetCtx())); __ movl(eax, intptr_t(rt_->GetBaseContext()));
__ movl(eax, Operand(eax, offsetof(sp_context_t, n_err))); __ movl(eax, Operand(eax, PluginContext::offsetOfNativeError()));
__ jmp(ExternalAddress(env_->stubs()->ReturnStub())); __ jmp(ExternalAddress(env_->stubs()->ReturnStub()));
} }
} }