Coverage Report

Created: 2026-07-25 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright © Zend Technologies Ltd., a subsidiary company of          |
6
   |     Perforce Software, Inc., and Contributors.                       |
7
   +----------------------------------------------------------------------+
8
   | This source file is subject to the Modified BSD License that is      |
9
   | bundled with this package in the file LICENSE, and is available      |
10
   | through the World Wide Web at <https://www.php.net/license/>.        |
11
   |                                                                      |
12
   | SPDX-License-Identifier: BSD-3-Clause                                |
13
   +----------------------------------------------------------------------+
14
   | Authors: Andi Gutmans <andi@php.net>                                 |
15
   |          Zeev Suraski <zeev@php.net>                                 |
16
   +----------------------------------------------------------------------+
17
*/
18
19
#include "zend.h"
20
#include "zend_extensions.h"
21
#include "zend_modules.h"
22
#include "zend_constants.h"
23
#include "zend_list.h"
24
#include "zend_API.h"
25
#include "zend_exceptions.h"
26
#include "zend_builtin_functions.h"
27
#include "zend_ini.h"
28
#include "zend_vm.h"
29
#include "zend_dtrace.h"
30
#include "zend_virtual_cwd.h"
31
#include "zend_smart_str.h"
32
#include "zend_smart_string.h"
33
#include "zend_cpuinfo.h"
34
#include "zend_attributes.h"
35
#include "zend_observer.h"
36
#include "zend_fibers.h"
37
#include "zend_call_stack.h"
38
#include "zend_max_execution_timer.h"
39
#include "zend_hrtime.h"
40
#include "zend_enum.h"
41
#include "zend_closures.h"
42
#include "Optimizer/zend_optimizer.h"
43
#include "php.h"
44
#include "php_globals.h"
45
46
// FIXME: Breaks the declaration of the function below
47
#undef zenderror
48
49
static size_t global_map_ptr_last = 0;
50
static bool startup_done = false;
51
52
#ifdef ZTS
53
ZEND_API int compiler_globals_id;
54
ZEND_API int executor_globals_id;
55
ZEND_API size_t compiler_globals_offset;
56
ZEND_API size_t executor_globals_offset;
57
/* ts_allocate_tls_id takes a callback so each thread resolves its own block.
58
 * A plain &language_scanner_globals would capture only the registering thread's address. */
59
static void *language_scanner_globals_tls_addr(void) { return &language_scanner_globals; }
60
static HashTable *global_function_table = NULL;
61
static HashTable *global_class_table = NULL;
62
static HashTable *global_constants_table = NULL;
63
static HashTable *global_auto_globals_table = NULL;
64
static HashTable *global_persistent_list = NULL;
65
TSRMLS_MAIN_CACHE_DEFINE()
66
# define GLOBAL_FUNCTION_TABLE    global_function_table
67
# define GLOBAL_CLASS_TABLE     global_class_table
68
# define GLOBAL_CONSTANTS_TABLE   global_constants_table
69
# define GLOBAL_AUTO_GLOBALS_TABLE  global_auto_globals_table
70
#else
71
16
# define GLOBAL_FUNCTION_TABLE    CG(function_table)
72
16
# define GLOBAL_CLASS_TABLE     CG(class_table)
73
16
# define GLOBAL_AUTO_GLOBALS_TABLE  CG(auto_globals)
74
16
# define GLOBAL_CONSTANTS_TABLE   EG(zend_constants)
75
#endif
76
77
ZEND_API zend_utility_values zend_uv;
78
ZEND_API bool zend_dtrace_enabled;
79
80
/* version information */
81
static char *zend_version_info;
82
static uint32_t zend_version_info_length;
83
32
#define ZEND_CORE_VERSION_INFO  "Zend Engine v" ZEND_VERSION ", Copyright © Zend by Perforce\n"
84
53.3k
#define PRINT_ZVAL_INDENT 4
85
86
/* true multithread-shared globals */
87
ZEND_API zend_class_entry *zend_standard_class_def = NULL;
88
ZEND_API size_t (*zend_printf)(const char *format, ...);
89
ZEND_API zend_write_func_t zend_write;
90
ZEND_API FILE *(*zend_fopen)(zend_string *filename, zend_string **opened_path);
91
ZEND_API zend_result (*zend_stream_open_function)(zend_file_handle *handle);
92
ZEND_API void (*zend_ticks_function)(int ticks);
93
ZEND_API void (*zend_interrupt_function)(zend_execute_data *execute_data);
94
ZEND_API void (*zend_error_cb)(int type, zend_string *error_filename, const uint32_t error_lineno, zend_string *message);
95
void (*zend_printf_to_smart_string)(smart_string *buf, const char *format, va_list ap);
96
void (*zend_printf_to_smart_str)(smart_str *buf, const char *format, va_list ap);
97
ZEND_API char *(*zend_getenv)(const char *name, size_t name_len);
98
ZEND_API zend_string *(*zend_resolve_path)(zend_string *filename);
99
ZEND_API zend_result (*zend_post_startup_cb)(void) = NULL;
100
ZEND_API void (*zend_post_shutdown_cb)(void) = NULL;
101
ZEND_API void (*zend_accel_schedule_restart_hook)(int reason) = NULL;
102
ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_random_bytes)(void *bytes, size_t size, char *errstr, size_t errstr_size) = NULL;
103
ZEND_ATTRIBUTE_NONNULL ZEND_API void (*zend_random_bytes_insecure)(zend_random_bytes_insecure_state *state, void *bytes, size_t size) = NULL;
104
105
/* This callback must be signal handler safe! */
106
void (*zend_on_timeout)(int seconds);
107
108
static void (*zend_message_dispatcher_p)(zend_long message, const void *data);
109
static zval *(*zend_get_configuration_directive_p)(zend_string *name);
110
111
#if ZEND_RC_DEBUG
112
ZEND_API bool zend_rc_debug = 0;
113
#endif
114
115
static ZEND_INI_MH(OnUpdateErrorReporting) /* {{{ */
116
409
{
117
409
  if (!new_value) {
118
0
    EG(error_reporting) = E_ALL;
119
409
  } else {
120
409
    EG(error_reporting) = atoi(ZSTR_VAL(new_value));
121
409
  }
122
409
  return SUCCESS;
123
409
}
124
/* }}} */
125
126
static ZEND_INI_MH(OnUpdateGCEnabled) /* {{{ */
127
304
{
128
304
  bool val;
129
130
304
  val = zend_ini_parse_bool(new_value);
131
304
  gc_enable(val);
132
133
304
  return SUCCESS;
134
304
}
135
/* }}} */
136
137
static ZEND_INI_DISP(zend_gc_enabled_displayer_cb) /* {{{ */
138
8
{
139
8
  if (gc_enabled()) {
140
8
    ZEND_PUTS("On");
141
8
  } else {
142
0
    ZEND_PUTS("Off");
143
0
  }
144
8
}
145
/* }}} */
146
147
148
static ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */
149
16
{
150
16
  if (!CG(multibyte)) {
151
16
    return FAILURE;
152
16
  }
153
0
  if (!zend_multibyte_get_functions()) {
154
0
    return SUCCESS;
155
0
  }
156
0
  return zend_multibyte_set_script_encoding_by_string(new_value ? ZSTR_VAL(new_value) : NULL, new_value ? ZSTR_LEN(new_value) : 0);
157
0
}
158
/* }}} */
159
160
static ZEND_INI_MH(OnUpdateAssertions) /* {{{ */
161
132
{
162
132
  zend_long *p = ZEND_INI_GET_ADDR();
163
164
132
  zend_long val = zend_ini_parse_quantity_warn(new_value, entry->name);
165
166
132
  if (stage != ZEND_INI_STAGE_STARTUP &&
167
116
      stage != ZEND_INI_STAGE_SHUTDOWN &&
168
116
      *p != val &&
169
83
      (*p < 0 || val < 0)) {
170
5
    zend_error(E_WARNING, "zend.assertions may be completely enabled or disabled only in php.ini");
171
5
    return FAILURE;
172
5
  }
173
174
127
  *p = val;
175
127
  return SUCCESS;
176
132
}
177
/* }}} */
178
179
static ZEND_INI_MH(OnSetExceptionStringParamMaxLen) /* {{{ */
180
131
{
181
131
  zend_long i = ZEND_ATOL(ZSTR_VAL(new_value));
182
131
  if (i >= 0 && i <= 1000000) {
183
83
    EG(exception_string_param_max_len) = i;
184
83
    return SUCCESS;
185
83
  } else {
186
48
    return FAILURE;
187
48
  }
188
131
}
189
/* }}} */
190
191
#ifdef ZEND_CHECK_STACK_LIMIT
192
static ZEND_INI_MH(OnUpdateMaxAllowedStackSize) /* {{{ */
193
16
{
194
16
  zend_long size = zend_ini_parse_quantity_warn(new_value, entry->name);
195
196
16
  if (size < ZEND_MAX_ALLOWED_STACK_SIZE_UNCHECKED) {
197
0
    zend_error(E_WARNING, "Invalid \"%s\" setting. Value must be >= %d, but got " ZEND_LONG_FMT,
198
0
      ZSTR_VAL(entry->name), ZEND_MAX_ALLOWED_STACK_SIZE_UNCHECKED, size);
199
0
    return FAILURE;
200
0
  }
201
202
16
  EG(max_allowed_stack_size) = size;
203
204
16
  return SUCCESS;
205
16
}
206
/* }}} */
207
208
static ZEND_INI_MH(OnUpdateReservedStackSize) /* {{{ */
209
16
{
210
16
  zend_ulong size = zend_ini_parse_uquantity_warn(new_value, entry->name);
211
212
  /* Min value accounts for alloca, PCRE2 START_FRAMES_SIZE, and some buffer
213
   * for normal function calls.
214
   * We could reduce this on systems without alloca if we also add stack size
215
   * checks before pcre2_match(). */
216
#ifdef ZEND_ALLOCA_MAX_SIZE
217
  zend_ulong min = ZEND_ALLOCA_MAX_SIZE + 16*1024;
218
#else
219
16
  zend_ulong min = 32*1024;
220
16
#endif
221
222
#if defined(__SANITIZE_ADDRESS__) || __has_feature(memory_sanitizer)
223
  /* AddressSanitizer and MemorySanitizer use more stack due to
224
   * instrumentation */
225
  min *= 10;
226
#endif
227
228
16
  if (size == 0) {
229
16
    size = min;
230
16
  } else if (size < min) {
231
0
    zend_error(E_WARNING, "Invalid \"%s\" setting. Value must be >= " ZEND_ULONG_FMT ", but got " ZEND_ULONG_FMT "\n",
232
0
      ZSTR_VAL(entry->name), min, size);
233
0
    return FAILURE;
234
0
  }
235
236
16
  EG(reserved_stack_size) = size;
237
238
16
  return SUCCESS;
239
16
}
240
/* }}} */
241
#endif /* ZEND_CHECK_STACK_LIMIT */
242
243
static ZEND_INI_MH(OnUpdateFiberStackSize) /* {{{ */
244
50
{
245
50
  if (new_value) {
246
17
    zend_long tmp = zend_ini_parse_quantity_warn(new_value, entry->name);
247
17
    if (tmp < 0) {
248
9
      zend_error(E_WARNING, "fiber.stack_size must be a positive number");
249
9
      return FAILURE;
250
9
    }
251
8
    EG(fiber_stack_size) = tmp;
252
33
  } else {
253
33
    EG(fiber_stack_size) = ZEND_FIBER_DEFAULT_C_STACK_SIZE;
254
33
  }
255
41
  return SUCCESS;
256
50
}
257
/* }}} */
258
259
#if ZEND_DEBUG
260
# define SIGNAL_CHECK_DEFAULT "1"
261
#else
262
# define SIGNAL_CHECK_DEFAULT "0"
263
#endif
264
265
ZEND_INI_BEGIN()
266
  ZEND_INI_ENTRY("error_reporting",       NULL,   ZEND_INI_ALL,   OnUpdateErrorReporting)
267
  STD_ZEND_INI_BOOLEAN("fatal_error_backtraces",      "1",  ZEND_INI_ALL,       OnUpdateBool, fatal_error_backtrace_on,      zend_executor_globals, executor_globals)
268
  STD_ZEND_INI_ENTRY("zend.assertions",       "1",    ZEND_INI_ALL,       OnUpdateAssertions,           assertions,   zend_executor_globals,  executor_globals)
269
  ZEND_INI_ENTRY3_EX("zend.enable_gc",        "1",  ZEND_INI_ALL,   OnUpdateGCEnabled, NULL, NULL, NULL, zend_gc_enabled_displayer_cb)
270
  STD_ZEND_INI_BOOLEAN("zend.multibyte", "0", ZEND_INI_PERDIR, OnUpdateBool, multibyte,      zend_compiler_globals, compiler_globals)
271
  ZEND_INI_ENTRY("zend.script_encoding",      NULL,   ZEND_INI_ALL,   OnUpdateScriptEncoding)
272
  STD_ZEND_INI_BOOLEAN("zend.detect_unicode",     "1",  ZEND_INI_ALL,   OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals)
273
#ifdef ZEND_SIGNALS
274
  STD_ZEND_INI_BOOLEAN("zend.signal_check", SIGNAL_CHECK_DEFAULT, ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals)
275
#endif
276
  STD_ZEND_INI_BOOLEAN("zend.exception_ignore_args",  "0",  ZEND_INI_ALL,   OnUpdateBool, exception_ignore_args, zend_executor_globals, executor_globals)
277
  STD_ZEND_INI_ENTRY("zend.exception_string_param_max_len", "15", ZEND_INI_ALL, OnSetExceptionStringParamMaxLen,  exception_string_param_max_len,   zend_executor_globals,  executor_globals)
278
  STD_ZEND_INI_ENTRY("fiber.stack_size",    NULL,     ZEND_INI_ALL,   OnUpdateFiberStackSize,   fiber_stack_size, zend_executor_globals,    executor_globals)
279
#ifdef ZEND_CHECK_STACK_LIMIT
280
  /* The maximum allowed call stack size. 0: auto detect, -1: no limit. For fibers, this is fiber.stack_size. */
281
  STD_ZEND_INI_ENTRY("zend.max_allowed_stack_size", "0",  ZEND_INI_SYSTEM,  OnUpdateMaxAllowedStackSize,  max_allowed_stack_size,   zend_executor_globals,  executor_globals)
282
  /* Subtracted from the max allowed stack size, as a buffer, when checking for overflow. 0: auto detect. */
283
  STD_ZEND_INI_ENTRY("zend.reserved_stack_size",  "0",  ZEND_INI_SYSTEM,  OnUpdateReservedStackSize,  reserved_stack_size,    zend_executor_globals,  executor_globals)
284
#endif
285
286
ZEND_INI_END()
287
288
ZEND_API size_t zend_vspprintf(char **pbuf, size_t max_len, const char *format, va_list ap) /* {{{ */
289
17.5M
{
290
17.5M
  smart_string buf = {0};
291
292
  /* since there are places where (v)spprintf called without checking for null,
293
     a bit of defensive coding here */
294
17.5M
  if (!pbuf) {
295
0
    return 0;
296
0
  }
297
298
17.5M
  zend_printf_to_smart_string(&buf, format, ap);
299
300
17.5M
  if (max_len && buf.len > max_len) {
301
0
    buf.len = max_len;
302
0
  }
303
304
17.5M
  smart_string_0(&buf);
305
306
17.5M
  if (buf.c) {
307
17.5M
    *pbuf = buf.c;
308
17.5M
    return buf.len;
309
17.5M
  } else {
310
0
    *pbuf = estrndup("", 0);
311
0
    return 0;
312
0
  }
313
17.5M
}
314
/* }}} */
315
316
ZEND_API size_t zend_spprintf(char **message, size_t max_len, const char *format, ...) /* {{{ */
317
30.6k
{
318
30.6k
  va_list arg;
319
30.6k
  size_t len;
320
321
30.6k
  va_start(arg, format);
322
30.6k
  len = zend_vspprintf(message, max_len, format, arg);
323
30.6k
  va_end(arg);
324
30.6k
  return len;
325
30.6k
}
326
/* }}} */
327
328
ZEND_API size_t zend_spprintf_unchecked(char **message, size_t max_len, const char *format, ...) /* {{{ */
329
0
{
330
0
  va_list arg;
331
0
  size_t len;
332
333
0
  va_start(arg, format);
334
0
  len = zend_vspprintf(message, max_len, format, arg);
335
0
  va_end(arg);
336
0
  return len;
337
0
}
338
/* }}} */
339
340
ZEND_API zend_string *zend_vstrpprintf(size_t max_len, const char *format, va_list ap) /* {{{ */
341
6.21M
{
342
6.21M
  smart_str buf = {0};
343
344
6.21M
  zend_printf_to_smart_str(&buf, format, ap);
345
346
6.21M
  if (!buf.s) {
347
0
    return ZSTR_EMPTY_ALLOC();
348
0
  }
349
350
6.21M
  if (max_len && ZSTR_LEN(buf.s) > max_len) {
351
0
    ZSTR_LEN(buf.s) = max_len;
352
0
  }
353
354
6.21M
  return smart_str_extract(&buf);
355
6.21M
}
356
/* }}} */
357
358
ZEND_API zend_string *zend_strpprintf(size_t max_len, const char *format, ...) /* {{{ */
359
421k
{
360
421k
  va_list arg;
361
421k
  zend_string *str;
362
363
421k
  va_start(arg, format);
364
421k
  str = zend_vstrpprintf(max_len, format, arg);
365
421k
  va_end(arg);
366
421k
  return str;
367
421k
}
368
/* }}} */
369
370
ZEND_API zend_string *zend_strpprintf_unchecked(size_t max_len, const char *format, ...) /* {{{ */
371
2.42M
{
372
2.42M
  va_list arg;
373
2.42M
  zend_string *str;
374
375
2.42M
  va_start(arg, format);
376
2.42M
  str = zend_vstrpprintf(max_len, format, arg);
377
2.42M
  va_end(arg);
378
2.42M
  return str;
379
2.42M
}
380
/* }}} */
381
382
static void zend_print_zval_r_to_buf(smart_str *buf, zval *expr, int indent);
383
384
static void print_hash(smart_str *buf, HashTable *ht, int indent, bool is_object) /* {{{ */
385
12.9k
{
386
12.9k
  zval *tmp;
387
12.9k
  zend_string *string_key;
388
12.9k
  zend_ulong num_key;
389
12.9k
  int i;
390
391
139k
  for (i = 0; i < indent; i++) {
392
126k
    smart_str_appendc(buf, ' ');
393
126k
  }
394
12.9k
  smart_str_appends(buf, "(\n");
395
12.9k
  indent += PRINT_ZVAL_INDENT;
396
68.1k
  ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) {
397
269k
    for (i = 0; i < indent; i++) {
398
242k
      smart_str_appendc(buf, ' ');
399
242k
    }
400
68.1k
    smart_str_appendc(buf, '[');
401
68.1k
    if (string_key) {
402
7.25k
      if (is_object) {
403
6.03k
        const char *prop_name, *class_name;
404
6.03k
        size_t prop_len;
405
6.03k
        int mangled = zend_unmangle_property_name_ex(string_key, &class_name, &prop_name, &prop_len);
406
407
6.03k
        smart_str_appendl(buf, prop_name, prop_len);
408
6.03k
        if (class_name && mangled == SUCCESS) {
409
0
          if (class_name[0] == '*') {
410
0
            smart_str_appends(buf, ":protected");
411
0
          } else {
412
0
            smart_str_appends(buf, ":");
413
0
            smart_str_appends(buf, class_name);
414
0
            smart_str_appends(buf, ":private");
415
0
          }
416
0
        }
417
6.03k
      } else {
418
1.21k
        smart_str_append(buf, string_key);
419
1.21k
      }
420
20.1k
    } else {
421
20.1k
      smart_str_append_long(buf, num_key);
422
20.1k
    }
423
68.1k
    smart_str_appends(buf, "] => ");
424
68.1k
    zend_print_zval_r_to_buf(buf, tmp, indent+PRINT_ZVAL_INDENT);
425
68.1k
    smart_str_appends(buf, "\n");
426
68.1k
  } ZEND_HASH_FOREACH_END();
427
12.9k
  indent -= PRINT_ZVAL_INDENT;
428
139k
  for (i = 0; i < indent; i++) {
429
126k
    smart_str_appendc(buf, ' ');
430
126k
  }
431
12.9k
  smart_str_appends(buf, ")\n");
432
12.9k
}
433
/* }}} */
434
435
static void print_flat_hash(smart_str *buf, HashTable *ht) /* {{{ */
436
0
{
437
0
  zval *tmp;
438
0
  zend_string *string_key;
439
0
  zend_ulong num_key;
440
0
  int i = 0;
441
442
0
  ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) {
443
0
    if (i++ > 0) {
444
0
      smart_str_appendc(buf, ',');
445
0
    }
446
0
    smart_str_appendc(buf, '[');
447
0
    if (string_key) {
448
0
      smart_str_append(buf, string_key);
449
0
    } else {
450
0
      smart_str_append_unsigned(buf, num_key);
451
0
    }
452
0
    smart_str_appends(buf, "] => ");
453
0
    zend_print_flat_zval_r_to_buf(buf, tmp);
454
0
  } ZEND_HASH_FOREACH_END();
455
0
}
456
/* }}} */
457
458
ZEND_API bool zend_make_printable_zval(zval *expr, zval *expr_copy) /* {{{ */
459
0
{
460
0
  if (Z_TYPE_P(expr) == IS_STRING) {
461
0
    return 0;
462
0
  } else {
463
0
    ZVAL_STR(expr_copy, zval_get_string_func(expr));
464
0
    return 1;
465
0
  }
466
0
}
467
/* }}} */
468
469
ZEND_API size_t zend_print_zval(zval *expr, int indent) /* {{{ */
470
0
{
471
0
  zend_string *tmp_str;
472
0
  zend_string *str = zval_get_tmp_string(expr, &tmp_str);
473
0
  size_t len = ZSTR_LEN(str);
474
475
0
  if (len != 0) {
476
0
    zend_write(ZSTR_VAL(str), len);
477
0
  }
478
479
0
  zend_tmp_string_release(tmp_str);
480
0
  return len;
481
0
}
482
/* }}} */
483
484
void zend_print_flat_zval_r_to_buf(smart_str *buf, zval *expr) /* {{{ */
485
0
{
486
0
  switch (Z_TYPE_P(expr)) {
487
0
    case IS_ARRAY:
488
0
      smart_str_appends(buf, "Array (");
489
0
      if (!(GC_FLAGS(Z_ARRVAL_P(expr)) & GC_IMMUTABLE)) {
490
0
        if (GC_IS_RECURSIVE(Z_ARRVAL_P(expr))) {
491
0
          smart_str_appends(buf, " *RECURSION*");
492
0
          return;
493
0
        }
494
0
        GC_PROTECT_RECURSION(Z_ARRVAL_P(expr));
495
0
      }
496
0
      print_flat_hash(buf, Z_ARRVAL_P(expr));
497
0
      smart_str_appendc(buf, ')');
498
0
      GC_TRY_UNPROTECT_RECURSION(Z_ARRVAL_P(expr));
499
0
      break;
500
0
    case IS_OBJECT:
501
0
    {
502
0
      HashTable *properties;
503
0
      zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr));
504
0
      smart_str_append(buf, class_name);
505
0
      smart_str_appends(buf, " Object (");
506
0
      zend_string_release_ex(class_name, 0);
507
508
0
      if (GC_IS_RECURSIVE(Z_COUNTED_P(expr))) {
509
0
        smart_str_appends(buf, " *RECURSION*");
510
0
        return;
511
0
      }
512
513
0
      properties = Z_OBJPROP_P(expr);
514
0
      if (properties) {
515
0
        GC_PROTECT_RECURSION(Z_OBJ_P(expr));
516
0
        print_flat_hash(buf, properties);
517
0
        GC_UNPROTECT_RECURSION(Z_OBJ_P(expr));
518
0
      }
519
0
      smart_str_appendc(buf, ')');
520
0
      break;
521
0
    }
522
0
    case IS_REFERENCE:
523
0
      zend_print_flat_zval_r_to_buf(buf, Z_REFVAL_P(expr));
524
0
      break;
525
0
    case IS_STRING:
526
0
      smart_str_append(buf, Z_STR_P(expr));
527
0
      break;
528
0
    default:
529
0
    {
530
0
      zend_string *str = zval_get_string_func(expr);
531
0
      smart_str_append(buf, str);
532
0
      zend_string_release_ex(str, 0);
533
0
      break;
534
0
    }
535
0
  }
536
0
}
537
/* }}} */
538
539
ZEND_API void zend_print_flat_zval_r(zval *expr)
540
0
{
541
0
  smart_str buf = {0};
542
0
  zend_print_flat_zval_r_to_buf(&buf, expr);
543
0
  smart_str_0(&buf);
544
0
  zend_write(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s));
545
0
  smart_str_free(&buf);
546
0
}
547
548
static void zend_print_zval_r_to_buf(smart_str *buf, zval *expr, int indent) /* {{{ */
549
30.4k
{
550
30.4k
  switch (Z_TYPE_P(expr)) {
551
6.84k
    case IS_ARRAY:
552
6.84k
      smart_str_appends(buf, "Array\n");
553
6.84k
      if (!(GC_FLAGS(Z_ARRVAL_P(expr)) & GC_IMMUTABLE)) {
554
2.11k
        if (GC_IS_RECURSIVE(Z_ARRVAL_P(expr))) {
555
11
          smart_str_appends(buf, " *RECURSION*");
556
11
          return;
557
11
        }
558
2.09k
        GC_PROTECT_RECURSION(Z_ARRVAL_P(expr));
559
2.09k
      }
560
6.83k
      print_hash(buf, Z_ARRVAL_P(expr), indent, false);
561
6.83k
      GC_TRY_UNPROTECT_RECURSION(Z_ARRVAL_P(expr));
562
6.83k
      break;
563
6.16k
    case IS_OBJECT:
564
6.16k
      {
565
6.16k
        HashTable *properties;
566
567
6.16k
        zend_object *zobj = Z_OBJ_P(expr);
568
6.16k
        uint32_t *guard = zend_get_recursion_guard(zobj);
569
6.16k
        zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(zobj);
570
        /* cut off on NULL byte ... class@anonymous */
571
6.16k
        smart_str_appends(buf, ZSTR_VAL(class_name));
572
6.16k
        zend_string_release_ex(class_name, 0);
573
574
6.16k
        if (!(zobj->ce->ce_flags & ZEND_ACC_ENUM)) {
575
6.13k
          smart_str_appends(buf, " Object\n");
576
6.13k
        } else {
577
25
          smart_str_appends(buf, " Enum");
578
25
          if (zobj->ce->enum_backing_type != IS_UNDEF) {
579
15
            smart_str_appendc(buf, ':');
580
15
            smart_str_appends(buf, zend_get_type_by_const(zobj->ce->enum_backing_type));
581
15
          }
582
25
          smart_str_appendc(buf, '\n');
583
25
        }
584
585
6.16k
        if (ZEND_GUARD_OR_GC_IS_RECURSIVE(guard, DEBUG, zobj)) {
586
0
          smart_str_appends(buf, " *RECURSION*");
587
0
          return;
588
0
        }
589
590
6.16k
        if ((properties = zend_get_properties_for(expr, ZEND_PROP_PURPOSE_DEBUG)) == NULL) {
591
86
          print_hash(buf, (HashTable*) &zend_empty_array, indent, true);
592
86
          break;
593
86
        }
594
595
6.07k
        ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, DEBUG, zobj);
596
6.07k
        print_hash(buf, properties, indent, true);
597
6.07k
        ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, DEBUG, zobj);
598
599
6.07k
        zend_release_properties(properties);
600
6.07k
        break;
601
6.16k
      }
602
2.47k
    case IS_LONG:
603
2.47k
      smart_str_append_long(buf, Z_LVAL_P(expr));
604
2.47k
      break;
605
114
    case IS_REFERENCE:
606
114
      zend_print_zval_r_to_buf(buf, Z_REFVAL_P(expr), indent);
607
114
      break;
608
1.62k
    case IS_STRING:
609
1.62k
      smart_str_append(buf, Z_STR_P(expr));
610
1.62k
      break;
611
13.1k
    default:
612
13.1k
      {
613
13.1k
        zend_string *str = zval_get_string_func(expr);
614
13.1k
        smart_str_append(buf, str);
615
13.1k
        zend_string_release_ex(str, 0);
616
13.1k
      }
617
13.1k
      break;
618
30.4k
  }
619
30.4k
}
620
/* }}} */
621
622
ZEND_API zend_string *zend_print_zval_r_to_str(zval *expr, int indent) /* {{{ */
623
2.90k
{
624
2.90k
  smart_str buf = {0};
625
2.90k
  zend_print_zval_r_to_buf(&buf, expr, indent);
626
2.90k
  smart_str_0(&buf);
627
2.90k
  return buf.s;
628
2.90k
}
629
/* }}} */
630
631
ZEND_API void zend_print_zval_r(zval *expr, int indent) /* {{{ */
632
2.86k
{
633
2.86k
  zend_string *str = zend_print_zval_r_to_str(expr, indent);
634
2.86k
  zend_write(ZSTR_VAL(str), ZSTR_LEN(str));
635
2.86k
  zend_string_release_ex(str, 0);
636
2.86k
}
637
/* }}} */
638
639
static FILE *zend_fopen_wrapper(zend_string *filename, zend_string **opened_path) /* {{{ */
640
0
{
641
0
  if (opened_path) {
642
0
    *opened_path = zend_string_copy(filename);
643
0
  }
644
0
  return fopen(ZSTR_VAL(filename), "rb");
645
0
}
646
/* }}} */
647
648
#ifdef ZTS
649
static bool short_tags_default = true;
650
static uint32_t compiler_options_default = ZEND_COMPILE_DEFAULT;
651
#else
652
16
# define short_tags_default     1
653
16
# define compiler_options_default ZEND_COMPILE_DEFAULT
654
#endif
655
656
static void zend_set_default_compile_time_values(void) /* {{{ */
657
16
{
658
  /* default compile-time values */
659
16
  CG(short_tags) = short_tags_default;
660
16
  CG(compiler_options) = compiler_options_default;
661
662
16
  CG(rtd_key_counter) = 0;
663
16
}
664
/* }}} */
665
666
#ifdef ZEND_WIN32
667
static void zend_get_windows_version_info(OSVERSIONINFOEX *osvi) /* {{{ */
668
{
669
  ZeroMemory(osvi, sizeof(OSVERSIONINFOEX));
670
  osvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
671
  if(!GetVersionEx((OSVERSIONINFO *) osvi)) {
672
    ZEND_UNREACHABLE(); /* Should not happen as sizeof is used. */
673
  }
674
}
675
/* }}} */
676
#endif
677
678
static void zend_init_exception_op(void) /* {{{ */
679
16
{
680
16
  memset(EG(exception_op), 0, sizeof(EG(exception_op)));
681
16
  EG(exception_op)[0].opcode = ZEND_HANDLE_EXCEPTION;
682
16
  ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op));
683
16
  EG(exception_op)[1].opcode = ZEND_HANDLE_EXCEPTION;
684
16
  ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+1);
685
16
  EG(exception_op)[2].opcode = ZEND_HANDLE_EXCEPTION;
686
16
  ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+2);
687
16
}
688
/* }}} */
689
690
static void zend_init_call_trampoline_op(void) /* {{{ */
691
16
{
692
16
  memset(&EG(call_trampoline_op), 0, sizeof(EG(call_trampoline_op)));
693
16
  EG(call_trampoline_op).opcode = ZEND_CALL_TRAMPOLINE;
694
16
  ZEND_VM_SET_OPCODE_HANDLER(&EG(call_trampoline_op));
695
16
}
696
/* }}} */
697
698
static void auto_global_dtor(zval *zv) /* {{{ */
699
0
{
700
0
  free(Z_PTR_P(zv));
701
0
}
702
/* }}} */
703
704
#ifdef ZTS
705
static void auto_global_copy_ctor(zval *zv) /* {{{ */
706
{
707
  zend_auto_global *old_ag = (zend_auto_global *) Z_PTR_P(zv);
708
  zend_auto_global *new_ag = pemalloc(sizeof(zend_auto_global), 1);
709
710
  new_ag->name = old_ag->name;
711
  new_ag->auto_global_callback = old_ag->auto_global_callback;
712
  new_ag->jit = old_ag->jit;
713
714
  Z_PTR_P(zv) = new_ag;
715
}
716
/* }}} */
717
718
static void compiler_globals_ctor(zend_compiler_globals *compiler_globals) /* {{{ */
719
{
720
  compiler_globals->compiled_filename = NULL;
721
  compiler_globals->zend_lineno = 0;
722
723
  compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
724
  zend_hash_init(compiler_globals->function_table, 1024, NULL, ZEND_FUNCTION_DTOR, 1);
725
  zend_hash_copy(compiler_globals->function_table, global_function_table, NULL);
726
  compiler_globals->copied_functions_count = zend_hash_num_elements(compiler_globals->function_table);
727
728
  compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
729
  zend_hash_init(compiler_globals->class_table, 64, NULL, ZEND_CLASS_DTOR, 1);
730
  zend_hash_copy(compiler_globals->class_table, global_class_table, zend_class_add_ref);
731
732
  zend_set_default_compile_time_values();
733
734
  compiler_globals->auto_globals = (HashTable *) malloc(sizeof(HashTable));
735
  zend_hash_init(compiler_globals->auto_globals, 8, NULL, auto_global_dtor, 1);
736
  zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, auto_global_copy_ctor);
737
738
  compiler_globals->script_encoding_list = NULL;
739
  compiler_globals->current_linking_class = NULL;
740
741
  /* Map region is going to be created and resized at run-time. */
742
  compiler_globals->map_ptr_real_base = NULL;
743
  compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
744
  compiler_globals->map_ptr_size = 0;
745
  compiler_globals->map_ptr_last = global_map_ptr_last;
746
  compiler_globals->internal_run_time_cache = NULL;
747
  if (compiler_globals->map_ptr_last || zend_map_ptr_static_size) {
748
    /* Allocate map_ptr table */
749
    compiler_globals->map_ptr_size = ZEND_MM_ALIGNED_SIZE_EX(compiler_globals->map_ptr_last, 4096);
750
    void *base = pemalloc((zend_map_ptr_static_size + compiler_globals->map_ptr_size) * sizeof(void*), 1);
751
    compiler_globals->map_ptr_real_base = base;
752
    compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(base);
753
    memset(base, 0, (zend_map_ptr_static_size + compiler_globals->map_ptr_last) * sizeof(void*));
754
  }
755
  zend_init_internal_run_time_cache();
756
}
757
/* }}} */
758
759
static void compiler_globals_dtor(zend_compiler_globals *compiler_globals) /* {{{ */
760
{
761
  if (compiler_globals->function_table != GLOBAL_FUNCTION_TABLE) {
762
    uint32_t n = compiler_globals->copied_functions_count;
763
764
      /* Prevent destruction of functions copied from the main process context */
765
    if (zend_hash_num_elements(compiler_globals->function_table) <= n) {
766
      compiler_globals->function_table->nNumUsed = 0;
767
    } else {
768
      Bucket *p = compiler_globals->function_table->arData;
769
770
      compiler_globals->function_table->nNumOfElements -= n;
771
      while (n != 0) {
772
        ZVAL_UNDEF(&p->val);
773
        p++;
774
        n--;
775
      }
776
    }
777
    zend_hash_destroy(compiler_globals->function_table);
778
    free(compiler_globals->function_table);
779
  }
780
  if (compiler_globals->class_table != GLOBAL_CLASS_TABLE) {
781
    /* Child classes may reuse structures from parent classes, so destroy in reverse order. */
782
    zend_hash_graceful_reverse_destroy(compiler_globals->class_table);
783
    free(compiler_globals->class_table);
784
  }
785
  if (compiler_globals->auto_globals != GLOBAL_AUTO_GLOBALS_TABLE) {
786
    zend_hash_destroy(compiler_globals->auto_globals);
787
    free(compiler_globals->auto_globals);
788
  }
789
  if (compiler_globals->script_encoding_list) {
790
    pefree((char*)compiler_globals->script_encoding_list, 1);
791
  }
792
  if (compiler_globals->map_ptr_real_base) {
793
    free(compiler_globals->map_ptr_real_base);
794
    compiler_globals->map_ptr_real_base = NULL;
795
    compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
796
    compiler_globals->map_ptr_size = 0;
797
  }
798
  if (compiler_globals->internal_run_time_cache) {
799
    pefree(compiler_globals->internal_run_time_cache, 1);
800
    compiler_globals->internal_run_time_cache = NULL;
801
  }
802
}
803
/* }}} */
804
805
static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{{ */
806
{
807
  zend_startup_constants();
808
  zend_copy_constants(executor_globals->zend_constants, GLOBAL_CONSTANTS_TABLE);
809
  zend_init_rsrc_plist();
810
  zend_init_exception_op();
811
  zend_init_call_trampoline_op();
812
  memset(&executor_globals->trampoline, 0, sizeof(zend_op_array));
813
  executor_globals->capture_warnings_during_sccp = 0;
814
  executor_globals->user_error_handler_error_reporting = 0;
815
  ZVAL_UNDEF(&executor_globals->user_error_handler);
816
  ZVAL_UNDEF(&executor_globals->user_exception_handler);
817
  ZVAL_UNDEF(&executor_globals->last_fatal_error_backtrace);
818
  executor_globals->current_execute_data = NULL;
819
  executor_globals->current_module = NULL;
820
  executor_globals->exit_status = 0;
821
#if XPFPA_HAVE_CW
822
  executor_globals->saved_fpu_cw = 0;
823
#endif
824
  executor_globals->saved_fpu_cw_ptr = NULL;
825
  executor_globals->active = false;
826
  executor_globals->bailout = NULL;
827
  executor_globals->error_handling  = EH_NORMAL;
828
  executor_globals->exception_class = NULL;
829
  executor_globals->exception = NULL;
830
  executor_globals->objects_store.object_buckets = NULL;
831
  executor_globals->current_fiber_context = NULL;
832
  executor_globals->main_fiber_context = NULL;
833
  executor_globals->active_fiber = NULL;
834
#ifdef ZEND_WIN32
835
  zend_get_windows_version_info(&executor_globals->windows_version_info);
836
#endif
837
  executor_globals->flags = EG_FLAGS_INITIAL;
838
  executor_globals->record_errors = false;
839
  memset(&executor_globals->errors, 0, sizeof(executor_globals->errors));
840
  executor_globals->filename_override = NULL;
841
  executor_globals->lineno_override = -1;
842
#ifdef ZEND_CHECK_STACK_LIMIT
843
  executor_globals->stack_limit = (void*)0;
844
  executor_globals->stack_base = (void*)0;
845
#endif
846
#ifdef ZEND_MAX_EXECUTION_TIMERS
847
  executor_globals->pid = 0;
848
  executor_globals->oldact = (struct sigaction){0};
849
#endif
850
  memset(executor_globals->strtod_state.freelist, 0,
851
      sizeof(executor_globals->strtod_state.freelist));
852
  executor_globals->strtod_state.p5s = NULL;
853
  executor_globals->strtod_state.result = NULL;
854
}
855
/* }}} */
856
857
static void executor_globals_persistent_list_dtor(void *storage)
858
{
859
  zend_executor_globals *executor_globals = storage;
860
861
  if (&executor_globals->persistent_list != global_persistent_list) {
862
    zend_destroy_rsrc_list(&executor_globals->persistent_list);
863
  }
864
}
865
866
static void executor_globals_dtor(zend_executor_globals *executor_globals) /* {{{ */
867
{
868
  zend_ini_dtor(executor_globals->ini_directives);
869
870
  if (executor_globals->zend_constants != GLOBAL_CONSTANTS_TABLE) {
871
    zend_hash_destroy(executor_globals->zend_constants);
872
    free(executor_globals->zend_constants);
873
  }
874
}
875
/* }}} */
876
877
static void zend_new_thread_end_handler(THREAD_T thread_id) /* {{{ */
878
{
879
  zend_copy_ini_directives();
880
  zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP);
881
#ifdef ZEND_CHECK_STACK_LIMIT
882
  zend_call_stack_init();
883
#endif
884
  zend_max_execution_timer_init();
885
}
886
/* }}} */
887
#endif
888
889
#if defined(__FreeBSD__) || defined(__DragonFly__)
890
/* FreeBSD and DragonFly floating point precision fix */
891
#include <floatingpoint.h>
892
#endif
893
894
static void ini_scanner_globals_ctor(zend_ini_scanner_globals *scanner_globals_p) /* {{{ */
895
16
{
896
16
  memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
897
16
}
898
/* }}} */
899
900
static void php_scanner_globals_ctor(zend_php_scanner_globals *scanner_globals_p) /* {{{ */
901
16
{
902
16
  memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
903
16
}
904
/* }}} */
905
906
static void module_destructor_zval(zval *zv) /* {{{ */
907
0
{
908
0
  zend_module_entry *module = (zend_module_entry*)Z_PTR_P(zv);
909
0
  module_destructor(module);
910
0
}
911
/* }}} */
912
913
static bool php_auto_globals_create_globals(zend_string *name) /* {{{ */
914
65
{
915
  /* While we keep registering $GLOBALS as an auto-global, we do not create an
916
   * actual variable for it. Access to it handled specially by the compiler. */
917
65
  return false;
918
65
}
919
/* }}} */
920
921
void zend_startup(zend_utility_functions *utility_functions) /* {{{ */
922
16
{
923
#ifdef ZTS
924
  zend_compiler_globals *compiler_globals;
925
  zend_executor_globals *executor_globals;
926
  extern ZEND_API ts_rsrc_id ini_scanner_globals_id;
927
  extern ZEND_API ts_rsrc_id language_scanner_globals_id;
928
#else
929
16
  extern zend_ini_scanner_globals ini_scanner_globals;
930
16
  extern zend_php_scanner_globals language_scanner_globals;
931
16
#endif
932
933
16
  zend_cpu_startup();
934
935
#ifdef ZEND_WIN32
936
  php_win32_cp_set_by_id(65001);
937
#endif
938
939
  /* Set up early utility functions. zend_mm depends on
940
   * zend_random_bytes_insecure */
941
16
  zend_random_bytes = utility_functions->random_bytes_function;
942
16
  zend_random_bytes_insecure = utility_functions->random_bytes_insecure_function;
943
944
16
  start_memory_manager();
945
946
16
  virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */
947
948
#if defined(__FreeBSD__) || defined(__DragonFly__)
949
  /* FreeBSD and DragonFly floating point precision fix */
950
  fpsetmask(0);
951
#endif
952
953
16
  zend_startup_hrtime();
954
16
  zend_startup_extensions_mechanism();
955
956
  /* Set up utility functions and values */
957
16
  zend_error_cb = utility_functions->error_function;
958
16
  zend_printf = utility_functions->printf_function;
959
16
  zend_write = utility_functions->write_function;
960
16
  zend_fopen = utility_functions->fopen_function;
961
16
  if (!zend_fopen) {
962
0
    zend_fopen = zend_fopen_wrapper;
963
0
  }
964
16
  zend_stream_open_function = utility_functions->stream_open_function;
965
16
  zend_message_dispatcher_p = utility_functions->message_handler;
966
16
  zend_get_configuration_directive_p = utility_functions->get_configuration_directive;
967
16
  zend_ticks_function = utility_functions->ticks_function;
968
16
  zend_on_timeout = utility_functions->on_timeout;
969
16
  zend_printf_to_smart_string = utility_functions->printf_to_smart_string_function;
970
16
  zend_printf_to_smart_str = utility_functions->printf_to_smart_str_function;
971
16
  zend_getenv = utility_functions->getenv_function;
972
16
  zend_resolve_path = utility_functions->resolve_path_function;
973
974
16
  zend_interrupt_function = NULL;
975
976
#ifdef HAVE_DTRACE
977
/* build with dtrace support */
978
  {
979
    char *tmp = getenv("USE_ZEND_DTRACE");
980
981
    if (tmp && ZEND_ATOL(tmp)) {
982
      zend_dtrace_enabled = 1;
983
      zend_compile_file = dtrace_compile_file;
984
      zend_execute_ex = dtrace_execute_ex;
985
      zend_execute_internal = dtrace_execute_internal;
986
987
      zend_observer_error_register(dtrace_error_notify_cb);
988
    } else {
989
      zend_compile_file = compile_file;
990
      zend_execute_ex = execute_ex;
991
      zend_execute_internal = NULL;
992
    }
993
  }
994
#else
995
16
  zend_compile_file = compile_file;
996
16
  zend_execute_ex = execute_ex;
997
16
  zend_execute_internal = NULL;
998
16
#endif /* HAVE_DTRACE */
999
16
  zend_compile_string = compile_string;
1000
16
  zend_throw_exception_hook = NULL;
1001
1002
  /* Set up the default garbage collection implementation. */
1003
16
  gc_collect_cycles = zend_gc_collect_cycles;
1004
1005
16
  zend_vm_init();
1006
1007
  /* set up version */
1008
16
  zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
1009
16
  zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1;
1010
1011
16
  GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
1012
16
  GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
1013
16
  GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable));
1014
16
  GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable));
1015
1016
16
  zend_hash_init(GLOBAL_FUNCTION_TABLE, 1024, NULL, ZEND_FUNCTION_DTOR, 1);
1017
16
  zend_hash_init(GLOBAL_CLASS_TABLE, 64, NULL, ZEND_CLASS_DTOR, 1);
1018
16
  zend_hash_init(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, auto_global_dtor, 1);
1019
16
  zend_hash_init(GLOBAL_CONSTANTS_TABLE, 128, NULL, ZEND_CONSTANT_DTOR, 1);
1020
1021
16
  zend_hash_init(&module_registry, 32, NULL, module_destructor_zval, 1);
1022
16
  zend_init_rsrc_list_dtors();
1023
1024
#ifdef ZTS
1025
  ts_allocate_fast_id_at(&compiler_globals_id, &compiler_globals_offset, ZEND_CG_OFFSET, sizeof(zend_compiler_globals), (ts_allocate_ctor) compiler_globals_ctor, (ts_allocate_dtor) compiler_globals_dtor);
1026
  ts_allocate_fast_id_at(&executor_globals_id, &executor_globals_offset, ZEND_EG_OFFSET, sizeof(zend_executor_globals), (ts_allocate_ctor) executor_globals_ctor, (ts_allocate_dtor) executor_globals_dtor);
1027
  ts_allocate_tls_id(&language_scanner_globals_id, language_scanner_globals_tls_addr, sizeof(zend_php_scanner_globals), (ts_allocate_ctor) php_scanner_globals_ctor, NULL);
1028
  ZEND_ASSERT(compiler_globals_offset == ZEND_CG_OFFSET);
1029
  ZEND_ASSERT(executor_globals_offset == ZEND_EG_OFFSET);
1030
  ts_allocate_fast_id(&ini_scanner_globals_id, &ini_scanner_globals_offset, sizeof(zend_ini_scanner_globals), (ts_allocate_ctor) ini_scanner_globals_ctor, NULL);
1031
  compiler_globals = ts_resource(compiler_globals_id);
1032
  executor_globals = ts_resource(executor_globals_id);
1033
1034
  compiler_globals_dtor(compiler_globals);
1035
  compiler_globals->in_compilation = false;
1036
  compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
1037
  compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
1038
1039
  *compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE;
1040
  *compiler_globals->class_table = *GLOBAL_CLASS_TABLE;
1041
  compiler_globals->auto_globals = GLOBAL_AUTO_GLOBALS_TABLE;
1042
1043
  zend_hash_destroy(executor_globals->zend_constants);
1044
  *executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE;
1045
#else
1046
16
  ini_scanner_globals_ctor(&ini_scanner_globals);
1047
16
  php_scanner_globals_ctor(&language_scanner_globals);
1048
16
  zend_set_default_compile_time_values();
1049
#ifdef ZEND_WIN32
1050
  zend_get_windows_version_info(&EG(windows_version_info));
1051
#endif
1052
  /* Map region is going to be created and resized at run-time. */
1053
16
  CG(map_ptr_real_base) = NULL;
1054
16
  CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(NULL);
1055
16
  CG(map_ptr_size) = 0;
1056
16
  CG(map_ptr_last) = 0;
1057
16
#endif /* ZTS */
1058
16
  EG(error_reporting) = E_ALL & ~E_NOTICE;
1059
16
  EG(fatal_error_backtrace_on) = false;
1060
16
  ZVAL_UNDEF(&EG(last_fatal_error_backtrace));
1061
1062
16
  zend_interned_strings_init();
1063
16
  zend_object_handlers_startup();
1064
16
  zend_startup_builtin_functions();
1065
16
  zend_register_standard_constants();
1066
16
  zend_register_auto_global(zend_string_init_interned("GLOBALS", sizeof("GLOBALS") - 1, 1), 1, php_auto_globals_create_globals);
1067
1068
16
#ifndef ZTS
1069
16
  zend_init_rsrc_plist();
1070
16
  zend_init_exception_op();
1071
16
  zend_init_call_trampoline_op();
1072
16
#endif
1073
1074
16
  zend_ini_startup();
1075
1076
#ifdef ZEND_WIN32
1077
  /* Uses INI settings, so needs to be run after it. */
1078
  php_win32_cp_setup();
1079
#endif
1080
1081
16
  zend_optimizer_startup();
1082
1083
#ifdef ZTS
1084
  tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
1085
  tsrm_set_shutdown_handler(zend_interned_strings_dtor);
1086
#endif
1087
1088
16
    zend_enum_startup();
1089
16
}
1090
/* }}} */
1091
1092
void zend_register_standard_ini_entries(void) /* {{{ */
1093
16
{
1094
16
  zend_register_ini_entries_ex(ini_entries, 0, MODULE_PERSISTENT);
1095
16
}
1096
/* }}} */
1097
1098
1099
/* Unlink the global (r/o) copies of the class, function and constant tables,
1100
 * and use a fresh r/w copy for the startup thread
1101
 */
1102
zend_result zend_post_startup(void) /* {{{ */
1103
16
{
1104
#ifdef ZTS
1105
  zend_encoding **script_encoding_list;
1106
1107
  zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id);
1108
  zend_executor_globals *executor_globals = ts_resource(executor_globals_id);
1109
#endif
1110
1111
16
  startup_done = true;
1112
1113
16
  if (zend_post_startup_cb) {
1114
16
    zend_result (*cb)(void) = zend_post_startup_cb;
1115
1116
16
    zend_post_startup_cb = NULL;
1117
16
    if (cb() != SUCCESS) {
1118
0
      return FAILURE;
1119
0
    }
1120
16
  }
1121
1122
#ifdef ZTS
1123
  *GLOBAL_FUNCTION_TABLE = *compiler_globals->function_table;
1124
  *GLOBAL_CLASS_TABLE = *compiler_globals->class_table;
1125
  *GLOBAL_CONSTANTS_TABLE = *executor_globals->zend_constants;
1126
  global_map_ptr_last = compiler_globals->map_ptr_last;
1127
1128
  short_tags_default = CG(short_tags);
1129
  compiler_options_default = CG(compiler_options);
1130
1131
  zend_destroy_rsrc_list(&EG(persistent_list));
1132
  free(compiler_globals->function_table);
1133
  compiler_globals->function_table = NULL;
1134
  free(compiler_globals->class_table);
1135
  compiler_globals->class_table = NULL;
1136
  if (compiler_globals->map_ptr_real_base) {
1137
    free(compiler_globals->map_ptr_real_base);
1138
  }
1139
  compiler_globals->map_ptr_real_base = NULL;
1140
  compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
1141
  if (compiler_globals->internal_run_time_cache) {
1142
    pefree(compiler_globals->internal_run_time_cache, 1);
1143
  }
1144
  compiler_globals->internal_run_time_cache = NULL;
1145
  if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
1146
    compiler_globals_ctor(compiler_globals);
1147
    compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list;
1148
  } else {
1149
    compiler_globals_ctor(compiler_globals);
1150
  }
1151
  free(EG(zend_constants));
1152
  EG(zend_constants) = NULL;
1153
1154
  executor_globals_ctor(executor_globals);
1155
  global_persistent_list = &EG(persistent_list);
1156
  zend_copy_ini_directives();
1157
#else
1158
16
  global_map_ptr_last = CG(map_ptr_last);
1159
16
#endif
1160
1161
16
#ifdef ZEND_CHECK_STACK_LIMIT
1162
16
  zend_call_stack_init();
1163
16
#endif
1164
16
  gc_init();
1165
1166
16
  return SUCCESS;
1167
16
}
1168
/* }}} */
1169
1170
void zend_shutdown(void) /* {{{ */
1171
0
{
1172
0
  zend_vm_dtor();
1173
1174
0
  zend_destroy_rsrc_list(&EG(persistent_list));
1175
#ifdef ZTS
1176
  ts_apply_for_id(executor_globals_id, executor_globals_persistent_list_dtor);
1177
#endif
1178
0
  zend_destroy_modules();
1179
1180
0
  virtual_cwd_deactivate();
1181
0
  virtual_cwd_shutdown();
1182
1183
0
  zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
1184
  /* Child classes may reuse structures from parent classes, so destroy in reverse order. */
1185
0
  zend_hash_graceful_reverse_destroy(GLOBAL_CLASS_TABLE);
1186
1187
0
  zend_flf_capacity = 0;
1188
0
  zend_flf_count = 0;
1189
0
  free(zend_flf_functions);
1190
0
  free(zend_flf_handlers);
1191
0
  zend_flf_functions = NULL;
1192
0
  zend_flf_handlers = NULL;
1193
1194
0
  zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE);
1195
0
  free(GLOBAL_AUTO_GLOBALS_TABLE);
1196
1197
0
  zend_shutdown_extensions();
1198
0
  free(zend_version_info);
1199
1200
0
  free(GLOBAL_FUNCTION_TABLE);
1201
0
  free(GLOBAL_CLASS_TABLE);
1202
1203
0
  zend_hash_destroy(GLOBAL_CONSTANTS_TABLE);
1204
0
  free(GLOBAL_CONSTANTS_TABLE);
1205
0
  zend_shutdown_strtod();
1206
0
  zend_attributes_shutdown();
1207
1208
#ifdef ZTS
1209
  GLOBAL_FUNCTION_TABLE = NULL;
1210
  GLOBAL_CLASS_TABLE = NULL;
1211
  GLOBAL_AUTO_GLOBALS_TABLE = NULL;
1212
  GLOBAL_CONSTANTS_TABLE = NULL;
1213
  ts_free_id(executor_globals_id);
1214
  ts_free_id(compiler_globals_id);
1215
#else
1216
0
  if (CG(map_ptr_real_base)) {
1217
0
    free(CG(map_ptr_real_base));
1218
0
    CG(map_ptr_real_base) = NULL;
1219
0
    CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(NULL);
1220
0
    CG(map_ptr_size) = 0;
1221
0
  }
1222
0
  if (CG(script_encoding_list)) {
1223
0
    free(ZEND_VOIDP(CG(script_encoding_list)));
1224
0
    CG(script_encoding_list) = NULL;
1225
0
    CG(script_encoding_list_size) = 0;
1226
0
  }
1227
0
  if (CG(internal_run_time_cache)) {
1228
0
    pefree(CG(internal_run_time_cache), 1);
1229
0
    CG(internal_run_time_cache) = NULL;
1230
0
  }
1231
0
#endif
1232
0
  zend_map_ptr_static_last = 0;
1233
0
  zend_map_ptr_static_size = 0;
1234
1235
0
  zend_destroy_rsrc_list_dtors();
1236
1237
0
  zend_unload_modules();
1238
1239
0
  zend_optimizer_shutdown();
1240
0
  startup_done = false;
1241
0
}
1242
/* }}} */
1243
1244
void zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */
1245
16
{
1246
16
  zend_uv = *utility_values;
1247
16
}
1248
/* }}} */
1249
1250
/* this should be compatible with the standard zenderror */
1251
ZEND_COLD void zenderror(const char *error) /* {{{ */
1252
35.8k
{
1253
35.8k
  CG(parse_error) = 0;
1254
1255
35.8k
  if (EG(exception)) {
1256
    /* An exception was thrown in the lexer, don't throw another in the parser. */
1257
5.90k
    return;
1258
5.90k
  }
1259
1260
29.9k
  zend_throw_exception(zend_ce_parse_error, error, 0);
1261
29.9k
}
1262
/* }}} */
1263
1264
ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32_t lineno) /* {{{ */
1265
27.4k
{
1266
1267
27.4k
  if (!EG(bailout)) {
1268
0
    zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno);
1269
0
    exit(-1);
1270
0
  }
1271
27.4k
  gc_protect(1);
1272
27.4k
  CG(unclean_shutdown) = 1;
1273
27.4k
  CG(active_class_entry) = NULL;
1274
27.4k
  CG(in_compilation) = 0;
1275
27.4k
  CG(memoize_mode) = 0;
1276
27.4k
  EG(current_execute_data) = NULL;
1277
27.4k
  LONGJMP(*EG(bailout), FAILURE);
1278
27.4k
}
1279
/* }}} */
1280
1281
ZEND_API size_t zend_get_page_size(void)
1282
3
{
1283
#ifdef _WIN32
1284
  SYSTEM_INFO system_info;
1285
  GetSystemInfo(&system_info);
1286
  return system_info.dwPageSize;
1287
#elif defined(__FreeBSD__) || defined(__APPLE__)
1288
  /* This returns the value obtained from
1289
   * the auxv vector, avoiding a
1290
   * syscall (on FreeBSD)/function call (on macOS). */
1291
  return getpagesize();
1292
#else
1293
3
  return (size_t) sysconf(_SC_PAGESIZE);
1294
3
#endif
1295
3
}
1296
1297
ZEND_API void zend_append_version_info(const zend_extension *extension) /* {{{ */
1298
16
{
1299
16
  char *new_info;
1300
16
  uint32_t new_info_length;
1301
1302
16
  new_info_length = (uint32_t)(sizeof("    with  v, , by \n")
1303
16
            + strlen(extension->name)
1304
16
            + strlen(extension->version)
1305
16
            + strlen(extension->copyright)
1306
16
            + strlen(extension->author));
1307
1308
16
  new_info = (char *) malloc(new_info_length + 1);
1309
1310
16
  snprintf(new_info, new_info_length, "    with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
1311
1312
16
  zend_version_info = (char *) perealloc(zend_version_info, zend_version_info_length+new_info_length + 1, true);
1313
16
  strncat(zend_version_info, new_info, new_info_length);
1314
16
  zend_version_info_length += new_info_length;
1315
16
  free(new_info);
1316
16
}
1317
/* }}} */
1318
1319
ZEND_API const char *get_zend_version(void) /* {{{ */
1320
4
{
1321
4
  return zend_version_info;
1322
4
}
1323
/* }}} */
1324
1325
ZEND_API void zend_activate(void) /* {{{ */
1326
300k
{
1327
#ifdef ZTS
1328
  virtual_cwd_activate();
1329
#endif
1330
300k
  gc_reset();
1331
300k
  init_compiler();
1332
300k
  init_executor();
1333
300k
  startup_scanner();
1334
300k
  if (CG(map_ptr_last)) {
1335
300k
    memset((void **)CG(map_ptr_real_base) + zend_map_ptr_static_size, 0, CG(map_ptr_last) * sizeof(void*));
1336
300k
  }
1337
300k
  zend_reset_internal_run_time_cache();
1338
300k
  zend_observer_activate();
1339
300k
}
1340
/* }}} */
1341
1342
void zend_call_destructors(void) /* {{{ */
1343
300k
{
1344
300k
  zend_try {
1345
300k
    shutdown_destructors();
1346
300k
  } zend_end_try();
1347
300k
}
1348
/* }}} */
1349
1350
ZEND_API void zend_deactivate(void) /* {{{ */
1351
300k
{
1352
  /* we're no longer executing anything */
1353
300k
  EG(current_execute_data) = NULL;
1354
1355
300k
  zend_try {
1356
300k
    shutdown_scanner();
1357
300k
  } zend_end_try();
1358
1359
  /* shutdown_executor() takes care of its own bailout handling */
1360
300k
  shutdown_executor();
1361
1362
300k
  zend_try {
1363
300k
    zend_ini_deactivate();
1364
300k
  } zend_end_try();
1365
1366
300k
  zend_try {
1367
300k
    shutdown_compiler();
1368
300k
  } zend_end_try();
1369
1370
300k
  zend_destroy_rsrc_list(&EG(regular_list));
1371
1372
  /* See GH-8646: https://github.com/php/php-src/issues/8646
1373
   *
1374
   * Interned strings that hold class entries can get a corresponding slot in map_ptr for the CE cache.
1375
   * map_ptr works like a bump allocator: there is a counter which increases to allocate the next slot in the map.
1376
   *
1377
   * For class name strings in non-opcache we have:
1378
   *   - on startup: permanent + interned
1379
   *   - on request: interned
1380
   * For class name strings in opcache we have:
1381
   *   - on startup: permanent + interned
1382
   *   - on request: either not interned at all, which we can ignore because they won't get a CE cache entry
1383
   *                 or they were already permanent + interned
1384
   *                 or we get a new permanent + interned string in the opcache persistence code
1385
   *
1386
   * Notice that the map_ptr layout always has the permanent strings first, and the request strings after.
1387
   * In non-opcache, a request string may get a slot in map_ptr, and that interned request string
1388
   * gets destroyed at the end of the request. The corresponding map_ptr slot can thereafter never be used again.
1389
   * This causes map_ptr to keep reallocating to larger and larger sizes.
1390
   *
1391
   * We solve it as follows:
1392
   * We can check whether we had any interned request strings, which only happens in non-opcache.
1393
   * If we have any, we reset map_ptr to the last permanent string.
1394
   * We can't lose any permanent strings because of map_ptr's layout.
1395
   */
1396
300k
  if (zend_hash_num_elements(&CG(interned_strings)) > 0) {
1397
0
    zend_map_ptr_reset();
1398
0
  }
1399
1400
#if GC_BENCH
1401
  gc_bench_print();
1402
#endif
1403
300k
}
1404
/* }}} */
1405
1406
ZEND_API void zend_message_dispatcher(zend_long message, const void *data) /* {{{ */
1407
2.09k
{
1408
2.09k
  if (zend_message_dispatcher_p) {
1409
2.09k
    zend_message_dispatcher_p(message, data);
1410
2.09k
  }
1411
2.09k
}
1412
/* }}} */
1413
1414
ZEND_API zval *zend_get_configuration_directive(zend_string *name) /* {{{ */
1415
2.89k
{
1416
2.89k
  if (zend_get_configuration_directive_p) {
1417
2.89k
    return zend_get_configuration_directive_p(name);
1418
2.89k
  } else {
1419
0
    return NULL;
1420
0
  }
1421
2.89k
}
1422
/* }}} */
1423
1424
0
#define SAVE_STACK(stack) do { \
1425
0
    if (CG(stack).top) { \
1426
0
      memcpy(&stack, &CG(stack), sizeof(zend_stack)); \
1427
0
      CG(stack).top = CG(stack).max = 0; \
1428
0
      CG(stack).elements = NULL; \
1429
0
    } else { \
1430
0
      stack.top = 0; \
1431
0
    } \
1432
0
  } while (0)
1433
1434
0
#define RESTORE_STACK(stack) do { \
1435
0
    if (stack.top) { \
1436
0
      zend_stack_destroy(&CG(stack)); \
1437
0
      memcpy(&CG(stack), &stack, sizeof(zend_stack)); \
1438
0
    } \
1439
0
  } while (0)
1440
1441
ZEND_API ZEND_COLD void zend_error_zstr_at(
1442
    int orig_type, zend_string *error_filename, uint32_t error_lineno, zend_string *message)
1443
3.19M
{
1444
3.19M
  zval params[4];
1445
3.19M
  zval retval;
1446
3.19M
  zval orig_user_error_handler;
1447
3.19M
  bool in_compilation;
1448
3.19M
  zend_class_entry *saved_class_entry = NULL;
1449
3.19M
  zend_stack loop_var_stack;
1450
3.19M
  zend_stack delayed_oplines_stack;
1451
3.19M
  int type = orig_type & E_ALL;
1452
3.19M
  bool orig_record_errors;
1453
3.19M
  zend_err_buf orig_errors_buf;
1454
3.19M
  zend_result res;
1455
1456
  /* If we're executing a function during SCCP, count any warnings that may be emitted,
1457
   * but don't perform any other error handling. */
1458
3.19M
  if (EG(capture_warnings_during_sccp)) {
1459
0
    ZEND_ASSERT(!(type & E_FATAL_ERRORS) && "Fatal error during SCCP");
1460
0
    EG(capture_warnings_during_sccp)++;
1461
0
    return;
1462
0
  }
1463
1464
  /* Emit any delayed error before handling fatal error */
1465
3.19M
  if ((type & E_FATAL_ERRORS) && !(type & E_DONT_BAIL) && EG(errors).size) {
1466
277
    zend_err_buf errors_buf = EG(errors);
1467
277
    EG(errors).size = 0;
1468
1469
277
    bool orig_record_errors = EG(record_errors);
1470
277
    EG(record_errors) = false;
1471
1472
    /* Disable user error handler before emitting delayed errors, as
1473
     * it's unsafe to execute user code after a fatal error. */
1474
277
    int orig_user_error_handler_error_reporting = EG(user_error_handler_error_reporting);
1475
277
    EG(user_error_handler_error_reporting) = 0;
1476
1477
277
    zend_emit_recorded_errors_ex(errors_buf.size, errors_buf.errors);
1478
1479
277
    EG(user_error_handler_error_reporting) = orig_user_error_handler_error_reporting;
1480
277
    EG(record_errors) = orig_record_errors;
1481
277
    EG(errors) = errors_buf;
1482
277
  }
1483
1484
3.19M
  if (EG(record_errors)) {
1485
163k
    zend_error_info *info = emalloc(sizeof(zend_error_info));
1486
163k
    info->type = type;
1487
163k
    info->lineno = error_lineno;
1488
163k
    info->filename = zend_string_copy(error_filename);
1489
163k
    info->message = zend_string_copy(message);
1490
163k
    EG(errors).size++;
1491
163k
    if (EG(errors).size > EG(errors).capacity) {
1492
12.6k
      uint32_t capacity = EG(errors).capacity ? EG(errors).capacity + (EG(errors).capacity >> 1) : 2;
1493
12.6k
      EG(errors).errors = erealloc(EG(errors).errors, sizeof(zend_error_info *) * capacity);
1494
12.6k
      EG(errors).capacity = capacity;
1495
12.6k
    }
1496
163k
    EG(errors).errors[EG(errors).size - 1] = info;
1497
1498
    /* Do not process non-fatal recorded error */
1499
163k
    if (!(type & E_FATAL_ERRORS) || (type & E_DONT_BAIL)) {
1500
157k
      return;
1501
157k
    }
1502
163k
  }
1503
1504
  // Always clear the last backtrace.
1505
3.03M
  zval_ptr_dtor(&EG(last_fatal_error_backtrace));
1506
3.03M
  ZVAL_UNDEF(&EG(last_fatal_error_backtrace));
1507
1508
  /* Report about uncaught exception in case of fatal errors */
1509
3.03M
  if (EG(exception)) {
1510
978
    if (type & E_FATAL_ERRORS) {
1511
20
      zend_execute_data *ex = EG(current_execute_data);
1512
20
      const zend_op *opline = NULL;
1513
1514
29
      while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
1515
9
        ex = ex->prev_execute_data;
1516
9
      }
1517
20
      if (ex && ex->opline->opcode == ZEND_HANDLE_EXCEPTION &&
1518
11
          EG(opline_before_exception)) {
1519
11
        opline = EG(opline_before_exception);
1520
11
      }
1521
20
      zend_exception_error(EG(exception), E_WARNING);
1522
20
      EG(exception) = NULL;
1523
20
      if (opline) {
1524
11
        ex->opline = opline;
1525
11
      }
1526
20
    }
1527
3.03M
  } else if (EG(fatal_error_backtrace_on) && (type & E_FATAL_ERRORS)) {
1528
7.44k
    zend_fetch_debug_backtrace(&EG(last_fatal_error_backtrace), 0, EG(exception_ignore_args) ? DEBUG_BACKTRACE_IGNORE_ARGS : 0, 0);
1529
7.44k
  }
1530
1531
3.03M
  zend_observer_error_notify(type, error_filename, error_lineno, message);
1532
1533
  /* if we don't have a user defined error handler */
1534
3.03M
  if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF
1535
0
    || !(EG(user_error_handler_error_reporting) & type)
1536
3.03M
    || EG(error_handling) != EH_NORMAL) {
1537
3.03M
    zend_error_cb(orig_type, error_filename, error_lineno, message);
1538
3.03M
  } else switch (type) {
1539
0
    case E_ERROR:
1540
0
    case E_PARSE:
1541
0
    case E_CORE_ERROR:
1542
0
    case E_CORE_WARNING:
1543
0
    case E_COMPILE_ERROR:
1544
0
    case E_COMPILE_WARNING:
1545
      /* The error may not be safe to handle in user-space */
1546
0
      zend_error_cb(orig_type, error_filename, error_lineno, message);
1547
0
      break;
1548
0
    default:
1549
      /* Handle the error in user space */
1550
0
      ZVAL_STR_COPY(&params[1], message);
1551
0
      ZVAL_LONG(&params[0], type);
1552
1553
0
      if (error_filename) {
1554
0
        ZVAL_STR_COPY(&params[2], error_filename);
1555
0
      } else {
1556
0
        ZVAL_NULL(&params[2]);
1557
0
      }
1558
1559
0
      ZVAL_LONG(&params[3], error_lineno);
1560
1561
0
      ZVAL_COPY_VALUE(&orig_user_error_handler, &EG(user_error_handler));
1562
0
      ZVAL_UNDEF(&EG(user_error_handler));
1563
1564
      /* User error handler may include() additional PHP files.
1565
       * If an error was generated during compilation PHP will compile
1566
       * such scripts recursively, but some CG() variables may be
1567
       * inconsistent. */
1568
1569
0
      in_compilation = CG(in_compilation);
1570
0
      if (in_compilation) {
1571
0
        saved_class_entry = CG(active_class_entry);
1572
0
        CG(active_class_entry) = NULL;
1573
0
        SAVE_STACK(loop_var_stack);
1574
0
        SAVE_STACK(delayed_oplines_stack);
1575
0
        CG(in_compilation) = 0;
1576
0
      }
1577
1578
0
      orig_record_errors = EG(record_errors);
1579
0
      EG(record_errors) = false;
1580
1581
0
      orig_errors_buf = EG(errors);
1582
0
      memset(&EG(errors), 0, sizeof(EG(errors)));
1583
1584
0
      res = call_user_function(CG(function_table), NULL, &orig_user_error_handler, &retval, 4, params);
1585
1586
0
      EG(record_errors) = orig_record_errors;
1587
0
      EG(errors) = orig_errors_buf;
1588
1589
0
      if (res == SUCCESS) {
1590
0
        if (Z_TYPE(retval) != IS_UNDEF) {
1591
0
          if (Z_TYPE(retval) == IS_FALSE) {
1592
0
            zend_error_cb(orig_type, error_filename, error_lineno, message);
1593
0
          }
1594
0
          zval_ptr_dtor(&retval);
1595
0
        }
1596
0
      } else if (!EG(exception)) {
1597
        /* The user error handler failed, use built-in error handler */
1598
0
        zend_error_cb(orig_type, error_filename, error_lineno, message);
1599
0
      }
1600
1601
0
      if (in_compilation) {
1602
0
        CG(active_class_entry) = saved_class_entry;
1603
0
        RESTORE_STACK(loop_var_stack);
1604
0
        RESTORE_STACK(delayed_oplines_stack);
1605
0
        CG(in_compilation) = 1;
1606
0
      }
1607
1608
0
      zval_ptr_dtor(&params[2]);
1609
0
      zval_ptr_dtor(&params[1]);
1610
1611
0
      if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF) {
1612
0
        ZVAL_COPY_VALUE(&EG(user_error_handler), &orig_user_error_handler);
1613
0
      } else {
1614
0
        zval_ptr_dtor(&orig_user_error_handler);
1615
0
      }
1616
0
      break;
1617
12
  }
1618
1619
3.03M
  if (type == E_PARSE) {
1620
    /* eval() errors do not affect exit_status */
1621
0
    if (!(EG(current_execute_data) &&
1622
0
      EG(current_execute_data)->func &&
1623
0
      ZEND_USER_CODE(EG(current_execute_data)->func->type) &&
1624
0
      EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL &&
1625
0
      EG(current_execute_data)->opline->extended_value == ZEND_EVAL)) {
1626
0
      EG(exit_status) = 255;
1627
0
    }
1628
0
  }
1629
3.03M
}
1630
/* }}} */
1631
1632
static ZEND_COLD void zend_error_va_list(
1633
    int orig_type, zend_string *error_filename, uint32_t error_lineno,
1634
    const char *format, va_list args)
1635
2.59M
{
1636
2.59M
  zend_string *message = zend_vstrpprintf(0, format, args);
1637
2.59M
  zend_error_zstr_at(orig_type, error_filename, error_lineno, message);
1638
2.59M
  zend_string_release(message);
1639
2.59M
}
1640
1641
3.01M
static ZEND_COLD void get_filename_lineno(int type, zend_string **filename, uint32_t *lineno) {
1642
  /* Obtain relevant filename and lineno */
1643
3.01M
  switch (type) {
1644
8
    case E_CORE_ERROR:
1645
8
    case E_CORE_WARNING:
1646
8
      *filename = NULL;
1647
8
      *lineno = 0;
1648
8
      break;
1649
0
    case E_PARSE:
1650
5.42k
    case E_COMPILE_ERROR:
1651
50.6k
    case E_COMPILE_WARNING:
1652
51.6k
    case E_ERROR:
1653
62.0k
    case E_NOTICE:
1654
1.08M
    case E_DEPRECATED:
1655
3.01M
    case E_WARNING:
1656
3.01M
    case E_USER_ERROR:
1657
3.01M
    case E_USER_WARNING:
1658
3.01M
    case E_USER_NOTICE:
1659
3.01M
    case E_USER_DEPRECATED:
1660
3.01M
    case E_RECOVERABLE_ERROR:
1661
3.01M
      if (zend_is_compiling()) {
1662
162k
        *filename = zend_get_compiled_filename();
1663
162k
        *lineno = zend_get_compiled_lineno();
1664
2.85M
      } else if (zend_is_executing()) {
1665
2.78M
        *filename = zend_get_executed_filename_ex();
1666
2.78M
        *lineno = zend_get_executed_lineno();
1667
2.78M
      } else {
1668
75.0k
        *filename = NULL;
1669
75.0k
        *lineno = 0;
1670
75.0k
      }
1671
3.01M
      break;
1672
0
    default:
1673
0
      *filename = NULL;
1674
0
      *lineno = 0;
1675
0
      break;
1676
3.01M
  }
1677
3.01M
  if (!*filename) {
1678
997k
    *filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED);
1679
997k
  }
1680
3.01M
}
1681
1682
ZEND_API ZEND_COLD void zend_error_at(
1683
1.20k
    int type, zend_string *filename, uint32_t lineno, const char *format, ...) {
1684
1.20k
  va_list args;
1685
1686
1.20k
  if (!filename) {
1687
0
    uint32_t dummy_lineno;
1688
0
    get_filename_lineno(type, &filename, &dummy_lineno);
1689
0
  }
1690
1691
1.20k
  va_start(args, format);
1692
1.20k
  zend_error_va_list(type, filename, lineno, format, args);
1693
1.20k
  va_end(args);
1694
1.20k
}
1695
1696
2.58M
#define zend_error_impl(type, format) do { \
1697
2.58M
    zend_string *filename; \
1698
2.58M
    uint32_t lineno; \
1699
2.58M
    va_list args; \
1700
2.58M
    get_filename_lineno(type, &filename, &lineno); \
1701
2.58M
    va_start(args, format); \
1702
2.58M
    zend_error_va_list(type, filename, lineno, format, args); \
1703
2.58M
    va_end(args); \
1704
2.58M
  } while (0)
1705
1706
913k
ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) {
1707
913k
  zend_error_impl(type, format);
1708
913k
}
1709
1710
1.67M
ZEND_API ZEND_COLD void zend_error_unchecked(int type, const char *format, ...) {
1711
1.67M
  zend_error_impl(type, format);
1712
1.67M
}
1713
1714
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_at_noreturn(
1715
    int type, zend_string *filename, uint32_t lineno, const char *format, ...)
1716
93
{
1717
93
  va_list args;
1718
1719
93
  if (!filename) {
1720
0
    uint32_t dummy_lineno;
1721
0
    get_filename_lineno(type, &filename, &dummy_lineno);
1722
0
  }
1723
1724
93
  va_start(args, format);
1725
93
  zend_error_va_list(type, filename, lineno, format, args);
1726
93
  va_end(args);
1727
  /* Should never reach this. */
1728
93
  abort();
1729
93
}
1730
1731
5.84k
#define zend_error_noreturn_impl(type, format) do { \
1732
5.84k
    zend_string *filename; \
1733
5.84k
    uint32_t lineno; \
1734
5.84k
    va_list args; \
1735
5.84k
    get_filename_lineno(type, &filename, &lineno); \
1736
5.84k
    va_start(args, format); \
1737
5.84k
    zend_error_va_list(type, filename, lineno, format, args); \
1738
5.84k
    va_end(args); \
1739
5.84k
    /* Should never reach this. */ \
1740
5.84k
    abort(); \
1741
5.84k
  } while (0)
1742
1743
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...)
1744
5.82k
{
1745
5.82k
  zend_error_noreturn_impl(type, format);
1746
5.82k
}
1747
1748
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn_unchecked(int type, const char *format, ...)
1749
25
{
1750
25
  zend_error_noreturn_impl(type, format);
1751
25
}
1752
1753
ZEND_API ZEND_COLD ZEND_NORETURN void zend_strerror_noreturn(int type, int errn, const char *message)
1754
0
{
1755
0
#ifdef HAVE_STRERROR_R
1756
0
  char b[1024];
1757
1758
0
# ifdef STRERROR_R_CHAR_P
1759
0
  char *buf = strerror_r(errn, b, sizeof(b));
1760
# else
1761
  strerror_r(errn, b, sizeof(b));
1762
  char *buf = b;
1763
# endif
1764
#else
1765
  char *buf = strerror(errn);
1766
#endif
1767
1768
0
  zend_error_noreturn(type, "%s: %s (%d)", message, buf, errn);
1769
0
}
1770
1771
427k
ZEND_API ZEND_COLD void zend_error_zstr(int type, zend_string *message) {
1772
427k
  zend_string *filename;
1773
427k
  uint32_t lineno;
1774
427k
  get_filename_lineno(type, &filename, &lineno);
1775
427k
  zend_error_zstr_at(type, filename, lineno, message);
1776
427k
}
1777
1778
ZEND_API void zend_begin_record_errors(void)
1779
129k
{
1780
129k
  ZEND_ASSERT(!EG(record_errors) && "Error recording already enabled");
1781
129k
  EG(record_errors) = true;
1782
129k
  EG(errors).size = 0;
1783
129k
}
1784
1785
ZEND_API void zend_emit_recorded_errors_ex(uint32_t num_errors, zend_error_info **errors)
1786
272k
{
1787
447k
  for (uint32_t i = 0; i < num_errors; i++) {
1788
175k
    zend_error_info *error = errors[i];
1789
175k
    zend_error_zstr_at(error->type, error->filename, error->lineno, error->message);
1790
175k
  }
1791
272k
}
1792
1793
ZEND_API void zend_emit_recorded_errors(void)
1794
122k
{
1795
122k
  EG(record_errors) = false;
1796
122k
  zend_emit_recorded_errors_ex(EG(errors).size, EG(errors).errors);
1797
122k
}
1798
1799
ZEND_API void zend_free_recorded_errors(void)
1800
126k
{
1801
126k
  if (!EG(errors).size) {
1802
120k
    return;
1803
120k
  }
1804
1805
64.0k
  for (uint32_t i = 0; i < EG(errors).size; i++) {
1806
58.3k
    zend_error_info *info = EG(errors).errors[i];
1807
58.3k
    zend_string_release(info->filename);
1808
58.3k
    zend_string_release(info->message);
1809
58.3k
    efree_size(info, sizeof(zend_error_info));
1810
58.3k
  }
1811
5.65k
  efree(EG(errors).errors);
1812
5.65k
  memset(&EG(errors), 0, sizeof(EG(errors)));
1813
5.65k
}
1814
1815
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format, ...) /* {{{ */
1816
130k
{
1817
130k
  va_list va;
1818
130k
  char *message = NULL;
1819
1820
130k
  if (!exception_ce) {
1821
128k
    exception_ce = zend_ce_error;
1822
128k
  }
1823
1824
  /* Marker used to disable exception generation during preloading. */
1825
130k
  if (EG(exception) == (void*)(uintptr_t)-1) {
1826
0
    return;
1827
0
  }
1828
1829
130k
  va_start(va, format);
1830
130k
  zend_vspprintf(&message, 0, format, va);
1831
1832
  //TODO: we can't convert compile-time errors to exceptions yet???
1833
130k
  if (EG(current_execute_data) && !CG(in_compilation)) {
1834
130k
    zend_throw_exception(exception_ce, message, 0);
1835
130k
  } else {
1836
12
    zend_error_noreturn(E_ERROR, "%s", message);
1837
12
  }
1838
1839
130k
  efree(message);
1840
130k
  va_end(va);
1841
130k
}
1842
/* }}} */
1843
1844
/* type should be one of the BP_VAR_* constants, only special messages happen for isset/empty and unset */
1845
ZEND_API ZEND_COLD void zend_illegal_container_offset(const zend_string *container, const zval *offset, int type)
1846
558
{
1847
558
  switch (type) {
1848
35
    case BP_VAR_IS:
1849
35
      zend_type_error("Cannot access offset of type %s in isset or empty",
1850
35
        zend_zval_type_name(offset));
1851
35
      return;
1852
16
    case BP_VAR_UNSET:
1853
      /* Consistent error for when trying to unset a string offset */
1854
16
      if (zend_string_equals(container, ZSTR_KNOWN(ZEND_STR_STRING))) {
1855
7
        zend_throw_error(NULL, "Cannot unset string offsets");
1856
9
      } else {
1857
9
        zend_type_error("Cannot unset offset of type %s on %s", zend_zval_type_name(offset), ZSTR_VAL(container));
1858
9
      }
1859
16
      return;
1860
507
    default:
1861
507
      zend_type_error("Cannot access offset of type %s on %s",
1862
507
        zend_zval_type_name(offset), ZSTR_VAL(container));
1863
507
      return;
1864
558
  }
1865
558
}
1866
1867
ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) /* {{{ */
1868
7.61k
{
1869
7.61k
  va_list va;
1870
7.61k
  char *message = NULL;
1871
1872
7.61k
  va_start(va, format);
1873
7.61k
  zend_vspprintf(&message, 0, format, va);
1874
7.61k
  zend_throw_exception(zend_ce_type_error, message, 0);
1875
7.61k
  efree(message);
1876
7.61k
  va_end(va);
1877
7.61k
} /* }}} */
1878
1879
ZEND_API ZEND_COLD void zend_argument_count_error(const char *format, ...) /* {{{ */
1880
682
{
1881
682
  va_list va;
1882
682
  char *message = NULL;
1883
1884
682
  va_start(va, format);
1885
682
  zend_vspprintf(&message, 0, format, va);
1886
682
  zend_throw_exception(zend_ce_argument_count_error, message, 0);
1887
682
  efree(message);
1888
1889
682
  va_end(va);
1890
682
} /* }}} */
1891
1892
ZEND_API ZEND_COLD void zend_value_error(const char *format, ...) /* {{{ */
1893
141
{
1894
141
  va_list va;
1895
141
  char *message = NULL;
1896
1897
141
  va_start(va, format);
1898
141
  zend_vspprintf(&message, 0, format, va);
1899
141
  zend_throw_exception(zend_ce_value_error, message, 0);
1900
141
  efree(message);
1901
141
  va_end(va);
1902
141
} /* }}} */
1903
1904
ZEND_API ZEND_COLD void zend_output_debug_string(bool trigger_break, const char *format, ...) /* {{{ */
1905
0
{
1906
0
#if ZEND_DEBUG
1907
0
  va_list args;
1908
1909
0
  va_start(args, format);
1910
# ifdef ZEND_WIN32
1911
  {
1912
    char output_buf[1024];
1913
1914
    vsnprintf(output_buf, 1024, format, args);
1915
    OutputDebugString(output_buf);
1916
    OutputDebugString("\n");
1917
    if (trigger_break && IsDebuggerPresent()) {
1918
      DebugBreak();
1919
    }
1920
  }
1921
# else
1922
0
  vfprintf(stderr, format, args);
1923
0
  fprintf(stderr, "\n");
1924
0
# endif
1925
0
  va_end(args);
1926
0
#endif
1927
0
}
1928
/* }}} */
1929
1930
ZEND_API ZEND_COLD void zend_user_exception_handler(void) /* {{{ */
1931
16
{
1932
16
  zval orig_user_exception_handler;
1933
16
  zval params[1], retval2;
1934
16
  zend_object *old_exception;
1935
1936
16
  if (zend_is_unwind_exit(EG(exception))) {
1937
0
    return;
1938
0
  }
1939
1940
16
  old_exception = EG(exception);
1941
16
  EG(exception) = NULL;
1942
16
  ZVAL_OBJ(&params[0], old_exception);
1943
1944
16
  ZVAL_COPY_VALUE(&orig_user_exception_handler, &EG(user_exception_handler));
1945
16
  zend_stack_push(&EG(user_exception_handlers), &orig_user_exception_handler);
1946
16
  ZVAL_UNDEF(&EG(user_exception_handler));
1947
1948
16
  if (call_user_function(CG(function_table), NULL, &orig_user_exception_handler, &retval2, 1, params) == SUCCESS) {
1949
6
    zval_ptr_dtor(&retval2);
1950
6
    if (EG(exception)) {
1951
0
      OBJ_RELEASE(EG(exception));
1952
0
      EG(exception) = NULL;
1953
0
    }
1954
6
    OBJ_RELEASE(old_exception);
1955
10
  } else {
1956
10
    EG(exception) = old_exception;
1957
10
  }
1958
1959
16
  if (Z_TYPE(EG(user_exception_handler)) == IS_UNDEF) {
1960
6
    zval *tmp = zend_stack_top(&EG(user_exception_handlers));
1961
6
    if (tmp) {
1962
6
      ZVAL_COPY_VALUE(&EG(user_exception_handler), tmp);
1963
6
      zend_stack_del_top(&EG(user_exception_handlers));
1964
6
    }
1965
6
  }
1966
16
} /* }}} */
1967
1968
ZEND_API zend_result zend_execute_script(int type, zval *retval, zend_file_handle *file_handle)
1969
0
{
1970
0
  zend_op_array *op_array = zend_compile_file(file_handle, type);
1971
0
  if (file_handle->opened_path) {
1972
0
    zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path);
1973
0
  }
1974
1975
0
  zend_result ret = SUCCESS;
1976
0
  if (op_array) {
1977
0
    zend_execute(op_array, retval);
1978
0
    if (UNEXPECTED(EG(exception))) {
1979
0
      if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1980
0
        zend_user_exception_handler();
1981
0
      }
1982
0
      if (EG(exception)) {
1983
0
        ret = zend_exception_error(EG(exception), E_ERROR);
1984
0
      }
1985
0
    }
1986
0
    zend_destroy_static_vars(op_array);
1987
0
    destroy_op_array(op_array);
1988
0
    efree_size(op_array, sizeof(zend_op_array));
1989
0
  } else if (type == ZEND_REQUIRE) {
1990
0
    ret = FAILURE;
1991
0
  }
1992
1993
0
  return ret;
1994
0
}
1995
1996
ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count, ...) /* {{{ */
1997
0
{
1998
0
  va_list files;
1999
0
  int i;
2000
0
  zend_file_handle *file_handle;
2001
0
  zend_result ret = SUCCESS;
2002
2003
0
  va_start(files, file_count);
2004
0
  for (i = 0; i < file_count; i++) {
2005
0
    file_handle = va_arg(files, zend_file_handle *);
2006
0
    if (!file_handle) {
2007
0
      continue;
2008
0
    }
2009
0
    if (ret == FAILURE) {
2010
0
      continue;
2011
0
    }
2012
0
    ret = zend_execute_script(type, retval, file_handle);
2013
0
  }
2014
0
  va_end(files);
2015
2016
0
  return ret;
2017
0
}
2018
/* }}} */
2019
2020
28.1k
#define COMPILED_STRING_DESCRIPTION_FORMAT "%s(%d) : %s"
2021
2022
ZEND_API char *zend_make_compiled_string_description(const char *name) /* {{{ */
2023
28.1k
{
2024
28.1k
  const char *cur_filename;
2025
28.1k
  int cur_lineno;
2026
28.1k
  char *compiled_string_description;
2027
2028
28.1k
  if (zend_is_compiling()) {
2029
0
    cur_filename = ZSTR_VAL(zend_get_compiled_filename());
2030
0
    cur_lineno = zend_get_compiled_lineno();
2031
28.1k
  } else if (zend_is_executing()) {
2032
28.1k
    cur_filename = zend_get_executed_filename();
2033
28.1k
    cur_lineno = zend_get_executed_lineno();
2034
28.1k
  } else {
2035
0
    cur_filename = "Unknown";
2036
0
    cur_lineno = 0;
2037
0
  }
2038
2039
28.1k
  zend_spprintf(&compiled_string_description, 0, COMPILED_STRING_DESCRIPTION_FORMAT, cur_filename, cur_lineno, name);
2040
28.1k
  return compiled_string_description;
2041
28.1k
}
2042
/* }}} */
2043
2044
void free_estring(char **str_p) /* {{{ */
2045
0
{
2046
0
  efree(*str_p);
2047
0
}
2048
/* }}} */
2049
2050
ZEND_API size_t zend_map_ptr_static_size;
2051
ZEND_API size_t zend_map_ptr_static_last;
2052
2053
ZEND_API void zend_map_ptr_reset(void)
2054
0
{
2055
0
  CG(map_ptr_last) = global_map_ptr_last;
2056
0
}
2057
2058
ZEND_API void *zend_map_ptr_new(void)
2059
65.7k
{
2060
65.7k
  void **ptr;
2061
2062
65.7k
  if (CG(map_ptr_last) >= CG(map_ptr_size)) {
2063
    /* Grow map_ptr table */
2064
30
    CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(CG(map_ptr_last) + 1, 4096);
2065
30
    CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), (zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1);
2066
30
    CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(CG(map_ptr_real_base));
2067
30
  }
2068
65.7k
  ptr = (void**)CG(map_ptr_real_base) + zend_map_ptr_static_size + CG(map_ptr_last);
2069
65.7k
  *ptr = NULL;
2070
65.7k
  CG(map_ptr_last)++;
2071
65.7k
  return ZEND_MAP_PTR_PTR2OFFSET(ptr);
2072
65.7k
}
2073
2074
ZEND_API void *zend_map_ptr_new_static(void)
2075
0
{
2076
0
  void **ptr;
2077
2078
0
  if (zend_map_ptr_static_last >= zend_map_ptr_static_size) {
2079
0
    zend_map_ptr_static_size += 4096;
2080
    /* Grow map_ptr table */
2081
0
    void *new_base = pemalloc((zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1);
2082
0
    if (CG(map_ptr_real_base)) {
2083
0
      memcpy((void **)new_base + 4096, CG(map_ptr_real_base), (CG(map_ptr_last) + zend_map_ptr_static_size - 4096) * sizeof(void *));
2084
0
      pefree(CG(map_ptr_real_base), 1);
2085
0
    }
2086
0
    CG(map_ptr_real_base) = new_base;
2087
0
    CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(new_base);
2088
0
  }
2089
0
  ptr = (void**)CG(map_ptr_real_base) + (zend_map_ptr_static_last & 4095);
2090
0
  *ptr = NULL;
2091
0
  zend_map_ptr_static_last++;
2092
0
  return ZEND_MAP_PTR_PTR2OFFSET(ptr);
2093
0
}
2094
2095
ZEND_API void zend_map_ptr_extend(size_t last)
2096
69.9k
{
2097
69.9k
  if (last > CG(map_ptr_last)) {
2098
0
    void **ptr;
2099
2100
0
    if (last >= CG(map_ptr_size)) {
2101
      /* Grow map_ptr table */
2102
0
      CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(last, 4096);
2103
0
      CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), (zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1);
2104
0
      CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(CG(map_ptr_real_base));
2105
0
    }
2106
0
    ptr = (void**)CG(map_ptr_real_base) + zend_map_ptr_static_size + CG(map_ptr_last);
2107
0
    memset(ptr, 0, (last - CG(map_ptr_last)) * sizeof(void*));
2108
0
    CG(map_ptr_last) = last;
2109
0
  }
2110
69.9k
}
2111
2112
ZEND_API void zend_alloc_ce_cache(zend_string *type_name)
2113
963k
{
2114
963k
  if (ZSTR_HAS_CE_CACHE(type_name) || !ZSTR_IS_INTERNED(type_name)) {
2115
942k
    return;
2116
942k
  }
2117
2118
21.7k
  if ((GC_FLAGS(type_name) & IS_STR_PERMANENT) && startup_done) {
2119
    /* Don't allocate slot on permanent interned string outside module startup.
2120
     * The cache slot would no longer be valid on the next request. */
2121
18.6k
    return;
2122
18.6k
  }
2123
2124
3.07k
  if (zend_string_equals_ci(type_name, ZSTR_KNOWN(ZEND_STR_SELF))
2125
3.07k
      || zend_string_equals_ci(type_name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
2126
0
    return;
2127
0
  }
2128
2129
  /* We use the refcount to keep map_ptr of corresponding type */
2130
3.07k
  uint32_t ret;
2131
3.08k
  do {
2132
3.08k
    ret = ZEND_MAP_PTR_NEW_OFFSET();
2133
3.08k
  } while (ret <= 2);
2134
3.07k
  GC_ADD_FLAGS(type_name, IS_STR_CLASS_NAME_MAP_PTR);
2135
3.07k
  GC_SET_REFCOUNT(type_name, ret);
2136
3.07k
}