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 <stdio.h>
23#include <signal.h>
24
25#include "zend.h"
26#include "zend_compile.h"
27#include "zend_execute.h"
28#include "zend_API.h"
29#include "zend_ptr_stack.h"
30#include "zend_constants.h"
31#include "zend_extensions.h"
32#include "zend_exceptions.h"
33#include "zend_closures.h"
34#include "zend_generators.h"
35#include "zend_vm.h"
36#include "zend_float.h"
37#ifdef HAVE_SYS_TIME_H
38#include <sys/time.h>
39#endif
40
41ZEND_API void (*zend_execute_ex)(zend_execute_data *execute_data TSRMLS_DC);
42ZEND_API void (*zend_execute_internal)(zend_execute_data *execute_data_ptr, zend_fcall_info *fci, int return_value_used TSRMLS_DC);
43
44/* true globals */
45ZEND_API const zend_fcall_info empty_fcall_info = { 0, NULL, NULL, NULL, NULL, 0, NULL, NULL, 0 };
46ZEND_API const zend_fcall_info_cache empty_fcall_info_cache = { 0, NULL, NULL, NULL, NULL };
47
48#ifdef ZEND_WIN32
49#include <process.h>
50static WNDCLASS wc;
51static HWND timeout_window;
52static HANDLE timeout_thread_event;
53static HANDLE timeout_thread_handle;
54static DWORD timeout_thread_id;
55static int timeout_thread_initialized=0;
56#endif
57
58#if 0&&ZEND_DEBUG
59static void (*original_sigsegv_handler)(int);
60static void zend_handle_sigsegv(int dummy) /* {{{ */
61{
62    fflush(stdout);
63    fflush(stderr);
64    if (original_sigsegv_handler == zend_handle_sigsegv) {
65        signal(SIGSEGV, original_sigsegv_handler);
66    } else {
67        signal(SIGSEGV, SIG_DFL);
68    }
69    {
70        TSRMLS_FETCH();
71
72        fprintf(stderr, "SIGSEGV caught on opcode %d on opline %d of %s() at %s:%d\n\n",
73                active_opline->opcode,
74                active_opline-EG(active_op_array)->opcodes,
75                get_active_function_name(TSRMLS_C),
76                zend_get_executed_filename(TSRMLS_C),
77                zend_get_executed_lineno(TSRMLS_C));
78/* See http://support.microsoft.com/kb/190351 */
79#ifdef PHP_WIN32
80        fflush(stderr);
81#endif
82    }
83    if (original_sigsegv_handler!=zend_handle_sigsegv) {
84        original_sigsegv_handler(dummy);
85    }
86}
87/* }}} */
88#endif
89
90static void zend_extension_activator(zend_extension *extension TSRMLS_DC) /* {{{ */
91{
92    if (extension->activate) {
93        extension->activate();
94    }
95}
96/* }}} */
97
98static void zend_extension_deactivator(zend_extension *extension TSRMLS_DC) /* {{{ */
99{
100    if (extension->deactivate) {
101        extension->deactivate();
102    }
103}
104/* }}} */
105
106static int clean_non_persistent_function(zend_function *function TSRMLS_DC) /* {{{ */
107{
108    return (function->type == ZEND_INTERNAL_FUNCTION) ? ZEND_HASH_APPLY_STOP : ZEND_HASH_APPLY_REMOVE;
109}
110/* }}} */
111
112static int clean_non_persistent_function_full(zend_function *function TSRMLS_DC) /* {{{ */
113{
114    return (function->type == ZEND_INTERNAL_FUNCTION) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
115}
116/* }}} */
117
118static int clean_non_persistent_class(zend_class_entry **ce TSRMLS_DC) /* {{{ */
119{
120    return ((*ce)->type == ZEND_INTERNAL_CLASS) ? ZEND_HASH_APPLY_STOP : ZEND_HASH_APPLY_REMOVE;
121}
122/* }}} */
123
124static int clean_non_persistent_class_full(zend_class_entry **ce TSRMLS_DC) /* {{{ */
125{
126    return ((*ce)->type == ZEND_INTERNAL_CLASS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
127}
128/* }}} */
129
130void init_executor(TSRMLS_D) /* {{{ */
131{
132    zend_init_fpu(TSRMLS_C);
133
134    INIT_ZVAL(EG(uninitialized_zval));
135    /* trick to make uninitialized_zval never be modified, passed by ref, etc. */
136    Z_ADDREF(EG(uninitialized_zval));
137    INIT_ZVAL(EG(error_zval));
138    EG(uninitialized_zval_ptr)=&EG(uninitialized_zval);
139    EG(error_zval_ptr)=&EG(error_zval);
140/* destroys stack frame, therefore makes core dumps worthless */
141#if 0&&ZEND_DEBUG
142    original_sigsegv_handler = signal(SIGSEGV, zend_handle_sigsegv);
143#endif
144    EG(return_value_ptr_ptr) = NULL;
145
146    EG(symtable_cache_ptr) = EG(symtable_cache) - 1;
147    EG(symtable_cache_limit) = EG(symtable_cache) + SYMTABLE_CACHE_SIZE - 1;
148    EG(no_extensions) = 0;
149
150    EG(function_table) = CG(function_table);
151    EG(class_table) = CG(class_table);
152
153    EG(in_execution) = 0;
154    EG(in_autoload) = NULL;
155    EG(autoload_func) = NULL;
156    EG(error_handling) = EH_NORMAL;
157
158    zend_vm_stack_init(TSRMLS_C);
159    zend_vm_stack_push((void *) NULL TSRMLS_CC);
160
161    zend_hash_init(&EG(symbol_table), 50, NULL, ZVAL_PTR_DTOR, 0);
162    EG(active_symbol_table) = &EG(symbol_table);
163
164    zend_llist_apply(&zend_extensions, (llist_apply_func_t) zend_extension_activator TSRMLS_CC);
165    EG(opline_ptr) = NULL;
166
167    zend_hash_init(&EG(included_files), 5, NULL, NULL, 0);
168
169    EG(ticks_count) = 0;
170
171    EG(user_error_handler) = NULL;
172
173    EG(current_execute_data) = NULL;
174
175    zend_stack_init(&EG(user_error_handlers_error_reporting));
176    zend_ptr_stack_init(&EG(user_error_handlers));
177    zend_ptr_stack_init(&EG(user_exception_handlers));
178
179    zend_objects_store_init(&EG(objects_store), 1024);
180
181    EG(full_tables_cleanup) = 0;
182#ifdef ZEND_WIN32
183    EG(timed_out) = 0;
184#endif
185
186    EG(exception) = NULL;
187    EG(prev_exception) = NULL;
188
189    EG(scope) = NULL;
190    EG(called_scope) = NULL;
191
192    EG(This) = NULL;
193
194    EG(active_op_array) = NULL;
195
196    EG(active) = 1;
197    EG(start_op) = NULL;
198}
199/* }}} */
200
201static int zval_call_destructor(zval **zv TSRMLS_DC) /* {{{ */
202{
203    if (Z_TYPE_PP(zv) == IS_OBJECT && Z_REFCOUNT_PP(zv) == 1) {
204        return ZEND_HASH_APPLY_REMOVE;
205    } else {
206        return ZEND_HASH_APPLY_KEEP;
207    }
208}
209/* }}} */
210
211void shutdown_destructors(TSRMLS_D) /* {{{ */
212{
213    zend_try {
214        int symbols;
215        do {
216            symbols = zend_hash_num_elements(&EG(symbol_table));
217            zend_hash_reverse_apply(&EG(symbol_table), (apply_func_t) zval_call_destructor TSRMLS_CC);
218        } while (symbols != zend_hash_num_elements(&EG(symbol_table)));
219        zend_objects_store_call_destructors(&EG(objects_store) TSRMLS_CC);
220    } zend_catch {
221        /* if we couldn't destruct cleanly, mark all objects as destructed anyway */
222        zend_objects_store_mark_destructed(&EG(objects_store) TSRMLS_CC);
223    } zend_end_try();
224}
225/* }}} */
226
227void shutdown_executor(TSRMLS_D) /* {{{ */
228{
229    zend_try {
230
231/* Removed because this can not be safely done, e.g. in this situation:
232   Object 1 creates object 2
233   Object 3 holds reference to object 2.
234   Now when 1 and 2 are destroyed, 3 can still access 2 in its destructor, with
235   very problematic results */
236/*      zend_objects_store_call_destructors(&EG(objects_store) TSRMLS_CC); */
237
238/* Moved after symbol table cleaners, because  some of the cleaners can call
239   destructors, which would use EG(symtable_cache_ptr) and thus leave leaks */
240/*      while (EG(symtable_cache_ptr)>=EG(symtable_cache)) {
241            zend_hash_destroy(*EG(symtable_cache_ptr));
242            efree(*EG(symtable_cache_ptr));
243            EG(symtable_cache_ptr)--;
244        }
245*/
246        zend_llist_apply(&zend_extensions, (llist_apply_func_t) zend_extension_deactivator TSRMLS_CC);
247        zend_hash_graceful_reverse_destroy(&EG(symbol_table));
248    } zend_end_try();
249
250    zend_try {
251        zval *zeh;
252        /* remove error handlers before destroying classes and functions,
253         * so that if handler used some class, crash would not happen */
254        if (EG(user_error_handler)) {
255            zeh = EG(user_error_handler);
256            EG(user_error_handler) = NULL;
257            zval_ptr_dtor(&zeh);
258        }
259
260        if (EG(user_exception_handler)) {
261            zeh = EG(user_exception_handler);
262            EG(user_exception_handler) = NULL;
263            zval_ptr_dtor(&zeh);
264        }
265
266        zend_stack_destroy(&EG(user_error_handlers_error_reporting));
267        zend_stack_init(&EG(user_error_handlers_error_reporting));
268        zend_ptr_stack_clean(&EG(user_error_handlers), ZVAL_DESTRUCTOR, 1);
269        zend_ptr_stack_clean(&EG(user_exception_handlers), ZVAL_DESTRUCTOR, 1);
270    } zend_end_try();
271
272    zend_try {
273        /* Cleanup static data for functions and arrays.
274         * We need a separate cleanup stage because of the following problem:
275         * Suppose we destroy class X, which destroys the class's function table,
276         * and in the function table we have function foo() that has static $bar.
277         * Now if an object of class X is assigned to $bar, its destructor will be
278         * called and will fail since X's function table is in mid-destruction.
279         * So we want first of all to clean up all data and then move to tables destruction.
280         * Note that only run-time accessed data need to be cleaned up, pre-defined data can
281         * not contain objects and thus are not probelmatic */
282        if (EG(full_tables_cleanup)) {
283            zend_hash_apply(EG(function_table), (apply_func_t) zend_cleanup_function_data_full TSRMLS_CC);
284            zend_hash_apply(EG(class_table), (apply_func_t) zend_cleanup_class_data TSRMLS_CC);
285        } else {
286            zend_hash_reverse_apply(EG(function_table), (apply_func_t) zend_cleanup_function_data TSRMLS_CC);
287            zend_hash_reverse_apply(EG(class_table), (apply_func_t) zend_cleanup_user_class_data TSRMLS_CC);
288            zend_cleanup_internal_classes(TSRMLS_C);
289        }
290    } zend_end_try();
291
292    zend_try {
293        zend_objects_store_free_object_storage(&EG(objects_store) TSRMLS_CC);
294
295        zend_vm_stack_destroy(TSRMLS_C);
296
297        /* Destroy all op arrays */
298        if (EG(full_tables_cleanup)) {
299            zend_hash_reverse_apply(EG(function_table), (apply_func_t) clean_non_persistent_function_full TSRMLS_CC);
300            zend_hash_reverse_apply(EG(class_table), (apply_func_t) clean_non_persistent_class_full TSRMLS_CC);
301        } else {
302            zend_hash_reverse_apply(EG(function_table), (apply_func_t) clean_non_persistent_function TSRMLS_CC);
303            zend_hash_reverse_apply(EG(class_table), (apply_func_t) clean_non_persistent_class TSRMLS_CC);
304        }
305
306        while (EG(symtable_cache_ptr)>=EG(symtable_cache)) {
307            zend_hash_destroy(*EG(symtable_cache_ptr));
308            FREE_HASHTABLE(*EG(symtable_cache_ptr));
309            EG(symtable_cache_ptr)--;
310        }
311    } zend_end_try();
312
313    zend_try {
314        clean_non_persistent_constants(TSRMLS_C);
315    } zend_end_try();
316
317    zend_try {
318#if 0&&ZEND_DEBUG
319    signal(SIGSEGV, original_sigsegv_handler);
320#endif
321
322        zend_hash_destroy(&EG(included_files));
323
324        zend_stack_destroy(&EG(user_error_handlers_error_reporting));
325        zend_ptr_stack_destroy(&EG(user_error_handlers));
326        zend_ptr_stack_destroy(&EG(user_exception_handlers));
327        zend_objects_store_destroy(&EG(objects_store));
328        if (EG(in_autoload)) {
329            zend_hash_destroy(EG(in_autoload));
330            FREE_HASHTABLE(EG(in_autoload));
331        }
332    } zend_end_try();
333
334    zend_shutdown_fpu(TSRMLS_C);
335
336    EG(active) = 0;
337}
338/* }}} */
339
340/* return class name and "::" or "". */
341ZEND_API const char *get_active_class_name(const char **space TSRMLS_DC) /* {{{ */
342{
343    if (!zend_is_executing(TSRMLS_C)) {
344        if (space) {
345            *space = "";
346        }
347        return "";
348    }
349    switch (EG(current_execute_data)->function_state.function->type) {
350        case ZEND_USER_FUNCTION:
351        case ZEND_INTERNAL_FUNCTION:
352        {
353            zend_class_entry *ce = EG(current_execute_data)->function_state.function->common.scope;
354
355            if (space) {
356                *space = ce ? "::" : "";
357            }
358            return ce ? ce->name : "";
359        }
360        default:
361            if (space) {
362                *space = "";
363            }
364            return "";
365    }
366}
367/* }}} */
368
369ZEND_API const char *get_active_function_name(TSRMLS_D) /* {{{ */
370{
371    if (!zend_is_executing(TSRMLS_C)) {
372        return NULL;
373    }
374    switch (EG(current_execute_data)->function_state.function->type) {
375        case ZEND_USER_FUNCTION: {
376                const char *function_name = ((zend_op_array *) EG(current_execute_data)->function_state.function)->function_name;
377
378                if (function_name) {
379                    return function_name;
380                } else {
381                    return "main";
382                }
383            }
384            break;
385        case ZEND_INTERNAL_FUNCTION:
386            return ((zend_internal_function *) EG(current_execute_data)->function_state.function)->function_name;
387            break;
388        default:
389            return NULL;
390    }
391}
392/* }}} */
393
394ZEND_API const char *zend_get_executed_filename(TSRMLS_D) /* {{{ */
395{
396    if (EG(active_op_array)) {
397        return EG(active_op_array)->filename;
398    } else {
399        return "[no active file]";
400    }
401}
402/* }}} */
403
404ZEND_API uint zend_get_executed_lineno(TSRMLS_D) /* {{{ */
405{
406    if(EG(exception) && EG(opline_ptr) && active_opline->opcode == ZEND_HANDLE_EXCEPTION &&
407        active_opline->lineno == 0 && EG(opline_before_exception)) {
408        return EG(opline_before_exception)->lineno;
409    }
410    if (EG(opline_ptr)) {
411        return active_opline->lineno;
412    } else {
413        return 0;
414    }
415}
416/* }}} */
417
418ZEND_API zend_bool zend_is_executing(TSRMLS_D) /* {{{ */
419{
420    return EG(in_execution);
421}
422/* }}} */
423
424ZEND_API void _zval_ptr_dtor(zval **zval_ptr ZEND_FILE_LINE_DC) /* {{{ */
425{
426    TSRMLS_FETCH();
427    i_zval_ptr_dtor(*zval_ptr ZEND_FILE_LINE_RELAY_CC TSRMLS_CC);
428}
429/* }}} */
430
431ZEND_API void _zval_internal_ptr_dtor(zval **zval_ptr ZEND_FILE_LINE_DC) /* {{{ */
432{
433#if DEBUG_ZEND>=2
434    printf("Reducing refcount for %x (%x): %d->%d\n", *zval_ptr, zval_ptr, Z_REFCOUNT_PP(zval_ptr), Z_REFCOUNT_PP(zval_ptr) - 1);
435#endif
436    Z_DELREF_PP(zval_ptr);
437    if (Z_REFCOUNT_PP(zval_ptr) == 0) {
438        zval_internal_dtor(*zval_ptr);
439        free(*zval_ptr);
440    } else if (Z_REFCOUNT_PP(zval_ptr) == 1) {
441        Z_UNSET_ISREF_PP(zval_ptr);
442    }
443}
444/* }}} */
445
446ZEND_API int zend_is_true(zval *op TSRMLS_DC) /* {{{ */
447{
448    return i_zend_is_true(op TSRMLS_CC);
449}
450/* }}} */
451
452#define IS_VISITED_CONSTANT         0x80
453#define IS_CONSTANT_VISITED(p)      (Z_TYPE_P(p) & IS_VISITED_CONSTANT)
454#define Z_REAL_TYPE_P(p)            (Z_TYPE_P(p) & ~IS_VISITED_CONSTANT)
455#define MARK_CONSTANT_VISITED(p)    Z_TYPE_P(p) |= IS_VISITED_CONSTANT
456
457ZEND_API int zval_update_constant_ex(zval **pp, zend_bool inline_change, zend_class_entry *scope TSRMLS_DC) /* {{{ */
458{
459    zval *p = *pp;
460    zval const_value;
461    char *colon;
462
463    if (IS_CONSTANT_VISITED(p)) {
464        zend_error(E_ERROR, "Cannot declare self-referencing constant '%s'", Z_STRVAL_P(p));
465    } else if ((Z_TYPE_P(p) & IS_CONSTANT_TYPE_MASK) == IS_CONSTANT) {
466        int refcount;
467        zend_uchar is_ref;
468
469        SEPARATE_ZVAL_IF_NOT_REF(pp);
470        p = *pp;
471
472        MARK_CONSTANT_VISITED(p);
473
474        refcount = Z_REFCOUNT_P(p);
475        is_ref = Z_ISREF_P(p);
476
477        if (!zend_get_constant_ex(p->value.str.val, p->value.str.len, &const_value, scope, Z_REAL_TYPE_P(p) TSRMLS_CC)) {
478            char *actual = Z_STRVAL_P(p);
479
480            if ((colon = (char*)zend_memrchr(Z_STRVAL_P(p), ':', Z_STRLEN_P(p)))) {
481                zend_error(E_ERROR, "Undefined class constant '%s'", Z_STRVAL_P(p));
482                Z_STRLEN_P(p) -= ((colon - Z_STRVAL_P(p)) + 1);
483                if (inline_change) {
484                    colon = estrndup(colon, Z_STRLEN_P(p));
485                    str_efree(Z_STRVAL_P(p));
486                    Z_STRVAL_P(p) = colon;
487                } else {
488                    Z_STRVAL_P(p) = colon + 1;
489                }
490            } else {
491                char *save = actual, *slash;
492                int actual_len = Z_STRLEN_P(p);
493                if ((Z_TYPE_P(p) & IS_CONSTANT_UNQUALIFIED) && (slash = (char *)zend_memrchr(actual, '\\', actual_len))) {
494                    actual = slash + 1;
495                    actual_len -= (actual - Z_STRVAL_P(p));
496                    if (inline_change) {
497                        actual = estrndup(actual, actual_len);
498                        Z_STRVAL_P(p) = actual;
499                        Z_STRLEN_P(p) = actual_len;
500                    }
501                }
502                if (actual[0] == '\\') {
503                    if (inline_change) {
504                        memmove(Z_STRVAL_P(p), Z_STRVAL_P(p)+1, Z_STRLEN_P(p));
505                        --Z_STRLEN_P(p);
506                    } else {
507                        ++actual;
508                    }
509                    --actual_len;
510                }
511                if ((Z_TYPE_P(p) & IS_CONSTANT_UNQUALIFIED) == 0) {
512                    int fix_save = 0;
513                    if (save[0] == '\\') {
514                        save++;
515                        fix_save = 1;
516                    }
517                    zend_error(E_ERROR, "Undefined constant '%s'", save);
518                    if (fix_save) {
519                        save--;
520                    }
521                    if (inline_change) {
522                        str_efree(save);
523                    }
524                    save = NULL;
525                }
526                if (inline_change && save && save != actual) {
527                    str_efree(save);
528                }
529                zend_error(E_NOTICE, "Use of undefined constant %s - assumed '%s'",  actual,  actual);
530                p->type = IS_STRING;
531                if (!inline_change) {
532                    Z_STRVAL_P(p) = actual;
533                    Z_STRLEN_P(p) = actual_len;
534                    zval_copy_ctor(p);
535                }
536            }
537        } else {
538            if (inline_change) {
539                str_efree(Z_STRVAL_P(p));
540            }
541            *p = const_value;
542        }
543
544        Z_SET_REFCOUNT_P(p, refcount);
545        Z_SET_ISREF_TO_P(p, is_ref);
546    } else if (Z_TYPE_P(p) == IS_CONSTANT_AST) {
547        SEPARATE_ZVAL_IF_NOT_REF(pp);
548        p = *pp;
549
550        zend_ast_evaluate(&const_value, Z_AST_P(p), scope TSRMLS_CC);
551        if (inline_change) {
552            zend_ast_destroy(Z_AST_P(p));
553        }
554        ZVAL_COPY_VALUE(p, &const_value);
555    }
556    return 0;
557}
558/* }}} */
559
560ZEND_API int zval_update_constant_inline_change(zval **pp, zend_class_entry *scope TSRMLS_DC) /* {{{ */
561{
562    return zval_update_constant_ex(pp, 1, scope TSRMLS_CC);
563}
564/* }}} */
565
566ZEND_API int zval_update_constant_no_inline_change(zval **pp, zend_class_entry *scope TSRMLS_DC) /* {{{ */
567{
568    return zval_update_constant_ex(pp, 0, scope TSRMLS_CC);
569}
570/* }}} */
571
572ZEND_API int zval_update_constant(zval **pp, zend_bool inline_change TSRMLS_DC) /* {{{ */
573{
574    return zval_update_constant_ex(pp, inline_change, NULL TSRMLS_CC);
575}
576/* }}} */
577
578int call_user_function(HashTable *function_table, zval **object_pp, zval *function_name, zval *retval_ptr, zend_uint param_count, zval *params[] TSRMLS_DC) /* {{{ */
579{
580    zval ***params_array;
581    zend_uint i;
582    int ex_retval;
583    zval *local_retval_ptr = NULL;
584
585    if (param_count) {
586        params_array = (zval ***) emalloc(sizeof(zval **)*param_count);
587        for (i=0; i<param_count; i++) {
588            params_array[i] = &params[i];
589        }
590    } else {
591        params_array = NULL;
592    }
593    ex_retval = call_user_function_ex(function_table, object_pp, function_name, &local_retval_ptr, param_count, params_array, 1, NULL TSRMLS_CC);
594    if (local_retval_ptr) {
595        COPY_PZVAL_TO_ZVAL(*retval_ptr, local_retval_ptr);
596    } else {
597        INIT_ZVAL(*retval_ptr);
598    }
599    if (params_array) {
600        efree(params_array);
601    }
602    return ex_retval;
603}
604/* }}} */
605
606int call_user_function_ex(HashTable *function_table, zval **object_pp, zval *function_name, zval **retval_ptr_ptr, zend_uint param_count, zval **params[], int no_separation, HashTable *symbol_table TSRMLS_DC) /* {{{ */
607{
608    zend_fcall_info fci;
609
610    fci.size = sizeof(fci);
611    fci.function_table = function_table;
612    fci.object_ptr = object_pp ? *object_pp : NULL;
613    fci.function_name = function_name;
614    fci.retval_ptr_ptr = retval_ptr_ptr;
615    fci.param_count = param_count;
616    fci.params = params;
617    fci.no_separation = (zend_bool) no_separation;
618    fci.symbol_table = symbol_table;
619
620    return zend_call_function(&fci, NULL TSRMLS_CC);
621}
622/* }}} */
623
624int zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache TSRMLS_DC) /* {{{ */
625{
626    zend_uint i;
627    zval **original_return_value;
628    HashTable *calling_symbol_table;
629    zend_op_array *original_op_array;
630    zend_op **original_opline_ptr;
631    zend_class_entry *current_scope;
632    zend_class_entry *current_called_scope;
633    zend_class_entry *calling_scope = NULL;
634    zend_class_entry *called_scope = NULL;
635    zval *current_this;
636    zend_execute_data execute_data;
637    zend_fcall_info_cache fci_cache_local;
638
639    *fci->retval_ptr_ptr = NULL;
640
641    if (!EG(active)) {
642        return FAILURE; /* executor is already inactive */
643    }
644
645    if (EG(exception)) {
646        return FAILURE; /* we would result in an instable executor otherwise */
647    }
648
649    switch (fci->size) {
650        case sizeof(zend_fcall_info):
651            break; /* nothing to do currently */
652        default:
653            zend_error(E_ERROR, "Corrupted fcall_info provided to zend_call_function()");
654            break;
655    }
656
657    /* Initialize execute_data */
658    if (EG(current_execute_data)) {
659        execute_data = *EG(current_execute_data);
660        EX(op_array) = NULL;
661        EX(opline) = NULL;
662        EX(object) = NULL;
663    } else {
664        /* This only happens when we're called outside any execute()'s
665         * It shouldn't be strictly necessary to NULL execute_data out,
666         * but it may make bugs easier to spot
667         */
668        memset(&execute_data, 0, sizeof(zend_execute_data));
669    }
670
671    if (!fci_cache || !fci_cache->initialized) {
672        char *callable_name;
673        char *error = NULL;
674
675        if (!fci_cache) {
676            fci_cache = &fci_cache_local;
677        }
678
679        if (!zend_is_callable_ex(fci->function_name, fci->object_ptr, IS_CALLABLE_CHECK_SILENT, &callable_name, NULL, fci_cache, &error TSRMLS_CC)) {
680            if (error) {
681                zend_error(E_WARNING, "Invalid callback %s, %s", callable_name, error);
682                efree(error);
683            }
684            if (callable_name) {
685                efree(callable_name);
686            }
687            return FAILURE;
688        } else if (error) {
689            /* Capitalize the first latter of the error message */
690            if (error[0] >= 'a' && error[0] <= 'z') {
691                error[0] += ('A' - 'a');
692            }
693            zend_error(E_STRICT, "%s", error);
694            efree(error);
695        }
696        efree(callable_name);
697    }
698
699    EX(function_state).function = fci_cache->function_handler;
700    calling_scope = fci_cache->calling_scope;
701    called_scope = fci_cache->called_scope;
702    fci->object_ptr = fci_cache->object_ptr;
703    EX(object) = fci->object_ptr;
704    if (fci->object_ptr && Z_TYPE_P(fci->object_ptr) == IS_OBJECT &&
705        (!EG(objects_store).object_buckets || !EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(fci->object_ptr)].valid)) {
706        return FAILURE;
707    }
708
709    if (EX(function_state).function->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED)) {
710        if (EX(function_state).function->common.fn_flags & ZEND_ACC_ABSTRACT) {
711            zend_error_noreturn(E_ERROR, "Cannot call abstract method %s::%s()", EX(function_state).function->common.scope->name, EX(function_state).function->common.function_name);
712        }
713        if (EX(function_state).function->common.fn_flags & ZEND_ACC_DEPRECATED) {
714            zend_error(E_DEPRECATED, "Function %s%s%s() is deprecated",
715                EX(function_state).function->common.scope ? EX(function_state).function->common.scope->name : "",
716                EX(function_state).function->common.scope ? "::" : "",
717                EX(function_state).function->common.function_name);
718        }
719    }
720
721    ZEND_VM_STACK_GROW_IF_NEEDED(fci->param_count + 1);
722
723    for (i=0; i<fci->param_count; i++) {
724        zval *param;
725
726        if (ARG_SHOULD_BE_SENT_BY_REF(EX(function_state).function, i + 1)) {
727            if (!PZVAL_IS_REF(*fci->params[i]) && Z_REFCOUNT_PP(fci->params[i]) > 1) {
728                zval *new_zval;
729
730                if (fci->no_separation &&
731                    !ARG_MAY_BE_SENT_BY_REF(EX(function_state).function, i + 1)) {
732                    if (i || UNEXPECTED(ZEND_VM_STACK_ELEMETS(EG(argument_stack)) == (EG(argument_stack)->top))) {
733                        /* hack to clean up the stack */
734                        zend_vm_stack_push((void *) (zend_uintptr_t)i TSRMLS_CC);
735                        zend_vm_stack_clear_multiple(0 TSRMLS_CC);
736                    }
737
738                    zend_error(E_WARNING, "Parameter %d to %s%s%s() expected to be a reference, value given",
739                        i+1,
740                        EX(function_state).function->common.scope ? EX(function_state).function->common.scope->name : "",
741                        EX(function_state).function->common.scope ? "::" : "",
742                        EX(function_state).function->common.function_name);
743                    return FAILURE;
744                }
745
746                ALLOC_ZVAL(new_zval);
747                *new_zval = **fci->params[i];
748                zval_copy_ctor(new_zval);
749                Z_SET_REFCOUNT_P(new_zval, 1);
750                Z_DELREF_PP(fci->params[i]);
751                *fci->params[i] = new_zval;
752            }
753            Z_ADDREF_PP(fci->params[i]);
754            Z_SET_ISREF_PP(fci->params[i]);
755            param = *fci->params[i];
756        } else if (PZVAL_IS_REF(*fci->params[i]) &&
757                   /* don't separate references for __call */
758                   (EX(function_state).function->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) == 0 ) {
759            ALLOC_ZVAL(param);
760            *param = **(fci->params[i]);
761            INIT_PZVAL(param);
762            zval_copy_ctor(param);
763        } else if (*fci->params[i] != &EG(uninitialized_zval)) {
764            Z_ADDREF_PP(fci->params[i]);
765            param = *fci->params[i];
766        } else {
767            ALLOC_ZVAL(param);
768            *param = **(fci->params[i]);
769            INIT_PZVAL(param);
770        }
771        zend_vm_stack_push(param TSRMLS_CC);
772    }
773
774    EX(function_state).arguments = zend_vm_stack_top(TSRMLS_C);
775    zend_vm_stack_push((void*)(zend_uintptr_t)fci->param_count TSRMLS_CC);
776
777    current_scope = EG(scope);
778    EG(scope) = calling_scope;
779
780    current_this = EG(This);
781
782    current_called_scope = EG(called_scope);
783    if (called_scope) {
784        EG(called_scope) = called_scope;
785    } else if (EX(function_state).function->type != ZEND_INTERNAL_FUNCTION) {
786        EG(called_scope) = NULL;
787    }
788
789    if (fci->object_ptr) {
790        if ((EX(function_state).function->common.fn_flags & ZEND_ACC_STATIC)) {
791            EG(This) = NULL;
792        } else {
793            EG(This) = fci->object_ptr;
794
795            if (!PZVAL_IS_REF(EG(This))) {
796                Z_ADDREF_P(EG(This)); /* For $this pointer */
797            } else {
798                zval *this_ptr;
799
800                ALLOC_ZVAL(this_ptr);
801                *this_ptr = *EG(This);
802                INIT_PZVAL(this_ptr);
803                zval_copy_ctor(this_ptr);
804                EG(This) = this_ptr;
805            }
806        }
807    } else {
808        EG(This) = NULL;
809    }
810
811    EX(prev_execute_data) = EG(current_execute_data);
812    EG(current_execute_data) = &execute_data;
813
814    if (EX(function_state).function->type == ZEND_USER_FUNCTION) {
815        calling_symbol_table = EG(active_symbol_table);
816        EG(scope) = EX(function_state).function->common.scope;
817        if (fci->symbol_table) {
818            EG(active_symbol_table) = fci->symbol_table;
819        } else {
820            EG(active_symbol_table) = NULL;
821        }
822
823        original_return_value = EG(return_value_ptr_ptr);
824        original_op_array = EG(active_op_array);
825        EG(return_value_ptr_ptr) = fci->retval_ptr_ptr;
826        EG(active_op_array) = (zend_op_array *) EX(function_state).function;
827        original_opline_ptr = EG(opline_ptr);
828
829        if (EG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
830            *fci->retval_ptr_ptr = zend_generator_create_zval(EG(active_op_array) TSRMLS_CC);
831        } else {
832            zend_execute(EG(active_op_array) TSRMLS_CC);
833        }
834
835        if (!fci->symbol_table && EG(active_symbol_table)) {
836            zend_clean_and_cache_symbol_table(EG(active_symbol_table) TSRMLS_CC);
837        }
838        EG(active_symbol_table) = calling_symbol_table;
839        EG(active_op_array) = original_op_array;
840        EG(return_value_ptr_ptr)=original_return_value;
841        EG(opline_ptr) = original_opline_ptr;
842    } else if (EX(function_state).function->type == ZEND_INTERNAL_FUNCTION) {
843        int call_via_handler = (EX(function_state).function->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) != 0;
844        ALLOC_INIT_ZVAL(*fci->retval_ptr_ptr);
845        if (EX(function_state).function->common.scope) {
846            EG(scope) = EX(function_state).function->common.scope;
847        }
848        if (EXPECTED(zend_execute_internal == NULL)) {
849            /* saves one function call if zend_execute_internal is not used */
850            EX(function_state).function->internal_function.handler(fci->param_count, *fci->retval_ptr_ptr, fci->retval_ptr_ptr, fci->object_ptr, 1 TSRMLS_CC);
851        } else {
852            zend_execute_internal(&execute_data, fci, 1 TSRMLS_CC);
853        }
854        /*  We shouldn't fix bad extensions here,
855            because it can break proper ones (Bug #34045)
856        if (!EX(function_state).function->common.return_reference)
857        {
858            INIT_PZVAL(*fci->retval_ptr_ptr);
859        }*/
860        if (EG(exception) && fci->retval_ptr_ptr) {
861            zval_ptr_dtor(fci->retval_ptr_ptr);
862            *fci->retval_ptr_ptr = NULL;
863        }
864
865        if (call_via_handler) {
866            /* We must re-initialize function again */
867            fci_cache->initialized = 0;
868        }
869    } else { /* ZEND_OVERLOADED_FUNCTION */
870        ALLOC_INIT_ZVAL(*fci->retval_ptr_ptr);
871
872        /* Not sure what should be done here if it's a static method */
873        if (fci->object_ptr) {
874            Z_OBJ_HT_P(fci->object_ptr)->call_method(EX(function_state).function->common.function_name, fci->param_count, *fci->retval_ptr_ptr, fci->retval_ptr_ptr, fci->object_ptr, 1 TSRMLS_CC);
875        } else {
876            zend_error_noreturn(E_ERROR, "Cannot call overloaded function for non-object");
877        }
878
879        if (EX(function_state).function->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY) {
880            efree((char*)EX(function_state).function->common.function_name);
881        }
882        efree(EX(function_state).function);
883
884        if (EG(exception) && fci->retval_ptr_ptr) {
885            zval_ptr_dtor(fci->retval_ptr_ptr);
886            *fci->retval_ptr_ptr = NULL;
887        }
888    }
889    zend_vm_stack_clear_multiple(0 TSRMLS_CC);
890
891    if (EG(This)) {
892        zval_ptr_dtor(&EG(This));
893    }
894    EG(called_scope) = current_called_scope;
895    EG(scope) = current_scope;
896    EG(This) = current_this;
897    EG(current_execute_data) = EX(prev_execute_data);
898
899    if (EG(exception)) {
900        zend_throw_exception_internal(NULL TSRMLS_CC);
901    }
902    return SUCCESS;
903}
904/* }}} */
905
906ZEND_API int zend_lookup_class_ex(const char *name, int name_length, const zend_literal *key, int use_autoload, zend_class_entry ***ce TSRMLS_DC) /* {{{ */
907{
908    zval **args[1];
909    zval autoload_function;
910    zval *class_name_ptr;
911    zval *retval_ptr = NULL;
912    int retval, lc_length;
913    char *lc_name;
914    char *lc_free;
915    zend_fcall_info fcall_info;
916    zend_fcall_info_cache fcall_cache;
917    char dummy = 1;
918    ulong hash;
919    ALLOCA_FLAG(use_heap)
920
921    if (key) {
922        lc_name = Z_STRVAL(key->constant);
923        lc_length = Z_STRLEN(key->constant) + 1;
924        hash = key->hash_value;
925    } else {
926        if (name == NULL || !name_length) {
927            return FAILURE;
928        }
929
930        lc_free = lc_name = do_alloca(name_length + 1, use_heap);
931        zend_str_tolower_copy(lc_name, name, name_length);
932        lc_length = name_length + 1;
933
934        if (lc_name[0] == '\\') {
935            lc_name += 1;
936            lc_length -= 1;
937        }
938
939        hash = zend_inline_hash_func(lc_name, lc_length);
940    }
941
942    if (zend_hash_quick_find(EG(class_table), lc_name, lc_length, hash, (void **) ce) == SUCCESS) {
943        if (!key) {
944            free_alloca(lc_free, use_heap);
945        }
946        return SUCCESS;
947    }
948
949    /* The compiler is not-reentrant. Make sure we __autoload() only during run-time
950     * (doesn't impact functionality of __autoload()
951    */
952    if (!use_autoload || zend_is_compiling(TSRMLS_C)) {
953        if (!key) {
954            free_alloca(lc_free, use_heap);
955        }
956        return FAILURE;
957    }
958
959    /* Verify class name before passing it to __autoload() */
960    if (strspn(name, "0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377\\") != name_length) {
961        if (!key) {
962            free_alloca(lc_free, use_heap);
963        }
964        return FAILURE;
965    }
966
967    if (EG(in_autoload) == NULL) {
968        ALLOC_HASHTABLE(EG(in_autoload));
969        zend_hash_init(EG(in_autoload), 0, NULL, NULL, 0);
970    }
971
972    if (zend_hash_quick_add(EG(in_autoload), lc_name, lc_length, hash, (void**)&dummy, sizeof(char), NULL) == FAILURE) {
973        if (!key) {
974            free_alloca(lc_free, use_heap);
975        }
976        return FAILURE;
977    }
978
979    ZVAL_STRINGL(&autoload_function, ZEND_AUTOLOAD_FUNC_NAME, sizeof(ZEND_AUTOLOAD_FUNC_NAME) - 1, 0);
980
981    ALLOC_ZVAL(class_name_ptr);
982    INIT_PZVAL(class_name_ptr);
983    if (name[0] == '\\') {
984        ZVAL_STRINGL(class_name_ptr, name+1, name_length-1, 1);
985    } else {
986        ZVAL_STRINGL(class_name_ptr, name, name_length, 1);
987    }
988
989    args[0] = &class_name_ptr;
990
991    fcall_info.size = sizeof(fcall_info);
992    fcall_info.function_table = EG(function_table);
993    fcall_info.function_name = &autoload_function;
994    fcall_info.symbol_table = NULL;
995    fcall_info.retval_ptr_ptr = &retval_ptr;
996    fcall_info.param_count = 1;
997    fcall_info.params = args;
998    fcall_info.object_ptr = NULL;
999    fcall_info.no_separation = 1;
1000
1001    fcall_cache.initialized = EG(autoload_func) ? 1 : 0;
1002    fcall_cache.function_handler = EG(autoload_func);
1003    fcall_cache.calling_scope = NULL;
1004    fcall_cache.called_scope = NULL;
1005    fcall_cache.object_ptr = NULL;
1006
1007    zend_exception_save(TSRMLS_C);
1008    retval = zend_call_function(&fcall_info, &fcall_cache TSRMLS_CC);
1009    zend_exception_restore(TSRMLS_C);
1010
1011    EG(autoload_func) = fcall_cache.function_handler;
1012
1013    zval_ptr_dtor(&class_name_ptr);
1014
1015    zend_hash_quick_del(EG(in_autoload), lc_name, lc_length, hash);
1016
1017    if (retval_ptr) {
1018        zval_ptr_dtor(&retval_ptr);
1019    }
1020
1021    if (retval == SUCCESS) {
1022        retval = zend_hash_quick_find(EG(class_table), lc_name, lc_length, hash, (void **) ce);
1023    }
1024    if (!key) {
1025        free_alloca(lc_free, use_heap);
1026    }
1027    return retval;
1028}
1029/* }}} */
1030
1031ZEND_API int zend_lookup_class(const char *name, int name_length, zend_class_entry ***ce TSRMLS_DC) /* {{{ */
1032{
1033    return zend_lookup_class_ex(name, name_length, NULL, 1, ce TSRMLS_CC);
1034}
1035/* }}} */
1036
1037ZEND_API int zend_eval_stringl(char *str, int str_len, zval *retval_ptr, char *string_name TSRMLS_DC) /* {{{ */
1038{
1039    zval pv;
1040    zend_op_array *new_op_array;
1041    zend_op_array *original_active_op_array = EG(active_op_array);
1042    zend_uint original_compiler_options;
1043    int retval;
1044
1045    if (retval_ptr) {
1046        Z_STRLEN(pv) = str_len + sizeof("return ;") - 1;
1047        Z_STRVAL(pv) = emalloc(Z_STRLEN(pv) + 1);
1048        memcpy(Z_STRVAL(pv), "return ", sizeof("return ") - 1);
1049        memcpy(Z_STRVAL(pv) + sizeof("return ") - 1, str, str_len);
1050        Z_STRVAL(pv)[Z_STRLEN(pv) - 1] = ';';
1051        Z_STRVAL(pv)[Z_STRLEN(pv)] = '\0';
1052    } else {
1053        Z_STRLEN(pv) = str_len;
1054        Z_STRVAL(pv) = str;
1055    }
1056    Z_TYPE(pv) = IS_STRING;
1057
1058    /*printf("Evaluating '%s'\n", pv.value.str.val);*/
1059
1060    original_compiler_options = CG(compiler_options);
1061    CG(compiler_options) = ZEND_COMPILE_DEFAULT_FOR_EVAL;
1062    new_op_array = zend_compile_string(&pv, string_name TSRMLS_CC);
1063    CG(compiler_options) = original_compiler_options;
1064
1065    if (new_op_array) {
1066        zval *local_retval_ptr=NULL;
1067        zval **original_return_value_ptr_ptr = EG(return_value_ptr_ptr);
1068        zend_op **original_opline_ptr = EG(opline_ptr);
1069        int orig_interactive = CG(interactive);
1070
1071        EG(return_value_ptr_ptr) = &local_retval_ptr;
1072        EG(active_op_array) = new_op_array;
1073        EG(no_extensions)=1;
1074        if (!EG(active_symbol_table)) {
1075            zend_rebuild_symbol_table(TSRMLS_C);
1076        }
1077        CG(interactive) = 0;
1078
1079        zend_try {
1080            zend_execute(new_op_array TSRMLS_CC);
1081        } zend_catch {
1082            destroy_op_array(new_op_array TSRMLS_CC);
1083            efree(new_op_array);
1084            zend_bailout();
1085        } zend_end_try();
1086
1087        CG(interactive) = orig_interactive;
1088        if (local_retval_ptr) {
1089            if (retval_ptr) {
1090                COPY_PZVAL_TO_ZVAL(*retval_ptr, local_retval_ptr);
1091            } else {
1092                zval_ptr_dtor(&local_retval_ptr);
1093            }
1094        } else {
1095            if (retval_ptr) {
1096                INIT_ZVAL(*retval_ptr);
1097            }
1098        }
1099
1100        EG(no_extensions)=0;
1101        EG(opline_ptr) = original_opline_ptr;
1102        EG(active_op_array) = original_active_op_array;
1103        destroy_op_array(new_op_array TSRMLS_CC);
1104        efree(new_op_array);
1105        EG(return_value_ptr_ptr) = original_return_value_ptr_ptr;
1106        retval = SUCCESS;
1107    } else {
1108        retval = FAILURE;
1109    }
1110    if (retval_ptr) {
1111        zval_dtor(&pv);
1112    }
1113    return retval;
1114}
1115/* }}} */
1116
1117ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSRMLS_DC) /* {{{ */
1118{
1119    return zend_eval_stringl(str, strlen(str), retval_ptr, string_name TSRMLS_CC);
1120}
1121/* }}} */
1122
1123ZEND_API int zend_eval_stringl_ex(char *str, int str_len, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC) /* {{{ */
1124{
1125    int result;
1126
1127    result = zend_eval_stringl(str, str_len, retval_ptr, string_name TSRMLS_CC);
1128    if (handle_exceptions && EG(exception)) {
1129        zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
1130        result = FAILURE;
1131    }
1132    return result;
1133}
1134/* }}} */
1135
1136ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC) /* {{{ */
1137{
1138    return zend_eval_stringl_ex(str, strlen(str), retval_ptr, string_name, handle_exceptions TSRMLS_CC);
1139}
1140/* }}} */
1141
1142void execute_new_code(TSRMLS_D) /* {{{ */
1143{
1144    zend_op *opline, *end;
1145    zend_op *ret_opline;
1146    int orig_interactive;
1147
1148    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_INTERACTIVE)
1149        || CG(context).backpatch_count>0
1150        || CG(active_op_array)->function_name
1151        || CG(active_op_array)->type!=ZEND_USER_FUNCTION) {
1152        return;
1153    }
1154
1155    ret_opline = get_next_op(CG(active_op_array) TSRMLS_CC);
1156    ret_opline->opcode = ZEND_RETURN;
1157    ret_opline->op1_type = IS_CONST;
1158    ret_opline->op1.constant = zend_add_literal(CG(active_op_array), &EG(uninitialized_zval) TSRMLS_CC);
1159    SET_UNUSED(ret_opline->op2);
1160
1161    if (!EG(start_op)) {
1162        EG(start_op) = CG(active_op_array)->opcodes;
1163    }
1164
1165    opline=EG(start_op);
1166    end=CG(active_op_array)->opcodes+CG(active_op_array)->last;
1167
1168    while (opline<end) {
1169        if (opline->op1_type == IS_CONST) {
1170            opline->op1.zv = &CG(active_op_array)->literals[opline->op1.constant].constant;
1171        }
1172        if (opline->op2_type == IS_CONST) {
1173            opline->op2.zv = &CG(active_op_array)->literals[opline->op2.constant].constant;
1174        }
1175        switch (opline->opcode) {
1176            case ZEND_GOTO:
1177                if (Z_TYPE_P(opline->op2.zv) != IS_LONG) {
1178                    zend_resolve_goto_label(CG(active_op_array), opline, 1 TSRMLS_CC);
1179                }
1180                /* break omitted intentionally */
1181            case ZEND_JMP:
1182                opline->op1.jmp_addr = &CG(active_op_array)->opcodes[opline->op1.opline_num];
1183                break;
1184            case ZEND_JMPZ:
1185            case ZEND_JMPNZ:
1186            case ZEND_JMPZ_EX:
1187            case ZEND_JMPNZ_EX:
1188            case ZEND_JMP_SET:
1189            case ZEND_JMP_SET_VAR:
1190                opline->op2.jmp_addr = &CG(active_op_array)->opcodes[opline->op2.opline_num];
1191                break;
1192        }
1193        ZEND_VM_SET_OPCODE_HANDLER(opline);
1194        opline++;
1195    }
1196
1197    zend_release_labels(1 TSRMLS_CC);
1198
1199    EG(return_value_ptr_ptr) = NULL;
1200    EG(active_op_array) = CG(active_op_array);
1201    orig_interactive = CG(interactive);
1202    CG(interactive) = 0;
1203    zend_execute(CG(active_op_array) TSRMLS_CC);
1204    CG(interactive) = orig_interactive;
1205
1206    if (EG(exception)) {
1207        zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
1208    }
1209
1210    CG(active_op_array)->last -= 1; /* get rid of that ZEND_RETURN */
1211    EG(start_op) = CG(active_op_array)->opcodes+CG(active_op_array)->last;
1212}
1213/* }}} */
1214
1215ZEND_API void zend_timeout(int dummy) /* {{{ */
1216{
1217    TSRMLS_FETCH();
1218
1219    if (zend_on_timeout) {
1220#ifdef ZEND_SIGNALS
1221        /*
1222           We got here because we got a timeout signal, so we are in a signal handler
1223           at this point. However, we want to be able to timeout any user-supplied
1224           shutdown functions, so pretend we are not in a signal handler while we are
1225           calling these
1226        */
1227        SIGG(running) = 0;
1228#endif
1229        zend_on_timeout(EG(timeout_seconds) TSRMLS_CC);
1230    }
1231
1232    zend_error(E_ERROR, "Maximum execution time of %d second%s exceeded", EG(timeout_seconds), EG(timeout_seconds) == 1 ? "" : "s");
1233}
1234/* }}} */
1235
1236#ifdef ZEND_WIN32
1237static LRESULT CALLBACK zend_timeout_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) /* {{{ */
1238{
1239    switch (message) {
1240        case WM_DESTROY:
1241            PostQuitMessage(0);
1242            break;
1243        case WM_REGISTER_ZEND_TIMEOUT:
1244            /* wParam is the thread id pointer, lParam is the timeout amount in seconds */
1245            if (lParam == 0) {
1246                KillTimer(timeout_window, wParam);
1247            } else {
1248#ifdef ZTS
1249                void ***tsrm_ls;
1250#endif
1251                SetTimer(timeout_window, wParam, lParam*1000, NULL);
1252#ifdef ZTS
1253                tsrm_ls = ts_resource_ex(0, &wParam);
1254                if (!tsrm_ls) {
1255                    /* shouldn't normally happen */
1256                    break;
1257                }
1258#endif
1259                EG(timed_out) = 0;
1260            }
1261            break;
1262        case WM_UNREGISTER_ZEND_TIMEOUT:
1263            /* wParam is the thread id pointer */
1264            KillTimer(timeout_window, wParam);
1265            break;
1266        case WM_TIMER: {
1267#ifdef ZTS
1268                void ***tsrm_ls;
1269
1270                tsrm_ls = ts_resource_ex(0, &wParam);
1271                if (!tsrm_ls) {
1272                    /* Thread died before receiving its timeout? */
1273                    break;
1274                }
1275#endif
1276                KillTimer(timeout_window, wParam);
1277                EG(timed_out) = 1;
1278            }
1279            break;
1280        default:
1281            return DefWindowProc(hWnd, message, wParam, lParam);
1282    }
1283    return 0;
1284}
1285/* }}} */
1286
1287static unsigned __stdcall timeout_thread_proc(void *pArgs) /* {{{ */
1288{
1289    MSG message;
1290
1291    wc.style=0;
1292    wc.lpfnWndProc = zend_timeout_WndProc;
1293    wc.cbClsExtra=0;
1294    wc.cbWndExtra=0;
1295    wc.hInstance=NULL;
1296    wc.hIcon=NULL;
1297    wc.hCursor=NULL;
1298    wc.hbrBackground=(HBRUSH)(COLOR_BACKGROUND + 5);
1299    wc.lpszMenuName=NULL;
1300    wc.lpszClassName = "Zend Timeout Window";
1301    if (!RegisterClass(&wc)) {
1302        return -1;
1303    }
1304    timeout_window = CreateWindow(wc.lpszClassName, wc.lpszClassName, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL);
1305    SetEvent(timeout_thread_event);
1306    while (GetMessage(&message, NULL, 0, 0)) {
1307        SendMessage(timeout_window, message.message, message.wParam, message.lParam);
1308        if (message.message == WM_QUIT) {
1309            break;
1310        }
1311    }
1312    DestroyWindow(timeout_window);
1313    UnregisterClass(wc.lpszClassName, NULL);
1314    SetEvent(timeout_thread_handle);
1315    return 0;
1316}
1317/* }}} */
1318
1319void zend_init_timeout_thread(void) /* {{{ */
1320{
1321    timeout_thread_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1322    timeout_thread_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
1323    _beginthreadex(NULL, 0, timeout_thread_proc, NULL, 0, &timeout_thread_id);
1324    WaitForSingleObject(timeout_thread_event, INFINITE);
1325}
1326/* }}} */
1327
1328void zend_shutdown_timeout_thread(void) /* {{{ */
1329{
1330    if (!timeout_thread_initialized) {
1331        return;
1332    }
1333    PostThreadMessage(timeout_thread_id, WM_QUIT, 0, 0);
1334
1335    /* Wait for thread termination */
1336    WaitForSingleObject(timeout_thread_handle, 5000);
1337    CloseHandle(timeout_thread_handle);
1338    timeout_thread_initialized = 0;
1339}
1340/* }}} */
1341
1342#endif
1343
1344/* This one doesn't exists on QNX */
1345#ifndef SIGPROF
1346#define SIGPROF 27
1347#endif
1348
1349void zend_set_timeout(long seconds, int reset_signals) /* {{{ */
1350{
1351    TSRMLS_FETCH();
1352
1353    EG(timeout_seconds) = seconds;
1354
1355#ifdef ZEND_WIN32
1356    if(!seconds) {
1357        return;
1358    }
1359    if (timeout_thread_initialized == 0 && InterlockedIncrement(&timeout_thread_initialized) == 1) {
1360        /* We start up this process-wide thread here and not in zend_startup(), because if Zend
1361         * is initialized inside a DllMain(), you're not supposed to start threads from it.
1362         */
1363        zend_init_timeout_thread();
1364    }
1365    PostThreadMessage(timeout_thread_id, WM_REGISTER_ZEND_TIMEOUT, (WPARAM) GetCurrentThreadId(), (LPARAM) seconds);
1366#else
1367#   ifdef HAVE_SETITIMER
1368    {
1369        struct itimerval t_r;       /* timeout requested */
1370        int signo;
1371
1372        if(seconds) {
1373            t_r.it_value.tv_sec = seconds;
1374            t_r.it_value.tv_usec = t_r.it_interval.tv_sec = t_r.it_interval.tv_usec = 0;
1375
1376#   ifdef __CYGWIN__
1377            setitimer(ITIMER_REAL, &t_r, NULL);
1378        }
1379        signo = SIGALRM;
1380#   else
1381            setitimer(ITIMER_PROF, &t_r, NULL);
1382        }
1383        signo = SIGPROF;
1384#   endif
1385
1386        if (reset_signals) {
1387#   ifdef ZEND_SIGNALS
1388            zend_signal(signo, zend_timeout TSRMLS_CC);
1389#   else
1390            sigset_t sigset;
1391
1392            signal(signo, zend_timeout);
1393            sigemptyset(&sigset);
1394            sigaddset(&sigset, signo);
1395            sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1396#   endif
1397        }
1398    }
1399#   endif /* HAVE_SETITIMER */
1400#endif
1401}
1402/* }}} */
1403
1404void zend_unset_timeout(TSRMLS_D) /* {{{ */
1405{
1406#ifdef ZEND_WIN32
1407    if(timeout_thread_initialized) {
1408        PostThreadMessage(timeout_thread_id, WM_UNREGISTER_ZEND_TIMEOUT, (WPARAM) GetCurrentThreadId(), (LPARAM) 0);
1409    }
1410#else
1411#   ifdef HAVE_SETITIMER
1412    if (EG(timeout_seconds)) {
1413        struct itimerval no_timeout;
1414
1415        no_timeout.it_value.tv_sec = no_timeout.it_value.tv_usec = no_timeout.it_interval.tv_sec = no_timeout.it_interval.tv_usec = 0;
1416
1417#ifdef __CYGWIN__
1418        setitimer(ITIMER_REAL, &no_timeout, NULL);
1419#else
1420        setitimer(ITIMER_PROF, &no_timeout, NULL);
1421#endif
1422    }
1423#   endif
1424#endif
1425}
1426/* }}} */
1427
1428zend_class_entry *zend_fetch_class(const char *class_name, uint class_name_len, int fetch_type TSRMLS_DC) /* {{{ */
1429{
1430    zend_class_entry **pce;
1431    int use_autoload = (fetch_type & ZEND_FETCH_CLASS_NO_AUTOLOAD) == 0;
1432    int silent       = (fetch_type & ZEND_FETCH_CLASS_SILENT) != 0;
1433
1434    fetch_type &= ZEND_FETCH_CLASS_MASK;
1435
1436check_fetch_type:
1437    switch (fetch_type) {
1438        case ZEND_FETCH_CLASS_SELF:
1439            if (!EG(scope)) {
1440                zend_error(E_ERROR, "Cannot access self:: when no class scope is active");
1441            }
1442            return EG(scope);
1443        case ZEND_FETCH_CLASS_PARENT:
1444            if (!EG(scope)) {
1445                zend_error(E_ERROR, "Cannot access parent:: when no class scope is active");
1446            }
1447            if (!EG(scope)->parent) {
1448                zend_error(E_ERROR, "Cannot access parent:: when current class scope has no parent");
1449            }
1450            return EG(scope)->parent;
1451        case ZEND_FETCH_CLASS_STATIC:
1452            if (!EG(called_scope)) {
1453                zend_error(E_ERROR, "Cannot access static:: when no class scope is active");
1454            }
1455            return EG(called_scope);
1456        case ZEND_FETCH_CLASS_AUTO: {
1457                fetch_type = zend_get_class_fetch_type(class_name, class_name_len);
1458                if (fetch_type!=ZEND_FETCH_CLASS_DEFAULT) {
1459                    goto check_fetch_type;
1460                }
1461            }
1462            break;
1463    }
1464
1465    if (zend_lookup_class_ex(class_name, class_name_len, NULL, use_autoload, &pce TSRMLS_CC) == FAILURE) {
1466        if (use_autoload) {
1467            if (!silent && !EG(exception)) {
1468                if (fetch_type == ZEND_FETCH_CLASS_INTERFACE) {
1469                    zend_error(E_ERROR, "Interface '%s' not found", class_name);
1470                } else if (fetch_type == ZEND_FETCH_CLASS_TRAIT) {
1471                    zend_error(E_ERROR, "Trait '%s' not found", class_name);
1472                } else {
1473                    zend_error(E_ERROR, "Class '%s' not found", class_name);
1474                }
1475            }
1476        }
1477        return NULL;
1478    }
1479    return *pce;
1480}
1481/* }}} */
1482
1483zend_class_entry *zend_fetch_class_by_name(const char *class_name, uint class_name_len, const zend_literal *key, int fetch_type TSRMLS_DC) /* {{{ */
1484{
1485    zend_class_entry **pce;
1486    int use_autoload = (fetch_type & ZEND_FETCH_CLASS_NO_AUTOLOAD) == 0;
1487
1488    if (zend_lookup_class_ex(class_name, class_name_len, key, use_autoload, &pce TSRMLS_CC) == FAILURE) {
1489        if (use_autoload) {
1490            if ((fetch_type & ZEND_FETCH_CLASS_SILENT) == 0 && !EG(exception)) {
1491                if ((fetch_type & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_INTERFACE) {
1492                    zend_error(E_ERROR, "Interface '%s' not found", class_name);
1493                } else if ((fetch_type & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_TRAIT) {
1494                    zend_error(E_ERROR, "Trait '%s' not found", class_name);
1495                } else {
1496                    zend_error(E_ERROR, "Class '%s' not found", class_name);
1497                }
1498            }
1499        }
1500        return NULL;
1501    }
1502    return *pce;
1503}
1504/* }}} */
1505
1506#define MAX_ABSTRACT_INFO_CNT 3
1507#define MAX_ABSTRACT_INFO_FMT "%s%s%s%s"
1508#define DISPLAY_ABSTRACT_FN(idx) \
1509    ai.afn[idx] ? ZEND_FN_SCOPE_NAME(ai.afn[idx]) : "", \
1510    ai.afn[idx] ? "::" : "", \
1511    ai.afn[idx] ? ai.afn[idx]->common.function_name : "", \
1512    ai.afn[idx] && ai.afn[idx + 1] ? ", " : (ai.afn[idx] && ai.cnt > MAX_ABSTRACT_INFO_CNT ? ", ..." : "")
1513
1514typedef struct _zend_abstract_info {
1515    zend_function *afn[MAX_ABSTRACT_INFO_CNT + 1];
1516    int cnt;
1517    int ctor;
1518} zend_abstract_info;
1519
1520static int zend_verify_abstract_class_function(zend_function *fn, zend_abstract_info *ai TSRMLS_DC) /* {{{ */
1521{
1522    if (fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
1523        if (ai->cnt < MAX_ABSTRACT_INFO_CNT) {
1524            ai->afn[ai->cnt] = fn;
1525        }
1526        if (fn->common.fn_flags & ZEND_ACC_CTOR) {
1527            if (!ai->ctor) {
1528                ai->cnt++;
1529                ai->ctor = 1;
1530            } else {
1531                ai->afn[ai->cnt] = NULL;
1532            }
1533        } else {
1534            ai->cnt++;
1535        }
1536    }
1537    return 0;
1538}
1539/* }}} */
1540
1541void zend_verify_abstract_class(zend_class_entry *ce TSRMLS_DC) /* {{{ */
1542{
1543    zend_abstract_info ai;
1544
1545    if ((ce->ce_flags & ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) && !(ce->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
1546        memset(&ai, 0, sizeof(ai));
1547
1548        zend_hash_apply_with_argument(&ce->function_table, (apply_func_arg_t) zend_verify_abstract_class_function, &ai TSRMLS_CC);
1549
1550        if (ai.cnt) {
1551            zend_error(E_ERROR, "Class %s contains %d abstract method%s and must therefore be declared abstract or implement the remaining methods (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")",
1552                ce->name, ai.cnt,
1553                ai.cnt > 1 ? "s" : "",
1554                DISPLAY_ABSTRACT_FN(0),
1555                DISPLAY_ABSTRACT_FN(1),
1556                DISPLAY_ABSTRACT_FN(2)
1557                );
1558        }
1559    }
1560}
1561/* }}} */
1562
1563ZEND_API void zend_reset_all_cv(HashTable *symbol_table TSRMLS_DC) /* {{{ */
1564{
1565    zend_execute_data *ex;
1566    int i;
1567
1568    for (ex = EG(current_execute_data); ex; ex = ex->prev_execute_data) {
1569        if (ex->op_array && ex->symbol_table == symbol_table) {
1570            for (i = 0; i < ex->op_array->last_var; i++) {
1571                *EX_CV_NUM(ex, i) = NULL;
1572            }
1573        }
1574    }
1575}
1576/* }}} */
1577
1578ZEND_API void zend_delete_variable(zend_execute_data *ex, HashTable *ht, const char *name, int name_len, ulong hash_value TSRMLS_DC) /* {{{ */
1579{
1580    if (zend_hash_quick_del(ht, name, name_len, hash_value) == SUCCESS) {
1581        name_len--;
1582        while (ex && ex->symbol_table == ht) {
1583            int i;
1584
1585            if (ex->op_array) {
1586                for (i = 0; i < ex->op_array->last_var; i++) {
1587                    if (ex->op_array->vars[i].hash_value == hash_value &&
1588                        ex->op_array->vars[i].name_len == name_len &&
1589                        !memcmp(ex->op_array->vars[i].name, name, name_len)) {
1590                        *EX_CV_NUM(ex, i) = NULL;
1591                        break;
1592                    }
1593                }
1594            }
1595            ex = ex->prev_execute_data;
1596        }
1597    }
1598}
1599/* }}} */
1600
1601ZEND_API int zend_delete_global_variable_ex(const char *name, int name_len, ulong hash_value TSRMLS_DC) /* {{{ */
1602{
1603    zend_execute_data *ex;
1604
1605    if (zend_hash_quick_exists(&EG(symbol_table), name, name_len + 1, hash_value)) {
1606        for (ex = EG(current_execute_data); ex; ex = ex->prev_execute_data) {
1607            if (ex->op_array && ex->symbol_table == &EG(symbol_table)) {
1608                int i;
1609                for (i = 0; i < ex->op_array->last_var; i++) {
1610                    if (ex->op_array->vars[i].hash_value == hash_value &&
1611                        ex->op_array->vars[i].name_len == name_len &&
1612                        !memcmp(ex->op_array->vars[i].name, name, name_len)
1613                    ) {
1614                        *EX_CV_NUM(ex, i) = NULL;
1615                        break;
1616                    }
1617                }
1618            }
1619        }
1620        return zend_hash_quick_del(&EG(symbol_table), name, name_len + 1, hash_value);
1621    }
1622    return FAILURE;
1623}
1624/* }}} */
1625
1626ZEND_API int zend_delete_global_variable(const char *name, int name_len TSRMLS_DC) /* {{{ */
1627{
1628    return zend_delete_global_variable_ex(name, name_len, zend_inline_hash_func(name, name_len + 1) TSRMLS_CC);
1629}
1630/* }}} */
1631
1632ZEND_API void zend_rebuild_symbol_table(TSRMLS_D) /* {{{ */
1633{
1634    zend_uint i;
1635    zend_execute_data *ex;
1636
1637    if (!EG(active_symbol_table)) {
1638
1639        /* Search for last called user function */
1640        ex = EG(current_execute_data);
1641        while (ex && !ex->op_array) {
1642            ex = ex->prev_execute_data;
1643        }
1644        if (ex && ex->symbol_table) {
1645            EG(active_symbol_table) = ex->symbol_table;
1646            return;
1647        }
1648
1649        if (ex && ex->op_array) {
1650            if (EG(symtable_cache_ptr)>=EG(symtable_cache)) {
1651                /*printf("Cache hit!  Reusing %x\n", symtable_cache[symtable_cache_ptr]);*/
1652                EG(active_symbol_table) = *(EG(symtable_cache_ptr)--);
1653            } else {
1654                ALLOC_HASHTABLE(EG(active_symbol_table));
1655                zend_hash_init(EG(active_symbol_table), ex->op_array->last_var, NULL, ZVAL_PTR_DTOR, 0);
1656                /*printf("Cache miss!  Initialized %x\n", EG(active_symbol_table));*/
1657            }
1658            ex->symbol_table = EG(active_symbol_table);
1659
1660            if (ex->op_array->this_var != -1 &&
1661                !*EX_CV_NUM(ex, ex->op_array->this_var) &&
1662                EG(This)) {
1663                *EX_CV_NUM(ex, ex->op_array->this_var) = (zval**)EX_CV_NUM(ex, ex->op_array->last_var + ex->op_array->this_var);
1664                **EX_CV_NUM(ex, ex->op_array->this_var) = EG(This);
1665            }
1666            for (i = 0; i < ex->op_array->last_var; i++) {
1667                if (*EX_CV_NUM(ex, i)) {
1668                    zend_hash_quick_update(EG(active_symbol_table),
1669                        ex->op_array->vars[i].name,
1670                        ex->op_array->vars[i].name_len + 1,
1671                        ex->op_array->vars[i].hash_value,
1672                        (void**)*EX_CV_NUM(ex, i),
1673                        sizeof(zval*),
1674                        (void**)EX_CV_NUM(ex, i));
1675                }
1676            }
1677        }
1678    }
1679}
1680/* }}} */
1681
1682/*
1683 * Local variables:
1684 * tab-width: 4
1685 * c-basic-offset: 4
1686 * indent-tabs-mode: t
1687 * End:
1688 */
1689