xref: /PHP-8.2/ext/zend_test/fiber.c (revision 5955ce89)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Authors: Aaron Piotrowski <aaron@trowski.com>                         |
14   +----------------------------------------------------------------------+
15 */
16 
17 #include "php_test.h"
18 #include "fiber.h"
19 #include "fiber_arginfo.h"
20 #include "zend_fibers.h"
21 #include "zend_exceptions.h"
22 
23 static zend_class_entry *zend_test_fiber_class;
24 static zend_object_handlers zend_test_fiber_handlers;
25 
zend_test_fiber_switch_to(zend_fiber_context * context,zval * value,bool exception)26 static zend_fiber_transfer zend_test_fiber_switch_to(zend_fiber_context *context, zval *value, bool exception)
27 {
28 	zend_fiber_transfer transfer = {
29 			.context = context,
30 			.flags = exception ? ZEND_FIBER_TRANSFER_FLAG_ERROR : 0,
31 	};
32 
33 	if (value) {
34 		ZVAL_COPY(&transfer.value, value);
35 	} else {
36 		ZVAL_NULL(&transfer.value);
37 	}
38 
39 	zend_fiber_switch_context(&transfer);
40 
41 	/* Forward bailout into current fiber. */
42 	if (UNEXPECTED(transfer.flags & ZEND_FIBER_TRANSFER_FLAG_BAILOUT)) {
43 		zend_bailout();
44 	}
45 
46 	return transfer;
47 }
48 
zend_test_fiber_resume(zend_test_fiber * fiber,zval * value,bool exception)49 static zend_fiber_transfer zend_test_fiber_resume(zend_test_fiber *fiber, zval *value, bool exception)
50 {
51 	zend_test_fiber *previous = ZT_G(active_fiber);
52 
53 	fiber->caller = EG(current_fiber_context);
54 	ZT_G(active_fiber) = fiber;
55 
56 	zend_fiber_transfer transfer = zend_test_fiber_switch_to(fiber->previous, value, exception);
57 
58 	ZT_G(active_fiber) = previous;
59 
60 	return transfer;
61 }
62 
zend_test_fiber_suspend(zend_test_fiber * fiber,zval * value)63 static zend_fiber_transfer zend_test_fiber_suspend(zend_test_fiber *fiber, zval *value)
64 {
65 	ZEND_ASSERT(fiber->caller != NULL);
66 
67 	zend_fiber_context *caller = fiber->caller;
68 	fiber->previous = EG(current_fiber_context);
69 	fiber->caller = NULL;
70 
71 	return zend_test_fiber_switch_to(caller, value, false);
72 }
73 
zend_test_fiber_execute(zend_fiber_transfer * transfer)74 static ZEND_STACK_ALIGNED void zend_test_fiber_execute(zend_fiber_transfer *transfer)
75 {
76 	zend_test_fiber *fiber = ZT_G(active_fiber);
77 	zval retval;
78 
79 	zend_execute_data *execute_data;
80 
81 	EG(vm_stack) = NULL;
82 	transfer->flags = 0;
83 
84 	zend_first_try {
85 		zend_vm_stack stack = zend_vm_stack_new_page(ZEND_FIBER_VM_STACK_SIZE, NULL);
86 		EG(vm_stack) = stack;
87 		EG(vm_stack_top) = stack->top + ZEND_CALL_FRAME_SLOT;
88 		EG(vm_stack_end) = stack->end;
89 		EG(vm_stack_page_size) = ZEND_FIBER_VM_STACK_SIZE;
90 
91 		execute_data = (zend_execute_data *) stack->top;
92 
93 		memset(execute_data, 0, sizeof(zend_execute_data));
94 		execute_data->func = (zend_function *) &zend_pass_function;
95 
96 		EG(current_execute_data) = execute_data;
97 		EG(jit_trace_num) = 0;
98 
99 		fiber->fci.retval = &retval;
100 
101 		zend_call_function(&fiber->fci, &fiber->fci_cache);
102 
103 		zval_ptr_dtor(&fiber->result); // Destroy param from symmetric coroutine.
104 		zval_ptr_dtor(&fiber->fci.function_name);
105 
106 		if (EG(exception)) {
107 			if (!(fiber->flags & ZEND_FIBER_FLAG_DESTROYED)
108 				|| !(zend_is_graceful_exit(EG(exception)) || zend_is_unwind_exit(EG(exception)))
109 			) {
110 				fiber->flags |= ZEND_FIBER_FLAG_THREW;
111 				transfer->flags = ZEND_FIBER_TRANSFER_FLAG_ERROR;
112 
113 				ZVAL_OBJ_COPY(&transfer->value, EG(exception));
114 			}
115 
116 			zend_clear_exception();
117 		} else {
118 			ZVAL_COPY_VALUE(&fiber->result, &retval);
119 			ZVAL_COPY(&transfer->value, &fiber->result);
120 		}
121 	} zend_catch {
122 		fiber->flags |= ZEND_FIBER_FLAG_BAILOUT;
123 		transfer->flags = ZEND_FIBER_TRANSFER_FLAG_BAILOUT;
124 	} zend_end_try();
125 
126 	zend_vm_stack_destroy();
127 
128 	if (fiber->target) {
129 		zend_fiber_context *target = &fiber->target->context;
130 		zend_fiber_init_context(target, zend_test_fiber_class, zend_test_fiber_execute, EG(fiber_stack_size));
131 		transfer->context = target;
132 
133 		ZVAL_COPY(&fiber->target->result, &fiber->result);
134 		fiber->target->fci.params = &fiber->target->result;
135 		fiber->target->fci.param_count = 1;
136 
137 		fiber->target->caller = fiber->caller;
138 		ZT_G(active_fiber) = fiber->target;
139 	} else {
140 		transfer->context = fiber->caller;
141 	}
142 
143 	fiber->caller = NULL;
144 }
145 
zend_test_fiber_object_create(zend_class_entry * ce)146 static zend_object *zend_test_fiber_object_create(zend_class_entry *ce)
147 {
148 	zend_test_fiber *fiber;
149 
150 	fiber = emalloc(sizeof(zend_test_fiber));
151 	memset(fiber, 0, sizeof(zend_test_fiber));
152 
153 	zend_object_std_init(&fiber->std, ce);
154 	fiber->std.handlers = &zend_test_fiber_handlers;
155 
156 	return &fiber->std;
157 }
158 
zend_test_fiber_object_destroy(zend_object * object)159 static void zend_test_fiber_object_destroy(zend_object *object)
160 {
161 	zend_test_fiber *fiber = (zend_test_fiber *) object;
162 
163 	if (fiber->context.status != ZEND_FIBER_STATUS_SUSPENDED) {
164 		return;
165 	}
166 
167 	zend_object *exception = EG(exception);
168 	EG(exception) = NULL;
169 
170 	fiber->flags |= ZEND_FIBER_FLAG_DESTROYED;
171 
172 	zend_fiber_transfer transfer = zend_test_fiber_resume(fiber, NULL, false);
173 
174 	if (transfer.flags & ZEND_FIBER_TRANSFER_FLAG_ERROR) {
175 		EG(exception) = Z_OBJ(transfer.value);
176 
177 		if (!exception && EG(current_execute_data) && EG(current_execute_data)->func
178 			&& ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) {
179 			zend_rethrow_exception(EG(current_execute_data));
180 		}
181 
182 		zend_exception_set_previous(EG(exception), exception);
183 
184 		if (!EG(current_execute_data)) {
185 			zend_exception_error(EG(exception), E_ERROR);
186 		}
187 	} else {
188 		zval_ptr_dtor(&transfer.value);
189 		EG(exception) = exception;
190 	}
191 }
192 
zend_test_fiber_object_free(zend_object * object)193 static void zend_test_fiber_object_free(zend_object *object)
194 {
195 	zend_test_fiber *fiber = (zend_test_fiber *) object;
196 
197 	if (fiber->context.status == ZEND_FIBER_STATUS_INIT) {
198 		// Fiber was never started, so we need to release the reference to the callback.
199 		zval_ptr_dtor(&fiber->fci.function_name);
200 	}
201 
202 	if (fiber->target) {
203 		OBJ_RELEASE(&fiber->target->std);
204 	}
205 
206 	zval_ptr_dtor(&fiber->result);
207 
208 	zend_object_std_dtor(&fiber->std);
209 }
210 
delegate_transfer_result(zend_test_fiber * fiber,zend_fiber_transfer * transfer,INTERNAL_FUNCTION_PARAMETERS)211 static zend_always_inline void delegate_transfer_result(
212 	zend_test_fiber *fiber, zend_fiber_transfer *transfer, INTERNAL_FUNCTION_PARAMETERS
213 ) {
214 	if (transfer->flags & ZEND_FIBER_TRANSFER_FLAG_ERROR) {
215 		zend_throw_exception_internal(Z_OBJ(transfer->value));
216 		RETURN_THROWS();
217 	}
218 
219 	if (fiber->context.status == ZEND_FIBER_STATUS_DEAD) {
220 		zval_ptr_dtor(&transfer->value);
221 		RETURN_NULL();
222 	}
223 
224 	RETURN_COPY_VALUE(&transfer->value);
225 }
226 
ZEND_METHOD(_ZendTestFiber,__construct)227 static ZEND_METHOD(_ZendTestFiber, __construct)
228 {
229 	zend_test_fiber *fiber = (zend_test_fiber *) Z_OBJ_P(getThis());
230 
231 	ZEND_PARSE_PARAMETERS_START(1, 1)
232 		Z_PARAM_FUNC(fiber->fci, fiber->fci_cache)
233 	ZEND_PARSE_PARAMETERS_END();
234 
235 	// Keep a reference to closures or callable objects while the fiber is running.
236 	Z_TRY_ADDREF(fiber->fci.function_name);
237 }
238 
ZEND_METHOD(_ZendTestFiber,start)239 static ZEND_METHOD(_ZendTestFiber, start)
240 {
241 	zend_test_fiber *fiber = (zend_test_fiber *) Z_OBJ_P(getThis());
242 	zval *params;
243 	uint32_t param_count;
244 	zend_array *named_params;
245 
246 	ZEND_PARSE_PARAMETERS_START(0, -1)
247 		Z_PARAM_VARIADIC_WITH_NAMED(params, param_count, named_params);
248 	ZEND_PARSE_PARAMETERS_END();
249 
250 	ZEND_ASSERT(fiber->context.status == ZEND_FIBER_STATUS_INIT);
251 
252 	if (fiber->previous != NULL) {
253 		zend_throw_error(NULL, "Cannot start a fiber that is the target of another fiber");
254 		RETURN_THROWS();
255 	}
256 
257 	fiber->fci.params = params;
258 	fiber->fci.param_count = param_count;
259 	fiber->fci.named_params = named_params;
260 
261 	zend_fiber_init_context(&fiber->context, zend_test_fiber_class, zend_test_fiber_execute, EG(fiber_stack_size));
262 
263 	fiber->previous = &fiber->context;
264 
265 	zend_fiber_transfer transfer = zend_test_fiber_resume(fiber, NULL, false);
266 
267 	delegate_transfer_result(fiber, &transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
268 }
269 
ZEND_METHOD(_ZendTestFiber,suspend)270 static ZEND_METHOD(_ZendTestFiber, suspend)
271 {
272 	zval *value = NULL;
273 
274 	ZEND_PARSE_PARAMETERS_START(0, 1)
275 		Z_PARAM_OPTIONAL
276 		Z_PARAM_ZVAL(value);
277 	ZEND_PARSE_PARAMETERS_END();
278 
279 	zend_test_fiber *fiber = ZT_G(active_fiber);
280 
281 	ZEND_ASSERT(fiber);
282 
283 	zend_fiber_transfer transfer = zend_test_fiber_suspend(fiber, value);
284 
285 	if (fiber->flags & ZEND_FIBER_FLAG_DESTROYED) {
286 		// This occurs when the test fiber is GC'ed while suspended.
287 		zval_ptr_dtor(&transfer.value);
288 		zend_throw_graceful_exit();
289 		RETURN_THROWS();
290 	}
291 
292 	delegate_transfer_result(fiber, &transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
293 }
294 
ZEND_METHOD(_ZendTestFiber,resume)295 static ZEND_METHOD(_ZendTestFiber, resume)
296 {
297 	zend_test_fiber *fiber;
298 	zval *value = NULL;
299 
300 	ZEND_PARSE_PARAMETERS_START(0, 1)
301 		Z_PARAM_OPTIONAL
302 		Z_PARAM_ZVAL(value);
303 	ZEND_PARSE_PARAMETERS_END();
304 
305 	fiber = (zend_test_fiber *) Z_OBJ_P(getThis());
306 
307 	if (UNEXPECTED(fiber->context.status != ZEND_FIBER_STATUS_SUSPENDED || fiber->caller != NULL)) {
308 		zend_throw_error(NULL, "Cannot resume a fiber that is not suspended");
309 		RETURN_THROWS();
310 	}
311 
312 	zend_fiber_transfer transfer = zend_test_fiber_resume(fiber, value, false);
313 
314 	delegate_transfer_result(fiber, &transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
315 }
316 
ZEND_METHOD(_ZendTestFiber,pipeTo)317 static ZEND_METHOD(_ZendTestFiber, pipeTo)
318 {
319 	zend_fcall_info fci;
320 	zend_fcall_info_cache fci_cache;
321 
322 	ZEND_PARSE_PARAMETERS_START(1, 1)
323 		Z_PARAM_FUNC(fci, fci_cache)
324 	ZEND_PARSE_PARAMETERS_END();
325 
326 	zend_test_fiber *fiber = (zend_test_fiber *) Z_OBJ_P(getThis());
327 	zend_test_fiber *target = (zend_test_fiber *) zend_test_fiber_class->create_object(zend_test_fiber_class);
328 
329 	target->fci = fci;
330 	target->fci_cache = fci_cache;
331 	Z_TRY_ADDREF(target->fci.function_name);
332 
333 	target->previous = &fiber->context;
334 
335 	if (fiber->target) {
336 		OBJ_RELEASE(&fiber->target->std);
337 	}
338 
339 	fiber->target = target;
340 
341 	RETURN_OBJ_COPY(&target->std);
342 }
343 
zend_test_fiber_init(void)344 void zend_test_fiber_init(void)
345 {
346 	zend_test_fiber_class = register_class__ZendTestFiber();
347 	zend_test_fiber_class->create_object = zend_test_fiber_object_create;
348 
349 	zend_test_fiber_handlers = std_object_handlers;
350 	zend_test_fiber_handlers.dtor_obj = zend_test_fiber_object_destroy;
351 	zend_test_fiber_handlers.free_obj = zend_test_fiber_object_free;
352 	zend_test_fiber_handlers.clone_obj = NULL;
353 }
354