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