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