diff --git a/core/vm/sp_vm_basecontext.cpp b/core/vm/sp_vm_basecontext.cpp
index ac505533..8e132a4e 100644
--- a/core/vm/sp_vm_basecontext.cpp
+++ b/core/vm/sp_vm_basecontext.cpp
@@ -150,7 +150,11 @@ int BaseContext::Execute(funcid_t funcid, cell_t *result)
}
code_addr = pubfunc->code_offs;
} else {
+#if 0
code_addr = funcid >> 1;
+#endif
+ assert(false);
+ return SP_ERROR_INVALID_ADDRESS;
}
PushCell(pushcount++);
@@ -875,6 +879,7 @@ IPluginFunction *BaseContext::GetFunctionById(funcid_t func_id)
pFunc = m_pub_funcs[func_id];
}
} else {
+#if 0
func_id >>= 1;
unsigned int index;
if (!g_pVM->FunctionLookup(ctx, func_id, &index))
@@ -887,6 +892,8 @@ IPluginFunction *BaseContext::GetFunctionById(funcid_t func_id)
m_priv_funcs[func_id] = new CFunction(save, this);
pFunc = m_priv_funcs[func_id];
}
+#endif
+ assert(false);
}
return pFunc;
diff --git a/public/sourcepawn/sp_vm_api.h b/public/sourcepawn/sp_vm_api.h
index 21ff120d..a8232f6e 100644
--- a/public/sourcepawn/sp_vm_api.h
+++ b/public/sourcepawn/sp_vm_api.h
@@ -794,6 +794,16 @@ namespace SourcePawn
* @return String describing CPU specific optimizations.
*/
virtual const char *GetCPUOptimizations() =0;
+
+ /**
+ * @brief Given a context and a p-code address, returns the index of the function.
+ *
+ * @param ctx Context to search.
+ * @param code_addr Index into the p-code section.
+ * @param result Pointer to store result into.
+ * @return True if code index is valid, false otherwise.
+ */
+ virtual bool FunctionPLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result) =0;
};
};
diff --git a/sourcepawn/jit/x86/jit_x86.cpp b/sourcepawn/jit/x86/jit_x86.cpp
index 346cacfb..2483aadf 100644
--- a/sourcepawn/jit/x86/jit_x86.cpp
+++ b/sourcepawn/jit/x86/jit_x86.cpp
@@ -2161,10 +2161,12 @@ jit_rewind:
functracker_t *fnc = new functracker_t;
ctx->vm[JITVARS_FUNCINFO] = fnc;
+ ctx->vm[JITVARS_REBASE] = data->rebase;
fnc->code_size = codemem;
fnc->num_functions = data->func_idx;
/* clean up relocation+compilation memory */
+ data->rebase = NULL;
AbortCompilation(co);
*err = SP_ERROR_NONE;
@@ -2194,6 +2196,7 @@ void JITX86::FreeContext(sp_context_t *ctx)
delete [] ctx->publics;
delete [] ctx->pubvars;
delete [] ctx->symbols;
+ engine->BaseFree(ctx->vm[JITVARS_REBASE]);
free(((tracker_t *)(ctx->vm[JITVARS_TRACKER]))->pBase);
delete ctx->vm[JITVARS_TRACKER];
delete ctx;
@@ -2247,15 +2250,51 @@ unsigned int JITX86::GetAPIVersion()
return SOURCEPAWN_VM_API_VERSION;
}
-bool JITX86::FunctionLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result)
+bool JITX86::FunctionPLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result)
{
- functracker_t *fnc = (functracker_t *)ctx->vm[JITVARS_FUNCINFO];
+ uint8_t *rebase = (uint8_t *)ctx->vm[JITVARS_REBASE];
+ /* Is this within the pcode bounds? */
+ if (code_addr >= ctx->plugin->pcode_size - sizeof(uint32_t))
+ {
+ return false;
+ }
+
+ /* Relocate this */
+ code_addr = *(jitoffs_t *)(rebase + code_addr);
+
+ /* Check if this is in the relocation bounds */
+ functracker_t *fnc = (functracker_t *)ctx->vm[JITVARS_FUNCINFO];
if (code_addr >= fnc->code_size)
{
return false;
}
+ /* Get the function info and sanity check */
+ funcinfo_t *f = (funcinfo_t *)((char *)ctx->codebase + code_addr - sizeof(funcinfo_t));
+ if (f->magic != JIT_FUNCMAGIC || f->index >= fnc->num_functions)
+ {
+ return false;
+ }
+
+ if (result)
+ {
+ *result = f->index;
+ }
+
+ return true;
+}
+
+bool JITX86::FunctionLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result)
+{
+ /* Check if this is in the relocation bounds */
+ functracker_t *fnc = (functracker_t *)ctx->vm[JITVARS_FUNCINFO];
+ if (code_addr >= fnc->code_size)
+ {
+ return false;
+ }
+
+ /* Get the function info and sanity check */
funcinfo_t *f = (funcinfo_t *)((char *)ctx->codebase + code_addr - sizeof(funcinfo_t));
if (f->magic != JIT_FUNCMAGIC || f->index >= fnc->num_functions)
{
diff --git a/sourcepawn/jit/x86/jit_x86.h b/sourcepawn/jit/x86/jit_x86.h
index fcdcbccf..51de2322 100644
--- a/sourcepawn/jit/x86/jit_x86.h
+++ b/sourcepawn/jit/x86/jit_x86.h
@@ -14,6 +14,7 @@ using namespace SourcePawn;
#define JITVARS_TRACKER 0 //important: don't change this to avoid trouble
#define JITVARS_FUNCINFO 1 //important: don't change this aWOAWOGJQG I LIKE HAM
+#define JITVARS_REBASE 2 //important: hi, i'm bail
typedef struct tracker_s
{
@@ -80,6 +81,7 @@ public:
int ContextExecute(sp_context_t *ctx, uint32_t code_idx, cell_t *result);
unsigned int GetAPIVersion();
bool FunctionLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result);
+ bool FunctionPLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result);
unsigned int FunctionCount(const sp_context_t *ctx);
const char *GetVersionString();
const char *GetCPUOptimizations();
diff --git a/sourcepawn/jit/x86/msvc8/jit-x86.vcproj b/sourcepawn/jit/x86/msvc8/jit-x86.vcproj
index 537fd87b..f697686c 100644
--- a/sourcepawn/jit/x86/msvc8/jit-x86.vcproj
+++ b/sourcepawn/jit/x86/msvc8/jit-x86.vcproj
@@ -247,19 +247,23 @@
>
+
+