xref: /PHP-8.3/Zend/zend.c (revision 1f35e2a9)
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 	compiler_globals->zend_lineno = 0;
707 
708 	compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
709 	zend_hash_init(compiler_globals->function_table, 1024, NULL, ZEND_FUNCTION_DTOR, 1);
710 	zend_hash_copy(compiler_globals->function_table, global_function_table, NULL);
711 	compiler_globals->copied_functions_count = zend_hash_num_elements(compiler_globals->function_table);
712 
713 	compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
714 	zend_hash_init(compiler_globals->class_table, 64, NULL, ZEND_CLASS_DTOR, 1);
715 	zend_hash_copy(compiler_globals->class_table, global_class_table, zend_class_add_ref);
716 
717 	zend_set_default_compile_time_values();
718 
719 	compiler_globals->auto_globals = (HashTable *) malloc(sizeof(HashTable));
720 	zend_hash_init(compiler_globals->auto_globals, 8, NULL, auto_global_dtor, 1);
721 	zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, auto_global_copy_ctor);
722 
723 	compiler_globals->script_encoding_list = NULL;
724 	compiler_globals->current_linking_class = NULL;
725 
726 	/* Map region is going to be created and resized at run-time. */
727 	compiler_globals->map_ptr_real_base = NULL;
728 	compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
729 	compiler_globals->map_ptr_size = 0;
730 	compiler_globals->map_ptr_last = global_map_ptr_last;
731 	if (compiler_globals->map_ptr_last) {
732 		/* Allocate map_ptr table */
733 		compiler_globals->map_ptr_size = ZEND_MM_ALIGNED_SIZE_EX(compiler_globals->map_ptr_last, 4096);
734 		void *base = pemalloc(compiler_globals->map_ptr_size * sizeof(void*), 1);
735 		compiler_globals->map_ptr_real_base = base;
736 		compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(base);
737 		memset(base, 0, compiler_globals->map_ptr_last * sizeof(void*));
738 	}
739 }
740 /* }}} */
741 
compiler_globals_dtor(zend_compiler_globals * compiler_globals)742 static void compiler_globals_dtor(zend_compiler_globals *compiler_globals) /* {{{ */
743 {
744 	if (compiler_globals->function_table != GLOBAL_FUNCTION_TABLE) {
745 		uint32_t n = compiler_globals->copied_functions_count;
746 
747 	    /* Prevent destruction of functions copied from the main process context */
748 		if (zend_hash_num_elements(compiler_globals->function_table) <= n) {
749 			compiler_globals->function_table->nNumUsed = 0;
750 		} else {
751 			Bucket *p = compiler_globals->function_table->arData;
752 
753 			compiler_globals->function_table->nNumOfElements -= n;
754 			while (n != 0) {
755 				ZVAL_UNDEF(&p->val);
756 				p++;
757 				n--;
758 			}
759 		}
760 		zend_hash_destroy(compiler_globals->function_table);
761 		free(compiler_globals->function_table);
762 	}
763 	if (compiler_globals->class_table != GLOBAL_CLASS_TABLE) {
764 		/* Child classes may reuse structures from parent classes, so destroy in reverse order. */
765 		zend_hash_graceful_reverse_destroy(compiler_globals->class_table);
766 		free(compiler_globals->class_table);
767 	}
768 	if (compiler_globals->auto_globals != GLOBAL_AUTO_GLOBALS_TABLE) {
769 		zend_hash_destroy(compiler_globals->auto_globals);
770 		free(compiler_globals->auto_globals);
771 	}
772 	if (compiler_globals->script_encoding_list) {
773 		pefree((char*)compiler_globals->script_encoding_list, 1);
774 	}
775 	if (compiler_globals->map_ptr_real_base) {
776 		free(compiler_globals->map_ptr_real_base);
777 		compiler_globals->map_ptr_real_base = NULL;
778 		compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
779 		compiler_globals->map_ptr_size = 0;
780 	}
781 }
782 /* }}} */
783 
executor_globals_ctor(zend_executor_globals * executor_globals)784 static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{{ */
785 {
786 	zend_startup_constants();
787 	zend_copy_constants(executor_globals->zend_constants, GLOBAL_CONSTANTS_TABLE);
788 	zend_init_rsrc_plist();
789 	zend_init_exception_op();
790 	zend_init_call_trampoline_op();
791 	memset(&executor_globals->trampoline, 0, sizeof(zend_op_array));
792 	executor_globals->capture_warnings_during_sccp = 0;
793 	executor_globals->user_error_handler_error_reporting = 0;
794 	ZVAL_UNDEF(&executor_globals->user_error_handler);
795 	ZVAL_UNDEF(&executor_globals->user_exception_handler);
796 	executor_globals->in_autoload = NULL;
797 	executor_globals->current_execute_data = NULL;
798 	executor_globals->current_module = NULL;
799 	executor_globals->exit_status = 0;
800 #if XPFPA_HAVE_CW
801 	executor_globals->saved_fpu_cw = 0;
802 #endif
803 	executor_globals->saved_fpu_cw_ptr = NULL;
804 	executor_globals->active = 0;
805 	executor_globals->bailout = NULL;
806 	executor_globals->error_handling  = EH_NORMAL;
807 	executor_globals->exception_class = NULL;
808 	executor_globals->exception = NULL;
809 	executor_globals->objects_store.object_buckets = NULL;
810 	executor_globals->current_fiber_context = NULL;
811 	executor_globals->main_fiber_context = NULL;
812 	executor_globals->active_fiber = NULL;
813 #ifdef ZEND_WIN32
814 	zend_get_windows_version_info(&executor_globals->windows_version_info);
815 #endif
816 	executor_globals->flags = EG_FLAGS_INITIAL;
817 	executor_globals->record_errors = false;
818 	executor_globals->num_errors = 0;
819 	executor_globals->errors = NULL;
820 #ifdef ZEND_CHECK_STACK_LIMIT
821 	executor_globals->stack_limit = (void*)0;
822 	executor_globals->stack_base = (void*)0;
823 #endif
824 #ifdef ZEND_MAX_EXECUTION_TIMERS
825 	executor_globals->pid = 0;
826 	executor_globals->oldact = (struct sigaction){0};
827 #endif
828 }
829 /* }}} */
830 
executor_globals_dtor(zend_executor_globals * executor_globals)831 static void executor_globals_dtor(zend_executor_globals *executor_globals) /* {{{ */
832 {
833 	zend_ini_dtor(executor_globals->ini_directives);
834 
835 	if (&executor_globals->persistent_list != global_persistent_list) {
836 		zend_destroy_rsrc_list(&executor_globals->persistent_list);
837 	}
838 	if (executor_globals->zend_constants != GLOBAL_CONSTANTS_TABLE) {
839 		zend_hash_destroy(executor_globals->zend_constants);
840 		free(executor_globals->zend_constants);
841 	}
842 }
843 /* }}} */
844 
zend_new_thread_end_handler(THREAD_T thread_id)845 static void zend_new_thread_end_handler(THREAD_T thread_id) /* {{{ */
846 {
847 	zend_copy_ini_directives();
848 	zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP);
849 #ifdef ZEND_CHECK_STACK_LIMIT
850 	zend_call_stack_init();
851 #endif
852 	zend_max_execution_timer_init();
853 }
854 /* }}} */
855 #endif
856 
857 #if defined(__FreeBSD__) || defined(__DragonFly__)
858 /* FreeBSD and DragonFly floating point precision fix */
859 #include <floatingpoint.h>
860 #endif
861 
ini_scanner_globals_ctor(zend_ini_scanner_globals * scanner_globals_p)862 static void ini_scanner_globals_ctor(zend_ini_scanner_globals *scanner_globals_p) /* {{{ */
863 {
864 	memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
865 }
866 /* }}} */
867 
php_scanner_globals_ctor(zend_php_scanner_globals * scanner_globals_p)868 static void php_scanner_globals_ctor(zend_php_scanner_globals *scanner_globals_p) /* {{{ */
869 {
870 	memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
871 }
872 /* }}} */
873 
module_destructor_zval(zval * zv)874 static void module_destructor_zval(zval *zv) /* {{{ */
875 {
876 	zend_module_entry *module = (zend_module_entry*)Z_PTR_P(zv);
877 	module_destructor(module);
878 }
879 /* }}} */
880 
php_auto_globals_create_globals(zend_string * name)881 static bool php_auto_globals_create_globals(zend_string *name) /* {{{ */
882 {
883 	/* While we keep registering $GLOBALS as an auto-global, we do not create an
884 	 * actual variable for it. Access to it handled specially by the compiler. */
885 	return 0;
886 }
887 /* }}} */
888 
zend_startup(zend_utility_functions * utility_functions)889 void zend_startup(zend_utility_functions *utility_functions) /* {{{ */
890 {
891 #ifdef ZTS
892 	zend_compiler_globals *compiler_globals;
893 	zend_executor_globals *executor_globals;
894 	extern ZEND_API ts_rsrc_id ini_scanner_globals_id;
895 	extern ZEND_API ts_rsrc_id language_scanner_globals_id;
896 #else
897 	extern zend_ini_scanner_globals ini_scanner_globals;
898 	extern zend_php_scanner_globals language_scanner_globals;
899 #endif
900 
901 	zend_cpu_startup();
902 
903 #ifdef ZEND_WIN32
904 	php_win32_cp_set_by_id(65001);
905 #endif
906 
907 	start_memory_manager();
908 
909 	virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */
910 
911 #if defined(__FreeBSD__) || defined(__DragonFly__)
912 	/* FreeBSD and DragonFly floating point precision fix */
913 	fpsetmask(0);
914 #endif
915 
916 	zend_startup_hrtime();
917 	zend_startup_strtod();
918 	zend_startup_extensions_mechanism();
919 
920 	/* Set up utility functions and values */
921 	zend_error_cb = utility_functions->error_function;
922 	zend_printf = utility_functions->printf_function;
923 	zend_write = utility_functions->write_function;
924 	zend_fopen = utility_functions->fopen_function;
925 	if (!zend_fopen) {
926 		zend_fopen = zend_fopen_wrapper;
927 	}
928 	zend_stream_open_function = utility_functions->stream_open_function;
929 	zend_message_dispatcher_p = utility_functions->message_handler;
930 	zend_get_configuration_directive_p = utility_functions->get_configuration_directive;
931 	zend_ticks_function = utility_functions->ticks_function;
932 	zend_on_timeout = utility_functions->on_timeout;
933 	zend_printf_to_smart_string = utility_functions->printf_to_smart_string_function;
934 	zend_printf_to_smart_str = utility_functions->printf_to_smart_str_function;
935 	zend_getenv = utility_functions->getenv_function;
936 	zend_resolve_path = utility_functions->resolve_path_function;
937 
938 	zend_interrupt_function = NULL;
939 
940 #ifdef HAVE_DTRACE
941 /* build with dtrace support */
942 	{
943 		char *tmp = getenv("USE_ZEND_DTRACE");
944 
945 		if (tmp && ZEND_ATOL(tmp)) {
946 			zend_dtrace_enabled = 1;
947 			zend_compile_file = dtrace_compile_file;
948 			zend_execute_ex = dtrace_execute_ex;
949 			zend_execute_internal = dtrace_execute_internal;
950 
951 			zend_observer_error_register(dtrace_error_notify_cb);
952 		} else {
953 			zend_compile_file = compile_file;
954 			zend_execute_ex = execute_ex;
955 			zend_execute_internal = NULL;
956 		}
957 	}
958 #else
959 	zend_compile_file = compile_file;
960 	zend_execute_ex = execute_ex;
961 	zend_execute_internal = NULL;
962 #endif /* HAVE_DTRACE */
963 	zend_compile_string = compile_string;
964 	zend_throw_exception_hook = NULL;
965 
966 	/* Set up the default garbage collection implementation. */
967 	gc_collect_cycles = zend_gc_collect_cycles;
968 
969 	zend_vm_init();
970 
971 	/* set up version */
972 	zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
973 	zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1;
974 
975 	GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
976 	GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
977 	GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable));
978 	GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable));
979 
980 	zend_hash_init(GLOBAL_FUNCTION_TABLE, 1024, NULL, ZEND_FUNCTION_DTOR, 1);
981 	zend_hash_init(GLOBAL_CLASS_TABLE, 64, NULL, ZEND_CLASS_DTOR, 1);
982 	zend_hash_init(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, auto_global_dtor, 1);
983 	zend_hash_init(GLOBAL_CONSTANTS_TABLE, 128, NULL, ZEND_CONSTANT_DTOR, 1);
984 
985 	zend_hash_init(&module_registry, 32, NULL, module_destructor_zval, 1);
986 	zend_init_rsrc_list_dtors();
987 
988 #ifdef ZTS
989 	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);
990 	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);
991 	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);
992 	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);
993 	compiler_globals = ts_resource(compiler_globals_id);
994 	executor_globals = ts_resource(executor_globals_id);
995 
996 	compiler_globals_dtor(compiler_globals);
997 	compiler_globals->in_compilation = 0;
998 	compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
999 	compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
1000 
1001 	*compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE;
1002 	*compiler_globals->class_table = *GLOBAL_CLASS_TABLE;
1003 	compiler_globals->auto_globals = GLOBAL_AUTO_GLOBALS_TABLE;
1004 
1005 	zend_hash_destroy(executor_globals->zend_constants);
1006 	*executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE;
1007 #else
1008 	ini_scanner_globals_ctor(&ini_scanner_globals);
1009 	php_scanner_globals_ctor(&language_scanner_globals);
1010 	zend_set_default_compile_time_values();
1011 #ifdef ZEND_WIN32
1012 	zend_get_windows_version_info(&EG(windows_version_info));
1013 #endif
1014 	/* Map region is going to be created and resized at run-time. */
1015 	CG(map_ptr_real_base) = NULL;
1016 	CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(NULL);
1017 	CG(map_ptr_size) = 0;
1018 	CG(map_ptr_last) = 0;
1019 #endif /* ZTS */
1020 	EG(error_reporting) = E_ALL & ~E_NOTICE;
1021 
1022 	zend_interned_strings_init();
1023 	zend_startup_builtin_functions();
1024 	zend_register_standard_constants();
1025 	zend_register_auto_global(zend_string_init_interned("GLOBALS", sizeof("GLOBALS") - 1, 1), 1, php_auto_globals_create_globals);
1026 
1027 #ifndef ZTS
1028 	zend_init_rsrc_plist();
1029 	zend_init_exception_op();
1030 	zend_init_call_trampoline_op();
1031 #endif
1032 
1033 	zend_ini_startup();
1034 
1035 #ifdef ZEND_WIN32
1036 	/* Uses INI settings, so needs to be run after it. */
1037 	php_win32_cp_setup();
1038 #endif
1039 
1040 	zend_optimizer_startup();
1041 
1042 #ifdef ZTS
1043 	tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
1044 	tsrm_set_shutdown_handler(zend_interned_strings_dtor);
1045 #endif
1046 }
1047 /* }}} */
1048 
zend_register_standard_ini_entries(void)1049 void zend_register_standard_ini_entries(void) /* {{{ */
1050 {
1051 	zend_register_ini_entries_ex(ini_entries, 0, MODULE_PERSISTENT);
1052 }
1053 /* }}} */
1054 
1055 
1056 /* Unlink the global (r/o) copies of the class, function and constant tables,
1057  * and use a fresh r/w copy for the startup thread
1058  */
zend_post_startup(void)1059 zend_result zend_post_startup(void) /* {{{ */
1060 {
1061 #ifdef ZTS
1062 	zend_encoding **script_encoding_list;
1063 
1064 	zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id);
1065 	zend_executor_globals *executor_globals = ts_resource(executor_globals_id);
1066 #endif
1067 
1068 	startup_done = true;
1069 
1070 	if (zend_post_startup_cb) {
1071 		zend_result (*cb)(void) = zend_post_startup_cb;
1072 
1073 		zend_post_startup_cb = NULL;
1074 		if (cb() != SUCCESS) {
1075 			return FAILURE;
1076 		}
1077 	}
1078 
1079 #ifdef ZTS
1080 	*GLOBAL_FUNCTION_TABLE = *compiler_globals->function_table;
1081 	*GLOBAL_CLASS_TABLE = *compiler_globals->class_table;
1082 	*GLOBAL_CONSTANTS_TABLE = *executor_globals->zend_constants;
1083 	global_map_ptr_last = compiler_globals->map_ptr_last;
1084 
1085 	short_tags_default = CG(short_tags);
1086 	compiler_options_default = CG(compiler_options);
1087 
1088 	zend_destroy_rsrc_list(&EG(persistent_list));
1089 	free(compiler_globals->function_table);
1090 	compiler_globals->function_table = NULL;
1091 	free(compiler_globals->class_table);
1092 	compiler_globals->class_table = NULL;
1093 	if (compiler_globals->map_ptr_real_base) {
1094 		free(compiler_globals->map_ptr_real_base);
1095 	}
1096 	compiler_globals->map_ptr_real_base = NULL;
1097 	compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
1098 	if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
1099 		compiler_globals_ctor(compiler_globals);
1100 		compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list;
1101 	} else {
1102 		compiler_globals_ctor(compiler_globals);
1103 	}
1104 	free(EG(zend_constants));
1105 	EG(zend_constants) = NULL;
1106 
1107 	executor_globals_ctor(executor_globals);
1108 	global_persistent_list = &EG(persistent_list);
1109 	zend_copy_ini_directives();
1110 #else
1111 	global_map_ptr_last = CG(map_ptr_last);
1112 #endif
1113 
1114 #ifdef ZEND_CHECK_STACK_LIMIT
1115 	zend_call_stack_init();
1116 #endif
1117 
1118 	return SUCCESS;
1119 }
1120 /* }}} */
1121 
zend_shutdown(void)1122 void zend_shutdown(void) /* {{{ */
1123 {
1124 	zend_vm_dtor();
1125 
1126 	zend_destroy_rsrc_list(&EG(persistent_list));
1127 	zend_destroy_modules();
1128 
1129 	virtual_cwd_deactivate();
1130 	virtual_cwd_shutdown();
1131 
1132 	zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
1133 	/* Child classes may reuse structures from parent classes, so destroy in reverse order. */
1134 	zend_hash_graceful_reverse_destroy(GLOBAL_CLASS_TABLE);
1135 
1136 	zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE);
1137 	free(GLOBAL_AUTO_GLOBALS_TABLE);
1138 
1139 	zend_shutdown_extensions();
1140 	free(zend_version_info);
1141 
1142 	free(GLOBAL_FUNCTION_TABLE);
1143 	free(GLOBAL_CLASS_TABLE);
1144 
1145 	zend_hash_destroy(GLOBAL_CONSTANTS_TABLE);
1146 	free(GLOBAL_CONSTANTS_TABLE);
1147 	zend_shutdown_strtod();
1148 	zend_attributes_shutdown();
1149 
1150 #ifdef ZTS
1151 	GLOBAL_FUNCTION_TABLE = NULL;
1152 	GLOBAL_CLASS_TABLE = NULL;
1153 	GLOBAL_AUTO_GLOBALS_TABLE = NULL;
1154 	GLOBAL_CONSTANTS_TABLE = NULL;
1155 	ts_free_id(executor_globals_id);
1156 	ts_free_id(compiler_globals_id);
1157 #else
1158 	if (CG(map_ptr_real_base)) {
1159 		free(CG(map_ptr_real_base));
1160 		CG(map_ptr_real_base) = NULL;
1161 		CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(NULL);
1162 		CG(map_ptr_size) = 0;
1163 	}
1164 	if (CG(script_encoding_list)) {
1165 		free(ZEND_VOIDP(CG(script_encoding_list)));
1166 		CG(script_encoding_list) = NULL;
1167 		CG(script_encoding_list_size) = 0;
1168 	}
1169 #endif
1170 	zend_destroy_rsrc_list_dtors();
1171 
1172 	zend_unload_modules();
1173 
1174 	zend_optimizer_shutdown();
1175 	startup_done = false;
1176 }
1177 /* }}} */
1178 
zend_set_utility_values(zend_utility_values * utility_values)1179 void zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */
1180 {
1181 	zend_uv = *utility_values;
1182 }
1183 /* }}} */
1184 
1185 /* this should be compatible with the standard zenderror */
zenderror(const char * error)1186 ZEND_COLD void zenderror(const char *error) /* {{{ */
1187 {
1188 	CG(parse_error) = 0;
1189 
1190 	if (EG(exception)) {
1191 		/* An exception was thrown in the lexer, don't throw another in the parser. */
1192 		return;
1193 	}
1194 
1195 	zend_throw_exception(zend_ce_parse_error, error, 0);
1196 }
1197 /* }}} */
1198 
_zend_bailout(const char * filename,uint32_t lineno)1199 ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32_t lineno) /* {{{ */
1200 {
1201 
1202 	if (!EG(bailout)) {
1203 		zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno);
1204 		exit(-1);
1205 	}
1206 	gc_protect(1);
1207 	CG(unclean_shutdown) = 1;
1208 	CG(active_class_entry) = NULL;
1209 	CG(in_compilation) = 0;
1210 	CG(memoize_mode) = 0;
1211 	EG(current_execute_data) = NULL;
1212 	LONGJMP(*EG(bailout), FAILURE);
1213 }
1214 /* }}} */
1215 
zend_get_page_size(void)1216 ZEND_API size_t zend_get_page_size(void)
1217 {
1218 #ifdef _WIN32
1219 	SYSTEM_INFO system_info;
1220 	GetSystemInfo(&system_info);
1221 	return system_info.dwPageSize;
1222 #elif defined(__FreeBSD__)
1223 	/* This returns the value obtained from
1224 	 * the auxv vector, avoiding a syscall. */
1225 	return getpagesize();
1226 #else
1227 	return (size_t) sysconf(_SC_PAGESIZE);
1228 #endif
1229 }
1230 
zend_append_version_info(const zend_extension * extension)1231 ZEND_API void zend_append_version_info(const zend_extension *extension) /* {{{ */
1232 {
1233 	char *new_info;
1234 	uint32_t new_info_length;
1235 
1236 	new_info_length = (uint32_t)(sizeof("    with  v, , by \n")
1237 						+ strlen(extension->name)
1238 						+ strlen(extension->version)
1239 						+ strlen(extension->copyright)
1240 						+ strlen(extension->author));
1241 
1242 	new_info = (char *) malloc(new_info_length + 1);
1243 
1244 	snprintf(new_info, new_info_length, "    with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
1245 
1246 	zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1);
1247 	strncat(zend_version_info, new_info, new_info_length);
1248 	zend_version_info_length += new_info_length;
1249 	free(new_info);
1250 }
1251 /* }}} */
1252 
get_zend_version(void)1253 ZEND_API const char *get_zend_version(void) /* {{{ */
1254 {
1255 	return zend_version_info;
1256 }
1257 /* }}} */
1258 
zend_activate(void)1259 ZEND_API void zend_activate(void) /* {{{ */
1260 {
1261 #ifdef ZTS
1262 	virtual_cwd_activate();
1263 #endif
1264 	gc_reset();
1265 	init_compiler();
1266 	init_executor();
1267 	startup_scanner();
1268 	if (CG(map_ptr_last)) {
1269 		memset(CG(map_ptr_real_base), 0, CG(map_ptr_last) * sizeof(void*));
1270 	}
1271 	zend_init_internal_run_time_cache();
1272 	zend_observer_activate();
1273 }
1274 /* }}} */
1275 
zend_call_destructors(void)1276 void zend_call_destructors(void) /* {{{ */
1277 {
1278 	zend_try {
1279 		shutdown_destructors();
1280 	} zend_end_try();
1281 }
1282 /* }}} */
1283 
zend_deactivate(void)1284 ZEND_API void zend_deactivate(void) /* {{{ */
1285 {
1286 	/* we're no longer executing anything */
1287 	EG(current_execute_data) = NULL;
1288 
1289 	zend_try {
1290 		shutdown_scanner();
1291 	} zend_end_try();
1292 
1293 	/* shutdown_executor() takes care of its own bailout handling */
1294 	shutdown_executor();
1295 
1296 	zend_try {
1297 		zend_ini_deactivate();
1298 	} zend_end_try();
1299 
1300 	zend_try {
1301 		shutdown_compiler();
1302 	} zend_end_try();
1303 
1304 	zend_destroy_rsrc_list(&EG(regular_list));
1305 
1306 	/* See GH-8646: https://github.com/php/php-src/issues/8646
1307 	 *
1308 	 * Interned strings that hold class entries can get a corresponding slot in map_ptr for the CE cache.
1309 	 * map_ptr works like a bump allocator: there is a counter which increases to allocate the next slot in the map.
1310 	 *
1311 	 * For class name strings in non-opcache we have:
1312 	 *   - on startup: permanent + interned
1313 	 *   - on request: interned
1314 	 * For class name strings in opcache we have:
1315 	 *   - on startup: permanent + interned
1316 	 *   - on request: either not interned at all, which we can ignore because they won't get a CE cache entry
1317 	 *                 or they were already permanent + interned
1318 	 *                 or we get a new permanent + interned string in the opcache persistence code
1319 	 *
1320 	 * Notice that the map_ptr layout always has the permanent strings first, and the request strings after.
1321 	 * In non-opcache, a request string may get a slot in map_ptr, and that interned request string
1322 	 * gets destroyed at the end of the request. The corresponding map_ptr slot can thereafter never be used again.
1323 	 * This causes map_ptr to keep reallocating to larger and larger sizes.
1324 	 *
1325 	 * We solve it as follows:
1326 	 * We can check whether we had any interned request strings, which only happens in non-opcache.
1327 	 * If we have any, we reset map_ptr to the last permanent string.
1328 	 * We can't lose any permanent strings because of map_ptr's layout.
1329 	 */
1330 	if (zend_hash_num_elements(&CG(interned_strings)) > 0) {
1331 		zend_map_ptr_reset();
1332 	}
1333 
1334 #if GC_BENCH
1335 	gc_bench_print();
1336 #endif
1337 }
1338 /* }}} */
1339 
zend_message_dispatcher(zend_long message,const void * data)1340 ZEND_API void zend_message_dispatcher(zend_long message, const void *data) /* {{{ */
1341 {
1342 	if (zend_message_dispatcher_p) {
1343 		zend_message_dispatcher_p(message, data);
1344 	}
1345 }
1346 /* }}} */
1347 
zend_get_configuration_directive(zend_string * name)1348 ZEND_API zval *zend_get_configuration_directive(zend_string *name) /* {{{ */
1349 {
1350 	if (zend_get_configuration_directive_p) {
1351 		return zend_get_configuration_directive_p(name);
1352 	} else {
1353 		return NULL;
1354 	}
1355 }
1356 /* }}} */
1357 
1358 #define SAVE_STACK(stack) do { \
1359 		if (CG(stack).top) { \
1360 			memcpy(&stack, &CG(stack), sizeof(zend_stack)); \
1361 			CG(stack).top = CG(stack).max = 0; \
1362 			CG(stack).elements = NULL; \
1363 		} else { \
1364 			stack.top = 0; \
1365 		} \
1366 	} while (0)
1367 
1368 #define RESTORE_STACK(stack) do { \
1369 		if (stack.top) { \
1370 			zend_stack_destroy(&CG(stack)); \
1371 			memcpy(&CG(stack), &stack, sizeof(zend_stack)); \
1372 		} \
1373 	} while (0)
1374 
zend_error_zstr_at(int orig_type,zend_string * error_filename,uint32_t error_lineno,zend_string * message)1375 ZEND_API ZEND_COLD void zend_error_zstr_at(
1376 		int orig_type, zend_string *error_filename, uint32_t error_lineno, zend_string *message)
1377 {
1378 	zval params[4];
1379 	zval retval;
1380 	zval orig_user_error_handler;
1381 	bool in_compilation;
1382 	zend_class_entry *saved_class_entry;
1383 	zend_stack loop_var_stack;
1384 	zend_stack delayed_oplines_stack;
1385 	int type = orig_type & E_ALL;
1386 	bool orig_record_errors;
1387 	uint32_t orig_num_errors;
1388 	zend_error_info **orig_errors;
1389 	zend_result res;
1390 
1391 	/* If we're executing a function during SCCP, count any warnings that may be emitted,
1392 	 * but don't perform any other error handling. */
1393 	if (EG(capture_warnings_during_sccp)) {
1394 		ZEND_ASSERT(!(type & E_FATAL_ERRORS) && "Fatal error during SCCP");
1395 		EG(capture_warnings_during_sccp)++;
1396 		return;
1397 	}
1398 
1399 	if (EG(record_errors)) {
1400 		zend_error_info *info = emalloc(sizeof(zend_error_info));
1401 		info->type = type;
1402 		info->lineno = error_lineno;
1403 		info->filename = zend_string_copy(error_filename);
1404 		info->message = zend_string_copy(message);
1405 
1406 		/* This is very inefficient for a large number of errors.
1407 		 * Use pow2 realloc if it becomes a problem. */
1408 		EG(num_errors)++;
1409 		EG(errors) = erealloc(EG(errors), sizeof(zend_error_info*) * EG(num_errors));
1410 		EG(errors)[EG(num_errors)-1] = info;
1411 	}
1412 
1413 	/* Report about uncaught exception in case of fatal errors */
1414 	if (EG(exception)) {
1415 		zend_execute_data *ex;
1416 		const zend_op *opline;
1417 
1418 		if (type & E_FATAL_ERRORS) {
1419 			ex = EG(current_execute_data);
1420 			opline = NULL;
1421 			while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
1422 				ex = ex->prev_execute_data;
1423 			}
1424 			if (ex && ex->opline->opcode == ZEND_HANDLE_EXCEPTION &&
1425 			    EG(opline_before_exception)) {
1426 				opline = EG(opline_before_exception);
1427 			}
1428 			zend_exception_error(EG(exception), E_WARNING);
1429 			EG(exception) = NULL;
1430 			if (opline) {
1431 				ex->opline = opline;
1432 			}
1433 		}
1434 	}
1435 
1436 	zend_observer_error_notify(type, error_filename, error_lineno, message);
1437 
1438 	/* if we don't have a user defined error handler */
1439 	if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF
1440 		|| !(EG(user_error_handler_error_reporting) & type)
1441 		|| EG(error_handling) != EH_NORMAL) {
1442 		zend_error_cb(orig_type, error_filename, error_lineno, message);
1443 	} else switch (type) {
1444 		case E_ERROR:
1445 		case E_PARSE:
1446 		case E_CORE_ERROR:
1447 		case E_CORE_WARNING:
1448 		case E_COMPILE_ERROR:
1449 		case E_COMPILE_WARNING:
1450 			/* The error may not be safe to handle in user-space */
1451 			zend_error_cb(orig_type, error_filename, error_lineno, message);
1452 			break;
1453 		default:
1454 			/* Handle the error in user space */
1455 			ZVAL_STR_COPY(&params[1], message);
1456 			ZVAL_LONG(&params[0], type);
1457 
1458 			if (error_filename) {
1459 				ZVAL_STR_COPY(&params[2], error_filename);
1460 			} else {
1461 				ZVAL_NULL(&params[2]);
1462 			}
1463 
1464 			ZVAL_LONG(&params[3], error_lineno);
1465 
1466 			ZVAL_COPY_VALUE(&orig_user_error_handler, &EG(user_error_handler));
1467 			ZVAL_UNDEF(&EG(user_error_handler));
1468 
1469 			/* User error handler may include() additional PHP files.
1470 			 * If an error was generated during compilation PHP will compile
1471 			 * such scripts recursively, but some CG() variables may be
1472 			 * inconsistent. */
1473 
1474 			in_compilation = CG(in_compilation);
1475 			if (in_compilation) {
1476 				saved_class_entry = CG(active_class_entry);
1477 				CG(active_class_entry) = NULL;
1478 				SAVE_STACK(loop_var_stack);
1479 				SAVE_STACK(delayed_oplines_stack);
1480 				CG(in_compilation) = 0;
1481 			}
1482 
1483 			orig_record_errors = EG(record_errors);
1484 			orig_num_errors = EG(num_errors);
1485 			orig_errors = EG(errors);
1486 			EG(record_errors) = false;
1487 			EG(num_errors) = 0;
1488 			EG(errors) = NULL;
1489 
1490 			res = call_user_function(CG(function_table), NULL, &orig_user_error_handler, &retval, 4, params);
1491 
1492 			EG(record_errors) = orig_record_errors;
1493 			EG(num_errors) = orig_num_errors;
1494 			EG(errors) = orig_errors;
1495 
1496 			if (res == SUCCESS) {
1497 				if (Z_TYPE(retval) != IS_UNDEF) {
1498 					if (Z_TYPE(retval) == IS_FALSE) {
1499 						zend_error_cb(orig_type, error_filename, error_lineno, message);
1500 					}
1501 					zval_ptr_dtor(&retval);
1502 				}
1503 			} else if (!EG(exception)) {
1504 				/* The user error handler failed, use built-in error handler */
1505 				zend_error_cb(orig_type, error_filename, error_lineno, message);
1506 			}
1507 
1508 			if (in_compilation) {
1509 				CG(active_class_entry) = saved_class_entry;
1510 				RESTORE_STACK(loop_var_stack);
1511 				RESTORE_STACK(delayed_oplines_stack);
1512 				CG(in_compilation) = 1;
1513 			}
1514 
1515 			zval_ptr_dtor(&params[2]);
1516 			zval_ptr_dtor(&params[1]);
1517 
1518 			if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF) {
1519 				ZVAL_COPY_VALUE(&EG(user_error_handler), &orig_user_error_handler);
1520 			} else {
1521 				zval_ptr_dtor(&orig_user_error_handler);
1522 			}
1523 			break;
1524 	}
1525 
1526 	if (type == E_PARSE) {
1527 		/* eval() errors do not affect exit_status */
1528 		if (!(EG(current_execute_data) &&
1529 			EG(current_execute_data)->func &&
1530 			ZEND_USER_CODE(EG(current_execute_data)->func->type) &&
1531 			EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL &&
1532 			EG(current_execute_data)->opline->extended_value == ZEND_EVAL)) {
1533 			EG(exit_status) = 255;
1534 		}
1535 	}
1536 }
1537 /* }}} */
1538 
zend_error_va_list(int orig_type,zend_string * error_filename,uint32_t error_lineno,const char * format,va_list args)1539 static ZEND_COLD void zend_error_va_list(
1540 		int orig_type, zend_string *error_filename, uint32_t error_lineno,
1541 		const char *format, va_list args)
1542 {
1543 	zend_string *message = zend_vstrpprintf(0, format, args);
1544 	zend_error_zstr_at(orig_type, error_filename, error_lineno, message);
1545 	zend_string_release(message);
1546 }
1547 
get_filename_lineno(int type,zend_string ** filename,uint32_t * lineno)1548 static ZEND_COLD void get_filename_lineno(int type, zend_string **filename, uint32_t *lineno) {
1549 	/* Obtain relevant filename and lineno */
1550 	switch (type) {
1551 		case E_CORE_ERROR:
1552 		case E_CORE_WARNING:
1553 			*filename = NULL;
1554 			*lineno = 0;
1555 			break;
1556 		case E_PARSE:
1557 		case E_COMPILE_ERROR:
1558 		case E_COMPILE_WARNING:
1559 		case E_ERROR:
1560 		case E_NOTICE:
1561 		case E_STRICT:
1562 		case E_DEPRECATED:
1563 		case E_WARNING:
1564 		case E_USER_ERROR:
1565 		case E_USER_WARNING:
1566 		case E_USER_NOTICE:
1567 		case E_USER_DEPRECATED:
1568 		case E_RECOVERABLE_ERROR:
1569 			if (zend_is_compiling()) {
1570 				*filename = zend_get_compiled_filename();
1571 				*lineno = zend_get_compiled_lineno();
1572 			} else if (zend_is_executing()) {
1573 				*filename = zend_get_executed_filename_ex();
1574 				*lineno = zend_get_executed_lineno();
1575 			} else {
1576 				*filename = NULL;
1577 				*lineno = 0;
1578 			}
1579 			break;
1580 		default:
1581 			*filename = NULL;
1582 			*lineno = 0;
1583 			break;
1584 	}
1585 	if (!*filename) {
1586 		*filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED);
1587 	}
1588 }
1589 
zend_error_at(int type,zend_string * filename,uint32_t lineno,const char * format,...)1590 ZEND_API ZEND_COLD void zend_error_at(
1591 		int type, zend_string *filename, uint32_t lineno, const char *format, ...) {
1592 	va_list args;
1593 
1594 	if (!filename) {
1595 		uint32_t dummy_lineno;
1596 		get_filename_lineno(type, &filename, &dummy_lineno);
1597 	}
1598 
1599 	va_start(args, format);
1600 	zend_error_va_list(type, filename, lineno, format, args);
1601 	va_end(args);
1602 }
1603 
zend_error(int type,const char * format,...)1604 ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) {
1605 	zend_string *filename;
1606 	uint32_t lineno;
1607 	va_list args;
1608 
1609 	get_filename_lineno(type, &filename, &lineno);
1610 	va_start(args, format);
1611 	zend_error_va_list(type, filename, lineno, format, args);
1612 	va_end(args);
1613 }
1614 
zend_error_unchecked(int type,const char * format,...)1615 ZEND_API ZEND_COLD void zend_error_unchecked(int type, const char *format, ...) {
1616 	zend_string *filename;
1617 	uint32_t lineno;
1618 	va_list args;
1619 
1620 	get_filename_lineno(type, &filename, &lineno);
1621 	va_start(args, format);
1622 	zend_error_va_list(type, filename, lineno, format, args);
1623 	va_end(args);
1624 }
1625 
zend_error_at_noreturn(int type,zend_string * filename,uint32_t lineno,const char * format,...)1626 ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_at_noreturn(
1627 		int type, zend_string *filename, uint32_t lineno, const char *format, ...)
1628 {
1629 	va_list args;
1630 
1631 	if (!filename) {
1632 		uint32_t dummy_lineno;
1633 		get_filename_lineno(type, &filename, &dummy_lineno);
1634 	}
1635 
1636 	va_start(args, format);
1637 	zend_error_va_list(type, filename, lineno, format, args);
1638 	va_end(args);
1639 	/* Should never reach this. */
1640 	abort();
1641 }
1642 
zend_error_noreturn(int type,const char * format,...)1643 ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...)
1644 {
1645 	zend_string *filename;
1646 	uint32_t lineno;
1647 	va_list args;
1648 
1649 	get_filename_lineno(type, &filename, &lineno);
1650 	va_start(args, format);
1651 	zend_error_va_list(type, filename, lineno, format, args);
1652 	va_end(args);
1653 	/* Should never reach this. */
1654 	abort();
1655 }
1656 
zend_strerror_noreturn(int type,int errn,const char * message)1657 ZEND_API ZEND_COLD ZEND_NORETURN void zend_strerror_noreturn(int type, int errn, const char *message)
1658 {
1659 #ifdef HAVE_STRERROR_R
1660 	char b[1024];
1661 
1662 # ifdef STRERROR_R_CHAR_P
1663 	char *buf = strerror_r(errn, b, sizeof(b));
1664 # else
1665 	strerror_r(errn, b, sizeof(b));
1666 	char *buf = b;
1667 # endif
1668 #else
1669 	char *buf = strerror(errn);
1670 #endif
1671 
1672 	zend_error_noreturn(type, "%s: %s (%d)", message, buf, errn);
1673 }
1674 
zend_error_zstr(int type,zend_string * message)1675 ZEND_API ZEND_COLD void zend_error_zstr(int type, zend_string *message) {
1676 	zend_string *filename;
1677 	uint32_t lineno;
1678 	get_filename_lineno(type, &filename, &lineno);
1679 	zend_error_zstr_at(type, filename, lineno, message);
1680 }
1681 
zend_begin_record_errors(void)1682 ZEND_API void zend_begin_record_errors(void)
1683 {
1684 	ZEND_ASSERT(!EG(record_errors) && "Error recording already enabled");
1685 	EG(record_errors) = true;
1686 	EG(num_errors) = 0;
1687 	EG(errors) = NULL;
1688 }
1689 
zend_emit_recorded_errors(void)1690 ZEND_API void zend_emit_recorded_errors(void)
1691 {
1692 	EG(record_errors) = false;
1693 	for (uint32_t i = 0; i < EG(num_errors); i++) {
1694 		zend_error_info *error = EG(errors)[i];
1695 		zend_error_zstr_at(error->type, error->filename, error->lineno, error->message);
1696 	}
1697 }
1698 
zend_free_recorded_errors(void)1699 ZEND_API void zend_free_recorded_errors(void)
1700 {
1701 	if (!EG(num_errors)) {
1702 		return;
1703 	}
1704 
1705 	for (uint32_t i = 0; i < EG(num_errors); i++) {
1706 		zend_error_info *info = EG(errors)[i];
1707 		zend_string_release(info->filename);
1708 		zend_string_release(info->message);
1709 		efree(info);
1710 	}
1711 	efree(EG(errors));
1712 	EG(errors) = NULL;
1713 	EG(num_errors) = 0;
1714 }
1715 
zend_throw_error(zend_class_entry * exception_ce,const char * format,...)1716 ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format, ...) /* {{{ */
1717 {
1718 	va_list va;
1719 	char *message = NULL;
1720 
1721 	if (!exception_ce) {
1722 		exception_ce = zend_ce_error;
1723 	}
1724 
1725 	/* Marker used to disable exception generation during preloading. */
1726 	if (EG(exception) == (void*)(uintptr_t)-1) {
1727 		return;
1728 	}
1729 
1730 	va_start(va, format);
1731 	zend_vspprintf(&message, 0, format, va);
1732 
1733 	//TODO: we can't convert compile-time errors to exceptions yet???
1734 	if (EG(current_execute_data) && !CG(in_compilation)) {
1735 		zend_throw_exception(exception_ce, message, 0);
1736 	} else {
1737 		zend_error(E_ERROR, "%s", message);
1738 	}
1739 
1740 	efree(message);
1741 	va_end(va);
1742 }
1743 /* }}} */
1744 
1745 /* 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)1746 ZEND_API ZEND_COLD void zend_illegal_container_offset(const zend_string *container, const zval *offset, int type)
1747 {
1748 	switch (type) {
1749 		case BP_VAR_IS:
1750 			zend_type_error("Cannot access offset of type %s in isset or empty",
1751 				zend_zval_type_name(offset));
1752 			return;
1753 		case BP_VAR_UNSET:
1754 			/* Consistent error for when trying to unset a string offset */
1755 			if (zend_string_equals(container, ZSTR_KNOWN(ZEND_STR_STRING))) {
1756 				zend_throw_error(NULL, "Cannot unset string offsets");
1757 			} else {
1758 				zend_type_error("Cannot unset offset of type %s on %s", zend_zval_type_name(offset), ZSTR_VAL(container));
1759 			}
1760 			return;
1761 		default:
1762 			zend_type_error("Cannot access offset of type %s on %s",
1763 				zend_zval_type_name(offset), ZSTR_VAL(container));
1764 			return;
1765 	}
1766 }
1767 
zend_type_error(const char * format,...)1768 ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) /* {{{ */
1769 {
1770 	va_list va;
1771 	char *message = NULL;
1772 
1773 	va_start(va, format);
1774 	zend_vspprintf(&message, 0, format, va);
1775 	zend_throw_exception(zend_ce_type_error, message, 0);
1776 	efree(message);
1777 	va_end(va);
1778 } /* }}} */
1779 
zend_argument_count_error(const char * format,...)1780 ZEND_API ZEND_COLD void zend_argument_count_error(const char *format, ...) /* {{{ */
1781 {
1782 	va_list va;
1783 	char *message = NULL;
1784 
1785 	va_start(va, format);
1786 	zend_vspprintf(&message, 0, format, va);
1787 	zend_throw_exception(zend_ce_argument_count_error, message, 0);
1788 	efree(message);
1789 
1790 	va_end(va);
1791 } /* }}} */
1792 
zend_value_error(const char * format,...)1793 ZEND_API ZEND_COLD void zend_value_error(const char *format, ...) /* {{{ */
1794 {
1795 	va_list va;
1796 	char *message = NULL;
1797 
1798 	va_start(va, format);
1799 	zend_vspprintf(&message, 0, format, va);
1800 	zend_throw_exception(zend_ce_value_error, message, 0);
1801 	efree(message);
1802 	va_end(va);
1803 } /* }}} */
1804 
zend_output_debug_string(bool trigger_break,const char * format,...)1805 ZEND_API ZEND_COLD void zend_output_debug_string(bool trigger_break, const char *format, ...) /* {{{ */
1806 {
1807 #if ZEND_DEBUG
1808 	va_list args;
1809 
1810 	va_start(args, format);
1811 #	ifdef ZEND_WIN32
1812 	{
1813 		char output_buf[1024];
1814 
1815 		vsnprintf(output_buf, 1024, format, args);
1816 		OutputDebugString(output_buf);
1817 		OutputDebugString("\n");
1818 		if (trigger_break && IsDebuggerPresent()) {
1819 			DebugBreak();
1820 		}
1821 	}
1822 #	else
1823 	vfprintf(stderr, format, args);
1824 	fprintf(stderr, "\n");
1825 #	endif
1826 	va_end(args);
1827 #endif
1828 }
1829 /* }}} */
1830 
zend_user_exception_handler(void)1831 ZEND_API ZEND_COLD void zend_user_exception_handler(void) /* {{{ */
1832 {
1833 	zval orig_user_exception_handler;
1834 	zval params[1], retval2;
1835 	zend_object *old_exception;
1836 
1837 	if (zend_is_unwind_exit(EG(exception))) {
1838 		return;
1839 	}
1840 
1841 	old_exception = EG(exception);
1842 	EG(exception) = NULL;
1843 	ZVAL_OBJ(&params[0], old_exception);
1844 
1845 	ZVAL_COPY_VALUE(&orig_user_exception_handler, &EG(user_exception_handler));
1846 	zend_stack_push(&EG(user_exception_handlers), &orig_user_exception_handler);
1847 	ZVAL_UNDEF(&EG(user_exception_handler));
1848 
1849 	if (call_user_function(CG(function_table), NULL, &orig_user_exception_handler, &retval2, 1, params) == SUCCESS) {
1850 		zval_ptr_dtor(&retval2);
1851 		if (EG(exception)) {
1852 			OBJ_RELEASE(EG(exception));
1853 			EG(exception) = NULL;
1854 		}
1855 		OBJ_RELEASE(old_exception);
1856 	} else {
1857 		EG(exception) = old_exception;
1858 	}
1859 
1860 	if (Z_TYPE(EG(user_exception_handler)) == IS_UNDEF) {
1861 		zval *tmp = zend_stack_top(&EG(user_exception_handlers));
1862 		if (tmp) {
1863 			ZVAL_COPY_VALUE(&EG(user_exception_handler), tmp);
1864 			zend_stack_del_top(&EG(user_exception_handlers));
1865 		}
1866 	}
1867 } /* }}} */
1868 
zend_execute_scripts(int type,zval * retval,int file_count,...)1869 ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count, ...) /* {{{ */
1870 {
1871 	va_list files;
1872 	int i;
1873 	zend_file_handle *file_handle;
1874 	zend_op_array *op_array;
1875 	zend_result ret = SUCCESS;
1876 
1877 	va_start(files, file_count);
1878 	for (i = 0; i < file_count; i++) {
1879 		file_handle = va_arg(files, zend_file_handle *);
1880 		if (!file_handle) {
1881 			continue;
1882 		}
1883 
1884 		if (ret == FAILURE) {
1885 			continue;
1886 		}
1887 
1888 		op_array = zend_compile_file(file_handle, type);
1889 		if (file_handle->opened_path) {
1890 			zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path);
1891 		}
1892 		if (op_array) {
1893 			zend_execute(op_array, retval);
1894 			zend_exception_restore();
1895 			if (UNEXPECTED(EG(exception))) {
1896 				if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1897 					zend_user_exception_handler();
1898 				}
1899 				if (EG(exception)) {
1900 					ret = zend_exception_error(EG(exception), E_ERROR);
1901 				}
1902 			}
1903 			zend_destroy_static_vars(op_array);
1904 			destroy_op_array(op_array);
1905 			efree_size(op_array, sizeof(zend_op_array));
1906 		} else if (type==ZEND_REQUIRE) {
1907 			ret = FAILURE;
1908 		}
1909 	}
1910 	va_end(files);
1911 
1912 	return ret;
1913 }
1914 /* }}} */
1915 
1916 #define COMPILED_STRING_DESCRIPTION_FORMAT "%s(%d) : %s"
1917 
zend_make_compiled_string_description(const char * name)1918 ZEND_API char *zend_make_compiled_string_description(const char *name) /* {{{ */
1919 {
1920 	const char *cur_filename;
1921 	int cur_lineno;
1922 	char *compiled_string_description;
1923 
1924 	if (zend_is_compiling()) {
1925 		cur_filename = ZSTR_VAL(zend_get_compiled_filename());
1926 		cur_lineno = zend_get_compiled_lineno();
1927 	} else if (zend_is_executing()) {
1928 		cur_filename = zend_get_executed_filename();
1929 		cur_lineno = zend_get_executed_lineno();
1930 	} else {
1931 		cur_filename = "Unknown";
1932 		cur_lineno = 0;
1933 	}
1934 
1935 	zend_spprintf(&compiled_string_description, 0, COMPILED_STRING_DESCRIPTION_FORMAT, cur_filename, cur_lineno, name);
1936 	return compiled_string_description;
1937 }
1938 /* }}} */
1939 
free_estring(char ** str_p)1940 void free_estring(char **str_p) /* {{{ */
1941 {
1942 	efree(*str_p);
1943 }
1944 /* }}} */
1945 
zend_map_ptr_reset(void)1946 ZEND_API void zend_map_ptr_reset(void)
1947 {
1948 	CG(map_ptr_last) = global_map_ptr_last;
1949 }
1950 
zend_map_ptr_new(void)1951 ZEND_API void *zend_map_ptr_new(void)
1952 {
1953 	void **ptr;
1954 
1955 	if (CG(map_ptr_last) >= CG(map_ptr_size)) {
1956 		/* Grow map_ptr table */
1957 		CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(CG(map_ptr_last) + 1, 4096);
1958 		CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), CG(map_ptr_size) * sizeof(void*), 1);
1959 		CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(CG(map_ptr_real_base));
1960 	}
1961 	ptr = (void**)CG(map_ptr_real_base) + CG(map_ptr_last);
1962 	*ptr = NULL;
1963 	CG(map_ptr_last)++;
1964 	return ZEND_MAP_PTR_PTR2OFFSET(ptr);
1965 }
1966 
zend_map_ptr_extend(size_t last)1967 ZEND_API void zend_map_ptr_extend(size_t last)
1968 {
1969 	if (last > CG(map_ptr_last)) {
1970 		void **ptr;
1971 
1972 		if (last >= CG(map_ptr_size)) {
1973 			/* Grow map_ptr table */
1974 			CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(last, 4096);
1975 			CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), CG(map_ptr_size) * sizeof(void*), 1);
1976 			CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(CG(map_ptr_real_base));
1977 		}
1978 		ptr = (void**)CG(map_ptr_real_base) + CG(map_ptr_last);
1979 		memset(ptr, 0, (last - CG(map_ptr_last)) * sizeof(void*));
1980 		CG(map_ptr_last) = last;
1981 	}
1982 }
1983 
zend_alloc_ce_cache(zend_string * type_name)1984 ZEND_API void zend_alloc_ce_cache(zend_string *type_name)
1985 {
1986 	if (ZSTR_HAS_CE_CACHE(type_name) || !ZSTR_IS_INTERNED(type_name)) {
1987 		return;
1988 	}
1989 
1990 	if ((GC_FLAGS(type_name) & IS_STR_PERMANENT) && startup_done) {
1991 		/* Don't allocate slot on permanent interned string outside module startup.
1992 		 * The cache slot would no longer be valid on the next request. */
1993 		return;
1994 	}
1995 
1996 	if (zend_string_equals_literal_ci(type_name, "self")
1997 			|| zend_string_equals_literal_ci(type_name, "parent")) {
1998 		return;
1999 	}
2000 
2001 	/* We use the refcount to keep map_ptr of corresponding type */
2002 	uint32_t ret;
2003 	do {
2004 		ret = ZEND_MAP_PTR_NEW_OFFSET();
2005 	} while (ret <= 2);
2006 	GC_ADD_FLAGS(type_name, IS_STR_CLASS_NAME_MAP_PTR);
2007 	GC_SET_REFCOUNT(type_name, ret);
2008 }
2009