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