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 #ifndef ZEND_EXECUTE_H
23 #define ZEND_EXECUTE_H
24
25 #include "zend_compile.h"
26 #include "zend_hash.h"
27 #include "zend_operators.h"
28 #include "zend_variables.h"
29
30 typedef union _temp_variable {
31 zval tmp_var;
32 struct {
33 zval **ptr_ptr;
34 zval *ptr;
35 zend_bool fcall_returned_reference;
36 } var;
37 struct {
38 zval **ptr_ptr; /* shared with var.ptr_ptr */
39 zval *str;
40 zend_uint offset;
41 } str_offset;
42 struct {
43 zval **ptr_ptr; /* shared with var.ptr_ptr */
44 zval *ptr; /* shared with var.ptr */
45 HashPointer fe_pos;
46 } fe;
47 zend_class_entry *class_entry;
48 } temp_variable;
49
50
51 BEGIN_EXTERN_C()
52 struct _zend_fcall_info;
53 ZEND_API extern void (*zend_execute_ex)(zend_execute_data *execute_data TSRMLS_DC);
54 ZEND_API extern void (*zend_execute_internal)(zend_execute_data *execute_data_ptr, struct _zend_fcall_info *fci, int return_value_used TSRMLS_DC);
55
56 void init_executor(TSRMLS_D);
57 void shutdown_executor(TSRMLS_D);
58 void shutdown_destructors(TSRMLS_D);
59 zend_execute_data *zend_create_execute_data_from_op_array(zend_op_array *op_array, zend_bool nested TSRMLS_DC);
60 ZEND_API void zend_execute(zend_op_array *op_array TSRMLS_DC);
61 ZEND_API void execute_ex(zend_execute_data *execute_data TSRMLS_DC);
62 ZEND_API void execute_internal(zend_execute_data *execute_data_ptr, struct _zend_fcall_info *fci, int return_value_used TSRMLS_DC);
63 ZEND_API int zend_is_true(zval *op);
64 ZEND_API int zend_lookup_class(const char *name, int name_length, zend_class_entry ***ce TSRMLS_DC);
65 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);
66 ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSRMLS_DC);
67 ZEND_API int zend_eval_stringl(char *str, int str_len, zval *retval_ptr, char *string_name TSRMLS_DC);
68 ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC);
69 ZEND_API int zend_eval_stringl_ex(char *str, int str_len, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC);
70
71 ZEND_API char * zend_verify_arg_class_kind(const zend_arg_info *cur_arg_info, ulong fetch_type, const char **class_name, zend_class_entry **pce TSRMLS_DC);
72 ZEND_API int zend_verify_arg_error(int error_type, const zend_function *zf, zend_uint arg_num, const char *need_msg, const char *need_kind, const char *given_msg, const char *given_kind TSRMLS_DC);
73
i_zval_ptr_dtor(zval * zval_ptr ZEND_FILE_LINE_DC)74 static zend_always_inline void i_zval_ptr_dtor(zval *zval_ptr ZEND_FILE_LINE_DC)
75 {
76 if (!Z_DELREF_P(zval_ptr)) {
77 TSRMLS_FETCH();
78
79 ZEND_ASSERT(zval_ptr != &EG(uninitialized_zval));
80 GC_REMOVE_ZVAL_FROM_BUFFER(zval_ptr);
81 zval_dtor(zval_ptr);
82 efree_rel(zval_ptr);
83 } else {
84 TSRMLS_FETCH();
85
86 if (Z_REFCOUNT_P(zval_ptr) == 1) {
87 Z_UNSET_ISREF_P(zval_ptr);
88 }
89
90 GC_ZVAL_CHECK_POSSIBLE_ROOT(zval_ptr);
91 }
92 }
93
i_zend_is_true(zval * op)94 static zend_always_inline int i_zend_is_true(zval *op)
95 {
96 int result;
97
98 switch (Z_TYPE_P(op)) {
99 case IS_NULL:
100 result = 0;
101 break;
102 case IS_LONG:
103 case IS_BOOL:
104 case IS_RESOURCE:
105 result = (Z_LVAL_P(op)?1:0);
106 break;
107 case IS_DOUBLE:
108 result = (Z_DVAL_P(op) ? 1 : 0);
109 break;
110 case IS_STRING:
111 if (Z_STRLEN_P(op) == 0
112 || (Z_STRLEN_P(op)==1 && Z_STRVAL_P(op)[0]=='0')) {
113 result = 0;
114 } else {
115 result = 1;
116 }
117 break;
118 case IS_ARRAY:
119 result = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
120 break;
121 case IS_OBJECT:
122 if(IS_ZEND_STD_OBJECT(*op)) {
123 TSRMLS_FETCH();
124
125 if (Z_OBJ_HT_P(op)->cast_object) {
126 zval tmp;
127 if (Z_OBJ_HT_P(op)->cast_object(op, &tmp, IS_BOOL TSRMLS_CC) == SUCCESS) {
128 result = Z_LVAL(tmp);
129 break;
130 }
131 } else if (Z_OBJ_HT_P(op)->get) {
132 zval *tmp = Z_OBJ_HT_P(op)->get(op TSRMLS_CC);
133 if(Z_TYPE_P(tmp) != IS_OBJECT) {
134 /* for safety - avoid loop */
135 convert_to_boolean(tmp);
136 result = Z_LVAL_P(tmp);
137 zval_ptr_dtor(&tmp);
138 break;
139 }
140 }
141 }
142 result = 1;
143 break;
144 default:
145 result = 0;
146 break;
147 }
148 return result;
149 }
150
151 ZEND_API int zval_update_constant(zval **pp, void *arg TSRMLS_DC);
152 ZEND_API int zval_update_constant_inline_change(zval **pp, void *arg TSRMLS_DC);
153 ZEND_API int zval_update_constant_no_inline_change(zval **pp, void *arg TSRMLS_DC);
154 ZEND_API int zval_update_constant_ex(zval **pp, void *arg, zend_class_entry *scope TSRMLS_DC);
155
156 /* dedicated Zend executor functions - do not use! */
157 #define ZEND_VM_STACK_PAGE_SIZE ((16 * 1024) - 16)
158
159 struct _zend_vm_stack {
160 void **top;
161 void **end;
162 zend_vm_stack prev;
163 };
164
165 #define ZEND_VM_STACK_ELEMETS(stack) \
166 ((void**)(((char*)(stack)) + ZEND_MM_ALIGNED_SIZE(sizeof(struct _zend_vm_stack))))
167
168 #define ZEND_VM_STACK_GROW_IF_NEEDED(count) \
169 do { \
170 if (UNEXPECTED((count) > \
171 EG(argument_stack)->end - EG(argument_stack)->top)) { \
172 zend_vm_stack_extend((count) TSRMLS_CC); \
173 } \
174 } while (0)
175
zend_vm_stack_new_page(int count)176 static zend_always_inline zend_vm_stack zend_vm_stack_new_page(int count) {
177 zend_vm_stack page = (zend_vm_stack)emalloc(ZEND_MM_ALIGNED_SIZE(sizeof(*page)) + sizeof(void*) * count);
178
179 page->top = ZEND_VM_STACK_ELEMETS(page);
180 page->end = page->top + count;
181 page->prev = NULL;
182 return page;
183 }
184
zend_vm_stack_init(TSRMLS_D)185 static zend_always_inline void zend_vm_stack_init(TSRMLS_D)
186 {
187 EG(argument_stack) = zend_vm_stack_new_page(ZEND_VM_STACK_PAGE_SIZE);
188 }
189
zend_vm_stack_destroy(TSRMLS_D)190 static zend_always_inline void zend_vm_stack_destroy(TSRMLS_D)
191 {
192 zend_vm_stack stack = EG(argument_stack);
193
194 while (stack != NULL) {
195 zend_vm_stack p = stack->prev;
196 efree(stack);
197 stack = p;
198 }
199 }
200
zend_vm_stack_extend(int count TSRMLS_DC)201 static zend_always_inline void zend_vm_stack_extend(int count TSRMLS_DC)
202 {
203 zend_vm_stack p = zend_vm_stack_new_page(count >= ZEND_VM_STACK_PAGE_SIZE ? count : ZEND_VM_STACK_PAGE_SIZE);
204 p->prev = EG(argument_stack);
205 EG(argument_stack) = p;
206 }
207
zend_vm_stack_top(TSRMLS_D)208 static zend_always_inline void **zend_vm_stack_top(TSRMLS_D)
209 {
210 return EG(argument_stack)->top;
211 }
212
zend_vm_stack_push(void * ptr TSRMLS_DC)213 static zend_always_inline void zend_vm_stack_push(void *ptr TSRMLS_DC)
214 {
215 *(EG(argument_stack)->top++) = ptr;
216 }
217
zend_vm_stack_pop(TSRMLS_D)218 static zend_always_inline void *zend_vm_stack_pop(TSRMLS_D)
219 {
220 void *el = *(--EG(argument_stack)->top);
221
222 return el;
223 }
224
zend_vm_stack_alloc(size_t size TSRMLS_DC)225 static zend_always_inline void *zend_vm_stack_alloc(size_t size TSRMLS_DC)
226 {
227 void *ret;
228
229 size = (size + (sizeof(void*) - 1)) / sizeof(void*);
230
231 /* the following comparison must be optimized out at compile time */
232 if (ZEND_MM_ALIGNMENT > sizeof(void*)) {
233 int extra = (ZEND_MM_ALIGNMENT - ((zend_uintptr_t)EG(argument_stack)->top & (ZEND_MM_ALIGNMENT - 1))) / sizeof(void*);
234
235 if (UNEXPECTED(size + extra + ZEND_MM_ALIGNED_SIZE(sizeof(void*)) / sizeof(void*) >
236 (zend_uintptr_t)(EG(argument_stack)->end - EG(argument_stack)->top))) {
237 zend_vm_stack_extend(size TSRMLS_CC);
238 } else {
239 void **old_top = EG(argument_stack)->top;
240
241 EG(argument_stack)->top += extra;
242 /* store old top on the stack */
243 *EG(argument_stack)->top = (void*)old_top;
244 EG(argument_stack)->top += ZEND_MM_ALIGNED_SIZE(sizeof(void*)) / sizeof(void*);
245 }
246 } else {
247 ZEND_VM_STACK_GROW_IF_NEEDED((int)size);
248 }
249 ret = (void*)EG(argument_stack)->top;
250 EG(argument_stack)->top += size;
251 return ret;
252 }
253
zend_vm_stack_frame_base(zend_execute_data * ex)254 static zend_always_inline void** zend_vm_stack_frame_base(zend_execute_data *ex)
255 {
256 return (void**)((char*)ex->call_slots +
257 ZEND_MM_ALIGNED_SIZE(sizeof(call_slot)) * ex->op_array->nested_calls);
258 }
259
zend_vm_stack_free_int(void * ptr TSRMLS_DC)260 static zend_always_inline void zend_vm_stack_free_int(void *ptr TSRMLS_DC)
261 {
262 if (UNEXPECTED(ZEND_VM_STACK_ELEMETS(EG(argument_stack)) == (void**)ptr)) {
263 zend_vm_stack p = EG(argument_stack);
264
265 EG(argument_stack) = p->prev;
266 efree(p);
267 } else {
268 EG(argument_stack)->top = (void**)ptr;
269 }
270 }
271
zend_vm_stack_free(void * ptr TSRMLS_DC)272 static zend_always_inline void zend_vm_stack_free(void *ptr TSRMLS_DC)
273 {
274 if (UNEXPECTED(ZEND_VM_STACK_ELEMETS(EG(argument_stack)) == (void**)ptr)) {
275 zend_vm_stack p = EG(argument_stack);
276
277 EG(argument_stack) = p->prev;
278 efree(p);
279 } else {
280 /* the following comparison must be optimized out at compile time */
281 if (ZEND_MM_ALIGNMENT > sizeof(void*)) {
282 ptr = (void*)(((char*)ptr) - ZEND_MM_ALIGNED_SIZE(sizeof(void*)));
283 EG(argument_stack)->top = *(void***)ptr;
284 } else {
285 EG(argument_stack)->top = (void**)ptr;
286 }
287 }
288 }
289
zend_vm_stack_clear_multiple(int nested TSRMLS_DC)290 static zend_always_inline void zend_vm_stack_clear_multiple(int nested TSRMLS_DC)
291 {
292 void **p = EG(argument_stack)->top - 1;
293 void **end = p - (int)(zend_uintptr_t)*p;
294
295 while (p != end) {
296 zval *q = (zval *) *(--p);
297 *p = NULL;
298 i_zval_ptr_dtor(q ZEND_FILE_LINE_CC);
299 }
300 if (nested) {
301 EG(argument_stack)->top = p;
302 } else {
303 zend_vm_stack_free_int(p TSRMLS_CC);
304 }
305 }
306
zend_vm_stack_get_args_count_ex(zend_execute_data * ex)307 static zend_always_inline int zend_vm_stack_get_args_count_ex(zend_execute_data *ex)
308 {
309 if (ex) {
310 void **p = ex->function_state.arguments;
311 return (int)(zend_uintptr_t) *p;
312 } else {
313 return 0;
314 }
315 }
316
zend_vm_stack_get_arg_ex(zend_execute_data * ex,int requested_arg)317 static zend_always_inline zval** zend_vm_stack_get_arg_ex(zend_execute_data *ex, int requested_arg)
318 {
319 void **p = ex->function_state.arguments;
320 int arg_count = (int)(zend_uintptr_t) *p;
321
322 if (UNEXPECTED(requested_arg > arg_count)) {
323 return NULL;
324 }
325 return (zval**)p - arg_count + requested_arg - 1;
326 }
327
zend_vm_stack_get_args_count(TSRMLS_D)328 static zend_always_inline int zend_vm_stack_get_args_count(TSRMLS_D)
329 {
330 return zend_vm_stack_get_args_count_ex(EG(current_execute_data)->prev_execute_data);
331 }
332
zend_vm_stack_get_arg(int requested_arg TSRMLS_DC)333 static zend_always_inline zval** zend_vm_stack_get_arg(int requested_arg TSRMLS_DC)
334 {
335 return zend_vm_stack_get_arg_ex(EG(current_execute_data)->prev_execute_data, requested_arg);
336 }
337
338 void execute_new_code(TSRMLS_D);
339
340
341 /* services */
342 ZEND_API const char *get_active_class_name(const char **space TSRMLS_DC);
343 ZEND_API const char *get_active_function_name(TSRMLS_D);
344 ZEND_API const char *zend_get_executed_filename(TSRMLS_D);
345 ZEND_API uint zend_get_executed_lineno(TSRMLS_D);
346 ZEND_API zend_bool zend_is_executing(TSRMLS_D);
347
348 ZEND_API void zend_set_timeout(long seconds, int reset_signals);
349 ZEND_API void zend_unset_timeout(TSRMLS_D);
350 ZEND_API void zend_timeout(int dummy);
351 ZEND_API zend_class_entry *zend_fetch_class(const char *class_name, uint class_name_len, int fetch_type TSRMLS_DC);
352 ZEND_API 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);
353 void zend_verify_abstract_class(zend_class_entry *ce TSRMLS_DC);
354
355 #define zendi_zval_copy_ctor(p) zval_copy_ctor(&(p))
356 #define zendi_zval_dtor(p) zval_dtor(&(p))
357
358 #define active_opline (*EG(opline_ptr))
359
360 /* The following tries to resolve the classname of a zval of type object.
361 * Since it is slow it should be only used in error messages.
362 */
363 #define Z_OBJ_CLASS_NAME_P(zval) ((zval) && Z_TYPE_P(zval) == IS_OBJECT && Z_OBJ_HT_P(zval)->get_class_entry != NULL && Z_OBJ_HT_P(zval)->get_class_entry(zval TSRMLS_CC) ? Z_OBJ_HT_P(zval)->get_class_entry(zval TSRMLS_CC)->name : "")
364
365 ZEND_API zval** zend_get_compiled_variable_value(const zend_execute_data *execute_data_ptr, zend_uint var);
366
367 #define ZEND_USER_OPCODE_CONTINUE 0 /* execute next opcode */
368 #define ZEND_USER_OPCODE_RETURN 1 /* exit from executor (return from function) */
369 #define ZEND_USER_OPCODE_DISPATCH 2 /* call original opcode handler */
370 #define ZEND_USER_OPCODE_ENTER 3 /* enter into new op_array without recursion */
371 #define ZEND_USER_OPCODE_LEAVE 4 /* return to calling op_array within the same executor */
372
373 #define ZEND_USER_OPCODE_DISPATCH_TO 0x100 /* call original handler of returned opcode */
374
375 ZEND_API int zend_set_user_opcode_handler(zend_uchar opcode, user_opcode_handler_t handler);
376 ZEND_API user_opcode_handler_t zend_get_user_opcode_handler(zend_uchar opcode);
377
378 /* former zend_execute_locks.h */
379 typedef struct _zend_free_op {
380 zval* var;
381 /* int is_var; */
382 } zend_free_op;
383
384 ZEND_API zval *zend_get_zval_ptr(int op_type, const znode_op *node, const zend_execute_data *execute_data, zend_free_op *should_free, int type TSRMLS_DC);
385 ZEND_API zval **zend_get_zval_ptr_ptr(int op_type, const znode_op *node, const zend_execute_data *execute_data, zend_free_op *should_free, int type TSRMLS_DC);
386
387 ZEND_API int zend_do_fcall(ZEND_OPCODE_HANDLER_ARGS);
388
389 void zend_clean_and_cache_symbol_table(HashTable *symbol_table TSRMLS_DC);
390 void zend_free_compiled_variables(zend_execute_data *execute_data);
391
392 #define CACHED_PTR(num) \
393 EG(active_op_array)->run_time_cache[(num)]
394
395 #define CACHE_PTR(num, ptr) do { \
396 EG(active_op_array)->run_time_cache[(num)] = (ptr); \
397 } while (0)
398
399 #define CACHED_POLYMORPHIC_PTR(num, ce) \
400 ((EG(active_op_array)->run_time_cache[(num)] == (ce)) ? \
401 EG(active_op_array)->run_time_cache[(num) + 1] : \
402 NULL)
403
404 #define CACHE_POLYMORPHIC_PTR(num, ce, ptr) do { \
405 EG(active_op_array)->run_time_cache[(num)] = (ce); \
406 EG(active_op_array)->run_time_cache[(num) + 1] = (ptr); \
407 } while (0)
408
409 END_EXTERN_C()
410
411 #endif /* ZEND_EXECUTE_H */
412
413 /*
414 * Local variables:
415 * tab-width: 4
416 * c-basic-offset: 4
417 * indent-tabs-mode: t
418 * End:
419 */
420