Coverage Report

Created: 2025-06-13 06:43

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