Coverage Report

Created: 2026-01-18 06:47

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