v8js_v8.cc 8.5 KB

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