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