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