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