Coverage Report

Created: 2022-02-19 20:31

/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
1.21k
{
47
1.21k
  zval *param_ptr;
48
1.21k
  uint32_t arg_count;
49
50
1.21k
  param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
51
1.21k
  arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
52
53
1.21k
  if (param_count>arg_count) {
54
0
    return FAILURE;
55
0
  }
56
57
2.49k
  while (param_count-->0) {
58
1.27k
    ZVAL_COPY_VALUE(argument_array, param_ptr);
59
1.27k
    argument_array++;
60
1.27k
    param_ptr++;
61
1.27k
  }
62
63
1.21k
  return SUCCESS;
64
1.21k
}
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
134k
{
101
134k
  switch(type) {
102
2.35k
    case IS_FALSE:
103
4.14k
    case IS_TRUE:
104
4.14k
    case _IS_BOOL:
105
4.14k
      return "bool";
106
12.9k
    case IS_LONG:
107
12.9k
      return "int";
108
1.70k
    case IS_DOUBLE:
109
1.70k
      return "float";
110
13.6k
    case IS_STRING:
111
13.6k
      return "string";
112
0
    case IS_OBJECT:
113
0
      return "object";
114
0
    case IS_RESOURCE:
115
0
      return "resource";
116
96.0k
    case IS_NULL:
117
96.0k
      return "null";
118
0
    case IS_CALLABLE:
119
0
      return "callable";
120
0
    case IS_ITERABLE:
121
0
      return "iterable";
122
5.56k
    case IS_ARRAY:
123
5.56k
      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
134k
  }
132
134k
}
133
/* }}} */
134
135
ZEND_API const char *zend_zval_type_name(const zval *arg) /* {{{ */
136
177k
{
137
177k
  ZVAL_DEREF(arg);
138
139
177k
  if (Z_ISUNDEF_P(arg)) {
140
38.0k
    return "null";
141
38.0k
  }
142
143
139k
  if (Z_TYPE_P(arg) == IS_OBJECT) {
144
7.44k
    return ZSTR_VAL(Z_OBJCE_P(arg)->name);
145
7.44k
  }
146
147
131k
  return zend_get_type_by_const(Z_TYPE_P(arg));
148
131k
}
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
3.32k
{
155
3.32k
  switch (Z_TYPE_P(arg)) {
156
1.53k
    case IS_NULL:
157
1.53k
      return ZSTR_KNOWN(ZEND_STR_NULL);
158
101
    case IS_FALSE:
159
495
    case IS_TRUE:
160
495
      return ZSTR_KNOWN(ZEND_STR_BOOLEAN);
161
724
    case IS_LONG:
162
724
      return ZSTR_KNOWN(ZEND_STR_INTEGER);
163
132
    case IS_DOUBLE:
164
132
      return ZSTR_KNOWN(ZEND_STR_DOUBLE);
165
111
    case IS_STRING:
166
111
      return ZSTR_KNOWN(ZEND_STR_STRING);
167
315
    case IS_ARRAY:
168
315
      return ZSTR_KNOWN(ZEND_STR_ARRAY);
169
15
    case IS_OBJECT:
170
15
      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
3.32k
  }
180
3.32k
}
181
/* }}} */
182
183
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_none_error(void) /* {{{ */
184
2.74k
{
185
2.74k
  int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
186
2.74k
  zend_function *active_function = EG(current_execute_data)->func;
187
1.97k
  const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
188
189
2.74k
  zend_argument_count_error(
190
2.74k
        "%s%s%s() expects exactly 0 parameters, %d given",
191
2.74k
        class_name, \
192
1.97k
        class_name[0] ? "::" : "", \
193
2.74k
        ZSTR_VAL(active_function->common.function_name),
194
2.74k
        num_args);
195
2.74k
}
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
3.54k
{
200
3.54k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
201
3.54k
  zend_function *active_function = EG(current_execute_data)->func;
202
3.39k
  const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
203
204
3.54k
  zend_argument_count_error(
205
3.54k
        "%s%s%s() expects %s %d parameter%s, %d given",
206
3.54k
        class_name, \
207
3.39k
        class_name[0] ? "::" : "", \
208
3.54k
        ZSTR_VAL(active_function->common.function_name),
209
1.85k
        min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
210
2.16k
        num_args < min_num_args ? min_num_args : max_num_args,
211
2.16k
        (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
212
3.54k
        num_args);
213
3.54k
}
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
6.98k
{
218
6.98k
  switch (error_code) {
219
370
    case ZPP_ERROR_WRONG_CALLBACK:
220
370
      zend_wrong_callback_error(num, name);
221
370
      break;
222
20
    case ZPP_ERROR_WRONG_CLASS:
223
20
      zend_wrong_parameter_class_error(num, name, arg);
224
20
      break;
225
77
    case ZPP_ERROR_WRONG_CLASS_OR_NULL:
226
77
      zend_wrong_parameter_class_or_null_error(num, name, arg);
227
77
      break;
228
2.91k
    case ZPP_ERROR_WRONG_ARG:
229
2.91k
      zend_wrong_parameter_type_error(num, expected_type, arg);
230
2.91k
      break;
231
1
    case ZPP_ERROR_WRONG_STRING_OR_CLASS:
232
1
      zend_wrong_parameter_string_or_class_error(num, name, arg);
233
1
      break;
234
14
    case ZPP_ERROR_WRONG_STRING_OR_CLASS_OR_NULL:
235
14
      zend_wrong_parameter_string_or_class_or_null_error(num, name, arg);
236
14
      break;
237
45
    case ZPP_ERROR_UNEXPECTED_EXTRA_NAMED:
238
45
      zend_unexpected_extra_named_error();
239
45
      break;
240
3.54k
    default:
241
3.54k
      ZEND_ASSERT(error_code != ZPP_ERROR_OK);
242
6.98k
  }
243
6.98k
}
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
2.91k
{
248
2.91k
  static const char * const expected_error[] = {
249
81.6k
    Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR)
250
2.91k
    NULL
251
2.91k
  };
252
253
2.91k
  if (EG(exception)) {
254
59
    return;
255
59
  }
256
257
2.85k
  zend_argument_type_error(num, "must be %s, %s given", expected_error[expected_type], zend_zval_type_name(arg));
258
2.85k
}
259
/* }}} */
260
261
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_error(uint32_t num, const char *name, zval *arg) /* {{{ */
262
20
{
263
20
  if (EG(exception)) {
264
0
    return;
265
0
  }
266
267
20
  zend_argument_type_error(num, "must be of type %s, %s given", name, zend_zval_type_name(arg));
268
20
}
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
77
{
273
77
  if (EG(exception)) {
274
0
    return;
275
0
  }
276
277
77
  zend_argument_type_error(num, "must be of type ?%s, %s given", name, zend_zval_type_name(arg));
278
77
}
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
1
{
283
1
  if (EG(exception)) {
284
0
    return;
285
0
  }
286
287
1
  zend_argument_type_error(num, "must be of type %s|string, %s given", name, zend_zval_type_name(arg));
288
1
}
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
14
{
293
14
  if (EG(exception)) {
294
0
    return;
295
0
  }
296
297
14
  zend_argument_type_error(num, "must be of type %s|string|null, %s given", name, zend_zval_type_name(arg));
298
14
}
299
/* }}} */
300
301
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_error(uint32_t num, char *error) /* {{{ */
302
370
{
303
370
  if (EG(exception)) {
304
0
    return;
305
0
  }
306
307
370
  zend_argument_type_error(num, "must be a valid callback, %s", error);
308
370
  efree(error);
309
370
}
310
/* }}} */
311
312
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_unexpected_extra_named_error(void)
313
68
{
314
68
  const char *space;
315
68
  const char *class_name = get_active_class_name(&space);
316
68
  zend_argument_count_error("%s%s%s() does not accept unknown named parameters",
317
68
    class_name, space, get_active_function_name());
318
68
}
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
6.00k
{
322
6.00k
  const char *space;
323
6.00k
  const char *class_name;
324
6.00k
  const char *arg_name;
325
6.00k
  char *message = NULL;
326
6.00k
  if (EG(exception)) {
327
48
    return;
328
48
  }
329
330
5.95k
  class_name = get_active_class_name(&space);
331
5.95k
  arg_name = get_active_function_arg_name(arg_num);
332
333
5.95k
  zend_vspprintf(&message, 0, format, va);
334
5.95k
  zend_throw_error(error_ce, "%s%s%s(): Argument #%d%s%s%s %s",
335
5.95k
    class_name, space, get_active_function_name(), arg_num,
336
5.90k
    arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "", message
337
5.95k
    );
338
5.95k
  efree(message);
339
5.95k
}
340
/* }}} */
341
342
ZEND_API ZEND_COLD void zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format, ...) /* {{{ */
343
268
{
344
268
  va_list va;
345
346
268
  va_start(va, format);
347
268
  zend_argument_error_variadic(error_ce, arg_num, format, va);
348
268
  va_end(va);
349
268
}
350
/* }}} */
351
352
ZEND_API ZEND_COLD void zend_argument_type_error(uint32_t arg_num, const char *format, ...) /* {{{ */
353
4.44k
{
354
4.44k
  va_list va;
355
356
4.44k
  va_start(va, format);
357
4.44k
  zend_argument_error_variadic(zend_ce_type_error, arg_num, format, va);
358
4.44k
  va_end(va);
359
4.44k
}
360
/* }}} */
361
362
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format, ...) /* {{{ */
363
1.28k
{
364
1.28k
  va_list va;
365
366
1.28k
  va_start(va, format);
367
1.28k
  zend_argument_error_variadic(zend_ce_value_error, arg_num, format, va);
368
1.28k
  va_end(va);
369
1.28k
}
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
56.8k
{
403
56.8k
  if (EXPECTED(Z_TYPE_P(arg) <= IS_STRING)) {
404
56.8k
    *dest = zend_is_true(arg);
405
31
  } else {
406
31
    return 0;
407
31
  }
408
56.8k
  return 1;
409
56.8k
}
410
/* }}} */
411
412
ZEND_API bool ZEND_FASTCALL zend_parse_arg_bool_slow(zval *arg, zend_bool *dest) /* {{{ */
413
28.4k
{
414
28.4k
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
415
3
    return 0;
416
3
  }
417
28.4k
  return zend_parse_arg_bool_weak(arg, dest);
418
28.4k
}
419
/* }}} */
420
421
ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_weak(zval *arg, zend_long *dest) /* {{{ */
422
39.4k
{
423
39.4k
  if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
424
1.97k
    if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
425
41
      return 0;
426
41
    }
427
1.92k
    if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
428
854
      return 0;
429
1.07k
    } else {
430
1.07k
      *dest = zend_dval_to_lval(Z_DVAL_P(arg));
431
1.07k
    }
432
37.4k
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
433
8.95k
    double d;
434
8.95k
    zend_uchar type;
435
436
8.95k
    if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
437
2.09k
      if (EXPECTED(type != 0)) {
438
434
        if (UNEXPECTED(zend_isnan(d))) {
439
0
          return 0;
440
0
        }
441
434
        if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
442
143
          return 0;
443
291
        } else {
444
291
          *dest = zend_dval_to_lval(d);
445
291
        }
446
1.66k
      } else {
447
1.66k
        return 0;
448
1.66k
      }
449
7.15k
    }
450
7.15k
    if (UNEXPECTED(EG(exception))) {
451
0
      return 0;
452
0
    }
453
28.4k
  } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
454
26.3k
    *dest = 0;
455
2.08k
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
456
1.24k
    *dest = 1;
457
843
  } else {
458
843
    return 0;
459
843
  }
460
35.8k
  return 1;
461
35.8k
}
462
/* }}} */
463
464
ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_slow(zval *arg, zend_long *dest) /* {{{ */
465
18.5k
{
466
18.5k
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
467
7
    return 0;
468
7
  }
469
18.5k
  return zend_parse_arg_long_weak(arg, dest);
470
18.5k
}
471
/* }}} */
472
473
ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_weak(zval *arg, double *dest) /* {{{ */
474
6.47k
{
475
6.47k
  if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
476
3.19k
    *dest = (double)Z_LVAL_P(arg);
477
3.28k
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
478
1.12k
    zend_long l;
479
1.12k
    zend_uchar type;
480
481
1.12k
    if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), &l, dest)) != IS_DOUBLE)) {
482
848
      if (EXPECTED(type != 0)) {
483
274
        *dest = (double)(l);
484
574
      } else {
485
574
        return 0;
486
574
      }
487
553
    }
488
553
    if (UNEXPECTED(EG(exception))) {
489
0
      return 0;
490
0
    }
491
2.15k
  } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
492
1.37k
    *dest = 0.0;
493
785
  } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
494
298
    *dest = 1.0;
495
487
  } else {
496
487
    return 0;
497
487
  }
498
5.41k
  return 1;
499
5.41k
}
500
/* }}} */
501
502
ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_slow(zval *arg, double *dest) /* {{{ */
503
3.01k
{
504
3.01k
  if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
505
    /* SSTH Exception: IS_LONG may be accepted instead as IS_DOUBLE */
506
1.33k
    *dest = (double)Z_LVAL_P(arg);
507
1.67k
  } else if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
508
0
    return 0;
509
0
  }
510
3.01k
  return zend_parse_arg_double_weak(arg, dest);
511
3.01k
}
512
/* }}} */
513
514
ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_slow(zval *arg, zval **dest) /* {{{ */
515
54
{
516
54
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
517
0
    return 0;
518
0
  }
519
54
  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
54
  } else if (Z_TYPE_P(arg) < IS_TRUE) {
533
35
    ZVAL_LONG(arg, 0);
534
19
  } else if (Z_TYPE_P(arg) == IS_TRUE) {
535
19
    ZVAL_LONG(arg, 1);
536
0
  } else {
537
0
    return 0;
538
0
  }
539
54
  *dest = arg;
540
54
  return 1;
541
54
}
542
/* }}} */
543
544
ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest) /* {{{ */
545
141k
{
546
141k
  if (EXPECTED(Z_TYPE_P(arg) < IS_STRING)) {
547
117k
    convert_to_string(arg);
548
117k
    *dest = Z_STR_P(arg);
549
24.4k
  } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
550
24.2k
    zend_object *zobj = Z_OBJ_P(arg);
551
24.2k
    zval obj;
552
24.2k
    if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) {
553
23.0k
      OBJ_RELEASE(zobj);
554
23.0k
      ZVAL_COPY_VALUE(arg, &obj);
555
23.0k
      *dest = Z_STR_P(arg);
556
23.0k
      return 1;
557
23.0k
    }
558
1.16k
    return 0;
559
197
  } else {
560
197
    return 0;
561
197
  }
562
117k
  return 1;
563
117k
}
564
/* }}} */
565
566
ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest) /* {{{ */
567
130k
{
568
130k
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
569
26
    return 0;
570
26
  }
571
130k
  return zend_parse_arg_str_weak(arg, dest);
572
130k
}
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
4
{
577
4
  if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
578
0
    return 0;
579
0
  }
580
4
  if (zend_parse_arg_long_weak(arg, dest_long)) {
581
4
    *dest_str = NULL;
582
4
    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
4
}
590
/* }}} */
591
592
static const char *zend_parse_arg_impl(zval *arg, va_list *va, const char **spec, char **error) /* {{{ */
593
84.4k
{
594
84.4k
  const char *spec_walk = *spec;
595
84.4k
  char c = *spec_walk++;
596
84.4k
  bool check_null = 0;
597
84.4k
  bool separate = 0;
598
84.4k
  zval *real_arg = arg;
599
600
  /* scan through modifiers */
601
84.4k
  ZVAL_DEREF(arg);
602
93.0k
  while (1) {
603
93.0k
    if (*spec_walk == '/') {
604
0
      SEPARATE_ZVAL_NOREF(arg);
605
0
      real_arg = arg;
606
0
      separate = 1;
607
93.0k
    } else if (*spec_walk == '!') {
608
8.57k
      check_null = 1;
609
84.4k
    } else {
610
84.4k
      break;
611
84.4k
    }
612
8.57k
    spec_walk++;
613
8.57k
  }
614
615
84.4k
  switch (c) {
616
6.92k
    case 'l':
617
6.92k
      {
618
6.92k
        zend_long *p = va_arg(*va, zend_long *);
619
6.92k
        zend_bool *is_null = NULL;
620
621
6.92k
        if (check_null) {
622
0
          is_null = va_arg(*va, zend_bool *);
623
0
        }
624
625
6.92k
        if (!zend_parse_arg_long(arg, p, is_null, check_null)) {
626
54
          return check_null ? "?int" : "int";
627
54
        }
628
6.86k
      }
629
6.86k
      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
9.17k
    case 's':
657
9.17k
      {
658
9.17k
        char **p = va_arg(*va, char **);
659
9.17k
        size_t *pl = va_arg(*va, size_t *);
660
9.17k
        if (!zend_parse_arg_string(arg, p, pl, check_null)) {
661
2
          return check_null ? "?string" : "string";
662
2
        }
663
9.17k
      }
664
9.17k
      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
20.6k
    case 'S':
692
20.6k
      {
693
20.6k
        zend_string **str = va_arg(*va, zend_string **);
694
20.6k
        if (!zend_parse_arg_str(arg, str, check_null)) {
695
104
          return check_null ? "?string" : "string";
696
105
        }
697
20.5k
      }
698
20.5k
      break;
699
700
513
    case 'b':
701
513
      {
702
513
        zend_bool *p = va_arg(*va, zend_bool *);
703
513
        zend_bool *is_null = NULL;
704
705
513
        if (check_null) {
706
0
          is_null = va_arg(*va, zend_bool *);
707
0
        }
708
709
513
        if (!zend_parse_arg_bool(arg, p, is_null, check_null)) {
710
0
          return check_null ? "?bool" : "bool";
711
0
        }
712
513
      }
713
513
      break;
714
715
3
    case 'r':
716
3
      {
717
3
        zval **p = va_arg(*va, zval **);
718
719
3
        if (!zend_parse_arg_resource(arg, p, check_null)) {
720
3
          return check_null ? "resource or null" : "resource";
721
3
        }
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
164
    case 'h':
738
164
      {
739
164
        HashTable **p = va_arg(*va, HashTable **);
740
741
164
        if (!zend_parse_arg_array_ht(arg, p, check_null, c == 'H', separate)) {
742
2
          return check_null ? "?array" : "array";
743
2
        }
744
162
      }
745
162
      break;
746
747
25.1k
    case 'o':
748
25.1k
      {
749
25.1k
        zval **p = va_arg(*va, zval **);
750
751
25.1k
        if (!zend_parse_arg_object(arg, p, NULL, check_null)) {
752
77
          return check_null ? "?object" : "object";
753
90
        }
754
25.0k
      }
755
25.0k
      break;
756
757
1.28k
    case 'O':
758
1.28k
      {
759
1.28k
        zval **p = va_arg(*va, zval **);
760
1.28k
        zend_class_entry *ce = va_arg(*va, zend_class_entry *);
761
762
1.28k
        if (!zend_parse_arg_object(arg, p, ce, check_null)) {
763
70
          if (ce) {
764
70
            if (check_null) {
765
14
              zend_spprintf(error, 0, "of type ?%s, %s given", ZSTR_VAL(ce->name), zend_zval_type_name(arg));
766
14
              return "";
767
56
            } else {
768
56
              return ZSTR_VAL(ce->name);
769
56
            }
770
0
          } else {
771
0
            return check_null ? "?object" : "object";
772
0
          }
773
1.21k
        }
774
1.21k
      }
775
1.21k
      break;
776
777
2.21k
    case 'C':
778
2.21k
      {
779
2.21k
        zend_class_entry *lookup, **pce = va_arg(*va, zend_class_entry **);
780
2.21k
        zend_class_entry *ce_base = *pce;
781
782
2.21k
        if (check_null && Z_TYPE_P(arg) == IS_NULL) {
783
0
          *pce = NULL;
784
0
          break;
785
0
        }
786
2.21k
        if (!try_convert_to_string(arg)) {
787
20
          *pce = NULL;
788
20
          return ""; /* try_convert_to_string() throws an exception */
789
20
        }
790
791
2.19k
        if ((lookup = zend_lookup_class(Z_STR_P(arg))) == NULL) {
792
22
          *pce = NULL;
793
2.17k
        } else {
794
2.17k
          *pce = lookup;
795
2.17k
        }
796
2.19k
        if (ce_base) {
797
2
          if ((!*pce || !instanceof_function(*pce, ce_base))) {
798
2
            zend_spprintf(error, 0, "a class name derived from %s%s, %s given",
799
2
              ZSTR_VAL(ce_base->name), check_null ? " or null" : "", Z_STRVAL_P(arg));
800
2
            *pce = NULL;
801
2
            return "";
802
2
          }
803
2.19k
        }
804
2.19k
        if (!*pce) {
805
20
          zend_spprintf(error, 0, "a valid class name%s, %s given",
806
20
            check_null ? " or null" : "", Z_STRVAL_P(arg));
807
20
          return "";
808
20
        }
809
2.17k
        break;
810
811
2.17k
      }
812
0
      break;
813
814
4.87k
    case 'f':
815
4.87k
      {
816
4.87k
        zend_fcall_info *fci = va_arg(*va, zend_fcall_info *);
817
4.87k
        zend_fcall_info_cache *fcc = va_arg(*va, zend_fcall_info_cache *);
818
4.87k
        char *is_callable_error = NULL;
819
820
4.87k
        if (check_null && Z_TYPE_P(arg) == IS_NULL) {
821
139
          fci->size = 0;
822
139
          fcc->function_handler = 0;
823
139
          break;
824
139
        }
825
826
4.73k
        if (zend_fcall_info_init(arg, 0, fci, fcc, NULL, &is_callable_error) == SUCCESS) {
827
4.62k
          ZEND_ASSERT(!is_callable_error);
828
4.62k
          break;
829
4.62k
        }
830
831
105
        if (is_callable_error) {
832
105
          zend_spprintf(error, 0, "a valid callback%s, %s", check_null ? " or null" : "", is_callable_error);
833
105
          efree(is_callable_error);
834
105
          return "";
835
0
        } else {
836
0
          return check_null ? "a valid callback or null" : "a valid callback";
837
0
        }
838
0
      }
839
840
13.5k
    case 'z':
841
13.5k
      {
842
13.5k
        zval **p = va_arg(*va, zval **);
843
844
13.5k
        zend_parse_arg_zval_deref(real_arg, p, check_null);
845
13.5k
      }
846
13.5k
      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
83.9k
  }
854
855
83.9k
  *spec = spec_walk;
856
857
83.9k
  return NULL;
858
83.9k
}
859
/* }}} */
860
861
static zend_result zend_parse_arg(uint32_t arg_num, zval *arg, va_list *va, const char **spec, int flags) /* {{{ */
862
84.4k
{
863
84.4k
  const char *expected_type = NULL;
864
84.4k
  char *error = NULL;
865
866
84.4k
  expected_type = zend_parse_arg_impl(arg, va, spec, &error);
867
84.4k
  if (expected_type) {
868
473
    if (EG(exception)) {
869
20
      return FAILURE;
870
20
    }
871
453
    if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) {
872
453
      if (error) {
873
141
        zend_argument_type_error(arg_num, "must be %s", error);
874
141
        efree(error);
875
312
      } else {
876
312
        zend_argument_type_error(arg_num, "must be of type %s, %s given", expected_type, zend_zval_type_name(arg));
877
312
      }
878
0
    } else if (error) {
879
0
      efree(error);
880
0
    }
881
882
453
    return FAILURE;
883
453
  }
884
885
83.9k
  return SUCCESS;
886
83.9k
}
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
86.7k
{
912
86.7k
  const  char *spec_walk;
913
86.7k
  char c;
914
86.7k
  uint32_t i;
915
86.7k
  uint32_t min_num_args = 0;
916
86.7k
  uint32_t max_num_args = 0;
917
86.7k
  uint32_t post_varargs = 0;
918
86.7k
  zval *arg;
919
86.7k
  zend_bool have_varargs = 0;
920
86.7k
  zend_bool have_optional_args = 0;
921
86.7k
  zval **varargs = NULL;
922
86.7k
  int *n_varargs = NULL;
923
924
343k
  for (spec_walk = type_spec; *spec_walk; spec_walk++) {
925
256k
    c = *spec_walk;
926
256k
    switch (c) {
927
56.5k
      case 'l': case 'd':
928
67.9k
      case 's': case 'b':
929
68.0k
      case 'r': case 'a':
930
114k
      case 'o': case 'O':
931
136k
      case 'z': case 'Z':
932
139k
      case 'C': case 'h':
933
144k
      case 'f': case 'A':
934
144k
      case 'H': case 'p':
935
175k
      case 'S': case 'P':
936
175k
      case 'L': case 'n':
937
175k
        max_num_args++;
938
175k
        break;
939
940
49.9k
      case '|':
941
49.9k
        min_num_args = max_num_args;
942
49.9k
        have_optional_args = 1;
943
49.9k
        break;
944
945
0
      case '/':
946
31.0k
      case '!':
947
        /* Pass */
948
31.0k
        break;
949
950
0
      case '*':
951
25
      case '+':
952
25
        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
25
        have_varargs = 1;
958
        /* we expect at least one parameter in varargs */
959
25
        if (c == '+') {
960
25
          max_num_args++;
961
25
        }
962
        /* mark the beginning of varargs */
963
25
        post_varargs = max_num_args;
964
965
25
        if (ZEND_CALL_INFO(EG(current_execute_data)) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
966
23
          zend_unexpected_extra_named_error();
967
23
          return FAILURE;
968
23
        }
969
2
        break;
970
971
0
      default:
972
0
        zend_parse_parameters_debug_error("bad type specifier while parsing parameters");
973
0
        return FAILURE;
974
256k
    }
975
256k
  }
976
977
  /* with no optional arguments the minimum number of arguments must be the same as the maximum */
978
86.7k
  if (!have_optional_args) {
979
36.7k
    min_num_args = max_num_args;
980
36.7k
  }
981
982
86.7k
  if (have_varargs) {
983
    /* calculate how many required args are at the end of the specifier list */
984
2
    post_varargs = max_num_args - post_varargs;
985
2
    max_num_args = UINT32_MAX;
986
2
  }
987
988
86.7k
  if (num_args < min_num_args || num_args > max_num_args) {
989
647
    if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
990
589
      zend_function *active_function = EG(current_execute_data)->func;
991
504
      const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
992
589
      zend_argument_count_error("%s%s%s() expects %s %d parameter%s, %d given",
993
589
          class_name,
994
504
          class_name[0] ? "::" : "",
995
589
          ZSTR_VAL(active_function->common.function_name),
996
386
          min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
997
491
          num_args < min_num_args ? min_num_args : max_num_args,
998
491
          (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
999
589
          num_args);
1000
589
    }
1001
647
    return FAILURE;
1002
647
  }
1003
1004
86.1k
  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
86.1k
  i = 0;
1010
170k
  while (num_args-- > 0) {
1011
84.4k
    if (*type_spec == '|') {
1012
21.4k
      type_spec++;
1013
21.4k
    }
1014
1015
84.4k
    if (*type_spec == '*' || *type_spec == '+') {
1016
2
      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
2
      varargs = va_arg(*va, zval **);
1020
2
      n_varargs = va_arg(*va, int *);
1021
2
      type_spec++;
1022
1023
2
      if (num_varargs > 0) {
1024
2
        *n_varargs = num_varargs;
1025
2
        *varargs = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
1026
        /* adjust how many args we have left and restart loop */
1027
2
        num_args += 1 - num_varargs;
1028
2
        i += num_varargs;
1029
2
        continue;
1030
0
      } else {
1031
0
        *varargs = NULL;
1032
0
        *n_varargs = 0;
1033
0
      }
1034
2
    }
1035
1036
84.4k
    arg = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
1037
1038
84.4k
    if (zend_parse_arg(i+1, arg, va, &type_spec, flags) == FAILURE) {
1039
      /* clean up varargs array if it was used */
1040
473
      if (varargs && *varargs) {
1041
0
        *varargs = NULL;
1042
0
      }
1043
473
      return FAILURE;
1044
473
    }
1045
83.9k
    i++;
1046
83.9k
  }
1047
1048
85.6k
  return SUCCESS;
1049
86.1k
}
1050
/* }}} */
1051
1052
ZEND_API zend_result zend_parse_parameters_ex(int flags, uint32_t num_args, const char *type_spec, ...) /* {{{ */
1053
691
{
1054
691
  va_list va;
1055
691
  zend_result retval;
1056
1057
691
  va_start(va, type_spec);
1058
691
  retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1059
691
  va_end(va);
1060
1061
691
  return retval;
1062
691
}
1063
/* }}} */
1064
1065
ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec, ...) /* {{{ */
1066
83.9k
{
1067
83.9k
  va_list va;
1068
83.9k
  zend_result retval;
1069
83.9k
  int flags = 0;
1070
1071
83.9k
  va_start(va, type_spec);
1072
83.9k
  retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1073
83.9k
  va_end(va);
1074
1075
83.9k
  return retval;
1076
83.9k
}
1077
/* }}} */
1078
1079
ZEND_API zend_result zend_parse_method_parameters(uint32_t num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
1080
2.17k
{
1081
2.17k
  va_list va;
1082
2.17k
  zend_result retval;
1083
2.17k
  int flags = 0;
1084
2.17k
  const char *p = type_spec;
1085
2.17k
  zval **object;
1086
2.17k
  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
2.17k
  zend_bool is_method = EG(current_execute_data)->func->common.scope != NULL;
1093
1094
2.17k
  if (!is_method || !this_ptr || Z_TYPE_P(this_ptr) != IS_OBJECT) {
1095
146
    va_start(va, type_spec);
1096
146
    retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1097
146
    va_end(va);
1098
2.02k
  } else {
1099
2.02k
    p++;
1100
1101
2.02k
    va_start(va, type_spec);
1102
1103
2.02k
    object = va_arg(va, zval **);
1104
2.02k
    ce = va_arg(va, zend_class_entry *);
1105
2.02k
    *object = this_ptr;
1106
1107
2.02k
    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
2.02k
    retval = zend_parse_va_args(num_args, p, &va, flags);
1113
2.02k
    va_end(va);
1114
2.02k
  }
1115
2.17k
  return retval;
1116
2.17k
}
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
3.27k
{
1177
3.27k
  if (!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) {
1178
3.19k
    zend_class_constant *c;
1179
3.19k
    zval *val;
1180
3.19k
    zend_property_info *prop_info;
1181
1182
3.19k
    if (class_type->parent) {
1183
190
      if (UNEXPECTED(zend_update_class_constants(class_type->parent) != SUCCESS)) {
1184
81
        return FAILURE;
1185
81
      }
1186
3.11k
    }
1187
1188
5.67k
    ZEND_HASH_FOREACH_PTR(&class_type->constants_table, c) {
1189
5.67k
      val = &c->value;
1190
1.28k
      if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
1191
235
        if (UNEXPECTED(zval_update_constant_ex(val, c->ce) != SUCCESS)) {
1192
122
          return FAILURE;
1193
122
        }
1194
235
      }
1195
1.28k
    } ZEND_HASH_FOREACH_END();
1196
1197
2.99k
    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
11.4k
    ZEND_HASH_FOREACH_PTR(&class_type->properties_info, prop_info) {
1204
4.22k
      if (prop_info->flags & ZEND_ACC_STATIC) {
1205
1.39k
        val = CE_STATIC_MEMBERS(class_type) + prop_info->offset;
1206
2.83k
      } else {
1207
2.83k
        val = (zval*)((char*)class_type->default_properties_table + prop_info->offset - OBJ_PROP_TO_OFFSET(0));
1208
2.83k
      }
1209
4.22k
      if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
1210
3.78k
        if (ZEND_TYPE_IS_SET(prop_info->type)) {
1211
1.69k
          zval tmp;
1212
1213
1.69k
          ZVAL_COPY(&tmp, val);
1214
1.69k
          if (UNEXPECTED(zval_update_constant_ex(&tmp, prop_info->ce) != SUCCESS)) {
1215
334
            zval_ptr_dtor(&tmp);
1216
334
            return FAILURE;
1217
334
          }
1218
1.36k
          /* property initializers must always be evaluated with strict types */;
1219
1.36k
          if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, /* strict */ 1))) {
1220
1.21k
            zval_ptr_dtor(&tmp);
1221
1.21k
            return FAILURE;
1222
1.21k
          }
1223
150
          zval_ptr_dtor(val);
1224
150
          ZVAL_COPY_VALUE(val, &tmp);
1225
2.09k
        } else if (UNEXPECTED(zval_update_constant_ex(val, prop_info->ce) != SUCCESS)) {
1226
378
          return FAILURE;
1227
378
        }
1228
3.78k
      }
1229
4.22k
    } ZEND_HASH_FOREACH_END();
1230
1231
1.07k
    class_type->ce_flags |= ZEND_ACC_CONSTANTS_UPDATED;
1232
1.07k
  }
1233
1234
1.14k
  return SUCCESS;
1235
3.27k
}
1236
/* }}} */
1237
1238
static zend_always_inline void _object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1239
3.74M
{
1240
3.74M
  if (class_type->default_properties_count) {
1241
2.34M
    zval *src = class_type->default_properties_table;
1242
2.34M
    zval *dst = object->properties_table;
1243
2.34M
    zval *end = src + class_type->default_properties_count;
1244
1245
2.34M
    if (UNEXPECTED(class_type->type == ZEND_INTERNAL_CLASS)) {
1246
15.4M
      do {
1247
15.4M
        ZVAL_COPY_OR_DUP_PROP(dst, src);
1248
15.4M
        src++;
1249
15.4M
        dst++;
1250
15.4M
      } while (src != end);
1251
132k
    } else {
1252
211k
      do {
1253
211k
        ZVAL_COPY_PROP(dst, src);
1254
211k
        src++;
1255
211k
        dst++;
1256
211k
      } while (src != end);
1257
132k
    }
1258
2.34M
  }
1259
3.74M
}
1260
/* }}} */
1261
1262
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1263
3.40M
{
1264
3.40M
  object->properties = NULL;
1265
3.40M
  _object_properties_init(object, class_type);
1266
3.40M
}
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
3.86M
{
1366
3.86M
  if (UNEXPECTED(class_type->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) {
1367
97
    if (class_type->ce_flags & ZEND_ACC_INTERFACE) {
1368
30
      zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(class_type->name));
1369
67
    } else if (class_type->ce_flags & ZEND_ACC_TRAIT) {
1370
45
      zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(class_type->name));
1371
22
    } else {
1372
22
      zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name));
1373
22
    }
1374
97
    ZVAL_NULL(arg);
1375
97
    Z_OBJ_P(arg) = NULL;
1376
97
    return FAILURE;
1377
97
  }
1378
1379
3.86M
  if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
1380
2.38k
    if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
1381
1.87k
      ZVAL_NULL(arg);
1382
1.87k
      Z_OBJ_P(arg) = NULL;
1383
1.87k
      return FAILURE;
1384
1.87k
    }
1385
3.86M
  }
1386
1387
3.86M
  if (class_type->create_object == NULL) {
1388
344k
    zend_object *obj = zend_objects_new(class_type);
1389
1390
344k
    ZVAL_OBJ(arg, obj);
1391
344k
    if (properties) {
1392
0
      object_properties_init_ex(obj, properties);
1393
344k
    } else {
1394
344k
      _object_properties_init(obj, class_type);
1395
344k
    }
1396
3.52M
  } else {
1397
3.52M
    ZVAL_OBJ(arg, class_type->create_object(class_type));
1398
3.52M
  }
1399
3.86M
  return SUCCESS;
1400
3.86M
}
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
3.86M
{
1411
3.86M
  return _object_and_properties_init(arg, class_type, NULL);
1412
3.86M
}
1413
/* }}} */
1414
1415
ZEND_API void object_init(zval *arg) /* {{{ */
1416
0
{
1417
0
  ZVAL_OBJ(arg, zend_objects_new(zend_standard_class_def));
1418
0
}
1419
/* }}} */
1420
1421
ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
1422
205k
{
1423
205k
  zval tmp;
1424
1425
205k
  ZVAL_LONG(&tmp, n);
1426
205k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1427
205k
}
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
102k
{
1468
102k
  zval tmp;
1469
1470
102k
  ZVAL_STR(&tmp, str);
1471
102k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1472
102k
}
1473
/* }}} */
1474
1475
ZEND_API void add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
1476
102k
{
1477
102k
  zval tmp;
1478
1479
102k
  ZVAL_STRING(&tmp, str);
1480
102k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1481
102k
}
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
3.55k
{
1495
3.55k
  zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value);
1496
3.55k
}
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
334k
{
1618
334k
  zval tmp;
1619
1620
334k
  ZVAL_STR(&tmp, str);
1621
334k
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1622
334k
}
1623
/* }}} */
1624
1625
ZEND_API zend_result add_next_index_string(zval *arg, const char *str) /* {{{ */
1626
40
{
1627
40
  zval tmp;
1628
1629
40
  ZVAL_STRING(&tmp, str);
1630
40
  return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
1631
40
}
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
2.73k
{
1645
2.73k
  zval *result;
1646
1647
2.73k
  switch (Z_TYPE_P(key)) {
1648
2.71k
    case IS_STRING:
1649
2.71k
      result = zend_symtable_update(ht, Z_STR_P(key), value);
1650
2.71k
      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
18
    case IS_LONG:
1665
18
      result = zend_hash_index_update(ht, Z_LVAL_P(key), value);
1666
18
      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
2.73k
  }
1674
1675
2.73k
  if (result) {
1676
2.73k
    Z_TRY_ADDREF_P(result);
1677
2.73k
    return SUCCESS;
1678
0
  } else {
1679
0
    return FAILURE;
1680
0
  }
1681
2.73k
}
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
26.4k
{
1704
26.4k
  zval tmp;
1705
1706
26.4k
  ZVAL_NULL(&tmp);
1707
26.4k
  add_property_zval_ex(arg, key, key_len, &tmp);
1708
26.4k
}
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
26.4k
{
1762
26.4k
  zend_string *str;
1763
1764
26.4k
  str = zend_string_init(key, key_len, 0);
1765
26.4k
  Z_OBJ_HANDLER_P(arg, write_property)(Z_OBJ_P(arg), str, value, NULL);
1766
26.4k
  zend_string_release_ex(str, 0);
1767
26.4k
}
1768
/* }}} */
1769
1770
ZEND_API zend_result zend_startup_module_ex(zend_module_entry *module) /* {{{ */
1771
40.6k
{
1772
40.6k
  size_t name_len;
1773
40.6k
  zend_string *lcname;
1774
1775
40.6k
  if (module->module_started) {
1776
0
    return SUCCESS;
1777
0
  }
1778
40.6k
  module->module_started = 1;
1779
1780
  /* Check module dependencies */
1781
40.6k
  if (module->deps) {
1782
8.13k
    const zend_module_dep *dep = module->deps;
1783
1784
20.3k
    while (dep->name) {
1785
12.2k
      if (dep->type == MODULE_DEP_REQUIRED) {
1786
4.06k
        zend_module_entry *req_mod;
1787
1788
4.06k
        name_len = strlen(dep->name);
1789
4.06k
        lcname = zend_string_alloc(name_len, 0);
1790
4.06k
        zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
1791
1792
4.06k
        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
4.06k
        zend_string_efree(lcname);
1800
4.06k
      }
1801
12.2k
      ++dep;
1802
12.2k
    }
1803
8.13k
  }
1804
1805
  /* Initialize module globals */
1806
40.6k
  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
28.4k
    if (module->globals_ctor) {
1811
24.4k
      module->globals_ctor(module->globals_ptr);
1812
24.4k
    }
1813
28.4k
#endif
1814
28.4k
  }
1815
40.6k
  if (module->module_startup_func) {
1816
40.6k
    EG(current_module) = module;
1817
40.6k
    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
40.6k
    }
1822
40.6k
    EG(current_module) = NULL;
1823
40.6k
  }
1824
40.6k
  return SUCCESS;
1825
40.6k
}
1826
/* }}} */
1827
1828
static int zend_startup_module_zval(zval *zv) /* {{{ */
1829
40.6k
{
1830
40.6k
  zend_module_entry *module = Z_PTR_P(zv);
1831
1832
40.6k
  return (zend_startup_module_ex(module) == SUCCESS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
1833
40.6k
}
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
4.06k
{
1838
4.06k
  Bucket *b1 = base;
1839
4.06k
  Bucket *b2;
1840
4.06k
  Bucket *end = b1 + count;
1841
4.06k
  Bucket tmp;
1842
4.06k
  zend_module_entry *m, *r;
1843
1844
44.7k
  while (b1 < end) {
1845
44.7k
try_again:
1846
44.7k
    m = (zend_module_entry*)Z_PTR(b1->val);
1847
44.7k
    if (!m->module_started && m->deps) {
1848
12.2k
      const zend_module_dep *dep = m->deps;
1849
24.4k
      while (dep->name) {
1850
16.2k
        if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) {
1851
16.2k
          b2 = b1 + 1;
1852
61.0k
          while (b2 < end) {
1853
48.8k
            r = (zend_module_entry*)Z_PTR(b2->val);
1854
48.8k
            if (strcasecmp(dep->name, r->name) == 0) {
1855
4.06k
              tmp = *b1;
1856
4.06k
              *b1 = *b2;
1857
4.06k
              *b2 = tmp;
1858
4.06k
              goto try_again;
1859
4.06k
            }
1860
44.7k
            b2++;
1861
44.7k
          }
1862
16.2k
        }
1863
12.2k
        dep++;
1864
12.2k
      }
1865
12.2k
    }
1866
40.6k
    b1++;
1867
40.6k
  }
1868
4.06k
}
1869
/* }}} */
1870
1871
ZEND_API void zend_collect_module_handlers(void) /* {{{ */
1872
4.06k
{
1873
4.06k
  zend_module_entry *module;
1874
4.06k
  int startup_count = 0;
1875
4.06k
  int shutdown_count = 0;
1876
4.06k
  int post_deactivate_count = 0;
1877
4.06k
  zend_class_entry *ce;
1878
4.06k
  int class_count = 0;
1879
1880
  /* Collect extensions with request startup/shutdown handlers */
1881
85.4k
  ZEND_HASH_FOREACH_PTR(&module_registry, module) {
1882
40.6k
    if (module->request_startup_func) {
1883
20.3k
      startup_count++;
1884
20.3k
    }
1885
40.6k
    if (module->request_shutdown_func) {
1886
20.3k
      shutdown_count++;
1887
20.3k
    }
1888
40.6k
    if (module->post_deactivate_func) {
1889
0
      post_deactivate_count++;
1890
0
    }
1891
40.6k
  } ZEND_HASH_FOREACH_END();
1892
4.06k
  module_request_startup_handlers = (zend_module_entry**)malloc(
1893
4.06k
      sizeof(zend_module_entry*) *
1894
4.06k
    (startup_count + 1 +
1895
4.06k
     shutdown_count + 1 +
1896
4.06k
     post_deactivate_count + 1));
1897
4.06k
  module_request_startup_handlers[startup_count] = NULL;
1898
4.06k
  module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
1899
4.06k
  module_request_shutdown_handlers[shutdown_count] = NULL;
1900
4.06k
  module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
1901
4.06k
  module_post_deactivate_handlers[post_deactivate_count] = NULL;
1902
4.06k
  startup_count = 0;
1903
1904
85.4k
  ZEND_HASH_FOREACH_PTR(&module_registry, module) {
1905
40.6k
    if (module->request_startup_func) {
1906
20.3k
      module_request_startup_handlers[startup_count++] = module;
1907
20.3k
    }
1908
40.6k
    if (module->request_shutdown_func) {
1909
20.3k
      module_request_shutdown_handlers[--shutdown_count] = module;
1910
20.3k
    }
1911
40.6k
    if (module->post_deactivate_func) {
1912
0
      module_post_deactivate_handlers[--post_deactivate_count] = module;
1913
0
    }
1914
40.6k
  } ZEND_HASH_FOREACH_END();
1915
1916
  /* Collect internal classes with static members */
1917
931k
  ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
1918
463k
    if (ce->type == ZEND_INTERNAL_CLASS &&
1919
463k
        ce->default_static_members_count > 0) {
1920
0
        class_count++;
1921
0
    }
1922
463k
  } ZEND_HASH_FOREACH_END();
1923
1924
4.06k
  class_cleanup_handlers = (zend_class_entry**)malloc(
1925
4.06k
    sizeof(zend_class_entry*) *
1926
4.06k
    (class_count + 1));
1927
4.06k
  class_cleanup_handlers[class_count] = NULL;
1928
1929
4.06k
  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
4.06k
}
1938
/* }}} */
1939
1940
ZEND_API void zend_startup_modules(void) /* {{{ */
1941
4.06k
{
1942
4.06k
  zend_hash_sort_ex(&module_registry, zend_sort_modules, NULL, 0);
1943
4.06k
  zend_hash_apply(&module_registry, zend_startup_module_zval);
1944
4.06k
}
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
40.6k
{
1957
40.6k
  size_t name_len;
1958
40.6k
  zend_string *lcname;
1959
40.6k
  zend_module_entry *module_ptr;
1960
1961
40.6k
  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
40.6k
  if (module->deps) {
1971
8.13k
    const zend_module_dep *dep = module->deps;
1972
1973
20.3k
    while (dep->name) {
1974
12.2k
      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
12.2k
      ++dep;
1988
12.2k
    }
1989
8.13k
  }
1990
1991
40.6k
  name_len = strlen(module->name);
1992
40.6k
  lcname = zend_string_alloc(name_len, module->type == MODULE_PERSISTENT);
1993
40.6k
  zend_str_tolower_copy(ZSTR_VAL(lcname), module->name, name_len);
1994
1995
40.6k
  lcname = zend_new_interned_string(lcname);
1996
40.6k
  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
40.6k
  module = module_ptr;
2002
40.6k
  EG(current_module) = module;
2003
2004
40.6k
  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
40.6k
  EG(current_module) = NULL;
2013
40.6k
  zend_string_release(lcname);
2014
40.6k
  return module;
2015
40.6k
}
2016
/* }}} */
2017
2018
ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module) /* {{{ */
2019
36.6k
{
2020
36.6k
  module->module_number = zend_next_free_module();
2021
36.6k
  module->type = MODULE_PERSISTENT;
2022
36.6k
  return zend_register_module_ex(module);
2023
36.6k
}
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
270k
{
2029
270k
  if (fptr->common.num_args != num_args) {
2030
201
    if (num_args == 0) {
2031
121
      zend_error(error_type, "Method %s::%s() cannot take arguments",
2032
121
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2033
80
    } else if (num_args == 1) {
2034
60
      zend_error(error_type, "Method %s::%s() must take exactly 1 argument",
2035
60
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2036
20
    } else {
2037
20
      zend_error(error_type, "Method %s::%s() must take exactly %" PRIu32 " arguments",
2038
20
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name), num_args);
2039
20
    }
2040
201
    return;
2041
201
  }
2042
332k
  for (uint32_t i = 0; i < num_args; i++) {
2043
62.8k
    if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, i + 1)) {
2044
140
      zend_error(error_type, "Method %s::%s() cannot take arguments by reference",
2045
140
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2046
140
      return;
2047
140
    }
2048
62.8k
  }
2049
269k
}
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
59.1k
{
2053
59.1k
    if (
2054
59.1k
      ZEND_TYPE_IS_SET(fptr->common.arg_info[arg_num].type)
2055
42.5k
       && !(ZEND_TYPE_FULL_MASK(fptr->common.arg_info[arg_num].type) & arg_type)
2056
201
    ) {
2057
201
      zend_error(error_type, "%s::%s(): Parameter #%d ($%s) must be of type %s when declared",
2058
201
        ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2059
201
        arg_num + 1, ZSTR_VAL(fptr->common.arg_info[arg_num].name),
2060
201
        ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(arg_type))));
2061
201
    }
2062
59.1k
}
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
185k
{
2066
185k
  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
176k
    return;
2069
176k
  }
2070
2071
8.89k
  bool has_class_type = ZEND_TYPE_HAS_CLASS(fptr->common.arg_info[-1].type);
2072
8.89k
  uint32_t extra_types = ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & ~return_type;
2073
8.89k
  if (extra_types & MAY_BE_STATIC) {
2074
20
    extra_types &= ~MAY_BE_STATIC;
2075
20
    has_class_type = 1;
2076
20
  }
2077
2078
8.89k
  if (extra_types || (has_class_type && return_type != MAY_BE_OBJECT)) {
2079
200
    zend_error(error_type, "%s::%s(): Return type must be %s when declared",
2080
200
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2081
200
      ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(return_type))));
2082
200
  }
2083
8.89k
}
2084
2085
static void zend_check_magic_method_non_static(
2086
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2087
471k
{
2088
471k
  if (fptr->common.fn_flags & ZEND_ACC_STATIC) {
2089
120
    zend_error(error_type, "Method %s::%s() cannot be static",
2090
120
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2091
120
  }
2092
471k
}
2093
2094
static void zend_check_magic_method_static(
2095
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2096
21.9k
{
2097
21.9k
  if (!(fptr->common.fn_flags & ZEND_ACC_STATIC)) {
2098
40
    zend_error(error_type, "Method %s::%s() must be static",
2099
40
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2100
40
  }
2101
21.9k
}
2102
2103
static void zend_check_magic_method_public(
2104
    const zend_class_entry *ce, const zend_function *fptr, int error_type)
2105
214k
{
2106
  // TODO: Remove this warning after adding proper visibility handling.
2107
214k
  if (!(fptr->common.fn_flags & ZEND_ACC_PUBLIC)) {
2108
194
    zend_error(E_WARNING, "The magic method %s::%s() must have public visibility",
2109
194
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2110
194
  }
2111
214k
}
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
230k
{
2116
230k
  if (fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
2117
40
    zend_error_noreturn(error_type, "Method %s::%s() cannot declare a return type",
2118
40
      ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2119
40
  }
2120
230k
}
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
3.07M
{
2124
3.07M
  if (ZSTR_VAL(fptr->common.function_name)[0] != '_'
2125
2.57M
   || ZSTR_VAL(fptr->common.function_name)[1] != '_') {
2126
2.57M
    return;
2127
2.57M
  }
2128
2129
496k
  if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2130
223k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2131
223k
    zend_check_magic_method_no_return_type(ce, fptr, error_type);
2132
272k
  } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2133
6.71k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2134
6.71k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2135
6.71k
    zend_check_magic_method_no_return_type(ce, fptr, error_type);
2136
265k
  } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2137
49.2k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2138
49.2k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2139
49.2k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2140
216k
  } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2141
5.48k
    zend_check_magic_method_args(1, ce, fptr, error_type);
2142
5.48k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2143
5.48k
    zend_check_magic_method_public(ce, fptr, error_type);
2144
5.48k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2145
211k
  } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2146
3.39k
    zend_check_magic_method_args(2, ce, fptr, error_type);
2147
3.39k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2148
3.39k
    zend_check_magic_method_public(ce, fptr, error_type);
2149
3.39k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2150
3.39k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2151
207k
  } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
2152
779
    zend_check_magic_method_args(1, ce, fptr, error_type);
2153
779
    zend_check_magic_method_non_static(ce, fptr, error_type);
2154
779
    zend_check_magic_method_public(ce, fptr, error_type);
2155
779
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2156
779
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2157
206k
  } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2158
1.60k
    zend_check_magic_method_args(1, ce, fptr, error_type);
2159
1.60k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2160
1.60k
    zend_check_magic_method_public(ce, fptr, error_type);
2161
1.60k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2162
1.60k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_BOOL);
2163
205k
  } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2164
2.24k
    zend_check_magic_method_args(2, ce, fptr, error_type);
2165
2.24k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2166
2.24k
    zend_check_magic_method_public(ce, fptr, error_type);
2167
2.24k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2168
2.24k
    zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2169
203k
  } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
2170
1.45k
    zend_check_magic_method_args(2, ce, fptr, error_type);
2171
1.45k
    zend_check_magic_method_static(ce, fptr, error_type);
2172
1.45k
    zend_check_magic_method_public(ce, fptr, error_type);
2173
1.45k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2174
1.45k
    zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2175
201k
  } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
2176
68.0k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2177
68.0k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2178
68.0k
    zend_check_magic_method_public(ce, fptr, error_type);
2179
133k
  } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
2180
33.0k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2181
33.0k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2182
33.0k
    zend_check_magic_method_public(ce, fptr, error_type);
2183
33.0k
    zend_check_magic_method_return_type(ce, fptr, error_type, (MAY_BE_ARRAY | MAY_BE_NULL));
2184
100k
  } else if (zend_string_equals_literal(lcname, "__serialize")) {
2185
20.4k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2186
20.4k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2187
20.4k
    zend_check_magic_method_public(ce, fptr, error_type);
2188
20.4k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2189
80.2k
  } else if (zend_string_equals_literal(lcname, "__unserialize")) {
2190
20.4k
    zend_check_magic_method_args(1, ce, fptr, error_type);
2191
20.4k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2192
20.4k
    zend_check_magic_method_public(ce, fptr, error_type);
2193
20.4k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2194
20.4k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2195
59.7k
  } else if (zend_string_equals_literal(lcname, "__set_state")) {
2196
20.5k
    zend_check_magic_method_args(1, ce, fptr, error_type);
2197
20.5k
    zend_check_magic_method_static(ce, fptr, error_type);
2198
20.5k
    zend_check_magic_method_public(ce, fptr, error_type);
2199
20.5k
    zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2200
20.5k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_OBJECT);
2201
39.2k
  } else if (zend_string_equals_literal(lcname, "__invoke")) {
2202
605
    zend_check_magic_method_non_static(ce, fptr, error_type);
2203
605
    zend_check_magic_method_public(ce, fptr, error_type);
2204
38.6k
  } else if (zend_string_equals_literal(lcname, "__sleep")) {
2205
67
    zend_check_magic_method_args(0, ce, fptr, error_type);
2206
67
    zend_check_magic_method_non_static(ce, fptr, error_type);
2207
67
    zend_check_magic_method_public(ce, fptr, error_type);
2208
67
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2209
38.6k
  } else if (zend_string_equals_literal(lcname, "__wakeup")) {
2210
36.6k
    zend_check_magic_method_args(0, ce, fptr, error_type);
2211
36.6k
    zend_check_magic_method_non_static(ce, fptr, error_type);
2212
36.6k
    zend_check_magic_method_public(ce, fptr, error_type);
2213
36.6k
    zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2214
36.6k
  }
2215
496k
}
2216
/* }}} */
2217
2218
ZEND_API void zend_add_magic_method(zend_class_entry *ce, zend_function *fptr, zend_string *lcname)
2219
3.08M
{
2220
3.08M
  if (ZSTR_VAL(lcname)[0] != '_' || ZSTR_VAL(lcname)[1] != '_') {
2221
    /* pass */
2222
496k
  } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2223
49.3k
    ce->clone = fptr;
2224
447k
  } else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2225
223k
    ce->constructor = fptr;
2226
223k
    ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
2227
223k
  } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2228
6.73k
    ce->destructor = fptr;
2229
216k
  } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2230
5.56k
    ce->__get = fptr;
2231
5.56k
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2232
211k
  } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2233
3.42k
    ce->__set = fptr;
2234
3.42k
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2235
207k
  } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2236
2.24k
    ce->__call = fptr;
2237
205k
  } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
2238
780
    ce->__unset = fptr;
2239
780
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2240
204k
  } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2241
1.65k
    ce->__isset = fptr;
2242
1.65k
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2243
203k
  } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
2244
1.47k
    ce->__callstatic = fptr;
2245
201k
  } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
2246
68.0k
    ce->__tostring = fptr;
2247
133k
  } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
2248
33.0k
    ce->__debugInfo = fptr;
2249
100k
  } else if (zend_string_equals_literal(lcname, "__serialize")) {
2250
20.4k
    ce->__serialize = fptr;
2251
80.3k
  } else if (zend_string_equals_literal(lcname, "__unserialize")) {
2252
20.4k
    ce->__unserialize = fptr;
2253
20.4k
  }
2254
3.08M
}
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
422k
{
2259
422k
  const zend_function_entry *ptr = functions;
2260
422k
  zend_function function, *reg_function;
2261
422k
  zend_internal_function *internal_function = (zend_internal_function *)&function;
2262
422k
  int count=0, unload=0;
2263
422k
  HashTable *target_function_table = function_table;
2264
422k
  int error_type;
2265
422k
  zend_string *lowercase_name;
2266
422k
  size_t fname_len;
2267
2268
422k
  if (type==MODULE_PERSISTENT) {
2269
422k
    error_type = E_CORE_WARNING;
2270
0
  } else {
2271
0
    error_type = E_WARNING;
2272
0
  }
2273
2274
422k
  if (!target_function_table) {
2275
36.6k
    target_function_table = CG(function_table);
2276
36.6k
  }
2277
422k
  internal_function->type = ZEND_INTERNAL_FUNCTION;
2278
422k
  internal_function->module = EG(current_module);
2279
422k
  memset(internal_function->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
2280
2281
6.42M
  while (ptr->fname) {
2282
5.99M
    fname_len = strlen(ptr->fname);
2283
5.99M
    internal_function->handler = ptr->handler;
2284
5.99M
    internal_function->function_name = zend_string_init_interned(ptr->fname, fname_len, 1);
2285
5.99M
    internal_function->scope = scope;
2286
5.99M
    internal_function->prototype = NULL;
2287
5.99M
    if (ptr->flags) {
2288
2.96M
      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
2.96M
      } else {
2294
2.96M
        internal_function->fn_flags = ptr->flags;
2295
2.96M
      }
2296
3.03M
    } else {
2297
3.03M
      internal_function->fn_flags = ZEND_ACC_PUBLIC;
2298
3.03M
    }
2299
2300
5.99M
    if (ptr->arg_info) {
2301
5.99M
      zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info;
2302
5.99M
      internal_function->arg_info = (zend_internal_arg_info*)ptr->arg_info+1;
2303
5.99M
      internal_function->num_args = ptr->num_args;
2304
      /* Currently you cannot denote that the function can accept less arguments than num_args */
2305
5.99M
      if (info->required_num_args == (zend_uintptr_t)-1) {
2306
0
        internal_function->required_num_args = ptr->num_args;
2307
5.99M
      } else {
2308
5.99M
        internal_function->required_num_args = info->required_num_args;
2309
5.99M
      }
2310
5.99M
      if (ZEND_ARG_SEND_MODE(info)) {
2311
0
        internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE;
2312
0
      }
2313
5.99M
      if (ZEND_ARG_IS_VARIADIC(&ptr->arg_info[ptr->num_args])) {
2314
187k
        internal_function->fn_flags |= ZEND_ACC_VARIADIC;
2315
        /* Don't count the variadic argument */
2316
187k
        internal_function->num_args--;
2317
187k
      }
2318
5.99M
      if (ZEND_TYPE_IS_SET(info->type)) {
2319
3.27M
        if (ZEND_TYPE_HAS_NAME(info->type)) {
2320
130k
          const char *type_name = ZEND_TYPE_LITERAL_NAME(info->type);
2321
130k
          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
3.27M
        }
2325
2326
3.27M
        internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
2327
3.27M
      }
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
5.99M
    zend_set_function_arg_flags((zend_function*)internal_function);
2338
5.99M
    if (ptr->flags & ZEND_ACC_ABSTRACT) {
2339
154k
      if (scope) {
2340
        /* This is a class that must be abstract itself. Here we set the check info. */
2341
154k
        scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
2342
154k
        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
8.13k
          scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
2347
8.13k
        }
2348
154k
      }
2349
154k
      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
5.84M
    } else {
2353
5.84M
      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
5.84M
      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
5.99M
    }
2363
5.99M
    lowercase_name = zend_string_tolower_ex(internal_function->function_name, type == MODULE_PERSISTENT);
2364
5.99M
    lowercase_name = zend_new_interned_string(lowercase_name);
2365
5.99M
    reg_function = malloc(sizeof(zend_internal_function));
2366
5.99M
    memcpy(reg_function, &function, sizeof(zend_internal_function));
2367
5.99M
    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
5.99M
    uint32_t num_args = reg_function->common.num_args;
2376
5.99M
    if (reg_function->common.fn_flags & ZEND_ACC_VARIADIC) {
2377
187k
      num_args++;
2378
187k
    }
2379
2380
    /* If types of arguments have to be checked */
2381
5.99M
    if (reg_function->common.arg_info && num_args) {
2382
3.72M
      uint32_t i;
2383
10.9M
      for (i = 0; i < num_args; i++) {
2384
7.20M
        zend_internal_arg_info *arg_info = &reg_function->internal_function.arg_info[i];
2385
7.20M
        ZEND_ASSERT(arg_info->name && "Parameter must have a name");
2386
7.20M
        if (ZEND_TYPE_IS_SET(arg_info->type)) {
2387
6.12M
            reg_function->common.fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
2388
6.12M
        }
2389
7.20M
#if ZEND_DEBUG
2390
12.9M
        for (uint32_t j = 0; j < i; j++) {
2391
5.77M
          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
5.77M
        }
2397
7.20M
#endif
2398
7.20M
      }
2399
3.72M
    }
2400
2401
5.99M
    if (reg_function->common.arg_info &&
2402
5.99M
        (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
4.07M
      uint32_t i;
2405
4.07M
      zend_arg_info *arg_info = reg_function->common.arg_info - 1;
2406
4.07M
      zend_arg_info *new_arg_info;
2407
2408
      /* Treat return type as an extra argument */
2409
4.07M
      num_args++;
2410
4.07M
      new_arg_info = malloc(sizeof(zend_arg_info) * num_args);
2411
4.07M
      memcpy(new_arg_info, arg_info, sizeof(zend_arg_info) * num_args);
2412
4.07M
      reg_function->common.arg_info = new_arg_info + 1;
2413
15.0M
      for (i = 0; i < num_args; i++) {
2414
11.0M
        if (ZEND_TYPE_HAS_CLASS(new_arg_info[i].type)) {
2415
479k
          ZEND_ASSERT(ZEND_TYPE_HAS_NAME(new_arg_info[i].type)
2416
479k
            && "Only simple classes are currently supported");
2417
479k
          const char *class_name = ZEND_TYPE_LITERAL_NAME(new_arg_info[i].type);
2418
479k
          ZEND_TYPE_SET_PTR(new_arg_info[i].type,
2419
479k
            zend_string_init_interned(class_name, strlen(class_name), 1));
2420
479k
        }
2421
11.0M
      }
2422
4.07M
    }
2423
2424
5.99M
    if (scope) {
2425
2.96M
      zend_check_magic_method_implementation(
2426
2.96M
        scope, reg_function, lowercase_name, E_CORE_ERROR);
2427
2.96M
      zend_add_magic_method(scope, reg_function, lowercase_name);
2428
2.96M
    }
2429
5.99M
    ptr++;
2430
5.99M
    count++;
2431
5.99M
    zend_string_release(lowercase_name);
2432
5.99M
  }
2433
422k
  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
422k
  return SUCCESS;
2448
422k
}
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
939k
{
2566
939k
  zend_module_entry **p = module_request_startup_handlers;
2567
2568
5.63M
  while (*p) {
2569
4.69M
    zend_module_entry *module = *p;
2570
2571
4.69M
    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
4.69M
    p++;
2576
4.69M
  }
2577
939k
}
2578
/* }}} */
2579
2580
ZEND_API void zend_deactivate_modules(void) /* {{{ */
2581
938k
{
2582
938k
  EG(current_execute_data) = NULL; /* we're no longer executing anything */
2583
2584
938k
  zend_try {
2585
938k
    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
938k
    } else {
2597
938k
      zend_module_entry **p = module_request_shutdown_handlers;
2598
2599
5.63M
      while (*p) {
2600
4.69M
        zend_module_entry *module = *p;
2601
2602
4.69M
        module->request_shutdown_func(module->type, module->module_number);
2603
4.69M
        p++;
2604
4.69M
      }
2605
938k
    }
2606
938k
  } zend_end_try();
2607
938k
}
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
938k
{
2623
938k
  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
938k
  } else {
2643
938k
    zend_module_entry **p = module_post_deactivate_handlers;
2644
2645
938k
    while (*p) {
2646
0
      zend_module_entry *module = *p;
2647
2648
0
      module->post_deactivate_func();
2649
0
      p++;
2650
0
    }
2651
938k
  }
2652
938k
}
2653
/* }}} */
2654
2655
/* return the next free module number */
2656
ZEND_API int zend_next_free_module(void) /* {{{ */
2657
36.6k
{
2658
36.6k
  return zend_hash_num_elements(&module_registry) + 1;
2659
36.6k
}
2660
/* }}} */
2661
2662
static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
2663
463k
{
2664
463k
  zend_class_entry *class_entry = malloc(sizeof(zend_class_entry));
2665
463k
  zend_string *lowercase_name;
2666
463k
  *class_entry = *orig_class_entry;
2667
2668
463k
  class_entry->type = ZEND_INTERNAL_CLASS;
2669
463k
  zend_initialize_class_data(class_entry, 0);
2670
463k
  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
463k
  class_entry->info.internal.module = EG(current_module);
2672
2673
463k
  if (class_entry->info.internal.builtin_functions) {
2674
386k
    zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, EG(current_module)->type);
2675
386k
  }
2676
2677
463k
  lowercase_name = zend_string_tolower_ex(orig_class_entry->name, EG(current_module)->type == MODULE_PERSISTENT);
2678
463k
  lowercase_name = zend_new_interned_string(lowercase_name);
2679
463k
  zend_hash_update_ptr(CG(class_table), lowercase_name, class_entry);
2680
463k
  zend_string_release_ex(lowercase_name, 1);
2681
463k
  return class_entry;
2682
463k
}
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
256k
{
2692
256k
  zend_class_entry *register_class;
2693
2694
256k
  register_class = zend_register_internal_class(class_entry);
2695
2696
256k
  if (parent_ce) {
2697
227k
    zend_do_inheritance(register_class, parent_ce);
2698
227k
    zend_build_properties_info_table(register_class);
2699
227k
  }
2700
256k
  return register_class;
2701
256k
}
2702
/* }}} */
2703
2704
ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...) /* {{{ */
2705
276k
{
2706
276k
  zend_class_entry *interface_entry;
2707
276k
  va_list interface_list;
2708
276k
  va_start(interface_list, num_interfaces);
2709
2710
561k
  while (num_interfaces--) {
2711
284k
    interface_entry = va_arg(interface_list, zend_class_entry *);
2712
284k
    zend_do_implement_interface(class_entry, interface_entry);
2713
284k
  }
2714
2715
276k
  va_end(interface_list);
2716
276k
}
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
398k
{
2723
398k
  return do_register_internal_class(orig_class_entry, 0);
2724
398k
}
2725
/* }}} */
2726
2727
ZEND_API zend_class_entry *zend_register_internal_interface(zend_class_entry *orig_class_entry) /* {{{ */
2728
65.0k
{
2729
65.0k
  return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE);
2730
65.0k
}
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
1.11k
{
2735
1.11k
  zend_string *lcname;
2736
1.11k
  zval zv, *ret;
2737
2738
  /* TODO: Move this out of here in 7.4. */
2739
1.11k
  if (persistent && EG(current_module) && EG(current_module)->type == MODULE_TEMPORARY) {
2740
0
    persistent = 0;
2741
0
  }
2742
2743
1.11k
  if (name[0] == '\\') {
2744
33
    lcname = zend_string_alloc(name_len-1, persistent);
2745
33
    zend_str_tolower_copy(ZSTR_VAL(lcname), name+1, name_len-1);
2746
1.08k
  } else {
2747
1.08k
    lcname = zend_string_alloc(name_len, persistent);
2748
1.08k
    zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len);
2749
1.08k
  }
2750
2751
1.11k
  zend_assert_valid_class_name(lcname);
2752
2753
1.11k
  lcname = zend_new_interned_string(lcname);
2754
2755
1.11k
  ZVAL_ALIAS_PTR(&zv, ce);
2756
1.11k
  ret = zend_hash_add(CG(class_table), lcname, &zv);
2757
1.11k
  zend_string_release_ex(lcname, 0);
2758
1.11k
  if (ret) {
2759
924
    if (!(ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
2760
924
      ce->refcount++;
2761
924
    }
2762
924
    return SUCCESS;
2763
924
  }
2764
193
  return FAILURE;
2765
193
}
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
130k
{
2795
130k
  zend_hash_str_del(CG(function_table), function_name, function_name_length);
2796
130k
}
2797
2798
ZEND_API void zend_disable_functions(const char *function_list) /* {{{ */
2799
4.06k
{
2800
4.06k
  if (!function_list || !*function_list) {
2801
0
    return;
2802
0
  }
2803
2804
4.06k
  const char *s = NULL, *e = function_list;
2805
1.13M
  while (*e) {
2806
1.12M
    switch (*e) {
2807
0
      case ' ':
2808
126k
      case ',':
2809
126k
        if (s) {
2810
126k
          zend_disable_function(s, e - s);
2811
126k
          s = NULL;
2812
126k
        }
2813
126k
        break;
2814
1.00M
      default:
2815
1.00M
        if (!s) {
2816
130k
          s = e;
2817
130k
        }
2818
1.00M
        break;
2819
1.12M
    }
2820
1.12M
    e++;
2821
1.12M
  }
2822
4.06k
  if (s) {
2823
4.06k
    zend_disable_function(s, e - s);
2824
4.06k
  }
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
4.06k
  zend_hash_rehash(CG(function_table));
2830
4.06k
}
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
18.8k
{
2894
18.8k
  return frame && frame->func ? frame->func->common.scope : NULL;
2895
18.8k
}
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
12.9k
{
2899
12.9k
  bool ret = 0;
2900
12.9k
  zend_class_entry *ce;
2901
12.9k
  size_t name_len = ZSTR_LEN(name);
2902
12.9k
  zend_string *lcname;
2903
12.9k
  ALLOCA_FLAG(use_heap);
2904
2905
12.9k
  ZSTR_ALLOCA_ALLOC(lcname, name_len, use_heap);
2906
12.9k
  zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name), name_len);
2907
2908
12.9k
  *strict_class = 0;
2909
12.9k
  if (zend_string_equals_literal(lcname, "self")) {
2910
1.13k
    if (!scope) {
2911
64
      if (error) *error = estrdup("cannot access \"self\" when no class scope is active");
2912
1.06k
    } else {
2913
1.06k
      fcc->called_scope = zend_get_called_scope(frame);
2914
1.06k
      fcc->calling_scope = scope;
2915
1.06k
      if (!fcc->object) {
2916
1.00k
        fcc->object = zend_get_this_object(frame);
2917
1.00k
      }
2918
1.06k
      ret = 1;
2919
1.06k
    }
2920
11.8k
  } else if (zend_string_equals_literal(lcname, "parent")) {
2921
529
    if (!scope) {
2922
3
      if (error) *error = estrdup("cannot access \"parent\" when no class scope is active");
2923
526
    } else if (!scope->parent) {
2924
20
      if (error) *error = estrdup("cannot access \"parent\" when current class scope has no parent");
2925
506
    } else {
2926
506
      fcc->called_scope = zend_get_called_scope(frame);
2927
506
      fcc->calling_scope = scope->parent;
2928
506
      if (!fcc->object) {
2929
357
        fcc->object = zend_get_this_object(frame);
2930
357
      }
2931
506
      *strict_class = 1;
2932
506
      ret = 1;
2933
506
    }
2934
11.3k
  } else if (zend_string_equals_literal(lcname, "static")) {
2935
5.45k
    zend_class_entry *called_scope = zend_get_called_scope(frame);
2936
2937
5.45k
    if (!called_scope) {
2938
69
      if (error) *error = estrdup("cannot access \"static\" when no class scope is active");
2939
5.38k
    } else {
2940
5.38k
      fcc->called_scope = called_scope;
2941
5.38k
      fcc->calling_scope = called_scope;
2942
5.38k
      if (!fcc->object) {
2943
5.36k
        fcc->object = zend_get_this_object(frame);
2944
5.36k
      }
2945
5.38k
      *strict_class = 1;
2946
5.38k
      ret = 1;
2947
5.38k
    }
2948
5.85k
  } else if ((ce = zend_lookup_class(name)) != NULL) {
2949
5.03k
    zend_class_entry *scope = get_scope(frame);
2950
5.03k
    fcc->calling_scope = ce;
2951
5.03k
    if (scope && !fcc->object) {
2952
3.54k
      zend_object *object = zend_get_this_object(frame);
2953
2954
3.54k
      if (object &&
2955
2.61k
          instanceof_function(object->ce, scope) &&
2956
2.61k
          instanceof_function(scope, ce)) {
2957
1.87k
        fcc->object = object;
2958
1.87k
        fcc->called_scope = object->ce;
2959
1.66k
      } else {
2960
1.66k
        fcc->called_scope = ce;
2961
1.66k
      }
2962
1.48k
    } else {
2963
1.46k
      fcc->called_scope = fcc->object ? fcc->object->ce : ce;
2964
1.48k
    }
2965
5.03k
    *strict_class = 1;
2966
5.03k
    ret = 1;
2967
822
  } else {
2968
822
    if (error) zend_spprintf(error, 0, "class \"%.*s\" not found", (int)name_len, ZSTR_VAL(name));
2969
822
  }
2970
12.9k
  ZSTR_ALLOCA_FREE(lcname, use_heap);
2971
12.9k
  return ret;
2972
12.9k
}
2973
/* }}} */
2974
2975
36.4k
ZEND_API void zend_release_fcall_info_cache(zend_fcall_info_cache *fcc) {
2976
36.4k
  if (fcc->function_handler &&
2977
34.7k
    (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
2978
669
    if (fcc->function_handler->common.function_name) {
2979
669
      zend_string_release_ex(fcc->function_handler->common.function_name, 0);
2980
669
    }
2981
669
    zend_free_trampoline(fcc->function_handler);
2982
669
  }
2983
36.4k
  fcc->function_handler = NULL;
2984
36.4k
}
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
91.0k
{
2988
91.0k
  zend_class_entry *ce_org = fcc->calling_scope;
2989
91.0k
  bool retval = 0;
2990
91.0k
  zend_string *mname, *cname;
2991
91.0k
  zend_string *lmname;
2992
91.0k
  const char *colon;
2993
91.0k
  size_t clen;
2994
91.0k
  HashTable *ftable;
2995
91.0k
  int call_via_handler = 0;
2996
91.0k
  zend_class_entry *scope;
2997
91.0k
  zval *zv;
2998
91.0k
  ALLOCA_FLAG(use_heap)
2999
3000
91.0k
  fcc->calling_scope = NULL;
3001
3002
91.0k
  if (!ce_org) {
3003
16.1k
    zend_function *func;
3004
16.1k
    zend_string *lmname;
3005
3006
    /* Check if function with given name exists.
3007
     * This may be a compound name that includes namespace name */
3008
16.1k
    if (UNEXPECTED(Z_STRVAL_P(callable)[0] == '\\')) {
3009
      /* Skip leading \ */
3010
216
      ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable) - 1, use_heap);
3011
216
      zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable) + 1, Z_STRLEN_P(callable) - 1);
3012
216
      func = zend_fetch_function(lmname);
3013
216
      ZSTR_ALLOCA_FREE(lmname, use_heap);
3014
15.9k
    } else {
3015
15.9k
      lmname = Z_STR_P(callable);
3016
15.9k
      func = zend_fetch_function(lmname);
3017
15.9k
      if (!func) {
3018
9.25k
        ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable), use_heap);
3019
9.25k
        zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable), Z_STRLEN_P(callable));
3020
9.25k
        func = zend_fetch_function(lmname);
3021
9.25k
        ZSTR_ALLOCA_FREE(lmname, use_heap);
3022
9.25k
      }
3023
15.9k
    }
3024
16.1k
    if (EXPECTED(func != NULL)) {
3025
7.79k
      fcc->function_handler = func;
3026
7.79k
      return 1;
3027
7.79k
    }
3028
83.2k
  }
3029
3030
  /* Split name into class/namespace and method/function names */
3031
83.2k
  if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL &&
3032
83.2k
    colon > Z_STRVAL_P(callable) &&
3033
8.04k
    *(colon-1) == ':'
3034
7.63k
  ) {
3035
7.63k
    size_t mlen;
3036
3037
7.63k
    colon--;
3038
7.63k
    clen = colon - Z_STRVAL_P(callable);
3039
7.63k
    mlen = Z_STRLEN_P(callable) - clen - 2;
3040
3041
7.63k
    if (colon == Z_STRVAL_P(callable)) {
3042
8
      if (error) *error = estrdup("invalid function name");
3043
8
      return 0;
3044
8
    }
3045
3046
    /* This is a compound name.
3047
     * Try to fetch class and then find static method. */
3048
7.62k
    if (ce_org) {
3049
273
      scope = ce_org;
3050
7.35k
    } else {
3051
7.35k
      scope = get_scope(frame);
3052
7.35k
    }
3053
3054
7.62k
    cname = zend_string_init(Z_STRVAL_P(callable), clen, 0);
3055
7.62k
    if (!zend_is_callable_check_class(cname, scope, frame, fcc, &strict_class, error)) {
3056
277
      zend_string_release_ex(cname, 0);
3057
277
      return 0;
3058
277
    }
3059
7.34k
    zend_string_release_ex(cname, 0);
3060
3061
7.34k
    ftable = &fcc->calling_scope->function_table;
3062
7.34k
    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
7.34k
    mname = zend_string_init(Z_STRVAL_P(callable) + clen + 2, mlen, 0);
3067
75.6k
  } else if (ce_org) {
3068
    /* Try to fetch find static method of given class. */
3069
74.6k
    mname = Z_STR_P(callable);
3070
74.6k
    zend_string_addref(mname);
3071
74.6k
    ftable = &ce_org->function_table;
3072
74.6k
    fcc->calling_scope = ce_org;
3073
965
  } else {
3074
    /* We already checked for plain function before. */
3075
965
    if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
3076
563
      zend_spprintf(error, 0, "function \"%s\" not found or invalid function name", Z_STRVAL_P(callable));
3077
563
    }
3078
965
    return 0;
3079
965
  }
3080
3081
82.0k
  lmname = zend_string_tolower(mname);
3082
82.0k
  if (strict_class &&
3083
10.9k
      fcc->calling_scope &&
3084
10.9k
    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
82.0k
  } else if ((zv = zend_hash_find(ftable, lmname)) != NULL) {
3090
65.8k
    fcc->function_handler = Z_PTR_P(zv);
3091
65.8k
    retval = 1;
3092
65.8k
    if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) &&
3093
87
        !strict_class) {
3094
46
      scope = get_scope(frame);
3095
46
      if (scope &&
3096
46
          instanceof_function(fcc->function_handler->common.scope, scope)) {
3097
3098
46
        zv = zend_hash_find(&scope->function_table, lmname);
3099
46
        if (zv != NULL) {
3100
46
          zend_function *priv_fbc = Z_PTR_P(zv);
3101
3102
46
          if ((priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE)
3103
46
           && priv_fbc->common.scope == scope) {
3104
46
            fcc->function_handler = priv_fbc;
3105
46
          }
3106
46
        }
3107
46
      }
3108
46
    }
3109
65.8k
    if (!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC) &&
3110
997
        (fcc->calling_scope &&
3111
997
         ((fcc->object && fcc->calling_scope->__call) ||
3112
853
          (!fcc->object && fcc->calling_scope->__callstatic)))) {
3113
144
      scope = get_scope(frame);
3114
144
      if (fcc->function_handler->common.scope != scope) {
3115
82
        if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
3116
61
         || !zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope)) {
3117
42
          retval = 0;
3118
42
          fcc->function_handler = NULL;
3119
42
          goto get_function_via_handler;
3120
42
        }
3121
16.1k
      }
3122
144
    }
3123
16.1k
  } else {
3124
16.2k
get_function_via_handler:
3125
16.2k
    if (fcc->object && fcc->calling_scope == ce_org) {
3126
8.43k
      if (strict_class && ce_org->__call) {
3127
985
        fcc->function_handler = zend_get_call_trampoline_func(ce_org, mname, 0);
3128
985
        call_via_handler = 1;
3129
985
        retval = 1;
3130
7.44k
      } else {
3131
7.44k
        fcc->function_handler = fcc->object->handlers->get_method(&fcc->object, mname, NULL);
3132
7.44k
        if (fcc->function_handler) {
3133
2.74k
          if (strict_class &&
3134
20
              (!fcc->function_handler->common.scope ||
3135
20
               !instanceof_function(ce_org, fcc->function_handler->common.scope))) {
3136
20
            zend_release_fcall_info_cache(fcc);
3137
2.72k
          } else {
3138
2.72k
            retval = 1;
3139
2.72k
            call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3140
2.72k
          }
3141
2.74k
        }
3142
7.44k
      }
3143
7.79k
    } else if (fcc->calling_scope) {
3144
7.79k
      if (fcc->calling_scope->get_static_method) {
3145
0
        fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname);
3146
7.79k
      } else {
3147
7.79k
        fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, NULL);
3148
7.79k
      }
3149
7.79k
      if (fcc->function_handler) {
3150
7.26k
        retval = 1;
3151
7.26k
        call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3152
7.26k
        if (call_via_handler && !fcc->object) {
3153
6.56k
          zend_object *object = zend_get_this_object(frame);
3154
6.56k
          if (object &&
3155
700
              instanceof_function(object->ce, fcc->calling_scope)) {
3156
700
            fcc->object = object;
3157
700
          }
3158
6.56k
        }
3159
7.26k
      }
3160
7.79k
    }
3161
16.2k
  }
3162
3163
82.0k
  if (retval) {
3164
76.7k
    if (fcc->calling_scope && !call_via_handler) {
3165
65.7k
      if (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT) {
3166
126
        retval = 0;
3167
126
        if (error) {
3168
106
          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
106
        }
3170
65.6k
      } else if (!fcc->object && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
3171
86
        retval = 0;
3172
86
        if (error) {
3173
45
          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
45
        }
3175
86
      }
3176
65.7k
      if (retval
3177
65.5k
       && !(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC)) {
3178
935
        scope = get_scope(frame);
3179
935
        if (fcc->function_handler->common.scope != scope) {
3180
468
          if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
3181
340
           || (!zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope))) {
3182
340
            if (error) {
3183
300
              if (*error) {
3184
0
                efree(*error);
3185
0
              }
3186
300
              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
300
            }
3188
340
            retval = 0;
3189
340
          }
3190
468
        }
3191
935
      }
3192
65.7k
    }
3193
5.25k
  } else if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
3194
696
    if (fcc->calling_scope) {
3195
696
      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
696
  }
3200
82.0k
  zend_string_release_ex(lmname, 0);
3201
82.0k
  zend_string_release_ex(mname, 0);
3202
3203
82.0k
  if (fcc->object) {
3204
73.1k
    fcc->called_scope = fcc->object->ce;
3205
73.1k
    if (fcc->function_handler
3206
68.3k
     && (fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
3207
421
      fcc->object = NULL;
3208
421
    }
3209
73.1k
  }
3210
82.0k
  return retval;
3211
82.0k
}
3212
/* }}} */
3213
3214
ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *object) /* {{{ */
3215
1.00k
{
3216
1.00k
try_again:
3217
1.00k
  switch (Z_TYPE_P(callable)) {
3218
191
    case IS_STRING:
3219
191
      if (object) {
3220
5
        return zend_create_member_string(object->ce->name, Z_STR_P(callable));
3221
5
      }
3222
186
      return zend_string_copy(Z_STR_P(callable));
3223
3224
459
    case IS_ARRAY:
3225
459
    {
3226
459
      zval *method = NULL;
3227
459
      zval *obj = NULL;
3228
3229
459
      if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
3230
436
        obj = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 0);
3231
436
        method = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 1);
3232
436
      }
3233
3234
459
      if (obj == NULL || method == NULL || Z_TYPE_P(method) != IS_STRING) {
3235
37
        return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
3236
37
      }
3237
3238
422
      if (Z_TYPE_P(obj) == IS_STRING) {
3239
44
        return zend_create_member_string(Z_STR_P(obj), Z_STR_P(method));
3240
378
      } else if (Z_TYPE_P(obj) == IS_OBJECT) {
3241
263
        return zend_create_member_string(Z_OBJCE_P(obj)->name, Z_STR_P(method));
3242
115
      } else {
3243
115
        return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
3244
115
      }
3245
0
    }
3246
285
    case IS_OBJECT:
3247
285
    {
3248
285
      zend_class_entry *ce = Z_OBJCE_P(callable);
3249
285
      return zend_string_concat2(
3250
285
        ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
3251
285
        "::__invoke", sizeof("::__invoke") - 1);
3252
0
    }
3253
0
    case IS_REFERENCE:
3254
0
      callable = Z_REFVAL_P(callable);
3255
0
      goto try_again;
3256
70
    default:
3257
70
      return zval_get_string_func(callable);
3258
1.00k
  }
3259
1.00k
}
3260
/* }}} */
3261
3262
ZEND_API zend_string *zend_get_callable_name(zval *callable) /* {{{ */
3263
343
{
3264
343
  return zend_get_callable_name_ex(callable, NULL);
3265
343
}
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
182k
{
3272
182k
  zend_bool ret;
3273
182k
  zend_fcall_info_cache fcc_local;
3274
182k
  bool strict_class = 0;
3275
3276
182k
  if (fcc == NULL) {
3277
34.7k
    fcc = &fcc_local;
3278
34.7k
  }
3279
182k
  if (error) {
3280
151k
    *error = NULL;
3281
151k
  }
3282
3283
182k
  fcc->calling_scope = NULL;
3284
182k
  fcc->called_scope = NULL;
3285
182k
  fcc->function_handler = NULL;
3286
182k
  fcc->object = NULL;
3287
3288
182k
again:
3289
182k
  switch (Z_TYPE_P(callable)) {
3290
80.7k
    case IS_STRING:
3291
80.7k
      if (object) {
3292
64.5k
        fcc->object = object;
3293
64.5k
        fcc->calling_scope = object->ce;
3294
64.5k
      }
3295
3296
80.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
91.0k
check_func:
3302
91.0k
      ret = zend_is_callable_check_func(check_flags, callable, frame, fcc, strict_class, error);
3303
91.0k
      if (fcc == &fcc_local) {
3304
7.42k
        zend_release_fcall_info_cache(fcc);
3305
7.42k
      }
3306
91.0k
      return ret;
3307
3308
11.9k
    case IS_ARRAY:
3309
11.9k
      {
3310
11.9k
        zval *method = NULL;
3311
11.9k
        zval *obj = NULL;
3312
3313
11.9k
        if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
3314
11.6k
          obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0);
3315
11.6k
          method = zend_hash_index_find(Z_ARRVAL_P(callable), 1);
3316
11.6k
        }
3317
3318
11.9k
        do {
3319
11.9k
          if (obj == NULL || method == NULL) {
3320
264
            break;
3321
264
          }
3322
3323
11.6k
          ZVAL_DEREF(method);
3324
11.6k
          if (Z_TYPE_P(method) != IS_STRING) {
3325
104
            break;
3326
104
          }
3327
3328
11.5k
          ZVAL_DEREF(obj);
3329
11.5k
          if (Z_TYPE_P(obj) == IS_STRING) {
3330
5.34k
            if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3331
0
              return 1;
3332
0
            }
3333
3334
5.34k
            if (!zend_is_callable_check_class(Z_STR_P(obj), get_scope(frame), frame, fcc, &strict_class, error)) {
3335
701
              return 0;
3336
701
            }
3337
3338
6.19k
          } else if (Z_TYPE_P(obj) == IS_OBJECT) {
3339
3340
5.83k
            fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */
3341
3342
5.83k
            fcc->object = Z_OBJ_P(obj);
3343
3344
5.83k
            if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
3345
122
              fcc->called_scope = fcc->calling_scope;
3346
122
              return 1;
3347
122
            }
3348
358
          } else {
3349
358
            break;
3350
358
          }
3351
3352
10.3k
          callable = method;
3353
10.3k
          goto check_func;
3354
3355
0
        } while (0);
3356
726
        if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
3357
473
          if (!obj || (!Z_ISREF_P(obj)?
3358
469
                (Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT) :
3359
375
                (Z_TYPE_P(Z_REFVAL_P(obj)) != IS_STRING && Z_TYPE_P(Z_REFVAL_P(obj)) != IS_OBJECT))) {
3360
375
            if (error) *error = estrdup("first array member is not a valid class name or object");
3361
98
          } else {
3362
98
            if (error) *error = estrdup("second array member is not a valid method");
3363
98
          }
3364
253
        } else {
3365
253
          if (error) *error = estrdup("array must have exactly two members");
3366
253
        }
3367
726
      }
3368
726
      return 0;
3369
89.3k
    case IS_OBJECT:
3370
89.3k
      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
89.2k
        fcc->called_scope = fcc->calling_scope;
3372
89.2k
        if (fcc == &fcc_local) {
3373
25.9k
          zend_release_fcall_info_cache(fcc);
3374
25.9k
        }
3375
89.2k
        return 1;
3376
89.2k
      }
3377
127
      if (error) *error = estrdup("no array or string given");
3378
127
      return 0;
3379
33
    case IS_REFERENCE:
3380
33
      callable = Z_REFVAL_P(callable);
3381
33
      goto again;
3382
350
    default:
3383
350
      if (error) *error = estrdup("no array or string given");
3384
350
      return 0;
3385
182k
  }
3386
182k
}
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
182k
{
3391
  /* Determine callability at the first parent user frame. */
3392
182k
  zend_execute_data *frame = EG(current_execute_data);
3393
358k
  while (frame && (!frame->func || !ZEND_USER_CODE(frame->func->type))) {
3394
176k
    frame = frame->prev_execute_data;
3395
176k
  }
3396
3397
182k
  zend_bool ret = zend_is_callable_impl(callable, object, frame, check_flags, fcc, error);
3398
182k
  if (callable_name) {
3399
657
    *callable_name = zend_get_callable_name_ex(callable, object);
3400
657
  }
3401
182k
  return ret;
3402
182k
}
3403
3404
ZEND_API zend_bool zend_is_callable(zval *callable, uint32_t check_flags, zend_string **callable_name) /* {{{ */
3405
31.2k
{
3406
31.2k
  return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL);
3407
31.2k
}
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
27.7k
{
3430
27.7k
  if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, fcc, error)) {
3431
475
    return FAILURE;
3432
475
  }
3433
3434
27.2k
  fci->size = sizeof(*fci);
3435
27.2k
  fci->object = fcc->object;
3436
27.2k
  ZVAL_COPY_VALUE(&fci->function_name, callable);
3437
27.2k
  fci->retval = NULL;
3438
27.2k
  fci->param_count = 0;
3439
27.2k
  fci->params = NULL;
3440
27.2k
  fci->named_params = NULL;
3441
3442
27.2k
  return SUCCESS;
3443
27.2k
}
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
136k
{
3602
136k
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
3603
136k
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
3604
136k
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
3605
136k
    Z_TYPE_FLAGS_P(zv) = 0;
3606
136k
  }
3607
136k
  return Z_STR_P(zv);
3608
136k
}
3609
3610
309k
static zend_always_inline zend_bool is_persistent_class(zend_class_entry *ce) {
3611
309k
  return (ce->type & ZEND_INTERNAL_CLASS)
3612
292k
    && ce->info.internal.module->type == MODULE_PERSISTENT;
3613
309k
}
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
183k
{
3617
183k
  zend_property_info *property_info, *property_info_ptr;
3618
3619
183k
  if (ZEND_TYPE_IS_SET(type)) {
3620
38.3k
    ce->ce_flags |= ZEND_ACC_HAS_TYPE_HINTS;
3621
38.3k
  }
3622
3623
183k
  if (ce->type == ZEND_INTERNAL_CLASS) {
3624
126k
    property_info = pemalloc(sizeof(zend_property_info), 1);
3625
57.6k
  } else {
3626
57.6k
    property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
3627
57.6k
    if (Z_TYPE_P(property) == IS_CONSTANT_AST) {
3628
3.83k
      ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
3629
3.83k
    }
3630
57.6k
  }
3631
3632
183k
  if (Z_TYPE_P(property) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(property))) {
3633
78.9k
    zval_make_interned_string(property);
3634
78.9k
  }
3635
3636
183k
  if (!(access_type & ZEND_ACC_PPP_MASK)) {
3637
13.2k
    access_type |= ZEND_ACC_PUBLIC;
3638
13.2k
  }
3639
183k
  if (access_type & ZEND_ACC_STATIC) {
3640
11.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
11.9k
    } else {
3646
11.9k
      property_info->offset = ce->default_static_members_count++;
3647
11.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
11.9k
    }
3649
11.9k
    ZVAL_COPY_VALUE(&ce->default_static_members_table[property_info->offset], property);
3650
11.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
171k
  } else {
3660
171k
    zval *property_default_ptr;
3661
171k
    if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
3662
8.13k
        (property_info_ptr->flags & ZEND_ACC_STATIC) == 0) {
3663
8.13k
      property_info->offset = property_info_ptr->offset;
3664
8.13k
      zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]);
3665
8.13k
      zend_hash_del(&ce->properties_info, name);
3666
3667
8.13k
      ZEND_ASSERT(ce->type == ZEND_INTERNAL_CLASS);
3668
8.13k
      ZEND_ASSERT(ce->properties_info_table != NULL);
3669
8.13k
      ce->properties_info_table[OBJ_PROP_TO_NUM(property_info->offset)] = property_info;
3670
163k
    } else {
3671
163k
      property_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count);
3672
163k
      ce->default_properties_count++;
3673
163k
      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
163k
      if (ce->type == ZEND_INTERNAL_CLASS) {
3677
117k
        ce->properties_info_table = perealloc(ce->properties_info_table, sizeof(zend_property_info *) * ce->default_properties_count, 1);
3678
117k
        ce->properties_info_table[ce->default_properties_count - 1] = property_info;
3679
117k
      }
3680
163k
    }
3681
171k
    property_default_ptr = &ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
3682
171k
    ZVAL_COPY_VALUE(property_default_ptr, property);
3683
171k
    Z_PROP_FLAG_P(property_default_ptr) = Z_ISUNDEF_P(property) ? IS_PROP_UNINIT : 0;
3684
171k
  }
3685
183k
  if (ce->type & ZEND_INTERNAL_CLASS) {
3686
    /* Must be interned to avoid ZTS data races */
3687
126k
    if (is_persistent_class(ce)) {
3688
126k
      name = zend_new_interned_string(zend_string_copy(name));
3689
126k
    }
3690
3691
126k
    if (Z_REFCOUNTED_P(property)) {
3692
0
      zend_error_noreturn(E_CORE_ERROR, "Internal zvals cannot be refcounted");
3693
0
    }
3694
183k
  }
3695
3696
183k
  if (access_type & ZEND_ACC_PUBLIC) {
3697
106k
    property_info->name = zend_string_copy(name);
3698
77.4k
  } else if (access_type & ZEND_ACC_PRIVATE) {
3699
36.0k
    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
41.4k
  } else {
3701
41.4k
    ZEND_ASSERT(access_type & ZEND_ACC_PROTECTED);
3702
41.4k
    property_info->name = zend_mangle_property_name("*", 1, ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
3703
41.4k
  }
3704
3705
183k
  property_info->name = zend_new_interned_string(property_info->name);
3706
183k
  property_info->flags = access_type;
3707
183k
  property_info->doc_comment = doc_comment;
3708
183k
  property_info->attributes = NULL;
3709
183k
  property_info->ce = ce;
3710
183k
  property_info->type = type;
3711
3712
183k
  zend_hash_update_ptr(&ce->properties_info, name, property_info);
3713
3714
183k
  return property_info;
3715
183k
}
3716
/* }}} */
3717
3718
ZEND_API zend_result zend_try_assign_typed_ref_ex(zend_reference *ref, zval *val, zend_bool strict) /* {{{ */
3719
45
{
3720
45
  if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, val, strict))) {
3721
0
    zval_ptr_dtor(val);
3722
0
    return FAILURE;
3723
45
  } else {
3724
45
    zval_ptr_dtor(&ref->val);
3725
45
    ZVAL_COPY_VALUE(&ref->val, val);
3726
45
    return SUCCESS;
3727
45
  }
3728
45
}
3729
/* }}} */
3730
3731
ZEND_API zend_result zend_try_assign_typed_ref(zend_reference *ref, zval *val) /* {{{ */
3732
45
{
3733
45
  return zend_try_assign_typed_ref_ex(ref, val, ZEND_ARG_USES_STRICT_TYPES());
3734
45
}
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
45
{
3811
45
  zval tmp;
3812
3813
45
  ZVAL_ARR(&tmp, arr);
3814
45
  return zend_try_assign_typed_ref(ref, &tmp);
3815
45
}
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
105k
{
3847
105k
  zend_declare_typed_property(ce, name, property, access_type, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
3848
105k
}
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
105k
{
3853
105k
  zend_string *key = zend_string_init(name, name_length, is_persistent_class(ce));
3854
105k
  zend_declare_property_ex(ce, key, property, access_type, NULL);
3855
105k
  zend_string_release(key);
3856
105k
}
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
20.3k
{
3861
20.3k
  zval property;
3862
3863
20.3k
  ZVAL_NULL(&property);
3864
20.3k
  zend_declare_property(ce, name, name_length, &property, access_type);
3865
20.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
12.2k
{
3879
12.2k
  zval property;
3880
3881
12.2k
  ZVAL_LONG(&property, value);
3882
12.2k
  zend_declare_property(ce, name, name_length, &property, access_type);
3883
12.2k
}
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
73.2k
{
3897
73.2k
  zval property;
3898
3899
73.2k
  ZVAL_NEW_STR(&property, zend_string_init(value, strlen(value), ce->type & ZEND_INTERNAL_CLASS));
3900
73.2k
  zend_declare_property(ce, name, name_length, &property, access_type);
3901
73.2k
}
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
460k
{
3915
460k
  zend_class_constant *c;
3916
3917
460k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
3918
53.3k
    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
460k
  }
3922
3923
460k
  if (zend_string_equals_literal_ci(name, "class")) {
3924
20
    zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
3925
20
        "A class constant must not be called 'class'; it is reserved for class name fetching");
3926
20
  }
3927
3928
460k
  if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) {
3929
57.7k
    zval_make_interned_string(value);
3930
57.7k
  }
3931
3932
460k
  if (ce->type == ZEND_INTERNAL_CLASS) {
3933
451k
    c = pemalloc(sizeof(zend_class_constant), 1);
3934
9.27k
  } else {
3935
9.27k
    c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
3936
9.27k
  }
3937
460k
  ZVAL_COPY_VALUE(&c->value, value);
3938
460k
  Z_ACCESS_FLAGS(c->value) = access_type;
3939
460k
  c->doc_comment = doc_comment;
3940
460k
  c->attributes = NULL;
3941
460k
  c->ce = ce;
3942
460k
  if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
3943
1.58k
    ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
3944
1.58k
  }
3945
3946
460k
  if (!zend_hash_add_ptr(&ce->constants_table, name, c)) {
3947
25
    zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
3948
25
      "Cannot redefine class constant %s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
3949
25
  }
3950
3951
460k
  return c;
3952
460k
}
3953
/* }}} */
3954
3955
ZEND_API void zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
3956
451k
{
3957
451k
  zend_string *key;
3958
3959
451k
  if (ce->type == ZEND_INTERNAL_CLASS) {
3960
451k
    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
451k
  zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
3965
451k
  zend_string_release(key);
3966
451k
}
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
398k
{
3980
398k
  zval constant;
3981
3982
398k
  ZVAL_LONG(&constant, value);
3983
398k
  zend_declare_class_constant(ce, name, name_length, &constant);
3984
398k
}
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
52.8k
{
4007
52.8k
  zval constant;
4008
4009
52.8k
  ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type & ZEND_INTERNAL_CLASS));
4010
52.8k
  zend_declare_class_constant(ce, name, name_length, &constant);
4011
52.8k
}
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
9.86M
{
4022
9.86M
  zend_class_entry *old_scope = EG(fake_scope);
4023
4024
9.86M
  EG(fake_scope) = scope;
4025
4026
9.86M
  object->handlers->write_property(object, name, value, NULL);
4027
4028
9.86M
  EG(fake_scope) = old_scope;
4029
9.86M
}
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
0
{
4058
0
  zend_string *property;
4059
0
  zend_class_entry *old_scope = EG(fake_scope);
4060
4061
0
  EG(fake_scope) = scope;
4062
4063
0
  property = zend_string_init(name, name_length, 0);
4064
0
  object->handlers->unset_property(object, property, 0);
4065
0
  zend_string_release_ex(property, 0);
4066
4067
0
  EG(fake_scope) = old_scope;
4068
0
}
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
83
{
4129
83
  zval *property, tmp;
4130
83
  zend_property_info *prop_info;
4131
83
  zend_class_entry *old_scope = EG(fake_scope);
4132
4133
83
  if (UNEXPECTED(!(scope->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
4134
66
    if (UNEXPECTED(zend_update_class_constants(scope)) != SUCCESS) {
4135
25
      return FAILURE;
4136
25
    }
4137
58
  }
4138
4139
58
  EG(fake_scope) = scope;
4140
58
  property = zend_std_get_static_property_with_info(scope, name, BP_VAR_W, &prop_info);
4141
58
  EG(fake_scope) = old_scope;
4142
4143
58
  if (!property) {
4144
0
    return FAILURE;
4145
0
  }
4146
4147
58
  ZEND_ASSERT(!Z_ISREF_P(value));
4148
58
  Z_TRY_ADDREF_P(value);
4149
58
  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
58
  zend_assign_to_variable(property, value, IS_TMP_VAR, /* strict */ 0);
4159
58
  return SUCCESS;
4160
58
}
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
193M
{
4230
193M
  zval *value;
4231
193M
  zend_class_entry *old_scope = EG(fake_scope);
4232
4233
193M
  EG(fake_scope) = scope;
4234
4235
192M
  value = object->handlers->read_property(object, name, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
4236
4237
193M
  EG(fake_scope) = old_scope;
4238
193M
  return value;
4239
193M
}
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
43
{
4256
43
  zval *property;
4257
43
  zend_class_entry *old_scope = EG(fake_scope);
4258
4259
43
  EG(fake_scope) = scope;
4260
43
  property = zend_std_get_static_property(scope, name, silent ? BP_VAR_IS : BP_VAR_R);
4261
43
  EG(fake_scope) = old_scope;
4262
4263
43
  return property;
4264
43
}
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
1.17M
{
4278
1.17M
  current->handling = EG(error_handling);
4279
1.17M
  current->exception = EG(exception_class);
4280
1.17M
  ZVAL_COPY(&current->user_handler, &EG(user_error_handler));
4281
1.17M
}
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
1.17M
{
4286
1.17M
  if (current) {
4287
1.17M
    zend_save_error_handling(current);
4288
1.17M
    if (error_handling != EH_NORMAL && Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
4289
20
      zval_ptr_dtor(&EG(user_error_handler));
4290
20
      ZVAL_UNDEF(&EG(user_error_handler));
4291
20
    }
4292
1.17M
  }
4293
1.17M
  EG(error_handling) = error_handling;
4294
1.17M
  EG(exception_class) = error_handling == EH_THROW ? exception_class : NULL;
4295
1.17M
}
4296
/* }}} */
4297
4298
ZEND_API void zend_restore_error_handling(zend_error_handling *saved) /* {{{ */
4299
1.17M
{
4300
1.17M
  EG(error_handling) = saved->handling;
4301
1.17M
  EG(exception_class) = saved->handling == EH_THROW ? saved->exception : NULL;
4302
1.17M
  if (Z_TYPE(saved->user_handler) != IS_UNDEF) {
4303
20
    zval_ptr_dtor(&EG(user_error_handler));
4304
20
    ZVAL_COPY_VALUE(&EG(user_error_handler), &saved->user_handler);
4305
20
    ZVAL_UNDEF(&saved->user_handler);
4306
20
  }
4307
1.17M
}
4308
/* }}} */
4309
4310
ZEND_API ZEND_COLD const char *zend_get_object_type(const zend_class_entry *ce) /* {{{ */
4311
792
{
4312
792
  if(ce->ce_flags & ZEND_ACC_TRAIT) {
4313
119
    return "trait";
4314
673
  } else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
4315
94
    return "interface";
4316
579
  } else {
4317
579
    return "class";
4318
579
  }
4319
792
}
4320
/* }}} */
4321
4322
ZEND_API zend_bool zend_is_iterable(zval *iterable) /* {{{ */
4323
3.73k
{
4324
3.73k
  switch (Z_TYPE_P(iterable)) {
4325
1.46k
    case IS_ARRAY:
4326
1.46k
      return 1;
4327
2.03k
    case IS_OBJECT:
4328
2.03k
      return zend_class_implements_interface(Z_OBJCE_P(iterable), zend_ce_traversable);
4329
233
    default:
4330
233
      return 0;
4331
3.73k
  }
4332
3.73k
}
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
136
static zend_string *try_parse_string(const char *str, size_t len, char quote) {
4388
136
  if (len == 0) {
4389
0
    return ZSTR_EMPTY_ALLOC();
4390
0
  }
4391
4392
272
  for (size_t i = 0; i < len; i++) {
4393
136
    if (str[i] == '\\' || str[i] == quote) {
4394
0
      return NULL;
4395
0
    }
4396
136
  }
4397
136
  return zend_string_init(str, len, 0);
4398
136
}
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
554
{
4403
554
  const char *default_value = arg_info->default_value;
4404
554
  if (!default_value) {
4405
41
    return FAILURE;
4406
41
  }
4407
4408
  /* Avoid going through the full AST machinery for some simple and common cases. */
4409
513
  size_t default_value_len = strlen(default_value);
4410
513
  zend_ulong lval;
4411
513
  if (default_value_len == sizeof("null")-1
4412
377
      && !memcmp(default_value, "null", sizeof("null")-1)) {
4413
377
    ZVAL_NULL(default_value_zval);
4414
377
    return SUCCESS;
4415
136
  } 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
136
  } 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
136
  } else if (default_value_len >= 2
4424
136
      && (default_value[0] == '\'' || default_value[0] == '"')
4425
136
      && default_value[default_value_len - 1] == default_value[0]) {
4426
136
    zend_string *str = try_parse_string(
4427
136
      default_value + 1, default_value_len - 2, default_value[0]);
4428
136
    if (str) {
4429
136
      ZVAL_STR(default_value_zval, str);
4430
136
      return SUCCESS;
4431
136
    }
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
}