v8js_v8.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #include "php_v8js_macros.h"
  18. #include "v8js_v8.h"
  19. #include "v8js_timer.h"
  20. #include "v8js_exceptions.h"
  21. extern "C" {
  22. #include "ext/date/php_date.h"
  23. #include "ext/standard/php_string.h"
  24. #include "zend_interfaces.h"
  25. #include "zend_closures.h"
  26. #include "zend_exceptions.h"
  27. }
  28. #if PHP_V8_API_VERSION >= 3029036
  29. #include <libplatform/libplatform.h>
  30. #endif
  31. void v8js_v8_init(TSRMLS_D) /* {{{ */
  32. {
  33. /* Run only once; thread-local test first */
  34. if (V8JSG(v8_initialized)) {
  35. return;
  36. }
  37. /* Set thread-local flag, that V8 was initialized. */
  38. V8JSG(v8_initialized) = 1;
  39. #ifdef ZTS
  40. v8js_process_globals.lock.lock();
  41. if(v8js_process_globals.v8_initialized) {
  42. /* V8 already has been initialized by another thread */
  43. v8js_process_globals.lock.unlock();
  44. return;
  45. }
  46. #endif
  47. #if PHP_V8_API_VERSION >= 3029036
  48. v8js_process_globals.v8_platform = v8::platform::CreateDefaultPlatform();
  49. v8::V8::InitializePlatform(v8js_process_globals.v8_platform);
  50. #endif
  51. /* Set V8 command line flags (must be done before V8::Initialize()!) */
  52. if (v8js_process_globals.v8_flags) {
  53. v8::V8::SetFlagsFromString(v8js_process_globals.v8_flags,
  54. strlen(v8js_process_globals.v8_flags));
  55. }
  56. /* Initialize V8 */
  57. v8::V8::Initialize();
  58. #ifdef ZTS
  59. v8js_process_globals.v8_initialized = 1;
  60. v8js_process_globals.lock.unlock();
  61. #endif
  62. }
  63. /* }}} */
  64. /**
  65. * Prepare V8 call trampoline with time & memory limit, exception handling, etc.
  66. *
  67. * The caller MUST check V8JSG(fatal_error_abort) and trigger further bailout
  68. * either immediately after this function returns (or possibly after freeing
  69. * heap allocated memory).
  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, ZEND_MODULE_GLOBALS_BULK(v8js));
  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. if(!V8JSG(fatal_error_abort)) {
  121. char exception_string[64];
  122. if (c->time_limit_hit) {
  123. // Execution has been terminated due to time limit
  124. sprintf(exception_string, "Script time limit of %lu milliseconds exceeded", time_limit);
  125. zend_throw_exception(php_ce_v8js_time_limit_exception, exception_string, 0 TSRMLS_CC);
  126. return;
  127. }
  128. if (c->memory_limit_hit) {
  129. // Execution has been terminated due to memory limit
  130. sprintf(exception_string, "Script memory limit of %lu bytes exceeded", memory_limit);
  131. zend_throw_exception(php_ce_v8js_memory_limit_exception, exception_string, 0 TSRMLS_CC);
  132. return;
  133. }
  134. if (!try_catch.CanContinue()) {
  135. // At this point we can't re-throw the exception
  136. return;
  137. }
  138. /* There was pending exception left from earlier executions -> throw to PHP */
  139. if (Z_TYPE(c->pending_exception) == IS_OBJECT) {
  140. zend_throw_exception_object(&c->pending_exception TSRMLS_CC);
  141. ZVAL_NULL(&c->pending_exception);
  142. }
  143. /* Handle runtime JS exceptions */
  144. if (try_catch.HasCaught()) {
  145. /* Pending exceptions are set only in outer caller, inner caller exceptions are always rethrown */
  146. if (c->in_execution < 1) {
  147. /* Report immediately if report_uncaught is true */
  148. if (c->report_uncaught) {
  149. v8js_throw_script_exception(c->isolate, &try_catch TSRMLS_CC);
  150. return;
  151. }
  152. /* Exception thrown from JS, preserve it for future execution */
  153. if (result.IsEmpty()) {
  154. v8js_create_script_exception(&c->pending_exception, c->isolate, &try_catch TSRMLS_CC);
  155. return;
  156. }
  157. }
  158. /* Rethrow back to JS */
  159. try_catch.ReThrow();
  160. return;
  161. }
  162. /* Convert V8 value to PHP value */
  163. if (!result.IsEmpty()) {
  164. v8js_to_zval(result, *return_value, flags, c->isolate TSRMLS_CC);
  165. }
  166. }
  167. }
  168. /* }}} */
  169. void v8js_terminate_execution(v8::Isolate *isolate) /* {{{ */
  170. {
  171. if(v8::V8::IsExecutionTerminating(isolate)) {
  172. /* Execution already terminating, needn't trigger it again and
  173. * especially must not execute the spinning loop (which would cause
  174. * crashes in V8 itself, at least with 4.2 and 4.3 version lines). */
  175. return;
  176. }
  177. /* Unfortunately just calling TerminateExecution on the isolate is not
  178. * enough, since v8 just marks the thread as "to be aborted" and doesn't
  179. * immediately do so. Hence we enter an endless loop after signalling
  180. * termination, so we definitely don't execute JS code after the exit()
  181. * statement. */
  182. v8::Locker locker(isolate);
  183. v8::Isolate::Scope isolate_scope(isolate);
  184. v8::HandleScope handle_scope(isolate);
  185. v8::Local<v8::String> source = V8JS_STR("for(;;);");
  186. v8::Local<v8::Script> script = v8::Script::Compile(source);
  187. v8::V8::TerminateExecution(isolate);
  188. script->Run();
  189. }
  190. /* }}} */
  191. int v8js_get_properties_hash(v8::Handle<v8::Value> jsValue, HashTable *retval, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  192. {
  193. v8::Local<v8::Object> jsObj = jsValue->ToObject();
  194. if (!jsObj.IsEmpty()) {
  195. v8::Local<v8::Array> jsKeys = jsObj->GetPropertyNames();
  196. for (unsigned i = 0; i < jsKeys->Length(); i++)
  197. {
  198. v8::Local<v8::String> jsKey = jsKeys->Get(i)->ToString();
  199. /* Skip any prototype properties */
  200. if (!jsObj->HasOwnProperty(jsKey) && !jsObj->HasRealNamedProperty(jsKey) && !jsObj->HasRealNamedCallbackProperty(jsKey)) {
  201. continue;
  202. }
  203. v8::Local<v8::Value> jsVal = jsObj->Get(jsKey);
  204. v8::String::Utf8Value cstr(jsKey);
  205. const char *c_key = ToCString(cstr);
  206. zend_string *key = zend_string_init(c_key, jsKey->ToString()->Utf8Length(), 0);
  207. zval value;
  208. ZVAL_UNDEF(&value);
  209. v8::Local<v8::Value> php_object;
  210. if (jsVal->IsObject()) {
  211. php_object = v8::Local<v8::Object>::Cast(jsVal)->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  212. }
  213. if (!php_object.IsEmpty()) {
  214. /* This is a PHP object, passed to JS and back. */
  215. zend_object *object = reinterpret_cast<zend_object *>(v8::External::Cast(*php_object)->Value());
  216. ZVAL_OBJ(&value, object);
  217. Z_ADDREF_P(&value);
  218. }
  219. else {
  220. if (v8js_to_zval(jsVal, &value, flags, isolate TSRMLS_CC) == FAILURE) {
  221. zval_dtor(&value);
  222. return FAILURE;
  223. }
  224. }
  225. if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
  226. zend_symtable_update(retval, key, &value);
  227. } else {
  228. zend_hash_update(retval, key, &value);
  229. }
  230. zend_string_release(key);
  231. }
  232. return SUCCESS;
  233. }
  234. return FAILURE;
  235. }
  236. /* }}} */
  237. /*
  238. * Local variables:
  239. * tab-width: 4
  240. * c-basic-offset: 4
  241. * End:
  242. * vim600: noet sw=4 ts=4 fdm=marker
  243. * vim<600: noet sw=4 ts=4
  244. */