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