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