v8js_v8.cc 8.7 KB

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