Coverage Report

Created: 2023-12-13 20:06

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