v8js_v8.cc 8.4 KB

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