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