v8js_v8.cc 8.3 KB

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