Coverage Report

Created: 2025-11-16 06:23

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