v8js_v8.cc 7.9 KB

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