Coverage Report

Created: 2025-09-27 06:26

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