-
Notifications
You must be signed in to change notification settings - Fork 201
/
v8js_v8.cc
381 lines (312 loc) · 10.9 KB
/
v8js_v8.cc
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| http://www.opensource.org/licenses/mit-license.php MIT License |
+----------------------------------------------------------------------+
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
| Author: Patrick Reilly <preilly@php.net> |
| Author: Stefan Siegl <stesie@php.net> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php_v8js_macros.h"
#include "v8js_v8.h"
#include "v8js_timer.h"
#include "v8js_exceptions.h"
extern "C" {
#include "ext/date/php_date.h"
#include "ext/standard/php_string.h"
#include "zend_interfaces.h"
#include "zend_closures.h"
#include "zend_exceptions.h"
}
#include <libplatform/libplatform.h>
void v8js_v8_init() /* {{{ */
{
/* Run only once; thread-local test first */
if (V8JSG(v8_initialized)) {
return;
}
/* Set thread-local flag, that V8 was initialized. */
V8JSG(v8_initialized) = 1;
#ifdef ZTS
v8js_process_globals.lock.lock();
if(v8js_process_globals.v8_initialized) {
/* V8 already has been initialized by another thread */
v8js_process_globals.lock.unlock();
return;
}
#endif
#if defined(PHP_V8_SNAPSHOT_BLOB_PATH)
#if !defined(PHP_V8_NATIVES_BLOB_PATH)
/* Newer V8 version don't have a natives blob anymore. */
v8::V8::InitializeExternalStartupDataFromFile(
PHP_V8_SNAPSHOT_BLOB_PATH
);
#else
/* V8 doesn't work without startup data, load it. */
v8::V8::InitializeExternalStartupData(
PHP_V8_NATIVES_BLOB_PATH,
PHP_V8_SNAPSHOT_BLOB_PATH
);
#endif
#endif
v8js_process_globals.v8_platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(v8js_process_globals.v8_platform.get());
#ifdef V8_HAS_INITIALIZE_SANDBOX
v8::V8::InitializeSandbox();
#endif
/* Set V8 command line flags (must be done before V8::Initialize()!) */
if (v8js_process_globals.v8_flags) {
size_t flags_len = strlen(v8js_process_globals.v8_flags);
if (flags_len > std::numeric_limits<int>::max()) {
zend_throw_exception(php_ce_v8js_exception,
"Length of V8 flags exceeds maximum supported length", 0);
}
else {
v8::V8::SetFlagsFromString(v8js_process_globals.v8_flags, static_cast<int>(flags_len));
}
}
#if PHP_V8_API_VERSION >= 5003178
/* Initialize ICU, call introduced in V8 5.3.178 */
if (v8js_process_globals.icudtl_dat_path != NULL && v8js_process_globals.icudtl_dat_path[0] != 0) {
v8::V8::InitializeICUDefaultLocation(nullptr, v8js_process_globals.icudtl_dat_path);
}
#ifdef PHP_V8_EXEC_PATH
else {
v8::V8::InitializeICUDefaultLocation(PHP_V8_EXEC_PATH, nullptr);
}
#endif
#endif /* PHP_V8_API_VERSION >= 5003178 */
/* Initialize V8 */
v8::V8::Initialize();
#ifdef ZTS
v8js_process_globals.v8_initialized = 1;
v8js_process_globals.lock.unlock();
#endif
}
/* }}} */
/**
* Prepare V8 call trampoline with time & memory limit, exception handling, etc.
*
* The caller MUST check V8JSG(fatal_error_abort) and trigger further bailout
* either immediately after this function returns (or possibly after freeing
* heap allocated memory).
*/
void v8js_v8_call(v8js_ctx *c, zval **return_value,
long flags, long time_limit, size_t memory_limit,
std::function< v8::MaybeLocal<v8::Value>(v8::Isolate *) >& v8_call) /* {{{ */
{
char *tz = NULL;
// hold extra reference on v8 instance as long as we call into V8 (issue #472)
zend_object *obj = v8js_ctx_to_zend_object(c);
zval zv_v8inst;
ZVAL_OBJ(&zv_v8inst, obj);
Z_ADDREF_P(&zv_v8inst);
{
V8JS_CTX_PROLOGUE(c);
V8JSG(timer_mutex).lock();
c->time_limit_hit = false;
c->memory_limit_hit = false;
V8JSG(timer_mutex).unlock();
/* Catch JS exceptions */
v8::TryCatch try_catch(isolate);
/* Set flags for runtime use */
c->flags = flags;
/* Check if timezone has been changed and notify V8 */
tz = getenv("TZ");
if (tz != NULL) {
if (c->tz == NULL) {
c->tz = strdup(tz);
}
else if (strcmp(c->tz, tz) != 0) {
c->isolate->DateTimeConfigurationChangeNotification(v8::Isolate::TimeZoneDetection::kRedetect);
free(c->tz);
c->tz = strdup(tz);
}
}
if (time_limit > 0 || memory_limit > 0) {
// If timer thread is not running then start it
if (!V8JSG(timer_thread)) {
// If not, start timer thread
V8JSG(timer_thread) = new std::thread(v8js_timer_thread, ZEND_MODULE_GLOBALS_BULK(v8js));
}
}
/* Always pass the timer to the stack so there can be follow-up changes to
* the time & memory limit. */
v8js_timer_push(time_limit, memory_limit, c);
/* Execute script */
c->in_execution++;
v8::MaybeLocal<v8::Value> result = v8_call(c->isolate);
c->in_execution--;
/* Pop our context from the stack and read (possibly updated) limits
* into local variables. */
V8JSG(timer_mutex).lock();
v8js_timer_ctx *timer_ctx = V8JSG(timer_stack).front();
V8JSG(timer_stack).pop_front();
V8JSG(timer_mutex).unlock();
time_limit = timer_ctx->time_limit;
memory_limit = timer_ctx->memory_limit;
efree(timer_ctx);
if(!V8JSG(fatal_error_abort)) {
char exception_string[64];
if (c->time_limit_hit) {
// Execution has been terminated due to time limit
sprintf(exception_string, "Script time limit of %lu milliseconds exceeded", time_limit);
zend_throw_exception(php_ce_v8js_time_limit_exception, exception_string, 0);
zval_ptr_dtor(&zv_v8inst);
return;
}
if (memory_limit && !c->memory_limit_hit) {
// Re-check memory limit (very short executions might never be hit by timer thread)
v8::HeapStatistics hs;
isolate->GetHeapStatistics(&hs);
if (hs.used_heap_size() > memory_limit) {
isolate->LowMemoryNotification();
isolate->GetHeapStatistics(&hs);
if (hs.used_heap_size() > memory_limit) {
c->memory_limit_hit = true;
}
}
}
if (c->memory_limit_hit) {
// Execution has been terminated due to memory limit
sprintf(exception_string, "Script memory limit of %lu bytes exceeded", memory_limit);
zend_throw_exception(php_ce_v8js_memory_limit_exception, exception_string, 0);
zval_ptr_dtor(&zv_v8inst);
return;
}
if (!try_catch.CanContinue()) {
// At this point we can't re-throw the exception
zval_ptr_dtor(&zv_v8inst);
return;
}
/* There was pending exception left from earlier executions -> throw to PHP */
if (Z_TYPE(c->pending_exception) == IS_OBJECT) {
zend_throw_exception_object(&c->pending_exception);
ZVAL_NULL(&c->pending_exception);
}
/* Handle runtime JS exceptions */
if (try_catch.HasCaught()) {
/* Pending exceptions are set only in outer caller, inner caller exceptions are always rethrown */
if (c->in_execution < 1) {
/* Report immediately if report_uncaught is true */
if (c->report_uncaught) {
v8js_throw_script_exception(c->isolate, &try_catch);
zval_ptr_dtor(&zv_v8inst);
return;
}
/* Exception thrown from JS, preserve it for future execution */
if (result.IsEmpty()) {
v8js_create_script_exception(&c->pending_exception, c->isolate, &try_catch);
zval_ptr_dtor(&zv_v8inst);
return;
}
}
/* Rethrow back to JS */
try_catch.ReThrow();
zval_ptr_dtor(&zv_v8inst);
return;
}
/* Convert V8 value to PHP value */
if (return_value && !result.IsEmpty()) {
v8js_to_zval(result.ToLocalChecked(), *return_value, flags, c->isolate);
}
}
}
zval_ptr_dtor(&zv_v8inst);
}
/* }}} */
void v8js_terminate_execution(v8::Isolate *isolate) /* {{{ */
{
if(isolate->IsExecutionTerminating()) {
/* Execution already terminating, needn't trigger it again and
* especially must not execute the spinning loop (which would cause
* crashes in V8 itself, at least with 4.2 and 4.3 version lines). */
return;
}
/* Unfortunately just calling TerminateExecution on the isolate is not
* enough, since v8 just marks the thread as "to be aborted" and doesn't
* immediately do so. Hence we enter an endless loop after signalling
* termination, so we definitely don't execute JS code after the exit()
* statement. */
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, ctx->context);
v8::Local<v8::String> source = V8JS_STR("for(;;);");
v8::Local<v8::Script> script = v8::Script::Compile(context, source).ToLocalChecked();
isolate->TerminateExecution();
script->Run(context);
}
/* }}} */
int v8js_get_properties_hash(v8::Local<v8::Value> jsValue, HashTable *retval, int flags, v8::Isolate *isolate) /* {{{ */
{
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, ctx->context);
v8::Local<v8::Object> jsObj;
v8::Local<v8::Array> jsKeys;
if (!jsValue->ToObject(v8_context).ToLocal(&jsObj)
|| !jsObj->GetPropertyNames(v8_context).ToLocal(&jsKeys)) {
return FAILURE;
}
for (unsigned i = 0; i < jsKeys->Length(); i++)
{
v8::Local<v8::Value> jsKeySlot;
v8::Local<v8::String> jsKey;
if (!jsKeys->Get(v8_context, i).ToLocal(&jsKeySlot)
|| !jsKeySlot->ToString(v8_context).ToLocal(&jsKey)) {
continue;
}
/* Skip any prototype properties */
if (!jsObj->HasOwnProperty(isolate->GetEnteredOrMicrotaskContext(), jsKey).FromMaybe(false)
&& !jsObj->HasRealNamedProperty(v8_context, jsKey).FromMaybe(false)
&& !jsObj->HasRealNamedCallbackProperty(v8_context, jsKey).FromMaybe(false)) {
continue;
}
v8::Local<v8::Value> jsVal;
if (!jsObj->Get(v8_context, jsKey).ToLocal(&jsVal)) {
continue;
}
v8::String::Utf8Value cstr(isolate, jsKey);
zend_string *key = zend_string_init(ToCString(cstr), cstr.length(), 0);
zval value;
ZVAL_UNDEF(&value);
v8::Local<v8::Object> jsValObject;
if (jsVal->IsObject() && jsVal->ToObject(v8_context).ToLocal(&jsValObject) && jsValObject->InternalFieldCount() == 2) {
/* This is a PHP object, passed to JS and back. */
zend_object *object = reinterpret_cast<zend_object *>(jsValObject->GetAlignedPointerFromInternalField(1));
ZVAL_OBJ(&value, object);
Z_ADDREF_P(&value);
}
else {
if (v8js_to_zval(jsVal, &value, flags, isolate) == FAILURE) {
zval_ptr_dtor(&value);
return FAILURE;
}
}
if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
zend_symtable_update(retval, key, &value);
} else {
zend_hash_update(retval, key, &value);
}
zend_string_release(key);
}
return SUCCESS;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/