xref: /PHP-7.0/Zend/zend_execute.h (revision 29ee3e3c)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2017 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    |          Dmitry Stogov <dmitry@zend.com>                             |
18    +----------------------------------------------------------------------+
19 */
20 
21 /* $Id$ */
22 
23 #ifndef ZEND_EXECUTE_H
24 #define ZEND_EXECUTE_H
25 
26 #include "zend_compile.h"
27 #include "zend_hash.h"
28 #include "zend_operators.h"
29 #include "zend_variables.h"
30 
31 BEGIN_EXTERN_C()
32 struct _zend_fcall_info;
33 ZEND_API extern void (*zend_execute_ex)(zend_execute_data *execute_data);
34 ZEND_API extern void (*zend_execute_internal)(zend_execute_data *execute_data, zval *return_value);
35 
36 void init_executor(void);
37 void shutdown_executor(void);
38 void shutdown_destructors(void);
39 ZEND_API void zend_init_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value);
40 ZEND_API zend_execute_data *zend_create_generator_execute_data(zend_execute_data *call, zend_op_array *op_array, zval *return_value);
41 ZEND_API void zend_execute(zend_op_array *op_array, zval *return_value);
42 ZEND_API void execute_ex(zend_execute_data *execute_data);
43 ZEND_API void execute_internal(zend_execute_data *execute_data, zval *return_value);
44 ZEND_API zend_class_entry *zend_lookup_class(zend_string *name);
45 ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, const zval *key, int use_autoload);
46 ZEND_API zend_class_entry *zend_get_called_scope(zend_execute_data *ex);
47 ZEND_API zend_object *zend_get_this_object(zend_execute_data *ex);
48 ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name);
49 ZEND_API int zend_eval_stringl(char *str, size_t str_len, zval *retval_ptr, char *string_name);
50 ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions);
51 ZEND_API int zend_eval_stringl_ex(char *str, size_t str_len, zval *retval_ptr, char *string_name, int handle_exceptions);
52 
53 ZEND_API void ZEND_FASTCALL zend_check_internal_arg_type(zend_function *zf, uint32_t arg_num, zval *arg);
54 ZEND_API int  ZEND_FASTCALL zend_check_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, zval *default_value, void **cache_slot);
55 ZEND_API void ZEND_FASTCALL zend_check_missing_arg(zend_execute_data *execute_data, uint32_t arg_num, void **cache_slot);
56 
zend_assign_to_variable(zval * variable_ptr,zval * value,zend_uchar value_type)57 static zend_always_inline zval* zend_assign_to_variable(zval *variable_ptr, zval *value, zend_uchar value_type)
58 {
59 	zend_refcounted *ref = NULL;
60 
61 	if (ZEND_CONST_COND(value_type & (IS_VAR|IS_CV), 1) && Z_ISREF_P(value)) {
62 		ref = Z_COUNTED_P(value);
63 		value = Z_REFVAL_P(value);
64 	}
65 
66 	do {
67 		if (UNEXPECTED(Z_REFCOUNTED_P(variable_ptr))) {
68 			zend_refcounted *garbage;
69 
70 			if (Z_ISREF_P(variable_ptr)) {
71 				variable_ptr = Z_REFVAL_P(variable_ptr);
72 				if (EXPECTED(!Z_REFCOUNTED_P(variable_ptr))) {
73 					break;
74 				}
75 			}
76 			if (Z_TYPE_P(variable_ptr) == IS_OBJECT &&
77 	    		UNEXPECTED(Z_OBJ_HANDLER_P(variable_ptr, set) != NULL)) {
78 				Z_OBJ_HANDLER_P(variable_ptr, set)(variable_ptr, value);
79 				return variable_ptr;
80 			}
81 			if (ZEND_CONST_COND(value_type & (IS_VAR|IS_CV), 1) && variable_ptr == value) {
82 				if (value_type == IS_VAR && ref) {
83 					ZEND_ASSERT(GC_REFCOUNT(ref) > 1);
84 					--GC_REFCOUNT(ref);
85 				}
86 				return variable_ptr;
87 			}
88 			garbage = Z_COUNTED_P(variable_ptr);
89 			if (--GC_REFCOUNT(garbage) == 0) {
90 				ZVAL_COPY_VALUE(variable_ptr, value);
91 				if (value_type == IS_CONST) {
92 					/* IS_CONST can't be IS_OBJECT, IS_RESOURCE or IS_REFERENCE */
93 					if (UNEXPECTED(Z_OPT_COPYABLE_P(variable_ptr))) {
94 						zval_copy_ctor_func(variable_ptr);
95 					}
96 				} else if (value_type == IS_CV) {
97 					if (UNEXPECTED(Z_OPT_REFCOUNTED_P(variable_ptr))) {
98 						Z_ADDREF_P(variable_ptr);
99 					}
100 				} else if (ZEND_CONST_COND(value_type == IS_VAR, 1) && UNEXPECTED(ref)) {
101 					if (UNEXPECTED(--GC_REFCOUNT(ref) == 0)) {
102 						efree_size(ref, sizeof(zend_reference));
103 					} else if (Z_OPT_REFCOUNTED_P(variable_ptr)) {
104 						Z_ADDREF_P(variable_ptr);
105 					}
106 				}
107 				zval_dtor_func_for_ptr(garbage);
108 				return variable_ptr;
109 			} else { /* we need to split */
110 				/* optimized version of GC_ZVAL_CHECK_POSSIBLE_ROOT(variable_ptr) */
111 				if ((Z_COLLECTABLE_P(variable_ptr)) &&
112 		    		UNEXPECTED(!GC_INFO(garbage))) {
113 					gc_possible_root(garbage);
114 				}
115 			}
116 		}
117 	} while (0);
118 
119 	ZVAL_COPY_VALUE(variable_ptr, value);
120 	if (value_type == IS_CONST) {
121 		/* IS_CONST can't be IS_OBJECT, IS_RESOURCE or IS_REFERENCE */
122 		if (UNEXPECTED(Z_OPT_COPYABLE_P(variable_ptr))) {
123 			zval_copy_ctor_func(variable_ptr);
124 		}
125 	} else if (value_type == IS_CV) {
126 		if (UNEXPECTED(Z_OPT_REFCOUNTED_P(variable_ptr))) {
127 			Z_ADDREF_P(variable_ptr);
128 		}
129 	} else if (ZEND_CONST_COND(value_type == IS_VAR, 1) && UNEXPECTED(ref)) {
130 		if (UNEXPECTED(--GC_REFCOUNT(ref) == 0)) {
131 			efree_size(ref, sizeof(zend_reference));
132 		} else if (Z_OPT_REFCOUNTED_P(variable_ptr)) {
133 			Z_ADDREF_P(variable_ptr);
134 		}
135 	}
136 	return variable_ptr;
137 }
138 
139 ZEND_API int zval_update_constant(zval *pp, zend_bool inline_change);
140 ZEND_API int zval_update_constant_ex(zval *pp, zend_bool inline_change, zend_class_entry *scope);
141 
142 /* dedicated Zend executor functions - do not use! */
143 struct _zend_vm_stack {
144 	zval *top;
145 	zval *end;
146 	zend_vm_stack prev;
147 };
148 
149 #define ZEND_VM_STACK_HEADER_SLOTS \
150 	((ZEND_MM_ALIGNED_SIZE(sizeof(struct _zend_vm_stack)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval)) - 1) / ZEND_MM_ALIGNED_SIZE(sizeof(zval)))
151 
152 #define ZEND_VM_STACK_ELEMETS(stack) \
153 	(((zval*)(stack)) + ZEND_VM_STACK_HEADER_SLOTS)
154 
155 /*
156  * In general in RELEASE build ZEND_ASSERT() must be zero-cost, but for some
157  * reason, GCC generated worse code, performing CSE on assertion code and the
158  * following "slow path" and moving memory read operatins from slow path into
159  * common header. This made a degradation for the fast path.
160  * The following "#if ZEND_DEBUG" eliminates it.
161  */
162 #if ZEND_DEBUG
163 # define ZEND_ASSERT_VM_STACK(stack) ZEND_ASSERT(stack->top > (zval *) stack && stack->end > (zval *) stack && stack->top <= stack->end)
164 # define ZEND_ASSERT_VM_STACK_GLOBAL ZEND_ASSERT(EG(vm_stack_top) > (zval *) EG(vm_stack) && EG(vm_stack_end) > (zval *) EG(vm_stack) && EG(vm_stack_top) <= EG(vm_stack_end))
165 #else
166 # define ZEND_ASSERT_VM_STACK(stack)
167 # define ZEND_ASSERT_VM_STACK_GLOBAL
168 #endif
169 
170 ZEND_API void zend_vm_stack_init(void);
171 ZEND_API void zend_vm_stack_destroy(void);
172 ZEND_API void* zend_vm_stack_extend(size_t size);
173 
zend_vm_stack_push_call_frame_ex(uint32_t used_stack,uint32_t call_info,zend_function * func,uint32_t num_args,zend_class_entry * called_scope,zend_object * object)174 static zend_always_inline zend_execute_data *zend_vm_stack_push_call_frame_ex(uint32_t used_stack, uint32_t call_info, zend_function *func, uint32_t num_args, zend_class_entry *called_scope, zend_object *object)
175 {
176 	zend_execute_data *call = (zend_execute_data*)EG(vm_stack_top);
177 
178 	ZEND_ASSERT_VM_STACK_GLOBAL;
179 
180 	if (UNEXPECTED(used_stack > (size_t)(((char*)EG(vm_stack_end)) - (char*)call))) {
181 		call = (zend_execute_data*)zend_vm_stack_extend(used_stack);
182 		ZEND_SET_CALL_INFO(call, call_info | ZEND_CALL_ALLOCATED);
183 	} else {
184 		EG(vm_stack_top) = (zval*)((char*)call + used_stack);
185 		ZEND_SET_CALL_INFO(call, call_info);
186 	}
187 
188 	ZEND_ASSERT_VM_STACK_GLOBAL;
189 
190 	call->func = func;
191 	Z_OBJ(call->This) = object;
192 	ZEND_CALL_NUM_ARGS(call) = num_args;
193 	call->called_scope = called_scope;
194 	return call;
195 }
196 
zend_vm_calc_used_stack(uint32_t num_args,zend_function * func)197 static zend_always_inline uint32_t zend_vm_calc_used_stack(uint32_t num_args, zend_function *func)
198 {
199 	uint32_t used_stack = ZEND_CALL_FRAME_SLOT + num_args;
200 
201 	if (EXPECTED(ZEND_USER_CODE(func->type))) {
202 		used_stack += func->op_array.last_var + func->op_array.T - MIN(func->op_array.num_args, num_args);
203 	}
204 	return used_stack * sizeof(zval);
205 }
206 
zend_vm_stack_push_call_frame(uint32_t call_info,zend_function * func,uint32_t num_args,zend_class_entry * called_scope,zend_object * object)207 static zend_always_inline zend_execute_data *zend_vm_stack_push_call_frame(uint32_t call_info, zend_function *func, uint32_t num_args, zend_class_entry *called_scope, zend_object *object)
208 {
209 	uint32_t used_stack = zend_vm_calc_used_stack(num_args, func);
210 
211 	return zend_vm_stack_push_call_frame_ex(used_stack, call_info,
212 		func, num_args, called_scope, object);
213 }
214 
zend_vm_stack_free_extra_args_ex(uint32_t call_info,zend_execute_data * call)215 static zend_always_inline void zend_vm_stack_free_extra_args_ex(uint32_t call_info, zend_execute_data *call)
216 {
217 	if (UNEXPECTED(call_info & ZEND_CALL_FREE_EXTRA_ARGS)) {
218 		zval *end = ZEND_CALL_VAR_NUM(call, call->func->op_array.last_var + call->func->op_array.T);
219  		zval *p = end + (ZEND_CALL_NUM_ARGS(call) - call->func->op_array.num_args);
220 		do {
221 			p--;
222 			if (Z_REFCOUNTED_P(p)) {
223 				if (!Z_DELREF_P(p)) {
224 					zend_refcounted *r = Z_COUNTED_P(p);
225 					ZVAL_NULL(p);
226 					zval_dtor_func_for_ptr(r);
227 				} else {
228 					GC_ZVAL_CHECK_POSSIBLE_ROOT(p);
229 				}
230 			}
231 		} while (p != end);
232  	}
233 }
234 
zend_vm_stack_free_extra_args(zend_execute_data * call)235 static zend_always_inline void zend_vm_stack_free_extra_args(zend_execute_data *call)
236 {
237 	zend_vm_stack_free_extra_args_ex(ZEND_CALL_INFO(call), call);
238 }
239 
zend_vm_stack_free_args(zend_execute_data * call)240 static zend_always_inline void zend_vm_stack_free_args(zend_execute_data *call)
241 {
242 	uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
243 
244 	if (EXPECTED(num_args > 0)) {
245 		zval *end = ZEND_CALL_ARG(call, 1);
246 		zval *p = end + num_args;
247 
248 		do {
249 			p--;
250 			if (Z_REFCOUNTED_P(p)) {
251 				if (!Z_DELREF_P(p)) {
252 					zend_refcounted *r = Z_COUNTED_P(p);
253 					ZVAL_NULL(p);
254 					zval_dtor_func_for_ptr(r);
255 				}
256 			}
257 		} while (p != end);
258 	}
259 }
260 
zend_vm_stack_free_call_frame_ex(uint32_t call_info,zend_execute_data * call)261 static zend_always_inline void zend_vm_stack_free_call_frame_ex(uint32_t call_info, zend_execute_data *call)
262 {
263 	ZEND_ASSERT_VM_STACK_GLOBAL;
264 
265 	if (UNEXPECTED(call_info & ZEND_CALL_ALLOCATED)) {
266 		zend_vm_stack p = EG(vm_stack);
267 
268 		zend_vm_stack prev = p->prev;
269 
270 		EG(vm_stack_top) = prev->top;
271 		EG(vm_stack_end) = prev->end;
272 		EG(vm_stack) = prev;
273 		efree(p);
274 
275 	} else {
276 		EG(vm_stack_top) = (zval*)call;
277 	}
278 
279 	ZEND_ASSERT_VM_STACK_GLOBAL;
280 }
281 
zend_vm_stack_free_call_frame(zend_execute_data * call)282 static zend_always_inline void zend_vm_stack_free_call_frame(zend_execute_data *call)
283 {
284 	zend_vm_stack_free_call_frame_ex(ZEND_CALL_INFO(call), call);
285 }
286 
287 /* services */
288 ZEND_API const char *get_active_class_name(const char **space);
289 ZEND_API const char *get_active_function_name(void);
290 ZEND_API const char *zend_get_executed_filename(void);
291 ZEND_API zend_string *zend_get_executed_filename_ex(void);
292 ZEND_API uint zend_get_executed_lineno(void);
293 ZEND_API zend_bool zend_is_executing(void);
294 
295 ZEND_API void zend_set_timeout(zend_long seconds, int reset_signals);
296 ZEND_API void zend_unset_timeout(void);
297 ZEND_API void zend_timeout(int dummy);
298 ZEND_API zend_class_entry *zend_fetch_class(zend_string *class_name, int fetch_type);
299 ZEND_API zend_class_entry *zend_fetch_class_by_name(zend_string *class_name, const zval *key, int fetch_type);
300 void zend_verify_abstract_class(zend_class_entry *ce);
301 
302 ZEND_API void zend_fetch_dimension_by_zval(zval *result, zval *container, zval *dim);
303 ZEND_API void zend_fetch_dimension_by_zval_is(zval *result, zval *container, zval *dim, int dim_type);
304 
305 ZEND_API zval* zend_get_compiled_variable_value(const zend_execute_data *execute_data_ptr, uint32_t var);
306 
307 #define ZEND_USER_OPCODE_CONTINUE   0 /* execute next opcode */
308 #define ZEND_USER_OPCODE_RETURN     1 /* exit from executor (return from function) */
309 #define ZEND_USER_OPCODE_DISPATCH   2 /* call original opcode handler */
310 #define ZEND_USER_OPCODE_ENTER      3 /* enter into new op_array without recursion */
311 #define ZEND_USER_OPCODE_LEAVE      4 /* return to calling op_array within the same executor */
312 
313 #define ZEND_USER_OPCODE_DISPATCH_TO 0x100 /* call original handler of returned opcode */
314 
315 ZEND_API int zend_set_user_opcode_handler(zend_uchar opcode, user_opcode_handler_t handler);
316 ZEND_API user_opcode_handler_t zend_get_user_opcode_handler(zend_uchar opcode);
317 
318 /* former zend_execute_locks.h */
319 typedef zval* zend_free_op;
320 
321 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);
322 
323 ZEND_API void zend_clean_and_cache_symbol_table(zend_array *symbol_table);
324 void zend_free_compiled_variables(zend_execute_data *execute_data);
325 void zend_cleanup_unfinished_execution(zend_execute_data *execute_data, uint32_t op_num, uint32_t catch_op_num);
326 
327 #define CACHE_ADDR(num) \
328 	((void**)((char*)EX_RUN_TIME_CACHE() + (num)))
329 
330 #define CACHED_PTR(num) \
331 	((void**)((char*)EX_RUN_TIME_CACHE() + (num)))[0]
332 
333 #define CACHE_PTR(num, ptr) do { \
334 		((void**)((char*)EX_RUN_TIME_CACHE() + (num)))[0] = (ptr); \
335 	} while (0)
336 
337 #define CACHED_POLYMORPHIC_PTR(num, ce) \
338 	(EXPECTED(((void**)((char*)EX_RUN_TIME_CACHE() + (num)))[0] == (void*)(ce)) ? \
339 		((void**)((char*)EX_RUN_TIME_CACHE() + (num)))[1] : \
340 		NULL)
341 
342 #define CACHE_POLYMORPHIC_PTR(num, ce, ptr) do { \
343 		void **slot = (void**)((char*)EX_RUN_TIME_CACHE() + (num)); \
344 		slot[0] = (ce); \
345 		slot[1] = (ptr); \
346 	} while (0)
347 
348 #define CACHED_PTR_EX(slot) \
349 	(slot)[0]
350 
351 #define CACHE_PTR_EX(slot, ptr) do { \
352 		(slot)[0] = (ptr); \
353 	} while (0)
354 
355 #define CACHED_POLYMORPHIC_PTR_EX(slot, ce) \
356 	(EXPECTED((slot)[0] == (ce)) ? (slot)[1] : NULL)
357 
358 #define CACHE_POLYMORPHIC_PTR_EX(slot, ce, ptr) do { \
359 		(slot)[0] = (ce); \
360 		(slot)[1] = (ptr); \
361 	} while (0)
362 
363 #define SKIP_EXT_OPLINE(opline) do { \
364 		while (UNEXPECTED((opline)->opcode >= ZEND_EXT_STMT \
365 			&& (opline)->opcode <= ZEND_TICKS)) {     \
366 			(opline)--;                                  \
367 		}                                                \
368 	} while (0)
369 
370 END_EXTERN_C()
371 
372 #endif /* ZEND_EXECUTE_H */
373 
374 /*
375  * Local variables:
376  * tab-width: 4
377  * c-basic-offset: 4
378  * indent-tabs-mode: t
379  * End:
380  */
381