Coverage Report

Created: 2026-06-02 06:36

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