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