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 #ifdef ZEND_CHECK_STACK_LIMIT
100 EG(stack_base) = zend_fiber_stack_base(fiber->context.stack);
101 EG(stack_limit) = zend_fiber_stack_limit(fiber->context.stack);
102 #endif
103 fiber->fci.retval = &retval;
104
105 zend_call_function(&fiber->fci, &fiber->fci_cache);
106
107 zval_ptr_dtor(&fiber->result); // Destroy param from symmetric coroutine.
108 zval_ptr_dtor(&fiber->fci.function_name);
109
110 if (EG(exception)) {
111 if (!(fiber->flags & ZEND_FIBER_FLAG_DESTROYED)
112 || !(zend_is_graceful_exit(EG(exception)) || zend_is_unwind_exit(EG(exception)))
113 ) {
114 fiber->flags |= ZEND_FIBER_FLAG_THREW;
115 transfer->flags = ZEND_FIBER_TRANSFER_FLAG_ERROR;
116
117 ZVAL_OBJ_COPY(&transfer->value, EG(exception));
118 }
119
120 zend_clear_exception();
121 } else {
122 ZVAL_COPY_VALUE(&fiber->result, &retval);
123 ZVAL_COPY(&transfer->value, &fiber->result);
124 }
125 } zend_catch {
126 fiber->flags |= ZEND_FIBER_FLAG_BAILOUT;
127 transfer->flags = ZEND_FIBER_TRANSFER_FLAG_BAILOUT;
128 } zend_end_try();
129
130 zend_vm_stack_destroy();
131
132 if (fiber->target) {
133 zend_fiber_context *target = &fiber->target->context;
134 zend_fiber_init_context(target, zend_test_fiber_class, zend_test_fiber_execute, EG(fiber_stack_size));
135 transfer->context = target;
136
137 ZVAL_COPY(&fiber->target->result, &fiber->result);
138 fiber->target->fci.params = &fiber->target->result;
139 fiber->target->fci.param_count = 1;
140
141 fiber->target->caller = fiber->caller;
142 ZT_G(active_fiber) = fiber->target;
143 } else {
144 transfer->context = fiber->caller;
145 }
146
147 fiber->caller = NULL;
148 }
149
zend_test_fiber_object_create(zend_class_entry * ce)150 static zend_object *zend_test_fiber_object_create(zend_class_entry *ce)
151 {
152 zend_test_fiber *fiber;
153
154 fiber = emalloc(sizeof(zend_test_fiber));
155 memset(fiber, 0, sizeof(zend_test_fiber));
156
157 zend_object_std_init(&fiber->std, ce);
158
159 return &fiber->std;
160 }
161
zend_test_fiber_object_destroy(zend_object * object)162 static void zend_test_fiber_object_destroy(zend_object *object)
163 {
164 zend_test_fiber *fiber = (zend_test_fiber *) object;
165
166 if (fiber->context.status != ZEND_FIBER_STATUS_SUSPENDED) {
167 return;
168 }
169
170 zend_object *exception = EG(exception);
171 EG(exception) = NULL;
172
173 fiber->flags |= ZEND_FIBER_FLAG_DESTROYED;
174
175 zend_fiber_transfer transfer = zend_test_fiber_resume(fiber, NULL, false);
176
177 if (transfer.flags & ZEND_FIBER_TRANSFER_FLAG_ERROR) {
178 EG(exception) = Z_OBJ(transfer.value);
179
180 if (!exception && EG(current_execute_data) && EG(current_execute_data)->func
181 && ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) {
182 zend_rethrow_exception(EG(current_execute_data));
183 }
184
185 zend_exception_set_previous(EG(exception), exception);
186
187 if (!EG(current_execute_data)) {
188 zend_exception_error(EG(exception), E_ERROR);
189 }
190 } else {
191 zval_ptr_dtor(&transfer.value);
192 EG(exception) = exception;
193 }
194 }
195
zend_test_fiber_object_free(zend_object * object)196 static void zend_test_fiber_object_free(zend_object *object)
197 {
198 zend_test_fiber *fiber = (zend_test_fiber *) object;
199
200 if (fiber->context.status == ZEND_FIBER_STATUS_INIT) {
201 // Fiber was never started, so we need to release the reference to the callback.
202 zval_ptr_dtor(&fiber->fci.function_name);
203 }
204
205 if (fiber->target) {
206 OBJ_RELEASE(&fiber->target->std);
207 }
208
209 zval_ptr_dtor(&fiber->result);
210
211 zend_object_std_dtor(&fiber->std);
212 }
213
delegate_transfer_result(zend_test_fiber * fiber,zend_fiber_transfer * transfer,INTERNAL_FUNCTION_PARAMETERS)214 static zend_always_inline void delegate_transfer_result(
215 zend_test_fiber *fiber, zend_fiber_transfer *transfer, INTERNAL_FUNCTION_PARAMETERS
216 ) {
217 if (transfer->flags & ZEND_FIBER_TRANSFER_FLAG_ERROR) {
218 zend_throw_exception_internal(Z_OBJ(transfer->value));
219 RETURN_THROWS();
220 }
221
222 if (fiber->context.status == ZEND_FIBER_STATUS_DEAD) {
223 zval_ptr_dtor(&transfer->value);
224 RETURN_NULL();
225 }
226
227 RETURN_COPY_VALUE(&transfer->value);
228 }
229
ZEND_METHOD(_ZendTestFiber,__construct)230 static ZEND_METHOD(_ZendTestFiber, __construct)
231 {
232 zend_test_fiber *fiber = (zend_test_fiber *) Z_OBJ_P(ZEND_THIS);
233
234 ZEND_PARSE_PARAMETERS_START(1, 1)
235 Z_PARAM_FUNC(fiber->fci, fiber->fci_cache)
236 ZEND_PARSE_PARAMETERS_END();
237
238 // Keep a reference to closures or callable objects while the fiber is running.
239 Z_TRY_ADDREF(fiber->fci.function_name);
240 }
241
ZEND_METHOD(_ZendTestFiber,start)242 static ZEND_METHOD(_ZendTestFiber, start)
243 {
244 zend_test_fiber *fiber = (zend_test_fiber *) Z_OBJ_P(ZEND_THIS);
245 zval *params;
246 uint32_t param_count;
247 zend_array *named_params;
248
249 ZEND_PARSE_PARAMETERS_START(0, -1)
250 Z_PARAM_VARIADIC_WITH_NAMED(params, param_count, named_params);
251 ZEND_PARSE_PARAMETERS_END();
252
253 ZEND_ASSERT(fiber->context.status == ZEND_FIBER_STATUS_INIT);
254
255 if (fiber->previous != NULL) {
256 zend_throw_error(NULL, "Cannot start a fiber that is the target of another fiber");
257 RETURN_THROWS();
258 }
259
260 fiber->fci.params = params;
261 fiber->fci.param_count = param_count;
262 fiber->fci.named_params = named_params;
263
264 zend_fiber_init_context(&fiber->context, zend_test_fiber_class, zend_test_fiber_execute, EG(fiber_stack_size));
265
266 fiber->previous = &fiber->context;
267
268 zend_fiber_transfer transfer = zend_test_fiber_resume(fiber, NULL, false);
269
270 delegate_transfer_result(fiber, &transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
271 }
272
ZEND_METHOD(_ZendTestFiber,suspend)273 static ZEND_METHOD(_ZendTestFiber, suspend)
274 {
275 zval *value = NULL;
276
277 ZEND_PARSE_PARAMETERS_START(0, 1)
278 Z_PARAM_OPTIONAL
279 Z_PARAM_ZVAL(value);
280 ZEND_PARSE_PARAMETERS_END();
281
282 zend_test_fiber *fiber = ZT_G(active_fiber);
283
284 ZEND_ASSERT(fiber);
285
286 zend_fiber_transfer transfer = zend_test_fiber_suspend(fiber, value);
287
288 if (fiber->flags & ZEND_FIBER_FLAG_DESTROYED) {
289 // This occurs when the test fiber is GC'ed while suspended.
290 zval_ptr_dtor(&transfer.value);
291 zend_throw_graceful_exit();
292 RETURN_THROWS();
293 }
294
295 delegate_transfer_result(fiber, &transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
296 }
297
ZEND_METHOD(_ZendTestFiber,resume)298 static ZEND_METHOD(_ZendTestFiber, resume)
299 {
300 zend_test_fiber *fiber;
301 zval *value = NULL;
302
303 ZEND_PARSE_PARAMETERS_START(0, 1)
304 Z_PARAM_OPTIONAL
305 Z_PARAM_ZVAL(value);
306 ZEND_PARSE_PARAMETERS_END();
307
308 fiber = (zend_test_fiber *) Z_OBJ_P(ZEND_THIS);
309
310 if (UNEXPECTED(fiber->context.status != ZEND_FIBER_STATUS_SUSPENDED || fiber->caller != NULL)) {
311 zend_throw_error(NULL, "Cannot resume a fiber that is not suspended");
312 RETURN_THROWS();
313 }
314
315 zend_fiber_transfer transfer = zend_test_fiber_resume(fiber, value, false);
316
317 delegate_transfer_result(fiber, &transfer, INTERNAL_FUNCTION_PARAM_PASSTHRU);
318 }
319
ZEND_METHOD(_ZendTestFiber,pipeTo)320 static ZEND_METHOD(_ZendTestFiber, pipeTo)
321 {
322 zend_fcall_info fci;
323 zend_fcall_info_cache fci_cache;
324
325 ZEND_PARSE_PARAMETERS_START(1, 1)
326 Z_PARAM_FUNC(fci, fci_cache)
327 ZEND_PARSE_PARAMETERS_END();
328
329 zend_test_fiber *fiber = (zend_test_fiber *) Z_OBJ_P(ZEND_THIS);
330 zend_test_fiber *target = (zend_test_fiber *) zend_test_fiber_class->create_object(zend_test_fiber_class);
331
332 target->fci = fci;
333 target->fci_cache = fci_cache;
334 Z_TRY_ADDREF(target->fci.function_name);
335
336 target->previous = &fiber->context;
337
338 if (fiber->target) {
339 OBJ_RELEASE(&fiber->target->std);
340 }
341
342 fiber->target = target;
343
344 RETURN_OBJ_COPY(&target->std);
345 }
346
zend_test_fiber_init(void)347 void zend_test_fiber_init(void)
348 {
349 zend_test_fiber_class = register_class__ZendTestFiber();
350 zend_test_fiber_class->create_object = zend_test_fiber_object_create;
351 zend_test_fiber_class->default_object_handlers = &zend_test_fiber_handlers;
352
353 zend_test_fiber_handlers = std_object_handlers;
354 zend_test_fiber_handlers.dtor_obj = zend_test_fiber_object_destroy;
355 zend_test_fiber_handlers.free_obj = zend_test_fiber_object_free;
356 zend_test_fiber_handlers.clone_obj = NULL;
357 }
358