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