Coverage Report

Created: 2025-09-27 06:26

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