Coverage Report

Created: 2025-07-23 06:33

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