xref: /PHP-7.0/Zend/zend.c (revision 73d6456d)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2017 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@zend.com>                                |
16    |          Zeev Suraski <zeev@zend.com>                                |
17    +----------------------------------------------------------------------+
18 */
19 
20 /* $Id$ */
21 
22 #include "zend.h"
23 #include "zend_extensions.h"
24 #include "zend_modules.h"
25 #include "zend_constants.h"
26 #include "zend_list.h"
27 #include "zend_API.h"
28 #include "zend_exceptions.h"
29 #include "zend_builtin_functions.h"
30 #include "zend_ini.h"
31 #include "zend_vm.h"
32 #include "zend_dtrace.h"
33 #include "zend_virtual_cwd.h"
34 
35 #ifdef ZTS
36 # define GLOBAL_FUNCTION_TABLE		global_function_table
37 # define GLOBAL_CLASS_TABLE			global_class_table
38 # define GLOBAL_CONSTANTS_TABLE		global_constants_table
39 # define GLOBAL_AUTO_GLOBALS_TABLE	global_auto_globals_table
40 #else
41 # define GLOBAL_FUNCTION_TABLE		CG(function_table)
42 # define GLOBAL_CLASS_TABLE			CG(class_table)
43 # define GLOBAL_AUTO_GLOBALS_TABLE	CG(auto_globals)
44 # define GLOBAL_CONSTANTS_TABLE		EG(zend_constants)
45 #endif
46 
47 /* true multithread-shared globals */
48 ZEND_API zend_class_entry *zend_standard_class_def = NULL;
49 ZEND_API size_t (*zend_printf)(const char *format, ...);
50 ZEND_API zend_write_func_t zend_write;
51 ZEND_API FILE *(*zend_fopen)(const char *filename, zend_string **opened_path);
52 ZEND_API int (*zend_stream_open_function)(const char *filename, zend_file_handle *handle);
53 ZEND_API void (*zend_block_interruptions)(void);
54 ZEND_API void (*zend_unblock_interruptions)(void);
55 ZEND_API void (*zend_ticks_function)(int ticks);
56 ZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args);
57 size_t (*zend_vspprintf)(char **pbuf, size_t max_len, const char *format, va_list ap);
58 zend_string *(*zend_vstrpprintf)(size_t max_len, const char *format, va_list ap);
59 ZEND_API char *(*zend_getenv)(char *name, size_t name_len);
60 ZEND_API zend_string *(*zend_resolve_path)(const char *filename, int filename_len);
61 
62 void (*zend_on_timeout)(int seconds);
63 
64 static void (*zend_message_dispatcher_p)(zend_long message, const void *data);
65 static zval *(*zend_get_configuration_directive_p)(zend_string *name);
66 
ZEND_INI_MH(OnUpdateErrorReporting)67 static ZEND_INI_MH(OnUpdateErrorReporting) /* {{{ */
68 {
69 	if (!new_value) {
70 		EG(error_reporting) = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED;
71 	} else {
72 		EG(error_reporting) = atoi(ZSTR_VAL(new_value));
73 	}
74 	return SUCCESS;
75 }
76 /* }}} */
77 
ZEND_INI_MH(OnUpdateGCEnabled)78 static ZEND_INI_MH(OnUpdateGCEnabled) /* {{{ */
79 {
80 	OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
81 
82 	if (GC_G(gc_enabled)) {
83 		gc_init();
84 	}
85 
86 	return SUCCESS;
87 }
88 /* }}} */
89 
ZEND_INI_MH(OnUpdateScriptEncoding)90 static ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */
91 {
92 	if (!CG(multibyte)) {
93 		return FAILURE;
94 	}
95 	if (!zend_multibyte_get_functions()) {
96 		return SUCCESS;
97 	}
98 	return zend_multibyte_set_script_encoding_by_string(new_value ? ZSTR_VAL(new_value) : NULL, new_value ? ZSTR_LEN(new_value) : 0);
99 }
100 /* }}} */
101 
ZEND_INI_MH(OnUpdateAssertions)102 static ZEND_INI_MH(OnUpdateAssertions) /* {{{ */
103 {
104 	zend_long *p, val;
105 #ifndef ZTS
106 	char *base = (char *) mh_arg2;
107 #else
108 	char *base;
109 
110 	base = (char *) ts_resource(*((int *) mh_arg2));
111 #endif
112 
113 	p = (zend_long *) (base+(size_t) mh_arg1);
114 
115 	val = zend_atol(ZSTR_VAL(new_value), (int)ZSTR_LEN(new_value));
116 
117 	if (stage != ZEND_INI_STAGE_STARTUP &&
118 	    stage != ZEND_INI_STAGE_SHUTDOWN &&
119 	    *p != val &&
120 	    (*p < 0 || val < 0)) {
121 		zend_error(E_WARNING, "zend.assertions may be completely enabled or disabled only in php.ini");
122 		return FAILURE;
123 	}
124 
125 	*p = val;
126 	return SUCCESS;
127 }
128 /* }}} */
129 
130 ZEND_INI_BEGIN()
131 	ZEND_INI_ENTRY("error_reporting",				NULL,		ZEND_INI_ALL,		OnUpdateErrorReporting)
132 	STD_ZEND_INI_ENTRY("zend.assertions",				"1",    ZEND_INI_ALL,       OnUpdateAssertions,           assertions,   zend_executor_globals,  executor_globals)
133 	STD_ZEND_INI_BOOLEAN("zend.enable_gc",				"1",	ZEND_INI_ALL,		OnUpdateGCEnabled,      gc_enabled,     zend_gc_globals,        gc_globals)
134  	STD_ZEND_INI_BOOLEAN("zend.multibyte", "0", ZEND_INI_PERDIR, OnUpdateBool, multibyte,      zend_compiler_globals, compiler_globals)
135  	ZEND_INI_ENTRY("zend.script_encoding",			NULL,		ZEND_INI_ALL,		OnUpdateScriptEncoding)
136  	STD_ZEND_INI_BOOLEAN("zend.detect_unicode",			"1",	ZEND_INI_ALL,		OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals)
137 #ifdef ZEND_SIGNALS
138 	STD_ZEND_INI_BOOLEAN("zend.signal_check", "0", ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals)
139 #endif
140 ZEND_INI_END()
141 
142 
143 #ifdef ZTS
144 ZEND_API int compiler_globals_id;
145 ZEND_API int executor_globals_id;
146 static HashTable *global_function_table = NULL;
147 static HashTable *global_class_table = NULL;
148 static HashTable *global_constants_table = NULL;
149 static HashTable *global_auto_globals_table = NULL;
150 static HashTable *global_persistent_list = NULL;
151 ZEND_TSRMLS_CACHE_DEFINE()
152 #endif
153 
154 ZEND_API zend_utility_values zend_uv;
155 ZEND_API zend_bool zend_dtrace_enabled;
156 
157 /* version information */
158 static char *zend_version_info;
159 static uint zend_version_info_length;
160 #define ZEND_CORE_VERSION_INFO	"Zend Engine v" ZEND_VERSION ", Copyright (c) 1998-2017 Zend Technologies\n"
161 #define PRINT_ZVAL_INDENT 4
162 
print_hash(zend_write_func_t write_func,HashTable * ht,int indent,zend_bool is_object)163 static void print_hash(zend_write_func_t write_func, HashTable *ht, int indent, zend_bool is_object) /* {{{ */
164 {
165 	zval *tmp;
166 	zend_string *string_key;
167 	zend_ulong num_key;
168 	int i;
169 
170 	for (i = 0; i < indent; i++) {
171 		ZEND_PUTS_EX(" ");
172 	}
173 	ZEND_PUTS_EX("(\n");
174 	indent += PRINT_ZVAL_INDENT;
175 	ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, string_key, tmp) {
176 		if (Z_TYPE_P(tmp) == IS_INDIRECT) {
177 			tmp = Z_INDIRECT_P(tmp);
178 			if (Z_TYPE_P(tmp) == IS_UNDEF) {
179 				continue;
180 			}
181 		}
182 		for (i = 0; i < indent; i++) {
183 			ZEND_PUTS_EX(" ");
184 		}
185 		ZEND_PUTS_EX("[");
186 		if (string_key) {
187 			if (is_object) {
188 				const char *prop_name, *class_name;
189 				size_t prop_len;
190 				int mangled = zend_unmangle_property_name_ex(string_key, &class_name, &prop_name, &prop_len);
191 
192 				ZEND_WRITE_EX(prop_name, prop_len);
193 				if (class_name && mangled == SUCCESS) {
194 					if (class_name[0]=='*') {
195 						ZEND_PUTS_EX(":protected");
196 					} else {
197 						ZEND_PUTS_EX(":");
198 						ZEND_PUTS_EX(class_name);
199 						ZEND_PUTS_EX(":private");
200 					}
201 				}
202 			} else {
203 				ZEND_WRITE_EX(ZSTR_VAL(string_key), ZSTR_LEN(string_key));
204 			}
205 		} else {
206 			char key[25];
207 			snprintf(key, sizeof(key), ZEND_LONG_FMT, num_key);
208 			ZEND_PUTS_EX(key);
209 		}
210 		ZEND_PUTS_EX("] => ");
211 		zend_print_zval_r_ex(write_func, tmp, indent+PRINT_ZVAL_INDENT);
212 		ZEND_PUTS_EX("\n");
213 	} ZEND_HASH_FOREACH_END();
214 	indent -= PRINT_ZVAL_INDENT;
215 	for (i = 0; i < indent; i++) {
216 		ZEND_PUTS_EX(" ");
217 	}
218 	ZEND_PUTS_EX(")\n");
219 }
220 /* }}} */
221 
print_flat_hash(HashTable * ht)222 static void print_flat_hash(HashTable *ht) /* {{{ */
223 {
224 	zval *tmp;
225 	zend_string *string_key;
226 	zend_ulong num_key;
227 	int i = 0;
228 
229 	ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) {
230 		if (i++ > 0) {
231 			ZEND_PUTS(",");
232 		}
233 		ZEND_PUTS("[");
234 		if (string_key) {
235 			ZEND_WRITE(ZSTR_VAL(string_key), ZSTR_LEN(string_key));
236 		} else {
237 			zend_printf(ZEND_ULONG_FMT, num_key);
238 		}
239 		ZEND_PUTS("] => ");
240 		zend_print_flat_zval_r(tmp);
241 	} ZEND_HASH_FOREACH_END();
242 }
243 /* }}} */
244 
zend_make_printable_zval(zval * expr,zval * expr_copy)245 ZEND_API int zend_make_printable_zval(zval *expr, zval *expr_copy) /* {{{ */
246 {
247 	if (Z_TYPE_P(expr) == IS_STRING) {
248 		return 0;
249 	} else {
250 		ZVAL_STR(expr_copy, _zval_get_string_func(expr));
251 		return 1;
252 	}
253 }
254 /* }}} */
255 
zend_print_zval(zval * expr,int indent)256 ZEND_API size_t zend_print_zval(zval *expr, int indent) /* {{{ */
257 {
258 	return zend_print_zval_ex(zend_write, expr, indent);
259 }
260 /* }}} */
261 
zend_print_zval_ex(zend_write_func_t write_func,zval * expr,int indent)262 ZEND_API size_t zend_print_zval_ex(zend_write_func_t write_func, zval *expr, int indent) /* {{{ */
263 {
264 	zend_string *str = zval_get_string(expr);
265 	size_t len = ZSTR_LEN(str);
266 
267 	if (len != 0) {
268 		write_func(ZSTR_VAL(str), len);
269 	}
270 
271 	zend_string_release(str);
272 	return len;
273 }
274 /* }}} */
275 
zend_print_flat_zval_r(zval * expr)276 ZEND_API void zend_print_flat_zval_r(zval *expr) /* {{{ */
277 {
278 	switch (Z_TYPE_P(expr)) {
279 		case IS_ARRAY:
280 			ZEND_PUTS("Array (");
281 			if (ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(expr)) &&
282 			    ++Z_ARRVAL_P(expr)->u.v.nApplyCount>1) {
283 				ZEND_PUTS(" *RECURSION*");
284 				Z_ARRVAL_P(expr)->u.v.nApplyCount--;
285 				return;
286 			}
287 			print_flat_hash(Z_ARRVAL_P(expr));
288 			ZEND_PUTS(")");
289 			if (ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(expr))) {
290 				Z_ARRVAL_P(expr)->u.v.nApplyCount--;
291 			}
292 			break;
293 		case IS_OBJECT:
294 		{
295 			HashTable *properties = NULL;
296 			zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr));
297 			zend_printf("%s Object (", ZSTR_VAL(class_name));
298 			zend_string_release(class_name);
299 
300 			if (Z_OBJ_APPLY_COUNT_P(expr) > 0) {
301 				ZEND_PUTS(" *RECURSION*");
302 				return;
303 			}
304 
305 			if (Z_OBJ_HANDLER_P(expr, get_properties)) {
306 				properties = Z_OBJPROP_P(expr);
307 			}
308 			if (properties) {
309 				Z_OBJ_INC_APPLY_COUNT_P(expr);
310 				print_flat_hash(properties);
311 				Z_OBJ_DEC_APPLY_COUNT_P(expr);
312 			}
313 			ZEND_PUTS(")");
314 			break;
315 		}
316 		case IS_REFERENCE:
317 			zend_print_flat_zval_r(Z_REFVAL_P(expr));
318 			break;
319 		default:
320 			zend_print_variable(expr);
321 			break;
322 	}
323 }
324 /* }}} */
325 
zend_print_zval_r(zval * expr,int indent)326 ZEND_API void zend_print_zval_r(zval *expr, int indent) /* {{{ */
327 {
328 	zend_print_zval_r_ex(zend_write, expr, indent);
329 }
330 /* }}} */
331 
zend_print_zval_r_ex(zend_write_func_t write_func,zval * expr,int indent)332 ZEND_API void zend_print_zval_r_ex(zend_write_func_t write_func, zval *expr, int indent) /* {{{ */
333 {
334 	ZVAL_DEREF(expr);
335 	switch (Z_TYPE_P(expr)) {
336 		case IS_ARRAY:
337 			ZEND_PUTS_EX("Array\n");
338 			if (ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(expr)) &&
339 			    ++Z_ARRVAL_P(expr)->u.v.nApplyCount>1) {
340 				ZEND_PUTS_EX(" *RECURSION*");
341 				Z_ARRVAL_P(expr)->u.v.nApplyCount--;
342 				return;
343 			}
344 			print_hash(write_func, Z_ARRVAL_P(expr), indent, 0);
345 			if (ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(expr))) {
346 				Z_ARRVAL_P(expr)->u.v.nApplyCount--;
347 			}
348 			break;
349 		case IS_OBJECT:
350 			{
351 				HashTable *properties;
352 				int is_temp;
353 
354 				zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr));
355 				ZEND_PUTS_EX(ZSTR_VAL(class_name));
356 				zend_string_release(class_name);
357 
358 				ZEND_PUTS_EX(" Object\n");
359 				if (Z_OBJ_APPLY_COUNT_P(expr) > 0) {
360 					ZEND_PUTS_EX(" *RECURSION*");
361 					return;
362 				}
363 				if ((properties = Z_OBJDEBUG_P(expr, is_temp)) == NULL) {
364 					break;
365 				}
366 
367 				Z_OBJ_INC_APPLY_COUNT_P(expr);
368 				print_hash(write_func, properties, indent, 1);
369 				Z_OBJ_DEC_APPLY_COUNT_P(expr);
370 
371 				if (is_temp) {
372 					zend_hash_destroy(properties);
373 					FREE_HASHTABLE(properties);
374 				}
375 				break;
376 			}
377 		default:
378 			zend_print_zval_ex(write_func, expr, indent);
379 			break;
380 	}
381 }
382 /* }}} */
383 
zend_fopen_wrapper(const char * filename,zend_string ** opened_path)384 static FILE *zend_fopen_wrapper(const char *filename, zend_string **opened_path) /* {{{ */
385 {
386 	if (opened_path) {
387 		*opened_path = zend_string_init(filename, strlen(filename), 0);
388 	}
389 	return fopen(filename, "rb");
390 }
391 /* }}} */
392 
393 #ifdef ZTS
394 static zend_bool short_tags_default      = 1;
395 static uint32_t compiler_options_default = ZEND_COMPILE_DEFAULT;
396 #else
397 # define short_tags_default			1
398 # define compiler_options_default	ZEND_COMPILE_DEFAULT
399 #endif
400 
zend_set_default_compile_time_values(void)401 static void zend_set_default_compile_time_values(void) /* {{{ */
402 {
403 	/* default compile-time values */
404 	CG(short_tags) = short_tags_default;
405 	CG(compiler_options) = compiler_options_default;
406 }
407 /* }}} */
408 
409 #ifdef ZEND_WIN32
zend_get_windows_version_info(OSVERSIONINFOEX * osvi)410 static void zend_get_windows_version_info(OSVERSIONINFOEX *osvi) /* {{{ */
411 {
412 	ZeroMemory(osvi, sizeof(OSVERSIONINFOEX));
413 	osvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
414 	if(!GetVersionEx((OSVERSIONINFO *) osvi)) {
415 		ZEND_ASSERT(0); /* Should not happen as sizeof is used. */
416 	}
417 }
418 /* }}} */
419 #endif
420 
zend_init_exception_op(void)421 static void zend_init_exception_op(void) /* {{{ */
422 {
423 	memset(EG(exception_op), 0, sizeof(EG(exception_op)));
424 	EG(exception_op)[0].opcode = ZEND_HANDLE_EXCEPTION;
425 	EG(exception_op)[0].op1_type = IS_UNUSED;
426 	EG(exception_op)[0].op2_type = IS_UNUSED;
427 	EG(exception_op)[0].result_type = IS_UNUSED;
428 	ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op));
429 	EG(exception_op)[1].opcode = ZEND_HANDLE_EXCEPTION;
430 	EG(exception_op)[1].op1_type = IS_UNUSED;
431 	EG(exception_op)[1].op2_type = IS_UNUSED;
432 	EG(exception_op)[1].result_type = IS_UNUSED;
433 	ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+1);
434 	EG(exception_op)[2].opcode = ZEND_HANDLE_EXCEPTION;
435 	EG(exception_op)[2].op1_type = IS_UNUSED;
436 	EG(exception_op)[2].op2_type = IS_UNUSED;
437 	EG(exception_op)[2].result_type = IS_UNUSED;
438 	ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+2);
439 }
440 /* }}} */
441 
zend_init_call_trampoline_op(void)442 static void zend_init_call_trampoline_op(void) /* {{{ */
443 {
444 	memset(&EG(call_trampoline_op), 0, sizeof(EG(call_trampoline_op)));
445 	EG(call_trampoline_op).opcode = ZEND_CALL_TRAMPOLINE;
446 	EG(call_trampoline_op).op1_type = IS_UNUSED;
447 	EG(call_trampoline_op).op2_type = IS_UNUSED;
448 	EG(call_trampoline_op).result_type = IS_UNUSED;
449 	ZEND_VM_SET_OPCODE_HANDLER(&EG(call_trampoline_op));
450 }
451 /* }}} */
452 
auto_global_dtor(zval * zv)453 static void auto_global_dtor(zval *zv) /* {{{ */
454 {
455 	free(Z_PTR_P(zv));
456 }
457 /* }}} */
458 
459 #ifdef ZTS
function_copy_ctor(zval * zv)460 static void function_copy_ctor(zval *zv) /* {{{ */
461 {
462 	zend_function *old_func = Z_FUNC_P(zv);
463 	Z_FUNC_P(zv) = pemalloc(sizeof(zend_internal_function), 1);
464 	memcpy(Z_FUNC_P(zv), old_func, sizeof(zend_internal_function));
465 	function_add_ref(Z_FUNC_P(zv));
466 }
467 /* }}} */
468 
auto_global_copy_ctor(zval * zv)469 static void auto_global_copy_ctor(zval *zv) /* {{{ */
470 {
471 	zend_auto_global *old_ag = (zend_auto_global *) Z_PTR_P(zv);
472 	zend_auto_global *new_ag = pemalloc(sizeof(zend_auto_global), 1);
473 
474 	new_ag->name = old_ag->name;
475 	new_ag->auto_global_callback = old_ag->auto_global_callback;
476 	new_ag->jit = old_ag->jit;
477 
478 	Z_PTR_P(zv) = new_ag;
479 }
480 /* }}} */
481 
compiler_globals_ctor(zend_compiler_globals * compiler_globals)482 static void compiler_globals_ctor(zend_compiler_globals *compiler_globals) /* {{{ */
483 {
484 	compiler_globals->compiled_filename = NULL;
485 
486 	compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
487 	zend_hash_init_ex(compiler_globals->function_table, 1024, NULL, ZEND_FUNCTION_DTOR, 1, 0);
488 	zend_hash_copy(compiler_globals->function_table, global_function_table, function_copy_ctor);
489 
490 	compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
491 	zend_hash_init_ex(compiler_globals->class_table, 64, NULL, ZEND_CLASS_DTOR, 1, 0);
492 	zend_hash_copy(compiler_globals->class_table, global_class_table, zend_class_add_ref);
493 
494 	zend_set_default_compile_time_values();
495 
496 	compiler_globals->auto_globals = (HashTable *) malloc(sizeof(HashTable));
497 	zend_hash_init_ex(compiler_globals->auto_globals, 8, NULL, auto_global_dtor, 1, 0);
498 	zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, auto_global_copy_ctor);
499 
500 	compiler_globals->last_static_member = zend_hash_num_elements(compiler_globals->class_table);
501 	if (compiler_globals->last_static_member) {
502 		compiler_globals->static_members_table = calloc(compiler_globals->last_static_member, sizeof(zval*));
503 	} else {
504 		compiler_globals->static_members_table = NULL;
505 	}
506 	compiler_globals->script_encoding_list = NULL;
507 
508 	zend_interned_empty_string_init(&compiler_globals->empty_string);
509 
510 	memset(compiler_globals->one_char_string, 0, sizeof(compiler_globals->one_char_string));
511 }
512 /* }}} */
513 
compiler_globals_dtor(zend_compiler_globals * compiler_globals)514 static void compiler_globals_dtor(zend_compiler_globals *compiler_globals) /* {{{ */
515 {
516 	if (compiler_globals->function_table != GLOBAL_FUNCTION_TABLE) {
517 		zend_hash_destroy(compiler_globals->function_table);
518 		free(compiler_globals->function_table);
519 	}
520 	if (compiler_globals->class_table != GLOBAL_CLASS_TABLE) {
521 		zend_hash_destroy(compiler_globals->class_table);
522 		free(compiler_globals->class_table);
523 	}
524 	if (compiler_globals->auto_globals != GLOBAL_AUTO_GLOBALS_TABLE) {
525 		zend_hash_destroy(compiler_globals->auto_globals);
526 		free(compiler_globals->auto_globals);
527 	}
528 	if (compiler_globals->static_members_table) {
529 		free(compiler_globals->static_members_table);
530 	}
531 	if (compiler_globals->script_encoding_list) {
532 		pefree((char*)compiler_globals->script_encoding_list, 1);
533 	}
534 	compiler_globals->last_static_member = 0;
535 
536 	zend_interned_empty_string_free(&compiler_globals->empty_string);
537 }
538 /* }}} */
539 
executor_globals_ctor(zend_executor_globals * executor_globals)540 static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{{ */
541 {
542 	ZEND_TSRMLS_CACHE_UPDATE();
543 
544 	zend_startup_constants();
545 	zend_copy_constants(EG(zend_constants), GLOBAL_CONSTANTS_TABLE);
546 	zend_init_rsrc_plist();
547 	zend_init_exception_op();
548 	zend_init_call_trampoline_op();
549 	memset(&executor_globals->trampoline, 0, sizeof(zend_op_array));
550 	executor_globals->lambda_count = 0;
551 	ZVAL_UNDEF(&executor_globals->user_error_handler);
552 	ZVAL_UNDEF(&executor_globals->user_exception_handler);
553 	executor_globals->in_autoload = NULL;
554 	executor_globals->current_execute_data = NULL;
555 	executor_globals->current_module = NULL;
556 	executor_globals->exit_status = 0;
557 #if XPFPA_HAVE_CW
558 	executor_globals->saved_fpu_cw = 0;
559 #endif
560 	executor_globals->saved_fpu_cw_ptr = NULL;
561 	executor_globals->active = 0;
562 	executor_globals->bailout = NULL;
563 	executor_globals->error_handling  = EH_NORMAL;
564 	executor_globals->exception_class = NULL;
565 	executor_globals->exception = NULL;
566 	executor_globals->objects_store.object_buckets = NULL;
567 #ifdef ZEND_WIN32
568 	zend_get_windows_version_info(&executor_globals->windows_version_info);
569 #endif
570 	executor_globals->valid_symbol_table = 0;
571 }
572 /* }}} */
573 
executor_globals_dtor(zend_executor_globals * executor_globals)574 static void executor_globals_dtor(zend_executor_globals *executor_globals) /* {{{ */
575 {
576 	zend_ini_dtor(executor_globals->ini_directives);
577 
578 	if (&executor_globals->persistent_list != global_persistent_list) {
579 		zend_destroy_rsrc_list(&executor_globals->persistent_list);
580 	}
581 	if (executor_globals->zend_constants != GLOBAL_CONSTANTS_TABLE) {
582 		zend_hash_destroy(executor_globals->zend_constants);
583 		free(executor_globals->zend_constants);
584 	}
585 }
586 /* }}} */
587 
zend_new_thread_end_handler(THREAD_T thread_id)588 static void zend_new_thread_end_handler(THREAD_T thread_id) /* {{{ */
589 {
590 	if (zend_copy_ini_directives() == SUCCESS) {
591 		zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP);
592 	}
593 }
594 /* }}} */
595 #endif
596 
597 #if defined(__FreeBSD__) || defined(__DragonFly__)
598 /* FreeBSD and DragonFly floating point precision fix */
599 #include <floatingpoint.h>
600 #endif
601 
ini_scanner_globals_ctor(zend_ini_scanner_globals * scanner_globals_p)602 static void ini_scanner_globals_ctor(zend_ini_scanner_globals *scanner_globals_p) /* {{{ */
603 {
604 	memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
605 }
606 /* }}} */
607 
php_scanner_globals_ctor(zend_php_scanner_globals * scanner_globals_p)608 static void php_scanner_globals_ctor(zend_php_scanner_globals *scanner_globals_p) /* {{{ */
609 {
610 	memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
611 }
612 /* }}} */
613 
614 void zend_init_opcodes_handlers(void);
615 
module_destructor_zval(zval * zv)616 static void module_destructor_zval(zval *zv) /* {{{ */
617 {
618 	zend_module_entry *module = (zend_module_entry*)Z_PTR_P(zv);
619 
620 	module_destructor(module);
621 	free(module);
622 }
623 /* }}} */
624 
php_auto_globals_create_globals(zend_string * name)625 static zend_bool php_auto_globals_create_globals(zend_string *name) /* {{{ */
626 {
627 	zval globals;
628 
629 	ZVAL_ARR(&globals, &EG(symbol_table));
630 	Z_TYPE_INFO_P(&globals) = IS_ARRAY | (IS_TYPE_SYMBOLTABLE << Z_TYPE_FLAGS_SHIFT);
631 	ZVAL_NEW_REF(&globals, &globals);
632 	zend_hash_update(&EG(symbol_table), name, &globals);
633 	return 0;
634 }
635 /* }}} */
636 
zend_startup(zend_utility_functions * utility_functions,char ** extensions)637 int zend_startup(zend_utility_functions *utility_functions, char **extensions) /* {{{ */
638 {
639 #ifdef ZTS
640 	zend_compiler_globals *compiler_globals;
641 	zend_executor_globals *executor_globals;
642 	extern ZEND_API ts_rsrc_id ini_scanner_globals_id;
643 	extern ZEND_API ts_rsrc_id language_scanner_globals_id;
644 	ZEND_TSRMLS_CACHE_UPDATE();
645 #else
646 	extern zend_ini_scanner_globals ini_scanner_globals;
647 	extern zend_php_scanner_globals language_scanner_globals;
648 #endif
649 
650 	start_memory_manager();
651 
652 	virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */
653 
654 #if defined(__FreeBSD__) || defined(__DragonFly__)
655 	/* FreeBSD and DragonFly floating point precision fix */
656 	fpsetmask(0);
657 #endif
658 
659 	zend_startup_strtod();
660 	zend_startup_extensions_mechanism();
661 
662 	/* Set up utility functions and values */
663 	zend_error_cb = utility_functions->error_function;
664 	zend_printf = utility_functions->printf_function;
665 	zend_write = (zend_write_func_t) utility_functions->write_function;
666 	zend_fopen = utility_functions->fopen_function;
667 	if (!zend_fopen) {
668 		zend_fopen = zend_fopen_wrapper;
669 	}
670 	zend_stream_open_function = utility_functions->stream_open_function;
671 	zend_message_dispatcher_p = utility_functions->message_handler;
672 #ifndef ZEND_SIGNALS
673 	zend_block_interruptions = utility_functions->block_interruptions;
674 	zend_unblock_interruptions = utility_functions->unblock_interruptions;
675 #endif
676 	zend_get_configuration_directive_p = utility_functions->get_configuration_directive;
677 	zend_ticks_function = utility_functions->ticks_function;
678 	zend_on_timeout = utility_functions->on_timeout;
679 	zend_vspprintf = utility_functions->vspprintf_function;
680 	zend_vstrpprintf = utility_functions->vstrpprintf_function;
681 	zend_getenv = utility_functions->getenv_function;
682 	zend_resolve_path = utility_functions->resolve_path_function;
683 
684 #if HAVE_DTRACE
685 /* build with dtrace support */
686 	{
687 		char *tmp = getenv("USE_ZEND_DTRACE");
688 
689 		if (tmp && zend_atoi(tmp, 0)) {
690 			zend_dtrace_enabled = 1;
691 			zend_compile_file = dtrace_compile_file;
692 			zend_execute_ex = dtrace_execute_ex;
693 			zend_execute_internal = dtrace_execute_internal;
694 		} else {
695 			zend_compile_file = compile_file;
696 			zend_execute_ex = execute_ex;
697 			zend_execute_internal = NULL;
698 		}
699 	}
700 #else
701 	zend_compile_file = compile_file;
702 	zend_execute_ex = execute_ex;
703 	zend_execute_internal = NULL;
704 #endif /* HAVE_DTRACE */
705 	zend_compile_string = compile_string;
706 	zend_throw_exception_hook = NULL;
707 
708 	/* Set up the default garbage collection implementation. */
709 	gc_collect_cycles = zend_gc_collect_cycles;
710 
711 	zend_init_opcodes_handlers();
712 
713 	/* set up version */
714 	zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
715 	zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1;
716 
717 	GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
718 	GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
719 	GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable));
720 	GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable));
721 
722 	zend_hash_init_ex(GLOBAL_FUNCTION_TABLE, 1024, NULL, ZEND_FUNCTION_DTOR, 1, 0);
723 	zend_hash_init_ex(GLOBAL_CLASS_TABLE, 64, NULL, ZEND_CLASS_DTOR, 1, 0);
724 	zend_hash_init_ex(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, auto_global_dtor, 1, 0);
725 	zend_hash_init_ex(GLOBAL_CONSTANTS_TABLE, 128, NULL, ZEND_CONSTANT_DTOR, 1, 0);
726 
727 	zend_hash_init_ex(&module_registry, 32, NULL, module_destructor_zval, 1, 0);
728 	zend_init_rsrc_list_dtors();
729 
730 #ifdef ZTS
731 	ts_allocate_id(&compiler_globals_id, sizeof(zend_compiler_globals), (ts_allocate_ctor) compiler_globals_ctor, (ts_allocate_dtor) compiler_globals_dtor);
732 	ts_allocate_id(&executor_globals_id, sizeof(zend_executor_globals), (ts_allocate_ctor) executor_globals_ctor, (ts_allocate_dtor) executor_globals_dtor);
733 	ts_allocate_id(&language_scanner_globals_id, sizeof(zend_php_scanner_globals), (ts_allocate_ctor) php_scanner_globals_ctor, NULL);
734 	ts_allocate_id(&ini_scanner_globals_id, sizeof(zend_ini_scanner_globals), (ts_allocate_ctor) ini_scanner_globals_ctor, NULL);
735 	compiler_globals = ts_resource(compiler_globals_id);
736 	executor_globals = ts_resource(executor_globals_id);
737 
738 	compiler_globals_dtor(compiler_globals);
739 	compiler_globals->in_compilation = 0;
740 	compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
741 	compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
742 
743 	*compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE;
744 	*compiler_globals->class_table = *GLOBAL_CLASS_TABLE;
745 	compiler_globals->auto_globals = GLOBAL_AUTO_GLOBALS_TABLE;
746 
747 	zend_hash_destroy(executor_globals->zend_constants);
748 	*executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE;
749 #else
750 	ini_scanner_globals_ctor(&ini_scanner_globals);
751 	php_scanner_globals_ctor(&language_scanner_globals);
752 	zend_set_default_compile_time_values();
753 #ifdef ZEND_WIN32
754 	zend_get_windows_version_info(&EG(windows_version_info));
755 #endif
756 #endif
757 	EG(error_reporting) = E_ALL & ~E_NOTICE;
758 
759 	zend_interned_strings_init();
760 	zend_startup_builtin_functions();
761 	zend_register_standard_constants();
762 	zend_register_auto_global(zend_string_init("GLOBALS", sizeof("GLOBALS") - 1, 1), 1, php_auto_globals_create_globals);
763 
764 #ifndef ZTS
765 	zend_init_rsrc_plist();
766 	zend_init_exception_op();
767 	zend_init_call_trampoline_op();
768 #endif
769 
770 	zend_ini_startup();
771 
772 #ifdef ZTS
773 	tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
774 #endif
775 
776 	return SUCCESS;
777 }
778 /* }}} */
779 
zend_register_standard_ini_entries(void)780 void zend_register_standard_ini_entries(void) /* {{{ */
781 {
782 	int module_number = 0;
783 
784 	REGISTER_INI_ENTRIES();
785 }
786 /* }}} */
787 
788 /* Unlink the global (r/o) copies of the class, function and constant tables,
789  * and use a fresh r/w copy for the startup thread
790  */
zend_post_startup(void)791 void zend_post_startup(void) /* {{{ */
792 {
793 #ifdef ZTS
794 	zend_encoding **script_encoding_list;
795 
796 	zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id);
797 	zend_executor_globals *executor_globals = ts_resource(executor_globals_id);
798 
799 	*GLOBAL_FUNCTION_TABLE = *compiler_globals->function_table;
800 	*GLOBAL_CLASS_TABLE = *compiler_globals->class_table;
801 	*GLOBAL_CONSTANTS_TABLE = *executor_globals->zend_constants;
802 
803 	short_tags_default = CG(short_tags);
804 	compiler_options_default = CG(compiler_options);
805 
806 	zend_destroy_rsrc_list(&EG(persistent_list));
807 	free(compiler_globals->function_table);
808 	free(compiler_globals->class_table);
809 	if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
810 		compiler_globals_ctor(compiler_globals);
811 		compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list;
812 	} else {
813 		compiler_globals_ctor(compiler_globals);
814 	}
815 	free(EG(zend_constants));
816 
817 	virtual_cwd_deactivate();
818 
819 	executor_globals_ctor(executor_globals);
820 	global_persistent_list = &EG(persistent_list);
821 	zend_copy_ini_directives();
822 #else
823 	virtual_cwd_deactivate();
824 #endif
825 }
826 /* }}} */
827 
zend_shutdown(void)828 void zend_shutdown(void) /* {{{ */
829 {
830 	zend_destroy_rsrc_list(&EG(persistent_list));
831 	if (EG(active))
832 	{
833 		/*
834 		 * The order of destruction is important here.
835 		 * See bugs #65463 and 66036.
836 		 */
837 		zend_function *func;
838 		zend_class_entry *ce;
839 
840 		ZEND_HASH_REVERSE_FOREACH_PTR(GLOBAL_FUNCTION_TABLE, func) {
841 			if (func->type == ZEND_USER_FUNCTION) {
842 				zend_cleanup_op_array_data((zend_op_array *) func);
843 			}
844 		} ZEND_HASH_FOREACH_END();
845 		ZEND_HASH_REVERSE_FOREACH_PTR(GLOBAL_CLASS_TABLE, ce) {
846 			if (ce->type == ZEND_USER_CLASS) {
847 				zend_cleanup_user_class_data(ce);
848 			} else {
849 				break;
850 			}
851 		} ZEND_HASH_FOREACH_END();
852 		zend_cleanup_internal_classes();
853 		zend_hash_reverse_apply(GLOBAL_FUNCTION_TABLE, (apply_func_t) clean_non_persistent_function_full);
854 		zend_hash_reverse_apply(GLOBAL_CLASS_TABLE, (apply_func_t) clean_non_persistent_class_full);
855 	}
856 	zend_destroy_modules();
857 
858 	virtual_cwd_deactivate();
859 	virtual_cwd_shutdown();
860 
861 	zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
862 	zend_hash_destroy(GLOBAL_CLASS_TABLE);
863 
864 	zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE);
865 	free(GLOBAL_AUTO_GLOBALS_TABLE);
866 
867 	zend_shutdown_extensions();
868 	free(zend_version_info);
869 
870 	free(GLOBAL_FUNCTION_TABLE);
871 	free(GLOBAL_CLASS_TABLE);
872 
873 	zend_hash_destroy(GLOBAL_CONSTANTS_TABLE);
874 	free(GLOBAL_CONSTANTS_TABLE);
875 	zend_shutdown_strtod();
876 
877 #ifdef ZTS
878 	GLOBAL_FUNCTION_TABLE = NULL;
879 	GLOBAL_CLASS_TABLE = NULL;
880 	GLOBAL_AUTO_GLOBALS_TABLE = NULL;
881 	GLOBAL_CONSTANTS_TABLE = NULL;
882 #endif
883 	zend_destroy_rsrc_list_dtors();
884 
885 	zend_interned_strings_dtor();
886 }
887 /* }}} */
888 
zend_set_utility_values(zend_utility_values * utility_values)889 void zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */
890 {
891 	zend_uv = *utility_values;
892 	zend_uv.import_use_extension_length = (uint)strlen(zend_uv.import_use_extension);
893 }
894 /* }}} */
895 
896 /* this should be compatible with the standard zenderror */
zenderror(const char * error)897 ZEND_COLD void zenderror(const char *error) /* {{{ */
898 {
899 	CG(parse_error) = 0;
900 
901 	if (EG(exception)) {
902 		/* An exception was thrown in the lexer, don't throw another in the parser. */
903 		return;
904 	}
905 
906 	zend_throw_exception(zend_ce_parse_error, error, 0);
907 }
908 /* }}} */
909 
BEGIN_EXTERN_C()910 BEGIN_EXTERN_C()
911 ZEND_API ZEND_COLD void _zend_bailout(char *filename, uint lineno) /* {{{ */
912 {
913 
914 	if (!EG(bailout)) {
915 		zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno);
916 		exit(-1);
917 	}
918 	CG(unclean_shutdown) = 1;
919 	CG(active_class_entry) = NULL;
920 	CG(in_compilation) = 0;
921 	EG(current_execute_data) = NULL;
922 	LONGJMP(*EG(bailout), FAILURE);
923 }
924 /* }}} */
END_EXTERN_C()925 END_EXTERN_C()
926 
927 ZEND_API void zend_append_version_info(const zend_extension *extension) /* {{{ */
928 {
929 	char *new_info;
930 	uint new_info_length;
931 
932 	new_info_length = (uint)(sizeof("    with  v, , by \n")
933 						+ strlen(extension->name)
934 						+ strlen(extension->version)
935 						+ strlen(extension->copyright)
936 						+ strlen(extension->author));
937 
938 	new_info = (char *) malloc(new_info_length + 1);
939 
940 	snprintf(new_info, new_info_length, "    with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
941 
942 	zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1);
943 	strncat(zend_version_info, new_info, new_info_length);
944 	zend_version_info_length += new_info_length;
945 	free(new_info);
946 }
947 /* }}} */
948 
get_zend_version(void)949 ZEND_API char *get_zend_version(void) /* {{{ */
950 {
951 	return zend_version_info;
952 }
953 /* }}} */
954 
zend_activate(void)955 ZEND_API void zend_activate(void) /* {{{ */
956 {
957 #ifdef ZTS
958 	virtual_cwd_activate();
959 #endif
960 	gc_reset();
961 	init_compiler();
962 	init_executor();
963 	startup_scanner();
964 }
965 /* }}} */
966 
zend_call_destructors(void)967 void zend_call_destructors(void) /* {{{ */
968 {
969 	zend_try {
970 		shutdown_destructors();
971 	} zend_end_try();
972 }
973 /* }}} */
974 
zend_deactivate(void)975 ZEND_API void zend_deactivate(void) /* {{{ */
976 {
977 	/* we're no longer executing anything */
978 	EG(current_execute_data) = NULL;
979 
980 	zend_try {
981 		shutdown_scanner();
982 	} zend_end_try();
983 
984 	/* shutdown_executor() takes care of its own bailout handling */
985 	shutdown_executor();
986 
987 	zend_try {
988 		zend_ini_deactivate();
989 	} zend_end_try();
990 
991 	zend_try {
992 		shutdown_compiler();
993 	} zend_end_try();
994 
995 	zend_destroy_rsrc_list(&EG(regular_list));
996 
997 #if GC_BENCH
998 	fprintf(stderr, "GC Statistics\n");
999 	fprintf(stderr, "-------------\n");
1000 	fprintf(stderr, "Runs:               %d\n", GC_G(gc_runs));
1001 	fprintf(stderr, "Collected:          %d\n", GC_G(collected));
1002 	fprintf(stderr, "Root buffer length: %d\n", GC_G(root_buf_length));
1003 	fprintf(stderr, "Root buffer peak:   %d\n\n", GC_G(root_buf_peak));
1004 	fprintf(stderr, "      Possible            Remove from  Marked\n");
1005 	fprintf(stderr, "        Root    Buffered     buffer     grey\n");
1006 	fprintf(stderr, "      --------  --------  -----------  ------\n");
1007 	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));
1008 #endif
1009 }
1010 /* }}} */
1011 
BEGIN_EXTERN_C()1012 BEGIN_EXTERN_C()
1013 ZEND_API void zend_message_dispatcher(zend_long message, const void *data) /* {{{ */
1014 {
1015 	if (zend_message_dispatcher_p) {
1016 		zend_message_dispatcher_p(message, data);
1017 	}
1018 }
1019 /* }}} */
END_EXTERN_C()1020 END_EXTERN_C()
1021 
1022 ZEND_API zval *zend_get_configuration_directive(zend_string *name) /* {{{ */
1023 {
1024 	if (zend_get_configuration_directive_p) {
1025 		return zend_get_configuration_directive_p(name);
1026 	} else {
1027 		return NULL;
1028 	}
1029 }
1030 /* }}} */
1031 
1032 #define SAVE_STACK(stack) do { \
1033 		if (CG(stack).top) { \
1034 			memcpy(&stack, &CG(stack), sizeof(zend_stack)); \
1035 			CG(stack).top = CG(stack).max = 0; \
1036 			CG(stack).elements = NULL; \
1037 		} else { \
1038 			stack.top = 0; \
1039 		} \
1040 	} while (0)
1041 
1042 #define RESTORE_STACK(stack) do { \
1043 		if (stack.top) { \
1044 			zend_stack_destroy(&CG(stack)); \
1045 			memcpy(&CG(stack), &stack, sizeof(zend_stack)); \
1046 		} \
1047 	} while (0)
1048 
1049 #if !defined(HAVE_NORETURN) || defined(HAVE_NORETURN_ALIAS)
zend_error(int type,const char * format,...)1050 ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) /* {{{ */
1051 #else
1052 static ZEND_COLD void zend_error_va_list(int type, const char *format, va_list args)
1053 #endif
1054 {
1055 	char *str;
1056 	int len;
1057 #if !defined(HAVE_NORETURN) || defined(HAVE_NORETURN_ALIAS)
1058 	va_list args;
1059 #endif
1060 	va_list usr_copy;
1061 	zval params[5];
1062 	zval retval;
1063 	const char *error_filename;
1064 	uint error_lineno = 0;
1065 	zval orig_user_error_handler;
1066 	zend_bool in_compilation;
1067 	zend_class_entry *saved_class_entry;
1068 	zend_stack loop_var_stack;
1069 	zend_stack delayed_oplines_stack;
1070 	zend_array *symbol_table;
1071 
1072 	/* Report about uncaught exception in case of fatal errors */
1073 	if (EG(exception)) {
1074 		zend_execute_data *ex;
1075 		const zend_op *opline;
1076 
1077 		switch (type) {
1078 			case E_CORE_ERROR:
1079 			case E_ERROR:
1080 			case E_RECOVERABLE_ERROR:
1081 			case E_PARSE:
1082 			case E_COMPILE_ERROR:
1083 			case E_USER_ERROR:
1084 				ex = EG(current_execute_data);
1085 				opline = NULL;
1086 				while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
1087 					ex = ex->prev_execute_data;
1088 				}
1089 				if (ex && ex->opline->opcode == ZEND_HANDLE_EXCEPTION &&
1090 				    EG(opline_before_exception)) {
1091 					opline = EG(opline_before_exception);
1092 				}
1093 				zend_exception_error(EG(exception), E_WARNING);
1094 				EG(exception) = NULL;
1095 				if (opline) {
1096 					ex->opline = opline;
1097 				}
1098 				break;
1099 			default:
1100 				break;
1101 		}
1102 	}
1103 
1104 	/* Obtain relevant filename and lineno */
1105 	switch (type) {
1106 		case E_CORE_ERROR:
1107 		case E_CORE_WARNING:
1108 			error_filename = NULL;
1109 			error_lineno = 0;
1110 			break;
1111 		case E_PARSE:
1112 		case E_COMPILE_ERROR:
1113 		case E_COMPILE_WARNING:
1114 		case E_ERROR:
1115 		case E_NOTICE:
1116 		case E_STRICT:
1117 		case E_DEPRECATED:
1118 		case E_WARNING:
1119 		case E_USER_ERROR:
1120 		case E_USER_WARNING:
1121 		case E_USER_NOTICE:
1122 		case E_USER_DEPRECATED:
1123 		case E_RECOVERABLE_ERROR:
1124 			if (zend_is_compiling()) {
1125 				error_filename = ZSTR_VAL(zend_get_compiled_filename());
1126 				error_lineno = zend_get_compiled_lineno();
1127 			} else if (zend_is_executing()) {
1128 				error_filename = zend_get_executed_filename();
1129 				if (error_filename[0] == '[') { /* [no active file] */
1130 					error_filename = NULL;
1131 					error_lineno = 0;
1132 				} else {
1133 					error_lineno = zend_get_executed_lineno();
1134 				}
1135 			} else {
1136 				error_filename = NULL;
1137 				error_lineno = 0;
1138 			}
1139 			break;
1140 		default:
1141 			error_filename = NULL;
1142 			error_lineno = 0;
1143 			break;
1144 	}
1145 	if (!error_filename) {
1146 		error_filename = "Unknown";
1147 	}
1148 
1149 #ifdef HAVE_DTRACE
1150 	if (DTRACE_ERROR_ENABLED()) {
1151 		char *dtrace_error_buffer;
1152 #if !defined(HAVE_NORETURN) || defined(HAVE_NORETURN_ALIAS)
1153 		va_start(args, format);
1154 #endif
1155 		zend_vspprintf(&dtrace_error_buffer, 0, format, args);
1156 		DTRACE_ERROR(dtrace_error_buffer, (char *)error_filename, error_lineno);
1157 		efree(dtrace_error_buffer);
1158 #if !defined(HAVE_NORETURN) || defined(HAVE_NORETURN_ALIAS)
1159 		va_end(args);
1160 #endif
1161 	}
1162 #endif /* HAVE_DTRACE */
1163 
1164 #if !defined(HAVE_NORETURN) || defined(HAVE_NORETURN_ALIAS)
1165 	va_start(args, format);
1166 #endif
1167 
1168 	/* if we don't have a user defined error handler */
1169 	if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF
1170 		|| !(EG(user_error_handler_error_reporting) & type)
1171 		|| EG(error_handling) != EH_NORMAL) {
1172 		zend_error_cb(type, error_filename, error_lineno, format, args);
1173 	} else switch (type) {
1174 		case E_ERROR:
1175 		case E_PARSE:
1176 		case E_CORE_ERROR:
1177 		case E_CORE_WARNING:
1178 		case E_COMPILE_ERROR:
1179 		case E_COMPILE_WARNING:
1180 			/* The error may not be safe to handle in user-space */
1181 			zend_error_cb(type, error_filename, error_lineno, format, args);
1182 			break;
1183 		default:
1184 			/* Handle the error in user space */
1185 /* va_copy() is __va_copy() in old gcc versions.
1186  * According to the autoconf manual, using
1187  * memcpy(&dst, &src, sizeof(va_list))
1188  * gives maximum portability. */
1189 #ifndef va_copy
1190 # ifdef __va_copy
1191 #  define va_copy(dest, src)	__va_copy((dest), (src))
1192 # else
1193 #  define va_copy(dest, src)	memcpy(&(dest), &(src), sizeof(va_list))
1194 # endif
1195 #endif
1196 			va_copy(usr_copy, args);
1197 			len = (int)zend_vspprintf(&str, 0, format, usr_copy);
1198 			ZVAL_NEW_STR(&params[1], zend_string_init(str, len, 0));
1199 			efree(str);
1200 #ifdef va_copy
1201 			va_end(usr_copy);
1202 #endif
1203 
1204 			ZVAL_LONG(&params[0], type);
1205 
1206 			if (error_filename) {
1207 				ZVAL_STRING(&params[2], error_filename);
1208 			} else {
1209 				ZVAL_NULL(&params[2]);
1210 			}
1211 
1212 			ZVAL_LONG(&params[3], error_lineno);
1213 
1214 			symbol_table = zend_rebuild_symbol_table();
1215 
1216 			/* during shutdown the symbol table table can be still null */
1217 			if (!symbol_table) {
1218 				ZVAL_NULL(&params[4]);
1219 			} else {
1220 				ZVAL_ARR(&params[4], zend_array_dup(symbol_table));
1221 			}
1222 
1223 			ZVAL_COPY_VALUE(&orig_user_error_handler, &EG(user_error_handler));
1224 			ZVAL_UNDEF(&EG(user_error_handler));
1225 
1226 			/* User error handler may include() additinal PHP files.
1227 			 * If an error was generated during comilation PHP will compile
1228 			 * such scripts recursivly, but some CG() variables may be
1229 			 * inconsistent. */
1230 
1231 			in_compilation = CG(in_compilation);
1232 			if (in_compilation) {
1233 				saved_class_entry = CG(active_class_entry);
1234 				CG(active_class_entry) = NULL;
1235 				SAVE_STACK(loop_var_stack);
1236 				SAVE_STACK(delayed_oplines_stack);
1237 				CG(in_compilation) = 0;
1238 			}
1239 
1240 			if (call_user_function_ex(CG(function_table), NULL, &orig_user_error_handler, &retval, 5, params, 1, NULL) == SUCCESS) {
1241 				if (Z_TYPE(retval) != IS_UNDEF) {
1242 					if (Z_TYPE(retval) == IS_FALSE) {
1243 						zend_error_cb(type, error_filename, error_lineno, format, args);
1244 					}
1245 					zval_ptr_dtor(&retval);
1246 				}
1247 			} else if (!EG(exception)) {
1248 				/* The user error handler failed, use built-in error handler */
1249 				zend_error_cb(type, error_filename, error_lineno, format, args);
1250 			}
1251 
1252 			if (in_compilation) {
1253 				CG(active_class_entry) = saved_class_entry;
1254 				RESTORE_STACK(loop_var_stack);
1255 				RESTORE_STACK(delayed_oplines_stack);
1256 				CG(in_compilation) = 1;
1257 			}
1258 
1259 			zval_ptr_dtor(&params[4]);
1260 			zval_ptr_dtor(&params[3]);
1261 			zval_ptr_dtor(&params[2]);
1262 			zval_ptr_dtor(&params[1]);
1263 			zval_ptr_dtor(&params[0]);
1264 
1265 			if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF) {
1266 				ZVAL_COPY_VALUE(&EG(user_error_handler), &orig_user_error_handler);
1267 			} else {
1268 				zval_ptr_dtor(&orig_user_error_handler);
1269 			}
1270 			break;
1271 	}
1272 
1273 #if !defined(HAVE_NORETURN) || defined(HAVE_NORETURN_ALIAS)
1274 	va_end(args);
1275 #endif
1276 
1277 	if (type == E_PARSE) {
1278 		/* eval() errors do not affect exit_status */
1279 		if (!(EG(current_execute_data) &&
1280 			EG(current_execute_data)->func &&
1281 			ZEND_USER_CODE(EG(current_execute_data)->func->type) &&
1282 			EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL &&
1283 			EG(current_execute_data)->opline->extended_value == ZEND_EVAL)) {
1284 			EG(exit_status) = 255;
1285 		}
1286 	}
1287 }
1288 /* }}} */
1289 
1290 #ifdef HAVE_NORETURN
1291 # ifdef HAVE_NORETURN_ALIAS
1292 ZEND_COLD void zend_error_noreturn(int type, const char *format, ...) __attribute__ ((alias("zend_error"),noreturn));
1293 # else
zend_error(int type,const char * format,...)1294 ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) /* {{{ */
1295 {
1296 	va_list va;
1297 
1298 	va_start(va, format);
1299 	zend_error_va_list(type, format, va);
1300 	va_end(va);
1301 }
1302 
zend_error_noreturn(int type,const char * format,...)1303 ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...)
1304 {
1305 	va_list va;
1306 
1307 	va_start(va, format);
1308 	zend_error_va_list(type, format, va);
1309 	va_end(va);
1310 }
1311 /* }}} */
1312 # endif
1313 #endif
1314 
zend_throw_error(zend_class_entry * exception_ce,const char * format,...)1315 ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format, ...) /* {{{ */
1316 {
1317 	va_list va;
1318 	char *message = NULL;
1319 
1320 	if (exception_ce) {
1321 		if (!instanceof_function(exception_ce, zend_ce_error)) {
1322 			zend_error(E_NOTICE, "Error exceptions must be derived from Error");
1323 			exception_ce = zend_ce_error;
1324 		}
1325 	} else {
1326 		exception_ce = zend_ce_error;
1327 	}
1328 
1329 	va_start(va, format);
1330 	zend_vspprintf(&message, 0, format, va);
1331 
1332 	//TODO: we can't convert compile-time errors to exceptions yet???
1333 	if (EG(current_execute_data) && !CG(in_compilation)) {
1334 		zend_throw_exception(exception_ce, message, 0);
1335 	} else {
1336 		zend_error(E_ERROR, "%s", message);
1337 	}
1338 
1339 	efree(message);
1340 	va_end(va);
1341 }
1342 /* }}} */
1343 
zend_type_error(const char * format,...)1344 ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) /* {{{ */
1345 {
1346 	va_list va;
1347 	char *message = NULL;
1348 
1349 	va_start(va, format);
1350 	zend_vspprintf(&message, 0, format, va);
1351 	zend_throw_exception(zend_ce_type_error, message, 0);
1352 	efree(message);
1353 	va_end(va);
1354 } /* }}} */
1355 
zend_internal_type_error(zend_bool throw_exception,const char * format,...)1356 ZEND_API ZEND_COLD void zend_internal_type_error(zend_bool throw_exception, const char *format, ...) /* {{{ */
1357 {
1358 	va_list va;
1359 	char *message = NULL;
1360 
1361 	va_start(va, format);
1362 	zend_vspprintf(&message, 0, format, va);
1363 	if (throw_exception) {
1364 		zend_throw_exception(zend_ce_type_error, message, 0);
1365 	} else {
1366 		zend_error(E_WARNING, "%s", message);
1367 	}
1368 	efree(message);
1369 
1370 	va_end(va);
1371 } /* }}} */
1372 
zend_output_debug_string(zend_bool trigger_break,const char * format,...)1373 ZEND_API ZEND_COLD void zend_output_debug_string(zend_bool trigger_break, const char *format, ...) /* {{{ */
1374 {
1375 #if ZEND_DEBUG
1376 	va_list args;
1377 
1378 	va_start(args, format);
1379 #	ifdef ZEND_WIN32
1380 	{
1381 		char output_buf[1024];
1382 
1383 		vsnprintf(output_buf, 1024, format, args);
1384 		OutputDebugString(output_buf);
1385 		OutputDebugString("\n");
1386 		if (trigger_break && IsDebuggerPresent()) {
1387 			DebugBreak();
1388 		}
1389 	}
1390 #	else
1391 	vfprintf(stderr, format, args);
1392 	fprintf(stderr, "\n");
1393 #	endif
1394 	va_end(args);
1395 #endif
1396 }
1397 /* }}} */
1398 
zend_try_exception_handler()1399 ZEND_API void zend_try_exception_handler() /* {{{ */
1400 {
1401 	if (EG(exception)) {
1402 		if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1403 			zval orig_user_exception_handler;
1404 			zval params[1], retval2;
1405 			zend_object *old_exception;
1406 			old_exception = EG(exception);
1407 			EG(exception) = NULL;
1408 			ZVAL_OBJ(&params[0], old_exception);
1409 			ZVAL_COPY_VALUE(&orig_user_exception_handler, &EG(user_exception_handler));
1410 
1411 			if (call_user_function_ex(CG(function_table), NULL, &orig_user_exception_handler, &retval2, 1, params, 1, NULL) == SUCCESS) {
1412 				zval_ptr_dtor(&retval2);
1413 				if (EG(exception)) {
1414 					OBJ_RELEASE(EG(exception));
1415 					EG(exception) = NULL;
1416 				}
1417 				OBJ_RELEASE(old_exception);
1418 			} else {
1419 				EG(exception) = old_exception;
1420 			}
1421 		}
1422 	}
1423 } /* }}} */
1424 
zend_execute_scripts(int type,zval * retval,int file_count,...)1425 ZEND_API int zend_execute_scripts(int type, zval *retval, int file_count, ...) /* {{{ */
1426 {
1427 	va_list files;
1428 	int i;
1429 	zend_file_handle *file_handle;
1430 	zend_op_array *op_array;
1431 
1432 	va_start(files, file_count);
1433 	for (i = 0; i < file_count; i++) {
1434 		file_handle = va_arg(files, zend_file_handle *);
1435 		if (!file_handle) {
1436 			continue;
1437 		}
1438 
1439 		op_array = zend_compile_file(file_handle, type);
1440 		if (file_handle->opened_path) {
1441 			zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path);
1442 		}
1443 		zend_destroy_file_handle(file_handle);
1444 		if (op_array) {
1445 			zend_execute(op_array, retval);
1446 			zend_exception_restore();
1447 			zend_try_exception_handler();
1448 			if (EG(exception)) {
1449 				zend_exception_error(EG(exception), E_ERROR);
1450 			}
1451 			destroy_op_array(op_array);
1452 			efree_size(op_array, sizeof(zend_op_array));
1453 		} else if (type==ZEND_REQUIRE) {
1454 			va_end(files);
1455 			return FAILURE;
1456 		}
1457 	}
1458 	va_end(files);
1459 
1460 	return SUCCESS;
1461 }
1462 /* }}} */
1463 
1464 #define COMPILED_STRING_DESCRIPTION_FORMAT "%s(%d) : %s"
1465 
zend_make_compiled_string_description(const char * name)1466 ZEND_API char *zend_make_compiled_string_description(const char *name) /* {{{ */
1467 {
1468 	const char *cur_filename;
1469 	int cur_lineno;
1470 	char *compiled_string_description;
1471 
1472 	if (zend_is_compiling()) {
1473 		cur_filename = ZSTR_VAL(zend_get_compiled_filename());
1474 		cur_lineno = zend_get_compiled_lineno();
1475 	} else if (zend_is_executing()) {
1476 		cur_filename = zend_get_executed_filename();
1477 		cur_lineno = zend_get_executed_lineno();
1478 	} else {
1479 		cur_filename = "Unknown";
1480 		cur_lineno = 0;
1481 	}
1482 
1483 	zend_spprintf(&compiled_string_description, 0, COMPILED_STRING_DESCRIPTION_FORMAT, cur_filename, cur_lineno, name);
1484 	return compiled_string_description;
1485 }
1486 /* }}} */
1487 
free_estring(char ** str_p)1488 void free_estring(char **str_p) /* {{{ */
1489 {
1490 	efree(*str_p);
1491 }
1492 /* }}} */
1493 
1494 /*
1495  * Local variables:
1496  * tab-width: 4
1497  * c-basic-offset: 4
1498  * indent-tabs-mode: t
1499  * End:
1500  */
1501