Coverage Report

Created: 2026-06-02 06:36

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 © Zend Technologies Ltd., a subsidiary company of          |
6
   |     Perforce Software, Inc., and Contributors.                       |
7
   +----------------------------------------------------------------------+
8
   | This source file is subject to the Modified BSD License that is      |
9
   | bundled with this package in the file LICENSE, and is available      |
10
   | through the World Wide Web at <https://www.php.net/license/>.        |
11
   |                                                                      |
12
   | SPDX-License-Identifier: BSD-3-Clause                                |
13
   +----------------------------------------------------------------------+
14
   | Authors: Andi Gutmans <andi@php.net>                                 |
15
   |          Zeev Suraski <zeev@php.net>                                 |
16
   |          Andrei Zmievski <andrei@php.net>                            |
17
   |          Dmitry Stogov <dmitry@php.net>                              |
18
   +----------------------------------------------------------------------+
19
*/
20
21
#include "zend.h"
22
#include "zend_compile.h"
23
#include "zend_execute.h"
24
#include "zend_API.h"
25
#include "zend_hash.h"
26
#include "zend_modules.h"
27
#include "zend_extensions.h"
28
#include "zend_constants.h"
29
#include "zend_interfaces.h"
30
#include "zend_exceptions.h"
31
#include "zend_closures.h"
32
#include "zend_inheritance.h"
33
#include "zend_ini.h"
34
#include "zend_enum.h"
35
#include "zend_object_handlers.h"
36
#include "zend_observer.h"
37
38
#include <stdarg.h>
39
40
/* these variables are true statics/globals, and have to be mutex'ed on every access */
41
ZEND_API HashTable module_registry;
42
43
ZEND_API bool zend_dl_use_deepbind = false;
44
45
static zend_module_entry **module_request_startup_handlers;
46
static zend_module_entry **module_request_shutdown_handlers;
47
static zend_module_entry **module_post_deactivate_handlers;
48
static zend_module_entry **modules_dl_loaded;
49
50
static zend_class_entry  **class_cleanup_handlers;
51
52
ZEND_API void zend_set_dl_use_deepbind(bool use_deepbind)
53
0
{
54
0
  zend_dl_use_deepbind = use_deepbind;
55
0
}
56
57
ZEND_API zend_result zend_get_parameters_array_ex(uint32_t param_count, zval *argument_array) /* {{{ */
58
0
{
59
0
  zval *param_ptr;
60
0
  uint32_t arg_count;
61
62
0
  param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
63
0
  arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
64
65
0
  if (param_count>arg_count) {
66
0
    return FAILURE;
67
0
  }
68
69
0
  while (param_count-->0) {
70
0
    ZVAL_COPY_VALUE(argument_array, param_ptr);
71
0
    argument_array++;
72
0
    param_ptr++;
73
0
  }
74
75
0
  return SUCCESS;
76
0
}
77
/* }}} */
78
79
ZEND_API ZEND_COLD void zend_wrong_param_count(void) /* {{{ */
80
0
{
81
0
  const char *space;
82
0
  const char *class_name = get_active_class_name(&space);
83
84
0
  zend_argument_count_error("Wrong parameter count for %s%s%s()", class_name, space, get_active_function_name());
85
0
}
86
/* }}} */
87
88
ZEND_API ZEND_COLD void zend_wrong_property_read(const zval *object, zval *property)
89
0
{
90
0
  zend_string *tmp_property_name;
91
0
  zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name);
92
0
  zend_error(E_WARNING, "Attempt to read property \"%s\" on %s", ZSTR_VAL(property_name), zend_zval_value_name(object));
93
0
  zend_tmp_string_release(tmp_property_name);
94
0
}
95
96
/* Argument parsing API -- andrei */
97
ZEND_API const char *zend_get_type_by_const(int type) /* {{{ */
98
0
{
99
0
  switch(type) {
100
0
    case IS_FALSE:
101
0
    case IS_TRUE:
102
0
    case _IS_BOOL:
103
0
      return "bool";
104
0
    case IS_LONG:
105
0
      return "int";
106
0
    case IS_DOUBLE:
107
0
      return "float";
108
0
    case IS_STRING:
109
0
      return "string";
110
0
    case IS_OBJECT:
111
0
      return "object";
112
0
    case IS_RESOURCE:
113
0
      return "resource";
114
0
    case IS_NULL:
115
0
      return "null";
116
0
    case IS_CALLABLE:
117
0
      return "callable";
118
0
    case IS_ITERABLE:
119
0
      return "iterable";
120
0
    case IS_ARRAY:
121
0
      return "array";
122
0
    case IS_VOID:
123
0
      return "void";
124
0
    case IS_MIXED:
125
0
      return "mixed";
126
0
    case _IS_NUMBER:
127
0
      return "int|float";
128
0
    default: ZEND_UNREACHABLE();
129
0
  }
130
0
}
131
/* }}} */
132
133
ZEND_API const char *zend_zval_value_name(const zval *arg)
134
0
{
135
0
  ZVAL_DEREF(arg);
136
137
0
  if (Z_ISUNDEF_P(arg)) {
138
0
    return "null";
139
0
  }
140
141
0
  if (Z_TYPE_P(arg) == IS_OBJECT) {
142
0
    return ZSTR_VAL(Z_OBJCE_P(arg)->name);
143
0
  } else if (Z_TYPE_P(arg) == IS_FALSE) {
144
0
    return "false";
145
0
  } else if  (Z_TYPE_P(arg) == IS_TRUE) {
146
0
    return "true";
147
0
  }
148
149
0
  return zend_get_type_by_const(Z_TYPE_P(arg));
150
0
}
151
152
ZEND_API const char *zend_zval_type_name(const zval *arg)
153
0
{
154
0
  ZVAL_DEREF(arg);
155
156
0
  if (Z_ISUNDEF_P(arg)) {
157
0
    return "null";
158
0
  }
159
160
0
  if (Z_TYPE_P(arg) == IS_OBJECT) {
161
0
    return ZSTR_VAL(Z_OBJCE_P(arg)->name);
162
0
  }
163
164
0
  return zend_get_type_by_const(Z_TYPE_P(arg));
165
0
}
166
167
/* This API exists *only* for use in gettype().
168
 * For anything else, you likely want zend_zval_type_name(). */
169
ZEND_API zend_string *zend_zval_get_legacy_type(const zval *arg) /* {{{ */
170
0
{
171
0
  switch (Z_TYPE_P(arg)) {
172
0
    case IS_NULL:
173
0
      return ZSTR_KNOWN(ZEND_STR_NULL);
174
0
    case IS_FALSE:
175
0
    case IS_TRUE:
176
0
      return ZSTR_KNOWN(ZEND_STR_BOOLEAN);
177
0
    case IS_LONG:
178
0
      return ZSTR_KNOWN(ZEND_STR_INTEGER);
179
0
    case IS_DOUBLE:
180
0
      return ZSTR_KNOWN(ZEND_STR_DOUBLE);
181
0
    case IS_STRING:
182
0
      return ZSTR_KNOWN(ZEND_STR_STRING);
183
0
    case IS_ARRAY:
184
0
      return ZSTR_KNOWN(ZEND_STR_ARRAY);
185
0
    case IS_OBJECT:
186
0
      return ZSTR_KNOWN(ZEND_STR_OBJECT);
187
0
    case IS_RESOURCE:
188
0
      if (zend_rsrc_list_get_rsrc_type(Z_RES_P(arg))) {
189
0
        return ZSTR_KNOWN(ZEND_STR_RESOURCE);
190
0
      } else {
191
0
        return ZSTR_KNOWN(ZEND_STR_CLOSED_RESOURCE);
192
0
      }
193
0
    default:
194
0
      return NULL;
195
0
  }
196
0
}
197
/* }}} */
198
199
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_none_error(void) /* {{{ */
200
0
{
201
0
  int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
202
0
  zend_string *func_name = get_active_function_or_method_name();
203
204
0
  zend_argument_count_error("%s() expects exactly 0 arguments, %d given", ZSTR_VAL(func_name), num_args);
205
206
0
  zend_string_release(func_name);
207
0
}
208
/* }}} */
209
210
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_error(uint32_t min_num_args, uint32_t max_num_args) /* {{{ */
211
0
{
212
0
  uint32_t num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
213
0
  zend_string *func_name = get_active_function_or_method_name();
214
215
0
  zend_argument_count_error(
216
0
    "%s() expects %s %d argument%s, %d given",
217
0
    ZSTR_VAL(func_name),
218
0
    min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
219
0
    num_args < min_num_args ? min_num_args : max_num_args,
220
0
    (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
221
0
    num_args
222
0
  );
223
224
0
  zend_string_release(func_name);
225
0
}
226
/* }}} */
227
228
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_error(int error_code, uint32_t num, char *name, zend_expected_type expected_type, const zval *arg) /* {{{ */
229
0
{
230
0
  switch (error_code) {
231
0
    case ZPP_ERROR_WRONG_CALLBACK:
232
0
      zend_wrong_callback_error(num, name);
233
0
      break;
234
0
    case ZPP_ERROR_WRONG_CALLBACK_OR_NULL:
235
0
      zend_wrong_callback_or_null_error(num, name);
236
0
      break;
237
0
    case ZPP_ERROR_WRONG_CLASS:
238
0
      zend_wrong_parameter_class_error(num, name, arg);
239
0
      break;
240
0
    case ZPP_ERROR_WRONG_CLASS_OR_NULL:
241
0
      zend_wrong_parameter_class_or_null_error(num, name, arg);
242
0
      break;
243
0
    case ZPP_ERROR_WRONG_CLASS_OR_STRING:
244
0
      zend_wrong_parameter_class_or_string_error(num, name, arg);
245
0
      break;
246
0
    case ZPP_ERROR_WRONG_CLASS_OR_STRING_OR_NULL:
247
0
      zend_wrong_parameter_class_or_string_or_null_error(num, name, arg);
248
0
      break;
249
0
    case ZPP_ERROR_WRONG_CLASS_OR_LONG:
250
0
      zend_wrong_parameter_class_or_long_error(num, name, arg);
251
0
      break;
252
0
    case ZPP_ERROR_WRONG_CLASS_OR_LONG_OR_NULL:
253
0
      zend_wrong_parameter_class_or_long_or_null_error(num, name, arg);
254
0
      break;
255
0
    case ZPP_ERROR_WRONG_ARG:
256
0
      zend_wrong_parameter_type_error(num, expected_type, arg);
257
0
      break;
258
0
    case ZPP_ERROR_UNEXPECTED_EXTRA_NAMED:
259
0
      zend_unexpected_extra_named_error();
260
0
      break;
261
0
    case ZPP_ERROR_FAILURE:
262
0
      ZEND_ASSERT(EG(exception) && "Should have produced an error already");
263
0
      break;
264
0
    default: ZEND_UNREACHABLE();
265
0
  }
266
0
}
267
/* }}} */
268
269
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_type_error(uint32_t num, zend_expected_type expected_type, const zval *arg) /* {{{ */
270
0
{
271
0
  static const char * const expected_error[] = {
272
0
    Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR)
273
0
    NULL
274
0
  };
275
276
0
  if (EG(exception)) {
277
0
    return;
278
0
  }
279
280
0
  if ((expected_type == Z_EXPECTED_PATH || expected_type == Z_EXPECTED_PATH_OR_NULL)
281
0
      && Z_TYPE_P(arg) == IS_STRING) {
282
0
    zend_argument_value_error(num, "must not contain any null bytes");
283
0
    return;
284
0
  }
285
286
0
  zend_argument_type_error(num, "must be %s, %s given", expected_error[expected_type], zend_zval_value_name(arg));
287
0
}
288
/* }}} */
289
290
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_error(uint32_t num, const char *name, const zval *arg) /* {{{ */
291
0
{
292
0
  if (EG(exception)) {
293
0
    return;
294
0
  }
295
296
0
  zend_argument_type_error(num, "must be of type %s, %s given", name, zend_zval_value_name(arg));
297
0
}
298
/* }}} */
299
300
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_null_error(uint32_t num, const char *name, const zval *arg) /* {{{ */
301
0
{
302
0
  if (EG(exception)) {
303
0
    return;
304
0
  }
305
306
0
  zend_argument_type_error(num, "must be of type ?%s, %s given", name, zend_zval_value_name(arg));
307
0
}
308
/* }}} */
309
310
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_long_error(uint32_t num, const char *name, const zval *arg) /* {{{ */
311
0
{
312
0
  if (EG(exception)) {
313
0
    return;
314
0
  }
315
316
0
  zend_argument_type_error(num, "must be of type %s|int, %s given", name, zend_zval_value_name(arg));
317
0
}
318
/* }}} */
319
320
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_long_or_null_error(uint32_t num, const char *name, const zval *arg) /* {{{ */
321
0
{
322
0
  if (EG(exception)) {
323
0
    return;
324
0
  }
325
326
0
  zend_argument_type_error(num, "must be of type %s|int|null, %s given", name, zend_zval_value_name(arg));
327
0
}
328
/* }}} */
329
330
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_string_error(uint32_t num, const char *name, const zval *arg) /* {{{ */
331
0
{
332
0
  if (EG(exception)) {
333
0
    return;
334
0
  }
335
336
0
  zend_argument_type_error(num, "must be of type %s|string, %s given", name, zend_zval_value_name(arg));
337
0
}
338
/* }}} */
339
340
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_string_or_null_error(uint32_t num, const char *name, const zval *arg) /* {{{ */
341
0
{
342
0
  if (EG(exception)) {
343
0
    return;
344
0
  }
345
346
0
  zend_argument_type_error(num, "must be of type %s|string|null, %s given", name, zend_zval_value_name(arg));
347
0
}
348
/* }}} */
349
350
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_error(uint32_t num, char *error) /* {{{ */
351
0
{
352
0
  if (!EG(exception)) {
353
0
    zend_argument_type_error(num, "must be a valid callback, %s", error);
354
0
  }
355
0
  efree(error);
356
0
}
357
/* }}} */
358
359
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_or_null_error(uint32_t num, char *error) /* {{{ */
360
0
{
361
0
  if (!EG(exception)) {
362
0
    zend_argument_type_error(num, "must be a valid callback or null, %s", error);
363
0
  }
364
0
  efree(error);
365
0
}
366
/* }}} */
367
368
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_unexpected_extra_named_error(void)
369
0
{
370
0
  const char *space;
371
0
  const char *class_name = get_active_class_name(&space);
372
0
  zend_argument_count_error("Internal function %s%s%s() does not accept named variadic arguments",
373
0
    class_name, space, get_active_function_name()
374
0
  );
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 zpp_parse_bool_status ZEND_FASTCALL zend_parse_arg_bool_weak(const zval *arg, 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 ZPP_PARSE_BOOL_STATUS_ERROR;
517
0
    }
518
0
    return zend_is_true(arg);
519
0
  }
520
0
  return ZPP_PARSE_BOOL_STATUS_ERROR;
521
0
}
522
/* }}} */
523
524
ZEND_API zpp_parse_bool_status ZEND_FASTCALL zend_parse_arg_bool_slow(const zval *arg, uint32_t arg_num) /* {{{ */
525
0
{
526
0
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
527
0
    return ZPP_PARSE_BOOL_STATUS_ERROR;
528
0
  }
529
0
  return zend_parse_arg_bool_weak(arg, arg_num);
530
0
}
531
/* }}} */
532
533
ZEND_API zpp_parse_bool_status ZEND_FASTCALL zend_flf_parse_arg_bool_slow(const zval *arg, uint32_t arg_num)
534
0
{
535
0
  if (UNEXPECTED(ZEND_FLF_ARG_USES_STRICT_TYPES())) {
536
0
    return ZPP_PARSE_BOOL_STATUS_ERROR;
537
0
  }
538
0
  return zend_parse_arg_bool_weak(arg, arg_num);
539
0
}
540
541
ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_weak(const zval *arg, zend_long *dest, uint32_t arg_num) /* {{{ */
542
0
{
543
0
  if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
544
0
    if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
545
0
      return 0;
546
0
    }
547
0
    if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
548
0
      return 0;
549
0
    } else {
550
0
      zend_long lval = zend_dval_to_lval(Z_DVAL_P(arg));
551
0
      if (UNEXPECTED(!zend_is_long_compatible(Z_DVAL_P(arg), lval))) {
552
        /* Check arg_num is not (uint32_t)-1, as otherwise its called by
553
         * zend_verify_weak_scalar_type_hint_no_sideeffect() */
554
0
        if (arg_num != (uint32_t)-1) {
555
0
          zend_incompatible_double_to_long_error(Z_DVAL_P(arg));
556
0
        }
557
0
        if (UNEXPECTED(EG(exception))) {
558
0
          return 0;
559
0
        }
560
0
      }
561
0
      *dest = lval;
562
0
    }
563
0
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
564
0
    double d;
565
0
    uint8_t type;
566
567
0
    if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
568
0
      if (EXPECTED(type != 0)) {
569
0
        zend_long lval;
570
0
        if (UNEXPECTED(zend_isnan(d))) {
571
0
          return 0;
572
0
        }
573
0
        if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
574
0
          return 0;
575
0
        }
576
577
0
        lval = zend_dval_to_lval(d);
578
        /* This only checks for a fractional part as if doesn't fit it already throws a TypeError */
579
0
        if (UNEXPECTED(!zend_is_long_compatible(d, lval))) {
580
          /* Check arg_num is not (uint32_t)-1, as otherwise its called by
581
           * zend_verify_weak_scalar_type_hint_no_sideeffect() */
582
0
          if (arg_num != (uint32_t)-1) {
583
0
            zend_incompatible_string_to_long_error(Z_STR_P(arg));
584
0
          }
585
0
          if (UNEXPECTED(EG(exception))) {
586
0
            return 0;
587
0
          }
588
0
        }
589
0
        *dest = lval;
590
0
      } else {
591
0
        return 0;
592
0
      }
593
0
    }
594
0
  } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
595
0
    if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("int", arg_num)) {
596
0
      return 0;
597
0
    }
598
0
    *dest = 0;
599
0
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
600
0
    *dest = 1;
601
0
  } else {
602
0
    return 0;
603
0
  }
604
0
  return 1;
605
0
}
606
/* }}} */
607
608
ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_slow(const zval *arg, zend_long *dest, uint32_t arg_num) /* {{{ */
609
0
{
610
0
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
611
0
    return 0;
612
0
  }
613
0
  return zend_parse_arg_long_weak(arg, dest, arg_num);
614
0
}
615
/* }}} */
616
617
ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_long_slow(const zval *arg, zend_long *dest, uint32_t arg_num)
618
0
{
619
0
  if (UNEXPECTED(ZEND_FLF_ARG_USES_STRICT_TYPES())) {
620
0
    return 0;
621
0
  }
622
0
  return zend_parse_arg_long_weak(arg, dest, arg_num);
623
0
}
624
625
ZEND_API double ZEND_FASTCALL zend_parse_arg_double_weak(const zval *arg, uint32_t arg_num) /* {{{ */
626
0
{
627
0
  if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
628
0
    return (double)Z_LVAL_P(arg);
629
0
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
630
0
    zend_long l;
631
0
    double dval;
632
0
    uint8_t type;
633
634
0
    if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), &l, &dval)) != IS_DOUBLE)) {
635
0
      if (EXPECTED(type != 0)) {
636
0
        return (double)(l);
637
0
      } else {
638
0
        return NAN;
639
0
      }
640
0
    } else {
641
0
      return dval;
642
0
    }
643
0
  } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
644
0
    if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("float", arg_num)) {
645
0
      return NAN;
646
0
    }
647
0
    return 0.0;
648
0
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
649
0
    return 1.0;
650
0
  } else {
651
0
    return NAN;
652
0
  }
653
0
}
654
/* }}} */
655
656
ZEND_API double ZEND_FASTCALL zend_parse_arg_double_slow(const zval *arg, 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
    return (double)Z_LVAL_P(arg);
661
0
  } else if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
662
0
    return NAN;
663
0
  }
664
0
  return zend_parse_arg_double_weak(arg, 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 zend_string* ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, 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 NULL;
736
0
    }
737
0
    convert_to_string(arg);
738
0
    return 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
      return Z_STR_P(arg);
746
0
    }
747
0
    return NULL;
748
0
  } else {
749
0
    return NULL;
750
0
  }
751
0
}
752
/* }}} */
753
754
ZEND_API zend_string* ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, uint32_t arg_num) /* {{{ */
755
0
{
756
0
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
757
0
    return NULL;
758
0
  }
759
0
  return zend_parse_arg_str_weak(arg, arg_num);
760
0
}
761
/* }}} */
762
763
ZEND_API zend_string* ZEND_FASTCALL zend_flf_parse_arg_str_slow(zval *arg, uint32_t arg_num)
764
0
{
765
0
  if (UNEXPECTED(ZEND_FLF_ARG_USES_STRICT_TYPES())) {
766
0
    return NULL;
767
0
  }
768
0
  return zend_parse_arg_str_weak(arg, arg_num);
769
0
}
770
771
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) /* {{{ */
772
0
{
773
0
  zend_string *str;
774
0
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
775
0
    return 0;
776
0
  }
777
0
  if (zend_parse_arg_long_weak(arg, dest_long, arg_num)) {
778
0
    *dest_str = NULL;
779
0
    return 1;
780
0
  } else if ((str = zend_parse_arg_str_weak(arg, arg_num)) != NULL) {
781
0
    *dest_long = 0;
782
0
    *dest_str = str;
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
0
{
1647
0
  if (class_type->default_properties_count) {
1648
0
    zval *src = CE_DEFAULT_PROPERTIES_TABLE(class_type);
1649
0
    zval *dst = object->properties_table;
1650
0
    zval *end = src + class_type->default_properties_count;
1651
1652
0
    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
0
      do {
1656
0
        ZEND_ASSERT(!Z_REFCOUNTED_P(src));
1657
0
        ZVAL_COPY_VALUE_PROP(dst, src);
1658
0
        src++;
1659
0
        dst++;
1660
0
      } while (src != end);
1661
0
    } 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
0
  }
1669
0
}
1670
/* }}} */
1671
1672
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1673
0
{
1674
0
  object->properties = NULL;
1675
0
  _object_properties_init(object, class_type);
1676
0
}
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
0
{
1794
0
  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
0
  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
0
  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
0
  } else {
1828
0
    ZVAL_OBJ(arg, class_type->create_object(class_type));
1829
0
  }
1830
0
  return SUCCESS;
1831
0
}
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
0
{
1842
0
  return _object_and_properties_init(arg, class_type, NULL);
1843
0
}
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
8.58k
{
1921
8.58k
  zval tmp;
1922
1923
8.58k
  ZVAL_LONG(&tmp, n);
1924
8.58k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1925
8.58k
}
1926
/* }}} */
1927
1928
ZEND_API void add_assoc_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
1929
11.9k
{
1930
11.9k
  zval tmp;
1931
1932
11.9k
  ZVAL_NULL(&tmp);
1933
11.9k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1934
11.9k
}
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
1.02k
{
1957
1.02k
  zval tmp;
1958
1959
1.02k
  ZVAL_DOUBLE(&tmp, d);
1960
1.02k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1961
1.02k
}
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
6.23k
{
1975
6.23k
  zval tmp;
1976
1977
6.23k
  ZVAL_STRING(&tmp, str);
1978
6.23k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1979
6.23k
}
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
27.8k
{
1984
27.8k
  zval tmp;
1985
1986
27.8k
  ZVAL_STRINGL(&tmp, str, length);
1987
27.8k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1988
27.8k
}
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
22.4k
{
2020
22.4k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value);
2021
22.4k
}
2022
/* }}} */
2023
2024
ZEND_API void add_index_long(zval *arg, zend_ulong index, zend_long n) /* {{{ */
2025
4.60M
{
2026
4.60M
  zval tmp;
2027
2028
4.60M
  ZVAL_LONG(&tmp, n);
2029
4.60M
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2030
4.60M
}
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
355k
{
2062
355k
  zval tmp;
2063
2064
355k
  ZVAL_DOUBLE(&tmp, d);
2065
355k
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2066
355k
}
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
508k
{
2080
508k
  zval tmp;
2081
2082
508k
  ZVAL_STRING(&tmp, str);
2083
508k
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2084
508k
}
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
22
    while (dep->name) {
2398
14
      if (dep->type == MODULE_DEP_REQUIRED) {
2399
10
        zend_module_entry *req_mod;
2400
2401
10
        name_len = strlen(dep->name);
2402
10
        lcname = zend_string_alloc(name_len, 0);
2403
10
        zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
2404
2405
10
        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
10
        zend_string_efree(lcname);
2413
10
      }
2414
14
      ++dep;
2415
14
    }
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
    }
2433
26
    EG(current_module) = NULL;
2434
26
  }
2435
26
  return SUCCESS;
2436
26
}
2437
/* }}} */
2438
2439
static int zend_startup_module_zval(zval *zv) /* {{{ */
2440
26
{
2441
26
  zend_module_entry *module = Z_PTR_P(zv);
2442
2443
26
  return (zend_startup_module_ex(module) == SUCCESS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
2444
26
}
2445
/* }}} */
2446
2447
static void zend_sort_modules(void *base, size_t count, size_t siz, compare_func_t compare, swap_func_t swp) /* {{{ */
2448
2
{
2449
2
  Bucket *b1 = base;
2450
2
  Bucket *b2;
2451
2
  Bucket *end = b1 + count;
2452
2
  Bucket tmp;
2453
2
  zend_module_entry *m, *r;
2454
2455
28
  while (b1 < end) {
2456
34
try_again:
2457
34
    m = (zend_module_entry*)Z_PTR(b1->val);
2458
34
    if (!m->module_started && m->deps) {
2459
16
      const zend_module_dep *dep = m->deps;
2460
32
      while (dep->name) {
2461
24
        if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) {
2462
24
          b2 = b1 + 1;
2463
82
          while (b2 < end) {
2464
66
            r = (zend_module_entry*)Z_PTR(b2->val);
2465
66
            if (strcasecmp(dep->name, r->name) == 0) {
2466
8
              tmp = *b1;
2467
8
              *b1 = *b2;
2468
8
              *b2 = tmp;
2469
8
              goto try_again;
2470
8
            }
2471
58
            b2++;
2472
58
          }
2473
24
        }
2474
16
        dep++;
2475
16
      }
2476
16
    }
2477
26
    b1++;
2478
26
  }
2479
2
}
2480
/* }}} */
2481
2482
ZEND_API void zend_collect_module_handlers(void) /* {{{ */
2483
2
{
2484
2
  zend_module_entry *module;
2485
2
  int startup_count = 0;
2486
2
  int shutdown_count = 0;
2487
2
  int post_deactivate_count = 0;
2488
2
  int dl_loaded_count = 0;
2489
2
  zend_class_entry *ce;
2490
2
  int class_count = 0;
2491
2492
  /* Collect extensions with request startup/shutdown handlers */
2493
56
  ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
2494
56
    if (module->request_startup_func) {
2495
16
      startup_count++;
2496
16
    }
2497
56
    if (module->request_shutdown_func) {
2498
8
      shutdown_count++;
2499
8
    }
2500
56
    if (module->post_deactivate_func) {
2501
6
      post_deactivate_count++;
2502
6
    }
2503
56
    if (module->handle) {
2504
0
      dl_loaded_count++;
2505
0
    }
2506
56
  } ZEND_HASH_FOREACH_END();
2507
2
  module_request_startup_handlers = (zend_module_entry**)perealloc(
2508
2
    module_request_startup_handlers,
2509
2
      sizeof(zend_module_entry*) *
2510
2
    (startup_count + 1 +
2511
2
     shutdown_count + 1 +
2512
2
     post_deactivate_count + 1), true);
2513
2
  module_request_startup_handlers[startup_count] = NULL;
2514
2
  module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
2515
2
  module_request_shutdown_handlers[shutdown_count] = NULL;
2516
2
  module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
2517
2
  module_post_deactivate_handlers[post_deactivate_count] = NULL;
2518
  /* Cannot reuse module_request_startup_handlers because it is freed in zend_destroy_modules, which happens before zend_unload_modules. */
2519
2
  modules_dl_loaded = perealloc(modules_dl_loaded, sizeof(zend_module_entry*) * (dl_loaded_count + 1), true);
2520
2
  modules_dl_loaded[dl_loaded_count] = NULL;
2521
2
  startup_count = 0;
2522
2523
56
  ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
2524
56
    if (module->request_startup_func) {
2525
16
      module_request_startup_handlers[startup_count++] = module;
2526
16
    }
2527
56
    if (module->request_shutdown_func) {
2528
8
      module_request_shutdown_handlers[--shutdown_count] = module;
2529
8
    }
2530
56
    if (module->post_deactivate_func) {
2531
6
      module_post_deactivate_handlers[--post_deactivate_count] = module;
2532
6
    }
2533
56
    if (module->handle) {
2534
0
      modules_dl_loaded[--dl_loaded_count] = module;
2535
0
    }
2536
56
  } ZEND_HASH_FOREACH_END();
2537
2538
  /* Collect internal classes with static members */
2539
700
  ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
2540
700
    if (ce->type == ZEND_INTERNAL_CLASS &&
2541
348
        ce->default_static_members_count > 0) {
2542
0
        class_count++;
2543
0
    }
2544
700
  } ZEND_HASH_FOREACH_END();
2545
2546
2
  class_cleanup_handlers = (zend_class_entry**)perealloc(
2547
2
    class_cleanup_handlers,
2548
2
    sizeof(zend_class_entry*) *
2549
2
    (class_count + 1), true);
2550
2
  class_cleanup_handlers[class_count] = NULL;
2551
2552
2
  if (class_count) {
2553
0
    ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
2554
0
      if (ce->type == ZEND_INTERNAL_CLASS &&
2555
0
          ce->default_static_members_count > 0) {
2556
0
          class_cleanup_handlers[--class_count] = ce;
2557
0
      }
2558
0
    } ZEND_HASH_FOREACH_END();
2559
0
  }
2560
2
}
2561
/* }}} */
2562
2563
ZEND_API void zend_startup_modules(void) /* {{{ */
2564
2
{
2565
2
  zend_hash_sort_ex(&module_registry, zend_sort_modules, NULL, 0);
2566
2
  zend_hash_apply(&module_registry, zend_startup_module_zval);
2567
2
}
2568
/* }}} */
2569
2570
ZEND_API void zend_destroy_modules(void) /* {{{ */
2571
0
{
2572
0
  free(class_cleanup_handlers);
2573
0
  class_cleanup_handlers = NULL;
2574
0
  free(module_request_startup_handlers);
2575
0
  module_request_startup_handlers = NULL;
2576
0
  zend_hash_graceful_reverse_destroy(&module_registry);
2577
0
}
2578
/* }}} */
2579
2580
ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module, int module_type) /* {{{ */
2581
26
{
2582
26
  size_t name_len;
2583
26
  zend_string *lcname;
2584
26
  zend_module_entry *module_ptr;
2585
2586
26
  if (!module) {
2587
0
    return NULL;
2588
0
  }
2589
2590
#if 0
2591
  zend_printf("%s: Registering module %d\n", module->name, module->module_number);
2592
#endif
2593
2594
  /* Check module dependencies */
2595
26
  if (module->deps) {
2596
8
    const zend_module_dep *dep = module->deps;
2597
2598
22
    while (dep->name) {
2599
14
      if (dep->type == MODULE_DEP_CONFLICTS) {
2600
0
        name_len = strlen(dep->name);
2601
0
        lcname = zend_string_alloc(name_len, 0);
2602
0
        zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
2603
2604
0
        if (zend_hash_exists(&module_registry, lcname) || zend_get_extension(dep->name)) {
2605
0
          zend_string_efree(lcname);
2606
          /* TODO: Check version relationship */
2607
0
          zend_error(E_CORE_WARNING, "Cannot load module \"%s\" because conflicting module \"%s\" is already loaded", module->name, dep->name);
2608
0
          return NULL;
2609
0
        }
2610
0
        zend_string_efree(lcname);
2611
0
      }
2612
14
      ++dep;
2613
14
    }
2614
8
  }
2615
2616
26
  name_len = strlen(module->name);
2617
26
  lcname = zend_string_alloc(name_len, module_type == MODULE_PERSISTENT);
2618
26
  zend_str_tolower_copy(ZSTR_VAL(lcname), module->name, name_len);
2619
2620
26
  int module_number = zend_next_free_module();
2621
2622
26
  lcname = zend_new_interned_string(lcname);
2623
26
  if ((module_ptr = zend_hash_add_ptr(&module_registry, lcname, module)) == NULL) {
2624
0
    zend_error(E_CORE_WARNING, "Module \"%s\" is already loaded", module->name);
2625
0
    zend_string_release(lcname);
2626
0
    return NULL;
2627
0
  }
2628
26
  module = module_ptr;
2629
26
  EG(current_module) = module;
2630
2631
26
  module->module_number = module_number;
2632
26
  module->type = module_type;
2633
2634
26
  if (module->functions && zend_register_functions(NULL, module->functions, NULL, module_type)==FAILURE) {
2635
0
    zend_hash_del(&module_registry, lcname);
2636
0
    zend_string_release(lcname);
2637
0
    EG(current_module) = NULL;
2638
0
    zend_error(E_CORE_WARNING,"%s: Unable to register functions, unable to load", module->name);
2639
0
    return NULL;
2640
0
  }
2641
2642
26
  EG(current_module) = NULL;
2643
26
  zend_string_release(lcname);
2644
26
  return module;
2645
26
}
2646
/* }}} */
2647
2648
ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module) /* {{{ */
2649
24
{
2650
24
  return zend_register_module_ex(module, MODULE_PERSISTENT);
2651
24
}
2652
/* }}} */
2653
2654
static void zend_check_magic_method_args(
2655
    uint32_t num_args, const zend_class_entry *ce, const zend_function *fptr, int error_type)
2656
200
{
2657
200
  if (fptr->common.num_args != num_args) {
2658
0
    if (num_args == 0) {
2659
0
      zend_error(error_type, "Method %s::%s() cannot take arguments",
2660
0
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2661
0
    } else if (num_args == 1) {
2662
0
      zend_error(error_type, "Method %s::%s() must take exactly 1 argument",
2663
0
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2664
0
    } else {
2665
0
      zend_error(error_type, "Method %s::%s() must take exactly %" PRIu32 " arguments",
2666
0
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name), num_args);
2667
0
    }
2668
0
    return;
2669
0
  }
2670
250
  for (uint32_t i = 0; i < num_args; i++) {
2671
50
    if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, i + 1)) {
2672
0
      zend_error(error_type, "Method %s::%s() cannot take arguments by reference",
2673
0
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2674
0
      return;
2675
0
    }
2676
50
  }
2677
200
}
2678
2679
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)
2680
50
{
2681
50
    if (
2682
50
      ZEND_TYPE_IS_SET(fptr->common.arg_info[arg_num].type)
2683
50
       && !(ZEND_TYPE_FULL_MASK(fptr->common.arg_info[arg_num].type) & arg_type)
2684
50
    ) {
2685
0
      zend_error(error_type, "%s::%s(): Parameter #%d ($%s) must be of type %s when declared",
2686
0
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2687
0
        arg_num + 1, ZSTR_VAL(fptr->common.arg_info[arg_num].name),
2688
0
        ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(arg_type))));
2689
0
    }
2690
50
}
2691
2692
static void zend_check_magic_method_return_type(const zend_class_entry *ce, const zend_function *fptr, int error_type, int return_type)
2693
200
{
2694
200
  if (return_type == MAY_BE_VOID) {
2695
82
    if (fptr->common.fn_flags & ZEND_ACC_NODISCARD) {
2696
0
      zend_error_noreturn(error_type, "Method %s::%s cannot be #[\\NoDiscard]", ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2697
0
    }
2698
82
  }
2699
2700
200
  if (!(fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
2701
    /* For backwards compatibility reasons, do not enforce the return type if it is not set. */
2702
0
    return;
2703
0
  }
2704
2705
200
  if (ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & MAY_BE_NEVER) {
2706
    /* It is always legal to specify the never type. */
2707
0
    return;
2708
0
  }
2709
2710
200
  bool is_complex_type = ZEND_TYPE_IS_COMPLEX(fptr->common.arg_info[-1].type);
2711
200
  uint32_t extra_types = ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & ~return_type;
2712
200
  if (extra_types & MAY_BE_STATIC) {
2713
0
    extra_types &= ~MAY_BE_STATIC;
2714
0
    is_complex_type = true;
2715
0
  }
2716
2717
200
  if (extra_types || (is_complex_type && return_type != MAY_BE_OBJECT)) {
2718
0
    zend_error(error_type, "%s::%s(): Return type must be %s when declared",
2719
0
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2720
0
      ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(return_type))));
2721
0
  }
2722
200
}
2723
2724
static void zend_check_magic_method_non_static(
2725
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2726
338
{
2727
338
  if (fptr->common.fn_flags & ZEND_ACC_STATIC) {
2728
0
    zend_error(error_type, "Method %s::%s() cannot be static",
2729
0
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2730
0
  }
2731
338
}
2732
2733
static void zend_check_magic_method_static(
2734
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2735
10
{
2736
10
  if (!(fptr->common.fn_flags & ZEND_ACC_STATIC)) {
2737
0
    zend_error(error_type, "Method %s::%s() must be static",
2738
0
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2739
0
  }
2740
10
}
2741
2742
static void zend_check_magic_method_public(
2743
    const zend_class_entry *ce, const zend_function *fptr)
2744
176
{
2745
  // TODO: Remove this warning after adding proper visibility handling.
2746
176
  if (!(fptr->common.fn_flags & ZEND_ACC_PUBLIC)) {
2747
0
    zend_error(E_WARNING, "The magic method %s::%s() must have public visibility",
2748
0
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2749
0
  }
2750
176
}
2751
2752
static void zend_check_magic_method_no_return_type(
2753
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2754
148
{
2755
148
  if (fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
2756
0
    zend_error_noreturn(error_type, "Method %s::%s() cannot declare a return type",
2757
0
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2758
0
  }
2759
2760
148
  if (fptr->common.fn_flags & ZEND_ACC_NODISCARD) {
2761
0
    zend_error_noreturn(error_type, "Method %s::%s cannot be #[\\NoDiscard]", ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2762
0
  }
2763
148
}
2764
2765
ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, const zend_string *lcname, int error_type) /* {{{ */
2766
1.93k
{
2767
1.93k
  if (ZSTR_VAL(lcname)[0] != '_'
2768
1.58k
   || ZSTR_VAL(lcname)[1] != '_') {
2769
1.58k
    return;
2770
1.58k
  }
2771
2772
348
  if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2773
148
    zend_check_magic_method_non_static(ce, fptr, error_type);
2774
148
    zend_check_magic_method_no_return_type(ce, fptr, error_type);
2775
200
  } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2776
0
    zend_check_magic_method_args(0, ce, fptr, error_type);
2777
0
    zend_check_magic_method_non_static(ce, fptr, error_type);
2778
0
    zend_check_magic_method_no_return_type(ce, fptr, error_type);
2779
200
  } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2780
24
    zend_check_magic_method_args(0, ce, fptr, error_type);
2781
24
    zend_check_magic_method_non_static(ce, fptr, error_type);
2782
24
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2783
176
  } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2784
0
    zend_check_magic_method_args(1, ce, fptr, error_type);
2785
0
    zend_check_magic_method_non_static(ce, fptr, error_type);
2786
0
    zend_check_magic_method_public(ce, fptr);
2787
0
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2788
176
  } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2789
0
    zend_check_magic_method_args(2, ce, fptr, error_type);
2790
0
    zend_check_magic_method_non_static(ce, fptr, error_type);
2791
0
    zend_check_magic_method_public(ce, fptr);
2792
0
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2793
0
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2794
176
  } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
2795
0
    zend_check_magic_method_args(1, ce, fptr, error_type);
2796
0
    zend_check_magic_method_non_static(ce, fptr, error_type);
2797
0
    zend_check_magic_method_public(ce, fptr);
2798
0
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2799
0
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2800
176
  } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2801
0
    zend_check_magic_method_args(1, ce, fptr, error_type);
2802
0
    zend_check_magic_method_non_static(ce, fptr, error_type);
2803
0
    zend_check_magic_method_public(ce, fptr);
2804
0
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2805
0
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_BOOL);
2806
176
  } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2807
0
    zend_check_magic_method_args(2, ce, fptr, error_type);
2808
0
    zend_check_magic_method_non_static(ce, fptr, error_type);
2809
0
    zend_check_magic_method_public(ce, fptr);
2810
0
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2811
0
    zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2812
176
  } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
2813
0
    zend_check_magic_method_args(2, ce, fptr, error_type);
2814
0
    zend_check_magic_method_static(ce, fptr, error_type);
2815
0
    zend_check_magic_method_public(ce, fptr);
2816
0
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2817
0
    zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2818
176
  } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
2819
36
    zend_check_magic_method_args(0, ce, fptr, error_type);
2820
36
    zend_check_magic_method_non_static(ce, fptr, error_type);
2821
36
    zend_check_magic_method_public(ce, fptr);
2822
36
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_STRING);
2823
140
  } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
2824
32
    zend_check_magic_method_args(0, ce, fptr, error_type);
2825
32
    zend_check_magic_method_non_static(ce, fptr, error_type);
2826
32
    zend_check_magic_method_public(ce, fptr);
2827
32
    zend_check_magic_method_return_type(ce, fptr, error_type, (MAY_BE_ARRAY | MAY_BE_NULL));
2828
32
    if ((fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) && ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & MAY_BE_NULL) {
2829
0
      zend_error(E_DEPRECATED, "Returning null from %s::__debugInfo() is deprecated, make the return type non-nullable and return an empty array instead",
2830
0
        ZSTR_VAL(ce->name));
2831
0
    }
2832
108
  } else if (zend_string_equals_literal(lcname, "__serialize")) {
2833
40
    zend_check_magic_method_args(0, ce, fptr, error_type);
2834
40
    zend_check_magic_method_non_static(ce, fptr, error_type);
2835
40
    zend_check_magic_method_public(ce, fptr);
2836
40
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2837
68
  } else if (zend_string_equals_literal(lcname, "__unserialize")) {
2838
40
    zend_check_magic_method_args(1, ce, fptr, error_type);
2839
40
    zend_check_magic_method_non_static(ce, fptr, error_type);
2840
40
    zend_check_magic_method_public(ce, fptr);
2841
40
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2842
40
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2843
40
  } else if (zend_string_equals_literal(lcname, "__set_state")) {
2844
10
    zend_check_magic_method_args(1, ce, fptr, error_type);
2845
10
    zend_check_magic_method_static(ce, fptr, error_type);
2846
10
    zend_check_magic_method_public(ce, fptr);
2847
10
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2848
10
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_OBJECT);
2849
18
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) {
2850
0
    zend_check_magic_method_non_static(ce, fptr, error_type);
2851
0
    zend_check_magic_method_public(ce, fptr);
2852
18
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_SLEEP))) {
2853
0
    zend_check_magic_method_args(0, ce, fptr, error_type);
2854
0
    zend_check_magic_method_non_static(ce, fptr, error_type);
2855
0
    zend_check_magic_method_public(ce, fptr);
2856
0
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2857
18
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_WAKEUP))) {
2858
18
    zend_check_magic_method_args(0, ce, fptr, error_type);
2859
18
    zend_check_magic_method_non_static(ce, fptr, error_type);
2860
18
    zend_check_magic_method_public(ce, fptr);
2861
18
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2862
18
  }
2863
348
}
2864
/* }}} */
2865
2866
ZEND_API void zend_add_magic_method(zend_class_entry *ce, zend_function *fptr, const zend_string *lcname)
2867
1.93k
{
2868
1.93k
  if (ZSTR_VAL(lcname)[0] != '_' || ZSTR_VAL(lcname)[1] != '_') {
2869
    /* pass */
2870
1.58k
  } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2871
24
    ce->clone = fptr;
2872
324
  } else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2873
148
    ce->constructor = fptr;
2874
148
    ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
2875
176
  } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2876
0
    ce->destructor = fptr;
2877
176
  } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2878
0
    ce->__get = fptr;
2879
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2880
176
  } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2881
0
    ce->__set = fptr;
2882
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2883
176
  } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2884
0
    ce->__call = fptr;
2885
176
  } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
2886
0
    ce->__unset = fptr;
2887
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2888
176
  } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2889
0
    ce->__isset = fptr;
2890
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2891
176
  } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
2892
0
    ce->__callstatic = fptr;
2893
176
  } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
2894
36
    ce->__tostring = fptr;
2895
140
  } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
2896
32
    ce->__debugInfo = fptr;
2897
32
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2898
108
  } else if (zend_string_equals_literal(lcname, "__serialize")) {
2899
40
    ce->__serialize = fptr;
2900
68
  } else if (zend_string_equals_literal(lcname, "__unserialize")) {
2901
40
    ce->__unserialize = fptr;
2902
40
  }
2903
1.93k
}
2904
2905
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arg_info_toString, 0, 0, IS_STRING, 0)
2906
ZEND_END_ARG_INFO()
2907
2908
148
static zend_always_inline void zend_normalize_internal_type(zend_type *type) {
2909
148
  ZEND_ASSERT(!ZEND_TYPE_HAS_LITERAL_NAME(*type));
2910
148
  if (ZEND_TYPE_PURE_MASK(*type) != MAY_BE_ANY) {
2911
142
    ZEND_ASSERT(!ZEND_TYPE_CONTAINS_CODE(*type, IS_RESOURCE) && "resource is not allowed in a zend_type");
2912
142
  }
2913
148
  zend_type *current;
2914
296
  ZEND_TYPE_FOREACH_MUTABLE(*type, current) {
2915
296
    if (ZEND_TYPE_HAS_NAME(*current)) {
2916
18
      zend_string *name = zend_new_interned_string(ZEND_TYPE_NAME(*current));
2917
18
      zend_alloc_ce_cache(name);
2918
18
      ZEND_TYPE_SET_PTR(*current, name);
2919
130
    } else if (ZEND_TYPE_HAS_LIST(*current)) {
2920
0
      zend_type *inner;
2921
0
      ZEND_TYPE_FOREACH_MUTABLE(*current, inner) {
2922
0
        ZEND_ASSERT(!ZEND_TYPE_HAS_LITERAL_NAME(*inner) && !ZEND_TYPE_HAS_LIST(*inner));
2923
0
        if (ZEND_TYPE_HAS_NAME(*inner)) {
2924
0
          zend_string *name = zend_new_interned_string(ZEND_TYPE_NAME(*inner));
2925
0
          zend_alloc_ce_cache(name);
2926
0
          ZEND_TYPE_SET_PTR(*inner, name);
2927
0
        }
2928
0
      } ZEND_TYPE_FOREACH_END();
2929
0
    }
2930
296
  } ZEND_TYPE_FOREACH_END();
2931
148
}
2932
2933
static void zend_convert_internal_arg_info_type(zend_type *type, bool persistent)
2934
6.95k
{
2935
6.95k
  if (ZEND_TYPE_HAS_LITERAL_NAME(*type)) {
2936
    // gen_stubs.php does not support codegen for compound types. As a
2937
    // temporary workaround, we support union types by splitting
2938
    // the type name on `|` characters if necessary.
2939
470
    const char *class_name = ZEND_TYPE_LITERAL_NAME(*type);
2940
470
    type->type_mask &= ~_ZEND_TYPE_LITERAL_NAME_BIT;
2941
2942
470
    size_t num_types = 1;
2943
470
    const char *p = class_name;
2944
474
    while ((p = strchr(p, '|'))) {
2945
4
      num_types++;
2946
4
      p++;
2947
4
    }
2948
2949
470
    if (num_types == 1) {
2950
      /* Simple class type */
2951
466
      zend_string *str = zend_string_init_interned(class_name, strlen(class_name), persistent);
2952
466
      zend_alloc_ce_cache(str);
2953
466
      ZEND_TYPE_SET_PTR(*type, str);
2954
466
      type->type_mask |= _ZEND_TYPE_NAME_BIT;
2955
466
    } else {
2956
      /* Union type */
2957
4
      zend_type_list *list = pemalloc(ZEND_TYPE_LIST_SIZE(num_types), persistent);
2958
4
      list->num_types = num_types;
2959
4
      ZEND_TYPE_SET_LIST(*type, list);
2960
4
      ZEND_TYPE_FULL_MASK(*type) |= _ZEND_TYPE_UNION_BIT;
2961
2962
4
      const char *start = class_name;
2963
4
      uint32_t j = 0;
2964
8
      while (true) {
2965
8
        const char *end = strchr(start, '|');
2966
8
        zend_string *str = zend_string_init_interned(start, end ? end - start : strlen(start), persistent);
2967
8
        zend_alloc_ce_cache(str);
2968
8
        list->types[j] = (zend_type) ZEND_TYPE_INIT_CLASS(str, 0, 0);
2969
8
        if (!end) {
2970
4
          break;
2971
4
        }
2972
4
        start = end + 1;
2973
4
        j++;
2974
4
      }
2975
4
    }
2976
470
  }
2977
6.95k
  if (ZEND_TYPE_IS_ITERABLE_FALLBACK(*type)) {
2978
    /* Warning generated an extension load warning which is emitted for every test
2979
       zend_error(E_CORE_WARNING, "iterable type is now a compile time alias for array|Traversable,"
2980
       " regenerate the argument info via the php-src gen_stub build script");
2981
       */
2982
0
    zend_type legacy_iterable = ZEND_TYPE_INIT_CLASS_MASK(
2983
0
      ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
2984
0
      (type->type_mask | MAY_BE_ARRAY)
2985
0
    );
2986
0
    *type = legacy_iterable;
2987
0
  }
2988
6.95k
}
2989
2990
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)
2991
6.95k
{
2992
6.95k
  if (!is_return_info) {
2993
3.57k
    new_arg_info->name = zend_string_init_interned(arg_info->name, strlen(arg_info->name), persistent);
2994
3.57k
    if (arg_info->default_value) {
2995
1.15k
      new_arg_info->default_value = zend_string_init_interned(arg_info->default_value, strlen(arg_info->default_value), persistent);
2996
2.41k
    } else {
2997
2.41k
      new_arg_info->default_value = NULL;
2998
2.41k
    }
2999
3.57k
  } else {
3000
3.38k
    new_arg_info->name = NULL;
3001
3.38k
    new_arg_info->default_value = NULL;
3002
3.38k
  }
3003
6.95k
  new_arg_info->doc_comment = NULL;
3004
6.95k
  new_arg_info->type = arg_info->type;
3005
6.95k
  zend_convert_internal_arg_info_type(&new_arg_info->type, persistent);
3006
6.95k
}
3007
3008
/* registers all functions in *library_functions in the function hash */
3009
ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type) /* {{{ */
3010
268
{
3011
268
  const zend_function_entry *ptr = functions;
3012
268
  zend_function function;
3013
268
  zend_internal_function *reg_function, *internal_function = (zend_internal_function *)&function;
3014
268
  int count=0, unload=0;
3015
268
  HashTable *target_function_table = function_table;
3016
268
  int error_type;
3017
268
  zend_string *lowercase_name;
3018
268
  size_t fname_len;
3019
268
  const zend_internal_arg_info *internal_arg_info;
3020
3021
268
  if (type==MODULE_PERSISTENT) {
3022
268
    error_type = E_CORE_WARNING;
3023
268
  } else {
3024
0
    error_type = E_WARNING;
3025
0
  }
3026
3027
268
  if (!target_function_table) {
3028
20
    target_function_table = CG(function_table);
3029
20
  }
3030
268
  internal_function->type = ZEND_INTERNAL_FUNCTION;
3031
268
  internal_function->module = EG(current_module);
3032
268
  if (EG(active) && ZEND_OBSERVER_ENABLED) {
3033
    /* Add an observer temporary to store previous observed frames. This is
3034
     * normally handled by zend_observer_post_startup(), except for
3035
     * functions registered at runtime (EG(active)). */
3036
0
    internal_function->T = 1;
3037
268
  } else {
3038
268
    internal_function->T = 0;
3039
268
  }
3040
268
  memset(internal_function->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
3041
3042
3.64k
  while (ptr->fname) {
3043
3.38k
    fname_len = strlen(ptr->fname);
3044
3.38k
    internal_function->handler = ptr->handler;
3045
3.38k
    internal_function->doc_comment = ptr->doc_comment ? zend_string_init_interned(ptr->doc_comment, strlen(ptr->doc_comment), 1) : NULL;
3046
3.38k
    internal_function->function_name = zend_string_init_interned(ptr->fname, fname_len, 1);
3047
3.38k
    internal_function->scope = scope;
3048
3.38k
    internal_function->prototype = NULL;
3049
3.38k
    internal_function->prop_info = NULL;
3050
3.38k
    internal_function->attributes = NULL;
3051
3.38k
    internal_function->frameless_function_infos = ptr->frameless_function_infos;
3052
3.38k
    if (EG(active)) { // at run-time: this ought to only happen if registered with dl() or somehow temporarily at runtime
3053
0
      ZEND_MAP_PTR_INIT(internal_function->run_time_cache, zend_arena_calloc(&CG(arena), 1, zend_internal_run_time_cache_reserved_size()));
3054
3.38k
    } else {
3055
#ifdef ZTS
3056
      ZEND_MAP_PTR_NEW_STATIC(internal_function->run_time_cache);
3057
#else
3058
3.38k
      ZEND_MAP_PTR_INIT(internal_function->run_time_cache, NULL);
3059
3.38k
#endif
3060
3.38k
    }
3061
3.38k
    if (ptr->flags & UINT32_MAX) {
3062
2.30k
      if (!(ptr->flags & ZEND_ACC_PPP_MASK)) {
3063
370
        if (ptr->flags != ZEND_ACC_DEPRECATED && scope) {
3064
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);
3065
0
        }
3066
370
        internal_function->fn_flags = ZEND_ACC_PUBLIC | ptr->flags;
3067
1.93k
      } else {
3068
1.93k
        internal_function->fn_flags = ptr->flags;
3069
1.93k
      }
3070
2.30k
    } else {
3071
1.07k
      internal_function->fn_flags = ZEND_ACC_PUBLIC;
3072
1.07k
    }
3073
3.38k
    internal_function->fn_flags2 = ptr->flags >> 32;
3074
3075
3.38k
    if (ptr->arg_info) {
3076
3.38k
      zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info;
3077
3.38k
      internal_arg_info = ptr->arg_info+1;
3078
3.38k
      internal_function->num_args = ptr->num_args;
3079
      /* Currently you cannot denote that the function can accept less arguments than num_args */
3080
3.38k
      if (info->required_num_args == (uintptr_t)-1) {
3081
0
        internal_function->required_num_args = ptr->num_args;
3082
3.38k
      } else {
3083
3.38k
        internal_function->required_num_args = info->required_num_args;
3084
3.38k
      }
3085
3.38k
      if (ZEND_ARG_SEND_MODE(info)) {
3086
0
        internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE;
3087
0
      }
3088
3.38k
      if (ZEND_ARG_IS_VARIADIC(&ptr->arg_info[ptr->num_args])) {
3089
92
        internal_function->fn_flags |= ZEND_ACC_VARIADIC;
3090
        /* Don't count the variadic argument */
3091
92
        internal_function->num_args--;
3092
92
      }
3093
3.38k
      if (ZEND_TYPE_IS_SET(info->type)) {
3094
3.18k
        if (ZEND_TYPE_HAS_NAME(info->type)) {
3095
0
          const char *type_name = ZEND_TYPE_LITERAL_NAME(info->type);
3096
0
          if (!scope && (!strcasecmp(type_name, "self") || !strcasecmp(type_name, "parent"))) {
3097
0
            zend_error_noreturn(E_CORE_ERROR, "Cannot declare a return type of %s outside of a class scope", type_name);
3098
0
          }
3099
0
        }
3100
3101
3.18k
        internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
3102
3.18k
      }
3103
3.38k
    } else {
3104
0
      zend_error(E_CORE_WARNING, "Missing arginfo for %s%s%s()",
3105
0
         scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3106
3107
0
      internal_arg_info = NULL;
3108
0
      internal_function->num_args = 0;
3109
0
      internal_function->required_num_args = 0;
3110
0
    }
3111
3112
    /* If not specified, add __toString() return type for compatibility with Stringable
3113
     * interface. */
3114
3.38k
    if (scope && zend_string_equals_literal_ci(internal_function->function_name, "__tostring") &&
3115
36
        !(internal_function->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
3116
0
      zend_error(E_CORE_WARNING, "%s::__toString() implemented without string return type",
3117
0
        ZSTR_VAL(scope->name));
3118
0
      internal_arg_info = (zend_internal_arg_info *) arg_info_toString + 1;
3119
0
      internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
3120
0
      internal_function->num_args = internal_function->required_num_args = 0;
3121
0
    }
3122
3123
3.38k
    if (ptr->flags & ZEND_ACC_ABSTRACT) {
3124
90
      if (scope) {
3125
        /* This is a class that must be abstract itself. Here we set the check info. */
3126
90
        scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
3127
90
        if (!(scope->ce_flags & ZEND_ACC_INTERFACE)) {
3128
          /* Since the class is not an interface it needs to be declared as a abstract class. */
3129
          /* Since here we are handling internal functions only we can add the keyword flag. */
3130
          /* This time we set the flag for the keyword 'abstract'. */
3131
4
          scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
3132
4
        }
3133
90
      }
3134
90
      if ((ptr->flags & ZEND_ACC_STATIC) && (!scope || !(scope->ce_flags & ZEND_ACC_INTERFACE))) {
3135
0
        zend_error(error_type, "Static function %s%s%s() cannot be abstract", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3136
0
      }
3137
3.29k
    } else {
3138
3.29k
      if (scope && (scope->ce_flags & ZEND_ACC_INTERFACE)) {
3139
0
        zend_error(error_type, "Interface %s cannot contain non abstract method %s()", ZSTR_VAL(scope->name), ptr->fname);
3140
0
        return FAILURE;
3141
0
      }
3142
3.29k
      if (!internal_function->handler) {
3143
0
        zend_error(error_type, "Method %s%s%s() cannot be a NULL function", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3144
0
        zend_unregister_functions(functions, count, target_function_table);
3145
0
        return FAILURE;
3146
0
      }
3147
3.29k
    }
3148
3.38k
    lowercase_name = zend_string_tolower_ex(internal_function->function_name, type == MODULE_PERSISTENT);
3149
3.38k
    lowercase_name = zend_new_interned_string(lowercase_name);
3150
3.38k
    reg_function = pemalloc(sizeof(zend_internal_function), true);
3151
3.38k
    memcpy(reg_function, &function, sizeof(zend_internal_function));
3152
3.38k
    if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) {
3153
0
      unload=1;
3154
0
      free(reg_function);
3155
0
      zend_string_release(lowercase_name);
3156
0
      break;
3157
0
    }
3158
3.38k
    if (reg_function->frameless_function_infos) {
3159
40
      const zend_frameless_function_info *flf_info = reg_function->frameless_function_infos;
3160
98
      while (flf_info->handler) {
3161
58
        if (zend_flf_count == zend_flf_capacity) {
3162
6
          if (!zend_flf_capacity) {
3163
2
            zend_flf_capacity = 8;
3164
4
          } else {
3165
4
            zend_flf_capacity *= 2;
3166
4
          }
3167
          /* +1 for NULL terminator */
3168
6
          zend_flf_handlers = perealloc(zend_flf_handlers, (zend_flf_capacity + 1) * sizeof(void *), true);
3169
6
          zend_flf_functions = perealloc(zend_flf_functions, (zend_flf_capacity + 1) * sizeof(zend_function *), true);
3170
6
        }
3171
58
        zend_flf_handlers[zend_flf_count] = flf_info->handler;
3172
58
        zend_flf_functions[zend_flf_count] = (zend_function *)reg_function;
3173
58
        zend_flf_count++;
3174
58
        flf_info++;
3175
58
      }
3176
40
      zend_flf_handlers[zend_flf_count] = NULL;
3177
40
      zend_flf_functions[zend_flf_count] = NULL;
3178
40
    }
3179
3180
    /* Get parameter count including variadic parameter. */
3181
3.38k
    uint32_t num_args = reg_function->num_args;
3182
3.38k
    if (reg_function->fn_flags & ZEND_ACC_VARIADIC) {
3183
92
      num_args++;
3184
92
    }
3185
3186
    /* If types of arguments have to be checked */
3187
3.38k
    if (internal_arg_info && num_args) {
3188
1.96k
      uint32_t i;
3189
5.53k
      for (i = 0; i < num_args; i++) {
3190
3.56k
        const zend_internal_arg_info *arg_info = &internal_arg_info[i];
3191
3.56k
        ZEND_ASSERT(arg_info->name && "Parameter must have a name");
3192
3.56k
        if (ZEND_TYPE_IS_SET(arg_info->type)) {
3193
3.18k
            reg_function->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
3194
3.18k
        }
3195
3.56k
#if ZEND_DEBUG
3196
6.19k
        for (uint32_t j = 0; j < i; j++) {
3197
2.62k
          if (!strcmp(arg_info->name, internal_arg_info[j].name)) {
3198
0
            zend_error_noreturn(E_CORE_ERROR,
3199
0
              "Duplicate parameter name $%s for function %s%s%s()", arg_info->name,
3200
0
              scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3201
0
          }
3202
2.62k
        }
3203
3.56k
#endif
3204
3.56k
      }
3205
1.96k
    }
3206
3207
    /* Convert zend_internal_arg_info to zend_arg_info */
3208
3.38k
    if (internal_arg_info) {
3209
3.38k
      uint32_t i;
3210
3.38k
      const zend_internal_arg_info *arg_info = internal_arg_info - 1;
3211
3.38k
      zend_arg_info *new_arg_info;
3212
3213
      /* Treat return type as an extra argument */
3214
3.38k
      num_args++;
3215
3.38k
      new_arg_info = pemalloc(sizeof(zend_arg_info) * num_args, true);
3216
3.38k
      reg_function->arg_info = new_arg_info + 1;
3217
10.3k
      for (i = 0; i < num_args; i++) {
3218
6.94k
        zend_convert_internal_arg_info(&new_arg_info[i], &arg_info[i],
3219
6.94k
            i == 0, true);
3220
6.94k
      }
3221
3.38k
    }
3222
3223
3.38k
    zend_set_function_arg_flags((zend_function*)reg_function);
3224
3225
3.38k
    if (scope) {
3226
1.93k
      zend_check_magic_method_implementation(
3227
1.93k
        scope, (zend_function *)reg_function, lowercase_name, E_CORE_ERROR);
3228
1.93k
      zend_add_magic_method(scope, (zend_function *)reg_function, lowercase_name);
3229
1.93k
    }
3230
3.38k
    ptr++;
3231
3.38k
    count++;
3232
3.38k
    zend_string_release(lowercase_name);
3233
3.38k
  }
3234
268
  if (unload) { /* before unloading, display all remaining bad function in the module */
3235
0
    while (ptr->fname) {
3236
0
      fname_len = strlen(ptr->fname);
3237
0
      lowercase_name = zend_string_alloc(fname_len, 0);
3238
0
      zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
3239
0
      if (zend_hash_exists(target_function_table, lowercase_name)) {
3240
0
        zend_error(error_type, "Function registration failed - duplicate name - %s%s%s", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3241
0
      }
3242
0
      zend_string_efree(lowercase_name);
3243
0
      ptr++;
3244
0
    }
3245
0
    zend_unregister_functions(functions, count, target_function_table);
3246
0
    return FAILURE;
3247
0
  }
3248
268
  return SUCCESS;
3249
268
}
3250
/* }}} */
3251
3252
/* count=-1 means erase all functions, otherwise,
3253
 * erase the first count functions
3254
 */
3255
ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table) /* {{{ */
3256
0
{
3257
0
  const zend_function_entry *ptr = functions;
3258
0
  int i=0;
3259
0
  HashTable *target_function_table = function_table;
3260
0
  zend_string *lowercase_name;
3261
0
  size_t fname_len;
3262
3263
0
  if (!target_function_table) {
3264
0
    target_function_table = CG(function_table);
3265
0
  }
3266
0
  while (ptr->fname) {
3267
0
    if (count!=-1 && i>=count) {
3268
0
      break;
3269
0
    }
3270
0
    fname_len = strlen(ptr->fname);
3271
0
    lowercase_name = zend_string_alloc(fname_len, 0);
3272
0
    zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
3273
0
    zend_hash_del(target_function_table, lowercase_name);
3274
0
    zend_string_efree(lowercase_name);
3275
0
    ptr++;
3276
0
    i++;
3277
0
  }
3278
0
}
3279
/* }}} */
3280
3281
ZEND_API zend_result zend_startup_module(zend_module_entry *module) /* {{{ */
3282
0
{
3283
0
  if ((module = zend_register_internal_module(module)) != NULL && zend_startup_module_ex(module) == SUCCESS) {
3284
0
    return SUCCESS;
3285
0
  }
3286
0
  return FAILURE;
3287
0
}
3288
/* }}} */
3289
3290
ZEND_API zend_result zend_get_module_started(const char *module_name) /* {{{ */
3291
0
{
3292
0
  zend_module_entry *module;
3293
3294
0
  module = zend_hash_str_find_ptr(&module_registry, module_name, strlen(module_name));
3295
0
  return (module && module->module_started) ? SUCCESS : FAILURE;
3296
0
}
3297
/* }}} */
3298
3299
static void clean_module_classes(int module_number) /* {{{ */
3300
0
{
3301
  /* Child classes may reuse structures from parent classes, so destroy in reverse order. */
3302
0
  Bucket *bucket;
3303
0
  ZEND_HASH_REVERSE_FOREACH_BUCKET(EG(class_table), bucket) {
3304
0
    const zend_class_entry *ce = Z_CE(bucket->val);
3305
0
    if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->module_number == module_number) {
3306
0
      zend_hash_del_bucket(EG(class_table), bucket);
3307
0
    }
3308
0
  } ZEND_HASH_FOREACH_END();
3309
3310
0
}
3311
/* }}} */
3312
3313
static int clean_module_function(zval *el, void *arg) /* {{{ */
3314
0
{
3315
0
  const zend_function *fe = (zend_function *) Z_PTR_P(el);
3316
0
  const zend_module_entry *module = arg;
3317
0
  if (fe->common.type == ZEND_INTERNAL_FUNCTION && fe->internal_function.module == module) {
3318
0
    return ZEND_HASH_APPLY_REMOVE;
3319
0
  } else {
3320
0
    return ZEND_HASH_APPLY_KEEP;
3321
0
  }
3322
0
}
3323
/* }}} */
3324
3325
static void clean_module_functions(zend_module_entry *module) /* {{{ */
3326
0
{
3327
0
  zend_hash_apply_with_argument(CG(function_table), clean_module_function, module);
3328
0
}
3329
/* }}} */
3330
3331
void module_destructor(zend_module_entry *module) /* {{{ */
3332
0
{
3333
#if ZEND_RC_DEBUG
3334
  bool orig_rc_debug = zend_rc_debug;
3335
#endif
3336
3337
0
  if (module->type == MODULE_TEMPORARY) {
3338
#if ZEND_RC_DEBUG
3339
    /* FIXME: Loading extensions during the request breaks some invariants.
3340
     * In particular, it will create persistent interned strings, which is
3341
     * not allowed at this stage. */
3342
    zend_rc_debug = false;
3343
#endif
3344
0
    zend_clean_module_rsrc_dtors(module->module_number);
3345
0
    clean_module_constants(module->module_number);
3346
0
    clean_module_classes(module->module_number);
3347
0
  }
3348
3349
0
  if (module->module_started && module->module_shutdown_func) {
3350
#if 0
3351
    zend_printf("%s: Module shutdown\n", module->name);
3352
#endif
3353
0
    module->module_shutdown_func(module->type, module->module_number);
3354
0
  }
3355
3356
0
  if (module->module_started
3357
0
   && !module->module_shutdown_func
3358
0
   && module->type == MODULE_TEMPORARY) {
3359
0
    zend_unregister_ini_entries_ex(module->module_number, module->type);
3360
0
  }
3361
3362
  /* Deinitialize module globals */
3363
0
  if (module->globals_size) {
3364
#ifdef ZTS
3365
    if (*module->globals_id_ptr) {
3366
      ts_free_id(*module->globals_id_ptr);
3367
    }
3368
#else
3369
0
    if (module->globals_dtor) {
3370
0
      module->globals_dtor(module->globals_ptr);
3371
0
    }
3372
0
#endif
3373
0
  }
3374
3375
0
  module->module_started=0;
3376
0
  if (module->type == MODULE_TEMPORARY && module->functions) {
3377
0
    zend_unregister_functions(module->functions, -1, NULL);
3378
    /* Clean functions registered separately from module->functions */
3379
0
    clean_module_functions(module);
3380
0
  }
3381
3382
#if ZEND_RC_DEBUG
3383
  zend_rc_debug = orig_rc_debug;
3384
#endif
3385
0
}
3386
/* }}} */
3387
3388
void module_registry_unload(const zend_module_entry *module)
3389
0
{
3390
0
#ifdef HAVE_LIBDL
3391
0
  if (!getenv("ZEND_DONT_UNLOAD_MODULES")) {
3392
0
    DL_UNLOAD(module->handle);
3393
0
  }
3394
#else
3395
  ZEND_IGNORE_VALUE(module);
3396
#endif
3397
0
}
3398
3399
ZEND_API void zend_activate_modules(void) /* {{{ */
3400
1.99k
{
3401
1.99k
  zend_module_entry **p = module_request_startup_handlers;
3402
3403
17.9k
  while (*p) {
3404
15.9k
    const zend_module_entry *module = *p;
3405
3406
15.9k
    if (module->request_startup_func(module->type, module->module_number)==FAILURE) {
3407
0
      zend_error(E_WARNING, "request_startup() for %s module failed", module->name);
3408
0
      exit(1);
3409
0
    }
3410
15.9k
    p++;
3411
15.9k
  }
3412
1.99k
}
3413
/* }}} */
3414
3415
ZEND_API void zend_deactivate_modules(void) /* {{{ */
3416
1.99k
{
3417
1.99k
  EG(current_execute_data) = NULL; /* we're no longer executing anything */
3418
3419
1.99k
  if (EG(full_tables_cleanup)) {
3420
0
    zend_module_entry *module;
3421
3422
0
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) {
3423
0
      if (module->request_shutdown_func) {
3424
0
        zend_try {
3425
0
          module->request_shutdown_func(module->type, module->module_number);
3426
0
        } zend_end_try();
3427
0
      }
3428
0
    } ZEND_HASH_FOREACH_END();
3429
1.99k
  } else {
3430
1.99k
    zend_module_entry **p = module_request_shutdown_handlers;
3431
3432
9.95k
    while (*p) {
3433
7.96k
      const zend_module_entry *module = *p;
3434
7.96k
      zend_try {
3435
7.96k
        module->request_shutdown_func(module->type, module->module_number);
3436
7.96k
      } zend_end_try();
3437
7.96k
      p++;
3438
7.96k
    }
3439
1.99k
  }
3440
1.99k
}
3441
/* }}} */
3442
3443
void zend_unload_modules(void) /* {{{ */
3444
0
{
3445
0
  zend_module_entry **modules = modules_dl_loaded;
3446
0
  while (*modules) {
3447
0
    module_registry_unload(*modules);
3448
0
    modules++;
3449
0
  }
3450
0
  free(modules_dl_loaded);
3451
0
  modules_dl_loaded = NULL;
3452
0
}
3453
/* }}} */
3454
3455
ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
3456
1.99k
{
3457
1.99k
  if (EG(full_tables_cleanup)) {
3458
0
    zend_module_entry *module;
3459
0
    zval *zv;
3460
0
    zend_string *key;
3461
3462
0
    ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
3463
0
      if (module->post_deactivate_func) {
3464
0
        module->post_deactivate_func();
3465
0
      }
3466
0
    } ZEND_HASH_FOREACH_END();
3467
0
    ZEND_HASH_MAP_REVERSE_FOREACH_STR_KEY_VAL(&module_registry, key, zv) {
3468
0
      module = Z_PTR_P(zv);
3469
0
      if (module->type != MODULE_TEMPORARY) {
3470
0
        break;
3471
0
      }
3472
0
      module_destructor(module);
3473
0
      if (module->handle) {
3474
0
        module_registry_unload(module);
3475
0
      }
3476
0
      zend_string_release_ex(key, 0);
3477
0
    } ZEND_HASH_MAP_FOREACH_END_DEL();
3478
1.99k
  } else {
3479
1.99k
    zend_module_entry **p = module_post_deactivate_handlers;
3480
3481
7.96k
    while (*p) {
3482
5.97k
      const zend_module_entry *module = *p;
3483
3484
5.97k
      module->post_deactivate_func();
3485
5.97k
      p++;
3486
5.97k
    }
3487
1.99k
  }
3488
1.99k
}
3489
/* }}} */
3490
3491
/* return the next free module number */
3492
ZEND_API int zend_next_free_module(void) /* {{{ */
3493
26
{
3494
26
  return zend_hash_num_elements(&module_registry);
3495
26
}
3496
/* }}} */
3497
3498
static zend_class_entry *do_register_internal_class(const zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
3499
348
{
3500
348
  zend_class_entry *class_entry = pemalloc(sizeof(zend_class_entry), true);
3501
348
  zend_string *lowercase_name;
3502
348
  *class_entry = *orig_class_entry;
3503
3504
348
  class_entry->type = ZEND_INTERNAL_CLASS;
3505
348
  zend_initialize_class_data(class_entry, 0);
3506
348
  zend_alloc_ce_cache(class_entry->name);
3507
348
  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;
3508
348
  class_entry->info.internal.module = EG(current_module);
3509
3510
348
  if (class_entry->info.internal.builtin_functions) {
3511
224
    zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, EG(current_module)->type);
3512
224
  }
3513
3514
348
  lowercase_name = zend_string_tolower_ex(orig_class_entry->name, EG(current_module)->type == MODULE_PERSISTENT);
3515
348
  lowercase_name = zend_new_interned_string(lowercase_name);
3516
348
  zend_hash_update_ptr(CG(class_table), lowercase_name, class_entry);
3517
348
  zend_string_release_ex(lowercase_name, 1);
3518
3519
348
  if (class_entry->__tostring && !zend_string_equals_literal(class_entry->name, "Stringable")
3520
34
      && !(class_entry->ce_flags & ZEND_ACC_TRAIT)) {
3521
34
    ZEND_ASSERT(zend_ce_stringable
3522
34
      && "Should be registered before first class using __toString()");
3523
34
    zend_do_implement_interface(class_entry, zend_ce_stringable);
3524
34
  }
3525
348
  return class_entry;
3526
348
}
3527
/* }}} */
3528
3529
/* If parent_ce is not NULL then it inherits from parent_ce
3530
 * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it
3531
 * If both parent_ce and parent_name are NULL it does a regular class registration
3532
 * If parent_name is specified but not found NULL is returned
3533
 */
3534
ZEND_API zend_class_entry *zend_register_internal_class_ex(const zend_class_entry *class_entry, zend_class_entry *parent_ce) /* {{{ */
3535
0
{
3536
0
  return zend_register_internal_class_with_flags(class_entry, parent_ce, 0);
3537
0
}
3538
/* }}} */
3539
3540
ZEND_API zend_class_entry *zend_register_internal_class_with_flags(
3541
  const zend_class_entry *class_entry,
3542
  zend_class_entry *parent_ce,
3543
  uint32_t ce_flags
3544
284
) {
3545
284
  zend_class_entry *register_class = do_register_internal_class(class_entry, ce_flags);
3546
3547
284
  if (parent_ce) {
3548
158
    zend_do_inheritance(register_class, parent_ce);
3549
158
    zend_build_properties_info_table(register_class);
3550
158
  }
3551
3552
284
  return register_class;
3553
284
}
3554
3555
ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...) /* {{{ */
3556
130
{
3557
130
  zend_class_entry *interface_entry;
3558
130
  va_list interface_list;
3559
130
  va_start(interface_list, num_interfaces);
3560
3561
304
  while (num_interfaces--) {
3562
174
    interface_entry = va_arg(interface_list, zend_class_entry *);
3563
174
    if (interface_entry == zend_ce_stringable
3564
10
        && zend_class_implements_interface(class_entry, zend_ce_stringable)) {
3565
      /* Stringable is implemented automatically,
3566
       * silently ignore an explicit implementation. */
3567
6
      continue;
3568
6
    }
3569
3570
168
    zend_do_implement_interface(class_entry, interface_entry);
3571
168
  }
3572
3573
130
  va_end(interface_list);
3574
130
}
3575
/* }}} */
3576
3577
/* A class that contains at least one abstract method automatically becomes an abstract class.
3578
 */
3579
ZEND_API zend_class_entry *zend_register_internal_class(const zend_class_entry *orig_class_entry) /* {{{ */
3580
24
{
3581
24
  return do_register_internal_class(orig_class_entry, 0);
3582
24
}
3583
/* }}} */
3584
3585
ZEND_API zend_class_entry *zend_register_internal_interface(const zend_class_entry *orig_class_entry) /* {{{ */
3586
40
{
3587
40
  return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE);
3588
40
}
3589
/* }}} */
3590
3591
ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce, bool persistent) /* {{{ */
3592
0
{
3593
0
  zend_string *lcname;
3594
0
  zval zv, *ret;
3595
3596
  /* TODO: Move this out of here in 7.4. */
3597
0
  if (persistent && EG(current_module) && EG(current_module)->type == MODULE_TEMPORARY) {
3598
0
    persistent = false;
3599
0
  }
3600
3601
0
  if (name[0] == '\\') {
3602
0
    lcname = zend_string_alloc(name_len-1, persistent);
3603
0
    zend_str_tolower_copy(ZSTR_VAL(lcname), name+1, name_len-1);
3604
0
  } else {
3605
0
    lcname = zend_string_alloc(name_len, persistent);
3606
0
    zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len);
3607
0
  }
3608
3609
0
  zend_assert_valid_class_name(lcname, "a class alias");
3610
3611
0
  lcname = zend_new_interned_string(lcname);
3612
3613
  /* We cannot increase the refcount of an internal class during request time.
3614
   * Instead of having to deal with differentiating between class types and lifetimes,
3615
   * we simply don't increase the refcount of a class entry for aliases.
3616
   */
3617
0
  ZVAL_ALIAS_PTR(&zv, ce);
3618
3619
0
  ret = zend_hash_add(CG(class_table), lcname, &zv);
3620
0
  zend_string_release_ex(lcname, 0);
3621
0
  if (ret) {
3622
    // avoid notifying at MINIT time
3623
0
    if (ce->type == ZEND_USER_CLASS) {
3624
0
      zend_observer_class_linked_notify(ce, lcname);
3625
0
    }
3626
0
    return SUCCESS;
3627
0
  }
3628
0
  return FAILURE;
3629
0
}
3630
/* }}} */
3631
3632
/* Disabled functions support */
3633
3634
static void zend_disable_function(const char *function_name, size_t function_name_length)
3635
72
{
3636
72
  if (UNEXPECTED(
3637
72
    (function_name_length == strlen("exit") && !memcmp(function_name, "exit", strlen("exit")))
3638
72
    || (function_name_length == strlen("die") && !memcmp(function_name, "die", strlen("die")))
3639
72
    || (function_name_length == strlen("clone") && !memcmp(function_name, "clone", strlen("clone")))
3640
72
  )) {
3641
0
    zend_error(E_WARNING, "Cannot disable function %s()", function_name);
3642
0
    return;
3643
0
  }
3644
72
  zend_hash_str_del(CG(function_table), function_name, function_name_length);
3645
72
}
3646
3647
ZEND_API void zend_disable_functions(const char *function_list) /* {{{ */
3648
2
{
3649
2
  if (!function_list || !*function_list) {
3650
0
    return;
3651
0
  }
3652
3653
2
  const char *s = NULL, *e = function_list;
3654
668
  while (*e) {
3655
666
    switch (*e) {
3656
0
      case ' ':
3657
70
      case ',':
3658
70
        if (s) {
3659
70
          zend_disable_function(s, e - s);
3660
70
          s = NULL;
3661
70
        }
3662
70
        break;
3663
596
      default:
3664
596
        if (!s) {
3665
72
          s = e;
3666
72
        }
3667
596
        break;
3668
666
    }
3669
666
    e++;
3670
666
  }
3671
2
  if (s) {
3672
2
    zend_disable_function(s, e - s);
3673
2
  }
3674
3675
  /* Rehash the function table after deleting functions. This ensures that all internal
3676
   * functions are contiguous, which means we don't need to perform full table cleanup
3677
   * on shutdown. */
3678
2
  zend_hash_rehash(CG(function_table));
3679
2
}
3680
/* }}} */
3681
3682
static zend_always_inline zend_class_entry *get_scope(const zend_execute_data *frame)
3683
0
{
3684
0
  return frame && frame->func ? frame->func->common.scope : NULL;
3685
0
}
3686
3687
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) /* {{{ */
3688
0
{
3689
0
  bool ret = false;
3690
0
  zend_class_entry *ce;
3691
0
  size_t name_len = ZSTR_LEN(name);
3692
0
  zend_string *lcname;
3693
0
  ALLOCA_FLAG(use_heap);
3694
3695
0
  ZSTR_ALLOCA_ALLOC(lcname, name_len, use_heap);
3696
0
  zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name), name_len);
3697
3698
0
  *strict_class = false;
3699
0
  if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_SELF))) {
3700
0
    if (!scope) {
3701
0
      if (error) *error = estrdup("cannot access \"self\" when no class scope is active");
3702
0
    } else {
3703
0
      if (!suppress_deprecation) {
3704
0
        zend_error(E_DEPRECATED, "Use of \"self\" in callables is deprecated");
3705
0
      }
3706
0
      fcc->called_scope = zend_get_called_scope(frame);
3707
0
      if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope)) {
3708
0
        fcc->called_scope = scope;
3709
0
      }
3710
0
      fcc->calling_scope = scope;
3711
0
      if (!fcc->object) {
3712
0
        fcc->object = zend_get_this_object(frame);
3713
0
      }
3714
0
      ret = true;
3715
0
    }
3716
0
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_PARENT))) {
3717
0
    if (!scope) {
3718
0
      if (error) *error = estrdup("cannot access \"parent\" when no class scope is active");
3719
0
    } else if (!scope->parent) {
3720
0
      if (error) *error = estrdup("cannot access \"parent\" when current class scope has no parent");
3721
0
    } else {
3722
0
      if (!suppress_deprecation) {
3723
0
        zend_error(E_DEPRECATED, "Use of \"parent\" in callables is deprecated");
3724
0
      }
3725
0
      fcc->called_scope = zend_get_called_scope(frame);
3726
0
      if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope->parent)) {
3727
0
        fcc->called_scope = scope->parent;
3728
0
      }
3729
0
      fcc->calling_scope = scope->parent;
3730
0
      if (!fcc->object) {
3731
0
        fcc->object = zend_get_this_object(frame);
3732
0
      }
3733
0
      *strict_class = true;
3734
0
      ret = true;
3735
0
    }
3736
0
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_STATIC))) {
3737
0
    zend_class_entry *called_scope = zend_get_called_scope(frame);
3738
3739
0
    if (!called_scope) {
3740
0
      if (error) *error = estrdup("cannot access \"static\" when no class scope is active");
3741
0
    } else {
3742
0
      if (!suppress_deprecation) {
3743
0
        zend_error(E_DEPRECATED, "Use of \"static\" in callables is deprecated");
3744
0
      }
3745
0
      fcc->called_scope = called_scope;
3746
0
      fcc->calling_scope = called_scope;
3747
0
      if (!fcc->object) {
3748
0
        fcc->object = zend_get_this_object(frame);
3749
0
      }
3750
0
      *strict_class = true;
3751
0
      ret = true;
3752
0
    }
3753
0
  } else if ((ce = zend_lookup_class(name)) != NULL) {
3754
0
    const zend_class_entry *frame_scope = get_scope(frame);
3755
0
    fcc->calling_scope = ce;
3756
0
    if (frame_scope && !fcc->object) {
3757
0
      zend_object *object = zend_get_this_object(frame);
3758
3759
0
      if (object &&
3760
0
          instanceof_function(object->ce, frame_scope) &&
3761
0
          instanceof_function(frame_scope, ce)) {
3762
0
        fcc->object = object;
3763
0
        fcc->called_scope = object->ce;
3764
0
      } else {
3765
0
        fcc->called_scope = ce;
3766
0
      }
3767
0
    } else {
3768
0
      fcc->called_scope = fcc->object ? fcc->object->ce : ce;
3769
0
    }
3770
0
    *strict_class = true;
3771
0
    ret = true;
3772
0
  } else {
3773
0
    if (error) zend_spprintf(error, 0, "class \"%.*s\" not found", (int)name_len, ZSTR_VAL(name));
3774
0
  }
3775
0
  ZSTR_ALLOCA_FREE(lcname, use_heap);
3776
  /* User error handlers may throw from deprecations above; do not report callable as valid. */
3777
0
  if (UNEXPECTED(EG(exception))) {
3778
0
    return false;
3779
0
  }
3780
0
  return ret;
3781
0
}
3782
/* }}} */
3783
3784
0
ZEND_API void zend_release_fcall_info_cache(zend_fcall_info_cache *fcc) {
3785
0
  if (fcc->function_handler &&
3786
0
    (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
3787
0
    if (fcc->function_handler->common.function_name) {
3788
0
      zend_string_release_ex(fcc->function_handler->common.function_name, 0);
3789
0
    }
3790
0
    zend_free_trampoline(fcc->function_handler);
3791
0
    fcc->function_handler = NULL;
3792
0
  }
3793
0
}
3794
3795
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) /* {{{ */
3796
0
{
3797
0
  zend_class_entry *ce_org = fcc->calling_scope;
3798
0
  bool retval = false;
3799
0
  zend_string *mname, *cname;
3800
0
  zend_string *lmname;
3801
0
  const char *colon;
3802
0
  size_t clen;
3803
0
  HashTable *ftable;
3804
0
  int call_via_handler = 0;
3805
0
  zend_class_entry *scope;
3806
0
  zval *zv;
3807
0
  ALLOCA_FLAG(use_heap)
3808
3809
0
  fcc->calling_scope = NULL;
3810
3811
0
  if (!ce_org) {
3812
0
    zend_function *func;
3813
0
    zend_string *lmname;
3814
3815
    /* Check if function with given name exists.
3816
     * This may be a compound name that includes namespace name */
3817
0
    if (UNEXPECTED(Z_STRVAL_P(callable)[0] == '\\')) {
3818
      /* Skip leading \ */
3819
0
      ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable) - 1, use_heap);
3820
0
      zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable) + 1, Z_STRLEN_P(callable) - 1);
3821
0
      func = zend_fetch_function(lmname);
3822
0
      ZSTR_ALLOCA_FREE(lmname, use_heap);
3823
0
    } else {
3824
0
      lmname = Z_STR_P(callable);
3825
0
      func = zend_fetch_function(lmname);
3826
0
      if (!func) {
3827
0
        ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable), use_heap);
3828
0
        zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable), Z_STRLEN_P(callable));
3829
0
        func = zend_fetch_function(lmname);
3830
0
        ZSTR_ALLOCA_FREE(lmname, use_heap);
3831
0
      }
3832
0
    }
3833
0
    if (EXPECTED(func != NULL)) {
3834
0
      fcc->function_handler = func;
3835
0
      return 1;
3836
0
    }
3837
0
  }
3838
3839
  /* Split name into class/namespace and method/function names */
3840
0
  if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL &&
3841
0
    colon > Z_STRVAL_P(callable) &&
3842
0
    *(colon-1) == ':'
3843
0
  ) {
3844
0
    size_t mlen;
3845
3846
0
    colon--;
3847
0
    clen = colon - Z_STRVAL_P(callable);
3848
0
    mlen = Z_STRLEN_P(callable) - clen - 2;
3849
3850
0
    if (colon == Z_STRVAL_P(callable)) {
3851
0
      if (error) *error = estrdup("invalid function name");
3852
0
      return 0;
3853
0
    }
3854
3855
    /* This is a compound name.
3856
     * Try to fetch class and then find static method. */
3857
0
    if (ce_org) {
3858
0
      scope = ce_org;
3859
0
    } else {
3860
0
      scope = get_scope(frame);
3861
0
    }
3862
3863
0
    cname = zend_string_init_interned(Z_STRVAL_P(callable), clen, 0);
3864
0
    if (ZSTR_HAS_CE_CACHE(cname) && ZSTR_GET_CE_CACHE(cname)) {
3865
0
      fcc->calling_scope = ZSTR_GET_CE_CACHE(cname);
3866
0
      if (scope && !fcc->object) {
3867
0
        zend_object *object = zend_get_this_object(frame);
3868
3869
0
        if (object &&
3870
0
            instanceof_function(object->ce, scope) &&
3871
0
            instanceof_function(scope, fcc->calling_scope)) {
3872
0
          fcc->object = object;
3873
0
          fcc->called_scope = object->ce;
3874
0
        } else {
3875
0
          fcc->called_scope = fcc->calling_scope;
3876
0
        }
3877
0
      } else {
3878
0
        fcc->called_scope = fcc->object ? fcc->object->ce : fcc->calling_scope;
3879
0
      }
3880
0
      strict_class = true;
3881
0
    } else if (!zend_is_callable_check_class(cname, scope, frame, fcc, &strict_class, error, suppress_deprecation || ce_org != NULL)) {
3882
0
      zend_string_release_ex(cname, 0);
3883
0
      return 0;
3884
0
    }
3885
0
    zend_string_release_ex(cname, 0);
3886
3887
0
    ftable = &fcc->calling_scope->function_table;
3888
0
    if (ce_org && !instanceof_function(ce_org, fcc->calling_scope)) {
3889
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));
3890
0
      return 0;
3891
0
    }
3892
0
    if (ce_org && !suppress_deprecation) {
3893
0
      zend_error(E_DEPRECATED,
3894
0
        "Callables of the form [\"%s\", \"%s\"] are deprecated",
3895
0
        ZSTR_VAL(ce_org->name), Z_STRVAL_P(callable));
3896
0
    }
3897
0
    mname = zend_string_init(Z_STRVAL_P(callable) + clen + 2, mlen, 0);
3898
0
  } else if (ce_org) {
3899
    /* Try to fetch find static method of given class. */
3900
0
    mname = Z_STR_P(callable);
3901
0
    zend_string_addref(mname);
3902
0
    ftable = &ce_org->function_table;
3903
0
    fcc->calling_scope = ce_org;
3904
0
  } else {
3905
    /* We already checked for plain function before. */
3906
0
    if (error) {
3907
0
      zend_spprintf(error, 0, "function \"%s\" not found or invalid function name", Z_STRVAL_P(callable));
3908
0
    }
3909
0
    return 0;
3910
0
  }
3911
3912
0
  lmname = zend_string_tolower(mname);
3913
0
  if (strict_class &&
3914
0
      fcc->calling_scope &&
3915
0
    zend_string_equals_literal(lmname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
3916
0
    fcc->function_handler = fcc->calling_scope->constructor;
3917
0
    if (fcc->function_handler) {
3918
0
      retval = true;
3919
0
    }
3920
0
  } else if ((zv = zend_hash_find(ftable, lmname)) != NULL) {
3921
0
    fcc->function_handler = Z_PTR_P(zv);
3922
0
    retval = true;
3923
0
    if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) &&
3924
0
        !strict_class) {
3925
0
      scope = get_scope(frame);
3926
0
      if (scope &&
3927
0
          instanceof_function(fcc->function_handler->common.scope, scope)) {
3928
3929
0
        zv = zend_hash_find(&scope->function_table, lmname);
3930
0
        if (zv != NULL) {
3931
0
          zend_function *priv_fbc = Z_PTR_P(zv);
3932
3933
0
          if ((priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE)
3934
0
           && priv_fbc->common.scope == scope) {
3935
0
            fcc->function_handler = priv_fbc;
3936
0
          }
3937
0
        }
3938
0
      }
3939
0
    }
3940
0
    if (!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC) &&
3941
0
        (fcc->calling_scope &&
3942
0
         ((fcc->object && fcc->calling_scope->__call) ||
3943
0
          (!fcc->object && fcc->calling_scope->__callstatic)))) {
3944
0
      scope = get_scope(frame);
3945
0
      ZEND_ASSERT(!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC));
3946
0
      if (!zend_check_method_accessible(fcc->function_handler, scope)) {
3947
0
        retval = false;
3948
0
        fcc->function_handler = NULL;
3949
0
        goto get_function_via_handler;
3950
0
      }
3951
0
    }
3952
0
  } else {
3953
0
get_function_via_handler:
3954
0
    if (fcc->object && fcc->calling_scope == ce_org) {
3955
0
      if (strict_class && ce_org->__call) {
3956
0
        fcc->function_handler = zend_get_call_trampoline_func(ce_org->__call, mname);
3957
0
        call_via_handler = 1;
3958
0
        retval = true;
3959
0
      } else {
3960
0
        fcc->function_handler = fcc->object->handlers->get_method(&fcc->object, mname, NULL);
3961
0
        if (fcc->function_handler) {
3962
0
          if (strict_class &&
3963
0
              (!fcc->function_handler->common.scope ||
3964
0
               !instanceof_function(ce_org, fcc->function_handler->common.scope))) {
3965
0
            zend_release_fcall_info_cache(fcc);
3966
0
          } else {
3967
0
            retval = true;
3968
0
            call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3969
0
          }
3970
0
        }
3971
0
      }
3972
0
    } else if (fcc->calling_scope) {
3973
0
      if (fcc->calling_scope->get_static_method) {
3974
0
        fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname);
3975
0
      } else {
3976
0
        fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, NULL);
3977
0
      }
3978
0
      if (fcc->function_handler) {
3979
0
        retval = true;
3980
0
        call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3981
0
        if (call_via_handler && !fcc->object) {
3982
0
          zend_object *object = zend_get_this_object(frame);
3983
0
          if (object &&
3984
0
              instanceof_function(object->ce, fcc->calling_scope)) {
3985
0
            fcc->object = object;
3986
0
          }
3987
0
        }
3988
0
      }
3989
0
    }
3990
0
  }
3991
3992
0
  if (retval) {
3993
0
    if (fcc->calling_scope && !call_via_handler) {
3994
0
      if (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT) {
3995
0
        retval = false;
3996
0
        if (error) {
3997
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));
3998
0
        }
3999
0
      } else if (!fcc->object && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
4000
0
        retval = false;
4001
0
        if (error) {
4002
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));
4003
0
        }
4004
0
      }
4005
0
      if (retval
4006
0
       && !(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC)) {
4007
0
        scope = get_scope(frame);
4008
0
        ZEND_ASSERT(!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC));
4009
0
        if (!zend_check_method_accessible(fcc->function_handler, scope)) {
4010
0
          if (error) {
4011
0
            if (*error) {
4012
0
              efree(*error);
4013
0
            }
4014
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));
4015
0
          }
4016
0
          retval = false;
4017
0
        }
4018
0
      }
4019
0
    }
4020
0
  } else if (error) {
4021
0
    if (fcc->calling_scope) {
4022
0
      zend_spprintf(error, 0, "class %s does not have a method \"%s\"", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(mname));
4023
0
    } else {
4024
0
      zend_spprintf(error, 0, "function %s() does not exist", ZSTR_VAL(mname));
4025
0
    }
4026
0
  }
4027
0
  zend_string_release_ex(lmname, 0);
4028
0
  zend_string_release_ex(mname, 0);
4029
4030
0
  if (fcc->object) {
4031
0
    fcc->called_scope = fcc->object->ce;
4032
0
    if (fcc->function_handler
4033
0
     && (fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
4034
0
      fcc->object = NULL;
4035
0
    }
4036
0
  }
4037
0
  return retval;
4038
0
}
4039
/* }}} */
4040
4041
ZEND_API zend_string *zend_get_callable_name_ex(const zval *callable, const zend_object *object) /* {{{ */
4042
0
{
4043
0
try_again:
4044
0
  switch (Z_TYPE_P(callable)) {
4045
0
    case IS_STRING:
4046
0
      if (object) {
4047
0
        return zend_create_member_string(object->ce->name, Z_STR_P(callable));
4048
0
      }
4049
0
      return zend_string_copy(Z_STR_P(callable));
4050
4051
0
    case IS_ARRAY:
4052
0
    {
4053
0
      const zval *method = NULL;
4054
0
      const zval *obj = NULL;
4055
4056
0
      if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
4057
0
        obj = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 0);
4058
0
        method = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 1);
4059
0
      }
4060
4061
0
      if (obj == NULL || method == NULL || Z_TYPE_P(method) != IS_STRING) {
4062
0
        return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
4063
0
      }
4064
4065
0
      if (Z_TYPE_P(obj) == IS_STRING) {
4066
0
        return zend_create_member_string(Z_STR_P(obj), Z_STR_P(method));
4067
0
      } else if (Z_TYPE_P(obj) == IS_OBJECT) {
4068
0
        return zend_create_member_string(Z_OBJCE_P(obj)->name, Z_STR_P(method));
4069
0
      } else {
4070
0
        return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
4071
0
      }
4072
0
    }
4073
0
    case IS_OBJECT:
4074
0
    {
4075
0
      const zend_class_entry *ce = Z_OBJCE_P(callable);
4076
4077
0
      if (ce == zend_ce_closure) {
4078
0
        const zend_function *fn = zend_get_closure_method_def(Z_OBJ_P(callable));
4079
4080
0
        if ((fn->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) && fn->common.scope) {
4081
0
          return zend_create_member_string(fn->common.scope->name, fn->common.function_name);
4082
0
        }
4083
0
        return zend_string_copy(fn->common.function_name);
4084
0
      }
4085
4086
0
      return zend_string_concat2(
4087
0
        ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
4088
0
        "::__invoke", sizeof("::__invoke") - 1);
4089
0
    }
4090
0
    case IS_REFERENCE:
4091
0
      callable = Z_REFVAL_P(callable);
4092
0
      goto try_again;
4093
0
    default:
4094
0
      return zval_get_string_func(callable);
4095
0
  }
4096
0
}
4097
/* }}} */
4098
4099
ZEND_API zend_string *zend_get_callable_name(const zval *callable) /* {{{ */
4100
0
{
4101
0
  return zend_get_callable_name_ex(callable, NULL);
4102
0
}
4103
/* }}} */
4104
4105
ZEND_API bool zend_is_callable_at_frame(
4106
    const zval *callable, zend_object *object, const zend_execute_data *frame,
4107
    uint32_t check_flags, zend_fcall_info_cache *fcc, char **error) /* {{{ */
4108
0
{
4109
0
  bool ret;
4110
0
  zend_fcall_info_cache fcc_local;
4111
0
  bool strict_class = false;
4112
4113
0
  if (fcc == NULL) {
4114
0
    fcc = &fcc_local;
4115
0
  }
4116
0
  if (error) {
4117
0
    *error = NULL;
4118
0
  }
4119
4120
0
  fcc->calling_scope = NULL;
4121
0
  fcc->called_scope = NULL;
4122
0
  fcc->function_handler = NULL;
4123
0
  fcc->object = NULL;
4124
0
  fcc->closure = NULL;
4125
4126
0
again:
4127
0
  switch (Z_TYPE_P(callable)) {
4128
0
    case IS_STRING:
4129
0
      if (object) {
4130
0
        fcc->object = object;
4131
0
        fcc->calling_scope = object->ce;
4132
0
      }
4133
4134
0
      if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4135
0
        fcc->called_scope = fcc->calling_scope;
4136
0
        return 1;
4137
0
      }
4138
4139
0
check_func:
4140
0
      ret = zend_is_callable_check_func(callable, frame, fcc, strict_class, error, check_flags & IS_CALLABLE_SUPPRESS_DEPRECATIONS);
4141
0
      if (fcc == &fcc_local) {
4142
0
        zend_release_fcall_info_cache(fcc);
4143
0
      }
4144
0
      return ret;
4145
4146
0
    case IS_ARRAY:
4147
0
      {
4148
0
        if (zend_hash_num_elements(Z_ARRVAL_P(callable)) != 2) {
4149
0
          if (error) *error = estrdup("array callback must have exactly two members");
4150
0
          return 0;
4151
0
        }
4152
4153
0
        zval *obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0);
4154
0
        zval *method = zend_hash_index_find(Z_ARRVAL_P(callable), 1);
4155
0
        if (!obj || !method) {
4156
0
          if (error) *error = estrdup("array callback has to contain indices 0 and 1");
4157
0
          return 0;
4158
0
        }
4159
4160
0
        ZVAL_DEREF(obj);
4161
0
        if (Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT) {
4162
0
          if (error) *error = estrdup("first array member is not a valid class name or object");
4163
0
          return 0;
4164
0
        }
4165
4166
0
        ZVAL_DEREF(method);
4167
0
        if (Z_TYPE_P(method) != IS_STRING) {
4168
0
          if (error) *error = estrdup("second array member is not a valid method");
4169
0
          return 0;
4170
0
        }
4171
4172
0
        if (Z_TYPE_P(obj) == IS_STRING) {
4173
0
          if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4174
0
            return 1;
4175
0
          }
4176
4177
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)) {
4178
0
            return 0;
4179
0
          }
4180
0
        } else {
4181
0
          ZEND_ASSERT(Z_TYPE_P(obj) == IS_OBJECT);
4182
0
          fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */
4183
0
          fcc->object = Z_OBJ_P(obj);
4184
4185
0
          if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4186
0
            fcc->called_scope = fcc->calling_scope;
4187
0
            return 1;
4188
0
          }
4189
0
        }
4190
4191
0
        callable = method;
4192
0
        goto check_func;
4193
0
      }
4194
0
      return 0;
4195
0
    case IS_OBJECT:
4196
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) {
4197
0
        fcc->called_scope = fcc->calling_scope;
4198
0
        fcc->closure = Z_OBJ_P(callable);
4199
0
        if (fcc == &fcc_local) {
4200
0
          zend_release_fcall_info_cache(fcc);
4201
0
        }
4202
0
        return 1;
4203
0
      }
4204
0
      if (error) *error = estrdup("no array or string given");
4205
0
      return 0;
4206
0
    case IS_REFERENCE:
4207
0
      callable = Z_REFVAL_P(callable);
4208
0
      goto again;
4209
0
    default:
4210
0
      if (error) *error = estrdup("no array or string given");
4211
0
      return 0;
4212
0
  }
4213
0
}
4214
/* }}} */
4215
4216
ZEND_API bool zend_is_callable_ex(const zval *callable, zend_object *object, uint32_t check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error) /* {{{ */
4217
0
{
4218
  /* Determine callability at the first parent user frame. */
4219
0
  const zend_execute_data *frame = EG(current_execute_data);
4220
0
  while (frame && (!frame->func || !ZEND_USER_CODE(frame->func->type))) {
4221
0
    frame = frame->prev_execute_data;
4222
0
  }
4223
4224
0
  bool ret = zend_is_callable_at_frame(callable, object, frame, check_flags, fcc, error);
4225
0
  if (callable_name) {
4226
0
    *callable_name = zend_get_callable_name_ex(callable, object);
4227
0
  }
4228
0
  return ret;
4229
0
}
4230
4231
ZEND_API bool zend_is_callable(const zval *callable, uint32_t check_flags, zend_string **callable_name) /* {{{ */
4232
0
{
4233
0
  return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL);
4234
0
}
4235
/* }}} */
4236
4237
ZEND_API zend_result zend_fcall_info_init(const zval *callable, uint32_t check_flags, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_string **callable_name, char **error) /* {{{ */
4238
0
{
4239
0
  if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, fcc, error)) {
4240
0
    return FAILURE;
4241
0
  }
4242
4243
0
  fci->size = sizeof(*fci);
4244
0
  fci->object = fcc->object;
4245
0
  ZVAL_COPY_VALUE(&fci->function_name, callable);
4246
0
  fci->retval = NULL;
4247
0
  fci->param_count = 0;
4248
0
  fci->params = NULL;
4249
0
  fci->named_params = NULL;
4250
0
  fci->consumed_args = 0;
4251
4252
0
  return SUCCESS;
4253
0
}
4254
/* }}} */
4255
4256
ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, bool free_mem) /* {{{ */
4257
0
{
4258
0
  if (fci->params) {
4259
0
    zval *p = fci->params;
4260
0
    zval *end = p + fci->param_count;
4261
4262
0
    while (p != end) {
4263
0
      i_zval_ptr_dtor(p);
4264
0
      p++;
4265
0
    }
4266
0
    if (free_mem) {
4267
0
      efree(fci->params);
4268
0
      fci->params = NULL;
4269
0
    }
4270
0
  }
4271
0
  fci->param_count = 0;
4272
0
}
4273
/* }}} */
4274
4275
ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, uint32_t *param_count, zval **params) /* {{{ */
4276
0
{
4277
0
  *param_count = fci->param_count;
4278
0
  *params = fci->params;
4279
0
  fci->param_count = 0;
4280
0
  fci->params = NULL;
4281
0
}
4282
/* }}} */
4283
4284
ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, uint32_t param_count, zval *params) /* {{{ */
4285
0
{
4286
0
  zend_fcall_info_args_clear(fci, true);
4287
0
  fci->param_count = param_count;
4288
0
  fci->params = params;
4289
0
}
4290
/* }}} */
4291
4292
ZEND_API zend_result zend_fcall_info_args_ex(zend_fcall_info *fci, zend_function *func, zval *args) /* {{{ */
4293
0
{
4294
0
  zval *arg, *params;
4295
0
  uint32_t n = 1;
4296
4297
0
  zend_fcall_info_args_clear(fci, !args);
4298
4299
0
  if (!args) {
4300
0
    return SUCCESS;
4301
0
  }
4302
4303
0
  if (Z_TYPE_P(args) != IS_ARRAY) {
4304
0
    return FAILURE;
4305
0
  }
4306
4307
0
  fci->param_count = zend_hash_num_elements(Z_ARRVAL_P(args));
4308
0
  fci->params = params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4309
4310
0
  ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), arg) {
4311
0
    if (func && !Z_ISREF_P(arg) && ARG_SHOULD_BE_SENT_BY_REF(func, n)) {
4312
0
      ZVAL_NEW_REF(params, arg);
4313
0
      Z_TRY_ADDREF_P(arg);
4314
0
    } else {
4315
0
      ZVAL_COPY(params, arg);
4316
0
    }
4317
0
    params++;
4318
0
    n++;
4319
0
  } ZEND_HASH_FOREACH_END();
4320
4321
0
  return SUCCESS;
4322
0
}
4323
/* }}} */
4324
4325
ZEND_API zend_result zend_fcall_info_args(zend_fcall_info *fci, zval *args) /* {{{ */
4326
0
{
4327
0
  return zend_fcall_info_args_ex(fci, NULL, args);
4328
0
}
4329
/* }}} */
4330
4331
ZEND_API void zend_fcall_info_argp(zend_fcall_info *fci, uint32_t argc, zval *argv) /* {{{ */
4332
0
{
4333
0
  zend_fcall_info_args_clear(fci, !argc);
4334
4335
0
  if (argc) {
4336
0
    fci->param_count = argc;
4337
0
    fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4338
4339
0
    for (uint32_t i = 0; i < argc; ++i) {
4340
0
      ZVAL_COPY(&fci->params[i], &argv[i]);
4341
0
    }
4342
0
  }
4343
0
}
4344
/* }}} */
4345
4346
ZEND_API void zend_fcall_info_argv(zend_fcall_info *fci, uint32_t argc, va_list *argv) /* {{{ */
4347
0
{
4348
0
  zend_fcall_info_args_clear(fci, !argc);
4349
4350
0
  if (argc) {
4351
0
    zval *arg;
4352
0
    fci->param_count = argc;
4353
0
    fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4354
4355
0
    for (uint32_t i = 0; i < argc; ++i) {
4356
0
      arg = va_arg(*argv, zval *);
4357
0
      ZVAL_COPY(&fci->params[i], arg);
4358
0
    }
4359
0
  }
4360
0
}
4361
/* }}} */
4362
4363
ZEND_API void zend_fcall_info_argn(zend_fcall_info *fci, uint32_t argc, ...) /* {{{ */
4364
0
{
4365
0
  va_list argv;
4366
4367
0
  va_start(argv, argc);
4368
0
  zend_fcall_info_argv(fci, argc, &argv);
4369
0
  va_end(argv);
4370
0
}
4371
/* }}} */
4372
4373
ZEND_API zend_result zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval *retval_ptr, zval *args) /* {{{ */
4374
0
{
4375
0
  zval retval, *org_params = NULL;
4376
0
  uint32_t org_count = 0;
4377
0
  zend_result result;
4378
4379
0
  fci->retval = retval_ptr ? retval_ptr : &retval;
4380
0
  if (args) {
4381
0
    zend_fcall_info_args_save(fci, &org_count, &org_params);
4382
0
    zend_fcall_info_args(fci, args);
4383
0
  }
4384
0
  result = zend_call_function(fci, fcc);
4385
4386
0
  if (!retval_ptr && Z_TYPE(retval) != IS_UNDEF) {
4387
0
    zval_ptr_dtor(&retval);
4388
0
  }
4389
0
  if (args) {
4390
0
    zend_fcall_info_args_restore(fci, org_count, org_params);
4391
0
  }
4392
0
  return result;
4393
0
}
4394
/* }}} */
4395
4396
ZEND_API void zend_get_callable_zval_from_fcc(const zend_fcall_info_cache *fcc, zval *callable)
4397
0
{
4398
0
  if (fcc->closure) {
4399
0
    ZVAL_OBJ_COPY(callable, fcc->closure);
4400
0
  } else if (fcc->function_handler->common.scope) {
4401
0
    array_init(callable);
4402
0
    if (fcc->object) {
4403
0
      GC_ADDREF(fcc->object);
4404
0
      add_next_index_object(callable, fcc->object);
4405
0
    } else {
4406
0
      add_next_index_str(callable, zend_string_copy(fcc->calling_scope->name));
4407
0
    }
4408
0
    add_next_index_str(callable, zend_string_copy(fcc->function_handler->common.function_name));
4409
0
  } else {
4410
0
    ZVAL_STR_COPY(callable, fcc->function_handler->common.function_name);
4411
0
  }
4412
0
}
4413
4414
ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */
4415
0
{
4416
0
  size_t name_len = strlen(module_name);
4417
0
  zend_module_entry *module = zend_hash_str_find_ptr_lc(&module_registry, module_name, name_len);
4418
0
  return module ? module->version : NULL;
4419
0
}
4420
/* }}} */
4421
4422
330
static zend_always_inline bool is_persistent_class(const zend_class_entry *ce) {
4423
330
  return ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->type == MODULE_PERSISTENT;
4424
330
}
4425
4426
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) /* {{{ */
4427
148
{
4428
148
  zend_property_info *property_info, *property_info_ptr;
4429
4430
148
  if (ZEND_TYPE_IS_SET(type)) {
4431
136
    ce->ce_flags |= ZEND_ACC_HAS_TYPE_HINTS;
4432
4433
136
    if (access_type & ZEND_ACC_READONLY) {
4434
60
      ce->ce_flags |= ZEND_ACC_HAS_READONLY_PROPS;
4435
60
    }
4436
136
  }
4437
4438
148
  if (ce->type == ZEND_INTERNAL_CLASS) {
4439
148
    property_info = pemalloc(sizeof(zend_property_info), 1);
4440
148
  } else {
4441
0
    property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
4442
0
    if (Z_TYPE_P(property) == IS_CONSTANT_AST) {
4443
0
      ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
4444
0
      if (access_type & ZEND_ACC_STATIC) {
4445
0
        ce->ce_flags |= ZEND_ACC_HAS_AST_STATICS;
4446
0
      } else {
4447
0
        ce->ce_flags |= ZEND_ACC_HAS_AST_PROPERTIES;
4448
0
      }
4449
0
    }
4450
0
  }
4451
4452
148
  if (Z_TYPE_P(property) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(property))) {
4453
0
    zval_make_interned_string(property);
4454
0
  }
4455
4456
148
  if (!(access_type & ZEND_ACC_PPP_MASK)) {
4457
0
    access_type |= ZEND_ACC_PUBLIC;
4458
0
  }
4459
  /* Add the protected(set) bit for public readonly properties with no set visibility. */
4460
148
  if ((access_type & (ZEND_ACC_PUBLIC|ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK)) == (ZEND_ACC_PUBLIC|ZEND_ACC_READONLY)) {
4461
58
    access_type |= ZEND_ACC_PROTECTED_SET;
4462
90
  } else if (UNEXPECTED(access_type & ZEND_ACC_PPP_SET_MASK)) {
4463
0
    if (!ZEND_TYPE_IS_SET(type)) {
4464
0
      zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4465
0
        "Property with asymmetric visibility %s::$%s must have type",
4466
0
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
4467
0
    }
4468
0
    uint32_t get_visibility = zend_visibility_to_set_visibility(access_type & ZEND_ACC_PPP_MASK);
4469
0
    uint32_t set_visibility = access_type & ZEND_ACC_PPP_SET_MASK;
4470
0
    if (get_visibility > set_visibility) {
4471
0
      zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4472
0
        "Visibility of property %s::$%s must not be weaker than set visibility",
4473
0
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
4474
0
    }
4475
    /* Remove equivalent set visibility. */
4476
0
    if (((access_type & (ZEND_ACC_PUBLIC|ZEND_ACC_PUBLIC_SET)) == (ZEND_ACC_PUBLIC|ZEND_ACC_PUBLIC_SET))
4477
0
     || ((access_type & (ZEND_ACC_PROTECTED|ZEND_ACC_PROTECTED_SET)) == (ZEND_ACC_PROTECTED|ZEND_ACC_PROTECTED_SET))
4478
0
     || ((access_type & (ZEND_ACC_PRIVATE|ZEND_ACC_PRIVATE_SET)) == (ZEND_ACC_PRIVATE|ZEND_ACC_PRIVATE_SET))) {
4479
0
      access_type &= ~ZEND_ACC_PPP_SET_MASK;
4480
0
    }
4481
    /* private(set) properties are implicitly final. */
4482
0
    if (access_type & ZEND_ACC_PRIVATE_SET) {
4483
0
      access_type |= ZEND_ACC_FINAL;
4484
0
    }
4485
0
  }
4486
4487
  /* Virtual properties have no backing storage, the offset should never be used. However, the
4488
   * virtual flag cannot be definitively determined at compile time. Allow using default values
4489
   * anyway, and assert after inheritance that the property is not actually virtual. */
4490
148
  if (access_type & ZEND_ACC_VIRTUAL) {
4491
14
    if (Z_TYPE_P(property) == IS_UNDEF) {
4492
14
      property_info->offset = (uint32_t)-1;
4493
14
      goto skip_property_storage;
4494
14
    }
4495
14
  }
4496
134
  if (access_type & ZEND_ACC_STATIC) {
4497
0
    if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
4498
0
      ZEND_ASSERT(property_info_ptr->flags & ZEND_ACC_STATIC);
4499
0
      property_info->offset = property_info_ptr->offset;
4500
0
      zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]);
4501
0
      if (property_info_ptr->doc_comment && property_info_ptr->ce == ce) {
4502
0
        zend_string_release(property_info_ptr->doc_comment);
4503
0
      }
4504
0
      zend_hash_del(&ce->properties_info, name);
4505
0
    } else {
4506
0
      property_info->offset = ce->default_static_members_count++;
4507
0
      ce->default_static_members_table = perealloc(ce->default_static_members_table, sizeof(zval) * ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS);
4508
0
    }
4509
0
    ZVAL_COPY_VALUE(&ce->default_static_members_table[property_info->offset], property);
4510
0
    if (!ZEND_MAP_PTR(ce->static_members_table)) {
4511
0
      if (ce->type == ZEND_INTERNAL_CLASS &&
4512
0
          ce->info.internal.module->type == MODULE_PERSISTENT) {
4513
0
        ZEND_MAP_PTR_NEW(ce->static_members_table);
4514
0
      }
4515
0
    }
4516
134
  } else {
4517
134
    zval *property_default_ptr;
4518
134
    if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
4519
0
      ZEND_ASSERT(!(property_info_ptr->flags & ZEND_ACC_STATIC));
4520
0
      property_info->offset = property_info_ptr->offset;
4521
0
      zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]);
4522
0
      if (property_info_ptr->doc_comment && property_info_ptr->ce == ce) {
4523
0
        zend_string_release_ex(property_info_ptr->doc_comment, 1);
4524
0
      }
4525
0
      zend_hash_del(&ce->properties_info, name);
4526
4527
0
      ZEND_ASSERT(ce->type == ZEND_INTERNAL_CLASS);
4528
0
      ZEND_ASSERT(ce->properties_info_table != NULL);
4529
0
      ce->properties_info_table[OBJ_PROP_TO_NUM(property_info->offset)] = property_info;
4530
134
    } else {
4531
134
      property_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count);
4532
134
      ce->default_properties_count++;
4533
134
      ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
4534
4535
      /* For user classes this is handled during linking */
4536
134
      if (ce->type == ZEND_INTERNAL_CLASS) {
4537
134
        ce->properties_info_table = perealloc(ce->properties_info_table, sizeof(zend_property_info *) * ce->default_properties_count, 1);
4538
134
        ce->properties_info_table[ce->default_properties_count - 1] = property_info;
4539
134
      }
4540
134
    }
4541
134
    property_default_ptr = &ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
4542
134
    ZVAL_COPY_VALUE(property_default_ptr, property);
4543
134
    Z_PROP_FLAG_P(property_default_ptr) = Z_ISUNDEF_P(property) ? IS_PROP_UNINIT : 0;
4544
134
  }
4545
148
skip_property_storage:
4546
148
  if (ce->type == ZEND_INTERNAL_CLASS) {
4547
    /* Must be interned to avoid ZTS data races */
4548
148
    if (is_persistent_class(ce)) {
4549
148
      name = zend_new_interned_string(zend_string_copy(name));
4550
148
    }
4551
4552
148
    if (Z_REFCOUNTED_P(property)) {
4553
0
      zend_error_noreturn(E_CORE_ERROR, "Internal zvals cannot be refcounted");
4554
0
    }
4555
148
  }
4556
4557
148
  if (access_type & ZEND_ACC_PUBLIC) {
4558
114
    property_info->name = zend_string_copy(name);
4559
114
  } else if (access_type & ZEND_ACC_PRIVATE) {
4560
16
    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));
4561
18
  } else {
4562
18
    ZEND_ASSERT(access_type & ZEND_ACC_PROTECTED);
4563
18
    property_info->name = zend_mangle_property_name("*", 1, ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
4564
18
  }
4565
4566
148
  property_info->name = zend_new_interned_string(property_info->name);
4567
148
  property_info->flags = access_type;
4568
148
  property_info->doc_comment = doc_comment;
4569
148
  property_info->attributes = NULL;
4570
148
  property_info->prototype = property_info;
4571
148
  property_info->hooks = NULL;
4572
148
  property_info->ce = ce;
4573
148
  property_info->type = type;
4574
4575
148
  if (is_persistent_class(ce)) {
4576
148
    zend_normalize_internal_type(&property_info->type);
4577
148
  }
4578
4579
148
  zend_hash_update_ptr(&ce->properties_info, name, property_info);
4580
4581
148
  return property_info;
4582
148
}
4583
/* }}} */
4584
4585
ZEND_API zend_result zend_try_assign_typed_ref_ex(zend_reference *ref, zval *val, bool strict) /* {{{ */
4586
0
{
4587
0
  if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, val, strict))) {
4588
0
    zval_ptr_dtor(val);
4589
0
    return FAILURE;
4590
0
  } else {
4591
0
    zend_safe_assign_to_variable_noref(&ref->val, val);
4592
0
    return SUCCESS;
4593
0
  }
4594
0
}
4595
/* }}} */
4596
4597
ZEND_API zend_result zend_try_assign_typed_ref(zend_reference *ref, zval *val) /* {{{ */
4598
0
{
4599
0
  return zend_try_assign_typed_ref_ex(ref, val, ZEND_ARG_USES_STRICT_TYPES());
4600
0
}
4601
/* }}} */
4602
4603
ZEND_API zend_result zend_try_assign_typed_ref_null(zend_reference *ref) /* {{{ */
4604
0
{
4605
0
  zval tmp;
4606
4607
0
  ZVAL_NULL(&tmp);
4608
0
  return zend_try_assign_typed_ref(ref, &tmp);
4609
0
}
4610
/* }}} */
4611
4612
ZEND_API zend_result zend_try_assign_typed_ref_bool(zend_reference *ref, bool val) /* {{{ */
4613
0
{
4614
0
  zval tmp;
4615
4616
0
  ZVAL_BOOL(&tmp, val);
4617
0
  return zend_try_assign_typed_ref(ref, &tmp);
4618
0
}
4619
/* }}} */
4620
4621
ZEND_API zend_result zend_try_assign_typed_ref_long(zend_reference *ref, zend_long lval) /* {{{ */
4622
0
{
4623
0
  zval tmp;
4624
4625
0
  ZVAL_LONG(&tmp, lval);
4626
0
  return zend_try_assign_typed_ref(ref, &tmp);
4627
0
}
4628
/* }}} */
4629
4630
ZEND_API zend_result zend_try_assign_typed_ref_double(zend_reference *ref, double dval) /* {{{ */
4631
0
{
4632
0
  zval tmp;
4633
4634
0
  ZVAL_DOUBLE(&tmp, dval);
4635
0
  return zend_try_assign_typed_ref(ref, &tmp);
4636
0
}
4637
/* }}} */
4638
4639
ZEND_API zend_result zend_try_assign_typed_ref_empty_string(zend_reference *ref) /* {{{ */
4640
0
{
4641
0
  zval tmp;
4642
4643
0
  ZVAL_EMPTY_STRING(&tmp);
4644
0
  return zend_try_assign_typed_ref(ref, &tmp);
4645
0
}
4646
/* }}} */
4647
4648
ZEND_API zend_result zend_try_assign_typed_ref_str(zend_reference *ref, zend_string *str) /* {{{ */
4649
0
{
4650
0
  zval tmp;
4651
4652
0
  ZVAL_STR(&tmp, str);
4653
0
  return zend_try_assign_typed_ref(ref, &tmp);
4654
0
}
4655
/* }}} */
4656
4657
ZEND_API zend_result zend_try_assign_typed_ref_string(zend_reference *ref, const char *string) /* {{{ */
4658
0
{
4659
0
  zval tmp;
4660
4661
0
  ZVAL_STRING(&tmp, string);
4662
0
  return zend_try_assign_typed_ref(ref, &tmp);
4663
0
}
4664
/* }}} */
4665
4666
ZEND_API zend_result zend_try_assign_typed_ref_stringl(zend_reference *ref, const char *string, size_t len) /* {{{ */
4667
0
{
4668
0
  zval tmp;
4669
4670
0
  ZVAL_STRINGL(&tmp, string, len);
4671
0
  return zend_try_assign_typed_ref(ref, &tmp);
4672
0
}
4673
/* }}} */
4674
4675
ZEND_API zend_result zend_try_assign_typed_ref_arr(zend_reference *ref, zend_array *arr) /* {{{ */
4676
0
{
4677
0
  zval tmp;
4678
4679
0
  ZVAL_ARR(&tmp, arr);
4680
0
  return zend_try_assign_typed_ref(ref, &tmp);
4681
0
}
4682
/* }}} */
4683
4684
ZEND_API zend_result zend_try_assign_typed_ref_res(zend_reference *ref, zend_resource *res) /* {{{ */
4685
0
{
4686
0
  zval tmp;
4687
4688
0
  ZVAL_RES(&tmp, res);
4689
0
  return zend_try_assign_typed_ref(ref, &tmp);
4690
0
}
4691
/* }}} */
4692
4693
ZEND_API zend_result zend_try_assign_typed_ref_zval(zend_reference *ref, zval *zv) /* {{{ */
4694
0
{
4695
0
  zval tmp;
4696
4697
0
  ZVAL_COPY_VALUE(&tmp, zv);
4698
0
  return zend_try_assign_typed_ref(ref, &tmp);
4699
0
}
4700
/* }}} */
4701
4702
ZEND_API zend_result zend_try_assign_typed_ref_zval_ex(zend_reference *ref, zval *zv, bool strict) /* {{{ */
4703
0
{
4704
0
  zval tmp;
4705
4706
0
  ZVAL_COPY_VALUE(&tmp, zv);
4707
0
  return zend_try_assign_typed_ref_ex(ref, &tmp, strict);
4708
0
}
4709
/* }}} */
4710
4711
ZEND_API void zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */
4712
0
{
4713
0
  zend_declare_typed_property(ce, name, property, access_type, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
4714
0
}
4715
/* }}} */
4716
4717
ZEND_API void zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type) /* {{{ */
4718
0
{
4719
0
  zend_string *key = zend_string_init(name, name_length, is_persistent_class(ce));
4720
0
  zend_declare_property_ex(ce, key, property, access_type, NULL);
4721
0
  zend_string_release(key);
4722
0
}
4723
/* }}} */
4724
4725
ZEND_API void zend_declare_property_null(zend_class_entry *ce, const char *name, size_t name_length, int access_type) /* {{{ */
4726
0
{
4727
0
  zval property;
4728
4729
0
  ZVAL_NULL(&property);
4730
0
  zend_declare_property(ce, name, name_length, &property, access_type);
4731
0
}
4732
/* }}} */
4733
4734
ZEND_API void zend_declare_property_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
4735
0
{
4736
0
  zval property;
4737
4738
0
  ZVAL_BOOL(&property, value);
4739
0
  zend_declare_property(ce, name, name_length, &property, access_type);
4740
0
}
4741
/* }}} */
4742
4743
ZEND_API void zend_declare_property_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
4744
0
{
4745
0
  zval property;
4746
4747
0
  ZVAL_LONG(&property, value);
4748
0
  zend_declare_property(ce, name, name_length, &property, access_type);
4749
0
}
4750
/* }}} */
4751
4752
ZEND_API void zend_declare_property_double(zend_class_entry *ce, const char *name, size_t name_length, double value, int access_type) /* {{{ */
4753
0
{
4754
0
  zval property;
4755
4756
0
  ZVAL_DOUBLE(&property, value);
4757
0
  zend_declare_property(ce, name, name_length, &property, access_type);
4758
0
}
4759
/* }}} */
4760
4761
ZEND_API void zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type) /* {{{ */
4762
0
{
4763
0
  zval property;
4764
4765
0
  ZVAL_NEW_STR(&property, zend_string_init(value, strlen(value), ce->type == ZEND_INTERNAL_CLASS));
4766
0
  zend_declare_property(ce, name, name_length, &property, access_type);
4767
0
}
4768
/* }}} */
4769
4770
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) /* {{{ */
4771
0
{
4772
0
  zval property;
4773
4774
0
  ZVAL_NEW_STR(&property, zend_string_init(value, value_len, ce->type == ZEND_INTERNAL_CLASS));
4775
0
  zend_declare_property(ce, name, name_length, &property, access_type);
4776
0
}
4777
/* }}} */
4778
4779
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) /* {{{ */
4780
536
{
4781
536
  zend_class_constant *c;
4782
4783
536
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
4784
28
    if (!(flags & ZEND_ACC_PUBLIC)) {
4785
0
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface constant %s::%s must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4786
0
    }
4787
28
  }
4788
4789
536
  if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_CLASS))) {
4790
0
    zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4791
0
        "A class constant must not be called 'class'; it is reserved for class name fetching");
4792
0
  }
4793
4794
536
  if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) {
4795
28
    zval_make_interned_string(value);
4796
28
  }
4797
4798
536
  if (ce->type == ZEND_INTERNAL_CLASS) {
4799
536
    c = pemalloc(sizeof(zend_class_constant), 1);
4800
536
    if (ZEND_TYPE_PURE_MASK(type) != MAY_BE_ANY) {
4801
536
      ZEND_ASSERT(!ZEND_TYPE_CONTAINS_CODE(type, IS_RESOURCE) && "resource is not allowed in a zend_type");
4802
536
    }
4803
536
  } else {
4804
0
    c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
4805
0
  }
4806
536
  ZVAL_COPY_VALUE(&c->value, value);
4807
536
  ZEND_CLASS_CONST_FLAGS(c) = flags;
4808
536
  c->doc_comment = doc_comment;
4809
536
  c->attributes = NULL;
4810
536
  c->ce = ce;
4811
536
  c->type = type;
4812
4813
536
  if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
4814
288
    ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
4815
288
    ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
4816
288
    if (ce->type == ZEND_INTERNAL_CLASS && !ZEND_MAP_PTR(ce->mutable_data)) {
4817
24
      ZEND_MAP_PTR_NEW(ce->mutable_data);
4818
24
    }
4819
288
  }
4820
4821
536
  if (!zend_hash_add_ptr(&ce->constants_table, name, c)) {
4822
0
    zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4823
0
      "Cannot redefine class constant %s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4824
0
  }
4825
4826
536
  return c;
4827
536
}
4828
4829
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)
4830
288
{
4831
288
  return zend_declare_typed_class_constant(ce, name, value, flags, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
4832
288
}
4833
4834
ZEND_API void zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
4835
0
{
4836
0
  zend_string *key;
4837
4838
0
  if (ce->type == ZEND_INTERNAL_CLASS) {
4839
0
    key = zend_string_init_interned(name, name_length, 1);
4840
0
  } else {
4841
0
    key = zend_string_init(name, name_length, 0);
4842
0
  }
4843
0
  zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
4844
0
  zend_string_release(key);
4845
0
}
4846
/* }}} */
4847
4848
ZEND_API void zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length) /* {{{ */
4849
0
{
4850
0
  zval constant;
4851
4852
0
  ZVAL_NULL(&constant);
4853
0
  zend_declare_class_constant(ce, name, name_length, &constant);
4854
0
}
4855
/* }}} */
4856
4857
ZEND_API void zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value) /* {{{ */
4858
0
{
4859
0
  zval constant;
4860
4861
0
  ZVAL_LONG(&constant, value);
4862
0
  zend_declare_class_constant(ce, name, name_length, &constant);
4863
0
}
4864
/* }}} */
4865
4866
ZEND_API void zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, bool value) /* {{{ */
4867
0
{
4868
0
  zval constant;
4869
4870
0
  ZVAL_BOOL(&constant, value);
4871
0
  zend_declare_class_constant(ce, name, name_length, &constant);
4872
0
}
4873
/* }}} */
4874
4875
ZEND_API void zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value) /* {{{ */
4876
0
{
4877
0
  zval constant;
4878
4879
0
  ZVAL_DOUBLE(&constant, value);
4880
0
  zend_declare_class_constant(ce, name, name_length, &constant);
4881
0
}
4882
/* }}} */
4883
4884
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) /* {{{ */
4885
0
{
4886
0
  zval constant;
4887
4888
0
  ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type == ZEND_INTERNAL_CLASS));
4889
0
  zend_declare_class_constant(ce, name, name_length, &constant);
4890
0
}
4891
/* }}} */
4892
4893
ZEND_API void zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value) /* {{{ */
4894
0
{
4895
0
  zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value));
4896
0
}
4897
/* }}} */
4898
4899
ZEND_API void zend_update_property_ex(const zend_class_entry *scope, zend_object *object, zend_string *name, zval *value) /* {{{ */
4900
0
{
4901
0
  const zend_class_entry *old_scope = EG(fake_scope);
4902
4903
0
  EG(fake_scope) = scope;
4904
4905
0
  object->handlers->write_property(object, name, value, NULL);
4906
4907
0
  EG(fake_scope) = old_scope;
4908
0
}
4909
/* }}} */
4910
4911
ZEND_API void zend_update_property(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zval *value) /* {{{ */
4912
0
{
4913
0
  zend_string *property;
4914
0
  const zend_class_entry *old_scope = EG(fake_scope);
4915
4916
0
  EG(fake_scope) = scope;
4917
4918
0
  property = zend_string_init(name, name_length, 0);
4919
0
  object->handlers->write_property(object, property, value, NULL);
4920
0
  zend_string_release_ex(property, 0);
4921
4922
0
  EG(fake_scope) = old_scope;
4923
0
}
4924
/* }}} */
4925
4926
ZEND_API void zend_update_property_null(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */
4927
0
{
4928
0
  zval tmp;
4929
4930
0
  ZVAL_NULL(&tmp);
4931
0
  zend_update_property(scope, object, name, name_length, &tmp);
4932
0
}
4933
/* }}} */
4934
4935
ZEND_API void zend_unset_property(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */
4936
0
{
4937
0
  zend_string *property;
4938
0
  const zend_class_entry *old_scope = EG(fake_scope);
4939
4940
0
  EG(fake_scope) = scope;
4941
4942
0
  property = zend_string_init(name, name_length, 0);
4943
0
  object->handlers->unset_property(object, property, 0);
4944
0
  zend_string_release_ex(property, 0);
4945
4946
0
  EG(fake_scope) = old_scope;
4947
0
}
4948
/* }}} */
4949
4950
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) /* {{{ */
4951
0
{
4952
0
  zval tmp;
4953
4954
0
  ZVAL_BOOL(&tmp, value);
4955
0
  zend_update_property(scope, object, name, name_length, &tmp);
4956
0
}
4957
/* }}} */
4958
4959
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) /* {{{ */
4960
0
{
4961
0
  zval tmp;
4962
4963
0
  ZVAL_LONG(&tmp, value);
4964
0
  zend_update_property(scope, object, name, name_length, &tmp);
4965
0
}
4966
/* }}} */
4967
4968
ZEND_API void zend_update_property_double(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, double value) /* {{{ */
4969
0
{
4970
0
  zval tmp;
4971
4972
0
  ZVAL_DOUBLE(&tmp, value);
4973
0
  zend_update_property(scope, object, name, name_length, &tmp);
4974
0
}
4975
/* }}} */
4976
4977
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) /* {{{ */
4978
0
{
4979
0
  zval tmp;
4980
4981
0
  ZVAL_STR(&tmp, value);
4982
0
  zend_update_property(scope, object, name, name_length, &tmp);
4983
0
}
4984
/* }}} */
4985
4986
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) /* {{{ */
4987
0
{
4988
0
  zval tmp;
4989
4990
0
  ZVAL_STRING(&tmp, value);
4991
0
  Z_SET_REFCOUNT(tmp, 0);
4992
0
  zend_update_property(scope, object, name, name_length, &tmp);
4993
0
}
4994
/* }}} */
4995
4996
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) /* {{{ */
4997
0
{
4998
0
  zval tmp;
4999
5000
0
  ZVAL_STRINGL(&tmp, value, value_len);
5001
0
  Z_SET_REFCOUNT(tmp, 0);
5002
0
  zend_update_property(scope, object, name, name_length, &tmp);
5003
0
}
5004
/* }}} */
5005
5006
ZEND_API zend_result zend_update_static_property_ex(zend_class_entry *scope, zend_string *name, zval *value) /* {{{ */
5007
0
{
5008
0
  zval *property, tmp;
5009
0
  zend_property_info *prop_info;
5010
5011
0
  if (UNEXPECTED(!(scope->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
5012
0
    if (UNEXPECTED(zend_update_class_constants(scope) != SUCCESS)) {
5013
0
      return FAILURE;
5014
0
    }
5015
0
  }
5016
5017
0
  const zend_class_entry *old_scope = EG(fake_scope);
5018
0
  EG(fake_scope) = scope;
5019
0
  property = zend_std_get_static_property_with_info(scope, name, BP_VAR_W, &prop_info);
5020
0
  EG(fake_scope) = old_scope;
5021
5022
0
  if (!property) {
5023
0
    return FAILURE;
5024
0
  }
5025
5026
0
  ZEND_ASSERT(!Z_ISREF_P(value));
5027
0
  Z_TRY_ADDREF_P(value);
5028
0
  if (ZEND_TYPE_IS_SET(prop_info->type)) {
5029
0
    ZVAL_COPY_VALUE(&tmp, value);
5030
0
    if (!zend_verify_property_type(prop_info, &tmp, /* strict */ 0)) {
5031
0
      Z_TRY_DELREF_P(value);
5032
0
      return FAILURE;
5033
0
    }
5034
0
    value = &tmp;
5035
0
  }
5036
5037
0
  zend_assign_to_variable(property, value, IS_TMP_VAR, /* strict */ 0);
5038
0
  return SUCCESS;
5039
0
}
5040
/* }}} */
5041
5042
ZEND_API zend_result zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value) /* {{{ */
5043
0
{
5044
0
  zend_string *key = zend_string_init(name, name_length, 0);
5045
0
  zend_result retval = zend_update_static_property_ex(scope, key, value);
5046
0
  zend_string_efree(key);
5047
0
  return retval;
5048
0
}
5049
/* }}} */
5050
5051
ZEND_API zend_result zend_update_static_property_null(zend_class_entry *scope, const char *name, size_t name_length) /* {{{ */
5052
0
{
5053
0
  zval tmp;
5054
5055
0
  ZVAL_NULL(&tmp);
5056
0
  return zend_update_static_property(scope, name, name_length, &tmp);
5057
0
}
5058
/* }}} */
5059
5060
ZEND_API zend_result zend_update_static_property_bool(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
5061
0
{
5062
0
  zval tmp;
5063
5064
0
  ZVAL_BOOL(&tmp, value);
5065
0
  return zend_update_static_property(scope, name, name_length, &tmp);
5066
0
}
5067
/* }}} */
5068
5069
ZEND_API zend_result zend_update_static_property_long(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
5070
0
{
5071
0
  zval tmp;
5072
5073
0
  ZVAL_LONG(&tmp, value);
5074
0
  return zend_update_static_property(scope, name, name_length, &tmp);
5075
0
}
5076
/* }}} */
5077
5078
ZEND_API zend_result zend_update_static_property_double(zend_class_entry *scope, const char *name, size_t name_length, double value) /* {{{ */
5079
0
{
5080
0
  zval tmp;
5081
5082
0
  ZVAL_DOUBLE(&tmp, value);
5083
0
  return zend_update_static_property(scope, name, name_length, &tmp);
5084
0
}
5085
/* }}} */
5086
5087
ZEND_API zend_result zend_update_static_property_string(zend_class_entry *scope, const char *name, size_t name_length, const char *value) /* {{{ */
5088
0
{
5089
0
  zval tmp;
5090
5091
0
  ZVAL_STRING(&tmp, value);
5092
0
  Z_SET_REFCOUNT(tmp, 0);
5093
0
  return zend_update_static_property(scope, name, name_length, &tmp);
5094
0
}
5095
/* }}} */
5096
5097
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) /* {{{ */
5098
0
{
5099
0
  zval tmp;
5100
5101
0
  ZVAL_STRINGL(&tmp, value, value_len);
5102
0
  Z_SET_REFCOUNT(tmp, 0);
5103
0
  return zend_update_static_property(scope, name, name_length, &tmp);
5104
0
}
5105
/* }}} */
5106
5107
ZEND_API zval *zend_read_property_ex(const zend_class_entry *scope, zend_object *object, zend_string *name, bool silent, zval *rv) /* {{{ */
5108
0
{
5109
0
  zval *value;
5110
0
  const zend_class_entry *old_scope = EG(fake_scope);
5111
5112
0
  EG(fake_scope) = scope;
5113
5114
0
  value = object->handlers->read_property(object, name, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
5115
5116
0
  EG(fake_scope) = old_scope;
5117
0
  return value;
5118
0
}
5119
/* }}} */
5120
5121
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) /* {{{ */
5122
0
{
5123
0
  zval *value;
5124
0
  zend_string *str;
5125
5126
0
  str = zend_string_init(name, name_length, 0);
5127
0
  value = zend_read_property_ex(scope, object, str, silent, rv);
5128
0
  zend_string_release_ex(str, 0);
5129
0
  return value;
5130
0
}
5131
/* }}} */
5132
5133
ZEND_API zval *zend_read_static_property_ex(zend_class_entry *scope, zend_string *name, bool silent) /* {{{ */
5134
0
{
5135
0
  zval *property;
5136
0
  const zend_class_entry *old_scope = EG(fake_scope);
5137
5138
0
  EG(fake_scope) = scope;
5139
0
  property = zend_std_get_static_property(scope, name, silent ? BP_VAR_IS : BP_VAR_R);
5140
0
  EG(fake_scope) = old_scope;
5141
5142
0
  return property;
5143
0
}
5144
/* }}} */
5145
5146
ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, size_t name_length, bool silent) /* {{{ */
5147
0
{
5148
0
  zend_string *key = zend_string_init(name, name_length, 0);
5149
0
  zval *property = zend_read_static_property_ex(scope, key, silent);
5150
0
  zend_string_efree(key);
5151
0
  return property;
5152
0
}
5153
/* }}} */
5154
5155
ZEND_API void zend_save_error_handling(zend_error_handling *current) /* {{{ */
5156
0
{
5157
0
  current->handling = EG(error_handling);
5158
0
  current->exception = EG(exception_class);
5159
0
}
5160
/* }}} */
5161
5162
ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current) /* {{{ */
5163
0
{
5164
0
  if (current) {
5165
0
    zend_save_error_handling(current);
5166
0
  }
5167
0
  ZEND_ASSERT(error_handling == EH_THROW || exception_class == NULL);
5168
0
  EG(error_handling) = error_handling;
5169
0
  EG(exception_class) = exception_class;
5170
0
}
5171
/* }}} */
5172
5173
ZEND_API void zend_restore_error_handling(const zend_error_handling *saved) /* {{{ */
5174
0
{
5175
0
  EG(error_handling) = saved->handling;
5176
0
  EG(exception_class) = saved->exception;
5177
0
}
5178
/* }}} */
5179
5180
ZEND_API ZEND_COLD const char *zend_get_object_type_case(const zend_class_entry *ce, bool upper_case) /* {{{ */
5181
0
{
5182
0
  if (ce->ce_flags & ZEND_ACC_TRAIT) {
5183
0
    return upper_case ? "Trait" : "trait";
5184
0
  } else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
5185
0
    return upper_case ? "Interface" : "interface";
5186
0
  } else if (ce->ce_flags & ZEND_ACC_ENUM) {
5187
0
    return upper_case ? "Enum" : "enum";
5188
0
  } else {
5189
0
    return upper_case ? "Class" : "class";
5190
0
  }
5191
0
}
5192
/* }}} */
5193
5194
ZEND_API bool zend_is_iterable(const zval *iterable) /* {{{ */
5195
0
{
5196
0
  switch (Z_TYPE_P(iterable)) {
5197
0
    case IS_ARRAY:
5198
0
      return 1;
5199
0
    case IS_OBJECT:
5200
0
      return zend_class_implements_interface(Z_OBJCE_P(iterable), zend_ce_traversable);
5201
0
    default:
5202
0
      return 0;
5203
0
  }
5204
0
}
5205
/* }}} */
5206
5207
ZEND_API bool zend_is_countable(const zval *countable) /* {{{ */
5208
0
{
5209
0
  switch (Z_TYPE_P(countable)) {
5210
0
    case IS_ARRAY:
5211
0
      return 1;
5212
0
    case IS_OBJECT:
5213
0
      if (Z_OBJ_HT_P(countable)->count_elements) {
5214
0
        return 1;
5215
0
      }
5216
5217
0
      return zend_class_implements_interface(Z_OBJCE_P(countable), zend_ce_countable);
5218
0
    default:
5219
0
      return 0;
5220
0
  }
5221
0
}
5222
/* }}} */
5223
5224
0
static zend_result get_default_via_ast(zval *default_value_zval, const char *default_value) {
5225
0
  zend_ast *ast;
5226
0
  zend_arena *ast_arena;
5227
5228
0
  zend_string *code = zend_string_concat3(
5229
0
    "<?php ", sizeof("<?php ") - 1, default_value, strlen(default_value), ";", 1);
5230
5231
0
  ast = zend_compile_string_to_ast(code, &ast_arena, ZSTR_EMPTY_ALLOC());
5232
0
  zend_string_release(code);
5233
5234
0
  if (!ast) {
5235
0
    return FAILURE;
5236
0
  }
5237
5238
0
  zend_ast_list *statement_list = zend_ast_get_list(ast);
5239
0
  zend_ast **const_expr_ast_ptr = &statement_list->child[0];
5240
5241
0
  zend_arena *original_ast_arena = CG(ast_arena);
5242
0
  uint32_t original_compiler_options = CG(compiler_options);
5243
0
  zend_file_context original_file_context;
5244
0
  CG(ast_arena) = ast_arena;
5245
  /* Disable constant substitution, to make getDefaultValueConstant() work. */
5246
0
  CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
5247
0
  zend_file_context_begin(&original_file_context);
5248
0
  zend_const_expr_to_zval(default_value_zval, const_expr_ast_ptr, /* allow_dynamic */ true);
5249
0
  CG(ast_arena) = original_ast_arena;
5250
0
  CG(compiler_options) = original_compiler_options;
5251
0
  zend_file_context_end(&original_file_context);
5252
5253
0
  zend_ast_destroy(ast);
5254
0
  zend_arena_destroy(ast_arena);
5255
5256
0
  return SUCCESS;
5257
0
}
5258
5259
0
static zend_string *try_parse_string(const char *str, size_t len, char quote) {
5260
0
  if (len == 0) {
5261
0
    return ZSTR_EMPTY_ALLOC();
5262
0
  }
5263
5264
0
  for (size_t i = 0; i < len; i++) {
5265
0
    if (str[i] == '\\' || str[i] == quote) {
5266
0
      return NULL;
5267
0
    }
5268
0
  }
5269
0
  return zend_string_init(str, len, 0);
5270
0
}
5271
5272
ZEND_API zend_result zend_get_default_from_internal_arg_info(
5273
    zval *default_value_zval, const zend_arg_info *arg_info)
5274
0
{
5275
0
  const zend_string *default_value = arg_info->default_value;
5276
0
  if (!default_value) {
5277
0
    return FAILURE;
5278
0
  }
5279
5280
  /* Avoid going through the full AST machinery for some simple and common cases. */
5281
0
  zend_ulong lval;
5282
0
  if (zend_string_equals_literal(default_value, "null")) {
5283
0
    ZVAL_NULL(default_value_zval);
5284
0
    return SUCCESS;
5285
0
  } else if (zend_string_equals_literal(default_value, "true")) {
5286
0
    ZVAL_TRUE(default_value_zval);
5287
0
    return SUCCESS;
5288
0
  } else if (zend_string_equals_literal(default_value, "false")) {
5289
0
    ZVAL_FALSE(default_value_zval);
5290
0
    return SUCCESS;
5291
0
  } else if (ZSTR_LEN(default_value) >= 2
5292
0
      && (ZSTR_VAL(default_value)[0] == '\'' || ZSTR_VAL(default_value)[0] == '"')
5293
0
      && ZSTR_VAL(default_value)[ZSTR_LEN(default_value) - 1] == ZSTR_VAL(default_value)[0]) {
5294
0
    zend_string *str = try_parse_string(
5295
0
      ZSTR_VAL(default_value) + 1, ZSTR_LEN(default_value) - 2, ZSTR_VAL(default_value)[0]);
5296
0
    if (str) {
5297
0
      ZVAL_STR(default_value_zval, str);
5298
0
      return SUCCESS;
5299
0
    }
5300
0
  } else if (zend_string_equals_literal(default_value, "[]")) {
5301
0
    ZVAL_EMPTY_ARRAY(default_value_zval);
5302
0
    return SUCCESS;
5303
0
  } else if (ZEND_HANDLE_NUMERIC(default_value, lval)) {
5304
0
    ZVAL_LONG(default_value_zval, lval);
5305
0
    return SUCCESS;
5306
0
  }
5307
5308
#if 0
5309
  fprintf(stderr, "Evaluating %s via AST\n", ZSTR_VAL(default_value));
5310
#endif
5311
0
  return get_default_via_ast(default_value_zval, ZSTR_VAL(default_value));
5312
0
}