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