Coverage Report

Created: 2026-07-25 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_API.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: Andi Gutmans <andi@php.net>                                 |
15
   |          Zeev Suraski <zeev@php.net>                                 |
16
   |          Andrei Zmievski <andrei@php.net>                            |
17
   |          Dmitry Stogov <dmitry@php.net>                              |
18
   +----------------------------------------------------------------------+
19
*/
20
21
#include "zend.h"
22
#include "zend_compile.h"
23
#include "zend_execute.h"
24
#include "zend_API.h"
25
#include "zend_hash.h"
26
#include "zend_modules.h"
27
#include "zend_extensions.h"
28
#include "zend_constants.h"
29
#include "zend_interfaces.h"
30
#include "zend_exceptions.h"
31
#include "zend_closures.h"
32
#include "zend_inheritance.h"
33
#include "zend_ini.h"
34
#include "zend_enum.h"
35
#include "zend_object_handlers.h"
36
#include "zend_observer.h"
37
38
#include <stdarg.h>
39
40
/* these variables are true statics/globals, and have to be mutex'ed on every access */
41
ZEND_API HashTable module_registry;
42
43
ZEND_API bool zend_dl_use_deepbind = false;
44
45
static zend_module_entry **module_request_startup_handlers;
46
static zend_module_entry **module_request_shutdown_handlers;
47
static zend_module_entry **module_post_deactivate_handlers;
48
static zend_module_entry **modules_dl_loaded;
49
50
static zend_class_entry  **class_cleanup_handlers;
51
52
ZEND_API void zend_set_dl_use_deepbind(bool use_deepbind)
53
0
{
54
0
  zend_dl_use_deepbind = use_deepbind;
55
0
}
56
57
ZEND_API zend_result zend_get_parameters_array_ex(uint32_t param_count, zval *argument_array) /* {{{ */
58
0
{
59
0
  zval *param_ptr;
60
0
  uint32_t arg_count;
61
62
0
  param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
63
0
  arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
64
65
0
  if (param_count>arg_count) {
66
0
    return FAILURE;
67
0
  }
68
69
0
  while (param_count-->0) {
70
0
    ZVAL_COPY_VALUE(argument_array, param_ptr);
71
0
    argument_array++;
72
0
    param_ptr++;
73
0
  }
74
75
0
  return SUCCESS;
76
0
}
77
/* }}} */
78
79
ZEND_API ZEND_COLD void zend_wrong_param_count(void) /* {{{ */
80
0
{
81
0
  const char *space;
82
0
  const char *class_name = get_active_class_name(&space);
83
84
0
  zend_argument_count_error("Wrong parameter count for %s%s%s()", class_name, space, get_active_function_name());
85
0
}
86
/* }}} */
87
88
ZEND_API ZEND_COLD void zend_wrong_property_read(const zval *object, zval *property)
89
33.7k
{
90
33.7k
  zend_string *tmp_property_name;
91
33.7k
  zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name);
92
33.7k
  zend_error(E_WARNING, "Attempt to read property \"%s\" on %s", ZSTR_VAL(property_name), zend_zval_value_name(object));
93
33.7k
  zend_tmp_string_release(tmp_property_name);
94
33.7k
}
95
96
/* Argument parsing API -- andrei */
97
ZEND_API const char *zend_get_type_by_const(int type) /* {{{ */
98
36.6k
{
99
36.6k
  switch(type) {
100
388
    case IS_FALSE:
101
472
    case IS_TRUE:
102
686
    case _IS_BOOL:
103
686
      return "bool";
104
2.97k
    case IS_LONG:
105
2.97k
      return "int";
106
1.01k
    case IS_DOUBLE:
107
1.01k
      return "float";
108
4.90k
    case IS_STRING:
109
4.90k
      return "string";
110
30
    case IS_OBJECT:
111
30
      return "object";
112
5
    case IS_RESOURCE:
113
5
      return "resource";
114
25.0k
    case IS_NULL:
115
25.0k
      return "null";
116
0
    case IS_CALLABLE:
117
0
      return "callable";
118
0
    case IS_ITERABLE:
119
0
      return "iterable";
120
1.94k
    case IS_ARRAY:
121
1.94k
      return "array";
122
0
    case IS_VOID:
123
0
      return "void";
124
0
    case IS_MIXED:
125
0
      return "mixed";
126
0
    case _IS_NUMBER:
127
0
      return "int|float";
128
0
    default: ZEND_UNREACHABLE();
129
36.6k
  }
130
36.6k
}
131
/* }}} */
132
133
ZEND_API const char *zend_zval_value_name(const zval *arg)
134
57.6k
{
135
57.6k
  ZVAL_DEREF(arg);
136
137
57.6k
  if (Z_ISUNDEF_P(arg)) {
138
28.1k
    return "null";
139
28.1k
  }
140
141
29.5k
  if (Z_TYPE_P(arg) == IS_OBJECT) {
142
1.11k
    return ZSTR_VAL(Z_OBJCE_P(arg)->name);
143
28.4k
  } else if (Z_TYPE_P(arg) == IS_FALSE) {
144
420
    return "false";
145
28.0k
  } else if  (Z_TYPE_P(arg) == IS_TRUE) {
146
204
    return "true";
147
204
  }
148
149
27.8k
  return zend_get_type_by_const(Z_TYPE_P(arg));
150
29.5k
}
151
152
ZEND_API const char *zend_zval_type_name(const zval *arg)
153
7.72k
{
154
7.72k
  ZVAL_DEREF(arg);
155
156
7.72k
  if (Z_ISUNDEF_P(arg)) {
157
0
    return "null";
158
0
  }
159
160
7.72k
  if (Z_TYPE_P(arg) == IS_OBJECT) {
161
956
    return ZSTR_VAL(Z_OBJCE_P(arg)->name);
162
956
  }
163
164
6.77k
  return zend_get_type_by_const(Z_TYPE_P(arg));
165
7.72k
}
166
167
/* This API exists *only* for use in gettype().
168
 * For anything else, you likely want zend_zval_type_name(). */
169
ZEND_API zend_string *zend_zval_get_legacy_type(const zval *arg) /* {{{ */
170
177
{
171
177
  switch (Z_TYPE_P(arg)) {
172
32
    case IS_NULL:
173
32
      return ZSTR_KNOWN(ZEND_STR_NULL);
174
0
    case IS_FALSE:
175
3
    case IS_TRUE:
176
3
      return ZSTR_KNOWN(ZEND_STR_BOOLEAN);
177
108
    case IS_LONG:
178
108
      return ZSTR_KNOWN(ZEND_STR_INTEGER);
179
0
    case IS_DOUBLE:
180
0
      return ZSTR_KNOWN(ZEND_STR_DOUBLE);
181
6
    case IS_STRING:
182
6
      return ZSTR_KNOWN(ZEND_STR_STRING);
183
20
    case IS_ARRAY:
184
20
      return ZSTR_KNOWN(ZEND_STR_ARRAY);
185
8
    case IS_OBJECT:
186
8
      return ZSTR_KNOWN(ZEND_STR_OBJECT);
187
0
    case IS_RESOURCE:
188
0
      if (zend_rsrc_list_get_rsrc_type(Z_RES_P(arg))) {
189
0
        return ZSTR_KNOWN(ZEND_STR_RESOURCE);
190
0
      } else {
191
0
        return ZSTR_KNOWN(ZEND_STR_CLOSED_RESOURCE);
192
0
      }
193
0
    default:
194
0
      return NULL;
195
177
  }
196
177
}
197
/* }}} */
198
199
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_none_error(void) /* {{{ */
200
66
{
201
66
  int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
202
66
  zend_string *func_name = get_active_function_or_method_name();
203
204
66
  zend_argument_count_error("%s() expects exactly 0 arguments, %d given", ZSTR_VAL(func_name), num_args);
205
206
66
  zend_string_release(func_name);
207
66
}
208
/* }}} */
209
210
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_error(uint32_t min_num_args, uint32_t max_num_args) /* {{{ */
211
401
{
212
401
  uint32_t num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
213
401
  zend_string *func_name = get_active_function_or_method_name();
214
215
401
  zend_argument_count_error(
216
401
    "%s() expects %s %d argument%s, %d given",
217
401
    ZSTR_VAL(func_name),
218
401
    min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
219
401
    num_args < min_num_args ? min_num_args : max_num_args,
220
401
    (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
221
401
    num_args
222
401
  );
223
224
401
  zend_string_release(func_name);
225
401
}
226
/* }}} */
227
228
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_error(int error_code, uint32_t num, char *name, zend_expected_type expected_type, const zval *arg) /* {{{ */
229
1.06k
{
230
1.06k
  switch (error_code) {
231
63
    case ZPP_ERROR_WRONG_CALLBACK:
232
63
      zend_wrong_callback_error(num, name);
233
63
      break;
234
76
    case ZPP_ERROR_WRONG_CALLBACK_OR_NULL:
235
76
      zend_wrong_callback_or_null_error(num, name);
236
76
      break;
237
42
    case ZPP_ERROR_WRONG_CLASS:
238
42
      zend_wrong_parameter_class_error(num, name, arg);
239
42
      break;
240
6
    case ZPP_ERROR_WRONG_CLASS_OR_NULL:
241
6
      zend_wrong_parameter_class_or_null_error(num, name, arg);
242
6
      break;
243
0
    case ZPP_ERROR_WRONG_CLASS_OR_STRING:
244
0
      zend_wrong_parameter_class_or_string_error(num, name, arg);
245
0
      break;
246
0
    case ZPP_ERROR_WRONG_CLASS_OR_STRING_OR_NULL:
247
0
      zend_wrong_parameter_class_or_string_or_null_error(num, name, arg);
248
0
      break;
249
0
    case ZPP_ERROR_WRONG_CLASS_OR_LONG:
250
0
      zend_wrong_parameter_class_or_long_error(num, name, arg);
251
0
      break;
252
0
    case ZPP_ERROR_WRONG_CLASS_OR_LONG_OR_NULL:
253
0
      zend_wrong_parameter_class_or_long_or_null_error(num, name, arg);
254
0
      break;
255
458
    case ZPP_ERROR_WRONG_ARG:
256
458
      zend_wrong_parameter_type_error(num, expected_type, arg);
257
458
      break;
258
20
    case ZPP_ERROR_UNEXPECTED_EXTRA_NAMED:
259
20
      zend_unexpected_extra_named_error();
260
20
      break;
261
401
    case ZPP_ERROR_FAILURE:
262
401
      ZEND_ASSERT(EG(exception) && "Should have produced an error already");
263
401
      break;
264
401
    default: ZEND_UNREACHABLE();
265
1.06k
  }
266
1.06k
}
267
/* }}} */
268
269
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_type_error(uint32_t num, zend_expected_type expected_type, const zval *arg) /* {{{ */
270
497
{
271
497
  static const char * const expected_error[] = {
272
16.8k
    Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR)
273
497
    NULL
274
497
  };
275
276
497
  if (EG(exception)) {
277
24
    return;
278
24
  }
279
280
473
  if ((expected_type == Z_EXPECTED_PATH || expected_type == Z_EXPECTED_PATH_OR_NULL)
281
31
      && Z_TYPE_P(arg) == IS_STRING) {
282
31
    zend_argument_value_error(num, "must not contain any null bytes");
283
31
    return;
284
31
  }
285
286
442
  zend_argument_type_error(num, "must be %s, %s given", expected_error[expected_type], zend_zval_value_name(arg));
287
442
}
288
/* }}} */
289
290
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_error(uint32_t num, const char *name, const zval *arg) /* {{{ */
291
42
{
292
42
  if (EG(exception)) {
293
0
    return;
294
0
  }
295
296
42
  zend_argument_type_error(num, "must be of type %s, %s given", name, zend_zval_value_name(arg));
297
42
}
298
/* }}} */
299
300
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_null_error(uint32_t num, const char *name, const zval *arg) /* {{{ */
301
6
{
302
6
  if (EG(exception)) {
303
0
    return;
304
0
  }
305
306
6
  zend_argument_type_error(num, "must be of type ?%s, %s given", name, zend_zval_value_name(arg));
307
6
}
308
/* }}} */
309
310
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_long_error(uint32_t num, const char *name, const zval *arg) /* {{{ */
311
0
{
312
0
  if (EG(exception)) {
313
0
    return;
314
0
  }
315
316
0
  zend_argument_type_error(num, "must be of type %s|int, %s given", name, zend_zval_value_name(arg));
317
0
}
318
/* }}} */
319
320
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_long_or_null_error(uint32_t num, const char *name, const zval *arg) /* {{{ */
321
0
{
322
0
  if (EG(exception)) {
323
0
    return;
324
0
  }
325
326
0
  zend_argument_type_error(num, "must be of type %s|int|null, %s given", name, zend_zval_value_name(arg));
327
0
}
328
/* }}} */
329
330
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_string_error(uint32_t num, const char *name, const zval *arg) /* {{{ */
331
0
{
332
0
  if (EG(exception)) {
333
0
    return;
334
0
  }
335
336
0
  zend_argument_type_error(num, "must be of type %s|string, %s given", name, zend_zval_value_name(arg));
337
0
}
338
/* }}} */
339
340
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_string_or_null_error(uint32_t num, const char *name, const zval *arg) /* {{{ */
341
0
{
342
0
  if (EG(exception)) {
343
0
    return;
344
0
  }
345
346
0
  zend_argument_type_error(num, "must be of type %s|string|null, %s given", name, zend_zval_value_name(arg));
347
0
}
348
/* }}} */
349
350
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_error(uint32_t num, char *error) /* {{{ */
351
63
{
352
63
  if (!EG(exception)) {
353
63
    zend_argument_type_error(num, "must be a valid callback, %s", error);
354
63
  }
355
63
  efree(error);
356
63
}
357
/* }}} */
358
359
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_or_null_error(uint32_t num, char *error) /* {{{ */
360
76
{
361
76
  if (!EG(exception)) {
362
65
    zend_argument_type_error(num, "must be a valid callback or null, %s", error);
363
65
  }
364
76
  efree(error);
365
76
}
366
/* }}} */
367
368
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_unexpected_extra_named_error(void)
369
30
{
370
30
  const char *space;
371
30
  const char *class_name = get_active_class_name(&space);
372
30
  zend_argument_count_error("Internal function %s%s%s() does not accept named variadic arguments",
373
30
    class_name, space, get_active_function_name()
374
30
  );
375
30
}
376
377
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_error_variadic(
378
  zend_class_entry *error_ce, const zend_function *function, uint32_t arg_num, const char *format, va_list va)
379
2.09k
{
380
2.09k
  zend_string *func_name;
381
2.09k
  const char *arg_name;
382
2.09k
  char *message = NULL;
383
2.09k
  if (EG(exception)) {
384
0
    return;
385
0
  }
386
387
2.09k
  func_name = get_function_or_method_name(function);
388
2.09k
  arg_name = get_function_arg_name(function, arg_num);
389
390
2.09k
  zend_vspprintf(&message, 0, format, va);
391
2.09k
  zend_throw_error(error_ce, "%s(): Argument #%d%s%s%s %s",
392
2.09k
    ZSTR_VAL(func_name), arg_num,
393
2.09k
    arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "", message
394
2.09k
  );
395
2.09k
  efree(message);
396
2.09k
  zend_string_release(func_name);
397
2.09k
}
398
399
ZEND_API ZEND_COLD void zend_argument_error_ex(zend_class_entry *error_ce, const zend_function *function, uint32_t arg_num, const char *format, ...)
400
32
{
401
32
  va_list va;
402
403
32
  va_start(va, format);
404
32
  zend_argument_error_variadic(error_ce, function, arg_num, format, va);
405
32
  va_end(va);
406
32
}
407
408
ZEND_API ZEND_COLD void zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format, ...) /* {{{ */
409
104
{
410
104
  va_list va;
411
104
  const zend_function *function = zend_active_function();
412
413
104
  va_start(va, format);
414
104
  zend_argument_error_variadic(error_ce, function, arg_num, format, va);
415
104
  va_end(va);
416
104
}
417
/* }}} */
418
419
ZEND_API ZEND_COLD void zend_argument_type_error_ex(
420
    const zend_function *function, uint32_t arg_num, const char *format, ...)
421
25
{
422
25
  va_list va;
423
424
25
  va_start(va, format);
425
25
  zend_argument_error_variadic(zend_ce_type_error, function, arg_num, format, va);
426
25
  va_end(va);
427
25
}
428
429
ZEND_API ZEND_COLD void zend_argument_type_error(uint32_t arg_num, const char *format, ...) /* {{{ */
430
1.70k
{
431
1.70k
  va_list va;
432
1.70k
  const zend_function *function = zend_active_function();
433
434
1.70k
  va_start(va, format);
435
1.70k
  zend_argument_error_variadic(zend_ce_type_error, function, arg_num, format, va);
436
1.70k
  va_end(va);
437
1.70k
}
438
/* }}} */
439
440
ZEND_API ZEND_COLD void zend_argument_value_error_ex(
441
  const zend_function *function, uint32_t arg_num, const char *format, ...)
442
0
{
443
0
  va_list va;
444
445
0
  va_start(va, format);
446
0
  zend_argument_error_variadic(zend_ce_value_error, function, arg_num, format, va);
447
0
  va_end(va);
448
0
}
449
450
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format, ...) /* {{{ */
451
228
{
452
228
  va_list va;
453
228
  const zend_function *function = zend_active_function();
454
455
228
  va_start(va, format);
456
228
  zend_argument_error_variadic(zend_ce_value_error, function, arg_num, format, va);
457
228
  va_end(va);
458
228
}
459
/* }}} */
460
461
ZEND_API ZEND_COLD void zend_argument_must_not_be_empty_error(uint32_t arg_num)
462
2
{
463
2
  zend_argument_value_error(arg_num, "must not be empty");
464
2
}
465
466
ZEND_API ZEND_COLD void zend_class_redeclaration_error_ex(int type, zend_string *new_name, const zend_class_entry *old_ce)
467
192
{
468
192
  if (old_ce->type == ZEND_INTERNAL_CLASS) {
469
7
    zend_error(type, "Cannot redeclare %s %s",
470
7
      zend_get_object_type(old_ce),
471
7
      ZSTR_VAL(new_name));
472
185
  } else {
473
185
    zend_error(type, "Cannot redeclare %s %s (previously declared in %s:%d)",
474
185
      zend_get_object_type(old_ce),
475
185
      ZSTR_VAL(new_name),
476
185
      ZSTR_VAL(old_ce->info.user.filename),
477
185
      old_ce->info.user.line_start);
478
185
  }
479
192
}
480
481
ZEND_API ZEND_COLD void zend_class_redeclaration_error(int type, const zend_class_entry *old_ce)
482
177
{
483
177
  zend_class_redeclaration_error_ex(type, old_ce->name, old_ce);
484
177
}
485
486
ZEND_API bool ZEND_FASTCALL zend_parse_arg_class(zval *arg, zend_class_entry **pce, uint32_t num, bool check_null) /* {{{ */
487
0
{
488
0
  zend_class_entry *ce_base = *pce;
489
490
0
  if (check_null && Z_TYPE_P(arg) == IS_NULL) {
491
0
    *pce = NULL;
492
0
    return 1;
493
0
  }
494
0
  if (!try_convert_to_string(arg)) {
495
0
    *pce = NULL;
496
0
    return 0;
497
0
  }
498
499
0
  *pce = zend_lookup_class(Z_STR_P(arg));
500
0
  if (ce_base) {
501
0
    if ((!*pce || !instanceof_function(*pce, ce_base))) {
502
0
      zend_argument_type_error(num, "must be a class name derived from %s, %s given", ZSTR_VAL(ce_base->name), Z_STRVAL_P(arg));
503
0
      *pce = NULL;
504
0
      return 0;
505
0
    }
506
0
  }
507
0
  if (!*pce) {
508
0
    zend_argument_type_error(num, "must be a valid class name, %s given", Z_STRVAL_P(arg));
509
0
    return 0;
510
0
  }
511
0
  return 1;
512
0
}
513
/* }}} */
514
515
5.88k
static ZEND_COLD bool zend_null_arg_deprecated(const char *fallback_type, uint32_t arg_num) {
516
5.88k
  const zend_function *func = zend_active_function();
517
5.88k
  ZEND_ASSERT(arg_num > 0);
518
5.88k
  uint32_t arg_offset = arg_num - 1;
519
5.88k
  if (arg_offset >= func->common.num_args) {
520
0
    ZEND_ASSERT(func->common.fn_flags & ZEND_ACC_VARIADIC);
521
0
    arg_offset = func->common.num_args;
522
0
  }
523
524
5.88k
  const zend_arg_info *arg_info = &func->common.arg_info[arg_offset];
525
5.88k
  zend_string *func_name = get_active_function_or_method_name();
526
5.88k
  const char *arg_name = get_active_function_arg_name(arg_num);
527
528
  /* If no type is specified in arginfo, use the specified fallback_type determined through
529
   * zend_parse_parameters instead. */
530
5.88k
  zend_string *type_str = zend_type_to_string(arg_info->type);
531
5.88k
  const char *type = type_str ? ZSTR_VAL(type_str) : fallback_type;
532
5.88k
  zend_error(E_DEPRECATED,
533
5.88k
    "%s(): Passing null to parameter #%" PRIu32 "%s%s%s of type %s is deprecated",
534
5.88k
    ZSTR_VAL(func_name), arg_num,
535
5.88k
    arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "",
536
5.88k
    type);
537
5.88k
  zend_string_release(func_name);
538
5.88k
  if (type_str) {
539
5.88k
    zend_string_release(type_str);
540
5.88k
  }
541
5.88k
  return !EG(exception);
542
5.88k
}
543
544
ZEND_API zpp_parse_bool_status ZEND_FASTCALL zend_parse_arg_bool_weak(const zval *arg, uint32_t arg_num) /* {{{ */
545
3.77k
{
546
3.77k
  if (EXPECTED(Z_TYPE_P(arg) <= IS_STRING)) {
547
3.77k
    if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("bool", arg_num)) {
548
0
      return ZPP_PARSE_BOOL_STATUS_ERROR;
549
0
    }
550
3.77k
    bool result = zend_is_true(arg);
551
3.77k
    if (UNEXPECTED(EG(exception))) {
552
0
      return ZPP_PARSE_BOOL_STATUS_ERROR;
553
0
    }
554
3.77k
    return result;
555
3.77k
  }
556
0
  return ZPP_PARSE_BOOL_STATUS_ERROR;
557
3.77k
}
558
/* }}} */
559
560
ZEND_API zpp_parse_bool_status ZEND_FASTCALL zend_parse_arg_bool_slow(const zval *arg, uint32_t arg_num) /* {{{ */
561
2.01k
{
562
2.01k
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
563
0
    return ZPP_PARSE_BOOL_STATUS_ERROR;
564
0
  }
565
2.01k
  return zend_parse_arg_bool_weak(arg, arg_num);
566
2.01k
}
567
/* }}} */
568
569
ZEND_API zpp_parse_bool_status ZEND_FASTCALL zend_flf_parse_arg_bool_slow(const zval *arg, uint32_t arg_num)
570
0
{
571
0
  if (UNEXPECTED(ZEND_FLF_ARG_USES_STRICT_TYPES())) {
572
0
    return ZPP_PARSE_BOOL_STATUS_ERROR;
573
0
  }
574
0
  return zend_parse_arg_bool_weak(arg, arg_num);
575
0
}
576
577
ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_weak(const zval *arg, zend_long *dest, uint32_t arg_num) /* {{{ */
578
3.21k
{
579
3.21k
  if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
580
545
    if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
581
10
      return 0;
582
10
    }
583
535
    if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
584
177
      return 0;
585
358
    } else {
586
358
      zend_long lval = zend_dval_to_lval(Z_DVAL_P(arg));
587
358
      if (UNEXPECTED(!zend_is_long_compatible(Z_DVAL_P(arg), lval))) {
588
        /* Check arg_num is not (uint32_t)-1, as otherwise its called by
589
         * zend_verify_weak_scalar_type_hint_no_sideeffect() */
590
162
        if (arg_num != (uint32_t)-1) {
591
102
          zend_incompatible_double_to_long_error(Z_DVAL_P(arg));
592
102
        }
593
162
        if (UNEXPECTED(EG(exception))) {
594
0
          return 0;
595
0
        }
596
162
      }
597
358
      *dest = lval;
598
358
    }
599
2.66k
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
600
1.18k
    double d;
601
1.18k
    uint8_t type;
602
603
1.18k
    if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
604
591
      if (EXPECTED(type != 0)) {
605
316
        zend_long lval;
606
316
        if (UNEXPECTED(zend_isnan(d))) {
607
0
          return 0;
608
0
        }
609
316
        if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
610
29
          return 0;
611
29
        }
612
613
287
        lval = zend_dval_to_lval(d);
614
        /* This only checks for a fractional part as if doesn't fit it already throws a TypeError */
615
287
        if (UNEXPECTED(!zend_is_long_compatible(d, lval))) {
616
          /* Check arg_num is not (uint32_t)-1, as otherwise its called by
617
           * zend_verify_weak_scalar_type_hint_no_sideeffect() */
618
73
          if (arg_num != (uint32_t)-1) {
619
58
            zend_incompatible_string_to_long_error(Z_STR_P(arg));
620
58
          }
621
73
          if (UNEXPECTED(EG(exception))) {
622
0
            return 0;
623
0
          }
624
73
        }
625
287
        *dest = lval;
626
287
      } else {
627
275
        return 0;
628
275
      }
629
591
    }
630
1.48k
  } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
631
971
    if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("int", arg_num)) {
632
0
      return 0;
633
0
    }
634
971
    *dest = 0;
635
971
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
636
282
    *dest = 1;
637
282
  } else {
638
231
    return 0;
639
231
  }
640
2.49k
  return 1;
641
3.21k
}
642
/* }}} */
643
644
ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_slow(const zval *arg, zend_long *dest, uint32_t arg_num) /* {{{ */
645
1.10k
{
646
1.10k
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
647
6
    return 0;
648
6
  }
649
1.09k
  return zend_parse_arg_long_weak(arg, dest, arg_num);
650
1.10k
}
651
/* }}} */
652
653
ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_long_slow(const zval *arg, zend_long *dest, uint32_t arg_num)
654
0
{
655
0
  if (UNEXPECTED(ZEND_FLF_ARG_USES_STRICT_TYPES())) {
656
0
    return 0;
657
0
  }
658
0
  return zend_parse_arg_long_weak(arg, dest, arg_num);
659
0
}
660
661
ZEND_API double ZEND_FASTCALL zend_parse_arg_double_weak(const zval *arg, uint32_t arg_num) /* {{{ */
662
1.31k
{
663
1.31k
  if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
664
844
    return (double)Z_LVAL_P(arg);
665
844
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
666
221
    zend_long l;
667
221
    double dval;
668
221
    uint8_t type;
669
670
221
    if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), &l, &dval)) != IS_DOUBLE)) {
671
193
      if (EXPECTED(type != 0)) {
672
118
        return (double)(l);
673
118
      } else {
674
75
        return NAN;
675
75
      }
676
193
    } else {
677
28
      return dval;
678
28
    }
679
252
  } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
680
126
    if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("float", arg_num)) {
681
0
      return NAN;
682
0
    }
683
126
    return 0.0;
684
126
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
685
19
    return 1.0;
686
107
  } else {
687
107
    return NAN;
688
107
  }
689
1.31k
}
690
/* }}} */
691
692
ZEND_API double ZEND_FASTCALL zend_parse_arg_double_slow(const zval *arg, uint32_t arg_num) /* {{{ */
693
858
{
694
858
  if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
695
    /* SSTH Exception: IS_LONG may be accepted instead as IS_DOUBLE */
696
725
    return (double)Z_LVAL_P(arg);
697
725
  } else if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
698
0
    return NAN;
699
0
  }
700
133
  return zend_parse_arg_double_weak(arg, arg_num);
701
858
}
702
/* }}} */
703
704
ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_slow(zval *arg, zval **dest, uint32_t arg_num) /* {{{ */
705
6
{
706
6
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
707
0
    return 0;
708
0
  }
709
6
  if (Z_TYPE_P(arg) == IS_STRING) {
710
4
    zend_string *str = Z_STR_P(arg);
711
4
    zend_long lval;
712
4
    double dval;
713
4
    uint8_t type = is_numeric_str_function(str, &lval, &dval);
714
4
    if (type == IS_LONG) {
715
3
      ZVAL_LONG(arg, lval);
716
3
    } else if (type == IS_DOUBLE) {
717
0
      ZVAL_DOUBLE(arg, dval);
718
1
    } else {
719
1
      return 0;
720
1
    }
721
3
    zend_string_release(str);
722
3
  } else if (Z_TYPE_P(arg) < IS_TRUE) {
723
0
    if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("int|float", arg_num)) {
724
0
      return 0;
725
0
    }
726
0
    ZVAL_LONG(arg, 0);
727
2
  } else if (Z_TYPE_P(arg) == IS_TRUE) {
728
2
    ZVAL_LONG(arg, 1);
729
2
  } else {
730
0
    return 0;
731
0
  }
732
5
  *dest = arg;
733
5
  return 1;
734
6
}
735
/* }}} */
736
737
738
ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_or_str_slow(zval *arg, zval **dest, uint32_t arg_num) /* {{{ */
739
30
{
740
30
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
741
0
    return false;
742
0
  }
743
30
  if (Z_TYPE_P(arg) < IS_TRUE) {
744
9
    if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("string|int|float", arg_num)) {
745
0
      return false;
746
0
    }
747
9
    ZVAL_LONG(arg, 0);
748
21
  } else if (Z_TYPE_P(arg) == IS_TRUE) {
749
21
    ZVAL_LONG(arg, 1);
750
21
  } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
751
0
    zend_object *zobj = Z_OBJ_P(arg);
752
0
    zval obj;
753
0
    if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) {
754
0
      OBJ_RELEASE(zobj);
755
0
      ZVAL_COPY_VALUE(arg, &obj);
756
0
      *dest = arg;
757
0
      return true;
758
0
    }
759
0
    return false;
760
0
  } else {
761
0
    return false;
762
0
  }
763
30
  *dest = arg;
764
30
  return true;
765
30
}
766
767
ZEND_API zend_string* ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, uint32_t arg_num) /* {{{ */
768
9.78k
{
769
9.78k
  if (EXPECTED(Z_TYPE_P(arg) < IS_STRING)) {
770
8.43k
    if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("string", arg_num)) {
771
0
      return NULL;
772
0
    }
773
8.43k
    convert_to_string(arg);
774
8.43k
    if (UNEXPECTED(EG(exception))) {
775
0
      return NULL;
776
0
    }
777
8.43k
    return Z_STR_P(arg);
778
8.43k
  } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
779
1.30k
    zend_object *zobj = Z_OBJ_P(arg);
780
1.30k
    zval obj;
781
1.30k
    if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) {
782
1.17k
      OBJ_RELEASE(zobj);
783
1.17k
      ZVAL_COPY_VALUE(arg, &obj);
784
1.17k
      return Z_STR_P(arg);
785
1.17k
    }
786
135
    return NULL;
787
1.30k
  } else {
788
43
    return NULL;
789
43
  }
790
9.78k
}
791
/* }}} */
792
793
ZEND_API zend_string* ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, uint32_t arg_num) /* {{{ */
794
8.98k
{
795
8.98k
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
796
36
    return NULL;
797
36
  }
798
8.95k
  return zend_parse_arg_str_weak(arg, arg_num);
799
8.98k
}
800
/* }}} */
801
802
ZEND_API zend_string* ZEND_FASTCALL zend_flf_parse_arg_str_slow(zval *arg, uint32_t arg_num)
803
0
{
804
0
  if (UNEXPECTED(ZEND_FLF_ARG_USES_STRICT_TYPES())) {
805
0
    return NULL;
806
0
  }
807
0
  return zend_parse_arg_str_weak(arg, arg_num);
808
0
}
809
810
ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_or_long_slow(zval *arg, zend_string **dest_str, zend_long *dest_long, uint32_t arg_num) /* {{{ */
811
5
{
812
5
  zend_string *str;
813
5
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
814
0
    return 0;
815
0
  }
816
5
  if (zend_parse_arg_long_weak(arg, dest_long, arg_num)) {
817
1
    *dest_str = NULL;
818
1
    return 1;
819
4
  } else if ((str = zend_parse_arg_str_weak(arg, arg_num)) != NULL) {
820
0
    *dest_long = 0;
821
0
    *dest_str = str;
822
0
    return 1;
823
4
  } else {
824
4
    return 0;
825
4
  }
826
5
}
827
/* }}} */
828
829
static const char *zend_parse_arg_impl(zval *arg, va_list *va, const char **spec, char **error, uint32_t arg_num) /* {{{ */
830
360k
{
831
360k
  const char *spec_walk = *spec;
832
360k
  char c = *spec_walk++;
833
360k
  bool check_null = false;
834
360k
  bool separate = false;
835
360k
  zval *real_arg = arg;
836
837
  /* scan through modifiers */
838
360k
  ZVAL_DEREF(arg);
839
360k
  while (1) {
840
360k
    if (*spec_walk == '/') {
841
0
      SEPARATE_ZVAL_NOREF(arg);
842
0
      real_arg = arg;
843
0
      separate = true;
844
360k
    } else if (*spec_walk == '!') {
845
452
      check_null = true;
846
360k
    } else {
847
360k
      break;
848
360k
    }
849
452
    spec_walk++;
850
452
  }
851
852
360k
  switch (c) {
853
1.70k
    case 'l':
854
1.70k
      {
855
1.70k
        zend_long *p = va_arg(*va, zend_long *);
856
1.70k
        bool *is_null = NULL;
857
858
1.70k
        if (check_null) {
859
66
          is_null = va_arg(*va, bool *);
860
66
        }
861
862
1.70k
        if (!zend_parse_arg_long(arg, p, is_null, check_null, arg_num)) {
863
16
          return check_null ? "?int" : "int";
864
16
        }
865
1.70k
      }
866
1.68k
      break;
867
868
1.68k
    case 'd':
869
0
      {
870
0
        double *p = va_arg(*va, double *);
871
0
        bool *is_null = NULL;
872
873
0
        if (check_null) {
874
0
          is_null = va_arg(*va, bool *);
875
0
        }
876
877
0
        if (!zend_parse_arg_double(arg, p, is_null, check_null, arg_num)) {
878
0
          return check_null ? "?float" : "float";
879
0
        }
880
0
      }
881
0
      break;
882
883
0
    case 'n':
884
0
      {
885
0
        zval **p = va_arg(*va, zval **);
886
887
0
        if (!zend_parse_arg_number(arg, p, check_null, arg_num)) {
888
0
          return check_null ? "int|float|null" : "int|float";
889
0
        }
890
0
      }
891
0
      break;
892
893
155k
    case 's':
894
155k
      {
895
155k
        char **p = va_arg(*va, char **);
896
155k
        size_t *pl = va_arg(*va, size_t *);
897
155k
        if (!zend_parse_arg_string(arg, p, pl, check_null, arg_num)) {
898
0
          return check_null ? "?string" : "string";
899
0
        }
900
155k
      }
901
155k
      break;
902
903
155k
    case 'p':
904
0
      {
905
0
        char **p = va_arg(*va, char **);
906
0
        size_t *pl = va_arg(*va, size_t *);
907
0
        if (!zend_parse_arg_path(arg, p, pl, check_null, arg_num)) {
908
0
          if (Z_TYPE_P(arg) == IS_STRING) {
909
0
            zend_spprintf(error, 0, "must not contain any null bytes");
910
0
            return "";
911
0
          } else {
912
0
            return check_null ? "?string" : "string";
913
0
          }
914
0
        }
915
0
      }
916
0
      break;
917
918
0
    case 'P':
919
0
      {
920
0
        zend_string **str = va_arg(*va, zend_string **);
921
0
        if (!zend_parse_arg_path_str(arg, str, check_null, arg_num)) {
922
0
          if (Z_TYPE_P(arg) == IS_STRING) {
923
0
            zend_spprintf(error, 0, "must not contain any null bytes");
924
0
            return "";
925
0
          } else {
926
0
            return check_null ? "?string" : "string";
927
0
          }
928
0
        }
929
0
      }
930
0
      break;
931
932
83.4k
    case 'S':
933
83.4k
      {
934
83.4k
        zend_string **str = va_arg(*va, zend_string **);
935
83.4k
        if (!zend_parse_arg_str(arg, str, check_null, arg_num)) {
936
16
          return check_null ? "?string" : "string";
937
16
        }
938
83.4k
      }
939
83.4k
      break;
940
941
83.4k
    case 'b':
942
75.1k
      {
943
75.1k
        bool *p = va_arg(*va, bool *);
944
75.1k
        bool *is_null = NULL;
945
946
75.1k
        if (check_null) {
947
0
          is_null = va_arg(*va, bool *);
948
0
        }
949
950
75.1k
        if (!zend_parse_arg_bool(arg, p, is_null, check_null, arg_num)) {
951
0
          return check_null ? "?bool" : "bool";
952
0
        }
953
75.1k
      }
954
75.1k
      break;
955
956
75.1k
    case 'r':
957
12
      {
958
12
        zval **p = va_arg(*va, zval **);
959
960
12
        if (!zend_parse_arg_resource(arg, p, check_null)) {
961
0
          return check_null ? "resource or null" : "resource";
962
0
        }
963
12
      }
964
12
      break;
965
966
1.21k
    case 'A':
967
1.48k
    case 'a':
968
1.48k
      {
969
1.48k
        zval **p = va_arg(*va, zval **);
970
971
1.48k
        if (!zend_parse_arg_array(arg, p, check_null, c == 'A')) {
972
12
          return check_null ? "?array" : "array";
973
12
        }
974
1.48k
      }
975
1.47k
      break;
976
977
1.47k
    case 'H':
978
36.3k
    case 'h':
979
36.3k
      {
980
36.3k
        HashTable **p = va_arg(*va, HashTable **);
981
982
36.3k
        if (!zend_parse_arg_array_ht(arg, p, check_null, c == 'H', separate)) {
983
1
          return check_null ? "?array" : "array";
984
1
        }
985
36.3k
      }
986
36.3k
      break;
987
988
36.3k
    case 'o':
989
118
      {
990
118
        zval **p = va_arg(*va, zval **);
991
992
118
        if (!zend_parse_arg_object(arg, p, NULL, check_null)) {
993
2
          return check_null ? "?object" : "object";
994
2
        }
995
118
      }
996
116
      break;
997
998
4.81k
    case 'O':
999
4.81k
      {
1000
4.81k
        zval **p = va_arg(*va, zval **);
1001
4.81k
        zend_class_entry *ce = va_arg(*va, zend_class_entry *);
1002
1003
4.81k
        if (!zend_parse_arg_object(arg, p, ce, check_null)) {
1004
25
          if (ce) {
1005
25
            if (check_null) {
1006
0
              zend_spprintf(error, 0, "must be of type ?%s, %s given", ZSTR_VAL(ce->name), zend_zval_value_name(arg));
1007
0
              return "";
1008
25
            } else {
1009
25
              return ZSTR_VAL(ce->name);
1010
25
            }
1011
25
          } else {
1012
0
            return check_null ? "?object" : "object";
1013
0
          }
1014
25
        }
1015
4.81k
      }
1016
4.78k
      break;
1017
1018
4.78k
    case 'C':
1019
670
      {
1020
670
        zend_class_entry *lookup, **pce = va_arg(*va, zend_class_entry **);
1021
670
        zend_class_entry *ce_base = *pce;
1022
1023
670
        if (check_null && Z_TYPE_P(arg) == IS_NULL) {
1024
0
          *pce = NULL;
1025
0
          break;
1026
0
        }
1027
670
        if (!try_convert_to_string(arg)) {
1028
15
          *pce = NULL;
1029
15
          return ""; /* try_convert_to_string() throws an exception */
1030
15
        }
1031
1032
655
        if ((lookup = zend_lookup_class(Z_STR_P(arg))) == NULL) {
1033
19
          *pce = NULL;
1034
636
        } else {
1035
636
          *pce = lookup;
1036
636
        }
1037
655
        if (ce_base) {
1038
2
          if ((!*pce || !instanceof_function(*pce, ce_base))) {
1039
2
            zend_spprintf(error, 0, "must be a class name derived from %s%s, %s given",
1040
2
              ZSTR_VAL(ce_base->name), check_null ? " or null" : "", Z_STRVAL_P(arg));
1041
2
            *pce = NULL;
1042
2
            return "";
1043
2
          }
1044
2
        }
1045
653
        if (!*pce) {
1046
17
          zend_spprintf(error, 0, "must be a valid class name%s, %s given",
1047
17
            check_null ? " or null" : "", Z_STRVAL_P(arg));
1048
17
          return "";
1049
17
        }
1050
636
        break;
1051
1052
653
      }
1053
636
      break;
1054
1055
636
    case 'F':
1056
481
    case 'f':
1057
481
      {
1058
481
        zend_fcall_info *fci = va_arg(*va, zend_fcall_info *);
1059
481
        zend_fcall_info_cache *fcc = va_arg(*va, zend_fcall_info_cache *);
1060
481
        char *is_callable_error = NULL;
1061
1062
481
        if (check_null && Z_TYPE_P(arg) == IS_NULL) {
1063
0
          fci->size = 0;
1064
0
          fcc->function_handler = 0;
1065
0
          break;
1066
0
        }
1067
1068
481
        if (zend_fcall_info_init(arg, 0, fci, fcc, NULL, &is_callable_error) == SUCCESS) {
1069
474
          ZEND_ASSERT(!is_callable_error);
1070
474
          if (c == 'f') {
1071
            /* Release call trampolines: The function may not get called, in which case
1072
             * the trampoline will leak. Force it to be refetched during
1073
             * zend_call_function instead. */
1074
0
            zend_release_fcall_info_cache(fcc);
1075
0
          }
1076
474
          break;
1077
474
        }
1078
1079
7
        if (is_callable_error) {
1080
7
          zend_spprintf(error, 0, "must be a valid callback%s, %s", check_null ? " or null" : "", is_callable_error);
1081
7
          efree(is_callable_error);
1082
7
          return "";
1083
7
        } else {
1084
0
          return check_null ? "a valid callback or null" : "a valid callback";
1085
0
        }
1086
7
      }
1087
1088
532
    case 'z':
1089
532
      {
1090
532
        zval **p = va_arg(*va, zval **);
1091
1092
532
        zend_parse_arg_zval_deref(real_arg, p, check_null);
1093
532
      }
1094
532
      break;
1095
1096
0
    case 'Z': /* replace with 'z' */
1097
0
    case 'L': /* replace with 'l' */
1098
0
      ZEND_ASSERT(0 && "ZPP modifier no longer supported");
1099
0
      ZEND_FALLTHROUGH;
1100
0
    default:
1101
0
      return "unknown";
1102
360k
  }
1103
1104
359k
  *spec = spec_walk;
1105
1106
359k
  return NULL;
1107
360k
}
1108
/* }}} */
1109
1110
static zend_result zend_parse_arg(uint32_t arg_num, zval *arg, va_list *va, const char **spec, int flags) /* {{{ */
1111
360k
{
1112
360k
  const char *expected_type = NULL;
1113
360k
  char *error = NULL;
1114
1115
360k
  expected_type = zend_parse_arg_impl(arg, va, spec, &error, arg_num);
1116
360k
  if (expected_type) {
1117
113
    if (EG(exception)) {
1118
15
      return FAILURE;
1119
15
    }
1120
98
    if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) {
1121
82
      if (error) {
1122
26
        if (strcmp(error, "must not contain any null bytes") == 0) {
1123
0
          zend_argument_value_error(arg_num, "%s", error);
1124
26
        } else {
1125
26
          zend_argument_type_error(arg_num, "%s", error);
1126
26
        }
1127
26
        efree(error);
1128
56
      } else {
1129
56
        zend_argument_type_error(arg_num, "must be of type %s, %s given", expected_type, zend_zval_value_name(arg));
1130
56
      }
1131
82
    } else if (error) {
1132
0
      efree(error);
1133
0
    }
1134
1135
98
    return FAILURE;
1136
113
  }
1137
1138
359k
  return SUCCESS;
1139
360k
}
1140
/* }}} */
1141
1142
ZEND_API zend_result zend_parse_parameter(int flags, uint32_t arg_num, zval *arg, const char *spec, ...)
1143
0
{
1144
0
  va_list va;
1145
0
  zend_result ret;
1146
1147
0
  va_start(va, spec);
1148
0
  ret = zend_parse_arg(arg_num, arg, &va, &spec, flags);
1149
0
  va_end(va);
1150
1151
0
  return ret;
1152
0
}
1153
1154
0
static ZEND_COLD void zend_parse_parameters_debug_error(const char *msg) {
1155
0
  const zend_function *active_function = EG(current_execute_data)->func;
1156
0
  const char *class_name = active_function->common.scope
1157
0
    ? ZSTR_VAL(active_function->common.scope->name) : "";
1158
0
  zend_error_noreturn(E_CORE_ERROR, "%s%s%s(): %s",
1159
0
    class_name, class_name[0] ? "::" : "",
1160
0
    ZSTR_VAL(active_function->common.function_name), msg);
1161
0
}
1162
1163
static zend_result zend_parse_va_args(uint32_t num_args, const char *type_spec, va_list *va, int flags) /* {{{ */
1164
284k
{
1165
284k
  const  char *spec_walk;
1166
284k
  char c;
1167
284k
  uint32_t i;
1168
284k
  uint32_t min_num_args = 0;
1169
284k
  uint32_t max_num_args = 0;
1170
284k
  uint32_t post_varargs = 0;
1171
284k
  zval *arg;
1172
284k
  bool have_varargs = false;
1173
284k
  bool have_optional_args = false;
1174
284k
  zval **varargs = NULL;
1175
284k
  uint32_t *n_varargs = NULL;
1176
1177
760k
  for (spec_walk = type_spec; *spec_walk; spec_walk++) {
1178
475k
    c = *spec_walk;
1179
475k
    switch (c) {
1180
12.1k
      case 'l': case 'd':
1181
245k
      case 's': case 'b':
1182
245k
      case 'r': case 'a':
1183
254k
      case 'o': case 'O':
1184
256k
      case 'z': case 'Z':
1185
293k
      case 'C': case 'h':
1186
295k
      case 'f': case 'F': case 'A':
1187
295k
      case 'H': case 'p':
1188
381k
      case 'S': case 'P':
1189
381k
      case 'L': case 'n':
1190
381k
        max_num_args++;
1191
381k
        break;
1192
1193
87.8k
      case '|':
1194
87.8k
        min_num_args = max_num_args;
1195
87.8k
        have_optional_args = true;
1196
87.8k
        break;
1197
1198
0
      case '/':
1199
5.84k
      case '!':
1200
        /* Pass */
1201
5.84k
        break;
1202
1203
395
      case '*':
1204
405
      case '+':
1205
405
        if (have_varargs) {
1206
0
          zend_parse_parameters_debug_error(
1207
0
            "only one varargs specifier (* or +) is permitted");
1208
0
          return FAILURE;
1209
0
        }
1210
405
        have_varargs = true;
1211
        /* we expect at least one parameter in varargs */
1212
405
        if (c == '+') {
1213
10
          max_num_args++;
1214
10
        }
1215
        /* mark the beginning of varargs */
1216
405
        post_varargs = max_num_args;
1217
1218
405
        if (ZEND_CALL_INFO(EG(current_execute_data)) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
1219
10
          zend_unexpected_extra_named_error();
1220
10
          return FAILURE;
1221
10
        }
1222
395
        break;
1223
1224
395
      default:
1225
0
        zend_parse_parameters_debug_error("bad type specifier while parsing parameters");
1226
0
        return FAILURE;
1227
475k
    }
1228
475k
  }
1229
1230
  /* with no optional arguments the minimum number of arguments must be the same as the maximum */
1231
284k
  if (!have_optional_args) {
1232
196k
    min_num_args = max_num_args;
1233
196k
  }
1234
1235
284k
  if (have_varargs) {
1236
    /* calculate how many required args are at the end of the specifier list */
1237
395
    post_varargs = max_num_args - post_varargs;
1238
395
    max_num_args = UINT32_MAX;
1239
395
  }
1240
1241
284k
  if (num_args < min_num_args || num_args > max_num_args) {
1242
81
    if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
1243
44
      zend_string *func_name = get_active_function_or_method_name();
1244
1245
44
      zend_argument_count_error("%s() expects %s %d argument%s, %d given",
1246
44
        ZSTR_VAL(func_name),
1247
44
        min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
1248
44
        num_args < min_num_args ? min_num_args : max_num_args,
1249
44
        (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
1250
44
        num_args
1251
44
      );
1252
1253
44
      zend_string_release(func_name);
1254
44
    }
1255
81
    return FAILURE;
1256
81
  }
1257
1258
284k
  if (num_args > ZEND_CALL_NUM_ARGS(EG(current_execute_data))) {
1259
0
    zend_parse_parameters_debug_error("could not obtain parameters for parsing");
1260
0
    return FAILURE;
1261
0
  }
1262
1263
284k
  i = 0;
1264
644k
  while (num_args-- > 0) {
1265
360k
    if (*type_spec == '|') {
1266
80.1k
      type_spec++;
1267
80.1k
    }
1268
1269
360k
    if (*type_spec == '*' || *type_spec == '+') {
1270
10
      uint32_t num_varargs = num_args + 1 - post_varargs;
1271
1272
      /* eat up the passed in storage even if it won't be filled in with varargs */
1273
10
      varargs = va_arg(*va, zval **);
1274
10
      n_varargs = va_arg(*va, uint32_t *);
1275
10
      type_spec++;
1276
1277
10
      if (num_varargs > 0) {
1278
10
        *n_varargs = num_varargs;
1279
10
        *varargs = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
1280
        /* adjust how many args we have left and restart loop */
1281
10
        num_args += 1 - num_varargs;
1282
10
        i += num_varargs;
1283
10
        continue;
1284
10
      } else {
1285
0
        *varargs = NULL;
1286
0
        *n_varargs = 0;
1287
0
      }
1288
10
    }
1289
1290
360k
    arg = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
1291
1292
360k
    if (zend_parse_arg(i+1, arg, va, &type_spec, flags) == FAILURE) {
1293
      /* clean up varargs array if it was used */
1294
113
      if (varargs && *varargs) {
1295
0
        *varargs = NULL;
1296
0
      }
1297
113
      return FAILURE;
1298
113
    }
1299
359k
    i++;
1300
359k
  }
1301
1302
284k
  return SUCCESS;
1303
284k
}
1304
/* }}} */
1305
1306
ZEND_API zend_result zend_parse_parameters_ex(int flags, uint32_t num_args, const char *type_spec, ...) /* {{{ */
1307
66
{
1308
66
  va_list va;
1309
66
  zend_result retval;
1310
1311
66
  va_start(va, type_spec);
1312
66
  retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1313
66
  va_end(va);
1314
1315
66
  return retval;
1316
66
}
1317
/* }}} */
1318
1319
ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec, ...) /* {{{ */
1320
284k
{
1321
284k
  va_list va;
1322
284k
  zend_result retval;
1323
284k
  int flags = 0;
1324
1325
284k
  va_start(va, type_spec);
1326
284k
  retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1327
284k
  va_end(va);
1328
1329
284k
  return retval;
1330
284k
}
1331
/* }}} */
1332
1333
ZEND_API zend_result zend_parse_method_parameters(uint32_t num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
1334
15
{
1335
15
  va_list va;
1336
15
  zend_result retval;
1337
15
  int flags = 0;
1338
15
  const char *p = type_spec;
1339
15
  zval **object;
1340
15
  zend_class_entry *ce;
1341
1342
  /* Just checking this_ptr is not enough, because fcall_common_helper does not set
1343
   * Z_OBJ(EG(This)) to NULL when calling an internal function with common.scope == NULL.
1344
   * In that case EG(This) would still be the $this from the calling code and we'd take the
1345
   * wrong branch here. */
1346
15
  bool is_method = EG(current_execute_data)->func->common.scope != NULL;
1347
1348
15
  if (!is_method || !this_ptr || Z_TYPE_P(this_ptr) != IS_OBJECT) {
1349
0
    va_start(va, type_spec);
1350
0
    retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1351
0
    va_end(va);
1352
15
  } else {
1353
15
    p++;
1354
1355
15
    va_start(va, type_spec);
1356
1357
15
    object = va_arg(va, zval **);
1358
15
    ce = va_arg(va, zend_class_entry *);
1359
15
    *object = this_ptr;
1360
1361
15
    if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
1362
0
      zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s()",
1363
0
        ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name(), ZSTR_VAL(ce->name), get_active_function_name());
1364
0
    }
1365
1366
15
    retval = zend_parse_va_args(num_args, p, &va, flags);
1367
15
    va_end(va);
1368
15
  }
1369
15
  return retval;
1370
15
}
1371
/* }}} */
1372
1373
ZEND_API zend_result zend_parse_method_parameters_ex(int flags, uint32_t num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
1374
0
{
1375
0
  va_list va;
1376
0
  zend_result retval;
1377
0
  const char *p = type_spec;
1378
0
  zval **object;
1379
0
  zend_class_entry *ce;
1380
1381
0
  if (!this_ptr) {
1382
0
    va_start(va, type_spec);
1383
0
    retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1384
0
    va_end(va);
1385
0
  } else {
1386
0
    p++;
1387
0
    va_start(va, type_spec);
1388
1389
0
    object = va_arg(va, zval **);
1390
0
    ce = va_arg(va, zend_class_entry *);
1391
0
    *object = this_ptr;
1392
1393
0
    if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
1394
0
      if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
1395
0
        zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s()",
1396
0
          ZSTR_VAL(ce->name), get_active_function_name(), ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name());
1397
0
      }
1398
0
      va_end(va);
1399
0
      return FAILURE;
1400
0
    }
1401
1402
0
    retval = zend_parse_va_args(num_args, p, &va, flags);
1403
0
    va_end(va);
1404
0
  }
1405
0
  return retval;
1406
0
}
1407
/* }}} */
1408
1409
/* This function should be called after the constructor has been called
1410
 * because it may call __set from the uninitialized object otherwise. */
1411
ZEND_API void zend_merge_properties(const zval *obj, const HashTable *properties) /* {{{ */
1412
0
{
1413
0
  zend_object *zobj = Z_OBJ_P(obj);
1414
0
  zend_object_write_property_t write_property = zobj->handlers->write_property;
1415
0
  zend_string *key;
1416
0
  zval *value;
1417
1418
0
  if (HT_IS_PACKED(properties)) {
1419
0
    return;
1420
0
  }
1421
1422
0
  const zend_class_entry *old_scope = EG(fake_scope);
1423
0
  EG(fake_scope) = Z_OBJCE_P(obj);
1424
0
  ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(properties, key, value) {
1425
0
    if (key) {
1426
0
      write_property(zobj, key, value, NULL);
1427
0
    }
1428
0
  } ZEND_HASH_FOREACH_END();
1429
0
  EG(fake_scope) = old_scope;
1430
0
}
1431
/* }}} */
1432
1433
static zend_class_mutable_data *zend_allocate_mutable_data(const zend_class_entry *class_type) /* {{{ */
1434
792
{
1435
792
  zend_class_mutable_data *mutable_data;
1436
1437
792
  ZEND_ASSERT(ZEND_MAP_PTR(class_type->mutable_data) != NULL);
1438
792
  ZEND_ASSERT(ZEND_MAP_PTR_GET_IMM(class_type->mutable_data) == NULL);
1439
1440
792
  mutable_data = zend_arena_alloc(&CG(arena), sizeof(zend_class_mutable_data));
1441
792
  memset(mutable_data, 0, sizeof(zend_class_mutable_data));
1442
792
  mutable_data->ce_flags = class_type->ce_flags;
1443
792
  ZEND_MAP_PTR_SET_IMM(class_type->mutable_data, mutable_data);
1444
1445
792
  return mutable_data;
1446
792
}
1447
/* }}} */
1448
1449
ZEND_API HashTable *zend_separate_class_constants_table(const zend_class_entry *class_type) /* {{{ */
1450
514
{
1451
514
  zend_class_mutable_data *mutable_data;
1452
514
  HashTable *constants_table;
1453
514
  zend_string *key;
1454
514
  zend_class_constant *new_c, *c;
1455
1456
514
  constants_table = zend_arena_alloc(&CG(arena), sizeof(HashTable));
1457
514
  zend_hash_init(constants_table, zend_hash_num_elements(&class_type->constants_table), NULL, NULL, 0);
1458
514
  zend_hash_extend(constants_table, zend_hash_num_elements(&class_type->constants_table), 0);
1459
1460
3.48k
  ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&class_type->constants_table, key, c) {
1461
3.48k
    if (c->ce == class_type) {
1462
1.11k
      if (Z_TYPE(c->value) == IS_CONSTANT_AST || (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED)) {
1463
810
        new_c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
1464
810
        memcpy(new_c, c, sizeof(zend_class_constant));
1465
810
        c = new_c;
1466
810
      }
1467
1.11k
      Z_TRY_ADDREF(c->value);
1468
1.11k
    } else {
1469
111
      if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
1470
99
        c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(c->ce), key);
1471
99
        ZEND_ASSERT(c);
1472
99
      }
1473
111
    }
1474
3.48k
    _zend_hash_append_ptr(constants_table, key, c);
1475
1.22k
  } ZEND_HASH_FOREACH_END();
1476
1477
514
  ZEND_ASSERT(ZEND_MAP_PTR(class_type->mutable_data) != NULL);
1478
1479
514
  mutable_data = ZEND_MAP_PTR_GET_IMM(class_type->mutable_data);
1480
514
  if (!mutable_data) {
1481
412
    mutable_data = zend_allocate_mutable_data(class_type);
1482
412
  }
1483
1484
514
  mutable_data->constants_table = constants_table;
1485
1486
514
  return constants_table;
1487
514
}
1488
1489
590
static zend_result update_property(zval *val, const zend_property_info *prop_info) {
1490
590
  if (ZEND_TYPE_IS_SET(prop_info->type)) {
1491
127
    zval tmp;
1492
1493
127
    ZVAL_COPY(&tmp, val);
1494
127
    if (UNEXPECTED(zval_update_constant_ex(&tmp, prop_info->ce) != SUCCESS)) {
1495
14
      zval_ptr_dtor(&tmp);
1496
14
      return FAILURE;
1497
14
    }
1498
113
    /* property initializers must always be evaluated with strict types */;
1499
113
    if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, /* strict */ 1))) {
1500
26
      zval_ptr_dtor(&tmp);
1501
26
      return FAILURE;
1502
26
    }
1503
87
    zval_ptr_dtor(val);
1504
87
    ZVAL_COPY_VALUE(val, &tmp);
1505
87
    return SUCCESS;
1506
113
  }
1507
463
  return zval_update_constant_ex(val, prop_info->ce);
1508
590
}
1509
1510
ZEND_API zend_result zend_update_class_constant(zend_class_constant *c, const zend_string *name, zend_class_entry *scope)
1511
2.49k
{
1512
2.49k
  ZEND_ASSERT(Z_TYPE(c->value) == IS_CONSTANT_AST);
1513
1514
2.49k
  if (EXPECTED(!ZEND_TYPE_IS_SET(c->type) || ZEND_TYPE_PURE_MASK(c->type) == MAY_BE_ANY)) {
1515
2.21k
    return zval_update_constant_ex(&c->value, scope);
1516
2.21k
  }
1517
1518
281
  zval tmp;
1519
1520
281
  ZVAL_COPY(&tmp, &c->value);
1521
281
  zend_result result = zval_update_constant_ex(&tmp, scope);
1522
281
  if (result == FAILURE) {
1523
16
    zval_ptr_dtor(&tmp);
1524
16
    return FAILURE;
1525
16
  }
1526
1527
265
  if (UNEXPECTED(!zend_verify_class_constant_type(c, name, &tmp))) {
1528
73
    zval_ptr_dtor(&tmp);
1529
73
    return FAILURE;
1530
73
  }
1531
1532
192
  zval_ptr_dtor(&c->value);
1533
192
  ZVAL_COPY_VALUE(&c->value, &tmp);
1534
1535
  /* may not return SUCCESS in case of an exception,
1536
   * should've returned FAILURE in zval_update_constant_ex! */
1537
192
  ZEND_ASSERT(!EG(exception));
1538
192
  return SUCCESS;
1539
192
}
1540
1541
ZEND_API zend_result zend_update_class_constants(zend_class_entry *class_type) /* {{{ */
1542
1.10k
{
1543
1.10k
  zend_class_mutable_data *mutable_data = NULL;
1544
1.10k
  zval *default_properties_table = NULL;
1545
1.10k
  zval *static_members_table = NULL;
1546
1.10k
  zend_class_constant *c;
1547
1.10k
  zval *val;
1548
1.10k
  uint32_t ce_flags;
1549
1550
1.10k
  ce_flags = class_type->ce_flags;
1551
1552
1.10k
  if (ce_flags & ZEND_ACC_CONSTANTS_UPDATED) {
1553
48
    return SUCCESS;
1554
48
  }
1555
1556
1.10k
  bool uses_mutable_data = ZEND_MAP_PTR(class_type->mutable_data) != NULL;
1557
1.05k
  if (uses_mutable_data) {
1558
633
    mutable_data = ZEND_MAP_PTR_GET_IMM(class_type->mutable_data);
1559
633
    if (mutable_data) {
1560
253
      ce_flags = mutable_data->ce_flags;
1561
253
      if (ce_flags & ZEND_ACC_CONSTANTS_UPDATED) {
1562
195
        return SUCCESS;
1563
195
      }
1564
380
    } else {
1565
380
      mutable_data = zend_allocate_mutable_data(class_type);
1566
380
    }
1567
633
  }
1568
1569
860
  if (class_type->parent) {
1570
72
    if (UNEXPECTED(zend_update_class_constants(class_type->parent) != SUCCESS)) {
1571
9
      return FAILURE;
1572
9
    }
1573
72
  }
1574
1575
851
  if (ce_flags & ZEND_ACC_HAS_AST_CONSTANTS) {
1576
536
    HashTable *constants_table;
1577
1578
536
    if (uses_mutable_data) {
1579
136
      constants_table = mutable_data->constants_table;
1580
136
      if (!constants_table) {
1581
102
        constants_table = zend_separate_class_constants_table(class_type);
1582
102
      }
1583
400
    } else {
1584
400
      constants_table = &class_type->constants_table;
1585
400
    }
1586
1587
536
    zend_string *name;
1588
3.18k
    ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(constants_table, name, val) {
1589
3.18k
      c = Z_PTR_P(val);
1590
3.18k
      if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
1591
795
        if (c->ce != class_type) {
1592
16
          Z_PTR_P(val) = c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(c->ce), name);
1593
16
          if (Z_TYPE(c->value) != IS_CONSTANT_AST) {
1594
8
            continue;
1595
8
          }
1596
16
        }
1597
1598
787
        val = &c->value;
1599
787
        if (UNEXPECTED(zend_update_class_constant(c, name, c->ce) != SUCCESS)) {
1600
55
          return FAILURE;
1601
55
        }
1602
787
      }
1603
3.18k
    } ZEND_HASH_FOREACH_END();
1604
536
  }
1605
1606
796
  if (class_type->default_static_members_count) {
1607
127
    static_members_table = CE_STATIC_MEMBERS(class_type);
1608
127
    if (!static_members_table) {
1609
125
      zend_class_init_statics(class_type);
1610
125
      static_members_table = CE_STATIC_MEMBERS(class_type);
1611
125
    }
1612
127
  }
1613
1614
796
  default_properties_table = class_type->default_properties_table;
1615
796
  if (uses_mutable_data && (ce_flags & ZEND_ACC_HAS_AST_PROPERTIES)) {
1616
232
    zval *src, *dst, *end;
1617
1618
232
    default_properties_table = mutable_data->default_properties_table;
1619
232
    if (!default_properties_table) {
1620
210
      default_properties_table = zend_arena_alloc(&CG(arena), sizeof(zval) * class_type->default_properties_count);
1621
210
      src = class_type->default_properties_table;
1622
210
      dst = default_properties_table;
1623
210
      end = dst + class_type->default_properties_count;
1624
391
      do {
1625
391
        ZVAL_COPY_PROP(dst, src);
1626
391
        src++;
1627
391
        dst++;
1628
391
      } while (dst != end);
1629
210
      mutable_data->default_properties_table = default_properties_table;
1630
210
    }
1631
232
  }
1632
1633
796
  if (ce_flags & (ZEND_ACC_HAS_AST_PROPERTIES|ZEND_ACC_HAS_AST_STATICS)) {
1634
328
    zend_property_info *prop_info;
1635
1636
    /* Use the default properties table to also update initializers of private properties
1637
     * that have been shadowed in a child class. */
1638
638
    for (uint32_t i = 0; i < class_type->default_properties_count; i++) {
1639
396
      prop_info = class_type->properties_info_table[i];
1640
396
      if (!prop_info) {
1641
7
        continue;
1642
7
      }
1643
1644
389
      val = &default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
1645
389
      if (Z_TYPE_P(val) == IS_CONSTANT_AST
1646
308
          && UNEXPECTED(update_property(val, prop_info) != SUCCESS)) {
1647
86
        return FAILURE;
1648
86
      }
1649
389
    }
1650
1651
242
    if (class_type->default_static_members_count) {
1652
798
      ZEND_HASH_MAP_FOREACH_PTR(&class_type->properties_info, prop_info) {
1653
798
        if (prop_info->flags & ZEND_ACC_STATIC) {
1654
294
          val = static_members_table + prop_info->offset;
1655
294
          if (Z_TYPE_P(val) == IS_CONSTANT_AST
1656
282
              && UNEXPECTED(update_property(val, prop_info) != SUCCESS)) {
1657
33
            return FAILURE;
1658
33
          }
1659
294
        }
1660
798
      } ZEND_HASH_FOREACH_END();
1661
93
    }
1662
242
  }
1663
1664
677
  if (class_type->type == ZEND_USER_CLASS && class_type->ce_flags & ZEND_ACC_ENUM && class_type->enum_backing_type != IS_UNDEF) {
1665
373
    if (zend_enum_build_backed_enum_table(class_type) == FAILURE) {
1666
95
      return FAILURE;
1667
95
    }
1668
373
  }
1669
1670
582
  ce_flags |= ZEND_ACC_CONSTANTS_UPDATED;
1671
582
  ce_flags &= ~ZEND_ACC_HAS_AST_CONSTANTS;
1672
582
  ce_flags &= ~ZEND_ACC_HAS_AST_PROPERTIES;
1673
582
  ce_flags &= ~ZEND_ACC_HAS_AST_STATICS;
1674
582
  if (uses_mutable_data) {
1675
281
    mutable_data->ce_flags = ce_flags;
1676
301
  } else {
1677
301
    class_type->ce_flags = ce_flags;
1678
301
  }
1679
1680
582
  return SUCCESS;
1681
677
}
1682
/* }}} */
1683
1684
static zend_always_inline void _object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1685
2.14M
{
1686
2.14M
  if (class_type->default_properties_count) {
1687
951k
    zval *src = CE_DEFAULT_PROPERTIES_TABLE(class_type);
1688
951k
    zval *dst = object->properties_table;
1689
951k
    zval *end = src + class_type->default_properties_count;
1690
1691
951k
    if (UNEXPECTED(class_type->type == ZEND_INTERNAL_CLASS)) {
1692
      /* We don't have to account for refcounting because
1693
       * zend_declare_typed_property() disallows refcounted defaults for internal classes. */
1694
6.40M
      do {
1695
6.40M
        ZEND_ASSERT(!Z_REFCOUNTED_P(src));
1696
6.40M
        ZVAL_COPY_VALUE_PROP(dst, src);
1697
6.40M
        src++;
1698
6.40M
        dst++;
1699
6.40M
      } while (src != end);
1700
925k
    } else {
1701
38.7k
      do {
1702
38.7k
        ZVAL_COPY_PROP(dst, src);
1703
38.7k
        src++;
1704
38.7k
        dst++;
1705
38.7k
      } while (src != end);
1706
25.8k
    }
1707
951k
  }
1708
2.14M
}
1709
/* }}} */
1710
1711
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1712
2.03M
{
1713
2.03M
  object->properties = NULL;
1714
2.03M
  _object_properties_init(object, class_type);
1715
2.03M
}
1716
/* }}} */
1717
1718
ZEND_API void object_properties_init_ex(zend_object *object, HashTable *properties) /* {{{ */
1719
0
{
1720
0
  object->properties = properties;
1721
0
  if (object->ce->default_properties_count) {
1722
0
    zval *prop;
1723
0
    zend_string *key;
1724
1725
0
    ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(properties, key, prop) {
1726
0
      const zend_property_info *property_info = zend_get_property_info(object->ce, key, 1);
1727
0
      if (property_info != ZEND_WRONG_PROPERTY_INFO &&
1728
0
          property_info &&
1729
0
          (property_info->flags & ZEND_ACC_STATIC) == 0) {
1730
0
        zval *slot = OBJ_PROP(object, property_info->offset);
1731
1732
0
        if (ZEND_TYPE_IS_SET(property_info->type)) {
1733
0
          zval tmp;
1734
1735
0
          ZVAL_COPY_VALUE(&tmp, prop);
1736
0
          if (UNEXPECTED(!zend_verify_property_type(property_info, &tmp, 0))) {
1737
0
            continue;
1738
0
          }
1739
0
          ZVAL_COPY_VALUE(slot, &tmp);
1740
0
        } else {
1741
0
          ZVAL_COPY_VALUE(slot, prop);
1742
0
        }
1743
0
        ZVAL_INDIRECT(prop, slot);
1744
0
      }
1745
0
    } ZEND_HASH_FOREACH_END();
1746
0
  }
1747
0
}
1748
/* }}} */
1749
1750
ZEND_API void object_properties_load(zend_object *object, const HashTable *properties) /* {{{ */
1751
35.6k
{
1752
35.6k
  zval *prop, tmp;
1753
35.6k
  zend_string *key;
1754
35.6k
  zend_long h;
1755
35.6k
  const zend_property_info *property_info;
1756
1757
176k
  ZEND_HASH_FOREACH_KEY_VAL(properties, h, key, prop) {
1758
176k
    if (key) {
1759
8.82k
      if (ZSTR_VAL(key)[0] == '\0') {
1760
5.77k
        const char *class_name, *prop_name;
1761
5.77k
        size_t prop_name_len;
1762
5.77k
        if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) {
1763
5.53k
          zend_string *pname = zend_string_init(prop_name, prop_name_len, 0);
1764
5.53k
          const zend_class_entry *prev_scope = EG(fake_scope);
1765
5.53k
          if (class_name && class_name[0] != '*') {
1766
5.37k
            zend_string *cname = zend_string_init(class_name, strlen(class_name), 0);
1767
5.37k
            EG(fake_scope) = zend_lookup_class(cname);
1768
5.37k
            zend_string_release_ex(cname, 0);
1769
5.37k
          }
1770
5.53k
          property_info = zend_get_property_info(object->ce, pname, 1);
1771
5.53k
          zend_string_release_ex(pname, 0);
1772
5.53k
          EG(fake_scope) = prev_scope;
1773
5.53k
        } else {
1774
238
          property_info = ZEND_WRONG_PROPERTY_INFO;
1775
238
        }
1776
5.77k
      } else {
1777
3.04k
        property_info = zend_get_property_info(object->ce, key, 1);
1778
3.04k
      }
1779
8.82k
      if (property_info != ZEND_WRONG_PROPERTY_INFO &&
1780
6.70k
        property_info &&
1781
0
        (property_info->flags & ZEND_ACC_STATIC) == 0) {
1782
0
        zval *slot = OBJ_PROP(object, property_info->offset);
1783
0
        if (UNEXPECTED((property_info->flags & ZEND_ACC_READONLY) && !Z_ISUNDEF_P(slot))) {
1784
0
          if (Z_PROP_FLAG_P(slot) & IS_PROP_REINITABLE) {
1785
0
            Z_PROP_FLAG_P(slot) &= ~IS_PROP_REINITABLE;
1786
0
          } else {
1787
0
            zend_readonly_property_modification_error(property_info);
1788
0
            return;
1789
0
          }
1790
0
        }
1791
0
        zval_ptr_dtor(slot);
1792
0
        ZVAL_COPY_VALUE(slot, prop);
1793
0
        zval_add_ref(slot);
1794
0
        if (object->properties) {
1795
0
          ZVAL_INDIRECT(&tmp, slot);
1796
0
          zend_hash_update(object->properties, key, &tmp);
1797
0
        }
1798
8.82k
      } else {
1799
8.82k
        if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) {
1800
0
          zend_throw_error(NULL, "Cannot create dynamic property %s::$%s",
1801
0
            ZSTR_VAL(object->ce->name), property_info != ZEND_WRONG_PROPERTY_INFO ? zend_get_unmangled_property_name(key): "");
1802
0
          return;
1803
8.82k
        } else if (!(object->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES)) {
1804
8.82k
          zend_error(E_DEPRECATED, "Creation of dynamic property %s::$%s is deprecated",
1805
8.82k
            ZSTR_VAL(object->ce->name), property_info != ZEND_WRONG_PROPERTY_INFO ? zend_get_unmangled_property_name(key): "");
1806
8.82k
        }
1807
1808
8.82k
        prop = zend_hash_update(zend_std_get_properties_ex(object), key, prop);
1809
8.82k
        zval_add_ref(prop);
1810
8.82k
      }
1811
61.3k
    } else {
1812
61.3k
      if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) {
1813
0
        zend_throw_error(NULL, "Cannot create dynamic property %s::$" ZEND_LONG_FMT, ZSTR_VAL(object->ce->name), h);
1814
0
        return;
1815
61.3k
      } else if (!(object->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES)) {
1816
61.3k
        zend_error(E_DEPRECATED, "Creation of dynamic property %s::$" ZEND_LONG_FMT " is deprecated",
1817
61.3k
          ZSTR_VAL(object->ce->name), h);
1818
61.3k
      }
1819
1820
61.3k
      prop = zend_hash_index_update(zend_std_get_properties_ex(object), h, prop);
1821
61.3k
      zval_add_ref(prop);
1822
61.3k
    }
1823
176k
  } ZEND_HASH_FOREACH_END();
1824
35.6k
}
1825
/* }}} */
1826
1827
/* This function requires 'properties' to contain all props declared in the
1828
 * class and all props being public. If only a subset is given or the class
1829
 * has protected members then you need to merge the properties separately by
1830
 * calling zend_merge_properties(). */
1831
static zend_always_inline zend_result _object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
1832
2.16M
{
1833
2.16M
  if (UNEXPECTED(class_type->ce_flags & ZEND_ACC_UNINSTANTIABLE)) {
1834
119
    if (class_type->ce_flags & ZEND_ACC_INTERFACE) {
1835
5
      zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(class_type->name));
1836
114
    } else if (class_type->ce_flags & ZEND_ACC_TRAIT) {
1837
8
      zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(class_type->name));
1838
106
    } else if (class_type->ce_flags & ZEND_ACC_ENUM) {
1839
82
      zend_throw_error(NULL, "Cannot instantiate enum %s", ZSTR_VAL(class_type->name));
1840
82
    } else {
1841
24
      ZEND_ASSERT(class_type->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS));
1842
24
      zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name));
1843
24
    }
1844
119
    ZVAL_NULL(arg);
1845
119
    Z_OBJ_P(arg) = NULL;
1846
119
    return FAILURE;
1847
119
  }
1848
1849
2.16M
  if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
1850
328
    if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
1851
118
      ZVAL_NULL(arg);
1852
118
      Z_OBJ_P(arg) = NULL;
1853
118
      return FAILURE;
1854
118
    }
1855
328
  }
1856
1857
2.16M
  if (class_type->create_object == NULL) {
1858
114k
    zend_object *obj = zend_objects_new(class_type);
1859
1860
114k
    ZVAL_OBJ(arg, obj);
1861
114k
    if (properties) {
1862
0
      object_properties_init_ex(obj, properties);
1863
114k
    } else {
1864
114k
      _object_properties_init(obj, class_type);
1865
114k
    }
1866
2.05M
  } else {
1867
2.05M
    ZVAL_OBJ(arg, class_type->create_object(class_type));
1868
2.05M
  }
1869
2.16M
  return SUCCESS;
1870
2.16M
}
1871
/* }}} */
1872
1873
ZEND_API zend_result object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
1874
0
{
1875
0
  return _object_and_properties_init(arg, class_type, properties);
1876
0
}
1877
/* }}} */
1878
1879
ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *class_type) /* {{{ */
1880
2.16M
{
1881
2.16M
  return _object_and_properties_init(arg, class_type, NULL);
1882
2.16M
}
1883
/* }}} */
1884
1885
ZEND_API zend_result object_init_with_constructor(zval *arg, zend_class_entry *class_type, uint32_t param_count, zval *params, HashTable *named_params) /* {{{ */
1886
1.08k
{
1887
1.08k
  zend_result status = _object_and_properties_init(arg, class_type, NULL);
1888
1.08k
  if (UNEXPECTED(status == FAILURE)) {
1889
0
    ZVAL_UNDEF(arg);
1890
0
    return FAILURE;
1891
0
  }
1892
1.08k
  zend_object *obj = Z_OBJ_P(arg);
1893
1.08k
  zend_function *constructor = obj->handlers->get_constructor(obj);
1894
1.08k
  if (constructor == NULL) {
1895
    /* The constructor can be NULL for 2 different reasons:
1896
     * - It is not defined
1897
     * - We are not allowed to call the constructor (e.g. private, or internal opaque class)
1898
     *   and an exception has been thrown
1899
     * in the former case, we are (mostly) done and the object is initialized,
1900
     * in the latter we need to destroy the object as initialization failed
1901
     */
1902
102
    if (UNEXPECTED(EG(exception))) {
1903
5
      zval_ptr_dtor(arg);
1904
5
      ZVAL_UNDEF(arg);
1905
5
      return FAILURE;
1906
5
    }
1907
1908
    /* Surprisingly, this is the only case where internal classes will allow to pass extra arguments
1909
     * However, if there are named arguments (and it is not empty),
1910
     * an Error must be thrown to be consistent with new ClassName() */
1911
97
    if (UNEXPECTED(named_params != NULL && zend_hash_num_elements(named_params) != 0)) {
1912
      /* Throw standard Error */
1913
7
      zend_string *arg_name = NULL;
1914
7
      zend_hash_get_current_key(named_params, &arg_name, /* num_index */ NULL);
1915
7
      ZEND_ASSERT(arg_name != NULL);
1916
7
      zend_throw_error(NULL, "Unknown named parameter $%s", ZSTR_VAL(arg_name));
1917
      /* Do not call destructor, free object, and set arg to IS_UNDEF */
1918
7
      zend_object_store_ctor_failed(obj);
1919
7
      zval_ptr_dtor(arg);
1920
7
      ZVAL_UNDEF(arg);
1921
7
      return FAILURE;
1922
90
    } else {
1923
90
      return SUCCESS;
1924
90
    }
1925
97
  }
1926
  /* A constructor should not return a value, however if an exception is thrown
1927
   * zend_call_known_function() will set the retval to IS_UNDEF */
1928
985
  zval retval;
1929
985
  zend_call_known_function(
1930
985
    constructor,
1931
985
    obj,
1932
985
    class_type,
1933
985
    &retval,
1934
985
    param_count,
1935
985
    params,
1936
985
    named_params
1937
985
  );
1938
985
  if (Z_TYPE(retval) == IS_UNDEF) {
1939
    /* Do not call destructor, free object, and set arg to IS_UNDEF */
1940
44
    zend_object_store_ctor_failed(obj);
1941
44
    zval_ptr_dtor(arg);
1942
44
    ZVAL_UNDEF(arg);
1943
44
    return FAILURE;
1944
941
  } else {
1945
    /* Unlikely, but user constructors may return any value they want */
1946
941
    zval_ptr_dtor(&retval);
1947
941
    return SUCCESS;
1948
941
  }
1949
985
}
1950
/* }}} */
1951
1952
ZEND_API void object_init(zval *arg) /* {{{ */
1953
1.11M
{
1954
1.11M
  ZVAL_OBJ(arg, zend_objects_new(zend_standard_class_def));
1955
1.11M
}
1956
/* }}} */
1957
1958
ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
1959
32.4k
{
1960
32.4k
  zval tmp;
1961
1962
32.4k
  ZVAL_LONG(&tmp, n);
1963
32.4k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1964
32.4k
}
1965
/* }}} */
1966
1967
ZEND_API void add_assoc_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
1968
8.55k
{
1969
8.55k
  zval tmp;
1970
1971
8.55k
  ZVAL_NULL(&tmp);
1972
8.55k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1973
8.55k
}
1974
/* }}} */
1975
1976
ZEND_API void add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, bool b) /* {{{ */
1977
1.90k
{
1978
1.90k
  zval tmp;
1979
1980
1.90k
  ZVAL_BOOL(&tmp, b);
1981
1.90k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1982
1.90k
}
1983
/* }}} */
1984
1985
ZEND_API void add_assoc_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
1986
0
{
1987
0
  zval tmp;
1988
1989
0
  ZVAL_RES(&tmp, r);
1990
0
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1991
0
}
1992
/* }}} */
1993
1994
ZEND_API void add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
1995
1.85k
{
1996
1.85k
  zval tmp;
1997
1998
1.85k
  ZVAL_DOUBLE(&tmp, d);
1999
1.85k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
2000
1.85k
}
2001
/* }}} */
2002
2003
ZEND_API void add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
2004
5.55k
{
2005
5.55k
  zval tmp;
2006
2007
5.55k
  ZVAL_STR(&tmp, str);
2008
5.55k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
2009
5.55k
}
2010
/* }}} */
2011
2012
ZEND_API void add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
2013
5.63k
{
2014
5.63k
  zval tmp;
2015
2016
5.63k
  ZVAL_STRING(&tmp, str);
2017
5.63k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
2018
5.63k
}
2019
/* }}} */
2020
2021
ZEND_API void add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
2022
27.4k
{
2023
27.4k
  zval tmp;
2024
2025
27.4k
  ZVAL_STRINGL(&tmp, str, length);
2026
27.4k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
2027
27.4k
}
2028
/* }}} */
2029
2030
ZEND_API void add_assoc_array_ex(zval *arg, const char *key, size_t key_len, zend_array *arr) /* {{{ */
2031
0
{
2032
0
  zval tmp;
2033
2034
0
  ZVAL_ARR(&tmp, arr);
2035
0
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
2036
0
}
2037
/* }}} */
2038
2039
ZEND_API void add_assoc_object_ex(zval *arg, const char *key, size_t key_len, zend_object *obj) /* {{{ */
2040
368
{
2041
368
  zval tmp;
2042
2043
368
  ZVAL_OBJ(&tmp, obj);
2044
368
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
2045
368
}
2046
/* }}} */
2047
2048
ZEND_API void add_assoc_reference_ex(zval *arg, const char *key, size_t key_len, zend_reference *ref) /* {{{ */
2049
0
{
2050
0
  zval tmp;
2051
2052
0
  ZVAL_REF(&tmp, ref);
2053
0
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
2054
0
}
2055
/* }}} */
2056
2057
ZEND_API void add_assoc_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
2058
31.5k
{
2059
31.5k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value);
2060
31.5k
}
2061
/* }}} */
2062
2063
ZEND_API void add_index_long(zval *arg, zend_ulong index, zend_long n) /* {{{ */
2064
4.15M
{
2065
4.15M
  zval tmp;
2066
2067
4.15M
  ZVAL_LONG(&tmp, n);
2068
4.15M
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2069
4.15M
}
2070
/* }}} */
2071
2072
ZEND_API void add_index_null(zval *arg, zend_ulong index) /* {{{ */
2073
0
{
2074
0
  zval tmp;
2075
2076
0
  ZVAL_NULL(&tmp);
2077
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2078
0
}
2079
/* }}} */
2080
2081
ZEND_API void add_index_bool(zval *arg, zend_ulong index, bool b) /* {{{ */
2082
0
{
2083
0
  zval tmp;
2084
2085
0
  ZVAL_BOOL(&tmp, b);
2086
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2087
0
}
2088
/* }}} */
2089
2090
ZEND_API void add_index_resource(zval *arg, zend_ulong index, zend_resource *r) /* {{{ */
2091
0
{
2092
0
  zval tmp;
2093
2094
0
  ZVAL_RES(&tmp, r);
2095
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2096
0
}
2097
/* }}} */
2098
2099
ZEND_API void add_index_double(zval *arg, zend_ulong index, double d) /* {{{ */
2100
2.34M
{
2101
2.34M
  zval tmp;
2102
2103
2.34M
  ZVAL_DOUBLE(&tmp, d);
2104
2.34M
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2105
2.34M
}
2106
/* }}} */
2107
2108
ZEND_API void add_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */
2109
0
{
2110
0
  zval tmp;
2111
2112
0
  ZVAL_STR(&tmp, str);
2113
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2114
0
}
2115
/* }}} */
2116
2117
ZEND_API void add_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */
2118
598k
{
2119
598k
  zval tmp;
2120
2121
598k
  ZVAL_STRING(&tmp, str);
2122
598k
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2123
598k
}
2124
/* }}} */
2125
2126
ZEND_API void add_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */
2127
0
{
2128
0
  zval tmp;
2129
2130
0
  ZVAL_STRINGL(&tmp, str, length);
2131
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2132
0
}
2133
/* }}} */
2134
2135
ZEND_API void add_index_array(zval *arg, zend_ulong index, zend_array *arr) /* {{{ */
2136
0
{
2137
0
  zval tmp;
2138
2139
0
  ZVAL_ARR(&tmp, arr);
2140
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2141
0
}
2142
/* }}} */
2143
2144
ZEND_API void add_index_object(zval *arg, zend_ulong index, zend_object *obj) /* {{{ */
2145
0
{
2146
0
  zval tmp;
2147
2148
0
  ZVAL_OBJ(&tmp, obj);
2149
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2150
0
}
2151
/* }}} */
2152
2153
ZEND_API void add_index_reference(zval *arg, zend_ulong index, zend_reference *ref) /* {{{ */
2154
0
{
2155
0
  zval tmp;
2156
2157
0
  ZVAL_REF(&tmp, ref);
2158
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2159
0
}
2160
/* }}} */
2161
2162
ZEND_API zend_result add_next_index_long(zval *arg, zend_long n) /* {{{ */
2163
0
{
2164
0
  zval tmp;
2165
2166
0
  ZVAL_LONG(&tmp, n);
2167
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2168
0
}
2169
/* }}} */
2170
2171
ZEND_API zend_result add_next_index_null(zval *arg) /* {{{ */
2172
0
{
2173
0
  zval tmp;
2174
2175
0
  ZVAL_NULL(&tmp);
2176
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2177
0
}
2178
/* }}} */
2179
2180
ZEND_API zend_result add_next_index_bool(zval *arg, bool b) /* {{{ */
2181
0
{
2182
0
  zval tmp;
2183
2184
0
  ZVAL_BOOL(&tmp, b);
2185
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2186
0
}
2187
/* }}} */
2188
2189
ZEND_API zend_result add_next_index_resource(zval *arg, zend_resource *r) /* {{{ */
2190
0
{
2191
0
  zval tmp;
2192
2193
0
  ZVAL_RES(&tmp, r);
2194
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2195
0
}
2196
/* }}} */
2197
2198
ZEND_API zend_result add_next_index_double(zval *arg, double d) /* {{{ */
2199
0
{
2200
0
  zval tmp;
2201
2202
0
  ZVAL_DOUBLE(&tmp, d);
2203
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2204
0
}
2205
/* }}} */
2206
2207
ZEND_API zend_result add_next_index_str(zval *arg, zend_string *str) /* {{{ */
2208
63.1k
{
2209
63.1k
  zval tmp;
2210
2211
63.1k
  ZVAL_STR(&tmp, str);
2212
63.1k
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2213
63.1k
}
2214
/* }}} */
2215
2216
ZEND_API zend_result add_next_index_string(zval *arg, const char *str) /* {{{ */
2217
838
{
2218
838
  zval tmp;
2219
2220
838
  ZVAL_STRING(&tmp, str);
2221
838
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2222
838
}
2223
/* }}} */
2224
2225
ZEND_API zend_result add_next_index_stringl(zval *arg, const char *str, size_t length) /* {{{ */
2226
6
{
2227
6
  zval tmp;
2228
2229
6
  ZVAL_STRINGL(&tmp, str, length);
2230
6
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2231
6
}
2232
/* }}} */
2233
2234
ZEND_API zend_result add_next_index_array(zval *arg, zend_array *arr) /* {{{ */
2235
0
{
2236
0
  zval tmp;
2237
2238
0
  ZVAL_ARR(&tmp, arr);
2239
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2240
0
}
2241
/* }}} */
2242
2243
ZEND_API zend_result add_next_index_object(zval *arg, zend_object *obj) /* {{{ */
2244
0
{
2245
0
  zval tmp;
2246
2247
0
  ZVAL_OBJ(&tmp, obj);
2248
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2249
0
}
2250
/* }}} */
2251
2252
ZEND_API zend_result add_next_index_reference(zval *arg, zend_reference *ref) /* {{{ */
2253
0
{
2254
0
  zval tmp;
2255
2256
0
  ZVAL_REF(&tmp, ref);
2257
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2258
0
}
2259
/* }}} */
2260
2261
ZEND_API zend_result array_set_zval_key(HashTable *ht, zval *key, zval *value) /* {{{ */
2262
759
{
2263
759
  zval *result;
2264
2265
759
  switch (Z_TYPE_P(key)) {
2266
396
    case IS_STRING:
2267
396
      result = zend_symtable_update(ht, Z_STR_P(key), value);
2268
396
      break;
2269
0
    case IS_RESOURCE:
2270
0
      zend_use_resource_as_offset(key);
2271
0
      result = zend_hash_index_update(ht, Z_RES_HANDLE_P(key), value);
2272
0
      break;
2273
7
    case IS_FALSE:
2274
7
      result = zend_hash_index_update(ht, 0, value);
2275
7
      break;
2276
7
    case IS_TRUE:
2277
7
      result = zend_hash_index_update(ht, 1, value);
2278
7
      break;
2279
327
    case IS_LONG:
2280
327
      result = zend_hash_index_update(ht, Z_LVAL_P(key), value);
2281
327
      break;
2282
7
    case IS_DOUBLE:
2283
7
      result = zend_hash_index_update(ht, zend_dval_to_lval_safe(Z_DVAL_P(key)), value);
2284
7
      break;
2285
7
    case IS_NULL:
2286
7
      zend_error(E_DEPRECATED, "Using null as an array offset is deprecated, use an empty string instead");
2287
7
      if (UNEXPECTED(EG(exception))) {
2288
0
        return FAILURE;
2289
0
      }
2290
7
      result = zend_hash_update(ht, ZSTR_EMPTY_ALLOC(), value);
2291
7
      break;
2292
8
    default:
2293
8
      zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), key, BP_VAR_W);
2294
8
      result = NULL;
2295
759
  }
2296
2297
759
  if (result) {
2298
751
    Z_TRY_ADDREF_P(result);
2299
751
    return SUCCESS;
2300
751
  } else {
2301
8
    return FAILURE;
2302
8
  }
2303
759
}
2304
/* }}} */
2305
2306
ZEND_API void add_property_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
2307
0
{
2308
0
  zval tmp;
2309
2310
0
  ZVAL_LONG(&tmp, n);
2311
0
  add_property_zval_ex(arg, key, key_len, &tmp);
2312
0
}
2313
/* }}} */
2314
2315
ZEND_API void add_property_bool_ex(zval *arg, const char *key, size_t key_len, zend_long b) /* {{{ */
2316
0
{
2317
0
  zval tmp;
2318
2319
0
  ZVAL_BOOL(&tmp, b);
2320
0
  add_property_zval_ex(arg, key, key_len, &tmp);
2321
0
}
2322
/* }}} */
2323
2324
ZEND_API void add_property_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
2325
5.37k
{
2326
5.37k
  zval tmp;
2327
2328
5.37k
  ZVAL_NULL(&tmp);
2329
5.37k
  add_property_zval_ex(arg, key, key_len, &tmp);
2330
5.37k
}
2331
/* }}} */
2332
2333
ZEND_API void add_property_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
2334
241
{
2335
241
  zval tmp;
2336
2337
241
  ZVAL_RES(&tmp, r);
2338
241
  add_property_zval_ex(arg, key, key_len, &tmp);
2339
241
  zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2340
241
}
2341
/* }}} */
2342
2343
ZEND_API void add_property_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
2344
0
{
2345
0
  zval tmp;
2346
2347
0
  ZVAL_DOUBLE(&tmp, d);
2348
0
  add_property_zval_ex(arg, key, key_len, &tmp);
2349
0
}
2350
/* }}} */
2351
2352
ZEND_API void add_property_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
2353
0
{
2354
0
  zval tmp;
2355
2356
0
  ZVAL_STR(&tmp, str);
2357
0
  add_property_zval_ex(arg, key, key_len, &tmp);
2358
0
  zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2359
0
}
2360
/* }}} */
2361
2362
ZEND_API void add_property_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
2363
78
{
2364
78
  zval tmp;
2365
2366
78
  ZVAL_STRING(&tmp, str);
2367
78
  add_property_zval_ex(arg, key, key_len, &tmp);
2368
78
  zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2369
78
}
2370
/* }}} */
2371
2372
ZEND_API void add_property_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
2373
0
{
2374
0
  zval tmp;
2375
2376
0
  ZVAL_STRINGL(&tmp, str, length);
2377
0
  add_property_zval_ex(arg, key, key_len, &tmp);
2378
0
  zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2379
0
}
2380
/* }}} */
2381
2382
ZEND_API void add_property_array_ex(zval *arg, const char *key, size_t key_len, zend_array *arr) /* {{{ */
2383
0
{
2384
0
  zval tmp;
2385
2386
0
  ZVAL_ARR(&tmp, arr);
2387
0
  add_property_zval_ex(arg, key, key_len, &tmp);
2388
0
  zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2389
0
}
2390
/* }}} */
2391
2392
ZEND_API void add_property_object_ex(zval *arg, const char *key, size_t key_len, zend_object *obj) /* {{{ */
2393
0
{
2394
0
  zval tmp;
2395
2396
0
  ZVAL_OBJ(&tmp, obj);
2397
0
  add_property_zval_ex(arg, key, key_len, &tmp);
2398
0
  zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2399
0
}
2400
/* }}} */
2401
2402
ZEND_API void add_property_reference_ex(zval *arg, const char *key, size_t key_len, zend_reference *ref) /* {{{ */
2403
0
{
2404
0
  zval tmp;
2405
2406
0
  ZVAL_REF(&tmp, ref);
2407
0
  add_property_zval_ex(arg, key, key_len, &tmp);
2408
0
  zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2409
0
}
2410
/* }}} */
2411
2412
ZEND_API void add_property_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
2413
5.69k
{
2414
5.69k
  zend_string *str;
2415
2416
5.69k
  str = zend_string_init(key, key_len, 0);
2417
5.69k
  Z_OBJ_HANDLER_P(arg, write_property)(Z_OBJ_P(arg), str, value, NULL);
2418
5.69k
  zend_string_release_ex(str, 0);
2419
5.69k
}
2420
/* }}} */
2421
2422
ZEND_API zend_result zend_startup_module_ex(zend_module_entry *module) /* {{{ */
2423
208
{
2424
208
  size_t name_len;
2425
208
  zend_string *lcname;
2426
2427
208
  if (module->module_started) {
2428
0
    return SUCCESS;
2429
0
  }
2430
208
  module->module_started = 1;
2431
2432
  /* Check module dependencies */
2433
208
  if (module->deps) {
2434
64
    const zend_module_dep *dep = module->deps;
2435
2436
176
    while (dep->name) {
2437
112
      if (dep->type == MODULE_DEP_REQUIRED) {
2438
80
        zend_module_entry *req_mod;
2439
2440
80
        name_len = strlen(dep->name);
2441
80
        lcname = zend_string_alloc(name_len, 0);
2442
80
        zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
2443
2444
80
        if ((req_mod = zend_hash_find_ptr(&module_registry, lcname)) == NULL || !req_mod->module_started) {
2445
0
          zend_string_efree(lcname);
2446
          /* TODO: Check version relationship */
2447
0
          zend_error(E_CORE_WARNING, "Cannot load module \"%s\" because required module \"%s\" is not loaded", module->name, dep->name);
2448
0
          module->module_started = 0;
2449
0
          return FAILURE;
2450
0
        }
2451
80
        zend_string_efree(lcname);
2452
80
      }
2453
112
      ++dep;
2454
112
    }
2455
64
  }
2456
2457
  /* Initialize module globals */
2458
208
  if (module->globals_size) {
2459
#ifdef ZTS
2460
    ts_allocate_id(module->globals_id_ptr, module->globals_size, (ts_allocate_ctor) module->globals_ctor, (ts_allocate_dtor) module->globals_dtor);
2461
#else
2462
96
    if (module->globals_ctor) {
2463
80
      module->globals_ctor(module->globals_ptr);
2464
80
    }
2465
96
#endif
2466
96
  }
2467
208
  if (module->module_startup_func) {
2468
208
    EG(current_module) = module;
2469
208
    if (module->module_startup_func(module->type, module->module_number)==FAILURE) {
2470
0
      zend_error_noreturn(E_CORE_ERROR,"Unable to start %s module", module->name);
2471
0
    }
2472
208
    EG(current_module) = NULL;
2473
208
  }
2474
208
  return SUCCESS;
2475
208
}
2476
/* }}} */
2477
2478
static int zend_startup_module_zval(zval *zv) /* {{{ */
2479
208
{
2480
208
  zend_module_entry *module = Z_PTR_P(zv);
2481
2482
208
  return (zend_startup_module_ex(module) == SUCCESS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
2483
208
}
2484
/* }}} */
2485
2486
static void zend_sort_modules(void *base, size_t count, size_t siz, compare_func_t compare, swap_func_t swp) /* {{{ */
2487
16
{
2488
16
  Bucket *b1 = base;
2489
16
  Bucket *b2;
2490
16
  Bucket *end = b1 + count;
2491
16
  Bucket tmp;
2492
16
  zend_module_entry *m, *r;
2493
2494
224
  while (b1 < end) {
2495
272
try_again:
2496
272
    m = (zend_module_entry*)Z_PTR(b1->val);
2497
272
    if (!m->module_started && m->deps) {
2498
128
      const zend_module_dep *dep = m->deps;
2499
256
      while (dep->name) {
2500
192
        if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) {
2501
192
          b2 = b1 + 1;
2502
656
          while (b2 < end) {
2503
528
            r = (zend_module_entry*)Z_PTR(b2->val);
2504
528
            if (strcasecmp(dep->name, r->name) == 0) {
2505
64
              tmp = *b1;
2506
64
              *b1 = *b2;
2507
64
              *b2 = tmp;
2508
64
              goto try_again;
2509
64
            }
2510
464
            b2++;
2511
464
          }
2512
192
        }
2513
128
        dep++;
2514
128
      }
2515
128
    }
2516
208
    b1++;
2517
208
  }
2518
16
}
2519
/* }}} */
2520
2521
ZEND_API void zend_collect_module_handlers(void) /* {{{ */
2522
16
{
2523
16
  zend_module_entry *module;
2524
16
  int startup_count = 0;
2525
16
  int shutdown_count = 0;
2526
16
  int post_deactivate_count = 0;
2527
16
  int dl_loaded_count = 0;
2528
16
  zend_class_entry *ce;
2529
16
  int class_count = 0;
2530
2531
  /* Collect extensions with request startup/shutdown handlers */
2532
448
  ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
2533
448
    if (module->request_startup_func) {
2534
128
      startup_count++;
2535
128
    }
2536
448
    if (module->request_shutdown_func) {
2537
64
      shutdown_count++;
2538
64
    }
2539
448
    if (module->post_deactivate_func) {
2540
48
      post_deactivate_count++;
2541
48
    }
2542
448
    if (module->handle) {
2543
0
      dl_loaded_count++;
2544
0
    }
2545
448
  } ZEND_HASH_FOREACH_END();
2546
16
  module_request_startup_handlers = (zend_module_entry**)perealloc(
2547
16
    module_request_startup_handlers,
2548
16
      sizeof(zend_module_entry*) *
2549
16
    (startup_count + 1 +
2550
16
     shutdown_count + 1 +
2551
16
     post_deactivate_count + 1), true);
2552
16
  module_request_startup_handlers[startup_count] = NULL;
2553
16
  module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
2554
16
  module_request_shutdown_handlers[shutdown_count] = NULL;
2555
16
  module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
2556
16
  module_post_deactivate_handlers[post_deactivate_count] = NULL;
2557
  /* Cannot reuse module_request_startup_handlers because it is freed in zend_destroy_modules, which happens before zend_unload_modules. */
2558
16
  modules_dl_loaded = perealloc(modules_dl_loaded, sizeof(zend_module_entry*) * (dl_loaded_count + 1), true);
2559
16
  modules_dl_loaded[dl_loaded_count] = NULL;
2560
16
  startup_count = 0;
2561
2562
448
  ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
2563
448
    if (module->request_startup_func) {
2564
128
      module_request_startup_handlers[startup_count++] = module;
2565
128
    }
2566
448
    if (module->request_shutdown_func) {
2567
64
      module_request_shutdown_handlers[--shutdown_count] = module;
2568
64
    }
2569
448
    if (module->post_deactivate_func) {
2570
48
      module_post_deactivate_handlers[--post_deactivate_count] = module;
2571
48
    }
2572
448
    if (module->handle) {
2573
0
      modules_dl_loaded[--dl_loaded_count] = module;
2574
0
    }
2575
448
  } ZEND_HASH_FOREACH_END();
2576
2577
  /* Collect internal classes with static members */
2578
6.17k
  ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
2579
6.17k
    if (ce->type == ZEND_INTERNAL_CLASS &&
2580
3.07k
        ce->default_static_members_count > 0) {
2581
0
        class_count++;
2582
0
    }
2583
6.17k
  } ZEND_HASH_FOREACH_END();
2584
2585
16
  class_cleanup_handlers = (zend_class_entry**)perealloc(
2586
16
    class_cleanup_handlers,
2587
16
    sizeof(zend_class_entry*) *
2588
16
    (class_count + 1), true);
2589
16
  class_cleanup_handlers[class_count] = NULL;
2590
2591
16
  if (class_count) {
2592
0
    ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
2593
0
      if (ce->type == ZEND_INTERNAL_CLASS &&
2594
0
          ce->default_static_members_count > 0) {
2595
0
          class_cleanup_handlers[--class_count] = ce;
2596
0
      }
2597
0
    } ZEND_HASH_FOREACH_END();
2598
0
  }
2599
16
}
2600
/* }}} */
2601
2602
ZEND_API void zend_startup_modules(void) /* {{{ */
2603
16
{
2604
16
  zend_hash_sort_ex(&module_registry, zend_sort_modules, NULL, 0);
2605
16
  zend_hash_apply(&module_registry, zend_startup_module_zval);
2606
16
}
2607
/* }}} */
2608
2609
ZEND_API void zend_destroy_modules(void) /* {{{ */
2610
0
{
2611
0
  free(class_cleanup_handlers);
2612
0
  class_cleanup_handlers = NULL;
2613
0
  free(module_request_startup_handlers);
2614
0
  module_request_startup_handlers = NULL;
2615
0
  zend_hash_graceful_reverse_destroy(&module_registry);
2616
0
}
2617
/* }}} */
2618
2619
ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module, int module_type) /* {{{ */
2620
208
{
2621
208
  size_t name_len;
2622
208
  zend_string *lcname;
2623
208
  zend_module_entry *module_ptr;
2624
2625
208
  if (!module) {
2626
0
    return NULL;
2627
0
  }
2628
2629
#if 0
2630
  zend_printf("%s: Registering module %d\n", module->name, module->module_number);
2631
#endif
2632
2633
  /* Check module dependencies */
2634
208
  if (module->deps) {
2635
64
    const zend_module_dep *dep = module->deps;
2636
2637
176
    while (dep->name) {
2638
112
      if (dep->type == MODULE_DEP_CONFLICTS) {
2639
0
        name_len = strlen(dep->name);
2640
0
        lcname = zend_string_alloc(name_len, 0);
2641
0
        zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
2642
2643
0
        if (zend_hash_exists(&module_registry, lcname) || zend_get_extension(dep->name)) {
2644
0
          zend_string_efree(lcname);
2645
          /* TODO: Check version relationship */
2646
0
          zend_error(E_CORE_WARNING, "Cannot load module \"%s\" because conflicting module \"%s\" is already loaded", module->name, dep->name);
2647
0
          return NULL;
2648
0
        }
2649
0
        zend_string_efree(lcname);
2650
0
      }
2651
112
      ++dep;
2652
112
    }
2653
64
  }
2654
2655
208
  name_len = strlen(module->name);
2656
208
  lcname = zend_string_alloc(name_len, module_type == MODULE_PERSISTENT);
2657
208
  zend_str_tolower_copy(ZSTR_VAL(lcname), module->name, name_len);
2658
2659
208
  int module_number = zend_next_free_module();
2660
2661
208
  lcname = zend_new_interned_string(lcname);
2662
208
  if ((module_ptr = zend_hash_add_ptr(&module_registry, lcname, module)) == NULL) {
2663
0
    zend_error(E_CORE_WARNING, "Module \"%s\" is already loaded", module->name);
2664
0
    zend_string_release(lcname);
2665
0
    return NULL;
2666
0
  }
2667
208
  module = module_ptr;
2668
208
  EG(current_module) = module;
2669
2670
208
  module->module_number = module_number;
2671
208
  module->type = module_type;
2672
2673
208
  if (module->functions && zend_register_functions(NULL, module->functions, NULL, module_type)==FAILURE) {
2674
0
    zend_hash_del(&module_registry, lcname);
2675
0
    zend_string_release(lcname);
2676
0
    EG(current_module) = NULL;
2677
0
    zend_error(E_CORE_WARNING,"%s: Unable to register functions, unable to load", module->name);
2678
0
    return NULL;
2679
0
  }
2680
2681
208
  EG(current_module) = NULL;
2682
208
  zend_string_release(lcname);
2683
208
  return module;
2684
208
}
2685
/* }}} */
2686
2687
ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module) /* {{{ */
2688
192
{
2689
192
  return zend_register_module_ex(module, MODULE_PERSISTENT);
2690
192
}
2691
/* }}} */
2692
2693
static void zend_check_magic_method_args(
2694
    uint32_t num_args, const zend_class_entry *ce, const zend_function *fptr, int error_type)
2695
15.5k
{
2696
15.5k
  if (fptr->common.num_args != num_args) {
2697
51
    if (num_args == 0) {
2698
35
      zend_error(error_type, "Method %s::%s() cannot take arguments",
2699
35
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2700
35
    } else if (num_args == 1) {
2701
5
      zend_error(error_type, "Method %s::%s() must take exactly 1 argument",
2702
5
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2703
11
    } else {
2704
11
      zend_error(error_type, "Method %s::%s() must take exactly %" PRIu32 " arguments",
2705
11
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name), num_args);
2706
11
    }
2707
51
    return;
2708
51
  }
2709
21.9k
  for (uint32_t i = 0; i < num_args; i++) {
2710
6.54k
    if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, i + 1)) {
2711
5
      zend_error(error_type, "Method %s::%s() cannot take arguments by reference",
2712
5
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2713
5
      return;
2714
5
    }
2715
6.54k
  }
2716
15.4k
}
2717
2718
static void zend_check_magic_method_arg_type(uint32_t arg_num, const zend_class_entry *ce, const zend_function *fptr, int error_type, int arg_type)
2719
5.75k
{
2720
5.75k
    if (
2721
5.75k
      ZEND_TYPE_IS_SET(fptr->common.arg_info[arg_num].type)
2722
1.59k
       && !(ZEND_TYPE_FULL_MASK(fptr->common.arg_info[arg_num].type) & arg_type)
2723
5.75k
    ) {
2724
49
      zend_error(error_type, "%s::%s(): Parameter #%d ($%s) must be of type %s when declared",
2725
49
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2726
49
        arg_num + 1, ZSTR_VAL(fptr->common.arg_info[arg_num].name),
2727
49
        ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(arg_type))));
2728
49
    }
2729
5.75k
}
2730
2731
static void zend_check_magic_method_return_type(const zend_class_entry *ce, const zend_function *fptr, int error_type, int return_type)
2732
10.6k
{
2733
10.6k
  if (return_type == MAY_BE_VOID) {
2734
2.02k
    if (fptr->common.fn_flags & ZEND_ACC_NODISCARD) {
2735
5
      zend_error_noreturn(error_type, "Method %s::%s cannot be #[\\NoDiscard]", ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2736
5
    }
2737
2.02k
  }
2738
2739
10.6k
  if (!(fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
2740
    /* For backwards compatibility reasons, do not enforce the return type if it is not set. */
2741
2.14k
    return;
2742
2.14k
  }
2743
2744
8.52k
  if (ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & MAY_BE_NEVER) {
2745
    /* It is always legal to specify the never type. */
2746
60
    return;
2747
60
  }
2748
2749
8.52k
  bool is_complex_type = ZEND_TYPE_IS_COMPLEX(fptr->common.arg_info[-1].type);
2750
8.46k
  uint32_t extra_types = ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & ~return_type;
2751
8.46k
  if (extra_types & MAY_BE_STATIC) {
2752
36
    extra_types &= ~MAY_BE_STATIC;
2753
36
    is_complex_type = true;
2754
36
  }
2755
2756
8.46k
  if (extra_types || (is_complex_type && return_type != MAY_BE_OBJECT)) {
2757
68
    zend_error(error_type, "%s::%s(): Return type must be %s when declared",
2758
68
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2759
68
      ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(return_type))));
2760
68
  }
2761
8.46k
}
2762
2763
static void zend_check_magic_method_non_static(
2764
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2765
21.2k
{
2766
21.2k
  if (fptr->common.fn_flags & ZEND_ACC_STATIC) {
2767
35
    zend_error(error_type, "Method %s::%s() cannot be static",
2768
35
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2769
35
  }
2770
21.2k
}
2771
2772
static void zend_check_magic_method_static(
2773
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2774
731
{
2775
731
  if (!(fptr->common.fn_flags & ZEND_ACC_STATIC)) {
2776
10
    zend_error(error_type, "Method %s::%s() must be static",
2777
10
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2778
10
  }
2779
731
}
2780
2781
static void zend_check_magic_method_public(
2782
    const zend_class_entry *ce, const zend_function *fptr)
2783
12.9k
{
2784
  // TODO: Remove this warning after adding proper visibility handling.
2785
12.9k
  if (!(fptr->common.fn_flags & ZEND_ACC_PUBLIC)) {
2786
5.66k
    zend_error(E_WARNING, "The magic method %s::%s() must have public visibility",
2787
5.66k
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2788
5.66k
  }
2789
12.9k
}
2790
2791
static void zend_check_magic_method_no_return_type(
2792
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2793
8.47k
{
2794
8.47k
  if (fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
2795
10
    zend_error_noreturn(error_type, "Method %s::%s() cannot declare a return type",
2796
10
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2797
10
  }
2798
2799
8.46k
  if (fptr->common.fn_flags & ZEND_ACC_NODISCARD) {
2800
5
    zend_error_noreturn(error_type, "Method %s::%s cannot be #[\\NoDiscard]", ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2801
5
  }
2802
8.46k
}
2803
2804
ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, const zend_string *lcname, int error_type) /* {{{ */
2805
162k
{
2806
162k
  if (ZSTR_VAL(lcname)[0] != '_'
2807
132k
   || ZSTR_VAL(lcname)[1] != '_') {
2808
132k
    return;
2809
132k
  }
2810
2811
29.6k
  if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2812
6.24k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2813
6.24k
    zend_check_magic_method_no_return_type(ce, fptr, error_type);
2814
23.3k
  } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2815
2.25k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2816
2.25k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2817
2.25k
    zend_check_magic_method_no_return_type(ce, fptr, error_type);
2818
21.1k
  } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2819
447
    zend_check_magic_method_args(0, ce, fptr, error_type);
2820
447
    zend_check_magic_method_non_static(ce, fptr, error_type);
2821
447
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2822
20.6k
  } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2823
1.19k
    zend_check_magic_method_args(1, ce, fptr, error_type);
2824
1.19k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2825
1.19k
    zend_check_magic_method_public(ce, fptr);
2826
1.19k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2827
19.4k
  } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2828
748
    zend_check_magic_method_args(2, ce, fptr, error_type);
2829
748
    zend_check_magic_method_non_static(ce, fptr, error_type);
2830
748
    zend_check_magic_method_public(ce, fptr);
2831
748
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2832
748
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2833
18.7k
  } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
2834
238
    zend_check_magic_method_args(1, ce, fptr, error_type);
2835
238
    zend_check_magic_method_non_static(ce, fptr, error_type);
2836
238
    zend_check_magic_method_public(ce, fptr);
2837
238
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2838
238
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2839
18.5k
  } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2840
447
    zend_check_magic_method_args(1, ce, fptr, error_type);
2841
447
    zend_check_magic_method_non_static(ce, fptr, error_type);
2842
447
    zend_check_magic_method_public(ce, fptr);
2843
447
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2844
447
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_BOOL);
2845
18.0k
  } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2846
733
    zend_check_magic_method_args(2, ce, fptr, error_type);
2847
733
    zend_check_magic_method_non_static(ce, fptr, error_type);
2848
733
    zend_check_magic_method_public(ce, fptr);
2849
733
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2850
733
    zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2851
17.3k
  } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_LCNAME)) {
2852
558
    zend_check_magic_method_args(2, ce, fptr, error_type);
2853
558
    zend_check_magic_method_static(ce, fptr, error_type);
2854
558
    zend_check_magic_method_public(ce, fptr);
2855
558
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2856
558
    zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2857
16.7k
  } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_LCNAME)) {
2858
6.43k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2859
6.43k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2860
6.43k
    zend_check_magic_method_public(ce, fptr);
2861
6.43k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_STRING);
2862
10.3k
  } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_LCNAME)) {
2863
487
    zend_check_magic_method_args(0, ce, fptr, error_type);
2864
487
    zend_check_magic_method_non_static(ce, fptr, error_type);
2865
487
    zend_check_magic_method_public(ce, fptr);
2866
487
    zend_check_magic_method_return_type(ce, fptr, error_type, (MAY_BE_ARRAY | MAY_BE_NULL));
2867
487
    if ((fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) && ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & MAY_BE_NULL) {
2868
78
      zend_error(E_DEPRECATED, "Returning null from %s::__debugInfo() is deprecated, make the return type non-nullable and return an empty array instead",
2869
78
        ZSTR_VAL(ce->name));
2870
78
    }
2871
9.84k
  } else if (zend_string_equals_literal(lcname, ZEND_SERIALIZE_FUNC_NAME)) {
2872
879
    zend_check_magic_method_args(0, ce, fptr, error_type);
2873
879
    zend_check_magic_method_non_static(ce, fptr, error_type);
2874
879
    zend_check_magic_method_public(ce, fptr);
2875
879
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2876
8.96k
  } else if (zend_string_equals_literal(lcname, ZEND_UNSERIALIZE_FUNC_NAME)) {
2877
438
    zend_check_magic_method_args(1, ce, fptr, error_type);
2878
438
    zend_check_magic_method_non_static(ce, fptr, error_type);
2879
438
    zend_check_magic_method_public(ce, fptr);
2880
438
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2881
438
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2882
8.53k
  } else if (zend_string_equals_literal(lcname, ZEND_SET_STATE_FUNC_NAME)) {
2883
174
    zend_check_magic_method_args(1, ce, fptr, error_type);
2884
174
    zend_check_magic_method_static(ce, fptr, error_type);
2885
174
    zend_check_magic_method_public(ce, fptr);
2886
174
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2887
174
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_OBJECT);
2888
8.35k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) {
2889
246
    zend_check_magic_method_non_static(ce, fptr, error_type);
2890
246
    zend_check_magic_method_public(ce, fptr);
2891
8.11k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_SLEEP))) {
2892
274
    zend_check_magic_method_args(0, ce, fptr, error_type);
2893
274
    zend_check_magic_method_non_static(ce, fptr, error_type);
2894
274
    zend_check_magic_method_public(ce, fptr);
2895
274
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2896
7.83k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_WAKEUP))) {
2897
203
    zend_check_magic_method_args(0, ce, fptr, error_type);
2898
203
    zend_check_magic_method_non_static(ce, fptr, error_type);
2899
203
    zend_check_magic_method_public(ce, fptr);
2900
203
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2901
203
  }
2902
29.6k
}
2903
/* }}} */
2904
2905
ZEND_API void zend_add_magic_method(zend_class_entry *ce, zend_function *fptr, const zend_string *lcname)
2906
164k
{
2907
164k
  if (ZSTR_VAL(lcname)[0] != '_' || ZSTR_VAL(lcname)[1] != '_') {
2908
    /* pass */
2909
134k
  } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2910
484
    ce->clone = fptr;
2911
29.4k
  } else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2912
6.35k
    ce->constructor = fptr;
2913
6.35k
    ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
2914
23.1k
  } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2915
2.26k
    ce->destructor = fptr;
2916
20.8k
  } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2917
1.22k
    ce->__get = fptr;
2918
1.22k
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2919
19.6k
  } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2920
757
    ce->__set = fptr;
2921
757
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2922
18.8k
  } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2923
739
    ce->__call = fptr;
2924
18.1k
  } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
2925
238
    ce->__unset = fptr;
2926
238
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2927
17.8k
  } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2928
463
    ce->__isset = fptr;
2929
463
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2930
17.4k
  } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_LCNAME)) {
2931
575
    ce->__callstatic = fptr;
2932
16.8k
  } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_LCNAME)) {
2933
6.45k
    ce->__tostring = fptr;
2934
10.3k
  } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_LCNAME)) {
2935
487
    ce->__debugInfo = fptr;
2936
487
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2937
9.91k
  } else if (zend_string_equals_literal(lcname, ZEND_SERIALIZE_FUNC_NAME)) {
2938
879
    ce->__serialize = fptr;
2939
9.03k
  } else if (zend_string_equals_literal(lcname, ZEND_UNSERIALIZE_FUNC_NAME)) {
2940
438
    ce->__unserialize = fptr;
2941
438
  }
2942
164k
}
2943
2944
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arg_info_toString, 0, 0, IS_STRING, 0)
2945
ZEND_END_ARG_INFO()
2946
2947
1.31k
static zend_always_inline void zend_normalize_internal_type(zend_type *type) {
2948
1.31k
  ZEND_ASSERT(!ZEND_TYPE_HAS_LITERAL_NAME(*type));
2949
1.31k
  if (ZEND_TYPE_PURE_MASK(*type) != MAY_BE_ANY) {
2950
1.26k
    ZEND_ASSERT(!ZEND_TYPE_CONTAINS_CODE(*type, IS_RESOURCE) && "resource is not allowed in a zend_type");
2951
1.26k
  }
2952
1.31k
  zend_type *current;
2953
2.62k
  ZEND_TYPE_FOREACH_MUTABLE(*type, current) {
2954
2.62k
    if (ZEND_TYPE_HAS_NAME(*current)) {
2955
144
      zend_string *name = zend_new_interned_string(ZEND_TYPE_NAME(*current));
2956
144
      zend_alloc_ce_cache(name);
2957
144
      ZEND_TYPE_SET_PTR(*current, name);
2958
1.16k
    } else if (ZEND_TYPE_HAS_LIST(*current)) {
2959
0
      zend_type *inner;
2960
0
      ZEND_TYPE_FOREACH_MUTABLE(*current, inner) {
2961
0
        ZEND_ASSERT(!ZEND_TYPE_HAS_LITERAL_NAME(*inner) && !ZEND_TYPE_HAS_LIST(*inner));
2962
0
        if (ZEND_TYPE_HAS_NAME(*inner)) {
2963
0
          zend_string *name = zend_new_interned_string(ZEND_TYPE_NAME(*inner));
2964
0
          zend_alloc_ce_cache(name);
2965
0
          ZEND_TYPE_SET_PTR(*inner, name);
2966
0
        }
2967
0
      } ZEND_TYPE_FOREACH_END();
2968
0
    }
2969
2.62k
  } ZEND_TYPE_FOREACH_END();
2970
1.31k
}
2971
2972
static void zend_convert_internal_arg_info_type(zend_type *type, bool persistent)
2973
56.5k
{
2974
56.5k
  if (ZEND_TYPE_HAS_LITERAL_NAME(*type)) {
2975
    // gen_stubs.php does not support codegen for compound types. As a
2976
    // temporary workaround, we support union types by splitting
2977
    // the type name on `|` characters if necessary.
2978
3.88k
    const char *class_name = ZEND_TYPE_LITERAL_NAME(*type);
2979
3.88k
    type->type_mask &= ~_ZEND_TYPE_LITERAL_NAME_BIT;
2980
2981
3.88k
    size_t num_types = 1;
2982
3.88k
    const char *p = class_name;
2983
3.92k
    while ((p = strchr(p, '|'))) {
2984
32
      num_types++;
2985
32
      p++;
2986
32
    }
2987
2988
3.88k
    if (num_types == 1) {
2989
      /* Simple class type */
2990
3.85k
      zend_string *str = zend_string_init_interned(class_name, strlen(class_name), persistent);
2991
3.85k
      zend_alloc_ce_cache(str);
2992
3.85k
      ZEND_TYPE_SET_PTR(*type, str);
2993
3.85k
      type->type_mask |= _ZEND_TYPE_NAME_BIT;
2994
3.85k
    } else {
2995
      /* Union type */
2996
32
      zend_type_list *list = pemalloc(ZEND_TYPE_LIST_SIZE(num_types), persistent);
2997
32
      list->num_types = num_types;
2998
32
      ZEND_TYPE_SET_LIST(*type, list);
2999
32
      ZEND_TYPE_FULL_MASK(*type) |= _ZEND_TYPE_UNION_BIT;
3000
3001
32
      const char *start = class_name;
3002
32
      uint32_t j = 0;
3003
64
      while (true) {
3004
64
        const char *end = strchr(start, '|');
3005
64
        zend_string *str = zend_string_init_interned(start, end ? end - start : strlen(start), persistent);
3006
64
        zend_alloc_ce_cache(str);
3007
64
        list->types[j] = (zend_type) ZEND_TYPE_INIT_CLASS(str, 0, 0);
3008
64
        if (!end) {
3009
32
          break;
3010
32
        }
3011
32
        start = end + 1;
3012
32
        j++;
3013
32
      }
3014
32
    }
3015
3.88k
  }
3016
56.5k
  if (ZEND_TYPE_IS_ITERABLE_FALLBACK(*type)) {
3017
    /* Warning generated an extension load warning which is emitted for every test
3018
       zend_error(E_CORE_WARNING, "iterable type is now a compile time alias for array|Traversable,"
3019
       " regenerate the argument info via the php-src gen_stub build script");
3020
       */
3021
0
    zend_type legacy_iterable = ZEND_TYPE_INIT_CLASS_MASK(
3022
0
      ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
3023
0
      (type->type_mask | MAY_BE_ARRAY)
3024
0
    );
3025
0
    *type = legacy_iterable;
3026
0
  }
3027
56.5k
}
3028
3029
ZEND_API void zend_convert_internal_arg_info(zend_arg_info *new_arg_info, const zend_internal_arg_info *arg_info, bool is_return_info, bool persistent)
3030
56.5k
{
3031
56.5k
  if (!is_return_info) {
3032
28.9k
    new_arg_info->name = zend_string_init_interned(arg_info->name, strlen(arg_info->name), persistent);
3033
28.9k
    if (arg_info->default_value) {
3034
9.36k
      new_arg_info->default_value = zend_string_init_interned(arg_info->default_value, strlen(arg_info->default_value), persistent);
3035
19.5k
    } else {
3036
19.5k
      new_arg_info->default_value = NULL;
3037
19.5k
    }
3038
28.9k
  } else {
3039
27.6k
    new_arg_info->name = NULL;
3040
27.6k
    new_arg_info->default_value = NULL;
3041
27.6k
  }
3042
56.5k
  new_arg_info->doc_comment = NULL;
3043
56.5k
  new_arg_info->type = arg_info->type;
3044
56.5k
  zend_convert_internal_arg_info_type(&new_arg_info->type, persistent);
3045
56.5k
}
3046
3047
/* registers all functions in *library_functions in the function hash */
3048
ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type) /* {{{ */
3049
2.25k
{
3050
2.25k
  const zend_function_entry *ptr = functions;
3051
2.25k
  zend_function function;
3052
2.25k
  zend_internal_function *reg_function, *internal_function = (zend_internal_function *)&function;
3053
2.25k
  int count=0, unload=0;
3054
2.25k
  HashTable *target_function_table = function_table;
3055
2.25k
  int error_type;
3056
2.25k
  zend_string *lowercase_name;
3057
2.25k
  size_t fname_len;
3058
2.25k
  const zend_internal_arg_info *internal_arg_info;
3059
3060
2.25k
  if (type==MODULE_PERSISTENT) {
3061
2.25k
    error_type = E_CORE_WARNING;
3062
2.25k
  } else {
3063
0
    error_type = E_WARNING;
3064
0
  }
3065
3066
2.25k
  if (!target_function_table) {
3067
160
    target_function_table = CG(function_table);
3068
160
  }
3069
2.25k
  internal_function->type = ZEND_INTERNAL_FUNCTION;
3070
2.25k
  internal_function->module = EG(current_module);
3071
2.25k
  if (EG(active) && ZEND_OBSERVER_ENABLED) {
3072
    /* Add an observer temporary to store previous observed frames. This is
3073
     * normally handled by zend_observer_post_startup(), except for
3074
     * functions registered at runtime (EG(active)). */
3075
0
    internal_function->T = 1;
3076
2.25k
  } else {
3077
2.25k
    internal_function->T = 0;
3078
2.25k
  }
3079
2.25k
  memset(internal_function->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
3080
3081
29.8k
  while (ptr->fname) {
3082
27.5k
    fname_len = strlen(ptr->fname);
3083
27.5k
    internal_function->handler = ptr->handler;
3084
27.5k
    internal_function->doc_comment = ptr->doc_comment ? zend_string_init_interned(ptr->doc_comment, strlen(ptr->doc_comment), 1) : NULL;
3085
27.5k
    internal_function->function_name = zend_string_init_interned(ptr->fname, fname_len, 1);
3086
27.5k
    internal_function->scope = scope;
3087
27.5k
    internal_function->prototype = NULL;
3088
27.5k
    internal_function->prop_info = NULL;
3089
27.5k
    internal_function->attributes = NULL;
3090
27.5k
    internal_function->frameless_function_infos = ptr->frameless_function_infos;
3091
27.5k
    if (EG(active)) { // at run-time: this ought to only happen if registered with dl() or somehow temporarily at runtime
3092
0
      ZEND_MAP_PTR_INIT(internal_function->run_time_cache, zend_arena_calloc(&CG(arena), 1, zend_internal_run_time_cache_reserved_size()));
3093
27.5k
    } else {
3094
#ifdef ZTS
3095
      ZEND_MAP_PTR_NEW_STATIC(internal_function->run_time_cache);
3096
#else
3097
27.5k
      ZEND_MAP_PTR_INIT(internal_function->run_time_cache, NULL);
3098
27.5k
#endif
3099
27.5k
    }
3100
27.5k
    if (ptr->flags & UINT32_MAX) {
3101
18.9k
      if (!(ptr->flags & ZEND_ACC_PPP_MASK)) {
3102
2.96k
        if (ptr->flags != ZEND_ACC_DEPRECATED && scope) {
3103
0
          zend_error(error_type, "Invalid access level for %s::%s() - access must be exactly one of public, protected or private", ZSTR_VAL(scope->name), ptr->fname);
3104
0
        }
3105
2.96k
        internal_function->fn_flags = ZEND_ACC_PUBLIC | ptr->flags;
3106
16.0k
      } else {
3107
16.0k
        internal_function->fn_flags = ptr->flags;
3108
16.0k
      }
3109
18.9k
    } else {
3110
8.60k
      internal_function->fn_flags = ZEND_ACC_PUBLIC;
3111
8.60k
    }
3112
27.5k
    internal_function->fn_flags2 = ptr->flags >> 32;
3113
3114
27.5k
    if (ptr->arg_info) {
3115
27.5k
      zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info;
3116
27.5k
      internal_arg_info = ptr->arg_info+1;
3117
27.5k
      internal_function->num_args = ptr->num_args;
3118
      /* Currently you cannot denote that the function can accept less arguments than num_args */
3119
27.5k
      if (info->required_num_args == (uintptr_t)-1) {
3120
0
        internal_function->required_num_args = ptr->num_args;
3121
27.5k
      } else {
3122
27.5k
        internal_function->required_num_args = info->required_num_args;
3123
27.5k
      }
3124
27.5k
      if (ZEND_ARG_SEND_MODE(info)) {
3125
0
        internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE;
3126
0
      }
3127
27.5k
      if (ZEND_ARG_IS_VARIADIC(&ptr->arg_info[ptr->num_args])) {
3128
736
        internal_function->fn_flags |= ZEND_ACC_VARIADIC;
3129
        /* Don't count the variadic argument */
3130
736
        internal_function->num_args--;
3131
736
      }
3132
27.5k
      if (ZEND_TYPE_IS_SET(info->type)) {
3133
25.9k
        if (ZEND_TYPE_HAS_NAME(info->type)) {
3134
0
          const char *type_name = ZEND_TYPE_LITERAL_NAME(info->type);
3135
0
          if (!scope && (!strcasecmp(type_name, "self") || !strcasecmp(type_name, "parent"))) {
3136
0
            zend_error_noreturn(E_CORE_ERROR, "Cannot declare a return type of %s outside of a class scope", type_name);
3137
0
          }
3138
0
        }
3139
3140
25.9k
        internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
3141
25.9k
      }
3142
27.5k
    } else {
3143
0
      zend_error(E_CORE_WARNING, "Missing arginfo for %s%s%s()",
3144
0
         scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3145
3146
0
      internal_arg_info = NULL;
3147
0
      internal_function->num_args = 0;
3148
0
      internal_function->required_num_args = 0;
3149
0
    }
3150
3151
    /* If not specified, add __toString() return type for compatibility with Stringable
3152
     * interface. */
3153
27.5k
    if (scope && zend_string_equals_literal_ci(internal_function->function_name, ZEND_TOSTRING_FUNC_LCNAME) &&
3154
288
        !(internal_function->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
3155
0
      zend_error(E_CORE_WARNING, "%s::__toString() implemented without string return type",
3156
0
        ZSTR_VAL(scope->name));
3157
0
      internal_arg_info = (zend_internal_arg_info *) arg_info_toString + 1;
3158
0
      internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
3159
0
      internal_function->num_args = internal_function->required_num_args = 0;
3160
0
    }
3161
3162
27.5k
    if (ptr->flags & ZEND_ACC_ABSTRACT) {
3163
720
      if (scope) {
3164
        /* This is a class that must be abstract itself. Here we set the check info. */
3165
720
        scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
3166
720
        if (!(scope->ce_flags & ZEND_ACC_INTERFACE)) {
3167
          /* Since the class is not an interface it needs to be declared as a abstract class. */
3168
          /* Since here we are handling internal functions only we can add the keyword flag. */
3169
          /* This time we set the flag for the keyword 'abstract'. */
3170
32
          scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
3171
32
        }
3172
720
      }
3173
720
      if ((ptr->flags & ZEND_ACC_STATIC) && (!scope || !(scope->ce_flags & ZEND_ACC_INTERFACE))) {
3174
0
        zend_error(error_type, "Static function %s%s%s() cannot be abstract", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3175
0
      }
3176
26.8k
    } else {
3177
26.8k
      if (scope && (scope->ce_flags & ZEND_ACC_INTERFACE)) {
3178
0
        zend_error(error_type, "Interface %s cannot contain non abstract method %s()", ZSTR_VAL(scope->name), ptr->fname);
3179
0
        return FAILURE;
3180
0
      }
3181
26.8k
      if (!internal_function->handler) {
3182
0
        zend_error(error_type, "Method %s%s%s() cannot be a NULL function", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3183
0
        zend_unregister_functions(functions, count, target_function_table);
3184
0
        return FAILURE;
3185
0
      }
3186
26.8k
    }
3187
27.5k
    lowercase_name = zend_string_tolower_ex(internal_function->function_name, type == MODULE_PERSISTENT);
3188
27.5k
    lowercase_name = zend_new_interned_string(lowercase_name);
3189
27.5k
    reg_function = pemalloc(sizeof(zend_internal_function), true);
3190
27.5k
    memcpy(reg_function, &function, sizeof(zend_internal_function));
3191
27.5k
    if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) {
3192
0
      unload=1;
3193
0
      free(reg_function);
3194
0
      zend_string_release(lowercase_name);
3195
0
      break;
3196
0
    }
3197
27.5k
    if (reg_function->frameless_function_infos) {
3198
320
      const zend_frameless_function_info *flf_info = reg_function->frameless_function_infos;
3199
784
      while (flf_info->handler) {
3200
464
        if (zend_flf_count == zend_flf_capacity) {
3201
48
          if (!zend_flf_capacity) {
3202
16
            zend_flf_capacity = 8;
3203
32
          } else {
3204
32
            zend_flf_capacity *= 2;
3205
32
          }
3206
          /* +1 for NULL terminator */
3207
48
          zend_flf_handlers = perealloc(zend_flf_handlers, (zend_flf_capacity + 1) * sizeof(void *), true);
3208
48
          zend_flf_functions = perealloc(zend_flf_functions, (zend_flf_capacity + 1) * sizeof(zend_function *), true);
3209
48
        }
3210
464
        zend_flf_handlers[zend_flf_count] = flf_info->handler;
3211
464
        zend_flf_functions[zend_flf_count] = (zend_function *)reg_function;
3212
464
        zend_flf_count++;
3213
464
        flf_info++;
3214
464
      }
3215
320
      zend_flf_handlers[zend_flf_count] = NULL;
3216
320
      zend_flf_functions[zend_flf_count] = NULL;
3217
320
    }
3218
3219
    /* Get parameter count including variadic parameter. */
3220
27.5k
    uint32_t num_args = reg_function->num_args;
3221
27.5k
    if (reg_function->fn_flags & ZEND_ACC_VARIADIC) {
3222
736
      num_args++;
3223
736
    }
3224
3225
    /* If types of arguments have to be checked */
3226
27.5k
    if (internal_arg_info && num_args) {
3227
15.9k
      uint32_t i;
3228
44.8k
      for (i = 0; i < num_args; i++) {
3229
28.8k
        const zend_internal_arg_info *arg_info = &internal_arg_info[i];
3230
28.8k
        ZEND_ASSERT(arg_info->name && "Parameter must have a name");
3231
28.8k
        if (ZEND_TYPE_IS_SET(arg_info->type)) {
3232
25.8k
            reg_function->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
3233
25.8k
        }
3234
28.8k
#if ZEND_DEBUG
3235
50.0k
        for (uint32_t j = 0; j < i; j++) {
3236
21.1k
          if (!strcmp(arg_info->name, internal_arg_info[j].name)) {
3237
0
            zend_error_noreturn(E_CORE_ERROR,
3238
0
              "Duplicate parameter name $%s for function %s%s%s()", arg_info->name,
3239
0
              scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3240
0
          }
3241
21.1k
        }
3242
28.8k
#endif
3243
28.8k
      }
3244
15.9k
    }
3245
3246
    /* Convert zend_internal_arg_info to zend_arg_info */
3247
27.5k
    if (internal_arg_info) {
3248
27.5k
      uint32_t i;
3249
27.5k
      const zend_internal_arg_info *arg_info = internal_arg_info - 1;
3250
27.5k
      zend_arg_info *new_arg_info;
3251
3252
      /* Treat return type as an extra argument */
3253
27.5k
      num_args++;
3254
27.5k
      new_arg_info = pemalloc(sizeof(zend_arg_info) * num_args, true);
3255
27.5k
      reg_function->arg_info = new_arg_info + 1;
3256
84.0k
      for (i = 0; i < num_args; i++) {
3257
56.4k
        zend_convert_internal_arg_info(&new_arg_info[i], &arg_info[i],
3258
56.4k
            i == 0, true);
3259
56.4k
      }
3260
27.5k
    }
3261
3262
27.5k
    zend_set_function_arg_flags((zend_function*)reg_function);
3263
3264
27.5k
    if (scope) {
3265
16.0k
      zend_check_magic_method_implementation(
3266
16.0k
        scope, (zend_function *)reg_function, lowercase_name, E_CORE_ERROR);
3267
16.0k
      zend_add_magic_method(scope, (zend_function *)reg_function, lowercase_name);
3268
16.0k
    }
3269
27.5k
    ptr++;
3270
27.5k
    count++;
3271
27.5k
    zend_string_release(lowercase_name);
3272
27.5k
  }
3273
2.25k
  if (unload) { /* before unloading, display all remaining bad function in the module */
3274
0
    while (ptr->fname) {
3275
0
      fname_len = strlen(ptr->fname);
3276
0
      lowercase_name = zend_string_alloc(fname_len, 0);
3277
0
      zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
3278
0
      if (zend_hash_exists(target_function_table, lowercase_name)) {
3279
0
        zend_error(error_type, "Function registration failed - duplicate name - %s%s%s", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3280
0
      }
3281
0
      zend_string_efree(lowercase_name);
3282
0
      ptr++;
3283
0
    }
3284
0
    zend_unregister_functions(functions, count, target_function_table);
3285
0
    return FAILURE;
3286
0
  }
3287
2.25k
  return SUCCESS;
3288
2.25k
}
3289
/* }}} */
3290
3291
/* count=-1 means erase all functions, otherwise,
3292
 * erase the first count functions
3293
 */
3294
ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table) /* {{{ */
3295
0
{
3296
0
  const zend_function_entry *ptr = functions;
3297
0
  int i=0;
3298
0
  HashTable *target_function_table = function_table;
3299
0
  zend_string *lowercase_name;
3300
0
  size_t fname_len;
3301
3302
0
  if (!target_function_table) {
3303
0
    target_function_table = CG(function_table);
3304
0
  }
3305
0
  while (ptr->fname) {
3306
0
    if (count!=-1 && i>=count) {
3307
0
      break;
3308
0
    }
3309
0
    fname_len = strlen(ptr->fname);
3310
0
    lowercase_name = zend_string_alloc(fname_len, 0);
3311
0
    zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
3312
0
    zend_hash_del(target_function_table, lowercase_name);
3313
0
    zend_string_efree(lowercase_name);
3314
0
    ptr++;
3315
0
    i++;
3316
0
  }
3317
0
}
3318
/* }}} */
3319
3320
ZEND_API zend_result zend_startup_module(zend_module_entry *module) /* {{{ */
3321
0
{
3322
0
  if ((module = zend_register_internal_module(module)) != NULL && zend_startup_module_ex(module) == SUCCESS) {
3323
0
    return SUCCESS;
3324
0
  }
3325
0
  return FAILURE;
3326
0
}
3327
/* }}} */
3328
3329
ZEND_API zend_result zend_get_module_started(const char *module_name) /* {{{ */
3330
0
{
3331
0
  zend_module_entry *module;
3332
3333
0
  module = zend_hash_str_find_ptr(&module_registry, module_name, strlen(module_name));
3334
0
  return (module && module->module_started) ? SUCCESS : FAILURE;
3335
0
}
3336
/* }}} */
3337
3338
static void clean_module_classes(int module_number) /* {{{ */
3339
0
{
3340
  /* Child classes may reuse structures from parent classes, so destroy in reverse order. */
3341
0
  Bucket *bucket;
3342
0
  ZEND_HASH_REVERSE_FOREACH_BUCKET(EG(class_table), bucket) {
3343
0
    const zend_class_entry *ce = Z_CE(bucket->val);
3344
0
    if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->module_number == module_number) {
3345
0
      zend_hash_del_bucket(EG(class_table), bucket);
3346
0
    }
3347
0
  } ZEND_HASH_FOREACH_END();
3348
3349
0
}
3350
/* }}} */
3351
3352
static int clean_module_function(zval *el, void *arg) /* {{{ */
3353
0
{
3354
0
  const zend_function *fe = (zend_function *) Z_PTR_P(el);
3355
0
  const zend_module_entry *module = arg;
3356
0
  if (fe->common.type == ZEND_INTERNAL_FUNCTION && fe->internal_function.module == module) {
3357
0
    return ZEND_HASH_APPLY_REMOVE;
3358
0
  } else {
3359
0
    return ZEND_HASH_APPLY_KEEP;
3360
0
  }
3361
0
}
3362
/* }}} */
3363
3364
static void clean_module_functions(zend_module_entry *module) /* {{{ */
3365
0
{
3366
0
  zend_hash_apply_with_argument(CG(function_table), clean_module_function, module);
3367
0
}
3368
/* }}} */
3369
3370
void module_destructor(zend_module_entry *module) /* {{{ */
3371
0
{
3372
#if ZEND_RC_DEBUG
3373
  bool orig_rc_debug = zend_rc_debug;
3374
#endif
3375
3376
0
  if (module->type == MODULE_TEMPORARY) {
3377
#if ZEND_RC_DEBUG
3378
    /* FIXME: Loading extensions during the request breaks some invariants.
3379
     * In particular, it will create persistent interned strings, which is
3380
     * not allowed at this stage. */
3381
    zend_rc_debug = false;
3382
#endif
3383
0
    zend_clean_module_rsrc_dtors(module->module_number);
3384
0
    clean_module_constants(module->module_number);
3385
0
    clean_module_classes(module->module_number);
3386
0
  }
3387
3388
0
  if (module->module_started && module->module_shutdown_func) {
3389
#if 0
3390
    zend_printf("%s: Module shutdown\n", module->name);
3391
#endif
3392
0
    module->module_shutdown_func(module->type, module->module_number);
3393
0
  }
3394
3395
0
  if (module->module_started
3396
0
   && !module->module_shutdown_func
3397
0
   && module->type == MODULE_TEMPORARY) {
3398
0
    zend_unregister_ini_entries_ex(module->module_number, module->type);
3399
0
  }
3400
3401
  /* Deinitialize module globals */
3402
0
  if (module->globals_size) {
3403
#ifdef ZTS
3404
    if (*module->globals_id_ptr) {
3405
      ts_free_id(*module->globals_id_ptr);
3406
    }
3407
#else
3408
0
    if (module->globals_dtor) {
3409
0
      module->globals_dtor(module->globals_ptr);
3410
0
    }
3411
0
#endif
3412
0
  }
3413
3414
0
  module->module_started=0;
3415
0
  if (module->type == MODULE_TEMPORARY && module->functions) {
3416
0
    zend_unregister_functions(module->functions, -1, NULL);
3417
    /* Clean functions registered separately from module->functions */
3418
0
    clean_module_functions(module);
3419
0
  }
3420
3421
#if ZEND_RC_DEBUG
3422
  zend_rc_debug = orig_rc_debug;
3423
#endif
3424
0
}
3425
/* }}} */
3426
3427
void module_registry_unload(const zend_module_entry *module)
3428
0
{
3429
0
#ifdef HAVE_LIBDL
3430
0
  if (!getenv("ZEND_DONT_UNLOAD_MODULES")) {
3431
0
    DL_UNLOAD(module->handle);
3432
0
  }
3433
#else
3434
  ZEND_IGNORE_VALUE(module);
3435
#endif
3436
0
}
3437
3438
ZEND_API void zend_activate_modules(void) /* {{{ */
3439
300k
{
3440
300k
  zend_module_entry **p = module_request_startup_handlers;
3441
3442
2.70M
  while (*p) {
3443
2.40M
    const zend_module_entry *module = *p;
3444
3445
2.40M
    if (module->request_startup_func(module->type, module->module_number)==FAILURE) {
3446
0
      zend_error(E_WARNING, "request_startup() for %s module failed", module->name);
3447
0
      exit(1);
3448
0
    }
3449
2.40M
    p++;
3450
2.40M
  }
3451
300k
}
3452
/* }}} */
3453
3454
ZEND_API void zend_deactivate_modules(void) /* {{{ */
3455
300k
{
3456
300k
  EG(current_execute_data) = NULL; /* we're no longer executing anything */
3457
3458
300k
  if (EG(full_tables_cleanup)) {
3459
0
    zend_module_entry *module;
3460
3461
0
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) {
3462
0
      if (module->request_shutdown_func) {
3463
0
        zend_try {
3464
0
          module->request_shutdown_func(module->type, module->module_number);
3465
0
        } zend_end_try();
3466
0
      }
3467
0
    } ZEND_HASH_FOREACH_END();
3468
300k
  } else {
3469
300k
    zend_module_entry **p = module_request_shutdown_handlers;
3470
3471
1.50M
    while (*p) {
3472
1.20M
      const zend_module_entry *module = *p;
3473
1.20M
      zend_try {
3474
1.20M
        module->request_shutdown_func(module->type, module->module_number);
3475
1.20M
      } zend_end_try();
3476
1.20M
      p++;
3477
1.20M
    }
3478
300k
  }
3479
300k
}
3480
/* }}} */
3481
3482
void zend_unload_modules(void) /* {{{ */
3483
0
{
3484
0
  zend_module_entry **modules = modules_dl_loaded;
3485
0
  while (*modules) {
3486
0
    module_registry_unload(*modules);
3487
0
    modules++;
3488
0
  }
3489
0
  free(modules_dl_loaded);
3490
0
  modules_dl_loaded = NULL;
3491
0
}
3492
/* }}} */
3493
3494
ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
3495
300k
{
3496
300k
  if (EG(full_tables_cleanup)) {
3497
0
    zend_module_entry *module;
3498
0
    zval *zv;
3499
0
    zend_string *key;
3500
3501
0
    ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
3502
0
      if (module->post_deactivate_func) {
3503
0
        module->post_deactivate_func();
3504
0
      }
3505
0
    } ZEND_HASH_FOREACH_END();
3506
0
    ZEND_HASH_MAP_REVERSE_FOREACH_STR_KEY_VAL(&module_registry, key, zv) {
3507
0
      module = Z_PTR_P(zv);
3508
0
      if (module->type != MODULE_TEMPORARY) {
3509
0
        break;
3510
0
      }
3511
0
      module_destructor(module);
3512
0
      if (module->handle) {
3513
0
        module_registry_unload(module);
3514
0
      }
3515
0
      zend_string_release_ex(key, 0);
3516
0
    } ZEND_HASH_MAP_FOREACH_END_DEL();
3517
300k
  } else {
3518
300k
    zend_module_entry **p = module_post_deactivate_handlers;
3519
3520
1.20M
    while (*p) {
3521
902k
      const zend_module_entry *module = *p;
3522
3523
902k
      module->post_deactivate_func();
3524
902k
      p++;
3525
902k
    }
3526
300k
  }
3527
300k
}
3528
/* }}} */
3529
3530
/* return the next free module number */
3531
ZEND_API int zend_next_free_module(void) /* {{{ */
3532
208
{
3533
208
  return zend_hash_num_elements(&module_registry);
3534
208
}
3535
/* }}} */
3536
3537
static zend_class_entry *do_register_internal_class(const zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
3538
3.07k
{
3539
3.07k
  zend_class_entry *class_entry = pemalloc(sizeof(zend_class_entry), true);
3540
3.07k
  zend_string *lowercase_name;
3541
3.07k
  *class_entry = *orig_class_entry;
3542
3543
3.07k
  class_entry->type = ZEND_INTERNAL_CLASS;
3544
3.07k
  zend_initialize_class_data(class_entry, 0);
3545
3.07k
  zend_alloc_ce_cache(class_entry->name);
3546
3.07k
  class_entry->ce_flags = orig_class_entry->ce_flags | ce_flags | ZEND_ACC_CONSTANTS_UPDATED | ZEND_ACC_LINKED | ZEND_ACC_RESOLVED_PARENT | ZEND_ACC_RESOLVED_INTERFACES;
3547
3.07k
  class_entry->info.internal.module = EG(current_module);
3548
3549
3.07k
  if (class_entry->info.internal.builtin_functions) {
3550
1.87k
    zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, EG(current_module)->type);
3551
1.87k
  }
3552
3553
3.07k
  lowercase_name = zend_string_tolower_ex(orig_class_entry->name, EG(current_module)->type == MODULE_PERSISTENT);
3554
3.07k
  lowercase_name = zend_new_interned_string(lowercase_name);
3555
3.07k
  zend_hash_update_ptr(CG(class_table), lowercase_name, class_entry);
3556
3.07k
  zend_string_release_ex(lowercase_name, 1);
3557
3558
3.07k
  if (class_entry->__tostring && !zend_string_equals_literal(class_entry->name, "Stringable")
3559
272
      && !(class_entry->ce_flags & ZEND_ACC_TRAIT)) {
3560
272
    ZEND_ASSERT(zend_ce_stringable
3561
272
      && "Should be registered before first class using __toString()");
3562
272
    zend_do_implement_interface(class_entry, zend_ce_stringable);
3563
272
  }
3564
3.07k
  return class_entry;
3565
3.07k
}
3566
/* }}} */
3567
3568
/* If parent_ce is not NULL then it inherits from parent_ce
3569
 * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it
3570
 * If both parent_ce and parent_name are NULL it does a regular class registration
3571
 * If parent_name is specified but not found NULL is returned
3572
 */
3573
ZEND_API zend_class_entry *zend_register_internal_class_ex(const zend_class_entry *class_entry, zend_class_entry *parent_ce) /* {{{ */
3574
0
{
3575
0
  return zend_register_internal_class_with_flags(class_entry, parent_ce, 0);
3576
0
}
3577
/* }}} */
3578
3579
ZEND_API zend_class_entry *zend_register_internal_class_with_flags(
3580
  const zend_class_entry *class_entry,
3581
  zend_class_entry *parent_ce,
3582
  uint32_t ce_flags
3583
2.51k
) {
3584
2.51k
  zend_class_entry *register_class = do_register_internal_class(class_entry, ce_flags);
3585
3586
2.51k
  if (parent_ce) {
3587
1.44k
    zend_do_inheritance(register_class, parent_ce);
3588
1.44k
    zend_build_properties_info_table(register_class);
3589
1.44k
  }
3590
3591
2.51k
  return register_class;
3592
2.51k
}
3593
3594
ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...) /* {{{ */
3595
1.08k
{
3596
1.08k
  zend_class_entry *interface_entry;
3597
1.08k
  va_list interface_list;
3598
1.08k
  va_start(interface_list, num_interfaces);
3599
3600
2.52k
  while (num_interfaces--) {
3601
1.44k
    interface_entry = va_arg(interface_list, zend_class_entry *);
3602
1.44k
    if (interface_entry == zend_ce_stringable
3603
80
        && zend_class_implements_interface(class_entry, zend_ce_stringable)) {
3604
      /* Stringable is implemented automatically,
3605
       * silently ignore an explicit implementation. */
3606
48
      continue;
3607
48
    }
3608
3609
1.39k
    zend_do_implement_interface(class_entry, interface_entry);
3610
1.39k
  }
3611
3612
1.08k
  va_end(interface_list);
3613
1.08k
}
3614
/* }}} */
3615
3616
/* A class that contains at least one abstract method automatically becomes an abstract class.
3617
 */
3618
ZEND_API zend_class_entry *zend_register_internal_class(const zend_class_entry *orig_class_entry) /* {{{ */
3619
224
{
3620
224
  return do_register_internal_class(orig_class_entry, 0);
3621
224
}
3622
/* }}} */
3623
3624
ZEND_API zend_class_entry *zend_register_internal_interface(const zend_class_entry *orig_class_entry) /* {{{ */
3625
336
{
3626
336
  return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE);
3627
336
}
3628
/* }}} */
3629
3630
ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce, bool persistent) /* {{{ */
3631
216
{
3632
216
  zend_string *lcname;
3633
216
  zval zv, *ret;
3634
3635
  /* TODO: Move this out of here in 7.4. */
3636
216
  if (persistent && EG(current_module) && EG(current_module)->type == MODULE_TEMPORARY) {
3637
0
    persistent = false;
3638
0
  }
3639
3640
216
  if (name[0] == '\\') {
3641
1
    lcname = zend_string_alloc(name_len-1, persistent);
3642
1
    zend_str_tolower_copy(ZSTR_VAL(lcname), name+1, name_len-1);
3643
215
  } else {
3644
215
    lcname = zend_string_alloc(name_len, persistent);
3645
215
    zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len);
3646
215
  }
3647
3648
216
  zend_assert_valid_class_name(lcname, "a class alias");
3649
3650
216
  lcname = zend_new_interned_string(lcname);
3651
3652
  /* We cannot increase the refcount of an internal class during request time.
3653
   * Instead of having to deal with differentiating between class types and lifetimes,
3654
   * we simply don't increase the refcount of a class entry for aliases.
3655
   */
3656
216
  ZVAL_ALIAS_PTR(&zv, ce);
3657
3658
216
  ret = zend_hash_add(CG(class_table), lcname, &zv);
3659
216
  zend_string_release_ex(lcname, 0);
3660
216
  if (ret) {
3661
    // avoid notifying at MINIT time
3662
178
    if (ce->type == ZEND_USER_CLASS) {
3663
163
      zend_observer_class_linked_notify(ce, lcname);
3664
163
    }
3665
178
    return SUCCESS;
3666
178
  }
3667
38
  return FAILURE;
3668
216
}
3669
/* }}} */
3670
3671
/* Disabled functions support */
3672
3673
static void zend_disable_function(const char *function_name, size_t function_name_length)
3674
576
{
3675
576
  if (UNEXPECTED(
3676
576
    (function_name_length == strlen("exit") && !memcmp(function_name, "exit", strlen("exit")))
3677
576
    || (function_name_length == strlen("die") && !memcmp(function_name, "die", strlen("die")))
3678
576
    || (function_name_length == strlen("clone") && !memcmp(function_name, "clone", strlen("clone")))
3679
576
  )) {
3680
0
    zend_error(E_WARNING, "Cannot disable function %s()", function_name);
3681
0
    return;
3682
0
  }
3683
576
  zend_hash_str_del(CG(function_table), function_name, function_name_length);
3684
576
}
3685
3686
ZEND_API void zend_disable_functions(const char *function_list) /* {{{ */
3687
16
{
3688
16
  if (!function_list || !*function_list) {
3689
0
    return;
3690
0
  }
3691
3692
16
  const char *s = NULL, *e = function_list;
3693
5.34k
  while (*e) {
3694
5.32k
    switch (*e) {
3695
0
      case ' ':
3696
560
      case ',':
3697
560
        if (s) {
3698
560
          zend_disable_function(s, e - s);
3699
560
          s = NULL;
3700
560
        }
3701
560
        break;
3702
4.76k
      default:
3703
4.76k
        if (!s) {
3704
576
          s = e;
3705
576
        }
3706
4.76k
        break;
3707
5.32k
    }
3708
5.32k
    e++;
3709
5.32k
  }
3710
16
  if (s) {
3711
16
    zend_disable_function(s, e - s);
3712
16
  }
3713
3714
  /* Rehash the function table after deleting functions. This ensures that all internal
3715
   * functions are contiguous, which means we don't need to perform full table cleanup
3716
   * on shutdown. */
3717
16
  zend_hash_rehash(CG(function_table));
3718
16
}
3719
/* }}} */
3720
3721
static zend_always_inline zend_class_entry *get_scope(const zend_execute_data *frame)
3722
3.14k
{
3723
3.14k
  return frame && frame->func ? frame->func->common.scope : NULL;
3724
3.14k
}
3725
3726
static bool zend_is_callable_check_class(zend_string *name, zend_class_entry *scope, const zend_execute_data *frame, zend_fcall_info_cache *fcc, bool *strict_class, char **error, bool suppress_deprecation) /* {{{ */
3727
1.99k
{
3728
1.99k
  bool ret = false;
3729
1.99k
  zend_class_entry *ce;
3730
1.99k
  size_t name_len = ZSTR_LEN(name);
3731
1.99k
  zend_string *lcname;
3732
1.99k
  ALLOCA_FLAG(use_heap);
3733
3734
1.99k
  ZSTR_ALLOCA_ALLOC(lcname, name_len, use_heap);
3735
1.99k
  zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name), name_len);
3736
3737
1.99k
  *strict_class = false;
3738
1.99k
  if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_SELF))) {
3739
436
    if (!scope) {
3740
5
      if (error) *error = estrdup("cannot access \"self\" when no class scope is active");
3741
431
    } else {
3742
431
      if (!suppress_deprecation) {
3743
337
        zend_error(E_DEPRECATED, "Use of \"self\" in callables is deprecated");
3744
337
      }
3745
431
      fcc->called_scope = zend_get_called_scope(frame);
3746
431
      if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope)) {
3747
0
        fcc->called_scope = scope;
3748
0
      }
3749
431
      fcc->calling_scope = scope;
3750
431
      if (!fcc->object) {
3751
352
        fcc->object = zend_get_this_object(frame);
3752
352
      }
3753
431
      ret = true;
3754
431
    }
3755
1.56k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_PARENT))) {
3756
267
    if (!scope) {
3757
0
      if (error) *error = estrdup("cannot access \"parent\" when no class scope is active");
3758
267
    } else if (!scope->parent) {
3759
8
      if (error) *error = estrdup("cannot access \"parent\" when current class scope has no parent");
3760
259
    } else {
3761
259
      if (!suppress_deprecation) {
3762
127
        zend_error(E_DEPRECATED, "Use of \"parent\" in callables is deprecated");
3763
127
      }
3764
259
      fcc->called_scope = zend_get_called_scope(frame);
3765
259
      if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope->parent)) {
3766
15
        fcc->called_scope = scope->parent;
3767
15
      }
3768
259
      fcc->calling_scope = scope->parent;
3769
259
      if (!fcc->object) {
3770
152
        fcc->object = zend_get_this_object(frame);
3771
152
      }
3772
259
      *strict_class = true;
3773
259
      ret = true;
3774
259
    }
3775
1.29k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_STATIC))) {
3776
231
    zend_class_entry *called_scope = zend_get_called_scope(frame);
3777
3778
231
    if (!called_scope) {
3779
0
      if (error) *error = estrdup("cannot access \"static\" when no class scope is active");
3780
231
    } else {
3781
231
      if (!suppress_deprecation) {
3782
154
        zend_error(E_DEPRECATED, "Use of \"static\" in callables is deprecated");
3783
154
      }
3784
231
      fcc->called_scope = called_scope;
3785
231
      fcc->calling_scope = called_scope;
3786
231
      if (!fcc->object) {
3787
164
        fcc->object = zend_get_this_object(frame);
3788
164
      }
3789
231
      *strict_class = true;
3790
231
      ret = true;
3791
231
    }
3792
1.06k
  } else if ((ce = zend_lookup_class(name)) != NULL) {
3793
839
    const zend_class_entry *frame_scope = get_scope(frame);
3794
839
    fcc->calling_scope = ce;
3795
839
    if (frame_scope && !fcc->object) {
3796
427
      zend_object *object = zend_get_this_object(frame);
3797
3798
427
      if (object &&
3799
374
          instanceof_function(object->ce, frame_scope) &&
3800
374
          instanceof_function(frame_scope, ce)) {
3801
205
        fcc->object = object;
3802
205
        fcc->called_scope = object->ce;
3803
222
      } else {
3804
222
        fcc->called_scope = ce;
3805
222
      }
3806
427
    } else {
3807
412
      fcc->called_scope = fcc->object ? fcc->object->ce : ce;
3808
412
    }
3809
839
    *strict_class = true;
3810
839
    ret = true;
3811
839
  } else {
3812
225
    if (error) zend_spprintf(error, 0, "class \"%.*s\" not found", (int)name_len, ZSTR_VAL(name));
3813
225
  }
3814
1.99k
  ZSTR_ALLOCA_FREE(lcname, use_heap);
3815
  /* User error handlers may throw from deprecations above; do not report callable as valid. */
3816
1.99k
  if (UNEXPECTED(EG(exception))) {
3817
22
    return false;
3818
22
  }
3819
1.97k
  return ret;
3820
1.99k
}
3821
/* }}} */
3822
3823
23.5k
ZEND_API void zend_release_fcall_info_cache(zend_fcall_info_cache *fcc) {
3824
23.5k
  if (fcc->function_handler &&
3825
23.2k
    (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
3826
464
    if (fcc->function_handler->common.function_name) {
3827
464
      zend_string_release_ex(fcc->function_handler->common.function_name, 0);
3828
464
    }
3829
464
    zend_free_trampoline(fcc->function_handler);
3830
464
    fcc->function_handler = NULL;
3831
464
  }
3832
23.5k
}
3833
3834
static zend_always_inline bool zend_is_callable_check_func(const zval *callable, const zend_execute_data *frame, zend_fcall_info_cache *fcc, bool strict_class, char **error, bool suppress_deprecation) /* {{{ */
3835
15.5k
{
3836
15.5k
  zend_class_entry *ce_org = fcc->calling_scope;
3837
15.5k
  bool retval = false;
3838
15.5k
  zend_string *mname, *cname;
3839
15.5k
  zend_string *lmname;
3840
15.5k
  const char *colon;
3841
15.5k
  size_t clen;
3842
15.5k
  HashTable *ftable;
3843
15.5k
  int call_via_handler = 0;
3844
15.5k
  zend_class_entry *scope;
3845
15.5k
  zval *zv;
3846
15.5k
  ALLOCA_FLAG(use_heap)
3847
3848
15.5k
  fcc->calling_scope = NULL;
3849
3850
15.5k
  if (!ce_org) {
3851
4.53k
    zend_function *func;
3852
4.53k
    zend_string *lmname;
3853
3854
    /* Check if function with given name exists.
3855
     * This may be a compound name that includes namespace name */
3856
4.53k
    if (UNEXPECTED(Z_STRVAL_P(callable)[0] == '\\')) {
3857
      /* Skip leading \ */
3858
52
      ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable) - 1, use_heap);
3859
52
      zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable) + 1, Z_STRLEN_P(callable) - 1);
3860
52
      func = zend_fetch_function(lmname);
3861
52
      ZSTR_ALLOCA_FREE(lmname, use_heap);
3862
4.48k
    } else {
3863
4.48k
      lmname = Z_STR_P(callable);
3864
4.48k
      func = zend_fetch_function(lmname);
3865
4.48k
      if (!func) {
3866
1.22k
        ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable), use_heap);
3867
1.22k
        zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable), Z_STRLEN_P(callable));
3868
1.22k
        func = zend_fetch_function(lmname);
3869
1.22k
        ZSTR_ALLOCA_FREE(lmname, use_heap);
3870
1.22k
      }
3871
4.48k
    }
3872
4.53k
    if (EXPECTED(func != NULL)) {
3873
3.64k
      fcc->function_handler = func;
3874
3.64k
      return 1;
3875
3.64k
    }
3876
4.53k
  }
3877
3878
  /* Split name into class/namespace and method/function names */
3879
11.9k
  if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL &&
3880
11.9k
    colon > Z_STRVAL_P(callable) &&
3881
1.01k
    *(colon-1) == ':'
3882
11.9k
  ) {
3883
1.00k
    size_t mlen;
3884
3885
1.00k
    colon--;
3886
1.00k
    clen = colon - Z_STRVAL_P(callable);
3887
1.00k
    mlen = Z_STRLEN_P(callable) - clen - 2;
3888
3889
1.00k
    if (colon == Z_STRVAL_P(callable)) {
3890
0
      if (error) *error = estrdup("invalid function name");
3891
0
      return 0;
3892
0
    }
3893
3894
    /* This is a compound name.
3895
     * Try to fetch class and then find static method. */
3896
1.00k
    if (ce_org) {
3897
340
      scope = ce_org;
3898
661
    } else {
3899
661
      scope = get_scope(frame);
3900
661
    }
3901
3902
1.00k
    cname = zend_string_init_interned(Z_STRVAL_P(callable), clen, 0);
3903
1.00k
    if (ZSTR_HAS_CE_CACHE(cname) && ZSTR_GET_CE_CACHE(cname)) {
3904
328
      fcc->calling_scope = ZSTR_GET_CE_CACHE(cname);
3905
328
      if (scope && !fcc->object) {
3906
123
        zend_object *object = zend_get_this_object(frame);
3907
3908
123
        if (object &&
3909
73
            instanceof_function(object->ce, scope) &&
3910
73
            instanceof_function(scope, fcc->calling_scope)) {
3911
73
          fcc->object = object;
3912
73
          fcc->called_scope = object->ce;
3913
73
        } else {
3914
50
          fcc->called_scope = fcc->calling_scope;
3915
50
        }
3916
205
      } else {
3917
205
        fcc->called_scope = fcc->object ? fcc->object->ce : fcc->calling_scope;
3918
205
      }
3919
328
      strict_class = true;
3920
673
    } else if (!zend_is_callable_check_class(cname, scope, frame, fcc, &strict_class, error, suppress_deprecation || ce_org != NULL)) {
3921
141
      zend_string_release_ex(cname, 0);
3922
141
      return 0;
3923
141
    }
3924
860
    zend_string_release_ex(cname, 0);
3925
3926
860
    ftable = &fcc->calling_scope->function_table;
3927
860
    if (ce_org && !instanceof_function(ce_org, fcc->calling_scope)) {
3928
0
      if (error) zend_spprintf(error, 0, "class %s is not a subclass of %s", ZSTR_VAL(ce_org->name), ZSTR_VAL(fcc->calling_scope->name));
3929
0
      return 0;
3930
0
    }
3931
860
    if (ce_org && !suppress_deprecation) {
3932
295
      zend_error(E_DEPRECATED,
3933
295
        "Callables of the form [\"%s\", \"%s\"] are deprecated",
3934
295
        ZSTR_VAL(ce_org->name), Z_STRVAL_P(callable));
3935
295
    }
3936
860
    mname = zend_string_init(Z_STRVAL_P(callable) + clen + 2, mlen, 0);
3937
10.9k
  } else if (ce_org) {
3938
    /* Try to fetch find static method of given class. */
3939
10.6k
    mname = Z_STR_P(callable);
3940
10.6k
    zend_string_addref(mname);
3941
10.6k
    ftable = &ce_org->function_table;
3942
10.6k
    fcc->calling_scope = ce_org;
3943
10.6k
  } else {
3944
    /* We already checked for plain function before. */
3945
230
    if (error) {
3946
103
      zend_spprintf(error, 0, "function \"%s\" not found or invalid function name", Z_STRVAL_P(callable));
3947
103
    }
3948
230
    return 0;
3949
230
  }
3950
3951
11.5k
  lmname = zend_string_tolower(mname);
3952
11.5k
  if (strict_class &&
3953
1.51k
      fcc->calling_scope &&
3954
1.51k
    zend_string_equals_literal(lmname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
3955
0
    fcc->function_handler = fcc->calling_scope->constructor;
3956
0
    if (fcc->function_handler) {
3957
0
      retval = true;
3958
0
    }
3959
11.5k
  } else if ((zv = zend_hash_find(ftable, lmname)) != NULL) {
3960
8.34k
    fcc->function_handler = Z_PTR_P(zv);
3961
8.34k
    retval = true;
3962
8.34k
    if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) &&
3963
32
        !strict_class) {
3964
10
      scope = get_scope(frame);
3965
10
      if (scope &&
3966
10
          instanceof_function(fcc->function_handler->common.scope, scope)) {
3967
3968
10
        zv = zend_hash_find(&scope->function_table, lmname);
3969
10
        if (zv != NULL) {
3970
10
          zend_function *priv_fbc = Z_PTR_P(zv);
3971
3972
10
          if ((priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE)
3973
10
           && priv_fbc->common.scope == scope) {
3974
10
            fcc->function_handler = priv_fbc;
3975
10
          }
3976
10
        }
3977
10
      }
3978
10
    }
3979
8.34k
    if (!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC) &&
3980
283
        (fcc->calling_scope &&
3981
283
         ((fcc->object && fcc->calling_scope->__call) ||
3982
246
          (!fcc->object && fcc->calling_scope->__callstatic)))) {
3983
37
      scope = get_scope(frame);
3984
37
      ZEND_ASSERT(!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC));
3985
37
      if (!zend_check_method_accessible(fcc->function_handler, scope)) {
3986
10
        retval = false;
3987
10
        fcc->function_handler = NULL;
3988
10
        goto get_function_via_handler;
3989
10
      }
3990
37
    }
3991
8.34k
  } else {
3992
3.21k
get_function_via_handler:
3993
3.21k
    if (fcc->object && fcc->calling_scope == ce_org) {
3994
2.81k
      if (strict_class && ce_org->__call) {
3995
22
        fcc->function_handler = zend_get_call_trampoline_func(ce_org->__call, mname);
3996
22
        call_via_handler = 1;
3997
22
        retval = true;
3998
2.79k
      } else {
3999
2.79k
        fcc->function_handler = fcc->object->handlers->get_method(&fcc->object, mname, NULL);
4000
2.79k
        if (fcc->function_handler) {
4001
2.41k
          if (strict_class &&
4002
13
              (!fcc->function_handler->common.scope ||
4003
13
               !instanceof_function(ce_org, fcc->function_handler->common.scope))) {
4004
13
            zend_release_fcall_info_cache(fcc);
4005
2.40k
          } else {
4006
2.40k
            retval = true;
4007
2.40k
            call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
4008
2.40k
          }
4009
2.41k
        }
4010
2.79k
      }
4011
2.81k
    } else if (fcc->calling_scope) {
4012
397
      if (fcc->calling_scope->get_static_method) {
4013
0
        fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname);
4014
397
      } else {
4015
397
        fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, NULL);
4016
397
      }
4017
397
      if (fcc->function_handler) {
4018
317
        retval = true;
4019
317
        call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
4020
317
        if (call_via_handler && !fcc->object) {
4021
254
          zend_object *object = zend_get_this_object(frame);
4022
254
          if (object &&
4023
164
              instanceof_function(object->ce, fcc->calling_scope)) {
4024
164
            fcc->object = object;
4025
164
          }
4026
254
        }
4027
317
      }
4028
397
    }
4029
3.21k
  }
4030
4031
11.5k
  if (retval) {
4032
11.0k
    if (fcc->calling_scope && !call_via_handler) {
4033
8.33k
      if (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT) {
4034
25
        retval = false;
4035
25
        if (error) {
4036
0
          zend_spprintf(error, 0, "cannot call abstract method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
4037
0
        }
4038
8.30k
      } else if (!fcc->object && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
4039
22
        retval = false;
4040
22
        if (error) {
4041
6
          zend_spprintf(error, 0, "non-static method %s::%s() cannot be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
4042
6
        }
4043
22
      }
4044
8.33k
      if (retval
4045
8.28k
       && !(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC)) {
4046
268
        scope = get_scope(frame);
4047
268
        ZEND_ASSERT(!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC));
4048
268
        if (!zend_check_method_accessible(fcc->function_handler, scope)) {
4049
120
          if (error) {
4050
24
            if (*error) {
4051
0
              efree(*error);
4052
0
            }
4053
24
            zend_spprintf(error, 0, "cannot access %s method %s::%s()", zend_visibility_string(fcc->function_handler->common.fn_flags), ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
4054
24
          }
4055
120
          retval = false;
4056
120
        }
4057
268
      }
4058
8.33k
    }
4059
11.0k
  } else if (error) {
4060
42
    if (fcc->calling_scope) {
4061
42
      zend_spprintf(error, 0, "class %s does not have a method \"%s\"", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(mname));
4062
42
    } else {
4063
0
      zend_spprintf(error, 0, "function %s() does not exist", ZSTR_VAL(mname));
4064
0
    }
4065
42
  }
4066
11.5k
  zend_string_release_ex(lmname, 0);
4067
11.5k
  zend_string_release_ex(mname, 0);
4068
4069
11.5k
  if (fcc->object) {
4070
10.7k
    fcc->called_scope = fcc->object->ce;
4071
10.7k
    if (fcc->function_handler
4072
10.3k
     && (fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
4073
53
      fcc->object = NULL;
4074
53
    }
4075
10.7k
  }
4076
11.5k
  return retval;
4077
11.5k
}
4078
/* }}} */
4079
4080
ZEND_API zend_string *zend_get_callable_name_ex(const zval *callable, const zend_object *object) /* {{{ */
4081
196
{
4082
196
try_again:
4083
196
  switch (Z_TYPE_P(callable)) {
4084
7
    case IS_STRING:
4085
7
      if (object) {
4086
0
        return zend_create_member_string(object->ce->name, Z_STR_P(callable));
4087
0
      }
4088
7
      return zend_string_copy(Z_STR_P(callable));
4089
4090
48
    case IS_ARRAY:
4091
48
    {
4092
48
      const zval *method = NULL;
4093
48
      const zval *obj = NULL;
4094
4095
48
      if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
4096
48
        obj = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 0);
4097
48
        method = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 1);
4098
48
      }
4099
4100
48
      if (obj == NULL || method == NULL || Z_TYPE_P(method) != IS_STRING) {
4101
0
        return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
4102
0
      }
4103
4104
48
      if (Z_TYPE_P(obj) == IS_STRING) {
4105
0
        return zend_create_member_string(Z_STR_P(obj), Z_STR_P(method));
4106
48
      } else if (Z_TYPE_P(obj) == IS_OBJECT) {
4107
46
        return zend_create_member_string(Z_OBJCE_P(obj)->name, Z_STR_P(method));
4108
46
      } else {
4109
2
        return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
4110
2
      }
4111
48
    }
4112
141
    case IS_OBJECT:
4113
141
    {
4114
141
      const zend_class_entry *ce = Z_OBJCE_P(callable);
4115
4116
141
      if (ce == zend_ce_closure) {
4117
111
        const zend_function *fn = zend_get_closure_method_def(Z_OBJ_P(callable));
4118
4119
111
        if ((fn->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) && fn->common.scope) {
4120
13
          return zend_create_member_string(fn->common.scope->name, fn->common.function_name);
4121
13
        }
4122
98
        return zend_string_copy(fn->common.function_name);
4123
111
      }
4124
4125
30
      return zend_string_concat2(
4126
30
        ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
4127
30
        "::__invoke", sizeof("::__invoke") - 1);
4128
141
    }
4129
0
    case IS_REFERENCE:
4130
0
      callable = Z_REFVAL_P(callable);
4131
0
      goto try_again;
4132
0
    default:
4133
0
      return zval_get_string_func(callable);
4134
196
  }
4135
196
}
4136
/* }}} */
4137
4138
static bool zend_fcc_function_handler_equals(const zend_function *func1, const zend_function *func2) /* {{{ */
4139
18
{
4140
18
  if (func1 == func2) {
4141
0
    return true;
4142
0
  }
4143
4144
18
  const bool fake_closure1 = (func1->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) != 0;
4145
18
  const bool fake_closure2 = (func2->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) != 0;
4146
4147
18
  if (!fake_closure1 && !fake_closure2) {
4148
18
    return false;
4149
18
  }
4150
0
  if (((func1->common.fn_flags & ZEND_ACC_CLOSURE) && !fake_closure1) ||
4151
0
      ((func2->common.fn_flags & ZEND_ACC_CLOSURE) && !fake_closure2)) {
4152
0
    return false;
4153
0
  }
4154
0
  if (func1->type != func2->type ||
4155
0
      func1->common.scope != func2->common.scope ||
4156
0
      !zend_string_equals(func1->common.function_name, func2->common.function_name)) {
4157
0
    return false;
4158
0
  }
4159
4160
0
  if (func1->type == ZEND_USER_FUNCTION) {
4161
0
    return func1->op_array.opcodes == func2->op_array.opcodes;
4162
0
  }
4163
4164
0
  return func1->internal_function.handler == func2->internal_function.handler;
4165
0
}
4166
/* }}} */
4167
4168
ZEND_API bool zend_fcc_closure_equals_ex(const zend_fcall_info_cache* a, const zend_fcall_info_cache* b) /* {{{ */
4169
18
{
4170
18
  const zend_function *func1 = a->function_handler;
4171
18
  const zend_function *func2 = b->function_handler;
4172
4173
18
  if (a->closure && a->closure->ce == zend_ce_closure) {
4174
18
    func1 = zend_get_closure_method_def(a->closure);
4175
18
  }
4176
18
  if (b->closure && b->closure->ce == zend_ce_closure) {
4177
18
    func2 = zend_get_closure_method_def(b->closure);
4178
18
  }
4179
4180
18
  return zend_fcc_function_handler_equals(func1, func2);
4181
18
}
4182
/* }}} */
4183
4184
ZEND_API zend_string *zend_get_callable_name(const zval *callable) /* {{{ */
4185
0
{
4186
0
  return zend_get_callable_name_ex(callable, NULL);
4187
0
}
4188
/* }}} */
4189
4190
ZEND_API bool zend_is_callable_at_frame(
4191
    const zval *callable, zend_object *object, const zend_execute_data *frame,
4192
    uint32_t check_flags, zend_fcall_info_cache *fcc, char **error) /* {{{ */
4193
28.3k
{
4194
28.3k
  bool ret;
4195
28.3k
  zend_fcall_info_cache fcc_local;
4196
28.3k
  bool strict_class = false;
4197
4198
28.3k
  if (fcc == NULL) {
4199
9.95k
    fcc = &fcc_local;
4200
9.95k
  }
4201
28.3k
  if (error) {
4202
10.2k
    *error = NULL;
4203
10.2k
  }
4204
4205
28.3k
  fcc->calling_scope = NULL;
4206
28.3k
  fcc->called_scope = NULL;
4207
28.3k
  fcc->function_handler = NULL;
4208
28.3k
  fcc->object = NULL;
4209
28.3k
  fcc->closure = NULL;
4210
4211
28.3k
again:
4212
28.3k
  switch (Z_TYPE_P(callable)) {
4213
12.6k
    case IS_STRING:
4214
12.6k
      if (object) {
4215
8.15k
        fcc->object = object;
4216
8.15k
        fcc->calling_scope = object->ce;
4217
8.15k
      }
4218
4219
12.6k
      if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4220
0
        fcc->called_scope = fcc->calling_scope;
4221
0
        return 1;
4222
0
      }
4223
4224
15.5k
check_func:
4225
15.5k
      ret = zend_is_callable_check_func(callable, frame, fcc, strict_class, error, check_flags & IS_CALLABLE_SUPPRESS_DEPRECATIONS);
4226
15.5k
      if (fcc == &fcc_local) {
4227
3.28k
        zend_release_fcall_info_cache(fcc);
4228
3.28k
      }
4229
15.5k
      return ret;
4230
4231
3.06k
    case IS_ARRAY:
4232
3.06k
      {
4233
3.06k
        if (zend_hash_num_elements(Z_ARRVAL_P(callable)) != 2) {
4234
8
          if (error) *error = estrdup("array callback must have exactly two members");
4235
8
          return 0;
4236
8
        }
4237
4238
3.06k
        zval *obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0);
4239
3.06k
        zval *method = zend_hash_index_find(Z_ARRVAL_P(callable), 1);
4240
3.06k
        if (!obj || !method) {
4241
0
          if (error) *error = estrdup("array callback has to contain indices 0 and 1");
4242
0
          return 0;
4243
0
        }
4244
4245
3.06k
        ZVAL_DEREF(obj);
4246
3.06k
        if (Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT) {
4247
61
          if (error) *error = estrdup("first array member is not a valid class name or object");
4248
61
          return 0;
4249
61
        }
4250
4251
2.99k
        ZVAL_DEREF(method);
4252
2.99k
        if (Z_TYPE_P(method) != IS_STRING) {
4253
14
          if (error) *error = estrdup("second array member is not a valid method");
4254
14
          return 0;
4255
14
        }
4256
4257
2.98k
        if (Z_TYPE_P(obj) == IS_STRING) {
4258
1.32k
          if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4259
0
            return 1;
4260
0
          }
4261
4262
1.32k
          if (!zend_is_callable_check_class(Z_STR_P(obj), get_scope(frame), frame, fcc, &strict_class, error, check_flags & IS_CALLABLE_SUPPRESS_DEPRECATIONS)) {
4263
97
            return 0;
4264
97
          }
4265
1.66k
        } else {
4266
1.66k
          ZEND_ASSERT(Z_TYPE_P(obj) == IS_OBJECT);
4267
1.66k
          fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */
4268
1.66k
          fcc->object = Z_OBJ_P(obj);
4269
4270
1.66k
          if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4271
24
            fcc->called_scope = fcc->calling_scope;
4272
24
            return 1;
4273
24
          }
4274
1.66k
        }
4275
4276
2.86k
        callable = method;
4277
2.86k
        goto check_func;
4278
2.98k
      }
4279
0
      return 0;
4280
12.1k
    case IS_OBJECT:
4281
12.1k
      if (Z_OBJ_HANDLER_P(callable, get_closure) && Z_OBJ_HANDLER_P(callable, get_closure)(Z_OBJ_P(callable), &fcc->calling_scope, &fcc->function_handler, &fcc->object, 1) == SUCCESS) {
4282
12.1k
        fcc->called_scope = fcc->calling_scope;
4283
12.1k
        fcc->closure = Z_OBJ_P(callable);
4284
12.1k
        if (fcc == &fcc_local) {
4285
6.20k
          zend_release_fcall_info_cache(fcc);
4286
6.20k
        }
4287
12.1k
        return 1;
4288
12.1k
      }
4289
11
      if (error) *error = estrdup("no array or string given");
4290
11
      return 0;
4291
4
    case IS_REFERENCE:
4292
4
      callable = Z_REFVAL_P(callable);
4293
4
      goto again;
4294
393
    default:
4295
393
      if (error) *error = estrdup("no array or string given");
4296
393
      return 0;
4297
28.3k
  }
4298
28.3k
}
4299
/* }}} */
4300
4301
ZEND_API bool zend_is_callable_ex(const zval *callable, zend_object *object, uint32_t check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error) /* {{{ */
4302
28.3k
{
4303
  /* Determine callability at the first parent user frame. */
4304
28.3k
  const zend_execute_data *frame = EG(current_execute_data);
4305
47.2k
  while (frame && (!frame->func || !ZEND_USER_CODE(frame->func->type))) {
4306
18.8k
    frame = frame->prev_execute_data;
4307
18.8k
  }
4308
4309
28.3k
  bool ret = zend_is_callable_at_frame(callable, object, frame, check_flags, fcc, error);
4310
28.3k
  if (callable_name) {
4311
189
    *callable_name = zend_get_callable_name_ex(callable, object);
4312
189
  }
4313
28.3k
  return ret;
4314
28.3k
}
4315
4316
ZEND_API bool zend_is_callable(const zval *callable, uint32_t check_flags, zend_string **callable_name) /* {{{ */
4317
8.92k
{
4318
8.92k
  return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL);
4319
8.92k
}
4320
/* }}} */
4321
4322
ZEND_API zend_result zend_fcall_info_init(const zval *callable, uint32_t check_flags, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_string **callable_name, char **error) /* {{{ */
4323
7.72k
{
4324
7.72k
  if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, fcc, error)) {
4325
146
    return FAILURE;
4326
146
  }
4327
4328
7.57k
  fci->size = sizeof(*fci);
4329
7.57k
  fci->object = fcc->object;
4330
7.57k
  ZVAL_COPY_VALUE(&fci->function_name, callable);
4331
7.57k
  fci->retval = NULL;
4332
7.57k
  fci->param_count = 0;
4333
7.57k
  fci->params = NULL;
4334
7.57k
  fci->named_params = NULL;
4335
7.57k
  fci->consumed_args = 0;
4336
4337
7.57k
  return SUCCESS;
4338
7.72k
}
4339
/* }}} */
4340
4341
ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, bool free_mem) /* {{{ */
4342
0
{
4343
0
  if (fci->params) {
4344
0
    zval *p = fci->params;
4345
0
    zval *end = p + fci->param_count;
4346
4347
0
    while (p != end) {
4348
0
      i_zval_ptr_dtor(p);
4349
0
      p++;
4350
0
    }
4351
0
    if (free_mem) {
4352
0
      efree(fci->params);
4353
0
      fci->params = NULL;
4354
0
    }
4355
0
  }
4356
0
  fci->param_count = 0;
4357
0
}
4358
/* }}} */
4359
4360
ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, uint32_t *param_count, zval **params) /* {{{ */
4361
0
{
4362
0
  *param_count = fci->param_count;
4363
0
  *params = fci->params;
4364
0
  fci->param_count = 0;
4365
0
  fci->params = NULL;
4366
0
}
4367
/* }}} */
4368
4369
ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, uint32_t param_count, zval *params) /* {{{ */
4370
0
{
4371
0
  zend_fcall_info_args_clear(fci, true);
4372
0
  fci->param_count = param_count;
4373
0
  fci->params = params;
4374
0
}
4375
/* }}} */
4376
4377
ZEND_API zend_result zend_fcall_info_args_ex(zend_fcall_info *fci, zend_function *func, zval *args) /* {{{ */
4378
0
{
4379
0
  zval *arg, *params;
4380
0
  uint32_t n = 1;
4381
4382
0
  zend_fcall_info_args_clear(fci, !args);
4383
4384
0
  if (!args) {
4385
0
    return SUCCESS;
4386
0
  }
4387
4388
0
  if (Z_TYPE_P(args) != IS_ARRAY) {
4389
0
    return FAILURE;
4390
0
  }
4391
4392
0
  fci->param_count = zend_hash_num_elements(Z_ARRVAL_P(args));
4393
0
  fci->params = params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4394
4395
0
  ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), arg) {
4396
0
    if (func && !Z_ISREF_P(arg) && ARG_SHOULD_BE_SENT_BY_REF(func, n)) {
4397
0
      ZVAL_NEW_REF(params, arg);
4398
0
      Z_TRY_ADDREF_P(arg);
4399
0
    } else {
4400
0
      ZVAL_COPY(params, arg);
4401
0
    }
4402
0
    params++;
4403
0
    n++;
4404
0
  } ZEND_HASH_FOREACH_END();
4405
4406
0
  return SUCCESS;
4407
0
}
4408
/* }}} */
4409
4410
ZEND_API zend_result zend_fcall_info_args(zend_fcall_info *fci, zval *args) /* {{{ */
4411
0
{
4412
0
  return zend_fcall_info_args_ex(fci, NULL, args);
4413
0
}
4414
/* }}} */
4415
4416
ZEND_API void zend_fcall_info_argp(zend_fcall_info *fci, uint32_t argc, zval *argv) /* {{{ */
4417
0
{
4418
0
  zend_fcall_info_args_clear(fci, !argc);
4419
4420
0
  if (argc) {
4421
0
    fci->param_count = argc;
4422
0
    fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4423
4424
0
    for (uint32_t i = 0; i < argc; ++i) {
4425
0
      ZVAL_COPY(&fci->params[i], &argv[i]);
4426
0
    }
4427
0
  }
4428
0
}
4429
/* }}} */
4430
4431
ZEND_API void zend_fcall_info_argv(zend_fcall_info *fci, uint32_t argc, va_list *argv) /* {{{ */
4432
0
{
4433
0
  zend_fcall_info_args_clear(fci, !argc);
4434
4435
0
  if (argc) {
4436
0
    zval *arg;
4437
0
    fci->param_count = argc;
4438
0
    fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4439
4440
0
    for (uint32_t i = 0; i < argc; ++i) {
4441
0
      arg = va_arg(*argv, zval *);
4442
0
      ZVAL_COPY(&fci->params[i], arg);
4443
0
    }
4444
0
  }
4445
0
}
4446
/* }}} */
4447
4448
ZEND_API void zend_fcall_info_argn(zend_fcall_info *fci, uint32_t argc, ...) /* {{{ */
4449
0
{
4450
0
  va_list argv;
4451
4452
0
  va_start(argv, argc);
4453
0
  zend_fcall_info_argv(fci, argc, &argv);
4454
0
  va_end(argv);
4455
0
}
4456
/* }}} */
4457
4458
ZEND_API zend_result zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval *retval_ptr, zval *args) /* {{{ */
4459
0
{
4460
0
  zval retval, *org_params = NULL;
4461
0
  uint32_t org_count = 0;
4462
0
  zend_result result;
4463
4464
0
  fci->retval = retval_ptr ? retval_ptr : &retval;
4465
0
  if (args) {
4466
0
    zend_fcall_info_args_save(fci, &org_count, &org_params);
4467
0
    zend_fcall_info_args(fci, args);
4468
0
  }
4469
0
  result = zend_call_function(fci, fcc);
4470
4471
0
  if (!retval_ptr && Z_TYPE(retval) != IS_UNDEF) {
4472
0
    zval_ptr_dtor(&retval);
4473
0
  }
4474
0
  if (args) {
4475
0
    zend_fcall_info_args_restore(fci, org_count, org_params);
4476
0
  }
4477
0
  return result;
4478
0
}
4479
/* }}} */
4480
4481
ZEND_API void zend_get_callable_zval_from_fcc(const zend_fcall_info_cache *fcc, zval *callable)
4482
0
{
4483
0
  if (fcc->closure) {
4484
0
    ZVAL_OBJ_COPY(callable, fcc->closure);
4485
0
  } else if (fcc->function_handler->common.scope) {
4486
0
    array_init(callable);
4487
0
    if (fcc->object) {
4488
0
      GC_ADDREF(fcc->object);
4489
0
      add_next_index_object(callable, fcc->object);
4490
0
    } else {
4491
0
      add_next_index_str(callable, zend_string_copy(fcc->calling_scope->name));
4492
0
    }
4493
0
    add_next_index_str(callable, zend_string_copy(fcc->function_handler->common.function_name));
4494
0
  } else {
4495
0
    ZVAL_STR_COPY(callable, fcc->function_handler->common.function_name);
4496
0
  }
4497
0
}
4498
4499
ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */
4500
0
{
4501
0
  size_t name_len = strlen(module_name);
4502
0
  zend_module_entry *module = zend_hash_str_find_ptr_lc(&module_registry, module_name, name_len);
4503
0
  return module ? module->version : NULL;
4504
0
}
4505
/* }}} */
4506
4507
55.2k
static zend_always_inline bool is_persistent_class(const zend_class_entry *ce) {
4508
55.2k
  return ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->type == MODULE_PERSISTENT;
4509
55.2k
}
4510
4511
ZEND_API zend_property_info *zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_type type) /* {{{ */
4512
48.5k
{
4513
48.5k
  zend_property_info *property_info, *property_info_ptr;
4514
4515
48.5k
  if (ZEND_TYPE_IS_SET(type)) {
4516
31.2k
    ce->ce_flags |= ZEND_ACC_HAS_TYPE_HINTS;
4517
4518
31.2k
    if (access_type & ZEND_ACC_READONLY) {
4519
14.3k
      ce->ce_flags |= ZEND_ACC_HAS_READONLY_PROPS;
4520
14.3k
    }
4521
31.2k
  }
4522
4523
48.5k
  if (ce->type == ZEND_INTERNAL_CLASS) {
4524
1.31k
    property_info = pemalloc(sizeof(zend_property_info), 1);
4525
47.2k
  } else {
4526
47.2k
    property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
4527
47.2k
    if (Z_TYPE_P(property) == IS_CONSTANT_AST) {
4528
2.92k
      ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
4529
2.92k
      if (access_type & ZEND_ACC_STATIC) {
4530
596
        ce->ce_flags |= ZEND_ACC_HAS_AST_STATICS;
4531
2.32k
      } else {
4532
2.32k
        ce->ce_flags |= ZEND_ACC_HAS_AST_PROPERTIES;
4533
2.32k
      }
4534
2.92k
    }
4535
47.2k
  }
4536
4537
48.5k
  if (Z_TYPE_P(property) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(property))) {
4538
1.93k
    zval_make_interned_string(property);
4539
1.93k
  }
4540
4541
48.5k
  if (!(access_type & ZEND_ACC_PPP_MASK)) {
4542
1.97k
    access_type |= ZEND_ACC_PUBLIC;
4543
1.97k
  }
4544
  /* Add the protected(set) bit for public readonly properties with no set visibility. */
4545
48.5k
  if ((access_type & (ZEND_ACC_PUBLIC|ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK)) == (ZEND_ACC_PUBLIC|ZEND_ACC_READONLY)) {
4546
14.2k
    access_type |= ZEND_ACC_PROTECTED_SET;
4547
34.3k
  } else if (UNEXPECTED(access_type & ZEND_ACC_PPP_SET_MASK)) {
4548
768
    if (!ZEND_TYPE_IS_SET(type)) {
4549
10
      zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4550
10
        "Property with asymmetric visibility %s::$%s must have type",
4551
10
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
4552
10
    }
4553
758
    uint32_t get_visibility = zend_visibility_to_set_visibility(access_type & ZEND_ACC_PPP_MASK);
4554
758
    uint32_t set_visibility = access_type & ZEND_ACC_PPP_SET_MASK;
4555
758
    if (get_visibility > set_visibility) {
4556
9
      zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4557
9
        "Visibility of property %s::$%s must not be weaker than set visibility",
4558
9
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
4559
9
    }
4560
    /* Remove equivalent set visibility. */
4561
749
    if (((access_type & (ZEND_ACC_PUBLIC|ZEND_ACC_PUBLIC_SET)) == (ZEND_ACC_PUBLIC|ZEND_ACC_PUBLIC_SET))
4562
510
     || ((access_type & (ZEND_ACC_PROTECTED|ZEND_ACC_PROTECTED_SET)) == (ZEND_ACC_PROTECTED|ZEND_ACC_PROTECTED_SET))
4563
506
     || ((access_type & (ZEND_ACC_PRIVATE|ZEND_ACC_PRIVATE_SET)) == (ZEND_ACC_PRIVATE|ZEND_ACC_PRIVATE_SET))) {
4564
304
      access_type &= ~ZEND_ACC_PPP_SET_MASK;
4565
304
    }
4566
    /* private(set) properties are implicitly final. */
4567
749
    if (access_type & ZEND_ACC_PRIVATE_SET) {
4568
262
      access_type |= ZEND_ACC_FINAL;
4569
262
    }
4570
749
  }
4571
4572
  /* Virtual properties have no backing storage, the offset should never be used. However, the
4573
   * virtual flag cannot be definitively determined at compile time. Allow using default values
4574
   * anyway, and assert after inheritance that the property is not actually virtual. */
4575
48.5k
  if (access_type & ZEND_ACC_VIRTUAL) {
4576
3.49k
    if (Z_TYPE_P(property) == IS_UNDEF) {
4577
3.35k
      property_info->offset = (uint32_t)-1;
4578
3.35k
      goto skip_property_storage;
4579
3.35k
    }
4580
3.49k
  }
4581
45.2k
  if (access_type & ZEND_ACC_STATIC) {
4582
4.30k
    if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
4583
0
      ZEND_ASSERT(property_info_ptr->flags & ZEND_ACC_STATIC);
4584
0
      property_info->offset = property_info_ptr->offset;
4585
0
      zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]);
4586
0
      if (property_info_ptr->doc_comment && property_info_ptr->ce == ce) {
4587
0
        zend_string_release(property_info_ptr->doc_comment);
4588
0
      }
4589
0
      zend_hash_del(&ce->properties_info, name);
4590
4.30k
    } else {
4591
4.30k
      property_info->offset = ce->default_static_members_count++;
4592
4.30k
      ce->default_static_members_table = perealloc(ce->default_static_members_table, sizeof(zval) * ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS);
4593
4.30k
    }
4594
4.30k
    ZVAL_COPY_VALUE(&ce->default_static_members_table[property_info->offset], property);
4595
4.30k
    if (!ZEND_MAP_PTR(ce->static_members_table)) {
4596
4.30k
      if (ce->type == ZEND_INTERNAL_CLASS &&
4597
0
          ce->info.internal.module->type == MODULE_PERSISTENT) {
4598
0
        ZEND_MAP_PTR_NEW(ce->static_members_table);
4599
0
      }
4600
4.30k
    }
4601
40.9k
  } else {
4602
40.9k
    zval *property_default_ptr;
4603
40.9k
    if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
4604
0
      ZEND_ASSERT(!(property_info_ptr->flags & ZEND_ACC_STATIC));
4605
0
      property_info->offset = property_info_ptr->offset;
4606
0
      zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]);
4607
0
      if (property_info_ptr->doc_comment && property_info_ptr->ce == ce) {
4608
0
        zend_string_release_ex(property_info_ptr->doc_comment, 1);
4609
0
      }
4610
0
      zend_hash_del(&ce->properties_info, name);
4611
4612
0
      ZEND_ASSERT(ce->type == ZEND_INTERNAL_CLASS);
4613
0
      ZEND_ASSERT(ce->properties_info_table != NULL);
4614
0
      ce->properties_info_table[OBJ_PROP_TO_NUM(property_info->offset)] = property_info;
4615
40.9k
    } else {
4616
40.9k
      property_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count);
4617
40.9k
      ce->default_properties_count++;
4618
40.9k
      ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
4619
4620
      /* For user classes this is handled during linking */
4621
40.9k
      if (ce->type == ZEND_INTERNAL_CLASS) {
4622
1.20k
        ce->properties_info_table = perealloc(ce->properties_info_table, sizeof(zend_property_info *) * ce->default_properties_count, 1);
4623
1.20k
        ce->properties_info_table[ce->default_properties_count - 1] = property_info;
4624
1.20k
      }
4625
40.9k
    }
4626
40.9k
    property_default_ptr = &ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
4627
40.9k
    ZVAL_COPY_VALUE(property_default_ptr, property);
4628
40.9k
    Z_PROP_FLAG_P(property_default_ptr) = Z_ISUNDEF_P(property) ? IS_PROP_UNINIT : 0;
4629
40.9k
  }
4630
48.5k
skip_property_storage:
4631
48.5k
  if (ce->type == ZEND_INTERNAL_CLASS) {
4632
    /* Must be interned to avoid ZTS data races */
4633
1.31k
    if (is_persistent_class(ce)) {
4634
1.31k
      name = zend_new_interned_string(zend_string_copy(name));
4635
1.31k
    }
4636
4637
1.31k
    if (Z_REFCOUNTED_P(property)) {
4638
0
      zend_error_noreturn(E_CORE_ERROR, "Internal zvals cannot be refcounted");
4639
0
    }
4640
1.31k
  }
4641
4642
48.5k
  if (access_type & ZEND_ACC_PUBLIC) {
4643
43.2k
    property_info->name = zend_string_copy(name);
4644
43.2k
  } else if (access_type & ZEND_ACC_PRIVATE) {
4645
3.86k
    property_info->name = zend_mangle_property_name(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
4646
3.86k
  } else {
4647
1.50k
    ZEND_ASSERT(access_type & ZEND_ACC_PROTECTED);
4648
1.50k
    property_info->name = zend_mangle_property_name("*", 1, ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
4649
1.50k
  }
4650
4651
48.5k
  property_info->name = zend_new_interned_string(property_info->name);
4652
48.5k
  property_info->flags = access_type;
4653
48.5k
  property_info->doc_comment = doc_comment;
4654
48.5k
  property_info->attributes = NULL;
4655
48.5k
  property_info->prototype = property_info;
4656
48.5k
  property_info->hooks = NULL;
4657
48.5k
  property_info->ce = ce;
4658
48.5k
  property_info->type = type;
4659
4660
48.5k
  if (is_persistent_class(ce)) {
4661
1.31k
    zend_normalize_internal_type(&property_info->type);
4662
1.31k
  }
4663
4664
48.5k
  zend_hash_update_ptr(&ce->properties_info, name, property_info);
4665
4666
48.5k
  return property_info;
4667
48.5k
}
4668
/* }}} */
4669
4670
ZEND_API zend_result zend_try_assign_typed_ref_ex(zend_reference *ref, zval *val, bool strict) /* {{{ */
4671
17
{
4672
17
  if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, val, strict))) {
4673
5
    zval_ptr_dtor(val);
4674
5
    return FAILURE;
4675
12
  } else {
4676
12
    zend_safe_assign_to_variable_noref(&ref->val, val);
4677
12
    return SUCCESS;
4678
12
  }
4679
17
}
4680
/* }}} */
4681
4682
ZEND_API zend_result zend_try_assign_typed_ref(zend_reference *ref, zval *val) /* {{{ */
4683
17
{
4684
17
  return zend_try_assign_typed_ref_ex(ref, val, ZEND_ARG_USES_STRICT_TYPES());
4685
17
}
4686
/* }}} */
4687
4688
ZEND_API zend_result zend_try_assign_typed_ref_null(zend_reference *ref) /* {{{ */
4689
0
{
4690
0
  zval tmp;
4691
4692
0
  ZVAL_NULL(&tmp);
4693
0
  return zend_try_assign_typed_ref(ref, &tmp);
4694
0
}
4695
/* }}} */
4696
4697
ZEND_API zend_result zend_try_assign_typed_ref_bool(zend_reference *ref, bool val) /* {{{ */
4698
0
{
4699
0
  zval tmp;
4700
4701
0
  ZVAL_BOOL(&tmp, val);
4702
0
  return zend_try_assign_typed_ref(ref, &tmp);
4703
0
}
4704
/* }}} */
4705
4706
ZEND_API zend_result zend_try_assign_typed_ref_long(zend_reference *ref, zend_long lval) /* {{{ */
4707
0
{
4708
0
  zval tmp;
4709
4710
0
  ZVAL_LONG(&tmp, lval);
4711
0
  return zend_try_assign_typed_ref(ref, &tmp);
4712
0
}
4713
/* }}} */
4714
4715
ZEND_API zend_result zend_try_assign_typed_ref_double(zend_reference *ref, double dval) /* {{{ */
4716
0
{
4717
0
  zval tmp;
4718
4719
0
  ZVAL_DOUBLE(&tmp, dval);
4720
0
  return zend_try_assign_typed_ref(ref, &tmp);
4721
0
}
4722
/* }}} */
4723
4724
ZEND_API zend_result zend_try_assign_typed_ref_empty_string(zend_reference *ref) /* {{{ */
4725
0
{
4726
0
  zval tmp;
4727
4728
0
  ZVAL_EMPTY_STRING(&tmp);
4729
0
  return zend_try_assign_typed_ref(ref, &tmp);
4730
0
}
4731
/* }}} */
4732
4733
ZEND_API zend_result zend_try_assign_typed_ref_str(zend_reference *ref, zend_string *str) /* {{{ */
4734
0
{
4735
0
  zval tmp;
4736
4737
0
  ZVAL_STR(&tmp, str);
4738
0
  return zend_try_assign_typed_ref(ref, &tmp);
4739
0
}
4740
/* }}} */
4741
4742
ZEND_API zend_result zend_try_assign_typed_ref_string(zend_reference *ref, const char *string) /* {{{ */
4743
7
{
4744
7
  zval tmp;
4745
4746
7
  ZVAL_STRING(&tmp, string);
4747
7
  return zend_try_assign_typed_ref(ref, &tmp);
4748
7
}
4749
/* }}} */
4750
4751
ZEND_API zend_result zend_try_assign_typed_ref_stringl(zend_reference *ref, const char *string, size_t len) /* {{{ */
4752
0
{
4753
0
  zval tmp;
4754
4755
0
  ZVAL_STRINGL(&tmp, string, len);
4756
0
  return zend_try_assign_typed_ref(ref, &tmp);
4757
0
}
4758
/* }}} */
4759
4760
ZEND_API zend_result zend_try_assign_typed_ref_arr(zend_reference *ref, zend_array *arr) /* {{{ */
4761
10
{
4762
10
  zval tmp;
4763
4764
10
  ZVAL_ARR(&tmp, arr);
4765
10
  return zend_try_assign_typed_ref(ref, &tmp);
4766
10
}
4767
/* }}} */
4768
4769
ZEND_API zend_result zend_try_assign_typed_ref_res(zend_reference *ref, zend_resource *res) /* {{{ */
4770
0
{
4771
0
  zval tmp;
4772
4773
0
  ZVAL_RES(&tmp, res);
4774
0
  return zend_try_assign_typed_ref(ref, &tmp);
4775
0
}
4776
/* }}} */
4777
4778
ZEND_API zend_result zend_try_assign_typed_ref_zval(zend_reference *ref, zval *zv) /* {{{ */
4779
0
{
4780
0
  zval tmp;
4781
4782
0
  ZVAL_COPY_VALUE(&tmp, zv);
4783
0
  return zend_try_assign_typed_ref(ref, &tmp);
4784
0
}
4785
/* }}} */
4786
4787
ZEND_API zend_result zend_try_assign_typed_ref_zval_ex(zend_reference *ref, zval *zv, bool strict) /* {{{ */
4788
0
{
4789
0
  zval tmp;
4790
4791
0
  ZVAL_COPY_VALUE(&tmp, zv);
4792
0
  return zend_try_assign_typed_ref_ex(ref, &tmp, strict);
4793
0
}
4794
/* }}} */
4795
4796
ZEND_API void zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */
4797
0
{
4798
0
  zend_declare_typed_property(ce, name, property, access_type, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
4799
0
}
4800
/* }}} */
4801
4802
ZEND_API void zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type) /* {{{ */
4803
0
{
4804
0
  zend_string *key = zend_string_init(name, name_length, is_persistent_class(ce));
4805
0
  zend_declare_property_ex(ce, key, property, access_type, NULL);
4806
0
  zend_string_release(key);
4807
0
}
4808
/* }}} */
4809
4810
ZEND_API void zend_declare_property_null(zend_class_entry *ce, const char *name, size_t name_length, int access_type) /* {{{ */
4811
0
{
4812
0
  zval property;
4813
4814
0
  ZVAL_NULL(&property);
4815
0
  zend_declare_property(ce, name, name_length, &property, access_type);
4816
0
}
4817
/* }}} */
4818
4819
ZEND_API void zend_declare_property_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
4820
0
{
4821
0
  zval property;
4822
4823
0
  ZVAL_BOOL(&property, value);
4824
0
  zend_declare_property(ce, name, name_length, &property, access_type);
4825
0
}
4826
/* }}} */
4827
4828
ZEND_API void zend_declare_property_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
4829
0
{
4830
0
  zval property;
4831
4832
0
  ZVAL_LONG(&property, value);
4833
0
  zend_declare_property(ce, name, name_length, &property, access_type);
4834
0
}
4835
/* }}} */
4836
4837
ZEND_API void zend_declare_property_double(zend_class_entry *ce, const char *name, size_t name_length, double value, int access_type) /* {{{ */
4838
0
{
4839
0
  zval property;
4840
4841
0
  ZVAL_DOUBLE(&property, value);
4842
0
  zend_declare_property(ce, name, name_length, &property, access_type);
4843
0
}
4844
/* }}} */
4845
4846
ZEND_API void zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type) /* {{{ */
4847
0
{
4848
0
  zval property;
4849
4850
0
  ZVAL_NEW_STR(&property, zend_string_init(value, strlen(value), ce->type == ZEND_INTERNAL_CLASS));
4851
0
  zend_declare_property(ce, name, name_length, &property, access_type);
4852
0
}
4853
/* }}} */
4854
4855
ZEND_API void zend_declare_property_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_len, int access_type) /* {{{ */
4856
0
{
4857
0
  zval property;
4858
4859
0
  ZVAL_NEW_STR(&property, zend_string_init(value, value_len, ce->type == ZEND_INTERNAL_CLASS));
4860
0
  zend_declare_property(ce, name, name_length, &property, access_type);
4861
0
}
4862
/* }}} */
4863
4864
ZEND_API zend_class_constant *zend_declare_typed_class_constant(zend_class_entry *ce, zend_string *name, zval *value, int flags, zend_string *doc_comment, zend_type type) /* {{{ */
4865
34.8k
{
4866
34.8k
  zend_class_constant *c;
4867
4868
34.8k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
4869
496
    if (!(flags & ZEND_ACC_PUBLIC)) {
4870
1
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface constant %s::%s must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4871
1
    }
4872
496
  }
4873
4874
34.8k
  if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_CLASS))) {
4875
5
    zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4876
5
        "A class constant must not be called 'class'; it is reserved for class name fetching");
4877
5
  }
4878
4879
34.8k
  if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) {
4880
2.16k
    zval_make_interned_string(value);
4881
2.16k
  }
4882
4883
34.8k
  if (ce->type == ZEND_INTERNAL_CLASS) {
4884
4.72k
    c = pemalloc(sizeof(zend_class_constant), 1);
4885
4.72k
    if (ZEND_TYPE_PURE_MASK(type) != MAY_BE_ANY) {
4886
4.72k
      ZEND_ASSERT(!ZEND_TYPE_CONTAINS_CODE(type, IS_RESOURCE) && "resource is not allowed in a zend_type");
4887
4.72k
    }
4888
30.1k
  } else {
4889
30.1k
    c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
4890
30.1k
  }
4891
34.8k
  ZVAL_COPY_VALUE(&c->value, value);
4892
34.8k
  ZEND_CLASS_CONST_FLAGS(c) = flags;
4893
34.8k
  c->doc_comment = doc_comment;
4894
34.8k
  c->attributes = NULL;
4895
34.8k
  c->ce = ce;
4896
34.8k
  c->type = type;
4897
4898
34.8k
  if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
4899
27.9k
    ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
4900
27.9k
    ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
4901
27.9k
    if (ce->type == ZEND_INTERNAL_CLASS && !ZEND_MAP_PTR(ce->mutable_data)) {
4902
224
      ZEND_MAP_PTR_NEW(ce->mutable_data);
4903
224
    }
4904
27.9k
  }
4905
4906
34.8k
  if (!zend_hash_add_ptr(&ce->constants_table, name, c)) {
4907
40
    zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4908
40
      "Cannot redefine class constant %s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4909
40
  }
4910
4911
34.8k
  return c;
4912
34.8k
}
4913
4914
ZEND_API zend_class_constant *zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int flags, zend_string *doc_comment)
4915
25.4k
{
4916
25.4k
  return zend_declare_typed_class_constant(ce, name, value, flags, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
4917
25.4k
}
4918
4919
ZEND_API void zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
4920
0
{
4921
0
  zend_string *key;
4922
4923
0
  if (ce->type == ZEND_INTERNAL_CLASS) {
4924
0
    key = zend_string_init_interned(name, name_length, 1);
4925
0
  } else {
4926
0
    key = zend_string_init(name, name_length, 0);
4927
0
  }
4928
0
  zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
4929
0
  zend_string_release(key);
4930
0
}
4931
/* }}} */
4932
4933
ZEND_API void zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length) /* {{{ */
4934
0
{
4935
0
  zval constant;
4936
4937
0
  ZVAL_NULL(&constant);
4938
0
  zend_declare_class_constant(ce, name, name_length, &constant);
4939
0
}
4940
/* }}} */
4941
4942
ZEND_API void zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value) /* {{{ */
4943
0
{
4944
0
  zval constant;
4945
4946
0
  ZVAL_LONG(&constant, value);
4947
0
  zend_declare_class_constant(ce, name, name_length, &constant);
4948
0
}
4949
/* }}} */
4950
4951
ZEND_API void zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, bool value) /* {{{ */
4952
0
{
4953
0
  zval constant;
4954
4955
0
  ZVAL_BOOL(&constant, value);
4956
0
  zend_declare_class_constant(ce, name, name_length, &constant);
4957
0
}
4958
/* }}} */
4959
4960
ZEND_API void zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value) /* {{{ */
4961
0
{
4962
0
  zval constant;
4963
4964
0
  ZVAL_DOUBLE(&constant, value);
4965
0
  zend_declare_class_constant(ce, name, name_length, &constant);
4966
0
}
4967
/* }}} */
4968
4969
ZEND_API void zend_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length) /* {{{ */
4970
0
{
4971
0
  zval constant;
4972
4973
0
  ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type == ZEND_INTERNAL_CLASS));
4974
0
  zend_declare_class_constant(ce, name, name_length, &constant);
4975
0
}
4976
/* }}} */
4977
4978
ZEND_API void zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value) /* {{{ */
4979
0
{
4980
0
  zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value));
4981
0
}
4982
/* }}} */
4983
4984
ZEND_API void zend_update_property_ex(const zend_class_entry *scope, zend_object *object, zend_string *name, zval *value) /* {{{ */
4985
941k
{
4986
941k
  const zend_class_entry *old_scope = EG(fake_scope);
4987
4988
941k
  EG(fake_scope) = scope;
4989
4990
941k
  object->handlers->write_property(object, name, value, NULL);
4991
4992
941k
  EG(fake_scope) = old_scope;
4993
941k
}
4994
/* }}} */
4995
4996
ZEND_API void zend_update_property(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zval *value) /* {{{ */
4997
19.4k
{
4998
19.4k
  zend_string *property;
4999
19.4k
  const zend_class_entry *old_scope = EG(fake_scope);
5000
5001
19.4k
  EG(fake_scope) = scope;
5002
5003
19.4k
  property = zend_string_init(name, name_length, 0);
5004
19.4k
  object->handlers->write_property(object, property, value, NULL);
5005
19.4k
  zend_string_release_ex(property, 0);
5006
5007
19.4k
  EG(fake_scope) = old_scope;
5008
19.4k
}
5009
/* }}} */
5010
5011
ZEND_API void zend_update_property_null(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */
5012
54
{
5013
54
  zval tmp;
5014
5015
54
  ZVAL_NULL(&tmp);
5016
54
  zend_update_property(scope, object, name, name_length, &tmp);
5017
54
}
5018
/* }}} */
5019
5020
ZEND_API void zend_unset_property(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */
5021
4
{
5022
4
  zend_string *property;
5023
4
  const zend_class_entry *old_scope = EG(fake_scope);
5024
5025
4
  EG(fake_scope) = scope;
5026
5027
4
  property = zend_string_init(name, name_length, 0);
5028
4
  object->handlers->unset_property(object, property, 0);
5029
4
  zend_string_release_ex(property, 0);
5030
5031
4
  EG(fake_scope) = old_scope;
5032
4
}
5033
/* }}} */
5034
5035
ZEND_API void zend_update_property_bool(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value) /* {{{ */
5036
0
{
5037
0
  zval tmp;
5038
5039
0
  ZVAL_BOOL(&tmp, value);
5040
0
  zend_update_property(scope, object, name, name_length, &tmp);
5041
0
}
5042
/* }}} */
5043
5044
ZEND_API void zend_update_property_long(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value) /* {{{ */
5045
50
{
5046
50
  zval tmp;
5047
5048
50
  ZVAL_LONG(&tmp, value);
5049
50
  zend_update_property(scope, object, name, name_length, &tmp);
5050
50
}
5051
/* }}} */
5052
5053
ZEND_API void zend_update_property_double(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, double value) /* {{{ */
5054
0
{
5055
0
  zval tmp;
5056
5057
0
  ZVAL_DOUBLE(&tmp, value);
5058
0
  zend_update_property(scope, object, name, name_length, &tmp);
5059
0
}
5060
/* }}} */
5061
5062
ZEND_API void zend_update_property_str(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_string *value) /* {{{ */
5063
0
{
5064
0
  zval tmp;
5065
5066
0
  ZVAL_STR(&tmp, value);
5067
0
  zend_update_property(scope, object, name, name_length, &tmp);
5068
0
}
5069
/* }}} */
5070
5071
ZEND_API void zend_update_property_string(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, const char *value) /* {{{ */
5072
0
{
5073
0
  zval tmp;
5074
5075
0
  ZVAL_STRING(&tmp, value);
5076
0
  Z_SET_REFCOUNT(tmp, 0);
5077
0
  zend_update_property(scope, object, name, name_length, &tmp);
5078
0
}
5079
/* }}} */
5080
5081
ZEND_API void zend_update_property_stringl(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */
5082
25
{
5083
25
  zval tmp;
5084
5085
25
  ZVAL_STRINGL(&tmp, value, value_len);
5086
25
  Z_SET_REFCOUNT(tmp, 0);
5087
25
  zend_update_property(scope, object, name, name_length, &tmp);
5088
25
}
5089
/* }}} */
5090
5091
ZEND_API zend_result zend_update_static_property_ex(zend_class_entry *scope, zend_string *name, zval *value) /* {{{ */
5092
10
{
5093
10
  zval *property, tmp;
5094
10
  zend_property_info *prop_info;
5095
5096
10
  if (UNEXPECTED(!(scope->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
5097
10
    if (UNEXPECTED(zend_update_class_constants(scope) != SUCCESS)) {
5098
5
      return FAILURE;
5099
5
    }
5100
10
  }
5101
5102
5
  const zend_class_entry *old_scope = EG(fake_scope);
5103
5
  EG(fake_scope) = scope;
5104
5
  property = zend_std_get_static_property_with_info(scope, name, BP_VAR_W, &prop_info);
5105
5
  EG(fake_scope) = old_scope;
5106
5107
5
  if (!property) {
5108
0
    return FAILURE;
5109
0
  }
5110
5111
5
  ZEND_ASSERT(!Z_ISREF_P(value));
5112
5
  Z_TRY_ADDREF_P(value);
5113
5
  if (ZEND_TYPE_IS_SET(prop_info->type)) {
5114
0
    ZVAL_COPY_VALUE(&tmp, value);
5115
0
    if (!zend_verify_property_type(prop_info, &tmp, /* strict */ 0)) {
5116
0
      Z_TRY_DELREF_P(value);
5117
0
      return FAILURE;
5118
0
    }
5119
0
    value = &tmp;
5120
0
  }
5121
5122
5
  zend_assign_to_variable(property, value, IS_TMP_VAR, /* strict */ 0);
5123
5
  return SUCCESS;
5124
5
}
5125
/* }}} */
5126
5127
ZEND_API zend_result zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value) /* {{{ */
5128
0
{
5129
0
  zend_string *key = zend_string_init(name, name_length, 0);
5130
0
  zend_result retval = zend_update_static_property_ex(scope, key, value);
5131
0
  zend_string_efree(key);
5132
0
  return retval;
5133
0
}
5134
/* }}} */
5135
5136
ZEND_API zend_result zend_update_static_property_null(zend_class_entry *scope, const char *name, size_t name_length) /* {{{ */
5137
0
{
5138
0
  zval tmp;
5139
5140
0
  ZVAL_NULL(&tmp);
5141
0
  return zend_update_static_property(scope, name, name_length, &tmp);
5142
0
}
5143
/* }}} */
5144
5145
ZEND_API zend_result zend_update_static_property_bool(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
5146
0
{
5147
0
  zval tmp;
5148
5149
0
  ZVAL_BOOL(&tmp, value);
5150
0
  return zend_update_static_property(scope, name, name_length, &tmp);
5151
0
}
5152
/* }}} */
5153
5154
ZEND_API zend_result zend_update_static_property_long(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
5155
0
{
5156
0
  zval tmp;
5157
5158
0
  ZVAL_LONG(&tmp, value);
5159
0
  return zend_update_static_property(scope, name, name_length, &tmp);
5160
0
}
5161
/* }}} */
5162
5163
ZEND_API zend_result zend_update_static_property_double(zend_class_entry *scope, const char *name, size_t name_length, double value) /* {{{ */
5164
0
{
5165
0
  zval tmp;
5166
5167
0
  ZVAL_DOUBLE(&tmp, value);
5168
0
  return zend_update_static_property(scope, name, name_length, &tmp);
5169
0
}
5170
/* }}} */
5171
5172
ZEND_API zend_result zend_update_static_property_string(zend_class_entry *scope, const char *name, size_t name_length, const char *value) /* {{{ */
5173
0
{
5174
0
  zval tmp;
5175
5176
0
  ZVAL_STRING(&tmp, value);
5177
0
  Z_SET_REFCOUNT(tmp, 0);
5178
0
  return zend_update_static_property(scope, name, name_length, &tmp);
5179
0
}
5180
/* }}} */
5181
5182
ZEND_API zend_result zend_update_static_property_stringl(zend_class_entry *scope, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */
5183
0
{
5184
0
  zval tmp;
5185
5186
0
  ZVAL_STRINGL(&tmp, value, value_len);
5187
0
  Z_SET_REFCOUNT(tmp, 0);
5188
0
  return zend_update_static_property(scope, name, name_length, &tmp);
5189
0
}
5190
/* }}} */
5191
5192
ZEND_API zval *zend_read_property_ex(const zend_class_entry *scope, zend_object *object, zend_string *name, bool silent, zval *rv) /* {{{ */
5193
25.3M
{
5194
25.3M
  zval *value;
5195
25.3M
  const zend_class_entry *old_scope = EG(fake_scope);
5196
5197
25.3M
  EG(fake_scope) = scope;
5198
5199
25.3M
  value = object->handlers->read_property(object, name, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
5200
5201
25.3M
  EG(fake_scope) = old_scope;
5202
25.3M
  return value;
5203
25.3M
}
5204
/* }}} */
5205
5206
ZEND_API zval *zend_read_property(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, bool silent, zval *rv) /* {{{ */
5207
34
{
5208
34
  zval *value;
5209
34
  zend_string *str;
5210
5211
34
  str = zend_string_init(name, name_length, 0);
5212
34
  value = zend_read_property_ex(scope, object, str, silent, rv);
5213
34
  zend_string_release_ex(str, 0);
5214
34
  return value;
5215
34
}
5216
/* }}} */
5217
5218
ZEND_API zval *zend_read_static_property_ex(zend_class_entry *scope, zend_string *name, bool silent) /* {{{ */
5219
13
{
5220
13
  zval *property;
5221
13
  const zend_class_entry *old_scope = EG(fake_scope);
5222
5223
13
  EG(fake_scope) = scope;
5224
13
  property = zend_std_get_static_property(scope, name, silent ? BP_VAR_IS : BP_VAR_R);
5225
13
  EG(fake_scope) = old_scope;
5226
5227
13
  return property;
5228
13
}
5229
/* }}} */
5230
5231
ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, size_t name_length, bool silent) /* {{{ */
5232
0
{
5233
0
  zend_string *key = zend_string_init(name, name_length, 0);
5234
0
  zval *property = zend_read_static_property_ex(scope, key, silent);
5235
0
  zend_string_efree(key);
5236
0
  return property;
5237
0
}
5238
/* }}} */
5239
5240
ZEND_API void zend_save_error_handling(zend_error_handling *current) /* {{{ */
5241
9
{
5242
9
  current->handling = EG(error_handling);
5243
9
  current->exception = EG(exception_class);
5244
9
}
5245
/* }}} */
5246
5247
ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current) /* {{{ */
5248
9
{
5249
9
  if (current) {
5250
9
    zend_save_error_handling(current);
5251
9
  }
5252
9
  ZEND_ASSERT(error_handling == EH_THROW || exception_class == NULL);
5253
9
  EG(error_handling) = error_handling;
5254
9
  EG(exception_class) = exception_class;
5255
9
}
5256
/* }}} */
5257
5258
ZEND_API void zend_restore_error_handling(const zend_error_handling *saved) /* {{{ */
5259
9
{
5260
9
  EG(error_handling) = saved->handling;
5261
9
  EG(exception_class) = saved->exception;
5262
9
}
5263
/* }}} */
5264
5265
ZEND_API ZEND_COLD const char *zend_get_object_type_case(const zend_class_entry *ce, bool upper_case) /* {{{ */
5266
498
{
5267
498
  if (ce->ce_flags & ZEND_ACC_TRAIT) {
5268
12
    return upper_case ? "Trait" : "trait";
5269
486
  } else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
5270
74
    return upper_case ? "Interface" : "interface";
5271
412
  } else if (ce->ce_flags & ZEND_ACC_ENUM) {
5272
53
    return upper_case ? "Enum" : "enum";
5273
359
  } else {
5274
359
    return upper_case ? "Class" : "class";
5275
359
  }
5276
498
}
5277
/* }}} */
5278
5279
ZEND_API bool zend_is_iterable(const zval *iterable) /* {{{ */
5280
68
{
5281
68
  switch (Z_TYPE_P(iterable)) {
5282
0
    case IS_ARRAY:
5283
0
      return 1;
5284
62
    case IS_OBJECT:
5285
62
      return zend_class_implements_interface(Z_OBJCE_P(iterable), zend_ce_traversable);
5286
6
    default:
5287
6
      return 0;
5288
68
  }
5289
68
}
5290
/* }}} */
5291
5292
ZEND_API bool zend_is_countable(const zval *countable) /* {{{ */
5293
0
{
5294
0
  switch (Z_TYPE_P(countable)) {
5295
0
    case IS_ARRAY:
5296
0
      return 1;
5297
0
    case IS_OBJECT:
5298
0
      if (Z_OBJ_HT_P(countable)->count_elements) {
5299
0
        return 1;
5300
0
      }
5301
5302
0
      return zend_class_implements_interface(Z_OBJCE_P(countable), zend_ce_countable);
5303
0
    default:
5304
0
      return 0;
5305
0
  }
5306
0
}
5307
/* }}} */
5308
5309
16
static zend_result get_default_via_ast(zval *default_value_zval, const char *default_value) {
5310
16
  zend_ast *ast;
5311
16
  zend_arena *ast_arena;
5312
5313
16
  zend_string *code = zend_string_concat3(
5314
16
    "<?php ", sizeof("<?php ") - 1, default_value, strlen(default_value), ";", 1);
5315
5316
16
  ast = zend_compile_string_to_ast(code, &ast_arena, ZSTR_EMPTY_ALLOC());
5317
16
  zend_string_release(code);
5318
5319
16
  if (!ast) {
5320
0
    return FAILURE;
5321
0
  }
5322
5323
16
  zend_ast_list *statement_list = zend_ast_get_list(ast);
5324
16
  zend_ast **const_expr_ast_ptr = &statement_list->child[0];
5325
5326
16
  zend_arena *original_ast_arena = CG(ast_arena);
5327
16
  uint32_t original_compiler_options = CG(compiler_options);
5328
16
  zend_file_context original_file_context;
5329
16
  CG(ast_arena) = ast_arena;
5330
  /* Disable constant substitution, to make getDefaultValueConstant() work. */
5331
16
  CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
5332
16
  zend_file_context_begin(&original_file_context);
5333
16
  zend_const_expr_to_zval(default_value_zval, const_expr_ast_ptr, /* allow_dynamic */ true);
5334
16
  CG(ast_arena) = original_ast_arena;
5335
16
  CG(compiler_options) = original_compiler_options;
5336
16
  zend_file_context_end(&original_file_context);
5337
5338
16
  zend_ast_destroy(ast);
5339
16
  zend_arena_destroy(ast_arena);
5340
5341
16
  return SUCCESS;
5342
16
}
5343
5344
7
static zend_string *try_parse_string(const char *str, size_t len, char quote) {
5345
7
  if (len == 0) {
5346
0
    return ZSTR_EMPTY_ALLOC();
5347
0
  }
5348
5349
14
  for (size_t i = 0; i < len; i++) {
5350
7
    if (str[i] == '\\' || str[i] == quote) {
5351
0
      return NULL;
5352
0
    }
5353
7
  }
5354
7
  return zend_string_init(str, len, 0);
5355
7
}
5356
5357
ZEND_API zend_result zend_get_default_from_internal_arg_info(
5358
    zval *default_value_zval, const zend_arg_info *arg_info)
5359
198
{
5360
198
  const zend_string *default_value = arg_info->default_value;
5361
198
  if (!default_value) {
5362
19
    return FAILURE;
5363
19
  }
5364
5365
  /* Avoid going through the full AST machinery for some simple and common cases. */
5366
179
  zend_ulong lval;
5367
179
  if (zend_string_equals_literal(default_value, "null")) {
5368
135
    ZVAL_NULL(default_value_zval);
5369
135
    return SUCCESS;
5370
135
  } else if (zend_string_equals_literal(default_value, "true")) {
5371
0
    ZVAL_TRUE(default_value_zval);
5372
0
    return SUCCESS;
5373
44
  } else if (zend_string_equals_literal(default_value, "false")) {
5374
21
    ZVAL_FALSE(default_value_zval);
5375
21
    return SUCCESS;
5376
23
  } else if (ZSTR_LEN(default_value) >= 2
5377
23
      && (ZSTR_VAL(default_value)[0] == '\'' || ZSTR_VAL(default_value)[0] == '"')
5378
7
      && ZSTR_VAL(default_value)[ZSTR_LEN(default_value) - 1] == ZSTR_VAL(default_value)[0]) {
5379
7
    zend_string *str = try_parse_string(
5380
7
      ZSTR_VAL(default_value) + 1, ZSTR_LEN(default_value) - 2, ZSTR_VAL(default_value)[0]);
5381
7
    if (str) {
5382
7
      ZVAL_STR(default_value_zval, str);
5383
7
      return SUCCESS;
5384
7
    }
5385
16
  } else if (zend_string_equals_literal(default_value, "[]")) {
5386
0
    ZVAL_EMPTY_ARRAY(default_value_zval);
5387
0
    return SUCCESS;
5388
16
  } else if (ZEND_HANDLE_NUMERIC(default_value, lval)) {
5389
0
    ZVAL_LONG(default_value_zval, lval);
5390
0
    return SUCCESS;
5391
0
  }
5392
5393
#if 0
5394
  fprintf(stderr, "Evaluating %s via AST\n", ZSTR_VAL(default_value));
5395
#endif
5396
16
  return get_default_via_ast(default_value_zval, ZSTR_VAL(default_value));
5397
179
}