Coverage Report

Created: 2025-12-14 06:09

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
2
ZEND_MINIT_FUNCTION(core) { /* {{{ */
38
2
  zend_register_default_classes();
39
40
2
  zend_standard_class_def = register_class_stdClass();
41
42
2
  return SUCCESS;
43
2
}
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
2
{
62
2
  zend_module_entry *module;
63
2
  EG(current_module) = module = zend_register_module_ex(&zend_builtin_module, MODULE_PERSISTENT);
64
2
  if (UNEXPECTED(module == NULL)) {
65
0
    return FAILURE;
66
0
  }
67
2
  ZEND_ASSERT(module->module_number == 0);
68
2
  return SUCCESS;
69
2
}
70
/* }}} */
71
72
ZEND_FUNCTION(clone)
73
108
{
74
108
  zend_object *zobj;
75
108
  HashTable *with = (HashTable*)&zend_empty_array;
76
77
324
  ZEND_PARSE_PARAMETERS_START(1, 2)
78
432
    Z_PARAM_OBJ(zobj)
79
108
    Z_PARAM_OPTIONAL
80
390
    Z_PARAM_ARRAY_HT(with)
81
108
  ZEND_PARSE_PARAMETERS_END();
82
83
  /* clone() also exists as the ZEND_CLONE OPcode and both implementations must be kept in sync. */
84
85
105
  zend_class_entry *scope = zend_get_executed_scope();
86
87
105
  zend_class_entry *ce = zobj->ce;
88
105
  zend_function *clone = ce->clone;
89
90
105
  if (UNEXPECTED(zobj->handlers->clone_obj == NULL)) {
91
3
    zend_throw_error(NULL, "Trying to clone an uncloneable object of class %s", ZSTR_VAL(ce->name));
92
3
    RETURN_THROWS();
93
3
  }
94
95
102
  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
102
  zend_object *cloned;
101
102
  if (zend_hash_num_elements(with) > 0) {
102
81
    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
81
    cloned = zobj->handlers->clone_obj_with(zobj, scope, with);
108
81
  } else {
109
21
    cloned = zobj->handlers->clone_obj(zobj);
110
21
  }
111
112
102
  ZEND_ASSERT(cloned || EG(exception));
113
102
  if (EXPECTED(cloned)) {
114
102
    RETURN_OBJ(cloned);
115
102
  }
116
102
}
117
118
ZEND_FUNCTION(exit)
119
81
{
120
81
  zend_string *str = NULL;
121
81
  zend_long status = 0;
122
123
243
  ZEND_PARSE_PARAMETERS_START(0, 1)
124
243
    Z_PARAM_OPTIONAL
125
330
    Z_PARAM_STR_OR_LONG(str, status)
126
330
  ZEND_PARSE_PARAMETERS_END();
127
128
78
  if (str) {
129
51
    size_t len = ZSTR_LEN(str);
130
51
    if (len != 0) {
131
      /* An exception might be emitted by an output handler */
132
51
      zend_write(ZSTR_VAL(str), len);
133
51
      if (EG(exception)) {
134
3
        RETURN_THROWS();
135
3
      }
136
51
    }
137
51
  } else {
138
27
    EG(exit_status) = status;
139
27
  }
140
141
75
  ZEND_ASSERT(!EG(exception));
142
75
  zend_throw_unwind_exit();
143
75
}
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
738
{
168
738
  ZEND_PARSE_PARAMETERS_NONE();
169
170
738
  RETURN_LONG(gc_collect_cycles());
171
738
}
172
/* }}} */
173
174
/* {{{ Returns status of the circular reference collector */
175
ZEND_FUNCTION(gc_enabled)
176
44
{
177
44
  ZEND_PARSE_PARAMETERS_NONE();
178
179
44
  RETURN_BOOL(gc_enabled());
180
44
}
181
/* }}} */
182
183
/* {{{ Activates the circular reference collector */
184
ZEND_FUNCTION(gc_enable)
185
18
{
186
18
  zend_string *key;
187
188
18
  ZEND_PARSE_PARAMETERS_NONE();
189
190
18
  key = ZSTR_INIT_LITERAL("zend.enable_gc", 0);
191
18
  zend_alter_ini_entry_chars(key, "1", sizeof("1")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
192
18
  zend_string_release_ex(key, 0);
193
18
}
194
/* }}} */
195
196
/* {{{ Deactivates the circular reference collector */
197
ZEND_FUNCTION(gc_disable)
198
50
{
199
50
  zend_string *key;
200
201
50
  ZEND_PARSE_PARAMETERS_NONE();
202
203
50
  key = ZSTR_INIT_LITERAL("zend.enable_gc", 0);
204
50
  zend_alter_ini_entry_chars(key, "0", sizeof("0")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
205
50
  zend_string_release_ex(key, 0);
206
50
}
207
/* }}} */
208
209
/* {{{ Returns current GC statistics */
210
ZEND_FUNCTION(gc_status)
211
144
{
212
144
  zend_gc_status status;
213
214
144
  ZEND_PARSE_PARAMETERS_NONE();
215
216
144
  zend_gc_get_status(&status);
217
218
144
  array_init_size(return_value, 16);
219
220
144
  add_assoc_bool_ex(return_value, "running", sizeof("running")-1, status.active);
221
144
  add_assoc_bool_ex(return_value, "protected", sizeof("protected")-1, status.gc_protected);
222
144
  add_assoc_bool_ex(return_value, "full", sizeof("full")-1, status.full);
223
144
  add_assoc_long_ex(return_value, "runs", sizeof("runs")-1, (long)status.runs);
224
144
  add_assoc_long_ex(return_value, "collected", sizeof("collected")-1, (long)status.collected);
225
144
  add_assoc_long_ex(return_value, "threshold", sizeof("threshold")-1, (long)status.threshold);
226
144
  add_assoc_long_ex(return_value, "buffer_size", sizeof("buffer_size")-1, (long)status.buf_size);
227
144
  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
144
  add_assoc_double_ex(return_value, "application_time", sizeof("application_time")-1, (double) status.application_time / ZEND_NANO_IN_SEC);
231
144
  add_assoc_double_ex(return_value, "collector_time", sizeof("collector_time")-1, (double) status.collector_time / ZEND_NANO_IN_SEC);
232
144
  add_assoc_double_ex(return_value, "destructor_time", sizeof("destructor_time")-1, (double) status.dtor_time / ZEND_NANO_IN_SEC);
233
144
  add_assoc_double_ex(return_value, "free_time", sizeof("free_time")-1, (double) status.free_time / ZEND_NANO_IN_SEC);
234
144
}
235
/* }}} */
236
237
/* {{{ Get the number of arguments that were passed to the function */
238
ZEND_FUNCTION(func_num_args)
239
12
{
240
12
  zend_execute_data *ex = EX(prev_execute_data);
241
242
12
  ZEND_PARSE_PARAMETERS_NONE();
243
244
9
  if (ex && (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE)) {
245
3
    zend_throw_error(NULL, "func_num_args() must be called from a function context");
246
3
    RETURN_THROWS();
247
3
  }
248
249
6
  if (zend_forbid_dynamic_call() == FAILURE) {
250
6
    RETURN_LONG(-1);
251
6
  }
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
168
{
260
168
  uint32_t arg_count, first_extra_arg;
261
168
  zval *arg;
262
168
  zend_long requested_offset;
263
168
  zend_execute_data *ex;
264
265
168
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &requested_offset) == FAILURE) {
266
3
    RETURN_THROWS();
267
3
  }
268
269
165
  if (requested_offset < 0) {
270
9
    zend_argument_value_error(1, "must be greater than or equal to 0");
271
9
    RETURN_THROWS();
272
9
  }
273
274
156
  ex = EX(prev_execute_data);
275
156
  if (ex && (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE)) {
276
6
    zend_throw_error(NULL, "func_get_arg() cannot be called from the global scope");
277
6
    RETURN_THROWS();
278
6
  }
279
280
150
  if (zend_forbid_dynamic_call() == FAILURE) {
281
3
    RETURN_THROWS();
282
3
  }
283
284
147
  arg_count = ZEND_CALL_NUM_ARGS(ex);
285
286
147
  if ((zend_ulong)requested_offset >= arg_count) {
287
33
    zend_argument_value_error(1, "must be less than the number of the arguments passed to the currently executed function");
288
33
    RETURN_THROWS();
289
33
  }
290
291
114
  first_extra_arg = ex->func->op_array.num_args;
292
114
  if ((zend_ulong)requested_offset >= first_extra_arg && (ZEND_CALL_NUM_ARGS(ex) > first_extra_arg)) {
293
24
    arg = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var + ex->func->op_array.T) + (requested_offset - first_extra_arg);
294
90
  } else {
295
90
    arg = ZEND_CALL_ARG(ex, requested_offset + 1);
296
90
  }
297
114
  if (EXPECTED(!Z_ISUNDEF_P(arg))) {
298
87
    RETURN_COPY_DEREF(arg);
299
87
  }
300
114
}
301
/* }}} */
302
303
/* {{{ Get an array of the arguments that were passed to the function */
304
ZEND_FUNCTION(func_get_args)
305
12
{
306
12
  zval *p, *q;
307
12
  uint32_t arg_count, first_extra_arg;
308
12
  uint32_t i;
309
12
  zend_execute_data *ex = EX(prev_execute_data);
310
311
12
  ZEND_PARSE_PARAMETERS_NONE();
312
313
9
  if (ex && (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE)) {
314
6
    zend_throw_error(NULL, "func_get_args() cannot be called from the global scope");
315
6
    RETURN_THROWS();
316
6
  }
317
318
3
  if (zend_forbid_dynamic_call() == FAILURE) {
319
3
    RETURN_THROWS();
320
3
  }
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
48
{
376
48
  zend_string *s;
377
378
141
  ZEND_PARSE_PARAMETERS_START(1, 1)
379
180
    Z_PARAM_STR(s)
380
48
  ZEND_PARSE_PARAMETERS_END();
381
382
42
  RETVAL_LONG(ZSTR_LEN(s));
383
42
}
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
42
{
403
42
  zend_string *s1, *s2;
404
42
  zend_long len;
405
406
126
  ZEND_PARSE_PARAMETERS_START(3, 3)
407
168
    Z_PARAM_STR(s1)
408
210
    Z_PARAM_STR(s2)
409
210
    Z_PARAM_LONG(len)
410
42
  ZEND_PARSE_PARAMETERS_END();
411
412
42
  if (len < 0) {
413
6
    zend_argument_value_error(3, "must be greater than or equal to 0");
414
6
    RETURN_THROWS();
415
6
  }
416
417
36
  RETURN_LONG(zend_binary_strncmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2), len));
418
36
}
419
/* }}} */
420
421
/* {{{ Binary safe case-insensitive string comparison */
422
ZEND_FUNCTION(strcasecmp)
423
21
{
424
21
  zend_string *s1, *s2;
425
426
63
  ZEND_PARSE_PARAMETERS_START(2, 2)
427
84
    Z_PARAM_STR(s1)
428
105
    Z_PARAM_STR(s2)
429
21
  ZEND_PARSE_PARAMETERS_END();
430
431
21
  RETURN_LONG(zend_binary_strcasecmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2)));
432
21
}
433
/* }}} */
434
435
/* {{{ Binary safe string comparison */
436
ZEND_FUNCTION(strncasecmp)
437
48
{
438
48
  zend_string *s1, *s2;
439
48
  zend_long len;
440
441
141
  ZEND_PARSE_PARAMETERS_START(3, 3)
442
180
    Z_PARAM_STR(s1)
443
225
    Z_PARAM_STR(s2)
444
225
    Z_PARAM_LONG(len)
445
48
  ZEND_PARSE_PARAMETERS_END();
446
447
45
  if (len < 0) {
448
9
    zend_argument_value_error(3, "must be greater than or equal to 0");
449
9
    RETURN_THROWS();
450
9
  }
451
452
36
  RETURN_LONG(zend_binary_strncasecmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2), len));
453
36
}
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
336
{
459
336
  zend_long err;
460
336
  bool err_is_null = 1;
461
336
  int old_error_reporting;
462
463
1.00k
  ZEND_PARSE_PARAMETERS_START(0, 1)
464
1.00k
    Z_PARAM_OPTIONAL
465
1.23k
    Z_PARAM_LONG_OR_NULL(err, err_is_null)
466
336
  ZEND_PARSE_PARAMETERS_END();
467
468
333
  old_error_reporting = EG(error_reporting);
469
470
333
  if (!err_is_null && err != old_error_reporting) {
471
264
    zend_ini_entry *p = EG(error_reporting_ini_entry);
472
473
264
    if (!p) {
474
1
      zval *zv = zend_hash_find_known_hash(EG(ini_directives), ZSTR_KNOWN(ZEND_STR_ERROR_REPORTING));
475
1
      if (!zv) {
476
        /* Ini setting does not exist -- can this happen? */
477
0
        RETURN_LONG(old_error_reporting);
478
0
      }
479
480
1
      p = EG(error_reporting_ini_entry) = (zend_ini_entry*)Z_PTR_P(zv);
481
1
    }
482
264
    if (!p->modified) {
483
240
      if (!EG(modified_ini_directives)) {
484
79
        ALLOC_HASHTABLE(EG(modified_ini_directives));
485
79
        zend_hash_init(EG(modified_ini_directives), 8, NULL, NULL, 0);
486
79
      }
487
240
      if (EXPECTED(zend_hash_add_ptr(EG(modified_ini_directives), ZSTR_KNOWN(ZEND_STR_ERROR_REPORTING), p) != NULL)) {
488
240
        p->orig_value = p->value;
489
240
        p->orig_modifiable = p->modifiable;
490
240
        p->modified = 1;
491
240
      }
492
240
    } else if (p->orig_value != p->value) {
493
21
      zend_string_release_ex(p->value, 0);
494
21
    }
495
496
264
    p->value = zend_long_to_str(err);
497
264
    EG(error_reporting) = err;
498
264
  }
499
500
333
  RETURN_LONG(old_error_reporting);
501
333
}
502
/* }}} */
503
504
static bool validate_constant_array_argument(HashTable *ht, int argument_number) /* {{{ */
505
9
{
506
9
  bool ret = true;
507
9
  zval *val;
508
509
9
  GC_PROTECT_RECURSION(ht);
510
27
  ZEND_HASH_FOREACH_VAL(ht, val) {
511
27
    ZVAL_DEREF(val);
512
27
    if (Z_TYPE_P(val) == IS_ARRAY && Z_REFCOUNTED_P(val)) {
513
3
      if (Z_IS_RECURSIVE_P(val)) {
514
3
        zend_argument_value_error(argument_number, "cannot be a recursive array");
515
3
        ret = false;
516
3
        break;
517
3
      } else if (!validate_constant_array_argument(Z_ARRVAL_P(val), argument_number)) {
518
0
        ret = false;
519
0
        break;
520
0
      }
521
3
    }
522
27
  } ZEND_HASH_FOREACH_END();
523
9
  GC_UNPROTECT_RECURSION(ht);
524
9
  return ret;
525
9
}
526
/* }}} */
527
528
static void copy_constant_array(zval *dst, zval *src) /* {{{ */
529
6
{
530
6
  zend_string *key;
531
6
  zend_ulong idx;
532
6
  zval *new_val, *val;
533
534
6
  array_init_size(dst, zend_hash_num_elements(Z_ARRVAL_P(src)));
535
18
  ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(src), idx, key, val) {
536
    /* constant arrays can't contain references */
537
18
    ZVAL_DEREF(val);
538
18
    if (key) {
539
0
      new_val = zend_hash_add_new(Z_ARRVAL_P(dst), key, val);
540
6
    } else {
541
6
      new_val = zend_hash_index_add_new(Z_ARRVAL_P(dst), idx, val);
542
6
    }
543
18
    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
6
    } else {
548
6
      Z_TRY_ADDREF_P(val);
549
6
    }
550
18
  } ZEND_HASH_FOREACH_END();
551
6
}
552
/* }}} */
553
554
/* {{{ Define a new constant */
555
ZEND_FUNCTION(define)
556
315
{
557
315
  zend_string *name;
558
315
  zval *val;
559
315
  bool non_cs = 0;
560
315
  zend_constant c;
561
562
942
  ZEND_PARSE_PARAMETERS_START(2, 3)
563
1.24k
    Z_PARAM_STR(name)
564
1.54k
    Z_PARAM_ZVAL(val)
565
1.54k
    Z_PARAM_OPTIONAL
566
1.54k
    Z_PARAM_BOOL(non_cs)
567
315
  ZEND_PARSE_PARAMETERS_END();
568
569
309
  if (zend_memnstr(ZSTR_VAL(name), "::", sizeof("::") - 1, ZSTR_VAL(name) + ZSTR_LEN(name))) {
570
15
    zend_argument_value_error(1, "cannot be a class constant");
571
15
    RETURN_THROWS();
572
15
  }
573
574
294
  if (non_cs) {
575
0
    zend_error(E_WARNING, "define(): Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported");
576
0
  }
577
578
294
  if (Z_TYPE_P(val) == IS_ARRAY && Z_REFCOUNTED_P(val)) {
579
9
    if (!validate_constant_array_argument(Z_ARRVAL_P(val), 2)) {
580
3
      RETURN_THROWS();
581
6
    } else {
582
6
      copy_constant_array(&c.value, val);
583
6
    }
584
285
  } else {
585
285
    ZVAL_COPY(&c.value, val);
586
285
  }
587
588
  /* non persistent */
589
291
  ZEND_CONSTANT_SET_FLAGS(&c, 0, PHP_USER_CONSTANT);
590
291
  c.name = zend_string_copy(name);
591
291
  if (zend_register_constant(&c) != NULL) {
592
264
    RETURN_TRUE;
593
264
  } else {
594
27
    RETURN_FALSE;
595
27
  }
596
291
}
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
117
{
603
117
  zend_string *name;
604
605
351
  ZEND_PARSE_PARAMETERS_START(1, 1)
606
468
    Z_PARAM_STR(name)
607
117
  ZEND_PARSE_PARAMETERS_END();
608
609
117
  if (zend_get_constant_ex(name, zend_get_executed_scope(), ZEND_FETCH_CLASS_SILENT)) {
610
54
    RETURN_TRUE;
611
63
  } else {
612
63
    RETURN_FALSE;
613
63
  }
614
117
}
615
/* }}} */
616
617
/* {{{ Retrieves the class name */
618
ZEND_FUNCTION(get_class)
619
39
{
620
39
  zval *obj = NULL;
621
622
39
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o", &obj) == FAILURE) {
623
0
    RETURN_THROWS();
624
0
  }
625
626
39
  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
39
  RETURN_STR_COPY(Z_OBJCE_P(obj)->name);
642
39
}
643
/* }}} */
644
645
/* {{{ Retrieves the "Late Static Binding" class name */
646
ZEND_FUNCTION(get_called_class)
647
3
{
648
3
  zend_class_entry *called_scope;
649
650
3
  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
12
{
665
12
  zend_class_entry *ce = NULL;
666
667
36
  ZEND_PARSE_PARAMETERS_START(0, 1)
668
36
    Z_PARAM_OPTIONAL
669
42
    Z_PARAM_OBJ_OR_CLASS_NAME(ce)
670
42
  ZEND_PARSE_PARAMETERS_END();
671
672
12
  if (!ce) {
673
6
    zend_error(E_DEPRECATED, "Calling get_parent_class() without arguments is deprecated");
674
6
    if (UNEXPECTED(EG(exception))) {
675
0
      RETURN_THROWS();
676
0
    }
677
6
    ce = zend_get_executed_scope();
678
6
  }
679
680
12
  if (ce && ce->parent) {
681
6
    RETURN_STR_COPY(ce->parent->name);
682
6
  } else {
683
6
    RETURN_FALSE;
684
6
  }
685
12
}
686
/* }}} */
687
688
static void is_a_impl(INTERNAL_FUNCTION_PARAMETERS, bool only_subclass) /* {{{ */
689
117
{
690
117
  zval *obj;
691
117
  zend_string *class_name;
692
117
  zend_class_entry *instance_ce;
693
117
  zend_class_entry *ce;
694
117
  bool allow_string = only_subclass;
695
117
  bool retval;
696
697
342
  ZEND_PARSE_PARAMETERS_START(2, 3)
698
432
    Z_PARAM_ZVAL(obj)
699
432
    Z_PARAM_STR(class_name)
700
108
    Z_PARAM_OPTIONAL
701
216
    Z_PARAM_BOOL(allow_string)
702
117
  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
108
  if (allow_string && Z_TYPE_P(obj) == IS_STRING) {
711
15
    instance_ce = zend_lookup_class(Z_STR_P(obj));
712
15
    if (!instance_ce) {
713
0
      RETURN_FALSE;
714
0
    }
715
93
  } else if (Z_TYPE_P(obj) == IS_OBJECT) {
716
90
    instance_ce = Z_OBJCE_P(obj);
717
90
  } else {
718
3
    RETURN_FALSE;
719
3
  }
720
721
105
  if (!only_subclass && EXPECTED(zend_string_equals(instance_ce->name, class_name))) {
722
9
    retval = 1;
723
96
  } else {
724
96
    ce = zend_lookup_class_ex(class_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
725
96
    if (!ce) {
726
24
      retval = 0;
727
72
    } else {
728
72
      if (only_subclass && instance_ce == ce) {
729
24
        retval = 0;
730
48
      } else {
731
48
        retval = instanceof_function(instance_ce, ce);
732
48
      }
733
72
    }
734
96
  }
735
736
105
  RETURN_BOOL(retval);
737
105
}
738
/* }}} */
739
740
/* {{{ Returns true if the object has this class as one of its parents */
741
ZEND_FUNCTION(is_subclass_of)
742
75
{
743
75
  is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
744
75
}
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
42
{
750
42
  is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
751
42
}
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
174
{
757
174
  zend_property_info *prop_info;
758
174
  zval *prop, prop_copy;
759
174
  zend_string *key;
760
174
  zval *default_properties_table = CE_DEFAULT_PROPERTIES_TABLE(ce);
761
762
1.54k
  ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
763
1.54k
    if (((prop_info->flags & ZEND_ACC_PROTECTED) &&
764
186
       !zend_check_protected(prop_info->ce, scope)) ||
765
522
      ((prop_info->flags & ZEND_ACC_PRIVATE) &&
766
216
        prop_info->ce != scope) ||
767
366
      (prop_info->flags & ZEND_ACC_VIRTUAL)) {
768
240
      continue;
769
240
    }
770
360
    prop = NULL;
771
360
    if (statics && (prop_info->flags & ZEND_ACC_STATIC) != 0) {
772
99
      prop = &ce->default_static_members_table[prop_info->offset];
773
99
      ZVAL_DEINDIRECT(prop);
774
261
    } else if (!statics && (prop_info->flags & ZEND_ACC_STATIC) == 0) {
775
81
      prop = &default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
776
81
    }
777
360
    if (!prop) {
778
180
      continue;
779
180
    }
780
781
180
    if (Z_ISUNDEF_P(prop)) {
782
      /* Return uninitialized typed properties as a null value */
783
18
      ZVAL_NULL(&prop_copy);
784
162
    } else {
785
      /* copy: enforce read only access */
786
162
      ZVAL_COPY_OR_DUP(&prop_copy, prop);
787
162
    }
788
180
    prop = &prop_copy;
789
790
    /* this is necessary to make it able to work with default array
791
     * properties, returned to user */
792
180
    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
180
    zend_hash_add_new(Z_ARRVAL_P(return_value), key, prop);
799
180
  } ZEND_HASH_FOREACH_END();
800
174
}
801
/* }}} */
802
803
/* {{{ Returns an array of default properties of the class. */
804
ZEND_FUNCTION(get_class_vars)
805
93
{
806
93
  zend_class_entry *ce = NULL, *scope;
807
808
93
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "C", &ce) == FAILURE) {
809
3
    RETURN_THROWS();
810
3
  }
811
812
90
  array_init(return_value);
813
90
  if (UNEXPECTED(!(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
814
3
    if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
815
3
      return;
816
3
    }
817
3
  }
818
819
87
  scope = zend_get_executed_scope();
820
87
  add_class_vars(scope, ce, false, return_value);
821
87
  add_class_vars(scope, ce, true, return_value);
822
87
}
823
/* }}} */
824
825
/* {{{ Returns an array of object properties */
826
ZEND_FUNCTION(get_object_vars)
827
132
{
828
132
  zval *value;
829
132
  HashTable *properties;
830
132
  zend_string *key;
831
132
  zend_object *zobj;
832
132
  zend_ulong num_key;
833
834
396
  ZEND_PARSE_PARAMETERS_START(1, 1)
835
528
    Z_PARAM_OBJ(zobj)
836
132
  ZEND_PARSE_PARAMETERS_END();
837
838
132
  zval obj_zv;
839
132
  ZVAL_OBJ(&obj_zv, zobj);
840
132
  properties = zend_get_properties_for(&obj_zv, ZEND_PROP_PURPOSE_GET_OBJECT_VARS);
841
132
  if (properties == NULL) {
842
0
    RETURN_EMPTY_ARRAY();
843
0
  }
844
845
132
  if (!zobj->ce->default_properties_count && properties == zobj->properties && !GC_IS_RECURSIVE(properties)) {
846
    /* fast copy */
847
3
    bool always_duplicate = zobj->handlers != &std_object_handlers;
848
3
    RETVAL_ARR(zend_proptable_to_symtable(properties, always_duplicate));
849
129
  } else {
850
129
    array_init_size(return_value, zend_hash_num_elements(properties));
851
852
951
    ZEND_HASH_FOREACH_KEY_VAL(properties, num_key, key, value) {
853
951
      bool is_dynamic = 1;
854
951
      zval tmp;
855
951
      ZVAL_UNDEF(&tmp);
856
951
      if (Z_TYPE_P(value) == IS_INDIRECT) {
857
153
        value = Z_INDIRECT_P(value);
858
153
        if (UNEXPECTED(Z_ISUNDEF_P(value))) {
859
0
          continue;
860
0
        }
861
862
153
        is_dynamic = 0;
863
258
      } else if (Z_TYPE_P(value) == IS_PTR) {
864
255
        is_dynamic = 0;
865
255
      }
866
867
411
      if (key && zend_check_property_access(zobj, key, is_dynamic) == FAILURE) {
868
126
        continue;
869
126
      }
870
871
285
      if (Z_ISREF_P(value) && Z_REFCOUNT_P(value) == 1) {
872
6
        value = Z_REFVAL_P(value);
873
6
      }
874
285
      if (Z_TYPE_P(value) == IS_PTR) {
875
        /* value is IS_PTR for properties with hooks. */
876
177
        zend_property_info *prop_info = Z_PTR_P(value);
877
177
        if ((prop_info->flags & ZEND_ACC_VIRTUAL) && !prop_info->hooks[ZEND_PROPERTY_HOOK_GET]) {
878
36
          continue;
879
36
        }
880
141
        const char *unmangled_name_cstr = zend_get_unmangled_property_name(prop_info->name);
881
141
        zend_string *unmangled_name = zend_string_init(unmangled_name_cstr, strlen(unmangled_name_cstr), false);
882
141
        value = zend_read_property_ex(prop_info->ce, zobj, unmangled_name, /* silent */ true, &tmp);
883
141
        zend_string_release_ex(unmangled_name, false);
884
141
        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
141
      }
891
246
      Z_TRY_ADDREF_P(value);
892
893
246
      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
246
      } else if (!is_dynamic && ZSTR_VAL(key)[0] == 0) {
897
84
        const char *prop_name, *class_name;
898
84
        size_t prop_len;
899
84
        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
84
        zend_hash_str_add_new(Z_ARRVAL_P(return_value), prop_name, prop_len, value);
906
162
      } else {
907
162
        zend_symtable_add_new(Z_ARRVAL_P(return_value), key, value);
908
162
      }
909
246
      zval_ptr_dtor(&tmp);
910
246
    } ZEND_HASH_FOREACH_END();
911
129
  }
912
129
  zend_release_properties(properties);
913
129
}
914
/* }}} */
915
916
/* {{{ Returns an array of mangled object properties. Does not respect property visibility. */
917
ZEND_FUNCTION(get_mangled_object_vars)
918
45
{
919
45
  zend_object *obj;
920
45
  HashTable *properties;
921
922
135
  ZEND_PARSE_PARAMETERS_START(1, 1)
923
180
    Z_PARAM_OBJ(obj)
924
45
  ZEND_PARSE_PARAMETERS_END();
925
926
45
  properties = zend_get_properties_no_lazy_init(obj);
927
45
  if (!properties) {
928
0
    ZVAL_EMPTY_ARRAY(return_value);
929
0
    return;
930
0
  }
931
932
45
  properties = zend_proptable_to_symtable(properties,
933
45
    (obj->ce->default_properties_count ||
934
0
     obj->handlers != &std_object_handlers ||
935
0
     GC_IS_RECURSIVE(properties)));
936
45
  RETURN_ARR(properties);
937
45
}
938
/* }}} */
939
940
/* {{{ Returns an array of method names for class or class instance. */
941
ZEND_FUNCTION(get_class_methods)
942
66
{
943
66
  zval method_name;
944
66
  zend_class_entry *ce = NULL;
945
66
  zend_class_entry *scope;
946
66
  zend_function *mptr;
947
948
198
  ZEND_PARSE_PARAMETERS_START(1, 1)
949
324
    Z_PARAM_OBJ_OR_CLASS_NAME(ce)
950
324
  ZEND_PARSE_PARAMETERS_END();
951
952
60
  array_init(return_value);
953
60
  scope = zend_get_executed_scope();
954
955
600
  ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, mptr) {
956
600
    if (zend_check_method_accessible(mptr, scope)) {
957
177
      ZVAL_STR_COPY(&method_name, mptr->common.function_name);
958
177
      zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &method_name);
959
177
    }
960
600
  } ZEND_HASH_FOREACH_END();
961
60
}
962
/* }}} */
963
964
/* {{{ Checks if the class method exists */
965
ZEND_FUNCTION(method_exists)
966
147
{
967
147
  zval *klass;
968
147
  zend_string *method_name;
969
147
  zend_string *lcname;
970
147
  zend_class_entry *ce;
971
147
  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
441
  ZEND_PARSE_PARAMETERS_START(2, 2)
975
588
    Z_PARAM_ZVAL(klass)
976
588
    Z_PARAM_STR(method_name)
977
147
  ZEND_PARSE_PARAMETERS_END();
978
979
147
  if (Z_TYPE_P(klass) == IS_OBJECT) {
980
63
    ce = Z_OBJCE_P(klass);
981
84
  } else if (Z_TYPE_P(klass) == IS_STRING) {
982
78
    if ((ce = zend_lookup_class(Z_STR_P(klass))) == NULL) {
983
3
      RETURN_FALSE;
984
3
    }
985
78
  } else {
986
6
    zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_value_name(klass));
987
6
    RETURN_THROWS();
988
6
  }
989
990
138
  lcname = zend_string_tolower(method_name);
991
138
  func = zend_hash_find_ptr(&ce->function_table, lcname);
992
138
  zend_string_release_ex(lcname, 0);
993
994
138
  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
57
    RETURN_BOOL(Z_TYPE_P(klass) == IS_OBJECT
999
57
      || !(func->common.fn_flags & ZEND_ACC_PRIVATE) || func->common.scope == ce);
1000
57
  }
1001
1002
81
  if (Z_TYPE_P(klass) == IS_OBJECT) {
1003
39
    zend_object *obj = Z_OBJ_P(klass);
1004
39
    func = Z_OBJ_HT_P(klass)->get_method(&obj, method_name, NULL);
1005
39
    if (func != NULL) {
1006
36
      if (func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
1007
        /* Returns true for the fake Closure's __invoke */
1008
27
        RETVAL_BOOL(func->common.scope == zend_ce_closure
1009
27
          && zend_string_equals_literal_ci(method_name, ZEND_INVOKE_FUNC_NAME));
1010
1011
27
        zend_string_release_ex(func->common.function_name, 0);
1012
27
        zend_free_trampoline(func);
1013
27
        return;
1014
27
      }
1015
9
      RETURN_TRUE;
1016
9
    }
1017
42
  } else {
1018
      /* Returns true for fake Closure::__invoke */
1019
42
      if (ce == zend_ce_closure
1020
33
          && zend_string_equals_literal_ci(method_name, ZEND_INVOKE_FUNC_NAME)) {
1021
18
          RETURN_TRUE;
1022
18
      }
1023
42
  }
1024
27
  RETURN_FALSE;
1025
27
}
1026
/* }}} */
1027
1028
static void _property_exists(zval *return_value, zval *object, zend_string *property)
1029
246
{
1030
246
  zend_class_entry *ce;
1031
246
  zend_property_info *property_info;
1032
1033
246
  if (Z_TYPE_P(object) == IS_STRING) {
1034
144
    ce = zend_lookup_class(Z_STR_P(object));
1035
144
    if (!ce) {
1036
15
      RETURN_FALSE;
1037
15
    }
1038
144
  } else if (Z_TYPE_P(object) == IS_OBJECT) {
1039
87
    ce = Z_OBJCE_P(object);
1040
87
  } else {
1041
15
    zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_value_name(object));
1042
15
    RETURN_THROWS();
1043
15
  }
1044
1045
216
  property_info = zend_hash_find_ptr(&ce->properties_info, property);
1046
216
  if (property_info != NULL
1047
168
   && (!(property_info->flags & ZEND_ACC_PRIVATE)
1048
165
    || property_info->ce == ce)) {
1049
165
    RETURN_TRUE;
1050
165
  }
1051
1052
51
  if (Z_TYPE_P(object) == IS_OBJECT &&
1053
24
    Z_OBJ_HANDLER_P(object, has_property)(Z_OBJ_P(object), property, ZEND_PROPERTY_EXISTS, NULL)) {
1054
3
    RETURN_TRUE;
1055
3
  }
1056
48
  RETURN_FALSE;
1057
48
}
1058
1059
/* {{{ Checks if the object or class has a property */
1060
ZEND_FUNCTION(property_exists)
1061
246
{
1062
246
  zval *object;
1063
246
  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
246
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "zS", &object, &property) == FAILURE) {
1067
0
    RETURN_THROWS();
1068
0
  }
1069
1070
246
  _property_exists(return_value, object, property);
1071
246
}
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.20k
{
1091
1.20k
  zend_string *lcname;
1092
1.20k
  zend_class_entry *ce;
1093
1094
1.20k
  if (ZSTR_HAS_CE_CACHE(name)) {
1095
1.07k
    ce = ZSTR_GET_CE_CACHE(name);
1096
1.07k
    if (ce) {
1097
114
      RETURN_BOOL(((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags));
1098
114
    }
1099
1.07k
  }
1100
1101
1.09k
  if (!autoload) {
1102
72
    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
72
    } else {
1107
72
      lcname = zend_string_tolower(name);
1108
72
    }
1109
1110
72
    ce = zend_hash_find_ptr(EG(class_table), lcname);
1111
72
    zend_string_release_ex(lcname, 0);
1112
1.02k
  } else {
1113
1.02k
    ce = zend_lookup_class(name);
1114
1.02k
  }
1115
1116
1.09k
  if (ce) {
1117
930
    RETURN_BOOL(((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags));
1118
930
  } else {
1119
162
    RETURN_FALSE;
1120
162
  }
1121
1.09k
}
1122
/* {{{ */
1123
1124
static inline void class_exists_impl(INTERNAL_FUNCTION_PARAMETERS, int flags, int skip_flags) /* {{{ */
1125
1.20k
{
1126
1.20k
  zend_string *name;
1127
1.20k
  bool autoload = true;
1128
1129
3.61k
  ZEND_PARSE_PARAMETERS_START(1, 2)
1130
4.82k
    Z_PARAM_STR(name)
1131
1.20k
    Z_PARAM_OPTIONAL
1132
2.65k
    Z_PARAM_BOOL(autoload)
1133
1.20k
  ZEND_PARSE_PARAMETERS_END();
1134
1135
1.20k
  _class_exists_impl(return_value, name, autoload, flags, skip_flags);
1136
1.20k
}
1137
1138
/* {{{ Checks if the class exists */
1139
ZEND_FUNCTION(class_exists)
1140
1.01k
{
1141
1.01k
  class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED, ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT);
1142
1.01k
}
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
102
{
1176
102
  class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED|ZEND_ACC_INTERFACE, 0);
1177
102
}
1178
/* }}} */
1179
1180
/* {{{ Checks if the trait exists */
1181
ZEND_FUNCTION(trait_exists)
1182
30
{
1183
30
  class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_TRAIT, 0);
1184
30
}
1185
/* }}} */
1186
1187
ZEND_FUNCTION(enum_exists)
1188
60
{
1189
60
  class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_ENUM, 0);
1190
60
}
1191
1192
/* {{{ Checks if the function exists */
1193
ZEND_FUNCTION(function_exists)
1194
33
{
1195
33
  zend_string *name;
1196
33
  bool exists;
1197
33
  zend_string *lcname;
1198
1199
99
  ZEND_PARSE_PARAMETERS_START(1, 1)
1200
132
    Z_PARAM_STR(name)
1201
33
  ZEND_PARSE_PARAMETERS_END();
1202
1203
33
  if (ZSTR_VAL(name)[0] == '\\') {
1204
    /* Ignore leading "\" */
1205
9
    lcname = zend_string_alloc(ZSTR_LEN(name) - 1, 0);
1206
9
    zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
1207
24
  } else {
1208
24
    lcname = zend_string_tolower(name);
1209
24
  }
1210
1211
33
  exists = zend_hash_exists(EG(function_table), lcname);
1212
33
  zend_string_release_ex(lcname, 0);
1213
1214
33
  RETURN_BOOL(exists);
1215
33
}
1216
/* }}} */
1217
1218
/* {{{ Creates an alias for user defined class */
1219
ZEND_FUNCTION(class_alias)
1220
138
{
1221
138
  zend_string *class_name;
1222
138
  zend_string *alias_name;
1223
138
  zend_class_entry *ce;
1224
138
  bool autoload = 1;
1225
1226
411
  ZEND_PARSE_PARAMETERS_START(2, 3)
1227
540
    Z_PARAM_STR(class_name)
1228
675
    Z_PARAM_STR(alias_name)
1229
135
    Z_PARAM_OPTIONAL
1230
276
    Z_PARAM_BOOL(autoload)
1231
138
  ZEND_PARSE_PARAMETERS_END();
1232
1233
135
  ce = zend_lookup_class_ex(class_name, NULL, !autoload ? ZEND_FETCH_CLASS_NO_AUTOLOAD : 0);
1234
1235
135
  if (ce) {
1236
126
    if (zend_register_class_alias_ex(ZSTR_VAL(alias_name), ZSTR_LEN(alias_name), ce, false) == SUCCESS) {
1237
105
      RETURN_TRUE;
1238
105
    } else {
1239
21
      zend_class_redeclaration_error_ex(E_WARNING, alias_name, ce);
1240
21
      RETURN_FALSE;
1241
21
    }
1242
126
  } else {
1243
9
    zend_error(E_WARNING, "Class \"%s\" not found", ZSTR_VAL(class_name));
1244
9
    RETURN_FALSE;
1245
9
  }
1246
135
}
1247
/* }}} */
1248
1249
/* {{{ Returns an array with the file names that were include_once()'d */
1250
ZEND_FUNCTION(get_included_files)
1251
21
{
1252
21
  zend_string *entry;
1253
1254
21
  ZEND_PARSE_PARAMETERS_NONE();
1255
1256
21
  array_init(return_value);
1257
56
  ZEND_HASH_MAP_FOREACH_STR_KEY(&EG(included_files), entry) {
1258
56
    if (entry) {
1259
7
      add_next_index_str(return_value, zend_string_copy(entry));
1260
7
    }
1261
56
  } ZEND_HASH_FOREACH_END();
1262
21
}
1263
/* }}} */
1264
1265
/* {{{ Generates a user-level error/warning/notice message */
1266
ZEND_FUNCTION(trigger_error)
1267
72
{
1268
72
  zend_long error_type = E_USER_NOTICE;
1269
72
  zend_string *message;
1270
1271
72
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &message, &error_type) == FAILURE) {
1272
0
    RETURN_THROWS();
1273
0
  }
1274
1275
72
  switch (error_type) {
1276
24
    case E_USER_ERROR:
1277
24
      zend_error(E_DEPRECATED, "Passing E_USER_ERROR to trigger_error() is deprecated since 8.4,"
1278
24
        " throw an exception or call exit with a string message instead");
1279
24
      if (UNEXPECTED(EG(exception))) {
1280
0
        RETURN_THROWS();
1281
0
      }
1282
48
    case E_USER_WARNING:
1283
63
    case E_USER_NOTICE:
1284
66
    case E_USER_DEPRECATED:
1285
66
      break;
1286
6
    default:
1287
6
      zend_argument_value_error(2, "must be one of E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE,"
1288
6
        " or E_USER_DEPRECATED");
1289
6
      RETURN_THROWS();
1290
0
      break;
1291
72
  }
1292
1293
66
  zend_error_zstr_at(error_type, zend_get_executed_filename_ex(), zend_get_executed_lineno(), message);
1294
  // TODO Change to void
1295
66
  RETURN_TRUE;
1296
66
}
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
6
{
1360
6
  ZEND_PARSE_PARAMETERS_NONE();
1361
1362
6
  if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
1363
0
    RETURN_COPY(&EG(user_error_handler));
1364
0
  }
1365
6
}
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
105
{
1370
105
  zend_fcall_info fci;
1371
105
  zend_fcall_info_cache fcc;
1372
1373
315
  ZEND_PARSE_PARAMETERS_START(1, 1)
1374
420
    Z_PARAM_FUNC_OR_NULL(fci, fcc)
1375
105
  ZEND_PARSE_PARAMETERS_END();
1376
1377
96
  if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1378
42
    ZVAL_COPY(return_value, &EG(user_exception_handler));
1379
42
  }
1380
1381
96
  zend_stack_push(&EG(user_exception_handlers), &EG(user_exception_handler));
1382
1383
96
  if (!ZEND_FCI_INITIALIZED(fci)) { /* unset user-defined handler */
1384
15
    ZVAL_UNDEF(&EG(user_exception_handler));
1385
15
    return;
1386
15
  }
1387
1388
81
  ZVAL_COPY(&EG(user_exception_handler), &(fci.function_name));
1389
81
}
1390
/* }}} */
1391
1392
/* {{{ Restores the previously defined exception handler function */
1393
ZEND_FUNCTION(restore_exception_handler)
1394
12
{
1395
12
  ZEND_PARSE_PARAMETERS_NONE();
1396
1397
12
  if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1398
6
    zval_ptr_dtor(&EG(user_exception_handler));
1399
6
  }
1400
12
  if (zend_stack_is_empty(&EG(user_exception_handlers))) {
1401
0
    ZVAL_UNDEF(&EG(user_exception_handler));
1402
12
  } else {
1403
12
    zval *tmp = zend_stack_top(&EG(user_exception_handlers));
1404
12
    ZVAL_COPY_VALUE(&EG(user_exception_handler), tmp);
1405
12
    zend_stack_del_top(&EG(user_exception_handlers));
1406
12
  }
1407
1408
  // TODO Change to void
1409
12
  RETURN_TRUE;
1410
12
}
1411
/* }}} */
1412
1413
ZEND_FUNCTION(get_exception_handler)
1414
33
{
1415
33
  ZEND_PARSE_PARAMETERS_NONE();
1416
1417
33
  if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1418
27
    RETURN_COPY(&EG(user_exception_handler));
1419
27
  }
1420
33
}
1421
1422
static inline void get_declared_class_impl(INTERNAL_FUNCTION_PARAMETERS, int flags) /* {{{ */
1423
30
{
1424
30
  zend_string *key;
1425
30
  zval *zv;
1426
30
  zend_class_entry *ce;
1427
1428
30
  ZEND_PARSE_PARAMETERS_NONE();
1429
1430
30
  array_init(return_value);
1431
30
  zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
1432
30
  ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
1433
10.1k
    ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(EG(class_table), key, zv) {
1434
10.1k
      ce = Z_PTR_P(zv);
1435
10.1k
      if ((ce->ce_flags & (ZEND_ACC_LINKED|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT)) == flags
1436
2.64k
       && key
1437
2.64k
       && ZSTR_VAL(key)[0] != 0) {
1438
2.64k
        ZEND_HASH_FILL_GROW();
1439
2.64k
        if (EXPECTED(Z_TYPE_P(zv) == IS_PTR)) {
1440
2.64k
          ZEND_HASH_FILL_SET_STR_COPY(ce->name);
1441
2.64k
        } else {
1442
6
          ZEND_ASSERT(Z_TYPE_P(zv) == IS_ALIAS_PTR);
1443
6
          ZEND_HASH_FILL_SET_STR_COPY(key);
1444
6
        }
1445
2.64k
        ZEND_HASH_FILL_NEXT();
1446
2.64k
      }
1447
10.1k
    } ZEND_HASH_FOREACH_END();
1448
30
  } ZEND_HASH_FILL_END();
1449
30
}
1450
/* {{{ */
1451
1452
/* {{{ Returns an array of all declared traits. */
1453
ZEND_FUNCTION(get_declared_traits)
1454
12
{
1455
12
  get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED | ZEND_ACC_TRAIT);
1456
12
}
1457
/* }}} */
1458
1459
/* {{{ Returns an array of all declared classes. */
1460
ZEND_FUNCTION(get_declared_classes)
1461
18
{
1462
18
  get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED);
1463
18
}
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
19
{
1476
19
  zval internal, user;
1477
19
  zend_string *key;
1478
19
  zend_function *func;
1479
19
  bool exclude_disabled = true;
1480
1481
19
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &exclude_disabled) == FAILURE) {
1482
0
    RETURN_THROWS();
1483
0
  }
1484
1485
19
  if (ZEND_NUM_ARGS() == 1) {
1486
9
    zend_error(E_DEPRECATED,
1487
9
      "get_defined_functions(): The $exclude_disabled parameter has no effect since PHP 8.0");
1488
9
  }
1489
1490
19
  array_init(&internal);
1491
19
  array_init(&user);
1492
19
  array_init(return_value);
1493
1494
26.1k
  ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(EG(function_table), key, func) {
1495
26.1k
    if (key && ZSTR_VAL(key)[0] != 0) {
1496
13.0k
      if (func->type == ZEND_INTERNAL_FUNCTION) {
1497
13.0k
        add_next_index_str(&internal, zend_string_copy(key));
1498
13.0k
      } else if (func->type == ZEND_USER_FUNCTION) {
1499
12
        add_next_index_str(&user, zend_string_copy(key));
1500
12
      }
1501
13.0k
    }
1502
26.1k
  } ZEND_HASH_FOREACH_END();
1503
1504
19
  zend_hash_str_add_new(Z_ARRVAL_P(return_value), "internal", sizeof("internal")-1, &internal);
1505
19
  zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_USER), &user);
1506
19
}
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
57
{
1512
57
  zend_array *symbol_table;
1513
1514
57
  ZEND_PARSE_PARAMETERS_NONE();
1515
1516
57
  if (zend_forbid_dynamic_call() == FAILURE) {
1517
9
    return;
1518
9
  }
1519
1520
48
  symbol_table = zend_rebuild_symbol_table();
1521
48
  if (UNEXPECTED(symbol_table == NULL)) {
1522
0
    RETURN_EMPTY_ARRAY();
1523
0
  }
1524
1525
48
  RETURN_ARR(zend_array_dup(symbol_table));
1526
48
}
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
6
{
1650
6
  bool categorize = false;
1651
1652
6
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &categorize) == FAILURE) {
1653
0
    RETURN_THROWS();
1654
0
  }
1655
1656
6
  array_init(return_value);
1657
1658
6
  if (categorize) {
1659
3
    zend_constant *val;
1660
3
    int module_number;
1661
3
    zval *modules, const_val;
1662
3
    char **module_names;
1663
3
    zend_module_entry *module;
1664
3
    int i = 1;
1665
1666
3
    modules = ecalloc(zend_hash_num_elements(&module_registry) + 2, sizeof(zval));
1667
3
    module_names = emalloc((zend_hash_num_elements(&module_registry) + 2) * sizeof(char *));
1668
1669
3
    module_names[0] = "internal";
1670
84
    ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
1671
84
      module_names[module->module_number] = (char *)module->name;
1672
84
      i++;
1673
84
    } ZEND_HASH_FOREACH_END();
1674
3
    module_names[i] = "user";
1675
1676
3.27k
    ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), val) {
1677
3.27k
      if (!val->name) {
1678
        /* skip special constants */
1679
0
        continue;
1680
0
      }
1681
1682
1.63k
      if (ZEND_CONSTANT_MODULE_NUMBER(val) == PHP_USER_CONSTANT) {
1683
0
        module_number = i;
1684
1.63k
      } else if (ZEND_CONSTANT_MODULE_NUMBER(val) > i) {
1685
        /* should not happen */
1686
0
        continue;
1687
1.63k
      } else {
1688
1.63k
        module_number = ZEND_CONSTANT_MODULE_NUMBER(val);
1689
1.63k
      }
1690
1691
1.63k
      if (Z_TYPE(modules[module_number]) == IS_UNDEF) {
1692
24
        array_init(&modules[module_number]);
1693
24
        add_assoc_zval(return_value, module_names[module_number], &modules[module_number]);
1694
24
      }
1695
1696
1.63k
      ZVAL_COPY_OR_DUP(&const_val, &val->value);
1697
1.63k
      zend_hash_add_new(Z_ARRVAL(modules[module_number]), val->name, &const_val);
1698
1.63k
    } ZEND_HASH_FOREACH_END();
1699
1700
3
    efree(module_names);
1701
3
    efree(modules);
1702
3
  } else {
1703
3
    zend_constant *constant;
1704
3
    zval const_val;
1705
1706
3.27k
    ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), constant) {
1707
3.27k
      if (!constant->name) {
1708
        /* skip special constants */
1709
0
        continue;
1710
0
      }
1711
1.63k
      ZVAL_COPY_OR_DUP(&const_val, &constant->value);
1712
1.63k
      zend_hash_add_new(Z_ARRVAL_P(return_value), constant->name, &const_val);
1713
1.63k
    } ZEND_HASH_FOREACH_END();
1714
3
  }
1715
6
}
1716
/* }}} */
1717
1718
static bool backtrace_is_arg_sensitive(const zend_execute_data *call, uint32_t offset)
1719
406k
{
1720
406k
  zend_attribute *attribute = zend_get_parameter_attribute_str(
1721
406k
    call->func->common.attributes,
1722
406k
    "sensitiveparameter",
1723
406k
    sizeof("sensitiveparameter") - 1,
1724
406k
    offset
1725
406k
  );
1726
1727
406k
  return attribute != NULL;
1728
406k
}
1729
1730
static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array) /* {{{ */
1731
303k
{
1732
303k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
1733
1734
303k
  if (num_args) {
1735
236k
    uint32_t i = 0;
1736
236k
    zval *p = ZEND_CALL_ARG(call, 1);
1737
1738
236k
    array_init_size(arg_array, num_args);
1739
236k
    zend_hash_real_init_packed(Z_ARRVAL_P(arg_array));
1740
236k
    ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(arg_array)) {
1741
236k
      if (call->func->type == ZEND_USER_FUNCTION) {
1742
7.57k
        uint32_t first_extra_arg = MIN(num_args, call->func->op_array.num_args);
1743
1744
7.57k
        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
530
          while (i < first_extra_arg) {
1750
287
            zend_string *arg_name = call->func->op_array.vars[i];
1751
287
            zval original_arg;
1752
287
            zval *arg = zend_hash_find_ex_ind(call->symbol_table, arg_name, 1);
1753
287
            bool is_sensitive = backtrace_is_arg_sensitive(call, i);
1754
1755
287
            if (arg) {
1756
287
              ZVAL_DEREF(arg);
1757
287
              ZVAL_COPY_VALUE(&original_arg, arg);
1758
287
            } else {
1759
0
              ZVAL_NULL(&original_arg);
1760
0
            }
1761
1762
287
            if (is_sensitive) {
1763
3
              zval redacted_arg;
1764
3
              object_init_with_constructor(&redacted_arg, zend_ce_sensitive_parameter_value, 1, &original_arg, NULL);
1765
3
              ZEND_HASH_FILL_SET(&redacted_arg);
1766
284
            } else {
1767
284
              Z_TRY_ADDREF_P(&original_arg);
1768
284
              ZEND_HASH_FILL_SET(&original_arg);
1769
284
            }
1770
1771
287
            ZEND_HASH_FILL_NEXT();
1772
287
            i++;
1773
287
          }
1774
7.33k
        } else {
1775
16.3k
          while (i < first_extra_arg) {
1776
9.05k
            zval original_arg;
1777
9.05k
            bool is_sensitive = backtrace_is_arg_sensitive(call, i);
1778
1779
9.05k
            if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
1780
8.93k
              zval *arg = p;
1781
8.93k
              ZVAL_DEREF(arg);
1782
8.93k
              ZVAL_COPY_VALUE(&original_arg, arg);
1783
8.93k
            } else {
1784
114
              ZVAL_NULL(&original_arg);
1785
114
            }
1786
1787
9.05k
            if (is_sensitive) {
1788
177
              zval redacted_arg;
1789
177
              object_init_with_constructor(&redacted_arg, zend_ce_sensitive_parameter_value, 1, &original_arg, NULL);
1790
177
              ZEND_HASH_FILL_SET(&redacted_arg);
1791
8.87k
            } else {
1792
8.87k
              Z_TRY_ADDREF_P(&original_arg);
1793
8.87k
              ZEND_HASH_FILL_SET(&original_arg);
1794
8.87k
            }
1795
1796
9.05k
            ZEND_HASH_FILL_NEXT();
1797
9.05k
            p++;
1798
9.05k
            i++;
1799
9.05k
          }
1800
7.33k
        }
1801
7.57k
        p = ZEND_CALL_VAR_NUM(call, call->func->op_array.last_var + call->func->op_array.T);
1802
7.57k
      }
1803
1804
683k
      while (i < num_args) {
1805
446k
        zval original_arg;
1806
446k
        bool is_sensitive = 0;
1807
1808
446k
        if (i < call->func->common.num_args || call->func->common.fn_flags & ZEND_ACC_VARIADIC) {
1809
397k
          is_sensitive = backtrace_is_arg_sensitive(call, MIN(i, call->func->common.num_args));
1810
397k
        }
1811
1812
446k
        if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
1813
446k
          zval *arg = p;
1814
446k
          ZVAL_DEREF(arg);
1815
446k
          ZVAL_COPY_VALUE(&original_arg, arg);
1816
446k
        } else {
1817
15
          ZVAL_NULL(&original_arg);
1818
15
        }
1819
1820
446k
        if (is_sensitive) {
1821
36
          zval redacted_arg;
1822
36
          object_init_with_constructor(&redacted_arg, zend_ce_sensitive_parameter_value, 1, &original_arg, NULL);
1823
36
          ZEND_HASH_FILL_SET(&redacted_arg);
1824
446k
        } else {
1825
446k
          Z_TRY_ADDREF_P(&original_arg);
1826
446k
          ZEND_HASH_FILL_SET(&original_arg);
1827
446k
        }
1828
1829
446k
        ZEND_HASH_FILL_NEXT();
1830
446k
        p++;
1831
446k
        i++;
1832
446k
      }
1833
236k
    } ZEND_HASH_FILL_END();
1834
236k
    Z_ARRVAL_P(arg_array)->nNumOfElements = num_args;
1835
236k
  } else {
1836
66.8k
    ZVAL_EMPTY_ARRAY(arg_array);
1837
66.8k
  }
1838
1839
303k
  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
72
   && (call->func->common.fn_flags & ZEND_ACC_VARIADIC)) {
1844
51
    zend_string *name;
1845
51
    zval *arg;
1846
1847
51
    bool is_sensitive = backtrace_is_arg_sensitive(call, call->func->common.num_args);
1848
1849
51
    SEPARATE_ARRAY(arg_array);
1850
234
    ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(call->extra_named_params, name, arg) {
1851
234
      ZVAL_DEREF(arg);
1852
234
      if (is_sensitive) {
1853
6
        zval redacted_arg;
1854
6
        object_init_with_constructor(&redacted_arg, zend_ce_sensitive_parameter_value, 1, arg, NULL);
1855
6
        zend_hash_add_new(Z_ARRVAL_P(arg_array), name, &redacted_arg);
1856
60
      } else {
1857
60
        Z_TRY_ADDREF_P(arg);
1858
60
        zend_hash_add_new(Z_ARRVAL_P(arg_array), name, arg);
1859
60
      }
1860
234
    } ZEND_HASH_FOREACH_END();
1861
51
  }
1862
303k
}
1863
/* }}} */
1864
1865
/* {{{ */
1866
ZEND_FUNCTION(debug_print_backtrace)
1867
177
{
1868
177
  zend_long options = 0;
1869
177
  zend_long limit = 0;
1870
177
  zval backtrace;
1871
1872
177
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &options, &limit) == FAILURE) {
1873
0
    RETURN_THROWS();
1874
0
  }
1875
1876
177
  zend_fetch_debug_backtrace(&backtrace, 1, options, limit);
1877
177
  ZEND_ASSERT(Z_TYPE(backtrace) == IS_ARRAY);
1878
1879
177
  zend_string *str = zend_trace_to_string(Z_ARRVAL(backtrace), /* include_main */ false);
1880
177
  ZEND_WRITE(ZSTR_VAL(str), ZSTR_LEN(str));
1881
177
  zend_string_release(str);
1882
177
  zval_ptr_dtor(&backtrace);
1883
177
}
1884
1885
/* }}} */
1886
1887
ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit) /* {{{ */
1888
249k
{
1889
249k
  zend_execute_data *call, *last_call = NULL;
1890
249k
  zend_object *object;
1891
249k
  bool fake_frame = false;
1892
249k
  int lineno, frameno = 0;
1893
249k
  zend_function *func;
1894
249k
  zend_string *filename;
1895
249k
  zend_string *include_filename = NULL;
1896
249k
  zval tmp;
1897
249k
  HashTable *stack_frame, *prev_stack_frame = NULL;
1898
1899
249k
  array_init(return_value);
1900
1901
249k
  call = EG(current_execute_data);
1902
249k
  if (!call) {
1903
2.26k
    return;
1904
2.26k
  }
1905
1906
247k
  if (EG(filename_override)) {
1907
    // Add the current execution point to the frame so we don't lose it
1908
87
    zend_string *filename_override = EG(filename_override);
1909
87
    zend_long lineno_override = EG(lineno_override);
1910
87
    EG(filename_override) = NULL;
1911
87
    EG(lineno_override) = -1;
1912
1913
87
    zend_string *filename = zend_get_executed_filename_ex();
1914
87
    zend_long lineno = zend_get_executed_lineno();
1915
87
    if (filename && (!zend_string_equals(filename, filename_override) || lineno != lineno_override)) {
1916
81
      stack_frame = zend_new_array(8);
1917
81
      zend_hash_real_init_mixed(stack_frame);
1918
81
      ZVAL_STR_COPY(&tmp, filename);
1919
81
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FILE), &tmp, 1);
1920
81
      ZVAL_LONG(&tmp, lineno);
1921
81
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_LINE), &tmp, 1);
1922
81
      ZVAL_STR_COPY(&tmp, ZSTR_KNOWN(ZEND_STR_CONST_EXPR_PLACEHOLDER));
1923
81
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp, 1);
1924
81
      ZVAL_ARR(&tmp, stack_frame);
1925
81
      zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
1926
81
    }
1927
1928
87
    EG(filename_override) = filename_override;
1929
87
    EG(lineno_override) = lineno_override;
1930
87
  }
1931
1932
247k
  if (skip_last) {
1933
    /* skip debug_backtrace() */
1934
366
    last_call = call;
1935
366
    call = call->prev_execute_data;
1936
366
  }
1937
1938
1.03M
  while (call && (limit == 0 || frameno < limit)) {
1939
784k
    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
3
      call = zend_generator_check_placeholder_frame(call);
1946
3
      ZEND_ASSERT(call->func);
1947
3
    }
1948
1949
784k
    zend_execute_data *prev = call->prev_execute_data;
1950
1951
784k
    if (!prev) {
1952
      /* add frame for a handler call without {main} code */
1953
247k
      if (EXPECTED((ZEND_CALL_INFO(call) & ZEND_CALL_TOP_FUNCTION) == 0)) {
1954
18
        break;
1955
18
      }
1956
537k
    } else if (UNEXPECTED((ZEND_CALL_INFO(call) & ZEND_CALL_GENERATOR) != 0)) {
1957
255
      prev = zend_generator_check_placeholder_frame(prev);
1958
255
    }
1959
1960
    /* For frameless calls we add an additional frame for the call itself. */
1961
784k
    if (ZEND_USER_CODE(call->func->type)) {
1962
555k
      const zend_op *opline = call->opline;
1963
555k
      if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) {
1964
555k
        goto not_frameless_call;
1965
555k
      }
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
784k
not_frameless_call:
2039
2040
    /* We use _zend_hash_append*() and the array must be preallocated */
2041
784k
    stack_frame = zend_new_array(8);
2042
784k
    zend_hash_real_init_mixed(stack_frame);
2043
2044
784k
    if (prev && prev->func && ZEND_USER_CODE(prev->func->common.type)) {
2045
536k
      filename = prev->func->op_array.filename;
2046
536k
      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
536k
      } else {
2053
536k
        lineno = prev->opline->lineno;
2054
536k
      }
2055
536k
      ZVAL_STR_COPY(&tmp, filename);
2056
536k
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FILE), &tmp, 1);
2057
536k
      ZVAL_LONG(&tmp, lineno);
2058
536k
      _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
536k
    } else {
2064
247k
      zend_execute_data *prev_call = prev;
2065
2066
247k
      while (prev_call) {
2067
762
        zend_execute_data *prev;
2068
2069
762
        if (prev_call &&
2070
762
          prev_call->func &&
2071
759
          !ZEND_USER_CODE(prev_call->func->common.type) &&
2072
759
          !(prev_call->func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
2073
753
          break;
2074
753
        }
2075
2076
9
        prev = prev_call->prev_execute_data;
2077
9
        if (prev && prev->func && ZEND_USER_CODE(prev->func->common.type)) {
2078
9
          ZVAL_STR_COPY(&tmp, prev->func->op_array.filename);
2079
9
          _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FILE), &tmp, 1);
2080
9
          ZVAL_LONG(&tmp, prev->opline->lineno);
2081
9
          _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_LINE), &tmp, 1);
2082
9
          break;
2083
9
        }
2084
0
        prev_call = prev;
2085
0
      }
2086
247k
      filename = NULL;
2087
247k
    }
2088
2089
784k
    func = call->func;
2090
784k
    if (!fake_frame && func->common.function_name) {
2091
303k
      ZVAL_STR_COPY(&tmp, func->common.function_name);
2092
303k
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp, 1);
2093
2094
303k
      if (Z_TYPE(call->This) == IS_OBJECT) {
2095
231k
        object = Z_OBJ(call->This);
2096
        /* $this may be passed into regular internal functions */
2097
231k
        if (func->common.scope) {
2098
231k
          ZVAL_STR_COPY(&tmp, func->common.scope->name);
2099
231k
        } 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
231k
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_CLASS), &tmp, 1);
2105
231k
        if ((options & DEBUG_BACKTRACE_PROVIDE_OBJECT) != 0) {
2106
108
          ZVAL_OBJ_COPY(&tmp, object);
2107
108
          _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_OBJECT), &tmp, 1);
2108
108
        }
2109
2110
231k
        ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_OBJECT_OPERATOR));
2111
231k
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_TYPE), &tmp, 1);
2112
231k
      } else if (func->common.scope) {
2113
483
        ZVAL_STR_COPY(&tmp, func->common.scope->name);
2114
483
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_CLASS), &tmp, 1);
2115
483
        ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM));
2116
483
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_TYPE), &tmp, 1);
2117
483
      }
2118
2119
303k
      if ((options & DEBUG_BACKTRACE_IGNORE_ARGS) == 0 &&
2120
303k
        func->type != ZEND_EVAL_CODE) {
2121
2122
303k
        debug_backtrace_get_args(call, &tmp);
2123
303k
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_ARGS), &tmp, 1);
2124
303k
      }
2125
481k
    } else {
2126
      /* i know this is kinda ugly, but i'm trying to avoid extra cycles in the main execution loop */
2127
481k
      bool build_filename_arg = true;
2128
481k
      zend_string *pseudo_function_name;
2129
481k
      uint32_t include_kind = 0;
2130
481k
      if (prev && prev->func && ZEND_USER_CODE(prev->func->common.type) && prev->opline->opcode == ZEND_INCLUDE_OR_EVAL) {
2131
242k
        include_kind = prev->opline->extended_value;
2132
242k
      }
2133
2134
481k
      switch (include_kind) {
2135
276
        case ZEND_EVAL:
2136
276
          pseudo_function_name = ZSTR_KNOWN(ZEND_STR_EVAL);
2137
276
          build_filename_arg = false;
2138
276
          break;
2139
21
        case ZEND_INCLUDE:
2140
21
          pseudo_function_name = ZSTR_KNOWN(ZEND_STR_INCLUDE);
2141
21
          break;
2142
242k
        case ZEND_REQUIRE:
2143
242k
          pseudo_function_name = ZSTR_KNOWN(ZEND_STR_REQUIRE);
2144
242k
          break;
2145
3
        case ZEND_INCLUDE_ONCE:
2146
3
          pseudo_function_name = ZSTR_KNOWN(ZEND_STR_INCLUDE_ONCE);
2147
3
          break;
2148
11
        case ZEND_REQUIRE_ONCE:
2149
11
          pseudo_function_name = ZSTR_KNOWN(ZEND_STR_REQUIRE_ONCE);
2150
11
          break;
2151
238k
        default:
2152
          /* Skip dummy frame unless it is needed to preserve filename/lineno info. */
2153
238k
          if (!filename) {
2154
238k
            zend_array_destroy(stack_frame);
2155
238k
            goto skip_frame;
2156
238k
          }
2157
2158
0
          pseudo_function_name = ZSTR_KNOWN(ZEND_STR_UNKNOWN);
2159
0
          build_filename_arg = false;
2160
0
          break;
2161
481k
      }
2162
2163
242k
      if (build_filename_arg && include_filename) {
2164
242k
        zval arg_array;
2165
2166
242k
        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
242k
        ZVAL_STR_COPY(&tmp, include_filename);
2173
242k
        zend_hash_next_index_insert_new(Z_ARRVAL(arg_array), &tmp);
2174
242k
        _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_ARGS), &arg_array, 1);
2175
242k
      }
2176
2177
242k
      ZVAL_INTERNED_STR(&tmp, pseudo_function_name);
2178
242k
      _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp, 1);
2179
242k
    }
2180
2181
546k
    ZVAL_ARR(&tmp, stack_frame);
2182
546k
    zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
2183
546k
    frameno++;
2184
546k
    prev_stack_frame = stack_frame;
2185
2186
784k
skip_frame:
2187
784k
    if (UNEXPECTED(ZEND_CALL_KIND(call) == ZEND_CALL_TOP_FUNCTION)
2188
68.9k
     && !fake_frame
2189
68.9k
     && prev
2190
60.2k
     && prev->func
2191
60.2k
     && ZEND_USER_CODE(prev->func->common.type)
2192
59.6k
     && prev->opline->opcode == ZEND_INCLUDE_OR_EVAL) {
2193
39
      fake_frame = true;
2194
784k
    } else {
2195
784k
      fake_frame = false;
2196
784k
      include_filename = filename;
2197
784k
      last_call = call;
2198
784k
      call = prev;
2199
784k
    }
2200
784k
  }
2201
247k
}
2202
/* }}} */
2203
2204
/* {{{ Return backtrace as array */
2205
ZEND_FUNCTION(debug_backtrace)
2206
189
{
2207
189
  zend_long options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
2208
189
  zend_long limit = 0;
2209
2210
189
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &options, &limit) == FAILURE) {
2211
0
    RETURN_THROWS();
2212
0
  }
2213
2214
189
  zend_fetch_debug_backtrace(return_value, 1, options, limit);
2215
189
}
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
3
{
2241
3
  zend_string *extension_name;
2242
3
  zend_string *lcname;
2243
3
  bool array;
2244
3
  zend_module_entry *module;
2245
3
  zend_function *zif;
2246
2247
3
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &extension_name) == FAILURE) {
2248
0
    RETURN_THROWS();
2249
0
  }
2250
3
  if (strncasecmp(ZSTR_VAL(extension_name), "zend", sizeof("zend"))) {
2251
3
    lcname = zend_string_tolower(extension_name);
2252
3
    module = zend_hash_find_ptr(&module_registry, lcname);
2253
3
    zend_string_release_ex(lcname, 0);
2254
3
  } else {
2255
0
    module = zend_hash_str_find_ptr(&module_registry, "core", sizeof("core") - 1);
2256
0
  }
2257
2258
3
  if (!module) {
2259
0
    RETURN_FALSE;
2260
0
  }
2261
2262
3
  if (module->functions) {
2263
    /* avoid BC break, if functions list is empty, will return an empty array */
2264
3
    array_init(return_value);
2265
3
    array = true;
2266
3
  } else {
2267
0
    array = false;
2268
0
  }
2269
2270
4.12k
  ZEND_HASH_MAP_FOREACH_PTR(CG(function_table), zif) {
2271
4.12k
    if (zif->common.type == ZEND_INTERNAL_FUNCTION
2272
2.06k
      && zif->internal_function.module == module) {
2273
1.53k
      if (!array) {
2274
0
        array_init(return_value);
2275
0
        array = true;
2276
0
      }
2277
1.53k
      add_next_index_str(return_value, zend_string_copy(zif->common.function_name));
2278
1.53k
    }
2279
4.12k
  } ZEND_HASH_FOREACH_END();
2280
2281
3
  if (!array) {
2282
0
    RETURN_FALSE;
2283
0
  }
2284
3
}
2285
/* }}} */