v8js_timer.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2013 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. +----------------------------------------------------------------------+
  12. */
  13. #ifdef HAVE_CONFIG_H
  14. #include "config.h"
  15. #endif
  16. #ifdef _WIN32
  17. #include <concrt.h>
  18. #endif
  19. #include "php_v8js_macros.h"
  20. #include "v8js_v8.h"
  21. #include "v8js_exceptions.h"
  22. #include "v8js_timer.h"
  23. extern "C" {
  24. #include "ext/date/php_date.h"
  25. #include "ext/standard/php_string.h"
  26. #include "zend_interfaces.h"
  27. #include "zend_closures.h"
  28. #include "ext/spl/spl_exceptions.h"
  29. #include "zend_exceptions.h"
  30. }
  31. static void v8js_timer_interrupt_handler(v8::Isolate *isolate, void *data) { /* {{{ */
  32. zend_v8js_globals *globals = static_cast<zend_v8js_globals *>(data);
  33. if (!globals->timer_stack.size()) {
  34. return;
  35. }
  36. v8::Locker locker(isolate);
  37. v8::HeapStatistics hs;
  38. bool send_notification = false;
  39. bool has_sent_notification = false;
  40. do {
  41. if (send_notification) {
  42. isolate->LowMemoryNotification();
  43. has_sent_notification = true;
  44. }
  45. isolate->GetHeapStatistics(&hs);
  46. globals->timer_mutex.lock();
  47. for (std::deque< v8js_timer_ctx* >::iterator it = globals->timer_stack.begin();
  48. it != globals->timer_stack.end(); it ++) {
  49. v8js_timer_ctx *timer_ctx = *it;
  50. v8js_ctx *c = timer_ctx->ctx;
  51. if(c->isolate != isolate || timer_ctx->killed) {
  52. continue;
  53. }
  54. if (timer_ctx->memory_limit > 0 && hs.used_heap_size() > timer_ctx->memory_limit) {
  55. if (has_sent_notification) {
  56. timer_ctx->killed = true;
  57. v8::V8::TerminateExecution(c->isolate);
  58. c->memory_limit_hit = true;
  59. } else {
  60. // force garbage collection, then check again
  61. send_notification = true;
  62. break;
  63. }
  64. }
  65. }
  66. globals->timer_mutex.unlock();
  67. } while(send_notification != has_sent_notification);
  68. }
  69. /* }}} */
  70. void v8js_timer_thread(zend_v8js_globals *globals) /* {{{ */
  71. {
  72. while (!globals->timer_stop) {
  73. globals->timer_mutex.lock();
  74. if (globals->timer_stack.size()) {
  75. v8js_timer_ctx *timer_ctx = globals->timer_stack.front();
  76. v8js_ctx *c = timer_ctx->ctx;
  77. std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
  78. if(timer_ctx->killed) {
  79. /* execution already terminated, nothing to check anymore,
  80. * but wait for caller to pop this timer context. */
  81. }
  82. else if(timer_ctx->time_limit > 0 && now > timer_ctx->time_point) {
  83. timer_ctx->killed = true;
  84. v8::V8::TerminateExecution(c->isolate);
  85. c->time_limit_hit = true;
  86. }
  87. else if (timer_ctx->memory_limit > 0) {
  88. /* If a memory_limit is set, we need to interrupt execution
  89. * and check heap size within the callback. We must *not*
  90. * directly call GetHeapStatistics here, since we don't have
  91. * a v8::Locker on the isolate, but are expected to hold one,
  92. * and cannot aquire it as v8 is executing the script ... */
  93. c->isolate->RequestInterrupt(v8js_timer_interrupt_handler, static_cast<void *>(globals));
  94. }
  95. }
  96. globals->timer_mutex.unlock();
  97. // Sleep for 10ms
  98. #ifdef _WIN32
  99. concurrency::wait(10);
  100. #else
  101. std::chrono::milliseconds duration(10);
  102. std::this_thread::sleep_for(duration);
  103. #endif
  104. }
  105. }
  106. /* }}} */
  107. void v8js_timer_push(long time_limit, size_t memory_limit, v8js_ctx *c) /* {{{ */
  108. {
  109. V8JSG(timer_mutex).lock();
  110. // Create context for this timer
  111. v8js_timer_ctx *timer_ctx = (v8js_timer_ctx *)emalloc(sizeof(v8js_timer_ctx));
  112. // Calculate the time point when the time limit is exceeded
  113. std::chrono::milliseconds duration(time_limit);
  114. std::chrono::time_point<std::chrono::high_resolution_clock> from = std::chrono::high_resolution_clock::now();
  115. // Push the timer context
  116. timer_ctx->time_limit = time_limit;
  117. timer_ctx->memory_limit = memory_limit;
  118. timer_ctx->time_point = from + duration;
  119. timer_ctx->ctx = c;
  120. timer_ctx->killed = false;
  121. V8JSG(timer_stack).push_front(timer_ctx);
  122. V8JSG(timer_mutex).unlock();
  123. }
  124. /* }}} */
  125. /*
  126. * Local variables:
  127. * tab-width: 4
  128. * c-basic-offset: 4
  129. * indent-tabs-mode: t
  130. * End:
  131. * vim600: noet sw=4 ts=4 fdm=marker
  132. * vim<600: noet sw=4 ts=4
  133. */