Coverage Report

Created: 2025-11-16 06:23

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