Coverage Report

Created: 2026-06-02 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_closures.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: Christian Seiler <chris_se@gmx.net>                         |
15
   |          Dmitry Stogov <dmitry@php.net>                              |
16
   |          Marcus Boerger <helly@php.net>                              |
17
   +----------------------------------------------------------------------+
18
*/
19
20
#include "zend.h"
21
#include "zend_API.h"
22
#include "zend_closures.h"
23
#include "zend_exceptions.h"
24
#include "zend_interfaces.h"
25
#include "zend_objects.h"
26
#include "zend_objects_API.h"
27
#include "zend_globals.h"
28
#include "zend_closures_arginfo.h"
29
30
typedef struct _zend_closure {
31
  zend_object       std;
32
  zend_function     func;
33
  zval              this_ptr;
34
  zend_class_entry *called_scope;
35
  zif_handler       orig_internal_handler;
36
} zend_closure;
37
38
/* non-static since it needs to be referenced */
39
ZEND_API zend_class_entry *zend_ce_closure;
40
static zend_object_handlers closure_handlers;
41
42
static zend_result zend_closure_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only);
43
44
ZEND_METHOD(Closure, __invoke) /* {{{ */
45
120
{
46
120
  zend_function *func = EX(func);
47
120
  zval *args;
48
120
  uint32_t num_args;
49
120
  HashTable *named_args;
50
51
360
  ZEND_PARSE_PARAMETERS_START(0, -1)
52
360
    Z_PARAM_VARIADIC_WITH_NAMED(args, num_args, named_args)
53
360
  ZEND_PARSE_PARAMETERS_END();
54
55
120
  zend_fcall_info_cache fcc = {
56
120
    .closure = Z_OBJ_P(ZEND_THIS),
57
120
  };
58
120
  zend_closure_get_closure(Z_OBJ_P(ZEND_THIS), &fcc.calling_scope, &fcc.function_handler, &fcc.object, false);
59
120
  fcc.called_scope = fcc.calling_scope;
60
120
  zend_call_known_fcc(&fcc, return_value, num_args, args, named_args);
61
62
  /* destruct the function also, then - we have allocated it in get_method */
63
120
  zend_string_release_ex(func->internal_function.function_name, 0);
64
120
  efree(func);
65
66
  /* Set the func pointer to NULL. Prior to PHP 8.3, this was only done for debug builds,
67
   * because debug builds check certain properties after the call and needed to know this
68
   * had been freed.
69
   * However, extensions can proxy zend_execute_internal, and it's a bit surprising to have
70
   * an invalid func pointer sitting on there, so this was changed in PHP 8.3.
71
   */
72
120
  execute_data->func = NULL;
73
120
}
74
/* }}} */
75
76
static bool zend_valid_closure_binding(
77
    zend_closure *closure, zval *newthis, zend_class_entry *scope) /* {{{ */
78
1.30k
{
79
1.30k
  zend_function *func = &closure->func;
80
1.30k
  bool is_fake_closure = (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) != 0;
81
1.30k
  if (newthis) {
82
1.16k
    if (func->common.fn_flags & ZEND_ACC_STATIC) {
83
21
      zend_error(E_WARNING, "Cannot bind an instance to a static closure, this will be an error in PHP 9");
84
21
      return false;
85
21
    }
86
87
1.14k
    if (is_fake_closure && func->common.scope &&
88
78
        !instanceof_function(Z_OBJCE_P(newthis), func->common.scope)) {
89
      /* Binding incompatible $this to an internal method is not supported. */
90
6
      zend_error(E_WARNING, "Cannot bind method %s::%s() to object of class %s, this will be an error in PHP 9",
91
6
          ZSTR_VAL(func->common.scope->name),
92
6
          ZSTR_VAL(func->common.function_name),
93
6
          ZSTR_VAL(Z_OBJCE_P(newthis)->name));
94
6
      return false;
95
6
    }
96
1.14k
  } else if (is_fake_closure && func->common.scope
97
3
      && !(func->common.fn_flags & ZEND_ACC_STATIC)) {
98
3
    zend_error(E_WARNING, "Cannot unbind $this of method, this will be an error in PHP 9");
99
3
    return false;
100
141
  } else if (!is_fake_closure && !Z_ISUNDEF(closure->this_ptr)
101
18
      && (func->common.fn_flags & ZEND_ACC_USES_THIS)) {
102
15
    zend_error(E_WARNING, "Cannot unbind $this of closure using $this, this will be an error in PHP 9");
103
15
    return false;
104
15
  }
105
106
1.26k
  if (scope && scope != func->common.scope && scope->type == ZEND_INTERNAL_CLASS) {
107
    /* rebinding to internal class is not allowed */
108
327
    zend_error(E_WARNING, "Cannot bind closure to scope of internal class %s, this will be an error in PHP 9",
109
327
        ZSTR_VAL(scope->name));
110
327
    return false;
111
327
  }
112
113
933
  if (is_fake_closure && scope != func->common.scope) {
114
6
    if (func->common.scope == NULL) {
115
3
      zend_error(E_WARNING, "Cannot rebind scope of closure created from function, this will be an error in PHP 9");
116
3
    } else {
117
3
      zend_error(E_WARNING, "Cannot rebind scope of closure created from method, this will be an error in PHP 9");
118
3
    }
119
6
    return false;
120
6
  }
121
122
927
  return true;
123
933
}
124
/* }}} */
125
126
/* {{{ Call closure, binding to a given object with its class as the scope */
127
ZEND_METHOD(Closure, call)
128
966
{
129
966
  zval *newthis, closure_result;
130
966
  zend_closure *closure;
131
966
  zend_fcall_info fci;
132
966
  zend_fcall_info_cache fci_cache;
133
966
  zend_object *newobj;
134
966
  zend_class_entry *newclass;
135
136
966
  fci.param_count = 0;
137
966
  fci.params = NULL;
138
139
2.89k
  ZEND_PARSE_PARAMETERS_START(1, -1)
140
3.86k
    Z_PARAM_OBJECT(newthis)
141
963
    Z_PARAM_VARIADIC_WITH_NAMED(fci.params, fci.param_count, fci.named_params)
142
966
  ZEND_PARSE_PARAMETERS_END();
143
144
963
  closure = (zend_closure *) Z_OBJ_P(ZEND_THIS);
145
146
963
  newobj = Z_OBJ_P(newthis);
147
963
  newclass = newobj->ce;
148
149
963
  if (!zend_valid_closure_binding(closure, newthis, newclass)) {
150
333
    return;
151
333
  }
152
153
630
  fci_cache.called_scope = newclass;
154
630
  fci_cache.object = fci.object = newobj;
155
156
630
  fci.size = sizeof(fci);
157
630
  fci.consumed_args = 0;
158
630
  ZVAL_OBJ(&fci.function_name, &closure->std);
159
630
  ZVAL_UNDEF(&closure_result);
160
630
  fci.retval = &closure_result;
161
162
630
  if (closure->func.common.fn_flags & ZEND_ACC_GENERATOR) {
163
3
    zval new_closure;
164
3
    zend_create_closure(&new_closure, &closure->func, newclass, closure->called_scope, newthis);
165
3
    closure = (zend_closure *) Z_OBJ(new_closure);
166
3
    fci_cache.function_handler = &closure->func;
167
168
3
    zend_call_function(&fci, &fci_cache);
169
170
    /* copied upon generator creation */
171
3
    GC_DELREF(&closure->std);
172
627
  } else {
173
627
    zend_closure *fake_closure;
174
627
    zend_function *my_function;
175
176
627
    fake_closure = emalloc(sizeof(zend_closure));
177
627
    memset(&fake_closure->std, 0, sizeof(fake_closure->std));
178
627
    fake_closure->std.gc.refcount = 1;
179
627
    fake_closure->std.gc.u.type_info = GC_NULL;
180
627
    ZVAL_UNDEF(&fake_closure->this_ptr);
181
627
    fake_closure->called_scope = NULL;
182
627
    my_function = &fake_closure->func;
183
627
    if (ZEND_USER_CODE(closure->func.type)) {
184
621
      memcpy(my_function, &closure->func, sizeof(zend_op_array));
185
621
    } else {
186
6
      memcpy(my_function, &closure->func, sizeof(zend_internal_function));
187
6
    }
188
    /* use scope of passed object */
189
627
    my_function->common.scope = newclass;
190
627
    if (closure->func.type == ZEND_INTERNAL_FUNCTION) {
191
6
      my_function->internal_function.handler = closure->orig_internal_handler;
192
6
    }
193
627
    fci_cache.function_handler = my_function;
194
195
    /* Runtime cache relies on bound scope to be immutable, hence we need a separate rt cache in case scope changed */
196
627
    if (ZEND_USER_CODE(my_function->type)
197
621
     && (closure->func.common.scope != newclass
198
621
      || (closure->func.common.fn_flags & ZEND_ACC_HEAP_RT_CACHE))) {
199
621
      void *ptr;
200
201
621
      my_function->op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
202
621
      ptr = emalloc(my_function->op_array.cache_size);
203
621
      ZEND_MAP_PTR_INIT(my_function->op_array.run_time_cache, ptr);
204
621
      memset(ptr, 0, my_function->op_array.cache_size);
205
621
    }
206
207
627
    zend_call_function(&fci, &fci_cache);
208
209
627
    if (ZEND_USER_CODE(my_function->type)) {
210
621
      if (fci_cache.function_handler->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE) {
211
621
        efree(ZEND_MAP_PTR(my_function->op_array.run_time_cache));
212
621
      }
213
621
    }
214
627
    efree_size(fake_closure, sizeof(zend_closure));
215
627
  }
216
217
630
  if (Z_TYPE(closure_result) != IS_UNDEF) {
218
630
    if (Z_ISREF(closure_result)) {
219
3
      zend_unwrap_reference(&closure_result);
220
3
    }
221
630
    ZVAL_COPY_VALUE(return_value, &closure_result);
222
630
  }
223
630
}
224
/* }}} */
225
226
static void do_closure_bind(zval *return_value, zval *zclosure, zval *newthis, zend_object *scope_obj, zend_string *scope_str)
227
345
{
228
345
  zend_class_entry *ce, *called_scope;
229
345
  zend_closure *closure = (zend_closure *) Z_OBJ_P(zclosure);
230
231
345
  if (scope_obj) {
232
15
    ce = scope_obj->ce;
233
330
  } else if (scope_str) {
234
294
    if (zend_string_equals(scope_str, ZSTR_KNOWN(ZEND_STR_STATIC))) {
235
156
      ce = closure->func.common.scope;
236
156
    } else if ((ce = zend_lookup_class(scope_str)) == NULL) {
237
3
      zend_error(E_WARNING, "Class \"%s\" not found", ZSTR_VAL(scope_str));
238
3
      RETURN_NULL();
239
3
    }
240
294
  } else {
241
36
    ce = NULL;
242
36
  }
243
244
342
  if (!zend_valid_closure_binding(closure, newthis, ce)) {
245
45
    return;
246
45
  }
247
248
297
  if (newthis) {
249
171
    called_scope = Z_OBJCE_P(newthis);
250
171
  } else {
251
126
    called_scope = ce;
252
126
  }
253
254
297
  zend_create_closure(return_value, &closure->func, ce, called_scope, newthis);
255
297
}
256
257
/* {{{ Create a closure from another one and bind to another object and scope */
258
ZEND_METHOD(Closure, bind)
259
54
{
260
54
  zval *zclosure, *newthis;
261
54
  zend_object *scope_obj = NULL;
262
54
  zend_string *scope_str = ZSTR_KNOWN(ZEND_STR_STATIC);
263
264
162
  ZEND_PARSE_PARAMETERS_START(2, 3)
265
216
    Z_PARAM_OBJECT_OF_CLASS(zclosure, zend_ce_closure)
266
270
    Z_PARAM_OBJECT_OR_NULL(newthis)
267
54
    Z_PARAM_OPTIONAL
268
261
    Z_PARAM_OBJ_OR_STR_OR_NULL(scope_obj, scope_str)
269
261
  ZEND_PARSE_PARAMETERS_END();
270
271
54
  do_closure_bind(return_value, zclosure, newthis, scope_obj, scope_str);
272
54
}
273
274
/* {{{ Create a closure from another one and bind to another object and scope */
275
ZEND_METHOD(Closure, bindTo)
276
294
{
277
294
  zval *newthis;
278
294
  zend_object *scope_obj = NULL;
279
294
  zend_string *scope_str = ZSTR_KNOWN(ZEND_STR_STATIC);
280
281
882
  ZEND_PARSE_PARAMETERS_START(1, 2)
282
1.17k
    Z_PARAM_OBJECT_OR_NULL(newthis)
283
294
    Z_PARAM_OPTIONAL
284
1.05k
    Z_PARAM_OBJ_OR_STR_OR_NULL(scope_obj, scope_str)
285
1.05k
  ZEND_PARSE_PARAMETERS_END();
286
287
291
  do_closure_bind(return_value, ZEND_THIS, newthis, scope_obj, scope_str);
288
291
}
289
290
static void zend_copy_parameters_array(const uint32_t param_count, HashTable *argument_array) /* {{{ */
291
90
{
292
90
  zval *param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
293
294
90
  ZEND_ASSERT(param_count <= ZEND_CALL_NUM_ARGS(EG(current_execute_data)));
295
296
318
  for (uint32_t i = 0; i < param_count; i++) {
297
228
    Z_TRY_ADDREF_P(param_ptr);
298
228
    zend_hash_next_index_insert_new(argument_array, param_ptr);
299
228
    param_ptr++;
300
228
  }
301
90
}
302
303
186
static ZEND_NAMED_FUNCTION(zend_closure_call_magic) /* {{{ */ {
304
186
  zend_fcall_info fci;
305
186
  zend_fcall_info_cache fcc;
306
186
  zval params[2];
307
308
186
  memset(&fci, 0, sizeof(zend_fcall_info));
309
186
  memset(&fcc, 0, sizeof(zend_fcall_info_cache));
310
311
186
  fci.size = sizeof(zend_fcall_info);
312
186
  fci.retval = return_value;
313
314
186
  fcc.function_handler = (EX(func)->internal_function.fn_flags & ZEND_ACC_STATIC) ?
315
150
    EX(func)->internal_function.scope->__callstatic : EX(func)->internal_function.scope->__call;
316
186
  fci.named_params = NULL;
317
186
  fci.params = params;
318
186
  fci.param_count = 2;
319
186
  ZVAL_STR(&fci.params[0], EX(func)->common.function_name);
320
186
  if (EX_CALL_INFO() & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
321
12
    zend_string *name;
322
12
    zval *named_param_zval;
323
12
    array_init_size(&fci.params[1], ZEND_NUM_ARGS() + zend_hash_num_elements(EX(extra_named_params)));
324
    /* Avoid conversion from packed to mixed later. */
325
12
    zend_hash_real_init_mixed(Z_ARRVAL(fci.params[1]));
326
12
    zend_copy_parameters_array(ZEND_NUM_ARGS(), Z_ARRVAL(fci.params[1]));
327
72
    ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(EX(extra_named_params), name, named_param_zval) {
328
72
      Z_TRY_ADDREF_P(named_param_zval);
329
72
      zend_hash_add_new(Z_ARRVAL(fci.params[1]), name, named_param_zval);
330
72
    } ZEND_HASH_FOREACH_END();
331
174
  } else if (ZEND_NUM_ARGS()) {
332
78
    array_init_size(&fci.params[1], ZEND_NUM_ARGS());
333
78
    zend_copy_parameters_array(ZEND_NUM_ARGS(), Z_ARRVAL(fci.params[1]));
334
96
  } else {
335
96
    ZVAL_EMPTY_ARRAY(&fci.params[1]);
336
96
  }
337
338
186
  fcc.object = fci.object = Z_OBJ_P(ZEND_THIS);
339
186
  fcc.called_scope = zend_get_called_scope(EG(current_execute_data));
340
341
186
  zend_call_function(&fci, &fcc);
342
186
  zend_return_unwrap_ref(EG(current_execute_data), return_value);
343
186
  zval_ptr_dtor(&fci.params[1]);
344
186
}
345
/* }}} */
346
347
312
static zend_result zend_create_closure_from_callable(zval *return_value, zval *callable, char **error) /* {{{ */ {
348
312
  zend_fcall_info_cache fcc;
349
312
  zend_function *mptr;
350
312
  zval instance;
351
312
  zend_internal_function call;
352
353
312
  if (!zend_is_callable_ex(callable, NULL, 0, NULL, &fcc, error)) {
354
33
    return FAILURE;
355
33
  }
356
357
279
  mptr = fcc.function_handler;
358
279
  if (mptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
359
    /* For Closure::fromCallable([$closure, "__invoke"]) return $closure. */
360
75
    if (fcc.object && fcc.object->ce == zend_ce_closure
361
15
        && zend_string_equals(mptr->common.function_name, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) {
362
15
      RETVAL_OBJ_COPY(fcc.object);
363
15
      zend_free_trampoline(mptr);
364
15
      return SUCCESS;
365
15
    }
366
367
60
    if (!mptr->common.scope) {
368
0
      return FAILURE;
369
0
    }
370
60
    if (mptr->common.fn_flags & ZEND_ACC_STATIC) {
371
6
      if (!mptr->common.scope->__callstatic) {
372
0
        return FAILURE;
373
0
      }
374
54
    } else {
375
54
      if (!mptr->common.scope->__call) {
376
0
        return FAILURE;
377
0
      }
378
54
    }
379
380
60
    memset(&call, 0, sizeof(zend_internal_function));
381
60
    call.type = ZEND_INTERNAL_FUNCTION;
382
60
    call.fn_flags = mptr->common.fn_flags & (ZEND_ACC_STATIC | ZEND_ACC_DEPRECATED);
383
60
    call.handler = zend_closure_call_magic;
384
60
    call.function_name = mptr->common.function_name;
385
60
    call.scope = mptr->common.scope;
386
60
    call.doc_comment = NULL;
387
60
    call.attributes = mptr->common.attributes;
388
389
60
    zend_free_trampoline(mptr);
390
60
    mptr = (zend_function *) &call;
391
60
  }
392
393
264
  if (fcc.object) {
394
108
    ZVAL_OBJ(&instance, fcc.object);
395
108
    zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, &instance);
396
156
  } else {
397
156
    zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, NULL);
398
156
  }
399
400
264
  if (&mptr->internal_function == &call) {
401
60
    zend_string_release(mptr->common.function_name);
402
60
  }
403
404
264
  return SUCCESS;
405
279
}
406
/* }}} */
407
408
/* {{{ Create a closure from a callable using the current scope. */
409
ZEND_METHOD(Closure, fromCallable)
410
315
{
411
315
  zval *callable;
412
315
  char *error = NULL;
413
414
945
  ZEND_PARSE_PARAMETERS_START(1, 1)
415
1.26k
    Z_PARAM_ZVAL(callable)
416
1.26k
  ZEND_PARSE_PARAMETERS_END();
417
418
315
  if (Z_TYPE_P(callable) == IS_OBJECT && instanceof_function(Z_OBJCE_P(callable), zend_ce_closure)) {
419
    /* It's already a closure */
420
3
    RETURN_COPY(callable);
421
3
  }
422
423
312
  if (zend_create_closure_from_callable(return_value, callable, &error) == FAILURE) {
424
33
    if (error) {
425
33
      zend_type_error("Failed to create closure from callable: %s", error);
426
33
      efree(error);
427
33
    } else {
428
0
      zend_type_error("Failed to create closure from callable");
429
0
    }
430
33
  }
431
312
}
432
/* }}} */
433
434
ZEND_METHOD(Closure, getCurrent)
435
98
{
436
98
  ZEND_PARSE_PARAMETERS_NONE();
437
438
98
  zend_execute_data *prev_ex = EX(prev_execute_data);
439
440
98
  if (!prev_ex
441
98
   || !prev_ex->func
442
98
   || (prev_ex->func->common.fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_FAKE_CLOSURE)) != ZEND_ACC_CLOSURE) {
443
6
      zend_throw_error(NULL, "Current function is not a closure");
444
6
      RETURN_THROWS();
445
6
  }
446
447
92
  zend_object *obj = ZEND_CLOSURE_OBJECT(prev_ex->func);
448
92
  RETURN_OBJ_COPY(obj);
449
92
}
450
451
static ZEND_COLD zend_function *zend_closure_get_constructor(zend_object *object) /* {{{ */
452
5
{
453
5
  zend_throw_error(NULL, "Instantiation of class Closure is not allowed");
454
5
  return NULL;
455
5
}
456
/* }}} */
457
458
/* int return due to Object Handler API */
459
static int zend_closure_compare(zval *o1, zval *o2) /* {{{ */
460
63
{
461
63
  ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
462
463
63
  zend_closure *lhs = (zend_closure*) Z_OBJ_P(o1);
464
63
  zend_closure *rhs = (zend_closure*) Z_OBJ_P(o2);
465
466
63
  if (!((lhs->func.common.fn_flags & ZEND_ACC_FAKE_CLOSURE) && (rhs->func.common.fn_flags & ZEND_ACC_FAKE_CLOSURE))) {
467
0
    return ZEND_UNCOMPARABLE;
468
0
  }
469
470
63
  if (Z_TYPE(lhs->this_ptr) != Z_TYPE(rhs->this_ptr)) {
471
0
    return ZEND_UNCOMPARABLE;
472
0
  }
473
474
63
  if (Z_TYPE(lhs->this_ptr) == IS_OBJECT && Z_OBJ(lhs->this_ptr) != Z_OBJ(rhs->this_ptr)) {
475
12
    return ZEND_UNCOMPARABLE;
476
12
  }
477
478
51
  if (lhs->called_scope != rhs->called_scope) {
479
6
    return ZEND_UNCOMPARABLE;
480
6
  }
481
482
45
  if (lhs->func.type != rhs->func.type) {
483
0
    return ZEND_UNCOMPARABLE;
484
0
  }
485
486
45
  if (lhs->func.common.scope != rhs->func.common.scope) {
487
0
    return ZEND_UNCOMPARABLE;
488
0
  }
489
490
45
  if (!zend_string_equals(lhs->func.common.function_name, rhs->func.common.function_name)) {
491
18
    return ZEND_UNCOMPARABLE;
492
18
  }
493
494
27
  return 0;
495
45
}
496
/* }}} */
497
498
ZEND_API zend_function *zend_get_closure_invoke_method(zend_object *object) /* {{{ */
499
213
{
500
213
  zend_closure *closure = (zend_closure *)object;
501
213
  zend_function *invoke = (zend_function*)emalloc(sizeof(zend_function));
502
213
  const uint32_t keep_flags =
503
213
    ZEND_ACC_RETURN_REFERENCE | ZEND_ACC_VARIADIC | ZEND_ACC_HAS_RETURN_TYPE | ZEND_ACC_DEPRECATED;
504
505
213
  invoke->common = closure->func.common;
506
  /* We return ZEND_INTERNAL_FUNCTION, but arg_info representation is the
507
   * same as for ZEND_USER_FUNCTION (uses zend_string* instead of char*).
508
   * This is not a problem, because ZEND_ACC_HAS_TYPE_HINTS is never set,
509
   * and we won't check arguments on internal function. We also set
510
   * ZEND_ACC_USER_ARG_INFO flag to prevent invalid usage by Reflection */
511
213
  invoke->type = ZEND_INTERNAL_FUNCTION;
512
213
  invoke->internal_function.fn_flags =
513
213
    ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER | ZEND_ACC_NEVER_CACHE | (closure->func.common.fn_flags & keep_flags);
514
213
  if (closure->func.type != ZEND_INTERNAL_FUNCTION || (closure->func.common.fn_flags & ZEND_ACC_USER_ARG_INFO)) {
515
183
    invoke->internal_function.fn_flags |=
516
183
      ZEND_ACC_USER_ARG_INFO;
517
183
  }
518
213
  invoke->internal_function.handler = ZEND_MN(Closure___invoke);
519
213
  invoke->internal_function.doc_comment = NULL;
520
213
  invoke->internal_function.module = 0;
521
213
  invoke->internal_function.scope = zend_ce_closure;
522
213
  invoke->internal_function.function_name = ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE);
523
213
  return invoke;
524
213
}
525
/* }}} */
526
527
ZEND_API const zend_function *zend_get_closure_method_def(zend_object *obj) /* {{{ */
528
282
{
529
282
  zend_closure *closure = (zend_closure *) obj;
530
282
  return &closure->func;
531
282
}
532
/* }}} */
533
534
ZEND_API zval* zend_get_closure_this_ptr(zval *obj) /* {{{ */
535
3
{
536
3
  zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
537
3
  return &closure->this_ptr;
538
3
}
539
/* }}} */
540
541
static zend_function *zend_closure_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */
542
1.20k
{
543
1.20k
  if (zend_string_equals_literal_ci(method, ZEND_INVOKE_FUNC_NAME)) {
544
201
    return zend_get_closure_invoke_method(*object);
545
201
  }
546
547
999
  return zend_std_get_method(object, method, key);
548
1.20k
}
549
/* }}} */
550
551
static void zend_closure_free_storage(zend_object *object) /* {{{ */
552
7.55k
{
553
7.55k
  zend_closure *closure = (zend_closure *)object;
554
555
7.55k
  zend_object_std_dtor(&closure->std);
556
557
7.55k
  if (closure->func.type == ZEND_USER_FUNCTION) {
558
    /* We don't own the static variables of fake closures. */
559
7.13k
    if (!(closure->func.op_array.fn_flags & ZEND_ACC_FAKE_CLOSURE)) {
560
6.77k
      zend_destroy_static_vars(&closure->func.op_array);
561
6.77k
    }
562
7.13k
    destroy_op_array(&closure->func.op_array);
563
7.13k
  } else if (closure->func.type == ZEND_INTERNAL_FUNCTION) {
564
417
    zend_string_release(closure->func.common.function_name);
565
417
  }
566
567
7.55k
  if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
568
1.67k
    zval_ptr_dtor(&closure->this_ptr);
569
1.67k
  }
570
7.55k
}
571
/* }}} */
572
573
static zend_object *zend_closure_new(zend_class_entry *class_type) /* {{{ */
574
7.55k
{
575
7.55k
  zend_closure *closure;
576
577
7.55k
  closure = emalloc(sizeof(zend_closure));
578
7.55k
  memset(closure, 0, sizeof(zend_closure));
579
580
7.55k
  zend_object_std_init(&closure->std, class_type);
581
582
7.55k
  return (zend_object*)closure;
583
7.55k
}
584
/* }}} */
585
586
static zend_object *zend_closure_clone(zend_object *zobject) /* {{{ */
587
6
{
588
6
  zend_closure *closure = (zend_closure *)zobject;
589
6
  zval result;
590
591
6
  zend_create_closure(&result, &closure->func,
592
6
    closure->func.common.scope, closure->called_scope, &closure->this_ptr);
593
6
  return Z_OBJ(result);
594
6
}
595
/* }}} */
596
597
static zend_result zend_closure_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only) /* {{{ */
598
8.76k
{
599
8.76k
  zend_closure *closure = (zend_closure*)obj;
600
601
8.76k
  *fptr_ptr = &closure->func;
602
8.76k
  *ce_ptr = closure->called_scope;
603
604
8.76k
  if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
605
958
    *obj_ptr = Z_OBJ(closure->this_ptr);
606
7.80k
  } else {
607
7.80k
    *obj_ptr = NULL;
608
7.80k
  }
609
610
8.76k
  return SUCCESS;
611
8.76k
}
612
/* }}} */
613
614
/* *is_temp is int due to Object Handler API */
615
static HashTable *zend_closure_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
616
876
{
617
876
  zend_closure *closure = (zend_closure *)object;
618
876
  zval val;
619
876
  struct _zend_arg_info *arg_info = closure->func.common.arg_info;
620
876
  HashTable *debug_info;
621
622
876
  *is_temp = 1;
623
624
876
  debug_info = zend_new_array(8);
625
626
876
  if (closure->func.op_array.fn_flags & ZEND_ACC_FAKE_CLOSURE) {
627
108
    if (closure->func.common.scope) {
628
27
      zend_string *class_name = closure->func.common.scope->name;
629
27
      zend_string *func_name = closure->func.common.function_name;
630
27
      zend_string *combined = zend_string_concat3(
631
27
        ZSTR_VAL(class_name), ZSTR_LEN(class_name),
632
27
        "::", strlen("::"),
633
27
        ZSTR_VAL(func_name), ZSTR_LEN(func_name)
634
27
      );
635
27
      ZVAL_STR(&val, combined);
636
81
    } else {
637
81
      ZVAL_STR_COPY(&val, closure->func.common.function_name);
638
81
    }
639
108
    zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_FUNCTION), &val);
640
768
  } else {
641
768
    ZVAL_STR_COPY(&val, closure->func.common.function_name);
642
768
    zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_NAME), &val);
643
644
768
    ZVAL_STR_COPY(&val, closure->func.op_array.filename);
645
768
    zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_FILE), &val);
646
647
768
    ZVAL_LONG(&val, closure->func.op_array.line_start);
648
768
    zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_LINE), &val);
649
768
  }
650
651
876
  if (closure->func.type == ZEND_USER_FUNCTION && closure->func.op_array.static_variables) {
652
87
    zval *var;
653
87
    zend_string *key;
654
87
    HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr);
655
656
87
    array_init(&val);
657
658
369
    ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(static_variables, key, var) {
659
369
      zval copy;
660
661
369
      if (Z_ISREF_P(var) && Z_REFCOUNT_P(var) == 1) {
662
21
        var = Z_REFVAL_P(var);
663
21
      }
664
369
      ZVAL_COPY(&copy, var);
665
666
369
      zend_hash_add_new(Z_ARRVAL(val), key, &copy);
667
369
    } ZEND_HASH_FOREACH_END();
668
669
87
    if (zend_hash_num_elements(Z_ARRVAL(val))) {
670
75
      zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_STATIC), &val);
671
75
    } else {
672
12
      zval_ptr_dtor(&val);
673
12
    }
674
87
  }
675
676
876
  if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
677
594
    Z_ADDREF(closure->this_ptr);
678
594
    zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_THIS), &closure->this_ptr);
679
594
  }
680
681
876
  if (arg_info &&
682
102
    (closure->func.common.num_args ||
683
99
     (closure->func.common.fn_flags & ZEND_ACC_VARIADIC))) {
684
99
    uint32_t i, num_args, required = closure->func.common.required_num_args;
685
686
99
    array_init(&val);
687
688
99
    num_args = closure->func.common.num_args;
689
99
    if (closure->func.common.fn_flags & ZEND_ACC_VARIADIC) {
690
12
      num_args++;
691
12
    }
692
210
    for (i = 0; i < num_args; i++) {
693
111
      zend_string *name;
694
111
      zval info;
695
111
      ZEND_ASSERT(arg_info->name && "Argument should have name");
696
111
      name = zend_strpprintf(0, "%s$%s",
697
111
          ZEND_ARG_SEND_MODE(arg_info) ? "&" : "",
698
111
          ZSTR_VAL(arg_info->name));
699
111
      ZVAL_NEW_STR(&info, zend_strpprintf(0, "%s", i >= required ? "<optional>" : "<required>"));
700
111
      zend_hash_update(Z_ARRVAL(val), name, &info);
701
111
      zend_string_release_ex(name, 0);
702
111
      arg_info++;
703
111
    }
704
99
    zend_hash_str_update(debug_info, "parameter", sizeof("parameter")-1, &val);
705
99
  }
706
707
876
  return debug_info;
708
876
}
709
/* }}} */
710
711
static HashTable *zend_closure_get_gc(zend_object *obj, zval **table, int *n) /* {{{ */
712
6.18k
{
713
6.18k
  zend_closure *closure = (zend_closure *)obj;
714
715
6.18k
  *table = Z_TYPE(closure->this_ptr) != IS_NULL ? &closure->this_ptr : NULL;
716
6.18k
  *n = Z_TYPE(closure->this_ptr) != IS_NULL ? 1 : 0;
717
  /* Fake closures don't own the static variables they reference. */
718
6.18k
  return (closure->func.type == ZEND_USER_FUNCTION
719
6.02k
      && !(closure->func.op_array.fn_flags & ZEND_ACC_FAKE_CLOSURE)) ?
720
6.18k
    ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr) : NULL;
721
6.18k
}
722
/* }}} */
723
724
/* {{{ Private constructor preventing instantiation */
725
ZEND_COLD ZEND_METHOD(Closure, __construct)
726
0
{
727
0
  zend_throw_error(NULL, "Instantiation of class Closure is not allowed");
728
0
}
729
/* }}} */
730
731
void zend_register_closure_ce(void) /* {{{ */
732
2
{
733
2
  zend_ce_closure = register_class_Closure();
734
2
  zend_ce_closure->create_object = zend_closure_new;
735
2
  zend_ce_closure->default_object_handlers = &closure_handlers;
736
737
2
  memcpy(&closure_handlers, &std_object_handlers, sizeof(zend_object_handlers));
738
2
  closure_handlers.free_obj = zend_closure_free_storage;
739
2
  closure_handlers.get_constructor = zend_closure_get_constructor;
740
2
  closure_handlers.get_method = zend_closure_get_method;
741
2
  closure_handlers.compare = zend_closure_compare;
742
2
  closure_handlers.clone_obj = zend_closure_clone;
743
2
  closure_handlers.get_debug_info = zend_closure_get_debug_info;
744
2
  closure_handlers.get_closure = zend_closure_get_closure;
745
2
  closure_handlers.get_gc = zend_closure_get_gc;
746
2
}
747
/* }}} */
748
749
static ZEND_NAMED_FUNCTION(zend_closure_internal_handler) /* {{{ */
750
288
{
751
288
  zend_closure *closure = (zend_closure*)ZEND_CLOSURE_OBJECT(EX(func));
752
288
  closure->orig_internal_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
753
288
  ZEND_ASSERT(!(closure->func.common.fn_flags2 & ZEND_ACC2_FORBID_DYN_CALLS) || EG(exception));
754
  // Assign to EX(this) so that it is released after observer checks etc.
755
288
  ZEND_ADD_CALL_FLAG(execute_data, ZEND_CALL_RELEASE_THIS);
756
288
  Z_OBJ(EX(This)) = &closure->std;
757
288
}
758
/* }}} */
759
760
static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr, bool is_fake) /* {{{ */
761
7.55k
{
762
7.55k
  zend_closure *closure;
763
7.55k
  void *ptr;
764
765
7.55k
  object_init_ex(res, zend_ce_closure);
766
767
7.55k
  closure = (zend_closure *)Z_OBJ_P(res);
768
769
7.55k
  if ((scope == NULL) && this_ptr && (Z_TYPE_P(this_ptr) != IS_UNDEF)) {
770
    /* use dummy scope if we're binding an object without specifying a scope */
771
    /* maybe it would be better to create one for this purpose */
772
36
    scope = zend_ce_closure;
773
36
  }
774
775
7.55k
  if (func->type == ZEND_USER_FUNCTION) {
776
7.13k
    memcpy(&closure->func, func, sizeof(zend_op_array));
777
7.13k
    closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
778
7.13k
    closure->func.common.fn_flags &= ~ZEND_ACC_IMMUTABLE;
779
780
7.13k
    zend_string_addref(closure->func.op_array.function_name);
781
7.13k
    if (closure->func.op_array.refcount) {
782
32
      (*closure->func.op_array.refcount)++;
783
32
    }
784
785
    /* For fake closures, we want to reuse the static variables of the original function. */
786
7.13k
    HashTable *ht = ZEND_MAP_PTR_GET(func->op_array.static_variables_ptr);
787
7.13k
    if (!is_fake) {
788
6.77k
      if (!ht) {
789
6.75k
        ht = closure->func.op_array.static_variables;
790
6.75k
      }
791
6.77k
      ZEND_MAP_PTR_INIT(closure->func.op_array.static_variables_ptr,
792
6.77k
        ht ? zend_array_dup(ht) : NULL);
793
6.77k
    } else if (func->op_array.static_variables) {
794
75
      if (!ht) {
795
36
        ht = zend_array_dup(func->op_array.static_variables);
796
36
        ZEND_MAP_PTR_SET(func->op_array.static_variables_ptr, ht);
797
36
      }
798
75
      ZEND_MAP_PTR_INIT(closure->func.op_array.static_variables_ptr, ht);
799
75
    }
800
801
    /* Runtime cache is scope-dependent, so we cannot reuse it if the scope changed */
802
7.13k
    ptr = ZEND_MAP_PTR_GET(func->op_array.run_time_cache);
803
7.13k
    if (!ptr
804
1.41k
      || func->common.scope != scope
805
1.25k
      || (func->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE)
806
7.13k
    ) {
807
5.94k
      if (!ptr
808
5.71k
       && (func->common.fn_flags & ZEND_ACC_CLOSURE)
809
5.61k
       && (func->common.scope == scope ||
810
4.19k
           !(func->common.fn_flags & ZEND_ACC_IMMUTABLE))) {
811
        /* If a real closure is used for the first time, we create a shared runtime cache
812
         * and remember which scope it is for. */
813
4.19k
        if (func->common.scope != scope) {
814
0
          func->common.scope = scope;
815
0
        }
816
4.19k
        ptr = zend_arena_alloc(&CG(arena), func->op_array.cache_size);
817
4.19k
        ZEND_MAP_PTR_SET(func->op_array.run_time_cache, ptr);
818
4.19k
        closure->func.op_array.fn_flags &= ~ZEND_ACC_HEAP_RT_CACHE;
819
4.19k
      } else {
820
        /* Otherwise, we use a non-shared runtime cache */
821
1.74k
        ptr = emalloc(func->op_array.cache_size);
822
1.74k
        closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
823
1.74k
      }
824
5.94k
      memset(ptr, 0, func->op_array.cache_size);
825
5.94k
    }
826
7.13k
    ZEND_MAP_PTR_INIT(closure->func.op_array.run_time_cache, ptr);
827
7.13k
  } else {
828
417
    memcpy(&closure->func, func, sizeof(zend_internal_function));
829
417
    closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
830
    /* wrap internal function handler to avoid memory leak */
831
417
    if (UNEXPECTED(closure->func.internal_function.handler == zend_closure_internal_handler)) {
832
      /* avoid infinity recursion, by taking handler from nested closure */
833
60
      zend_closure *nested = ZEND_CONTAINER_OF(func, zend_closure, func);
834
60
      ZEND_ASSERT(nested->std.ce == zend_ce_closure);
835
60
      closure->orig_internal_handler = nested->orig_internal_handler;
836
357
    } else {
837
357
      closure->orig_internal_handler = closure->func.internal_function.handler;
838
357
    }
839
417
    closure->func.internal_function.handler = zend_closure_internal_handler;
840
417
    zend_string_addref(closure->func.op_array.function_name);
841
417
    if (!func->common.scope) {
842
      /* if it's a free function, we won't set scope & this since they're meaningless */
843
189
      this_ptr = NULL;
844
189
      scope = NULL;
845
189
    }
846
417
  }
847
848
7.55k
  ZVAL_UNDEF(&closure->this_ptr);
849
  /* Invariant:
850
   * If the closure is unscoped or static, it has no bound object. */
851
7.55k
  closure->func.common.scope = scope;
852
7.55k
  closure->called_scope = called_scope;
853
7.55k
  if (scope) {
854
2.05k
    closure->func.common.fn_flags |= ZEND_ACC_PUBLIC;
855
2.05k
    if (this_ptr && Z_TYPE_P(this_ptr) == IS_OBJECT && (closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0) {
856
1.67k
      ZVAL_OBJ_COPY(&closure->this_ptr, Z_OBJ_P(this_ptr));
857
1.67k
    }
858
2.05k
  }
859
7.55k
}
860
/* }}} */
861
862
ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr)
863
6.84k
{
864
6.84k
  zend_create_closure_ex(res, func, scope, called_scope, this_ptr,
865
6.84k
    /* is_fake */ (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) != 0);
866
6.84k
}
867
868
ZEND_API void zend_create_fake_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) /* {{{ */
869
711
{
870
711
  zend_closure *closure;
871
872
711
  zend_create_closure_ex(res, func, scope, called_scope, this_ptr, /* is_fake */ true);
873
874
711
  closure = (zend_closure *)Z_OBJ_P(res);
875
711
  closure->func.common.fn_flags |= ZEND_ACC_FAKE_CLOSURE;
876
711
  if (Z_TYPE(closure->this_ptr) != IS_OBJECT) {
877
495
    GC_ADD_FLAGS(&closure->std, GC_NOT_COLLECTABLE);
878
495
  }
879
711
}
880
/* }}} */
881
882
339
void zend_closure_from_frame(zval *return_value, const zend_execute_data *call) { /* {{{ */
883
339
  zval instance;
884
339
  zend_internal_function trampoline;
885
339
  zend_function *mptr = call->func;
886
887
339
  if (ZEND_CALL_INFO(call) & ZEND_CALL_CLOSURE) {
888
3
    RETURN_OBJ(ZEND_CLOSURE_OBJECT(mptr));
889
3
  }
890
891
336
  if (mptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
892
102
    if ((ZEND_CALL_INFO(call) & ZEND_CALL_HAS_THIS) &&
893
66
      (Z_OBJCE(call->This) == zend_ce_closure)
894
9
      && zend_string_equals(mptr->common.function_name, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) {
895
9
          zend_free_trampoline(mptr);
896
9
          RETURN_OBJ_COPY(Z_OBJ(call->This));
897
9
      }
898
899
93
    memset(&trampoline, 0, sizeof(zend_internal_function));
900
93
    trampoline.type = ZEND_INTERNAL_FUNCTION;
901
93
    trampoline.fn_flags = mptr->common.fn_flags & (ZEND_ACC_STATIC | ZEND_ACC_VARIADIC | ZEND_ACC_RETURN_REFERENCE | ZEND_ACC_DEPRECATED);
902
93
    trampoline.handler = zend_closure_call_magic;
903
93
    trampoline.function_name = mptr->common.function_name;
904
93
    trampoline.scope = mptr->common.scope;
905
93
    trampoline.doc_comment = NULL;
906
93
    trampoline.arg_info = mptr->common.arg_info;
907
93
    trampoline.attributes = mptr->common.attributes;
908
909
93
    zend_free_trampoline(mptr);
910
93
    mptr = (zend_function *) &trampoline;
911
93
  }
912
913
327
  if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_THIS) {
914
99
    ZVAL_OBJ(&instance, Z_OBJ(call->This));
915
916
99
    zend_create_fake_closure(return_value, mptr, mptr->common.scope, Z_OBJCE(instance), &instance);
917
228
  } else {
918
228
    zend_create_fake_closure(return_value, mptr, mptr->common.scope, Z_CE(call->This), NULL);
919
228
  }
920
921
327
  if (&mptr->internal_function == &trampoline) {
922
93
    zend_string_release(mptr->common.function_name);
923
93
  }
924
327
} /* }}} */
925
926
void zend_closure_bind_var(zval *closure_zv, zend_string *var_name, zval *var) /* {{{ */
927
0
{
928
0
  zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv);
929
0
  HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr);
930
0
  zend_hash_update(static_variables, var_name, var);
931
0
}
932
/* }}} */
933
934
void zend_closure_bind_var_ex(zval *closure_zv, uint32_t offset, zval *val) /* {{{ */
935
2.87k
{
936
2.87k
  zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv);
937
2.87k
  HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr);
938
2.87k
  zval *var = (zval*)((char*)static_variables->arData + offset);
939
2.87k
  zval_ptr_dtor(var);
940
2.87k
  ZVAL_COPY_VALUE(var, val);
941
2.87k
}
942
/* }}} */