Coverage Report

Created: 2026-02-09 07:07

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