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