Coverage Report

Created: 2026-04-01 06:49

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
27.4k
{
91
27.4k
  zend_string *tmp_property_name;
92
27.4k
  zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name);
93
27.4k
  zend_error(E_WARNING, "Attempt to read property \"%s\" on %s", ZSTR_VAL(property_name), zend_zval_value_name(object));
94
27.4k
  zend_tmp_string_release(tmp_property_name);
95
27.4k
}
96
97
/* Argument parsing API -- andrei */
98
ZEND_API const char *zend_get_type_by_const(int type) /* {{{ */
99
23.8k
{
100
23.8k
  switch(type) {
101
58
    case IS_FALSE:
102
269
    case IS_TRUE:
103
360
    case _IS_BOOL:
104
360
      return "bool";
105
2.46k
    case IS_LONG:
106
2.46k
      return "int";
107
807
    case IS_DOUBLE:
108
807
      return "float";
109
3.64k
    case IS_STRING:
110
3.64k
      return "string";
111
18
    case IS_OBJECT:
112
18
      return "object";
113
2
    case IS_RESOURCE:
114
2
      return "resource";
115
15.0k
    case IS_NULL:
116
15.0k
      return "null";
117
0
    case IS_CALLABLE:
118
0
      return "callable";
119
0
    case IS_ITERABLE:
120
0
      return "iterable";
121
1.49k
    case IS_ARRAY:
122
1.49k
      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
23.8k
    EMPTY_SWITCH_DEFAULT_CASE()
130
23.8k
  }
131
23.8k
}
132
/* }}} */
133
134
ZEND_API const char *zend_zval_value_name(const zval *arg)
135
41.2k
{
136
41.2k
  ZVAL_DEREF(arg);
137
138
41.2k
  if (Z_ISUNDEF_P(arg)) {
139
22.2k
    return "null";
140
22.2k
  }
141
142
18.9k
  if (Z_TYPE_P(arg) == IS_OBJECT) {
143
916
    return ZSTR_VAL(Z_OBJCE_P(arg)->name);
144
18.0k
  } else if (Z_TYPE_P(arg) == IS_FALSE) {
145
212
    return "false";
146
17.8k
  } else if  (Z_TYPE_P(arg) == IS_TRUE) {
147
139
    return "true";
148
139
  }
149
150
17.7k
  return zend_get_type_by_const(Z_TYPE_P(arg));
151
18.9k
}
152
153
ZEND_API const char *zend_zval_type_name(const zval *arg)
154
4.99k
{
155
4.99k
  ZVAL_DEREF(arg);
156
157
4.99k
  if (Z_ISUNDEF_P(arg)) {
158
0
    return "null";
159
0
  }
160
161
4.99k
  if (Z_TYPE_P(arg) == IS_OBJECT) {
162
475
    return ZSTR_VAL(Z_OBJCE_P(arg)->name);
163
475
  }
164
165
4.51k
  return zend_get_type_by_const(Z_TYPE_P(arg));
166
4.99k
}
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
129
{
172
129
  switch (Z_TYPE_P(arg)) {
173
15
    case IS_NULL:
174
15
      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
21
    case IS_ARRAY:
185
21
      return ZSTR_KNOWN(ZEND_STR_ARRAY);
186
1
    case IS_OBJECT:
187
1
      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
129
  }
197
129
}
198
/* }}} */
199
200
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_none_error(void) /* {{{ */
201
46
{
202
46
  int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
203
46
  zend_string *func_name = get_active_function_or_method_name();
204
205
46
  zend_argument_count_error("%s() expects exactly 0 arguments, %d given", ZSTR_VAL(func_name), num_args);
206
207
46
  zend_string_release(func_name);
208
46
}
209
/* }}} */
210
211
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_error(uint32_t min_num_args, uint32_t max_num_args) /* {{{ */
212
251
{
213
251
  uint32_t num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
214
251
  zend_string *func_name = get_active_function_or_method_name();
215
216
251
  zend_argument_count_error(
217
251
    "%s() expects %s %d argument%s, %d given",
218
251
    ZSTR_VAL(func_name),
219
251
    min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
220
251
    num_args < min_num_args ? min_num_args : max_num_args,
221
251
    (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
222
251
    num_args
223
251
  );
224
225
251
  zend_string_release(func_name);
226
251
}
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
710
{
231
710
  switch (error_code) {
232
43
    case ZPP_ERROR_WRONG_CALLBACK:
233
43
      zend_wrong_callback_error(num, name);
234
43
      break;
235
87
    case ZPP_ERROR_WRONG_CALLBACK_OR_NULL:
236
87
      zend_wrong_callback_or_null_error(num, name);
237
87
      break;
238
23
    case ZPP_ERROR_WRONG_CLASS:
239
23
      zend_wrong_parameter_class_error(num, name, arg);
240
23
      break;
241
2
    case ZPP_ERROR_WRONG_CLASS_OR_NULL:
242
2
      zend_wrong_parameter_class_or_null_error(num, name, arg);
243
2
      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
287
    case ZPP_ERROR_WRONG_ARG:
257
287
      zend_wrong_parameter_type_error(num, expected_type, arg);
258
287
      break;
259
17
    case ZPP_ERROR_UNEXPECTED_EXTRA_NAMED:
260
17
      zend_unexpected_extra_named_error();
261
17
      break;
262
251
    case ZPP_ERROR_FAILURE:
263
251
      ZEND_ASSERT(EG(exception) && "Should have produced an error already");
264
251
      break;
265
710
    EMPTY_SWITCH_DEFAULT_CASE()
266
710
  }
267
710
}
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
287
{
272
287
  static const char * const expected_error[] = {
273
9.75k
    Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR)
274
287
    NULL
275
287
  };
276
277
287
  if (EG(exception)) {
278
21
    return;
279
21
  }
280
281
266
  if ((expected_type == Z_EXPECTED_PATH || expected_type == Z_EXPECTED_PATH_OR_NULL)
282
13
      && Z_TYPE_P(arg) == IS_STRING) {
283
13
    zend_argument_value_error(num, "must not contain any null bytes");
284
13
    return;
285
13
  }
286
287
253
  zend_argument_type_error(num, "must be %s, %s given", expected_error[expected_type], zend_zval_value_name(arg));
288
253
}
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
23
{
293
23
  if (EG(exception)) {
294
0
    return;
295
0
  }
296
297
23
  zend_argument_type_error(num, "must be of type %s, %s given", name, zend_zval_value_name(arg));
298
23
}
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
2
{
303
2
  if (EG(exception)) {
304
0
    return;
305
0
  }
306
307
2
  zend_argument_type_error(num, "must be of type ?%s, %s given", name, zend_zval_value_name(arg));
308
2
}
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
43
{
353
43
  if (!EG(exception)) {
354
43
    zend_argument_type_error(num, "must be a valid callback, %s", error);
355
43
  }
356
43
  efree(error);
357
43
}
358
/* }}} */
359
360
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_or_null_error(uint32_t num, char *error) /* {{{ */
361
87
{
362
87
  if (!EG(exception)) {
363
72
    zend_argument_type_error(num, "must be a valid callback or null, %s", error);
364
72
  }
365
87
  efree(error);
366
87
}
367
/* }}} */
368
369
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_unexpected_extra_named_error(void)
370
29
{
371
29
  const char *space;
372
29
  const char *class_name = get_active_class_name(&space);
373
29
  zend_argument_count_error("Internal function %s%s%s() does not accept named variadic arguments",
374
29
    class_name, space, get_active_function_name()
375
29
  );
376
29
}
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.52k
{
380
1.52k
  zend_string *func_name;
381
1.52k
  const char *arg_name;
382
1.52k
  char *message = NULL;
383
1.52k
  if (EG(exception)) {
384
0
    return;
385
0
  }
386
387
1.52k
  func_name = get_active_function_or_method_name();
388
1.52k
  arg_name = get_active_function_arg_name(arg_num);
389
390
1.52k
  zend_vspprintf(&message, 0, format, va);
391
1.52k
  zend_throw_error(error_ce, "%s(): Argument #%d%s%s%s %s",
392
1.52k
    ZSTR_VAL(func_name), arg_num,
393
1.52k
    arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "", message
394
1.52k
  );
395
1.52k
  efree(message);
396
1.52k
  zend_string_release(func_name);
397
1.52k
}
398
/* }}} */
399
400
ZEND_API ZEND_COLD void zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format, ...) /* {{{ */
401
70
{
402
70
  va_list va;
403
404
70
  va_start(va, format);
405
70
  zend_argument_error_variadic(error_ce, arg_num, format, va);
406
70
  va_end(va);
407
70
}
408
/* }}} */
409
410
ZEND_API ZEND_COLD void zend_argument_type_error(uint32_t arg_num, const char *format, ...) /* {{{ */
411
1.29k
{
412
1.29k
  va_list va;
413
414
1.29k
  va_start(va, format);
415
1.29k
  zend_argument_error_variadic(zend_ce_type_error, arg_num, format, va);
416
1.29k
  va_end(va);
417
1.29k
}
418
/* }}} */
419
420
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format, ...) /* {{{ */
421
169
{
422
169
  va_list va;
423
424
169
  va_start(va, format);
425
169
  zend_argument_error_variadic(zend_ce_value_error, arg_num, format, va);
426
169
  va_end(va);
427
169
}
428
/* }}} */
429
430
ZEND_API ZEND_COLD void zend_argument_must_not_be_empty_error(uint32_t arg_num)
431
1
{
432
1
  zend_argument_value_error(arg_num, "must not be empty");
433
1
}
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
151
{
437
151
  if (old_ce->type == ZEND_INTERNAL_CLASS) {
438
9
    zend_error(type, "Cannot redeclare %s %s",
439
9
      zend_get_object_type(old_ce),
440
9
      ZSTR_VAL(new_name));
441
142
  } else {
442
142
    zend_error(type, "Cannot redeclare %s %s (previously declared in %s:%d)",
443
142
      zend_get_object_type(old_ce),
444
142
      ZSTR_VAL(new_name),
445
142
      ZSTR_VAL(old_ce->info.user.filename),
446
142
      old_ce->info.user.line_start);
447
142
  }
448
151
}
449
450
ZEND_API ZEND_COLD void zend_class_redeclaration_error(int type, const zend_class_entry *old_ce)
451
136
{
452
136
  zend_class_redeclaration_error_ex(type, old_ce->name, old_ce);
453
136
}
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
5.22k
static ZEND_COLD bool zend_null_arg_deprecated(const char *fallback_type, uint32_t arg_num) {
485
5.22k
  const zend_function *func = zend_active_function();
486
5.22k
  ZEND_ASSERT(arg_num > 0);
487
5.22k
  uint32_t arg_offset = arg_num - 1;
488
5.22k
  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
5.22k
  const zend_arg_info *arg_info = &func->common.arg_info[arg_offset];
494
5.22k
  zend_string *func_name = get_active_function_or_method_name();
495
5.22k
  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
5.22k
  zend_string *type_str = zend_type_to_string(arg_info->type);
500
5.22k
  const char *type = type_str ? ZSTR_VAL(type_str) : fallback_type;
501
5.22k
  zend_error(E_DEPRECATED,
502
5.22k
    "%s(): Passing null to parameter #%" PRIu32 "%s%s%s of type %s is deprecated",
503
5.22k
    ZSTR_VAL(func_name), arg_num,
504
5.22k
    arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "",
505
5.22k
    type);
506
5.22k
  zend_string_release(func_name);
507
5.22k
  if (type_str) {
508
5.22k
    zend_string_release(type_str);
509
5.22k
  }
510
5.22k
  return !EG(exception);
511
5.22k
}
512
513
ZEND_API bool ZEND_FASTCALL zend_parse_arg_bool_weak(const zval *arg, bool *dest, uint32_t arg_num) /* {{{ */
514
2.59k
{
515
2.59k
  if (EXPECTED(Z_TYPE_P(arg) <= IS_STRING)) {
516
2.59k
    if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("bool", arg_num)) {
517
0
      return 0;
518
0
    }
519
2.59k
    *dest = zend_is_true(arg);
520
2.59k
  } else {
521
0
    return 0;
522
0
  }
523
2.59k
  return 1;
524
2.59k
}
525
/* }}} */
526
527
ZEND_API bool ZEND_FASTCALL zend_parse_arg_bool_slow(const zval *arg, bool *dest, uint32_t arg_num) /* {{{ */
528
1.38k
{
529
1.38k
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
530
0
    return 0;
531
0
  }
532
1.38k
  return zend_parse_arg_bool_weak(arg, dest, arg_num);
533
1.38k
}
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.29k
{
546
2.29k
  if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
547
382
    if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
548
10
      return 0;
549
10
    }
550
372
    if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
551
110
      return 0;
552
262
    } else {
553
262
      zend_long lval = zend_dval_to_lval(Z_DVAL_P(arg));
554
262
      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
108
        if (arg_num != (uint32_t)-1) {
558
80
          zend_incompatible_double_to_long_error(Z_DVAL_P(arg));
559
80
        }
560
108
        if (UNEXPECTED(EG(exception))) {
561
0
          return 0;
562
0
        }
563
108
      }
564
262
      *dest = lval;
565
262
    }
566
1.91k
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
567
888
    double d;
568
888
    uint8_t type;
569
570
888
    if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
571
447
      if (EXPECTED(type != 0)) {
572
165
        zend_long lval;
573
165
        if (UNEXPECTED(zend_isnan(d))) {
574
0
          return 0;
575
0
        }
576
165
        if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
577
25
          return 0;
578
25
        }
579
580
140
        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
140
        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
140
        *dest = lval;
593
282
      } else {
594
282
        return 0;
595
282
      }
596
447
    }
597
1.02k
  } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
598
799
    if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("int", arg_num)) {
599
0
      return 0;
600
0
    }
601
799
    *dest = 0;
602
799
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
603
53
    *dest = 1;
604
171
  } else {
605
171
    return 0;
606
171
  }
607
1.69k
  return 1;
608
2.29k
}
609
/* }}} */
610
611
ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_slow(const zval *arg, zend_long *dest, uint32_t arg_num) /* {{{ */
612
778
{
613
778
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
614
6
    return 0;
615
6
  }
616
772
  return zend_parse_arg_long_weak(arg, dest, arg_num);
617
778
}
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.46k
{
630
1.46k
  if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
631
1.16k
    *dest = (double)Z_LVAL_P(arg);
632
1.16k
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
633
89
    zend_long l;
634
89
    uint8_t type;
635
636
89
    if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), &l, dest)) != IS_DOUBLE)) {
637
75
      if (EXPECTED(type != 0)) {
638
46
        *dest = (double)(l);
639
46
      } else {
640
29
        return 0;
641
29
      }
642
75
    }
643
204
  } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
644
157
    if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("float", arg_num)) {
645
0
      return 0;
646
0
    }
647
157
    *dest = 0.0;
648
157
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
649
4
    *dest = 1.0;
650
43
  } else {
651
43
    return 0;
652
43
  }
653
1.39k
  return 1;
654
1.46k
}
655
/* }}} */
656
657
ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_slow(const zval *arg, double *dest, uint32_t arg_num) /* {{{ */
658
705
{
659
705
  if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
660
    /* SSTH Exception: IS_LONG may be accepted instead as IS_DOUBLE */
661
545
    *dest = (double)Z_LVAL_P(arg);
662
545
  } else if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
663
0
    return 0;
664
0
  }
665
705
  return zend_parse_arg_double_weak(arg, dest, arg_num);
666
705
}
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
9.08k
{
734
9.08k
  if (EXPECTED(Z_TYPE_P(arg) < IS_STRING)) {
735
7.93k
    if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("string", arg_num)) {
736
0
      return 0;
737
0
    }
738
7.93k
    convert_to_string(arg);
739
7.93k
    *dest = Z_STR_P(arg);
740
7.93k
  } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
741
1.10k
    zend_object *zobj = Z_OBJ_P(arg);
742
1.10k
    zval obj;
743
1.10k
    if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) {
744
986
      OBJ_RELEASE(zobj);
745
986
      ZVAL_COPY_VALUE(arg, &obj);
746
986
      *dest = Z_STR_P(arg);
747
986
      return 1;
748
986
    }
749
122
    return 0;
750
1.10k
  } else {
751
38
    return 0;
752
38
  }
753
7.93k
  return 1;
754
9.08k
}
755
/* }}} */
756
757
ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest, uint32_t arg_num) /* {{{ */
758
8.48k
{
759
8.48k
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
760
17
    return 0;
761
17
  }
762
8.47k
  return zend_parse_arg_str_weak(arg, dest, arg_num);
763
8.48k
}
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
11
{
776
11
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
777
0
    return 0;
778
0
  }
779
11
  if (zend_parse_arg_long_weak(arg, dest_long, arg_num)) {
780
4
    *dest_str = NULL;
781
4
    return 1;
782
7
  } else if (zend_parse_arg_str_weak(arg, dest_str, arg_num)) {
783
3
    *dest_long = 0;
784
3
    return 1;
785
4
  } else {
786
4
    return 0;
787
4
  }
788
11
}
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
421k
{
793
421k
  const char *spec_walk = *spec;
794
421k
  char c = *spec_walk++;
795
421k
  bool check_null = false;
796
421k
  bool separate = false;
797
421k
  zval *real_arg = arg;
798
799
  /* scan through modifiers */
800
421k
  ZVAL_DEREF(arg);
801
421k
  while (1) {
802
421k
    if (*spec_walk == '/') {
803
0
      SEPARATE_ZVAL_NOREF(arg);
804
0
      real_arg = arg;
805
0
      separate = true;
806
421k
    } else if (*spec_walk == '!') {
807
417
      check_null = true;
808
421k
    } else {
809
421k
      break;
810
421k
    }
811
417
    spec_walk++;
812
417
  }
813
814
421k
  switch (c) {
815
1.75k
    case 'l':
816
1.75k
      {
817
1.75k
        zend_long *p = va_arg(*va, zend_long *);
818
1.75k
        bool *is_null = NULL;
819
820
1.75k
        if (check_null) {
821
65
          is_null = va_arg(*va, bool *);
822
65
        }
823
824
1.75k
        if (!zend_parse_arg_long(arg, p, is_null, check_null, arg_num)) {
825
12
          return check_null ? "?int" : "int";
826
12
        }
827
1.75k
      }
828
1.74k
      break;
829
830
1.74k
    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
263k
    case 's':
856
263k
      {
857
263k
        char **p = va_arg(*va, char **);
858
263k
        size_t *pl = va_arg(*va, size_t *);
859
263k
        if (!zend_parse_arg_string(arg, p, pl, check_null, arg_num)) {
860
0
          return check_null ? "?string" : "string";
861
0
        }
862
263k
      }
863
263k
      break;
864
865
263k
    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.0k
    case 'S':
895
63.0k
      {
896
63.0k
        zend_string **str = va_arg(*va, zend_string **);
897
63.0k
        if (!zend_parse_arg_str(arg, str, check_null, arg_num)) {
898
16
          return check_null ? "?string" : "string";
899
16
        }
900
63.0k
      }
901
63.0k
      break;
902
903
63.0k
    case 'b':
904
55.8k
      {
905
55.8k
        bool *p = va_arg(*va, bool *);
906
55.8k
        bool *is_null = NULL;
907
908
55.8k
        if (check_null) {
909
0
          is_null = va_arg(*va, bool *);
910
0
        }
911
912
55.8k
        if (!zend_parse_arg_bool(arg, p, is_null, check_null, arg_num)) {
913
0
          return check_null ? "?bool" : "bool";
914
0
        }
915
55.8k
      }
916
55.8k
      break;
917
918
55.8k
    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
300
    case 'A':
929
430
    case 'a':
930
430
      {
931
430
        zval **p = va_arg(*va, zval **);
932
933
430
        if (!zend_parse_arg_array(arg, p, check_null, c == 'A')) {
934
5
          return check_null ? "?array" : "array";
935
5
        }
936
430
      }
937
425
      break;
938
939
425
    case 'H':
940
31.3k
    case 'h':
941
31.3k
      {
942
31.3k
        HashTable **p = va_arg(*va, HashTable **);
943
944
31.3k
        if (!zend_parse_arg_array_ht(arg, p, check_null, c == 'H', separate)) {
945
1
          return check_null ? "?array" : "array";
946
1
        }
947
31.3k
      }
948
31.3k
      break;
949
950
31.3k
    case 'o':
951
124
      {
952
124
        zval **p = va_arg(*va, zval **);
953
954
124
        if (!zend_parse_arg_object(arg, p, NULL, check_null)) {
955
3
          return check_null ? "?object" : "object";
956
3
        }
957
124
      }
958
121
      break;
959
960
3.91k
    case 'O':
961
3.91k
      {
962
3.91k
        zval **p = va_arg(*va, zval **);
963
3.91k
        zend_class_entry *ce = va_arg(*va, zend_class_entry *);
964
965
3.91k
        if (!zend_parse_arg_object(arg, p, ce, check_null)) {
966
11
          if (ce) {
967
11
            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
11
            } else {
971
11
              return ZSTR_VAL(ce->name);
972
11
            }
973
11
          } else {
974
0
            return check_null ? "?object" : "object";
975
0
          }
976
11
        }
977
3.91k
      }
978
3.89k
      break;
979
980
3.89k
    case 'C':
981
466
      {
982
466
        zend_class_entry *lookup, **pce = va_arg(*va, zend_class_entry **);
983
466
        zend_class_entry *ce_base = *pce;
984
985
466
        if (check_null && Z_TYPE_P(arg) == IS_NULL) {
986
0
          *pce = NULL;
987
0
          break;
988
0
        }
989
466
        if (!try_convert_to_string(arg)) {
990
13
          *pce = NULL;
991
13
          return ""; /* try_convert_to_string() throws an exception */
992
13
        }
993
994
453
        if ((lookup = zend_lookup_class(Z_STR_P(arg))) == NULL) {
995
8
          *pce = NULL;
996
445
        } else {
997
445
          *pce = lookup;
998
445
        }
999
453
        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
453
        if (!*pce) {
1008
8
          zend_spprintf(error, 0, "must be a valid class name%s, %s given",
1009
8
            check_null ? " or null" : "", Z_STRVAL_P(arg));
1010
8
          return "";
1011
8
        }
1012
445
        break;
1013
1014
453
      }
1015
445
      break;
1016
1017
445
    case 'F':
1018
377
    case 'f':
1019
377
      {
1020
377
        zend_fcall_info *fci = va_arg(*va, zend_fcall_info *);
1021
377
        zend_fcall_info_cache *fcc = va_arg(*va, zend_fcall_info_cache *);
1022
377
        char *is_callable_error = NULL;
1023
1024
377
        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
377
        if (zend_fcall_info_init(arg, 0, fci, fcc, NULL, &is_callable_error) == SUCCESS) {
1031
372
          ZEND_ASSERT(!is_callable_error);
1032
372
          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
372
          break;
1039
372
        }
1040
1041
5
        if (is_callable_error) {
1042
5
          zend_spprintf(error, 0, "must be a valid callback%s, %s", check_null ? " or null" : "", is_callable_error);
1043
5
          efree(is_callable_error);
1044
5
          return "";
1045
5
        } else {
1046
0
          return check_null ? "a valid callback or null" : "a valid callback";
1047
0
        }
1048
5
      }
1049
1050
504
    case 'z':
1051
504
      {
1052
504
        zval **p = va_arg(*va, zval **);
1053
1054
504
        zend_parse_arg_zval_deref(real_arg, p, check_null);
1055
504
      }
1056
504
      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
421k
  }
1065
1066
421k
  *spec = spec_walk;
1067
1068
421k
  return NULL;
1069
421k
}
1070
/* }}} */
1071
1072
static zend_result zend_parse_arg(uint32_t arg_num, zval *arg, va_list *va, const char **spec, int flags) /* {{{ */
1073
421k
{
1074
421k
  const char *expected_type = NULL;
1075
421k
  char *error = NULL;
1076
1077
421k
  expected_type = zend_parse_arg_impl(arg, va, spec, &error, arg_num);
1078
421k
  if (expected_type) {
1079
74
    if (EG(exception)) {
1080
13
      return FAILURE;
1081
13
    }
1082
61
    if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) {
1083
55
      if (error) {
1084
13
        if (strcmp(error, "must not contain any null bytes") == 0) {
1085
0
          zend_argument_value_error(arg_num, "%s", error);
1086
13
        } else {
1087
13
          zend_argument_type_error(arg_num, "%s", error);
1088
13
        }
1089
13
        efree(error);
1090
42
      } else {
1091
42
        zend_argument_type_error(arg_num, "must be of type %s, %s given", expected_type, zend_zval_value_name(arg));
1092
42
      }
1093
55
    } else if (error) {
1094
0
      efree(error);
1095
0
    }
1096
1097
61
    return FAILURE;
1098
74
  }
1099
1100
421k
  return SUCCESS;
1101
421k
}
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
365k
{
1127
365k
  const  char *spec_walk;
1128
365k
  char c;
1129
365k
  uint32_t i;
1130
365k
  uint32_t min_num_args = 0;
1131
365k
  uint32_t max_num_args = 0;
1132
365k
  uint32_t post_varargs = 0;
1133
365k
  zval *arg;
1134
365k
  bool have_varargs = false;
1135
365k
  bool have_optional_args = false;
1136
365k
  zval **varargs = NULL;
1137
365k
  uint32_t *n_varargs = NULL;
1138
1139
875k
  for (spec_walk = type_spec; *spec_walk; spec_walk++) {
1140
509k
    c = *spec_walk;
1141
509k
    switch (c) {
1142
10.1k
      case 'l': case 'd':
1143
331k
      case 's': case 'b':
1144
331k
      case 'r': case 'a':
1145
339k
      case 'o': case 'O':
1146
340k
      case 'z': case 'Z':
1147
372k
      case 'C': case 'h':
1148
373k
      case 'f': case 'F': case 'A':
1149
373k
      case 'H': case 'p':
1150
438k
      case 'S': case 'P':
1151
438k
      case 'L': case 'n':
1152
438k
        max_num_args++;
1153
438k
        break;
1154
1155
65.8k
      case '|':
1156
65.8k
        min_num_args = max_num_args;
1157
65.8k
        have_optional_args = true;
1158
65.8k
        break;
1159
1160
0
      case '/':
1161
5.08k
      case '!':
1162
        /* Pass */
1163
5.08k
        break;
1164
1165
317
      case '*':
1166
329
      case '+':
1167
329
        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
329
        have_varargs = true;
1173
        /* we expect at least one parameter in varargs */
1174
329
        if (c == '+') {
1175
12
          max_num_args++;
1176
12
        }
1177
        /* mark the beginning of varargs */
1178
329
        post_varargs = max_num_args;
1179
1180
329
        if (ZEND_CALL_INFO(EG(current_execute_data)) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
1181
12
          zend_unexpected_extra_named_error();
1182
12
          return FAILURE;
1183
12
        }
1184
317
        break;
1185
1186
317
      default:
1187
0
        zend_parse_parameters_debug_error("bad type specifier while parsing parameters");
1188
0
        return FAILURE;
1189
509k
    }
1190
509k
  }
1191
1192
  /* with no optional arguments the minimum number of arguments must be the same as the maximum */
1193
365k
  if (!have_optional_args) {
1194
299k
    min_num_args = max_num_args;
1195
299k
  }
1196
1197
365k
  if (have_varargs) {
1198
    /* calculate how many required args are at the end of the specifier list */
1199
317
    post_varargs = max_num_args - post_varargs;
1200
317
    max_num_args = UINT32_MAX;
1201
317
  }
1202
1203
365k
  if (num_args < min_num_args || num_args > max_num_args) {
1204
107
    if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
1205
86
      zend_string *func_name = get_active_function_or_method_name();
1206
1207
86
      zend_argument_count_error("%s() expects %s %d argument%s, %d given",
1208
86
        ZSTR_VAL(func_name),
1209
86
        min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
1210
86
        num_args < min_num_args ? min_num_args : max_num_args,
1211
86
        (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
1212
86
        num_args
1213
86
      );
1214
1215
86
      zend_string_release(func_name);
1216
86
    }
1217
107
    return FAILURE;
1218
107
  }
1219
1220
365k
  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
365k
  i = 0;
1226
787k
  while (num_args-- > 0) {
1227
421k
    if (*type_spec == '|') {
1228
59.8k
      type_spec++;
1229
59.8k
    }
1230
1231
421k
    if (*type_spec == '*' || *type_spec == '+') {
1232
7
      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
7
      varargs = va_arg(*va, zval **);
1236
7
      n_varargs = va_arg(*va, uint32_t *);
1237
7
      type_spec++;
1238
1239
7
      if (num_varargs > 0) {
1240
7
        *n_varargs = num_varargs;
1241
7
        *varargs = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
1242
        /* adjust how many args we have left and restart loop */
1243
7
        num_args += 1 - num_varargs;
1244
7
        i += num_varargs;
1245
7
        continue;
1246
7
      } else {
1247
0
        *varargs = NULL;
1248
0
        *n_varargs = 0;
1249
0
      }
1250
7
    }
1251
1252
421k
    arg = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
1253
1254
421k
    if (zend_parse_arg(i+1, arg, va, &type_spec, flags) == FAILURE) {
1255
      /* clean up varargs array if it was used */
1256
74
      if (varargs && *varargs) {
1257
0
        *varargs = NULL;
1258
0
      }
1259
74
      return FAILURE;
1260
74
    }
1261
421k
    i++;
1262
421k
  }
1263
1264
365k
  return SUCCESS;
1265
365k
}
1266
/* }}} */
1267
1268
ZEND_API zend_result zend_parse_parameters_ex(int flags, uint32_t num_args, const char *type_spec, ...) /* {{{ */
1269
35
{
1270
35
  va_list va;
1271
35
  zend_result retval;
1272
1273
35
  va_start(va, type_spec);
1274
35
  retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1275
35
  va_end(va);
1276
1277
35
  return retval;
1278
35
}
1279
/* }}} */
1280
1281
ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec, ...) /* {{{ */
1282
365k
{
1283
365k
  va_list va;
1284
365k
  zend_result retval;
1285
365k
  int flags = 0;
1286
1287
365k
  va_start(va, type_spec);
1288
365k
  retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1289
365k
  va_end(va);
1290
1291
365k
  return retval;
1292
365k
}
1293
/* }}} */
1294
1295
ZEND_API zend_result zend_parse_method_parameters(uint32_t num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
1296
17
{
1297
17
  va_list va;
1298
17
  zend_result retval;
1299
17
  int flags = 0;
1300
17
  const char *p = type_spec;
1301
17
  zval **object;
1302
17
  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
17
  bool is_method = EG(current_execute_data)->func->common.scope != NULL;
1309
1310
17
  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
17
  } else {
1315
17
    p++;
1316
1317
17
    va_start(va, type_spec);
1318
1319
17
    object = va_arg(va, zval **);
1320
17
    ce = va_arg(va, zend_class_entry *);
1321
17
    *object = this_ptr;
1322
1323
17
    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
17
    retval = zend_parse_va_args(num_args, p, &va, flags);
1329
17
    va_end(va);
1330
17
  }
1331
17
  return retval;
1332
17
}
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
633
{
1397
633
  zend_class_mutable_data *mutable_data;
1398
1399
633
  ZEND_ASSERT(ZEND_MAP_PTR(class_type->mutable_data) != NULL);
1400
633
  ZEND_ASSERT(ZEND_MAP_PTR_GET_IMM(class_type->mutable_data) == NULL);
1401
1402
633
  mutable_data = zend_arena_alloc(&CG(arena), sizeof(zend_class_mutable_data));
1403
633
  memset(mutable_data, 0, sizeof(zend_class_mutable_data));
1404
633
  mutable_data->ce_flags = class_type->ce_flags;
1405
633
  ZEND_MAP_PTR_SET_IMM(class_type->mutable_data, mutable_data);
1406
1407
633
  return mutable_data;
1408
633
}
1409
/* }}} */
1410
1411
ZEND_API HashTable *zend_separate_class_constants_table(const zend_class_entry *class_type) /* {{{ */
1412
404
{
1413
404
  zend_class_mutable_data *mutable_data;
1414
404
  HashTable *constants_table;
1415
404
  zend_string *key;
1416
404
  zend_class_constant *new_c, *c;
1417
1418
404
  constants_table = zend_arena_alloc(&CG(arena), sizeof(HashTable));
1419
404
  zend_hash_init(constants_table, zend_hash_num_elements(&class_type->constants_table), NULL, NULL, 0);
1420
404
  zend_hash_extend(constants_table, zend_hash_num_elements(&class_type->constants_table), 0);
1421
1422
2.74k
  ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&class_type->constants_table, key, c) {
1423
2.74k
    if (c->ce == class_type) {
1424
880
      if (Z_TYPE(c->value) == IS_CONSTANT_AST || (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED)) {
1425
675
        new_c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
1426
675
        memcpy(new_c, c, sizeof(zend_class_constant));
1427
675
        c = new_c;
1428
675
      }
1429
880
      Z_TRY_ADDREF(c->value);
1430
880
    } else {
1431
86
      if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
1432
78
        c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(c->ce), key);
1433
78
        ZEND_ASSERT(c);
1434
78
      }
1435
86
    }
1436
2.74k
    _zend_hash_append_ptr(constants_table, key, c);
1437
966
  } ZEND_HASH_FOREACH_END();
1438
1439
404
  ZEND_ASSERT(ZEND_MAP_PTR(class_type->mutable_data) != NULL);
1440
1441
404
  mutable_data = ZEND_MAP_PTR_GET_IMM(class_type->mutable_data);
1442
404
  if (!mutable_data) {
1443
329
    mutable_data = zend_allocate_mutable_data(class_type);
1444
329
  }
1445
1446
404
  mutable_data->constants_table = constants_table;
1447
1448
404
  return constants_table;
1449
404
}
1450
1451
549
static zend_result update_property(zval *val, const zend_property_info *prop_info) {
1452
549
  if (ZEND_TYPE_IS_SET(prop_info->type)) {
1453
132
    zval tmp;
1454
1455
132
    ZVAL_COPY(&tmp, val);
1456
132
    if (UNEXPECTED(zval_update_constant_ex(&tmp, prop_info->ce) != SUCCESS)) {
1457
13
      zval_ptr_dtor(&tmp);
1458
13
      return FAILURE;
1459
13
    }
1460
119
    /* property initializers must always be evaluated with strict types */;
1461
119
    if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, /* strict */ 1))) {
1462
19
      zval_ptr_dtor(&tmp);
1463
19
      return FAILURE;
1464
19
    }
1465
100
    zval_ptr_dtor(val);
1466
100
    ZVAL_COPY_VALUE(val, &tmp);
1467
100
    return SUCCESS;
1468
119
  }
1469
417
  return zval_update_constant_ex(val, prop_info->ce);
1470
549
}
1471
1472
ZEND_API zend_result zend_update_class_constant(zend_class_constant *c, const zend_string *name, zend_class_entry *scope)
1473
2.12k
{
1474
2.12k
  ZEND_ASSERT(Z_TYPE(c->value) == IS_CONSTANT_AST);
1475
1476
2.12k
  if (EXPECTED(!ZEND_TYPE_IS_SET(c->type) || ZEND_TYPE_PURE_MASK(c->type) == MAY_BE_ANY)) {
1477
1.86k
    return zval_update_constant_ex(&c->value, scope);
1478
1.86k
  }
1479
1480
261
  zval tmp;
1481
1482
261
  ZVAL_COPY(&tmp, &c->value);
1483
261
  zend_result result = zval_update_constant_ex(&tmp, scope);
1484
261
  if (result == FAILURE) {
1485
16
    zval_ptr_dtor(&tmp);
1486
16
    return FAILURE;
1487
16
  }
1488
1489
245
  if (UNEXPECTED(!zend_verify_class_constant_type(c, name, &tmp))) {
1490
65
    zval_ptr_dtor(&tmp);
1491
65
    return FAILURE;
1492
65
  }
1493
1494
180
  zval_ptr_dtor(&c->value);
1495
180
  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
180
  ZEND_ASSERT(!EG(exception));
1500
180
  return SUCCESS;
1501
180
}
1502
1503
ZEND_API zend_result zend_update_class_constants(zend_class_entry *class_type) /* {{{ */
1504
952
{
1505
952
  zend_class_mutable_data *mutable_data = NULL;
1506
952
  zval *default_properties_table = NULL;
1507
952
  zval *static_members_table = NULL;
1508
952
  zend_class_constant *c;
1509
952
  zval *val;
1510
952
  uint32_t ce_flags;
1511
1512
952
  ce_flags = class_type->ce_flags;
1513
1514
952
  if (ce_flags & ZEND_ACC_CONSTANTS_UPDATED) {
1515
40
    return SUCCESS;
1516
40
  }
1517
1518
952
  bool uses_mutable_data = ZEND_MAP_PTR(class_type->mutable_data) != NULL;
1519
912
  if (uses_mutable_data) {
1520
540
    mutable_data = ZEND_MAP_PTR_GET_IMM(class_type->mutable_data);
1521
540
    if (mutable_data) {
1522
236
      ce_flags = mutable_data->ce_flags;
1523
236
      if (ce_flags & ZEND_ACC_CONSTANTS_UPDATED) {
1524
192
        return SUCCESS;
1525
192
      }
1526
304
    } else {
1527
304
      mutable_data = zend_allocate_mutable_data(class_type);
1528
304
    }
1529
540
  }
1530
1531
720
  if (class_type->parent) {
1532
58
    if (UNEXPECTED(zend_update_class_constants(class_type->parent) != SUCCESS)) {
1533
4
      return FAILURE;
1534
4
    }
1535
58
  }
1536
1537
716
  if (ce_flags & ZEND_ACC_HAS_AST_CONSTANTS) {
1538
430
    HashTable *constants_table;
1539
1540
430
    if (uses_mutable_data) {
1541
102
      constants_table = mutable_data->constants_table;
1542
102
      if (!constants_table) {
1543
75
        constants_table = zend_separate_class_constants_table(class_type);
1544
75
      }
1545
328
    } else {
1546
328
      constants_table = &class_type->constants_table;
1547
328
    }
1548
1549
430
    zend_string *name;
1550
2.66k
    ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(constants_table, name, val) {
1551
2.66k
      c = Z_PTR_P(val);
1552
2.66k
      if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
1553
673
        if (c->ce != class_type) {
1554
16
          Z_PTR_P(val) = c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(c->ce), name);
1555
16
          if (Z_TYPE(c->value) != IS_CONSTANT_AST) {
1556
8
            continue;
1557
8
          }
1558
16
        }
1559
1560
665
        val = &c->value;
1561
665
        if (UNEXPECTED(zend_update_class_constant(c, name, c->ce) != SUCCESS)) {
1562
36
          return FAILURE;
1563
36
        }
1564
665
      }
1565
2.66k
    } ZEND_HASH_FOREACH_END();
1566
430
  }
1567
1568
680
  if (class_type->default_static_members_count) {
1569
108
    static_members_table = CE_STATIC_MEMBERS(class_type);
1570
108
    if (!static_members_table) {
1571
106
      zend_class_init_statics(class_type);
1572
106
      static_members_table = CE_STATIC_MEMBERS(class_type);
1573
106
    }
1574
108
  }
1575
1576
680
  default_properties_table = class_type->default_properties_table;
1577
680
  if (uses_mutable_data && (ce_flags & ZEND_ACC_HAS_AST_PROPERTIES)) {
1578
190
    zval *src, *dst, *end;
1579
1580
190
    default_properties_table = mutable_data->default_properties_table;
1581
190
    if (!default_properties_table) {
1582
175
      default_properties_table = zend_arena_alloc(&CG(arena), sizeof(zval) * class_type->default_properties_count);
1583
175
      src = class_type->default_properties_table;
1584
175
      dst = default_properties_table;
1585
175
      end = dst + class_type->default_properties_count;
1586
286
      do {
1587
286
        ZVAL_COPY_PROP(dst, src);
1588
286
        src++;
1589
286
        dst++;
1590
286
      } while (dst != end);
1591
175
      mutable_data->default_properties_table = default_properties_table;
1592
175
    }
1593
190
  }
1594
1595
680
  if (ce_flags & (ZEND_ACC_HAS_AST_PROPERTIES|ZEND_ACC_HAS_AST_STATICS)) {
1596
298
    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
579
    for (uint32_t i = 0; i < class_type->default_properties_count; i++) {
1601
334
      prop_info = class_type->properties_info_table[i];
1602
334
      if (!prop_info) {
1603
7
        continue;
1604
7
      }
1605
1606
327
      val = &default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
1607
327
      if (Z_TYPE_P(val) == IS_CONSTANT_AST
1608
291
          && UNEXPECTED(update_property(val, prop_info) != SUCCESS)) {
1609
53
        return FAILURE;
1610
53
      }
1611
327
    }
1612
1613
245
    if (class_type->default_static_members_count) {
1614
708
      ZEND_HASH_MAP_FOREACH_PTR(&class_type->properties_info, prop_info) {
1615
708
        if (prop_info->flags & ZEND_ACC_STATIC) {
1616
260
          val = static_members_table + prop_info->offset;
1617
260
          if (Z_TYPE_P(val) == IS_CONSTANT_AST
1618
258
              && UNEXPECTED(update_property(val, prop_info) != SUCCESS)) {
1619
20
            return FAILURE;
1620
20
          }
1621
260
        }
1622
708
      } ZEND_HASH_FOREACH_END();
1623
82
    }
1624
245
  }
1625
1626
607
  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
85
      return FAILURE;
1629
85
    }
1630
303
  }
1631
1632
522
  ce_flags |= ZEND_ACC_CONSTANTS_UPDATED;
1633
522
  ce_flags &= ~ZEND_ACC_HAS_AST_CONSTANTS;
1634
522
  ce_flags &= ~ZEND_ACC_HAS_AST_PROPERTIES;
1635
522
  ce_flags &= ~ZEND_ACC_HAS_AST_STATICS;
1636
522
  if (uses_mutable_data) {
1637
259
    mutable_data->ce_flags = ce_flags;
1638
263
  } else {
1639
263
    class_type->ce_flags = ce_flags;
1640
263
  }
1641
1642
522
  return SUCCESS;
1643
607
}
1644
/* }}} */
1645
1646
static zend_always_inline void _object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1647
2.20M
{
1648
2.20M
  if (class_type->default_properties_count) {
1649
911k
    zval *src = CE_DEFAULT_PROPERTIES_TABLE(class_type);
1650
911k
    zval *dst = object->properties_table;
1651
911k
    zval *end = src + class_type->default_properties_count;
1652
1653
911k
    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.17M
      do {
1657
6.17M
        ZEND_ASSERT(!Z_REFCOUNTED_P(src));
1658
6.17M
        ZVAL_COPY_VALUE_PROP(dst, src);
1659
6.17M
        src++;
1660
6.17M
        dst++;
1661
6.17M
      } while (src != end);
1662
890k
    } else {
1663
30.4k
      do {
1664
30.4k
        ZVAL_COPY_PROP(dst, src);
1665
30.4k
        src++;
1666
30.4k
        dst++;
1667
30.4k
      } while (src != end);
1668
20.1k
    }
1669
911k
  }
1670
2.20M
}
1671
/* }}} */
1672
1673
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1674
2.09M
{
1675
2.09M
  object->properties = NULL;
1676
2.09M
  _object_properties_init(object, class_type);
1677
2.09M
}
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
37.3k
{
1714
37.3k
  zval *prop, tmp;
1715
37.3k
  zend_string *key;
1716
37.3k
  zend_long h;
1717
37.3k
  const zend_property_info *property_info;
1718
1719
165k
  ZEND_HASH_FOREACH_KEY_VAL(properties, h, key, prop) {
1720
165k
    if (key) {
1721
5.68k
      if (ZSTR_VAL(key)[0] == '\0') {
1722
3.29k
        const char *class_name, *prop_name;
1723
3.29k
        size_t prop_name_len;
1724
3.29k
        if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) {
1725
3.06k
          zend_string *pname = zend_string_init(prop_name, prop_name_len, 0);
1726
3.06k
          const zend_class_entry *prev_scope = EG(fake_scope);
1727
3.06k
          if (class_name && class_name[0] != '*') {
1728
2.93k
            zend_string *cname = zend_string_init(class_name, strlen(class_name), 0);
1729
2.93k
            EG(fake_scope) = zend_lookup_class(cname);
1730
2.93k
            zend_string_release_ex(cname, 0);
1731
2.93k
          }
1732
3.06k
          property_info = zend_get_property_info(object->ce, pname, 1);
1733
3.06k
          zend_string_release_ex(pname, 0);
1734
3.06k
          EG(fake_scope) = prev_scope;
1735
3.06k
        } else {
1736
228
          property_info = ZEND_WRONG_PROPERTY_INFO;
1737
228
        }
1738
3.29k
      } else {
1739
2.38k
        property_info = zend_get_property_info(object->ce, key, 1);
1740
2.38k
      }
1741
5.68k
      if (property_info != ZEND_WRONG_PROPERTY_INFO &&
1742
4.83k
        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
5.68k
      } else {
1761
5.68k
        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
5.68k
        } else if (!(object->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES)) {
1766
5.68k
          zend_error(E_DEPRECATED, "Creation of dynamic property %s::$%s is deprecated",
1767
5.68k
            ZSTR_VAL(object->ce->name), property_info != ZEND_WRONG_PROPERTY_INFO ? zend_get_unmangled_property_name(key): "");
1768
5.68k
        }
1769
1770
5.68k
        prop = zend_hash_update(zend_std_get_properties_ex(object), key, prop);
1771
5.68k
        zval_add_ref(prop);
1772
5.68k
      }
1773
58.5k
    } else {
1774
58.5k
      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
58.5k
      } else if (!(object->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES)) {
1778
58.5k
        zend_error(E_DEPRECATED, "Creation of dynamic property %s::$" ZEND_LONG_FMT " is deprecated",
1779
58.5k
          ZSTR_VAL(object->ce->name), h);
1780
58.5k
      }
1781
1782
58.5k
      prop = zend_hash_index_update(zend_std_get_properties_ex(object), h, prop);
1783
58.5k
      zval_add_ref(prop);
1784
58.5k
    }
1785
165k
  } ZEND_HASH_FOREACH_END();
1786
37.3k
}
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.22M
{
1795
2.22M
  if (UNEXPECTED(class_type->ce_flags & ZEND_ACC_UNINSTANTIABLE)) {
1796
1.75k
    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.74k
    } 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.73k
    } 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.69k
    } else {
1803
1.69k
      ZEND_ASSERT(class_type->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS));
1804
1.69k
      zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name));
1805
1.69k
    }
1806
1.75k
    ZVAL_NULL(arg);
1807
1.75k
    Z_OBJ_P(arg) = NULL;
1808
1.75k
    return FAILURE;
1809
1.75k
  }
1810
1811
2.22M
  if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
1812
288
    if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
1813
78
      ZVAL_NULL(arg);
1814
78
      Z_OBJ_P(arg) = NULL;
1815
78
      return FAILURE;
1816
78
    }
1817
288
  }
1818
1819
2.22M
  if (class_type->create_object == NULL) {
1820
102k
    zend_object *obj = zend_objects_new(class_type);
1821
1822
102k
    ZVAL_OBJ(arg, obj);
1823
102k
    if (properties) {
1824
0
      object_properties_init_ex(obj, properties);
1825
102k
    } else {
1826
102k
      _object_properties_init(obj, class_type);
1827
102k
    }
1828
2.11M
  } else {
1829
2.11M
    ZVAL_OBJ(arg, class_type->create_object(class_type));
1830
2.11M
  }
1831
2.22M
  return SUCCESS;
1832
2.22M
}
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.22M
{
1843
2.22M
  return _object_and_properties_init(arg, class_type, NULL);
1844
2.22M
}
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
948
{
1849
948
  zend_result status = _object_and_properties_init(arg, class_type, NULL);
1850
948
  if (UNEXPECTED(status == FAILURE)) {
1851
0
    ZVAL_UNDEF(arg);
1852
0
    return FAILURE;
1853
0
  }
1854
948
  zend_object *obj = Z_OBJ_P(arg);
1855
948
  zend_function *constructor = obj->handlers->get_constructor(obj);
1856
948
  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
90
    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
85
    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
78
    } else {
1885
78
      return SUCCESS;
1886
78
    }
1887
85
  }
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
858
  zval retval;
1891
858
  zend_call_known_function(
1892
858
    constructor,
1893
858
    obj,
1894
858
    class_type,
1895
858
    &retval,
1896
858
    param_count,
1897
858
    params,
1898
858
    named_params
1899
858
  );
1900
858
  if (Z_TYPE(retval) == IS_UNDEF) {
1901
    /* Do not call destructor, free object, and set arg to IS_UNDEF */
1902
36
    zend_object_store_ctor_failed(obj);
1903
36
    zval_ptr_dtor(arg);
1904
36
    ZVAL_UNDEF(arg);
1905
36
    return FAILURE;
1906
822
  } else {
1907
    /* Unlikely, but user constructors may return any value they want */
1908
822
    zval_ptr_dtor(&retval);
1909
822
    return SUCCESS;
1910
822
  }
1911
858
}
1912
/* }}} */
1913
1914
ZEND_API void object_init(zval *arg) /* {{{ */
1915
1.75M
{
1916
1.75M
  ZVAL_OBJ(arg, zend_objects_new(zend_standard_class_def));
1917
1.75M
}
1918
/* }}} */
1919
1920
ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
1921
9.23k
{
1922
9.23k
  zval tmp;
1923
1924
9.23k
  ZVAL_LONG(&tmp, n);
1925
9.23k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1926
9.23k
}
1927
/* }}} */
1928
1929
ZEND_API void add_assoc_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
1930
10.4k
{
1931
10.4k
  zval tmp;
1932
1933
10.4k
  ZVAL_NULL(&tmp);
1934
10.4k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1935
10.4k
}
1936
/* }}} */
1937
1938
ZEND_API void add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, bool b) /* {{{ */
1939
1.19k
{
1940
1.19k
  zval tmp;
1941
1942
1.19k
  ZVAL_BOOL(&tmp, b);
1943
1.19k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1944
1.19k
}
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
2.22k
{
1958
2.22k
  zval tmp;
1959
1960
2.22k
  ZVAL_DOUBLE(&tmp, d);
1961
2.22k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1962
2.22k
}
1963
/* }}} */
1964
1965
ZEND_API void add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
1966
23
{
1967
23
  zval tmp;
1968
1969
23
  ZVAL_STR(&tmp, str);
1970
23
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1971
23
}
1972
/* }}} */
1973
1974
ZEND_API void add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
1975
6.01k
{
1976
6.01k
  zval tmp;
1977
1978
6.01k
  ZVAL_STRING(&tmp, str);
1979
6.01k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1980
6.01k
}
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
23.6k
{
1985
23.6k
  zval tmp;
1986
1987
23.6k
  ZVAL_STRINGL(&tmp, str, length);
1988
23.6k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1989
23.6k
}
1990
/* }}} */
1991
1992
ZEND_API void add_assoc_array_ex(zval *arg, const char *key, size_t key_len, zend_array *arr) /* {{{ */
1993
0
{
1994
0
  zval tmp;
1995
1996
0
  ZVAL_ARR(&tmp, arr);
1997
0
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1998
0
}
1999
/* }}} */
2000
2001
ZEND_API void add_assoc_object_ex(zval *arg, const char *key, size_t key_len, zend_object *obj) /* {{{ */
2002
264
{
2003
264
  zval tmp;
2004
2005
264
  ZVAL_OBJ(&tmp, obj);
2006
264
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
2007
264
}
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
22.5k
{
2021
22.5k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value);
2022
22.5k
}
2023
/* }}} */
2024
2025
ZEND_API void add_index_long(zval *arg, zend_ulong index, zend_long n) /* {{{ */
2026
3.76M
{
2027
3.76M
  zval tmp;
2028
2029
3.76M
  ZVAL_LONG(&tmp, n);
2030
3.76M
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2031
3.76M
}
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
406k
{
2063
406k
  zval tmp;
2064
2065
406k
  ZVAL_DOUBLE(&tmp, d);
2066
406k
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2067
406k
}
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
627k
{
2081
627k
  zval tmp;
2082
2083
627k
  ZVAL_STRING(&tmp, str);
2084
627k
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2085
627k
}
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
42.8k
{
2171
42.8k
  zval tmp;
2172
2173
42.8k
  ZVAL_STR(&tmp, str);
2174
42.8k
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2175
42.8k
}
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
4.79k
{
2225
4.79k
  zval *result;
2226
2227
4.79k
  switch (Z_TYPE_P(key)) {
2228
4.46k
    case IS_STRING:
2229
4.46k
      result = zend_symtable_update(ht, Z_STR_P(key), value);
2230
4.46k
      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
6
    case IS_FALSE:
2236
6
      result = zend_hash_index_update(ht, 0, value);
2237
6
      break;
2238
5
    case IS_TRUE:
2239
5
      result = zend_hash_index_update(ht, 1, value);
2240
5
      break;
2241
301
    case IS_LONG:
2242
301
      result = zend_hash_index_update(ht, Z_LVAL_P(key), value);
2243
301
      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
4.79k
  }
2258
2259
4.79k
  if (result) {
2260
4.79k
    Z_TRY_ADDREF_P(result);
2261
4.79k
    return SUCCESS;
2262
4.79k
  } else {
2263
8
    return FAILURE;
2264
8
  }
2265
4.79k
}
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
4.99k
{
2288
4.99k
  zval tmp;
2289
2290
4.99k
  ZVAL_NULL(&tmp);
2291
4.99k
  add_property_zval_ex(arg, key, key_len, &tmp);
2292
4.99k
}
2293
/* }}} */
2294
2295
ZEND_API void add_property_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
2296
155
{
2297
155
  zval tmp;
2298
2299
155
  ZVAL_RES(&tmp, r);
2300
155
  add_property_zval_ex(arg, key, key_len, &tmp);
2301
155
  zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2302
155
}
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
68
{
2326
68
  zval tmp;
2327
2328
68
  ZVAL_STRING(&tmp, str);
2329
68
  add_property_zval_ex(arg, key, key_len, &tmp);
2330
68
  zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2331
68
}
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.22k
{
2376
5.22k
  zend_string *str;
2377
2378
5.22k
  str = zend_string_init(key, key_len, 0);
2379
5.22k
  Z_OBJ_HANDLER_P(arg, write_property)(Z_OBJ_P(arg), str, value, NULL);
2380
5.22k
  zend_string_release_ex(str, 0);
2381
5.22k
}
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
13.9k
{
2658
13.9k
  if (fptr->common.num_args != num_args) {
2659
42
    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
6
      zend_error(error_type, "Method %s::%s() must take exactly 1 argument",
2664
6
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2665
6
    } 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
42
    return;
2670
42
  }
2671
19.1k
  for (uint32_t i = 0; i < num_args; i++) {
2672
5.29k
    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.29k
  }
2678
13.8k
}
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.67k
{
2682
4.67k
    if (
2683
4.67k
      ZEND_TYPE_IS_SET(fptr->common.arg_info[arg_num].type)
2684
1.55k
       && !(ZEND_TYPE_FULL_MASK(fptr->common.arg_info[arg_num].type) & arg_type)
2685
4.67k
    ) {
2686
58
      zend_error(error_type, "%s::%s(): Parameter #%d ($%s) must be of type %s when declared",
2687
58
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2688
58
        arg_num + 1, ZSTR_VAL(fptr->common.arg_info[arg_num].name),
2689
58
        ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(arg_type))));
2690
58
    }
2691
4.67k
}
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
9.54k
{
2695
9.54k
  if (return_type == MAY_BE_VOID) {
2696
1.75k
    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.75k
  }
2700
2701
9.53k
  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.35k
    return;
2704
1.35k
  }
2705
2706
8.18k
  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
20
    return;
2709
20
  }
2710
2711
8.18k
  bool is_complex_type = ZEND_TYPE_IS_COMPLEX(fptr->common.arg_info[-1].type);
2712
8.16k
  uint32_t extra_types = ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & ~return_type;
2713
8.16k
  if (extra_types & MAY_BE_STATIC) {
2714
20
    extra_types &= ~MAY_BE_STATIC;
2715
20
    is_complex_type = true;
2716
20
  }
2717
2718
8.16k
  if (extra_types || (is_complex_type && return_type != MAY_BE_OBJECT)) {
2719
58
    zend_error(error_type, "%s::%s(): Return type must be %s when declared",
2720
58
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2721
58
      ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(return_type))));
2722
58
  }
2723
8.16k
}
2724
2725
static void zend_check_magic_method_non_static(
2726
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2727
18.2k
{
2728
18.2k
  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
18.2k
}
2733
2734
static void zend_check_magic_method_static(
2735
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2736
610
{
2737
610
  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
610
}
2742
2743
static void zend_check_magic_method_public(
2744
    const zend_class_entry *ce, const zend_function *fptr)
2745
11.4k
{
2746
  // TODO: Remove this warning after adding proper visibility handling.
2747
11.4k
  if (!(fptr->common.fn_flags & ZEND_ACC_PUBLIC)) {
2748
5.49k
    zend_error(E_WARNING, "The magic method %s::%s() must have public visibility",
2749
5.49k
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2750
5.49k
  }
2751
11.4k
}
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
7.00k
{
2756
7.00k
  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
6.99k
  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
6.99k
}
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
75.4k
{
2768
75.4k
  if (ZSTR_VAL(lcname)[0] != '_'
2769
38.1k
   || ZSTR_VAL(lcname)[1] != '_') {
2770
38.1k
    return;
2771
38.1k
  }
2772
2773
37.2k
  if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2774
4.77k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2775
4.77k
    zend_check_magic_method_no_return_type(ce, fptr, error_type);
2776
32.5k
  } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2777
2.25k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2778
2.25k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2779
2.25k
    zend_check_magic_method_no_return_type(ce, fptr, error_type);
2780
30.2k
  } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2781
427
    zend_check_magic_method_args(0, ce, fptr, error_type);
2782
427
    zend_check_magic_method_non_static(ce, fptr, error_type);
2783
427
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2784
29.8k
  } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2785
955
    zend_check_magic_method_args(1, ce, fptr, error_type);
2786
955
    zend_check_magic_method_non_static(ce, fptr, error_type);
2787
955
    zend_check_magic_method_public(ce, fptr);
2788
955
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2789
28.8k
  } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2790
572
    zend_check_magic_method_args(2, ce, fptr, error_type);
2791
572
    zend_check_magic_method_non_static(ce, fptr, error_type);
2792
572
    zend_check_magic_method_public(ce, fptr);
2793
572
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2794
572
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2795
28.3k
  } 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
28.1k
  } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2802
268
    zend_check_magic_method_args(1, ce, fptr, error_type);
2803
268
    zend_check_magic_method_non_static(ce, fptr, error_type);
2804
268
    zend_check_magic_method_public(ce, fptr);
2805
268
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2806
268
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_BOOL);
2807
27.8k
  } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2808
649
    zend_check_magic_method_args(2, ce, fptr, error_type);
2809
649
    zend_check_magic_method_non_static(ce, fptr, error_type);
2810
649
    zend_check_magic_method_public(ce, fptr);
2811
649
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2812
649
    zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2813
27.2k
  } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
2814
449
    zend_check_magic_method_args(2, ce, fptr, error_type);
2815
449
    zend_check_magic_method_static(ce, fptr, error_type);
2816
449
    zend_check_magic_method_public(ce, fptr);
2817
449
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2818
449
    zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2819
26.7k
  } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
2820
6.24k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2821
6.24k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2822
6.24k
    zend_check_magic_method_public(ce, fptr);
2823
6.24k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_STRING);
2824
20.5k
  } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
2825
411
    zend_check_magic_method_args(0, ce, fptr, error_type);
2826
411
    zend_check_magic_method_non_static(ce, fptr, error_type);
2827
411
    zend_check_magic_method_public(ce, fptr);
2828
411
    zend_check_magic_method_return_type(ce, fptr, error_type, (MAY_BE_ARRAY | MAY_BE_NULL));
2829
20.0k
  } else if (zend_string_equals_literal(lcname, "__serialize")) {
2830
581
    zend_check_magic_method_args(0, ce, fptr, error_type);
2831
581
    zend_check_magic_method_non_static(ce, fptr, error_type);
2832
581
    zend_check_magic_method_public(ce, fptr);
2833
581
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2834
19.5k
  } else if (zend_string_equals_literal(lcname, "__unserialize")) {
2835
395
    zend_check_magic_method_args(1, ce, fptr, error_type);
2836
395
    zend_check_magic_method_non_static(ce, fptr, error_type);
2837
395
    zend_check_magic_method_public(ce, fptr);
2838
395
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2839
395
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2840
19.1k
  } else if (zend_string_equals_literal(lcname, "__set_state")) {
2841
161
    zend_check_magic_method_args(1, ce, fptr, error_type);
2842
161
    zend_check_magic_method_static(ce, fptr, error_type);
2843
161
    zend_check_magic_method_public(ce, fptr);
2844
161
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2845
161
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_OBJECT);
2846
18.9k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) {
2847
226
    zend_check_magic_method_non_static(ce, fptr, error_type);
2848
226
    zend_check_magic_method_public(ce, fptr);
2849
18.7k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_SLEEP))) {
2850
159
    zend_check_magic_method_args(0, ce, fptr, error_type);
2851
159
    zend_check_magic_method_non_static(ce, fptr, error_type);
2852
159
    zend_check_magic_method_public(ce, fptr);
2853
159
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2854
18.5k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_WAKEUP))) {
2855
207
    zend_check_magic_method_args(0, ce, fptr, error_type);
2856
207
    zend_check_magic_method_non_static(ce, fptr, error_type);
2857
207
    zend_check_magic_method_public(ce, fptr);
2858
207
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2859
207
  }
2860
37.2k
}
2861
/* }}} */
2862
2863
ZEND_API void zend_add_magic_method(zend_class_entry *ce, zend_function *fptr, const zend_string *lcname)
2864
77.1k
{
2865
77.1k
  if (ZSTR_VAL(lcname)[0] != '_' || ZSTR_VAL(lcname)[1] != '_') {
2866
    /* pass */
2867
39.6k
  } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2868
449
    ce->clone = fptr;
2869
37.0k
  } else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2870
4.88k
    ce->constructor = fptr;
2871
4.88k
    ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
2872
32.2k
  } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2873
2.26k
    ce->destructor = fptr;
2874
29.9k
  } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2875
971
    ce->__get = fptr;
2876
971
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2877
28.9k
  } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2878
576
    ce->__set = fptr;
2879
576
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2880
28.3k
  } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2881
652
    ce->__call = fptr;
2882
27.7k
  } 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
27.5k
  } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2886
276
    ce->__isset = fptr;
2887
276
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2888
27.2k
  } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
2889
462
    ce->__callstatic = fptr;
2890
26.8k
  } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
2891
6.27k
    ce->__tostring = fptr;
2892
20.5k
  } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
2893
411
    ce->__debugInfo = fptr;
2894
411
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2895
20.1k
  } else if (zend_string_equals_literal(lcname, "__serialize")) {
2896
581
    ce->__serialize = fptr;
2897
19.5k
  } else if (zend_string_equals_literal(lcname, "__unserialize")) {
2898
395
    ce->__unserialize = fptr;
2899
395
  }
2900
77.1k
}
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.3k
{
2932
55.3k
  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.3k
  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.3k
}
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.3k
{
2989
55.3k
  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.18k
      new_arg_info->default_value = zend_string_init_interned(arg_info->default_value, strlen(arg_info->default_value), persistent);
2993
19.3k
    } else {
2994
19.3k
      new_arg_info->default_value = NULL;
2995
19.3k
    }
2996
28.4k
  } else {
2997
26.8k
    new_arg_info->name = NULL;
2998
26.8k
    new_arg_info->default_value = NULL;
2999
26.8k
  }
3000
55.3k
  new_arg_info->type = arg_info->type;
3001
55.3k
  zend_convert_internal_arg_info_type(&new_arg_info->type, persistent);
3002
55.3k
}
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.8k
  while (ptr->fname) {
3039
26.8k
    fname_len = strlen(ptr->fname);
3040
26.8k
    internal_function->handler = ptr->handler;
3041
26.8k
    internal_function->doc_comment = ptr->doc_comment ? zend_string_init_interned(ptr->doc_comment, strlen(ptr->doc_comment), 1) : NULL;
3042
26.8k
    internal_function->function_name = zend_string_init_interned(ptr->fname, fname_len, 1);
3043
26.8k
    internal_function->scope = scope;
3044
26.8k
    internal_function->prototype = NULL;
3045
26.8k
    internal_function->prop_info = NULL;
3046
26.8k
    internal_function->attributes = NULL;
3047
26.8k
    internal_function->frameless_function_infos = ptr->frameless_function_infos;
3048
26.8k
    internal_function->fn_flags2 = 0;
3049
26.8k
    if (EG(active)) { // at run-time: this ought to only happen if registered with dl() or somehow temporarily at runtime
3050
0
      ZEND_MAP_PTR_INIT(internal_function->run_time_cache, zend_arena_calloc(&CG(arena), 1, zend_internal_run_time_cache_reserved_size()));
3051
26.8k
    } else {
3052
#ifdef ZTS
3053
      ZEND_MAP_PTR_NEW_STATIC(internal_function->run_time_cache);
3054
#else
3055
26.8k
      ZEND_MAP_PTR_INIT(internal_function->run_time_cache, NULL);
3056
26.8k
#endif
3057
26.8k
    }
3058
26.8k
    if (ptr->flags) {
3059
18.2k
      if (!(ptr->flags & ZEND_ACC_PPP_MASK)) {
3060
2.96k
        if (ptr->flags != ZEND_ACC_DEPRECATED && scope) {
3061
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);
3062
0
        }
3063
2.96k
        internal_function->fn_flags = ZEND_ACC_PUBLIC | ptr->flags;
3064
15.2k
      } else {
3065
15.2k
        internal_function->fn_flags = ptr->flags;
3066
15.2k
      }
3067
18.2k
    } else {
3068
8.56k
      internal_function->fn_flags = ZEND_ACC_PUBLIC;
3069
8.56k
    }
3070
3071
26.8k
    if (ptr->arg_info) {
3072
26.8k
      zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info;
3073
26.8k
      internal_arg_info = ptr->arg_info+1;
3074
26.8k
      internal_function->num_args = ptr->num_args;
3075
      /* Currently you cannot denote that the function can accept less arguments than num_args */
3076
26.8k
      if (info->required_num_args == (uintptr_t)-1) {
3077
0
        internal_function->required_num_args = ptr->num_args;
3078
26.8k
      } else {
3079
26.8k
        internal_function->required_num_args = info->required_num_args;
3080
26.8k
      }
3081
26.8k
      if (ZEND_ARG_SEND_MODE(info)) {
3082
0
        internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE;
3083
0
      }
3084
26.8k
      if (ZEND_ARG_IS_VARIADIC(&ptr->arg_info[ptr->num_args])) {
3085
736
        internal_function->fn_flags |= ZEND_ACC_VARIADIC;
3086
        /* Don't count the variadic argument */
3087
736
        internal_function->num_args--;
3088
736
      }
3089
26.8k
      if (ZEND_TYPE_IS_SET(info->type)) {
3090
25.2k
        if (ZEND_TYPE_HAS_NAME(info->type)) {
3091
0
          const char *type_name = ZEND_TYPE_LITERAL_NAME(info->type);
3092
0
          if (!scope && (!strcasecmp(type_name, "self") || !strcasecmp(type_name, "parent"))) {
3093
0
            zend_error_noreturn(E_CORE_ERROR, "Cannot declare a return type of %s outside of a class scope", type_name);
3094
0
          }
3095
0
        }
3096
3097
25.2k
        internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
3098
25.2k
      }
3099
26.8k
    } else {
3100
0
      zend_error(E_CORE_WARNING, "Missing arginfo for %s%s%s()",
3101
0
         scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3102
3103
0
      internal_arg_info = NULL;
3104
0
      internal_function->num_args = 0;
3105
0
      internal_function->required_num_args = 0;
3106
0
    }
3107
3108
    /* If not specified, add __toString() return type for compatibility with Stringable
3109
     * interface. */
3110
26.8k
    if (scope && zend_string_equals_literal_ci(internal_function->function_name, "__tostring") &&
3111
288
        !(internal_function->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
3112
0
      zend_error(E_CORE_WARNING, "%s::__toString() implemented without string return type",
3113
0
        ZSTR_VAL(scope->name));
3114
0
      internal_arg_info = (zend_internal_arg_info *) arg_info_toString + 1;
3115
0
      internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
3116
0
      internal_function->num_args = internal_function->required_num_args = 0;
3117
0
    }
3118
3119
26.8k
    if (ptr->flags & ZEND_ACC_ABSTRACT) {
3120
720
      if (scope) {
3121
        /* This is a class that must be abstract itself. Here we set the check info. */
3122
720
        scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
3123
720
        if (!(scope->ce_flags & ZEND_ACC_INTERFACE)) {
3124
          /* Since the class is not an interface it needs to be declared as a abstract class. */
3125
          /* Since here we are handling internal functions only we can add the keyword flag. */
3126
          /* This time we set the flag for the keyword 'abstract'. */
3127
32
          scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
3128
32
        }
3129
720
      }
3130
720
      if ((ptr->flags & ZEND_ACC_STATIC) && (!scope || !(scope->ce_flags & ZEND_ACC_INTERFACE))) {
3131
0
        zend_error(error_type, "Static function %s%s%s() cannot be abstract", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3132
0
      }
3133
26.0k
    } else {
3134
26.0k
      if (scope && (scope->ce_flags & ZEND_ACC_INTERFACE)) {
3135
0
        zend_error(error_type, "Interface %s cannot contain non abstract method %s()", ZSTR_VAL(scope->name), ptr->fname);
3136
0
        return FAILURE;
3137
0
      }
3138
26.0k
      if (!internal_function->handler) {
3139
0
        zend_error(error_type, "Method %s%s%s() cannot be a NULL function", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3140
0
        zend_unregister_functions(functions, count, target_function_table);
3141
0
        return FAILURE;
3142
0
      }
3143
26.0k
    }
3144
26.8k
    lowercase_name = zend_string_tolower_ex(internal_function->function_name, type == MODULE_PERSISTENT);
3145
26.8k
    lowercase_name = zend_new_interned_string(lowercase_name);
3146
26.8k
    reg_function = malloc(sizeof(zend_internal_function));
3147
26.8k
    memcpy(reg_function, &function, sizeof(zend_internal_function));
3148
26.8k
    if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) {
3149
0
      unload=1;
3150
0
      free(reg_function);
3151
0
      zend_string_release(lowercase_name);
3152
0
      break;
3153
0
    }
3154
26.8k
    if (reg_function->frameless_function_infos) {
3155
320
      const zend_frameless_function_info *flf_info = reg_function->frameless_function_infos;
3156
784
      while (flf_info->handler) {
3157
464
        if (zend_flf_count == zend_flf_capacity) {
3158
48
          if (!zend_flf_capacity) {
3159
16
            zend_flf_capacity = 8;
3160
32
          } else {
3161
32
            zend_flf_capacity *= 2;
3162
32
          }
3163
          /* +1 for NULL terminator */
3164
48
          zend_flf_handlers = realloc(zend_flf_handlers, (zend_flf_capacity + 1) * sizeof(void *));
3165
48
          zend_flf_functions = realloc(zend_flf_functions, (zend_flf_capacity + 1) * sizeof(zend_function *));
3166
48
        }
3167
464
        zend_flf_handlers[zend_flf_count] = flf_info->handler;
3168
464
        zend_flf_functions[zend_flf_count] = (zend_function *)reg_function;
3169
464
        zend_flf_count++;
3170
464
        flf_info++;
3171
464
      }
3172
320
      zend_flf_handlers[zend_flf_count] = NULL;
3173
320
      zend_flf_functions[zend_flf_count] = NULL;
3174
320
    }
3175
3176
    /* Get parameter count including variadic parameter. */
3177
26.8k
    uint32_t num_args = reg_function->num_args;
3178
26.8k
    if (reg_function->fn_flags & ZEND_ACC_VARIADIC) {
3179
736
      num_args++;
3180
736
    }
3181
3182
    /* If types of arguments have to be checked */
3183
26.8k
    if (internal_arg_info && num_args) {
3184
15.6k
      uint32_t i;
3185
44.1k
      for (i = 0; i < num_args; i++) {
3186
28.4k
        const zend_internal_arg_info *arg_info = &internal_arg_info[i];
3187
28.4k
        ZEND_ASSERT(arg_info->name && "Parameter must have a name");
3188
28.4k
        if (ZEND_TYPE_IS_SET(arg_info->type)) {
3189
25.4k
            reg_function->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
3190
25.4k
        }
3191
28.4k
#if ZEND_DEBUG
3192
49.2k
        for (uint32_t j = 0; j < i; j++) {
3193
20.7k
          if (!strcmp(arg_info->name, internal_arg_info[j].name)) {
3194
0
            zend_error_noreturn(E_CORE_ERROR,
3195
0
              "Duplicate parameter name $%s for function %s%s%s()", arg_info->name,
3196
0
              scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3197
0
          }
3198
20.7k
        }
3199
28.4k
#endif
3200
28.4k
      }
3201
15.6k
    }
3202
3203
    /* Convert zend_internal_arg_info to zend_arg_info */
3204
26.8k
    if (internal_arg_info) {
3205
26.8k
      uint32_t i;
3206
26.8k
      const zend_internal_arg_info *arg_info = internal_arg_info - 1;
3207
26.8k
      zend_arg_info *new_arg_info;
3208
3209
      /* Treat return type as an extra argument */
3210
26.8k
      num_args++;
3211
26.8k
      new_arg_info = malloc(sizeof(zend_arg_info) * num_args);
3212
26.8k
      reg_function->arg_info = new_arg_info + 1;
3213
82.0k
      for (i = 0; i < num_args; i++) {
3214
55.2k
        zend_convert_internal_arg_info(&new_arg_info[i], &arg_info[i],
3215
55.2k
            i == 0, true);
3216
55.2k
      }
3217
26.8k
    }
3218
3219
26.8k
    zend_set_function_arg_flags((zend_function*)reg_function);
3220
3221
26.8k
    if (scope) {
3222
15.2k
      zend_check_magic_method_implementation(
3223
15.2k
        scope, (zend_function *)reg_function, lowercase_name, E_CORE_ERROR);
3224
15.2k
      zend_add_magic_method(scope, (zend_function *)reg_function, lowercase_name);
3225
15.2k
    }
3226
26.8k
    ptr++;
3227
26.8k
    count++;
3228
26.8k
    zend_string_release(lowercase_name);
3229
26.8k
  }
3230
2.01k
  if (unload) { /* before unloading, display all remaining bad function in the module */
3231
0
    while (ptr->fname) {
3232
0
      fname_len = strlen(ptr->fname);
3233
0
      lowercase_name = zend_string_alloc(fname_len, 0);
3234
0
      zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
3235
0
      if (zend_hash_exists(target_function_table, lowercase_name)) {
3236
0
        zend_error(error_type, "Function registration failed - duplicate name - %s%s%s", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3237
0
      }
3238
0
      zend_string_efree(lowercase_name);
3239
0
      ptr++;
3240
0
    }
3241
0
    zend_unregister_functions(functions, count, target_function_table);
3242
0
    return FAILURE;
3243
0
  }
3244
2.01k
  return SUCCESS;
3245
2.01k
}
3246
/* }}} */
3247
3248
/* count=-1 means erase all functions, otherwise,
3249
 * erase the first count functions
3250
 */
3251
ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table) /* {{{ */
3252
0
{
3253
0
  const zend_function_entry *ptr = functions;
3254
0
  int i=0;
3255
0
  HashTable *target_function_table = function_table;
3256
0
  zend_string *lowercase_name;
3257
0
  size_t fname_len;
3258
3259
0
  if (!target_function_table) {
3260
0
    target_function_table = CG(function_table);
3261
0
  }
3262
0
  while (ptr->fname) {
3263
0
    if (count!=-1 && i>=count) {
3264
0
      break;
3265
0
    }
3266
0
    fname_len = strlen(ptr->fname);
3267
0
    lowercase_name = zend_string_alloc(fname_len, 0);
3268
0
    zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
3269
0
    zend_hash_del(target_function_table, lowercase_name);
3270
0
    zend_string_efree(lowercase_name);
3271
0
    ptr++;
3272
0
    i++;
3273
0
  }
3274
0
}
3275
/* }}} */
3276
3277
ZEND_API zend_result zend_startup_module(zend_module_entry *module) /* {{{ */
3278
0
{
3279
0
  if ((module = zend_register_internal_module(module)) != NULL && zend_startup_module_ex(module) == SUCCESS) {
3280
0
    return SUCCESS;
3281
0
  }
3282
0
  return FAILURE;
3283
0
}
3284
/* }}} */
3285
3286
ZEND_API zend_result zend_get_module_started(const char *module_name) /* {{{ */
3287
0
{
3288
0
  zend_module_entry *module;
3289
3290
0
  module = zend_hash_str_find_ptr(&module_registry, module_name, strlen(module_name));
3291
0
  return (module && module->module_started) ? SUCCESS : FAILURE;
3292
0
}
3293
/* }}} */
3294
3295
static void clean_module_classes(int module_number) /* {{{ */
3296
0
{
3297
  /* Child classes may reuse structures from parent classes, so destroy in reverse order. */
3298
0
  Bucket *bucket;
3299
0
  ZEND_HASH_REVERSE_FOREACH_BUCKET(EG(class_table), bucket) {
3300
0
    const zend_class_entry *ce = Z_CE(bucket->val);
3301
0
    if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->module_number == module_number) {
3302
0
      zend_hash_del_bucket(EG(class_table), bucket);
3303
0
    }
3304
0
  } ZEND_HASH_FOREACH_END();
3305
3306
0
}
3307
/* }}} */
3308
3309
static int clean_module_function(zval *el, void *arg) /* {{{ */
3310
0
{
3311
0
  const zend_function *fe = (zend_function *) Z_PTR_P(el);
3312
0
  const zend_module_entry *module = arg;
3313
0
  if (fe->common.type == ZEND_INTERNAL_FUNCTION && fe->internal_function.module == module) {
3314
0
    return ZEND_HASH_APPLY_REMOVE;
3315
0
  } else {
3316
0
    return ZEND_HASH_APPLY_KEEP;
3317
0
  }
3318
0
}
3319
/* }}} */
3320
3321
static void clean_module_functions(zend_module_entry *module) /* {{{ */
3322
0
{
3323
0
  zend_hash_apply_with_argument(CG(function_table), clean_module_function, module);
3324
0
}
3325
/* }}} */
3326
3327
void module_destructor(zend_module_entry *module) /* {{{ */
3328
0
{
3329
#if ZEND_RC_DEBUG
3330
  bool orig_rc_debug = zend_rc_debug;
3331
#endif
3332
3333
0
  if (module->type == MODULE_TEMPORARY) {
3334
#if ZEND_RC_DEBUG
3335
    /* FIXME: Loading extensions during the request breaks some invariants.
3336
     * In particular, it will create persistent interned strings, which is
3337
     * not allowed at this stage. */
3338
    zend_rc_debug = false;
3339
#endif
3340
0
    zend_clean_module_rsrc_dtors(module->module_number);
3341
0
    clean_module_constants(module->module_number);
3342
0
    clean_module_classes(module->module_number);
3343
0
  }
3344
3345
0
  if (module->module_started && module->module_shutdown_func) {
3346
#if 0
3347
    zend_printf("%s: Module shutdown\n", module->name);
3348
#endif
3349
0
    module->module_shutdown_func(module->type, module->module_number);
3350
0
  }
3351
3352
0
  if (module->module_started
3353
0
   && !module->module_shutdown_func
3354
0
   && module->type == MODULE_TEMPORARY) {
3355
0
    zend_unregister_ini_entries_ex(module->module_number, module->type);
3356
0
  }
3357
3358
  /* Deinitialize module globals */
3359
0
  if (module->globals_size) {
3360
#ifdef ZTS
3361
    if (*module->globals_id_ptr) {
3362
      ts_free_id(*module->globals_id_ptr);
3363
    }
3364
#else
3365
0
    if (module->globals_dtor) {
3366
0
      module->globals_dtor(module->globals_ptr);
3367
0
    }
3368
0
#endif
3369
0
  }
3370
3371
0
  module->module_started=0;
3372
0
  if (module->type == MODULE_TEMPORARY && module->functions) {
3373
0
    zend_unregister_functions(module->functions, -1, NULL);
3374
    /* Clean functions registered separately from module->functions */
3375
0
    clean_module_functions(module);
3376
0
  }
3377
3378
#if ZEND_RC_DEBUG
3379
  zend_rc_debug = orig_rc_debug;
3380
#endif
3381
0
}
3382
/* }}} */
3383
3384
void module_registry_unload(const zend_module_entry *module)
3385
0
{
3386
0
#ifdef HAVE_LIBDL
3387
0
  if (!getenv("ZEND_DONT_UNLOAD_MODULES")) {
3388
0
    DL_UNLOAD(module->handle);
3389
0
  }
3390
#else
3391
  ZEND_IGNORE_VALUE(module);
3392
#endif
3393
0
}
3394
3395
ZEND_API void zend_activate_modules(void) /* {{{ */
3396
224k
{
3397
224k
  zend_module_entry **p = module_request_startup_handlers;
3398
3399
2.02M
  while (*p) {
3400
1.79M
    const zend_module_entry *module = *p;
3401
3402
1.79M
    if (module->request_startup_func(module->type, module->module_number)==FAILURE) {
3403
0
      zend_error(E_WARNING, "request_startup() for %s module failed", module->name);
3404
0
      exit(1);
3405
0
    }
3406
1.79M
    p++;
3407
1.79M
  }
3408
224k
}
3409
/* }}} */
3410
3411
ZEND_API void zend_deactivate_modules(void) /* {{{ */
3412
224k
{
3413
224k
  EG(current_execute_data) = NULL; /* we're no longer executing anything */
3414
3415
224k
  if (EG(full_tables_cleanup)) {
3416
0
    zend_module_entry *module;
3417
3418
0
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) {
3419
0
      if (module->request_shutdown_func) {
3420
0
        zend_try {
3421
0
          module->request_shutdown_func(module->type, module->module_number);
3422
0
        } zend_end_try();
3423
0
      }
3424
0
    } ZEND_HASH_FOREACH_END();
3425
224k
  } else {
3426
224k
    zend_module_entry **p = module_request_shutdown_handlers;
3427
3428
1.12M
    while (*p) {
3429
898k
      const zend_module_entry *module = *p;
3430
898k
      zend_try {
3431
898k
        module->request_shutdown_func(module->type, module->module_number);
3432
898k
      } zend_end_try();
3433
898k
      p++;
3434
898k
    }
3435
224k
  }
3436
224k
}
3437
/* }}} */
3438
3439
void zend_unload_modules(void) /* {{{ */
3440
0
{
3441
0
  zend_module_entry **modules = modules_dl_loaded;
3442
0
  while (*modules) {
3443
0
    module_registry_unload(*modules);
3444
0
    modules++;
3445
0
  }
3446
0
  free(modules_dl_loaded);
3447
0
  modules_dl_loaded = NULL;
3448
0
}
3449
/* }}} */
3450
3451
ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
3452
224k
{
3453
224k
  if (EG(full_tables_cleanup)) {
3454
0
    zend_module_entry *module;
3455
0
    zval *zv;
3456
0
    zend_string *key;
3457
3458
0
    ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
3459
0
      if (module->post_deactivate_func) {
3460
0
        module->post_deactivate_func();
3461
0
      }
3462
0
    } ZEND_HASH_FOREACH_END();
3463
0
    ZEND_HASH_MAP_REVERSE_FOREACH_STR_KEY_VAL(&module_registry, key, zv) {
3464
0
      module = Z_PTR_P(zv);
3465
0
      if (module->type != MODULE_TEMPORARY) {
3466
0
        break;
3467
0
      }
3468
0
      module_destructor(module);
3469
0
      if (module->handle) {
3470
0
        module_registry_unload(module);
3471
0
      }
3472
0
      zend_string_release_ex(key, 0);
3473
0
    } ZEND_HASH_MAP_FOREACH_END_DEL();
3474
224k
  } else {
3475
224k
    zend_module_entry **p = module_post_deactivate_handlers;
3476
3477
898k
    while (*p) {
3478
673k
      const zend_module_entry *module = *p;
3479
3480
673k
      module->post_deactivate_func();
3481
673k
      p++;
3482
673k
    }
3483
224k
  }
3484
224k
}
3485
/* }}} */
3486
3487
/* return the next free module number */
3488
ZEND_API int zend_next_free_module(void) /* {{{ */
3489
208
{
3490
208
  return zend_hash_num_elements(&module_registry);
3491
208
}
3492
/* }}} */
3493
3494
static zend_class_entry *do_register_internal_class(const zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
3495
2.64k
{
3496
2.64k
  zend_class_entry *class_entry = malloc(sizeof(zend_class_entry));
3497
2.64k
  zend_string *lowercase_name;
3498
2.64k
  *class_entry = *orig_class_entry;
3499
3500
2.64k
  class_entry->type = ZEND_INTERNAL_CLASS;
3501
2.64k
  zend_initialize_class_data(class_entry, 0);
3502
2.64k
  zend_alloc_ce_cache(class_entry->name);
3503
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;
3504
2.64k
  class_entry->info.internal.module = EG(current_module);
3505
3506
2.64k
  if (class_entry->info.internal.builtin_functions) {
3507
1.77k
    zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, EG(current_module)->type);
3508
1.77k
  }
3509
3510
2.64k
  lowercase_name = zend_string_tolower_ex(orig_class_entry->name, EG(current_module)->type == MODULE_PERSISTENT);
3511
2.64k
  lowercase_name = zend_new_interned_string(lowercase_name);
3512
2.64k
  zend_hash_update_ptr(CG(class_table), lowercase_name, class_entry);
3513
2.64k
  zend_string_release_ex(lowercase_name, 1);
3514
3515
2.64k
  if (class_entry->__tostring && !zend_string_equals_literal(class_entry->name, "Stringable")
3516
272
      && !(class_entry->ce_flags & ZEND_ACC_TRAIT)) {
3517
272
    ZEND_ASSERT(zend_ce_stringable
3518
272
      && "Should be registered before first class using __toString()");
3519
272
    zend_do_implement_interface(class_entry, zend_ce_stringable);
3520
272
  }
3521
2.64k
  return class_entry;
3522
2.64k
}
3523
/* }}} */
3524
3525
/* If parent_ce is not NULL then it inherits from parent_ce
3526
 * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it
3527
 * If both parent_ce and parent_name are NULL it does a regular class registration
3528
 * If parent_name is specified but not found NULL is returned
3529
 */
3530
ZEND_API zend_class_entry *zend_register_internal_class_ex(const zend_class_entry *class_entry, zend_class_entry *parent_ce) /* {{{ */
3531
0
{
3532
0
  return zend_register_internal_class_with_flags(class_entry, parent_ce, 0);
3533
0
}
3534
/* }}} */
3535
3536
ZEND_API zend_class_entry *zend_register_internal_class_with_flags(
3537
  const zend_class_entry *class_entry,
3538
  zend_class_entry *parent_ce,
3539
  uint32_t ce_flags
3540
2.24k
) {
3541
2.24k
  zend_class_entry *register_class = do_register_internal_class(class_entry, ce_flags);
3542
3543
2.24k
  if (parent_ce) {
3544
1.24k
    zend_do_inheritance(register_class, parent_ce);
3545
1.24k
    zend_build_properties_info_table(register_class);
3546
1.24k
  }
3547
3548
2.24k
  return register_class;
3549
2.24k
}
3550
3551
ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...) /* {{{ */
3552
928
{
3553
928
  zend_class_entry *interface_entry;
3554
928
  va_list interface_list;
3555
928
  va_start(interface_list, num_interfaces);
3556
3557
2.20k
  while (num_interfaces--) {
3558
1.28k
    interface_entry = va_arg(interface_list, zend_class_entry *);
3559
1.28k
    if (interface_entry == zend_ce_stringable
3560
80
        && zend_class_implements_interface(class_entry, zend_ce_stringable)) {
3561
      /* Stringable is implemented automatically,
3562
       * silently ignore an explicit implementation. */
3563
48
      continue;
3564
48
    }
3565
3566
1.23k
    zend_do_implement_interface(class_entry, interface_entry);
3567
1.23k
  }
3568
3569
928
  va_end(interface_list);
3570
928
}
3571
/* }}} */
3572
3573
/* A class that contains at least one abstract method automatically becomes an abstract class.
3574
 */
3575
ZEND_API zend_class_entry *zend_register_internal_class(const zend_class_entry *orig_class_entry) /* {{{ */
3576
80
{
3577
80
  return do_register_internal_class(orig_class_entry, 0);
3578
80
}
3579
/* }}} */
3580
3581
ZEND_API zend_class_entry *zend_register_internal_interface(const zend_class_entry *orig_class_entry) /* {{{ */
3582
320
{
3583
320
  return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE);
3584
320
}
3585
/* }}} */
3586
3587
ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce, bool persistent) /* {{{ */
3588
212
{
3589
212
  zend_string *lcname;
3590
212
  zval zv, *ret;
3591
3592
  /* TODO: Move this out of here in 7.4. */
3593
212
  if (persistent && EG(current_module) && EG(current_module)->type == MODULE_TEMPORARY) {
3594
0
    persistent = false;
3595
0
  }
3596
3597
212
  if (name[0] == '\\') {
3598
1
    lcname = zend_string_alloc(name_len-1, persistent);
3599
1
    zend_str_tolower_copy(ZSTR_VAL(lcname), name+1, name_len-1);
3600
211
  } else {
3601
211
    lcname = zend_string_alloc(name_len, persistent);
3602
211
    zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len);
3603
211
  }
3604
3605
212
  zend_assert_valid_class_name(lcname, "a class alias");
3606
3607
212
  lcname = zend_new_interned_string(lcname);
3608
3609
  /* We cannot increase the refcount of an internal class during request time.
3610
   * Instead of having to deal with differentiating between class types and lifetimes,
3611
   * we simply don't increase the refcount of a class entry for aliases.
3612
   */
3613
212
  ZVAL_ALIAS_PTR(&zv, ce);
3614
3615
212
  ret = zend_hash_add(CG(class_table), lcname, &zv);
3616
212
  zend_string_release_ex(lcname, 0);
3617
212
  if (ret) {
3618
    // avoid notifying at MINIT time
3619
176
    if (ce->type == ZEND_USER_CLASS) {
3620
164
      zend_observer_class_linked_notify(ce, lcname);
3621
164
    }
3622
176
    return SUCCESS;
3623
176
  }
3624
36
  return FAILURE;
3625
212
}
3626
/* }}} */
3627
3628
/* Disabled functions support */
3629
3630
static void zend_disable_function(const char *function_name, size_t function_name_length)
3631
576
{
3632
576
  if (UNEXPECTED(
3633
576
    (function_name_length == strlen("exit") && !memcmp(function_name, "exit", strlen("exit")))
3634
576
    || (function_name_length == strlen("die") && !memcmp(function_name, "die", strlen("die")))
3635
576
    || (function_name_length == strlen("clone") && !memcmp(function_name, "clone", strlen("clone")))
3636
576
  )) {
3637
0
    zend_error(E_WARNING, "Cannot disable function %s()", function_name);
3638
0
    return;
3639
0
  }
3640
576
  zend_hash_str_del(CG(function_table), function_name, function_name_length);
3641
576
}
3642
3643
ZEND_API void zend_disable_functions(const char *function_list) /* {{{ */
3644
16
{
3645
16
  if (!function_list || !*function_list) {
3646
0
    return;
3647
0
  }
3648
3649
16
  const char *s = NULL, *e = function_list;
3650
5.34k
  while (*e) {
3651
5.32k
    switch (*e) {
3652
0
      case ' ':
3653
560
      case ',':
3654
560
        if (s) {
3655
560
          zend_disable_function(s, e - s);
3656
560
          s = NULL;
3657
560
        }
3658
560
        break;
3659
4.76k
      default:
3660
4.76k
        if (!s) {
3661
576
          s = e;
3662
576
        }
3663
4.76k
        break;
3664
5.32k
    }
3665
5.32k
    e++;
3666
5.32k
  }
3667
16
  if (s) {
3668
16
    zend_disable_function(s, e - s);
3669
16
  }
3670
3671
  /* Rehash the function table after deleting functions. This ensures that all internal
3672
   * functions are contiguous, which means we don't need to perform full table cleanup
3673
   * on shutdown. */
3674
16
  zend_hash_rehash(CG(function_table));
3675
16
}
3676
/* }}} */
3677
3678
static zend_always_inline zend_class_entry *get_scope(const zend_execute_data *frame)
3679
2.74k
{
3680
2.74k
  return frame && frame->func ? frame->func->common.scope : NULL;
3681
2.74k
}
3682
3683
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) /* {{{ */
3684
1.80k
{
3685
1.80k
  bool ret = false;
3686
1.80k
  zend_class_entry *ce;
3687
1.80k
  size_t name_len = ZSTR_LEN(name);
3688
1.80k
  zend_string *lcname;
3689
1.80k
  ALLOCA_FLAG(use_heap);
3690
3691
1.80k
  ZSTR_ALLOCA_ALLOC(lcname, name_len, use_heap);
3692
1.80k
  zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name), name_len);
3693
3694
1.80k
  *strict_class = false;
3695
1.80k
  if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_SELF))) {
3696
435
    if (!scope) {
3697
5
      if (error) *error = estrdup("cannot access \"self\" when no class scope is active");
3698
430
    } else {
3699
430
      if (!suppress_deprecation) {
3700
335
        zend_error(E_DEPRECATED, "Use of \"self\" in callables is deprecated");
3701
335
      }
3702
430
      fcc->called_scope = zend_get_called_scope(frame);
3703
430
      if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope)) {
3704
0
        fcc->called_scope = scope;
3705
0
      }
3706
430
      fcc->calling_scope = scope;
3707
430
      if (!fcc->object) {
3708
351
        fcc->object = zend_get_this_object(frame);
3709
351
      }
3710
430
      ret = true;
3711
430
    }
3712
1.36k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_PARENT))) {
3713
262
    if (!scope) {
3714
0
      if (error) *error = estrdup("cannot access \"parent\" when no class scope is active");
3715
262
    } else if (!scope->parent) {
3716
9
      if (error) *error = estrdup("cannot access \"parent\" when current class scope has no parent");
3717
253
    } else {
3718
253
      if (!suppress_deprecation) {
3719
127
        zend_error(E_DEPRECATED, "Use of \"parent\" in callables is deprecated");
3720
127
      }
3721
253
      fcc->called_scope = zend_get_called_scope(frame);
3722
253
      if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope->parent)) {
3723
13
        fcc->called_scope = scope->parent;
3724
13
      }
3725
253
      fcc->calling_scope = scope->parent;
3726
253
      if (!fcc->object) {
3727
150
        fcc->object = zend_get_this_object(frame);
3728
150
      }
3729
253
      *strict_class = true;
3730
253
      ret = true;
3731
253
    }
3732
1.10k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_STATIC))) {
3733
227
    zend_class_entry *called_scope = zend_get_called_scope(frame);
3734
3735
227
    if (!called_scope) {
3736
0
      if (error) *error = estrdup("cannot access \"static\" when no class scope is active");
3737
227
    } else {
3738
227
      if (!suppress_deprecation) {
3739
150
        zend_error(E_DEPRECATED, "Use of \"static\" in callables is deprecated");
3740
150
      }
3741
227
      fcc->called_scope = called_scope;
3742
227
      fcc->calling_scope = called_scope;
3743
227
      if (!fcc->object) {
3744
160
        fcc->object = zend_get_this_object(frame);
3745
160
      }
3746
227
      *strict_class = true;
3747
227
      ret = true;
3748
227
    }
3749
879
  } else if ((ce = zend_lookup_class(name)) != NULL) {
3750
645
    const zend_class_entry *frame_scope = get_scope(frame);
3751
645
    fcc->calling_scope = ce;
3752
645
    if (frame_scope && !fcc->object) {
3753
297
      zend_object *object = zend_get_this_object(frame);
3754
3755
297
      if (object &&
3756
242
          instanceof_function(object->ce, frame_scope) &&
3757
242
          instanceof_function(frame_scope, ce)) {
3758
220
        fcc->object = object;
3759
220
        fcc->called_scope = object->ce;
3760
220
      } else {
3761
77
        fcc->called_scope = ce;
3762
77
      }
3763
348
    } else {
3764
348
      fcc->called_scope = fcc->object ? fcc->object->ce : ce;
3765
348
    }
3766
645
    *strict_class = true;
3767
645
    ret = true;
3768
645
  } else {
3769
234
    if (error) zend_spprintf(error, 0, "class \"%.*s\" not found", (int)name_len, ZSTR_VAL(name));
3770
234
  }
3771
1.80k
  ZSTR_ALLOCA_FREE(lcname, use_heap);
3772
1.80k
  return ret;
3773
1.80k
}
3774
/* }}} */
3775
3776
20.2k
ZEND_API void zend_release_fcall_info_cache(zend_fcall_info_cache *fcc) {
3777
20.2k
  if (fcc->function_handler &&
3778
19.8k
    (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
3779
333
    if (fcc->function_handler->common.function_name) {
3780
333
      zend_string_release_ex(fcc->function_handler->common.function_name, 0);
3781
333
    }
3782
333
    zend_free_trampoline(fcc->function_handler);
3783
333
    fcc->function_handler = NULL;
3784
333
  }
3785
20.2k
}
3786
3787
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) /* {{{ */
3788
13.8k
{
3789
13.8k
  zend_class_entry *ce_org = fcc->calling_scope;
3790
13.8k
  bool retval = false;
3791
13.8k
  zend_string *mname, *cname;
3792
13.8k
  zend_string *lmname;
3793
13.8k
  const char *colon;
3794
13.8k
  size_t clen;
3795
13.8k
  HashTable *ftable;
3796
13.8k
  int call_via_handler = 0;
3797
13.8k
  zend_class_entry *scope;
3798
13.8k
  zval *zv;
3799
13.8k
  ALLOCA_FLAG(use_heap)
3800
3801
13.8k
  fcc->calling_scope = NULL;
3802
3803
13.8k
  if (!ce_org) {
3804
4.17k
    zend_function *func;
3805
4.17k
    zend_string *lmname;
3806
3807
    /* Check if function with given name exists.
3808
     * This may be a compound name that includes namespace name */
3809
4.17k
    if (UNEXPECTED(Z_STRVAL_P(callable)[0] == '\\')) {
3810
      /* Skip leading \ */
3811
66
      ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable) - 1, use_heap);
3812
66
      zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable) + 1, Z_STRLEN_P(callable) - 1);
3813
66
      func = zend_fetch_function(lmname);
3814
66
      ZSTR_ALLOCA_FREE(lmname, use_heap);
3815
4.10k
    } else {
3816
4.10k
      lmname = Z_STR_P(callable);
3817
4.10k
      func = zend_fetch_function(lmname);
3818
4.10k
      if (!func) {
3819
1.33k
        ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable), use_heap);
3820
1.33k
        zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable), Z_STRLEN_P(callable));
3821
1.33k
        func = zend_fetch_function(lmname);
3822
1.33k
        ZSTR_ALLOCA_FREE(lmname, use_heap);
3823
1.33k
      }
3824
4.10k
    }
3825
4.17k
    if (EXPECTED(func != NULL)) {
3826
3.27k
      fcc->function_handler = func;
3827
3.27k
      return 1;
3828
3.27k
    }
3829
4.17k
  }
3830
3831
  /* Split name into class/namespace and method/function names */
3832
10.5k
  if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL &&
3833
10.5k
    colon > Z_STRVAL_P(callable) &&
3834
1.01k
    *(colon-1) == ':'
3835
10.5k
  ) {
3836
1.00k
    size_t mlen;
3837
3838
1.00k
    colon--;
3839
1.00k
    clen = colon - Z_STRVAL_P(callable);
3840
1.00k
    mlen = Z_STRLEN_P(callable) - clen - 2;
3841
3842
1.00k
    if (colon == Z_STRVAL_P(callable)) {
3843
0
      if (error) *error = estrdup("invalid function name");
3844
0
      return 0;
3845
0
    }
3846
3847
    /* This is a compound name.
3848
     * Try to fetch class and then find static method. */
3849
1.00k
    if (ce_org) {
3850
334
      scope = ce_org;
3851
672
    } else {
3852
672
      scope = get_scope(frame);
3853
672
    }
3854
3855
1.00k
    cname = zend_string_init_interned(Z_STRVAL_P(callable), clen, 0);
3856
1.00k
    if (ZSTR_HAS_CE_CACHE(cname) && ZSTR_GET_CE_CACHE(cname)) {
3857
316
      fcc->calling_scope = ZSTR_GET_CE_CACHE(cname);
3858
316
      if (scope && !fcc->object) {
3859
137
        zend_object *object = zend_get_this_object(frame);
3860
3861
137
        if (object &&
3862
89
            instanceof_function(object->ce, scope) &&
3863
89
            instanceof_function(scope, fcc->calling_scope)) {
3864
89
          fcc->object = object;
3865
89
          fcc->called_scope = object->ce;
3866
89
        } else {
3867
48
          fcc->called_scope = fcc->calling_scope;
3868
48
        }
3869
179
      } else {
3870
179
        fcc->called_scope = fcc->object ? fcc->object->ce : fcc->calling_scope;
3871
179
      }
3872
316
      strict_class = true;
3873
690
    } else if (!zend_is_callable_check_class(cname, scope, frame, fcc, &strict_class, error, suppress_deprecation || ce_org != NULL)) {
3874
145
      zend_string_release_ex(cname, 0);
3875
145
      return 0;
3876
145
    }
3877
861
    zend_string_release_ex(cname, 0);
3878
3879
861
    ftable = &fcc->calling_scope->function_table;
3880
861
    if (ce_org && !instanceof_function(ce_org, fcc->calling_scope)) {
3881
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));
3882
0
      return 0;
3883
0
    }
3884
861
    if (ce_org && !suppress_deprecation) {
3885
289
      zend_error(E_DEPRECATED,
3886
289
        "Callables of the form [\"%s\", \"%s\"] are deprecated",
3887
289
        ZSTR_VAL(ce_org->name), Z_STRVAL_P(callable));
3888
289
    }
3889
861
    mname = zend_string_init(Z_STRVAL_P(callable) + clen + 2, mlen, 0);
3890
9.54k
  } else if (ce_org) {
3891
    /* Try to fetch find static method of given class. */
3892
9.31k
    mname = Z_STR_P(callable);
3893
9.31k
    zend_string_addref(mname);
3894
9.31k
    ftable = &ce_org->function_table;
3895
9.31k
    fcc->calling_scope = ce_org;
3896
9.31k
  } else {
3897
    /* We already checked for plain function before. */
3898
227
    if (error) {
3899
88
      zend_spprintf(error, 0, "function \"%s\" not found or invalid function name", Z_STRVAL_P(callable));
3900
88
    }
3901
227
    return 0;
3902
227
  }
3903
3904
10.1k
  lmname = zend_string_tolower(mname);
3905
10.1k
  if (strict_class &&
3906
1.29k
      fcc->calling_scope &&
3907
1.29k
    zend_string_equals_literal(lmname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
3908
0
    fcc->function_handler = fcc->calling_scope->constructor;
3909
0
    if (fcc->function_handler) {
3910
0
      retval = true;
3911
0
    }
3912
10.1k
  } else if ((zv = zend_hash_find(ftable, lmname)) != NULL) {
3913
7.33k
    fcc->function_handler = Z_PTR_P(zv);
3914
7.33k
    retval = true;
3915
7.33k
    if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) &&
3916
30
        !strict_class) {
3917
10
      scope = get_scope(frame);
3918
10
      if (scope &&
3919
10
          instanceof_function(fcc->function_handler->common.scope, scope)) {
3920
3921
10
        zv = zend_hash_find(&scope->function_table, lmname);
3922
10
        if (zv != NULL) {
3923
10
          zend_function *priv_fbc = Z_PTR_P(zv);
3924
3925
10
          if ((priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE)
3926
10
           && priv_fbc->common.scope == scope) {
3927
10
            fcc->function_handler = priv_fbc;
3928
10
          }
3929
10
        }
3930
10
      }
3931
10
    }
3932
7.33k
    if (!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC) &&
3933
281
        (fcc->calling_scope &&
3934
281
         ((fcc->object && fcc->calling_scope->__call) ||
3935
242
          (!fcc->object && fcc->calling_scope->__callstatic)))) {
3936
39
      scope = get_scope(frame);
3937
39
      ZEND_ASSERT(!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC));
3938
39
      if (!zend_check_method_accessible(fcc->function_handler, scope)) {
3939
12
        retval = false;
3940
12
        fcc->function_handler = NULL;
3941
12
        goto get_function_via_handler;
3942
12
      }
3943
39
    }
3944
7.33k
  } else {
3945
2.84k
get_function_via_handler:
3946
2.84k
    if (fcc->object && fcc->calling_scope == ce_org) {
3947
2.58k
      if (strict_class && ce_org->__call) {
3948
16
        fcc->function_handler = zend_get_call_trampoline_func(ce_org->__call, mname);
3949
16
        call_via_handler = 1;
3950
16
        retval = true;
3951
2.56k
      } else {
3952
2.56k
        fcc->function_handler = fcc->object->handlers->get_method(&fcc->object, mname, NULL);
3953
2.56k
        if (fcc->function_handler) {
3954
2.32k
          if (strict_class &&
3955
13
              (!fcc->function_handler->common.scope ||
3956
13
               !instanceof_function(ce_org, fcc->function_handler->common.scope))) {
3957
13
            zend_release_fcall_info_cache(fcc);
3958
2.31k
          } else {
3959
2.31k
            retval = true;
3960
2.31k
            call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3961
2.31k
          }
3962
2.32k
        }
3963
2.56k
      }
3964
2.58k
    } else if (fcc->calling_scope) {
3965
269
      if (fcc->calling_scope->get_static_method) {
3966
0
        fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname);
3967
269
      } else {
3968
269
        fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, NULL);
3969
269
      }
3970
269
      if (fcc->function_handler) {
3971
188
        retval = true;
3972
188
        call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3973
188
        if (call_via_handler && !fcc->object) {
3974
86
          zend_object *object = zend_get_this_object(frame);
3975
86
          if (object &&
3976
16
              instanceof_function(object->ce, fcc->calling_scope)) {
3977
16
            fcc->object = object;
3978
16
          }
3979
86
        }
3980
188
      }
3981
269
    }
3982
2.84k
  }
3983
3984
10.1k
  if (retval) {
3985
9.84k
    if (fcc->calling_scope && !call_via_handler) {
3986
7.32k
      if (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT) {
3987
28
        retval = false;
3988
28
        if (error) {
3989
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));
3990
0
        }
3991
7.29k
      } else if (!fcc->object && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
3992
20
        retval = false;
3993
20
        if (error) {
3994
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));
3995
6
        }
3996
20
      }
3997
7.32k
      if (retval
3998
7.27k
       && !(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC)) {
3999
264
        scope = get_scope(frame);
4000
264
        ZEND_ASSERT(!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC));
4001
264
        if (!zend_check_method_accessible(fcc->function_handler, scope)) {
4002
120
          if (error) {
4003
24
            if (*error) {
4004
0
              efree(*error);
4005
0
            }
4006
24
            zend_spprintf(error, 0, "cannot access %s method %s::%s()", zend_visibility_string(fcc->function_handler->common.fn_flags), ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
4007
24
          }
4008
120
          retval = false;
4009
120
        }
4010
264
      }
4011
7.32k
    }
4012
9.84k
  } else if (error) {
4013
34
    if (fcc->calling_scope) {
4014
34
      zend_spprintf(error, 0, "class %s does not have a method \"%s\"", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(mname));
4015
34
    } else {
4016
0
      zend_spprintf(error, 0, "function %s() does not exist", ZSTR_VAL(mname));
4017
0
    }
4018
34
  }
4019
10.1k
  zend_string_release_ex(lmname, 0);
4020
10.1k
  zend_string_release_ex(mname, 0);
4021
4022
10.1k
  if (fcc->object) {
4023
9.43k
    fcc->called_scope = fcc->object->ce;
4024
9.43k
    if (fcc->function_handler
4025
9.18k
     && (fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
4026
56
      fcc->object = NULL;
4027
56
    }
4028
9.43k
  }
4029
10.1k
  return retval;
4030
10.1k
}
4031
/* }}} */
4032
4033
ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, const zend_object *object) /* {{{ */
4034
171
{
4035
171
try_again:
4036
171
  switch (Z_TYPE_P(callable)) {
4037
7
    case IS_STRING:
4038
7
      if (object) {
4039
0
        return zend_create_member_string(object->ce->name, Z_STR_P(callable));
4040
0
      }
4041
7
      return zend_string_copy(Z_STR_P(callable));
4042
4043
36
    case IS_ARRAY:
4044
36
    {
4045
36
      const zval *method = NULL;
4046
36
      const zval *obj = NULL;
4047
4048
36
      if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
4049
36
        obj = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 0);
4050
36
        method = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 1);
4051
36
      }
4052
4053
36
      if (obj == NULL || method == NULL || Z_TYPE_P(method) != IS_STRING) {
4054
0
        return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
4055
0
      }
4056
4057
36
      if (Z_TYPE_P(obj) == IS_STRING) {
4058
0
        return zend_create_member_string(Z_STR_P(obj), Z_STR_P(method));
4059
36
      } else if (Z_TYPE_P(obj) == IS_OBJECT) {
4060
32
        return zend_create_member_string(Z_OBJCE_P(obj)->name, Z_STR_P(method));
4061
32
      } else {
4062
4
        return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
4063
4
      }
4064
36
    }
4065
124
    case IS_OBJECT:
4066
124
    {
4067
124
      const zend_class_entry *ce = Z_OBJCE_P(callable);
4068
4069
124
      if (ce == zend_ce_closure) {
4070
112
        const zend_function *fn = zend_get_closure_method_def(Z_OBJ_P(callable));
4071
4072
112
        if ((fn->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) && fn->common.scope) {
4073
15
          return zend_create_member_string(fn->common.scope->name, fn->common.function_name);
4074
15
        }
4075
97
        return zend_string_copy(fn->common.function_name);
4076
112
      }
4077
4078
12
      return zend_string_concat2(
4079
12
        ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
4080
12
        "::__invoke", sizeof("::__invoke") - 1);
4081
124
    }
4082
0
    case IS_REFERENCE:
4083
0
      callable = Z_REFVAL_P(callable);
4084
0
      goto try_again;
4085
4
    default:
4086
4
      return zval_get_string_func(callable);
4087
171
  }
4088
171
}
4089
/* }}} */
4090
4091
ZEND_API zend_string *zend_get_callable_name(zval *callable) /* {{{ */
4092
0
{
4093
0
  return zend_get_callable_name_ex(callable, NULL);
4094
0
}
4095
/* }}} */
4096
4097
ZEND_API bool zend_is_callable_at_frame(
4098
    const zval *callable, zend_object *object, const zend_execute_data *frame,
4099
    uint32_t check_flags, zend_fcall_info_cache *fcc, char **error) /* {{{ */
4100
24.6k
{
4101
24.6k
  bool ret;
4102
24.6k
  zend_fcall_info_cache fcc_local;
4103
24.6k
  bool strict_class = false;
4104
4105
24.6k
  if (fcc == NULL) {
4106
8.41k
    fcc = &fcc_local;
4107
8.41k
  }
4108
24.6k
  if (error) {
4109
9.09k
    *error = NULL;
4110
9.09k
  }
4111
4112
24.6k
  fcc->calling_scope = NULL;
4113
24.6k
  fcc->called_scope = NULL;
4114
24.6k
  fcc->function_handler = NULL;
4115
24.6k
  fcc->object = NULL;
4116
24.6k
  fcc->closure = NULL;
4117
4118
24.6k
again:
4119
24.6k
  switch (Z_TYPE_P(callable)) {
4120
11.3k
    case IS_STRING:
4121
11.3k
      if (object) {
4122
7.19k
        fcc->object = object;
4123
7.19k
        fcc->calling_scope = object->ce;
4124
7.19k
      }
4125
4126
11.3k
      if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4127
0
        fcc->called_scope = fcc->calling_scope;
4128
0
        return 1;
4129
0
      }
4130
4131
13.8k
check_func:
4132
13.8k
      ret = zend_is_callable_check_func(callable, frame, fcc, strict_class, error, check_flags & IS_CALLABLE_SUPPRESS_DEPRECATIONS);
4133
13.8k
      if (fcc == &fcc_local) {
4134
2.89k
        zend_release_fcall_info_cache(fcc);
4135
2.89k
      }
4136
13.8k
      return ret;
4137
4138
2.62k
    case IS_ARRAY:
4139
2.62k
      {
4140
2.62k
        if (zend_hash_num_elements(Z_ARRVAL_P(callable)) != 2) {
4141
4
          if (error) *error = estrdup("array callback must have exactly two members");
4142
4
          return 0;
4143
4
        }
4144
4145
2.62k
        zval *obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0);
4146
2.62k
        zval *method = zend_hash_index_find(Z_ARRVAL_P(callable), 1);
4147
2.62k
        if (!obj || !method) {
4148
0
          if (error) *error = estrdup("array callback has to contain indices 0 and 1");
4149
0
          return 0;
4150
0
        }
4151
4152
2.62k
        ZVAL_DEREF(obj);
4153
2.62k
        if (Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT) {
4154
42
          if (error) *error = estrdup("first array member is not a valid class name or object");
4155
42
          return 0;
4156
42
        }
4157
4158
2.58k
        ZVAL_DEREF(method);
4159
2.58k
        if (Z_TYPE_P(method) != IS_STRING) {
4160
4
          if (error) *error = estrdup("second array member is not a valid method");
4161
4
          return 0;
4162
4
        }
4163
4164
2.57k
        if (Z_TYPE_P(obj) == IS_STRING) {
4165
1.11k
          if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4166
0
            return 1;
4167
0
          }
4168
4169
1.11k
          if (!zend_is_callable_check_class(Z_STR_P(obj), get_scope(frame), frame, fcc, &strict_class, error, check_flags & IS_CALLABLE_SUPPRESS_DEPRECATIONS)) {
4170
103
            return 0;
4171
103
          }
4172
1.46k
        } else {
4173
1.46k
          ZEND_ASSERT(Z_TYPE_P(obj) == IS_OBJECT);
4174
1.46k
          fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */
4175
1.46k
          fcc->object = Z_OBJ_P(obj);
4176
4177
1.46k
          if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4178
16
            fcc->called_scope = fcc->calling_scope;
4179
16
            return 1;
4180
16
          }
4181
1.46k
        }
4182
4183
2.45k
        callable = method;
4184
2.45k
        goto check_func;
4185
2.57k
      }
4186
0
      return 0;
4187
10.6k
    case IS_OBJECT:
4188
10.6k
      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) {
4189
10.6k
        fcc->called_scope = fcc->calling_scope;
4190
10.6k
        fcc->closure = Z_OBJ_P(callable);
4191
10.6k
        if (fcc == &fcc_local) {
4192
5.39k
          zend_release_fcall_info_cache(fcc);
4193
5.39k
        }
4194
10.6k
        return 1;
4195
10.6k
      }
4196
15
      if (error) *error = estrdup("no array or string given");
4197
15
      return 0;
4198
4
    case IS_REFERENCE:
4199
4
      callable = Z_REFVAL_P(callable);
4200
4
      goto again;
4201
41
    default:
4202
41
      if (error) *error = estrdup("no array or string given");
4203
41
      return 0;
4204
24.6k
  }
4205
24.6k
}
4206
/* }}} */
4207
4208
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) /* {{{ */
4209
24.6k
{
4210
  /* Determine callability at the first parent user frame. */
4211
24.6k
  const zend_execute_data *frame = EG(current_execute_data);
4212
40.7k
  while (frame && (!frame->func || !ZEND_USER_CODE(frame->func->type))) {
4213
16.0k
    frame = frame->prev_execute_data;
4214
16.0k
  }
4215
4216
24.6k
  bool ret = zend_is_callable_at_frame(callable, object, frame, check_flags, fcc, error);
4217
24.6k
  if (callable_name) {
4218
164
    *callable_name = zend_get_callable_name_ex(callable, object);
4219
164
  }
4220
24.6k
  return ret;
4221
24.6k
}
4222
4223
ZEND_API bool zend_is_callable(zval *callable, uint32_t check_flags, zend_string **callable_name) /* {{{ */
4224
7.79k
{
4225
7.79k
  return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL);
4226
7.79k
}
4227
/* }}} */
4228
4229
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) /* {{{ */
4230
6.95k
{
4231
6.95k
  if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, fcc, error)) {
4232
135
    return FAILURE;
4233
135
  }
4234
4235
6.81k
  fci->size = sizeof(*fci);
4236
6.81k
  fci->object = fcc->object;
4237
6.81k
  ZVAL_COPY_VALUE(&fci->function_name, callable);
4238
6.81k
  fci->retval = NULL;
4239
6.81k
  fci->param_count = 0;
4240
6.81k
  fci->params = NULL;
4241
6.81k
  fci->named_params = NULL;
4242
4243
6.81k
  return SUCCESS;
4244
6.95k
}
4245
/* }}} */
4246
4247
ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, bool free_mem) /* {{{ */
4248
0
{
4249
0
  if (fci->params) {
4250
0
    zval *p = fci->params;
4251
0
    zval *end = p + fci->param_count;
4252
4253
0
    while (p != end) {
4254
0
      i_zval_ptr_dtor(p);
4255
0
      p++;
4256
0
    }
4257
0
    if (free_mem) {
4258
0
      efree(fci->params);
4259
0
      fci->params = NULL;
4260
0
    }
4261
0
  }
4262
0
  fci->param_count = 0;
4263
0
}
4264
/* }}} */
4265
4266
ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, uint32_t *param_count, zval **params) /* {{{ */
4267
0
{
4268
0
  *param_count = fci->param_count;
4269
0
  *params = fci->params;
4270
0
  fci->param_count = 0;
4271
0
  fci->params = NULL;
4272
0
}
4273
/* }}} */
4274
4275
ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, uint32_t param_count, zval *params) /* {{{ */
4276
0
{
4277
0
  zend_fcall_info_args_clear(fci, true);
4278
0
  fci->param_count = param_count;
4279
0
  fci->params = params;
4280
0
}
4281
/* }}} */
4282
4283
ZEND_API zend_result zend_fcall_info_args_ex(zend_fcall_info *fci, zend_function *func, zval *args) /* {{{ */
4284
0
{
4285
0
  zval *arg, *params;
4286
0
  uint32_t n = 1;
4287
4288
0
  zend_fcall_info_args_clear(fci, !args);
4289
4290
0
  if (!args) {
4291
0
    return SUCCESS;
4292
0
  }
4293
4294
0
  if (Z_TYPE_P(args) != IS_ARRAY) {
4295
0
    return FAILURE;
4296
0
  }
4297
4298
0
  fci->param_count = zend_hash_num_elements(Z_ARRVAL_P(args));
4299
0
  fci->params = params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4300
4301
0
  ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), arg) {
4302
0
    if (func && !Z_ISREF_P(arg) && ARG_SHOULD_BE_SENT_BY_REF(func, n)) {
4303
0
      ZVAL_NEW_REF(params, arg);
4304
0
      Z_TRY_ADDREF_P(arg);
4305
0
    } else {
4306
0
      ZVAL_COPY(params, arg);
4307
0
    }
4308
0
    params++;
4309
0
    n++;
4310
0
  } ZEND_HASH_FOREACH_END();
4311
4312
0
  return SUCCESS;
4313
0
}
4314
/* }}} */
4315
4316
ZEND_API zend_result zend_fcall_info_args(zend_fcall_info *fci, zval *args) /* {{{ */
4317
0
{
4318
0
  return zend_fcall_info_args_ex(fci, NULL, args);
4319
0
}
4320
/* }}} */
4321
4322
ZEND_API void zend_fcall_info_argp(zend_fcall_info *fci, uint32_t argc, zval *argv) /* {{{ */
4323
0
{
4324
0
  zend_fcall_info_args_clear(fci, !argc);
4325
4326
0
  if (argc) {
4327
0
    fci->param_count = argc;
4328
0
    fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4329
4330
0
    for (uint32_t i = 0; i < argc; ++i) {
4331
0
      ZVAL_COPY(&fci->params[i], &argv[i]);
4332
0
    }
4333
0
  }
4334
0
}
4335
/* }}} */
4336
4337
ZEND_API void zend_fcall_info_argv(zend_fcall_info *fci, uint32_t argc, va_list *argv) /* {{{ */
4338
0
{
4339
0
  zend_fcall_info_args_clear(fci, !argc);
4340
4341
0
  if (argc) {
4342
0
    zval *arg;
4343
0
    fci->param_count = argc;
4344
0
    fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4345
4346
0
    for (uint32_t i = 0; i < argc; ++i) {
4347
0
      arg = va_arg(*argv, zval *);
4348
0
      ZVAL_COPY(&fci->params[i], arg);
4349
0
    }
4350
0
  }
4351
0
}
4352
/* }}} */
4353
4354
ZEND_API void zend_fcall_info_argn(zend_fcall_info *fci, uint32_t argc, ...) /* {{{ */
4355
0
{
4356
0
  va_list argv;
4357
4358
0
  va_start(argv, argc);
4359
0
  zend_fcall_info_argv(fci, argc, &argv);
4360
0
  va_end(argv);
4361
0
}
4362
/* }}} */
4363
4364
ZEND_API zend_result zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval *retval_ptr, zval *args) /* {{{ */
4365
0
{
4366
0
  zval retval, *org_params = NULL;
4367
0
  uint32_t org_count = 0;
4368
0
  zend_result result;
4369
4370
0
  fci->retval = retval_ptr ? retval_ptr : &retval;
4371
0
  if (args) {
4372
0
    zend_fcall_info_args_save(fci, &org_count, &org_params);
4373
0
    zend_fcall_info_args(fci, args);
4374
0
  }
4375
0
  result = zend_call_function(fci, fcc);
4376
4377
0
  if (!retval_ptr && Z_TYPE(retval) != IS_UNDEF) {
4378
0
    zval_ptr_dtor(&retval);
4379
0
  }
4380
0
  if (args) {
4381
0
    zend_fcall_info_args_restore(fci, org_count, org_params);
4382
0
  }
4383
0
  return result;
4384
0
}
4385
/* }}} */
4386
4387
ZEND_API void zend_get_callable_zval_from_fcc(const zend_fcall_info_cache *fcc, zval *callable)
4388
0
{
4389
0
  if (fcc->closure) {
4390
0
    ZVAL_OBJ_COPY(callable, fcc->closure);
4391
0
  } else if (fcc->function_handler->common.scope) {
4392
0
    array_init(callable);
4393
0
    if (fcc->object) {
4394
0
      GC_ADDREF(fcc->object);
4395
0
      add_next_index_object(callable, fcc->object);
4396
0
    } else {
4397
0
      add_next_index_str(callable, zend_string_copy(fcc->calling_scope->name));
4398
0
    }
4399
0
    add_next_index_str(callable, zend_string_copy(fcc->function_handler->common.function_name));
4400
0
  } else {
4401
0
    ZVAL_STR_COPY(callable, fcc->function_handler->common.function_name);
4402
0
  }
4403
0
}
4404
4405
ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */
4406
0
{
4407
0
  size_t name_len = strlen(module_name);
4408
0
  zend_module_entry *module = zend_hash_str_find_ptr_lc(&module_registry, module_name, name_len);
4409
0
  return module ? module->version : NULL;
4410
0
}
4411
/* }}} */
4412
4413
42.4k
static zend_always_inline bool is_persistent_class(const zend_class_entry *ce) {
4414
42.4k
  return ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->type == MODULE_PERSISTENT;
4415
42.4k
}
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
37.4k
{
4419
37.4k
  zend_property_info *property_info, *property_info_ptr;
4420
4421
37.4k
  if (ZEND_TYPE_IS_SET(type)) {
4422
22.1k
    ce->ce_flags |= ZEND_ACC_HAS_TYPE_HINTS;
4423
4424
22.1k
    if (access_type & ZEND_ACC_READONLY) {
4425
6.11k
      ce->ce_flags |= ZEND_ACC_HAS_READONLY_PROPS;
4426
6.11k
    }
4427
22.1k
  }
4428
4429
37.4k
  if (ce->type == ZEND_INTERNAL_CLASS) {
4430
960
    property_info = pemalloc(sizeof(zend_property_info), 1);
4431
36.5k
  } else {
4432
36.5k
    property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
4433
36.5k
    if (Z_TYPE_P(property) == IS_CONSTANT_AST) {
4434
1.65k
      ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
4435
1.65k
      if (access_type & ZEND_ACC_STATIC) {
4436
511
        ce->ce_flags |= ZEND_ACC_HAS_AST_STATICS;
4437
1.14k
      } else {
4438
1.14k
        ce->ce_flags |= ZEND_ACC_HAS_AST_PROPERTIES;
4439
1.14k
      }
4440
1.65k
    }
4441
36.5k
  }
4442
4443
37.4k
  if (Z_TYPE_P(property) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(property))) {
4444
1.75k
    zval_make_interned_string(property);
4445
1.75k
  }
4446
4447
37.4k
  if (!(access_type & ZEND_ACC_PPP_MASK)) {
4448
1.72k
    access_type |= ZEND_ACC_PUBLIC;
4449
1.72k
  }
4450
  /* Add the protected(set) bit for public readonly properties with no set visibility. */
4451
37.4k
  if ((access_type & (ZEND_ACC_PUBLIC|ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK)) == (ZEND_ACC_PUBLIC|ZEND_ACC_READONLY)) {
4452
6.01k
    access_type |= ZEND_ACC_PROTECTED_SET;
4453
31.4k
  } else if (UNEXPECTED(access_type & ZEND_ACC_PPP_SET_MASK)) {
4454
596
    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
586
    uint32_t get_visibility = zend_visibility_to_set_visibility(access_type & ZEND_ACC_PPP_MASK);
4460
586
    uint32_t set_visibility = access_type & ZEND_ACC_PPP_SET_MASK;
4461
586
    if (get_visibility > set_visibility) {
4462
11
      zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4463
11
        "Visibility of property %s::$%s must not be weaker than set visibility",
4464
11
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
4465
11
    }
4466
    /* Remove equivalent set visibility. */
4467
575
    if (((access_type & (ZEND_ACC_PUBLIC|ZEND_ACC_PUBLIC_SET)) == (ZEND_ACC_PUBLIC|ZEND_ACC_PUBLIC_SET))
4468
405
     || ((access_type & (ZEND_ACC_PROTECTED|ZEND_ACC_PROTECTED_SET)) == (ZEND_ACC_PROTECTED|ZEND_ACC_PROTECTED_SET))
4469
401
     || ((access_type & (ZEND_ACC_PRIVATE|ZEND_ACC_PRIVATE_SET)) == (ZEND_ACC_PRIVATE|ZEND_ACC_PRIVATE_SET))) {
4470
178
      access_type &= ~ZEND_ACC_PPP_SET_MASK;
4471
178
    }
4472
    /* private(set) properties are implicitly final. */
4473
575
    if (access_type & ZEND_ACC_PRIVATE_SET) {
4474
236
      access_type |= ZEND_ACC_FINAL;
4475
236
    }
4476
575
  }
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
37.4k
  if (access_type & ZEND_ACC_VIRTUAL) {
4482
3.02k
    if (Z_TYPE_P(property) == IS_UNDEF) {
4483
2.90k
      property_info->offset = (uint32_t)-1;
4484
2.90k
      goto skip_property_storage;
4485
2.90k
    }
4486
3.02k
  }
4487
34.5k
  if (access_type & ZEND_ACC_STATIC) {
4488
3.62k
    if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
4489
0
      ZEND_ASSERT(property_info_ptr->flags & ZEND_ACC_STATIC);
4490
0
      property_info->offset = property_info_ptr->offset;
4491
0
      zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]);
4492
0
      if (property_info_ptr->doc_comment && property_info_ptr->ce == ce) {
4493
0
        zend_string_release(property_info_ptr->doc_comment);
4494
0
      }
4495
0
      zend_hash_del(&ce->properties_info, name);
4496
3.62k
    } else {
4497
3.62k
      property_info->offset = ce->default_static_members_count++;
4498
3.62k
      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.62k
    }
4500
3.62k
    ZVAL_COPY_VALUE(&ce->default_static_members_table[property_info->offset], property);
4501
3.62k
    if (!ZEND_MAP_PTR(ce->static_members_table)) {
4502
3.62k
      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.62k
    }
4507
30.9k
  } else {
4508
30.9k
    zval *property_default_ptr;
4509
30.9k
    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
30.9k
    } else {
4522
30.9k
      property_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count);
4523
30.9k
      ce->default_properties_count++;
4524
30.9k
      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
30.9k
      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
30.9k
    }
4532
30.9k
    property_default_ptr = &ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
4533
30.9k
    ZVAL_COPY_VALUE(property_default_ptr, property);
4534
30.9k
    Z_PROP_FLAG_P(property_default_ptr) = Z_ISUNDEF_P(property) ? IS_PROP_UNINIT : 0;
4535
30.9k
  }
4536
37.4k
skip_property_storage:
4537
37.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
37.4k
  if (access_type & ZEND_ACC_PUBLIC) {
4549
33.3k
    property_info->name = zend_string_copy(name);
4550
33.3k
  } else if (access_type & ZEND_ACC_PRIVATE) {
4551
2.77k
    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.77k
  } else {
4553
1.28k
    ZEND_ASSERT(access_type & ZEND_ACC_PROTECTED);
4554
1.28k
    property_info->name = zend_mangle_property_name("*", 1, ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
4555
1.28k
  }
4556
4557
37.4k
  property_info->name = zend_new_interned_string(property_info->name);
4558
37.4k
  property_info->flags = access_type;
4559
37.4k
  property_info->doc_comment = doc_comment;
4560
37.4k
  property_info->attributes = NULL;
4561
37.4k
  property_info->prototype = property_info;
4562
37.4k
  property_info->hooks = NULL;
4563
37.4k
  property_info->ce = ce;
4564
37.4k
  property_info->type = type;
4565
4566
37.4k
  if (is_persistent_class(ce)) {
4567
960
    zend_normalize_internal_type(&property_info->type);
4568
960
  }
4569
4570
37.4k
  zend_hash_update_ptr(&ce->properties_info, name, property_info);
4571
4572
37.4k
  return property_info;
4573
37.4k
}
4574
/* }}} */
4575
4576
ZEND_API zend_result zend_try_assign_typed_ref_ex(zend_reference *ref, zval *val, bool strict) /* {{{ */
4577
22
{
4578
22
  if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, val, strict))) {
4579
3
    zval_ptr_dtor(val);
4580
3
    return FAILURE;
4581
19
  } else {
4582
19
    zend_safe_assign_to_variable_noref(&ref->val, val);
4583
19
    return SUCCESS;
4584
19
  }
4585
22
}
4586
/* }}} */
4587
4588
ZEND_API zend_result zend_try_assign_typed_ref(zend_reference *ref, zval *val) /* {{{ */
4589
22
{
4590
22
  return zend_try_assign_typed_ref_ex(ref, val, ZEND_ARG_USES_STRICT_TYPES());
4591
22
}
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
9
{
4650
9
  zval tmp;
4651
4652
9
  ZVAL_STRING(&tmp, string);
4653
9
  return zend_try_assign_typed_ref(ref, &tmp);
4654
9
}
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
13
{
4668
13
  zval tmp;
4669
4670
13
  ZVAL_ARR(&tmp, arr);
4671
13
  return zend_try_assign_typed_ref(ref, &tmp);
4672
13
}
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
15.7k
{
4772
15.7k
  zend_class_constant *c;
4773
4774
15.7k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
4775
414
    if (!(flags & ZEND_ACC_PUBLIC)) {
4776
0
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface constant %s::%s must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4777
0
    }
4778
414
  }
4779
4780
15.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
15.7k
  if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) {
4786
2.13k
    zval_make_interned_string(value);
4787
2.13k
  }
4788
4789
15.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
13.0k
  } else {
4795
13.0k
    c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
4796
13.0k
  }
4797
15.7k
  ZVAL_COPY_VALUE(&c->value, value);
4798
15.7k
  ZEND_CLASS_CONST_FLAGS(c) = flags;
4799
15.7k
  c->doc_comment = doc_comment;
4800
15.7k
  c->attributes = NULL;
4801
15.7k
  c->ce = ce;
4802
15.7k
  c->type = type;
4803
4804
15.7k
  if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
4805
8.99k
    ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
4806
8.99k
    ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
4807
8.99k
    if (ce->type == ZEND_INTERNAL_CLASS && !ZEND_MAP_PTR(ce->mutable_data)) {
4808
80
      ZEND_MAP_PTR_NEW(ce->mutable_data);
4809
80
    }
4810
8.99k
  }
4811
4812
15.7k
  if (!zend_hash_add_ptr(&ce->constants_table, name, c)) {
4813
33
    zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4814
33
      "Cannot redefine class constant %s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4815
33
  }
4816
4817
15.7k
  return c;
4818
15.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.08k
{
4822
7.08k
  return zend_declare_typed_class_constant(ce, name, value, flags, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
4823
7.08k
}
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
933k
{
4892
933k
  const zend_class_entry *old_scope = EG(fake_scope);
4893
4894
933k
  EG(fake_scope) = scope;
4895
4896
933k
  object->handlers->write_property(object, name, value, NULL);
4897
4898
933k
  EG(fake_scope) = old_scope;
4899
933k
}
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
28.8k
{
4904
28.8k
  zend_string *property;
4905
28.8k
  const zend_class_entry *old_scope = EG(fake_scope);
4906
4907
28.8k
  EG(fake_scope) = scope;
4908
4909
28.8k
  property = zend_string_init(name, name_length, 0);
4910
28.8k
  object->handlers->write_property(object, property, value, NULL);
4911
28.8k
  zend_string_release_ex(property, 0);
4912
4913
28.8k
  EG(fake_scope) = old_scope;
4914
28.8k
}
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
44
{
4919
44
  zval tmp;
4920
4921
44
  ZVAL_NULL(&tmp);
4922
44
  zend_update_property(scope, object, name, name_length, &tmp);
4923
44
}
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
2
{
4928
2
  zend_string *property;
4929
2
  const zend_class_entry *old_scope = EG(fake_scope);
4930
4931
2
  EG(fake_scope) = scope;
4932
4933
2
  property = zend_string_init(name, name_length, 0);
4934
2
  object->handlers->unset_property(object, property, 0);
4935
2
  zend_string_release_ex(property, 0);
4936
4937
2
  EG(fake_scope) = old_scope;
4938
2
}
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
40
{
4952
40
  zval tmp;
4953
4954
40
  ZVAL_LONG(&tmp, value);
4955
40
  zend_update_property(scope, object, name, name_length, &tmp);
4956
40
}
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
20
{
4989
20
  zval tmp;
4990
4991
20
  ZVAL_STRINGL(&tmp, value, value_len);
4992
20
  Z_SET_REFCOUNT(tmp, 0);
4993
20
  zend_update_property(scope, object, name, name_length, &tmp);
4994
20
}
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
5
    if (UNEXPECTED(zend_update_class_constants(scope) != SUCCESS)) {
5004
0
      return FAILURE;
5005
0
    }
5006
5
  }
5007
5008
7
  const zend_class_entry *old_scope = EG(fake_scope);
5009
7
  EG(fake_scope) = scope;
5010
7
  property = zend_std_get_static_property_with_info(scope, name, BP_VAR_W, &prop_info);
5011
7
  EG(fake_scope) = old_scope;
5012
5013
7
  if (!property) {
5014
0
    return FAILURE;
5015
0
  }
5016
5017
7
  ZEND_ASSERT(!Z_ISREF_P(value));
5018
7
  Z_TRY_ADDREF_P(value);
5019
7
  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
7
  zend_assign_to_variable(property, value, IS_TMP_VAR, /* strict */ 0);
5029
7
  return SUCCESS;
5030
7
}
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
14.0M
{
5100
14.0M
  zval *value;
5101
14.0M
  const zend_class_entry *old_scope = EG(fake_scope);
5102
5103
14.0M
  EG(fake_scope) = scope;
5104
5105
14.0M
  value = object->handlers->read_property(object, name, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
5106
5107
14.0M
  EG(fake_scope) = old_scope;
5108
14.0M
  return value;
5109
14.0M
}
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
10
{
5126
10
  zval *property;
5127
10
  const zend_class_entry *old_scope = EG(fake_scope);
5128
5129
10
  EG(fake_scope) = scope;
5130
10
  property = zend_std_get_static_property(scope, name, silent ? BP_VAR_IS : BP_VAR_R);
5131
10
  EG(fake_scope) = old_scope;
5132
5133
10
  return property;
5134
10
}
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
17
{
5148
17
  current->handling = EG(error_handling);
5149
17
  current->exception = EG(exception_class);
5150
17
}
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
17
{
5155
17
  if (current) {
5156
17
    zend_save_error_handling(current);
5157
17
  }
5158
17
  ZEND_ASSERT(error_handling == EH_THROW || exception_class == NULL);
5159
17
  EG(error_handling) = error_handling;
5160
17
  EG(exception_class) = exception_class;
5161
17
}
5162
/* }}} */
5163
5164
ZEND_API void zend_restore_error_handling(const zend_error_handling *saved) /* {{{ */
5165
17
{
5166
17
  EG(error_handling) = saved->handling;
5167
17
  EG(exception_class) = saved->exception;
5168
17
}
5169
/* }}} */
5170
5171
ZEND_API ZEND_COLD const char *zend_get_object_type_case(const zend_class_entry *ce, bool upper_case) /* {{{ */
5172
458
{
5173
458
  if (ce->ce_flags & ZEND_ACC_TRAIT) {
5174
9
    return upper_case ? "Trait" : "trait";
5175
449
  } else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
5176
70
    return upper_case ? "Interface" : "interface";
5177
379
  } else if (ce->ce_flags & ZEND_ACC_ENUM) {
5178
53
    return upper_case ? "Enum" : "enum";
5179
326
  } else {
5180
326
    return upper_case ? "Class" : "class";
5181
326
  }
5182
458
}
5183
/* }}} */
5184
5185
ZEND_API bool zend_is_iterable(const zval *iterable) /* {{{ */
5186
61
{
5187
61
  switch (Z_TYPE_P(iterable)) {
5188
0
    case IS_ARRAY:
5189
0
      return 1;
5190
55
    case IS_OBJECT:
5191
55
      return zend_class_implements_interface(Z_OBJCE_P(iterable), zend_ce_traversable);
5192
6
    default:
5193
6
      return 0;
5194
61
  }
5195
61
}
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
16
static zend_result get_default_via_ast(zval *default_value_zval, const char *default_value) {
5216
16
  zend_ast *ast;
5217
16
  zend_arena *ast_arena;
5218
5219
16
  zend_string *code = zend_string_concat3(
5220
16
    "<?php ", sizeof("<?php ") - 1, default_value, strlen(default_value), ";", 1);
5221
5222
16
  ast = zend_compile_string_to_ast(code, &ast_arena, ZSTR_EMPTY_ALLOC());
5223
16
  zend_string_release(code);
5224
5225
16
  if (!ast) {
5226
0
    return FAILURE;
5227
0
  }
5228
5229
16
  zend_ast_list *statement_list = zend_ast_get_list(ast);
5230
16
  zend_ast **const_expr_ast_ptr = &statement_list->child[0];
5231
5232
16
  zend_arena *original_ast_arena = CG(ast_arena);
5233
16
  uint32_t original_compiler_options = CG(compiler_options);
5234
16
  zend_file_context original_file_context;
5235
16
  CG(ast_arena) = ast_arena;
5236
  /* Disable constant substitution, to make getDefaultValueConstant() work. */
5237
16
  CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
5238
16
  zend_file_context_begin(&original_file_context);
5239
16
  zend_const_expr_to_zval(default_value_zval, const_expr_ast_ptr, /* allow_dynamic */ true);
5240
16
  CG(ast_arena) = original_ast_arena;
5241
16
  CG(compiler_options) = original_compiler_options;
5242
16
  zend_file_context_end(&original_file_context);
5243
5244
16
  zend_ast_destroy(ast);
5245
16
  zend_arena_destroy(ast_arena);
5246
5247
16
  return SUCCESS;
5248
16
}
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
150
{
5266
150
  const zend_string *default_value = arg_info->default_value;
5267
150
  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
143
  zend_ulong lval;
5273
143
  if (zend_string_equals_literal(default_value, "null")) {
5274
96
    ZVAL_NULL(default_value_zval);
5275
96
    return SUCCESS;
5276
96
  } else if (zend_string_equals_literal(default_value, "true")) {
5277
0
    ZVAL_TRUE(default_value_zval);
5278
0
    return SUCCESS;
5279
47
  } else if (zend_string_equals_literal(default_value, "false")) {
5280
26
    ZVAL_FALSE(default_value_zval);
5281
26
    return SUCCESS;
5282
26
  } else if (ZSTR_LEN(default_value) >= 2
5283
21
      && (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
16
  } else if (zend_string_equals_literal(default_value, "[]")) {
5292
0
    ZVAL_EMPTY_ARRAY(default_value_zval);
5293
0
    return SUCCESS;
5294
16
  } 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
16
  return get_default_via_ast(default_value_zval, ZSTR_VAL(default_value));
5303
143
}