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