xref: /PHP-7.1/Zend/zend_execute.h (revision ccd4716e)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 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 void zend_execute(zend_op_array *op_array, zval *return_value);
41 ZEND_API void execute_ex(zend_execute_data *execute_data);
42 ZEND_API void execute_internal(zend_execute_data *execute_data, zval *return_value);
43 ZEND_API zend_class_entry *zend_lookup_class(zend_string *name);
44 ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, const zval *key, int use_autoload);
45 ZEND_API zend_class_entry *zend_get_called_scope(zend_execute_data *ex);
46 ZEND_API zend_object *zend_get_this_object(zend_execute_data *ex);
47 ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name);
48 ZEND_API int zend_eval_stringl(char *str, size_t str_len, zval *retval_ptr, char *string_name);
49 ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions);
50 ZEND_API int zend_eval_stringl_ex(char *str, size_t str_len, zval *retval_ptr, char *string_name, int handle_exceptions);
51 
52 /* export zend_pass_function to allow comparisons against it */
53 extern ZEND_API const zend_internal_function zend_pass_function;
54 
55 ZEND_API void ZEND_FASTCALL zend_check_internal_arg_type(zend_function *zf, uint32_t arg_num, zval *arg);
56 ZEND_API int  ZEND_FASTCALL zend_check_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, zval *default_value, void **cache_slot);
57 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_missing_arg_error(zend_execute_data *execute_data);
58 
zend_assign_to_variable(zval * variable_ptr,zval * value,zend_uchar value_type)59 static zend_always_inline zval* zend_assign_to_variable(zval *variable_ptr, zval *value, zend_uchar value_type)
60 {
61 	zend_refcounted *ref = NULL;
62 
63 	if (ZEND_CONST_COND(value_type & (IS_VAR|IS_CV), 1) && Z_ISREF_P(value)) {
64 		ref = Z_COUNTED_P(value);
65 		value = Z_REFVAL_P(value);
66 	}
67 
68 	do {
69 		if (UNEXPECTED(Z_REFCOUNTED_P(variable_ptr))) {
70 			zend_refcounted *garbage;
71 
72 			if (Z_ISREF_P(variable_ptr)) {
73 				variable_ptr = Z_REFVAL_P(variable_ptr);
74 				if (EXPECTED(!Z_REFCOUNTED_P(variable_ptr))) {
75 					break;
76 				}
77 			}
78 			if (Z_TYPE_P(variable_ptr) == IS_OBJECT &&
79 	    		UNEXPECTED(Z_OBJ_HANDLER_P(variable_ptr, set) != NULL)) {
80 				Z_OBJ_HANDLER_P(variable_ptr, set)(variable_ptr, value);
81 				return variable_ptr;
82 			}
83 			if (ZEND_CONST_COND(value_type & (IS_VAR|IS_CV), 1) && variable_ptr == value) {
84 				if (value_type == IS_VAR && ref) {
85 					ZEND_ASSERT(GC_REFCOUNT(ref) > 1);
86 					--GC_REFCOUNT(ref);
87 				}
88 				return variable_ptr;
89 			}
90 			garbage = Z_COUNTED_P(variable_ptr);
91 			if (--GC_REFCOUNT(garbage) == 0) {
92 				ZVAL_COPY_VALUE(variable_ptr, value);
93 				if (value_type & (IS_CONST|IS_CV)) {
94 					if (UNEXPECTED(Z_OPT_REFCOUNTED_P(variable_ptr))) {
95 						Z_ADDREF_P(variable_ptr);
96 					}
97 				} else if (ZEND_CONST_COND(value_type == IS_VAR, 1) && UNEXPECTED(ref)) {
98 					if (UNEXPECTED(--GC_REFCOUNT(ref) == 0)) {
99 						efree_size(ref, sizeof(zend_reference));
100 					} else if (Z_OPT_REFCOUNTED_P(variable_ptr)) {
101 						Z_ADDREF_P(variable_ptr);
102 					}
103 				}
104 				zval_dtor_func(garbage);
105 				return variable_ptr;
106 			} else { /* we need to split */
107 				/* optimized version of GC_ZVAL_CHECK_POSSIBLE_ROOT(variable_ptr) */
108 				if ((Z_COLLECTABLE_P(variable_ptr)) &&
109 		    		UNEXPECTED(!GC_INFO(garbage))) {
110 					gc_possible_root(garbage);
111 				}
112 			}
113 		}
114 	} while (0);
115 
116 	ZVAL_COPY_VALUE(variable_ptr, value);
117 	if (value_type & (IS_CONST|IS_CV)) {
118 		if (UNEXPECTED(Z_OPT_REFCOUNTED_P(variable_ptr))) {
119 			Z_ADDREF_P(variable_ptr);
120 		}
121 	} else if (ZEND_CONST_COND(value_type == IS_VAR, 1) && UNEXPECTED(ref)) {
122 		if (UNEXPECTED(--GC_REFCOUNT(ref) == 0)) {
123 			efree_size(ref, sizeof(zend_reference));
124 		} else if (Z_OPT_REFCOUNTED_P(variable_ptr)) {
125 			Z_ADDREF_P(variable_ptr);
126 		}
127 	}
128 	return variable_ptr;
129 }
130 
131 ZEND_API int zval_update_constant(zval *pp);
132 ZEND_API int zval_update_constant_ex(zval *pp, zend_class_entry *scope);
133 
134 /* dedicated Zend executor functions - do not use! */
135 struct _zend_vm_stack {
136 	zval *top;
137 	zval *end;
138 	zend_vm_stack prev;
139 };
140 
141 #define ZEND_VM_STACK_HEADER_SLOTS \
142 	((ZEND_MM_ALIGNED_SIZE(sizeof(struct _zend_vm_stack)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval)) - 1) / ZEND_MM_ALIGNED_SIZE(sizeof(zval)))
143 
144 #define ZEND_VM_STACK_ELEMENTS(stack) \
145 	(((zval*)(stack)) + ZEND_VM_STACK_HEADER_SLOTS)
146 
147 /*
148  * In general in RELEASE build ZEND_ASSERT() must be zero-cost, but for some
149  * reason, GCC generated worse code, performing CSE on assertion code and the
150  * following "slow path" and moving memory read operatins from slow path into
151  * common header. This made a degradation for the fast path.
152  * The following "#if ZEND_DEBUG" eliminates it.
153  */
154 #if ZEND_DEBUG
155 # define ZEND_ASSERT_VM_STACK(stack) ZEND_ASSERT(stack->top > (zval *) stack && stack->end > (zval *) stack && stack->top <= stack->end)
156 # 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))
157 #else
158 # define ZEND_ASSERT_VM_STACK(stack)
159 # define ZEND_ASSERT_VM_STACK_GLOBAL
160 #endif
161 
162 ZEND_API void zend_vm_stack_init(void);
163 ZEND_API void zend_vm_stack_destroy(void);
164 ZEND_API void* zend_vm_stack_extend(size_t size);
165 
zend_vm_init_call_frame(zend_execute_data * call,uint32_t call_info,zend_function * func,uint32_t num_args,zend_class_entry * called_scope,zend_object * object)166 static zend_always_inline void zend_vm_init_call_frame(zend_execute_data *call, uint32_t call_info, zend_function *func, uint32_t num_args, zend_class_entry *called_scope, zend_object *object)
167 {
168 	call->func = func;
169 	if (object) {
170 		Z_OBJ(call->This) = object;
171 		ZEND_SET_CALL_INFO(call, 1, call_info);
172 	} else {
173 		Z_CE(call->This) = called_scope;
174 		ZEND_SET_CALL_INFO(call, 0, call_info);
175 	}
176 	ZEND_CALL_NUM_ARGS(call) = num_args;
177 }
178 
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)179 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)
180 {
181 	zend_execute_data *call = (zend_execute_data*)EG(vm_stack_top);
182 
183 	ZEND_ASSERT_VM_STACK_GLOBAL;
184 
185 	if (UNEXPECTED(used_stack > (size_t)(((char*)EG(vm_stack_end)) - (char*)call))) {
186 		call = (zend_execute_data*)zend_vm_stack_extend(used_stack);
187 		ZEND_ASSERT_VM_STACK_GLOBAL;
188 		zend_vm_init_call_frame(call, call_info | ZEND_CALL_ALLOCATED, func, num_args, called_scope, object);
189 		return call;
190 	} else {
191 		EG(vm_stack_top) = (zval*)((char*)call + used_stack);
192 		zend_vm_init_call_frame(call, call_info, func, num_args, called_scope, object);
193 		return call;
194 	}
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(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(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 		zend_vm_stack prev = p->prev;
268 
269 		ZEND_ASSERT(call == (zend_execute_data*)ZEND_VM_STACK_ELEMENTS(EG(vm_stack)));
270 		EG(vm_stack_top) = prev->top;
271 		EG(vm_stack_end) = prev->end;
272 		EG(vm_stack) = prev;
273 		efree(p);
274 	} else {
275 		EG(vm_stack_top) = (zval*)call;
276 	}
277 
278 	ZEND_ASSERT_VM_STACK_GLOBAL;
279 }
280 
zend_vm_stack_free_call_frame(zend_execute_data * call)281 static zend_always_inline void zend_vm_stack_free_call_frame(zend_execute_data *call)
282 {
283 	zend_vm_stack_free_call_frame_ex(ZEND_CALL_INFO(call), call);
284 }
285 
286 /* services */
287 ZEND_API const char *get_active_class_name(const char **space);
288 ZEND_API const char *get_active_function_name(void);
289 ZEND_API const char *zend_get_executed_filename(void);
290 ZEND_API zend_string *zend_get_executed_filename_ex(void);
291 ZEND_API uint zend_get_executed_lineno(void);
292 ZEND_API zend_class_entry *zend_get_executed_scope(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 ZEND_NORETURN 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