/
engine2.cpp
212 lines (174 loc) · 4.21 KB
/
engine2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// vim: set ts=4 sw=4 tw=99 noet:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "engine2.h"
#include "x86/jit_x86.h"
#include "zlib/zlib.h"
#include "BaseRuntime.h"
#include "sp_vm_engine.h"
#include "watchdog_timer.h"
#include <sourcemod_version.h>
using namespace SourcePawn;
SourcePawnEngine2::SourcePawnEngine2()
{
profiler_ = NULL;
jit_enabled_ = true;
}
IPluginRuntime *SourcePawnEngine2::LoadPlugin(ICompilation *co, const char *file, int *err)
{
sp_file_hdr_t hdr;
uint8_t *base;
int z_result;
int error;
BaseRuntime *pRuntime;
FILE *fp = fopen(file, "rb");
if (!fp)
{
error = SP_ERROR_NOT_FOUND;
goto return_error;
}
/* Rewind for safety */
fread(&hdr, sizeof(sp_file_hdr_t), 1, fp);
if (hdr.magic != SmxConsts::FILE_MAGIC)
{
error = SP_ERROR_FILE_FORMAT;
goto return_error;
}
switch (hdr.compression)
{
case SmxConsts::FILE_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 SmxConsts::FILE_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;
}
}
pRuntime = new BaseRuntime();
if ((error = pRuntime->CreateFromMemory(&hdr, base)) != SP_ERROR_NONE)
{
delete pRuntime;
goto return_error;
}
size_t len;
len = strlen(file);
for (size_t i = len - 1; i < len; i--)
{
if (file[i] == '/'
#if defined WIN32
|| file[i] == '\\'
#endif
)
{
pRuntime->SetName(&file[i+1]);
break;
}
}
if (!pRuntime->plugin()->name)
{
pRuntime->SetName(file);
}
pRuntime->ApplyCompilationOptions(co);
fclose(fp);
return pRuntime;
return_error:
*err = error;
if (fp != NULL)
{
fclose(fp);
}
return NULL;
}
SPVM_NATIVE_FUNC SourcePawnEngine2::CreateFakeNative(SPVM_FAKENATIVE_FUNC callback, void *pData)
{
return g_Jit.CreateFakeNative(callback, pData);
}
void SourcePawnEngine2::DestroyFakeNative(SPVM_NATIVE_FUNC func)
{
g_Jit.DestroyFakeNative(func);
}
const char *SourcePawnEngine2::GetEngineName()
{
return "SourcePawn 1.7, jit-x86";
}
const char *SourcePawnEngine2::GetVersionString()
{
return SOURCEMOD_VERSION;
}
IDebugListener *SourcePawnEngine2::SetDebugListener(IDebugListener *listener)
{
return g_engine1.SetDebugListener(listener);
}
unsigned int SourcePawnEngine2::GetAPIVersion()
{
return SOURCEPAWN_ENGINE2_API_VERSION;
}
ICompilation *SourcePawnEngine2::StartCompilation()
{
return g_Jit.StartCompilation();
}
const char *SourcePawnEngine2::GetErrorString(int err)
{
return g_engine1.GetErrorString(err);
}
bool SourcePawnEngine2::Initialize()
{
return g_Jit.InitializeJIT();
}
void SourcePawnEngine2::Shutdown()
{
g_WatchdogTimer.Shutdown();
g_Jit.ShutdownJIT();
}
IPluginRuntime *SourcePawnEngine2::CreateEmptyRuntime(const char *name, uint32_t memory)
{
int err;
BaseRuntime *rt;
rt = new BaseRuntime();
if ((err = rt->CreateBlank(memory)) != SP_ERROR_NONE)
{
delete rt;
return NULL;
}
rt->SetName(name != NULL ? name : "<anonymous>");
rt->ApplyCompilationOptions(NULL);
return rt;
}
bool SourcePawnEngine2::InstallWatchdogTimer(size_t timeout_ms)
{
return g_WatchdogTimer.Initialize(timeout_ms);
}