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