Coverage Report

Created: 2022-02-19 20:29

/src/php-src/Zend/zend_API.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 2.00 of the Zend license,     |
8
   | that is bundled with this package in the file LICENSE, and is        |
9
   | available through the world-wide-web at the following url:           |
10
   | http://www.zend.com/license/2_00.txt.                                |
11
   | If you did not receive a copy of the Zend license and are unable to  |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@zend.com so we can mail you a copy immediately.              |
14
   +----------------------------------------------------------------------+
15
   | Authors: Andi Gutmans <andi@php.net>                                 |
16
   |          Zeev Suraski <zeev@php.net>                                 |
17
   |          Andrei Zmievski <andrei@php.net>                            |
18
   |          Dmitry Stogov <dmitry@php.net>                              |
19
   +----------------------------------------------------------------------+
20
*/
21
22
#include "zend.h"
23
#include "zend_execute.h"
24
#include "zend_API.h"
25
#include "zend_modules.h"
26
#include "zend_extensions.h"
27
#include "zend_constants.h"
28
#include "zend_interfaces.h"
29
#include "zend_exceptions.h"
30
#include "zend_closures.h"
31
#include "zend_inheritance.h"
32
#include "zend_ini.h"
33
34
#include <stdarg.h>
35
36
/* these variables are true statics/globals, and have to be mutex'ed on every access */
37
ZEND_API HashTable module_registry;
38
39
static zend_module_entry **module_request_startup_handlers;
40
static zend_module_entry **module_request_shutdown_handlers;
41
static zend_module_entry **module_post_deactivate_handlers;
42
43
static zend_class_entry  **class_cleanup_handlers;
44
45
ZEND_API zend_result _zend_get_parameters_array_ex(uint32_t param_count, zval *argument_array) /* {{{ */
46
710
{
47
710
  zval *param_ptr;
48
710
  uint32_t arg_count;
49
50
710
  param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
51
710
  arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
52
53
710
  if (param_count>arg_count) {
54
0
    return FAILURE;
55
0
  }
56
57
1.42k
  while (param_count-->0) {
58
710
    ZVAL_COPY_VALUE(argument_array, param_ptr);
59
710
    argument_array++;
60
710
    param_ptr++;
61
710
  }
62
63
710
  return SUCCESS;
64
710
}
65
/* }}} */
66
67
ZEND_API zend_result zend_copy_parameters_array(uint32_t param_count, zval *argument_array) /* {{{ */
68
0
{
69
0
  zval *param_ptr;
70
0
  uint32_t arg_count;
71
72
0
  param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
73
0
  arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
74
75
0
  if (param_count>arg_count) {
76
0
    return FAILURE;
77
0
  }
78
79
0
  while (param_count-->0) {
80
0
    Z_TRY_ADDREF_P(param_ptr);
81
0
    zend_hash_next_index_insert_new(Z_ARRVAL_P(argument_array), param_ptr);
82
0
    param_ptr++;
83
0
  }
84
85
0
  return SUCCESS;
86
0
}
87
/* }}} */
88
89
ZEND_API ZEND_COLD void zend_wrong_param_count(void) /* {{{ */
90
0
{
91
0
  const char *space;
92
0
  const char *class_name = get_active_class_name(&space);
93
94
0
  zend_argument_count_error("Wrong parameter count for %s%s%s()", class_name, space, get_active_function_name());
95
0
}
96
/* }}} */
97
98
/* Argument parsing API -- andrei */
99
ZEND_API const char *zend_get_type_by_const(int type) /* {{{ */
100
170k
{
101
170k
  switch(type) {
102
1.58k
    case IS_FALSE:
103
3.74k
    case IS_TRUE:
104
3.74k
    case _IS_BOOL:
105
3.74k
      return "bool";
106
18.2k
    case IS_LONG:
107
18.2k
      return "int";
108
3.33k
    case IS_DOUBLE:
109
3.33k
      return "float";
110
26.6k
    case IS_STRING:
111
26.6k
      return "string";
112
0
    case IS_OBJECT:
113
0
      return "object";
114
0
    case IS_RESOURCE:
115
0
      return "resource";
116
108k
    case IS_NULL:
117
108k
      return "null";
118
0
    case IS_CALLABLE:
119
0
      return "callable";
120
0
    case IS_ITERABLE:
121
0
      return "iterable";
122
9.53k
    case IS_ARRAY:
123
9.53k
      return "array";
124
0
    case IS_VOID:
125
0
      return "void";
126
0
    case IS_MIXED:
127
0
      return "mixed";
128
0
    case _IS_NUMBER:
129
0
      return "number";
130
0
    EMPTY_SWITCH_DEFAULT_CASE()
131
170k
  }
132
170k
}
133
/* }}} */
134
135
ZEND_API const char *zend_zval_type_name(const zval *arg) /* {{{ */
136
206k
{
137
206k
  ZVAL_DEREF(arg);
138
139
206k
  if (Z_ISUNDEF_P(arg)) {
140
16.3k
    return "null";
141
16.3k
  }
142
143
190k
  if (Z_TYPE_P(arg) == IS_OBJECT) {
144
21.4k
    return ZSTR_VAL(Z_OBJCE_P(arg)->name);
145
21.4k
  }
146
147
169k
  return zend_get_type_by_const(Z_TYPE_P(arg));
148
169k
}
149
/* }}} */
150
151
/* This API exists *only* for use in gettype().
152
 * For anything else, you likely want zend_zval_type_name(). */
153
ZEND_API zend_string *zend_zval_get_legacy_type(const zval *arg) /* {{{ */
154
1.65k
{
155
1.65k
  switch (Z_TYPE_P(arg)) {
156
207
    case IS_NULL:
157
207
      return ZSTR_KNOWN(ZEND_STR_NULL);
158
48
    case IS_FALSE:
159
48
    case IS_TRUE:
160
48
      return ZSTR_KNOWN(ZEND_STR_BOOLEAN);
161
677
    case IS_LONG:
162
677
      return ZSTR_KNOWN(ZEND_STR_INTEGER);
163
115
    case IS_DOUBLE:
164
115
      return ZSTR_KNOWN(ZEND_STR_DOUBLE);
165
1
    case IS_STRING:
166
1
      return ZSTR_KNOWN(ZEND_STR_STRING);
167
607
    case IS_ARRAY:
168
607
      return ZSTR_KNOWN(ZEND_STR_ARRAY);
169
0
    case IS_OBJECT:
170
0
      return ZSTR_KNOWN(ZEND_STR_OBJECT);
171
0
    case IS_RESOURCE:
172
0
      if (zend_rsrc_list_get_rsrc_type(Z_RES_P(arg))) {
173
0
        return ZSTR_KNOWN(ZEND_STR_RESOURCE);
174
0
      } else {
175
0
        return ZSTR_KNOWN(ZEND_STR_CLOSED_RESOURCE);
176
0
      }
177
0
    default:
178
0
      return NULL;
179
1.65k
  }
180
1.65k
}
181
/* }}} */
182
183
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_none_error(void) /* {{{ */
184
1.99k
{
185
1.99k
  int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
186
1.99k
  zend_function *active_function = EG(current_execute_data)->func;
187
1.19k
  const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
188
189
1.99k
  zend_argument_count_error(
190
1.99k
        "%s%s%s() expects exactly 0 parameters, %d given",
191
1.99k
        class_name, \
192
1.19k
        class_name[0] ? "::" : "", \
193
1.99k
        ZSTR_VAL(active_function->common.function_name),
194
1.99k
        num_args);
195
1.99k
}
196
/* }}} */
197
198
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_error(uint32_t min_num_args, uint32_t max_num_args) /* {{{ */
199
2.72k
{
200
2.72k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
201
2.72k
  zend_function *active_function = EG(current_execute_data)->func;
202
2.40k
  const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
203
204
2.72k
  zend_argument_count_error(
205
2.72k
        "%s%s%s() expects %s %d parameter%s, %d given",
206
2.72k
        class_name, \
207
2.40k
        class_name[0] ? "::" : "", \
208
2.72k
        ZSTR_VAL(active_function->common.function_name),
209
1.40k
        min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
210
1.59k
        num_args < min_num_args ? min_num_args : max_num_args,
211
1.59k
        (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
212
2.72k
        num_args);
213
2.72k
}
214
/* }}} */
215
216
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_error(int error_code, uint32_t num, char *name, zend_expected_type expected_type, zval *arg) /* {{{ */
217
5.01k
{
218
5.01k
  switch (error_code) {
219
409
    case ZPP_ERROR_WRONG_CALLBACK:
220
409
      zend_wrong_callback_error(num, name);
221
409
      break;
222
19
    case ZPP_ERROR_WRONG_CLASS:
223
19
      zend_wrong_parameter_class_error(num, name, arg);
224
19
      break;
225
57
    case ZPP_ERROR_WRONG_CLASS_OR_NULL:
226
57
      zend_wrong_parameter_class_or_null_error(num, name, arg);
227
57
      break;
228
1.72k
    case ZPP_ERROR_WRONG_ARG:
229
1.72k
      zend_wrong_parameter_type_error(num, expected_type, arg);
230
1.72k
      break;
231
7
    case ZPP_ERROR_WRONG_STRING_OR_CLASS:
232
7
      zend_wrong_parameter_string_or_class_error(num, name, arg);
233
7
      break;
234
7
    case ZPP_ERROR_WRONG_STRING_OR_CLASS_OR_NULL:
235
7
      zend_wrong_parameter_string_or_class_or_null_error(num, name, arg);
236
7
      break;
237
63
    case ZPP_ERROR_UNEXPECTED_EXTRA_NAMED:
238
63
      zend_unexpected_extra_named_error();
239
63
      break;
240
2.72k
    default:
241
2.72k
      ZEND_ASSERT(error_code != ZPP_ERROR_OK);
242
5.01k
  }
243
5.01k
}
244
/* }}} */
245
246
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_type_error(uint32_t num, zend_expected_type expected_type, zval *arg) /* {{{ */
247
1.72k
{
248
1.72k
  static const char * const expected_error[] = {
249
48.2k
    Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR)
250
1.72k
    NULL
251
1.72k
  };
252
253
1.72k
  if (EG(exception)) {
254
86
    return;
255
86
  }
256
257
1.63k
  zend_argument_type_error(num, "must be %s, %s given", expected_error[expected_type], zend_zval_type_name(arg));
258
1.63k
}
259
/* }}} */
260
261
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_error(uint32_t num, const char *name, zval *arg) /* {{{ */
262
19
{
263
19
  if (EG(exception)) {
264
0
    return;
265
0
  }
266
267
19
  zend_argument_type_error(num, "must be of type %s, %s given", name, zend_zval_type_name(arg));
268
19
}
269
/* }}} */
270
271
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_null_error(uint32_t num, const char *name, zval *arg) /* {{{ */
272
57
{
273
57
  if (EG(exception)) {
274
0
    return;
275
0
  }
276
277
57
  zend_argument_type_error(num, "must be of type ?%s, %s given", name, zend_zval_type_name(arg));
278
57
}
279
/* }}} */
280
281
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_string_or_class_error(uint32_t num, const char *name, zval *arg) /* {{{ */
282
7
{
283
7
  if (EG(exception)) {
284
0
    return;
285
0
  }
286
287
7
  zend_argument_type_error(num, "must be of type %s|string, %s given", name, zend_zval_type_name(arg));
288
7
}
289
/* }}} */
290
291
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_string_or_class_or_null_error(uint32_t num, const char *name, zval *arg) /* {{{ */
292
7
{
293
7
  if (EG(exception)) {
294
0
    return;
295
0
  }
296
297
7
  zend_argument_type_error(num, "must be of type %s|string|null, %s given", name, zend_zval_type_name(arg));
298
7
}
299
/* }}} */
300
301
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_error(uint32_t num, char *error) /* {{{ */
302
409
{
303
409
  if (EG(exception)) {
304
0
    return;
305
0
  }
306
307
409
  zend_argument_type_error(num, "must be a valid callback, %s", error);
308
409
  efree(error);
309
409
}
310
/* }}} */
311
312
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_unexpected_extra_named_error(void)
313
91
{
314
91
  const char *space;
315
91
  const char *class_name = get_active_class_name(&space);
316
91
  zend_argument_count_error("%s%s%s() does not accept unknown named parameters",
317
91
    class_name, space, get_active_function_name());
318
91
}
319
320
static ZEND_COLD void ZEND_FASTCALL zend_argument_error_variadic(zend_class_entry *error_ce, uint32_t arg_num, const char *format, va_list va) /* {{{ */
321
5.28k
{
322
5.28k
  const char *space;
323
5.28k
  const char *class_name;
324
5.28k
  const char *arg_name;
325
5.28k
  char *message = NULL;
326
5.28k
  if (EG(exception)) {
327
181
    return;
328
181
  }
329
330
5.10k
  class_name = get_active_class_name(&space);
331
5.10k
  arg_name = get_active_function_arg_name(arg_num);
332
333
5.10k
  zend_vspprintf(&message, 0, format, va);
334
5.10k
  zend_throw_error(error_ce, "%s%s%s(): Argument #%d%s%s%s %s",
335
5.10k
    class_name, space, get_active_function_name(), arg_num,
336
5.08k
    arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "", message
337
5.10k
    );
338
5.10k
  efree(message);
339
5.10k
}
340
/* }}} */
341
342
ZEND_API ZEND_COLD void zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format, ...) /* {{{ */
343
824
{
344
824
  va_list va;
345
346
824
  va_start(va, format);
347
824
  zend_argument_error_variadic(error_ce, arg_num, format, va);
348
824
  va_end(va);
349
824
}
350
/* }}} */
351
352
ZEND_API ZEND_COLD void zend_argument_type_error(uint32_t arg_num, const char *format, ...) /* {{{ */
353
3.45k
{
354
3.45k
  va_list va;
355
356
3.45k
  va_start(va, format);
357
3.45k
  zend_argument_error_variadic(zend_ce_type_error, arg_num, format, va);
358
3.45k
  va_end(va);
359
3.45k
}
360
/* }}} */
361
362
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format, ...) /* {{{ */
363
1.00k
{
364
1.00k
  va_list va;
365
366
1.00k
  va_start(va, format);
367
1.00k
  zend_argument_error_variadic(zend_ce_value_error, arg_num, format, va);
368
1.00k
  va_end(va);
369
1.00k
}
370
/* }}} */
371
372
ZEND_API bool ZEND_FASTCALL zend_parse_arg_class(zval *arg, zend_class_entry **pce, uint32_t num, bool check_null) /* {{{ */
373
0
{
374
0
  zend_class_entry *ce_base = *pce;
375
376
0
  if (check_null && Z_TYPE_P(arg) == IS_NULL) {
377
0
    *pce = NULL;
378
0
    return 1;
379
0
  }
380
0
  if (!try_convert_to_string(arg)) {
381
0
    *pce = NULL;
382
0
    return 0;
383
0
  }
384
385
0
  *pce = zend_lookup_class(Z_STR_P(arg));
386
0
  if (ce_base) {
387
0
    if ((!*pce || !instanceof_function(*pce, ce_base))) {
388
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));
389
0
      *pce = NULL;
390
0
      return 0;
391
0
    }
392
0
  }
393
0
  if (!*pce) {
394
0
    zend_argument_type_error(num, "must be a valid class name, %s given", Z_STRVAL_P(arg));
395
0
    return 0;
396
0
  }
397
0
  return 1;
398
0
}
399
/* }}} */
400
401
ZEND_API bool ZEND_FASTCALL zend_parse_arg_bool_weak(zval *arg, zend_bool *dest) /* {{{ */
402
14.0k
{
403
14.0k
  if (EXPECTED(Z_TYPE_P(arg) <= IS_STRING)) {
404
14.0k
    *dest = zend_is_true(arg);
405
5
  } else {
406
5
    return 0;
407
5
  }
408
14.0k
  return 1;
409
14.0k
}
410
/* }}} */
411
412
ZEND_API bool ZEND_FASTCALL zend_parse_arg_bool_slow(zval *arg, zend_bool *dest) /* {{{ */
413
6.99k
{
414
6.99k
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
415
0
    return 0;
416
0
  }
417
6.99k
  return zend_parse_arg_bool_weak(arg, dest);
418
6.99k
}
419
/* }}} */
420
421
ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_weak(zval *arg, zend_long *dest) /* {{{ */
422
24.3k
{
423
24.3k
  if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
424
2.61k
    if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
425
18
      return 0;
426
18
    }
427
2.59k
    if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
428
186
      return 0;
429
2.41k
    } else {
430
2.41k
      *dest = zend_dval_to_lval(Z_DVAL_P(arg));
431
2.41k
    }
432
21.7k
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
433
11.3k
    double d;
434
11.3k
    zend_uchar type;
435
436
11.3k
    if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
437
2.47k
      if (EXPECTED(type != 0)) {
438
1.70k
        if (UNEXPECTED(zend_isnan(d))) {
439
0
          return 0;
440
0
        }
441
1.70k
        if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
442
210
          return 0;
443
1.49k
        } else {
444
1.49k
          *dest = zend_dval_to_lval(d);
445
1.49k
        }
446
771
      } else {
447
771
        return 0;
448
771
      }
449
10.3k
    }
450
10.3k
    if (UNEXPECTED(EG(exception))) {
451
0
      return 0;
452
0
    }
453
10.4k
  } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
454
7.42k
    *dest = 0;
455
2.98k
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
456
1.02k
    *dest = 1;
457
1.95k
  } else {
458
1.95k
    return 0;
459
1.95k
  }
460
21.1k
  return 1;
461
21.1k
}
462
/* }}} */
463
464
ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_slow(zval *arg, zend_long *dest) /* {{{ */
465
9.14k
{
466
9.14k
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
467
20
    return 0;
468
20
  }
469
9.12k
  return zend_parse_arg_long_weak(arg, dest);
470
9.12k
}
471
/* }}} */
472
473
ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_weak(zval *arg, double *dest) /* {{{ */
474
11.6k
{
475
11.6k
  if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
476
4.48k
    *dest = (double)Z_LVAL_P(arg);
477
7.16k
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
478
4.39k
    zend_long l;
479
4.39k
    zend_uchar type;
480
481
4.39k
    if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), &l, dest)) != IS_DOUBLE)) {
482
3.69k
      if (EXPECTED(type != 0)) {
483
2.19k
        *dest = (double)(l);
484
1.50k
      } else {
485
1.50k
        return 0;
486
1.50k
      }
487
2.89k
    }
488
2.89k
    if (UNEXPECTED(EG(exception))) {
489
0
      return 0;
490
0
    }
491
2.76k
  } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
492
1.22k
    *dest = 0.0;
493
1.54k
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
494
352
    *dest = 1.0;
495
1.19k
  } else {
496
1.19k
    return 0;
497
1.19k
  }
498
8.95k
  return 1;
499
8.95k
}
500
/* }}} */
501
502
ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_slow(zval *arg, double *dest) /* {{{ */
503
2.86k
{
504
2.86k
  if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
505
    /* SSTH Exception: IS_LONG may be accepted instead as IS_DOUBLE */
506
1.38k
    *dest = (double)Z_LVAL_P(arg);
507
1.48k
  } else if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
508
0
    return 0;
509
0
  }
510
2.86k
  return zend_parse_arg_double_weak(arg, dest);
511
2.86k
}
512
/* }}} */
513
514
ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_slow(zval *arg, zval **dest) /* {{{ */
515
4
{
516
4
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
517
0
    return 0;
518
0
  }
519
4
  if (Z_TYPE_P(arg) == IS_STRING) {
520
0
    zend_string *str = Z_STR_P(arg);
521
0
    zend_long lval;
522
0
    double dval;
523
0
    zend_uchar type = is_numeric_str_function(str, &lval, &dval);
524
0
    if (type == IS_LONG) {
525
0
      ZVAL_LONG(arg, lval);
526
0
    } else if (type == IS_DOUBLE) {
527
0
      ZVAL_DOUBLE(arg, dval);
528
0
    } else {
529
0
      return 0;
530
0
    }
531
0
    zend_string_release(str);
532
4
  } else if (Z_TYPE_P(arg) < IS_TRUE) {
533
4
    ZVAL_LONG(arg, 0);
534
0
  } else if (Z_TYPE_P(arg) == IS_TRUE) {
535
0
    ZVAL_LONG(arg, 1);
536
0
  } else {
537
0
    return 0;
538
0
  }
539
4
  *dest = arg;
540
4
  return 1;
541
4
}
542
/* }}} */
543
544
ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest) /* {{{ */
545
68.3k
{
546
68.3k
  if (EXPECTED(Z_TYPE_P(arg) < IS_STRING)) {
547
59.9k
    convert_to_string(arg);
548
59.9k
    *dest = Z_STR_P(arg);
549
8.41k
  } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
550
8.24k
    zend_object *zobj = Z_OBJ_P(arg);
551
8.24k
    zval obj;
552
8.24k
    if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) {
553
6.81k
      OBJ_RELEASE(zobj);
554
6.81k
      ZVAL_COPY_VALUE(arg, &obj);
555
6.81k
      *dest = Z_STR_P(arg);
556
6.81k
      return 1;
557
6.81k
    }
558
1.42k
    return 0;
559
172
  } else {
560
172
    return 0;
561
172
  }
562
59.9k
  return 1;
563
59.9k
}
564
/* }}} */
565
566
ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest) /* {{{ */
567
62.5k
{
568
62.5k
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
569
101
    return 0;
570
101
  }
571
62.4k
  return zend_parse_arg_str_weak(arg, dest);
572
62.4k
}
573
/* }}} */
574
575
ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_or_long_slow(zval *arg, zend_string **dest_str, zend_long *dest_long) /* {{{ */
576
1
{
577
1
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
578
0
    return 0;
579
0
  }
580
1
  if (zend_parse_arg_long_weak(arg, dest_long)) {
581
1
    *dest_str = NULL;
582
1
    return 1;
583
0
  } else if (zend_parse_arg_str_weak(arg, dest_str)) {
584
0
    *dest_long = 0;
585
0
    return 1;
586
0
  } else {
587
0
    return 0;
588
0
  }
589
1
}
590
/* }}} */
591
592
static const char *zend_parse_arg_impl(zval *arg, va_list *va, const char **spec, char **error) /* {{{ */
593
107k
{
594
107k
  const char *spec_walk = *spec;
595
107k
  char c = *spec_walk++;
596
107k
  bool check_null = 0;
597
107k
  bool separate = 0;
598
107k
  zval *real_arg = arg;
599
600
  /* scan through modifiers */
601
107k
  ZVAL_DEREF(arg);
602
121k
  while (1) {
603
121k
    if (*spec_walk == '/') {
604
0
      SEPARATE_ZVAL_NOREF(arg);
605
0
      real_arg = arg;
606
0
      separate = 1;
607
121k
    } else if (*spec_walk == '!') {
608
13.9k
      check_null = 1;
609
107k
    } else {
610
107k
      break;
611
107k
    }
612
13.9k
    spec_walk++;
613
13.9k
  }
614
615
107k
  switch (c) {
616
6.17k
    case 'l':
617
6.17k
      {
618
6.17k
        zend_long *p = va_arg(*va, zend_long *);
619
6.17k
        zend_bool *is_null = NULL;
620
621
6.17k
        if (check_null) {
622
0
          is_null = va_arg(*va, zend_bool *);
623
0
        }
624
625
6.17k
        if (!zend_parse_arg_long(arg, p, is_null, check_null)) {
626
54
          return check_null ? "?int" : "int";
627
54
        }
628
6.12k
      }
629
6.12k
      break;
630
631
0
    case 'd':
632
0
      {
633
0
        double *p = va_arg(*va, double *);
634
0
        zend_bool *is_null = NULL;
635
636
0
        if (check_null) {
637
0
          is_null = va_arg(*va, zend_bool *);
638
0
        }
639
640
0
        if (!zend_parse_arg_double(arg, p, is_null, check_null)) {
641
0
          return check_null ? "?float" : "float";
642
0
        }
643
0
      }
644
0
      break;
645
646
0
    case 'n':
647
0
      {
648
0
        zval **p = va_arg(*va, zval **);
649
650
0
        if (!zend_parse_arg_number(arg, p, check_null)) {
651
0
          return check_null ? "int|float|null" : "int|float";
652
0
        }
653
0
      }
654
0
      break;
655
656
3.11k
    case 's':
657
3.11k
      {
658
3.11k
        char **p = va_arg(*va, char **);
659
3.11k
        size_t *pl = va_arg(*va, size_t *);
660
3.11k
        if (!zend_parse_arg_string(arg, p, pl, check_null)) {
661
0
          return check_null ? "?string" : "string";
662
0
        }
663
3.11k
      }
664
3.11k
      break;
665
666
0
    case 'p':
667
0
      {
668
0
        char **p = va_arg(*va, char **);
669
0
        size_t *pl = va_arg(*va, size_t *);
670
0
        if (!zend_parse_arg_path(arg, p, pl, check_null)) {
671
0
          zend_spprintf(error, 0, "a valid path%s, %s given",
672
0
            check_null ? " or null" : "", zend_zval_type_name(arg)
673
0
          );
674
0
          return "";
675
0
        }
676
0
      }
677
0
      break;
678
679
0
    case 'P':
680
0
      {
681
0
        zend_string **str = va_arg(*va, zend_string **);
682
0
        if (!zend_parse_arg_path_str(arg, str, check_null)) {
683
0
          zend_spprintf(error, 0, "a valid path%s, %s given",
684
0
            check_null ? " or null" : "", zend_zval_type_name(arg)
685
0
          );
686
0
          return "";
687
0
        }
688
0
      }
689
0
      break;
690
691
27.7k
    case 'S':
692
27.7k
      {
693
27.7k
        zend_string **str = va_arg(*va, zend_string **);
694
27.7k
        if (!zend_parse_arg_str(arg, str, check_null)) {
695
90
          return check_null ? "?string" : "string";
696
90
        }
697
27.6k
      }
698
27.6k
      break;
699
700
722
    case 'b':
701
722
      {
702
722
        zend_bool *p = va_arg(*va, zend_bool *);
703
722
        zend_bool *is_null = NULL;
704
705
722
        if (check_null) {
706
0
          is_null = va_arg(*va, zend_bool *);
707
0
        }
708
709
722
        if (!zend_parse_arg_bool(arg, p, is_null, check_null)) {
710
0
          return check_null ? "?bool" : "bool";
711
0
        }
712
722
      }
713
722
      break;
714
715
2
    case 'r':
716
2
      {
717
2
        zval **p = va_arg(*va, zval **);
718
719
2
        if (!zend_parse_arg_resource(arg, p, check_null)) {
720
2
          return check_null ? "resource or null" : "resource";
721
2
        }
722
0
      }
723
0
      break;
724
725
0
    case 'A':
726
0
    case 'a':
727
0
      {
728
0
        zval **p = va_arg(*va, zval **);
729
730
0
        if (!zend_parse_arg_array(arg, p, check_null, c == 'A')) {
731
0
          return check_null ? "?array" : "array";
732
0
        }
733
0
      }
734
0
      break;
735
736
0
    case 'H':
737
289
    case 'h':
738
289
      {
739
289
        HashTable **p = va_arg(*va, HashTable **);
740
741
289
        if (!zend_parse_arg_array_ht(arg, p, check_null, c == 'H', separate)) {
742
0
          return check_null ? "?array" : "array";
743
0
        }
744
289
      }
745
289
      break;
746
747
45.5k
    case 'o':
748
45.5k
      {
749
45.5k
        zval **p = va_arg(*va, zval **);
750
751
45.5k
        if (!zend_parse_arg_object(arg, p, NULL, check_null)) {
752
130
          return check_null ? "?object" : "object";
753
138
        }
754
45.3k
      }
755
45.3k
      break;
756
757
1.14k
    case 'O':
758
1.14k
      {
759
1.14k
        zval **p = va_arg(*va, zval **);
760
1.14k
        zend_class_entry *ce = va_arg(*va, zend_class_entry *);
761
762
1.14k
        if (!zend_parse_arg_object(arg, p, ce, check_null)) {
763
43
          if (ce) {
764
43
            if (check_null) {
765
5
              zend_spprintf(error, 0, "of type ?%s, %s given", ZSTR_VAL(ce->name), zend_zval_type_name(arg));
766
5
              return "";
767
38
            } else {
768
38
              return ZSTR_VAL(ce->name);
769
38
            }
770
0
          } else {
771
0
            return check_null ? "?object" : "object";
772
0
          }
773
1.09k
        }
774
1.09k
      }
775
1.09k
      break;
776
777
1.68k
    case 'C':
778
1.68k
      {
779
1.68k
        zend_class_entry *lookup, **pce = va_arg(*va, zend_class_entry **);
780
1.68k
        zend_class_entry *ce_base = *pce;
781
782
1.68k
        if (check_null && Z_TYPE_P(arg) == IS_NULL) {
783
0
          *pce = NULL;
784
0
          break;
785
0
        }
786
1.68k
        if (!try_convert_to_string(arg)) {
787
52
          *pce = NULL;
788
52
          return ""; /* try_convert_to_string() throws an exception */
789
52
        }
790
791
1.63k
        if ((lookup = zend_lookup_class(Z_STR_P(arg))) == NULL) {
792
68
          *pce = NULL;
793
1.56k
        } else {
794
1.56k
          *pce = lookup;
795
1.56k
        }
796
1.63k
        if (ce_base) {
797
34
          if ((!*pce || !instanceof_function(*pce, ce_base))) {
798
34
            zend_spprintf(error, 0, "a class name derived from %s%s, %s given",
799
34
              ZSTR_VAL(ce_base->name), check_null ? " or null" : "", Z_STRVAL_P(arg));
800
34
            *pce = NULL;
801
34
            return "";
802
34
          }
803
1.59k
        }
804
1.59k
        if (!*pce) {
805
34
          zend_spprintf(error, 0, "a valid class name%s, %s given",
806
34
            check_null ? " or null" : "", Z_STRVAL_P(arg));
807
34
          return "";
808
34
        }
809
1.56k
        break;
810
811
1.56k
      }
812
0
      break;
813
814
4.14k
    case 'f':
815
4.14k
      {
816
4.14k
        zend_fcall_info *fci = va_arg(*va, zend_fcall_info *);
817
4.14k
        zend_fcall_info_cache *fcc = va_arg(*va, zend_fcall_info_cache *);
818
4.14k
        char *is_callable_error = NULL;
819
820
4.14k
        if (check_null && Z_TYPE_P(arg) == IS_NULL) {
821
101
          fci->size = 0;
822
101
          fcc->function_handler = 0;
823
101
          break;
824
101
        }
825
826
4.04k
        if (zend_fcall_info_init(arg, 0, fci, fcc, NULL, &is_callable_error) == SUCCESS) {
827
3.94k
          ZEND_ASSERT(!is_callable_error);
828
3.94k
          break;
829
3.94k
        }
830
831
102
        if (is_callable_error) {
832
102
          zend_spprintf(error, 0, "a valid callback%s, %s", check_null ? " or null" : "", is_callable_error);
833
102
          efree(is_callable_error);
834
102
          return "";
835
0
        } else {
836
0
          return check_null ? "a valid callback or null" : "a valid callback";
837
0
        }
838
0
      }
839
840
17.3k
    case 'z':
841
17.3k
      {
842
17.3k
        zval **p = va_arg(*va, zval **);
843
844
17.3k
        zend_parse_arg_zval_deref(real_arg, p, check_null);
845
17.3k
      }
846
17.3k
      break;
847
848
0
    case 'Z': /* replace with 'z' */
849
0
    case 'L': /* replace with 'l' */
850
0
      ZEND_ASSERT(0 && "ZPP modifier no longer supported");
851
0
    default:
852
0
      return "unknown";
853
107k
  }
854
855
107k
  *spec = spec_walk;
856
857
107k
  return NULL;
858
107k
}
859
/* }}} */
860
861
static zend_result zend_parse_arg(uint32_t arg_num, zval *arg, va_list *va, const char **spec, int flags) /* {{{ */
862
107k
{
863
107k
  const char *expected_type = NULL;
864
107k
  char *error = NULL;
865
866
107k
  expected_type = zend_parse_arg_impl(arg, va, spec, &error);
867
107k
  if (expected_type) {
868
549
    if (EG(exception)) {
869
52
      return FAILURE;
870
52
    }
871
497
    if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) {
872
497
      if (error) {
873
175
        zend_argument_type_error(arg_num, "must be %s", error);
874
175
        efree(error);
875
322
      } else {
876
322
        zend_argument_type_error(arg_num, "must be of type %s, %s given", expected_type, zend_zval_type_name(arg));
877
322
      }
878
0
    } else if (error) {
879
0
      efree(error);
880
0
    }
881
882
497
    return FAILURE;
883
497
  }
884
885
107k
  return SUCCESS;
886
107k
}
887
/* }}} */
888
889
ZEND_API zend_result zend_parse_parameter(int flags, uint32_t arg_num, zval *arg, const char *spec, ...)
890
0
{
891
0
  va_list va;
892
0
  zend_result ret;
893
894
0
  va_start(va, spec);
895
0
  ret = zend_parse_arg(arg_num, arg, &va, &spec, flags);
896
0
  va_end(va);
897
898
0
  return ret;
899
0
}
900
901
0
static ZEND_COLD void zend_parse_parameters_debug_error(const char *msg) {
902
0
  zend_function *active_function = EG(current_execute_data)->func;
903
0
  const char *class_name = active_function->common.scope
904
0
    ? ZSTR_VAL(active_function->common.scope->name) : "";
905
0
  zend_error_noreturn(E_CORE_ERROR, "%s%s%s(): %s",
906
0
    class_name, class_name[0] ? "::" : "",
907
0
    ZSTR_VAL(active_function->common.function_name), msg);
908
0
}
909
910
static zend_result zend_parse_va_args(uint32_t num_args, const char *type_spec, va_list *va, int flags) /* {{{ */
911
123k
{
912
123k
  const  char *spec_walk;
913
123k
  char c;
914
123k
  uint32_t i;
915
123k
  uint32_t min_num_args = 0;
916
123k
  uint32_t max_num_args = 0;
917
123k
  uint32_t post_varargs = 0;
918
123k
  zval *arg;
919
123k
  zend_bool have_varargs = 0;
920
123k
  zend_bool have_optional_args = 0;
921
123k
  zval **varargs = NULL;
922
123k
  int *n_varargs = NULL;
923
924
492k
  for (spec_walk = type_spec; *spec_walk; spec_walk++) {
925
368k
    c = *spec_walk;
926
368k
    switch (c) {
927
81.6k
      case 'l': case 'd':
928
87.4k
      case 's': case 'b':
929
87.5k
      case 'r': case 'a':
930
163k
      case 'o': case 'O':
931
195k
      case 'z': case 'Z':
932
197k
      case 'C': case 'h':
933
201k
      case 'f': case 'A':
934
201k
      case 'H': case 'p':
935
247k
      case 'S': case 'P':
936
247k
      case 'L': case 'n':
937
247k
        max_num_args++;
938
247k
        break;
939
940
73.7k
      case '|':
941
73.7k
        min_num_args = max_num_args;
942
73.7k
        have_optional_args = 1;
943
73.7k
        break;
944
945
0
      case '/':
946
48.0k
      case '!':
947
        /* Pass */
948
48.0k
        break;
949
950
0
      case '*':
951
28
      case '+':
952
28
        if (have_varargs) {
953
0
          zend_parse_parameters_debug_error(
954
0
            "only one varargs specifier (* or +) is permitted");
955
0
          return FAILURE;
956
0
        }
957
28
        have_varargs = 1;
958
        /* we expect at least one parameter in varargs */
959
28
        if (c == '+') {
960
28
          max_num_args++;
961
28
        }
962
        /* mark the beginning of varargs */
963
28
        post_varargs = max_num_args;
964
965
28
        if (ZEND_CALL_INFO(EG(current_execute_data)) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
966
28
          zend_unexpected_extra_named_error();
967
28
          return FAILURE;
968
28
        }
969
0
        break;
970
971
0
      default:
972
0
        zend_parse_parameters_debug_error("bad type specifier while parsing parameters");
973
0
        return FAILURE;
974
368k
    }
975
368k
  }
976
977
  /* with no optional arguments the minimum number of arguments must be the same as the maximum */
978
123k
  if (!have_optional_args) {
979
50.0k
    min_num_args = max_num_args;
980
50.0k
  }
981
982
123k
  if (have_varargs) {
983
    /* calculate how many required args are at the end of the specifier list */
984
0
    post_varargs = max_num_args - post_varargs;
985
0
    max_num_args = UINT32_MAX;
986
0
  }
987
988
123k
  if (num_args < min_num_args || num_args > max_num_args) {
989
1.21k
    if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
990
1.19k
      zend_function *active_function = EG(current_execute_data)->func;
991
1.06k
      const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
992
1.19k
      zend_argument_count_error("%s%s%s() expects %s %d parameter%s, %d given",
993
1.19k
          class_name,
994
1.06k
          class_name[0] ? "::" : "",
995
1.19k
          ZSTR_VAL(active_function->common.function_name),
996
784
          min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
997
1.15k
          num_args < min_num_args ? min_num_args : max_num_args,
998
1.15k
          (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
999
1.19k
          num_args);
1000
1.19k
    }
1001
1.21k
    return FAILURE;
1002
1.21k
  }
1003
1004
122k
  if (num_args > ZEND_CALL_NUM_ARGS(EG(current_execute_data))) {
1005
0
    zend_parse_parameters_debug_error("could not obtain parameters for parsing");
1006
0
    return FAILURE;
1007
0
  }
1008
1009
122k
  i = 0;
1010
229k
  while (num_args-- > 0) {
1011
107k
    if (*type_spec == '|') {
1012
32.6k
      type_spec++;
1013
32.6k
    }
1014
1015
107k
    if (*type_spec == '*' || *type_spec == '+') {
1016
0
      int num_varargs = num_args + 1 - post_varargs;
1017
1018
      /* eat up the passed in storage even if it won't be filled in with varargs */
1019
0
      varargs = va_arg(*va, zval **);
1020
0
      n_varargs = va_arg(*va, int *);
1021
0
      type_spec++;
1022
1023
0
      if (num_varargs > 0) {
1024
0
        *n_varargs = num_varargs;
1025
0
        *varargs = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
1026
        /* adjust how many args we have left and restart loop */
1027
0
        num_args += 1 - num_varargs;
1028
0
        i += num_varargs;
1029
0
        continue;
1030
0
      } else {
1031
0
        *varargs = NULL;
1032
0
        *n_varargs = 0;
1033
0
      }
1034
0
    }
1035
1036
107k
    arg = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
1037
1038
107k
    if (zend_parse_arg(i+1, arg, va, &type_spec, flags) == FAILURE) {
1039
      /* clean up varargs array if it was used */
1040
549
      if (varargs && *varargs) {
1041
0
        *varargs = NULL;
1042
0
      }
1043
549
      return FAILURE;
1044
549
    }
1045
107k
    i++;
1046
107k
  }
1047
1048
121k
  return SUCCESS;
1049
122k
}
1050
/* }}} */
1051
1052
ZEND_API zend_result zend_parse_parameters_ex(int flags, uint32_t num_args, const char *type_spec, ...) /* {{{ */
1053
1.01k
{
1054
1.01k
  va_list va;
1055
1.01k
  zend_result retval;
1056
1057
1.01k
  va_start(va, type_spec);
1058
1.01k
  retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1059
1.01k
  va_end(va);
1060
1061
1.01k
  return retval;
1062
1.01k
}
1063
/* }}} */
1064
1065
ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec, ...) /* {{{ */
1066
116k
{
1067
116k
  va_list va;
1068
116k
  zend_result retval;
1069
116k
  int flags = 0;
1070
1071
116k
  va_start(va, type_spec);
1072
116k
  retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1073
116k
  va_end(va);
1074
1075
116k
  return retval;
1076
116k
}
1077
/* }}} */
1078
1079
ZEND_API zend_result zend_parse_method_parameters(uint32_t num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
1080
6.74k
{
1081
6.74k
  va_list va;
1082
6.74k
  zend_result retval;
1083
6.74k
  int flags = 0;
1084
6.74k
  const char *p = type_spec;
1085
6.74k
  zval **object;
1086
6.74k
  zend_class_entry *ce;
1087
1088
  /* Just checking this_ptr is not enough, because fcall_common_helper does not set
1089
   * Z_OBJ(EG(This)) to NULL when calling an internal function with common.scope == NULL.
1090
   * In that case EG(This) would still be the $this from the calling code and we'd take the
1091
   * wrong branch here. */
1092
6.74k
  zend_bool is_method = EG(current_execute_data)->func->common.scope != NULL;
1093
1094
6.74k
  if (!is_method || !this_ptr || Z_TYPE_P(this_ptr) != IS_OBJECT) {
1095
142
    va_start(va, type_spec);
1096
142
    retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1097
142
    va_end(va);
1098
6.60k
  } else {
1099
6.60k
    p++;
1100
1101
6.60k
    va_start(va, type_spec);
1102
1103
6.60k
    object = va_arg(va, zval **);
1104
6.60k
    ce = va_arg(va, zend_class_entry *);
1105
6.60k
    *object = this_ptr;
1106
1107
6.60k
    if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
1108
0
      zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s()",
1109
0
        ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name(), ZSTR_VAL(ce->name), get_active_function_name());
1110
0
    }
1111
1112
6.60k
    retval = zend_parse_va_args(num_args, p, &va, flags);
1113
6.60k
    va_end(va);
1114
6.60k
  }
1115
6.74k
  return retval;
1116
6.74k
}
1117
/* }}} */
1118
1119
ZEND_API zend_result zend_parse_method_parameters_ex(int flags, uint32_t num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
1120
0
{
1121
0
  va_list va;
1122
0
  zend_result retval;
1123
0
  const char *p = type_spec;
1124
0
  zval **object;
1125
0
  zend_class_entry *ce;
1126
1127
0
  if (!this_ptr) {
1128
0
    va_start(va, type_spec);
1129
0
    retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1130
0
    va_end(va);
1131
0
  } else {
1132
0
    p++;
1133
0
    va_start(va, type_spec);
1134
1135
0
    object = va_arg(va, zval **);
1136
0
    ce = va_arg(va, zend_class_entry *);
1137
0
    *object = this_ptr;
1138
1139
0
    if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
1140
0
      if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
1141
0
        zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s()",
1142
0
          ZSTR_VAL(ce->name), get_active_function_name(), ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name());
1143
0
      }
1144
0
      va_end(va);
1145
0
      return FAILURE;
1146
0
    }
1147
1148
0
    retval = zend_parse_va_args(num_args, p, &va, flags);
1149
0
    va_end(va);
1150
0
  }
1151
0
  return retval;
1152
0
}
1153
/* }}} */
1154
1155
/* This function should be called after the constructor has been called
1156
 * because it may call __set from the uninitialized object otherwise. */
1157
ZEND_API void zend_merge_properties(zval *obj, HashTable *properties) /* {{{ */
1158
0
{
1159
0
  zend_object *zobj = Z_OBJ_P(obj);
1160
0
  zend_object_write_property_t write_property = zobj->handlers->write_property;
1161
0
  zend_class_entry *old_scope = EG(fake_scope);
1162
0
  zend_string *key;
1163
0
  zval *value;
1164
1165
0
  EG(fake_scope) = Z_OBJCE_P(obj);
1166
0
  ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, value) {
1167
0
    if (key) {
1168
0
      write_property(zobj, key, value, NULL);
1169
0
    }
1170
0
  } ZEND_HASH_FOREACH_END();
1171
0
  EG(fake_scope) = old_scope;
1172
0
}
1173
/* }}} */
1174
1175
ZEND_API zend_result zend_update_class_constants(zend_class_entry *class_type) /* {{{ */
1176
2.19k
{
1177
2.19k
  if (!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) {
1178
2.15k
    zend_class_constant *c;
1179
2.15k
    zval *val;
1180
2.15k
    zend_property_info *prop_info;
1181
1182
2.15k
    if (class_type->parent) {
1183
138
      if (UNEXPECTED(zend_update_class_constants(class_type->parent) != SUCCESS)) {
1184
33
        return FAILURE;
1185
33
      }
1186
2.11k
    }
1187
1188
4.60k
    ZEND_HASH_FOREACH_PTR(&class_type->constants_table, c) {
1189
4.60k
      val = &c->value;
1190
1.24k
      if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
1191
247
        if (UNEXPECTED(zval_update_constant_ex(val, c->ce) != SUCCESS)) {
1192
92
          return FAILURE;
1193
92
        }
1194
247
      }
1195
1.24k
    } ZEND_HASH_FOREACH_END();
1196
1197
2.02k
    if (class_type->default_static_members_count && !CE_STATIC_MEMBERS(class_type)) {
1198
0
      if (class_type->type == ZEND_INTERNAL_CLASS || (class_type->ce_flags & (ZEND_ACC_IMMUTABLE|ZEND_ACC_PRELOADED))) {
1199
0
        zend_class_init_statics(class_type);
1200
0
      }
1201
0
    }
1202
1203
9.20k
    ZEND_HASH_FOREACH_PTR(&class_type->properties_info, prop_info) {
1204
3.58k
      if (prop_info->flags & ZEND_ACC_STATIC) {
1205
1.95k
        val = CE_STATIC_MEMBERS(class_type) + prop_info->offset;
1206
1.63k
      } else {
1207
1.63k
        val = (zval*)((char*)class_type->default_properties_table + prop_info->offset - OBJ_PROP_TO_OFFSET(0));
1208
1.63k
      }
1209
3.58k
      if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
1210
3.32k
        if (ZEND_TYPE_IS_SET(prop_info->type)) {
1211
757
          zval tmp;
1212
1213
757
          ZVAL_COPY(&tmp, val);
1214
757
          if (UNEXPECTED(zval_update_constant_ex(&tmp, prop_info->ce) != SUCCESS)) {
1215
72
            zval_ptr_dtor(&tmp);
1216
72
            return FAILURE;
1217
72
          }
1218
685
          /* property initializers must always be evaluated with strict types */;
1219
685
          if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, /* strict */ 1))) {
1220
530
            zval_ptr_dtor(&tmp);
1221
530
            return FAILURE;
1222
530
          }
1223
155
          zval_ptr_dtor(val);
1224
155
          ZVAL_COPY_VALUE(val, &tmp);
1225
2.56k
        } else if (UNEXPECTED(zval_update_constant_ex(val, prop_info->ce) != SUCCESS)) {
1226
228
          return FAILURE;
1227
228
        }
1228
3.32k
      }
1229
3.58k
    } ZEND_HASH_FOREACH_END();
1230
1231
1.19k
    class_type->ce_flags |= ZEND_ACC_CONSTANTS_UPDATED;
1232
1.19k
  }
1233
1234
1.24k
  return SUCCESS;
1235
2.19k
}
1236
/* }}} */
1237
1238
static zend_always_inline void _object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1239
1.66M
{
1240
1.66M
  if (class_type->default_properties_count) {
1241
1.06M
    zval *src = class_type->default_properties_table;
1242
1.06M
    zval *dst = object->properties_table;
1243
1.06M
    zval *end = src + class_type->default_properties_count;
1244
1245
1.06M
    if (UNEXPECTED(class_type->type == ZEND_INTERNAL_CLASS)) {
1246
6.55M
      do {
1247
6.55M
        ZVAL_COPY_OR_DUP_PROP(dst, src);
1248
6.55M
        src++;
1249
6.55M
        dst++;
1250
6.55M
      } while (src != end);
1251
114k
    } else {
1252
196k
      do {
1253
196k
        ZVAL_COPY_PROP(dst, src);
1254
196k
        src++;
1255
196k
        dst++;
1256
196k
      } while (src != end);
1257
114k
    }
1258
1.06M
  }
1259
1.66M
}
1260
/* }}} */
1261
1262
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1263
1.32M
{
1264
1.32M
  object->properties = NULL;
1265
1.32M
  _object_properties_init(object, class_type);
1266
1.32M
}
1267
/* }}} */
1268
1269
ZEND_API void object_properties_init_ex(zend_object *object, HashTable *properties) /* {{{ */
1270
0
{
1271
0
  object->properties = properties;
1272
0
  if (object->ce->default_properties_count) {
1273
0
    zval *prop;
1274
0
    zend_string *key;
1275
0
    zend_property_info *property_info;
1276
1277
0
    ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, prop) {
1278
0
      property_info = zend_get_property_info(object->ce, key, 1);
1279
0
      if (property_info != ZEND_WRONG_PROPERTY_INFO &&
1280
0
          property_info &&
1281
0
          (property_info->flags & ZEND_ACC_STATIC) == 0) {
1282
0
        zval *slot = OBJ_PROP(object, property_info->offset);
1283
1284
0
        if (UNEXPECTED(ZEND_TYPE_IS_SET(property_info->type))) {
1285
0
          zval tmp;
1286
1287
0
          ZVAL_COPY_VALUE(&tmp, prop);
1288
0
          if (UNEXPECTED(!zend_verify_property_type(property_info, &tmp, 0))) {
1289
0
            continue;
1290
0
          }
1291
0
          ZVAL_COPY_VALUE(slot, &tmp);
1292
0
        } else {
1293
0
          ZVAL_COPY_VALUE(slot, prop);
1294
0
        }
1295
0
        ZVAL_INDIRECT(prop, slot);
1296
0
      }
1297
0
    } ZEND_HASH_FOREACH_END();
1298
0
  }
1299
0
}
1300
/* }}} */
1301
1302
ZEND_API void object_properties_load(zend_object *object, HashTable *properties) /* {{{ */
1303
0
{
1304
0
    zval *prop, tmp;
1305
0
    zend_string *key;
1306
0
    zend_long h;
1307
0
    zend_property_info *property_info;
1308
1309
0
    ZEND_HASH_FOREACH_KEY_VAL(properties, h, key, prop) {
1310
0
    if (key) {
1311
0
      if (ZSTR_VAL(key)[0] == '\0') {
1312
0
        const char *class_name, *prop_name;
1313
0
        size_t prop_name_len;
1314
0
        if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) {
1315
0
          zend_string *pname = zend_string_init(prop_name, prop_name_len, 0);
1316
0
          zend_class_entry *prev_scope = EG(fake_scope);
1317
0
          if (class_name && class_name[0] != '*') {
1318
0
            zend_string *cname = zend_string_init(class_name, strlen(class_name), 0);
1319
0
            EG(fake_scope) = zend_lookup_class(cname);
1320
0
            zend_string_release_ex(cname, 0);
1321
0
          }
1322
0
          property_info = zend_get_property_info(object->ce, pname, 1);
1323
0
          zend_string_release_ex(pname, 0);
1324
0
          EG(fake_scope) = prev_scope;
1325
0
        } else {
1326
0
          property_info = ZEND_WRONG_PROPERTY_INFO;
1327
0
        }
1328
0
      } else {
1329
0
        property_info = zend_get_property_info(object->ce, key, 1);
1330
0
      }
1331
0
      if (property_info != ZEND_WRONG_PROPERTY_INFO &&
1332
0
        property_info &&
1333
0
        (property_info->flags & ZEND_ACC_STATIC) == 0) {
1334
0
        zval *slot = OBJ_PROP(object, property_info->offset);
1335
0
        zval_ptr_dtor(slot);
1336
0
        ZVAL_COPY_VALUE(slot, prop);
1337
0
        zval_add_ref(slot);
1338
0
        if (object->properties) {
1339
0
          ZVAL_INDIRECT(&tmp, slot);
1340
0
          zend_hash_update(object->properties, key, &tmp);
1341
0
        }
1342
0
      } else {
1343
0
        if (!object->properties) {
1344
0
          rebuild_object_properties(object);
1345
0
        }
1346
0
        prop = zend_hash_update(object->properties, key, prop);
1347
0
        zval_add_ref(prop);
1348
0
      }
1349
0
    } else {
1350
0
      if (!object->properties) {
1351
0
        rebuild_object_properties(object);
1352
0
      }
1353
0
      prop = zend_hash_index_update(object->properties, h, prop);
1354
0
      zval_add_ref(prop);
1355
0
    }
1356
0
  } ZEND_HASH_FOREACH_END();
1357
0
}
1358
/* }}} */
1359
1360
/* This function requires 'properties' to contain all props declared in the
1361
 * class and all props being public. If only a subset is given or the class
1362
 * has protected members then you need to merge the properties separately by
1363
 * calling zend_merge_properties(). */
1364
static zend_always_inline zend_result _object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
1365
1.74M
{
1366
1.74M
  if (UNEXPECTED(class_type->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) {
1367
89
    if (class_type->ce_flags & ZEND_ACC_INTERFACE) {
1368
20
      zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(class_type->name));
1369
69
    } else if (class_type->ce_flags & ZEND_ACC_TRAIT) {
1370
36
      zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(class_type->name));
1371
33
    } else {
1372
33
      zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name));
1373
33
    }
1374
89
    ZVAL_NULL(arg);
1375
89
    Z_OBJ_P(arg) = NULL;
1376
89
    return FAILURE;
1377
89
  }
1378
1379
1.74M
  if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
1380
1.31k
    if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
1381
761
      ZVAL_NULL(arg);
1382
761
      Z_OBJ_P(arg) = NULL;
1383
761
      return FAILURE;
1384
761
    }
1385
1.74M
  }
1386
1387
1.74M
  if (class_type->create_object == NULL) {
1388
346k
    zend_object *obj = zend_objects_new(class_type);
1389
1390
346k
    ZVAL_OBJ(arg, obj);
1391
346k
    if (properties) {
1392
0
      object_properties_init_ex(obj, properties);
1393
346k
    } else {
1394
346k
      _object_properties_init(obj, class_type);
1395
346k
    }
1396
1.40M
  } else {
1397
1.40M
    ZVAL_OBJ(arg, class_type->create_object(class_type));
1398
1.40M
  }
1399
1.74M
  return SUCCESS;
1400
1.74M
}
1401
/* }}} */
1402
1403
ZEND_API zend_result object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
1404
0
{
1405
0
  return _object_and_properties_init(arg, class_type, properties);
1406
0
}
1407
/* }}} */
1408
1409
ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *class_type) /* {{{ */
1410
1.74M
{
1411
1.74M
  return _object_and_properties_init(arg, class_type, NULL);
1412
1.74M
}
1413
/* }}} */
1414
1415
ZEND_API void object_init(zval *arg) /* {{{ */
1416
77
{
1417
77
  ZVAL_OBJ(arg, zend_objects_new(zend_standard_class_def));
1418
77
}
1419
/* }}} */
1420
1421
ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
1422
14.6k
{
1423
14.6k
  zval tmp;
1424
1425
14.6k
  ZVAL_LONG(&tmp, n);
1426
14.6k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1427
14.6k
}
1428
/* }}} */
1429
1430
ZEND_API void add_assoc_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
1431
0
{
1432
0
  zval tmp;
1433
1434
0
  ZVAL_NULL(&tmp);
1435
0
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1436
0
}
1437
/* }}} */
1438
1439
ZEND_API void add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, bool b) /* {{{ */
1440
0
{
1441
0
  zval tmp;
1442
1443
0
  ZVAL_BOOL(&tmp, b);
1444
0
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1445
0
}
1446
/* }}} */
1447
1448
ZEND_API void add_assoc_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
1449
0
{
1450
0
  zval tmp;
1451
1452
0
  ZVAL_RES(&tmp, r);
1453
0
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1454
0
}
1455
/* }}} */
1456
1457
ZEND_API void add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
1458
0
{
1459
0
  zval tmp;
1460
1461
0
  ZVAL_DOUBLE(&tmp, d);
1462
0
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1463
0
}
1464
/* }}} */
1465
1466
ZEND_API void add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
1467
7.19k
{
1468
7.19k
  zval tmp;
1469
1470
7.19k
  ZVAL_STR(&tmp, str);
1471
7.19k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1472
7.19k
}
1473
/* }}} */
1474
1475
ZEND_API void add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
1476
7.19k
{
1477
7.19k
  zval tmp;
1478
1479
7.19k
  ZVAL_STRING(&tmp, str);
1480
7.19k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1481
7.19k
}
1482
/* }}} */
1483
1484
ZEND_API void add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
1485
0
{
1486
0
  zval tmp;
1487
1488
0
  ZVAL_STRINGL(&tmp, str, length);
1489
0
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1490
0
}
1491
/* }}} */
1492
1493
ZEND_API void add_assoc_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
1494
5.92k
{
1495
5.92k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value);
1496
5.92k
}
1497
/* }}} */
1498
1499
ZEND_API void add_index_long(zval *arg, zend_ulong index, zend_long n) /* {{{ */
1500
0
{
1501
0
  zval tmp;
1502
1503
0
  ZVAL_LONG(&tmp, n);
1504
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1505
0
}
1506
/* }}} */
1507
1508
ZEND_API void add_index_null(zval *arg, zend_ulong index) /* {{{ */
1509
0
{
1510
0
  zval tmp;
1511
1512
0
  ZVAL_NULL(&tmp);
1513
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1514
0
}
1515
/* }}} */
1516
1517
ZEND_API void add_index_bool(zval *arg, zend_ulong index, bool b) /* {{{ */
1518
0
{
1519
0
  zval tmp;
1520
1521
0
  ZVAL_BOOL(&tmp, b);
1522
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1523
0
}
1524
/* }}} */
1525
1526
ZEND_API void add_index_resource(zval *arg, zend_ulong index, zend_resource *r) /* {{{ */
1527
0
{
1528
0
  zval tmp;
1529
1530
0
  ZVAL_RES(&tmp, r);
1531
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1532
0
}
1533
/* }}} */
1534
1535
ZEND_API void add_index_double(zval *arg, zend_ulong index, double d) /* {{{ */
1536
0
{
1537
0
  zval tmp;
1538
1539
0
  ZVAL_DOUBLE(&tmp, d);
1540
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1541
0
}
1542
/* }}} */
1543
1544
ZEND_API void add_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */
1545
0
{
1546
0
  zval tmp;
1547
1548
0
  ZVAL_STR(&tmp, str);
1549
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1550
0
}
1551
/* }}} */
1552
1553
ZEND_API void add_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */
1554
0
{
1555
0
  zval tmp;
1556
1557
0
  ZVAL_STRING(&tmp, str);
1558
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1559
0
}
1560
/* }}} */
1561
1562
ZEND_API void add_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */
1563
0
{
1564
0
  zval tmp;
1565
1566
0
  ZVAL_STRINGL(&tmp, str, length);
1567
0
  zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1568
0
}
1569
/* }}} */
1570
1571
ZEND_API zend_result add_next_index_long(zval *arg, zend_long n) /* {{{ */
1572
0
{
1573
0
  zval tmp;
1574
1575
0
  ZVAL_LONG(&tmp, n);
1576
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1577
0
}
1578
/* }}} */
1579
1580
ZEND_API zend_result add_next_index_null(zval *arg) /* {{{ */
1581
0
{
1582
0
  zval tmp;
1583
1584
0
  ZVAL_NULL(&tmp);
1585
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1586
0
}
1587
/* }}} */
1588
1589
ZEND_API zend_result add_next_index_bool(zval *arg, zend_bool b) /* {{{ */
1590
0
{
1591
0
  zval tmp;
1592
1593
0
  ZVAL_BOOL(&tmp, b);
1594
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1595
0
}
1596
/* }}} */
1597
1598
ZEND_API zend_result add_next_index_resource(zval *arg, zend_resource *r) /* {{{ */
1599
0
{
1600
0
  zval tmp;
1601
1602
0
  ZVAL_RES(&tmp, r);
1603
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1604
0
}
1605
/* }}} */
1606
1607
ZEND_API zend_result add_next_index_double(zval *arg, double d) /* {{{ */
1608
0
{
1609
0
  zval tmp;
1610
1611
0
  ZVAL_DOUBLE(&tmp, d);
1612
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1613
0
}
1614
/* }}} */
1615
1616
ZEND_API zend_result add_next_index_str(zval *arg, zend_string *str) /* {{{ */
1617
751k
{
1618
751k
  zval tmp;
1619
1620
751k
  ZVAL_STR(&tmp, str);
1621
751k
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1622
751k
}
1623
/* }}} */
1624
1625
ZEND_API zend_result add_next_index_string(zval *arg, const char *str) /* {{{ */
1626
870
{
1627
870
  zval tmp;
1628
1629
870
  ZVAL_STRING(&tmp, str);
1630
870
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1631
870
}
1632
/* }}} */
1633
1634
ZEND_API zend_result add_next_index_stringl(zval *arg, const char *str, size_t length) /* {{{ */
1635
0
{
1636
0
  zval tmp;
1637
1638
0
  ZVAL_STRINGL(&tmp, str, length);
1639
0
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1640
0
}
1641
/* }}} */
1642
1643
ZEND_API zend_result array_set_zval_key(HashTable *ht, zval *key, zval *value) /* {{{ */
1644
3.94k
{
1645
3.94k
  zval *result;
1646
1647
3.94k
  switch (Z_TYPE_P(key)) {
1648
3.94k
    case IS_STRING:
1649
3.94k
      result = zend_symtable_update(ht, Z_STR_P(key), value);
1650
3.94k
      break;
1651
0
    case IS_NULL:
1652
0
      result = zend_hash_update(ht, ZSTR_EMPTY_ALLOC(), value);
1653
0
      break;
1654
0
    case IS_RESOURCE:
1655
0
      zend_error(E_WARNING, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(key), Z_RES_HANDLE_P(key));
1656
0
      result = zend_hash_index_update(ht, Z_RES_HANDLE_P(key), value);
1657
0
      break;
1658
0
    case IS_FALSE:
1659
0
      result = zend_hash_index_update(ht, 0, value);
1660
0
      break;
1661
0
    case IS_TRUE:
1662
0
      result = zend_hash_index_update(ht, 1, value);
1663
0
      break;
1664
1
    case IS_LONG:
1665
1
      result = zend_hash_index_update(ht, Z_LVAL_P(key), value);
1666
1
      break;
1667
0
    case IS_DOUBLE:
1668
0
      result = zend_hash_index_update(ht, zend_dval_to_lval(Z_DVAL_P(key)), value);
1669
0
      break;
1670
0
    default:
1671
0
      zend_type_error("Illegal offset type");
1672
0
      result = NULL;
1673
3.94k
  }
1674
1675
3.94k
  if (result) {
1676
3.94k
    Z_TRY_ADDREF_P(result);
1677
3.94k
    return SUCCESS;
1678
0
  } else {
1679
0
    return FAILURE;
1680
0
  }
1681
3.94k
}
1682
/* }}} */
1683
1684
ZEND_API void add_property_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
1685
0
{
1686
0
  zval tmp;
1687
1688
0
  ZVAL_LONG(&tmp, n);
1689
0
  add_property_zval_ex(arg, key, key_len, &tmp);
1690
0
}
1691
/* }}} */
1692
1693
ZEND_API void add_property_bool_ex(zval *arg, const char *key, size_t key_len, zend_long b) /* {{{ */
1694
0
{
1695
0
  zval tmp;
1696
1697
0
  ZVAL_BOOL(&tmp, b);
1698
0
  add_property_zval_ex(arg, key, key_len, &tmp);
1699
0
}
1700
/* }}} */
1701
1702
ZEND_API void add_property_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
1703
8.14k
{
1704
8.14k
  zval tmp;
1705
1706
8.14k
  ZVAL_NULL(&tmp);
1707
8.14k
  add_property_zval_ex(arg, key, key_len, &tmp);
1708
8.14k
}
1709
/* }}} */
1710
1711
ZEND_API void add_property_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
1712
0
{
1713
0
  zval tmp;
1714
1715
0
  ZVAL_RES(&tmp, r);
1716
0
  add_property_zval_ex(arg, key, key_len, &tmp);
1717
0
  zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1718
0
}
1719
/* }}} */
1720
1721
ZEND_API void add_property_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
1722
0
{
1723
0
  zval tmp;
1724
1725
0
  ZVAL_DOUBLE(&tmp, d);
1726
0
  add_property_zval_ex(arg, key, key_len, &tmp);
1727
0
}
1728
/* }}} */
1729
1730
ZEND_API void add_property_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
1731
0
{
1732
0
  zval tmp;
1733
1734
0
  ZVAL_STR(&tmp, str);
1735
0
  add_property_zval_ex(arg, key, key_len, &tmp);
1736
0
  zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1737
0
}
1738
/* }}} */
1739
1740
ZEND_API void add_property_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
1741
0
{
1742
0
  zval tmp;
1743
1744
0
  ZVAL_STRING(&tmp, str);
1745
0
  add_property_zval_ex(arg, key, key_len, &tmp);
1746
0
  zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1747
0
}
1748
/* }}} */
1749
1750
ZEND_API void add_property_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
1751
0
{
1752
0
  zval tmp;
1753
1754
0
  ZVAL_STRINGL(&tmp, str, length);
1755
0
  add_property_zval_ex(arg, key, key_len, &tmp);
1756
0
  zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
1757
0
}
1758
/* }}} */
1759
1760
ZEND_API void add_property_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
1761
8.14k
{
1762
8.14k
  zend_string *str;
1763
1764
8.14k
  str = zend_string_init(key, key_len, 0);
1765
8.14k
  Z_OBJ_HANDLER_P(arg, write_property)(Z_OBJ_P(arg), str, value, NULL);
1766
8.14k
  zend_string_release_ex(str, 0);
1767
8.14k
}
1768
/* }}} */
1769
1770
ZEND_API zend_result zend_startup_module_ex(zend_module_entry *module) /* {{{ */
1771
62.7k
{
1772
62.7k
  size_t name_len;
1773
62.7k
  zend_string *lcname;
1774
1775
62.7k
  if (module->module_started) {
1776
0
    return SUCCESS;
1777
0
  }
1778
62.7k
  module->module_started = 1;
1779
1780
  /* Check module dependencies */
1781
62.7k
  if (module->deps) {
1782
12.5k
    const zend_module_dep *dep = module->deps;
1783
1784
31.3k
    while (dep->name) {
1785
18.8k
      if (dep->type == MODULE_DEP_REQUIRED) {
1786
6.27k
        zend_module_entry *req_mod;
1787
1788
6.27k
        name_len = strlen(dep->name);
1789
6.27k
        lcname = zend_string_alloc(name_len, 0);
1790
6.27k
        zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
1791
1792
6.27k
        if ((req_mod = zend_hash_find_ptr(&module_registry, lcname)) == NULL || !req_mod->module_started) {
1793
0
          zend_string_efree(lcname);
1794
          /* TODO: Check version relationship */
1795
0
          zend_error(E_CORE_WARNING, "Cannot load module \"%s\" because required module \"%s\" is not loaded", module->name, dep->name);
1796
0
          module->module_started = 0;
1797
0
          return FAILURE;
1798
0
        }
1799
6.27k
        zend_string_efree(lcname);
1800
6.27k
      }
1801
18.8k
      ++dep;
1802
18.8k
    }
1803
12.5k
  }
1804
1805
  /* Initialize module globals */
1806
62.7k
  if (module->globals_size) {
1807
#ifdef ZTS
1808
    ts_allocate_id(module->globals_id_ptr, module->globals_size, (ts_allocate_ctor) module->globals_ctor, (ts_allocate_dtor) module->globals_dtor);
1809
#else
1810
43.9k
    if (module->globals_ctor) {
1811
37.6k
      module->globals_ctor(module->globals_ptr);
1812
37.6k
    }
1813
43.9k
#endif
1814
43.9k
  }
1815
62.7k
  if (module->module_startup_func) {
1816
62.7k
    EG(current_module) = module;
1817
62.7k
    if (module->module_startup_func(module->type, module->module_number)==FAILURE) {
1818
0
      zend_error_noreturn(E_CORE_ERROR,"Unable to start %s module", module->name);
1819
0
      EG(current_module) = NULL;
1820
0
      return FAILURE;
1821
62.7k
    }
1822
62.7k
    EG(current_module) = NULL;
1823
62.7k
  }
1824
62.7k
  return SUCCESS;
1825
62.7k
}
1826
/* }}} */
1827
1828
static int zend_startup_module_zval(zval *zv) /* {{{ */
1829
62.7k
{
1830
62.7k
  zend_module_entry *module = Z_PTR_P(zv);
1831
1832
62.7k
  return (zend_startup_module_ex(module) == SUCCESS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
1833
62.7k
}
1834
/* }}} */
1835
1836
static void zend_sort_modules(void *base, size_t count, size_t siz, compare_func_t compare, swap_func_t swp) /* {{{ */
1837
6.27k
{
1838
6.27k
  Bucket *b1 = base;
1839
6.27k
  Bucket *b2;
1840
6.27k
  Bucket *end = b1 + count;
1841
6.27k
  Bucket tmp;
1842
6.27k
  zend_module_entry *m, *r;
1843
1844
69.0k
  while (b1 < end) {
1845
69.0k
try_again:
1846
69.0k
    m = (zend_module_entry*)Z_PTR(b1->val);
1847
69.0k
    if (!m->module_started && m->deps) {
1848
18.8k
      const zend_module_dep *dep = m->deps;
1849
37.6k
      while (dep->name) {
1850
25.1k
        if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) {
1851
25.1k
          b2 = b1 + 1;
1852
94.1k
          while (b2 < end) {
1853
75.3k
            r = (zend_module_entry*)Z_PTR(b2->val);
1854
75.3k
            if (strcasecmp(dep->name, r->name) == 0) {
1855
6.27k
              tmp = *b1;
1856
6.27k
              *b1 = *b2;
1857
6.27k
              *b2 = tmp;
1858
6.27k
              goto try_again;
1859
6.27k
            }
1860
69.0k
            b2++;
1861
69.0k
          }
1862
25.1k
        }
1863
18.8k
        dep++;
1864
18.8k
      }
1865
18.8k
    }
1866
62.7k
    b1++;
1867
62.7k
  }
1868
6.27k
}
1869
/* }}} */
1870
1871
ZEND_API void zend_collect_module_handlers(void) /* {{{ */
1872
6.27k
{
1873
6.27k
  zend_module_entry *module;
1874
6.27k
  int startup_count = 0;
1875
6.27k
  int shutdown_count = 0;
1876
6.27k
  int post_deactivate_count = 0;
1877
6.27k
  zend_class_entry *ce;
1878
6.27k
  int class_count = 0;
1879
1880
  /* Collect extensions with request startup/shutdown handlers */
1881
131k
  ZEND_HASH_FOREACH_PTR(&module_registry, module) {
1882
62.7k
    if (module->request_startup_func) {
1883
31.3k
      startup_count++;
1884
31.3k
    }
1885
62.7k
    if (module->request_shutdown_func) {
1886
31.3k
      shutdown_count++;
1887
31.3k
    }
1888
62.7k
    if (module->post_deactivate_func) {
1889
0
      post_deactivate_count++;
1890
0
    }
1891
62.7k
  } ZEND_HASH_FOREACH_END();
1892
6.27k
  module_request_startup_handlers = (zend_module_entry**)malloc(
1893
6.27k
      sizeof(zend_module_entry*) *
1894
6.27k
    (startup_count + 1 +
1895
6.27k
     shutdown_count + 1 +
1896
6.27k
     post_deactivate_count + 1));
1897
6.27k
  module_request_startup_handlers[startup_count] = NULL;
1898
6.27k
  module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
1899
6.27k
  module_request_shutdown_handlers[shutdown_count] = NULL;
1900
6.27k
  module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
1901
6.27k
  module_post_deactivate_handlers[post_deactivate_count] = NULL;
1902
6.27k
  startup_count = 0;
1903
1904
131k
  ZEND_HASH_FOREACH_PTR(&module_registry, module) {
1905
62.7k
    if (module->request_startup_func) {
1906
31.3k
      module_request_startup_handlers[startup_count++] = module;
1907
31.3k
    }
1908
62.7k
    if (module->request_shutdown_func) {
1909
31.3k
      module_request_shutdown_handlers[--shutdown_count] = module;
1910
31.3k
    }
1911
62.7k
    if (module->post_deactivate_func) {
1912
0
      module_post_deactivate_handlers[--post_deactivate_count] = module;
1913
0
    }
1914
62.7k
  } ZEND_HASH_FOREACH_END();
1915
1916
  /* Collect internal classes with static members */
1917
1.43M
  ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
1918
715k
    if (ce->type == ZEND_INTERNAL_CLASS &&
1919
715k
        ce->default_static_members_count > 0) {
1920
0
        class_count++;
1921
0
    }
1922
715k
  } ZEND_HASH_FOREACH_END();
1923
1924
6.27k
  class_cleanup_handlers = (zend_class_entry**)malloc(
1925
6.27k
    sizeof(zend_class_entry*) *
1926
6.27k
    (class_count + 1));
1927
6.27k
  class_cleanup_handlers[class_count] = NULL;
1928
1929
6.27k
  if (class_count) {
1930
0
    ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
1931
0
      if (ce->type == ZEND_INTERNAL_CLASS &&
1932
0
          ce->default_static_members_count > 0) {
1933
0
          class_cleanup_handlers[--class_count] = ce;
1934
0
      }
1935
0
    } ZEND_HASH_FOREACH_END();
1936
0
  }
1937
6.27k
}
1938
/* }}} */
1939
1940
ZEND_API void zend_startup_modules(void) /* {{{ */
1941
6.27k
{
1942
6.27k
  zend_hash_sort_ex(&module_registry, zend_sort_modules, NULL, 0);
1943
6.27k
  zend_hash_apply(&module_registry, zend_startup_module_zval);
1944
6.27k
}
1945
/* }}} */
1946
1947
ZEND_API void zend_destroy_modules(void) /* {{{ */
1948
0
{
1949
0
  free(class_cleanup_handlers);
1950
0
  free(module_request_startup_handlers);
1951
0
  zend_hash_graceful_reverse_destroy(&module_registry);
1952
0
}
1953
/* }}} */
1954
1955
ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module) /* {{{ */
1956
62.7k
{
1957
62.7k
  size_t name_len;
1958
62.7k
  zend_string *lcname;
1959
62.7k
  zend_module_entry *module_ptr;
1960
1961
62.7k
  if (!module) {
1962
0
    return NULL;
1963
0
  }
1964
1965
#if 0
1966
  zend_printf("%s: Registering module %d\n", module->name, module->module_number);
1967
#endif
1968
1969
  /* Check module dependencies */
1970
62.7k
  if (module->deps) {
1971
12.5k
    const zend_module_dep *dep = module->deps;
1972
1973
31.3k
    while (dep->name) {
1974
18.8k
      if (dep->type == MODULE_DEP_CONFLICTS) {
1975
0
        name_len = strlen(dep->name);
1976
0
        lcname = zend_string_alloc(name_len, 0);
1977
0
        zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
1978
1979
0
        if (zend_hash_exists(&module_registry, lcname) || zend_get_extension(dep->name)) {
1980
0
          zend_string_efree(lcname);
1981
          /* TODO: Check version relationship */
1982
0
          zend_error(E_CORE_WARNING, "Cannot load module \"%s\" because conflicting module \"%s\" is already loaded", module->name, dep->name);
1983
0
          return NULL;
1984
0
        }
1985
0
        zend_string_efree(lcname);
1986
0
      }
1987
18.8k
      ++dep;
1988
18.8k
    }
1989
12.5k
  }
1990
1991
62.7k
  name_len = strlen(module->name);
1992
62.7k
  lcname = zend_string_alloc(name_len, module->type == MODULE_PERSISTENT);
1993
62.7k
  zend_str_tolower_copy(ZSTR_VAL(lcname), module->name, name_len);
1994
1995
62.7k
  lcname = zend_new_interned_string(lcname);
1996
62.7k
  if ((module_ptr = zend_hash_add_mem(&module_registry, lcname, module, sizeof(zend_module_entry))) == NULL) {
1997
0
    zend_error(E_CORE_WARNING, "Module \"%s\" is already loaded", module->name);
1998
0
    zend_string_release(lcname);
1999
0
    return NULL;
2000
0
  }
2001
62.7k
  module = module_ptr;
2002
62.7k
  EG(current_module) = module;
2003
2004
62.7k
  if (module->functions && zend_register_functions(NULL, module->functions, NULL, module->type)==FAILURE) {
2005
0
    zend_hash_del(&module_registry, lcname);
2006
0
    zend_string_release(lcname);
2007
0
    EG(current_module) = NULL;
2008
0
    zend_error(E_CORE_WARNING,"%s: Unable to register functions, unable to load", module->name);
2009
0
    return NULL;
2010
0
  }
2011
2012
62.7k
  EG(current_module) = NULL;
2013
62.7k
  zend_string_release(lcname);
2014
62.7k
  return module;
2015
62.7k
}
2016
/* }}} */
2017
2018
ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module) /* {{{ */
2019
56.5k
{
2020
56.5k
  module->module_number = zend_next_free_module();
2021
56.5k
  module->type = MODULE_PERSISTENT;
2022
56.5k
  return zend_register_module_ex(module);
2023
56.5k
}
2024
/* }}} */
2025
2026
static void zend_check_magic_method_args(
2027
    uint32_t num_args, const zend_class_entry *ce, const zend_function *fptr, int error_type)
2028
432k
{
2029
432k
  if (fptr->common.num_args != num_args) {
2030
185
    if (num_args == 0) {
2031
108
      zend_error(error_type, "Method %s::%s() cannot take arguments",
2032
108
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2033
77
    } else if (num_args == 1) {
2034
54
      zend_error(error_type, "Method %s::%s() must take exactly 1 argument",
2035
54
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2036
23
    } else {
2037
23
      zend_error(error_type, "Method %s::%s() must take exactly %" PRIu32 " arguments",
2038
23
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name), num_args);
2039
23
    }
2040
185
    return;
2041
185
  }
2042
537k
  for (uint32_t i = 0; i < num_args; i++) {
2043
105k
    if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, i + 1)) {
2044
127
      zend_error(error_type, "Method %s::%s() cannot take arguments by reference",
2045
127
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2046
127
      return;
2047
127
    }
2048
105k
  }
2049
432k
}
2050
2051
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)
2052
97.6k
{
2053
97.6k
    if (
2054
97.6k
      ZEND_TYPE_IS_SET(fptr->common.arg_info[arg_num].type)
2055
69.4k
       && !(ZEND_TYPE_FULL_MASK(fptr->common.arg_info[arg_num].type) & arg_type)
2056
242
    ) {
2057
242
      zend_error(error_type, "%s::%s(): Parameter #%d ($%s) must be of type %s when declared",
2058
242
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2059
242
        arg_num + 1, ZSTR_VAL(fptr->common.arg_info[arg_num].name),
2060
242
        ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(arg_type))));
2061
242
    }
2062
97.6k
}
2063
2064
static void zend_check_magic_method_return_type(const zend_class_entry *ce, const zend_function *fptr, int error_type, int return_type)
2065
290k
{
2066
290k
  if (!(fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
2067
    /* For backwards compatibility reasons, do not enforce the return type if it is not set. */
2068
274k
    return;
2069
274k
  }
2070
2071
16.1k
  bool has_class_type = ZEND_TYPE_HAS_CLASS(fptr->common.arg_info[-1].type);
2072
16.1k
  uint32_t extra_types = ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & ~return_type;
2073
16.1k
  if (extra_types & MAY_BE_STATIC) {
2074
21
    extra_types &= ~MAY_BE_STATIC;
2075
21
    has_class_type = 1;
2076
21
  }
2077
2078
16.1k
  if (extra_types || (has_class_type && return_type != MAY_BE_OBJECT)) {
2079
223
    zend_error(error_type, "%s::%s(): Return type must be %s when declared",
2080
223
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2081
223
      ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(return_type))));
2082
223
  }
2083
16.1k
}
2084
2085
static void zend_check_magic_method_non_static(
2086
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2087
741k
{
2088
741k
  if (fptr->common.fn_flags & ZEND_ACC_STATIC) {
2089
109
    zend_error(error_type, "Method %s::%s() cannot be static",
2090
109
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2091
109
  }
2092
741k
}
2093
2094
static void zend_check_magic_method_static(
2095
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2096
34.5k
{
2097
34.5k
  if (!(fptr->common.fn_flags & ZEND_ACC_STATIC)) {
2098
36
    zend_error(error_type, "Method %s::%s() must be static",
2099
36
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2100
36
  }
2101
34.5k
}
2102
2103
static void zend_check_magic_method_public(
2104
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2105
337k
{
2106
  // TODO: Remove this warning after adding proper visibility handling.
2107
337k
  if (!(fptr->common.fn_flags & ZEND_ACC_PUBLIC)) {
2108
143
    zend_error(E_WARNING, "The magic method %s::%s() must have public visibility",
2109
143
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2110
143
  }
2111
337k
}
2112
2113
static void zend_check_magic_method_no_return_type(
2114
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2115
361k
{
2116
361k
  if (fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
2117
36
    zend_error_noreturn(error_type, "Method %s::%s() cannot declare a return type",
2118
36
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2119
36
  }
2120
361k
}
2121
2122
ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, zend_string *lcname, int error_type) /* {{{ */
2123
4.75M
{
2124
4.75M
  if (ZSTR_VAL(fptr->common.function_name)[0] != '_'
2125
3.97M
   || ZSTR_VAL(fptr->common.function_name)[1] != '_') {
2126
3.97M
    return;
2127
3.97M
  }
2128
2129
779k
  if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2130
342k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2131
342k
    zend_check_magic_method_no_return_type(ce, fptr, error_type);
2132
436k
  } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2133
18.8k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2134
18.8k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2135
18.8k
    zend_check_magic_method_no_return_type(ce, fptr, error_type);
2136
417k
  } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2137
76.7k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2138
76.7k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2139
76.7k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2140
340k
  } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2141
10.7k
    zend_check_magic_method_args(1, ce, fptr, error_type);
2142
10.7k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2143
10.7k
    zend_check_magic_method_public(ce, fptr, error_type);
2144
10.7k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2145
330k
  } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2146
7.43k
    zend_check_magic_method_args(2, ce, fptr, error_type);
2147
7.43k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2148
7.43k
    zend_check_magic_method_public(ce, fptr, error_type);
2149
7.43k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2150
7.43k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2151
322k
  } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
2152
1.35k
    zend_check_magic_method_args(1, ce, fptr, error_type);
2153
1.35k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2154
1.35k
    zend_check_magic_method_public(ce, fptr, error_type);
2155
1.35k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2156
1.35k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2157
321k
  } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2158
2.16k
    zend_check_magic_method_args(1, ce, fptr, error_type);
2159
2.16k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2160
2.16k
    zend_check_magic_method_public(ce, fptr, error_type);
2161
2.16k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2162
2.16k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_BOOL);
2163
319k
  } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2164
3.64k
    zend_check_magic_method_args(2, ce, fptr, error_type);
2165
3.64k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2166
3.64k
    zend_check_magic_method_public(ce, fptr, error_type);
2167
3.64k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2168
3.64k
    zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2169
315k
  } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
2170
2.68k
    zend_check_magic_method_args(2, ce, fptr, error_type);
2171
2.68k
    zend_check_magic_method_static(ce, fptr, error_type);
2172
2.68k
    zend_check_magic_method_public(ce, fptr, error_type);
2173
2.68k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2174
2.68k
    zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2175
312k
  } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
2176
105k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2177
105k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2178
105k
    zend_check_magic_method_public(ce, fptr, error_type);
2179
207k
  } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
2180
51.0k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2181
51.0k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2182
51.0k
    zend_check_magic_method_public(ce, fptr, error_type);
2183
51.0k
    zend_check_magic_method_return_type(ce, fptr, error_type, (MAY_BE_ARRAY | MAY_BE_NULL));
2184
156k
  } else if (zend_string_equals_literal(lcname, "__serialize")) {
2185
31.6k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2186
31.6k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2187
31.6k
    zend_check_magic_method_public(ce, fptr, error_type);
2188
31.6k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2189
124k
  } else if (zend_string_equals_literal(lcname, "__unserialize")) {
2190
31.8k
    zend_check_magic_method_args(1, ce, fptr, error_type);
2191
31.8k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2192
31.8k
    zend_check_magic_method_public(ce, fptr, error_type);
2193
31.8k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2194
31.8k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2195
92.8k
  } else if (zend_string_equals_literal(lcname, "__set_state")) {
2196
31.8k
    zend_check_magic_method_args(1, ce, fptr, error_type);
2197
31.8k
    zend_check_magic_method_static(ce, fptr, error_type);
2198
31.8k
    zend_check_magic_method_public(ce, fptr, error_type);
2199
31.8k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2200
31.8k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_OBJECT);
2201
60.9k
  } else if (zend_string_equals_literal(lcname, "__invoke")) {
2202
1.25k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2203
1.25k
    zend_check_magic_method_public(ce, fptr, error_type);
2204
59.7k
  } else if (zend_string_equals_literal(lcname, "__sleep")) {
2205
257
    zend_check_magic_method_args(0, ce, fptr, error_type);
2206
257
    zend_check_magic_method_non_static(ce, fptr, error_type);
2207
257
    zend_check_magic_method_public(ce, fptr, error_type);
2208
257
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2209
59.4k
  } else if (zend_string_equals_literal(lcname, "__wakeup")) {
2210
56.7k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2211
56.7k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2212
56.7k
    zend_check_magic_method_public(ce, fptr, error_type);
2213
56.7k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2214
56.7k
  }
2215
779k
}
2216
/* }}} */
2217
2218
ZEND_API void zend_add_magic_method(zend_class_entry *ce, zend_function *fptr, zend_string *lcname)
2219
4.75M
{
2220
4.75M
  if (ZSTR_VAL(lcname)[0] != '_' || ZSTR_VAL(lcname)[1] != '_') {
2221
    /* pass */
2222
779k
  } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2223
76.7k
    ce->clone = fptr;
2224
703k
  } else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2225
343k
    ce->constructor = fptr;
2226
343k
    ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
2227
360k
  } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2228
18.9k
    ce->destructor = fptr;
2229
341k
  } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2230
10.7k
    ce->__get = fptr;
2231
10.7k
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2232
330k
  } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2233
7.45k
    ce->__set = fptr;
2234
7.45k
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2235
322k
  } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2236
3.64k
    ce->__call = fptr;
2237
319k
  } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
2238
1.35k
    ce->__unset = fptr;
2239
1.35k
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2240
317k
  } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2241
2.20k
    ce->__isset = fptr;
2242
2.20k
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2243
315k
  } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
2244
2.70k
    ce->__callstatic = fptr;
2245
313k
  } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
2246
105k
    ce->__tostring = fptr;
2247
207k
  } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
2248
51.0k
    ce->__debugInfo = fptr;
2249
156k
  } else if (zend_string_equals_literal(lcname, "__serialize")) {
2250
31.6k
    ce->__serialize = fptr;
2251
124k
  } else if (zend_string_equals_literal(lcname, "__unserialize")) {
2252
31.8k
    ce->__unserialize = fptr;
2253
31.8k
  }
2254
4.75M
}
2255
2256
/* registers all functions in *library_functions in the function hash */
2257
ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type) /* {{{ */
2258
653k
{
2259
653k
  const zend_function_entry *ptr = functions;
2260
653k
  zend_function function, *reg_function;
2261
653k
  zend_internal_function *internal_function = (zend_internal_function *)&function;
2262
653k
  int count=0, unload=0;
2263
653k
  HashTable *target_function_table = function_table;
2264
653k
  int error_type;
2265
653k
  zend_string *lowercase_name;
2266
653k
  size_t fname_len;
2267
2268
653k
  if (type==MODULE_PERSISTENT) {
2269
653k
    error_type = E_CORE_WARNING;
2270
0
  } else {
2271
0
    error_type = E_WARNING;
2272
0
  }
2273
2274
653k
  if (!target_function_table) {
2275
56.5k
    target_function_table = CG(function_table);
2276
56.5k
  }
2277
653k
  internal_function->type = ZEND_INTERNAL_FUNCTION;
2278
653k
  internal_function->module = EG(current_module);
2279
653k
  memset(internal_function->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
2280
2281
9.91M
  while (ptr->fname) {
2282
9.26M
    fname_len = strlen(ptr->fname);
2283
9.26M
    internal_function->handler = ptr->handler;
2284
9.26M
    internal_function->function_name = zend_string_init_interned(ptr->fname, fname_len, 1);
2285
9.26M
    internal_function->scope = scope;
2286
9.26M
    internal_function->prototype = NULL;
2287
9.26M
    if (ptr->flags) {
2288
4.57M
      if (!(ptr->flags & ZEND_ACC_PPP_MASK)) {
2289
0
        if (ptr->flags != ZEND_ACC_DEPRECATED && scope) {
2290
0
          zend_error(error_type, "Invalid access level for %s%s%s() - access must be exactly one of public, protected or private", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2291
0
        }
2292
0
        internal_function->fn_flags = ZEND_ACC_PUBLIC | ptr->flags;
2293
4.57M
      } else {
2294
4.57M
        internal_function->fn_flags = ptr->flags;
2295
4.57M
      }
2296
4.68M
    } else {
2297
4.68M
      internal_function->fn_flags = ZEND_ACC_PUBLIC;
2298
4.68M
    }
2299
2300
9.26M
    if (ptr->arg_info) {
2301
9.26M
      zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info;
2302
9.26M
      internal_function->arg_info = (zend_internal_arg_info*)ptr->arg_info+1;
2303
9.26M
      internal_function->num_args = ptr->num_args;
2304
      /* Currently you cannot denote that the function can accept less arguments than num_args */
2305
9.26M
      if (info->required_num_args == (zend_uintptr_t)-1) {
2306
0
        internal_function->required_num_args = ptr->num_args;
2307
9.26M
      } else {
2308
9.26M
        internal_function->required_num_args = info->required_num_args;
2309
9.26M
      }
2310
9.26M
      if (ZEND_ARG_SEND_MODE(info)) {
2311
0
        internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE;
2312
0
      }
2313
9.26M
      if (ZEND_ARG_IS_VARIADIC(&ptr->arg_info[ptr->num_args])) {
2314
288k
        internal_function->fn_flags |= ZEND_ACC_VARIADIC;
2315
        /* Don't count the variadic argument */
2316
288k
        internal_function->num_args--;
2317
288k
      }
2318
9.26M
      if (ZEND_TYPE_IS_SET(info->type)) {
2319
5.06M
        if (ZEND_TYPE_HAS_NAME(info->type)) {
2320
200k
          const char *type_name = ZEND_TYPE_LITERAL_NAME(info->type);
2321
200k
          if (!scope && (!strcasecmp(type_name, "self") || !strcasecmp(type_name, "parent"))) {
2322
0
            zend_error_noreturn(E_CORE_ERROR, "Cannot declare a return type of %s outside of a class scope", type_name);
2323
0
          }
2324
5.06M
        }
2325
2326
5.06M
        internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
2327
5.06M
      }
2328
0
    } else {
2329
0
      zend_error(E_CORE_WARNING, "Missing arginfo for %s%s%s()",
2330
0
         scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2331
2332
0
      internal_function->arg_info = NULL;
2333
0
      internal_function->num_args = 0;
2334
0
      internal_function->required_num_args = 0;
2335
0
    }
2336
2337
9.26M
    zend_set_function_arg_flags((zend_function*)internal_function);
2338
9.26M
    if (ptr->flags & ZEND_ACC_ABSTRACT) {
2339
238k
      if (scope) {
2340
        /* This is a class that must be abstract itself. Here we set the check info. */
2341
238k
        scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
2342
238k
        if (!(scope->ce_flags & ZEND_ACC_INTERFACE)) {
2343
          /* Since the class is not an interface it needs to be declared as a abstract class. */
2344
          /* Since here we are handling internal functions only we can add the keyword flag. */
2345
          /* This time we set the flag for the keyword 'abstract'. */
2346
12.5k
          scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
2347
12.5k
        }
2348
238k
      }
2349
238k
      if ((ptr->flags & ZEND_ACC_STATIC) && (!scope || !(scope->ce_flags & ZEND_ACC_INTERFACE))) {
2350
0
        zend_error(error_type, "Static function %s%s%s() cannot be abstract", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2351
0
      }
2352
9.02M
    } else {
2353
9.02M
      if (scope && (scope->ce_flags & ZEND_ACC_INTERFACE)) {
2354
0
        zend_error(error_type, "Interface %s cannot contain non abstract method %s()", ZSTR_VAL(scope->name), ptr->fname);
2355
0
        return FAILURE;
2356
0
      }
2357
9.02M
      if (!internal_function->handler) {
2358
0
        zend_error(error_type, "Method %s%s%s() cannot be a NULL function", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2359
0
        zend_unregister_functions(functions, count, target_function_table);
2360
0
        return FAILURE;
2361
0
      }
2362
9.26M
    }
2363
9.26M
    lowercase_name = zend_string_tolower_ex(internal_function->function_name, type == MODULE_PERSISTENT);
2364
9.26M
    lowercase_name = zend_new_interned_string(lowercase_name);
2365
9.26M
    reg_function = malloc(sizeof(zend_internal_function));
2366
9.26M
    memcpy(reg_function, &function, sizeof(zend_internal_function));
2367
9.26M
    if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) {
2368
0
      unload=1;
2369
0
      free(reg_function);
2370
0
      zend_string_release(lowercase_name);
2371
0
      break;
2372
0
    }
2373
2374
    /* Get parameter count including variadic parameter. */
2375
9.26M
    uint32_t num_args = reg_function->common.num_args;
2376
9.26M
    if (reg_function->common.fn_flags & ZEND_ACC_VARIADIC) {
2377
288k
      num_args++;
2378
288k
    }
2379
2380
    /* If types of arguments have to be checked */
2381
9.26M
    if (reg_function->common.arg_info && num_args) {
2382
5.75M
      uint32_t i;
2383
16.8M
      for (i = 0; i < num_args; i++) {
2384
11.1M
        zend_internal_arg_info *arg_info = &reg_function->internal_function.arg_info[i];
2385
11.1M
        ZEND_ASSERT(arg_info->name && "Parameter must have a name");
2386
11.1M
        if (ZEND_TYPE_IS_SET(arg_info->type)) {
2387
9.45M
            reg_function->common.fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
2388
9.45M
        }
2389
11.1M
#if ZEND_DEBUG
2390
20.0M
        for (uint32_t j = 0; j < i; j++) {
2391
8.91M
          if (!strcmp(arg_info->name, reg_function->internal_function.arg_info[j].name)) {
2392
0
            zend_error_noreturn(E_CORE_ERROR,
2393
0
              "Duplicate parameter name $%s for function %s%s%s()", arg_info->name,
2394
0
              scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2395
0
          }
2396
8.91M
        }
2397
11.1M
#endif
2398
11.1M
      }
2399
5.75M
    }
2400
2401
9.26M
    if (reg_function->common.arg_info &&
2402
9.26M
        (reg_function->common.fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS))) {
2403
      /* convert "const char*" class type names into "zend_string*" */
2404
6.29M
      uint32_t i;
2405
6.29M
      zend_arg_info *arg_info = reg_function->common.arg_info - 1;
2406
6.29M
      zend_arg_info *new_arg_info;
2407
2408
      /* Treat return type as an extra argument */
2409
6.29M
      num_args++;
2410
6.29M
      new_arg_info = malloc(sizeof(zend_arg_info) * num_args);
2411
6.29M
      memcpy(new_arg_info, arg_info, sizeof(zend_arg_info) * num_args);
2412
6.29M
      reg_function->common.arg_info = new_arg_info + 1;
2413
23.2M
      for (i = 0; i < num_args; i++) {
2414
16.9M
        if (ZEND_TYPE_HAS_CLASS(new_arg_info[i].type)) {
2415
740k
          ZEND_ASSERT(ZEND_TYPE_HAS_NAME(new_arg_info[i].type)
2416
740k
            && "Only simple classes are currently supported");
2417
740k
          const char *class_name = ZEND_TYPE_LITERAL_NAME(new_arg_info[i].type);
2418
740k
          ZEND_TYPE_SET_PTR(new_arg_info[i].type,
2419
740k
            zend_string_init_interned(class_name, strlen(class_name), 1));
2420
740k
        }
2421
16.9M
      }
2422
6.29M
    }
2423
2424
9.26M
    if (scope) {
2425
4.57M
      zend_check_magic_method_implementation(
2426
4.57M
        scope, reg_function, lowercase_name, E_CORE_ERROR);
2427
4.57M
      zend_add_magic_method(scope, reg_function, lowercase_name);
2428
4.57M
    }
2429
9.26M
    ptr++;
2430
9.26M
    count++;
2431
9.26M
    zend_string_release(lowercase_name);
2432
9.26M
  }
2433
653k
  if (unload) { /* before unloading, display all remaining bad function in the module */
2434
0
    while (ptr->fname) {
2435
0
      fname_len = strlen(ptr->fname);
2436
0
      lowercase_name = zend_string_alloc(fname_len, 0);
2437
0
      zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
2438
0
      if (zend_hash_exists(target_function_table, lowercase_name)) {
2439
0
        zend_error(error_type, "Function registration failed - duplicate name - %s%s%s", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2440
0
      }
2441
0
      zend_string_efree(lowercase_name);
2442
0
      ptr++;
2443
0
    }
2444
0
    zend_unregister_functions(functions, count, target_function_table);
2445
0
    return FAILURE;
2446
0
  }
2447
653k
  return SUCCESS;
2448
653k
}
2449
/* }}} */
2450
2451
/* count=-1 means erase all functions, otherwise,
2452
 * erase the first count functions
2453
 */
2454
ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table) /* {{{ */
2455
0
{
2456
0
  const zend_function_entry *ptr = functions;
2457
0
  int i=0;
2458
0
  HashTable *target_function_table = function_table;
2459
0
  zend_string *lowercase_name;
2460
0
  size_t fname_len;
2461
2462
0
  if (!target_function_table) {
2463
0
    target_function_table = CG(function_table);
2464
0
  }
2465
0
  while (ptr->fname) {
2466
0
    if (count!=-1 && i>=count) {
2467
0
      break;
2468
0
    }
2469
0
    fname_len = strlen(ptr->fname);
2470
0
    lowercase_name = zend_string_alloc(fname_len, 0);
2471
0
    zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
2472
0
    zend_hash_del(target_function_table, lowercase_name);
2473
0
    zend_string_efree(lowercase_name);
2474
0
    ptr++;
2475
0
    i++;
2476
0
  }
2477
0
}
2478
/* }}} */
2479
2480
ZEND_API zend_result zend_startup_module(zend_module_entry *module) /* {{{ */
2481
0
{
2482
0
  if ((module = zend_register_internal_module(module)) != NULL && zend_startup_module_ex(module) == SUCCESS) {
2483
0
    return SUCCESS;
2484
0
  }
2485
0
  return FAILURE;
2486
0
}
2487
/* }}} */
2488
2489
ZEND_API zend_result zend_get_module_started(const char *module_name) /* {{{ */
2490
0
{
2491
0
  zend_module_entry *module;
2492
2493
0
  module = zend_hash_str_find_ptr(&module_registry, module_name, strlen(module_name));
2494
0
  return (module && module->module_started) ? SUCCESS : FAILURE;
2495
0
}
2496
/* }}} */
2497
2498
static int clean_module_class(zval *el, void *arg) /* {{{ */
2499
0
{
2500
0
  zend_class_entry *ce = (zend_class_entry *)Z_PTR_P(el);
2501
0
  int module_number = *(int *)arg;
2502
0
  if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->module_number == module_number) {
2503
0
    return ZEND_HASH_APPLY_REMOVE;
2504
0
  } else {
2505
0
    return ZEND_HASH_APPLY_KEEP;
2506
0
  }
2507
0
}
2508
/* }}} */
2509
2510
static void clean_module_classes(int module_number) /* {{{ */
2511
0
{
2512
0
  zend_hash_apply_with_argument(EG(class_table), clean_module_class, (void *) &module_number);
2513
0
}
2514
/* }}} */
2515
2516
void module_destructor(zend_module_entry *module) /* {{{ */
2517
0
{
2518
2519
0
  if (module->type == MODULE_TEMPORARY) {
2520
0
    zend_clean_module_rsrc_dtors(module->module_number);
2521
0
    clean_module_constants(module->module_number);
2522
0
    clean_module_classes(module->module_number);
2523
0
  }
2524
2525
0
  if (module->module_started && module->module_shutdown_func) {
2526
#if 0
2527
    zend_printf("%s: Module shutdown\n", module->name);
2528
#endif
2529
0
    module->module_shutdown_func(module->type, module->module_number);
2530
0
  }
2531
2532
0
  if (module->module_started
2533
0
   && !module->module_shutdown_func
2534
0
   && module->type == MODULE_TEMPORARY) {
2535
0
    zend_unregister_ini_entries(module->module_number);
2536
0
  }
2537
2538
  /* Deinitilaise module globals */
2539
0
  if (module->globals_size) {
2540
#ifdef ZTS
2541
    if (*module->globals_id_ptr) {
2542
      ts_free_id(*module->globals_id_ptr);
2543
    }
2544
#else
2545
0
    if (module->globals_dtor) {
2546
0
      module->globals_dtor(module->globals_ptr);
2547
0
    }
2548
0
#endif
2549
0
  }
2550
2551
0
  module->module_started=0;
2552
0
  if (module->type == MODULE_TEMPORARY && module->functions) {
2553
0
    zend_unregister_functions(module->functions, -1, NULL);
2554
0
  }
2555
2556
0
#if HAVE_LIBDL
2557
0
  if (module->handle && !getenv("ZEND_DONT_UNLOAD_MODULES")) {
2558
0
    DL_UNLOAD(module->handle);
2559
0
  }
2560
0
#endif
2561
0
}
2562
/* }}} */
2563
2564
ZEND_API void zend_activate_modules(void) /* {{{ */
2565
613k
{
2566
613k
  zend_module_entry **p = module_request_startup_handlers;
2567
2568
3.67M
  while (*p) {
2569
3.06M
    zend_module_entry *module = *p;
2570
2571
3.06M
    if (module->request_startup_func(module->type, module->module_number)==FAILURE) {
2572
0
      zend_error(E_WARNING, "request_startup() for %s module failed", module->name);
2573
0
      exit(1);
2574
0
    }
2575
3.06M
    p++;
2576
3.06M
  }
2577
613k
}
2578
/* }}} */
2579
2580
ZEND_API void zend_deactivate_modules(void) /* {{{ */
2581
610k
{
2582
610k
  EG(current_execute_data) = NULL; /* we're no longer executing anything */
2583
2584
610k
  zend_try {
2585
610k
    if (EG(full_tables_cleanup)) {
2586
0
      zend_module_entry *module;
2587
2588
0
      ZEND_HASH_REVERSE_FOREACH_PTR(&module_registry, module) {
2589
0
        if (module->request_shutdown_func) {
2590
#if 0
2591
          zend_printf("%s: Request shutdown\n", module->name);
2592
#endif
2593
0
          module->request_shutdown_func(module->type, module->module_number);
2594
0
        }
2595
0
      } ZEND_HASH_FOREACH_END();
2596
610k
    } else {
2597
610k
      zend_module_entry **p = module_request_shutdown_handlers;
2598
2599
3.66M
      while (*p) {
2600
3.05M
        zend_module_entry *module = *p;
2601
2602
3.05M
        module->request_shutdown_func(module->type, module->module_number);
2603
3.05M
        p++;
2604
3.05M
      }
2605
610k
    }
2606
610k
  } zend_end_try();
2607
610k
}
2608
/* }}} */
2609
2610
ZEND_API void zend_cleanup_internal_classes(void) /* {{{ */
2611
0
{
2612
0
  zend_class_entry **p = class_cleanup_handlers;
2613
2614
0
  while (*p) {
2615
0
    zend_cleanup_internal_class_data(*p);
2616
0
    p++;
2617
0
  }
2618
0
}
2619
/* }}} */
2620
2621
ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
2622
610k
{
2623
610k
  if (EG(full_tables_cleanup)) {
2624
0
    zend_module_entry *module;
2625
0
    zval *zv;
2626
0
    zend_string *key;
2627
2628
0
    ZEND_HASH_FOREACH_PTR(&module_registry, module) {
2629
0
      if (module->post_deactivate_func) {
2630
0
        module->post_deactivate_func();
2631
0
      }
2632
0
    } ZEND_HASH_FOREACH_END();
2633
0
    ZEND_HASH_REVERSE_FOREACH_STR_KEY_VAL(&module_registry, key, zv) {
2634
0
      module = Z_PTR_P(zv);
2635
0
      if (module->type != MODULE_TEMPORARY) {
2636
0
        break;
2637
0
      }
2638
0
      module_destructor(module);
2639
0
      free(module);
2640
0
      zend_string_release_ex(key, 0);
2641
0
    } ZEND_HASH_FOREACH_END_DEL();
2642
610k
  } else {
2643
610k
    zend_module_entry **p = module_post_deactivate_handlers;
2644
2645
610k
    while (*p) {
2646
0
      zend_module_entry *module = *p;
2647
2648
0
      module->post_deactivate_func();
2649
0
      p++;
2650
0
    }
2651
610k
  }
2652
610k
}
2653
/* }}} */
2654
2655
/* return the next free module number */
2656
ZEND_API int zend_next_free_module(void) /* {{{ */
2657
56.5k
{
2658
56.5k
  return zend_hash_num_elements(&module_registry) + 1;
2659
56.5k
}
2660
/* }}} */
2661
2662
static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
2663
715k
{
2664
715k
  zend_class_entry *class_entry = malloc(sizeof(zend_class_entry));
2665
715k
  zend_string *lowercase_name;
2666
715k
  *class_entry = *orig_class_entry;
2667
2668
715k
  class_entry->type = ZEND_INTERNAL_CLASS;
2669
715k
  zend_initialize_class_data(class_entry, 0);
2670
715k
  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;
2671
715k
  class_entry->info.internal.module = EG(current_module);
2672
2673
715k
  if (class_entry->info.internal.builtin_functions) {
2674
596k
    zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, EG(current_module)->type);
2675
596k
  }
2676
2677
715k
  lowercase_name = zend_string_tolower_ex(orig_class_entry->name, EG(current_module)->type == MODULE_PERSISTENT);
2678
715k
  lowercase_name = zend_new_interned_string(lowercase_name);
2679
715k
  zend_hash_update_ptr(CG(class_table), lowercase_name, class_entry);
2680
715k
  zend_string_release_ex(lowercase_name, 1);
2681
715k
  return class_entry;
2682
715k
}
2683
/* }}} */
2684
2685
/* If parent_ce is not NULL then it inherits from parent_ce
2686
 * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it
2687
 * If both parent_ce and parent_name are NULL it does a regular class registration
2688
 * If parent_name is specified but not found NULL is returned
2689
 */
2690
ZEND_API zend_class_entry *zend_register_internal_class_ex(zend_class_entry *class_entry, zend_class_entry *parent_ce) /* {{{ */
2691
395k
{
2692
395k
  zend_class_entry *register_class;
2693
2694
395k
  register_class = zend_register_internal_class(class_entry);
2695
2696
395k
  if (parent_ce) {
2697
351k
    zend_do_inheritance(register_class, parent_ce);
2698
351k
    zend_build_properties_info_table(register_class);
2699
351k
  }
2700
395k
  return register_class;
2701
395k
}
2702
/* }}} */
2703
2704
ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...) /* {{{ */
2705
426k
{
2706
426k
  zend_class_entry *interface_entry;
2707
426k
  va_list interface_list;
2708
426k
  va_start(interface_list, num_interfaces);
2709
2710
866k
  while (num_interfaces--) {
2711
439k
    interface_entry = va_arg(interface_list, zend_class_entry *);
2712
439k
    zend_do_implement_interface(class_entry, interface_entry);
2713
439k
  }
2714
2715
426k
  va_end(interface_list);
2716
426k
}
2717
/* }}} */
2718
2719
/* A class that contains at least one abstract method automatically becomes an abstract class.
2720
 */
2721
ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *orig_class_entry) /* {{{ */
2722
615k
{
2723
615k
  return do_register_internal_class(orig_class_entry, 0);
2724
615k
}
2725
/* }}} */
2726
2727
ZEND_API zend_class_entry *zend_register_internal_interface(zend_class_entry *orig_class_entry) /* {{{ */
2728
100k
{
2729
100k
  return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE);
2730
100k
}
2731
/* }}} */
2732
2733
ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce, bool persistent) /* {{{ */
2734
624
{
2735
624
  zend_string *lcname;
2736
624
  zval zv, *ret;
2737
2738
  /* TODO: Move this out of here in 7.4. */
2739
624
  if (persistent && EG(current_module) && EG(current_module)->type == MODULE_TEMPORARY) {
2740
0
    persistent = 0;
2741
0
  }
2742
2743
624
  if (name[0] == '\\') {
2744
1
    lcname = zend_string_alloc(name_len-1, persistent);
2745
1
    zend_str_tolower_copy(ZSTR_VAL(lcname), name+1, name_len-1);
2746
623
  } else {
2747
623
    lcname = zend_string_alloc(name_len, persistent);
2748
623
    zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len);
2749
623
  }
2750
2751
624
  zend_assert_valid_class_name(lcname);
2752
2753
624
  lcname = zend_new_interned_string(lcname);
2754
2755
624
  ZVAL_ALIAS_PTR(&zv, ce);
2756
624
  ret = zend_hash_add(CG(class_table), lcname, &zv);
2757
624
  zend_string_release_ex(lcname, 0);
2758
624
  if (ret) {
2759
480
    if (!(ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
2760
480
      ce->refcount++;
2761
480
    }
2762
480
    return SUCCESS;
2763
480
  }
2764
144
  return FAILURE;
2765
144
}
2766
/* }}} */
2767
2768
// TODO num_symbol_tables as unsigned int?
2769
ZEND_API zend_result zend_set_hash_symbol(zval *symbol, const char *name, size_t name_length, zend_bool is_ref, int num_symbol_tables, ...) /* {{{ */
2770
0
{
2771
0
  HashTable *symbol_table;
2772
0
  va_list symbol_table_list;
2773
2774
0
  if (num_symbol_tables <= 0) return FAILURE;
2775
2776
0
  if (is_ref) {
2777
0
    ZVAL_MAKE_REF(symbol);
2778
0
  }
2779
2780
0
  va_start(symbol_table_list, num_symbol_tables);
2781
0
  while (num_symbol_tables-- > 0) {
2782
0
    symbol_table = va_arg(symbol_table_list, HashTable *);
2783
0
    zend_hash_str_update(symbol_table, name, name_length, symbol);
2784
0
    Z_TRY_ADDREF_P(symbol);
2785
0
  }
2786
0
  va_end(symbol_table_list);
2787
0
  return SUCCESS;
2788
0
}
2789
/* }}} */
2790
2791
/* Disabled functions support */
2792
2793
static void zend_disable_function(const char *function_name, size_t function_name_length)
2794
200k
{
2795
200k
  zend_hash_str_del(CG(function_table), function_name, function_name_length);
2796
200k
}
2797
2798
ZEND_API void zend_disable_functions(const char *function_list) /* {{{ */
2799
6.27k
{
2800
6.27k
  if (!function_list || !*function_list) {
2801
0
    return;
2802
0
  }
2803
2804
6.27k
  const char *s = NULL, *e = function_list;
2805
1.74M
  while (*e) {
2806
1.73M
    switch (*e) {
2807
0
      case ' ':
2808
194k
      case ',':
2809
194k
        if (s) {
2810
194k
          zend_disable_function(s, e - s);
2811
194k
          s = NULL;
2812
194k
        }
2813
194k
        break;
2814
1.54M
      default:
2815
1.54M
        if (!s) {
2816
200k
          s = e;
2817
200k
        }
2818
1.54M
        break;
2819
1.73M
    }
2820
1.73M
    e++;
2821
1.73M
  }
2822
6.27k
  if (s) {
2823
6.27k
    zend_disable_function(s, e - s);
2824
6.27k
  }
2825
2826
  /* Rehash the function table after deleting functions. This ensures that all internal
2827
   * functions are contiguous, which means we don't need to perform full table cleanup
2828
   * on shutdown. */
2829
6.27k
  zend_hash_rehash(CG(function_table));
2830
6.27k
}
2831
/* }}} */
2832
2833
#ifdef ZEND_WIN32
2834
#pragma optimize("", off)
2835
#endif
2836
static ZEND_COLD zend_object *display_disabled_class(zend_class_entry *class_type) /* {{{ */
2837
0
{
2838
0
  zend_object *intern;
2839
2840
0
  intern = zend_objects_new(class_type);
2841
2842
  /* Initialize default properties */
2843
0
  if (EXPECTED(class_type->default_properties_count != 0)) {
2844
0
    zval *p = intern->properties_table;
2845
0
    zval *end = p + class_type->default_properties_count;
2846
0
    do {
2847
0
      ZVAL_UNDEF(p);
2848
0
      p++;
2849
0
    } while (p != end);
2850
0
  }
2851
2852
0
  zend_error(E_WARNING, "%s() has been disabled for security reasons", ZSTR_VAL(class_type->name));
2853
0
  return intern;
2854
0
}
2855
#ifdef ZEND_WIN32
2856
#pragma optimize("", on)
2857
#endif
2858
/* }}} */
2859
2860
static const zend_function_entry disabled_class_new[] = {
2861
  ZEND_FE_END
2862
};
2863
2864
ZEND_API zend_result zend_disable_class(const char *class_name, size_t class_name_length) /* {{{ */
2865
0
{
2866
0
  zend_class_entry *disabled_class;
2867
0
  zend_string *key;
2868
0
  zend_function *fn;
2869
2870
0
  key = zend_string_alloc(class_name_length, 0);
2871
0
  zend_str_tolower_copy(ZSTR_VAL(key), class_name, class_name_length);
2872
0
  disabled_class = zend_hash_find_ptr(CG(class_table), key);
2873
0
  zend_string_release_ex(key, 0);
2874
0
  if (!disabled_class) {
2875
0
    return FAILURE;
2876
0
  }
2877
2878
0
  INIT_CLASS_ENTRY_INIT_METHODS((*disabled_class), disabled_class_new);
2879
0
  disabled_class->create_object = display_disabled_class;
2880
2881
0
  ZEND_HASH_FOREACH_PTR(&disabled_class->function_table, fn) {
2882
0
    if ((fn->common.fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS)) &&
2883
0
      fn->common.scope == disabled_class) {
2884
0
      zend_free_internal_arg_info(&fn->internal_function);
2885
0
    }
2886
0
  } ZEND_HASH_FOREACH_END();
2887
0
  zend_hash_clean(&disabled_class->function_table);
2888
0
  return SUCCESS;
2889
0
}
2890
/* }}} */
2891
2892
static zend_always_inline zend_class_entry *get_scope(zend_execute_data *frame)
2893
12.2k
{
2894
12.2k
  return frame && frame->func ? frame->func->common.scope : NULL;
2895
12.2k
}
2896
2897
static bool zend_is_callable_check_class(zend_string *name, zend_class_entry *scope, zend_execute_data *frame, zend_fcall_info_cache *fcc, bool *strict_class, char **error) /* {{{ */
2898
5.87k
{
2899
5.87k
  bool ret = 0;
2900
5.87k
  zend_class_entry *ce;
2901
5.87k
  size_t name_len = ZSTR_LEN(name);
2902
5.87k
  zend_string *lcname;
2903
5.87k
  ALLOCA_FLAG(use_heap);
2904
2905
5.87k
  ZSTR_ALLOCA_ALLOC(lcname, name_len, use_heap);
2906
5.87k
  zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name), name_len);
2907
2908
5.87k
  *strict_class = 0;
2909
5.87k
  if (zend_string_equals_literal(lcname, "self")) {
2910
497
    if (!scope) {
2911
73
      if (error) *error = estrdup("cannot access \"self\" when no class scope is active");
2912
424
    } else {
2913
424
      fcc->called_scope = zend_get_called_scope(frame);
2914
424
      fcc->calling_scope = scope;
2915
424
      if (!fcc->object) {
2916
244
        fcc->object = zend_get_this_object(frame);
2917
244
      }
2918
424
      ret = 1;
2919
424
    }
2920
5.37k
  } else if (zend_string_equals_literal(lcname, "parent")) {
2921
515
    if (!scope) {
2922
0
      if (error) *error = estrdup("cannot access \"parent\" when no class scope is active");
2923
515
    } else if (!scope->parent) {
2924
18
      if (error) *error = estrdup("cannot access \"parent\" when current class scope has no parent");
2925
497
    } else {
2926
497
      fcc->called_scope = zend_get_called_scope(frame);
2927
497
      fcc->calling_scope = scope->parent;
2928
497
      if (!fcc->object) {
2929
318
        fcc->object = zend_get_this_object(frame);
2930
318
      }
2931
497
      *strict_class = 1;
2932
497
      ret = 1;
2933
497
    }
2934
4.86k
  } else if (zend_string_equals_literal(lcname, "static")) {
2935
220
    zend_class_entry *called_scope = zend_get_called_scope(frame);
2936
2937
220
    if (!called_scope) {
2938
3
      if (error) *error = estrdup("cannot access \"static\" when no class scope is active");
2939
217
    } else {
2940
217
      fcc->called_scope = called_scope;
2941
217
      fcc->calling_scope = called_scope;
2942
217
      if (!fcc->object) {
2943
199
        fcc->object = zend_get_this_object(frame);
2944
199
      }
2945
217
      *strict_class = 1;
2946
217
      ret = 1;
2947
217
    }
2948
4.64k
  } else if ((ce = zend_lookup_class(name)) != NULL) {
2949
3.78k
    zend_class_entry *scope = get_scope(frame);
2950
3.78k
    fcc->calling_scope = ce;
2951
3.78k
    if (scope && !fcc->object) {
2952
1.51k
      zend_object *object = zend_get_this_object(frame);
2953
2954
1.51k
      if (object &&
2955
1.27k
          instanceof_function(object->ce, scope) &&
2956
1.27k
          instanceof_function(scope, ce)) {
2957
1.15k
        fcc->object = object;
2958
1.15k
        fcc->called_scope = object->ce;
2959
362
      } else {
2960
362
        fcc->called_scope = ce;
2961
362
      }
2962
2.26k
    } else {
2963
2.16k
      fcc->called_scope = fcc->object ? fcc->object->ce : ce;
2964
2.26k
    }
2965
3.78k
    *strict_class = 1;
2966
3.78k
    ret = 1;
2967
862
  } else {
2968
862
    if (error) zend_spprintf(error, 0, "class \"%.*s\" not found", (int)name_len, ZSTR_VAL(name));
2969
862
  }
2970
5.87k
  ZSTR_ALLOCA_FREE(lcname, use_heap);
2971
5.87k
  return ret;
2972
5.87k
}
2973
/* }}} */
2974
2975
30.0k
ZEND_API void zend_release_fcall_info_cache(zend_fcall_info_cache *fcc) {
2976
30.0k
  if (fcc->function_handler &&
2977
28.3k
    (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
2978
411
    if (fcc->function_handler->common.function_name) {
2979
411
      zend_string_release_ex(fcc->function_handler->common.function_name, 0);
2980
411
    }
2981
411
    zend_free_trampoline(fcc->function_handler);
2982
411
  }
2983
30.0k
  fcc->function_handler = NULL;
2984
30.0k
}
2985
2986
static zend_always_inline bool zend_is_callable_check_func(int check_flags, zval *callable, zend_execute_data *frame, zend_fcall_info_cache *fcc, bool strict_class, char **error) /* {{{ */
2987
44.0k
{
2988
44.0k
  zend_class_entry *ce_org = fcc->calling_scope;
2989
44.0k
  bool retval = 0;
2990
44.0k
  zend_string *mname, *cname;
2991
44.0k
  zend_string *lmname;
2992
44.0k
  const char *colon;
2993
44.0k
  size_t clen;
2994
44.0k
  HashTable *ftable;
2995
44.0k
  int call_via_handler = 0;
2996
44.0k
  zend_class_entry *scope;
2997
44.0k
  zval *zv;
2998
44.0k
  ALLOCA_FLAG(use_heap)
2999
3000
44.0k
  fcc->calling_scope = NULL;
3001
3002
44.0k
  if (!ce_org) {
3003
9.26k
    zend_function *func;
3004
9.26k
    zend_string *lmname;
3005
3006
    /* Check if function with given name exists.
3007
     * This may be a compound name that includes namespace name */
3008
9.26k
    if (UNEXPECTED(Z_STRVAL_P(callable)[0] == '\\')) {
3009
      /* Skip leading \ */
3010
314
      ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable) - 1, use_heap);
3011
314
      zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable) + 1, Z_STRLEN_P(callable) - 1);
3012
314
      func = zend_fetch_function(lmname);
3013
314
      ZSTR_ALLOCA_FREE(lmname, use_heap);
3014
8.94k
    } else {
3015
8.94k
      lmname = Z_STR_P(callable);
3016
8.94k
      func = zend_fetch_function(lmname);
3017
8.94k
      if (!func) {
3018
4.29k
        ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable), use_heap);
3019
4.29k
        zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable), Z_STRLEN_P(callable));
3020
4.29k
        func = zend_fetch_function(lmname);
3021
4.29k
        ZSTR_ALLOCA_FREE(lmname, use_heap);
3022
4.29k
      }
3023
8.94k
    }
3024
9.26k
    if (EXPECTED(func != NULL)) {
3025
6.27k
      fcc->function_handler = func;
3026
6.27k
      return 1;
3027
6.27k
    }
3028
37.7k
  }
3029
3030
  /* Split name into class/namespace and method/function names */
3031
37.7k
  if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL &&
3032
37.7k
    colon > Z_STRVAL_P(callable) &&
3033
2.33k
    *(colon-1) == ':'
3034
2.26k
  ) {
3035
2.26k
    size_t mlen;
3036
3037
2.26k
    colon--;
3038
2.26k
    clen = colon - Z_STRVAL_P(callable);
3039
2.26k
    mlen = Z_STRLEN_P(callable) - clen - 2;
3040
3041
2.26k
    if (colon == Z_STRVAL_P(callable)) {
3042
1
      if (error) *error = estrdup("invalid function name");
3043
1
      return 0;
3044
1
    }
3045
3046
    /* This is a compound name.
3047
     * Try to fetch class and then find static method. */
3048
2.26k
    if (ce_org) {
3049
495
      scope = ce_org;
3050
1.77k
    } else {
3051
1.77k
      scope = get_scope(frame);
3052
1.77k
    }
3053
3054
2.26k
    cname = zend_string_init(Z_STRVAL_P(callable), clen, 0);
3055
2.26k
    if (!zend_is_callable_check_class(cname, scope, frame, fcc, &strict_class, error)) {
3056
451
      zend_string_release_ex(cname, 0);
3057
451
      return 0;
3058
451
    }
3059
1.81k
    zend_string_release_ex(cname, 0);
3060
3061
1.81k
    ftable = &fcc->calling_scope->function_table;
3062
1.81k
    if (ce_org && !instanceof_function(ce_org, fcc->calling_scope)) {
3063
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));
3064
0
      return 0;
3065
0
    }
3066
1.81k
    mname = zend_string_init(Z_STRVAL_P(callable) + clen + 2, mlen, 0);
3067
35.4k
  } else if (ce_org) {
3068
    /* Try to fetch find static method of given class. */
3069
34.2k
    mname = Z_STR_P(callable);
3070
34.2k
    zend_string_addref(mname);
3071
34.2k
    ftable = &ce_org->function_table;
3072
34.2k
    fcc->calling_scope = ce_org;
3073
1.21k
  } else {
3074
    /* We already checked for plain function before. */
3075
1.21k
    if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
3076
858
      zend_spprintf(error, 0, "function \"%s\" not found or invalid function name", Z_STRVAL_P(callable));
3077
858
    }
3078
1.21k
    return 0;
3079
1.21k
  }
3080
3081
36.0k
  lmname = zend_string_tolower(mname);
3082
36.0k
  if (strict_class &&
3083
4.49k
      fcc->calling_scope &&
3084
4.49k
    zend_string_equals_literal(lmname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
3085
0
    fcc->function_handler = fcc->calling_scope->constructor;
3086
0
    if (fcc->function_handler) {
3087
0
      retval = 1;
3088
0
    }
3089
36.0k
  } else if ((zv = zend_hash_find(ftable, lmname)) != NULL) {
3090
31.4k
    fcc->function_handler = Z_PTR_P(zv);
3091
31.4k
    retval = 1;
3092
31.4k
    if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) &&
3093
150
        !strict_class) {
3094
106
      scope = get_scope(frame);
3095
106
      if (scope &&
3096
106
          instanceof_function(fcc->function_handler->common.scope, scope)) {
3097
3098
106
        zv = zend_hash_find(&scope->function_table, lmname);
3099
106
        if (zv != NULL) {
3100
106
          zend_function *priv_fbc = Z_PTR_P(zv);
3101
3102
106
          if ((priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE)
3103
106
           && priv_fbc->common.scope == scope) {
3104
106
            fcc->function_handler = priv_fbc;
3105
106
          }
3106
106
        }
3107
106
      }
3108
106
    }
3109
31.4k
    if (!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC) &&
3110
2.90k
        (fcc->calling_scope &&
3111
2.90k
         ((fcc->object && fcc->calling_scope->__call) ||
3112
2.73k
          (!fcc->object && fcc->calling_scope->__callstatic)))) {
3113
175
      scope = get_scope(frame);
3114
175
      if (fcc->function_handler->common.scope != scope) {
3115
109
        if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
3116
84
         || !zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope)) {
3117
52
          retval = 0;
3118
52
          fcc->function_handler = NULL;
3119
52
          goto get_function_via_handler;
3120
52
        }
3121
4.67k
      }
3122
175
    }
3123
4.67k
  } else {
3124
4.72k
get_function_via_handler:
3125
4.72k
    if (fcc->object && fcc->calling_scope == ce_org) {
3126
3.77k
      if (strict_class && ce_org->__call) {
3127
81
        fcc->function_handler = zend_get_call_trampoline_func(ce_org, mname, 0);
3128
81
        call_via_handler = 1;
3129
81
        retval = 1;
3130
3.69k
      } else {
3131
3.69k
        fcc->function_handler = fcc->object->handlers->get_method(&fcc->object, mname, NULL);
3132
3.69k
        if (fcc->function_handler) {
3133
2.59k
          if (strict_class &&
3134
36
              (!fcc->function_handler->common.scope ||
3135
36
               !instanceof_function(ce_org, fcc->function_handler->common.scope))) {
3136
36
            zend_release_fcall_info_cache(fcc);
3137
2.55k
          } else {
3138
2.55k
            retval = 1;
3139
2.55k
            call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3140
2.55k
          }
3141
2.59k
        }
3142
3.69k
      }
3143
945
    } else if (fcc->calling_scope) {
3144
945
      if (fcc->calling_scope->get_static_method) {
3145
0
        fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname);
3146
945
      } else {
3147
945
        fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, NULL);
3148
945
      }
3149
945
      if (fcc->function_handler) {
3150
647
        retval = 1;
3151
647
        call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3152
647
        if (call_via_handler && !fcc->object) {
3153
520
          zend_object *object = zend_get_this_object(frame);
3154
520
          if (object &&
3155
76
              instanceof_function(object->ce, fcc->calling_scope)) {
3156
76
            fcc->object = object;
3157
76
          }
3158
520
        }
3159
647
      }
3160
945
    }
3161
4.72k
  }
3162
3163
36.0k
  if (retval) {
3164
34.6k
    if (fcc->calling_scope && !call_via_handler) {
3165
31.3k
      if (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT) {
3166
347
        retval = 0;
3167
347
        if (error) {
3168
323
          zend_spprintf(error, 0, "cannot call abstract method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
3169
323
        }
3170
31.0k
      } else if (!fcc->object && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
3171
78
        retval = 0;
3172
78
        if (error) {
3173
42
          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));
3174
42
        }
3175
78
      }
3176
31.3k
      if (retval
3177
30.9k
       && !(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC)) {
3178
2.83k
        scope = get_scope(frame);
3179
2.83k
        if (fcc->function_handler->common.scope != scope) {
3180
1.50k
          if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
3181
1.16k
           || (!zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope))) {
3182
1.16k
            if (error) {
3183
1.09k
              if (*error) {
3184
0
                efree(*error);
3185
0
              }
3186
1.09k
              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));
3187
1.09k
            }
3188
1.16k
            retval = 0;
3189
1.16k
          }
3190
1.50k
        }
3191
2.83k
      }
3192
31.3k
    }
3193
1.44k
  } else if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
3194
653
    if (fcc->calling_scope) {
3195
653
      if (error) zend_spprintf(error, 0, "class %s does not have a method \"%s\"", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(mname));
3196
0
    } else {
3197
0
      if (error) zend_spprintf(error, 0, "function %s() does not exist", ZSTR_VAL(mname));
3198
0
    }
3199
653
  }
3200
36.0k
  zend_string_release_ex(lmname, 0);
3201
36.0k
  zend_string_release_ex(mname, 0);
3202
3203
36.0k
  if (fcc->object) {
3204
33.1k
    fcc->called_scope = fcc->object->ce;
3205
33.1k
    if (fcc->function_handler
3206
31.9k
     && (fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
3207
872
      fcc->object = NULL;
3208
872
    }
3209
33.1k
  }
3210
36.0k
  return retval;
3211
36.0k
}
3212
/* }}} */
3213
3214
ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *object) /* {{{ */
3215
1.80k
{
3216
1.80k
try_again:
3217
1.80k
  switch (Z_TYPE_P(callable)) {
3218
74
    case IS_STRING:
3219
74
      if (object) {
3220
7
        return zend_create_member_string(object->ce->name, Z_STR_P(callable));
3221
7
      }
3222
67
      return zend_string_copy(Z_STR_P(callable));
3223
3224
788
    case IS_ARRAY:
3225
788
    {
3226
788
      zval *method = NULL;
3227
788
      zval *obj = NULL;
3228
3229
788
      if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
3230
780
        obj = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 0);
3231
780
        method = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 1);
3232
780
      }
3233
3234
788
      if (obj == NULL || method == NULL || Z_TYPE_P(method) != IS_STRING) {
3235
8
        return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
3236
8
      }
3237
3238
780
      if (Z_TYPE_P(obj) == IS_STRING) {
3239
30
        return zend_create_member_string(Z_STR_P(obj), Z_STR_P(method));
3240
750
      } else if (Z_TYPE_P(obj) == IS_OBJECT) {
3241
619
        return zend_create_member_string(Z_OBJCE_P(obj)->name, Z_STR_P(method));
3242
131
      } else {
3243
131
        return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
3244
131
      }
3245
0
    }
3246
811
    case IS_OBJECT:
3247
811
    {
3248
811
      zend_class_entry *ce = Z_OBJCE_P(callable);
3249
811
      return zend_string_concat2(
3250
811
        ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
3251
811
        "::__invoke", sizeof("::__invoke") - 1);
3252
0
    }
3253
0
    case IS_REFERENCE:
3254
0
      callable = Z_REFVAL_P(callable);
3255
0
      goto try_again;
3256
136
    default:
3257
136
      return zval_get_string_func(callable);
3258
1.80k
  }
3259
1.80k
}
3260
/* }}} */
3261
3262
ZEND_API zend_string *zend_get_callable_name(zval *callable) /* {{{ */
3263
123
{
3264
123
  return zend_get_callable_name_ex(callable, NULL);
3265
123
}
3266
/* }}} */
3267
3268
static zend_always_inline zend_bool zend_is_callable_impl(
3269
    zval *callable, zend_object *object, zend_execute_data *frame,
3270
    uint32_t check_flags, zend_fcall_info_cache *fcc, char **error) /* {{{ */
3271
97.8k
{
3272
97.8k
  zend_bool ret;
3273
97.8k
  zend_fcall_info_cache fcc_local;
3274
97.8k
  bool strict_class = 0;
3275
3276
97.8k
  if (fcc == NULL) {
3277
29.3k
    fcc = &fcc_local;
3278
29.3k
  }
3279
97.8k
  if (error) {
3280
74.2k
    *error = NULL;
3281
74.2k
  }
3282
3283
97.8k
  fcc->calling_scope = NULL;
3284
97.8k
  fcc->called_scope = NULL;
3285
97.8k
  fcc->function_handler = NULL;
3286
97.8k
  fcc->object = NULL;
3287
3288
97.8k
again:
3289
97.8k
  switch (Z_TYPE_P(callable)) {
3290
33.7k
    case IS_STRING:
3291
33.7k
      if (object) {
3292
24.5k
        fcc->object = object;
3293
24.5k
        fcc->calling_scope = object->ce;
3294
24.5k
      }
3295
3296
33.7k
      if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3297
0
        fcc->called_scope = fcc->calling_scope;
3298
0
        return 1;
3299
0
      }
3300
3301
44.0k
check_func:
3302
44.0k
      ret = zend_is_callable_check_func(check_flags, callable, frame, fcc, strict_class, error);
3303
44.0k
      if (fcc == &fcc_local) {
3304
8.63k
        zend_release_fcall_info_cache(fcc);
3305
8.63k
      }
3306
44.0k
      return ret;
3307
3308
11.5k
    case IS_ARRAY:
3309
11.5k
      {
3310
11.5k
        zval *method = NULL;
3311
11.5k
        zval *obj = NULL;
3312
3313
11.5k
        if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
3314
11.4k
          obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0);
3315
11.4k
          method = zend_hash_index_find(Z_ARRVAL_P(callable), 1);
3316
11.4k
        }
3317
3318
11.5k
        do {
3319
11.5k
          if (obj == NULL || method == NULL) {
3320
58
            break;
3321
58
          }
3322
3323
11.4k
          ZVAL_DEREF(method);
3324
11.4k
          if (Z_TYPE_P(method) != IS_STRING) {
3325
55
            break;
3326
55
          }
3327
3328
11.4k
          ZVAL_DEREF(obj);
3329
11.4k
          if (Z_TYPE_P(obj) == IS_STRING) {
3330
3.60k
            if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3331
0
              return 1;
3332
0
            }
3333
3334
3.60k
            if (!zend_is_callable_check_class(Z_STR_P(obj), get_scope(frame), frame, fcc, &strict_class, error)) {
3335
505
              return 0;
3336
505
            }
3337
3338
7.80k
          } else if (Z_TYPE_P(obj) == IS_OBJECT) {
3339
3340
7.46k
            fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */
3341
3342
7.46k
            fcc->object = Z_OBJ_P(obj);
3343
3344
7.46k
            if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3345
331
              fcc->called_scope = fcc->calling_scope;
3346
331
              return 1;
3347
331
            }
3348
338
          } else {
3349
338
            break;
3350
338
          }
3351
3352
10.2k
          callable = method;
3353
10.2k
          goto check_func;
3354
3355
0
        } while (0);
3356
451
        if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
3357
393
          if (!obj || (!Z_ISREF_P(obj)?
3358
393
                (Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT) :
3359
338
                (Z_TYPE_P(Z_REFVAL_P(obj)) != IS_STRING && Z_TYPE_P(Z_REFVAL_P(obj)) != IS_OBJECT))) {
3360
338
            if (error) *error = estrdup("first array member is not a valid class name or object");
3361
55
          } else {
3362
55
            if (error) *error = estrdup("second array member is not a valid method");
3363
55
          }
3364
58
        } else {
3365
58
          if (error) *error = estrdup("array must have exactly two members");
3366
58
        }
3367
451
      }
3368
451
      return 0;
3369
51.9k
    case IS_OBJECT:
3370
51.9k
      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) {
3371
51.8k
        fcc->called_scope = fcc->calling_scope;
3372
51.8k
        if (fcc == &fcc_local) {
3373
19.4k
          zend_release_fcall_info_cache(fcc);
3374
19.4k
        }
3375
51.8k
        return 1;
3376
51.8k
      }
3377
102
      if (error) *error = estrdup("no array or string given");
3378
102
      return 0;
3379
18
    case IS_REFERENCE:
3380
18
      callable = Z_REFVAL_P(callable);
3381
18
      goto again;
3382
561
    default:
3383
561
      if (error) *error = estrdup("no array or string given");
3384
561
      return 0;
3385
97.8k
  }
3386
97.8k
}
3387
/* }}} */
3388
3389
ZEND_API zend_bool zend_is_callable_ex(zval *callable, zend_object *object, uint32_t check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error) /* {{{ */
3390
97.8k
{
3391
  /* Determine callability at the first parent user frame. */
3392
97.8k
  zend_execute_data *frame = EG(current_execute_data);
3393
191k
  while (frame && (!frame->func || !ZEND_USER_CODE(frame->func->type))) {
3394
93.5k
    frame = frame->prev_execute_data;
3395
93.5k
  }
3396
3397
97.8k
  zend_bool ret = zend_is_callable_impl(callable, object, frame, check_flags, fcc, error);
3398
97.8k
  if (callable_name) {
3399
1.67k
    *callable_name = zend_get_callable_name_ex(callable, object);
3400
1.67k
  }
3401
97.8k
  return ret;
3402
97.8k
}
3403
3404
ZEND_API zend_bool zend_is_callable(zval *callable, uint32_t check_flags, zend_string **callable_name) /* {{{ */
3405
23.6k
{
3406
23.6k
  return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL);
3407
23.6k
}
3408
/* }}} */
3409
3410
ZEND_API zend_bool zend_make_callable(zval *callable, zend_string **callable_name) /* {{{ */
3411
0
{
3412
0
  zend_fcall_info_cache fcc;
3413
3414
0
  if (zend_is_callable_ex(callable, NULL, 0, callable_name, &fcc, NULL)) {
3415
0
    if (Z_TYPE_P(callable) == IS_STRING && fcc.calling_scope) {
3416
0
      zval_ptr_dtor_str(callable);
3417
0
      array_init(callable);
3418
0
      add_next_index_str(callable, zend_string_copy(fcc.calling_scope->name));
3419
0
      add_next_index_str(callable, zend_string_copy(fcc.function_handler->common.function_name));
3420
0
    }
3421
0
    zend_release_fcall_info_cache(&fcc);
3422
0
    return 1;
3423
0
  }
3424
0
  return 0;
3425
0
}
3426
/* }}} */
3427
3428
ZEND_API zend_result zend_fcall_info_init(zval *callable, uint32_t check_flags, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_string **callable_name, char **error) /* {{{ */
3429
18.1k
{
3430
18.1k
  if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, fcc, error)) {
3431
512
    return FAILURE;
3432
512
  }
3433
3434
17.6k
  fci->size = sizeof(*fci);
3435
17.6k
  fci->object = fcc->object;
3436
17.6k
  ZVAL_COPY_VALUE(&fci->function_name, callable);
3437
17.6k
  fci->retval = NULL;
3438
17.6k
  fci->param_count = 0;
3439
17.6k
  fci->params = NULL;
3440
17.6k
  fci->named_params = NULL;
3441
3442
17.6k
  return SUCCESS;
3443
17.6k
}
3444
/* }}} */
3445
3446
ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, bool free_mem) /* {{{ */
3447
0
{
3448
0
  if (fci->params) {
3449
0
    zval *p = fci->params;
3450
0
    zval *end = p + fci->param_count;
3451
3452
0
    while (p != end) {
3453
0
      i_zval_ptr_dtor(p);
3454
0
      p++;
3455
0
    }
3456
0
    if (free_mem) {
3457
0
      efree(fci->params);
3458
0
      fci->params = NULL;
3459
0
    }
3460
0
  }
3461
0
  fci->param_count = 0;
3462
0
}
3463
/* }}} */
3464
3465
ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, uint32_t *param_count, zval **params) /* {{{ */
3466
0
{
3467
0
  *param_count = fci->param_count;
3468
0
  *params = fci->params;
3469
0
  fci->param_count = 0;
3470
0
  fci->params = NULL;
3471
0
}
3472
/* }}} */
3473
3474
ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, uint32_t param_count, zval *params) /* {{{ */
3475
0
{
3476
0
  zend_fcall_info_args_clear(fci, 1);
3477
0
  fci->param_count = param_count;
3478
0
  fci->params = params;
3479
0
}
3480
/* }}} */
3481
3482
ZEND_API zend_result zend_fcall_info_args_ex(zend_fcall_info *fci, zend_function *func, zval *args) /* {{{ */
3483
0
{
3484
0
  zval *arg, *params;
3485
0
  uint32_t n = 1;
3486
3487
0
  zend_fcall_info_args_clear(fci, !args);
3488
3489
0
  if (!args) {
3490
0
    return SUCCESS;
3491
0
  }
3492
3493
0
  if (Z_TYPE_P(args) != IS_ARRAY) {
3494
0
    return FAILURE;
3495
0
  }
3496
3497
0
  fci->param_count = zend_hash_num_elements(Z_ARRVAL_P(args));
3498
0
  fci->params = params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
3499
3500
0
  ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), arg) {
3501
0
    if (func && !Z_ISREF_P(arg) && ARG_SHOULD_BE_SENT_BY_REF(func, n)) {
3502
0
      ZVAL_NEW_REF(params, arg);
3503
0
      Z_TRY_ADDREF_P(arg);
3504
0
    } else {
3505
0
      ZVAL_COPY(params, arg);
3506
0
    }
3507
0
    params++;
3508
0
    n++;
3509
0
  } ZEND_HASH_FOREACH_END();
3510
3511
0
  return SUCCESS;
3512
0
}
3513
/* }}} */
3514
3515
ZEND_API zend_result zend_fcall_info_args(zend_fcall_info *fci, zval *args) /* {{{ */
3516
0
{
3517
0
  return zend_fcall_info_args_ex(fci, NULL, args);
3518
0
}
3519
/* }}} */
3520
3521
ZEND_API void zend_fcall_info_argp(zend_fcall_info *fci, uint32_t argc, zval *argv) /* {{{ */
3522
0
{
3523
0
  zend_fcall_info_args_clear(fci, !argc);
3524
3525
0
  if (argc) {
3526
0
    fci->param_count = argc;
3527
0
    fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
3528
3529
0
    for (uint32_t i = 0; i < argc; ++i) {
3530
0
      ZVAL_COPY(&fci->params[i], &argv[i]);
3531
0
    }
3532
0
  }
3533
0
}
3534
/* }}} */
3535
3536
ZEND_API void zend_fcall_info_argv(zend_fcall_info *fci, uint32_t argc, va_list *argv) /* {{{ */
3537
0
{
3538
0
  zend_fcall_info_args_clear(fci, !argc);
3539
3540
0
  if (argc) {
3541
0
    zval *arg;
3542
0
    fci->param_count = argc;
3543
0
    fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
3544
3545
0
    for (uint32_t i = 0; i < argc; ++i) {
3546
0
      arg = va_arg(*argv, zval *);
3547
0
      ZVAL_COPY(&fci->params[i], arg);
3548
0
    }
3549
0
  }
3550
0
}
3551
/* }}} */
3552
3553
ZEND_API void zend_fcall_info_argn(zend_fcall_info *fci, uint32_t argc, ...) /* {{{ */
3554
0
{
3555
0
  va_list argv;
3556
3557
0
  va_start(argv, argc);
3558
0
  zend_fcall_info_argv(fci, argc, &argv);
3559
0
  va_end(argv);
3560
0
}
3561
/* }}} */
3562
3563
ZEND_API zend_result zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval *retval_ptr, zval *args) /* {{{ */
3564
0
{
3565
0
  zval retval, *org_params = NULL;
3566
0
  uint32_t org_count = 0;
3567
0
  zend_result result;
3568
3569
0
  fci->retval = retval_ptr ? retval_ptr : &retval;
3570
0
  if (args) {
3571
0
    zend_fcall_info_args_save(fci, &org_count, &org_params);
3572
0
    zend_fcall_info_args(fci, args);
3573
0
  }
3574
0
  result = zend_call_function(fci, fcc);
3575
3576
0
  if (!retval_ptr && Z_TYPE(retval) != IS_UNDEF) {
3577
0
    zval_ptr_dtor(&retval);
3578
0
  }
3579
0
  if (args) {
3580
0
    zend_fcall_info_args_restore(fci, org_count, org_params);
3581
0
  }
3582
0
  return result;
3583
0
}
3584
/* }}} */
3585
3586
ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */
3587
0
{
3588
0
  zend_string *lname;
3589
0
  size_t name_len = strlen(module_name);
3590
0
  zend_module_entry *module;
3591
3592
0
  lname = zend_string_alloc(name_len, 0);
3593
0
  zend_str_tolower_copy(ZSTR_VAL(lname), module_name, name_len);
3594
0
  module = zend_hash_find_ptr(&module_registry, lname);
3595
0
  zend_string_efree(lname);
3596
0
  return module ? module->version : NULL;
3597
0
}
3598
/* }}} */
3599
3600
static inline zend_string *zval_make_interned_string(zval *zv) /* {{{ */
3601
221k
{
3602
221k
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
3603
221k
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
3604
221k
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
3605
221k
    Z_TYPE_FLAGS_P(zv) = 0;
3606
221k
  }
3607
221k
  return Z_STR_P(zv);
3608
221k
}
3609
3610
472k
static zend_always_inline zend_bool is_persistent_class(zend_class_entry *ce) {
3611
472k
  return (ce->type & ZEND_INTERNAL_CLASS)
3612
452k
    && ce->info.internal.module->type == MODULE_PERSISTENT;
3613
472k
}
3614
3615
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) /* {{{ */
3616
276k
{
3617
276k
  zend_property_info *property_info, *property_info_ptr;
3618
3619
276k
  if (ZEND_TYPE_IS_SET(type)) {
3620
61.0k
    ce->ce_flags |= ZEND_ACC_HAS_TYPE_HINTS;
3621
61.0k
  }
3622
3623
276k
  if (ce->type == ZEND_INTERNAL_CLASS) {
3624
194k
    property_info = pemalloc(sizeof(zend_property_info), 1);
3625
81.9k
  } else {
3626
81.9k
    property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
3627
81.9k
    if (Z_TYPE_P(property) == IS_CONSTANT_AST) {
3628
6.67k
      ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
3629
6.67k
    }
3630
81.9k
  }
3631
3632
276k
  if (Z_TYPE_P(property) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(property))) {
3633
118k
    zval_make_interned_string(property);
3634
118k
  }
3635
3636
276k
  if (!(access_type & ZEND_ACC_PPP_MASK)) {
3637
18.5k
    access_type |= ZEND_ACC_PUBLIC;
3638
18.5k
  }
3639
276k
  if (access_type & ZEND_ACC_STATIC) {
3640
16.9k
    if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
3641
0
        (property_info_ptr->flags & ZEND_ACC_STATIC) != 0) {
3642
0
      property_info->offset = property_info_ptr->offset;
3643
0
      zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]);
3644
0
      zend_hash_del(&ce->properties_info, name);
3645
16.9k
    } else {
3646
16.9k
      property_info->offset = ce->default_static_members_count++;
3647
16.9k
      ce->default_static_members_table = perealloc(ce->default_static_members_table, sizeof(zval) * ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS);
3648
16.9k
    }
3649
16.9k
    ZVAL_COPY_VALUE(&ce->default_static_members_table[property_info->offset], property);
3650
16.9k
    if (!ZEND_MAP_PTR(ce->static_members_table)) {
3651
0
      ZEND_ASSERT(ce->type == ZEND_INTERNAL_CLASS);
3652
0
      if (!EG(current_execute_data)) {
3653
0
        ZEND_MAP_PTR_NEW(ce->static_members_table);
3654
0
      } else {
3655
        /* internal class loaded by dl() */
3656
0
        ZEND_MAP_PTR_INIT(ce->static_members_table, &ce->default_static_members_table);
3657
0
      }
3658
0
    }
3659
259k
  } else {
3660
259k
    zval *property_default_ptr;
3661
259k
    if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
3662
12.5k
        (property_info_ptr->flags & ZEND_ACC_STATIC) == 0) {
3663
12.5k
      property_info->offset = property_info_ptr->offset;
3664
12.5k
      zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]);
3665
12.5k
      zend_hash_del(&ce->properties_info, name);
3666
3667
12.5k
      ZEND_ASSERT(ce->type == ZEND_INTERNAL_CLASS);
3668
12.5k
      ZEND_ASSERT(ce->properties_info_table != NULL);
3669
12.5k
      ce->properties_info_table[OBJ_PROP_TO_NUM(property_info->offset)] = property_info;
3670
247k
    } else {
3671
247k
      property_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count);
3672
247k
      ce->default_properties_count++;
3673
247k
      ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
3674
3675
      /* For user classes this is handled during linking */
3676
247k
      if (ce->type == ZEND_INTERNAL_CLASS) {
3677
182k
        ce->properties_info_table = perealloc(ce->properties_info_table, sizeof(zend_property_info *) * ce->default_properties_count, 1);
3678
182k
        ce->properties_info_table[ce->default_properties_count - 1] = property_info;
3679
182k
      }
3680
247k
    }
3681
259k
    property_default_ptr = &ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
3682
259k
    ZVAL_COPY_VALUE(property_default_ptr, property);
3683
259k
    Z_PROP_FLAG_P(property_default_ptr) = Z_ISUNDEF_P(property) ? IS_PROP_UNINIT : 0;
3684
259k
  }
3685
276k
  if (ce->type & ZEND_INTERNAL_CLASS) {
3686
    /* Must be interned to avoid ZTS data races */
3687
194k
    if (is_persistent_class(ce)) {
3688
194k
      name = zend_new_interned_string(zend_string_copy(name));
3689
194k
    }
3690
3691
194k
    if (Z_REFCOUNTED_P(property)) {
3692
0
      zend_error_noreturn(E_CORE_ERROR, "Internal zvals cannot be refcounted");
3693
0
    }
3694
276k
  }
3695
3696
276k
  if (access_type & ZEND_ACC_PUBLIC) {
3697
162k
    property_info->name = zend_string_copy(name);
3698
114k
  } else if (access_type & ZEND_ACC_PRIVATE) {
3699
52.2k
    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));
3700
62.3k
  } else {
3701
62.3k
    ZEND_ASSERT(access_type & ZEND_ACC_PROTECTED);
3702
62.3k
    property_info->name = zend_mangle_property_name("*", 1, ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
3703
62.3k
  }
3704
3705
276k
  property_info->name = zend_new_interned_string(property_info->name);
3706
276k
  property_info->flags = access_type;
3707
276k
  property_info->doc_comment = doc_comment;
3708
276k
  property_info->attributes = NULL;
3709
276k
  property_info->ce = ce;
3710
276k
  property_info->type = type;
3711
3712
276k
  zend_hash_update_ptr(&ce->properties_info, name, property_info);
3713
3714
276k
  return property_info;
3715
276k
}
3716
/* }}} */
3717
3718
ZEND_API zend_result zend_try_assign_typed_ref_ex(zend_reference *ref, zval *val, zend_bool strict) /* {{{ */
3719
54
{
3720
54
  if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, val, strict))) {
3721
0
    zval_ptr_dtor(val);
3722
0
    return FAILURE;
3723
54
  } else {
3724
54
    zval_ptr_dtor(&ref->val);
3725
54
    ZVAL_COPY_VALUE(&ref->val, val);
3726
54
    return SUCCESS;
3727
54
  }
3728
54
}
3729
/* }}} */
3730
3731
ZEND_API zend_result zend_try_assign_typed_ref(zend_reference *ref, zval *val) /* {{{ */
3732
54
{
3733
54
  return zend_try_assign_typed_ref_ex(ref, val, ZEND_ARG_USES_STRICT_TYPES());
3734
54
}
3735
/* }}} */
3736
3737
ZEND_API zend_result zend_try_assign_typed_ref_null(zend_reference *ref) /* {{{ */
3738
0
{
3739
0
  zval tmp;
3740
3741
0
  ZVAL_NULL(&tmp);
3742
0
  return zend_try_assign_typed_ref(ref, &tmp);
3743
0
}
3744
/* }}} */
3745
3746
ZEND_API zend_result zend_try_assign_typed_ref_bool(zend_reference *ref, zend_bool val) /* {{{ */
3747
0
{
3748
0
  zval tmp;
3749
3750
0
  ZVAL_BOOL(&tmp, val);
3751
0
  return zend_try_assign_typed_ref(ref, &tmp);
3752
0
}
3753
/* }}} */
3754
3755
ZEND_API zend_result zend_try_assign_typed_ref_long(zend_reference *ref, zend_long lval) /* {{{ */
3756
0
{
3757
0
  zval tmp;
3758
3759
0
  ZVAL_LONG(&tmp, lval);
3760
0
  return zend_try_assign_typed_ref(ref, &tmp);
3761
0
}
3762
/* }}} */
3763
3764
ZEND_API zend_result zend_try_assign_typed_ref_double(zend_reference *ref, double dval) /* {{{ */
3765
0
{
3766
0
  zval tmp;
3767
3768
0
  ZVAL_DOUBLE(&tmp, dval);
3769
0
  return zend_try_assign_typed_ref(ref, &tmp);
3770
0
}
3771
/* }}} */
3772
3773
ZEND_API zend_result zend_try_assign_typed_ref_empty_string(zend_reference *ref) /* {{{ */
3774
0
{
3775
0
  zval tmp;
3776
3777
0
  ZVAL_EMPTY_STRING(&tmp);
3778
0
  return zend_try_assign_typed_ref(ref, &tmp);
3779
0
}
3780
/* }}} */
3781
3782
ZEND_API zend_result zend_try_assign_typed_ref_str(zend_reference *ref, zend_string *str) /* {{{ */
3783
0
{
3784
0
  zval tmp;
3785
3786
0
  ZVAL_STR(&tmp, str);
3787
0
  return zend_try_assign_typed_ref(ref, &tmp);
3788
0
}
3789
/* }}} */
3790
3791
ZEND_API zend_result zend_try_assign_typed_ref_string(zend_reference *ref, const char *string) /* {{{ */
3792
0
{
3793
0
  zval tmp;
3794
3795
0
  ZVAL_STRING(&tmp, string);
3796
0
  return zend_try_assign_typed_ref(ref, &tmp);
3797
0
}
3798
/* }}} */
3799
3800
ZEND_API zend_result zend_try_assign_typed_ref_stringl(zend_reference *ref, const char *string, size_t len) /* {{{ */
3801
0
{
3802
0
  zval tmp;
3803
3804
0
  ZVAL_STRINGL(&tmp, string, len);
3805
0
  return zend_try_assign_typed_ref(ref, &tmp);
3806
0
}
3807
/* }}} */
3808
3809
ZEND_API zend_result zend_try_assign_typed_ref_arr(zend_reference *ref, zend_array *arr) /* {{{ */
3810
54
{
3811
54
  zval tmp;
3812
3813
54
  ZVAL_ARR(&tmp, arr);
3814
54
  return zend_try_assign_typed_ref(ref, &tmp);
3815
54
}
3816
/* }}} */
3817
3818
ZEND_API zend_result zend_try_assign_typed_ref_res(zend_reference *ref, zend_resource *res) /* {{{ */
3819
0
{
3820
0
  zval tmp;
3821
3822
0
  ZVAL_RES(&tmp, res);
3823
0
  return zend_try_assign_typed_ref(ref, &tmp);
3824
0
}
3825
/* }}} */
3826
3827
ZEND_API zend_result zend_try_assign_typed_ref_zval(zend_reference *ref, zval *zv) /* {{{ */
3828
0
{
3829
0
  zval tmp;
3830
3831
0
  ZVAL_COPY_VALUE(&tmp, zv);
3832
0
  return zend_try_assign_typed_ref(ref, &tmp);
3833
0
}
3834
/* }}} */
3835
3836
ZEND_API zend_result zend_try_assign_typed_ref_zval_ex(zend_reference *ref, zval *zv, zend_bool strict) /* {{{ */
3837
0
{
3838
0
  zval tmp;
3839
3840
0
  ZVAL_COPY_VALUE(&tmp, zv);
3841
0
  return zend_try_assign_typed_ref_ex(ref, &tmp, strict);
3842
0
}
3843
/* }}} */
3844
3845
ZEND_API void zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */
3846
163k
{
3847
163k
  zend_declare_typed_property(ce, name, property, access_type, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
3848
163k
}
3849
/* }}} */
3850
3851
ZEND_API void zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type) /* {{{ */
3852
163k
{
3853
163k
  zend_string *key = zend_string_init(name, name_length, is_persistent_class(ce));
3854
163k
  zend_declare_property_ex(ce, key, property, access_type, NULL);
3855
163k
  zend_string_release(key);
3856
163k
}
3857
/* }}} */
3858
3859
ZEND_API void zend_declare_property_null(zend_class_entry *ce, const char *name, size_t name_length, int access_type) /* {{{ */
3860
31.3k
{
3861
31.3k
  zval property;
3862
3863
31.3k
  ZVAL_NULL(&property);
3864
31.3k
  zend_declare_property(ce, name, name_length, &property, access_type);
3865
31.3k
}
3866
/* }}} */
3867
3868
ZEND_API void zend_declare_property_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
3869
0
{
3870
0
  zval property;
3871
3872
0
  ZVAL_BOOL(&property, value);
3873
0
  zend_declare_property(ce, name, name_length, &property, access_type);
3874
0
}
3875
/* }}} */
3876
3877
ZEND_API void zend_declare_property_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
3878
18.8k
{
3879
18.8k
  zval property;
3880
3881
18.8k
  ZVAL_LONG(&property, value);
3882
18.8k
  zend_declare_property(ce, name, name_length, &property, access_type);
3883
18.8k
}
3884
/* }}} */
3885
3886
ZEND_API void zend_declare_property_double(zend_class_entry *ce, const char *name, size_t name_length, double value, int access_type) /* {{{ */
3887
0
{
3888
0
  zval property;
3889
3890
0
  ZVAL_DOUBLE(&property, value);
3891
0
  zend_declare_property(ce, name, name_length, &property, access_type);
3892
0
}
3893
/* }}} */
3894
3895
ZEND_API void zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type) /* {{{ */
3896
113k
{
3897
113k
  zval property;
3898
3899
113k
  ZVAL_NEW_STR(&property, zend_string_init(value, strlen(value), ce->type & ZEND_INTERNAL_CLASS));
3900
113k
  zend_declare_property(ce, name, name_length, &property, access_type);
3901
113k
}
3902
/* }}} */
3903
3904
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) /* {{{ */
3905
0
{
3906
0
  zval property;
3907
3908
0
  ZVAL_NEW_STR(&property, zend_string_init(value, value_len, ce->type & ZEND_INTERNAL_CLASS));
3909
0
  zend_declare_property(ce, name, name_length, &property, access_type);
3910
0
}
3911
/* }}} */
3912
3913
ZEND_API zend_class_constant *zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment) /* {{{ */
3914
723k
{
3915
723k
  zend_class_constant *c;
3916
3917
723k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
3918
81.9k
    if (access_type != ZEND_ACC_PUBLIC) {
3919
0
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface constant %s::%s must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
3920
0
    }
3921
723k
  }
3922
3923
723k
  if (zend_string_equals_literal_ci(name, "class")) {
3924
18
    zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
3925
18
        "A class constant must not be called 'class'; it is reserved for class name fetching");
3926
18
  }
3927
3928
723k
  if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) {
3929
102k
    zval_make_interned_string(value);
3930
102k
  }
3931
3932
723k
  if (ce->type == ZEND_INTERNAL_CLASS) {
3933
696k
    c = pemalloc(sizeof(zend_class_constant), 1);
3934
26.1k
  } else {
3935
26.1k
    c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
3936
26.1k
  }
3937
723k
  ZVAL_COPY_VALUE(&c->value, value);
3938
723k
  Z_ACCESS_FLAGS(c->value) = access_type;
3939
723k
  c->doc_comment = doc_comment;
3940
723k
  c->attributes = NULL;
3941
723k
  c->ce = ce;
3942
723k
  if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
3943
2.62k
    ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
3944
2.62k
  }
3945
3946
723k
  if (!zend_hash_add_ptr(&ce->constants_table, name, c)) {
3947
19
    zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
3948
19
      "Cannot redefine class constant %s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
3949
19
  }
3950
3951
723k
  return c;
3952
723k
}
3953
/* }}} */
3954
3955
ZEND_API void zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
3956
696k
{
3957
696k
  zend_string *key;
3958
3959
696k
  if (ce->type == ZEND_INTERNAL_CLASS) {
3960
696k
    key = zend_string_init_interned(name, name_length, 1);
3961
0
  } else {
3962
0
    key = zend_string_init(name, name_length, 0);
3963
0
  }
3964
696k
  zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
3965
696k
  zend_string_release(key);
3966
696k
}
3967
/* }}} */
3968
3969
ZEND_API void zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length) /* {{{ */
3970
0
{
3971
0
  zval constant;
3972
3973
0
  ZVAL_NULL(&constant);
3974
0
  zend_declare_class_constant(ce, name, name_length, &constant);
3975
0
}
3976
/* }}} */
3977
3978
ZEND_API void zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value) /* {{{ */
3979
615k
{
3980
615k
  zval constant;
3981
3982
615k
  ZVAL_LONG(&constant, value);
3983
615k
  zend_declare_class_constant(ce, name, name_length, &constant);
3984
615k
}
3985
/* }}} */
3986
3987
ZEND_API void zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_bool value) /* {{{ */
3988
0
{
3989
0
  zval constant;
3990
3991
0
  ZVAL_BOOL(&constant, value);
3992
0
  zend_declare_class_constant(ce, name, name_length, &constant);
3993
0
}
3994
/* }}} */
3995
3996
ZEND_API void zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value) /* {{{ */
3997
0
{
3998
0
  zval constant;
3999
4000
0
  ZVAL_DOUBLE(&constant, value);
4001
0
  zend_declare_class_constant(ce, name, name_length, &constant);
4002
0
}
4003
/* }}} */
4004
4005
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) /* {{{ */
4006
81.6k
{
4007
81.6k
  zval constant;
4008
4009
81.6k
  ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type & ZEND_INTERNAL_CLASS));
4010
81.6k
  zend_declare_class_constant(ce, name, name_length, &constant);
4011
81.6k
}
4012
/* }}} */
4013
4014
ZEND_API void zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value) /* {{{ */
4015
0
{
4016
0
  zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value));
4017
0
}
4018
/* }}} */
4019
4020
ZEND_API void zend_update_property_ex(zend_class_entry *scope, zend_object *object, zend_string *name, zval *value) /* {{{ */
4021
4.08M
{
4022
4.08M
  zend_class_entry *old_scope = EG(fake_scope);
4023
4024
4.08M
  EG(fake_scope) = scope;
4025
4026
4.08M
  object->handlers->write_property(object, name, value, NULL);
4027
4028
4.08M
  EG(fake_scope) = old_scope;
4029
4.08M
}
4030
/* }}} */
4031
4032
ZEND_API void zend_update_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zval *value) /* {{{ */
4033
0
{
4034
0
  zend_string *property;
4035
0
  zend_class_entry *old_scope = EG(fake_scope);
4036
4037
0
  EG(fake_scope) = scope;
4038
4039
0
  property = zend_string_init(name, name_length, 0);
4040
0
  object->handlers->write_property(object, property, value, NULL);
4041
0
  zend_string_release_ex(property, 0);
4042
4043
0
  EG(fake_scope) = old_scope;
4044
0
}
4045
/* }}} */
4046
4047
ZEND_API void zend_update_property_null(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */
4048
0
{
4049
0
  zval tmp;
4050
4051
0
  ZVAL_NULL(&tmp);
4052
0
  zend_update_property(scope, object, name, name_length, &tmp);
4053
0
}
4054
/* }}} */
4055
4056
ZEND_API void zend_unset_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */
4057
2
{
4058
2
  zend_string *property;
4059
2
  zend_class_entry *old_scope = EG(fake_scope);
4060
4061
2
  EG(fake_scope) = scope;
4062
4063
2
  property = zend_string_init(name, name_length, 0);
4064
2
  object->handlers->unset_property(object, property, 0);
4065
2
  zend_string_release_ex(property, 0);
4066
4067
2
  EG(fake_scope) = old_scope;
4068
2
}
4069
/* }}} */
4070
4071
ZEND_API void zend_update_property_bool(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value) /* {{{ */
4072
0
{
4073
0
  zval tmp;
4074
4075
0
  ZVAL_BOOL(&tmp, value);
4076
0
  zend_update_property(scope, object, name, name_length, &tmp);
4077
0
}
4078
/* }}} */
4079
4080
ZEND_API void zend_update_property_long(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value) /* {{{ */
4081
0
{
4082
0
  zval tmp;
4083
4084
0
  ZVAL_LONG(&tmp, value);
4085
0
  zend_update_property(scope, object, name, name_length, &tmp);
4086
0
}
4087
/* }}} */
4088
4089
ZEND_API void zend_update_property_double(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, double value) /* {{{ */
4090
0
{
4091
0
  zval tmp;
4092
4093
0
  ZVAL_DOUBLE(&tmp, value);
4094
0
  zend_update_property(scope, object, name, name_length, &tmp);
4095
0
}
4096
/* }}} */
4097
4098
ZEND_API void zend_update_property_str(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_string *value) /* {{{ */
4099
0
{
4100
0
  zval tmp;
4101
4102
0
  ZVAL_STR(&tmp, value);
4103
0
  zend_update_property(scope, object, name, name_length, &tmp);
4104
0
}
4105
/* }}} */
4106
4107
ZEND_API void zend_update_property_string(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, const char *value) /* {{{ */
4108
0
{
4109
0
  zval tmp;
4110
4111
0
  ZVAL_STRING(&tmp, value);
4112
0
  Z_SET_REFCOUNT(tmp, 0);
4113
0
  zend_update_property(scope, object, name, name_length, &tmp);
4114
0
}
4115
/* }}} */
4116
4117
ZEND_API void zend_update_property_stringl(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */
4118
0
{
4119
0
  zval tmp;
4120
4121
0
  ZVAL_STRINGL(&tmp, value, value_len);
4122
0
  Z_SET_REFCOUNT(tmp, 0);
4123
0
  zend_update_property(scope, object, name, name_length, &tmp);
4124
0
}
4125
/* }}} */
4126
4127
ZEND_API zend_result zend_update_static_property_ex(zend_class_entry *scope, zend_string *name, zval *value) /* {{{ */
4128
37
{
4129
37
  zval *property, tmp;
4130
37
  zend_property_info *prop_info;
4131
37
  zend_class_entry *old_scope = EG(fake_scope);
4132
4133
37
  if (UNEXPECTED(!(scope->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
4134
37
    if (UNEXPECTED(zend_update_class_constants(scope)) != SUCCESS) {
4135
7
      return FAILURE;
4136
7
    }
4137
30
  }
4138
4139
30
  EG(fake_scope) = scope;
4140
30
  property = zend_std_get_static_property_with_info(scope, name, BP_VAR_W, &prop_info);
4141
30
  EG(fake_scope) = old_scope;
4142
4143
30
  if (!property) {
4144
0
    return FAILURE;
4145
0
  }
4146
4147
30
  ZEND_ASSERT(!Z_ISREF_P(value));
4148
30
  Z_TRY_ADDREF_P(value);
4149
30
  if (ZEND_TYPE_IS_SET(prop_info->type)) {
4150
0
    ZVAL_COPY_VALUE(&tmp, value);
4151
0
    if (!zend_verify_property_type(prop_info, &tmp, /* strict */ 0)) {
4152
0
      Z_TRY_DELREF_P(value);
4153
0
      return FAILURE;
4154
0
    }
4155
0
    value = &tmp;
4156
0
  }
4157
4158
30
  zend_assign_to_variable(property, value, IS_TMP_VAR, /* strict */ 0);
4159
30
  return SUCCESS;
4160
30
}
4161
/* }}} */
4162
4163
ZEND_API zend_result zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value) /* {{{ */
4164
0
{
4165
0
  zend_string *key = zend_string_init(name, name_length, 0);
4166
0
  bool retval = zend_update_static_property_ex(scope, key, value);
4167
0
  zend_string_efree(key);
4168
0
  return retval;
4169
0
}
4170
/* }}} */
4171
4172
ZEND_API zend_result zend_update_static_property_null(zend_class_entry *scope, const char *name, size_t name_length) /* {{{ */
4173
0
{
4174
0
  zval tmp;
4175
4176
0
  ZVAL_NULL(&tmp);
4177
0
  return zend_update_static_property(scope, name, name_length, &tmp);
4178
0
}
4179
/* }}} */
4180
4181
ZEND_API zend_result zend_update_static_property_bool(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
4182
0
{
4183
0
  zval tmp;
4184
4185
0
  ZVAL_BOOL(&tmp, value);
4186
0
  return zend_update_static_property(scope, name, name_length, &tmp);
4187
0
}
4188
/* }}} */
4189
4190
ZEND_API zend_result zend_update_static_property_long(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
4191
0
{
4192
0
  zval tmp;
4193
4194
0
  ZVAL_LONG(&tmp, value);
4195
0
  return zend_update_static_property(scope, name, name_length, &tmp);
4196
0
}
4197
/* }}} */
4198
4199
ZEND_API zend_result zend_update_static_property_double(zend_class_entry *scope, const char *name, size_t name_length, double value) /* {{{ */
4200
0
{
4201
0
  zval tmp;
4202
4203
0
  ZVAL_DOUBLE(&tmp, value);
4204
0
  return zend_update_static_property(scope, name, name_length, &tmp);
4205
0
}
4206
/* }}} */
4207
4208
ZEND_API zend_result zend_update_static_property_string(zend_class_entry *scope, const char *name, size_t name_length, const char *value) /* {{{ */
4209
0
{
4210
0
  zval tmp;
4211
4212
0
  ZVAL_STRING(&tmp, value);
4213
0
  Z_SET_REFCOUNT(tmp, 0);
4214
0
  return zend_update_static_property(scope, name, name_length, &tmp);
4215
0
}
4216
/* }}} */
4217
4218
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) /* {{{ */
4219
0
{
4220
0
  zval tmp;
4221
4222
0
  ZVAL_STRINGL(&tmp, value, value_len);
4223
0
  Z_SET_REFCOUNT(tmp, 0);
4224
0
  return zend_update_static_property(scope, name, name_length, &tmp);
4225
0
}
4226
/* }}} */
4227
4228
ZEND_API zval *zend_read_property_ex(zend_class_entry *scope, zend_object *object, zend_string *name, zend_bool silent, zval *rv) /* {{{ */
4229
11.2M
{
4230
11.2M
  zval *value;
4231
11.2M
  zend_class_entry *old_scope = EG(fake_scope);
4232
4233
11.2M
  EG(fake_scope) = scope;
4234
4235
11.0M
  value = object->handlers->read_property(object, name, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
4236
4237
11.2M
  EG(fake_scope) = old_scope;
4238
11.2M
  return value;
4239
11.2M
}
4240
/* }}} */
4241
4242
ZEND_API zval *zend_read_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_bool silent, zval *rv) /* {{{ */
4243
0
{
4244
0
  zval *value;
4245
0
  zend_string *str;
4246
4247
0
  str = zend_string_init(name, name_length, 0);
4248
0
  value = zend_read_property_ex(scope, object, str, silent, rv);
4249
0
  zend_string_release_ex(str, 0);
4250
0
  return value;
4251
0
}
4252
/* }}} */
4253
4254
ZEND_API zval *zend_read_static_property_ex(zend_class_entry *scope, zend_string *name, zend_bool silent) /* {{{ */
4255
49
{
4256
49
  zval *property;
4257
49
  zend_class_entry *old_scope = EG(fake_scope);
4258
4259
49
  EG(fake_scope) = scope;
4260
49
  property = zend_std_get_static_property(scope, name, silent ? BP_VAR_IS : BP_VAR_R);
4261
49
  EG(fake_scope) = old_scope;
4262
4263
49
  return property;
4264
49
}
4265
/* }}} */
4266
4267
ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, size_t name_length, zend_bool silent) /* {{{ */
4268
0
{
4269
0
  zend_string *key = zend_string_init(name, name_length, 0);
4270
0
  zval *property = zend_read_static_property_ex(scope, key, silent);
4271
0
  zend_string_efree(key);
4272
0
  return property;
4273
0
}
4274
/* }}} */
4275
4276
ZEND_API void zend_save_error_handling(zend_error_handling *current) /* {{{ */
4277
341k
{
4278
341k
  current->handling = EG(error_handling);
4279
341k
  current->exception = EG(exception_class);
4280
341k
  ZVAL_COPY(&current->user_handler, &EG(user_error_handler));
4281
341k
}
4282
/* }}} */
4283
4284
ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current) /* {{{ */
4285
341k
{
4286
341k
  if (current) {
4287
341k
    zend_save_error_handling(current);
4288
341k
    if (error_handling != EH_NORMAL && Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
4289
7
      zval_ptr_dtor(&EG(user_error_handler));
4290
7
      ZVAL_UNDEF(&EG(user_error_handler));
4291
7
    }
4292
341k
  }
4293
341k
  EG(error_handling) = error_handling;
4294
341k
  EG(exception_class) = error_handling == EH_THROW ? exception_class : NULL;
4295
341k
}
4296
/* }}} */
4297
4298
ZEND_API void zend_restore_error_handling(zend_error_handling *saved) /* {{{ */
4299
338k
{
4300
338k
  EG(error_handling) = saved->handling;
4301
338k
  EG(exception_class) = saved->handling == EH_THROW ? saved->exception : NULL;
4302
338k
  if (Z_TYPE(saved->user_handler) != IS_UNDEF) {
4303
7
    zval_ptr_dtor(&EG(user_error_handler));
4304
7
    ZVAL_COPY_VALUE(&EG(user_error_handler), &saved->user_handler);
4305
7
    ZVAL_UNDEF(&saved->user_handler);
4306
7
  }
4307
338k
}
4308
/* }}} */
4309
4310
ZEND_API ZEND_COLD const char *zend_get_object_type(const zend_class_entry *ce) /* {{{ */
4311
301
{
4312
301
  if(ce->ce_flags & ZEND_ACC_TRAIT) {
4313
54
    return "trait";
4314
247
  } else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
4315
75
    return "interface";
4316
172
  } else {
4317
172
    return "class";
4318
172
  }
4319
301
}
4320
/* }}} */
4321
4322
ZEND_API zend_bool zend_is_iterable(zval *iterable) /* {{{ */
4323
7.99k
{
4324
7.99k
  switch (Z_TYPE_P(iterable)) {
4325
3.21k
    case IS_ARRAY:
4326
3.21k
      return 1;
4327
4.28k
    case IS_OBJECT:
4328
4.28k
      return zend_class_implements_interface(Z_OBJCE_P(iterable), zend_ce_traversable);
4329
496
    default:
4330
496
      return 0;
4331
7.99k
  }
4332
7.99k
}
4333
/* }}} */
4334
4335
ZEND_API zend_bool zend_is_countable(zval *countable) /* {{{ */
4336
0
{
4337
0
  switch (Z_TYPE_P(countable)) {
4338
0
    case IS_ARRAY:
4339
0
      return 1;
4340
0
    case IS_OBJECT:
4341
0
      if (Z_OBJ_HT_P(countable)->count_elements) {
4342
0
        return 1;
4343
0
      }
4344
4345
0
      return zend_class_implements_interface(Z_OBJCE_P(countable), zend_ce_countable);
4346
0
    default:
4347
0
      return 0;
4348
0
  }
4349
0
}
4350
/* }}} */
4351
4352
0
static zend_result get_default_via_ast(zval *default_value_zval, const char *default_value) {
4353
0
  zend_ast *ast;
4354
0
  zend_arena *ast_arena;
4355
4356
0
  zend_string *code = zend_string_concat3(
4357
0
    "<?php ", sizeof("<?php ") - 1, default_value, strlen(default_value), ";", 1);
4358
4359
0
  ast = zend_compile_string_to_ast(code, &ast_arena, "");
4360
0
  zend_string_release(code);
4361
4362
0
  if (!ast) {
4363
0
    return FAILURE;
4364
0
  }
4365
4366
0
  zend_ast_list *statement_list = zend_ast_get_list(ast);
4367
0
  zend_ast *const_expression_ast = statement_list->child[0];
4368
4369
0
  zend_arena *original_ast_arena = CG(ast_arena);
4370
0
  uint32_t original_compiler_options = CG(compiler_options);
4371
0
  zend_file_context original_file_context;
4372
0
  CG(ast_arena) = ast_arena;
4373
  /* Disable constant substitution, to make getDefaultValueConstant() work. */
4374
0
  CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
4375
0
  zend_file_context_begin(&original_file_context);
4376
0
  zend_const_expr_to_zval(default_value_zval, const_expression_ast);
4377
0
  CG(ast_arena) = original_ast_arena;
4378
0
  CG(compiler_options) = original_compiler_options;
4379
0
  zend_file_context_end(&original_file_context);
4380
4381
0
  zend_ast_destroy(ast);
4382
0
  zend_arena_destroy(ast_arena);
4383
4384
0
  return SUCCESS;
4385
0
}
4386
4387
87
static zend_string *try_parse_string(const char *str, size_t len, char quote) {
4388
87
  if (len == 0) {
4389
0
    return ZSTR_EMPTY_ALLOC();
4390
0
  }
4391
4392
174
  for (size_t i = 0; i < len; i++) {
4393
87
    if (str[i] == '\\' || str[i] == quote) {
4394
0
      return NULL;
4395
0
    }
4396
87
  }
4397
87
  return zend_string_init(str, len, 0);
4398
87
}
4399
4400
ZEND_API zend_result zend_get_default_from_internal_arg_info(
4401
    zval *default_value_zval, zend_internal_arg_info *arg_info)
4402
605
{
4403
605
  const char *default_value = arg_info->default_value;
4404
605
  if (!default_value) {
4405
65
    return FAILURE;
4406
65
  }
4407
4408
  /* Avoid going through the full AST machinery for some simple and common cases. */
4409
540
  size_t default_value_len = strlen(default_value);
4410
540
  zend_ulong lval;
4411
540
  if (default_value_len == sizeof("null")-1
4412
453
      && !memcmp(default_value, "null", sizeof("null")-1)) {
4413
453
    ZVAL_NULL(default_value_zval);
4414
453
    return SUCCESS;
4415
87
  } else if (default_value_len == sizeof("true")-1
4416
0
      && !memcmp(default_value, "true", sizeof("true")-1)) {
4417
0
    ZVAL_TRUE(default_value_zval);
4418
0
    return SUCCESS;
4419
87
  } else if (default_value_len == sizeof("false")-1
4420
0
      && !memcmp(default_value, "false", sizeof("false")-1)) {
4421
0
    ZVAL_FALSE(default_value_zval);
4422
0
    return SUCCESS;
4423
87
  } else if (default_value_len >= 2
4424
87
      && (default_value[0] == '\'' || default_value[0] == '"')
4425
87
      && default_value[default_value_len - 1] == default_value[0]) {
4426
87
    zend_string *str = try_parse_string(
4427
87
      default_value + 1, default_value_len - 2, default_value[0]);
4428
87
    if (str) {
4429
87
      ZVAL_STR(default_value_zval, str);
4430
87
      return SUCCESS;
4431
87
    }
4432
0
  } else if (default_value_len == sizeof("[]")-1
4433
0
      && !memcmp(default_value, "[]", sizeof("[]")-1)) {
4434
0
    ZVAL_EMPTY_ARRAY(default_value_zval);
4435
0
    return SUCCESS;
4436
0
  } else if (ZEND_HANDLE_NUMERIC_STR(default_value, default_value_len, lval)) {
4437
0
    ZVAL_LONG(default_value_zval, lval);
4438
0
    return SUCCESS;
4439
0
  }
4440
4441
#if 0
4442
  fprintf(stderr, "Evaluating %s via AST\n", default_value);
4443
#endif
4444
0
  return get_default_via_ast(default_value_zval, default_value);
4445
0
}