Coverage Report

Created: 2025-12-14 06:09

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