1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2014 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@zend.com>                                |
16   |          Zeev Suraski <zeev@zend.com>                                |
17   +----------------------------------------------------------------------+
18*/
19
20/* $Id$ */
21
22#include "zend.h"
23#include "zend_extensions.h"
24#include "zend_modules.h"
25#include "zend_constants.h"
26#include "zend_list.h"
27#include "zend_API.h"
28#include "zend_exceptions.h"
29#include "zend_builtin_functions.h"
30#include "zend_ini.h"
31#include "zend_vm.h"
32#include "zend_dtrace.h"
33#include "zend_virtual_cwd.h"
34
35#ifdef ZTS
36# define GLOBAL_FUNCTION_TABLE      global_function_table
37# define GLOBAL_CLASS_TABLE         global_class_table
38# define GLOBAL_CONSTANTS_TABLE     global_constants_table
39# define GLOBAL_AUTO_GLOBALS_TABLE  global_auto_globals_table
40#else
41# define GLOBAL_FUNCTION_TABLE      CG(function_table)
42# define GLOBAL_CLASS_TABLE         CG(class_table)
43# define GLOBAL_AUTO_GLOBALS_TABLE  CG(auto_globals)
44# define GLOBAL_CONSTANTS_TABLE     EG(zend_constants)
45#endif
46
47/* true multithread-shared globals */
48ZEND_API zend_class_entry *zend_standard_class_def = NULL;
49ZEND_API size_t (*zend_printf)(const char *format, ...);
50ZEND_API zend_write_func_t zend_write;
51ZEND_API FILE *(*zend_fopen)(const char *filename, char **opened_path TSRMLS_DC);
52ZEND_API int (*zend_stream_open_function)(const char *filename, zend_file_handle *handle TSRMLS_DC);
53ZEND_API void (*zend_block_interruptions)(void);
54ZEND_API void (*zend_unblock_interruptions)(void);
55ZEND_API void (*zend_ticks_function)(int ticks TSRMLS_DC);
56ZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args);
57size_t (*zend_vspprintf)(char **pbuf, size_t max_len, const char *format, va_list ap);
58zend_string *(*zend_vstrpprintf)(size_t max_len, const char *format, va_list ap);
59ZEND_API char *(*zend_getenv)(char *name, size_t name_len TSRMLS_DC);
60ZEND_API char *(*zend_resolve_path)(const char *filename, int filename_len TSRMLS_DC);
61
62void (*zend_on_timeout)(int seconds TSRMLS_DC);
63
64static void (*zend_message_dispatcher_p)(zend_long message, const void *data TSRMLS_DC);
65static zval *(*zend_get_configuration_directive_p)(zend_string *name);
66
67static ZEND_INI_MH(OnUpdateErrorReporting) /* {{{ */
68{
69    if (!new_value) {
70        EG(error_reporting) = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED;
71    } else {
72        EG(error_reporting) = atoi(new_value->val);
73    }
74    return SUCCESS;
75}
76/* }}} */
77
78static ZEND_INI_MH(OnUpdateGCEnabled) /* {{{ */
79{
80    OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
81
82    if (GC_G(gc_enabled)) {
83        gc_init(TSRMLS_C);
84    }
85
86    return SUCCESS;
87}
88/* }}} */
89
90static ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */
91{
92    if (!CG(multibyte)) {
93        return FAILURE;
94    }
95    if (!zend_multibyte_get_functions(TSRMLS_C)) {
96        return SUCCESS;
97    }
98    return zend_multibyte_set_script_encoding_by_string(new_value->val, new_value->len TSRMLS_CC);
99}
100/* }}} */
101
102
103ZEND_INI_BEGIN()
104    ZEND_INI_ENTRY("error_reporting",               NULL,       ZEND_INI_ALL,       OnUpdateErrorReporting)
105    STD_ZEND_INI_BOOLEAN("zend.enable_gc",              "1",    ZEND_INI_ALL,       OnUpdateGCEnabled,      gc_enabled,     zend_gc_globals,        gc_globals)
106    STD_ZEND_INI_BOOLEAN("zend.multibyte", "0", ZEND_INI_PERDIR, OnUpdateBool, multibyte,      zend_compiler_globals, compiler_globals)
107    ZEND_INI_ENTRY("zend.script_encoding",          NULL,       ZEND_INI_ALL,       OnUpdateScriptEncoding)
108    STD_ZEND_INI_BOOLEAN("zend.detect_unicode",         "1",    ZEND_INI_ALL,       OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals)
109#ifdef ZEND_SIGNALS
110    STD_ZEND_INI_BOOLEAN("zend.signal_check", "0", ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals)
111#endif
112ZEND_INI_END()
113
114
115#ifdef ZTS
116ZEND_API int compiler_globals_id;
117ZEND_API int executor_globals_id;
118static HashTable *global_function_table = NULL;
119static HashTable *global_class_table = NULL;
120static HashTable *global_constants_table = NULL;
121static HashTable *global_auto_globals_table = NULL;
122static HashTable *global_persistent_list = NULL;
123#endif
124
125ZEND_API zend_utility_values zend_uv;
126
127/* version information */
128static char *zend_version_info;
129static uint zend_version_info_length;
130#define ZEND_CORE_VERSION_INFO  "Zend Engine v" ZEND_VERSION ", Copyright (c) 1998-2014 Zend Technologies\n"
131#define PRINT_ZVAL_INDENT 4
132
133static void print_hash(zend_write_func_t write_func, HashTable *ht, int indent, zend_bool is_object TSRMLS_DC) /* {{{ */
134{
135    zval *tmp;
136    zend_string *string_key;
137    zend_ulong num_key;
138    int i;
139
140    for (i = 0; i < indent; i++) {
141        ZEND_PUTS_EX(" ");
142    }
143    ZEND_PUTS_EX("(\n");
144    indent += PRINT_ZVAL_INDENT;
145    ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, string_key, tmp) {
146        if (Z_TYPE_P(tmp) == IS_INDIRECT) {
147            tmp = Z_INDIRECT_P(tmp);
148            if (Z_TYPE_P(tmp) == IS_UNDEF) {
149                continue;
150            }
151        }
152        for (i = 0; i < indent; i++) {
153            ZEND_PUTS_EX(" ");
154        }
155        ZEND_PUTS_EX("[");
156        if (string_key) {
157            if (is_object) {
158                const char *prop_name, *class_name;
159                size_t prop_len;
160                int mangled = zend_unmangle_property_name_ex(string_key, &class_name, &prop_name, &prop_len);
161
162                ZEND_WRITE_EX(prop_name, prop_len);
163                if (class_name && mangled == SUCCESS) {
164                    if (class_name[0]=='*') {
165                        ZEND_PUTS_EX(":protected");
166                    } else {
167                        ZEND_PUTS_EX(":");
168                        ZEND_PUTS_EX(class_name);
169                        ZEND_PUTS_EX(":private");
170                    }
171                }
172            } else {
173                ZEND_WRITE_EX(string_key->val, string_key->len);
174            }
175        } else {
176            char key[25];
177            snprintf(key, sizeof(key), ZEND_LONG_FMT, num_key);
178            ZEND_PUTS_EX(key);
179        }
180        ZEND_PUTS_EX("] => ");
181        zend_print_zval_r_ex(write_func, tmp, indent+PRINT_ZVAL_INDENT TSRMLS_CC);
182        ZEND_PUTS_EX("\n");
183    } ZEND_HASH_FOREACH_END();
184    indent -= PRINT_ZVAL_INDENT;
185    for (i = 0; i < indent; i++) {
186        ZEND_PUTS_EX(" ");
187    }
188    ZEND_PUTS_EX(")\n");
189}
190/* }}} */
191
192static void print_flat_hash(HashTable *ht TSRMLS_DC) /* {{{ */
193{
194    zval *tmp;
195    zend_string *string_key;
196    zend_ulong num_key;
197    int i = 0;
198
199    ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) {
200        if (i++ > 0) {
201            ZEND_PUTS(",");
202        }
203        ZEND_PUTS("[");
204        if (string_key) {
205            ZEND_WRITE(string_key->val, string_key->len);
206        } else {
207            zend_printf(ZEND_ULONG_FMT, num_key);
208        }
209        ZEND_PUTS("] => ");
210        zend_print_flat_zval_r(tmp TSRMLS_CC);
211    } ZEND_HASH_FOREACH_END();
212}
213/* }}} */
214
215ZEND_API int zend_make_printable_zval(zval *expr, zval *expr_copy TSRMLS_DC) /* {{{ */
216{
217    if (Z_TYPE_P(expr) == IS_STRING) {
218        return 0;
219    }
220
221again:
222    switch (Z_TYPE_P(expr)) {
223        case IS_NULL:
224        case IS_FALSE:
225            ZVAL_EMPTY_STRING(expr_copy);
226            break;
227        case IS_TRUE:
228            if (CG(one_char_string)['1']) {
229                ZVAL_INTERNED_STR(expr_copy, CG(one_char_string)['1']);
230            } else {
231                ZVAL_NEW_STR(expr_copy, zend_string_init("1", 1, 0));
232            }
233            break;
234        case IS_RESOURCE: {
235                char buf[sizeof("Resource id #") + MAX_LENGTH_OF_LONG];
236                int len;
237
238                len = snprintf(buf, sizeof(buf), "Resource id #" ZEND_LONG_FMT, Z_RES_HANDLE_P(expr));
239                ZVAL_NEW_STR(expr_copy, zend_string_init(buf, len, 0));
240            }
241            break;
242        case IS_ARRAY:
243            zend_error(E_NOTICE, "Array to string conversion");
244            // TODO: use interned string ???
245            ZVAL_NEW_STR(expr_copy, zend_string_init("Array", sizeof("Array") - 1, 0));
246            break;
247        case IS_OBJECT:
248            if (Z_OBJ_HANDLER_P(expr, cast_object)) {
249                Z_ADDREF_P(expr);
250                if (Z_OBJ_HANDLER_P(expr, cast_object)(expr, expr_copy, IS_STRING TSRMLS_CC) == SUCCESS) {
251                    zval_ptr_dtor(expr);
252                    break;
253                }
254                zval_ptr_dtor(expr);
255            }
256            if (!Z_OBJ_HANDLER_P(expr, cast_object) && Z_OBJ_HANDLER_P(expr, get)) {
257                zval rv;
258                zval *z = Z_OBJ_HANDLER_P(expr, get)(expr, &rv TSRMLS_CC);
259
260                Z_ADDREF_P(z);
261                if (Z_TYPE_P(z) != IS_OBJECT) {
262                    if (zend_make_printable_zval(z, expr_copy TSRMLS_CC)) {
263                        zval_ptr_dtor(z);
264                    } else {
265                        ZVAL_ZVAL(expr_copy, z, 0, 1);
266                    }
267                    return 1;
268                }
269                zval_ptr_dtor(z);
270            }
271            zend_error(EG(exception) ? E_ERROR : E_RECOVERABLE_ERROR, "Object of class %s could not be converted to string", Z_OBJCE_P(expr)->name->val);
272            ZVAL_EMPTY_STRING(expr_copy);
273            break;
274        case IS_DOUBLE:
275            ZVAL_DUP(expr_copy, expr);
276            zend_locale_sprintf_double(expr_copy ZEND_FILE_LINE_CC);
277            break;
278        case IS_REFERENCE:
279            expr = Z_REFVAL_P(expr);
280            if (Z_TYPE_P(expr) == IS_STRING) {
281                ZVAL_STR_COPY(expr_copy, Z_STR_P(expr));
282                return 1;
283            }
284            goto again;
285            break;
286        default:
287            ZVAL_DUP(expr_copy, expr);
288            convert_to_string(expr_copy);
289            break;
290    }
291    return 1;
292}
293/* }}} */
294
295ZEND_API int zend_print_zval(zval *expr, int indent TSRMLS_DC) /* {{{ */
296{
297    return zend_print_zval_ex(zend_write, expr, indent TSRMLS_CC);
298}
299/* }}} */
300
301ZEND_API int zend_print_zval_ex(zend_write_func_t write_func, zval *expr, int indent TSRMLS_DC) /* {{{ */
302{
303    zend_string *str = zval_get_string(expr);
304    size_t len = str->len;
305
306    if (len != 0) {
307        write_func(str->val, len);
308    }
309
310    zend_string_release(str);
311    return len;
312}
313/* }}} */
314
315ZEND_API void zend_print_flat_zval_r(zval *expr TSRMLS_DC) /* {{{ */
316{
317    switch (Z_TYPE_P(expr)) {
318        case IS_ARRAY:
319            ZEND_PUTS("Array (");
320            if (ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(expr)) &&
321                ++Z_ARRVAL_P(expr)->u.v.nApplyCount>1) {
322                ZEND_PUTS(" *RECURSION*");
323                Z_ARRVAL_P(expr)->u.v.nApplyCount--;
324                return;
325            }
326            print_flat_hash(Z_ARRVAL_P(expr) TSRMLS_CC);
327            ZEND_PUTS(")");
328            if (ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(expr))) {
329                Z_ARRVAL_P(expr)->u.v.nApplyCount--;
330            }
331            break;
332        case IS_OBJECT:
333        {
334            HashTable *properties = NULL;
335            zend_string *class_name = NULL;
336
337            if (Z_OBJ_HANDLER_P(expr, get_class_name)) {
338                class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr), 0 TSRMLS_CC);
339            }
340            if (class_name) {
341                zend_printf("%s Object (", class_name->val);
342            } else {
343                zend_printf("%s Object (", "Unknown Class");
344            }
345            if (class_name) {
346                zend_string_release(class_name);
347            }
348            if (Z_OBJ_HANDLER_P(expr, get_properties)) {
349                properties = Z_OBJPROP_P(expr);
350            }
351            if (properties) {
352                if (++properties->u.v.nApplyCount>1) {
353                    ZEND_PUTS(" *RECURSION*");
354                    properties->u.v.nApplyCount--;
355                    return;
356                }
357                print_flat_hash(properties TSRMLS_CC);
358                properties->u.v.nApplyCount--;
359            }
360            ZEND_PUTS(")");
361            break;
362        }
363        default:
364            zend_print_variable(expr TSRMLS_CC);
365            break;
366    }
367}
368/* }}} */
369
370ZEND_API void zend_print_zval_r(zval *expr, int indent TSRMLS_DC) /* {{{ */
371{
372    zend_print_zval_r_ex(zend_write, expr, indent TSRMLS_CC);
373}
374/* }}} */
375
376ZEND_API void zend_print_zval_r_ex(zend_write_func_t write_func, zval *expr, int indent TSRMLS_DC) /* {{{ */
377{
378    ZVAL_DEREF(expr);
379    switch (Z_TYPE_P(expr)) {
380        case IS_ARRAY:
381            ZEND_PUTS_EX("Array\n");
382            if (ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(expr)) &&
383                ++Z_ARRVAL_P(expr)->u.v.nApplyCount>1) {
384                ZEND_PUTS_EX(" *RECURSION*");
385                Z_ARRVAL_P(expr)->u.v.nApplyCount--;
386                return;
387            }
388            print_hash(write_func, Z_ARRVAL_P(expr), indent, 0 TSRMLS_CC);
389            if (ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P(expr))) {
390                Z_ARRVAL_P(expr)->u.v.nApplyCount--;
391            }
392            break;
393        case IS_OBJECT:
394            {
395                HashTable *properties;
396                zend_string *class_name = NULL;
397                int is_temp;
398
399                if (Z_OBJ_HANDLER_P(expr, get_class_name)) {
400                    class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr), 0 TSRMLS_CC);
401                }
402                if (class_name) {
403                    ZEND_PUTS_EX(class_name->val);
404                } else {
405                    ZEND_PUTS_EX("Unknown Class");
406                }
407                ZEND_PUTS_EX(" Object\n");
408                if (class_name) {
409                    zend_string_release(class_name);
410                }
411                if ((properties = Z_OBJDEBUG_P(expr, is_temp)) == NULL) {
412                    break;
413                }
414                if (++properties->u.v.nApplyCount>1) {
415                    ZEND_PUTS_EX(" *RECURSION*");
416                    properties->u.v.nApplyCount--;
417                    return;
418                }
419                print_hash(write_func, properties, indent, 1 TSRMLS_CC);
420                properties->u.v.nApplyCount--;
421                if (is_temp) {
422                    zend_hash_destroy(properties);
423                    FREE_HASHTABLE(properties);
424                }
425                break;
426            }
427        default:
428            zend_print_zval_ex(write_func, expr, indent TSRMLS_CC);
429            break;
430    }
431}
432/* }}} */
433
434static FILE *zend_fopen_wrapper(const char *filename, char **opened_path TSRMLS_DC) /* {{{ */
435{
436    if (opened_path) {
437        *opened_path = estrdup(filename);
438    }
439    return fopen(filename, "rb");
440}
441/* }}} */
442
443#ifdef ZTS
444static zend_bool asp_tags_default         = 0;
445static zend_bool short_tags_default       = 1;
446static uint32_t compiler_options_default = ZEND_COMPILE_DEFAULT;
447#else
448# define asp_tags_default           0
449# define short_tags_default         1
450# define compiler_options_default   ZEND_COMPILE_DEFAULT
451#endif
452
453static void zend_set_default_compile_time_values(TSRMLS_D) /* {{{ */
454{
455    /* default compile-time values */
456    CG(asp_tags) = asp_tags_default;
457    CG(short_tags) = short_tags_default;
458    CG(compiler_options) = compiler_options_default;
459}
460/* }}} */
461
462static void zend_init_exception_op(TSRMLS_D) /* {{{ */
463{
464    memset(EG(exception_op), 0, sizeof(EG(exception_op)));
465    EG(exception_op)[0].opcode = ZEND_HANDLE_EXCEPTION;
466    EG(exception_op)[0].op1_type = IS_UNUSED;
467    EG(exception_op)[0].op2_type = IS_UNUSED;
468    EG(exception_op)[0].result_type = IS_UNUSED;
469    ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op));
470    EG(exception_op)[1].opcode = ZEND_HANDLE_EXCEPTION;
471    EG(exception_op)[1].op1_type = IS_UNUSED;
472    EG(exception_op)[1].op2_type = IS_UNUSED;
473    EG(exception_op)[1].result_type = IS_UNUSED;
474    ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+1);
475    EG(exception_op)[2].opcode = ZEND_HANDLE_EXCEPTION;
476    EG(exception_op)[2].op1_type = IS_UNUSED;
477    EG(exception_op)[2].op2_type = IS_UNUSED;
478    EG(exception_op)[2].result_type = IS_UNUSED;
479    ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+2);
480}
481/* }}} */
482
483#ifdef ZTS
484static void function_copy_ctor(zval *zv)
485{
486    zend_function *old_func = Z_FUNC_P(zv);
487    Z_FUNC_P(zv) = pemalloc(sizeof(zend_internal_function), 1);
488    memcpy(Z_FUNC_P(zv), old_func, sizeof(zend_internal_function));
489    function_add_ref(Z_FUNC_P(zv));
490}
491
492static void compiler_globals_ctor(zend_compiler_globals *compiler_globals TSRMLS_DC) /* {{{ */
493{
494    compiler_globals->compiled_filename = NULL;
495
496    compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
497    zend_hash_init_ex(compiler_globals->function_table, 1024, NULL, ZEND_FUNCTION_DTOR, 1, 0);
498    zend_hash_copy(compiler_globals->function_table, global_function_table, function_copy_ctor);
499
500    compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
501    zend_hash_init_ex(compiler_globals->class_table, 64, NULL, ZEND_CLASS_DTOR, 1, 0);
502    zend_hash_copy(compiler_globals->class_table, global_class_table, zend_class_add_ref);
503
504    zend_set_default_compile_time_values(TSRMLS_C);
505
506    compiler_globals->auto_globals = (HashTable *) malloc(sizeof(HashTable));
507    zend_hash_init_ex(compiler_globals->auto_globals, 8, NULL, NULL, 1, 0);
508    zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, NULL /* empty element */);
509
510    compiler_globals->last_static_member = zend_hash_num_elements(compiler_globals->class_table);
511    if (compiler_globals->last_static_member) {
512        compiler_globals->static_members_table = calloc(compiler_globals->last_static_member, sizeof(zval*));
513    } else {
514        compiler_globals->static_members_table = NULL;
515    }
516    compiler_globals->script_encoding_list = NULL;
517
518#ifdef ZTS
519    compiler_globals->empty_string = zend_string_alloc(sizeof("")-1, 1);
520    compiler_globals->empty_string->val[0] = '\000';
521    zend_string_hash_val(compiler_globals->empty_string);
522    compiler_globals->empty_string->gc.u.v.flags |= IS_STR_INTERNED;
523
524    memset(compiler_globals->one_char_string, 0, sizeof(compiler_globals->one_char_string));
525#endif
526}
527/* }}} */
528
529static void compiler_globals_dtor(zend_compiler_globals *compiler_globals TSRMLS_DC) /* {{{ */
530{
531    if (compiler_globals->function_table != GLOBAL_FUNCTION_TABLE) {
532        zend_hash_destroy(compiler_globals->function_table);
533        free(compiler_globals->function_table);
534    }
535    if (compiler_globals->class_table != GLOBAL_CLASS_TABLE) {
536        zend_hash_destroy(compiler_globals->class_table);
537        free(compiler_globals->class_table);
538    }
539    if (compiler_globals->auto_globals != GLOBAL_AUTO_GLOBALS_TABLE) {
540        zend_hash_destroy(compiler_globals->auto_globals);
541        free(compiler_globals->auto_globals);
542    }
543    if (compiler_globals->static_members_table) {
544        free(compiler_globals->static_members_table);
545    }
546    if (compiler_globals->script_encoding_list) {
547        pefree((char*)compiler_globals->script_encoding_list, 1);
548    }
549    compiler_globals->last_static_member = 0;
550
551#ifdef ZTS
552    zend_string_release(compiler_globals->empty_string);
553#endif
554}
555/* }}} */
556
557static void executor_globals_ctor(zend_executor_globals *executor_globals TSRMLS_DC) /* {{{ */
558{
559    zend_startup_constants(TSRMLS_C);
560    zend_copy_constants(EG(zend_constants), GLOBAL_CONSTANTS_TABLE);
561    zend_init_rsrc_plist(TSRMLS_C);
562    zend_init_exception_op(TSRMLS_C);
563    EG(lambda_count) = 0;
564    ZVAL_UNDEF(&EG(user_error_handler));
565    ZVAL_UNDEF(&EG(user_exception_handler));
566    EG(in_autoload) = NULL;
567    EG(current_execute_data) = NULL;
568    EG(current_module) = NULL;
569    EG(exit_status) = 0;
570#if XPFPA_HAVE_CW
571    EG(saved_fpu_cw) = 0;
572#endif
573    EG(saved_fpu_cw_ptr) = NULL;
574    EG(active) = 0;
575}
576/* }}} */
577
578static void executor_globals_dtor(zend_executor_globals *executor_globals TSRMLS_DC) /* {{{ */
579{
580    zend_ini_shutdown(TSRMLS_C);
581    if (&executor_globals->persistent_list != global_persistent_list) {
582        zend_destroy_rsrc_list(&executor_globals->persistent_list TSRMLS_CC);
583    }
584    if (executor_globals->zend_constants != GLOBAL_CONSTANTS_TABLE) {
585        zend_hash_destroy(executor_globals->zend_constants);
586        free(executor_globals->zend_constants);
587    }
588}
589/* }}} */
590
591static void zend_new_thread_end_handler(THREAD_T thread_id TSRMLS_DC) /* {{{ */
592{
593    if (zend_copy_ini_directives(TSRMLS_C) == SUCCESS) {
594        zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP TSRMLS_CC);
595    }
596}
597/* }}} */
598#endif
599
600#if defined(__FreeBSD__) || defined(__DragonFly__)
601/* FreeBSD and DragonFly floating point precision fix */
602#include <floatingpoint.h>
603#endif
604
605static void ini_scanner_globals_ctor(zend_ini_scanner_globals *scanner_globals_p TSRMLS_DC) /* {{{ */
606{
607    memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
608}
609/* }}} */
610
611static void php_scanner_globals_ctor(zend_php_scanner_globals *scanner_globals_p TSRMLS_DC) /* {{{ */
612{
613    memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
614}
615/* }}} */
616
617void zend_init_opcodes_handlers(void);
618
619static void module_destructor_zval(zval *zv) /* {{{ */
620{
621    zend_module_entry *module = (zend_module_entry*)Z_PTR_P(zv);
622
623    module_destructor(module);
624    free(module);
625}
626/* }}} */
627
628static void auto_global_dtor(zval *zv) /* {{{ */
629{
630    free(Z_PTR_P(zv));
631}
632/* }}} */
633
634static zend_bool php_auto_globals_create_globals(zend_string *name TSRMLS_DC) /* {{{ */
635{
636    zval globals;
637
638    ZVAL_ARR(&globals, &EG(symbol_table));
639    ZVAL_NEW_REF(&globals, &globals);
640    zend_hash_update(&EG(symbol_table).ht, name, &globals);
641    return 0;
642}
643/* }}} */
644
645int zend_startup(zend_utility_functions *utility_functions, char **extensions TSRMLS_DC) /* {{{ */
646{
647#ifdef ZTS
648    zend_compiler_globals *compiler_globals;
649    zend_executor_globals *executor_globals;
650    extern ZEND_API ts_rsrc_id ini_scanner_globals_id;
651    extern ZEND_API ts_rsrc_id language_scanner_globals_id;
652#else
653    extern zend_ini_scanner_globals ini_scanner_globals;
654    extern zend_php_scanner_globals language_scanner_globals;
655#endif
656
657    start_memory_manager(TSRMLS_C);
658
659    virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */
660
661#if defined(__FreeBSD__) || defined(__DragonFly__)
662    /* FreeBSD and DragonFly floating point precision fix */
663    fpsetmask(0);
664#endif
665
666    zend_startup_strtod();
667    zend_startup_extensions_mechanism();
668
669    /* Set up utility functions and values */
670    zend_error_cb = utility_functions->error_function;
671    zend_printf = utility_functions->printf_function;
672    zend_write = (zend_write_func_t) utility_functions->write_function;
673    zend_fopen = utility_functions->fopen_function;
674    if (!zend_fopen) {
675        zend_fopen = zend_fopen_wrapper;
676    }
677    zend_stream_open_function = utility_functions->stream_open_function;
678    zend_message_dispatcher_p = utility_functions->message_handler;
679#ifndef ZEND_SIGNALS
680    zend_block_interruptions = utility_functions->block_interruptions;
681    zend_unblock_interruptions = utility_functions->unblock_interruptions;
682#endif
683    zend_get_configuration_directive_p = utility_functions->get_configuration_directive;
684    zend_ticks_function = utility_functions->ticks_function;
685    zend_on_timeout = utility_functions->on_timeout;
686    zend_vspprintf = utility_functions->vspprintf_function;
687    zend_vstrpprintf = utility_functions->vstrpprintf_function;
688    zend_getenv = utility_functions->getenv_function;
689    zend_resolve_path = utility_functions->resolve_path_function;
690
691#if HAVE_DTRACE
692/* build with dtrace support */
693    zend_compile_file = dtrace_compile_file;
694    zend_execute_ex = dtrace_execute_ex;
695    zend_execute_internal = dtrace_execute_internal;
696#else
697    zend_compile_file = compile_file;
698    zend_execute_ex = execute_ex;
699    zend_execute_internal = NULL;
700#endif /* HAVE_SYS_SDT_H */
701    zend_compile_string = compile_string;
702    zend_throw_exception_hook = NULL;
703
704    zend_init_opcodes_handlers();
705
706    /* set up version */
707    zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
708    zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1;
709
710    GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
711    GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
712    GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable));
713    GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable));
714
715    zend_hash_init_ex(GLOBAL_FUNCTION_TABLE, 1024, NULL, ZEND_FUNCTION_DTOR, 1, 0);
716    zend_hash_init_ex(GLOBAL_CLASS_TABLE, 64, NULL, ZEND_CLASS_DTOR, 1, 0);
717    zend_hash_init_ex(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, auto_global_dtor, 1, 0);
718    zend_hash_init_ex(GLOBAL_CONSTANTS_TABLE, 128, NULL, ZEND_CONSTANT_DTOR, 1, 0);
719
720    zend_hash_init_ex(&module_registry, 32, NULL, module_destructor_zval, 1, 0);
721    zend_init_rsrc_list_dtors();
722
723#ifdef ZTS
724    ts_allocate_id(&compiler_globals_id, sizeof(zend_compiler_globals), (ts_allocate_ctor) compiler_globals_ctor, (ts_allocate_dtor) compiler_globals_dtor);
725    ts_allocate_id(&executor_globals_id, sizeof(zend_executor_globals), (ts_allocate_ctor) executor_globals_ctor, (ts_allocate_dtor) executor_globals_dtor);
726    ts_allocate_id(&language_scanner_globals_id, sizeof(zend_php_scanner_globals), (ts_allocate_ctor) php_scanner_globals_ctor, NULL);
727    ts_allocate_id(&ini_scanner_globals_id, sizeof(zend_ini_scanner_globals), (ts_allocate_ctor) ini_scanner_globals_ctor, NULL);
728    compiler_globals = ts_resource(compiler_globals_id);
729    executor_globals = ts_resource(executor_globals_id);
730
731    compiler_globals_dtor(compiler_globals TSRMLS_CC);
732    compiler_globals->in_compilation = 0;
733    compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
734    compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
735
736    *compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE;
737    *compiler_globals->class_table = *GLOBAL_CLASS_TABLE;
738    compiler_globals->auto_globals = GLOBAL_AUTO_GLOBALS_TABLE;
739
740    zend_hash_destroy(executor_globals->zend_constants);
741    *executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE;
742#else
743    ini_scanner_globals_ctor(&ini_scanner_globals TSRMLS_CC);
744    php_scanner_globals_ctor(&language_scanner_globals TSRMLS_CC);
745    zend_set_default_compile_time_values(TSRMLS_C);
746    ZVAL_UNDEF(&EG(user_error_handler));
747    ZVAL_UNDEF(&EG(user_exception_handler));
748#endif
749
750    zend_interned_strings_init(TSRMLS_C);
751    zend_startup_builtin_functions(TSRMLS_C);
752    zend_register_standard_constants(TSRMLS_C);
753    zend_register_auto_global(zend_string_init("GLOBALS", sizeof("GLOBALS") - 1, 1), 1, php_auto_globals_create_globals TSRMLS_CC);
754
755#ifndef ZTS
756    zend_init_rsrc_plist(TSRMLS_C);
757    zend_init_exception_op(TSRMLS_C);
758#endif
759
760    zend_ini_startup(TSRMLS_C);
761
762#ifdef ZTS
763    tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
764#endif
765
766    return SUCCESS;
767}
768/* }}} */
769
770void zend_register_standard_ini_entries(TSRMLS_D) /* {{{ */
771{
772    int module_number = 0;
773
774    REGISTER_INI_ENTRIES();
775}
776/* }}} */
777
778/* Unlink the global (r/o) copies of the class, function and constant tables,
779 * and use a fresh r/w copy for the startup thread
780 */
781void zend_post_startup(TSRMLS_D) /* {{{ */
782{
783#ifdef ZTS
784    zend_encoding **script_encoding_list;
785
786    zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id);
787    zend_executor_globals *executor_globals = ts_resource(executor_globals_id);
788
789    *GLOBAL_FUNCTION_TABLE = *compiler_globals->function_table;
790    *GLOBAL_CLASS_TABLE = *compiler_globals->class_table;
791    *GLOBAL_CONSTANTS_TABLE = *executor_globals->zend_constants;
792
793    asp_tags_default = CG(asp_tags);
794    short_tags_default = CG(short_tags);
795    compiler_options_default = CG(compiler_options);
796
797    zend_destroy_rsrc_list(&EG(persistent_list) TSRMLS_CC);
798    free(compiler_globals->function_table);
799    free(compiler_globals->class_table);
800    if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
801        compiler_globals_ctor(compiler_globals, tsrm_ls);
802        compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list;
803    } else {
804        compiler_globals_ctor(compiler_globals, tsrm_ls);
805    }
806    free(EG(zend_constants));
807
808    virtual_cwd_deactivate(TSRMLS_C);
809
810    executor_globals_ctor(executor_globals, tsrm_ls);
811    global_persistent_list = &EG(persistent_list);
812    zend_copy_ini_directives(TSRMLS_C);
813#else
814    virtual_cwd_deactivate(TSRMLS_C);
815#endif
816}
817/* }}} */
818
819void zend_shutdown(TSRMLS_D) /* {{{ */
820{
821#ifdef ZEND_SIGNALS
822    zend_signal_shutdown(TSRMLS_C);
823#endif
824#ifdef ZEND_WIN32
825    zend_shutdown_timeout_thread();
826#endif
827    zend_destroy_rsrc_list(&EG(persistent_list) TSRMLS_CC);
828    if (EG(active))
829    {
830        /*
831         * The order of destruction is important here.
832         * See bugs #65463 and 66036.
833         */
834        zend_function *func;
835        zend_class_entry *ce;
836
837        ZEND_HASH_REVERSE_FOREACH_PTR(GLOBAL_FUNCTION_TABLE, func) {
838            if (func->type == ZEND_USER_FUNCTION) {
839                zend_cleanup_op_array_data((zend_op_array *) func);
840            }
841        } ZEND_HASH_FOREACH_END();
842        ZEND_HASH_REVERSE_FOREACH_PTR(GLOBAL_CLASS_TABLE, ce) {
843            if (ce->type == ZEND_USER_CLASS) {
844                zend_cleanup_user_class_data(ce TSRMLS_CC);
845            } else {
846                break;
847            }
848        } ZEND_HASH_FOREACH_END();
849        zend_cleanup_internal_classes(TSRMLS_C);
850        zend_hash_reverse_apply(GLOBAL_FUNCTION_TABLE, (apply_func_t) clean_non_persistent_function_full TSRMLS_CC);
851        zend_hash_reverse_apply(GLOBAL_CLASS_TABLE, (apply_func_t) clean_non_persistent_class_full TSRMLS_CC);
852    }
853    zend_destroy_modules();
854
855    virtual_cwd_deactivate(TSRMLS_C);
856    virtual_cwd_shutdown();
857
858    zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
859    zend_hash_destroy(GLOBAL_CLASS_TABLE);
860
861    zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE);
862    free(GLOBAL_AUTO_GLOBALS_TABLE);
863
864    zend_shutdown_extensions(TSRMLS_C);
865    free(zend_version_info);
866
867    free(GLOBAL_FUNCTION_TABLE);
868    free(GLOBAL_CLASS_TABLE);
869
870    zend_hash_destroy(GLOBAL_CONSTANTS_TABLE);
871    free(GLOBAL_CONSTANTS_TABLE);
872    zend_shutdown_strtod();
873
874#ifdef ZTS
875    GLOBAL_FUNCTION_TABLE = NULL;
876    GLOBAL_CLASS_TABLE = NULL;
877    GLOBAL_AUTO_GLOBALS_TABLE = NULL;
878    GLOBAL_CONSTANTS_TABLE = NULL;
879#endif
880    zend_destroy_rsrc_list_dtors();
881
882    zend_interned_strings_dtor(TSRMLS_C);
883}
884/* }}} */
885
886void zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */
887{
888    zend_uv = *utility_values;
889    zend_uv.import_use_extension_length = strlen(zend_uv.import_use_extension);
890}
891/* }}} */
892
893/* this should be compatible with the standard zenderror */
894void zenderror(const char *error) /* {{{ */
895{
896    zend_error(E_PARSE, "%s", error);
897}
898/* }}} */
899
900BEGIN_EXTERN_C()
901ZEND_API void _zend_bailout(char *filename, uint lineno) /* {{{ */
902{
903    TSRMLS_FETCH();
904
905    if (!EG(bailout)) {
906        zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno);
907        exit(-1);
908    }
909    CG(unclean_shutdown) = 1;
910    CG(active_class_entry) = NULL;
911    CG(in_compilation) = 0;
912    EG(current_execute_data) = NULL;
913    LONGJMP(*EG(bailout), FAILURE);
914}
915/* }}} */
916END_EXTERN_C()
917
918void zend_append_version_info(const zend_extension *extension) /* {{{ */
919{
920    char *new_info;
921    uint new_info_length;
922
923    new_info_length = sizeof("    with  v, , by \n")
924                        + strlen(extension->name)
925                        + strlen(extension->version)
926                        + strlen(extension->copyright)
927                        + strlen(extension->author);
928
929    new_info = (char *) malloc(new_info_length + 1);
930
931    snprintf(new_info, new_info_length, "    with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
932
933    zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1);
934    strncat(zend_version_info, new_info, new_info_length);
935    zend_version_info_length += new_info_length;
936    free(new_info);
937}
938/* }}} */
939
940ZEND_API char *get_zend_version(void) /* {{{ */
941{
942    return zend_version_info;
943}
944/* }}} */
945
946ZEND_API void zend_activate(TSRMLS_D) /* {{{ */
947{
948#ifdef ZTS
949    virtual_cwd_activate(TSRMLS_C);
950#endif
951    gc_reset(TSRMLS_C);
952    init_compiler(TSRMLS_C);
953    init_executor(TSRMLS_C);
954    startup_scanner(TSRMLS_C);
955}
956/* }}} */
957
958void zend_call_destructors(TSRMLS_D) /* {{{ */
959{
960    zend_try {
961        shutdown_destructors(TSRMLS_C);
962    } zend_end_try();
963}
964/* }}} */
965
966ZEND_API void zend_deactivate(TSRMLS_D) /* {{{ */
967{
968    /* we're no longer executing anything */
969    EG(current_execute_data) = NULL;
970
971    zend_try {
972        shutdown_scanner(TSRMLS_C);
973    } zend_end_try();
974
975    /* shutdown_executor() takes care of its own bailout handling */
976    shutdown_executor(TSRMLS_C);
977
978    zend_try {
979        shutdown_compiler(TSRMLS_C);
980    } zend_end_try();
981
982    zend_destroy_rsrc_list(&EG(regular_list) TSRMLS_CC);
983
984#if ZEND_DEBUG
985    if (GC_G(gc_enabled) && !CG(unclean_shutdown)) {
986        gc_collect_cycles(TSRMLS_C);
987    }
988#endif
989
990#if GC_BENCH
991    fprintf(stderr, "GC Statistics\n");
992    fprintf(stderr, "-------------\n");
993    fprintf(stderr, "Runs:               %d\n", GC_G(gc_runs));
994    fprintf(stderr, "Collected:          %d\n", GC_G(collected));
995    fprintf(stderr, "Root buffer length: %d\n", GC_G(root_buf_length));
996    fprintf(stderr, "Root buffer peak:   %d\n\n", GC_G(root_buf_peak));
997    fprintf(stderr, "      Possible            Remove from  Marked\n");
998    fprintf(stderr, "        Root    Buffered     buffer     grey\n");
999    fprintf(stderr, "      --------  --------  -----------  ------\n");
1000    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));
1001    fprintf(stderr, "ZOBJ  %8d  %8d  %9d  %8d\n", GC_G(zobj_possible_root), GC_G(zobj_buffered), GC_G(zobj_remove_from_buffer), GC_G(zobj_marked_grey));
1002#endif
1003
1004    zend_try {
1005        zend_ini_deactivate(TSRMLS_C);
1006    } zend_end_try();
1007}
1008/* }}} */
1009
1010BEGIN_EXTERN_C()
1011ZEND_API void zend_message_dispatcher(zend_long message, const void *data TSRMLS_DC) /* {{{ */
1012{
1013    if (zend_message_dispatcher_p) {
1014        zend_message_dispatcher_p(message, data TSRMLS_CC);
1015    }
1016}
1017/* }}} */
1018END_EXTERN_C()
1019
1020ZEND_API zval *zend_get_configuration_directive(zend_string *name) /* {{{ */
1021{
1022    if (zend_get_configuration_directive_p) {
1023        return zend_get_configuration_directive_p(name);
1024    } else {
1025        return NULL;
1026    }
1027}
1028/* }}} */
1029
1030#define SAVE_STACK(stack) do { \
1031        if (CG(stack).top) { \
1032            memcpy(&stack, &CG(stack), sizeof(zend_stack)); \
1033            CG(stack).top = CG(stack).max = 0; \
1034            CG(stack).elements = NULL; \
1035        } else { \
1036            stack.top = 0; \
1037        } \
1038    } while (0)
1039
1040#define RESTORE_STACK(stack) do { \
1041        if (stack.top) { \
1042            zend_stack_destroy(&CG(stack)); \
1043            memcpy(&CG(stack), &stack, sizeof(zend_stack)); \
1044        } \
1045    } while (0)
1046
1047#if !defined(ZEND_WIN32) && !defined(DARWIN)
1048ZEND_API void zend_error(int type, const char *format, ...) /* {{{ */
1049#else
1050static void zend_error_va_list(int type, const char *format, va_list args)
1051#endif
1052{
1053    char *str;
1054    int len;
1055#if !defined(ZEND_WIN32) && !defined(DARWIN)
1056    va_list args;
1057#endif
1058    va_list usr_copy;
1059    zval params[5];
1060    zval retval;
1061    const char *error_filename;
1062    uint error_lineno = 0;
1063    zval orig_user_error_handler;
1064    zend_bool in_compilation;
1065    zend_class_entry *saved_class_entry;
1066    zend_stack loop_var_stack;
1067    zend_stack delayed_oplines_stack;
1068    zend_stack context_stack;
1069    zend_array *symbol_table;
1070    TSRMLS_FETCH();
1071
1072    /* Report about uncaught exception in case of fatal errors */
1073    if (EG(exception)) {
1074        zend_execute_data *ex;
1075        const zend_op *opline;
1076
1077        switch (type) {
1078            case E_CORE_ERROR:
1079            case E_ERROR:
1080            case E_RECOVERABLE_ERROR:
1081            case E_PARSE:
1082            case E_COMPILE_ERROR:
1083            case E_USER_ERROR:
1084                ex = EG(current_execute_data);
1085                opline = NULL;
1086                while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
1087                    ex = ex->prev_execute_data;
1088                }
1089                if (ex && ex->opline->opcode == ZEND_HANDLE_EXCEPTION &&
1090                    EG(opline_before_exception)) {
1091                    opline = EG(opline_before_exception);
1092                }
1093                zend_exception_error(EG(exception), E_WARNING TSRMLS_CC);
1094                EG(exception) = NULL;
1095                if (opline) {
1096                    ex->opline = opline;
1097                }
1098                break;
1099            default:
1100                break;
1101        }
1102    }
1103
1104    /* Obtain relevant filename and lineno */
1105    switch (type) {
1106        case E_CORE_ERROR:
1107        case E_CORE_WARNING:
1108            error_filename = NULL;
1109            error_lineno = 0;
1110            break;
1111        case E_PARSE:
1112        case E_COMPILE_ERROR:
1113        case E_COMPILE_WARNING:
1114        case E_ERROR:
1115        case E_NOTICE:
1116        case E_STRICT:
1117        case E_DEPRECATED:
1118        case E_WARNING:
1119        case E_USER_ERROR:
1120        case E_USER_WARNING:
1121        case E_USER_NOTICE:
1122        case E_USER_DEPRECATED:
1123        case E_RECOVERABLE_ERROR:
1124            if (zend_is_compiling(TSRMLS_C)) {
1125                error_filename = zend_get_compiled_filename(TSRMLS_C)->val;
1126                error_lineno = zend_get_compiled_lineno(TSRMLS_C);
1127            } else if (zend_is_executing(TSRMLS_C)) {
1128                error_filename = zend_get_executed_filename(TSRMLS_C);
1129                if (error_filename[0] == '[') { /* [no active file] */
1130                    error_filename = NULL;
1131                    error_lineno = 0;
1132                } else {
1133                    error_lineno = zend_get_executed_lineno(TSRMLS_C);
1134                }
1135            } else {
1136                error_filename = NULL;
1137                error_lineno = 0;
1138            }
1139            break;
1140        default:
1141            error_filename = NULL;
1142            error_lineno = 0;
1143            break;
1144    }
1145    if (!error_filename) {
1146        error_filename = "Unknown";
1147    }
1148
1149#ifdef HAVE_DTRACE
1150    if(DTRACE_ERROR_ENABLED()) {
1151        char *dtrace_error_buffer;
1152        va_start(args, format);
1153        zend_vspprintf(&dtrace_error_buffer, 0, format, args);
1154        DTRACE_ERROR(dtrace_error_buffer, (char *)error_filename, error_lineno);
1155        efree(dtrace_error_buffer);
1156        va_end(args);
1157    }
1158#endif /* HAVE_DTRACE */
1159
1160#if !defined(ZEND_WIN32) && !defined(DARWIN)
1161    va_start(args, format);
1162#endif
1163
1164    /* if we don't have a user defined error handler */
1165    if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF
1166        || !(EG(user_error_handler_error_reporting) & type)
1167        || EG(error_handling) != EH_NORMAL) {
1168        zend_error_cb(type, error_filename, error_lineno, format, args);
1169    } else switch (type) {
1170        case E_ERROR:
1171        case E_PARSE:
1172        case E_CORE_ERROR:
1173        case E_CORE_WARNING:
1174        case E_COMPILE_ERROR:
1175        case E_COMPILE_WARNING:
1176            /* The error may not be safe to handle in user-space */
1177            zend_error_cb(type, error_filename, error_lineno, format, args);
1178            break;
1179        default:
1180            /* Handle the error in user space */
1181/* va_copy() is __va_copy() in old gcc versions.
1182 * According to the autoconf manual, using
1183 * memcpy(&dst, &src, sizeof(va_list))
1184 * gives maximum portability. */
1185#ifndef va_copy
1186# ifdef __va_copy
1187#  define va_copy(dest, src)    __va_copy((dest), (src))
1188# else
1189#  define va_copy(dest, src)    memcpy(&(dest), &(src), sizeof(va_list))
1190# endif
1191#endif
1192            va_copy(usr_copy, args);
1193            len = zend_vspprintf(&str, 0, format, usr_copy);
1194            ZVAL_NEW_STR(&params[1], zend_string_init(str, len, 0));
1195            efree(str);
1196#ifdef va_copy
1197            va_end(usr_copy);
1198#endif
1199
1200            ZVAL_LONG(&params[0], type);
1201
1202            if (error_filename) {
1203                ZVAL_STRING(&params[2], error_filename);
1204            } else {
1205                ZVAL_NULL(&params[2]);
1206            }
1207
1208            ZVAL_LONG(&params[3], error_lineno);
1209
1210            symbol_table = zend_rebuild_symbol_table(TSRMLS_C);
1211
1212            /* during shutdown the symbol table table can be still null */
1213            if (!symbol_table) {
1214                ZVAL_NULL(&params[4]);
1215            } else {
1216                ZVAL_NEW_ARR(&params[4]);
1217                zend_array_dup(Z_ARRVAL(params[4]), &symbol_table->ht);
1218            }
1219
1220            ZVAL_COPY_VALUE(&orig_user_error_handler, &EG(user_error_handler));
1221            ZVAL_UNDEF(&EG(user_error_handler));
1222
1223            /* User error handler may include() additinal PHP files.
1224             * If an error was generated during comilation PHP will compile
1225             * such scripts recursivly, but some CG() variables may be
1226             * inconsistent. */
1227
1228            in_compilation = CG(in_compilation);
1229            if (in_compilation) {
1230                saved_class_entry = CG(active_class_entry);
1231                CG(active_class_entry) = NULL;
1232                SAVE_STACK(loop_var_stack);
1233                SAVE_STACK(delayed_oplines_stack);
1234                SAVE_STACK(context_stack);
1235                CG(in_compilation) = 0;
1236            }
1237
1238            ZVAL_UNDEF(&retval);
1239            if (call_user_function_ex(CG(function_table), NULL, &orig_user_error_handler, &retval, 5, params, 1, NULL TSRMLS_CC) == SUCCESS) {
1240                if (Z_TYPE(retval) != IS_UNDEF) {
1241                    if (Z_TYPE(retval) == IS_FALSE) {
1242                        zend_error_cb(type, error_filename, error_lineno, format, args);
1243                    }
1244                    zval_ptr_dtor(&retval);
1245                }
1246            } else if (!EG(exception)) {
1247                /* The user error handler failed, use built-in error handler */
1248                zend_error_cb(type, error_filename, error_lineno, format, args);
1249            }
1250
1251            if (in_compilation) {
1252                CG(active_class_entry) = saved_class_entry;
1253                RESTORE_STACK(loop_var_stack);
1254                RESTORE_STACK(delayed_oplines_stack);
1255                RESTORE_STACK(context_stack);
1256                CG(in_compilation) = 1;
1257            }
1258
1259            zval_ptr_dtor(&params[4]);
1260            zval_ptr_dtor(&params[3]);
1261            zval_ptr_dtor(&params[2]);
1262            zval_ptr_dtor(&params[1]);
1263            zval_ptr_dtor(&params[0]);
1264
1265            if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF) {
1266                ZVAL_COPY_VALUE(&EG(user_error_handler), &orig_user_error_handler);
1267            } else {
1268                zval_ptr_dtor(&orig_user_error_handler);
1269            }
1270            break;
1271    }
1272
1273#if !defined(ZEND_WIN32) && !defined(DARWIN)
1274    va_end(args);
1275#endif
1276
1277    if (type == E_PARSE) {
1278        /* eval() errors do not affect exit_status */
1279        if (!(EG(current_execute_data) &&
1280            EG(current_execute_data)->func &&
1281            ZEND_USER_CODE(EG(current_execute_data)->func->type) &&
1282            EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL &&
1283            EG(current_execute_data)->opline->extended_value == ZEND_EVAL)) {
1284            EG(exit_status) = 255;
1285        }
1286    }
1287}
1288/* }}} */
1289
1290#if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__))
1291void zend_error_noreturn(int type, const char *format, ...) __attribute__ ((alias("zend_error"),noreturn));
1292#elif defined(ZEND_WIN32) || defined(DARWIN)
1293ZEND_API void zend_error(int type, const char *format, ...) /* {{{ */
1294{
1295    va_list va;
1296
1297    va_start(va, format);
1298    zend_error_va_list(type, format, va);
1299    va_end(va);
1300}
1301
1302ZEND_API ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...)
1303{
1304    va_list va;
1305
1306    va_start(va, format);
1307    zend_error_va_list(type, format, va);
1308    va_end(va);
1309}
1310/* }}} */
1311#endif
1312
1313ZEND_API void zend_output_debug_string(zend_bool trigger_break, const char *format, ...) /* {{{ */
1314{
1315#if ZEND_DEBUG
1316    va_list args;
1317
1318    va_start(args, format);
1319#   ifdef ZEND_WIN32
1320    {
1321        char output_buf[1024];
1322
1323        vsnprintf(output_buf, 1024, format, args);
1324        OutputDebugString(output_buf);
1325        OutputDebugString("\n");
1326        if (trigger_break && IsDebuggerPresent()) {
1327            DebugBreak();
1328        }
1329    }
1330#   else
1331    vfprintf(stderr, format, args);
1332    fprintf(stderr, "\n");
1333#   endif
1334    va_end(args);
1335#endif
1336}
1337/* }}} */
1338
1339ZEND_API int zend_execute_scripts(int type TSRMLS_DC, zval *retval, int file_count, ...) /* {{{ */
1340{
1341    va_list files;
1342    int i;
1343    zend_file_handle *file_handle;
1344    zend_op_array *op_array;
1345
1346    va_start(files, file_count);
1347    for (i = 0; i < file_count; i++) {
1348        file_handle = va_arg(files, zend_file_handle *);
1349        if (!file_handle) {
1350            continue;
1351        }
1352
1353        op_array = zend_compile_file(file_handle, type TSRMLS_CC);
1354        if (file_handle->opened_path) {
1355            zend_hash_str_add_empty_element(&EG(included_files), file_handle->opened_path, strlen(file_handle->opened_path));
1356        }
1357        zend_destroy_file_handle(file_handle TSRMLS_CC);
1358        if (op_array) {
1359            zend_execute(op_array, retval TSRMLS_CC);
1360            zend_exception_restore(TSRMLS_C);
1361            if (EG(exception)) {
1362                if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1363                    zval orig_user_exception_handler;
1364                    zval params[1], retval2;
1365                    zend_object *old_exception;
1366                    old_exception = EG(exception);
1367                    EG(exception) = NULL;
1368                    ZVAL_OBJ(&params[0], old_exception);
1369                    ZVAL_COPY_VALUE(&orig_user_exception_handler, &EG(user_exception_handler));
1370                    ZVAL_UNDEF(&retval2);
1371                    if (call_user_function_ex(CG(function_table), NULL, &orig_user_exception_handler, &retval2, 1, params, 1, NULL TSRMLS_CC) == SUCCESS) {
1372                        zval_ptr_dtor(&retval2);
1373                        if (EG(exception)) {
1374                            OBJ_RELEASE(EG(exception));
1375                            EG(exception) = NULL;
1376                        }
1377                        OBJ_RELEASE(old_exception);
1378                    } else {
1379                        EG(exception) = old_exception;
1380                        zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
1381                    }
1382                } else {
1383                    zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
1384                }
1385            }
1386            destroy_op_array(op_array TSRMLS_CC);
1387            efree_size(op_array, sizeof(zend_op_array));
1388        } else if (type==ZEND_REQUIRE) {
1389            va_end(files);
1390            return FAILURE;
1391        }
1392    }
1393    va_end(files);
1394
1395    return SUCCESS;
1396}
1397/* }}} */
1398
1399#define COMPILED_STRING_DESCRIPTION_FORMAT "%s(%d) : %s"
1400
1401ZEND_API char *zend_make_compiled_string_description(const char *name TSRMLS_DC) /* {{{ */
1402{
1403    const char *cur_filename;
1404    int cur_lineno;
1405    char *compiled_string_description;
1406
1407    if (zend_is_compiling(TSRMLS_C)) {
1408        cur_filename = zend_get_compiled_filename(TSRMLS_C)->val;
1409        cur_lineno = zend_get_compiled_lineno(TSRMLS_C);
1410    } else if (zend_is_executing(TSRMLS_C)) {
1411        cur_filename = zend_get_executed_filename(TSRMLS_C);
1412        cur_lineno = zend_get_executed_lineno(TSRMLS_C);
1413    } else {
1414        cur_filename = "Unknown";
1415        cur_lineno = 0;
1416    }
1417
1418    zend_spprintf(&compiled_string_description, 0, COMPILED_STRING_DESCRIPTION_FORMAT, cur_filename, cur_lineno, name);
1419    return compiled_string_description;
1420}
1421/* }}} */
1422
1423void free_estring(char **str_p) /* {{{ */
1424{
1425    efree(*str_p);
1426}
1427/* }}} */
1428
1429void free_string_zval(zval *zv) /* {{{ */
1430{
1431    zend_string *str = Z_PTR_P(zv);
1432    zend_string_release(str);
1433}
1434/* }}} */
1435
1436/*
1437 * Local variables:
1438 * tab-width: 4
1439 * c-basic-offset: 4
1440 * indent-tabs-mode: t
1441 * End:
1442 */
1443