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#include "../TSRM/tsrm_strtok_r.h"
453
454#define IS_VISITED_CONSTANT         0x80
455#define IS_CONSTANT_VISITED(p)      (Z_TYPE_P(p) & IS_VISITED_CONSTANT)
456#define Z_REAL_TYPE_P(p)            (Z_TYPE_P(p) & ~IS_VISITED_CONSTANT)
457#define MARK_CONSTANT_VISITED(p)    Z_TYPE_P(p) |= IS_VISITED_CONSTANT
458
459ZEND_API int zval_update_constant_ex(zval **pp, zend_bool inline_change, zend_class_entry *scope TSRMLS_DC) /* {{{ */
460{
461    zval *p = *pp;
462    zval const_value;
463    char *colon;
464
465    if (IS_CONSTANT_VISITED(p)) {
466        zend_error(E_ERROR, "Cannot declare self-referencing constant '%s'", Z_STRVAL_P(p));
467    } else if ((Z_TYPE_P(p) & IS_CONSTANT_TYPE_MASK) == IS_CONSTANT) {
468        int refcount;
469        zend_uchar is_ref;
470
471        SEPARATE_ZVAL_IF_NOT_REF(pp);
472        p = *pp;
473
474        MARK_CONSTANT_VISITED(p);
475
476        refcount = Z_REFCOUNT_P(p);
477        is_ref = Z_ISREF_P(p);
478
479        if (!zend_get_constant_ex(p->value.str.val, p->value.str.len, &const_value, scope, Z_REAL_TYPE_P(p) TSRMLS_CC)) {
480            char *actual = Z_STRVAL_P(p);
481
482            if ((colon = (char*)zend_memrchr(Z_STRVAL_P(p), ':', Z_STRLEN_P(p)))) {
483                zend_error(E_ERROR, "Undefined class constant '%s'", Z_STRVAL_P(p));
484                Z_STRLEN_P(p) -= ((colon - Z_STRVAL_P(p)) + 1);
485                if (inline_change) {
486                    colon = estrndup(colon, Z_STRLEN_P(p));
487                    str_efree(Z_STRVAL_P(p));
488                    Z_STRVAL_P(p) = colon;
489                } else {
490                    Z_STRVAL_P(p) = colon + 1;
491                }
492            } else {
493                char *save = actual, *slash;
494                int actual_len = Z_STRLEN_P(p);
495                if ((Z_TYPE_P(p) & IS_CONSTANT_UNQUALIFIED) && (slash = (char *)zend_memrchr(actual, '\\', actual_len))) {
496                    actual = slash + 1;
497                    actual_len -= (actual - Z_STRVAL_P(p));
498                    if (inline_change) {
499                        actual = estrndup(actual, actual_len);
500                        Z_STRVAL_P(p) = actual;
501                        Z_STRLEN_P(p) = actual_len;
502                    }
503                }
504                if (actual[0] == '\\') {
505                    if (inline_change) {
506                        memmove(Z_STRVAL_P(p), Z_STRVAL_P(p)+1, Z_STRLEN_P(p));
507                        --Z_STRLEN_P(p);
508                    } else {
509                        ++actual;
510                    }
511                    --actual_len;
512                }
513                if ((Z_TYPE_P(p) & IS_CONSTANT_UNQUALIFIED) == 0) {
514                    int fix_save = 0;
515                    if (save[0] == '\\') {
516                        save++;
517                        fix_save = 1;
518                    }
519                    zend_error(E_ERROR, "Undefined constant '%s'", save);
520                    if (fix_save) {
521                        save--;
522                    }
523                    if (inline_change) {
524                        str_efree(save);
525                    }
526                    save = NULL;
527                }
528                if (inline_change && save && save != actual) {
529                    str_efree(save);
530                }
531                zend_error(E_NOTICE, "Use of undefined constant %s - assumed '%s'",  actual,  actual);
532                p->type = IS_STRING;
533                if (!inline_change) {
534                    Z_STRVAL_P(p) = actual;
535                    Z_STRLEN_P(p) = actual_len;
536                    zval_copy_ctor(p);
537                }
538            }
539        } else {
540            if (inline_change) {
541                str_efree(Z_STRVAL_P(p));
542            }
543            *p = const_value;
544        }
545
546        Z_SET_REFCOUNT_P(p, refcount);
547        Z_SET_ISREF_TO_P(p, is_ref);
548    } else if (Z_TYPE_P(p) == IS_CONSTANT_AST) {
549        SEPARATE_ZVAL_IF_NOT_REF(pp);
550        p = *pp;
551
552        zend_ast_evaluate(&const_value, Z_AST_P(p), scope TSRMLS_CC);
553        if (inline_change) {
554            zend_ast_destroy(Z_AST_P(p));
555        }
556        ZVAL_COPY_VALUE(p, &const_value);
557    }
558    return 0;
559}
560/* }}} */
561
562ZEND_API int zval_update_constant_inline_change(zval **pp, zend_class_entry *scope TSRMLS_DC) /* {{{ */
563{
564    return zval_update_constant_ex(pp, 1, scope TSRMLS_CC);
565}
566/* }}} */
567
568ZEND_API int zval_update_constant_no_inline_change(zval **pp, zend_class_entry *scope TSRMLS_DC) /* {{{ */
569{
570    return zval_update_constant_ex(pp, 0, scope TSRMLS_CC);
571}
572/* }}} */
573
574ZEND_API int zval_update_constant(zval **pp, zend_bool inline_change TSRMLS_DC) /* {{{ */
575{
576    return zval_update_constant_ex(pp, inline_change, NULL TSRMLS_CC);
577}
578/* }}} */
579
580int call_user_function(HashTable *function_table, zval **object_pp, zval *function_name, zval *retval_ptr, zend_uint param_count, zval *params[] TSRMLS_DC) /* {{{ */
581{
582    zval ***params_array;
583    zend_uint i;
584    int ex_retval;
585    zval *local_retval_ptr = NULL;
586
587    if (param_count) {
588        params_array = (zval ***) emalloc(sizeof(zval **)*param_count);
589        for (i=0; i<param_count; i++) {
590            params_array[i] = &params[i];
591        }
592    } else {
593        params_array = NULL;
594    }
595    ex_retval = call_user_function_ex(function_table, object_pp, function_name, &local_retval_ptr, param_count, params_array, 1, NULL TSRMLS_CC);
596    if (local_retval_ptr) {
597        COPY_PZVAL_TO_ZVAL(*retval_ptr, local_retval_ptr);
598    } else {
599        INIT_ZVAL(*retval_ptr);
600    }
601    if (params_array) {
602        efree(params_array);
603    }
604    return ex_retval;
605}
606/* }}} */
607
608int 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) /* {{{ */
609{
610    zend_fcall_info fci;
611
612    fci.size = sizeof(fci);
613    fci.function_table = function_table;
614    fci.object_ptr = object_pp ? *object_pp : NULL;
615    fci.function_name = function_name;
616    fci.retval_ptr_ptr = retval_ptr_ptr;
617    fci.param_count = param_count;
618    fci.params = params;
619    fci.no_separation = (zend_bool) no_separation;
620    fci.symbol_table = symbol_table;
621
622    return zend_call_function(&fci, NULL TSRMLS_CC);
623}
624/* }}} */
625
626int zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache TSRMLS_DC) /* {{{ */
627{
628    zend_uint i;
629    zval **original_return_value;
630    HashTable *calling_symbol_table;
631    zend_op_array *original_op_array;
632    zend_op **original_opline_ptr;
633    zend_class_entry *current_scope;
634    zend_class_entry *current_called_scope;
635    zend_class_entry *calling_scope = NULL;
636    zend_class_entry *called_scope = NULL;
637    zval *current_this;
638    zend_execute_data execute_data;
639    zend_fcall_info_cache fci_cache_local;
640
641    *fci->retval_ptr_ptr = NULL;
642
643    if (!EG(active)) {
644        return FAILURE; /* executor is already inactive */
645    }
646
647    if (EG(exception)) {
648        return FAILURE; /* we would result in an instable executor otherwise */
649    }
650
651    switch (fci->size) {
652        case sizeof(zend_fcall_info):
653            break; /* nothing to do currently */
654        default:
655            zend_error(E_ERROR, "Corrupted fcall_info provided to zend_call_function()");
656            break;
657    }
658
659    /* Initialize execute_data */
660    if (EG(current_execute_data)) {
661        execute_data = *EG(current_execute_data);
662        EX(op_array) = NULL;
663        EX(opline) = NULL;
664        EX(object) = NULL;
665    } else {
666        /* This only happens when we're called outside any execute()'s
667         * It shouldn't be strictly necessary to NULL execute_data out,
668         * but it may make bugs easier to spot
669         */
670        memset(&execute_data, 0, sizeof(zend_execute_data));
671    }
672
673    if (!fci_cache || !fci_cache->initialized) {
674        char *callable_name;
675        char *error = NULL;
676
677        if (!fci_cache) {
678            fci_cache = &fci_cache_local;
679        }
680
681        if (!zend_is_callable_ex(fci->function_name, fci->object_ptr, IS_CALLABLE_CHECK_SILENT, &callable_name, NULL, fci_cache, &error TSRMLS_CC)) {
682            if (error) {
683                zend_error(E_WARNING, "Invalid callback %s, %s", callable_name, error);
684                efree(error);
685            }
686            if (callable_name) {
687                efree(callable_name);
688            }
689            return FAILURE;
690        } else if (error) {
691            /* Capitalize the first latter of the error message */
692            if (error[0] >= 'a' && error[0] <= 'z') {
693                error[0] += ('A' - 'a');
694            }
695            zend_error(E_STRICT, "%s", error);
696            efree(error);
697        }
698        efree(callable_name);
699    }
700
701    EX(function_state).function = fci_cache->function_handler;
702    calling_scope = fci_cache->calling_scope;
703    called_scope = fci_cache->called_scope;
704    fci->object_ptr = fci_cache->object_ptr;
705    EX(object) = fci->object_ptr;
706    if (fci->object_ptr && Z_TYPE_P(fci->object_ptr) == IS_OBJECT &&
707        (!EG(objects_store).object_buckets || !EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(fci->object_ptr)].valid)) {
708        return FAILURE;
709    }
710
711    if (EX(function_state).function->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED)) {
712        if (EX(function_state).function->common.fn_flags & ZEND_ACC_ABSTRACT) {
713            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);
714        }
715        if (EX(function_state).function->common.fn_flags & ZEND_ACC_DEPRECATED) {
716            zend_error(E_DEPRECATED, "Function %s%s%s() is deprecated",
717                EX(function_state).function->common.scope ? EX(function_state).function->common.scope->name : "",
718                EX(function_state).function->common.scope ? "::" : "",
719                EX(function_state).function->common.function_name);
720        }
721    }
722
723    ZEND_VM_STACK_GROW_IF_NEEDED(fci->param_count + 1);
724
725    for (i=0; i<fci->param_count; i++) {
726        zval *param;
727
728        if (ARG_SHOULD_BE_SENT_BY_REF(EX(function_state).function, i + 1)) {
729            if (!PZVAL_IS_REF(*fci->params[i]) && Z_REFCOUNT_PP(fci->params[i]) > 1) {
730                zval *new_zval;
731
732                if (fci->no_separation &&
733                    !ARG_MAY_BE_SENT_BY_REF(EX(function_state).function, i + 1)) {
734                    if (i || UNEXPECTED(ZEND_VM_STACK_ELEMETS(EG(argument_stack)) == (EG(argument_stack)->top))) {
735                        /* hack to clean up the stack */
736                        zend_vm_stack_push((void *) (zend_uintptr_t)i TSRMLS_CC);
737                        zend_vm_stack_clear_multiple(0 TSRMLS_CC);
738                    }
739
740                    zend_error(E_WARNING, "Parameter %d to %s%s%s() expected to be a reference, value given",
741                        i+1,
742                        EX(function_state).function->common.scope ? EX(function_state).function->common.scope->name : "",
743                        EX(function_state).function->common.scope ? "::" : "",
744                        EX(function_state).function->common.function_name);
745                    return FAILURE;
746                }
747
748                ALLOC_ZVAL(new_zval);
749                *new_zval = **fci->params[i];
750                zval_copy_ctor(new_zval);
751                Z_SET_REFCOUNT_P(new_zval, 1);
752                Z_DELREF_PP(fci->params[i]);
753                *fci->params[i] = new_zval;
754            }
755            Z_ADDREF_PP(fci->params[i]);
756            Z_SET_ISREF_PP(fci->params[i]);
757            param = *fci->params[i];
758        } else if (PZVAL_IS_REF(*fci->params[i]) &&
759                   /* don't separate references for __call */
760                   (EX(function_state).function->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) == 0 ) {
761            ALLOC_ZVAL(param);
762            *param = **(fci->params[i]);
763            INIT_PZVAL(param);
764            zval_copy_ctor(param);
765        } else if (*fci->params[i] != &EG(uninitialized_zval)) {
766            Z_ADDREF_PP(fci->params[i]);
767            param = *fci->params[i];
768        } else {
769            ALLOC_ZVAL(param);
770            *param = **(fci->params[i]);
771            INIT_PZVAL(param);
772        }
773        zend_vm_stack_push(param TSRMLS_CC);
774    }
775
776    EX(function_state).arguments = zend_vm_stack_top(TSRMLS_C);
777    zend_vm_stack_push((void*)(zend_uintptr_t)fci->param_count TSRMLS_CC);
778
779    current_scope = EG(scope);
780    EG(scope) = calling_scope;
781
782    current_this = EG(This);
783
784    current_called_scope = EG(called_scope);
785    if (called_scope) {
786        EG(called_scope) = called_scope;
787    } else if (EX(function_state).function->type != ZEND_INTERNAL_FUNCTION) {
788        EG(called_scope) = NULL;
789    }
790
791    if (fci->object_ptr) {
792        if ((EX(function_state).function->common.fn_flags & ZEND_ACC_STATIC)) {
793            EG(This) = NULL;
794        } else {
795            EG(This) = fci->object_ptr;
796
797            if (!PZVAL_IS_REF(EG(This))) {
798                Z_ADDREF_P(EG(This)); /* For $this pointer */
799            } else {
800                zval *this_ptr;
801
802                ALLOC_ZVAL(this_ptr);
803                *this_ptr = *EG(This);
804                INIT_PZVAL(this_ptr);
805                zval_copy_ctor(this_ptr);
806                EG(This) = this_ptr;
807            }
808        }
809    } else {
810        EG(This) = NULL;
811    }
812
813    EX(prev_execute_data) = EG(current_execute_data);
814    EG(current_execute_data) = &execute_data;
815
816    if (EX(function_state).function->type == ZEND_USER_FUNCTION) {
817        calling_symbol_table = EG(active_symbol_table);
818        EG(scope) = EX(function_state).function->common.scope;
819        if (fci->symbol_table) {
820            EG(active_symbol_table) = fci->symbol_table;
821        } else {
822            EG(active_symbol_table) = NULL;
823        }
824
825        original_return_value = EG(return_value_ptr_ptr);
826        original_op_array = EG(active_op_array);
827        EG(return_value_ptr_ptr) = fci->retval_ptr_ptr;
828        EG(active_op_array) = (zend_op_array *) EX(function_state).function;
829        original_opline_ptr = EG(opline_ptr);
830
831        if (EG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
832            *fci->retval_ptr_ptr = zend_generator_create_zval(EG(active_op_array) TSRMLS_CC);
833        } else {
834            zend_execute(EG(active_op_array) TSRMLS_CC);
835        }
836
837        if (!fci->symbol_table && EG(active_symbol_table)) {
838            zend_clean_and_cache_symbol_table(EG(active_symbol_table) TSRMLS_CC);
839        }
840        EG(active_symbol_table) = calling_symbol_table;
841        EG(active_op_array) = original_op_array;
842        EG(return_value_ptr_ptr)=original_return_value;
843        EG(opline_ptr) = original_opline_ptr;
844    } else if (EX(function_state).function->type == ZEND_INTERNAL_FUNCTION) {
845        int call_via_handler = (EX(function_state).function->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) != 0;
846        ALLOC_INIT_ZVAL(*fci->retval_ptr_ptr);
847        if (EX(function_state).function->common.scope) {
848            EG(scope) = EX(function_state).function->common.scope;
849        }
850        if (EXPECTED(zend_execute_internal == NULL)) {
851            /* saves one function call if zend_execute_internal is not used */
852            EX(function_state).function->internal_function.handler(fci->param_count, *fci->retval_ptr_ptr, fci->retval_ptr_ptr, fci->object_ptr, 1 TSRMLS_CC);
853        } else {
854            zend_execute_internal(&execute_data, fci, 1 TSRMLS_CC);
855        }
856        /*  We shouldn't fix bad extensions here,
857            because it can break proper ones (Bug #34045)
858        if (!EX(function_state).function->common.return_reference)
859        {
860            INIT_PZVAL(*fci->retval_ptr_ptr);
861        }*/
862        if (EG(exception) && fci->retval_ptr_ptr) {
863            zval_ptr_dtor(fci->retval_ptr_ptr);
864            *fci->retval_ptr_ptr = NULL;
865        }
866
867        if (call_via_handler) {
868            /* We must re-initialize function again */
869            fci_cache->initialized = 0;
870        }
871    } else { /* ZEND_OVERLOADED_FUNCTION */
872        ALLOC_INIT_ZVAL(*fci->retval_ptr_ptr);
873
874        /* Not sure what should be done here if it's a static method */
875        if (fci->object_ptr) {
876            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);
877        } else {
878            zend_error_noreturn(E_ERROR, "Cannot call overloaded function for non-object");
879        }
880
881        if (EX(function_state).function->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY) {
882            efree((char*)EX(function_state).function->common.function_name);
883        }
884        efree(EX(function_state).function);
885
886        if (EG(exception) && fci->retval_ptr_ptr) {
887            zval_ptr_dtor(fci->retval_ptr_ptr);
888            *fci->retval_ptr_ptr = NULL;
889        }
890    }
891    zend_vm_stack_clear_multiple(0 TSRMLS_CC);
892
893    if (EG(This)) {
894        zval_ptr_dtor(&EG(This));
895    }
896    EG(called_scope) = current_called_scope;
897    EG(scope) = current_scope;
898    EG(This) = current_this;
899    EG(current_execute_data) = EX(prev_execute_data);
900
901    if (EG(exception)) {
902        zend_throw_exception_internal(NULL TSRMLS_CC);
903    }
904    return SUCCESS;
905}
906/* }}} */
907
908ZEND_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) /* {{{ */
909{
910    zval **args[1];
911    zval autoload_function;
912    zval *class_name_ptr;
913    zval *retval_ptr = NULL;
914    int retval, lc_length;
915    char *lc_name;
916    char *lc_free;
917    zend_fcall_info fcall_info;
918    zend_fcall_info_cache fcall_cache;
919    char dummy = 1;
920    ulong hash;
921    ALLOCA_FLAG(use_heap)
922
923    if (key) {
924        lc_name = Z_STRVAL(key->constant);
925        lc_length = Z_STRLEN(key->constant) + 1;
926        hash = key->hash_value;
927    } else {
928        if (name == NULL || !name_length) {
929            return FAILURE;
930        }
931
932        lc_free = lc_name = do_alloca(name_length + 1, use_heap);
933        zend_str_tolower_copy(lc_name, name, name_length);
934        lc_length = name_length + 1;
935
936        if (lc_name[0] == '\\') {
937            lc_name += 1;
938            lc_length -= 1;
939        }
940
941        hash = zend_inline_hash_func(lc_name, lc_length);
942    }
943
944    if (zend_hash_quick_find(EG(class_table), lc_name, lc_length, hash, (void **) ce) == SUCCESS) {
945        if (!key) {
946            free_alloca(lc_free, use_heap);
947        }
948        return SUCCESS;
949    }
950
951    /* The compiler is not-reentrant. Make sure we __autoload() only during run-time
952     * (doesn't impact functionality of __autoload()
953    */
954    if (!use_autoload || zend_is_compiling(TSRMLS_C)) {
955        if (!key) {
956            free_alloca(lc_free, use_heap);
957        }
958        return FAILURE;
959    }
960
961    /* Verify class name before passing it to __autoload() */
962    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) {
963        if (!key) {
964            free_alloca(lc_free, use_heap);
965        }
966        return FAILURE;
967    }
968
969    if (EG(in_autoload) == NULL) {
970        ALLOC_HASHTABLE(EG(in_autoload));
971        zend_hash_init(EG(in_autoload), 0, NULL, NULL, 0);
972    }
973
974    if (zend_hash_quick_add(EG(in_autoload), lc_name, lc_length, hash, (void**)&dummy, sizeof(char), NULL) == FAILURE) {
975        if (!key) {
976            free_alloca(lc_free, use_heap);
977        }
978        return FAILURE;
979    }
980
981    ZVAL_STRINGL(&autoload_function, ZEND_AUTOLOAD_FUNC_NAME, sizeof(ZEND_AUTOLOAD_FUNC_NAME) - 1, 0);
982
983    ALLOC_ZVAL(class_name_ptr);
984    INIT_PZVAL(class_name_ptr);
985    if (name[0] == '\\') {
986        ZVAL_STRINGL(class_name_ptr, name+1, name_length-1, 1);
987    } else {
988        ZVAL_STRINGL(class_name_ptr, name, name_length, 1);
989    }
990
991    args[0] = &class_name_ptr;
992
993    fcall_info.size = sizeof(fcall_info);
994    fcall_info.function_table = EG(function_table);
995    fcall_info.function_name = &autoload_function;
996    fcall_info.symbol_table = NULL;
997    fcall_info.retval_ptr_ptr = &retval_ptr;
998    fcall_info.param_count = 1;
999    fcall_info.params = args;
1000    fcall_info.object_ptr = NULL;
1001    fcall_info.no_separation = 1;
1002
1003    fcall_cache.initialized = EG(autoload_func) ? 1 : 0;
1004    fcall_cache.function_handler = EG(autoload_func);
1005    fcall_cache.calling_scope = NULL;
1006    fcall_cache.called_scope = NULL;
1007    fcall_cache.object_ptr = NULL;
1008
1009    zend_exception_save(TSRMLS_C);
1010    retval = zend_call_function(&fcall_info, &fcall_cache TSRMLS_CC);
1011    zend_exception_restore(TSRMLS_C);
1012
1013    EG(autoload_func) = fcall_cache.function_handler;
1014
1015    zval_ptr_dtor(&class_name_ptr);
1016
1017    zend_hash_quick_del(EG(in_autoload), lc_name, lc_length, hash);
1018
1019    if (retval_ptr) {
1020        zval_ptr_dtor(&retval_ptr);
1021    }
1022
1023    if (retval == SUCCESS) {
1024        retval = zend_hash_quick_find(EG(class_table), lc_name, lc_length, hash, (void **) ce);
1025    }
1026    if (!key) {
1027        free_alloca(lc_free, use_heap);
1028    }
1029    return retval;
1030}
1031/* }}} */
1032
1033ZEND_API int zend_lookup_class(const char *name, int name_length, zend_class_entry ***ce TSRMLS_DC) /* {{{ */
1034{
1035    return zend_lookup_class_ex(name, name_length, NULL, 1, ce TSRMLS_CC);
1036}
1037/* }}} */
1038
1039ZEND_API int zend_eval_stringl(char *str, int str_len, zval *retval_ptr, char *string_name TSRMLS_DC) /* {{{ */
1040{
1041    zval pv;
1042    zend_op_array *new_op_array;
1043    zend_op_array *original_active_op_array = EG(active_op_array);
1044    zend_uint original_compiler_options;
1045    int retval;
1046
1047    if (retval_ptr) {
1048        Z_STRLEN(pv) = str_len + sizeof("return ;") - 1;
1049        Z_STRVAL(pv) = emalloc(Z_STRLEN(pv) + 1);
1050        memcpy(Z_STRVAL(pv), "return ", sizeof("return ") - 1);
1051        memcpy(Z_STRVAL(pv) + sizeof("return ") - 1, str, str_len);
1052        Z_STRVAL(pv)[Z_STRLEN(pv) - 1] = ';';
1053        Z_STRVAL(pv)[Z_STRLEN(pv)] = '\0';
1054    } else {
1055        Z_STRLEN(pv) = str_len;
1056        Z_STRVAL(pv) = str;
1057    }
1058    Z_TYPE(pv) = IS_STRING;
1059
1060    /*printf("Evaluating '%s'\n", pv.value.str.val);*/
1061
1062    original_compiler_options = CG(compiler_options);
1063    CG(compiler_options) = ZEND_COMPILE_DEFAULT_FOR_EVAL;
1064    new_op_array = zend_compile_string(&pv, string_name TSRMLS_CC);
1065    CG(compiler_options) = original_compiler_options;
1066
1067    if (new_op_array) {
1068        zval *local_retval_ptr=NULL;
1069        zval **original_return_value_ptr_ptr = EG(return_value_ptr_ptr);
1070        zend_op **original_opline_ptr = EG(opline_ptr);
1071        int orig_interactive = CG(interactive);
1072
1073        EG(return_value_ptr_ptr) = &local_retval_ptr;
1074        EG(active_op_array) = new_op_array;
1075        EG(no_extensions)=1;
1076        if (!EG(active_symbol_table)) {
1077            zend_rebuild_symbol_table(TSRMLS_C);
1078        }
1079        CG(interactive) = 0;
1080
1081        zend_try {
1082            zend_execute(new_op_array TSRMLS_CC);
1083        } zend_catch {
1084            destroy_op_array(new_op_array TSRMLS_CC);
1085            efree(new_op_array);
1086            zend_bailout();
1087        } zend_end_try();
1088
1089        CG(interactive) = orig_interactive;
1090        if (local_retval_ptr) {
1091            if (retval_ptr) {
1092                COPY_PZVAL_TO_ZVAL(*retval_ptr, local_retval_ptr);
1093            } else {
1094                zval_ptr_dtor(&local_retval_ptr);
1095            }
1096        } else {
1097            if (retval_ptr) {
1098                INIT_ZVAL(*retval_ptr);
1099            }
1100        }
1101
1102        EG(no_extensions)=0;
1103        EG(opline_ptr) = original_opline_ptr;
1104        EG(active_op_array) = original_active_op_array;
1105        destroy_op_array(new_op_array TSRMLS_CC);
1106        efree(new_op_array);
1107        EG(return_value_ptr_ptr) = original_return_value_ptr_ptr;
1108        retval = SUCCESS;
1109    } else {
1110        retval = FAILURE;
1111    }
1112    if (retval_ptr) {
1113        zval_dtor(&pv);
1114    }
1115    return retval;
1116}
1117/* }}} */
1118
1119ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSRMLS_DC) /* {{{ */
1120{
1121    return zend_eval_stringl(str, strlen(str), retval_ptr, string_name TSRMLS_CC);
1122}
1123/* }}} */
1124
1125ZEND_API int zend_eval_stringl_ex(char *str, int str_len, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC) /* {{{ */
1126{
1127    int result;
1128
1129    result = zend_eval_stringl(str, str_len, retval_ptr, string_name TSRMLS_CC);
1130    if (handle_exceptions && EG(exception)) {
1131        zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
1132        result = FAILURE;
1133    }
1134    return result;
1135}
1136/* }}} */
1137
1138ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC) /* {{{ */
1139{
1140    return zend_eval_stringl_ex(str, strlen(str), retval_ptr, string_name, handle_exceptions TSRMLS_CC);
1141}
1142/* }}} */
1143
1144void execute_new_code(TSRMLS_D) /* {{{ */
1145{
1146    zend_op *opline, *end;
1147    zend_op *ret_opline;
1148    int orig_interactive;
1149
1150    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_INTERACTIVE)
1151        || CG(context).backpatch_count>0
1152        || CG(active_op_array)->function_name
1153        || CG(active_op_array)->type!=ZEND_USER_FUNCTION) {
1154        return;
1155    }
1156
1157    ret_opline = get_next_op(CG(active_op_array) TSRMLS_CC);
1158    ret_opline->opcode = ZEND_RETURN;
1159    ret_opline->op1_type = IS_CONST;
1160    ret_opline->op1.constant = zend_add_literal(CG(active_op_array), &EG(uninitialized_zval) TSRMLS_CC);
1161    SET_UNUSED(ret_opline->op2);
1162
1163    if (!EG(start_op)) {
1164        EG(start_op) = CG(active_op_array)->opcodes;
1165    }
1166
1167    opline=EG(start_op);
1168    end=CG(active_op_array)->opcodes+CG(active_op_array)->last;
1169
1170    while (opline<end) {
1171        if (opline->op1_type == IS_CONST) {
1172            opline->op1.zv = &CG(active_op_array)->literals[opline->op1.constant].constant;
1173        }
1174        if (opline->op2_type == IS_CONST) {
1175            opline->op2.zv = &CG(active_op_array)->literals[opline->op2.constant].constant;
1176        }
1177        switch (opline->opcode) {
1178            case ZEND_GOTO:
1179                if (Z_TYPE_P(opline->op2.zv) != IS_LONG) {
1180                    zend_resolve_goto_label(CG(active_op_array), opline, 1 TSRMLS_CC);
1181                }
1182                /* break omitted intentionally */
1183            case ZEND_JMP:
1184                opline->op1.jmp_addr = &CG(active_op_array)->opcodes[opline->op1.opline_num];
1185                break;
1186            case ZEND_JMPZ:
1187            case ZEND_JMPNZ:
1188            case ZEND_JMPZ_EX:
1189            case ZEND_JMPNZ_EX:
1190            case ZEND_JMP_SET:
1191            case ZEND_JMP_SET_VAR:
1192                opline->op2.jmp_addr = &CG(active_op_array)->opcodes[opline->op2.opline_num];
1193                break;
1194        }
1195        ZEND_VM_SET_OPCODE_HANDLER(opline);
1196        opline++;
1197    }
1198
1199    zend_release_labels(1 TSRMLS_CC);
1200
1201    EG(return_value_ptr_ptr) = NULL;
1202    EG(active_op_array) = CG(active_op_array);
1203    orig_interactive = CG(interactive);
1204    CG(interactive) = 0;
1205    zend_execute(CG(active_op_array) TSRMLS_CC);
1206    CG(interactive) = orig_interactive;
1207
1208    if (EG(exception)) {
1209        zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
1210    }
1211
1212    CG(active_op_array)->last -= 1; /* get rid of that ZEND_RETURN */
1213    EG(start_op) = CG(active_op_array)->opcodes+CG(active_op_array)->last;
1214}
1215/* }}} */
1216
1217ZEND_API void zend_timeout(int dummy) /* {{{ */
1218{
1219    TSRMLS_FETCH();
1220
1221    if (zend_on_timeout) {
1222#ifdef ZEND_SIGNALS
1223        /*
1224           We got here because we got a timeout signal, so we are in a signal handler
1225           at this point. However, we want to be able to timeout any user-supplied
1226           shutdown functions, so pretend we are not in a signal handler while we are
1227           calling these
1228        */
1229        SIGG(running) = 0;
1230#endif
1231        zend_on_timeout(EG(timeout_seconds) TSRMLS_CC);
1232    }
1233
1234    zend_error(E_ERROR, "Maximum execution time of %d second%s exceeded", EG(timeout_seconds), EG(timeout_seconds) == 1 ? "" : "s");
1235}
1236/* }}} */
1237
1238#ifdef ZEND_WIN32
1239static LRESULT CALLBACK zend_timeout_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) /* {{{ */
1240{
1241    switch (message) {
1242        case WM_DESTROY:
1243            PostQuitMessage(0);
1244            break;
1245        case WM_REGISTER_ZEND_TIMEOUT:
1246            /* wParam is the thread id pointer, lParam is the timeout amount in seconds */
1247            if (lParam == 0) {
1248                KillTimer(timeout_window, wParam);
1249            } else {
1250#ifdef ZTS
1251                void ***tsrm_ls;
1252#endif
1253                SetTimer(timeout_window, wParam, lParam*1000, NULL);
1254#ifdef ZTS
1255                tsrm_ls = ts_resource_ex(0, &wParam);
1256                if (!tsrm_ls) {
1257                    /* shouldn't normally happen */
1258                    break;
1259                }
1260#endif
1261                EG(timed_out) = 0;
1262            }
1263            break;
1264        case WM_UNREGISTER_ZEND_TIMEOUT:
1265            /* wParam is the thread id pointer */
1266            KillTimer(timeout_window, wParam);
1267            break;
1268        case WM_TIMER: {
1269#ifdef ZTS
1270                void ***tsrm_ls;
1271
1272                tsrm_ls = ts_resource_ex(0, &wParam);
1273                if (!tsrm_ls) {
1274                    /* Thread died before receiving its timeout? */
1275                    break;
1276                }
1277#endif
1278                KillTimer(timeout_window, wParam);
1279                EG(timed_out) = 1;
1280            }
1281            break;
1282        default:
1283            return DefWindowProc(hWnd, message, wParam, lParam);
1284    }
1285    return 0;
1286}
1287/* }}} */
1288
1289static unsigned __stdcall timeout_thread_proc(void *pArgs) /* {{{ */
1290{
1291    MSG message;
1292
1293    wc.style=0;
1294    wc.lpfnWndProc = zend_timeout_WndProc;
1295    wc.cbClsExtra=0;
1296    wc.cbWndExtra=0;
1297    wc.hInstance=NULL;
1298    wc.hIcon=NULL;
1299    wc.hCursor=NULL;
1300    wc.hbrBackground=(HBRUSH)(COLOR_BACKGROUND + 5);
1301    wc.lpszMenuName=NULL;
1302    wc.lpszClassName = "Zend Timeout Window";
1303    if (!RegisterClass(&wc)) {
1304        return -1;
1305    }
1306    timeout_window = CreateWindow(wc.lpszClassName, wc.lpszClassName, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL);
1307    SetEvent(timeout_thread_event);
1308    while (GetMessage(&message, NULL, 0, 0)) {
1309        SendMessage(timeout_window, message.message, message.wParam, message.lParam);
1310        if (message.message == WM_QUIT) {
1311            break;
1312        }
1313    }
1314    DestroyWindow(timeout_window);
1315    UnregisterClass(wc.lpszClassName, NULL);
1316    SetEvent(timeout_thread_handle);
1317    return 0;
1318}
1319/* }}} */
1320
1321void zend_init_timeout_thread(void) /* {{{ */
1322{
1323    timeout_thread_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1324    timeout_thread_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
1325    _beginthreadex(NULL, 0, timeout_thread_proc, NULL, 0, &timeout_thread_id);
1326    WaitForSingleObject(timeout_thread_event, INFINITE);
1327}
1328/* }}} */
1329
1330void zend_shutdown_timeout_thread(void) /* {{{ */
1331{
1332    if (!timeout_thread_initialized) {
1333        return;
1334    }
1335    PostThreadMessage(timeout_thread_id, WM_QUIT, 0, 0);
1336
1337    /* Wait for thread termination */
1338    WaitForSingleObject(timeout_thread_handle, 5000);
1339    CloseHandle(timeout_thread_handle);
1340    timeout_thread_initialized = 0;
1341}
1342/* }}} */
1343
1344#endif
1345
1346/* This one doesn't exists on QNX */
1347#ifndef SIGPROF
1348#define SIGPROF 27
1349#endif
1350
1351void zend_set_timeout(long seconds, int reset_signals) /* {{{ */
1352{
1353    TSRMLS_FETCH();
1354
1355    EG(timeout_seconds) = seconds;
1356
1357#ifdef ZEND_WIN32
1358    if(!seconds) {
1359        return;
1360    }
1361    if (timeout_thread_initialized == 0 && InterlockedIncrement(&timeout_thread_initialized) == 1) {
1362        /* We start up this process-wide thread here and not in zend_startup(), because if Zend
1363         * is initialized inside a DllMain(), you're not supposed to start threads from it.
1364         */
1365        zend_init_timeout_thread();
1366    }
1367    PostThreadMessage(timeout_thread_id, WM_REGISTER_ZEND_TIMEOUT, (WPARAM) GetCurrentThreadId(), (LPARAM) seconds);
1368#else
1369#   ifdef HAVE_SETITIMER
1370    {
1371        struct itimerval t_r;       /* timeout requested */
1372        int signo;
1373
1374        if(seconds) {
1375            t_r.it_value.tv_sec = seconds;
1376            t_r.it_value.tv_usec = t_r.it_interval.tv_sec = t_r.it_interval.tv_usec = 0;
1377
1378#   ifdef __CYGWIN__
1379            setitimer(ITIMER_REAL, &t_r, NULL);
1380        }
1381        signo = SIGALRM;
1382#   else
1383            setitimer(ITIMER_PROF, &t_r, NULL);
1384        }
1385        signo = SIGPROF;
1386#   endif
1387
1388        if (reset_signals) {
1389#   ifdef ZEND_SIGNALS
1390            zend_signal(signo, zend_timeout TSRMLS_CC);
1391#   else
1392            sigset_t sigset;
1393
1394            signal(signo, zend_timeout);
1395            sigemptyset(&sigset);
1396            sigaddset(&sigset, signo);
1397            sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1398#   endif
1399        }
1400    }
1401#   endif /* HAVE_SETITIMER */
1402#endif
1403}
1404/* }}} */
1405
1406void zend_unset_timeout(TSRMLS_D) /* {{{ */
1407{
1408#ifdef ZEND_WIN32
1409    if(timeout_thread_initialized) {
1410        PostThreadMessage(timeout_thread_id, WM_UNREGISTER_ZEND_TIMEOUT, (WPARAM) GetCurrentThreadId(), (LPARAM) 0);
1411    }
1412#else
1413#   ifdef HAVE_SETITIMER
1414    if (EG(timeout_seconds)) {
1415        struct itimerval no_timeout;
1416
1417        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;
1418
1419#ifdef __CYGWIN__
1420        setitimer(ITIMER_REAL, &no_timeout, NULL);
1421#else
1422        setitimer(ITIMER_PROF, &no_timeout, NULL);
1423#endif
1424    }
1425#   endif
1426#endif
1427}
1428/* }}} */
1429
1430zend_class_entry *zend_fetch_class(const char *class_name, uint class_name_len, int fetch_type TSRMLS_DC) /* {{{ */
1431{
1432    zend_class_entry **pce;
1433    int use_autoload = (fetch_type & ZEND_FETCH_CLASS_NO_AUTOLOAD) == 0;
1434    int silent       = (fetch_type & ZEND_FETCH_CLASS_SILENT) != 0;
1435
1436    fetch_type &= ZEND_FETCH_CLASS_MASK;
1437
1438check_fetch_type:
1439    switch (fetch_type) {
1440        case ZEND_FETCH_CLASS_SELF:
1441            if (!EG(scope)) {
1442                zend_error(E_ERROR, "Cannot access self:: when no class scope is active");
1443            }
1444            return EG(scope);
1445        case ZEND_FETCH_CLASS_PARENT:
1446            if (!EG(scope)) {
1447                zend_error(E_ERROR, "Cannot access parent:: when no class scope is active");
1448            }
1449            if (!EG(scope)->parent) {
1450                zend_error(E_ERROR, "Cannot access parent:: when current class scope has no parent");
1451            }
1452            return EG(scope)->parent;
1453        case ZEND_FETCH_CLASS_STATIC:
1454            if (!EG(called_scope)) {
1455                zend_error(E_ERROR, "Cannot access static:: when no class scope is active");
1456            }
1457            return EG(called_scope);
1458        case ZEND_FETCH_CLASS_AUTO: {
1459                fetch_type = zend_get_class_fetch_type(class_name, class_name_len);
1460                if (fetch_type!=ZEND_FETCH_CLASS_DEFAULT) {
1461                    goto check_fetch_type;
1462                }
1463            }
1464            break;
1465    }
1466
1467    if (zend_lookup_class_ex(class_name, class_name_len, NULL, use_autoload, &pce TSRMLS_CC) == FAILURE) {
1468        if (use_autoload) {
1469            if (!silent && !EG(exception)) {
1470                if (fetch_type == ZEND_FETCH_CLASS_INTERFACE) {
1471                    zend_error(E_ERROR, "Interface '%s' not found", class_name);
1472                } else if (fetch_type == ZEND_FETCH_CLASS_TRAIT) {
1473                    zend_error(E_ERROR, "Trait '%s' not found", class_name);
1474                } else {
1475                    zend_error(E_ERROR, "Class '%s' not found", class_name);
1476                }
1477            }
1478        }
1479        return NULL;
1480    }
1481    return *pce;
1482}
1483/* }}} */
1484
1485zend_class_entry *zend_fetch_class_by_name(const char *class_name, uint class_name_len, const zend_literal *key, int fetch_type TSRMLS_DC) /* {{{ */
1486{
1487    zend_class_entry **pce;
1488    int use_autoload = (fetch_type & ZEND_FETCH_CLASS_NO_AUTOLOAD) == 0;
1489
1490    if (zend_lookup_class_ex(class_name, class_name_len, key, use_autoload, &pce TSRMLS_CC) == FAILURE) {
1491        if (use_autoload) {
1492            if ((fetch_type & ZEND_FETCH_CLASS_SILENT) == 0 && !EG(exception)) {
1493                if ((fetch_type & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_INTERFACE) {
1494                    zend_error(E_ERROR, "Interface '%s' not found", class_name);
1495                } else if ((fetch_type & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_TRAIT) {
1496                    zend_error(E_ERROR, "Trait '%s' not found", class_name);
1497                } else {
1498                    zend_error(E_ERROR, "Class '%s' not found", class_name);
1499                }
1500            }
1501        }
1502        return NULL;
1503    }
1504    return *pce;
1505}
1506/* }}} */
1507
1508#define MAX_ABSTRACT_INFO_CNT 3
1509#define MAX_ABSTRACT_INFO_FMT "%s%s%s%s"
1510#define DISPLAY_ABSTRACT_FN(idx) \
1511    ai.afn[idx] ? ZEND_FN_SCOPE_NAME(ai.afn[idx]) : "", \
1512    ai.afn[idx] ? "::" : "", \
1513    ai.afn[idx] ? ai.afn[idx]->common.function_name : "", \
1514    ai.afn[idx] && ai.afn[idx + 1] ? ", " : (ai.afn[idx] && ai.cnt > MAX_ABSTRACT_INFO_CNT ? ", ..." : "")
1515
1516typedef struct _zend_abstract_info {
1517    zend_function *afn[MAX_ABSTRACT_INFO_CNT + 1];
1518    int cnt;
1519    int ctor;
1520} zend_abstract_info;
1521
1522static int zend_verify_abstract_class_function(zend_function *fn, zend_abstract_info *ai TSRMLS_DC) /* {{{ */
1523{
1524    if (fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
1525        if (ai->cnt < MAX_ABSTRACT_INFO_CNT) {
1526            ai->afn[ai->cnt] = fn;
1527        }
1528        if (fn->common.fn_flags & ZEND_ACC_CTOR) {
1529            if (!ai->ctor) {
1530                ai->cnt++;
1531                ai->ctor = 1;
1532            } else {
1533                ai->afn[ai->cnt] = NULL;
1534            }
1535        } else {
1536            ai->cnt++;
1537        }
1538    }
1539    return 0;
1540}
1541/* }}} */
1542
1543void zend_verify_abstract_class(zend_class_entry *ce TSRMLS_DC) /* {{{ */
1544{
1545    zend_abstract_info ai;
1546
1547    if ((ce->ce_flags & ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) && !(ce->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
1548        memset(&ai, 0, sizeof(ai));
1549
1550        zend_hash_apply_with_argument(&ce->function_table, (apply_func_arg_t) zend_verify_abstract_class_function, &ai TSRMLS_CC);
1551
1552        if (ai.cnt) {
1553            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 ")",
1554                ce->name, ai.cnt,
1555                ai.cnt > 1 ? "s" : "",
1556                DISPLAY_ABSTRACT_FN(0),
1557                DISPLAY_ABSTRACT_FN(1),
1558                DISPLAY_ABSTRACT_FN(2)
1559                );
1560        }
1561    }
1562}
1563/* }}} */
1564
1565ZEND_API void zend_reset_all_cv(HashTable *symbol_table TSRMLS_DC) /* {{{ */
1566{
1567    zend_execute_data *ex;
1568    int i;
1569
1570    for (ex = EG(current_execute_data); ex; ex = ex->prev_execute_data) {
1571        if (ex->op_array && ex->symbol_table == symbol_table) {
1572            for (i = 0; i < ex->op_array->last_var; i++) {
1573                *EX_CV_NUM(ex, i) = NULL;
1574            }
1575        }
1576    }
1577}
1578/* }}} */
1579
1580ZEND_API void zend_delete_variable(zend_execute_data *ex, HashTable *ht, const char *name, int name_len, ulong hash_value TSRMLS_DC) /* {{{ */
1581{
1582    if (zend_hash_quick_del(ht, name, name_len, hash_value) == SUCCESS) {
1583        name_len--;
1584        while (ex && ex->symbol_table == ht) {
1585            int i;
1586
1587            if (ex->op_array) {
1588                for (i = 0; i < ex->op_array->last_var; i++) {
1589                    if (ex->op_array->vars[i].hash_value == hash_value &&
1590                        ex->op_array->vars[i].name_len == name_len &&
1591                        !memcmp(ex->op_array->vars[i].name, name, name_len)) {
1592                        *EX_CV_NUM(ex, i) = NULL;
1593                        break;
1594                    }
1595                }
1596            }
1597            ex = ex->prev_execute_data;
1598        }
1599    }
1600}
1601/* }}} */
1602
1603ZEND_API int zend_delete_global_variable_ex(const char *name, int name_len, ulong hash_value TSRMLS_DC) /* {{{ */
1604{
1605    zend_execute_data *ex;
1606
1607    if (zend_hash_quick_exists(&EG(symbol_table), name, name_len + 1, hash_value)) {
1608        for (ex = EG(current_execute_data); ex; ex = ex->prev_execute_data) {
1609            if (ex->op_array && ex->symbol_table == &EG(symbol_table)) {
1610                int i;
1611                for (i = 0; i < ex->op_array->last_var; i++) {
1612                    if (ex->op_array->vars[i].hash_value == hash_value &&
1613                        ex->op_array->vars[i].name_len == name_len &&
1614                        !memcmp(ex->op_array->vars[i].name, name, name_len)
1615                    ) {
1616                        *EX_CV_NUM(ex, i) = NULL;
1617                        break;
1618                    }
1619                }
1620            }
1621        }
1622        return zend_hash_quick_del(&EG(symbol_table), name, name_len + 1, hash_value);
1623    }
1624    return FAILURE;
1625}
1626/* }}} */
1627
1628ZEND_API int zend_delete_global_variable(const char *name, int name_len TSRMLS_DC) /* {{{ */
1629{
1630    return zend_delete_global_variable_ex(name, name_len, zend_inline_hash_func(name, name_len + 1) TSRMLS_CC);
1631}
1632/* }}} */
1633
1634ZEND_API void zend_rebuild_symbol_table(TSRMLS_D) /* {{{ */
1635{
1636    zend_uint i;
1637    zend_execute_data *ex;
1638
1639    if (!EG(active_symbol_table)) {
1640
1641        /* Search for last called user function */
1642        ex = EG(current_execute_data);
1643        while (ex && !ex->op_array) {
1644            ex = ex->prev_execute_data;
1645        }
1646        if (ex && ex->symbol_table) {
1647            EG(active_symbol_table) = ex->symbol_table;
1648            return;
1649        }
1650
1651        if (ex && ex->op_array) {
1652            if (EG(symtable_cache_ptr)>=EG(symtable_cache)) {
1653                /*printf("Cache hit!  Reusing %x\n", symtable_cache[symtable_cache_ptr]);*/
1654                EG(active_symbol_table) = *(EG(symtable_cache_ptr)--);
1655            } else {
1656                ALLOC_HASHTABLE(EG(active_symbol_table));
1657                zend_hash_init(EG(active_symbol_table), ex->op_array->last_var, NULL, ZVAL_PTR_DTOR, 0);
1658                /*printf("Cache miss!  Initialized %x\n", EG(active_symbol_table));*/
1659            }
1660            ex->symbol_table = EG(active_symbol_table);
1661
1662            if (ex->op_array->this_var != -1 &&
1663                !*EX_CV_NUM(ex, ex->op_array->this_var) &&
1664                EG(This)) {
1665                *EX_CV_NUM(ex, ex->op_array->this_var) = (zval**)EX_CV_NUM(ex, ex->op_array->last_var + ex->op_array->this_var);
1666                **EX_CV_NUM(ex, ex->op_array->this_var) = EG(This);
1667            }
1668            for (i = 0; i < ex->op_array->last_var; i++) {
1669                if (*EX_CV_NUM(ex, i)) {
1670                    zend_hash_quick_update(EG(active_symbol_table),
1671                        ex->op_array->vars[i].name,
1672                        ex->op_array->vars[i].name_len + 1,
1673                        ex->op_array->vars[i].hash_value,
1674                        (void**)*EX_CV_NUM(ex, i),
1675                        sizeof(zval*),
1676                        (void**)EX_CV_NUM(ex, i));
1677                }
1678            }
1679        }
1680    }
1681}
1682/* }}} */
1683
1684/*
1685 * Local variables:
1686 * tab-width: 4
1687 * c-basic-offset: 4
1688 * indent-tabs-mode: t
1689 * End:
1690 */
1691