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