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