Coverage Report

Created: 2026-06-02 06:40

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