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