Coverage Report

Created: 2026-10-28 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_builtin_functions.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright © Zend Technologies Ltd., a subsidiary company of          |
6
   |     Perforce Software, Inc., and Contributors.                       |
7
   +----------------------------------------------------------------------+
8
   | This source file is subject to the Modified BSD License that is      |
9
   | bundled with this package in the file LICENSE, and is available      |
10
   | through the World Wide Web at <https://www.php.net/license/>.        |
11
   |                                                                      |
12
   | SPDX-License-Identifier: BSD-3-Clause                                |
13
   +----------------------------------------------------------------------+
14
   | Authors: Andi Gutmans <andi@php.net>                                 |
15
   |          Zeev Suraski <zeev@php.net>                                 |
16
   +----------------------------------------------------------------------+
17
*/
18
19
#include "php_version.h"
20
#include "zend.h"
21
#include "zend_API.h"
22
#include "zend_attributes.h"
23
#include "zend_gc.h"
24
#include "zend_builtin_functions.h"
25
#include "zend_constants.h"
26
#include "zend_ini.h"
27
#include "zend_interfaces.h"
28
#include "zend_exceptions.h"
29
#include "zend_extensions.h"
30
#include "zend_closures.h"
31
#include "zend_generators.h"
32
#include "zend_autoload.h"
33
#include "zend_builtin_functions_arginfo.h"
34
#include "zend_smart_str.h"
35
36
/* }}} */
37
38
16
ZEND_MINIT_FUNCTION(core) { /* {{{ */
39
16
  zend_autoload = zend_perform_class_autoload;
40
16
  zend_register_default_classes();
41
42
16
  zend_standard_class_def = register_class_stdClass();
43
44
16
  return SUCCESS;
45
16
}
46
/* }}} */
47
48
static zend_module_entry zend_builtin_module = { /* {{{ */
49
  STANDARD_MODULE_HEADER,
50
  "Core",
51
  ext_functions,
52
  ZEND_MINIT(core),
53
  NULL,
54
  NULL,
55
  NULL,
56
  NULL,
57
  ZEND_VERSION,
58
  STANDARD_MODULE_PROPERTIES
59
};
60
/* }}} */
61
62
zend_result zend_startup_builtin_functions(void) /* {{{ */
63
16
{
64
16
  zend_module_entry *module;
65
16
  EG(current_module) = module = zend_register_module_ex(&zend_builtin_module, MODULE_PERSISTENT);
66
16
  if (UNEXPECTED(module == NULL)) {
67
0
    return FAILURE;
68
0
  }
69
16
  ZEND_ASSERT(module->module_number == 0);
70
16
  return SUCCESS;
71
16
}
72
/* }}} */
73
74
ZEND_FUNCTION(clone)
75
1
{
76
1
  zend_object *zobj;
77
1
  HashTable *with = (HashTable*)&zend_empty_array;
78
79
3
  ZEND_PARSE_PARAMETERS_START(1, 2)
80
4
    Z_PARAM_OBJ(zobj)
81
1
    Z_PARAM_OPTIONAL
82
4
    Z_PARAM_ARRAY_HT(with)
83
1
  ZEND_PARSE_PARAMETERS_END();
84
85
  /* clone() also exists as the ZEND_CLONE OPcode and both implementations must be kept in sync. */
86
87
1
  const zend_class_entry *scope = zend_get_executed_scope();
88
89
1
  const zend_class_entry *ce = zobj->ce;
90
1
  const zend_function *clone = ce->clone;
91
92
1
  if (UNEXPECTED(zobj->handlers->clone_obj == NULL)) {
93
0
    zend_throw_error(NULL, "Trying to clone an uncloneable object of class %s", ZSTR_VAL(ce->name));
94
0
    RETURN_THROWS();
95
0
  }
96
97
1
  if (clone && !zend_check_method_accessible(clone, scope)) {
98
0
    zend_bad_method_call(clone, clone->common.function_name, scope);
99
0
    RETURN_THROWS();
100
0
  }
101
102
1
  zend_object *cloned;
103
1
  if (zend_hash_num_elements(with) > 0) {
104
1
    if (UNEXPECTED(!zobj->handlers->clone_obj_with)) {
105
0
      zend_throw_error(NULL, "Cloning objects of class %s with updated properties is not supported", ZSTR_VAL(ce->name));
106
0
      RETURN_THROWS();
107
0
    }
108
109
1
    cloned = zobj->handlers->clone_obj_with(zobj, scope, with);
110
1
  } else {
111
0
    cloned = zobj->handlers->clone_obj(zobj);
112
0
  }
113
114
1
  ZEND_ASSERT(cloned || EG(exception));
115
1
  if (EXPECTED(cloned)) {
116
1
    RETURN_OBJ(cloned);
117
1
  }
118
1
}
119
120
ZEND_FUNCTION(exit)
121
26
{
122
26
  zend_string *str = NULL;
123
26
  zend_long status = 0;
124
125
78
  ZEND_PARSE_PARAMETERS_START(0, 1)
126
78
    Z_PARAM_OPTIONAL
127
78
    Z_PARAM_STR_OR_LONG(str, status)
128
64
  ZEND_PARSE_PARAMETERS_END();
129
130
26
  if (str) {
131
4
    size_t len = ZSTR_LEN(str);
132
4
    if (len != 0) {
133
      /* An exception might be emitted by an output handler */
134
4
      zend_write(ZSTR_VAL(str), len);
135
4
      if (EG(exception)) {
136
0
        RETURN_THROWS();
137
0
      }
138
4
    }
139
22
  } else {
140
22
    EG(exit_status) = status;
141
22
  }
142
143
26
  ZEND_ASSERT(!EG(exception));
144
26
  zend_throw_unwind_exit();
145
26
}
146
147
/* {{{ Get the version of the Zend Engine */
148
ZEND_FUNCTION(zend_version)
149
0
{
150
0
  ZEND_PARSE_PARAMETERS_NONE();
151
152
0
  RETURN_STRINGL(ZEND_VERSION, sizeof(ZEND_VERSION)-1);
153
0
}
154
/* }}} */
155
156
/* {{{ Reclaims memory used by MM caches.
157
   Returns number of freed bytes */
158
ZEND_FUNCTION(gc_mem_caches)
159
0
{
160
0
  ZEND_PARSE_PARAMETERS_NONE();
161
162
0
  RETURN_LONG(zend_mm_gc(zend_mm_get_heap()));
163
0
}
164
/* }}} */
165
166
/* {{{ Forces collection of any existing garbage cycles.
167
   Returns number of freed zvals */
168
ZEND_FUNCTION(gc_collect_cycles)
169
17
{
170
17
  ZEND_PARSE_PARAMETERS_NONE();
171
172
17
  RETURN_LONG(gc_collect_cycles());
173
17
}
174
/* }}} */
175
176
/* {{{ Returns status of the circular reference collector */
177
ZEND_FUNCTION(gc_enabled)
178
12
{
179
12
  ZEND_PARSE_PARAMETERS_NONE();
180
181
11
  RETURN_BOOL(gc_enabled());
182
11
}
183
/* }}} */
184
185
/* {{{ Activates the circular reference collector */
186
ZEND_FUNCTION(gc_enable)
187
4
{
188
4
  zend_string *key;
189
190
4
  ZEND_PARSE_PARAMETERS_NONE();
191
192
4
  key = ZSTR_INIT_LITERAL("zend.enable_gc", 0);
193
4
  zend_alter_ini_entry_chars(key, "1", sizeof("1")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
194
4
  zend_string_release_ex(key, 0);
195
4
}
196
/* }}} */
197
198
/* {{{ Deactivates the circular reference collector */
199
ZEND_FUNCTION(gc_disable)
200
4
{
201
4
  zend_string *key;
202
203
4
  ZEND_PARSE_PARAMETERS_NONE();
204
205
4
  key = ZSTR_INIT_LITERAL("zend.enable_gc", 0);
206
4
  zend_alter_ini_entry_chars(key, "0", sizeof("0")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
207
4
  zend_string_release_ex(key, 0);
208
4
}
209
/* }}} */
210
211
/* {{{ Returns current GC statistics */
212
ZEND_FUNCTION(gc_status)
213
0
{
214
0
  zend_gc_status status;
215
216
0
  ZEND_PARSE_PARAMETERS_NONE();
217
218
0
  zend_gc_get_status(&status);
219
220
0
  array_init_size(return_value, 16);
221
222
0
  add_assoc_bool_ex(return_value, "running", sizeof("running")-1, status.active);
223
0
  add_assoc_bool_ex(return_value, "protected", sizeof("protected")-1, status.gc_protected);
224
0
  add_assoc_bool_ex(return_value, "full", sizeof("full")-1, status.full);
225
0
  add_assoc_long_ex(return_value, "runs", sizeof("runs")-1, (long)status.runs);
226
0
  add_assoc_long_ex(return_value, "collected", sizeof("collected")-1, (long)status.collected);
227
0
  add_assoc_long_ex(return_value, "threshold", sizeof("threshold")-1, (long)status.threshold);
228
0
  add_assoc_long_ex(return_value, "buffer_size", sizeof("buffer_size")-1, (long)status.buf_size);
229
0
  add_assoc_long_ex(return_value, "roots", sizeof("roots")-1, (long)status.num_roots);
230
231
  /* Using double because zend_long may be too small on some platforms */
232
0
  add_assoc_double_ex(return_value, "application_time", sizeof("application_time")-1, (double) status.application_time / ZEND_NANO_IN_SEC);
233
0
  add_assoc_double_ex(return_value, "collector_time", sizeof("collector_time")-1, (double) status.collector_time / ZEND_NANO_IN_SEC);
234
0
  add_assoc_double_ex(return_value, "destructor_time", sizeof("destructor_time")-1, (double) status.dtor_time / ZEND_NANO_IN_SEC);
235
0
  add_assoc_double_ex(return_value, "free_time", sizeof("free_time")-1, (double) status.free_time / ZEND_NANO_IN_SEC);
236
0
}
237
/* }}} */
238
239
/* {{{ Get the number of arguments that were passed to the function */
240
ZEND_FUNCTION(func_num_args)
241
0
{
242
0
  const zend_execute_data *ex = EX(prev_execute_data);
243
244
0
  ZEND_PARSE_PARAMETERS_NONE();
245
246
0
  if (ex && (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE)) {
247
0
    zend_throw_error(NULL, "func_num_args() must be called from a function context");
248
0
    RETURN_THROWS();
249
0
  }
250
251
0
  if (zend_forbid_dynamic_call() == FAILURE) {
252
0
    RETURN_THROWS();
253
0
  }
254
255
0
  RETURN_LONG(ZEND_CALL_NUM_ARGS(ex));
256
0
}
257
/* }}} */
258
259
/* {{{ Get the $arg_num'th argument that was passed to the function */
260
ZEND_FUNCTION(func_get_arg)
261
0
{
262
0
  uint32_t arg_count, first_extra_arg;
263
0
  zval *arg;
264
0
  zend_long requested_offset;
265
0
  zend_execute_data *ex;
266
267
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &requested_offset) == FAILURE) {
268
0
    RETURN_THROWS();
269
0
  }
270
271
0
  if (requested_offset < 0) {
272
0
    zend_argument_value_error(1, "must be greater than or equal to 0");
273
0
    RETURN_THROWS();
274
0
  }
275
276
0
  ex = EX(prev_execute_data);
277
0
  if (ex && (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE)) {
278
0
    zend_throw_error(NULL, "func_get_arg() cannot be called from the global scope");
279
0
    RETURN_THROWS();
280
0
  }
281
282
0
  if (zend_forbid_dynamic_call() == FAILURE) {
283
0
    RETURN_THROWS();
284
0
  }
285
286
0
  arg_count = ZEND_CALL_NUM_ARGS(ex);
287
288
0
  if ((zend_ulong)requested_offset >= arg_count) {
289
0
    zend_argument_value_error(1, "must be less than the number of the arguments passed to the currently executed function");
290
0
    RETURN_THROWS();
291
0
  }
292
293
0
  first_extra_arg = ex->func->op_array.num_args;
294
0
  if ((zend_ulong)requested_offset >= first_extra_arg && (ZEND_CALL_NUM_ARGS(ex) > first_extra_arg)) {
295
0
    arg = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var + ex->func->op_array.T) + (requested_offset - first_extra_arg);
296
0
  } else {
297
0
    arg = ZEND_CALL_ARG(ex, requested_offset + 1);
298
0
  }
299
0
  if (EXPECTED(!Z_ISUNDEF_P(arg))) {
300
0
    RETURN_COPY_DEREF(arg);
301
0
  }
302
0
}
303
/* }}} */
304
305
/* {{{ Get an array of the arguments that were passed to the function */
306
ZEND_FUNCTION(func_get_args)
307
4
{
308
4
  zval *p, *q;
309
4
  uint32_t arg_count, first_extra_arg;
310
4
  uint32_t i;
311
4
  zend_execute_data *ex = EX(prev_execute_data);
312
313
4
  ZEND_PARSE_PARAMETERS_NONE();
314
315
4
  if (ex && (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE)) {
316
4
    zend_throw_error(NULL, "func_get_args() cannot be called from the global scope");
317
4
    RETURN_THROWS();
318
4
  }
319
320
0
  if (zend_forbid_dynamic_call() == FAILURE) {
321
0
    RETURN_THROWS();
322
0
  }
323
324
0
  arg_count = ZEND_CALL_NUM_ARGS(ex);
325
326
0
  if (arg_count) {
327
0
    array_init_size(return_value, arg_count);
328
0
    first_extra_arg = ex->func->op_array.num_args;
329
0
    zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
330
0
    ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
331
0
      i = 0;
332
0
      p = ZEND_CALL_ARG(ex, 1);
333
0
      if (arg_count > first_extra_arg) {
334
0
        while (i < first_extra_arg) {
335
0
          q = p;
336
0
          if (EXPECTED(Z_TYPE_INFO_P(q) != IS_UNDEF)) {
337
0
            ZVAL_DEREF(q);
338
0
            if (Z_OPT_REFCOUNTED_P(q)) {
339
0
              Z_ADDREF_P(q);
340
0
            }
341
0
            ZEND_HASH_FILL_SET(q);
342
0
          } else {
343
0
            ZEND_HASH_FILL_SET_NULL();
344
0
          }
345
0
          ZEND_HASH_FILL_NEXT();
346
0
          p++;
347
0
          i++;
348
0
        }
349
0
        p = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var + ex->func->op_array.T);
350
0
      }
351
0
      while (i < arg_count) {
352
0
        q = p;
353
0
        if (EXPECTED(Z_TYPE_INFO_P(q) != IS_UNDEF)) {
354
0
          ZVAL_DEREF(q);
355
0
          if (Z_OPT_REFCOUNTED_P(q)) {
356
0
            Z_ADDREF_P(q);
357
0
          }
358
0
          ZEND_HASH_FILL_SET(q);
359
0
        } else {
360
0
          ZEND_HASH_FILL_SET_NULL();
361
0
        }
362
0
        ZEND_HASH_FILL_NEXT();
363
0
        p++;
364
0
        i++;
365
0
      }
366
0
    } ZEND_HASH_FILL_END();
367
0
    Z_ARRVAL_P(return_value)->nNumOfElements = arg_count;
368
0
  } else {
369
0
    RETURN_EMPTY_ARRAY();
370
0
  }
371
0
}
372
/* }}} */
373
374
/* {{{ Get string length
375
   Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
376
ZEND_FUNCTION(strlen)
377
0
{
378
0
  zend_string *s;
379
380
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
381
0
    Z_PARAM_STR(s)
382
0
  ZEND_PARSE_PARAMETERS_END();
383
384
0
  RETVAL_LONG(ZSTR_LEN(s));
385
0
}
386
/* }}} */
387
388
/* {{{ Binary safe string comparison */
389
ZEND_FUNCTION(strcmp)
390
0
{
391
0
  zend_string *s1, *s2;
392
393
0
  ZEND_PARSE_PARAMETERS_START(2, 2)
394
0
    Z_PARAM_STR(s1)
395
0
    Z_PARAM_STR(s2)
396
0
  ZEND_PARSE_PARAMETERS_END();
397
398
0
  RETURN_LONG(zend_binary_strcmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2)));
399
0
}
400
/* }}} */
401
402
/* {{{ Binary safe string comparison */
403
ZEND_FUNCTION(strncmp)
404
0
{
405
0
  zend_string *s1, *s2;
406
0
  zend_long len;
407
408
0
  ZEND_PARSE_PARAMETERS_START(3, 3)
409
0
    Z_PARAM_STR(s1)
410
0
    Z_PARAM_STR(s2)
411
0
    Z_PARAM_LONG(len)
412
0
  ZEND_PARSE_PARAMETERS_END();
413
414
0
  if (len < 0) {
415
0
    zend_argument_value_error(3, "must be greater than or equal to 0");
416
0
    RETURN_THROWS();
417
0
  }
418
419
0
  RETURN_LONG(zend_binary_strncmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2), len));
420
0
}
421
/* }}} */
422
423
/* {{{ Binary safe case-insensitive string comparison */
424
ZEND_FUNCTION(strcasecmp)
425
0
{
426
0
  zend_string *s1, *s2;
427
428
0
  ZEND_PARSE_PARAMETERS_START(2, 2)
429
0
    Z_PARAM_STR(s1)
430
0
    Z_PARAM_STR(s2)
431
0
  ZEND_PARSE_PARAMETERS_END();
432
433
0
  RETURN_LONG(zend_binary_strcasecmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2)));
434
0
}
435
/* }}} */
436
437
/* {{{ Binary safe string comparison */
438
ZEND_FUNCTION(strncasecmp)
439
0
{
440
0
  zend_string *s1, *s2;
441
0
  zend_long len;
442
443
0
  ZEND_PARSE_PARAMETERS_START(3, 3)
444
0
    Z_PARAM_STR(s1)
445
0
    Z_PARAM_STR(s2)
446
0
    Z_PARAM_LONG(len)
447
0
  ZEND_PARSE_PARAMETERS_END();
448
449
0
  if (len < 0) {
450
0
    zend_argument_value_error(3, "must be greater than or equal to 0");
451
0
    RETURN_THROWS();
452
0
  }
453
454
0
  RETURN_LONG(zend_binary_strncasecmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2), len));
455
0
}
456
/* }}} */
457
458
/* {{{ Return the current error_reporting level, and if an argument was passed - change to the new level */
459
ZEND_FUNCTION(error_reporting)
460
20
{
461
20
  zend_long err;
462
20
  bool err_is_null = 1;
463
20
  int old_error_reporting;
464
465
60
  ZEND_PARSE_PARAMETERS_START(0, 1)
466
60
    Z_PARAM_OPTIONAL
467
78
    Z_PARAM_LONG_OR_NULL(err, err_is_null)
468
20
  ZEND_PARSE_PARAMETERS_END();
469
470
18
  old_error_reporting = EG(error_reporting);
471
472
18
  if (!err_is_null && err != old_error_reporting) {
473
16
    zend_ini_entry *p = EG(error_reporting_ini_entry);
474
475
16
    if (!p) {
476
2
      zval *zv = zend_hash_find_known_hash(EG(ini_directives), ZSTR_KNOWN(ZEND_STR_ERROR_REPORTING));
477
2
      if (!zv) {
478
        /* Ini setting does not exist -- can this happen? */
479
0
        RETURN_LONG(old_error_reporting);
480
0
      }
481
482
2
      p = EG(error_reporting_ini_entry) = (zend_ini_entry*)Z_PTR_P(zv);
483
2
    }
484
16
    if (!p->modified) {
485
15
      if (!EG(modified_ini_directives)) {
486
13
        ALLOC_HASHTABLE(EG(modified_ini_directives));
487
13
        zend_hash_init(EG(modified_ini_directives), 8, NULL, NULL, 0);
488
13
      }
489
15
      if (EXPECTED(zend_hash_add_ptr(EG(modified_ini_directives), ZSTR_KNOWN(ZEND_STR_ERROR_REPORTING), p) != NULL)) {
490
15
        p->orig_value = p->value;
491
15
        p->orig_modifiable = p->modifiable;
492
15
        p->modified = 1;
493
15
      }
494
15
    } else if (p->orig_value != p->value) {
495
1
      zend_string_release_ex(p->value, 0);
496
1
    }
497
498
16
    p->value = zend_long_to_str(err);
499
16
    EG(error_reporting) = err;
500
16
  }
501
502
18
  RETURN_LONG(old_error_reporting);
503
18
}
504
/* }}} */
505
506
static bool validate_constant_array_argument(HashTable *ht, uint32_t argument_number) /* {{{ */
507
0
{
508
0
  bool ret = true;
509
0
  zval *val;
510
511
0
  GC_PROTECT_RECURSION(ht);
512
0
  ZEND_HASH_FOREACH_VAL(ht, val) {
513
0
    ZVAL_DEREF(val);
514
0
    if (Z_TYPE_P(val) == IS_ARRAY && Z_REFCOUNTED_P(val)) {
515
0
      if (Z_IS_RECURSIVE_P(val)) {
516
0
        zend_argument_value_error(argument_number, "cannot be a recursive array");
517
0
        ret = false;
518
0
        break;
519
0
      } else if (!validate_constant_array_argument(Z_ARRVAL_P(val), argument_number)) {
520
0
        ret = false;
521
0
        break;
522
0
      }
523
0
    }
524
0
  } ZEND_HASH_FOREACH_END();
525
0
  GC_UNPROTECT_RECURSION(ht);
526
0
  return ret;
527
0
}
528
/* }}} */
529
530
static void copy_constant_array(zval *dst, const zval *src) /* {{{ */
531
0
{
532
0
  zend_string *key;
533
0
  zend_ulong idx;
534
0
  zval *new_val, *val;
535
536
0
  array_init_size(dst, zend_hash_num_elements(Z_ARRVAL_P(src)));
537
0
  ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(src), idx, key, val) {
538
    /* constant arrays can't contain references */
539
0
    ZVAL_DEREF(val);
540
0
    if (key) {
541
0
      new_val = zend_hash_add_new(Z_ARRVAL_P(dst), key, val);
542
0
    } else {
543
0
      new_val = zend_hash_index_add_new(Z_ARRVAL_P(dst), idx, val);
544
0
    }
545
0
    if (Z_TYPE_P(val) == IS_ARRAY) {
546
0
      if (Z_REFCOUNTED_P(val)) {
547
0
        copy_constant_array(new_val, val);
548
0
      }
549
0
    } else {
550
0
      Z_TRY_ADDREF_P(val);
551
0
    }
552
0
  } ZEND_HASH_FOREACH_END();
553
0
}
554
/* }}} */
555
556
/* {{{ Define a new constant */
557
ZEND_FUNCTION(define)
558
16
{
559
16
  zend_string *name;
560
16
  zval *val;
561
16
  bool non_cs = 0;
562
16
  zend_constant c;
563
564
45
  ZEND_PARSE_PARAMETERS_START(2, 3)
565
52
    Z_PARAM_STR(name)
566
65
    Z_PARAM_ZVAL(val)
567
65
    Z_PARAM_OPTIONAL
568
65
    Z_PARAM_BOOL(non_cs)
569
16
  ZEND_PARSE_PARAMETERS_END();
570
571
13
  if (ZEND_NUM_ARGS() == 3) {
572
1
    zend_error(E_DEPRECATED,
573
1
      "define(): Argument #3 ($case_insensitive) is ignored and treated as false since declaration of case-insensitive constants is no longer supported, passing the argument explicitly is unnecessary"
574
1
    );
575
1
    if (UNEXPECTED(EG(exception))) {
576
0
      RETURN_THROWS();
577
0
    }
578
1
  }
579
580
13
  if (zend_memnstr(ZSTR_VAL(name), "::", sizeof("::") - 1, ZSTR_VAL(name) + ZSTR_LEN(name))) {
581
0
    zend_argument_value_error(1, "cannot be a class constant");
582
0
    RETURN_THROWS();
583
0
  }
584
585
13
  if (non_cs) {
586
1
    zend_error(E_WARNING, "define(): Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported, this will be an error in PHP 9.0");
587
1
    if (UNEXPECTED(EG(exception))) {
588
0
      RETURN_THROWS();
589
0
    }
590
1
  }
591
592
13
  if (Z_TYPE_P(val) == IS_ARRAY && Z_REFCOUNTED_P(val)) {
593
0
    if (!validate_constant_array_argument(Z_ARRVAL_P(val), 2)) {
594
0
      RETURN_THROWS();
595
0
    } else {
596
0
      copy_constant_array(&c.value, val);
597
0
    }
598
13
  } else {
599
13
    ZVAL_COPY(&c.value, val);
600
13
  }
601
602
  /* non persistent */
603
13
  ZEND_CONSTANT_SET_FLAGS(&c, 0, PHP_USER_CONSTANT);
604
13
  c.name = zend_string_copy(name);
605
13
  RETURN_BOOL(zend_register_constant(&c) != NULL);
606
13
}
607
/* }}} */
608
609
/* {{{ Check whether a constant exists
610
   Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
611
ZEND_FUNCTION(defined)
612
4
{
613
4
  zend_string *name;
614
615
12
  ZEND_PARSE_PARAMETERS_START(1, 1)
616
16
    Z_PARAM_STR(name)
617
4
  ZEND_PARSE_PARAMETERS_END();
618
619
2
  RETURN_BOOL(zend_get_constant_ex(name, zend_get_executed_scope(), ZEND_FETCH_CLASS_SILENT));
620
2
}
621
/* }}} */
622
623
/* {{{ Retrieves the class name */
624
ZEND_FUNCTION(get_class)
625
0
{
626
0
  zval *obj = NULL;
627
628
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o", &obj) == FAILURE) {
629
0
    RETURN_THROWS();
630
0
  }
631
632
0
  if (!obj) {
633
0
    const zend_class_entry *scope = zend_get_executed_scope();
634
635
0
    if (scope) {
636
0
      zend_error(E_DEPRECATED, "Calling get_class() without arguments is deprecated");
637
0
      if (UNEXPECTED(EG(exception))) {
638
0
        RETURN_THROWS();
639
0
      }
640
0
      RETURN_STR_COPY(scope->name);
641
0
    } else {
642
0
      zend_throw_error(NULL, "get_class() without arguments must be called from within a class");
643
0
      RETURN_THROWS();
644
0
    }
645
0
  }
646
647
0
  RETURN_STR_COPY(Z_OBJCE_P(obj)->name);
648
0
}
649
/* }}} */
650
651
/* {{{ Retrieves the "Late Static Binding" class name */
652
ZEND_FUNCTION(get_called_class)
653
1
{
654
1
  ZEND_PARSE_PARAMETERS_NONE();
655
656
0
  const zend_class_entry *called_scope = zend_get_called_scope(execute_data);
657
0
  if (!called_scope) {
658
0
    zend_throw_error(NULL, "get_called_class() must be called from within a class");
659
0
    RETURN_THROWS();
660
0
  }
661
662
0
  RETURN_STR_COPY(called_scope->name);
663
0
}
664
/* }}} */
665
666
/* {{{ Retrieves the parent class name for object or class or current scope or false if not in a scope. */
667
ZEND_FUNCTION(get_parent_class)
668
0
{
669
0
  zend_class_entry *ce = NULL;
670
671
0
  ZEND_PARSE_PARAMETERS_START(0, 1)
672
0
    Z_PARAM_OPTIONAL
673
0
    Z_PARAM_OBJ_OR_CLASS_NAME(ce)
674
0
  ZEND_PARSE_PARAMETERS_END();
675
676
0
  if (!ce) {
677
0
    zend_error(E_DEPRECATED, "Calling get_parent_class() without arguments is deprecated");
678
0
    if (UNEXPECTED(EG(exception))) {
679
0
      RETURN_THROWS();
680
0
    }
681
0
    ce = zend_get_executed_scope();
682
0
  }
683
684
0
  if (ce && ce->parent) {
685
0
    RETURN_STR_COPY(ce->parent->name);
686
0
  } else {
687
0
    RETURN_FALSE;
688
0
  }
689
0
}
690
/* }}} */
691
692
static void is_a_impl(INTERNAL_FUNCTION_PARAMETERS, bool only_subclass) /* {{{ */
693
227
{
694
227
  zval *obj;
695
227
  zend_string *class_name;
696
227
  const zend_class_entry *instance_ce;
697
227
  bool allow_string = only_subclass;
698
699
462
  ZEND_PARSE_PARAMETERS_START(2, 3)
700
462
    Z_PARAM_ZVAL(obj)
701
32
    Z_PARAM_STR(class_name)
702
8
    Z_PARAM_OPTIONAL
703
20
    Z_PARAM_BOOL(allow_string)
704
227
  ZEND_PARSE_PARAMETERS_END();
705
  /*
706
   * allow_string - is_a default is no, is_subclass_of is yes.
707
   *   if it's allowed, then the autoloader will be called if the class does not exist.
708
   *   default behaviour is different, as 'is_a' used to be used to test mixed return values
709
   *   and there is no easy way to deprecate this.
710
   */
711
712
8
  if (allow_string && Z_TYPE_P(obj) == IS_STRING) {
713
1
    instance_ce = zend_lookup_class(Z_STR_P(obj));
714
1
    if (!instance_ce) {
715
0
      RETURN_FALSE;
716
0
    }
717
7
  } else if (Z_TYPE_P(obj) == IS_STRING) {
718
    // is_a() uses only_subclass as false, is_subclass_of() uses it as true
719
1
    zend_error(
720
1
      E_DEPRECATED,
721
1
      only_subclass
722
1
        ? "Calling is_subclass_of() with a string when $allow_string is false"
723
1
        : "Calling is_a() with a string when $allow_string is false"
724
1
    );
725
1
    if (UNEXPECTED(EG(exception))) {
726
0
      RETURN_THROWS();
727
0
    }
728
1
    RETURN_FALSE;
729
6
  } else if (Z_TYPE_P(obj) == IS_OBJECT) {
730
0
    instance_ce = Z_OBJCE_P(obj);
731
6
  } else {
732
6
    RETURN_FALSE;
733
6
  }
734
735
1
  if (!only_subclass && EXPECTED(zend_string_equals(instance_ce->name, class_name))) {
736
0
    RETURN_TRUE;
737
0
  }
738
739
1
  const zend_class_entry *ce = zend_lookup_class_ex(class_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
740
1
  if (!ce) {
741
0
    RETURN_FALSE;
742
0
  }
743
744
1
  if (only_subclass && instance_ce == ce) {
745
0
    RETURN_FALSE;
746
0
  }
747
748
1
  RETURN_BOOL(instanceof_function(instance_ce, ce));
749
1
}
750
/* }}} */
751
752
/* {{{ Returns true if the object has this class as one of its parents */
753
ZEND_FUNCTION(is_subclass_of)
754
2
{
755
2
  is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
756
2
}
757
/* }}} */
758
759
/* {{{ Returns true if the first argument is an object and is this class or has this class as one of its parents, */
760
ZEND_FUNCTION(is_a)
761
225
{
762
225
  is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
763
225
}
764
/* }}} */
765
766
/* {{{ add_class_vars */
767
static void add_class_vars(const zend_class_entry *scope, zend_class_entry *ce, bool statics, const zval *return_value)
768
2
{
769
2
  zend_property_info *prop_info;
770
2
  zval *prop, prop_copy;
771
2
  zend_string *key;
772
2
  zval *default_properties_table = CE_DEFAULT_PROPERTIES_TABLE(ce);
773
774
20
  ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
775
20
    if (((prop_info->flags & ZEND_ACC_PROTECTED) &&
776
0
       !zend_check_protected(prop_info->ce, scope)) ||
777
8
      ((prop_info->flags & ZEND_ACC_PRIVATE) &&
778
0
        prop_info->ce != scope) ||
779
8
      (prop_info->flags & ZEND_ACC_VIRTUAL)) {
780
0
      continue;
781
0
    }
782
8
    prop = NULL;
783
8
    if (statics && (prop_info->flags & ZEND_ACC_STATIC) != 0) {
784
2
      prop = &ce->default_static_members_table[prop_info->offset];
785
2
      ZVAL_DEINDIRECT(prop);
786
6
    } else if (!statics && (prop_info->flags & ZEND_ACC_STATIC) == 0) {
787
2
      prop = &default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
788
2
    }
789
8
    if (!prop) {
790
4
      continue;
791
4
    }
792
793
4
    if (Z_ISUNDEF_P(prop)) {
794
      /* Return uninitialized typed properties as a null value */
795
2
      ZVAL_NULL(&prop_copy);
796
2
    } else {
797
      /* copy: enforce read only access */
798
2
      ZVAL_COPY_OR_DUP(&prop_copy, prop);
799
2
    }
800
4
    prop = &prop_copy;
801
802
    /* this is necessary to make it able to work with default array
803
     * properties, returned to user */
804
4
    if (Z_OPT_TYPE_P(prop) == IS_CONSTANT_AST) {
805
0
      if (UNEXPECTED(zval_update_constant_ex(prop, ce) != SUCCESS)) {
806
0
        return;
807
0
      }
808
0
    }
809
810
4
    zend_hash_add_new(Z_ARRVAL_P(return_value), key, prop);
811
4
  } ZEND_HASH_FOREACH_END();
812
2
}
813
/* }}} */
814
815
/* {{{ Returns an array of default properties of the class. */
816
ZEND_FUNCTION(get_class_vars)
817
1
{
818
1
  zend_class_entry *ce = NULL;
819
820
1
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "C", &ce) == FAILURE) {
821
0
    RETURN_THROWS();
822
0
  }
823
824
1
  array_init(return_value);
825
1
  if (UNEXPECTED(!(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
826
0
    if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
827
0
      return;
828
0
    }
829
0
  }
830
831
1
  const zend_class_entry *scope = zend_get_executed_scope();
832
1
  add_class_vars(scope, ce, false, return_value);
833
1
  add_class_vars(scope, ce, true, return_value);
834
1
}
835
/* }}} */
836
837
/* {{{ Returns an array of object properties */
838
ZEND_FUNCTION(get_object_vars)
839
4
{
840
4
  zval *value;
841
4
  HashTable *properties;
842
4
  zend_string *key;
843
4
  zend_object *zobj;
844
4
  zend_ulong num_key;
845
846
12
  ZEND_PARSE_PARAMETERS_START(1, 1)
847
16
    Z_PARAM_OBJ(zobj)
848
4
  ZEND_PARSE_PARAMETERS_END();
849
850
4
  zval obj_zv;
851
4
  ZVAL_OBJ(&obj_zv, zobj);
852
4
  properties = zend_get_properties_for(&obj_zv, ZEND_PROP_PURPOSE_GET_OBJECT_VARS);
853
4
  if (properties == NULL) {
854
0
    RETURN_EMPTY_ARRAY();
855
0
  }
856
857
4
  if (!zobj->ce->default_properties_count && properties == zobj->properties && !GC_IS_RECURSIVE(properties)) {
858
    /* fast copy */
859
4
    bool always_duplicate = zobj->handlers != &std_object_handlers;
860
4
    RETVAL_ARR(zend_proptable_to_symtable(properties, always_duplicate));
861
4
  } else {
862
0
    array_init_size(return_value, zend_hash_num_elements(properties));
863
864
0
    ZEND_HASH_FOREACH_KEY_VAL(properties, num_key, key, value) {
865
0
      bool is_dynamic = 1;
866
0
      zval tmp;
867
0
      ZVAL_UNDEF(&tmp);
868
0
      if (Z_TYPE_P(value) == IS_INDIRECT) {
869
0
        value = Z_INDIRECT_P(value);
870
0
        if (UNEXPECTED(Z_ISUNDEF_P(value))) {
871
0
          continue;
872
0
        }
873
874
0
        is_dynamic = 0;
875
0
      } else if (Z_TYPE_P(value) == IS_PTR) {
876
0
        is_dynamic = 0;
877
0
      }
878
879
0
      if (key && zend_check_property_access(zobj, key, is_dynamic) == FAILURE) {
880
0
        continue;
881
0
      }
882
883
0
      if (Z_ISREF_P(value) && Z_REFCOUNT_P(value) == 1) {
884
0
        value = Z_REFVAL_P(value);
885
0
      }
886
0
      if (Z_TYPE_P(value) == IS_PTR) {
887
        /* value is IS_PTR for properties with hooks. */
888
0
        zend_property_info *prop_info = Z_PTR_P(value);
889
0
        if ((prop_info->flags & ZEND_ACC_VIRTUAL) && !prop_info->hooks[ZEND_PROPERTY_HOOK_GET]) {
890
0
          continue;
891
0
        }
892
0
        const char *unmangled_name_cstr = zend_get_unmangled_property_name(prop_info->name);
893
0
        zend_string *unmangled_name = zend_string_init(unmangled_name_cstr, strlen(unmangled_name_cstr), false);
894
0
        value = zend_read_property_ex(prop_info->ce, zobj, unmangled_name, /* silent */ true, &tmp);
895
0
        zend_string_release_ex(unmangled_name, false);
896
0
        if (EG(exception)) {
897
0
          zend_release_properties(properties);
898
0
          zval_ptr_dtor(return_value);
899
0
          ZVAL_UNDEF(return_value);
900
0
          RETURN_THROWS();
901
0
        }
902
0
      }
903
0
      Z_TRY_ADDREF_P(value);
904
905
0
      if (UNEXPECTED(!key)) {
906
        /* This case is only possible due to loopholes, e.g. ArrayObject */
907
0
        zend_hash_index_add(Z_ARRVAL_P(return_value), num_key, value);
908
0
      } else if (!is_dynamic && ZSTR_VAL(key)[0] == 0) {
909
0
        const char *prop_name, *class_name;
910
0
        size_t prop_len;
911
0
        zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_len);
912
        /* We assume here that a mangled property name is never
913
         * numeric. This is probably a safe assumption, but
914
         * theoretically someone might write an extension with
915
         * private, numeric properties. Well, too bad.
916
         */
917
0
        zend_hash_str_add_new(Z_ARRVAL_P(return_value), prop_name, prop_len, value);
918
0
      } else {
919
0
        zend_symtable_add_new(Z_ARRVAL_P(return_value), key, value);
920
0
      }
921
0
      zval_ptr_dtor(&tmp);
922
0
    } ZEND_HASH_FOREACH_END();
923
0
  }
924
4
  zend_release_properties(properties);
925
4
}
926
/* }}} */
927
928
/* {{{ Returns an array of mangled object properties. Does not respect property visibility. */
929
ZEND_FUNCTION(get_mangled_object_vars)
930
0
{
931
0
  zend_object *obj;
932
0
  HashTable *properties;
933
934
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
935
0
    Z_PARAM_OBJ(obj)
936
0
  ZEND_PARSE_PARAMETERS_END();
937
938
0
  properties = zend_get_properties_no_lazy_init(obj);
939
0
  if (!properties) {
940
0
    ZVAL_EMPTY_ARRAY(return_value);
941
0
    return;
942
0
  }
943
944
0
  properties = zend_proptable_to_symtable(properties,
945
0
    (obj->ce->default_properties_count ||
946
0
     obj->handlers != &std_object_handlers ||
947
0
     GC_IS_RECURSIVE(properties)));
948
0
  RETURN_ARR(properties);
949
0
}
950
/* }}} */
951
952
/* {{{ Returns an array of method names for class or class instance. */
953
ZEND_FUNCTION(get_class_methods)
954
4
{
955
4
  zval method_name;
956
4
  zend_class_entry *ce = NULL;
957
4
  zend_function *mptr;
958
959
12
  ZEND_PARSE_PARAMETERS_START(1, 1)
960
19
    Z_PARAM_OBJ_OR_CLASS_NAME(ce)
961
19
  ZEND_PARSE_PARAMETERS_END();
962
963
3
  array_init(return_value);
964
3
  const zend_class_entry *scope = zend_get_executed_scope();
965
966
18
  ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, mptr) {
967
18
    if (zend_check_method_accessible(mptr, scope)) {
968
6
      ZVAL_STR_COPY(&method_name, mptr->common.function_name);
969
6
      zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &method_name);
970
6
    }
971
18
  } ZEND_HASH_FOREACH_END();
972
973
3
  if (ce == zend_ce_closure) {
974
0
    ZVAL_STR_COPY(&method_name, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE));
975
0
    zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &method_name);
976
0
  }
977
3
}
978
/* }}} */
979
980
/* {{{ Checks if the class method exists */
981
ZEND_FUNCTION(method_exists)
982
10
{
983
10
  zval *klass;
984
10
  zend_string *method_name;
985
10
  zend_class_entry *ce;
986
10
  zend_function *func;
987
988
  /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */
989
30
  ZEND_PARSE_PARAMETERS_START(2, 2)
990
40
    Z_PARAM_ZVAL(klass)
991
40
    Z_PARAM_STR(method_name)
992
10
  ZEND_PARSE_PARAMETERS_END();
993
994
10
  if (Z_TYPE_P(klass) == IS_OBJECT) {
995
5
    ce = Z_OBJCE_P(klass);
996
5
  } else if (Z_TYPE_P(klass) == IS_STRING) {
997
4
    if ((ce = zend_lookup_class(Z_STR_P(klass))) == NULL) {
998
0
      RETURN_FALSE;
999
0
    }
1000
4
  } else {
1001
1
    zend_wrong_parameter_type_error(1, Z_EXPECTED_OBJECT_OR_STRING, klass);
1002
1
    RETURN_THROWS();
1003
1
  }
1004
1005
9
  func = zend_hash_find_ptr_lc(&ce->function_table, method_name);
1006
1007
9
  if (func) {
1008
    /* Exclude shadow properties when checking a method on a specific class. Include
1009
     * them when checking an object, as method_exists() generally ignores visibility.
1010
     * TODO: Should we use EG(scope) for the object case instead? */
1011
0
    RETURN_BOOL(Z_TYPE_P(klass) == IS_OBJECT
1012
0
      || !(func->common.fn_flags & ZEND_ACC_PRIVATE) || func->common.scope == ce);
1013
0
  }
1014
1015
9
  if (Z_TYPE_P(klass) == IS_OBJECT) {
1016
5
    zend_object *obj = Z_OBJ_P(klass);
1017
5
    func = Z_OBJ_HT_P(klass)->get_method(&obj, method_name, NULL);
1018
5
    if (func != NULL) {
1019
5
      if (func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
1020
        /* Returns true for the fake Closure's __invoke */
1021
1
        RETVAL_BOOL(func->common.scope == zend_ce_closure
1022
1
          && zend_string_equals_literal_ci(method_name, ZEND_INVOKE_FUNC_NAME));
1023
1024
1
        zend_string_release_ex(func->common.function_name, 0);
1025
1
        zend_free_trampoline(func);
1026
1
        return;
1027
1
      }
1028
4
      RETURN_TRUE;
1029
4
    }
1030
5
  } else {
1031
      /* Returns true for fake Closure::__invoke */
1032
4
      if (ce == zend_ce_closure
1033
4
          && zend_string_equals_literal_ci(method_name, ZEND_INVOKE_FUNC_NAME)) {
1034
2
          RETURN_TRUE;
1035
2
      }
1036
4
  }
1037
2
  RETURN_FALSE;
1038
2
}
1039
/* }}} */
1040
1041
static void _property_exists(zval *return_value, const zval *object, zend_string *property)
1042
1
{
1043
1
  zend_class_entry *ce;
1044
1045
1
  if (Z_TYPE_P(object) == IS_STRING) {
1046
0
    ce = zend_lookup_class(Z_STR_P(object));
1047
0
    if (!ce) {
1048
0
      RETURN_FALSE;
1049
0
    }
1050
1
  } else if (Z_TYPE_P(object) == IS_OBJECT) {
1051
1
    ce = Z_OBJCE_P(object);
1052
1
  } else {
1053
0
    zend_wrong_parameter_type_error(1, Z_EXPECTED_OBJECT_OR_STRING, object);
1054
0
    RETURN_THROWS();
1055
0
  }
1056
1057
1
  const zend_property_info *property_info = zend_hash_find_ptr(&ce->properties_info, property);
1058
1
  if (property_info != NULL
1059
0
   && (!(property_info->flags & ZEND_ACC_PRIVATE)
1060
0
    || property_info->ce == ce)) {
1061
0
    RETURN_TRUE;
1062
0
  }
1063
1064
1
  RETURN_BOOL(
1065
1
    Z_TYPE_P(object) == IS_OBJECT &&
1066
1
    Z_OBJ_HANDLER_P(object, has_property)(Z_OBJ_P(object), property, ZEND_PROPERTY_EXISTS, NULL)
1067
1
  );
1068
1
}
1069
1070
/* {{{ Checks if the object or class has a property */
1071
ZEND_FUNCTION(property_exists)
1072
1
{
1073
1
  zval *object;
1074
1
  zend_string *property;
1075
1076
  /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */
1077
1
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "zS", &object, &property) == FAILURE) {
1078
0
    RETURN_THROWS();
1079
0
  }
1080
1081
1
  _property_exists(return_value, object, property);
1082
1
}
1083
/* }}} */
1084
1085
ZEND_FRAMELESS_FUNCTION(property_exists, 2)
1086
0
{
1087
0
  zval *object;
1088
0
  zval property_tmp;
1089
0
  zend_string *property;
1090
1091
0
  Z_FLF_PARAM_ZVAL(1, object);
1092
0
  Z_FLF_PARAM_STR(2, property, property_tmp);
1093
1094
0
  _property_exists(return_value, object, property);
1095
1096
0
flf_clean:;
1097
0
  Z_FLF_PARAM_FREE_STR(2, property_tmp)
1098
0
}
1099
1100
static zend_always_inline void _class_exists_impl(zval *return_value, zend_string *name, bool autoload, int flags, int skip_flags) /* {{{ */
1101
12
{
1102
12
  const zend_class_entry *ce;
1103
1104
12
  if (ZSTR_HAS_CE_CACHE(name)) {
1105
0
    ce = ZSTR_GET_CE_CACHE(name);
1106
0
    if (ce) {
1107
0
      RETURN_BOOL(((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags));
1108
0
    }
1109
0
  }
1110
1111
12
  if (!autoload) {
1112
0
    if (ZSTR_VAL(name)[0] == '\\') {
1113
      /* Ignore leading "\" */
1114
0
      ce = zend_hash_str_find_ptr_lc(EG(class_table), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
1115
0
    } else {
1116
0
      ce = zend_hash_find_ptr_lc(EG(class_table), name);
1117
0
    }
1118
12
  } else {
1119
12
    ce = zend_lookup_class(name);
1120
12
  }
1121
1122
12
  RETURN_BOOL(ce && ((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags));
1123
12
}
1124
/* {{{ */
1125
1126
static inline void class_exists_impl(INTERNAL_FUNCTION_PARAMETERS, int flags, int skip_flags) /* {{{ */
1127
12
{
1128
12
  zend_string *name;
1129
12
  bool autoload = true;
1130
1131
36
  ZEND_PARSE_PARAMETERS_START(1, 2)
1132
48
    Z_PARAM_STR(name)
1133
12
    Z_PARAM_OPTIONAL
1134
24
    Z_PARAM_BOOL(autoload)
1135
12
  ZEND_PARSE_PARAMETERS_END();
1136
1137
12
  _class_exists_impl(return_value, name, autoload, flags, skip_flags);
1138
12
}
1139
1140
/* {{{ Checks if the class exists */
1141
ZEND_FUNCTION(class_exists)
1142
5
{
1143
5
  class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED, ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT);
1144
5
}
1145
/* }}} */
1146
1147
ZEND_FRAMELESS_FUNCTION(class_exists, 1)
1148
0
{
1149
0
  zval name_tmp;
1150
0
  zend_string *name;
1151
1152
0
  Z_FLF_PARAM_STR(1, name, name_tmp);
1153
1154
0
  _class_exists_impl(return_value, name, /* autoload */ true, ZEND_ACC_LINKED, ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT);
1155
1156
0
flf_clean:
1157
0
  Z_FLF_PARAM_FREE_STR(1, name_tmp);
1158
0
}
1159
1160
ZEND_FRAMELESS_FUNCTION(class_exists, 2)
1161
0
{
1162
0
  zval name_tmp;
1163
0
  zend_string *name;
1164
0
  bool autoload;
1165
1166
0
  Z_FLF_PARAM_STR(1, name, name_tmp);
1167
0
  Z_FLF_PARAM_BOOL(2, autoload);
1168
1169
0
  _class_exists_impl(return_value, name, autoload, ZEND_ACC_LINKED, ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT);
1170
1171
0
flf_clean:
1172
0
  Z_FLF_PARAM_FREE_STR(1, name_tmp);
1173
0
}
1174
1175
/* {{{ Checks if the class exists */
1176
ZEND_FUNCTION(interface_exists)
1177
0
{
1178
0
  class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED|ZEND_ACC_INTERFACE, 0);
1179
0
}
1180
/* }}} */
1181
1182
/* {{{ Checks if the trait exists */
1183
ZEND_FUNCTION(trait_exists)
1184
7
{
1185
7
  class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_TRAIT, 0);
1186
7
}
1187
/* }}} */
1188
1189
ZEND_FUNCTION(enum_exists)
1190
0
{
1191
0
  class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_ENUM, 0);
1192
0
}
1193
1194
/* {{{ Checks if the function exists */
1195
ZEND_FUNCTION(function_exists)
1196
0
{
1197
0
  zend_string *name;
1198
0
  bool exists;
1199
1200
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
1201
0
    Z_PARAM_STR(name)
1202
0
  ZEND_PARSE_PARAMETERS_END();
1203
1204
0
  if (ZSTR_VAL(name)[0] == '\\') {
1205
    /* Ignore leading "\" */
1206
0
    exists = zend_hash_str_find_ptr_lc(EG(function_table), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1) != NULL;
1207
0
  } else {
1208
0
    exists = zend_hash_find_ptr_lc(EG(function_table), name) != NULL;
1209
0
  }
1210
1211
0
  RETURN_BOOL(exists);
1212
0
}
1213
/* }}} */
1214
1215
/* {{{ Creates an alias for user defined class */
1216
ZEND_FUNCTION(class_alias)
1217
21
{
1218
21
  zend_string *class_name;
1219
21
  zend_string *alias_name;
1220
21
  zend_class_entry *ce;
1221
21
  bool autoload = 1;
1222
1223
58
  ZEND_PARSE_PARAMETERS_START(2, 3)
1224
64
    Z_PARAM_STR(class_name)
1225
80
    Z_PARAM_STR(alias_name)
1226
16
    Z_PARAM_OPTIONAL
1227
32
    Z_PARAM_BOOL(autoload)
1228
21
  ZEND_PARSE_PARAMETERS_END();
1229
1230
16
  ce = zend_lookup_class_ex(class_name, NULL, !autoload ? ZEND_FETCH_CLASS_NO_AUTOLOAD : 0);
1231
1232
16
  if (ce) {
1233
11
    if (zend_register_class_alias_ex(ZSTR_VAL(alias_name), ZSTR_LEN(alias_name), ce, false) == SUCCESS) {
1234
8
      RETURN_TRUE;
1235
8
    } else {
1236
3
      zend_class_redeclaration_error_ex(E_WARNING, alias_name, ce);
1237
3
      RETURN_FALSE;
1238
3
    }
1239
11
  } else {
1240
5
    zend_error(E_WARNING, "Class \"%s\" not found", ZSTR_VAL(class_name));
1241
5
    RETURN_FALSE;
1242
5
  }
1243
16
}
1244
/* }}} */
1245
1246
/* {{{ Returns an array with the file names that were include_once()'d */
1247
ZEND_FUNCTION(get_included_files)
1248
0
{
1249
0
  zend_string *entry;
1250
1251
0
  ZEND_PARSE_PARAMETERS_NONE();
1252
1253
0
  array_init(return_value);
1254
0
  ZEND_HASH_MAP_FOREACH_STR_KEY(&EG(included_files), entry) {
1255
0
    if (entry) {
1256
0
      add_next_index_str(return_value, zend_string_copy(entry));
1257
0
    }
1258
0
  } ZEND_HASH_FOREACH_END();
1259
0
}
1260
/* }}} */
1261
1262
/* {{{ Generates a user-level error/warning/notice message */
1263
ZEND_FUNCTION(trigger_error)
1264
0
{
1265
0
  zend_long error_type = E_USER_NOTICE;
1266
0
  zend_string *message;
1267
1268
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &message, &error_type) == FAILURE) {
1269
0
    RETURN_THROWS();
1270
0
  }
1271
1272
0
  switch (error_type) {
1273
0
    case E_USER_ERROR:
1274
0
      zend_error(E_DEPRECATED, "Passing E_USER_ERROR to trigger_error() is deprecated since 8.4,"
1275
0
        " throw an exception or call exit with a string message instead");
1276
0
      if (UNEXPECTED(EG(exception))) {
1277
0
        RETURN_THROWS();
1278
0
      }
1279
0
    case E_USER_WARNING:
1280
0
    case E_USER_NOTICE:
1281
0
    case E_USER_DEPRECATED:
1282
0
      break;
1283
0
    default:
1284
0
      zend_argument_value_error(2, "must be one of E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE,"
1285
0
        " or E_USER_DEPRECATED");
1286
0
      RETURN_THROWS();
1287
0
      break;
1288
0
  }
1289
1290
0
  zend_error_zstr_at(error_type, zend_get_executed_filename_ex(), zend_get_executed_lineno(), message);
1291
  // TODO Change to void
1292
0
  RETURN_TRUE;
1293
0
}
1294
/* }}} */
1295
1296
/* {{{ Sets a user-defined error handler function.  Returns the previously defined error handler, or false on error */
1297
ZEND_FUNCTION(set_error_handler)
1298
0
{
1299
0
  zend_fcall_info fci;
1300
0
  zend_fcall_info_cache fcc;
1301
0
  zend_long error_type = E_ALL;
1302
1303
0
  ZEND_PARSE_PARAMETERS_START(1, 2)
1304
0
    Z_PARAM_FUNC_OR_NULL(fci, fcc)
1305
0
    Z_PARAM_OPTIONAL
1306
0
    Z_PARAM_LONG(error_type)
1307
0
  ZEND_PARSE_PARAMETERS_END();
1308
1309
0
  if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
1310
0
    ZVAL_COPY(return_value, &EG(user_error_handler));
1311
0
  }
1312
1313
0
  zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting));
1314
0
  zend_stack_push(&EG(user_error_handlers), &EG(user_error_handler));
1315
1316
0
  if (!ZEND_FCI_INITIALIZED(fci)) { /* unset user-defined handler */
1317
0
    ZVAL_UNDEF(&EG(user_error_handler));
1318
0
    return;
1319
0
  }
1320
1321
0
  ZVAL_COPY(&EG(user_error_handler), &(fci.function_name));
1322
0
  EG(user_error_handler_error_reporting) = (int)error_type;
1323
0
}
1324
/* }}} */
1325
1326
/* {{{ Restores the previously defined error handler function */
1327
ZEND_FUNCTION(restore_error_handler)
1328
0
{
1329
0
  ZEND_PARSE_PARAMETERS_NONE();
1330
1331
0
  if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
1332
0
    zval zeh;
1333
1334
0
    ZVAL_COPY_VALUE(&zeh, &EG(user_error_handler));
1335
0
    ZVAL_UNDEF(&EG(user_error_handler));
1336
0
    zval_ptr_dtor(&zeh);
1337
0
  }
1338
1339
0
  if (zend_stack_is_empty(&EG(user_error_handlers))) {
1340
0
    ZVAL_UNDEF(&EG(user_error_handler));
1341
0
  } else {
1342
0
    zval *tmp;
1343
0
    EG(user_error_handler_error_reporting) = zend_stack_int_top(&EG(user_error_handlers_error_reporting));
1344
0
    zend_stack_del_top(&EG(user_error_handlers_error_reporting));
1345
0
    tmp = zend_stack_top(&EG(user_error_handlers));
1346
0
    ZVAL_COPY_VALUE(&EG(user_error_handler), tmp);
1347
0
    zend_stack_del_top(&EG(user_error_handlers));
1348
0
  }
1349
1350
  // TODO Change to void
1351
0
  RETURN_TRUE;
1352
0
}
1353
/* }}} */
1354
1355
ZEND_FUNCTION(get_error_handler)
1356
0
{
1357
0
  ZEND_PARSE_PARAMETERS_NONE();
1358
1359
0
  if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
1360
0
    RETURN_COPY(&EG(user_error_handler));
1361
0
  }
1362
0
}
1363
1364
/* {{{ Sets a user-defined exception handler function. Returns the previously defined exception handler, or false on error */
1365
ZEND_FUNCTION(set_exception_handler)
1366
6
{
1367
6
  zend_fcall_info fci;
1368
6
  zend_fcall_info_cache fcc;
1369
1370
18
  ZEND_PARSE_PARAMETERS_START(1, 1)
1371
24
    Z_PARAM_FUNC_OR_NULL(fci, fcc)
1372
6
  ZEND_PARSE_PARAMETERS_END();
1373
1374
6
  if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1375
1
    ZVAL_COPY(return_value, &EG(user_exception_handler));
1376
1
  }
1377
1378
6
  zend_stack_push(&EG(user_exception_handlers), &EG(user_exception_handler));
1379
1380
6
  if (!ZEND_FCI_INITIALIZED(fci)) { /* unset user-defined handler */
1381
1
    ZVAL_UNDEF(&EG(user_exception_handler));
1382
1
    return;
1383
1
  }
1384
1385
5
  ZVAL_COPY(&EG(user_exception_handler), &(fci.function_name));
1386
5
}
1387
/* }}} */
1388
1389
/* {{{ Restores the previously defined exception handler function */
1390
ZEND_FUNCTION(restore_exception_handler)
1391
0
{
1392
0
  ZEND_PARSE_PARAMETERS_NONE();
1393
1394
0
  if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1395
0
    zval_ptr_dtor(&EG(user_exception_handler));
1396
0
  }
1397
0
  if (zend_stack_is_empty(&EG(user_exception_handlers))) {
1398
0
    ZVAL_UNDEF(&EG(user_exception_handler));
1399
0
  } else {
1400
0
    zval *tmp = zend_stack_top(&EG(user_exception_handlers));
1401
0
    ZVAL_COPY_VALUE(&EG(user_exception_handler), tmp);
1402
0
    zend_stack_del_top(&EG(user_exception_handlers));
1403
0
  }
1404
1405
  // TODO Change to void
1406
0
  RETURN_TRUE;
1407
0
}
1408
/* }}} */
1409
1410
ZEND_FUNCTION(get_exception_handler)
1411
0
{
1412
0
  ZEND_PARSE_PARAMETERS_NONE();
1413
1414
0
  if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1415
0
    RETURN_COPY(&EG(user_exception_handler));
1416
0
  }
1417
0
}
1418
1419
static inline void get_declared_class_impl(INTERNAL_FUNCTION_PARAMETERS, int flags) /* {{{ */
1420
1
{
1421
1
  zend_string *key;
1422
1
  zval *zv;
1423
1424
1
  ZEND_PARSE_PARAMETERS_NONE();
1425
1426
1
  array_init(return_value);
1427
1
  zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
1428
1
  ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
1429
404
    ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(EG(class_table), key, zv) {
1430
404
      const zend_class_entry *ce = Z_PTR_P(zv);
1431
404
      if ((ce->ce_flags & (ZEND_ACC_LINKED|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT)) == flags
1432
1
       && key
1433
1
       && ZSTR_VAL(key)[0] != 0) {
1434
1
        ZEND_HASH_FILL_GROW();
1435
1
        if (EXPECTED(Z_TYPE_P(zv) == IS_PTR)) {
1436
1
          ZEND_HASH_FILL_SET_STR_COPY(ce->name);
1437
1
        } else {
1438
0
          ZEND_ASSERT(Z_TYPE_P(zv) == IS_ALIAS_PTR);
1439
0
          ZEND_HASH_FILL_SET_STR_COPY(key);
1440
0
        }
1441
1
        ZEND_HASH_FILL_NEXT();
1442
1
      }
1443
404
    } ZEND_HASH_FOREACH_END();
1444
1
  } ZEND_HASH_FILL_END();
1445
1
}
1446
/* {{{ */
1447
1448
/* {{{ Returns an array of all declared traits. */
1449
ZEND_FUNCTION(get_declared_traits)
1450
1
{
1451
1
  get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED | ZEND_ACC_TRAIT);
1452
1
}
1453
/* }}} */
1454
1455
/* {{{ Returns an array of all declared classes. */
1456
ZEND_FUNCTION(get_declared_classes)
1457
0
{
1458
0
  get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED);
1459
0
}
1460
/* }}} */
1461
1462
/* {{{ Returns an array of all declared interfaces. */
1463
ZEND_FUNCTION(get_declared_interfaces)
1464
0
{
1465
0
  get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED | ZEND_ACC_INTERFACE);
1466
0
}
1467
/* }}} */
1468
1469
/* {{{ Returns an array of all defined functions */
1470
ZEND_FUNCTION(get_defined_functions)
1471
7
{
1472
7
  zval internal, user;
1473
7
  zend_string *key;
1474
7
  zend_function *func;
1475
7
  bool exclude_disabled = true;
1476
1477
7
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &exclude_disabled) == FAILURE) {
1478
0
    RETURN_THROWS();
1479
0
  }
1480
1481
7
  if (ZEND_NUM_ARGS() == 1) {
1482
7
    zend_error(E_DEPRECATED,
1483
7
      "get_defined_functions(): The $exclude_disabled parameter has no effect since PHP 8.0");
1484
7
  }
1485
1486
7
  array_init(&internal);
1487
7
  array_init(&user);
1488
7
  array_init(return_value);
1489
1490
9.68k
  ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(EG(function_table), key, func) {
1491
9.68k
    if (key && ZSTR_VAL(key)[0] != 0) {
1492
4.83k
      if (func->type == ZEND_INTERNAL_FUNCTION) {
1493
4.83k
        add_next_index_str(&internal, zend_string_copy(key));
1494
4.83k
      } else if (func->type == ZEND_USER_FUNCTION) {
1495
0
        add_next_index_str(&user, zend_string_copy(key));
1496
0
      }
1497
4.83k
    }
1498
9.68k
  } ZEND_HASH_FOREACH_END();
1499
1500
7
  zend_hash_str_add_new(Z_ARRVAL_P(return_value), "internal", sizeof("internal")-1, &internal);
1501
7
  zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_USER), &user);
1502
7
}
1503
/* }}} */
1504
1505
/* {{{ Returns an associative array of names and values of all currently defined variable names (variables in the current scope) */
1506
ZEND_FUNCTION(get_defined_vars)
1507
0
{
1508
0
  zend_array *symbol_table;
1509
1510
0
  ZEND_PARSE_PARAMETERS_NONE();
1511
1512
0
  if (zend_forbid_dynamic_call() == FAILURE) {
1513
0
    return;
1514
0
  }
1515
1516
0
  symbol_table = zend_rebuild_symbol_table();
1517
0
  if (UNEXPECTED(symbol_table == NULL)) {
1518
0
    RETURN_EMPTY_ARRAY();
1519
0
  }
1520
1521
0
  RETURN_ARR(zend_array_dup(symbol_table));
1522
0
}
1523
/* }}} */
1524
1525
#if ZEND_DEBUG && defined(ZTS)
1526
ZEND_FUNCTION(zend_thread_id)
1527
{
1528
  ZEND_PARSE_PARAMETERS_NONE();
1529
1530
  RETURN_LONG((zend_long)tsrm_thread_id());
1531
}
1532
#endif
1533
1534
/* {{{ Get the resource type name for a given resource */
1535
ZEND_FUNCTION(get_resource_type)
1536
0
{
1537
0
  const char *resource_type;
1538
0
  zval *z_resource_type;
1539
1540
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &z_resource_type) == FAILURE) {
1541
0
    RETURN_THROWS();
1542
0
  }
1543
1544
0
  resource_type = zend_rsrc_list_get_rsrc_type(Z_RES_P(z_resource_type));
1545
0
  if (resource_type) {
1546
0
    RETURN_STRING(resource_type);
1547
0
  } else {
1548
0
    RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED));
1549
0
  }
1550
0
}
1551
/* }}} */
1552
1553
/* {{{ Get the resource ID for a given resource */
1554
ZEND_FUNCTION(get_resource_id)
1555
0
{
1556
0
  zval *resource;
1557
1558
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
1559
0
    Z_PARAM_RESOURCE(resource)
1560
0
  ZEND_PARSE_PARAMETERS_END();
1561
1562
0
  RETURN_LONG(Z_RES_HANDLE_P(resource));
1563
0
}
1564
/* }}} */
1565
1566
/* {{{ Get an array with all active resources */
1567
ZEND_FUNCTION(get_resources)
1568
0
{
1569
0
  zend_string *type = NULL;
1570
0
  zend_string *key;
1571
0
  zend_ulong index;
1572
0
  zval *val;
1573
1574
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &type) == FAILURE) {
1575
0
    RETURN_THROWS();
1576
0
  }
1577
1578
0
  if (!type) {
1579
0
    array_init(return_value);
1580
0
    ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1581
0
      if (!key) {
1582
0
        Z_ADDREF_P(val);
1583
0
        zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1584
0
      }
1585
0
    } ZEND_HASH_FOREACH_END();
1586
0
  } else if (zend_string_equals_literal(type, "Unknown")) {
1587
0
    array_init(return_value);
1588
0
    ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1589
0
      if (!key && Z_RES_TYPE_P(val) <= 0) {
1590
0
        Z_ADDREF_P(val);
1591
0
        zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1592
0
      }
1593
0
    } ZEND_HASH_FOREACH_END();
1594
0
  } else {
1595
0
    int id = zend_fetch_list_dtor_id(ZSTR_VAL(type));
1596
1597
0
    if (id <= 0) {
1598
0
      zend_argument_value_error(1, "must be a valid resource type");
1599
0
      RETURN_THROWS();
1600
0
    }
1601
1602
0
    array_init(return_value);
1603
0
    ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1604
0
      if (!key && Z_RES_TYPE_P(val) == id) {
1605
0
        Z_ADDREF_P(val);
1606
0
        zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1607
0
      }
1608
0
    } ZEND_HASH_FOREACH_END();
1609
0
  }
1610
0
}
1611
/* }}} */
1612
1613
static void add_zendext_info(const zend_extension *ext, void *arg) /* {{{ */
1614
0
{
1615
0
  zval *name_array = (zval *)arg;
1616
0
  add_next_index_string(name_array, ext->name);
1617
0
}
1618
/* }}} */
1619
1620
/* {{{ Return an array containing names of loaded extensions */
1621
ZEND_FUNCTION(get_loaded_extensions)
1622
0
{
1623
0
  bool zendext = false;
1624
1625
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &zendext) == FAILURE) {
1626
0
    RETURN_THROWS();
1627
0
  }
1628
1629
0
  array_init(return_value);
1630
1631
0
  if (zendext) {
1632
0
    zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) add_zendext_info, return_value);
1633
0
  } else {
1634
0
    zend_module_entry *module;
1635
1636
0
    ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
1637
0
      add_next_index_string(return_value, module->name);
1638
0
    } ZEND_HASH_FOREACH_END();
1639
0
  }
1640
0
}
1641
/* }}} */
1642
1643
/* {{{ Return an array containing the names and values of all defined constants */
1644
ZEND_FUNCTION(get_defined_constants)
1645
0
{
1646
0
  bool categorize = false;
1647
1648
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &categorize) == FAILURE) {
1649
0
    RETURN_THROWS();
1650
0
  }
1651
1652
0
  array_init(return_value);
1653
1654
0
  if (categorize) {
1655
0
    zend_constant *val;
1656
0
    int module_number;
1657
0
    zval *modules, const_val;
1658
0
    char **module_names;
1659
0
    zend_module_entry *module;
1660
0
    int i = 1;
1661
1662
0
    modules = ecalloc(zend_hash_num_elements(&module_registry) + 2, sizeof(zval));
1663
0
    module_names = emalloc((zend_hash_num_elements(&module_registry) + 2) * sizeof(char *));
1664
1665
0
    module_names[0] = "internal";
1666
0
    ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
1667
0
      module_names[module->module_number] = (char *)module->name;
1668
0
      i++;
1669
0
    } ZEND_HASH_FOREACH_END();
1670
0
    module_names[i] = "user";
1671
1672
0
    ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), val) {
1673
0
      if (!val->name) {
1674
        /* skip special constants */
1675
0
        continue;
1676
0
      }
1677
1678
0
      if (ZEND_CONSTANT_MODULE_NUMBER(val) == PHP_USER_CONSTANT) {
1679
0
        module_number = i;
1680
0
      } else if (ZEND_CONSTANT_MODULE_NUMBER(val) > i) {
1681
        /* should not happen */
1682
0
        continue;
1683
0
      } else {
1684
0
        module_number = ZEND_CONSTANT_MODULE_NUMBER(val);
1685
0
      }
1686
1687
0
      if (Z_TYPE(modules[module_number]) == IS_UNDEF) {
1688
0
        array_init(&modules[module_number]);
1689
0
        add_assoc_zval(return_value, module_names[module_number], &modules[module_number]);
1690
0
      }
1691
1692
0
      ZVAL_COPY_OR_DUP(&const_val, &val->value);
1693
0
      zend_hash_add_new(Z_ARRVAL(modules[module_number]), val->name, &const_val);
1694
0
    } ZEND_HASH_FOREACH_END();
1695
1696
0
    efree(module_names);
1697
0
    efree(modules);
1698
0
  } else {
1699
0
    zend_constant *constant;
1700
0
    zval const_val;
1701
1702
0
    ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), constant) {
1703
0
      if (!constant->name) {
1704
        /* skip special constants */
1705
0
        continue;
1706
0
      }
1707
0
      ZVAL_COPY_OR_DUP(&const_val, &constant->value);
1708
0
      zend_hash_add_new(Z_ARRVAL_P(return_value), constant->name, &const_val);
1709
0
    } ZEND_HASH_FOREACH_END();
1710
0
  }
1711
0
}
1712
/* }}} */
1713
1714
static bool backtrace_is_arg_sensitive(const zend_execute_data *call, uint32_t offset)
1715
123k
{
1716
123k
  const zend_attribute *attribute = zend_get_parameter_attribute_str(
1717
123k
    call->func->common.attributes,
1718
123k
    "sensitiveparameter",
1719
123k
    sizeof("sensitiveparameter") - 1,
1720
123k
    offset
1721
123k
  );
1722
1723
123k
  return attribute != NULL;
1724
123k
}
1725
1726
static bool debug_backtrace_get_args(zend_execute_data *call, zval *arg_array) /* {{{ */
1727
122k
{
1728
122k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
1729
1730
122k
  if (num_args) {
1731
121k
    uint32_t i = 0;
1732
121k
    zval *p = ZEND_CALL_ARG(call, 1);
1733
1734
121k
    if (!array_finit_size_packed(arg_array, num_args)) {
1735
0
      return false;
1736
0
    }
1737
121k
    ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(arg_array)) {
1738
121k
      if (call->func->type == ZEND_USER_FUNCTION) {
1739
64
        uint32_t first_extra_arg = MIN(num_args, call->func->op_array.num_args);
1740
1741
64
        if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_HAS_SYMBOL_TABLE)) {
1742
          /* In case of attached symbol_table, values on stack may be invalid
1743
           * and we have to access them through symbol_table
1744
           * See: https://bugs.php.net/bug.php?id=73156
1745
           */
1746
3
          while (i < first_extra_arg) {
1747
2
            zend_string *arg_name = call->func->op_array.vars[i];
1748
2
            zval original_arg;
1749
2
            zval *arg = zend_hash_find_ex_ind(call->symbol_table, arg_name, 1);
1750
2
            bool is_sensitive = backtrace_is_arg_sensitive(call, i);
1751
1752
2
            if (arg) {
1753
2
              ZVAL_DEREF(arg);
1754
2
              ZVAL_COPY_VALUE(&original_arg, arg);
1755
2
            } else {
1756
0
              ZVAL_NULL(&original_arg);
1757
0
            }
1758
1759
2
            if (is_sensitive) {
1760
0
              zval redacted_arg;
1761
0
              object_init_ex(&redacted_arg, zend_ce_sensitive_parameter_value);
1762
0
              zend_sensitive_parameter_value_init(Z_OBJ(redacted_arg), &original_arg);
1763
0
              ZEND_HASH_FILL_SET(&redacted_arg);
1764
0
              if (ZEND_HAS_OOM()) {
1765
0
                zval_ptr_dtor(&redacted_arg);
1766
0
                ZEND_HASH_FILL_FINISH();
1767
0
                goto oom;
1768
0
              }
1769
2
            } else {
1770
2
              Z_TRY_ADDREF_P(&original_arg);
1771
2
              ZEND_HASH_FILL_SET(&original_arg);
1772
2
            }
1773
1774
2
            ZEND_HASH_FILL_NEXT();
1775
2
            i++;
1776
2
          }
1777
63
        } else {
1778
123
          while (i < first_extra_arg) {
1779
60
            zval original_arg;
1780
60
            bool is_sensitive = backtrace_is_arg_sensitive(call, i);
1781
1782
60
            if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
1783
58
              zval *arg = p;
1784
58
              ZVAL_DEREF(arg);
1785
58
              ZVAL_COPY_VALUE(&original_arg, arg);
1786
58
            } else {
1787
2
              ZVAL_NULL(&original_arg);
1788
2
            }
1789
1790
60
            if (is_sensitive) {
1791
9
              zval redacted_arg;
1792
9
              object_init_ex(&redacted_arg, zend_ce_sensitive_parameter_value);
1793
9
              zend_sensitive_parameter_value_init(Z_OBJ(redacted_arg), &original_arg);
1794
9
              ZEND_HASH_FILL_SET(&redacted_arg);
1795
9
              if (ZEND_HAS_OOM()) {
1796
0
                zval_ptr_dtor(&redacted_arg);
1797
0
                ZEND_HASH_FILL_FINISH();
1798
0
                goto oom;
1799
0
              }
1800
51
            } else {
1801
51
              Z_TRY_ADDREF_P(&original_arg);
1802
51
              ZEND_HASH_FILL_SET(&original_arg);
1803
51
            }
1804
1805
60
            ZEND_HASH_FILL_NEXT();
1806
60
            p++;
1807
60
            i++;
1808
60
          }
1809
63
        }
1810
64
        p = ZEND_CALL_VAR_NUM(call, call->func->op_array.last_var + call->func->op_array.T);
1811
64
      }
1812
1813
244k
      while (i < num_args) {
1814
123k
        zval original_arg;
1815
123k
        bool is_sensitive = 0;
1816
1817
123k
        if (i < call->func->common.num_args || call->func->common.fn_flags & ZEND_ACC_VARIADIC) {
1818
122k
          is_sensitive = backtrace_is_arg_sensitive(call, MIN(i, call->func->common.num_args));
1819
122k
        }
1820
1821
123k
        if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
1822
123k
          zval *arg = p;
1823
123k
          ZVAL_DEREF(arg);
1824
123k
          ZVAL_COPY_VALUE(&original_arg, arg);
1825
123k
        } else {
1826
0
          ZVAL_NULL(&original_arg);
1827
0
        }
1828
1829
123k
        if (is_sensitive) {
1830
21
          zval redacted_arg;
1831
21
          object_init_ex(&redacted_arg, zend_ce_sensitive_parameter_value);
1832
21
          zend_sensitive_parameter_value_init(Z_OBJ(redacted_arg), &original_arg);
1833
21
          ZEND_HASH_FILL_SET(&redacted_arg);
1834
21
          if (ZEND_HAS_OOM()) {
1835
0
            zval_ptr_dtor(&redacted_arg);
1836
0
            ZEND_HASH_FILL_FINISH();
1837
0
            goto oom;
1838
0
          }
1839
122k
        } else {
1840
122k
          Z_TRY_ADDREF_P(&original_arg);
1841
122k
          ZEND_HASH_FILL_SET(&original_arg);
1842
122k
        }
1843
1844
123k
        ZEND_HASH_FILL_NEXT();
1845
123k
        p++;
1846
123k
        i++;
1847
123k
      }
1848
121k
    } ZEND_HASH_FILL_END();
1849
121k
    Z_ARRVAL_P(arg_array)->nNumOfElements = num_args;
1850
121k
  } else {
1851
696
    ZVAL_EMPTY_ARRAY(arg_array);
1852
696
  }
1853
1854
122k
  if ((ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS)
1855
   /* __call and __callStatic are non-variadic, potentially with
1856
    * HAS_EXTRA_NAMED_PARAMS set. Don't add extra args, as they're already
1857
    * contained in the 2nd param. */
1858
9
   && (call->func->common.fn_flags & ZEND_ACC_VARIADIC)) {
1859
9
    zend_string *name;
1860
9
    zval *arg;
1861
1862
9
    bool is_sensitive = backtrace_is_arg_sensitive(call, call->func->common.num_args);
1863
1864
9
    SEPARATE_ARRAY(arg_array);
1865
38
    ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(call->extra_named_params, name, arg) {
1866
38
      ZVAL_DEREF(arg);
1867
38
      zval *add_result;
1868
38
      if (is_sensitive) {
1869
2
        zval redacted_arg;
1870
2
        object_init_ex(&redacted_arg, zend_ce_sensitive_parameter_value);
1871
2
        zend_sensitive_parameter_value_init(Z_OBJ(redacted_arg), arg);
1872
2
        add_result = zend_hash_fadd_new(Z_ARRVAL_P(arg_array), name, &redacted_arg);
1873
8
      } else {
1874
8
        Z_TRY_ADDREF_P(arg);
1875
8
        add_result = zend_hash_fadd_new(Z_ARRVAL_P(arg_array), name, arg);
1876
8
      }
1877
38
      if (UNEXPECTED(add_result == HASH_NOMEM)) {
1878
0
        goto oom;
1879
0
      }
1880
38
    } ZEND_HASH_FOREACH_END();
1881
9
  }
1882
1883
122k
  return true;
1884
1885
0
oom:
1886
0
  zval_ptr_dtor(arg_array);
1887
0
  ZVAL_EMPTY_ARRAY(arg_array);
1888
0
  return false;
1889
122k
}
1890
/* }}} */
1891
1892
/* {{{ */
1893
ZEND_FUNCTION(debug_print_backtrace)
1894
16
{
1895
16
  zend_long options = 0;
1896
16
  zend_long limit = 0;
1897
16
  zval backtrace;
1898
1899
16
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &options, &limit) == FAILURE) {
1900
0
    RETURN_THROWS();
1901
0
  }
1902
1903
16
  zend_fetch_debug_backtrace(&backtrace, 1, options, limit);
1904
16
  ZEND_ASSERT(Z_TYPE(backtrace) == IS_ARRAY);
1905
1906
16
  zend_string *str = zend_trace_to_string(Z_ARRVAL(backtrace), /* include_main */ false);
1907
16
  ZEND_WRITE(ZSTR_VAL(str), ZSTR_LEN(str));
1908
16
  zend_string_release(str);
1909
16
  zval_ptr_dtor(&backtrace);
1910
16
}
1911
1912
/* }}} */
1913
1914
ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit) /* {{{ */
1915
300k
{
1916
300k
  zend_execute_data *call, *last_call = NULL;
1917
300k
  zend_object *object;
1918
300k
  bool fake_frame = false;
1919
300k
  int frameno = 0;
1920
300k
  zend_function *func;
1921
300k
  zend_string *filename;
1922
300k
  zend_string *include_filename = NULL;
1923
300k
  zval tmp;
1924
300k
  HashTable *stack_frame, *prev_stack_frame = NULL;
1925
1926
300k
  array_init(return_value);
1927
1928
300k
  call = EG(current_execute_data);
1929
300k
  if (!call) {
1930
5.55k
    return;
1931
5.55k
  }
1932
1933
295k
  if (EG(filename_override)) {
1934
    // Add the current execution point to the frame so we don't lose it
1935
1
    zend_string *filename_override = EG(filename_override);
1936
1
    zend_long lineno_override = EG(lineno_override);
1937
1
    EG(filename_override) = NULL;
1938
1
    EG(lineno_override) = -1;
1939
1940
1
    zend_string *executed_filename = zend_get_executed_filename_ex();
1941
1
    uint32_t lineno = zend_get_executed_lineno();
1942
1
    if (executed_filename && (!zend_string_equals(executed_filename, filename_override) || lineno != lineno_override)) {
1943
1
      stack_frame = zend_new_array(8);
1944
1
      zend_hash_real_init_mixed(stack_frame);
1945
1
      ZVAL_STR_COPY(&tmp, executed_filename);
1946
1
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FILE), &tmp, 1);
1947
1
      ZVAL_LONG(&tmp, lineno);
1948
1
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_LINE), &tmp, 1);
1949
1
      ZVAL_STR_COPY(&tmp, ZSTR_KNOWN(ZEND_STR_CONST_EXPR_PLACEHOLDER));
1950
1
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp, 1);
1951
1
      ZVAL_ARR(&tmp, stack_frame);
1952
1
      zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
1953
1
    }
1954
1955
1
    EG(filename_override) = filename_override;
1956
1
    EG(lineno_override) = lineno_override;
1957
1
  }
1958
1959
295k
  if (skip_last) {
1960
    /* skip debug_backtrace() */
1961
26
    last_call = call;
1962
26
    call = call->prev_execute_data;
1963
26
  }
1964
1965
441k
  while (call && (limit == 0 || frameno < limit)) {
1966
416k
    if (UNEXPECTED(!call->func)) {
1967
      /* This is the fake frame inserted for nested generators. Normally,
1968
       * this frame is preceded by the actual generator frame and then
1969
       * replaced by zend_generator_check_placeholder_frame() below.
1970
       * However, the frame is popped before cleaning the stack frame,
1971
       * which is observable by destructors. */
1972
0
      call = zend_generator_check_placeholder_frame(call);
1973
0
      ZEND_ASSERT(call->func);
1974
0
    }
1975
1976
416k
    zend_execute_data *prev = call->prev_execute_data;
1977
1978
416k
    if (!prev) {
1979
      /* add frame for a handler call without {main} code */
1980
294k
      if (EXPECTED((ZEND_CALL_INFO(call) & ZEND_CALL_TOP_FUNCTION) == 0)) {
1981
269k
        break;
1982
269k
      }
1983
294k
    } else if (UNEXPECTED((ZEND_CALL_INFO(call) & ZEND_CALL_GENERATOR) != 0)) {
1984
3
      prev = zend_generator_check_placeholder_frame(prev);
1985
3
    }
1986
1987
    /* For frameless calls we add an additional frame for the call itself. */
1988
147k
    if (ZEND_USER_CODE(call->func->type)) {
1989
25.3k
      const zend_op *opline = call->opline;
1990
25.3k
      if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) {
1991
25.3k
        goto not_frameless_call;
1992
25.3k
      }
1993
0
      int num_args = ZEND_FLF_NUM_ARGS(opline->opcode);
1994
      /* Check if any args were already freed. Skip the frame in that case. */
1995
0
      if (num_args >= 1) {
1996
0
        zval *arg = zend_get_zval_ptr(opline, opline->op1_type, &opline->op1, call);
1997
0
        if (Z_TYPE_P(arg) == IS_UNDEF) goto not_frameless_call;
1998
0
      }
1999
0
      if (num_args >= 2) {
2000
0
        zval *arg = zend_get_zval_ptr(opline, opline->op2_type, &opline->op2, call);
2001
0
        if (Z_TYPE_P(arg) == IS_UNDEF) goto not_frameless_call;
2002
0
      }
2003
0
      if (num_args >= 3) {
2004
0
        const zend_op *op_data = opline + 1;
2005
0
        zval *arg = zend_get_zval_ptr(op_data, op_data->op1_type, &op_data->op1, call);
2006
0
        if (Z_TYPE_P(arg) == IS_UNDEF) goto not_frameless_call;
2007
0
      }
2008
0
      zend_function *frameless_func = ZEND_FLF_FUNC(opline);
2009
      /* Assume frameless functions are not recursive with themselves.
2010
       * This condition may be true when observers are enabled:
2011
       * Observers will put a call frame on top of the frameless opcode. */
2012
0
      if (last_call && last_call->func == frameless_func) {
2013
0
        goto not_frameless_call;
2014
0
      }
2015
0
      stack_frame = zend_new_array(8);
2016
0
      zend_hash_real_init_mixed(stack_frame);
2017
0
      ZVAL_STR_COPY(&tmp, frameless_func->common.function_name);
2018
0
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp, 1);
2019
      /* Steal file and line from the previous frame. */
2020
0
      if (call->func && ZEND_USER_CODE(call->func->common.type)) {
2021
0
        uint32_t lineno;
2022
2023
0
        filename = call->func->op_array.filename;
2024
0
        if (call->opline->opcode == ZEND_HANDLE_EXCEPTION) {
2025
0
          if (EG(opline_before_exception)) {
2026
0
            lineno = EG(opline_before_exception)->lineno;
2027
0
          } else {
2028
0
            lineno = call->func->op_array.line_end;
2029
0
          }
2030
0
        } else {
2031
0
          lineno = call->opline->lineno;
2032
0
        }
2033
0
        ZVAL_STR_COPY(&tmp, filename);
2034
0
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FILE), &tmp, 1);
2035
0
        ZVAL_LONG(&tmp, lineno);
2036
0
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_LINE), &tmp, 1);
2037
0
        if (prev_stack_frame) {
2038
0
          zend_hash_del(prev_stack_frame, ZSTR_KNOWN(ZEND_STR_FILE));
2039
0
          zend_hash_del(prev_stack_frame, ZSTR_KNOWN(ZEND_STR_LINE));
2040
0
        }
2041
0
      }
2042
0
      if ((options & DEBUG_BACKTRACE_IGNORE_ARGS) == 0) {
2043
0
        HashTable *args = zend_new_array(8);
2044
0
        zend_hash_real_init_mixed(args);
2045
0
        if (num_args >= 1) {
2046
0
          zval *arg = zend_get_zval_ptr(opline, opline->op1_type, &opline->op1, call);
2047
0
          Z_TRY_ADDREF_P(arg);
2048
0
          zend_hash_next_index_insert_new(args, arg);
2049
0
        }
2050
0
        if (num_args >= 2) {
2051
0
          zval *arg = zend_get_zval_ptr(opline, opline->op2_type, &opline->op2, call);
2052
0
          Z_TRY_ADDREF_P(arg);
2053
0
          zend_hash_next_index_insert_new(args, arg);
2054
0
        }
2055
0
        if (num_args >= 3) {
2056
0
          const zend_op *op_data = opline + 1;
2057
0
          zval *arg = zend_get_zval_ptr(op_data, op_data->op1_type, &op_data->op1, call);
2058
0
          Z_TRY_ADDREF_P(arg);
2059
0
          zend_hash_next_index_insert_new(args, arg);
2060
0
        }
2061
0
        ZVAL_ARR(&tmp, args);
2062
0
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_ARGS), &tmp, 1);
2063
0
      }
2064
0
      ZVAL_ARR(&tmp, stack_frame);
2065
0
      zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
2066
0
    }
2067
147k
not_frameless_call:
2068
2069
    /* We use _zend_hash_append*() and the array must be preallocated */
2070
147k
    stack_frame = zend_new_array(8);
2071
147k
    zend_hash_real_init_mixed(stack_frame);
2072
2073
147k
    if (prev && prev->func && ZEND_USER_CODE(prev->func->common.type)) {
2074
12.4k
      uint32_t lineno;
2075
2076
12.4k
      filename = prev->func->op_array.filename;
2077
12.4k
      if (prev->opline->opcode == ZEND_HANDLE_EXCEPTION) {
2078
0
        if (EG(opline_before_exception)) {
2079
0
          lineno = EG(opline_before_exception)->lineno;
2080
0
        } else {
2081
0
          lineno = prev->func->op_array.line_end;
2082
0
        }
2083
12.4k
      } else {
2084
12.4k
        lineno = prev->opline->lineno;
2085
12.4k
      }
2086
12.4k
      ZVAL_STR_COPY(&tmp, filename);
2087
12.4k
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FILE), &tmp, 1);
2088
12.4k
      ZVAL_LONG(&tmp, lineno);
2089
12.4k
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_LINE), &tmp, 1);
2090
2091
      /* try to fetch args only if an FCALL was just made - elsewise we're in the middle of a function
2092
       * and debug_backtrace() might have been called by the error_handler. in this case we don't
2093
       * want to pop anything of the argument-stack */
2094
134k
    } else {
2095
134k
      zend_execute_data *prev_call = prev;
2096
2097
134k
      while (prev_call) {
2098
110k
        zend_execute_data *prev;
2099
2100
110k
        if (prev_call &&
2101
110k
          prev_call->func &&
2102
110k
          !ZEND_USER_CODE(prev_call->func->common.type) &&
2103
110k
          !(prev_call->func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
2104
110k
          break;
2105
110k
        }
2106
2107
0
        prev = prev_call->prev_execute_data;
2108
0
        if (prev && prev->func && ZEND_USER_CODE(prev->func->common.type)) {
2109
0
          ZVAL_STR_COPY(&tmp, prev->func->op_array.filename);
2110
0
          _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FILE), &tmp, 1);
2111
0
          ZVAL_LONG(&tmp, prev->opline->lineno);
2112
0
          _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_LINE), &tmp, 1);
2113
0
          break;
2114
0
        }
2115
0
        prev_call = prev;
2116
0
      }
2117
134k
      filename = NULL;
2118
134k
    }
2119
2120
147k
    func = call->func;
2121
147k
    if (!fake_frame && func->common.function_name) {
2122
122k
      ZVAL_STR_COPY(&tmp, func->common.function_name);
2123
122k
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp, 1);
2124
2125
122k
      if (Z_TYPE(call->This) == IS_OBJECT) {
2126
115k
        object = Z_OBJ(call->This);
2127
        /* $this may be passed into regular internal functions */
2128
115k
        if (func->common.scope) {
2129
115k
          ZVAL_STR_COPY(&tmp, func->common.scope->name);
2130
115k
        } else if (object->handlers->get_class_name == zend_std_get_class_name) {
2131
0
          ZVAL_STR_COPY(&tmp, object->ce->name);
2132
0
        } else {
2133
0
          ZVAL_STR(&tmp, object->handlers->get_class_name(object));
2134
0
        }
2135
115k
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_CLASS), &tmp, 1);
2136
115k
        if ((options & DEBUG_BACKTRACE_PROVIDE_OBJECT) != 0) {
2137
2
          ZVAL_OBJ_COPY(&tmp, object);
2138
2
          _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_OBJECT), &tmp, 1);
2139
2
        }
2140
2141
115k
        ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_OBJECT_OPERATOR));
2142
115k
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_TYPE), &tmp, 1);
2143
115k
      } else if (func->common.scope) {
2144
18
        ZVAL_STR_COPY(&tmp, func->common.scope->name);
2145
18
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_CLASS), &tmp, 1);
2146
18
        ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM));
2147
18
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_TYPE), &tmp, 1);
2148
18
      }
2149
2150
122k
      if ((options & DEBUG_BACKTRACE_IGNORE_ARGS) == 0 &&
2151
122k
        func->type != ZEND_EVAL_CODE) {
2152
2153
122k
        if (UNEXPECTED(!debug_backtrace_get_args(call, &tmp))) {
2154
0
          ZEND_ASSERT(ZEND_HAS_OOM());
2155
0
          zend_array_destroy(stack_frame);
2156
0
          return;
2157
0
        }
2158
122k
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_ARGS), &tmp, 1);
2159
122k
      }
2160
122k
    } else {
2161
      /* i know this is kinda ugly, but i'm trying to avoid extra cycles in the main execution loop */
2162
24.6k
      bool build_filename_arg = true;
2163
24.6k
      zend_string *pseudo_function_name;
2164
24.6k
      uint32_t include_kind = 0;
2165
24.6k
      if (prev && prev->func && ZEND_USER_CODE(prev->func->common.type) && prev->opline->opcode == ZEND_INCLUDE_OR_EVAL) {
2166
34
        include_kind = prev->opline->extended_value;
2167
34
      }
2168
2169
24.6k
      switch (include_kind) {
2170
32
        case ZEND_EVAL:
2171
32
          pseudo_function_name = ZSTR_KNOWN(ZEND_STR_EVAL);
2172
32
          build_filename_arg = false;
2173
32
          break;
2174
0
        case ZEND_INCLUDE:
2175
0
          pseudo_function_name = ZSTR_KNOWN(ZEND_STR_INCLUDE);
2176
0
          break;
2177
2
        case ZEND_REQUIRE:
2178
2
          pseudo_function_name = ZSTR_KNOWN(ZEND_STR_REQUIRE);
2179
2
          break;
2180
0
        case ZEND_INCLUDE_ONCE:
2181
0
          pseudo_function_name = ZSTR_KNOWN(ZEND_STR_INCLUDE_ONCE);
2182
0
          break;
2183
0
        case ZEND_REQUIRE_ONCE:
2184
0
          pseudo_function_name = ZSTR_KNOWN(ZEND_STR_REQUIRE_ONCE);
2185
0
          break;
2186
24.6k
        default:
2187
          /* Skip dummy frame unless it is needed to preserve filename/lineno info. */
2188
24.6k
          if (!filename) {
2189
24.6k
            zend_array_destroy(stack_frame);
2190
24.6k
            goto skip_frame;
2191
24.6k
          }
2192
2193
0
          pseudo_function_name = ZSTR_KNOWN(ZEND_STR_UNKNOWN);
2194
0
          build_filename_arg = false;
2195
0
          break;
2196
24.6k
      }
2197
2198
34
      if (build_filename_arg && include_filename) {
2199
0
        zval arg_array;
2200
2201
0
        array_init(&arg_array);
2202
2203
        /* include_filename always points to the last filename of the last last called-function.
2204
           if we have called include in the frame above - this is the file we have included.
2205
         */
2206
2207
0
        ZVAL_STR_COPY(&tmp, include_filename);
2208
0
        zend_hash_next_index_insert_new(Z_ARRVAL(arg_array), &tmp);
2209
0
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_ARGS), &arg_array, 1);
2210
0
      }
2211
2212
34
      ZVAL_INTERNED_STR(&tmp, pseudo_function_name);
2213
34
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp, 1);
2214
34
    }
2215
2216
122k
    ZVAL_ARR(&tmp, stack_frame);
2217
122k
    if (UNEXPECTED(zend_hash_next_index_finsert_new(Z_ARRVAL_P(return_value), &tmp) == HASH_NOMEM)) {
2218
0
      zend_array_destroy(stack_frame);
2219
0
      return;
2220
0
    }
2221
122k
    frameno++;
2222
122k
    prev_stack_frame = stack_frame;
2223
2224
147k
skip_frame:
2225
147k
    if (ZEND_HAS_OOM()) {
2226
763
      return;
2227
763
    }
2228
2229
146k
    if (UNEXPECTED(ZEND_CALL_KIND(call) == ZEND_CALL_TOP_FUNCTION)
2230
110k
     && !fake_frame
2231
110k
     && prev
2232
110k
     && prev->func
2233
110k
     && ZEND_USER_CODE(prev->func->common.type)
2234
364
     && prev->opline->opcode == ZEND_INCLUDE_OR_EVAL) {
2235
2
      fake_frame = true;
2236
146k
    } else {
2237
146k
      fake_frame = false;
2238
146k
      include_filename = filename;
2239
146k
      last_call = call;
2240
146k
      call = prev;
2241
146k
    }
2242
146k
  }
2243
295k
}
2244
/* }}} */
2245
2246
/* {{{ Return backtrace as array */
2247
ZEND_FUNCTION(debug_backtrace)
2248
10
{
2249
10
  zend_long options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
2250
10
  zend_long limit = 0;
2251
2252
10
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &options, &limit) == FAILURE) {
2253
0
    RETURN_THROWS();
2254
0
  }
2255
2256
10
  zend_fetch_debug_backtrace(return_value, 1, options, limit);
2257
10
}
2258
/* }}} */
2259
2260
/* {{{ Returns true if the named extension is loaded */
2261
ZEND_FUNCTION(extension_loaded)
2262
0
{
2263
0
  zend_string *extension_name;
2264
2265
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &extension_name) == FAILURE) {
2266
0
    RETURN_THROWS();
2267
0
  }
2268
2269
0
  RETURN_BOOL(zend_hash_find_ptr_lc(&module_registry, extension_name) != NULL);
2270
0
}
2271
/* }}} */
2272
2273
/* {{{ Returns an array with the names of functions belonging to the named extension */
2274
ZEND_FUNCTION(get_extension_funcs)
2275
2
{
2276
2
  zend_string *extension_name;
2277
2
  bool array;
2278
2
  zend_module_entry *module;
2279
2
  zend_function *zif;
2280
2281
2
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &extension_name) == FAILURE) {
2282
0
    RETURN_THROWS();
2283
0
  }
2284
2
  if (strncasecmp(ZSTR_VAL(extension_name), "zend", sizeof("zend"))) {
2285
2
    module = zend_hash_find_ptr_lc(&module_registry, extension_name);
2286
2
  } else {
2287
0
    module = zend_hash_str_find_ptr(&module_registry, "core", sizeof("core") - 1);
2288
0
  }
2289
2290
2
  if (!module) {
2291
1
    RETURN_FALSE;
2292
1
  }
2293
2294
1
  if (module->functions) {
2295
    /* avoid BC break, if functions list is empty, will return an empty array */
2296
1
    array_init(return_value);
2297
1
    array = true;
2298
1
  } else {
2299
0
    array = false;
2300
0
  }
2301
2302
1.38k
  ZEND_HASH_MAP_FOREACH_PTR(CG(function_table), zif) {
2303
1.38k
    if (zif->common.type == ZEND_INTERNAL_FUNCTION
2304
691
      && zif->internal_function.module == module) {
2305
514
      if (!array) {
2306
0
        array_init(return_value);
2307
0
        array = true;
2308
0
      }
2309
514
      add_next_index_str(return_value, zend_string_copy(zif->common.function_name));
2310
514
    }
2311
1.38k
  } ZEND_HASH_FOREACH_END();
2312
2313
1
  if (!array) {
2314
0
    RETURN_FALSE;
2315
0
  }
2316
1
}
2317
/* }}} */