v8js_v8.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | http://www.opensource.org/licenses/mit-license.php MIT License |
  8. +----------------------------------------------------------------------+
  9. | Author: Jani Taskinen <[email protected]> |
  10. | Author: Patrick Reilly <[email protected]> |
  11. | Author: Stefan Siegl <[email protected]> |
  12. +----------------------------------------------------------------------+
  13. */
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif
  17. extern "C" {
  18. #include "php.h"
  19. #include "ext/date/php_date.h"
  20. #include "ext/standard/php_string.h"
  21. #include "zend_interfaces.h"
  22. #include "zend_closures.h"
  23. #include "zend_exceptions.h"
  24. }
  25. #if !defined(_WIN32)
  26. #include <libplatform/libplatform.h>
  27. #endif
  28. #include "php_v8js_macros.h"
  29. #include "v8js_v8.h"
  30. #include "v8js_timer.h"
  31. #include "v8js_exceptions.h"
  32. void v8js_v8_init(TSRMLS_D) /* {{{ */
  33. {
  34. /* Run only once; thread-local test first */
  35. if (V8JSG(v8_initialized)) {
  36. return;
  37. }
  38. /* Set thread-local flag, that V8 was initialized. */
  39. V8JSG(v8_initialized) = 1;
  40. #ifdef ZTS
  41. v8js_process_globals.lock.lock();
  42. if(v8js_process_globals.v8_initialized) {
  43. /* V8 already has been initialized by another thread */
  44. v8js_process_globals.lock.unlock();
  45. return;
  46. }
  47. #endif
  48. #ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA
  49. /* V8 doesn't work without startup data, load it. */
  50. v8::V8::InitializeExternalStartupData(
  51. PHP_V8_NATIVES_BLOB_PATH,
  52. PHP_V8_SNAPSHOT_BLOB_PATH
  53. );
  54. #endif
  55. v8js_process_globals.v8_platform = v8::platform::CreateDefaultPlatform();
  56. v8::V8::InitializePlatform(v8js_process_globals.v8_platform);
  57. /* Set V8 command line flags (must be done before V8::Initialize()!) */
  58. if (v8js_process_globals.v8_flags) {
  59. v8::V8::SetFlagsFromString(v8js_process_globals.v8_flags,
  60. strlen(v8js_process_globals.v8_flags));
  61. }
  62. /* Initialize V8 */
  63. v8::V8::Initialize();
  64. #ifdef ZTS
  65. v8js_process_globals.v8_initialized = 1;
  66. v8js_process_globals.lock.unlock();
  67. #endif
  68. }
  69. /* }}} */
  70. void v8js_v8_call(v8js_ctx *c, zval **return_value,
  71. long flags, long time_limit, long memory_limit,
  72. std::function< v8::Local<v8::Value>(v8::Isolate *) >& v8_call TSRMLS_DC) /* {{{ */
  73. {
  74. char *tz = NULL;
  75. V8JS_CTX_PROLOGUE(c);
  76. V8JSG(timer_mutex).lock();
  77. c->time_limit_hit = false;
  78. c->memory_limit_hit = false;
  79. V8JSG(timer_mutex).unlock();
  80. /* Catch JS exceptions */
  81. v8::TryCatch try_catch;
  82. /* Set flags for runtime use */
  83. c->flags = flags;
  84. /* Check if timezone has been changed and notify V8 */
  85. tz = getenv("TZ");
  86. if (tz != NULL) {
  87. if (c->tz == NULL) {
  88. c->tz = strdup(tz);
  89. }
  90. else if (strcmp(c->tz, tz) != 0) {
  91. v8::Date::DateTimeConfigurationChangeNotification(c->isolate);
  92. free(c->tz);
  93. c->tz = strdup(tz);
  94. }
  95. }
  96. if (time_limit > 0 || memory_limit > 0) {
  97. // If timer thread is not running then start it
  98. if (!V8JSG(timer_thread)) {
  99. // If not, start timer thread
  100. V8JSG(timer_thread) = new std::thread(v8js_timer_thread TSRMLS_CC);
  101. }
  102. }
  103. /* Always pass the timer to the stack so there can be follow-up changes to
  104. * the time & memory limit. */
  105. v8js_timer_push(time_limit, memory_limit, c TSRMLS_CC);
  106. /* Execute script */
  107. c->in_execution++;
  108. v8::Local<v8::Value> result = v8_call(c->isolate);
  109. c->in_execution--;
  110. /* Pop our context from the stack and read (possibly updated) limits
  111. * into local variables. */
  112. V8JSG(timer_mutex).lock();
  113. v8js_timer_ctx *timer_ctx = V8JSG(timer_stack).front();
  114. V8JSG(timer_stack).pop_front();
  115. V8JSG(timer_mutex).unlock();
  116. time_limit = timer_ctx->time_limit;
  117. memory_limit = timer_ctx->memory_limit;
  118. efree(timer_ctx);
  119. /* Check for fatal error marker possibly set by v8js_error_handler; just
  120. * rethrow the error since we're now out of V8. */
  121. if(V8JSG(fatal_error_abort)) {
  122. zend_bailout();
  123. }
  124. char exception_string[64];
  125. if (c->time_limit_hit) {
  126. // Execution has been terminated due to time limit
  127. sprintf(exception_string, "Script time limit of %lu milliseconds exceeded", time_limit);
  128. zend_throw_exception(php_ce_v8js_time_limit_exception, exception_string, 0 TSRMLS_CC);
  129. return;
  130. }
  131. if (memory_limit && !c->memory_limit_hit) {
  132. // Re-check memory limit (very short executions might never be hit by timer thread)
  133. v8::HeapStatistics hs;
  134. isolate->GetHeapStatistics(&hs);
  135. if (hs.used_heap_size() > memory_limit) {
  136. #if PHP_V8_API_VERSION >= 3028036
  137. isolate->LowMemoryNotification();
  138. #else
  139. v8::V8::LowMemoryNotification();
  140. #endif
  141. isolate->GetHeapStatistics(&hs);
  142. if (hs.used_heap_size() > memory_limit) {
  143. c->memory_limit_hit = true;
  144. }
  145. }
  146. }
  147. if (c->memory_limit_hit) {
  148. // Execution has been terminated due to memory limit
  149. sprintf(exception_string, "Script memory limit of %lu bytes exceeded", memory_limit);
  150. zend_throw_exception(php_ce_v8js_memory_limit_exception, exception_string, 0 TSRMLS_CC);
  151. return;
  152. }
  153. if (!try_catch.CanContinue()) {
  154. // At this point we can't re-throw the exception
  155. return;
  156. }
  157. /* There was pending exception left from earlier executions -> throw to PHP */
  158. if (c->pending_exception) {
  159. zend_throw_exception_object(c->pending_exception TSRMLS_CC);
  160. c->pending_exception = NULL;
  161. }
  162. /* Handle runtime JS exceptions */
  163. if (try_catch.HasCaught()) {
  164. /* Pending exceptions are set only in outer caller, inner caller exceptions are always rethrown */
  165. if (c->in_execution < 1) {
  166. /* Report immediately if report_uncaught is true */
  167. if (c->report_uncaught) {
  168. v8js_throw_script_exception(c->isolate, &try_catch TSRMLS_CC);
  169. return;
  170. }
  171. /* Exception thrown from JS, preserve it for future execution */
  172. if (result.IsEmpty()) {
  173. MAKE_STD_ZVAL(c->pending_exception);
  174. v8js_create_script_exception(c->pending_exception, c->isolate, &try_catch TSRMLS_CC);
  175. return;
  176. }
  177. }
  178. /* Rethrow back to JS */
  179. try_catch.ReThrow();
  180. return;
  181. }
  182. /* Convert V8 value to PHP value */
  183. if (!result.IsEmpty()) {
  184. v8js_to_zval(result, *return_value, flags, c->isolate TSRMLS_CC);
  185. }
  186. }
  187. /* }}} */
  188. void v8js_terminate_execution(v8::Isolate *isolate) /* {{{ */
  189. {
  190. if(v8::V8::IsExecutionTerminating(isolate)) {
  191. /* Execution already terminating, needn't trigger it again and
  192. * especially must not execute the spinning loop (which would cause
  193. * crashes in V8 itself, at least with 4.2 and 4.3 version lines). */
  194. return;
  195. }
  196. /* Unfortunately just calling TerminateExecution on the isolate is not
  197. * enough, since v8 just marks the thread as "to be aborted" and doesn't
  198. * immediately do so. Hence we enter an endless loop after signalling
  199. * termination, so we definitely don't execute JS code after the exit()
  200. * statement. */
  201. v8::Locker locker(isolate);
  202. v8::Isolate::Scope isolate_scope(isolate);
  203. v8::HandleScope handle_scope(isolate);
  204. v8::Local<v8::String> source = V8JS_STR("for(;;);");
  205. v8::Local<v8::Script> script = v8::Script::Compile(source);
  206. v8::V8::TerminateExecution(isolate);
  207. script->Run();
  208. }
  209. /* }}} */
  210. int v8js_get_properties_hash(v8::Handle<v8::Value> jsValue, HashTable *retval, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  211. {
  212. v8::Local<v8::Object> jsObj = jsValue->ToObject();
  213. if (!jsObj.IsEmpty()) {
  214. v8::Local<v8::Array> jsKeys = jsObj->GetPropertyNames();
  215. for (unsigned i = 0; i < jsKeys->Length(); i++)
  216. {
  217. v8::Local<v8::String> jsKey = jsKeys->Get(i)->ToString();
  218. /* Skip any prototype properties */
  219. if (!jsObj->HasOwnProperty(jsKey) && !jsObj->HasRealNamedProperty(jsKey) && !jsObj->HasRealNamedCallbackProperty(jsKey)) {
  220. continue;
  221. }
  222. v8::Local<v8::Value> jsVal = jsObj->Get(jsKey);
  223. v8::String::Utf8Value cstr(jsKey);
  224. const char *key = ToCString(cstr);
  225. zval *value = NULL;
  226. if (jsVal->IsObject() && jsVal->ToObject()->InternalFieldCount() == 2) {
  227. value = reinterpret_cast<zval *>(jsVal->ToObject()->GetAlignedPointerFromInternalField(1));
  228. Z_ADDREF_P(value);
  229. }
  230. else {
  231. MAKE_STD_ZVAL(value);
  232. if (v8js_to_zval(jsVal, value, flags, isolate TSRMLS_CC) == FAILURE) {
  233. zval_ptr_dtor(&value);
  234. return FAILURE;
  235. }
  236. }
  237. if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
  238. zend_symtable_update(retval, key, strlen(key) + 1, (void *)&value, sizeof(zval *), NULL);
  239. } else {
  240. zend_hash_update(retval, key, strlen(key) + 1, (void *) &value, sizeof(zval *), NULL);
  241. }
  242. }
  243. return SUCCESS;
  244. }
  245. return FAILURE;
  246. }
  247. /* }}} */
  248. /*
  249. * Local variables:
  250. * tab-width: 4
  251. * c-basic-offset: 4
  252. * indent-tabs-mode: t
  253. * End:
  254. * vim600: noet sw=4 ts=4 fdm=marker
  255. * vim<600: noet sw=4 ts=4
  256. */