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
20 #include "zend.h"
21 #include "zend_API.h"
22 #include "zend_gc.h"
23 #include "zend_ini.h"
24 #include "zend_vm.h"
25 #include "zend_exceptions.h"
26 #include "zend_builtin_functions.h"
27 #include "zend_observer.h"
28 #include "zend_mmap.h"
29 #include "zend_compile.h"
30 #include "zend_closures.h"
31 #include "zend_generators.h"
32
33 #include "zend_fibers.h"
34 #include "zend_fibers_arginfo.h"
35
36 #ifdef HAVE_VALGRIND
37 # include <valgrind/valgrind.h>
38 #endif
39
40 #ifdef ZEND_FIBER_UCONTEXT
41 # include <ucontext.h>
42 #endif
43
44 #ifndef ZEND_WIN32
45 # include <unistd.h>
46 # include <sys/mman.h>
47 # include <limits.h>
48
49 # if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
50 # define MAP_ANONYMOUS MAP_ANON
51 # endif
52
53 /* FreeBSD require a first (i.e. addr) argument of mmap(2) is not NULL
54 * if MAP_STACK is passed.
55 * http://www.FreeBSD.org/cgi/query-pr.cgi?pr=158755 */
56 # if !defined(MAP_STACK) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
57 # undef MAP_STACK
58 # define MAP_STACK 0
59 # endif
60
61 # ifndef MAP_FAILED
62 # define MAP_FAILED ((void * ) -1)
63 # endif
64 #endif
65
66 #ifdef __SANITIZE_ADDRESS__
67 # include <sanitizer/asan_interface.h>
68 # include <sanitizer/common_interface_defs.h>
69 #endif
70
71 # if defined __CET__
72 # include <cet.h>
73 # define SHSTK_ENABLED (__CET__ & 0x2)
74 # define BOOST_CONTEXT_SHADOW_STACK (SHSTK_ENABLED && SHADOW_STACK_SYSCALL)
75 # define __NR_map_shadow_stack 451
76 # ifndef SHADOW_STACK_SET_TOKEN
77 # define SHADOW_STACK_SET_TOKEN 0x1
78 #endif
79 #endif
80
81 /* Encapsulates the fiber C stack with extension for debugging tools. */
82 struct _zend_fiber_stack {
83 void *pointer;
84 size_t size;
85
86 #ifdef HAVE_VALGRIND
87 unsigned int valgrind_stack_id;
88 #endif
89
90 #ifdef __SANITIZE_ADDRESS__
91 const void *asan_pointer;
92 size_t asan_size;
93 #endif
94
95 #ifdef ZEND_FIBER_UCONTEXT
96 /* Embedded ucontext to avoid unnecessary memory allocations. */
97 ucontext_t ucontext;
98 #elif BOOST_CONTEXT_SHADOW_STACK
99 /* Shadow stack: base, size */
100 void *ss_base;
101 size_t ss_size;
102 #endif
103 };
104
105 /* Zend VM state that needs to be captured / restored during fiber context switch. */
106 typedef struct _zend_fiber_vm_state {
107 zend_vm_stack vm_stack;
108 zval *vm_stack_top;
109 zval *vm_stack_end;
110 size_t vm_stack_page_size;
111 zend_execute_data *current_execute_data;
112 int error_reporting;
113 uint32_t jit_trace_num;
114 JMP_BUF *bailout;
115 zend_fiber *active_fiber;
116 #ifdef ZEND_CHECK_STACK_LIMIT
117 void *stack_base;
118 void *stack_limit;
119 #endif
120 } zend_fiber_vm_state;
121
zend_fiber_capture_vm_state(zend_fiber_vm_state * state)122 static zend_always_inline void zend_fiber_capture_vm_state(zend_fiber_vm_state *state)
123 {
124 state->vm_stack = EG(vm_stack);
125 state->vm_stack_top = EG(vm_stack_top);
126 state->vm_stack_end = EG(vm_stack_end);
127 state->vm_stack_page_size = EG(vm_stack_page_size);
128 state->current_execute_data = EG(current_execute_data);
129 state->error_reporting = EG(error_reporting);
130 state->jit_trace_num = EG(jit_trace_num);
131 state->bailout = EG(bailout);
132 state->active_fiber = EG(active_fiber);
133 #ifdef ZEND_CHECK_STACK_LIMIT
134 state->stack_base = EG(stack_base);
135 state->stack_limit = EG(stack_limit);
136 #endif
137 }
138
zend_fiber_restore_vm_state(zend_fiber_vm_state * state)139 static zend_always_inline void zend_fiber_restore_vm_state(zend_fiber_vm_state *state)
140 {
141 EG(vm_stack) = state->vm_stack;
142 EG(vm_stack_top) = state->vm_stack_top;
143 EG(vm_stack_end) = state->vm_stack_end;
144 EG(vm_stack_page_size) = state->vm_stack_page_size;
145 EG(current_execute_data) = state->current_execute_data;
146 EG(error_reporting) = state->error_reporting;
147 EG(jit_trace_num) = state->jit_trace_num;
148 EG(bailout) = state->bailout;
149 EG(active_fiber) = state->active_fiber;
150 #ifdef ZEND_CHECK_STACK_LIMIT
151 EG(stack_base) = state->stack_base;
152 EG(stack_limit) = state->stack_limit;
153 #endif
154 }
155
156 #ifdef ZEND_FIBER_UCONTEXT
157 ZEND_TLS zend_fiber_transfer *transfer_data;
158 #else
159 /* boost_context_data is our customized definition of struct transfer_t as
160 * provided by boost.context in fcontext.hpp:
161 *
162 * typedef void* fcontext_t;
163 *
164 * struct transfer_t {
165 * fcontext_t fctx;
166 * void *data;
167 * }; */
168
169 typedef struct {
170 void *handle;
171 zend_fiber_transfer *transfer;
172 } boost_context_data;
173
174 /* These functions are defined in assembler files provided by boost.context (located in "Zend/asm"). */
175 extern void *make_fcontext(void *sp, size_t size, void (*fn)(boost_context_data));
176 extern ZEND_INDIRECT_RETURN boost_context_data jump_fcontext(void *to, zend_fiber_transfer *transfer);
177 #endif
178
179 ZEND_API zend_class_entry *zend_ce_fiber;
180 static zend_class_entry *zend_ce_fiber_error;
181
182 static zend_object_handlers zend_fiber_handlers;
183
184 static zend_function zend_fiber_function = { ZEND_INTERNAL_FUNCTION };
185
186 ZEND_TLS uint32_t zend_fiber_switch_blocking = 0;
187
188 #define ZEND_FIBER_DEFAULT_PAGE_SIZE 4096
189
zend_fiber_get_page_size(void)190 static size_t zend_fiber_get_page_size(void)
191 {
192 static size_t page_size = 0;
193
194 if (!page_size) {
195 page_size = zend_get_page_size();
196 if (!page_size || (page_size & (page_size - 1))) {
197 /* anyway, we have to return a valid result */
198 page_size = ZEND_FIBER_DEFAULT_PAGE_SIZE;
199 }
200 }
201
202 return page_size;
203 }
204
zend_fiber_stack_allocate(size_t size)205 static zend_fiber_stack *zend_fiber_stack_allocate(size_t size)
206 {
207 void *pointer;
208 const size_t page_size = zend_fiber_get_page_size();
209 const size_t minimum_stack_size = page_size + ZEND_FIBER_GUARD_PAGES * page_size;
210
211 if (size < minimum_stack_size) {
212 zend_throw_exception_ex(NULL, 0, "Fiber stack size is too small, it needs to be at least %zu bytes", minimum_stack_size);
213 return NULL;
214 }
215
216 const size_t stack_size = (size + page_size - 1) / page_size * page_size;
217 const size_t alloc_size = stack_size + ZEND_FIBER_GUARD_PAGES * page_size;
218
219 #ifdef ZEND_WIN32
220 pointer = VirtualAlloc(0, alloc_size, MEM_COMMIT, PAGE_READWRITE);
221
222 if (!pointer) {
223 DWORD err = GetLastError();
224 char *errmsg = php_win32_error_to_msg(err);
225 zend_throw_exception_ex(NULL, 0, "Fiber stack allocate failed: VirtualAlloc failed: [0x%08lx] %s", err, errmsg[0] ? errmsg : "Unknown");
226 php_win32_error_msg_free(errmsg);
227 return NULL;
228 }
229
230 # if ZEND_FIBER_GUARD_PAGES
231 DWORD protect;
232
233 if (!VirtualProtect(pointer, ZEND_FIBER_GUARD_PAGES * page_size, PAGE_READWRITE | PAGE_GUARD, &protect)) {
234 DWORD err = GetLastError();
235 char *errmsg = php_win32_error_to_msg(err);
236 zend_throw_exception_ex(NULL, 0, "Fiber stack protect failed: VirtualProtect failed: [0x%08lx] %s", err, errmsg[0] ? errmsg : "Unknown");
237 php_win32_error_msg_free(errmsg);
238 VirtualFree(pointer, 0, MEM_RELEASE);
239 return NULL;
240 }
241 # endif
242 #else
243 pointer = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
244
245 if (pointer == MAP_FAILED) {
246 zend_throw_exception_ex(NULL, 0, "Fiber stack allocate failed: mmap failed: %s (%d)", strerror(errno), errno);
247 return NULL;
248 }
249
250 zend_mmap_set_name(pointer, alloc_size, "zend_fiber_stack");
251
252 # if ZEND_FIBER_GUARD_PAGES
253 if (mprotect(pointer, ZEND_FIBER_GUARD_PAGES * page_size, PROT_NONE) < 0) {
254 zend_throw_exception_ex(NULL, 0, "Fiber stack protect failed: mprotect failed: %s (%d)", strerror(errno), errno);
255 munmap(pointer, alloc_size);
256 return NULL;
257 }
258 # endif
259 #endif
260
261 zend_fiber_stack *stack = emalloc(sizeof(zend_fiber_stack));
262
263 stack->pointer = (void *) ((uintptr_t) pointer + ZEND_FIBER_GUARD_PAGES * page_size);
264 stack->size = stack_size;
265
266 #if !defined(ZEND_FIBER_UCONTEXT) && BOOST_CONTEXT_SHADOW_STACK
267 /* shadow stack saves ret address only, need less space */
268 stack->ss_size= stack_size >> 5;
269
270 /* align shadow stack to 8 bytes. */
271 stack->ss_size = (stack->ss_size + 7) & ~7;
272
273 /* issue syscall to create shadow stack for the new fcontext */
274 /* SHADOW_STACK_SET_TOKEN option will put "restore token" on the new shadow stack */
275 stack->ss_base = (void *)syscall(__NR_map_shadow_stack, 0, stack->ss_size, SHADOW_STACK_SET_TOKEN);
276
277 if (stack->ss_base == MAP_FAILED) {
278 zend_throw_exception_ex(NULL, 0, "Fiber shadow stack allocate failed: mmap failed: %s (%d)", strerror(errno), errno);
279 return NULL;
280 }
281 #endif
282
283 #ifdef VALGRIND_STACK_REGISTER
284 uintptr_t base = (uintptr_t) stack->pointer;
285 stack->valgrind_stack_id = VALGRIND_STACK_REGISTER(base, base + stack->size);
286 #endif
287
288 #ifdef __SANITIZE_ADDRESS__
289 stack->asan_pointer = stack->pointer;
290 stack->asan_size = stack->size;
291 #endif
292
293 return stack;
294 }
295
zend_fiber_stack_free(zend_fiber_stack * stack)296 static void zend_fiber_stack_free(zend_fiber_stack *stack)
297 {
298 #ifdef VALGRIND_STACK_DEREGISTER
299 VALGRIND_STACK_DEREGISTER(stack->valgrind_stack_id);
300 #endif
301
302 const size_t page_size = zend_fiber_get_page_size();
303
304 void *pointer = (void *) ((uintptr_t) stack->pointer - ZEND_FIBER_GUARD_PAGES * page_size);
305
306 #ifdef __SANITIZE_ADDRESS__
307 /* If another mmap happens after unmapping, it may trigger the stale stack red zones
308 * so we have to unpoison it before unmapping. */
309 ASAN_UNPOISON_MEMORY_REGION(pointer, stack->size + ZEND_FIBER_GUARD_PAGES * page_size);
310 #endif
311
312 #ifdef ZEND_WIN32
313 VirtualFree(pointer, 0, MEM_RELEASE);
314 #else
315 munmap(pointer, stack->size + ZEND_FIBER_GUARD_PAGES * page_size);
316 #endif
317
318 #if !defined(ZEND_FIBER_UCONTEXT) && BOOST_CONTEXT_SHADOW_STACK
319 munmap(stack->ss_base, stack->ss_size);
320 #endif
321
322 efree(stack);
323 }
324
325 #ifdef ZEND_CHECK_STACK_LIMIT
zend_fiber_stack_limit(zend_fiber_stack * stack)326 ZEND_API void* zend_fiber_stack_limit(zend_fiber_stack *stack)
327 {
328 zend_ulong reserve = EG(reserved_stack_size);
329
330 #ifdef __APPLE__
331 /* On Apple Clang, the stack probing function ___chkstk_darwin incorrectly
332 * probes a location that is twice the entered function's stack usage away
333 * from the stack pointer, when using an alternative stack.
334 * https://openradar.appspot.com/radar?id=5497722702397440
335 */
336 reserve = reserve * 2;
337 #endif
338
339 /* stack->pointer is the end of the stack */
340 return (int8_t*)stack->pointer + reserve;
341 }
342
zend_fiber_stack_base(zend_fiber_stack * stack)343 ZEND_API void* zend_fiber_stack_base(zend_fiber_stack *stack)
344 {
345 return (void*)((uintptr_t)stack->pointer + stack->size);
346 }
347 #endif
348
349 #ifdef ZEND_FIBER_UCONTEXT
zend_fiber_trampoline(void)350 static ZEND_NORETURN void zend_fiber_trampoline(void)
351 #else
352 static ZEND_NORETURN void zend_fiber_trampoline(boost_context_data data)
353 #endif
354 {
355 /* Initialize transfer struct with a copy of passed data. */
356 #ifdef ZEND_FIBER_UCONTEXT
357 zend_fiber_transfer transfer = *transfer_data;
358 #else
359 zend_fiber_transfer transfer = *data.transfer;
360 #endif
361
362 zend_fiber_context *from = transfer.context;
363
364 #ifdef __SANITIZE_ADDRESS__
365 __sanitizer_finish_switch_fiber(NULL, &from->stack->asan_pointer, &from->stack->asan_size);
366 #endif
367
368 #ifndef ZEND_FIBER_UCONTEXT
369 /* Get the context that resumed us and update its handle to allow for symmetric coroutines. */
370 from->handle = data.handle;
371 #endif
372
373 /* Ensure that previous fiber will be cleaned up (needed by symmetric coroutines). */
374 if (from->status == ZEND_FIBER_STATUS_DEAD) {
375 zend_fiber_destroy_context(from);
376 }
377
378 zend_fiber_context *context = EG(current_fiber_context);
379
380 context->function(&transfer);
381 context->status = ZEND_FIBER_STATUS_DEAD;
382
383 /* Final context switch, the fiber must not be resumed afterwards! */
384 zend_fiber_switch_context(&transfer);
385
386 /* Abort here because we are in an inconsistent program state. */
387 abort();
388 }
389
zend_fiber_switch_block(void)390 ZEND_API void zend_fiber_switch_block(void)
391 {
392 ++zend_fiber_switch_blocking;
393 }
394
zend_fiber_switch_unblock(void)395 ZEND_API void zend_fiber_switch_unblock(void)
396 {
397 ZEND_ASSERT(zend_fiber_switch_blocking && "Fiber switching was not blocked");
398 --zend_fiber_switch_blocking;
399 }
400
zend_fiber_switch_blocked(void)401 ZEND_API bool zend_fiber_switch_blocked(void)
402 {
403 return zend_fiber_switch_blocking;
404 }
405
zend_fiber_init_context(zend_fiber_context * context,void * kind,zend_fiber_coroutine coroutine,size_t stack_size)406 ZEND_API zend_result zend_fiber_init_context(zend_fiber_context *context, void *kind, zend_fiber_coroutine coroutine, size_t stack_size)
407 {
408 context->stack = zend_fiber_stack_allocate(stack_size);
409
410 if (UNEXPECTED(!context->stack)) {
411 return FAILURE;
412 }
413
414 #ifdef ZEND_FIBER_UCONTEXT
415 ucontext_t *handle = &context->stack->ucontext;
416
417 getcontext(handle);
418
419 handle->uc_stack.ss_size = context->stack->size;
420 handle->uc_stack.ss_sp = context->stack->pointer;
421 handle->uc_stack.ss_flags = 0;
422 handle->uc_link = NULL;
423
424 makecontext(handle, (void (*)(void)) zend_fiber_trampoline, 0);
425
426 context->handle = handle;
427 #else
428 // Stack grows down, calculate the top of the stack. make_fcontext then shifts pointer to lower 16-byte boundary.
429 void *stack = (void *) ((uintptr_t) context->stack->pointer + context->stack->size);
430
431 #if BOOST_CONTEXT_SHADOW_STACK
432 // pass the shadow stack pointer to make_fcontext
433 // i.e., link the new shadow stack with the new fcontext
434 // TODO should be a better way?
435 *((unsigned long*) (stack - 8)) = (unsigned long)context->stack->ss_base + context->stack->ss_size;
436 #endif
437
438 context->handle = make_fcontext(stack, context->stack->size, zend_fiber_trampoline);
439 ZEND_ASSERT(context->handle != NULL && "make_fcontext() never returns NULL");
440 #endif
441
442 context->kind = kind;
443 context->function = coroutine;
444
445 // Set status in case memory has not been zeroed.
446 context->status = ZEND_FIBER_STATUS_INIT;
447
448 zend_observer_fiber_init_notify(context);
449
450 return SUCCESS;
451 }
452
zend_fiber_destroy_context(zend_fiber_context * context)453 ZEND_API void zend_fiber_destroy_context(zend_fiber_context *context)
454 {
455 zend_observer_fiber_destroy_notify(context);
456
457 if (context->cleanup) {
458 context->cleanup(context);
459 }
460
461 zend_fiber_stack_free(context->stack);
462 }
463
zend_fiber_switch_context(zend_fiber_transfer * transfer)464 ZEND_API void zend_fiber_switch_context(zend_fiber_transfer *transfer)
465 {
466 zend_fiber_context *from = EG(current_fiber_context);
467 zend_fiber_context *to = transfer->context;
468 zend_fiber_vm_state state;
469
470 ZEND_ASSERT(to && to->handle && to->status != ZEND_FIBER_STATUS_DEAD && "Invalid fiber context");
471 ZEND_ASSERT(from && "From fiber context must be present");
472 ZEND_ASSERT(to != from && "Cannot switch into the running fiber context");
473
474 /* Assert that all error transfers hold a Throwable value. */
475 ZEND_ASSERT((
476 !(transfer->flags & ZEND_FIBER_TRANSFER_FLAG_ERROR) ||
477 (Z_TYPE(transfer->value) == IS_OBJECT && (
478 zend_is_unwind_exit(Z_OBJ(transfer->value)) ||
479 zend_is_graceful_exit(Z_OBJ(transfer->value)) ||
480 instanceof_function(Z_OBJCE(transfer->value), zend_ce_throwable)
481 ))
482 ) && "Error transfer requires a throwable value");
483
484 zend_observer_fiber_switch_notify(from, to);
485
486 zend_fiber_capture_vm_state(&state);
487
488 to->status = ZEND_FIBER_STATUS_RUNNING;
489
490 if (EXPECTED(from->status == ZEND_FIBER_STATUS_RUNNING)) {
491 from->status = ZEND_FIBER_STATUS_SUSPENDED;
492 }
493
494 /* Update transfer context with the current fiber before switching. */
495 transfer->context = from;
496
497 EG(current_fiber_context) = to;
498
499 #ifdef __SANITIZE_ADDRESS__
500 void *fake_stack = NULL;
501 __sanitizer_start_switch_fiber(
502 from->status != ZEND_FIBER_STATUS_DEAD ? &fake_stack : NULL,
503 to->stack->asan_pointer,
504 to->stack->asan_size);
505 #endif
506
507 #ifdef ZEND_FIBER_UCONTEXT
508 transfer_data = transfer;
509
510 swapcontext(from->handle, to->handle);
511
512 /* Copy transfer struct because it might live on the other fiber's stack that will eventually be destroyed. */
513 *transfer = *transfer_data;
514 #else
515 boost_context_data data = jump_fcontext(to->handle, transfer);
516
517 /* Copy transfer struct because it might live on the other fiber's stack that will eventually be destroyed. */
518 *transfer = *data.transfer;
519 #endif
520
521 to = transfer->context;
522
523 #ifndef ZEND_FIBER_UCONTEXT
524 /* Get the context that resumed us and update its handle to allow for symmetric coroutines. */
525 to->handle = data.handle;
526 #endif
527
528 #ifdef __SANITIZE_ADDRESS__
529 __sanitizer_finish_switch_fiber(fake_stack, &to->stack->asan_pointer, &to->stack->asan_size);
530 #endif
531
532 EG(current_fiber_context) = from;
533
534 zend_fiber_restore_vm_state(&state);
535
536 /* Destroy prior context if it has been marked as dead. */
537 if (to->status == ZEND_FIBER_STATUS_DEAD) {
538 zend_fiber_destroy_context(to);
539 }
540 }
541
zend_fiber_cleanup(zend_fiber_context * context)542 static void zend_fiber_cleanup(zend_fiber_context *context)
543 {
544 zend_fiber *fiber = zend_fiber_from_context(context);
545
546 zend_vm_stack current_stack = EG(vm_stack);
547 EG(vm_stack) = fiber->vm_stack;
548 zend_vm_stack_destroy();
549 EG(vm_stack) = current_stack;
550 fiber->execute_data = NULL;
551 fiber->stack_bottom = NULL;
552 fiber->caller = NULL;
553 }
554
zend_fiber_execute(zend_fiber_transfer * transfer)555 static ZEND_STACK_ALIGNED void zend_fiber_execute(zend_fiber_transfer *transfer)
556 {
557 ZEND_ASSERT(Z_TYPE(transfer->value) == IS_NULL && "Initial transfer value to fiber context must be NULL");
558 ZEND_ASSERT(!transfer->flags && "No flags should be set on initial transfer");
559
560 zend_fiber *fiber = EG(active_fiber);
561
562 /* Determine the current error_reporting ini setting. */
563 zend_long error_reporting = INI_INT("error_reporting");
564 /* If error_reporting is 0 and not explicitly set to 0, INI_STR returns a null pointer. */
565 if (!error_reporting && !INI_STR("error_reporting")) {
566 error_reporting = E_ALL;
567 }
568
569 EG(vm_stack) = NULL;
570
571 zend_first_try {
572 zend_vm_stack stack = zend_vm_stack_new_page(ZEND_FIBER_VM_STACK_SIZE, NULL);
573 EG(vm_stack) = stack;
574 EG(vm_stack_top) = stack->top + ZEND_CALL_FRAME_SLOT;
575 EG(vm_stack_end) = stack->end;
576 EG(vm_stack_page_size) = ZEND_FIBER_VM_STACK_SIZE;
577
578 fiber->execute_data = (zend_execute_data *) stack->top;
579 fiber->stack_bottom = fiber->execute_data;
580
581 memset(fiber->execute_data, 0, sizeof(zend_execute_data));
582
583 fiber->execute_data->func = &zend_fiber_function;
584 fiber->stack_bottom->prev_execute_data = EG(current_execute_data);
585
586 EG(current_execute_data) = fiber->execute_data;
587 EG(jit_trace_num) = 0;
588 EG(error_reporting) = error_reporting;
589
590 #ifdef ZEND_CHECK_STACK_LIMIT
591 EG(stack_base) = zend_fiber_stack_base(fiber->context.stack);
592 EG(stack_limit) = zend_fiber_stack_limit(fiber->context.stack);
593 #endif
594
595 fiber->fci.retval = &fiber->result;
596
597 zend_call_function(&fiber->fci, &fiber->fci_cache);
598
599 /* Cleanup callback and unset field to prevent GC / duplicate dtor issues. */
600 zval_ptr_dtor(&fiber->fci.function_name);
601 ZVAL_UNDEF(&fiber->fci.function_name);
602
603 if (EG(exception)) {
604 if (!(fiber->flags & ZEND_FIBER_FLAG_DESTROYED)
605 || !(zend_is_graceful_exit(EG(exception)) || zend_is_unwind_exit(EG(exception)))
606 ) {
607 fiber->flags |= ZEND_FIBER_FLAG_THREW;
608 transfer->flags = ZEND_FIBER_TRANSFER_FLAG_ERROR;
609
610 ZVAL_OBJ_COPY(&transfer->value, EG(exception));
611 }
612
613 zend_clear_exception();
614 }
615 } zend_catch {
616 fiber->flags |= ZEND_FIBER_FLAG_BAILOUT;
617 transfer->flags = ZEND_FIBER_TRANSFER_FLAG_BAILOUT;
618 } zend_end_try();
619
620 fiber->context.cleanup = &zend_fiber_cleanup;
621 fiber->vm_stack = EG(vm_stack);
622
623 transfer->context = fiber->caller;
624 }
625
626 /* Handles forwarding of result / error from a transfer into the running fiber. */
zend_fiber_delegate_transfer_result(zend_fiber_transfer * transfer,INTERNAL_FUNCTION_PARAMETERS)627 static zend_always_inline void zend_fiber_delegate_transfer_result(
628 zend_fiber_transfer *transfer, INTERNAL_FUNCTION_PARAMETERS
629 ) {
630 if (transfer->flags & ZEND_FIBER_TRANSFER_FLAG_ERROR) {
631 /* Use internal throw to skip the Throwable-check that would fail for (graceful) exit. */
632 zend_throw_exception_internal(Z_OBJ(transfer->value));
633 RETURN_THROWS();
634 }
635
636 RETURN_COPY_VALUE(&transfer->value);
637 }
638
zend_fiber_switch_to(zend_fiber_context * context,zval * value,bool exception)639 static zend_always_inline zend_fiber_transfer zend_fiber_switch_to(
640 zend_fiber_context *context, zval *value, bool exception
641 ) {
642 zend_fiber_transfer transfer = {
643 .context = context,
644 .flags = exception ? ZEND_FIBER_TRANSFER_FLAG_ERROR : 0,
645 };
646
647 if (value) {
648 ZVAL_COPY(&transfer.value, value);
649 } else {
650 ZVAL_NULL(&transfer.value);
651 }
652
653 zend_fiber_switch_context(&transfer);
654
655 /* Forward bailout into current fiber. */
656 if (UNEXPECTED(transfer.flags & ZEND_FIBER_TRANSFER_FLAG_BAILOUT)) {
657 EG(active_fiber) = NULL;
658 zend_bailout();
659 }
660
661 return transfer;
662 }
663
zend_fiber_resume(zend_fiber * fiber,zval * value,bool exception)664 static zend_always_inline zend_fiber_transfer zend_fiber_resume(zend_fiber *fiber, zval *value, bool exception)
665 {
666 zend_fiber *previous = EG(active_fiber);
667
668 if (previous) {
669 previous->execute_data = EG(current_execute_data);
670 }
671
672 fiber->caller = EG(current_fiber_context);
673 EG(active_fiber) = fiber;
674
675 zend_fiber_transfer transfer = zend_fiber_switch_to(fiber->previous, value, exception);
676
677 EG(active_fiber) = previous;
678
679 return transfer;
680 }
681
zend_fiber_suspend(zend_fiber * fiber,zval * value)682 static zend_always_inline zend_fiber_transfer zend_fiber_suspend(zend_fiber *fiber, zval *value)
683 {
684 ZEND_ASSERT(fiber->caller != NULL);
685
686 zend_fiber_context *caller = fiber->caller;
687 fiber->previous = EG(current_fiber_context);
688 fiber->caller = NULL;
689 fiber->execute_data = EG(current_execute_data);
690
691 return zend_fiber_switch_to(caller, value, false);
692 }
693
zend_fiber_object_create(zend_class_entry * ce)694 static zend_object *zend_fiber_object_create(zend_class_entry *ce)
695 {
696 zend_fiber *fiber = emalloc(sizeof(zend_fiber));
697 memset(fiber, 0, sizeof(zend_fiber));
698
699 zend_object_std_init(&fiber->std, ce);
700 return &fiber->std;
701 }
702
zend_fiber_object_destroy(zend_object * object)703 static void zend_fiber_object_destroy(zend_object *object)
704 {
705 zend_fiber *fiber = (zend_fiber *) object;
706
707 if (fiber->context.status != ZEND_FIBER_STATUS_SUSPENDED) {
708 return;
709 }
710
711 zend_object *exception = EG(exception);
712 EG(exception) = NULL;
713
714 zval graceful_exit;
715 ZVAL_OBJ(&graceful_exit, zend_create_graceful_exit());
716
717 fiber->flags |= ZEND_FIBER_FLAG_DESTROYED;
718
719 zend_fiber_transfer transfer = zend_fiber_resume(fiber, &graceful_exit, true);
720
721 zval_ptr_dtor(&graceful_exit);
722
723 if (transfer.flags & ZEND_FIBER_TRANSFER_FLAG_ERROR) {
724 EG(exception) = Z_OBJ(transfer.value);
725
726 if (!exception && EG(current_execute_data) && EG(current_execute_data)->func
727 && ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) {
728 zend_rethrow_exception(EG(current_execute_data));
729 }
730
731 zend_exception_set_previous(EG(exception), exception);
732
733 if (!EG(current_execute_data)) {
734 zend_exception_error(EG(exception), E_ERROR);
735 }
736 } else {
737 zval_ptr_dtor(&transfer.value);
738 EG(exception) = exception;
739 }
740 }
741
zend_fiber_object_free(zend_object * object)742 static void zend_fiber_object_free(zend_object *object)
743 {
744 zend_fiber *fiber = (zend_fiber *) object;
745
746 zval_ptr_dtor(&fiber->fci.function_name);
747 zval_ptr_dtor(&fiber->result);
748
749 zend_object_std_dtor(&fiber->std);
750 }
751
zend_fiber_object_gc(zend_object * object,zval ** table,int * num)752 static HashTable *zend_fiber_object_gc(zend_object *object, zval **table, int *num)
753 {
754 zend_fiber *fiber = (zend_fiber *) object;
755 zend_get_gc_buffer *buf = zend_get_gc_buffer_create();
756
757 zend_get_gc_buffer_add_zval(buf, &fiber->fci.function_name);
758 zend_get_gc_buffer_add_zval(buf, &fiber->result);
759
760 if (fiber->context.status != ZEND_FIBER_STATUS_SUSPENDED || fiber->caller != NULL) {
761 zend_get_gc_buffer_use(buf, table, num);
762 return NULL;
763 }
764
765 HashTable *lastSymTable = NULL;
766 zend_execute_data *ex = fiber->execute_data;
767 for (; ex; ex = ex->prev_execute_data) {
768 HashTable *symTable;
769 if (ZEND_CALL_INFO(ex) & ZEND_CALL_GENERATOR) {
770 /* The generator object is stored in ex->return_value */
771 zend_generator *generator = (zend_generator*)ex->return_value;
772 /* There are two cases to consider:
773 * - If the generator is currently running, the Generator's GC
774 * handler will ignore it because it is not collectable. However,
775 * in this context the generator is suspended in Fiber::suspend()
776 * and may be collectable, so we can inspect it.
777 * - If the generator is not running, the Generator's GC handler
778 * will inspect it. In this case we have to skip the frame.
779 */
780 if (!(generator->flags & ZEND_GENERATOR_CURRENTLY_RUNNING)) {
781 continue;
782 }
783 symTable = zend_generator_frame_gc(buf, generator);
784 } else {
785 symTable = zend_unfinished_execution_gc_ex(ex, ex->func && ZEND_USER_CODE(ex->func->type) ? ex->call : NULL, buf, false);
786 }
787 if (symTable) {
788 if (lastSymTable) {
789 zval *val;
790 ZEND_HASH_FOREACH_VAL(lastSymTable, val) {
791 if (EXPECTED(Z_TYPE_P(val) == IS_INDIRECT)) {
792 val = Z_INDIRECT_P(val);
793 }
794 zend_get_gc_buffer_add_zval(buf, val);
795 } ZEND_HASH_FOREACH_END();
796 }
797 lastSymTable = symTable;
798 }
799 }
800
801 zend_get_gc_buffer_use(buf, table, num);
802
803 return lastSymTable;
804 }
805
ZEND_METHOD(Fiber,__construct)806 ZEND_METHOD(Fiber, __construct)
807 {
808 zend_fcall_info fci;
809 zend_fcall_info_cache fcc;
810
811 ZEND_PARSE_PARAMETERS_START(1, 1)
812 Z_PARAM_FUNC(fci, fcc)
813 ZEND_PARSE_PARAMETERS_END();
814
815 zend_fiber *fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
816
817 if (UNEXPECTED(fiber->context.status != ZEND_FIBER_STATUS_INIT || Z_TYPE(fiber->fci.function_name) != IS_UNDEF)) {
818 zend_throw_error(zend_ce_fiber_error, "Cannot call constructor twice");
819 RETURN_THROWS();
820 }
821
822 fiber->fci = fci;
823 fiber->fci_cache = fcc;
824
825 // Keep a reference to closures or callable objects while the fiber is running.
826 Z_TRY_ADDREF(fiber->fci.function_name);
827 }
828
ZEND_METHOD(Fiber,start)829 ZEND_METHOD(Fiber, start)
830 {
831 zend_fiber *fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
832
833 ZEND_PARSE_PARAMETERS_START(0, -1)
834 Z_PARAM_VARIADIC_WITH_NAMED(fiber->fci.params, fiber->fci.param_count, fiber->fci.named_params);
835 ZEND_PARSE_PARAMETERS_END();
836
837 if (UNEXPECTED(zend_fiber_switch_blocked())) {
838 zend_throw_error(zend_ce_fiber_error, "Cannot switch fibers in current execution context");
839 RETURN_THROWS();
840 }
841
842 if (fiber->context.status != ZEND_FIBER_STATUS_INIT) {
843 zend_throw_error(zend_ce_fiber_error, "Cannot start a fiber that has already been started");
844 RETURN_THROWS();
845 }
846
847 if (zend_fiber_init_context(&fiber->context, zend_ce_fiber, zend_fiber_execute, EG(fiber_stack_size)) == FAILURE) {
848 RETURN_THROWS();
849 }
850
851 fiber->previous = &fiber->context;
852
853 zend_fiber_transfer transfer = zend_fiber_resume(fiber, NULL, false);
854
855 zend_fiber_delegate_transfer_result(&transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
856 }
857
ZEND_METHOD(Fiber,suspend)858 ZEND_METHOD(Fiber, suspend)
859 {
860 zval *value = NULL;
861
862 ZEND_PARSE_PARAMETERS_START(0, 1)
863 Z_PARAM_OPTIONAL
864 Z_PARAM_ZVAL(value);
865 ZEND_PARSE_PARAMETERS_END();
866
867 zend_fiber *fiber = EG(active_fiber);
868
869 if (UNEXPECTED(!fiber)) {
870 zend_throw_error(zend_ce_fiber_error, "Cannot suspend outside of a fiber");
871 RETURN_THROWS();
872 }
873
874 if (UNEXPECTED(fiber->flags & ZEND_FIBER_FLAG_DESTROYED)) {
875 zend_throw_error(zend_ce_fiber_error, "Cannot suspend in a force-closed fiber");
876 RETURN_THROWS();
877 }
878
879 if (UNEXPECTED(zend_fiber_switch_blocked())) {
880 zend_throw_error(zend_ce_fiber_error, "Cannot switch fibers in current execution context");
881 RETURN_THROWS();
882 }
883
884 ZEND_ASSERT(fiber->context.status == ZEND_FIBER_STATUS_RUNNING || fiber->context.status == ZEND_FIBER_STATUS_SUSPENDED);
885
886 fiber->stack_bottom->prev_execute_data = NULL;
887
888 zend_fiber_transfer transfer = zend_fiber_suspend(fiber, value);
889
890 zend_fiber_delegate_transfer_result(&transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
891 }
892
ZEND_METHOD(Fiber,resume)893 ZEND_METHOD(Fiber, resume)
894 {
895 zend_fiber *fiber;
896 zval *value = NULL;
897
898 ZEND_PARSE_PARAMETERS_START(0, 1)
899 Z_PARAM_OPTIONAL
900 Z_PARAM_ZVAL(value);
901 ZEND_PARSE_PARAMETERS_END();
902
903 if (UNEXPECTED(zend_fiber_switch_blocked())) {
904 zend_throw_error(zend_ce_fiber_error, "Cannot switch fibers in current execution context");
905 RETURN_THROWS();
906 }
907
908 fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
909
910 if (UNEXPECTED(fiber->context.status != ZEND_FIBER_STATUS_SUSPENDED || fiber->caller != NULL)) {
911 zend_throw_error(zend_ce_fiber_error, "Cannot resume a fiber that is not suspended");
912 RETURN_THROWS();
913 }
914
915 fiber->stack_bottom->prev_execute_data = EG(current_execute_data);
916
917 zend_fiber_transfer transfer = zend_fiber_resume(fiber, value, false);
918
919 zend_fiber_delegate_transfer_result(&transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
920 }
921
ZEND_METHOD(Fiber,throw)922 ZEND_METHOD(Fiber, throw)
923 {
924 zend_fiber *fiber;
925 zval *exception;
926
927 ZEND_PARSE_PARAMETERS_START(1, 1)
928 Z_PARAM_OBJECT_OF_CLASS(exception, zend_ce_throwable)
929 ZEND_PARSE_PARAMETERS_END();
930
931 if (UNEXPECTED(zend_fiber_switch_blocked())) {
932 zend_throw_error(zend_ce_fiber_error, "Cannot switch fibers in current execution context");
933 RETURN_THROWS();
934 }
935
936 fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
937
938 if (UNEXPECTED(fiber->context.status != ZEND_FIBER_STATUS_SUSPENDED || fiber->caller != NULL)) {
939 zend_throw_error(zend_ce_fiber_error, "Cannot resume a fiber that is not suspended");
940 RETURN_THROWS();
941 }
942
943 fiber->stack_bottom->prev_execute_data = EG(current_execute_data);
944
945 zend_fiber_transfer transfer = zend_fiber_resume(fiber, exception, true);
946
947 zend_fiber_delegate_transfer_result(&transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
948 }
949
ZEND_METHOD(Fiber,isStarted)950 ZEND_METHOD(Fiber, isStarted)
951 {
952 zend_fiber *fiber;
953
954 ZEND_PARSE_PARAMETERS_NONE();
955
956 fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
957
958 RETURN_BOOL(fiber->context.status != ZEND_FIBER_STATUS_INIT);
959 }
960
ZEND_METHOD(Fiber,isSuspended)961 ZEND_METHOD(Fiber, isSuspended)
962 {
963 zend_fiber *fiber;
964
965 ZEND_PARSE_PARAMETERS_NONE();
966
967 fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
968
969 RETURN_BOOL(fiber->context.status == ZEND_FIBER_STATUS_SUSPENDED && fiber->caller == NULL);
970 }
971
ZEND_METHOD(Fiber,isRunning)972 ZEND_METHOD(Fiber, isRunning)
973 {
974 zend_fiber *fiber;
975
976 ZEND_PARSE_PARAMETERS_NONE();
977
978 fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
979
980 RETURN_BOOL(fiber->context.status == ZEND_FIBER_STATUS_RUNNING || fiber->caller != NULL);
981 }
982
ZEND_METHOD(Fiber,isTerminated)983 ZEND_METHOD(Fiber, isTerminated)
984 {
985 zend_fiber *fiber;
986
987 ZEND_PARSE_PARAMETERS_NONE();
988
989 fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
990
991 RETURN_BOOL(fiber->context.status == ZEND_FIBER_STATUS_DEAD);
992 }
993
ZEND_METHOD(Fiber,getReturn)994 ZEND_METHOD(Fiber, getReturn)
995 {
996 zend_fiber *fiber;
997 const char *message;
998
999 ZEND_PARSE_PARAMETERS_NONE();
1000
1001 fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
1002
1003 if (fiber->context.status == ZEND_FIBER_STATUS_DEAD) {
1004 if (fiber->flags & ZEND_FIBER_FLAG_THREW) {
1005 message = "The fiber threw an exception";
1006 } else if (fiber->flags & ZEND_FIBER_FLAG_BAILOUT) {
1007 message = "The fiber exited with a fatal error";
1008 } else {
1009 RETURN_COPY_DEREF(&fiber->result);
1010 }
1011 } else if (fiber->context.status == ZEND_FIBER_STATUS_INIT) {
1012 message = "The fiber has not been started";
1013 } else {
1014 message = "The fiber has not returned";
1015 }
1016
1017 zend_throw_error(zend_ce_fiber_error, "Cannot get fiber return value: %s", message);
1018 RETURN_THROWS();
1019 }
1020
ZEND_METHOD(Fiber,getCurrent)1021 ZEND_METHOD(Fiber, getCurrent)
1022 {
1023 ZEND_PARSE_PARAMETERS_NONE();
1024
1025 zend_fiber *fiber = EG(active_fiber);
1026
1027 if (!fiber) {
1028 RETURN_NULL();
1029 }
1030
1031 RETURN_OBJ_COPY(&fiber->std);
1032 }
1033
ZEND_METHOD(FiberError,__construct)1034 ZEND_METHOD(FiberError, __construct)
1035 {
1036 zend_throw_error(
1037 NULL,
1038 "The \"%s\" class is reserved for internal use and cannot be manually instantiated",
1039 ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name)
1040 );
1041 }
1042
1043
zend_register_fiber_ce(void)1044 void zend_register_fiber_ce(void)
1045 {
1046 zend_ce_fiber = register_class_Fiber();
1047 zend_ce_fiber->create_object = zend_fiber_object_create;
1048 zend_ce_fiber->default_object_handlers = &zend_fiber_handlers;
1049
1050 zend_fiber_handlers = std_object_handlers;
1051 zend_fiber_handlers.dtor_obj = zend_fiber_object_destroy;
1052 zend_fiber_handlers.free_obj = zend_fiber_object_free;
1053 zend_fiber_handlers.get_gc = zend_fiber_object_gc;
1054 zend_fiber_handlers.clone_obj = NULL;
1055
1056 zend_ce_fiber_error = register_class_FiberError(zend_ce_error);
1057 zend_ce_fiber_error->create_object = zend_ce_error->create_object;
1058 }
1059
zend_fiber_init(void)1060 void zend_fiber_init(void)
1061 {
1062 zend_fiber_context *context = ecalloc(1, sizeof(zend_fiber_context));
1063
1064 #if defined(__SANITIZE_ADDRESS__) || defined(ZEND_FIBER_UCONTEXT)
1065 // Main fiber stack is only needed if ASan or ucontext is enabled.
1066 context->stack = emalloc(sizeof(zend_fiber_stack));
1067
1068 #ifdef ZEND_FIBER_UCONTEXT
1069 context->handle = &context->stack->ucontext;
1070 #endif
1071 #endif
1072
1073 context->status = ZEND_FIBER_STATUS_RUNNING;
1074
1075 EG(main_fiber_context) = context;
1076 EG(current_fiber_context) = context;
1077 EG(active_fiber) = NULL;
1078
1079 zend_fiber_switch_blocking = 0;
1080 }
1081
zend_fiber_shutdown(void)1082 void zend_fiber_shutdown(void)
1083 {
1084 #if defined(__SANITIZE_ADDRESS__) || defined(ZEND_FIBER_UCONTEXT)
1085 efree(EG(main_fiber_context)->stack);
1086 #endif
1087
1088 efree(EG(main_fiber_context));
1089
1090 zend_fiber_switch_block();
1091 }
1092