Coverage Report

Created: 2026-07-25 06:39

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