zend_fibers.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Aaron Piotrowski <aaron@trowski.com> |
  16. | Martin Schröder <m.schroeder2007@gmail.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "zend.h"
  20. #include "zend_API.h"
  21. #include "zend_ini.h"
  22. #include "zend_vm.h"
  23. #include "zend_exceptions.h"
  24. #include "zend_builtin_functions.h"
  25. #include "zend_observer.h"
  26. #include "zend_fibers.h"
  27. #include "zend_fibers_arginfo.h"
  28. #ifdef HAVE_VALGRIND
  29. # include <valgrind/valgrind.h>
  30. #endif
  31. #ifdef ZEND_FIBER_UCONTEXT
  32. # include <ucontext.h>
  33. #endif
  34. #ifndef ZEND_WIN32
  35. # include <unistd.h>
  36. # include <sys/mman.h>
  37. # include <limits.h>
  38. # if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
  39. # define MAP_ANONYMOUS MAP_ANON
  40. # endif
  41. /* FreeBSD require a first (i.e. addr) argument of mmap(2) is not NULL
  42. * if MAP_STACK is passed.
  43. * http://www.FreeBSD.org/cgi/query-pr.cgi?pr=158755 */
  44. # if !defined(MAP_STACK) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  45. # undef MAP_STACK
  46. # define MAP_STACK 0
  47. # endif
  48. # ifndef MAP_FAILED
  49. # define MAP_FAILED ((void * ) -1)
  50. # endif
  51. #endif
  52. #ifdef __SANITIZE_ADDRESS__
  53. # include <sanitizer/common_interface_defs.h>
  54. #endif
  55. /* Encapsulates the fiber C stack with extension for debugging tools. */
  56. struct _zend_fiber_stack {
  57. void *pointer;
  58. size_t size;
  59. #ifdef HAVE_VALGRIND
  60. unsigned int valgrind_stack_id;
  61. #endif
  62. #ifdef __SANITIZE_ADDRESS__
  63. const void *asan_pointer;
  64. size_t asan_size;
  65. #endif
  66. #ifdef ZEND_FIBER_UCONTEXT
  67. /* Embedded ucontext to avoid unnecessary memory allocations. */
  68. ucontext_t ucontext;
  69. #endif
  70. };
  71. /* Zend VM state that needs to be captured / restored during fiber context switch. */
  72. typedef struct _zend_fiber_vm_state {
  73. zend_vm_stack vm_stack;
  74. zval *vm_stack_top;
  75. zval *vm_stack_end;
  76. size_t vm_stack_page_size;
  77. zend_execute_data *current_execute_data;
  78. int error_reporting;
  79. uint32_t jit_trace_num;
  80. JMP_BUF *bailout;
  81. zend_fiber *active_fiber;
  82. } zend_fiber_vm_state;
  83. static zend_always_inline void zend_fiber_capture_vm_state(zend_fiber_vm_state *state)
  84. {
  85. state->vm_stack = EG(vm_stack);
  86. state->vm_stack_top = EG(vm_stack_top);
  87. state->vm_stack_end = EG(vm_stack_end);
  88. state->vm_stack_page_size = EG(vm_stack_page_size);
  89. state->current_execute_data = EG(current_execute_data);
  90. state->error_reporting = EG(error_reporting);
  91. state->jit_trace_num = EG(jit_trace_num);
  92. state->bailout = EG(bailout);
  93. state->active_fiber = EG(active_fiber);
  94. }
  95. static zend_always_inline void zend_fiber_restore_vm_state(zend_fiber_vm_state *state)
  96. {
  97. EG(vm_stack) = state->vm_stack;
  98. EG(vm_stack_top) = state->vm_stack_top;
  99. EG(vm_stack_end) = state->vm_stack_end;
  100. EG(vm_stack_page_size) = state->vm_stack_page_size;
  101. EG(current_execute_data) = state->current_execute_data;
  102. EG(error_reporting) = state->error_reporting;
  103. EG(jit_trace_num) = state->jit_trace_num;
  104. EG(bailout) = state->bailout;
  105. EG(active_fiber) = state->active_fiber;
  106. }
  107. #ifdef ZEND_FIBER_UCONTEXT
  108. ZEND_TLS zend_fiber_transfer *transfer_data;
  109. #else
  110. /* boost_context_data is our customized definition of struct transfer_t as
  111. * provided by boost.context in fcontext.hpp:
  112. *
  113. * typedef void* fcontext_t;
  114. *
  115. * struct transfer_t {
  116. * fcontext_t fctx;
  117. * void *data;
  118. * }; */
  119. typedef struct {
  120. void *handle;
  121. zend_fiber_transfer *transfer;
  122. } boost_context_data;
  123. /* These functions are defined in assembler files provided by boost.context (located in "Zend/asm"). */
  124. extern void *make_fcontext(void *sp, size_t size, void (*fn)(boost_context_data));
  125. extern ZEND_INDIRECT_RETURN boost_context_data jump_fcontext(void *to, zend_fiber_transfer *transfer);
  126. #endif
  127. ZEND_API zend_class_entry *zend_ce_fiber;
  128. static zend_class_entry *zend_ce_fiber_error;
  129. static zend_object_handlers zend_fiber_handlers;
  130. static zend_function zend_fiber_function = { ZEND_INTERNAL_FUNCTION };
  131. ZEND_TLS uint32_t zend_fiber_switch_blocking = 0;
  132. #define ZEND_FIBER_DEFAULT_PAGE_SIZE 4096
  133. static size_t zend_fiber_get_page_size(void)
  134. {
  135. static size_t page_size = 0;
  136. if (!page_size) {
  137. page_size = zend_get_page_size();
  138. if (!page_size || (page_size & (page_size - 1))) {
  139. /* anyway, we have to return a valid result */
  140. page_size = ZEND_FIBER_DEFAULT_PAGE_SIZE;
  141. }
  142. }
  143. return page_size;
  144. }
  145. static zend_fiber_stack *zend_fiber_stack_allocate(size_t size)
  146. {
  147. void *pointer;
  148. const size_t page_size = zend_fiber_get_page_size();
  149. ZEND_ASSERT(size >= page_size + ZEND_FIBER_GUARD_PAGES * page_size);
  150. const size_t stack_size = (size + page_size - 1) / page_size * page_size;
  151. const size_t alloc_size = stack_size + ZEND_FIBER_GUARD_PAGES * page_size;
  152. #ifdef ZEND_WIN32
  153. pointer = VirtualAlloc(0, alloc_size, MEM_COMMIT, PAGE_READWRITE);
  154. if (!pointer) {
  155. DWORD err = GetLastError();
  156. char *errmsg = php_win32_error_to_msg(err);
  157. zend_throw_exception_ex(NULL, 0, "Fiber stack allocate failed: VirtualAlloc failed: [0x%08lx] %s", err, errmsg[0] ? errmsg : "Unknown");
  158. php_win32_error_msg_free(errmsg);
  159. return NULL;
  160. }
  161. # if ZEND_FIBER_GUARD_PAGES
  162. DWORD protect;
  163. if (!VirtualProtect(pointer, ZEND_FIBER_GUARD_PAGES * page_size, PAGE_READWRITE | PAGE_GUARD, &protect)) {
  164. DWORD err = GetLastError();
  165. char *errmsg = php_win32_error_to_msg(err);
  166. zend_throw_exception_ex(NULL, 0, "Fiber stack protect failed: VirtualProtect failed: [0x%08lx] %s", err, errmsg[0] ? errmsg : "Unknown");
  167. php_win32_error_msg_free(errmsg);
  168. VirtualFree(pointer, 0, MEM_RELEASE);
  169. return NULL;
  170. }
  171. # endif
  172. #else
  173. pointer = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
  174. if (pointer == MAP_FAILED) {
  175. zend_throw_exception_ex(NULL, 0, "Fiber stack allocate failed: mmap failed: %s (%d)", strerror(errno), errno);
  176. return NULL;
  177. }
  178. # if ZEND_FIBER_GUARD_PAGES
  179. if (mprotect(pointer, ZEND_FIBER_GUARD_PAGES * page_size, PROT_NONE) < 0) {
  180. zend_throw_exception_ex(NULL, 0, "Fiber stack protect failed: mprotect failed: %s (%d)", strerror(errno), errno);
  181. munmap(pointer, alloc_size);
  182. return NULL;
  183. }
  184. # endif
  185. #endif
  186. zend_fiber_stack *stack = emalloc(sizeof(zend_fiber_stack));
  187. stack->pointer = (void *) ((uintptr_t) pointer + ZEND_FIBER_GUARD_PAGES * page_size);
  188. stack->size = stack_size;
  189. #ifdef VALGRIND_STACK_REGISTER
  190. uintptr_t base = (uintptr_t) stack->pointer;
  191. stack->valgrind_stack_id = VALGRIND_STACK_REGISTER(base, base + stack->size);
  192. #endif
  193. #ifdef __SANITIZE_ADDRESS__
  194. stack->asan_pointer = stack->pointer;
  195. stack->asan_size = stack->size;
  196. #endif
  197. return stack;
  198. }
  199. static void zend_fiber_stack_free(zend_fiber_stack *stack)
  200. {
  201. #ifdef VALGRIND_STACK_DEREGISTER
  202. VALGRIND_STACK_DEREGISTER(stack->valgrind_stack_id);
  203. #endif
  204. const size_t page_size = zend_fiber_get_page_size();
  205. void *pointer = (void *) ((uintptr_t) stack->pointer - ZEND_FIBER_GUARD_PAGES * page_size);
  206. #ifdef ZEND_WIN32
  207. VirtualFree(pointer, 0, MEM_RELEASE);
  208. #else
  209. munmap(pointer, stack->size + ZEND_FIBER_GUARD_PAGES * page_size);
  210. #endif
  211. efree(stack);
  212. }
  213. #ifdef ZEND_FIBER_UCONTEXT
  214. static ZEND_NORETURN void zend_fiber_trampoline(void)
  215. #else
  216. static ZEND_NORETURN void zend_fiber_trampoline(boost_context_data data)
  217. #endif
  218. {
  219. /* Initialize transfer struct with a copy of passed data. */
  220. #ifdef ZEND_FIBER_UCONTEXT
  221. zend_fiber_transfer transfer = *transfer_data;
  222. #else
  223. zend_fiber_transfer transfer = *data.transfer;
  224. #endif
  225. zend_fiber_context *from = transfer.context;
  226. #ifdef __SANITIZE_ADDRESS__
  227. __sanitizer_finish_switch_fiber(NULL, &from->stack->asan_pointer, &from->stack->asan_size);
  228. #endif
  229. #ifndef ZEND_FIBER_UCONTEXT
  230. /* Get the context that resumed us and update its handle to allow for symmetric coroutines. */
  231. from->handle = data.handle;
  232. #endif
  233. /* Ensure that previous fiber will be cleaned up (needed by symmetric coroutines). */
  234. if (from->status == ZEND_FIBER_STATUS_DEAD) {
  235. zend_fiber_destroy_context(from);
  236. }
  237. zend_fiber_context *context = EG(current_fiber_context);
  238. context->function(&transfer);
  239. context->status = ZEND_FIBER_STATUS_DEAD;
  240. /* Final context switch, the fiber must not be resumed afterwards! */
  241. zend_fiber_switch_context(&transfer);
  242. /* Abort here because we are in an inconsistent program state. */
  243. abort();
  244. }
  245. ZEND_API void zend_fiber_switch_block(void)
  246. {
  247. ++zend_fiber_switch_blocking;
  248. }
  249. ZEND_API void zend_fiber_switch_unblock(void)
  250. {
  251. ZEND_ASSERT(zend_fiber_switch_blocking && "Fiber switching was not blocked");
  252. --zend_fiber_switch_blocking;
  253. }
  254. ZEND_API bool zend_fiber_switch_blocked(void)
  255. {
  256. return zend_fiber_switch_blocking;
  257. }
  258. ZEND_API bool zend_fiber_init_context(zend_fiber_context *context, void *kind, zend_fiber_coroutine coroutine, size_t stack_size)
  259. {
  260. context->stack = zend_fiber_stack_allocate(stack_size);
  261. if (UNEXPECTED(!context->stack)) {
  262. return false;
  263. }
  264. #ifdef ZEND_FIBER_UCONTEXT
  265. ucontext_t *handle = &context->stack->ucontext;
  266. getcontext(handle);
  267. handle->uc_stack.ss_size = context->stack->size;
  268. handle->uc_stack.ss_sp = context->stack->pointer;
  269. handle->uc_stack.ss_flags = 0;
  270. handle->uc_link = NULL;
  271. makecontext(handle, (void (*)(void)) zend_fiber_trampoline, 0);
  272. context->handle = handle;
  273. #else
  274. // Stack grows down, calculate the top of the stack. make_fcontext then shifts pointer to lower 16-byte boundary.
  275. void *stack = (void *) ((uintptr_t) context->stack->pointer + context->stack->size);
  276. context->handle = make_fcontext(stack, context->stack->size, zend_fiber_trampoline);
  277. ZEND_ASSERT(context->handle != NULL && "make_fcontext() never returns NULL");
  278. #endif
  279. context->kind = kind;
  280. context->function = coroutine;
  281. // Set status in case memory has not been zeroed.
  282. context->status = ZEND_FIBER_STATUS_INIT;
  283. zend_observer_fiber_init_notify(context);
  284. return true;
  285. }
  286. ZEND_API void zend_fiber_destroy_context(zend_fiber_context *context)
  287. {
  288. zend_observer_fiber_destroy_notify(context);
  289. zend_fiber_stack_free(context->stack);
  290. }
  291. ZEND_API void zend_fiber_switch_context(zend_fiber_transfer *transfer)
  292. {
  293. zend_fiber_context *from = EG(current_fiber_context);
  294. zend_fiber_context *to = transfer->context;
  295. zend_fiber_vm_state state;
  296. ZEND_ASSERT(to && to->handle && to->status != ZEND_FIBER_STATUS_DEAD && "Invalid fiber context");
  297. ZEND_ASSERT(from && "From fiber context must be present");
  298. ZEND_ASSERT(to != from && "Cannot switch into the running fiber context");
  299. /* Assert that all error transfers hold a Throwable value. */
  300. ZEND_ASSERT((
  301. !(transfer->flags & ZEND_FIBER_TRANSFER_FLAG_ERROR) ||
  302. (Z_TYPE(transfer->value) == IS_OBJECT && (
  303. zend_is_unwind_exit(Z_OBJ(transfer->value)) ||
  304. zend_is_graceful_exit(Z_OBJ(transfer->value)) ||
  305. instanceof_function(Z_OBJCE(transfer->value), zend_ce_throwable)
  306. ))
  307. ) && "Error transfer requires a throwable value");
  308. zend_observer_fiber_switch_notify(from, to);
  309. zend_fiber_capture_vm_state(&state);
  310. to->status = ZEND_FIBER_STATUS_RUNNING;
  311. if (EXPECTED(from->status == ZEND_FIBER_STATUS_RUNNING)) {
  312. from->status = ZEND_FIBER_STATUS_SUSPENDED;
  313. }
  314. /* Update transfer context with the current fiber before switching. */
  315. transfer->context = from;
  316. EG(current_fiber_context) = to;
  317. #ifdef __SANITIZE_ADDRESS__
  318. void *fake_stack = NULL;
  319. __sanitizer_start_switch_fiber(
  320. from->status != ZEND_FIBER_STATUS_DEAD ? &fake_stack : NULL,
  321. to->stack->asan_pointer,
  322. to->stack->asan_size);
  323. #endif
  324. #ifdef ZEND_FIBER_UCONTEXT
  325. transfer_data = transfer;
  326. swapcontext(from->handle, to->handle);
  327. /* Copy transfer struct because it might live on the other fiber's stack that will eventually be destroyed. */
  328. *transfer = *transfer_data;
  329. #else
  330. boost_context_data data = jump_fcontext(to->handle, transfer);
  331. /* Copy transfer struct because it might live on the other fiber's stack that will eventually be destroyed. */
  332. *transfer = *data.transfer;
  333. #endif
  334. to = transfer->context;
  335. #ifndef ZEND_FIBER_UCONTEXT
  336. /* Get the context that resumed us and update its handle to allow for symmetric coroutines. */
  337. to->handle = data.handle;
  338. #endif
  339. #ifdef __SANITIZE_ADDRESS__
  340. __sanitizer_finish_switch_fiber(fake_stack, &to->stack->asan_pointer, &to->stack->asan_size);
  341. #endif
  342. EG(current_fiber_context) = from;
  343. zend_fiber_restore_vm_state(&state);
  344. /* Destroy prior context if it has been marked as dead. */
  345. if (to->status == ZEND_FIBER_STATUS_DEAD) {
  346. zend_fiber_destroy_context(to);
  347. }
  348. }
  349. static ZEND_STACK_ALIGNED void zend_fiber_execute(zend_fiber_transfer *transfer)
  350. {
  351. ZEND_ASSERT(Z_TYPE(transfer->value) == IS_NULL && "Initial transfer value to fiber context must be NULL");
  352. ZEND_ASSERT(!transfer->flags && "No flags should be set on initial transfer");
  353. zend_fiber *fiber = EG(active_fiber);
  354. /* Determine the current error_reporting ini setting. */
  355. zend_long error_reporting = INI_INT("error_reporting");
  356. /* If error_reporting is 0 and not explicitly set to 0, INI_STR returns a null pointer. */
  357. if (!error_reporting && !INI_STR("error_reporting")) {
  358. error_reporting = E_ALL;
  359. }
  360. EG(vm_stack) = NULL;
  361. zend_first_try {
  362. zend_vm_stack stack = zend_vm_stack_new_page(ZEND_FIBER_VM_STACK_SIZE, NULL);
  363. EG(vm_stack) = stack;
  364. EG(vm_stack_top) = stack->top + ZEND_CALL_FRAME_SLOT;
  365. EG(vm_stack_end) = stack->end;
  366. EG(vm_stack_page_size) = ZEND_FIBER_VM_STACK_SIZE;
  367. fiber->execute_data = (zend_execute_data *) stack->top;
  368. fiber->stack_bottom = fiber->execute_data;
  369. memset(fiber->execute_data, 0, sizeof(zend_execute_data));
  370. fiber->execute_data->func = &zend_fiber_function;
  371. fiber->stack_bottom->prev_execute_data = EG(current_execute_data);
  372. EG(current_execute_data) = fiber->execute_data;
  373. EG(jit_trace_num) = 0;
  374. EG(error_reporting) = error_reporting;
  375. fiber->fci.retval = &fiber->result;
  376. zend_call_function(&fiber->fci, &fiber->fci_cache);
  377. /* Cleanup callback and unset field to prevent GC / duplicate dtor issues. */
  378. zval_ptr_dtor(&fiber->fci.function_name);
  379. ZVAL_UNDEF(&fiber->fci.function_name);
  380. if (EG(exception)) {
  381. if (!(fiber->flags & ZEND_FIBER_FLAG_DESTROYED)
  382. || !(zend_is_graceful_exit(EG(exception)) || zend_is_unwind_exit(EG(exception)))
  383. ) {
  384. fiber->flags |= ZEND_FIBER_FLAG_THREW;
  385. transfer->flags = ZEND_FIBER_TRANSFER_FLAG_ERROR;
  386. ZVAL_OBJ_COPY(&transfer->value, EG(exception));
  387. }
  388. zend_clear_exception();
  389. }
  390. } zend_catch {
  391. fiber->flags |= ZEND_FIBER_FLAG_BAILOUT;
  392. transfer->flags = ZEND_FIBER_TRANSFER_FLAG_BAILOUT;
  393. } zend_end_try();
  394. transfer->context = fiber->caller;
  395. zend_vm_stack_destroy();
  396. fiber->execute_data = NULL;
  397. fiber->stack_bottom = NULL;
  398. fiber->caller = NULL;
  399. }
  400. /* Handles forwarding of result / error from a transfer into the running fiber. */
  401. static zend_always_inline void zend_fiber_delegate_transfer_result(
  402. zend_fiber_transfer *transfer, INTERNAL_FUNCTION_PARAMETERS
  403. ) {
  404. if (transfer->flags & ZEND_FIBER_TRANSFER_FLAG_ERROR) {
  405. /* Use internal throw to skip the Throwable-check that would fail for (graceful) exit. */
  406. zend_throw_exception_internal(Z_OBJ(transfer->value));
  407. RETURN_THROWS();
  408. }
  409. RETURN_COPY_VALUE(&transfer->value);
  410. }
  411. static zend_always_inline zend_fiber_transfer zend_fiber_switch_to(
  412. zend_fiber_context *context, zval *value, bool exception
  413. ) {
  414. zend_fiber_transfer transfer = {
  415. .context = context,
  416. .flags = exception ? ZEND_FIBER_TRANSFER_FLAG_ERROR : 0,
  417. };
  418. if (value) {
  419. ZVAL_COPY(&transfer.value, value);
  420. } else {
  421. ZVAL_NULL(&transfer.value);
  422. }
  423. zend_fiber_switch_context(&transfer);
  424. /* Forward bailout into current fiber. */
  425. if (UNEXPECTED(transfer.flags & ZEND_FIBER_TRANSFER_FLAG_BAILOUT)) {
  426. zend_bailout();
  427. }
  428. return transfer;
  429. }
  430. static zend_always_inline zend_fiber_transfer zend_fiber_resume(zend_fiber *fiber, zval *value, bool exception)
  431. {
  432. zend_fiber *previous = EG(active_fiber);
  433. fiber->caller = EG(current_fiber_context);
  434. EG(active_fiber) = fiber;
  435. zend_fiber_transfer transfer = zend_fiber_switch_to(fiber->previous, value, exception);
  436. EG(active_fiber) = previous;
  437. return transfer;
  438. }
  439. static zend_always_inline zend_fiber_transfer zend_fiber_suspend(zend_fiber *fiber, zval *value)
  440. {
  441. ZEND_ASSERT(fiber->caller != NULL);
  442. zend_fiber_context *caller = fiber->caller;
  443. fiber->previous = EG(current_fiber_context);
  444. fiber->caller = NULL;
  445. return zend_fiber_switch_to(caller, value, false);
  446. }
  447. static zend_object *zend_fiber_object_create(zend_class_entry *ce)
  448. {
  449. zend_fiber *fiber = emalloc(sizeof(zend_fiber));
  450. memset(fiber, 0, sizeof(zend_fiber));
  451. zend_object_std_init(&fiber->std, ce);
  452. fiber->std.handlers = &zend_fiber_handlers;
  453. return &fiber->std;
  454. }
  455. static void zend_fiber_object_destroy(zend_object *object)
  456. {
  457. zend_fiber *fiber = (zend_fiber *) object;
  458. if (fiber->context.status != ZEND_FIBER_STATUS_SUSPENDED) {
  459. return;
  460. }
  461. zend_object *exception = EG(exception);
  462. EG(exception) = NULL;
  463. zval graceful_exit;
  464. ZVAL_OBJ(&graceful_exit, zend_create_graceful_exit());
  465. fiber->flags |= ZEND_FIBER_FLAG_DESTROYED;
  466. zend_fiber_transfer transfer = zend_fiber_resume(fiber, &graceful_exit, true);
  467. zval_ptr_dtor(&graceful_exit);
  468. if (transfer.flags & ZEND_FIBER_TRANSFER_FLAG_ERROR) {
  469. EG(exception) = Z_OBJ(transfer.value);
  470. if (!exception && EG(current_execute_data) && EG(current_execute_data)->func
  471. && ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) {
  472. zend_rethrow_exception(EG(current_execute_data));
  473. }
  474. zend_exception_set_previous(EG(exception), exception);
  475. if (!EG(current_execute_data)) {
  476. zend_exception_error(EG(exception), E_ERROR);
  477. }
  478. } else {
  479. zval_ptr_dtor(&transfer.value);
  480. EG(exception) = exception;
  481. }
  482. }
  483. static void zend_fiber_object_free(zend_object *object)
  484. {
  485. zend_fiber *fiber = (zend_fiber *) object;
  486. zval_ptr_dtor(&fiber->fci.function_name);
  487. zval_ptr_dtor(&fiber->result);
  488. zend_object_std_dtor(&fiber->std);
  489. }
  490. static HashTable *zend_fiber_object_gc(zend_object *object, zval **table, int *num)
  491. {
  492. zend_fiber *fiber = (zend_fiber *) object;
  493. zend_get_gc_buffer *buf = zend_get_gc_buffer_create();
  494. zend_get_gc_buffer_add_zval(buf, &fiber->fci.function_name);
  495. zend_get_gc_buffer_add_zval(buf, &fiber->result);
  496. zend_get_gc_buffer_use(buf, table, num);
  497. return NULL;
  498. }
  499. ZEND_METHOD(Fiber, __construct)
  500. {
  501. zend_fiber *fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
  502. ZEND_PARSE_PARAMETERS_START(1, 1)
  503. Z_PARAM_FUNC(fiber->fci, fiber->fci_cache)
  504. ZEND_PARSE_PARAMETERS_END();
  505. // Keep a reference to closures or callable objects while the fiber is running.
  506. Z_TRY_ADDREF(fiber->fci.function_name);
  507. }
  508. ZEND_METHOD(Fiber, start)
  509. {
  510. zend_fiber *fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
  511. ZEND_PARSE_PARAMETERS_START(0, -1)
  512. Z_PARAM_VARIADIC_WITH_NAMED(fiber->fci.params, fiber->fci.param_count, fiber->fci.named_params);
  513. ZEND_PARSE_PARAMETERS_END();
  514. if (UNEXPECTED(zend_fiber_switch_blocked())) {
  515. zend_throw_error(zend_ce_fiber_error, "Cannot switch fibers in current execution context");
  516. RETURN_THROWS();
  517. }
  518. if (fiber->context.status != ZEND_FIBER_STATUS_INIT) {
  519. zend_throw_error(zend_ce_fiber_error, "Cannot start a fiber that has already been started");
  520. RETURN_THROWS();
  521. }
  522. if (!zend_fiber_init_context(&fiber->context, zend_ce_fiber, zend_fiber_execute, EG(fiber_stack_size))) {
  523. RETURN_THROWS();
  524. }
  525. fiber->previous = &fiber->context;
  526. zend_fiber_transfer transfer = zend_fiber_resume(fiber, NULL, false);
  527. zend_fiber_delegate_transfer_result(&transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  528. }
  529. ZEND_METHOD(Fiber, suspend)
  530. {
  531. zval *value = NULL;
  532. ZEND_PARSE_PARAMETERS_START(0, 1)
  533. Z_PARAM_OPTIONAL
  534. Z_PARAM_ZVAL(value);
  535. ZEND_PARSE_PARAMETERS_END();
  536. zend_fiber *fiber = EG(active_fiber);
  537. if (UNEXPECTED(!fiber)) {
  538. zend_throw_error(zend_ce_fiber_error, "Cannot suspend outside of a fiber");
  539. RETURN_THROWS();
  540. }
  541. if (UNEXPECTED(fiber->flags & ZEND_FIBER_FLAG_DESTROYED)) {
  542. zend_throw_error(zend_ce_fiber_error, "Cannot suspend in a force-closed fiber");
  543. RETURN_THROWS();
  544. }
  545. if (UNEXPECTED(zend_fiber_switch_blocked())) {
  546. zend_throw_error(zend_ce_fiber_error, "Cannot switch fibers in current execution context");
  547. RETURN_THROWS();
  548. }
  549. ZEND_ASSERT(fiber->context.status == ZEND_FIBER_STATUS_RUNNING || fiber->context.status == ZEND_FIBER_STATUS_SUSPENDED);
  550. fiber->execute_data = EG(current_execute_data);
  551. fiber->stack_bottom->prev_execute_data = NULL;
  552. zend_fiber_transfer transfer = zend_fiber_suspend(fiber, value);
  553. zend_fiber_delegate_transfer_result(&transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  554. }
  555. ZEND_METHOD(Fiber, resume)
  556. {
  557. zend_fiber *fiber;
  558. zval *value = NULL;
  559. ZEND_PARSE_PARAMETERS_START(0, 1)
  560. Z_PARAM_OPTIONAL
  561. Z_PARAM_ZVAL(value);
  562. ZEND_PARSE_PARAMETERS_END();
  563. if (UNEXPECTED(zend_fiber_switch_blocked())) {
  564. zend_throw_error(zend_ce_fiber_error, "Cannot switch fibers in current execution context");
  565. RETURN_THROWS();
  566. }
  567. fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
  568. if (UNEXPECTED(fiber->context.status != ZEND_FIBER_STATUS_SUSPENDED || fiber->caller != NULL)) {
  569. zend_throw_error(zend_ce_fiber_error, "Cannot resume a fiber that is not suspended");
  570. RETURN_THROWS();
  571. }
  572. fiber->stack_bottom->prev_execute_data = EG(current_execute_data);
  573. zend_fiber_transfer transfer = zend_fiber_resume(fiber, value, false);
  574. zend_fiber_delegate_transfer_result(&transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  575. }
  576. ZEND_METHOD(Fiber, throw)
  577. {
  578. zend_fiber *fiber;
  579. zval *exception;
  580. ZEND_PARSE_PARAMETERS_START(1, 1)
  581. Z_PARAM_OBJECT_OF_CLASS(exception, zend_ce_throwable)
  582. ZEND_PARSE_PARAMETERS_END();
  583. if (UNEXPECTED(zend_fiber_switch_blocked())) {
  584. zend_throw_error(zend_ce_fiber_error, "Cannot switch fibers in current execution context");
  585. RETURN_THROWS();
  586. }
  587. fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
  588. if (UNEXPECTED(fiber->context.status != ZEND_FIBER_STATUS_SUSPENDED || fiber->caller != NULL)) {
  589. zend_throw_error(zend_ce_fiber_error, "Cannot resume a fiber that is not suspended");
  590. RETURN_THROWS();
  591. }
  592. fiber->stack_bottom->prev_execute_data = EG(current_execute_data);
  593. zend_fiber_transfer transfer = zend_fiber_resume(fiber, exception, true);
  594. zend_fiber_delegate_transfer_result(&transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  595. }
  596. ZEND_METHOD(Fiber, isStarted)
  597. {
  598. zend_fiber *fiber;
  599. ZEND_PARSE_PARAMETERS_NONE();
  600. fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
  601. RETURN_BOOL(fiber->context.status != ZEND_FIBER_STATUS_INIT);
  602. }
  603. ZEND_METHOD(Fiber, isSuspended)
  604. {
  605. zend_fiber *fiber;
  606. ZEND_PARSE_PARAMETERS_NONE();
  607. fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
  608. RETURN_BOOL(fiber->context.status == ZEND_FIBER_STATUS_SUSPENDED && fiber->caller == NULL);
  609. }
  610. ZEND_METHOD(Fiber, isRunning)
  611. {
  612. zend_fiber *fiber;
  613. ZEND_PARSE_PARAMETERS_NONE();
  614. fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
  615. RETURN_BOOL(fiber->context.status == ZEND_FIBER_STATUS_RUNNING || fiber->caller != NULL);
  616. }
  617. ZEND_METHOD(Fiber, isTerminated)
  618. {
  619. zend_fiber *fiber;
  620. ZEND_PARSE_PARAMETERS_NONE();
  621. fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
  622. RETURN_BOOL(fiber->context.status == ZEND_FIBER_STATUS_DEAD);
  623. }
  624. ZEND_METHOD(Fiber, getReturn)
  625. {
  626. zend_fiber *fiber;
  627. const char *message;
  628. ZEND_PARSE_PARAMETERS_NONE();
  629. fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
  630. if (fiber->context.status == ZEND_FIBER_STATUS_DEAD) {
  631. if (fiber->flags & ZEND_FIBER_FLAG_THREW) {
  632. message = "The fiber threw an exception";
  633. } else if (fiber->flags & ZEND_FIBER_FLAG_BAILOUT) {
  634. message = "The fiber exited with a fatal error";
  635. } else {
  636. RETURN_COPY_DEREF(&fiber->result);
  637. }
  638. } else if (fiber->context.status == ZEND_FIBER_STATUS_INIT) {
  639. message = "The fiber has not been started";
  640. } else {
  641. message = "The fiber has not returned";
  642. }
  643. zend_throw_error(zend_ce_fiber_error, "Cannot get fiber return value: %s", message);
  644. RETURN_THROWS();
  645. }
  646. ZEND_METHOD(Fiber, getCurrent)
  647. {
  648. ZEND_PARSE_PARAMETERS_NONE();
  649. zend_fiber *fiber = EG(active_fiber);
  650. if (!fiber) {
  651. RETURN_NULL();
  652. }
  653. RETURN_OBJ_COPY(&fiber->std);
  654. }
  655. ZEND_METHOD(FiberError, __construct)
  656. {
  657. zend_throw_error(
  658. NULL,
  659. "The \"%s\" class is reserved for internal use and cannot be manually instantiated",
  660. ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name)
  661. );
  662. }
  663. void zend_register_fiber_ce(void)
  664. {
  665. zend_ce_fiber = register_class_Fiber();
  666. zend_ce_fiber->create_object = zend_fiber_object_create;
  667. zend_fiber_handlers = std_object_handlers;
  668. zend_fiber_handlers.dtor_obj = zend_fiber_object_destroy;
  669. zend_fiber_handlers.free_obj = zend_fiber_object_free;
  670. zend_fiber_handlers.get_gc = zend_fiber_object_gc;
  671. zend_fiber_handlers.clone_obj = NULL;
  672. zend_ce_fiber_error = register_class_FiberError(zend_ce_error);
  673. zend_ce_fiber_error->create_object = zend_ce_error->create_object;
  674. }
  675. void zend_fiber_init(void)
  676. {
  677. zend_fiber_context *context = ecalloc(1, sizeof(zend_fiber_context));
  678. #if defined(__SANITIZE_ADDRESS__) || defined(ZEND_FIBER_UCONTEXT)
  679. // Main fiber stack is only needed if ASan or ucontext is enabled.
  680. context->stack = emalloc(sizeof(zend_fiber_stack));
  681. #ifdef ZEND_FIBER_UCONTEXT
  682. context->handle = &context->stack->ucontext;
  683. #endif
  684. #endif
  685. context->status = ZEND_FIBER_STATUS_RUNNING;
  686. EG(main_fiber_context) = context;
  687. EG(current_fiber_context) = context;
  688. EG(active_fiber) = NULL;
  689. zend_fiber_switch_blocking = 0;
  690. }
  691. void zend_fiber_shutdown(void)
  692. {
  693. #if defined(__SANITIZE_ADDRESS__) || defined(ZEND_FIBER_UCONTEXT)
  694. efree(EG(main_fiber_context)->stack);
  695. #endif
  696. efree(EG(main_fiber_context));
  697. zend_fiber_switch_block();
  698. }