Coverage Report

Created: 2026-02-14 06:52

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