2014-12-12 22:59:28 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2017-03-11 11:33:49 +00:00
|
|
|
| PHP Version 7 |
|
2014-12-12 22:59:28 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2016-02-28 19:17:20 +00:00
|
|
|
| Copyright (c) 1997-2016 The PHP Group |
|
2014-12-12 22:59:28 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
|
|
|
| Author: Patrick Reilly <preilly@php.net> |
|
2015-08-23 13:09:21 +00:00
|
|
|
| Author: Stefan Siegl <stesie@php.net> |
|
2014-12-12 22:59:28 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2016-01-06 09:01:35 +00:00
|
|
|
#include "php_v8js_macros.h"
|
|
|
|
#include "v8js_v8.h"
|
|
|
|
#include "v8js_timer.h"
|
|
|
|
#include "v8js_exceptions.h"
|
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2015-08-02 16:15:04 +00:00
|
|
|
#include <libplatform/libplatform.h>
|
2016-02-28 19:17:20 +00:00
|
|
|
|
|
|
|
|
2017-03-11 12:45:23 +00:00
|
|
|
void v8js_v8_init() /* {{{ */
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
2015-09-26 16:58:12 +00:00
|
|
|
/* Run only once; thread-local test first */
|
2014-12-12 22:59:28 +00:00
|
|
|
if (V8JSG(v8_initialized)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-26 16:58:12 +00:00
|
|
|
/* 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
|
|
|
|
|
2016-12-05 16:28:59 +00:00
|
|
|
#if defined(PHP_V8_NATIVES_BLOB_PATH) && defined(PHP_V8_SNAPSHOT_BLOB_PATH)
|
2016-02-28 19:17:20 +00:00
|
|
|
/* V8 doesn't work without startup data, load it. */
|
2016-02-28 16:48:44 +00:00
|
|
|
v8::V8::InitializeExternalStartupData(
|
|
|
|
PHP_V8_NATIVES_BLOB_PATH,
|
|
|
|
PHP_V8_SNAPSHOT_BLOB_PATH
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
2015-09-26 16:58:12 +00:00
|
|
|
v8js_process_globals.v8_platform = v8::platform::CreateDefaultPlatform();
|
|
|
|
v8::V8::InitializePlatform(v8js_process_globals.v8_platform);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Set V8 command line flags (must be done before V8::Initialize()!) */
|
2015-09-26 19:39:26 +00:00
|
|
|
if (v8js_process_globals.v8_flags) {
|
2017-03-10 22:53:11 +00:00
|
|
|
size_t flags_len = strlen(v8js_process_globals.v8_flags);
|
|
|
|
|
2017-03-11 09:14:30 +00:00
|
|
|
if (flags_len > std::numeric_limits<int>::max()) {
|
2017-03-10 22:53:11 +00:00
|
|
|
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));
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 16:20:12 +00:00
|
|
|
#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);
|
|
|
|
}
|
2017-04-14 12:27:01 +00:00
|
|
|
#endif
|
2017-04-21 16:20:12 +00:00
|
|
|
#endif /* PHP_V8_API_VERSION >= 5003178 */
|
2017-04-14 12:27:01 +00:00
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
/* Initialize V8 */
|
|
|
|
v8::V8::Initialize();
|
|
|
|
|
2015-09-26 16:58:12 +00:00
|
|
|
#ifdef ZTS
|
|
|
|
v8js_process_globals.v8_initialized = 1;
|
|
|
|
v8js_process_globals.lock.unlock();
|
|
|
|
#endif
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
*/
|
2014-12-13 00:15:29 +00:00
|
|
|
void v8js_v8_call(v8js_ctx *c, zval **return_value,
|
2017-03-11 13:10:49 +00:00
|
|
|
long flags, long time_limit, size_t memory_limit,
|
2018-09-12 13:43:40 +00:00
|
|
|
std::function< v8::MaybeLocal<v8::Value>(v8::Isolate *) >& v8_call) /* {{{ */
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
|
|
|
char *tz = NULL;
|
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
V8JS_CTX_PROLOGUE(c);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
V8JSG(timer_mutex).lock();
|
|
|
|
c->time_limit_hit = false;
|
|
|
|
c->memory_limit_hit = false;
|
|
|
|
V8JSG(timer_mutex).unlock();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* Catch JS exceptions */
|
2017-04-24 18:43:52 +00:00
|
|
|
v8::TryCatch try_catch(isolate);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* Set flags for runtime use */
|
|
|
|
c->flags = flags;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* Check if timezone has been changed and notify V8 */
|
|
|
|
tz = getenv("TZ");
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
if (tz != NULL) {
|
|
|
|
if (c->tz == NULL) {
|
|
|
|
c->tz = strdup(tz);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
2016-01-01 18:10:04 +00:00
|
|
|
else if (strcmp(c->tz, tz) != 0) {
|
|
|
|
v8::Date::DateTimeConfigurationChangeNotification(c->isolate);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
free(c->tz);
|
|
|
|
c->tz = strdup(tz);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
2016-01-01 18:10:04 +00:00
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* Always pass the timer to the stack so there can be follow-up changes to
|
|
|
|
* the time & memory limit. */
|
2017-03-11 12:35:21 +00:00
|
|
|
v8js_timer_push(time_limit, memory_limit, c);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* Execute script */
|
|
|
|
c->in_execution++;
|
2018-09-12 13:43:40 +00:00
|
|
|
v8::MaybeLocal<v8::Value> result = v8_call(c->isolate);
|
2016-01-01 18:10:04 +00:00
|
|
|
c->in_execution--;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* 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();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
time_limit = timer_ctx->time_limit;
|
|
|
|
memory_limit = timer_ctx->memory_limit;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
efree(timer_ctx);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
if(!V8JSG(fatal_error_abort)) {
|
|
|
|
char exception_string[64];
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
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);
|
2017-03-11 12:35:21 +00:00
|
|
|
zend_throw_exception(php_ce_v8js_time_limit_exception, exception_string, 0);
|
2016-01-01 18:10:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-05-22 20:30:19 +00:00
|
|
|
if (memory_limit && !c->memory_limit_hit) {
|
|
|
|
// Re-check memory limit (very short executions might never be hit by timer thread)
|
|
|
|
v8::HeapStatistics hs;
|
2016-03-25 23:53:52 +00:00
|
|
|
isolate->GetHeapStatistics(&hs);
|
|
|
|
|
|
|
|
if (hs.used_heap_size() > memory_limit) {
|
2016-05-22 20:30:19 +00:00
|
|
|
isolate->LowMemoryNotification();
|
|
|
|
isolate->GetHeapStatistics(&hs);
|
|
|
|
|
|
|
|
if (hs.used_heap_size() > memory_limit) {
|
|
|
|
c->memory_limit_hit = true;
|
|
|
|
}
|
2016-03-25 23:53:52 +00:00
|
|
|
}
|
2016-03-25 23:37:53 +00:00
|
|
|
}
|
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
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);
|
2017-03-11 12:35:21 +00:00
|
|
|
zend_throw_exception(php_ce_v8js_memory_limit_exception, exception_string, 0);
|
2016-01-01 18:10:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
if (!try_catch.CanContinue()) {
|
|
|
|
// At this point we can't re-throw the exception
|
|
|
|
return;
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* There was pending exception left from earlier executions -> throw to PHP */
|
|
|
|
if (Z_TYPE(c->pending_exception) == IS_OBJECT) {
|
2017-03-11 12:35:21 +00:00
|
|
|
zend_throw_exception_object(&c->pending_exception);
|
2016-01-01 18:10:04 +00:00
|
|
|
ZVAL_NULL(&c->pending_exception);
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* Handle runtime JS exceptions */
|
|
|
|
if (try_catch.HasCaught()) {
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* Pending exceptions are set only in outer caller, inner caller exceptions are always rethrown */
|
|
|
|
if (c->in_execution < 1) {
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* Report immediately if report_uncaught is true */
|
|
|
|
if (c->report_uncaught) {
|
2017-03-11 12:35:21 +00:00
|
|
|
v8js_throw_script_exception(c->isolate, &try_catch);
|
2016-01-01 18:10:04 +00:00
|
|
|
return;
|
2015-12-31 22:44:01 +00:00
|
|
|
}
|
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* Exception thrown from JS, preserve it for future execution */
|
|
|
|
if (result.IsEmpty()) {
|
2017-03-11 12:35:21 +00:00
|
|
|
v8js_create_script_exception(&c->pending_exception, c->isolate, &try_catch);
|
2016-01-01 18:10:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* Rethrow back to JS */
|
|
|
|
try_catch.ReThrow();
|
|
|
|
return;
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* Convert V8 value to PHP value */
|
2016-01-07 20:35:44 +00:00
|
|
|
if (return_value && !result.IsEmpty()) {
|
2018-09-12 13:43:40 +00:00
|
|
|
v8js_to_zval(result.ToLocalChecked(), *return_value, flags, c->isolate);
|
2016-01-01 18:10:04 +00:00
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-08-21 13:55:52 +00:00
|
|
|
void v8js_terminate_execution(v8::Isolate *isolate) /* {{{ */
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
2017-04-24 18:43:52 +00:00
|
|
|
if(isolate->IsExecutionTerminating()) {
|
2015-09-22 20:36:50 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2015-08-21 13:55:52 +00:00
|
|
|
/* 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);
|
|
|
|
|
2018-09-12 13:43:40 +00:00
|
|
|
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
|
|
|
|
v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, ctx->context);
|
|
|
|
|
2015-08-21 13:55:52 +00:00
|
|
|
v8::Local<v8::String> source = V8JS_STR("for(;;);");
|
2018-09-12 13:43:40 +00:00
|
|
|
v8::Local<v8::Script> script = v8::Script::Compile(context, source).ToLocalChecked();
|
2017-04-24 18:43:52 +00:00
|
|
|
isolate->TerminateExecution();
|
2018-09-12 13:43:40 +00:00
|
|
|
script->Run(context);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-12 23:28:09 +00:00
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
int v8js_get_properties_hash(v8::Local<v8::Value> jsValue, HashTable *retval, int flags, v8::Isolate *isolate) /* {{{ */
|
2014-12-12 23:28:09 +00:00
|
|
|
{
|
2018-09-13 12:01:15 +00:00
|
|
|
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
|
|
|
|
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, ctx->context);
|
2014-12-12 23:28:09 +00:00
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-12-12 23:28:09 +00:00
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
for (unsigned i = 0; i < jsKeys->Length(); i++)
|
|
|
|
{
|
|
|
|
v8::Local<v8::Value> jsKeySlot;
|
|
|
|
v8::Local<v8::String> jsKey;
|
2014-12-12 23:28:09 +00:00
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
if (!jsKeys->Get(v8_context, i).ToLocal(&jsKeySlot)
|
|
|
|
|| !jsKeySlot->ToString(v8_context).ToLocal(&jsKey)) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-12 23:28:09 +00:00
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
/* Skip any prototype properties */
|
|
|
|
if (!jsObj->HasOwnProperty(isolate->GetEnteredContext(), jsKey).FromMaybe(false)
|
|
|
|
&& !jsObj->HasRealNamedProperty(v8_context, jsKey).FromMaybe(false)
|
|
|
|
&& !jsObj->HasRealNamedCallbackProperty(v8_context, jsKey).FromMaybe(false)) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-12 23:28:09 +00:00
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
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;
|
2014-12-12 23:28:09 +00:00
|
|
|
}
|
2018-09-13 12:01:15 +00:00
|
|
|
}
|
2015-08-29 22:26:55 +00:00
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
|
|
|
|
zend_symtable_update(retval, key, &value);
|
|
|
|
} else {
|
|
|
|
zend_hash_update(retval, key, &value);
|
2014-12-12 23:28:09 +00:00
|
|
|
}
|
2018-09-13 12:01:15 +00:00
|
|
|
|
|
|
|
zend_string_release(key);
|
2014-12-12 23:28:09 +00:00
|
|
|
}
|
2018-09-13 12:01:15 +00:00
|
|
|
return SUCCESS;
|
2014-12-12 23:28:09 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
2016-07-02 20:41:01 +00:00
|
|
|
* indent-tabs-mode: t
|
2014-12-12 22:59:28 +00:00
|
|
|
* End:
|
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
|
|
|
*/
|