Coverage Report

Created: 2025-12-14 06:06

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