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
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Copyright (c) 1997-2013 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> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2016-01-06 09:10:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-06 09:01:35 +00:00
|
|
|
#include <concrt.h>
|
2016-01-06 09:10:22 +00:00
|
|
|
#endif
|
2016-01-06 09:01:35 +00:00
|
|
|
|
|
|
|
#include "php_v8js_macros.h"
|
|
|
|
#include "v8js_v8.h"
|
|
|
|
#include "v8js_exceptions.h"
|
|
|
|
#include "v8js_timer.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 "ext/spl/spl_exceptions.h"
|
|
|
|
#include "zend_exceptions.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
static void v8js_timer_interrupt_handler(v8::Isolate *isolate, void *data) { /* {{{ */
|
2015-09-28 20:03:24 +00:00
|
|
|
zend_v8js_globals *globals = static_cast<zend_v8js_globals *>(data);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2015-09-28 20:03:24 +00:00
|
|
|
if (!globals->timer_stack.size()) {
|
2014-12-12 22:59:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
v8::HeapStatistics hs;
|
2016-03-26 11:10:32 +00:00
|
|
|
bool send_notification = false;
|
|
|
|
bool has_sent_notification = false;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-03-25 23:53:52 +00:00
|
|
|
do {
|
|
|
|
if (send_notification) {
|
|
|
|
isolate->LowMemoryNotification();
|
|
|
|
has_sent_notification = true;
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-03-25 23:53:52 +00:00
|
|
|
isolate->GetHeapStatistics(&hs);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-05-22 20:30:19 +00:00
|
|
|
globals->timer_mutex.lock();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-05-22 20:30:19 +00:00
|
|
|
for (std::deque< v8js_timer_ctx* >::iterator it = globals->timer_stack.begin();
|
|
|
|
it != globals->timer_stack.end(); it ++) {
|
2016-03-25 23:53:52 +00:00
|
|
|
v8js_timer_ctx *timer_ctx = *it;
|
|
|
|
v8js_ctx *c = timer_ctx->ctx;
|
|
|
|
|
|
|
|
if(c->isolate != isolate || timer_ctx->killed) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timer_ctx->memory_limit > 0 && hs.used_heap_size() > timer_ctx->memory_limit) {
|
|
|
|
if (has_sent_notification) {
|
|
|
|
timer_ctx->killed = true;
|
|
|
|
v8::V8::TerminateExecution(c->isolate);
|
|
|
|
c->memory_limit_hit = true;
|
|
|
|
} else {
|
|
|
|
// force garbage collection, then check again
|
|
|
|
send_notification = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 20:30:19 +00:00
|
|
|
globals->timer_mutex.unlock();
|
2016-03-25 23:53:52 +00:00
|
|
|
} while(send_notification != has_sent_notification);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-09-28 20:03:24 +00:00
|
|
|
void v8js_timer_thread(zend_v8js_globals *globals) /* {{{ */
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
2015-09-28 20:03:24 +00:00
|
|
|
while (!globals->timer_stop) {
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2015-09-28 20:03:24 +00:00
|
|
|
globals->timer_mutex.lock();
|
|
|
|
if (globals->timer_stack.size()) {
|
|
|
|
v8js_timer_ctx *timer_ctx = globals->timer_stack.front();
|
2014-12-13 00:15:29 +00:00
|
|
|
v8js_ctx *c = timer_ctx->ctx;
|
2014-12-12 22:59:28 +00:00
|
|
|
std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
if(timer_ctx->killed) {
|
|
|
|
/* execution already terminated, nothing to check anymore,
|
|
|
|
* but wait for caller to pop this timer context. */
|
|
|
|
}
|
|
|
|
else if(timer_ctx->time_limit > 0 && now > timer_ctx->time_point) {
|
|
|
|
timer_ctx->killed = true;
|
2015-08-21 13:55:52 +00:00
|
|
|
v8::V8::TerminateExecution(c->isolate);
|
2014-12-12 22:59:28 +00:00
|
|
|
c->time_limit_hit = true;
|
|
|
|
}
|
|
|
|
else if (timer_ctx->memory_limit > 0) {
|
|
|
|
/* If a memory_limit is set, we need to interrupt execution
|
|
|
|
* and check heap size within the callback. We must *not*
|
|
|
|
* directly call GetHeapStatistics here, since we don't have
|
|
|
|
* a v8::Locker on the isolate, but are expected to hold one,
|
|
|
|
* and cannot aquire it as v8 is executing the script ... */
|
2015-09-28 20:03:24 +00:00
|
|
|
c->isolate->RequestInterrupt(v8js_timer_interrupt_handler, static_cast<void *>(globals));
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-28 20:03:24 +00:00
|
|
|
globals->timer_mutex.unlock();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
// Sleep for 10ms
|
|
|
|
#ifdef _WIN32
|
|
|
|
concurrency::wait(10);
|
|
|
|
#else
|
|
|
|
std::chrono::milliseconds duration(10);
|
|
|
|
std::this_thread::sleep_for(duration);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
2017-03-11 13:10:49 +00:00
|
|
|
void v8js_timer_push(long time_limit, size_t memory_limit, v8js_ctx *c) /* {{{ */
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
|
|
|
V8JSG(timer_mutex).lock();
|
|
|
|
|
|
|
|
// Create context for this timer
|
2014-12-13 00:15:29 +00:00
|
|
|
v8js_timer_ctx *timer_ctx = (v8js_timer_ctx *)emalloc(sizeof(v8js_timer_ctx));
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
// Calculate the time point when the time limit is exceeded
|
|
|
|
std::chrono::milliseconds duration(time_limit);
|
|
|
|
std::chrono::time_point<std::chrono::high_resolution_clock> from = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
// Push the timer context
|
|
|
|
timer_ctx->time_limit = time_limit;
|
|
|
|
timer_ctx->memory_limit = memory_limit;
|
|
|
|
timer_ctx->time_point = from + duration;
|
2014-12-13 00:15:29 +00:00
|
|
|
timer_ctx->ctx = c;
|
2014-12-12 22:59:28 +00:00
|
|
|
timer_ctx->killed = false;
|
|
|
|
V8JSG(timer_stack).push_front(timer_ctx);
|
|
|
|
|
|
|
|
V8JSG(timer_mutex).unlock();
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|