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 #ifdef ZEND_CHECK_STACK_LIMIT
836 executor_globals->stack_limit = (void*)0;
837 executor_globals->stack_base = (void*)0;
838 #endif
839 #ifdef ZEND_MAX_EXECUTION_TIMERS
840 executor_globals->pid = 0;
841 executor_globals->oldact = (struct sigaction){0};
842 #endif
843 memset(executor_globals->strtod_state.freelist, 0,
844 sizeof(executor_globals->strtod_state.freelist));
845 executor_globals->strtod_state.p5s = NULL;
846 executor_globals->strtod_state.result = NULL;
847 }
848 /* }}} */
849
executor_globals_persistent_list_dtor(void * storage)850 static void executor_globals_persistent_list_dtor(void *storage)
851 {
852 zend_executor_globals *executor_globals = storage;
853
854 if (&executor_globals->persistent_list != global_persistent_list) {
855 zend_destroy_rsrc_list(&executor_globals->persistent_list);
856 }
857 }
858
executor_globals_dtor(zend_executor_globals * executor_globals)859 static void executor_globals_dtor(zend_executor_globals *executor_globals) /* {{{ */
860 {
861 zend_ini_dtor(executor_globals->ini_directives);
862
863 if (executor_globals->zend_constants != GLOBAL_CONSTANTS_TABLE) {
864 zend_hash_destroy(executor_globals->zend_constants);
865 free(executor_globals->zend_constants);
866 }
867 }
868 /* }}} */
869
zend_new_thread_end_handler(THREAD_T thread_id)870 static void zend_new_thread_end_handler(THREAD_T thread_id) /* {{{ */
871 {
872 zend_copy_ini_directives();
873 zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP);
874 #ifdef ZEND_CHECK_STACK_LIMIT
875 zend_call_stack_init();
876 #endif
877 zend_max_execution_timer_init();
878 }
879 /* }}} */
880 #endif
881
882 #if defined(__FreeBSD__) || defined(__DragonFly__)
883 /* FreeBSD and DragonFly floating point precision fix */
884 #include <floatingpoint.h>
885 #endif
886
ini_scanner_globals_ctor(zend_ini_scanner_globals * scanner_globals_p)887 static void ini_scanner_globals_ctor(zend_ini_scanner_globals *scanner_globals_p) /* {{{ */
888 {
889 memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
890 }
891 /* }}} */
892
php_scanner_globals_ctor(zend_php_scanner_globals * scanner_globals_p)893 static void php_scanner_globals_ctor(zend_php_scanner_globals *scanner_globals_p) /* {{{ */
894 {
895 memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
896 }
897 /* }}} */
898
module_destructor_zval(zval * zv)899 static void module_destructor_zval(zval *zv) /* {{{ */
900 {
901 zend_module_entry *module = (zend_module_entry*)Z_PTR_P(zv);
902 module_destructor(module);
903 }
904 /* }}} */
905
php_auto_globals_create_globals(zend_string * name)906 static bool php_auto_globals_create_globals(zend_string *name) /* {{{ */
907 {
908 /* While we keep registering $GLOBALS as an auto-global, we do not create an
909 * actual variable for it. Access to it handled specially by the compiler. */
910 return 0;
911 }
912 /* }}} */
913
zend_startup(zend_utility_functions * utility_functions)914 void zend_startup(zend_utility_functions *utility_functions) /* {{{ */
915 {
916 #ifdef ZTS
917 zend_compiler_globals *compiler_globals;
918 zend_executor_globals *executor_globals;
919 extern ZEND_API ts_rsrc_id ini_scanner_globals_id;
920 extern ZEND_API ts_rsrc_id language_scanner_globals_id;
921 #else
922 extern zend_ini_scanner_globals ini_scanner_globals;
923 extern zend_php_scanner_globals language_scanner_globals;
924 #endif
925
926 zend_cpu_startup();
927
928 #ifdef ZEND_WIN32
929 php_win32_cp_set_by_id(65001);
930 #endif
931
932 /* Set up early utility functions. zend_mm depends on
933 * zend_random_bytes_insecure */
934 zend_random_bytes = utility_functions->random_bytes_function;
935 zend_random_bytes_insecure = utility_functions->random_bytes_insecure_function;
936
937 start_memory_manager();
938
939 virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */
940
941 #if defined(__FreeBSD__) || defined(__DragonFly__)
942 /* FreeBSD and DragonFly floating point precision fix */
943 fpsetmask(0);
944 #endif
945
946 zend_startup_hrtime();
947 zend_startup_extensions_mechanism();
948
949 /* Set up utility functions and values */
950 zend_error_cb = utility_functions->error_function;
951 zend_printf = utility_functions->printf_function;
952 zend_write = utility_functions->write_function;
953 zend_fopen = utility_functions->fopen_function;
954 if (!zend_fopen) {
955 zend_fopen = zend_fopen_wrapper;
956 }
957 zend_stream_open_function = utility_functions->stream_open_function;
958 zend_message_dispatcher_p = utility_functions->message_handler;
959 zend_get_configuration_directive_p = utility_functions->get_configuration_directive;
960 zend_ticks_function = utility_functions->ticks_function;
961 zend_on_timeout = utility_functions->on_timeout;
962 zend_printf_to_smart_string = utility_functions->printf_to_smart_string_function;
963 zend_printf_to_smart_str = utility_functions->printf_to_smart_str_function;
964 zend_getenv = utility_functions->getenv_function;
965 zend_resolve_path = utility_functions->resolve_path_function;
966
967 zend_interrupt_function = NULL;
968
969 #ifdef HAVE_DTRACE
970 /* build with dtrace support */
971 {
972 char *tmp = getenv("USE_ZEND_DTRACE");
973
974 if (tmp && ZEND_ATOL(tmp)) {
975 zend_dtrace_enabled = 1;
976 zend_compile_file = dtrace_compile_file;
977 zend_execute_ex = dtrace_execute_ex;
978 zend_execute_internal = dtrace_execute_internal;
979
980 zend_observer_error_register(dtrace_error_notify_cb);
981 } else {
982 zend_compile_file = compile_file;
983 zend_execute_ex = execute_ex;
984 zend_execute_internal = NULL;
985 }
986 }
987 #else
988 zend_compile_file = compile_file;
989 zend_execute_ex = execute_ex;
990 zend_execute_internal = NULL;
991 #endif /* HAVE_DTRACE */
992 zend_compile_string = compile_string;
993 zend_throw_exception_hook = NULL;
994
995 /* Set up the default garbage collection implementation. */
996 gc_collect_cycles = zend_gc_collect_cycles;
997
998 zend_vm_init();
999
1000 /* set up version */
1001 zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
1002 zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1;
1003
1004 GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
1005 GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
1006 GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable));
1007 GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable));
1008
1009 zend_hash_init(GLOBAL_FUNCTION_TABLE, 1024, NULL, ZEND_FUNCTION_DTOR, 1);
1010 zend_hash_init(GLOBAL_CLASS_TABLE, 64, NULL, ZEND_CLASS_DTOR, 1);
1011 zend_hash_init(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, auto_global_dtor, 1);
1012 zend_hash_init(GLOBAL_CONSTANTS_TABLE, 128, NULL, ZEND_CONSTANT_DTOR, 1);
1013
1014 zend_hash_init(&module_registry, 32, NULL, module_destructor_zval, 1);
1015 zend_init_rsrc_list_dtors();
1016
1017 #ifdef ZTS
1018 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);
1019 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);
1020 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);
1021 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);
1022 compiler_globals = ts_resource(compiler_globals_id);
1023 executor_globals = ts_resource(executor_globals_id);
1024
1025 compiler_globals_dtor(compiler_globals);
1026 compiler_globals->in_compilation = 0;
1027 compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
1028 compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
1029
1030 *compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE;
1031 *compiler_globals->class_table = *GLOBAL_CLASS_TABLE;
1032 compiler_globals->auto_globals = GLOBAL_AUTO_GLOBALS_TABLE;
1033
1034 zend_hash_destroy(executor_globals->zend_constants);
1035 *executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE;
1036 #else
1037 ini_scanner_globals_ctor(&ini_scanner_globals);
1038 php_scanner_globals_ctor(&language_scanner_globals);
1039 zend_set_default_compile_time_values();
1040 #ifdef ZEND_WIN32
1041 zend_get_windows_version_info(&EG(windows_version_info));
1042 #endif
1043 /* Map region is going to be created and resized at run-time. */
1044 CG(map_ptr_real_base) = NULL;
1045 CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(NULL);
1046 CG(map_ptr_size) = 0;
1047 CG(map_ptr_last) = 0;
1048 #endif /* ZTS */
1049 EG(error_reporting) = E_ALL & ~E_NOTICE;
1050
1051 zend_interned_strings_init();
1052 zend_startup_builtin_functions();
1053 zend_register_standard_constants();
1054 zend_register_auto_global(zend_string_init_interned("GLOBALS", sizeof("GLOBALS") - 1, 1), 1, php_auto_globals_create_globals);
1055
1056 #ifndef ZTS
1057 zend_init_rsrc_plist();
1058 zend_init_exception_op();
1059 zend_init_call_trampoline_op();
1060 #endif
1061
1062 zend_ini_startup();
1063
1064 #ifdef ZEND_WIN32
1065 /* Uses INI settings, so needs to be run after it. */
1066 php_win32_cp_setup();
1067 #endif
1068
1069 zend_optimizer_startup();
1070
1071 #ifdef ZTS
1072 tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
1073 tsrm_set_shutdown_handler(zend_interned_strings_dtor);
1074 #endif
1075 }
1076 /* }}} */
1077
zend_register_standard_ini_entries(void)1078 void zend_register_standard_ini_entries(void) /* {{{ */
1079 {
1080 zend_register_ini_entries_ex(ini_entries, 0, MODULE_PERSISTENT);
1081 }
1082 /* }}} */
1083
1084
1085 /* Unlink the global (r/o) copies of the class, function and constant tables,
1086 * and use a fresh r/w copy for the startup thread
1087 */
zend_post_startup(void)1088 zend_result zend_post_startup(void) /* {{{ */
1089 {
1090 #ifdef ZTS
1091 zend_encoding **script_encoding_list;
1092
1093 zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id);
1094 zend_executor_globals *executor_globals = ts_resource(executor_globals_id);
1095 #endif
1096
1097 startup_done = true;
1098
1099 if (zend_post_startup_cb) {
1100 zend_result (*cb)(void) = zend_post_startup_cb;
1101
1102 zend_post_startup_cb = NULL;
1103 if (cb() != SUCCESS) {
1104 return FAILURE;
1105 }
1106 }
1107
1108 #ifdef ZTS
1109 *GLOBAL_FUNCTION_TABLE = *compiler_globals->function_table;
1110 *GLOBAL_CLASS_TABLE = *compiler_globals->class_table;
1111 *GLOBAL_CONSTANTS_TABLE = *executor_globals->zend_constants;
1112 global_map_ptr_last = compiler_globals->map_ptr_last;
1113
1114 short_tags_default = CG(short_tags);
1115 compiler_options_default = CG(compiler_options);
1116
1117 zend_destroy_rsrc_list(&EG(persistent_list));
1118 free(compiler_globals->function_table);
1119 compiler_globals->function_table = NULL;
1120 free(compiler_globals->class_table);
1121 compiler_globals->class_table = NULL;
1122 if (compiler_globals->map_ptr_real_base) {
1123 free(compiler_globals->map_ptr_real_base);
1124 }
1125 compiler_globals->map_ptr_real_base = NULL;
1126 compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
1127 if (compiler_globals->internal_run_time_cache) {
1128 pefree(compiler_globals->internal_run_time_cache, 1);
1129 }
1130 compiler_globals->internal_run_time_cache = NULL;
1131 if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
1132 compiler_globals_ctor(compiler_globals);
1133 compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list;
1134 } else {
1135 compiler_globals_ctor(compiler_globals);
1136 }
1137 free(EG(zend_constants));
1138 EG(zend_constants) = NULL;
1139
1140 executor_globals_ctor(executor_globals);
1141 global_persistent_list = &EG(persistent_list);
1142 zend_copy_ini_directives();
1143 #else
1144 global_map_ptr_last = CG(map_ptr_last);
1145 #endif
1146
1147 #ifdef ZEND_CHECK_STACK_LIMIT
1148 zend_call_stack_init();
1149 #endif
1150 gc_init();
1151
1152 return SUCCESS;
1153 }
1154 /* }}} */
1155
zend_shutdown(void)1156 void zend_shutdown(void) /* {{{ */
1157 {
1158 zend_vm_dtor();
1159
1160 zend_destroy_rsrc_list(&EG(persistent_list));
1161 #ifdef ZTS
1162 ts_apply_for_id(executor_globals_id, executor_globals_persistent_list_dtor);
1163 #endif
1164 zend_destroy_modules();
1165
1166 virtual_cwd_deactivate();
1167 virtual_cwd_shutdown();
1168
1169 zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
1170 /* Child classes may reuse structures from parent classes, so destroy in reverse order. */
1171 zend_hash_graceful_reverse_destroy(GLOBAL_CLASS_TABLE);
1172
1173 zend_flf_capacity = 0;
1174 zend_flf_count = 0;
1175 free(zend_flf_functions);
1176 free(zend_flf_handlers);
1177 zend_flf_functions = NULL;
1178 zend_flf_handlers = NULL;
1179
1180 zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE);
1181 free(GLOBAL_AUTO_GLOBALS_TABLE);
1182
1183 zend_shutdown_extensions();
1184 free(zend_version_info);
1185
1186 free(GLOBAL_FUNCTION_TABLE);
1187 free(GLOBAL_CLASS_TABLE);
1188
1189 zend_hash_destroy(GLOBAL_CONSTANTS_TABLE);
1190 free(GLOBAL_CONSTANTS_TABLE);
1191 zend_shutdown_strtod();
1192 zend_attributes_shutdown();
1193
1194 #ifdef ZTS
1195 GLOBAL_FUNCTION_TABLE = NULL;
1196 GLOBAL_CLASS_TABLE = NULL;
1197 GLOBAL_AUTO_GLOBALS_TABLE = NULL;
1198 GLOBAL_CONSTANTS_TABLE = NULL;
1199 ts_free_id(executor_globals_id);
1200 ts_free_id(compiler_globals_id);
1201 #else
1202 if (CG(map_ptr_real_base)) {
1203 free(CG(map_ptr_real_base));
1204 CG(map_ptr_real_base) = NULL;
1205 CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(NULL);
1206 CG(map_ptr_size) = 0;
1207 }
1208 if (CG(script_encoding_list)) {
1209 free(ZEND_VOIDP(CG(script_encoding_list)));
1210 CG(script_encoding_list) = NULL;
1211 CG(script_encoding_list_size) = 0;
1212 }
1213 #endif
1214 zend_map_ptr_static_last = 0;
1215 zend_map_ptr_static_size = 0;
1216
1217 zend_destroy_rsrc_list_dtors();
1218
1219 zend_unload_modules();
1220
1221 zend_optimizer_shutdown();
1222 startup_done = false;
1223 }
1224 /* }}} */
1225
zend_set_utility_values(zend_utility_values * utility_values)1226 void zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */
1227 {
1228 zend_uv = *utility_values;
1229 }
1230 /* }}} */
1231
1232 /* this should be compatible with the standard zenderror */
zenderror(const char * error)1233 ZEND_COLD void zenderror(const char *error) /* {{{ */
1234 {
1235 CG(parse_error) = 0;
1236
1237 if (EG(exception)) {
1238 /* An exception was thrown in the lexer, don't throw another in the parser. */
1239 return;
1240 }
1241
1242 zend_throw_exception(zend_ce_parse_error, error, 0);
1243 }
1244 /* }}} */
1245
_zend_bailout(const char * filename,uint32_t lineno)1246 ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32_t lineno) /* {{{ */
1247 {
1248
1249 if (!EG(bailout)) {
1250 zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno);
1251 exit(-1);
1252 }
1253 gc_protect(1);
1254 CG(unclean_shutdown) = 1;
1255 CG(active_class_entry) = NULL;
1256 CG(in_compilation) = 0;
1257 CG(memoize_mode) = 0;
1258 EG(current_execute_data) = NULL;
1259 LONGJMP(*EG(bailout), FAILURE);
1260 }
1261 /* }}} */
1262
zend_get_page_size(void)1263 ZEND_API size_t zend_get_page_size(void)
1264 {
1265 #ifdef _WIN32
1266 SYSTEM_INFO system_info;
1267 GetSystemInfo(&system_info);
1268 return system_info.dwPageSize;
1269 #elif defined(__FreeBSD__)
1270 /* This returns the value obtained from
1271 * the auxv vector, avoiding a syscall. */
1272 return getpagesize();
1273 #else
1274 return (size_t) sysconf(_SC_PAGESIZE);
1275 #endif
1276 }
1277
zend_append_version_info(const zend_extension * extension)1278 ZEND_API void zend_append_version_info(const zend_extension *extension) /* {{{ */
1279 {
1280 char *new_info;
1281 uint32_t new_info_length;
1282
1283 new_info_length = (uint32_t)(sizeof(" with v, , by \n")
1284 + strlen(extension->name)
1285 + strlen(extension->version)
1286 + strlen(extension->copyright)
1287 + strlen(extension->author));
1288
1289 new_info = (char *) malloc(new_info_length + 1);
1290
1291 snprintf(new_info, new_info_length, " with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
1292
1293 zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1);
1294 strncat(zend_version_info, new_info, new_info_length);
1295 zend_version_info_length += new_info_length;
1296 free(new_info);
1297 }
1298 /* }}} */
1299
get_zend_version(void)1300 ZEND_API const char *get_zend_version(void) /* {{{ */
1301 {
1302 return zend_version_info;
1303 }
1304 /* }}} */
1305
zend_activate(void)1306 ZEND_API void zend_activate(void) /* {{{ */
1307 {
1308 #ifdef ZTS
1309 virtual_cwd_activate();
1310 #endif
1311 gc_reset();
1312 init_compiler();
1313 init_executor();
1314 startup_scanner();
1315 if (CG(map_ptr_last)) {
1316 memset((void **)CG(map_ptr_real_base) + zend_map_ptr_static_size, 0, CG(map_ptr_last) * sizeof(void*));
1317 }
1318 zend_reset_internal_run_time_cache();
1319 zend_observer_activate();
1320 }
1321 /* }}} */
1322
zend_call_destructors(void)1323 void zend_call_destructors(void) /* {{{ */
1324 {
1325 zend_try {
1326 shutdown_destructors();
1327 } zend_end_try();
1328 }
1329 /* }}} */
1330
zend_deactivate(void)1331 ZEND_API void zend_deactivate(void) /* {{{ */
1332 {
1333 /* we're no longer executing anything */
1334 EG(current_execute_data) = NULL;
1335
1336 zend_try {
1337 shutdown_scanner();
1338 } zend_end_try();
1339
1340 /* shutdown_executor() takes care of its own bailout handling */
1341 shutdown_executor();
1342
1343 zend_try {
1344 zend_ini_deactivate();
1345 } zend_end_try();
1346
1347 zend_try {
1348 shutdown_compiler();
1349 } zend_end_try();
1350
1351 zend_destroy_rsrc_list(&EG(regular_list));
1352
1353 /* See GH-8646: https://github.com/php/php-src/issues/8646
1354 *
1355 * Interned strings that hold class entries can get a corresponding slot in map_ptr for the CE cache.
1356 * map_ptr works like a bump allocator: there is a counter which increases to allocate the next slot in the map.
1357 *
1358 * For class name strings in non-opcache we have:
1359 * - on startup: permanent + interned
1360 * - on request: interned
1361 * For class name strings in opcache we have:
1362 * - on startup: permanent + interned
1363 * - on request: either not interned at all, which we can ignore because they won't get a CE cache entry
1364 * or they were already permanent + interned
1365 * or we get a new permanent + interned string in the opcache persistence code
1366 *
1367 * Notice that the map_ptr layout always has the permanent strings first, and the request strings after.
1368 * In non-opcache, a request string may get a slot in map_ptr, and that interned request string
1369 * gets destroyed at the end of the request. The corresponding map_ptr slot can thereafter never be used again.
1370 * This causes map_ptr to keep reallocating to larger and larger sizes.
1371 *
1372 * We solve it as follows:
1373 * We can check whether we had any interned request strings, which only happens in non-opcache.
1374 * If we have any, we reset map_ptr to the last permanent string.
1375 * We can't lose any permanent strings because of map_ptr's layout.
1376 */
1377 if (zend_hash_num_elements(&CG(interned_strings)) > 0) {
1378 zend_map_ptr_reset();
1379 }
1380
1381 #if GC_BENCH
1382 gc_bench_print();
1383 #endif
1384 }
1385 /* }}} */
1386
zend_message_dispatcher(zend_long message,const void * data)1387 ZEND_API void zend_message_dispatcher(zend_long message, const void *data) /* {{{ */
1388 {
1389 if (zend_message_dispatcher_p) {
1390 zend_message_dispatcher_p(message, data);
1391 }
1392 }
1393 /* }}} */
1394
zend_get_configuration_directive(zend_string * name)1395 ZEND_API zval *zend_get_configuration_directive(zend_string *name) /* {{{ */
1396 {
1397 if (zend_get_configuration_directive_p) {
1398 return zend_get_configuration_directive_p(name);
1399 } else {
1400 return NULL;
1401 }
1402 }
1403 /* }}} */
1404
1405 #define SAVE_STACK(stack) do { \
1406 if (CG(stack).top) { \
1407 memcpy(&stack, &CG(stack), sizeof(zend_stack)); \
1408 CG(stack).top = CG(stack).max = 0; \
1409 CG(stack).elements = NULL; \
1410 } else { \
1411 stack.top = 0; \
1412 } \
1413 } while (0)
1414
1415 #define RESTORE_STACK(stack) do { \
1416 if (stack.top) { \
1417 zend_stack_destroy(&CG(stack)); \
1418 memcpy(&CG(stack), &stack, sizeof(zend_stack)); \
1419 } \
1420 } while (0)
1421
zend_error_zstr_at(int orig_type,zend_string * error_filename,uint32_t error_lineno,zend_string * message)1422 ZEND_API ZEND_COLD void zend_error_zstr_at(
1423 int orig_type, zend_string *error_filename, uint32_t error_lineno, zend_string *message)
1424 {
1425 zval params[4];
1426 zval retval;
1427 zval orig_user_error_handler;
1428 bool in_compilation;
1429 zend_class_entry *saved_class_entry = NULL;
1430 zend_stack loop_var_stack;
1431 zend_stack delayed_oplines_stack;
1432 int type = orig_type & E_ALL;
1433 bool orig_record_errors;
1434 uint32_t orig_num_errors;
1435 zend_error_info **orig_errors;
1436 zend_result res;
1437
1438 /* If we're executing a function during SCCP, count any warnings that may be emitted,
1439 * but don't perform any other error handling. */
1440 if (EG(capture_warnings_during_sccp)) {
1441 ZEND_ASSERT(!(type & E_FATAL_ERRORS) && "Fatal error during SCCP");
1442 EG(capture_warnings_during_sccp)++;
1443 return;
1444 }
1445
1446 if (EG(record_errors)) {
1447 zend_error_info *info = emalloc(sizeof(zend_error_info));
1448 info->type = type;
1449 info->lineno = error_lineno;
1450 info->filename = zend_string_copy(error_filename);
1451 info->message = zend_string_copy(message);
1452
1453 /* This is very inefficient for a large number of errors.
1454 * Use pow2 realloc if it becomes a problem. */
1455 EG(num_errors)++;
1456 EG(errors) = erealloc(EG(errors), sizeof(zend_error_info*) * EG(num_errors));
1457 EG(errors)[EG(num_errors)-1] = info;
1458 }
1459
1460 /* Report about uncaught exception in case of fatal errors */
1461 if (EG(exception)) {
1462 zend_execute_data *ex;
1463 const zend_op *opline;
1464
1465 if (type & E_FATAL_ERRORS) {
1466 ex = EG(current_execute_data);
1467 opline = NULL;
1468 while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
1469 ex = ex->prev_execute_data;
1470 }
1471 if (ex && ex->opline->opcode == ZEND_HANDLE_EXCEPTION &&
1472 EG(opline_before_exception)) {
1473 opline = EG(opline_before_exception);
1474 }
1475 zend_exception_error(EG(exception), E_WARNING);
1476 EG(exception) = NULL;
1477 if (opline) {
1478 ex->opline = opline;
1479 }
1480 }
1481 }
1482
1483 zend_observer_error_notify(type, error_filename, error_lineno, message);
1484
1485 /* if we don't have a user defined error handler */
1486 if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF
1487 || !(EG(user_error_handler_error_reporting) & type)
1488 || EG(error_handling) != EH_NORMAL) {
1489 zend_error_cb(orig_type, error_filename, error_lineno, message);
1490 } else switch (type) {
1491 case E_ERROR:
1492 case E_PARSE:
1493 case E_CORE_ERROR:
1494 case E_CORE_WARNING:
1495 case E_COMPILE_ERROR:
1496 case E_COMPILE_WARNING:
1497 /* The error may not be safe to handle in user-space */
1498 zend_error_cb(orig_type, error_filename, error_lineno, message);
1499 break;
1500 default:
1501 /* Handle the error in user space */
1502 ZVAL_STR_COPY(¶ms[1], message);
1503 ZVAL_LONG(¶ms[0], type);
1504
1505 if (error_filename) {
1506 ZVAL_STR_COPY(¶ms[2], error_filename);
1507 } else {
1508 ZVAL_NULL(¶ms[2]);
1509 }
1510
1511 ZVAL_LONG(¶ms[3], error_lineno);
1512
1513 ZVAL_COPY_VALUE(&orig_user_error_handler, &EG(user_error_handler));
1514 ZVAL_UNDEF(&EG(user_error_handler));
1515
1516 /* User error handler may include() additional PHP files.
1517 * If an error was generated during compilation PHP will compile
1518 * such scripts recursively, but some CG() variables may be
1519 * inconsistent. */
1520
1521 in_compilation = CG(in_compilation);
1522 if (in_compilation) {
1523 saved_class_entry = CG(active_class_entry);
1524 CG(active_class_entry) = NULL;
1525 SAVE_STACK(loop_var_stack);
1526 SAVE_STACK(delayed_oplines_stack);
1527 CG(in_compilation) = 0;
1528 }
1529
1530 orig_record_errors = EG(record_errors);
1531 orig_num_errors = EG(num_errors);
1532 orig_errors = EG(errors);
1533 EG(record_errors) = false;
1534 EG(num_errors) = 0;
1535 EG(errors) = NULL;
1536
1537 res = call_user_function(CG(function_table), NULL, &orig_user_error_handler, &retval, 4, params);
1538
1539 EG(record_errors) = orig_record_errors;
1540 EG(num_errors) = orig_num_errors;
1541 EG(errors) = orig_errors;
1542
1543 if (res == SUCCESS) {
1544 if (Z_TYPE(retval) != IS_UNDEF) {
1545 if (Z_TYPE(retval) == IS_FALSE) {
1546 zend_error_cb(orig_type, error_filename, error_lineno, message);
1547 }
1548 zval_ptr_dtor(&retval);
1549 }
1550 } else if (!EG(exception)) {
1551 /* The user error handler failed, use built-in error handler */
1552 zend_error_cb(orig_type, error_filename, error_lineno, message);
1553 }
1554
1555 if (in_compilation) {
1556 CG(active_class_entry) = saved_class_entry;
1557 RESTORE_STACK(loop_var_stack);
1558 RESTORE_STACK(delayed_oplines_stack);
1559 CG(in_compilation) = 1;
1560 }
1561
1562 zval_ptr_dtor(¶ms[2]);
1563 zval_ptr_dtor(¶ms[1]);
1564
1565 if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF) {
1566 ZVAL_COPY_VALUE(&EG(user_error_handler), &orig_user_error_handler);
1567 } else {
1568 zval_ptr_dtor(&orig_user_error_handler);
1569 }
1570 break;
1571 }
1572
1573 if (type == E_PARSE) {
1574 /* eval() errors do not affect exit_status */
1575 if (!(EG(current_execute_data) &&
1576 EG(current_execute_data)->func &&
1577 ZEND_USER_CODE(EG(current_execute_data)->func->type) &&
1578 EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL &&
1579 EG(current_execute_data)->opline->extended_value == ZEND_EVAL)) {
1580 EG(exit_status) = 255;
1581 }
1582 }
1583 }
1584 /* }}} */
1585
zend_error_va_list(int orig_type,zend_string * error_filename,uint32_t error_lineno,const char * format,va_list args)1586 static ZEND_COLD void zend_error_va_list(
1587 int orig_type, zend_string *error_filename, uint32_t error_lineno,
1588 const char *format, va_list args)
1589 {
1590 zend_string *message = zend_vstrpprintf(0, format, args);
1591 zend_error_zstr_at(orig_type, error_filename, error_lineno, message);
1592 zend_string_release(message);
1593 }
1594
get_filename_lineno(int type,zend_string ** filename,uint32_t * lineno)1595 static ZEND_COLD void get_filename_lineno(int type, zend_string **filename, uint32_t *lineno) {
1596 /* Obtain relevant filename and lineno */
1597 switch (type) {
1598 case E_CORE_ERROR:
1599 case E_CORE_WARNING:
1600 *filename = NULL;
1601 *lineno = 0;
1602 break;
1603 case E_PARSE:
1604 case E_COMPILE_ERROR:
1605 case E_COMPILE_WARNING:
1606 case E_ERROR:
1607 case E_NOTICE:
1608 case E_DEPRECATED:
1609 case E_WARNING:
1610 case E_USER_ERROR:
1611 case E_USER_WARNING:
1612 case E_USER_NOTICE:
1613 case E_USER_DEPRECATED:
1614 case E_RECOVERABLE_ERROR:
1615 if (zend_is_compiling()) {
1616 *filename = zend_get_compiled_filename();
1617 *lineno = zend_get_compiled_lineno();
1618 } else if (zend_is_executing()) {
1619 *filename = zend_get_executed_filename_ex();
1620 *lineno = zend_get_executed_lineno();
1621 } else {
1622 *filename = NULL;
1623 *lineno = 0;
1624 }
1625 break;
1626 default:
1627 *filename = NULL;
1628 *lineno = 0;
1629 break;
1630 }
1631 if (!*filename) {
1632 *filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED);
1633 }
1634 }
1635
zend_error_at(int type,zend_string * filename,uint32_t lineno,const char * format,...)1636 ZEND_API ZEND_COLD void zend_error_at(
1637 int type, zend_string *filename, uint32_t lineno, const char *format, ...) {
1638 va_list args;
1639
1640 if (!filename) {
1641 uint32_t dummy_lineno;
1642 get_filename_lineno(type, &filename, &dummy_lineno);
1643 }
1644
1645 va_start(args, format);
1646 zend_error_va_list(type, filename, lineno, format, args);
1647 va_end(args);
1648 }
1649
1650 #define zend_error_impl(type, format) do { \
1651 zend_string *filename; \
1652 uint32_t lineno; \
1653 va_list args; \
1654 get_filename_lineno(type, &filename, &lineno); \
1655 va_start(args, format); \
1656 zend_error_va_list(type, filename, lineno, format, args); \
1657 va_end(args); \
1658 } while (0)
1659
zend_error(int type,const char * format,...)1660 ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) {
1661 zend_error_impl(type, format);
1662 }
1663
zend_error_unchecked(int type,const char * format,...)1664 ZEND_API ZEND_COLD void zend_error_unchecked(int type, const char *format, ...) {
1665 zend_error_impl(type, format);
1666 }
1667
zend_error_at_noreturn(int type,zend_string * filename,uint32_t lineno,const char * format,...)1668 ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_at_noreturn(
1669 int type, zend_string *filename, uint32_t lineno, const char *format, ...)
1670 {
1671 va_list args;
1672
1673 if (!filename) {
1674 uint32_t dummy_lineno;
1675 get_filename_lineno(type, &filename, &dummy_lineno);
1676 }
1677
1678 va_start(args, format);
1679 zend_error_va_list(type, filename, lineno, format, args);
1680 va_end(args);
1681 /* Should never reach this. */
1682 abort();
1683 }
1684
1685 #define zend_error_noreturn_impl(type, format) do { \
1686 zend_string *filename; \
1687 uint32_t lineno; \
1688 va_list args; \
1689 get_filename_lineno(type, &filename, &lineno); \
1690 va_start(args, format); \
1691 zend_error_va_list(type, filename, lineno, format, args); \
1692 va_end(args); \
1693 /* Should never reach this. */ \
1694 abort(); \
1695 } while (0)
1696
zend_error_noreturn(int type,const char * format,...)1697 ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...)
1698 {
1699 zend_error_noreturn_impl(type, format);
1700 }
1701
zend_error_noreturn_unchecked(int type,const char * format,...)1702 ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn_unchecked(int type, const char *format, ...)
1703 {
1704 zend_error_noreturn_impl(type, format);
1705 }
1706
zend_strerror_noreturn(int type,int errn,const char * message)1707 ZEND_API ZEND_COLD ZEND_NORETURN void zend_strerror_noreturn(int type, int errn, const char *message)
1708 {
1709 #ifdef HAVE_STRERROR_R
1710 char b[1024];
1711
1712 # ifdef STRERROR_R_CHAR_P
1713 char *buf = strerror_r(errn, b, sizeof(b));
1714 # else
1715 strerror_r(errn, b, sizeof(b));
1716 char *buf = b;
1717 # endif
1718 #else
1719 char *buf = strerror(errn);
1720 #endif
1721
1722 zend_error_noreturn(type, "%s: %s (%d)", message, buf, errn);
1723 }
1724
zend_error_zstr(int type,zend_string * message)1725 ZEND_API ZEND_COLD void zend_error_zstr(int type, zend_string *message) {
1726 zend_string *filename;
1727 uint32_t lineno;
1728 get_filename_lineno(type, &filename, &lineno);
1729 zend_error_zstr_at(type, filename, lineno, message);
1730 }
1731
zend_begin_record_errors(void)1732 ZEND_API void zend_begin_record_errors(void)
1733 {
1734 ZEND_ASSERT(!EG(record_errors) && "Error recording already enabled");
1735 EG(record_errors) = true;
1736 EG(num_errors) = 0;
1737 EG(errors) = NULL;
1738 }
1739
zend_emit_recorded_errors(void)1740 ZEND_API void zend_emit_recorded_errors(void)
1741 {
1742 EG(record_errors) = false;
1743 for (uint32_t i = 0; i < EG(num_errors); i++) {
1744 zend_error_info *error = EG(errors)[i];
1745 zend_error_zstr_at(error->type, error->filename, error->lineno, error->message);
1746 }
1747 }
1748
zend_free_recorded_errors(void)1749 ZEND_API void zend_free_recorded_errors(void)
1750 {
1751 if (!EG(num_errors)) {
1752 return;
1753 }
1754
1755 for (uint32_t i = 0; i < EG(num_errors); i++) {
1756 zend_error_info *info = EG(errors)[i];
1757 zend_string_release(info->filename);
1758 zend_string_release(info->message);
1759 efree(info);
1760 }
1761 efree(EG(errors));
1762 EG(errors) = NULL;
1763 EG(num_errors) = 0;
1764 }
1765
zend_throw_error(zend_class_entry * exception_ce,const char * format,...)1766 ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format, ...) /* {{{ */
1767 {
1768 va_list va;
1769 char *message = NULL;
1770
1771 if (!exception_ce) {
1772 exception_ce = zend_ce_error;
1773 }
1774
1775 /* Marker used to disable exception generation during preloading. */
1776 if (EG(exception) == (void*)(uintptr_t)-1) {
1777 return;
1778 }
1779
1780 va_start(va, format);
1781 zend_vspprintf(&message, 0, format, va);
1782
1783 //TODO: we can't convert compile-time errors to exceptions yet???
1784 if (EG(current_execute_data) && !CG(in_compilation)) {
1785 zend_throw_exception(exception_ce, message, 0);
1786 } else {
1787 zend_error_noreturn(E_ERROR, "%s", message);
1788 }
1789
1790 efree(message);
1791 va_end(va);
1792 }
1793 /* }}} */
1794
1795 /* 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)1796 ZEND_API ZEND_COLD void zend_illegal_container_offset(const zend_string *container, const zval *offset, int type)
1797 {
1798 switch (type) {
1799 case BP_VAR_IS:
1800 zend_type_error("Cannot access offset of type %s in isset or empty",
1801 zend_zval_type_name(offset));
1802 return;
1803 case BP_VAR_UNSET:
1804 /* Consistent error for when trying to unset a string offset */
1805 if (zend_string_equals(container, ZSTR_KNOWN(ZEND_STR_STRING))) {
1806 zend_throw_error(NULL, "Cannot unset string offsets");
1807 } else {
1808 zend_type_error("Cannot unset offset of type %s on %s", zend_zval_type_name(offset), ZSTR_VAL(container));
1809 }
1810 return;
1811 default:
1812 zend_type_error("Cannot access offset of type %s on %s",
1813 zend_zval_type_name(offset), ZSTR_VAL(container));
1814 return;
1815 }
1816 }
1817
zend_type_error(const char * format,...)1818 ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) /* {{{ */
1819 {
1820 va_list va;
1821 char *message = NULL;
1822
1823 va_start(va, format);
1824 zend_vspprintf(&message, 0, format, va);
1825 zend_throw_exception(zend_ce_type_error, message, 0);
1826 efree(message);
1827 va_end(va);
1828 } /* }}} */
1829
zend_argument_count_error(const char * format,...)1830 ZEND_API ZEND_COLD void zend_argument_count_error(const char *format, ...) /* {{{ */
1831 {
1832 va_list va;
1833 char *message = NULL;
1834
1835 va_start(va, format);
1836 zend_vspprintf(&message, 0, format, va);
1837 zend_throw_exception(zend_ce_argument_count_error, message, 0);
1838 efree(message);
1839
1840 va_end(va);
1841 } /* }}} */
1842
zend_value_error(const char * format,...)1843 ZEND_API ZEND_COLD void zend_value_error(const char *format, ...) /* {{{ */
1844 {
1845 va_list va;
1846 char *message = NULL;
1847
1848 va_start(va, format);
1849 zend_vspprintf(&message, 0, format, va);
1850 zend_throw_exception(zend_ce_value_error, message, 0);
1851 efree(message);
1852 va_end(va);
1853 } /* }}} */
1854
zend_output_debug_string(bool trigger_break,const char * format,...)1855 ZEND_API ZEND_COLD void zend_output_debug_string(bool trigger_break, const char *format, ...) /* {{{ */
1856 {
1857 #if ZEND_DEBUG
1858 va_list args;
1859
1860 va_start(args, format);
1861 # ifdef ZEND_WIN32
1862 {
1863 char output_buf[1024];
1864
1865 vsnprintf(output_buf, 1024, format, args);
1866 OutputDebugString(output_buf);
1867 OutputDebugString("\n");
1868 if (trigger_break && IsDebuggerPresent()) {
1869 DebugBreak();
1870 }
1871 }
1872 # else
1873 vfprintf(stderr, format, args);
1874 fprintf(stderr, "\n");
1875 # endif
1876 va_end(args);
1877 #endif
1878 }
1879 /* }}} */
1880
zend_user_exception_handler(void)1881 ZEND_API ZEND_COLD void zend_user_exception_handler(void) /* {{{ */
1882 {
1883 zval orig_user_exception_handler;
1884 zval params[1], retval2;
1885 zend_object *old_exception;
1886
1887 if (zend_is_unwind_exit(EG(exception))) {
1888 return;
1889 }
1890
1891 old_exception = EG(exception);
1892 EG(exception) = NULL;
1893 ZVAL_OBJ(¶ms[0], old_exception);
1894
1895 ZVAL_COPY_VALUE(&orig_user_exception_handler, &EG(user_exception_handler));
1896 zend_stack_push(&EG(user_exception_handlers), &orig_user_exception_handler);
1897 ZVAL_UNDEF(&EG(user_exception_handler));
1898
1899 if (call_user_function(CG(function_table), NULL, &orig_user_exception_handler, &retval2, 1, params) == SUCCESS) {
1900 zval_ptr_dtor(&retval2);
1901 if (EG(exception)) {
1902 OBJ_RELEASE(EG(exception));
1903 EG(exception) = NULL;
1904 }
1905 OBJ_RELEASE(old_exception);
1906 } else {
1907 EG(exception) = old_exception;
1908 }
1909
1910 if (Z_TYPE(EG(user_exception_handler)) == IS_UNDEF) {
1911 zval *tmp = zend_stack_top(&EG(user_exception_handlers));
1912 if (tmp) {
1913 ZVAL_COPY_VALUE(&EG(user_exception_handler), tmp);
1914 zend_stack_del_top(&EG(user_exception_handlers));
1915 }
1916 }
1917 } /* }}} */
1918
zend_execute_script(int type,zval * retval,zend_file_handle * file_handle)1919 ZEND_API zend_result zend_execute_script(int type, zval *retval, zend_file_handle *file_handle)
1920 {
1921 zend_op_array *op_array = zend_compile_file(file_handle, type);
1922 if (file_handle->opened_path) {
1923 zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path);
1924 }
1925
1926 zend_result ret = SUCCESS;
1927 if (op_array) {
1928 zend_execute(op_array, retval);
1929 zend_exception_restore();
1930 if (UNEXPECTED(EG(exception))) {
1931 if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1932 zend_user_exception_handler();
1933 }
1934 if (EG(exception)) {
1935 ret = zend_exception_error(EG(exception), E_ERROR);
1936 }
1937 }
1938 zend_destroy_static_vars(op_array);
1939 destroy_op_array(op_array);
1940 efree_size(op_array, sizeof(zend_op_array));
1941 } else if (type == ZEND_REQUIRE) {
1942 ret = FAILURE;
1943 }
1944
1945 return ret;
1946 }
1947
zend_execute_scripts(int type,zval * retval,int file_count,...)1948 ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count, ...) /* {{{ */
1949 {
1950 va_list files;
1951 int i;
1952 zend_file_handle *file_handle;
1953 zend_result ret = SUCCESS;
1954
1955 va_start(files, file_count);
1956 for (i = 0; i < file_count; i++) {
1957 file_handle = va_arg(files, zend_file_handle *);
1958 if (!file_handle) {
1959 continue;
1960 }
1961 if (ret == FAILURE) {
1962 continue;
1963 }
1964 ret = zend_execute_script(type, retval, file_handle);
1965 }
1966 va_end(files);
1967
1968 return ret;
1969 }
1970 /* }}} */
1971
1972 #define COMPILED_STRING_DESCRIPTION_FORMAT "%s(%d) : %s"
1973
zend_make_compiled_string_description(const char * name)1974 ZEND_API char *zend_make_compiled_string_description(const char *name) /* {{{ */
1975 {
1976 const char *cur_filename;
1977 int cur_lineno;
1978 char *compiled_string_description;
1979
1980 if (zend_is_compiling()) {
1981 cur_filename = ZSTR_VAL(zend_get_compiled_filename());
1982 cur_lineno = zend_get_compiled_lineno();
1983 } else if (zend_is_executing()) {
1984 cur_filename = zend_get_executed_filename();
1985 cur_lineno = zend_get_executed_lineno();
1986 } else {
1987 cur_filename = "Unknown";
1988 cur_lineno = 0;
1989 }
1990
1991 zend_spprintf(&compiled_string_description, 0, COMPILED_STRING_DESCRIPTION_FORMAT, cur_filename, cur_lineno, name);
1992 return compiled_string_description;
1993 }
1994 /* }}} */
1995
free_estring(char ** str_p)1996 void free_estring(char **str_p) /* {{{ */
1997 {
1998 efree(*str_p);
1999 }
2000 /* }}} */
2001
2002 ZEND_API size_t zend_map_ptr_static_size;
2003 ZEND_API size_t zend_map_ptr_static_last;
2004
zend_map_ptr_reset(void)2005 ZEND_API void zend_map_ptr_reset(void)
2006 {
2007 CG(map_ptr_last) = global_map_ptr_last;
2008 }
2009
zend_map_ptr_new(void)2010 ZEND_API void *zend_map_ptr_new(void)
2011 {
2012 void **ptr;
2013
2014 if (CG(map_ptr_last) >= CG(map_ptr_size)) {
2015 /* Grow map_ptr table */
2016 CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(CG(map_ptr_last) + 1, 4096);
2017 CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), (zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1);
2018 CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(CG(map_ptr_real_base));
2019 }
2020 ptr = (void**)CG(map_ptr_real_base) + zend_map_ptr_static_size + CG(map_ptr_last);
2021 *ptr = NULL;
2022 CG(map_ptr_last)++;
2023 return ZEND_MAP_PTR_PTR2OFFSET(ptr);
2024 }
2025
zend_map_ptr_new_static(void)2026 ZEND_API void *zend_map_ptr_new_static(void)
2027 {
2028 void **ptr;
2029
2030 if (zend_map_ptr_static_last >= zend_map_ptr_static_size) {
2031 zend_map_ptr_static_size += 4096;
2032 /* Grow map_ptr table */
2033 void *new_base = pemalloc((zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1);
2034 if (CG(map_ptr_real_base)) {
2035 memcpy((void **)new_base + 4096, CG(map_ptr_real_base), (CG(map_ptr_last) + zend_map_ptr_static_size - 4096) * sizeof(void *));
2036 pefree(CG(map_ptr_real_base), 1);
2037 }
2038 CG(map_ptr_real_base) = new_base;
2039 CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(new_base);
2040 }
2041 ptr = (void**)CG(map_ptr_real_base) + (zend_map_ptr_static_last & 4095);
2042 *ptr = NULL;
2043 zend_map_ptr_static_last++;
2044 return ZEND_MAP_PTR_PTR2OFFSET(ptr);
2045 }
2046
zend_map_ptr_extend(size_t last)2047 ZEND_API void zend_map_ptr_extend(size_t last)
2048 {
2049 if (last > CG(map_ptr_last)) {
2050 void **ptr;
2051
2052 if (last >= CG(map_ptr_size)) {
2053 /* Grow map_ptr table */
2054 CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(last, 4096);
2055 CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), (zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1);
2056 CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(CG(map_ptr_real_base));
2057 }
2058 ptr = (void**)CG(map_ptr_real_base) + zend_map_ptr_static_size + CG(map_ptr_last);
2059 memset(ptr, 0, (last - CG(map_ptr_last)) * sizeof(void*));
2060 CG(map_ptr_last) = last;
2061 }
2062 }
2063
zend_alloc_ce_cache(zend_string * type_name)2064 ZEND_API void zend_alloc_ce_cache(zend_string *type_name)
2065 {
2066 if (ZSTR_HAS_CE_CACHE(type_name) || !ZSTR_IS_INTERNED(type_name)) {
2067 return;
2068 }
2069
2070 if ((GC_FLAGS(type_name) & IS_STR_PERMANENT) && startup_done) {
2071 /* Don't allocate slot on permanent interned string outside module startup.
2072 * The cache slot would no longer be valid on the next request. */
2073 return;
2074 }
2075
2076 if (zend_string_equals_literal_ci(type_name, "self")
2077 || zend_string_equals_literal_ci(type_name, "parent")) {
2078 return;
2079 }
2080
2081 /* We use the refcount to keep map_ptr of corresponding type */
2082 uint32_t ret;
2083 do {
2084 ret = ZEND_MAP_PTR_NEW_OFFSET();
2085 } while (ret <= 2);
2086 GC_ADD_FLAGS(type_name, IS_STR_CLASS_NAME_MAP_PTR);
2087 GC_SET_REFCOUNT(type_name, ret);
2088 }
2089