1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2015 Zend Technologies Ltd. (http://www.zend.com) |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 2.00 of the Zend license,     |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.zend.com/license/2_00.txt.                                |
11   | If you did not receive a copy of the Zend license and are unable to  |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@zend.com so we can mail you a copy immediately.              |
14   +----------------------------------------------------------------------+
15   | Authors: Andi Gutmans <andi@zend.com>                                |
16   |          Zeev Suraski <zeev@zend.com>                                |
17   +----------------------------------------------------------------------+
18*/
19
20/* $Id$ */
21
22#include <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_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);
42ZEND_API void (*zend_execute_internal)(zend_execute_data *execute_data, zval *return_value);
43
44/* true globals */
45ZEND_API const zend_fcall_info empty_fcall_info = { 0, NULL, {{0}, {{0}}, {0}}, NULL, NULL, NULL, NULL, 0, 0 };
46ZEND_API const zend_fcall_info_cache empty_fcall_info_cache = { 0, NULL, NULL, NULL, NULL };
47
48#ifdef ZEND_WIN32
49#ifdef ZTS
50__declspec(thread)
51#endif
52HANDLE tq_timer = NULL;
53#endif
54
55#if 0&&ZEND_DEBUG
56static void (*original_sigsegv_handler)(int);
57static void zend_handle_sigsegv(int dummy) /* {{{ */
58{
59    fflush(stdout);
60    fflush(stderr);
61    if (original_sigsegv_handler == zend_handle_sigsegv) {
62        signal(SIGSEGV, original_sigsegv_handler);
63    } else {
64        signal(SIGSEGV, SIG_DFL);
65    }
66    {
67
68        fprintf(stderr, "SIGSEGV caught on opcode %d on opline %d of %s() at %s:%d\n\n",
69                active_opline->opcode,
70                active_opline-EG(active_op_array)->opcodes,
71                get_active_function_name(),
72                zend_get_executed_filename(),
73                zend_get_executed_lineno());
74/* See http://support.microsoft.com/kb/190351 */
75#ifdef PHP_WIN32
76        fflush(stderr);
77#endif
78    }
79    if (original_sigsegv_handler!=zend_handle_sigsegv) {
80        original_sigsegv_handler(dummy);
81    }
82}
83/* }}} */
84#endif
85
86static void zend_extension_activator(zend_extension *extension) /* {{{ */
87{
88    if (extension->activate) {
89        extension->activate();
90    }
91}
92/* }}} */
93
94static void zend_extension_deactivator(zend_extension *extension) /* {{{ */
95{
96    if (extension->deactivate) {
97        extension->deactivate();
98    }
99}
100/* }}} */
101
102static int clean_non_persistent_function(zval *zv) /* {{{ */
103{
104    zend_function *function = Z_PTR_P(zv);
105    return (function->type == ZEND_INTERNAL_FUNCTION) ? ZEND_HASH_APPLY_STOP : ZEND_HASH_APPLY_REMOVE;
106}
107/* }}} */
108
109ZEND_API int clean_non_persistent_function_full(zval *zv) /* {{{ */
110{
111    zend_function *function = Z_PTR_P(zv);
112    return (function->type == ZEND_INTERNAL_FUNCTION) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
113}
114/* }}} */
115
116static int clean_non_persistent_class(zval *zv) /* {{{ */
117{
118    zend_class_entry *ce = Z_PTR_P(zv);
119    return (ce->type == ZEND_INTERNAL_CLASS) ? ZEND_HASH_APPLY_STOP : ZEND_HASH_APPLY_REMOVE;
120}
121/* }}} */
122
123ZEND_API int clean_non_persistent_class_full(zval *zv) /* {{{ */
124{
125    zend_class_entry *ce = Z_PTR_P(zv);
126    return (ce->type == ZEND_INTERNAL_CLASS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
127}
128/* }}} */
129
130void init_executor(void) /* {{{ */
131{
132    zend_init_fpu();
133
134    ZVAL_NULL(&EG(uninitialized_zval));
135    /* trick to make uninitialized_zval never be modified, passed by ref, etc. */
136    ZVAL_NULL(&EG(error_zval));
137/* destroys stack frame, therefore makes core dumps worthless */
138#if 0&&ZEND_DEBUG
139    original_sigsegv_handler = signal(SIGSEGV, zend_handle_sigsegv);
140#endif
141
142    EG(symtable_cache_ptr) = EG(symtable_cache) - 1;
143    EG(symtable_cache_limit) = EG(symtable_cache) + SYMTABLE_CACHE_SIZE - 1;
144    EG(no_extensions) = 0;
145
146    EG(function_table) = CG(function_table);
147    EG(class_table) = CG(class_table);
148
149    EG(in_autoload) = NULL;
150    EG(autoload_func) = NULL;
151    EG(error_handling) = EH_NORMAL;
152
153    zend_vm_stack_init();
154
155    zend_hash_init(&EG(symbol_table), 64, NULL, ZVAL_PTR_DTOR, 0);
156    EG(valid_symbol_table) = 1;
157
158    zend_llist_apply(&zend_extensions, (llist_apply_func_t) zend_extension_activator);
159
160    zend_hash_init(&EG(included_files), 8, NULL, NULL, 0);
161
162    EG(ticks_count) = 0;
163
164    ZVAL_UNDEF(&EG(user_error_handler));
165
166    EG(current_execute_data) = NULL;
167
168    zend_stack_init(&EG(user_error_handlers_error_reporting), sizeof(int));
169    zend_stack_init(&EG(user_error_handlers), sizeof(zval));
170    zend_stack_init(&EG(user_exception_handlers), sizeof(zval));
171
172    zend_objects_store_init(&EG(objects_store), 1024);
173
174    EG(full_tables_cleanup) = 0;
175#ifdef ZEND_WIN32
176    EG(timed_out) = 0;
177#endif
178
179    EG(exception) = NULL;
180    EG(prev_exception) = NULL;
181
182    EG(scope) = NULL;
183
184    EG(ht_iterators_count) = sizeof(EG(ht_iterators_slots)) / sizeof(HashTableIterator);
185    EG(ht_iterators_used) = 0;
186    EG(ht_iterators) = EG(ht_iterators_slots);
187    memset(EG(ht_iterators), 0, sizeof(EG(ht_iterators_slots)));
188
189    EG(active) = 1;
190}
191/* }}} */
192
193static int zval_call_destructor(zval *zv) /* {{{ */
194{
195    if (Z_TYPE_P(zv) == IS_INDIRECT) {
196        zv = Z_INDIRECT_P(zv);
197    }
198    if (Z_TYPE_P(zv) == IS_OBJECT && Z_REFCOUNT_P(zv) == 1) {
199        return ZEND_HASH_APPLY_REMOVE;
200    } else {
201        return ZEND_HASH_APPLY_KEEP;
202    }
203}
204/* }}} */
205
206static void zend_unclean_zval_ptr_dtor(zval *zv) /* {{{ */
207{
208
209    if (Z_TYPE_P(zv) == IS_INDIRECT) {
210        zv = Z_INDIRECT_P(zv);
211    }
212    i_zval_ptr_dtor(zv ZEND_FILE_LINE_CC);
213}
214/* }}} */
215
216void shutdown_destructors(void) /* {{{ */
217{
218    if (CG(unclean_shutdown)) {
219        EG(symbol_table).pDestructor = zend_unclean_zval_ptr_dtor;
220    }
221    zend_try {
222        uint32_t symbols;
223        do {
224            symbols = zend_hash_num_elements(&EG(symbol_table));
225            zend_hash_reverse_apply(&EG(symbol_table), (apply_func_t) zval_call_destructor);
226        } while (symbols != zend_hash_num_elements(&EG(symbol_table)));
227        zend_objects_store_call_destructors(&EG(objects_store));
228    } zend_catch {
229        /* if we couldn't destruct cleanly, mark all objects as destructed anyway */
230        zend_objects_store_mark_destructed(&EG(objects_store));
231    } zend_end_try();
232}
233/* }}} */
234
235void shutdown_executor(void) /* {{{ */
236{
237    zend_function *func;
238    zend_class_entry *ce;
239
240    zend_try {
241
242/* Removed because this can not be safely done, e.g. in this situation:
243   Object 1 creates object 2
244   Object 3 holds reference to object 2.
245   Now when 1 and 2 are destroyed, 3 can still access 2 in its destructor, with
246   very problematic results */
247/*      zend_objects_store_call_destructors(&EG(objects_store)); */
248
249/* Moved after symbol table cleaners, because  some of the cleaners can call
250   destructors, which would use EG(symtable_cache_ptr) and thus leave leaks */
251/*      while (EG(symtable_cache_ptr)>=EG(symtable_cache)) {
252            zend_hash_destroy(*EG(symtable_cache_ptr));
253            efree(*EG(symtable_cache_ptr));
254            EG(symtable_cache_ptr)--;
255        }
256*/
257        zend_llist_apply(&zend_extensions, (llist_apply_func_t) zend_extension_deactivator);
258
259        if (CG(unclean_shutdown)) {
260            EG(symbol_table).pDestructor = zend_unclean_zval_ptr_dtor;
261        }
262        zend_hash_graceful_reverse_destroy(&EG(symbol_table));
263    } zend_end_try();
264    EG(valid_symbol_table) = 0;
265
266    zend_try {
267        zval *zeh;
268        /* remove error handlers before destroying classes and functions,
269         * so that if handler used some class, crash would not happen */
270        if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
271            zeh = &EG(user_error_handler);
272            zval_ptr_dtor(zeh);
273            ZVAL_UNDEF(&EG(user_error_handler));
274        }
275
276        if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
277            zeh = &EG(user_exception_handler);
278            zval_ptr_dtor(zeh);
279            ZVAL_UNDEF(&EG(user_exception_handler));
280        }
281
282        zend_stack_clean(&EG(user_error_handlers_error_reporting), NULL, 1);
283        zend_stack_clean(&EG(user_error_handlers), (void (*)(void *))ZVAL_DESTRUCTOR, 1);
284        zend_stack_clean(&EG(user_exception_handlers), (void (*)(void *))ZVAL_DESTRUCTOR, 1);
285    } zend_end_try();
286
287    zend_try {
288        /* Cleanup static data for functions and arrays.
289         * We need a separate cleanup stage because of the following problem:
290         * Suppose we destroy class X, which destroys the class's function table,
291         * and in the function table we have function foo() that has static $bar.
292         * Now if an object of class X is assigned to $bar, its destructor will be
293         * called and will fail since X's function table is in mid-destruction.
294         * So we want first of all to clean up all data and then move to tables destruction.
295         * Note that only run-time accessed data need to be cleaned up, pre-defined data can
296         * not contain objects and thus are not probelmatic */
297        if (EG(full_tables_cleanup)) {
298            ZEND_HASH_FOREACH_PTR(EG(function_table), func) {
299                if (func->type == ZEND_USER_FUNCTION) {
300                    zend_cleanup_op_array_data((zend_op_array *) func);
301                }
302            } ZEND_HASH_FOREACH_END();
303            ZEND_HASH_REVERSE_FOREACH_PTR(EG(class_table), ce) {
304                if (ce->type == ZEND_USER_CLASS) {
305                    zend_cleanup_user_class_data(ce);
306                } else {
307                    zend_cleanup_internal_class_data(ce);
308                }
309            } ZEND_HASH_FOREACH_END();
310        } else {
311            ZEND_HASH_REVERSE_FOREACH_PTR(EG(function_table), func) {
312                if (func->type != ZEND_USER_FUNCTION) {
313                    break;
314                }
315                zend_cleanup_op_array_data((zend_op_array *) func);
316            } ZEND_HASH_FOREACH_END();
317            ZEND_HASH_REVERSE_FOREACH_PTR(EG(class_table), ce) {
318                if (ce->type != ZEND_USER_CLASS) {
319                    break;
320                }
321                zend_cleanup_user_class_data(ce);
322            } ZEND_HASH_FOREACH_END();
323            zend_cleanup_internal_classes();
324        }
325    } zend_end_try();
326
327    zend_try {
328        zend_llist_destroy(&CG(open_files));
329    } zend_end_try();
330
331    zend_try {
332        zend_close_rsrc_list(&EG(regular_list));
333    } zend_end_try();
334
335    zend_try {
336        zend_objects_store_free_object_storage(&EG(objects_store));
337
338        zend_vm_stack_destroy();
339
340        /* Destroy all op arrays */
341        if (EG(full_tables_cleanup)) {
342            zend_hash_reverse_apply(EG(function_table), clean_non_persistent_function_full);
343            zend_hash_reverse_apply(EG(class_table), clean_non_persistent_class_full);
344        } else {
345            zend_hash_reverse_apply(EG(function_table), clean_non_persistent_function);
346            zend_hash_reverse_apply(EG(class_table), clean_non_persistent_class);
347        }
348
349        while (EG(symtable_cache_ptr)>=EG(symtable_cache)) {
350            zend_hash_destroy(*EG(symtable_cache_ptr));
351            FREE_HASHTABLE(*EG(symtable_cache_ptr));
352            EG(symtable_cache_ptr)--;
353        }
354    } zend_end_try();
355
356    zend_try {
357        clean_non_persistent_constants();
358    } zend_end_try();
359
360    zend_try {
361#if 0&&ZEND_DEBUG
362    signal(SIGSEGV, original_sigsegv_handler);
363#endif
364
365        zend_hash_destroy(&EG(included_files));
366
367        zend_stack_destroy(&EG(user_error_handlers_error_reporting));
368        zend_stack_destroy(&EG(user_error_handlers));
369        zend_stack_destroy(&EG(user_exception_handlers));
370        zend_objects_store_destroy(&EG(objects_store));
371        if (EG(in_autoload)) {
372            zend_hash_destroy(EG(in_autoload));
373            FREE_HASHTABLE(EG(in_autoload));
374        }
375    } zend_end_try();
376
377    zend_shutdown_fpu();
378
379    EG(ht_iterators_used) = 0;
380    if (EG(ht_iterators) != EG(ht_iterators_slots)) {
381        efree(EG(ht_iterators));
382    }
383
384    EG(active) = 0;
385}
386/* }}} */
387
388/* return class name and "::" or "". */
389ZEND_API const char *get_active_class_name(const char **space) /* {{{ */
390{
391    zend_function *func;
392
393    if (!zend_is_executing()) {
394        if (space) {
395            *space = "";
396        }
397        return "";
398    }
399
400    func = EG(current_execute_data)->func;
401    switch (func->type) {
402        case ZEND_USER_FUNCTION:
403        case ZEND_INTERNAL_FUNCTION:
404        {
405            zend_class_entry *ce = func->common.scope;
406
407            if (space) {
408                *space = ce ? "::" : "";
409            }
410            return ce ? ce->name->val : "";
411        }
412        default:
413            if (space) {
414                *space = "";
415            }
416            return "";
417    }
418}
419/* }}} */
420
421ZEND_API const char *get_active_function_name(void) /* {{{ */
422{
423    zend_function *func;
424
425    if (!zend_is_executing()) {
426        return NULL;
427    }
428    func = EG(current_execute_data)->func;
429    switch (func->type) {
430        case ZEND_USER_FUNCTION: {
431                zend_string *function_name = func->common.function_name;
432
433                if (function_name) {
434                    return function_name->val;
435                } else {
436                    return "main";
437                }
438            }
439            break;
440        case ZEND_INTERNAL_FUNCTION:
441            return func->common.function_name->val;
442            break;
443        default:
444            return NULL;
445    }
446}
447/* }}} */
448
449ZEND_API const char *zend_get_executed_filename(void) /* {{{ */
450{
451    zend_execute_data *ex = EG(current_execute_data);
452
453    while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
454        ex = ex->prev_execute_data;
455    }
456    if (ex) {
457        return ex->func->op_array.filename->val;
458    } else {
459        return "[no active file]";
460    }
461}
462/* }}} */
463
464ZEND_API uint zend_get_executed_lineno(void) /* {{{ */
465{
466    zend_execute_data *ex = EG(current_execute_data);
467
468    while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
469        ex = ex->prev_execute_data;
470    }
471    if (ex) {
472        if (EG(exception) && ex->opline->opcode == ZEND_HANDLE_EXCEPTION &&
473            ex->opline->lineno == 0 && EG(opline_before_exception)) {
474            return EG(opline_before_exception)->lineno;
475        }
476        return ex->opline->lineno;
477    } else {
478        return 0;
479    }
480}
481/* }}} */
482
483ZEND_API zend_bool zend_is_executing(void) /* {{{ */
484{
485    return EG(current_execute_data) != 0;
486}
487/* }}} */
488
489ZEND_API void _zval_ptr_dtor(zval *zval_ptr ZEND_FILE_LINE_DC) /* {{{ */
490{
491    i_zval_ptr_dtor(zval_ptr ZEND_FILE_LINE_RELAY_CC);
492}
493/* }}} */
494
495ZEND_API void _zval_internal_ptr_dtor(zval *zval_ptr ZEND_FILE_LINE_DC) /* {{{ */
496{
497    if (Z_REFCOUNTED_P(zval_ptr)) {
498        Z_DELREF_P(zval_ptr);
499        if (Z_REFCOUNT_P(zval_ptr) == 0) {
500            _zval_internal_dtor_for_ptr(zval_ptr ZEND_FILE_LINE_CC);
501        }
502    }
503}
504/* }}} */
505
506#define IS_VISITED_CONSTANT         0x80
507#define IS_CONSTANT_VISITED(p)      (Z_TYPE_P(p) & IS_VISITED_CONSTANT)
508#define MARK_CONSTANT_VISITED(p)    Z_TYPE_INFO_P(p) |= IS_VISITED_CONSTANT
509
510ZEND_API int zval_update_constant_ex(zval *p, zend_bool inline_change, zend_class_entry *scope) /* {{{ */
511{
512    zval *const_value;
513    char *colon;
514
515    if (IS_CONSTANT_VISITED(p)) {
516        zend_error(E_ERROR, "Cannot declare self-referencing constant '%s'", Z_STRVAL_P(p));
517    } else if (Z_TYPE_P(p) == IS_CONSTANT) {
518        int refcount;
519
520        SEPARATE_ZVAL_NOREF(p);
521        MARK_CONSTANT_VISITED(p);
522        refcount =  Z_REFCOUNTED_P(p) ? Z_REFCOUNT_P(p) : 1;
523        const_value = zend_get_constant_ex(Z_STR_P(p), scope, Z_CONST_FLAGS_P(p));
524        if (!const_value) {
525            char *actual = Z_STRVAL_P(p);
526
527            if ((colon = (char*)zend_memrchr(Z_STRVAL_P(p), ':', Z_STRLEN_P(p)))) {
528                size_t len;
529
530                zend_error(E_ERROR, "Undefined class constant '%s'", Z_STRVAL_P(p));
531                len = Z_STRLEN_P(p) - ((colon - Z_STRVAL_P(p)) + 1);
532                if (inline_change) {
533                    zend_string *tmp = zend_string_init(colon + 1, len, 0);
534                    zend_string_release(Z_STR_P(p));
535                    Z_STR_P(p) = tmp;
536                } else {
537                    Z_STR_P(p) = zend_string_init(colon + 1, len, 0);
538                }
539                Z_TYPE_FLAGS_P(p) = IS_TYPE_REFCOUNTED | IS_TYPE_COPYABLE;
540            } else {
541                zend_string *save = Z_STR_P(p);
542                char *slash;
543                size_t actual_len = Z_STRLEN_P(p);
544                if ((Z_CONST_FLAGS_P(p) & IS_CONSTANT_UNQUALIFIED) && (slash = (char *)zend_memrchr(actual, '\\', actual_len))) {
545                    actual = slash + 1;
546                    actual_len -= (actual - Z_STRVAL_P(p));
547                    if (inline_change) {
548                        zend_string *s = zend_string_init(actual, actual_len, 0);
549                        Z_STR_P(p) = s;
550                        Z_TYPE_FLAGS_P(p) = IS_TYPE_REFCOUNTED | IS_TYPE_COPYABLE;
551                    }
552                }
553                if (actual[0] == '\\') {
554                    if (inline_change) {
555                        memmove(Z_STRVAL_P(p), Z_STRVAL_P(p)+1, Z_STRLEN_P(p));
556                        --Z_STRLEN_P(p);
557                    } else {
558                        ++actual;
559                    }
560                    --actual_len;
561                }
562                if ((Z_CONST_FLAGS_P(p) & IS_CONSTANT_UNQUALIFIED) == 0) {
563                    if (save->val[0] == '\\') {
564                        zend_error(E_ERROR, "Undefined constant '%s'", save->val + 1);
565                    } else {
566                        zend_error(E_ERROR, "Undefined constant '%s'", save->val);
567                    }
568                    if (inline_change) {
569                        zend_string_release(save);
570                    }
571                    save = NULL;
572                }
573                zend_error(E_NOTICE, "Use of undefined constant %s - assumed '%s'",  actual,  actual);
574                if (!inline_change) {
575                    ZVAL_STRINGL(p, actual, actual_len);
576                } else {
577                    Z_TYPE_INFO_P(p) = Z_REFCOUNTED_P(p) ?
578                        IS_STRING_EX : IS_INTERNED_STRING_EX;
579                    if (save && save->val != actual) {
580                        zend_string_release(save);
581                    }
582                }
583            }
584        } else {
585            if (inline_change) {
586                zend_string_release(Z_STR_P(p));
587            }
588            ZVAL_COPY_VALUE(p, const_value);
589            if (Z_OPT_CONSTANT_P(p)) {
590                zval_update_constant_ex(p, 1, NULL);
591            }
592            zval_opt_copy_ctor(p);
593        }
594
595        if (Z_REFCOUNTED_P(p)) Z_SET_REFCOUNT_P(p, refcount);
596    } else if (Z_TYPE_P(p) == IS_CONSTANT_AST) {
597        zval tmp;
598        if (inline_change) {
599            SEPARATE_ZVAL_NOREF(p);
600        }
601        zend_ast_evaluate(&tmp, Z_ASTVAL_P(p), scope);
602        if (inline_change) {
603            zend_ast_destroy_and_free(Z_ASTVAL_P(p));
604            efree_size(Z_AST_P(p), sizeof(zend_ast_ref));
605        }
606        ZVAL_COPY_VALUE(p, &tmp);
607    }
608    return 0;
609}
610/* }}} */
611
612ZEND_API int zval_update_constant_inline_change(zval *pp, zend_class_entry *scope) /* {{{ */
613{
614    return zval_update_constant_ex(pp, 1, scope);
615}
616/* }}} */
617
618ZEND_API int zval_update_constant_no_inline_change(zval *pp, zend_class_entry *scope) /* {{{ */
619{
620    return zval_update_constant_ex(pp, 0, scope);
621}
622/* }}} */
623
624ZEND_API int zval_update_constant(zval *pp, zend_bool inline_change) /* {{{ */
625{
626    return zval_update_constant_ex(pp, inline_change, NULL);
627}
628/* }}} */
629
630int call_user_function(HashTable *function_table, zval *object, zval *function_name, zval *retval_ptr, uint32_t param_count, zval params[]) /* {{{ */
631{
632    return call_user_function_ex(function_table, object, function_name, retval_ptr, param_count, params, 1, NULL);
633}
634/* }}} */
635
636int call_user_function_ex(HashTable *function_table, zval *object, zval *function_name, zval *retval_ptr, uint32_t param_count, zval params[], int no_separation, zend_array *symbol_table) /* {{{ */
637{
638    zend_fcall_info fci;
639
640    fci.size = sizeof(fci);
641    fci.function_table = function_table;
642    fci.object = object ? Z_OBJ_P(object) : NULL;
643    ZVAL_COPY_VALUE(&fci.function_name, function_name);
644    fci.retval = retval_ptr;
645    fci.param_count = param_count;
646    fci.params = params;
647    fci.no_separation = (zend_bool) no_separation;
648    fci.symbol_table = symbol_table;
649
650    return zend_call_function(&fci, NULL);
651}
652/* }}} */
653
654int zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache) /* {{{ */
655{
656    uint32_t i;
657    zend_class_entry *calling_scope = NULL;
658    zend_execute_data *call, dummy_execute_data;
659    zend_fcall_info_cache fci_cache_local;
660    zend_function *func;
661    zend_class_entry *orig_scope;
662    zval tmp;
663
664    ZVAL_UNDEF(fci->retval);
665
666    if (!EG(active)) {
667        return FAILURE; /* executor is already inactive */
668    }
669
670    if (EG(exception)) {
671        return FAILURE; /* we would result in an instable executor otherwise */
672    }
673
674    switch (fci->size) {
675        case sizeof(zend_fcall_info):
676            break; /* nothing to do currently */
677        default:
678            zend_error(E_ERROR, "Corrupted fcall_info provided to zend_call_function()");
679            break;
680    }
681
682    orig_scope = EG(scope);
683
684    /* Initialize execute_data */
685    if (!EG(current_execute_data)) {
686        /* This only happens when we're called outside any execute()'s
687         * It shouldn't be strictly necessary to NULL execute_data out,
688         * but it may make bugs easier to spot
689         */
690        memset(&dummy_execute_data, 0, sizeof(zend_execute_data));
691        EG(current_execute_data) = &dummy_execute_data;
692    } else if (EG(current_execute_data)->func &&
693               ZEND_USER_CODE(EG(current_execute_data)->func->common.type) &&
694               EG(current_execute_data)->opline->opcode != ZEND_DO_FCALL &&
695               EG(current_execute_data)->opline->opcode != ZEND_DO_ICALL &&
696               EG(current_execute_data)->opline->opcode != ZEND_DO_UCALL &&
697               EG(current_execute_data)->opline->opcode != ZEND_DO_FCALL_BY_NAME) {
698        /* Insert fake frame in case of include or magic calls */
699        dummy_execute_data = *EG(current_execute_data);
700        dummy_execute_data.prev_execute_data = EG(current_execute_data);
701        dummy_execute_data.call = NULL;
702        dummy_execute_data.opline = NULL;
703        dummy_execute_data.func = NULL;
704        EG(current_execute_data) = &dummy_execute_data;
705    }
706
707    if (!fci_cache || !fci_cache->initialized) {
708        zend_string *callable_name;
709        char *error = NULL;
710
711        if (!fci_cache) {
712            fci_cache = &fci_cache_local;
713        }
714
715        if (!zend_is_callable_ex(&fci->function_name, fci->object, IS_CALLABLE_CHECK_SILENT, &callable_name, fci_cache, &error)) {
716            if (error) {
717                zend_error(E_WARNING, "Invalid callback %s, %s", callable_name->val, error);
718                efree(error);
719            }
720            if (callable_name) {
721                zend_string_release(callable_name);
722            }
723            if (EG(current_execute_data) == &dummy_execute_data) {
724                EG(current_execute_data) = dummy_execute_data.prev_execute_data;
725            }
726            return FAILURE;
727        } else if (error) {
728            /* Capitalize the first latter of the error message */
729            if (error[0] >= 'a' && error[0] <= 'z') {
730                error[0] += ('A' - 'a');
731            }
732            zend_error(E_STRICT, "%s", error);
733            efree(error);
734        }
735        zend_string_release(callable_name);
736    }
737
738    func = fci_cache->function_handler;
739    call = zend_vm_stack_push_call_frame(ZEND_CALL_TOP_FUNCTION,
740        func, fci->param_count, fci_cache->called_scope, fci_cache->object, NULL);
741    calling_scope = fci_cache->calling_scope;
742    fci->object = fci_cache->object;
743    if (fci->object &&
744        (!EG(objects_store).object_buckets ||
745         !IS_OBJ_VALID(EG(objects_store).object_buckets[fci->object->handle]))) {
746        if (EG(current_execute_data) == &dummy_execute_data) {
747            EG(current_execute_data) = dummy_execute_data.prev_execute_data;
748        }
749        return FAILURE;
750    }
751
752    if (func->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED)) {
753        if (func->common.fn_flags & ZEND_ACC_ABSTRACT) {
754            zend_error_noreturn(E_ERROR, "Cannot call abstract method %s::%s()", func->common.scope->name->val, func->common.function_name->val);
755        }
756        if (func->common.fn_flags & ZEND_ACC_DEPRECATED) {
757            zend_error(E_DEPRECATED, "Function %s%s%s() is deprecated",
758                func->common.scope ? func->common.scope->name->val : "",
759                func->common.scope ? "::" : "",
760                func->common.function_name->val);
761        }
762    }
763
764    for (i=0; i<fci->param_count; i++) {
765        zval *param;
766
767        if (ARG_SHOULD_BE_SENT_BY_REF(func, i + 1)) {
768            // TODO: Scalar values don't have reference counters anymore.
769            // They are assumed to be 1, and they may be easily passed by
770            // reference now. However, previously scalars with refcount==1
771            // might be passed and with refcount>1 might not. We can support
772            // only single behavior ???
773#if 0
774            if (Z_REFCOUNTED(fci->params[i]) &&
775                // This solution breaks the following test (omit warning message) ???
776                // Zend/tests/bug61273.phpt
777                // ext/reflection/tests/bug42976.phpt
778                // ext/standard/tests/general_functions/call_user_func_array_variation_001.phpt
779#else
780            if (!Z_REFCOUNTED(fci->params[i]) ||
781                // This solution breaks the following test (emit warning message) ???
782                // ext/pdo_sqlite/tests/pdo_005.phpt
783#endif
784                (!Z_ISREF(fci->params[i]) && Z_REFCOUNT(fci->params[i]) > 1)) {
785
786                if (fci->no_separation &&
787                    !ARG_MAY_BE_SENT_BY_REF(func, i + 1)) {
788                    if (i) {
789                        /* hack to clean up the stack */
790                        ZEND_CALL_NUM_ARGS(call) = i;
791                        zend_vm_stack_free_args(call);
792                    }
793                    zend_vm_stack_free_call_frame(call);
794
795                    zend_error(E_WARNING, "Parameter %d to %s%s%s() expected to be a reference, value given",
796                        i+1,
797                        func->common.scope ? func->common.scope->name->val : "",
798                        func->common.scope ? "::" : "",
799                        func->common.function_name->val);
800                    if (EG(current_execute_data) == &dummy_execute_data) {
801                        EG(current_execute_data) = dummy_execute_data.prev_execute_data;
802                    }
803                    return FAILURE;
804                }
805
806                if (Z_REFCOUNTED(fci->params[i])) {
807                    Z_DELREF(fci->params[i]);
808                }
809                ZVAL_DUP(&tmp, &fci->params[i]);
810                ZVAL_NEW_REF(&fci->params[i], &tmp);
811                Z_ADDREF(fci->params[i]);
812            } else if (!Z_ISREF(fci->params[i])) {
813                ZVAL_NEW_REF(&fci->params[i], &fci->params[i]);
814                Z_ADDREF(fci->params[i]);
815            } else if (Z_REFCOUNTED(fci->params[i])) {
816                Z_ADDREF(fci->params[i]);
817            }
818            param = ZEND_CALL_ARG(call, i+1);
819            ZVAL_COPY_VALUE(param, &fci->params[i]);
820        } else if (Z_ISREF(fci->params[i]) &&
821                   /* don't separate references for __call */
822                   (func->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) == 0 ) {
823            param = ZEND_CALL_ARG(call, i+1);
824            ZVAL_DUP(param, Z_REFVAL(fci->params[i]));
825        } else {
826            param = ZEND_CALL_ARG(call, i+1);
827            ZVAL_COPY(param, &fci->params[i]);
828        }
829    }
830
831    EG(scope) = calling_scope;
832    if (func->common.fn_flags & ZEND_ACC_STATIC) {
833        fci->object = NULL;
834    }
835    if (!fci->object) {
836        Z_OBJ(call->This) = NULL;
837    } else {
838        Z_OBJ(call->This) = fci->object;
839        GC_REFCOUNT(fci->object)++;
840    }
841
842    if (func->type == ZEND_USER_FUNCTION) {
843        EG(scope) = func->common.scope;
844        call->symbol_table = fci->symbol_table;
845        if (EXPECTED((func->op_array.fn_flags & ZEND_ACC_GENERATOR) == 0)) {
846            zend_init_execute_data(call, &func->op_array, fci->retval);
847            zend_execute_ex(call);
848        } else {
849            zend_generator_create_zval(call, &func->op_array, fci->retval);
850        }
851    } else if (func->type == ZEND_INTERNAL_FUNCTION) {
852        int call_via_handler = (func->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) != 0;
853        ZVAL_NULL(fci->retval);
854        if (func->common.scope) {
855            EG(scope) = func->common.scope;
856        }
857        call->prev_execute_data = EG(current_execute_data);
858        call->return_value = NULL; /* this is not a constructor call */
859        EG(current_execute_data) = call;
860        if (EXPECTED(zend_execute_internal == NULL)) {
861            /* saves one function call if zend_execute_internal is not used */
862            func->internal_function.handler(call, fci->retval);
863        } else {
864            zend_execute_internal(call, fci->retval);
865        }
866        EG(current_execute_data) = call->prev_execute_data;
867        zend_vm_stack_free_args(call);
868        zend_vm_stack_free_call_frame(call);
869
870        /*  We shouldn't fix bad extensions here,
871            because it can break proper ones (Bug #34045)
872        if (!EX(function_state).function->common.return_reference)
873        {
874            INIT_PZVAL(f->retval);
875        }*/
876        if (EG(exception)) {
877            zval_ptr_dtor(fci->retval);
878            ZVAL_UNDEF(fci->retval);
879        }
880
881        if (call_via_handler) {
882            /* We must re-initialize function again */
883            fci_cache->initialized = 0;
884        }
885    } else { /* ZEND_OVERLOADED_FUNCTION */
886        ZVAL_NULL(fci->retval);
887
888        /* Not sure what should be done here if it's a static method */
889        if (fci->object) {
890            call->prev_execute_data = EG(current_execute_data);
891            EG(current_execute_data) = call;
892            fci->object->handlers->call_method(func->common.function_name, fci->object, call, fci->retval);
893            EG(current_execute_data) = call->prev_execute_data;
894        } else {
895            zend_error_noreturn(E_ERROR, "Cannot call overloaded function for non-object");
896        }
897
898        zend_vm_stack_free_args(call);
899        zend_vm_stack_free_call_frame(call);
900
901        if (func->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY) {
902            zend_string_release(func->common.function_name);
903        }
904        efree(func);
905
906        if (EG(exception)) {
907            zval_ptr_dtor(fci->retval);
908            ZVAL_UNDEF(fci->retval);
909        }
910    }
911
912    if (fci->object) {
913        OBJ_RELEASE(fci->object);
914    }
915
916    EG(scope) = orig_scope;
917    if (EG(current_execute_data) == &dummy_execute_data) {
918        EG(current_execute_data) = dummy_execute_data.prev_execute_data;
919    }
920
921    if (EG(exception)) {
922        zend_throw_exception_internal(NULL);
923    }
924    return SUCCESS;
925}
926/* }}} */
927
928ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, const zval *key, int use_autoload) /* {{{ */
929{
930    zend_class_entry *ce = NULL;
931    zval args[1];
932    zval local_retval;
933    int retval;
934    zend_string *lc_name;
935    zend_fcall_info fcall_info;
936    zend_fcall_info_cache fcall_cache;
937
938    if (key) {
939        lc_name = Z_STR_P(key);
940    } else {
941        if (name == NULL || !name->len) {
942            return NULL;
943        }
944
945        if (name->val[0] == '\\') {
946            lc_name = zend_string_alloc(name->len - 1, 0);
947            zend_str_tolower_copy(lc_name->val, name->val + 1, name->len - 1);
948        } else {
949            lc_name = zend_string_tolower(name);
950        }
951    }
952
953    ce = zend_hash_find_ptr(EG(class_table), lc_name);
954    if (ce) {
955        if (!key) {
956            zend_string_release(lc_name);
957        }
958        return ce;
959    }
960
961    /* The compiler is not-reentrant. Make sure we __autoload() only during run-time
962     * (doesn't impact functionality of __autoload()
963    */
964    if (!use_autoload || zend_is_compiling()) {
965        if (!key) {
966            zend_string_release(lc_name);
967        }
968        return NULL;
969    }
970
971    if (!EG(autoload_func)) {
972        zend_function *func = zend_hash_str_find_ptr(EG(function_table), ZEND_AUTOLOAD_FUNC_NAME, sizeof(ZEND_AUTOLOAD_FUNC_NAME) - 1);
973        if (func) {
974            EG(autoload_func) = func;
975        } else {
976            if (!key) {
977                zend_string_release(lc_name);
978            }
979            return NULL;
980        }
981
982    }
983
984    /* Verify class name before passing it to __autoload() */
985    if (strspn(name->val, "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->len) {
986        if (!key) {
987            zend_string_release(lc_name);
988        }
989        return NULL;
990    }
991
992    if (EG(in_autoload) == NULL) {
993        ALLOC_HASHTABLE(EG(in_autoload));
994        zend_hash_init(EG(in_autoload), 8, NULL, NULL, 0);
995    }
996
997    if (zend_hash_add_empty_element(EG(in_autoload), lc_name) == NULL) {
998        if (!key) {
999            zend_string_release(lc_name);
1000        }
1001        return NULL;
1002    }
1003
1004    ZVAL_UNDEF(&local_retval);
1005
1006    if (name->val[0] == '\\') {
1007        ZVAL_STRINGL(&args[0], name->val + 1, name->len - 1);
1008    } else {
1009        ZVAL_STR_COPY(&args[0], name);
1010    }
1011
1012    fcall_info.size = sizeof(fcall_info);
1013    fcall_info.function_table = EG(function_table);
1014    ZVAL_STR_COPY(&fcall_info.function_name, EG(autoload_func)->common.function_name);
1015    fcall_info.symbol_table = NULL;
1016    fcall_info.retval = &local_retval;
1017    fcall_info.param_count = 1;
1018    fcall_info.params = args;
1019    fcall_info.object = NULL;
1020    fcall_info.no_separation = 1;
1021
1022    fcall_cache.initialized = 1;
1023    fcall_cache.function_handler = EG(autoload_func);
1024    fcall_cache.calling_scope = NULL;
1025    fcall_cache.called_scope = NULL;
1026    fcall_cache.object = NULL;
1027
1028    zend_exception_save();
1029    retval = zend_call_function(&fcall_info, &fcall_cache);
1030    zend_exception_restore();
1031
1032    zval_ptr_dtor(&args[0]);
1033    zval_dtor(&fcall_info.function_name);
1034
1035    zend_hash_del(EG(in_autoload), lc_name);
1036
1037    zval_ptr_dtor(&local_retval);
1038
1039    if (retval == SUCCESS) {
1040        ce = zend_hash_find_ptr(EG(class_table), lc_name);
1041    }
1042    if (!key) {
1043        zend_string_release(lc_name);
1044    }
1045    return ce;
1046}
1047/* }}} */
1048
1049ZEND_API zend_class_entry *zend_lookup_class(zend_string *name) /* {{{ */
1050{
1051    return zend_lookup_class_ex(name, NULL, 1);
1052}
1053/* }}} */
1054
1055ZEND_API int zend_eval_stringl(char *str, size_t str_len, zval *retval_ptr, char *string_name) /* {{{ */
1056{
1057    zval pv;
1058    zend_op_array *new_op_array;
1059    uint32_t original_compiler_options;
1060    int retval;
1061
1062    if (retval_ptr) {
1063        ZVAL_NEW_STR(&pv, zend_string_alloc(str_len + sizeof("return ;")-1, 1));
1064        memcpy(Z_STRVAL(pv), "return ", sizeof("return ") - 1);
1065        memcpy(Z_STRVAL(pv) + sizeof("return ") - 1, str, str_len);
1066        Z_STRVAL(pv)[Z_STRLEN(pv) - 1] = ';';
1067        Z_STRVAL(pv)[Z_STRLEN(pv)] = '\0';
1068    } else {
1069        ZVAL_STRINGL(&pv, str, str_len);
1070    }
1071
1072    /*printf("Evaluating '%s'\n", pv.value.str.val);*/
1073
1074    original_compiler_options = CG(compiler_options);
1075    CG(compiler_options) = ZEND_COMPILE_DEFAULT_FOR_EVAL;
1076    new_op_array = zend_compile_string(&pv, string_name);
1077    CG(compiler_options) = original_compiler_options;
1078
1079    if (new_op_array) {
1080        zval local_retval;
1081
1082        EG(no_extensions)=1;
1083
1084        zend_try {
1085            ZVAL_UNDEF(&local_retval);
1086            zend_execute(new_op_array, &local_retval);
1087        } zend_catch {
1088            destroy_op_array(new_op_array);
1089            efree_size(new_op_array, sizeof(zend_op_array));
1090            zend_bailout();
1091        } zend_end_try();
1092
1093        if (Z_TYPE(local_retval) != IS_UNDEF) {
1094            if (retval_ptr) {
1095                ZVAL_COPY_VALUE(retval_ptr, &local_retval);
1096            } else {
1097                zval_ptr_dtor(&local_retval);
1098            }
1099        } else {
1100            if (retval_ptr) {
1101                ZVAL_NULL(retval_ptr);
1102            }
1103        }
1104
1105        EG(no_extensions)=0;
1106        destroy_op_array(new_op_array);
1107        efree_size(new_op_array, sizeof(zend_op_array));
1108        retval = SUCCESS;
1109    } else {
1110        retval = FAILURE;
1111    }
1112    zval_dtor(&pv);
1113    return retval;
1114}
1115/* }}} */
1116
1117ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name) /* {{{ */
1118{
1119    return zend_eval_stringl(str, strlen(str), retval_ptr, string_name);
1120}
1121/* }}} */
1122
1123ZEND_API int zend_eval_stringl_ex(char *str, size_t str_len, zval *retval_ptr, char *string_name, int handle_exceptions) /* {{{ */
1124{
1125    int result;
1126
1127    result = zend_eval_stringl(str, str_len, retval_ptr, string_name);
1128    if (handle_exceptions && EG(exception)) {
1129        zend_exception_error(EG(exception), E_ERROR);
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) /* {{{ */
1137{
1138    return zend_eval_stringl_ex(str, strlen(str), retval_ptr, string_name, handle_exceptions);
1139}
1140/* }}} */
1141
1142ZEND_API void zend_timeout(int dummy) /* {{{ */
1143{
1144
1145    if (zend_on_timeout) {
1146#ifdef ZEND_SIGNALS
1147        /*
1148           We got here because we got a timeout signal, so we are in a signal handler
1149           at this point. However, we want to be able to timeout any user-supplied
1150           shutdown functions, so pretend we are not in a signal handler while we are
1151           calling these
1152        */
1153        SIGG(running) = 0;
1154#endif
1155        zend_on_timeout(EG(timeout_seconds));
1156    }
1157
1158    zend_error(E_ERROR, "Maximum execution time of %pd second%s exceeded", EG(timeout_seconds), EG(timeout_seconds) == 1 ? "" : "s");
1159}
1160/* }}} */
1161
1162#ifdef ZEND_WIN32
1163VOID CALLBACK tq_timer_cb(PVOID arg, BOOLEAN timed_out)
1164{
1165    zend_bool *php_timed_out;
1166
1167    /* The doc states it'll be always true, however it theoretically
1168        could be FALSE when the thread was signaled. */
1169    if (!timed_out) {
1170        return;
1171    }
1172
1173    php_timed_out = (zend_bool *)arg;
1174    *php_timed_out = 1;
1175}
1176#endif
1177
1178/* This one doesn't exists on QNX */
1179#ifndef SIGPROF
1180#define SIGPROF 27
1181#endif
1182
1183void zend_set_timeout(zend_long seconds, int reset_signals) /* {{{ */
1184{
1185
1186    EG(timeout_seconds) = seconds;
1187
1188#ifdef ZEND_WIN32
1189    if(!seconds) {
1190        return;
1191    }
1192
1193        /* Don't use ChangeTimerQueueTimer() as it will not restart an expired
1194        timer, so we could end up with just an ignored timeout. Instead
1195        delete and recreate. */
1196    if (NULL != tq_timer) {
1197        if (!DeleteTimerQueueTimer(NULL, tq_timer, NULL)) {
1198            EG(timed_out) = 0;
1199            tq_timer = NULL;
1200            zend_error(E_ERROR, "Could not delete queued timer");
1201            return;
1202        }
1203        tq_timer = NULL;
1204    }
1205
1206    /* XXX passing NULL means the default timer queue provided by the system is used */
1207    if (!CreateTimerQueueTimer(&tq_timer, NULL, (WAITORTIMERCALLBACK)tq_timer_cb, (VOID*)&EG(timed_out), seconds*1000, 0, WT_EXECUTEONLYONCE)) {
1208        EG(timed_out) = 0;
1209        tq_timer = NULL;
1210        zend_error(E_ERROR, "Could not queue new timer");
1211        return;
1212    }
1213    EG(timed_out) = 0;
1214#else
1215#   ifdef HAVE_SETITIMER
1216    {
1217        struct itimerval t_r;       /* timeout requested */
1218        int signo;
1219
1220        if(seconds) {
1221            t_r.it_value.tv_sec = seconds;
1222            t_r.it_value.tv_usec = t_r.it_interval.tv_sec = t_r.it_interval.tv_usec = 0;
1223
1224#   ifdef __CYGWIN__
1225            setitimer(ITIMER_REAL, &t_r, NULL);
1226        }
1227        signo = SIGALRM;
1228#   else
1229            setitimer(ITIMER_PROF, &t_r, NULL);
1230        }
1231        signo = SIGPROF;
1232#   endif
1233
1234        if (reset_signals) {
1235#   ifdef ZEND_SIGNALS
1236            zend_signal(signo, zend_timeout);
1237#   else
1238            sigset_t sigset;
1239
1240            signal(signo, zend_timeout);
1241            sigemptyset(&sigset);
1242            sigaddset(&sigset, signo);
1243            sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1244#   endif
1245        }
1246    }
1247#   endif /* HAVE_SETITIMER */
1248#endif
1249}
1250/* }}} */
1251
1252void zend_unset_timeout(void) /* {{{ */
1253{
1254#ifdef ZEND_WIN32
1255    if (NULL != tq_timer) {
1256        if (!DeleteTimerQueueTimer(NULL, tq_timer, NULL)) {
1257            EG(timed_out) = 0;
1258            tq_timer = NULL;
1259            zend_error(E_ERROR, "Could not delete queued timer");
1260            return;
1261        }
1262        tq_timer = NULL;
1263    }
1264    EG(timed_out) = 0;
1265#else
1266#   ifdef HAVE_SETITIMER
1267    if (EG(timeout_seconds)) {
1268        struct itimerval no_timeout;
1269
1270        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;
1271
1272#ifdef __CYGWIN__
1273        setitimer(ITIMER_REAL, &no_timeout, NULL);
1274#else
1275        setitimer(ITIMER_PROF, &no_timeout, NULL);
1276#endif
1277    }
1278#   endif
1279#endif
1280}
1281/* }}} */
1282
1283zend_class_entry *zend_fetch_class(zend_string *class_name, int fetch_type) /* {{{ */
1284{
1285    zend_class_entry *ce;
1286    int use_autoload = (fetch_type & ZEND_FETCH_CLASS_NO_AUTOLOAD) == 0;
1287    int silent       = (fetch_type & ZEND_FETCH_CLASS_SILENT) != 0;
1288
1289    fetch_type &= ZEND_FETCH_CLASS_MASK;
1290
1291check_fetch_type:
1292    switch (fetch_type) {
1293        case ZEND_FETCH_CLASS_SELF:
1294            if (!EG(scope)) {
1295                zend_error(E_ERROR, "Cannot access self:: when no class scope is active");
1296            }
1297            return EG(scope);
1298        case ZEND_FETCH_CLASS_PARENT:
1299            if (!EG(scope)) {
1300                zend_error(E_ERROR, "Cannot access parent:: when no class scope is active");
1301            }
1302            if (!EG(scope)->parent) {
1303                zend_error(E_ERROR, "Cannot access parent:: when current class scope has no parent");
1304            }
1305            return EG(scope)->parent;
1306        case ZEND_FETCH_CLASS_STATIC:
1307            if (!EG(current_execute_data) || !EG(current_execute_data)->called_scope) {
1308                zend_error(E_ERROR, "Cannot access static:: when no class scope is active");
1309            }
1310            return EG(current_execute_data)->called_scope;
1311        case ZEND_FETCH_CLASS_AUTO: {
1312                fetch_type = zend_get_class_fetch_type(class_name);
1313                if (fetch_type!=ZEND_FETCH_CLASS_DEFAULT) {
1314                    goto check_fetch_type;
1315                }
1316            }
1317            break;
1318    }
1319
1320    if ((ce = zend_lookup_class_ex(class_name, NULL, use_autoload)) == NULL) {
1321        if (use_autoload) {
1322            if (!silent && !EG(exception)) {
1323                if (fetch_type == ZEND_FETCH_CLASS_INTERFACE) {
1324                    zend_error(E_ERROR, "Interface '%s' not found", class_name->val);
1325                } else if (fetch_type == ZEND_FETCH_CLASS_TRAIT) {
1326                    zend_error(E_ERROR, "Trait '%s' not found", class_name->val);
1327                } else {
1328                    zend_error(E_ERROR, "Class '%s' not found", class_name->val);
1329                }
1330            }
1331        }
1332        return NULL;
1333    }
1334    return ce;
1335}
1336/* }}} */
1337
1338zend_class_entry *zend_fetch_class_by_name(zend_string *class_name, const zval *key, int fetch_type) /* {{{ */
1339{
1340    zend_class_entry *ce;
1341    int use_autoload = (fetch_type & ZEND_FETCH_CLASS_NO_AUTOLOAD) == 0;
1342
1343    if ((ce = zend_lookup_class_ex(class_name, key, use_autoload)) == NULL) {
1344        if (use_autoload) {
1345            if ((fetch_type & ZEND_FETCH_CLASS_SILENT) == 0 && !EG(exception)) {
1346                if ((fetch_type & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_INTERFACE) {
1347                    zend_error(E_ERROR, "Interface '%s' not found", class_name->val);
1348                } else if ((fetch_type & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_TRAIT) {
1349                    zend_error(E_ERROR, "Trait '%s' not found", class_name->val);
1350                } else {
1351                    zend_error(E_ERROR, "Class '%s' not found", class_name->val);
1352                }
1353            }
1354        }
1355        return NULL;
1356    }
1357    return ce;
1358}
1359/* }}} */
1360
1361#define MAX_ABSTRACT_INFO_CNT 3
1362#define MAX_ABSTRACT_INFO_FMT "%s%s%s%s"
1363#define DISPLAY_ABSTRACT_FN(idx) \
1364    ai.afn[idx] ? ZEND_FN_SCOPE_NAME(ai.afn[idx]) : "", \
1365    ai.afn[idx] ? "::" : "", \
1366    ai.afn[idx] ? ai.afn[idx]->common.function_name->val : "", \
1367    ai.afn[idx] && ai.afn[idx + 1] ? ", " : (ai.afn[idx] && ai.cnt > MAX_ABSTRACT_INFO_CNT ? ", ..." : "")
1368
1369typedef struct _zend_abstract_info {
1370    zend_function *afn[MAX_ABSTRACT_INFO_CNT + 1];
1371    int cnt;
1372    int ctor;
1373} zend_abstract_info;
1374
1375static void zend_verify_abstract_class_function(zend_function *fn, zend_abstract_info *ai) /* {{{ */
1376{
1377    if (fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
1378        if (ai->cnt < MAX_ABSTRACT_INFO_CNT) {
1379            ai->afn[ai->cnt] = fn;
1380        }
1381        if (fn->common.fn_flags & ZEND_ACC_CTOR) {
1382            if (!ai->ctor) {
1383                ai->cnt++;
1384                ai->ctor = 1;
1385            } else {
1386                ai->afn[ai->cnt] = NULL;
1387            }
1388        } else {
1389            ai->cnt++;
1390        }
1391    }
1392}
1393/* }}} */
1394
1395void zend_verify_abstract_class(zend_class_entry *ce) /* {{{ */
1396{
1397    zend_function *func;
1398    zend_abstract_info ai;
1399
1400    if ((ce->ce_flags & ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) && !(ce->ce_flags & (ZEND_ACC_TRAIT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) {
1401        memset(&ai, 0, sizeof(ai));
1402
1403        ZEND_HASH_FOREACH_PTR(&ce->function_table, func) {
1404            zend_verify_abstract_class_function(func, &ai);
1405        } ZEND_HASH_FOREACH_END();
1406
1407        if (ai.cnt) {
1408            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 ")",
1409                ce->name->val, ai.cnt,
1410                ai.cnt > 1 ? "s" : "",
1411                DISPLAY_ABSTRACT_FN(0),
1412                DISPLAY_ABSTRACT_FN(1),
1413                DISPLAY_ABSTRACT_FN(2)
1414                );
1415        }
1416    }
1417}
1418/* }}} */
1419
1420ZEND_API int zend_delete_global_variable(zend_string *name) /* {{{ */
1421{
1422    return zend_hash_del_ind(&EG(symbol_table), name);
1423}
1424/* }}} */
1425
1426ZEND_API zend_array *zend_rebuild_symbol_table(void) /* {{{ */
1427{
1428    int i;
1429    zend_execute_data *ex;
1430    zend_array *symbol_table;
1431
1432    /* Search for last called user function */
1433    ex = EG(current_execute_data);
1434    while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->common.type))) {
1435        ex = ex->prev_execute_data;
1436    }
1437    if (!ex) {
1438        return NULL;
1439    }
1440    if (ex->symbol_table) {
1441        return ex->symbol_table;
1442    }
1443
1444    if (EG(symtable_cache_ptr) >= EG(symtable_cache)) {
1445        /*printf("Cache hit!  Reusing %x\n", symtable_cache[symtable_cache_ptr]);*/
1446        symbol_table = ex->symbol_table = *(EG(symtable_cache_ptr)--);
1447    } else {
1448        symbol_table = ex->symbol_table = emalloc(sizeof(zend_array));
1449        zend_hash_init(symbol_table, ex->func->op_array.last_var, NULL, ZVAL_PTR_DTOR, 0);
1450        /*printf("Cache miss!  Initialized %x\n", EG(active_symbol_table));*/
1451    }
1452    for (i = 0; i < ex->func->op_array.last_var; i++) {
1453        zval zv;
1454
1455        ZVAL_INDIRECT(&zv, ZEND_CALL_VAR_NUM(ex, i));
1456        zend_hash_add_new(symbol_table, ex->func->op_array.vars[i], &zv);
1457    }
1458    return symbol_table;
1459}
1460/* }}} */
1461
1462ZEND_API void zend_attach_symbol_table(zend_execute_data *execute_data) /* {{{ */
1463{
1464    int i;
1465    zend_op_array *op_array = &execute_data->func->op_array;
1466    HashTable *ht = execute_data->symbol_table;
1467
1468    /* copy real values from symbol table into CV slots and create
1469       INDIRECT references to CV in symbol table  */
1470    for (i = 0; i < op_array->last_var; i++) {
1471        zval *zv = zend_hash_find(ht, op_array->vars[i]);
1472
1473        if (zv) {
1474            if (Z_TYPE_P(zv) == IS_INDIRECT) {
1475                zval *val = Z_INDIRECT_P(zv);
1476
1477                ZVAL_COPY_VALUE(EX_VAR_NUM(i), val);
1478            } else {
1479                ZVAL_COPY_VALUE(EX_VAR_NUM(i), zv);
1480            }
1481        } else {
1482            ZVAL_UNDEF(EX_VAR_NUM(i));
1483            zv = zend_hash_add_new(ht, op_array->vars[i], EX_VAR_NUM(i));
1484        }
1485        ZVAL_INDIRECT(zv, EX_VAR_NUM(i));
1486    }
1487}
1488/* }}} */
1489
1490ZEND_API void zend_detach_symbol_table(zend_execute_data *execute_data) /* {{{ */
1491{
1492    int i;
1493    zend_op_array *op_array = &execute_data->func->op_array;
1494    HashTable *ht = execute_data->symbol_table;
1495
1496    /* copy real values from CV slots into symbol table */
1497    for (i = 0; i < op_array->last_var; i++) {
1498        if (Z_TYPE_P(EX_VAR_NUM(i)) == IS_UNDEF) {
1499            zend_hash_del(ht, op_array->vars[i]);
1500        } else {
1501            zend_hash_update(ht, op_array->vars[i], EX_VAR_NUM(i));
1502            ZVAL_UNDEF(EX_VAR_NUM(i));
1503        }
1504    }
1505}
1506/* }}} */
1507
1508ZEND_API int zend_set_local_var(zend_string *name, zval *value, int force) /* {{{ */
1509{
1510    zend_execute_data *execute_data = EG(current_execute_data);
1511
1512    while (execute_data && (!execute_data->func || !ZEND_USER_CODE(execute_data->func->common.type))) {
1513        execute_data = execute_data->prev_execute_data;
1514    }
1515
1516    if (execute_data) {
1517        if (!execute_data->symbol_table) {
1518            zend_ulong h = zend_string_hash_val(name);
1519            zend_op_array *op_array = &execute_data->func->op_array;
1520            int i;
1521
1522            for (i = 0; i < op_array->last_var; i++) {
1523                if (op_array->vars[i]->h == h &&
1524                    op_array->vars[i]->len == name->len &&
1525                    memcmp(op_array->vars[i]->val, name->val, name->len) == 0) {
1526                    ZVAL_COPY_VALUE(EX_VAR_NUM(i), value);
1527                    return SUCCESS;
1528                }
1529            }
1530            if (force) {
1531                zend_array *symbol_table = zend_rebuild_symbol_table();
1532                if (symbol_table) {
1533                    return zend_hash_update(symbol_table, name, value) ? SUCCESS : FAILURE;;
1534                }
1535            }
1536        } else {
1537            return (zend_hash_update_ind(execute_data->symbol_table, name, value) != NULL) ? SUCCESS : FAILURE;
1538        }
1539    }
1540    return FAILURE;
1541}
1542/* }}} */
1543
1544ZEND_API int zend_set_local_var_str(const char *name, size_t len, zval *value, int force) /* {{{ */
1545{
1546    zend_execute_data *execute_data = EG(current_execute_data);
1547
1548    while (execute_data && (!execute_data->func || !ZEND_USER_CODE(execute_data->func->common.type))) {
1549        execute_data = execute_data->prev_execute_data;
1550    }
1551
1552    if (execute_data) {
1553        if (!execute_data->symbol_table) {
1554            zend_ulong h = zend_hash_func(name, len);
1555            zend_op_array *op_array = &execute_data->func->op_array;
1556            int i;
1557
1558            for (i = 0; i < op_array->last_var; i++) {
1559                if (op_array->vars[i]->h == h &&
1560                    op_array->vars[i]->len == len &&
1561                    memcmp(op_array->vars[i]->val, name, len) == 0) {
1562                    zval_ptr_dtor(EX_VAR_NUM(i));
1563                    ZVAL_COPY_VALUE(EX_VAR_NUM(i), value);
1564                    return SUCCESS;
1565                }
1566            }
1567
1568            if (force) {
1569                zend_array *symbol_table = zend_rebuild_symbol_table();
1570                if (symbol_table) {
1571                    return zend_hash_str_update(symbol_table, name, len, value) ? SUCCESS : FAILURE;;
1572                }
1573            }
1574        } else {
1575            return (zend_hash_str_update_ind(execute_data->symbol_table, name, len, value) != NULL) ? SUCCESS : FAILURE;
1576        }
1577    }
1578    return FAILURE;
1579}
1580/* }}} */
1581
1582/*
1583 * Local variables:
1584 * tab-width: 4
1585 * c-basic-offset: 4
1586 * indent-tabs-mode: t
1587 * End:
1588 */
1589