2015-02-24 07:36:10 +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 <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include "x86/jit_x86.h"
|
|
|
|
#include "environment.h"
|
|
|
|
#include "api.h"
|
|
|
|
#include "zlib/zlib.h"
|
|
|
|
#if defined __GNUC__
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#elif defined __GNUC__
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined __linux__
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sourcemod_version.h>
|
2015-02-24 10:12:23 +01:00
|
|
|
#include "code-stubs.h"
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
#include "smx-v1-image.h"
|
2015-02-24 07:36:10 +01:00
|
|
|
|
|
|
|
using namespace sp;
|
|
|
|
using namespace SourcePawn;
|
|
|
|
|
|
|
|
// ////// //
|
|
|
|
// API v1
|
|
|
|
// ////// //
|
|
|
|
|
|
|
|
SourcePawnEngine::SourcePawnEngine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
SourcePawnEngine::GetErrorString(int error)
|
|
|
|
{
|
|
|
|
return Environment::get()->GetErrorString(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
SourcePawnEngine::ExecAlloc(size_t size)
|
|
|
|
{
|
|
|
|
#if defined WIN32
|
|
|
|
return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
|
|
|
#elif defined __GNUC__
|
|
|
|
# if defined __APPLE__
|
|
|
|
void *base = valloc(size);
|
|
|
|
# else
|
|
|
|
void *base = memalign(sysconf(_SC_PAGESIZE), size);
|
|
|
|
# endif
|
|
|
|
if (mprotect(base, size, PROT_READ|PROT_WRITE|PROT_EXEC) != 0) {
|
|
|
|
free(base);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return base;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
SourcePawnEngine::AllocatePageMemory(size_t size)
|
|
|
|
{
|
2015-02-24 08:12:45 +01:00
|
|
|
return Environment::get()->AllocateCode(size);
|
2015-02-24 07:36:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourcePawnEngine::SetReadExecute(void *ptr)
|
|
|
|
{
|
|
|
|
/* already re */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourcePawnEngine::SetReadWrite(void *ptr)
|
|
|
|
{
|
|
|
|
/* already rw */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourcePawnEngine::FreePageMemory(void *ptr)
|
|
|
|
{
|
2015-02-24 08:12:45 +01:00
|
|
|
Environment::get()->FreeCode(ptr);
|
2015-02-24 07:36:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourcePawnEngine::ExecFree(void *address)
|
|
|
|
{
|
|
|
|
#if defined WIN32
|
|
|
|
VirtualFree(address, 0, MEM_RELEASE);
|
|
|
|
#elif defined __GNUC__
|
|
|
|
free(address);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourcePawnEngine::SetReadWriteExecute(void *ptr)
|
|
|
|
{
|
|
|
|
//:TODO: g_ExeMemory.SetRWE(ptr);
|
|
|
|
SetReadExecute(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
SourcePawnEngine::BaseAlloc(size_t size)
|
|
|
|
{
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourcePawnEngine::BaseFree(void *memory)
|
|
|
|
{
|
|
|
|
free(memory);
|
|
|
|
}
|
|
|
|
|
|
|
|
sp_plugin_t *
|
|
|
|
SourcePawnEngine::LoadFromFilePointer(FILE *fp, int *err)
|
|
|
|
{
|
|
|
|
if (err != NULL)
|
|
|
|
*err = SP_ERROR_ABORTED;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sp_plugin_t *
|
|
|
|
SourcePawnEngine::LoadFromMemory(void *base, sp_plugin_t *plugin, int *err)
|
|
|
|
{
|
|
|
|
if (err != NULL)
|
|
|
|
*err = SP_ERROR_ABORTED;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SourcePawnEngine::FreeFromMemory(sp_plugin_t *plugin)
|
|
|
|
{
|
|
|
|
return SP_ERROR_ABORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
IDebugListener *
|
|
|
|
SourcePawnEngine::SetDebugListener(IDebugListener *pListener)
|
|
|
|
{
|
|
|
|
IDebugListener *old = Environment::get()->debugger();
|
|
|
|
Environment::get()->SetDebugger(pListener);
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
SourcePawnEngine::GetEngineAPIVersion()
|
|
|
|
{
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
SourcePawnEngine::GetContextCallCount()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ////// //
|
|
|
|
// API v2
|
|
|
|
// ////// //
|
|
|
|
|
|
|
|
SourcePawnEngine2::SourcePawnEngine2()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
Implement a new stack and error handling model for the SourcePawn VM.
This has three major changes to SourcePawn. First, the API now supports the concept of "exceptions". The exception state is a global property of an instance of the SourcePawn VM. Exceptions can be caught or suppressed. Many places in SourceMod have been updated to check exceptions instead of errors.
The new API obsoletes major parts of the embedder API - all but one method of invoking functions is obsoleted, and the debug interface has been scrapped. Extensions using the native API will not be affected, however, ThrowNativeError has been deprecated in favor of ReportError.
Second, the SourcePawn concept of a "stack" has been unified at the API level. A stack frame iterator now iterates over all SourcePawn invocations, rather than the topmost plugin. This makes error handling more consistent and removes another dependency on context-per-plugin.
Finally, the implementation of stack frames has been changed dramatically. Rather than maintain a complicated and expensive return pointer stack, we now rely on the implicit one provided by the CPU. The stack frame iterator now walks the JIT stack directly. This removes many unnecessary bookkeeping instructions from the generated code, in particular making the CALL instruction 40% faster.
These changes required some fair surgery to the JIT. Its error paths are now slightly more complicated, as they have to throw an exception rather than return an error code. In addition, any path that can throw an exception is now responsible for creating an "exit frame", which exists to tell the stack frame iterator about transitions from the JIT to the VM.
2015-02-27 09:32:44 +01:00
|
|
|
size_t
|
|
|
|
sp::UTIL_FormatVA(char *buffer, size_t maxlength, const char *fmt, va_list ap)
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
{
|
|
|
|
size_t len = vsnprintf(buffer, maxlength, fmt, ap);
|
|
|
|
|
|
|
|
if (len >= maxlength) {
|
|
|
|
buffer[maxlength - 1] = '\0';
|
|
|
|
return maxlength - 1;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
Implement a new stack and error handling model for the SourcePawn VM.
This has three major changes to SourcePawn. First, the API now supports the concept of "exceptions". The exception state is a global property of an instance of the SourcePawn VM. Exceptions can be caught or suppressed. Many places in SourceMod have been updated to check exceptions instead of errors.
The new API obsoletes major parts of the embedder API - all but one method of invoking functions is obsoleted, and the debug interface has been scrapped. Extensions using the native API will not be affected, however, ThrowNativeError has been deprecated in favor of ReportError.
Second, the SourcePawn concept of a "stack" has been unified at the API level. A stack frame iterator now iterates over all SourcePawn invocations, rather than the topmost plugin. This makes error handling more consistent and removes another dependency on context-per-plugin.
Finally, the implementation of stack frames has been changed dramatically. Rather than maintain a complicated and expensive return pointer stack, we now rely on the implicit one provided by the CPU. The stack frame iterator now walks the JIT stack directly. This removes many unnecessary bookkeeping instructions from the generated code, in particular making the CALL instruction 40% faster.
These changes required some fair surgery to the JIT. Its error paths are now slightly more complicated, as they have to throw an exception rather than return an error code. In addition, any path that can throw an exception is now responsible for creating an "exit frame", which exists to tell the stack frame iterator about transitions from the JIT to the VM.
2015-02-27 09:32:44 +01:00
|
|
|
size_t
|
|
|
|
sp::UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
size_t len = UTIL_FormatVA(buffer, maxlength, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-02-24 07:36:10 +01:00
|
|
|
IPluginRuntime *
|
|
|
|
SourcePawnEngine2::LoadPlugin(ICompilation *co, const char *file, int *err)
|
|
|
|
{
|
2015-02-24 09:21:52 +01:00
|
|
|
if (co) {
|
|
|
|
if (err)
|
|
|
|
*err = SP_ERROR_PARAM;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
IPluginRuntime *rt = LoadBinaryFromFile(file, nullptr, 0);
|
|
|
|
if (!rt) {
|
|
|
|
if (err) {
|
|
|
|
if (FILE *fp = fopen(file, "rb")) {
|
|
|
|
fclose(fp);
|
|
|
|
*err = SP_ERROR_FILE_FORMAT;
|
|
|
|
} else {
|
|
|
|
*err = SP_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2015-02-24 07:36:10 +01:00
|
|
|
}
|
|
|
|
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
return rt;
|
|
|
|
}
|
2015-02-24 07:36:10 +01:00
|
|
|
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
IPluginRuntime *
|
|
|
|
SourcePawnEngine2::LoadBinaryFromFile(const char *file, char *error, size_t maxlength)
|
|
|
|
{
|
|
|
|
FILE *fp = fopen(file, "rb");
|
|
|
|
|
|
|
|
if (!fp) {
|
|
|
|
UTIL_Format(error, maxlength, "file not found");
|
|
|
|
return nullptr;
|
2015-02-24 07:36:10 +01:00
|
|
|
}
|
|
|
|
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
ke::AutoPtr<SmxV1Image> image(new SmxV1Image(fp));
|
|
|
|
fclose(fp);
|
2015-02-24 07:36:10 +01:00
|
|
|
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
if (!image->validate()) {
|
|
|
|
const char *errorMessage = image->errorMessage();
|
|
|
|
if (!errorMessage)
|
|
|
|
errorMessage = "file parse error";
|
|
|
|
UTIL_Format(error, maxlength, "%s", errorMessage);
|
|
|
|
return nullptr;
|
2015-02-24 07:36:10 +01:00
|
|
|
}
|
|
|
|
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
PluginRuntime *pRuntime = new PluginRuntime(image.take());
|
|
|
|
if (!pRuntime->Initialize()) {
|
2015-02-24 07:36:10 +01:00
|
|
|
delete pRuntime;
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
|
|
|
|
UTIL_Format(error, maxlength, "out of memory");
|
|
|
|
return nullptr;
|
2015-02-24 07:36:10 +01:00
|
|
|
}
|
|
|
|
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
size_t len = strlen(file);
|
|
|
|
for (size_t i = len - 1; i < len; i--) {
|
2015-02-24 07:36:10 +01:00
|
|
|
if (file[i] == '/'
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
# if defined WIN32
|
2015-02-24 07:36:10 +01:00
|
|
|
|| file[i] == '\\'
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
# endif
|
2015-02-24 07:36:10 +01:00
|
|
|
)
|
|
|
|
{
|
Implement a new stack and error handling model for the SourcePawn VM.
This has three major changes to SourcePawn. First, the API now supports the concept of "exceptions". The exception state is a global property of an instance of the SourcePawn VM. Exceptions can be caught or suppressed. Many places in SourceMod have been updated to check exceptions instead of errors.
The new API obsoletes major parts of the embedder API - all but one method of invoking functions is obsoleted, and the debug interface has been scrapped. Extensions using the native API will not be affected, however, ThrowNativeError has been deprecated in favor of ReportError.
Second, the SourcePawn concept of a "stack" has been unified at the API level. A stack frame iterator now iterates over all SourcePawn invocations, rather than the topmost plugin. This makes error handling more consistent and removes another dependency on context-per-plugin.
Finally, the implementation of stack frames has been changed dramatically. Rather than maintain a complicated and expensive return pointer stack, we now rely on the implicit one provided by the CPU. The stack frame iterator now walks the JIT stack directly. This removes many unnecessary bookkeeping instructions from the generated code, in particular making the CALL instruction 40% faster.
These changes required some fair surgery to the JIT. Its error paths are now slightly more complicated, as they have to throw an exception rather than return an error code. In addition, any path that can throw an exception is now responsible for creating an "exit frame", which exists to tell the stack frame iterator about transitions from the JIT to the VM.
2015-02-27 09:32:44 +01:00
|
|
|
pRuntime->SetNames(file, &file[i + 1]);
|
2015-02-24 07:36:10 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
if (!pRuntime->Name())
|
Implement a new stack and error handling model for the SourcePawn VM.
This has three major changes to SourcePawn. First, the API now supports the concept of "exceptions". The exception state is a global property of an instance of the SourcePawn VM. Exceptions can be caught or suppressed. Many places in SourceMod have been updated to check exceptions instead of errors.
The new API obsoletes major parts of the embedder API - all but one method of invoking functions is obsoleted, and the debug interface has been scrapped. Extensions using the native API will not be affected, however, ThrowNativeError has been deprecated in favor of ReportError.
Second, the SourcePawn concept of a "stack" has been unified at the API level. A stack frame iterator now iterates over all SourcePawn invocations, rather than the topmost plugin. This makes error handling more consistent and removes another dependency on context-per-plugin.
Finally, the implementation of stack frames has been changed dramatically. Rather than maintain a complicated and expensive return pointer stack, we now rely on the implicit one provided by the CPU. The stack frame iterator now walks the JIT stack directly. This removes many unnecessary bookkeeping instructions from the generated code, in particular making the CALL instruction 40% faster.
These changes required some fair surgery to the JIT. Its error paths are now slightly more complicated, as they have to throw an exception rather than return an error code. In addition, any path that can throw an exception is now responsible for creating an "exit frame", which exists to tell the stack frame iterator about transitions from the JIT to the VM.
2015-02-27 09:32:44 +01:00
|
|
|
pRuntime->SetNames(file, file);
|
2015-02-24 07:36:10 +01:00
|
|
|
|
|
|
|
return pRuntime;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPVM_NATIVE_FUNC
|
|
|
|
SourcePawnEngine2::CreateFakeNative(SPVM_FAKENATIVE_FUNC callback, void *pData)
|
|
|
|
{
|
2015-02-24 10:12:23 +01:00
|
|
|
return Environment::get()->stubs()->CreateFakeNativeStub(callback, pData);
|
2015-02-24 07:36:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourcePawnEngine2::DestroyFakeNative(SPVM_NATIVE_FUNC func)
|
|
|
|
{
|
2015-02-24 10:12:23 +01:00
|
|
|
return Environment::get()->FreeCode((void *)func);
|
2015-02-24 07:36:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
SourcePawnEngine2::GetEngineName()
|
|
|
|
{
|
|
|
|
return "SourcePawn 1.7, jit-x86";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
SourcePawnEngine2::GetVersionString()
|
|
|
|
{
|
|
|
|
return SOURCEMOD_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
IDebugListener *
|
|
|
|
SourcePawnEngine2::SetDebugListener(IDebugListener *listener)
|
|
|
|
{
|
|
|
|
IDebugListener *old = Environment::get()->debugger();
|
|
|
|
Environment::get()->SetDebugger(listener);
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
SourcePawnEngine2::GetAPIVersion()
|
|
|
|
{
|
|
|
|
return SOURCEPAWN_ENGINE2_API_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
ICompilation *
|
|
|
|
SourcePawnEngine2::StartCompilation()
|
|
|
|
{
|
2015-02-24 09:21:52 +01:00
|
|
|
return nullptr;
|
2015-02-24 07:36:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
SourcePawnEngine2::GetErrorString(int err)
|
|
|
|
{
|
|
|
|
return Environment::get()->GetErrorString(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SourcePawnEngine2::Initialize()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourcePawnEngine2::Shutdown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IPluginRuntime *
|
|
|
|
SourcePawnEngine2::CreateEmptyRuntime(const char *name, uint32_t memory)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
Rewrite the .smx parser.
This removes one the last remnants of the SourceMod 1.0 VM implementation.
The new parser introduces a number of design changes in the VM. First, the VM now takes greater responsibility for validating and sanity checking the structure of the SMX container format. Previously, malformed SMX files could easily crash SourcePawn. The loader now rejects files that have out-of-bounds offsets or incomplete sections. Complex sections, like debug info or the code stream, are verified lazily.
Internally, the sp_plugin_t structure has been removed. It has been replaced by a new LegacyImage class, designed to be independent from the SPVM API. This potentially lets us load code streams from non-.smx containers. More importantly, it removes a lot of bookkeeping and pre-computed state from PluginRuntime. The LegacyImage class is now responsible for handling debug info as well.
PluginRuntime is now intended to hold only cached or immutable data, and PluginContext holds all VM state. As such PluginContext is now responsible for allocating a plugin's runtime memory, not PluginRuntime.
Finally, some aspects of the loading process have been cleaned up. The
decompression and image handoff logic should now be easier to
understand.
2015-02-25 11:19:38 +01:00
|
|
|
ke::AutoPtr<EmptyImage> image(new EmptyImage(memory));
|
|
|
|
|
|
|
|
PluginRuntime *rt = new PluginRuntime(image.take());
|
|
|
|
if (!rt->Initialize()) {
|
2015-02-24 07:36:10 +01:00
|
|
|
delete rt;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
Implement a new stack and error handling model for the SourcePawn VM.
This has three major changes to SourcePawn. First, the API now supports the concept of "exceptions". The exception state is a global property of an instance of the SourcePawn VM. Exceptions can be caught or suppressed. Many places in SourceMod have been updated to check exceptions instead of errors.
The new API obsoletes major parts of the embedder API - all but one method of invoking functions is obsoleted, and the debug interface has been scrapped. Extensions using the native API will not be affected, however, ThrowNativeError has been deprecated in favor of ReportError.
Second, the SourcePawn concept of a "stack" has been unified at the API level. A stack frame iterator now iterates over all SourcePawn invocations, rather than the topmost plugin. This makes error handling more consistent and removes another dependency on context-per-plugin.
Finally, the implementation of stack frames has been changed dramatically. Rather than maintain a complicated and expensive return pointer stack, we now rely on the implicit one provided by the CPU. The stack frame iterator now walks the JIT stack directly. This removes many unnecessary bookkeeping instructions from the generated code, in particular making the CALL instruction 40% faster.
These changes required some fair surgery to the JIT. Its error paths are now slightly more complicated, as they have to throw an exception rather than return an error code. In addition, any path that can throw an exception is now responsible for creating an "exit frame", which exists to tell the stack frame iterator about transitions from the JIT to the VM.
2015-02-27 09:32:44 +01:00
|
|
|
if (!name)
|
|
|
|
name = "<anonymous>";
|
|
|
|
rt->SetNames(name, name);
|
2015-02-24 07:36:10 +01:00
|
|
|
return rt;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SourcePawnEngine2::InstallWatchdogTimer(size_t timeout_ms)
|
|
|
|
{
|
|
|
|
return Environment::get()->InstallWatchdogTimer(timeout_ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SourcePawnEngine2::SetJitEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
Environment::get()->SetJitEnabled(enabled);
|
|
|
|
return Environment::get()->IsJitEnabled() == enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SourcePawnEngine2::IsJitEnabled()
|
|
|
|
{
|
|
|
|
return Environment::get()->IsJitEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourcePawnEngine2::SetProfiler(IProfiler *profiler)
|
|
|
|
{
|
|
|
|
// Deprecated.
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourcePawnEngine2::EnableProfiling()
|
|
|
|
{
|
|
|
|
Environment::get()->EnableProfiling();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourcePawnEngine2::DisableProfiling()
|
|
|
|
{
|
|
|
|
Environment::get()->DisableProfiling();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourcePawnEngine2::SetProfilingTool(IProfilingTool *tool)
|
|
|
|
{
|
|
|
|
Environment::get()->SetProfiler(tool);
|
|
|
|
}
|
Implement a new stack and error handling model for the SourcePawn VM.
This has three major changes to SourcePawn. First, the API now supports the concept of "exceptions". The exception state is a global property of an instance of the SourcePawn VM. Exceptions can be caught or suppressed. Many places in SourceMod have been updated to check exceptions instead of errors.
The new API obsoletes major parts of the embedder API - all but one method of invoking functions is obsoleted, and the debug interface has been scrapped. Extensions using the native API will not be affected, however, ThrowNativeError has been deprecated in favor of ReportError.
Second, the SourcePawn concept of a "stack" has been unified at the API level. A stack frame iterator now iterates over all SourcePawn invocations, rather than the topmost plugin. This makes error handling more consistent and removes another dependency on context-per-plugin.
Finally, the implementation of stack frames has been changed dramatically. Rather than maintain a complicated and expensive return pointer stack, we now rely on the implicit one provided by the CPU. The stack frame iterator now walks the JIT stack directly. This removes many unnecessary bookkeeping instructions from the generated code, in particular making the CALL instruction 40% faster.
These changes required some fair surgery to the JIT. Its error paths are now slightly more complicated, as they have to throw an exception rather than return an error code. In addition, any path that can throw an exception is now responsible for creating an "exit frame", which exists to tell the stack frame iterator about transitions from the JIT to the VM.
2015-02-27 09:32:44 +01:00
|
|
|
|
|
|
|
ISourcePawnEnvironment *
|
|
|
|
SourcePawnEngine2::Environment()
|
|
|
|
{
|
|
|
|
return Environment::get();
|
|
|
|
}
|