Coverage Report

Created: 2026-01-18 06:48

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