xref: /PHP-7.3/Zend/zend_execute.h (revision b45774ee)
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@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    |          Dmitry Stogov <dmitry@php.net>                              |
18    +----------------------------------------------------------------------+
19 */
20 
21 #ifndef ZEND_EXECUTE_H
22 #define ZEND_EXECUTE_H
23 
24 #include "zend_compile.h"
25 #include "zend_hash.h"
26 #include "zend_operators.h"
27 #include "zend_variables.h"
28 
29 BEGIN_EXTERN_C()
30 struct _zend_fcall_info;
31 ZEND_API extern void (*zend_execute_ex)(zend_execute_data *execute_data);
32 ZEND_API extern void (*zend_execute_internal)(zend_execute_data *execute_data, zval *return_value);
33 
34 void init_executor(void);
35 void shutdown_executor(void);
36 void shutdown_destructors(void);
37 ZEND_API void zend_init_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value);
38 ZEND_API void zend_init_func_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value);
39 ZEND_API void zend_init_code_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_DELREF(ref);
87 				}
88 				return variable_ptr;
89 			}
90 			garbage = Z_COUNTED_P(variable_ptr);
91 			if (GC_DELREF(garbage) == 0) {
92 				ZVAL_COPY_VALUE(variable_ptr, value);
93 				if (ZEND_CONST_COND(value_type  == IS_CONST, 0)) {
94 					if (UNEXPECTED(Z_OPT_REFCOUNTED_P(variable_ptr))) {
95 						Z_ADDREF_P(variable_ptr);
96 					}
97 				} else if (value_type & (IS_CONST|IS_CV)) {
98 					if (Z_OPT_REFCOUNTED_P(variable_ptr)) {
99 						Z_ADDREF_P(variable_ptr);
100 					}
101 				} else if (ZEND_CONST_COND(value_type == IS_VAR, 1) && UNEXPECTED(ref)) {
102 					if (UNEXPECTED(GC_DELREF(ref) == 0)) {
103 						efree_size(ref, sizeof(zend_reference));
104 					} else if (Z_OPT_REFCOUNTED_P(variable_ptr)) {
105 						Z_ADDREF_P(variable_ptr);
106 					}
107 				}
108 				rc_dtor_func(garbage);
109 				return variable_ptr;
110 			} else { /* we need to split */
111 				/* optimized version of GC_ZVAL_CHECK_POSSIBLE_ROOT(variable_ptr) */
112 				if (UNEXPECTED(GC_MAY_LEAK(garbage))) {
113 					gc_possible_root(garbage);
114 				}
115 			}
116 		}
117 	} while (0);
118 
119 	ZVAL_COPY_VALUE(variable_ptr, value);
120 	if (ZEND_CONST_COND(value_type == IS_CONST, 0)) {
121 		if (UNEXPECTED(Z_OPT_REFCOUNTED_P(variable_ptr))) {
122 			Z_ADDREF_P(variable_ptr);
123 		}
124 	} else if (value_type & (IS_CONST|IS_CV)) {
125 		if (Z_OPT_REFCOUNTED_P(variable_ptr)) {
126 			Z_ADDREF_P(variable_ptr);
127 		}
128 	} else if (ZEND_CONST_COND(value_type == IS_VAR, 1) && UNEXPECTED(ref)) {
129 		if (UNEXPECTED(GC_DELREF(ref) == 0)) {
130 			efree_size(ref, sizeof(zend_reference));
131 		} else if (Z_OPT_REFCOUNTED_P(variable_ptr)) {
132 			Z_ADDREF_P(variable_ptr);
133 		}
134 	}
135 	return variable_ptr;
136 }
137 
138 ZEND_API int zval_update_constant(zval *pp);
139 ZEND_API int zval_update_constant_ex(zval *pp, zend_class_entry *scope);
140 ZEND_API int zend_use_undefined_constant(zend_string *name, zend_ast_attr attr, zval *result);
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_ELEMENTS(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_init_ex(size_t page_size);
172 ZEND_API void zend_vm_stack_destroy(void);
173 ZEND_API void* zend_vm_stack_extend(size_t size);
174 
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)175 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)
176 {
177 	call->func = func;
178 	if (object) {
179 		Z_OBJ(call->This) = object;
180 		ZEND_SET_CALL_INFO(call, 1, call_info);
181 	} else {
182 		Z_CE(call->This) = called_scope;
183 		ZEND_SET_CALL_INFO(call, 0, call_info);
184 	}
185 	ZEND_CALL_NUM_ARGS(call) = num_args;
186 }
187 
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)188 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)
189 {
190 	zend_execute_data *call = (zend_execute_data*)EG(vm_stack_top);
191 
192 	ZEND_ASSERT_VM_STACK_GLOBAL;
193 
194 	if (UNEXPECTED(used_stack > (size_t)(((char*)EG(vm_stack_end)) - (char*)call))) {
195 		call = (zend_execute_data*)zend_vm_stack_extend(used_stack);
196 		ZEND_ASSERT_VM_STACK_GLOBAL;
197 		zend_vm_init_call_frame(call, call_info | ZEND_CALL_ALLOCATED, func, num_args, called_scope, object);
198 		return call;
199 	} else {
200 		EG(vm_stack_top) = (zval*)((char*)call + used_stack);
201 		zend_vm_init_call_frame(call, call_info, func, num_args, called_scope, object);
202 		return call;
203 	}
204 }
205 
zend_vm_calc_used_stack(uint32_t num_args,zend_function * func)206 static zend_always_inline uint32_t zend_vm_calc_used_stack(uint32_t num_args, zend_function *func)
207 {
208 	uint32_t used_stack = ZEND_CALL_FRAME_SLOT + num_args;
209 
210 	if (EXPECTED(ZEND_USER_CODE(func->type))) {
211 		used_stack += func->op_array.last_var + func->op_array.T - MIN(func->op_array.num_args, num_args);
212 	}
213 	return used_stack * sizeof(zval);
214 }
215 
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)216 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)
217 {
218 	uint32_t used_stack = zend_vm_calc_used_stack(num_args, func);
219 
220 	return zend_vm_stack_push_call_frame_ex(used_stack, call_info,
221 		func, num_args, called_scope, object);
222 }
223 
zend_vm_stack_free_extra_args_ex(uint32_t call_info,zend_execute_data * call)224 static zend_always_inline void zend_vm_stack_free_extra_args_ex(uint32_t call_info, zend_execute_data *call)
225 {
226 	if (UNEXPECTED(call_info & ZEND_CALL_FREE_EXTRA_ARGS)) {
227 		uint32_t count = ZEND_CALL_NUM_ARGS(call) - call->func->op_array.num_args;
228 		zval *p = ZEND_CALL_VAR_NUM(call, call->func->op_array.last_var + call->func->op_array.T);
229 		do {
230 			if (Z_REFCOUNTED_P(p)) {
231 				zend_refcounted *r = Z_COUNTED_P(p);
232 				if (!GC_DELREF(r)) {
233 					ZVAL_NULL(p);
234 					rc_dtor_func(r);
235 				} else {
236 					gc_check_possible_root(r);
237 				}
238 			}
239 			p++;
240 		} while (--count);
241  	}
242 }
243 
zend_vm_stack_free_extra_args(zend_execute_data * call)244 static zend_always_inline void zend_vm_stack_free_extra_args(zend_execute_data *call)
245 {
246 	zend_vm_stack_free_extra_args_ex(ZEND_CALL_INFO(call), call);
247 }
248 
zend_vm_stack_free_args(zend_execute_data * call)249 static zend_always_inline void zend_vm_stack_free_args(zend_execute_data *call)
250 {
251 	uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
252 
253 	if (EXPECTED(num_args > 0)) {
254 		zval *p = ZEND_CALL_ARG(call, 1);
255 
256 		do {
257 			if (Z_REFCOUNTED_P(p)) {
258 				zend_refcounted *r = Z_COUNTED_P(p);
259 				if (!GC_DELREF(r)) {
260 					ZVAL_NULL(p);
261 					rc_dtor_func(r);
262 				}
263 			}
264 			p++;
265 		} while (--num_args);
266 	}
267 }
268 
zend_vm_stack_free_call_frame_ex(uint32_t call_info,zend_execute_data * call)269 static zend_always_inline void zend_vm_stack_free_call_frame_ex(uint32_t call_info, zend_execute_data *call)
270 {
271 	ZEND_ASSERT_VM_STACK_GLOBAL;
272 
273 	if (UNEXPECTED(call_info & ZEND_CALL_ALLOCATED)) {
274 		zend_vm_stack p = EG(vm_stack);
275 		zend_vm_stack prev = p->prev;
276 
277 		ZEND_ASSERT(call == (zend_execute_data*)ZEND_VM_STACK_ELEMENTS(EG(vm_stack)));
278 		EG(vm_stack_top) = prev->top;
279 		EG(vm_stack_end) = prev->end;
280 		EG(vm_stack) = prev;
281 		efree(p);
282 	} else {
283 		EG(vm_stack_top) = (zval*)call;
284 	}
285 
286 	ZEND_ASSERT_VM_STACK_GLOBAL;
287 }
288 
zend_vm_stack_free_call_frame(zend_execute_data * call)289 static zend_always_inline void zend_vm_stack_free_call_frame(zend_execute_data *call)
290 {
291 	zend_vm_stack_free_call_frame_ex(ZEND_CALL_INFO(call), call);
292 }
293 
294 /* services */
295 ZEND_API const char *get_active_class_name(const char **space);
296 ZEND_API const char *get_active_function_name(void);
297 ZEND_API const char *zend_get_executed_filename(void);
298 ZEND_API zend_string *zend_get_executed_filename_ex(void);
299 ZEND_API uint32_t zend_get_executed_lineno(void);
300 ZEND_API zend_class_entry *zend_get_executed_scope(void);
301 ZEND_API zend_bool zend_is_executing(void);
302 
303 ZEND_API void zend_set_timeout(zend_long seconds, int reset_signals);
304 ZEND_API void zend_unset_timeout(void);
305 ZEND_API ZEND_NORETURN void zend_timeout(int dummy);
306 ZEND_API zend_class_entry *zend_fetch_class(zend_string *class_name, int fetch_type);
307 ZEND_API zend_class_entry *zend_fetch_class_by_name(zend_string *class_name, const zval *key, int fetch_type);
308 void zend_verify_abstract_class(zend_class_entry *ce);
309 
310 ZEND_API zend_function * ZEND_FASTCALL zend_fetch_function(zend_string *name);
311 ZEND_API zend_function * ZEND_FASTCALL zend_fetch_function_str(const char *name, size_t len);
312 
313 ZEND_API void zend_fetch_dimension_const(zval *result, zval *container, zval *dim, int type);
314 
315 ZEND_API zval* zend_get_compiled_variable_value(const zend_execute_data *execute_data_ptr, uint32_t var);
316 
317 #define ZEND_USER_OPCODE_CONTINUE   0 /* execute next opcode */
318 #define ZEND_USER_OPCODE_RETURN     1 /* exit from executor (return from function) */
319 #define ZEND_USER_OPCODE_DISPATCH   2 /* call original opcode handler */
320 #define ZEND_USER_OPCODE_ENTER      3 /* enter into new op_array without recursion */
321 #define ZEND_USER_OPCODE_LEAVE      4 /* return to calling op_array within the same executor */
322 
323 #define ZEND_USER_OPCODE_DISPATCH_TO 0x100 /* call original handler of returned opcode */
324 
325 ZEND_API int zend_set_user_opcode_handler(zend_uchar opcode, user_opcode_handler_t handler);
326 ZEND_API user_opcode_handler_t zend_get_user_opcode_handler(zend_uchar opcode);
327 
328 /* former zend_execute_locks.h */
329 typedef zval* zend_free_op;
330 
331 ZEND_API zval *zend_get_zval_ptr(const zend_op *opline, int op_type, const znode_op *node, const zend_execute_data *execute_data, zend_free_op *should_free, int type);
332 
333 ZEND_API void zend_clean_and_cache_symbol_table(zend_array *symbol_table);
334 ZEND_API void zend_free_compiled_variables(zend_execute_data *execute_data);
335 ZEND_API void zend_cleanup_unfinished_execution(zend_execute_data *execute_data, uint32_t op_num, uint32_t catch_op_num);
336 
337 ZEND_API int ZEND_FASTCALL zend_do_fcall_overloaded(zend_execute_data *call, zval *ret);
338 
339 #define CACHE_ADDR(num) \
340 	((void**)((char*)EX_RUN_TIME_CACHE() + (num)))
341 
342 #define CACHED_PTR(num) \
343 	((void**)((char*)EX_RUN_TIME_CACHE() + (num)))[0]
344 
345 #define CACHE_PTR(num, ptr) do { \
346 		((void**)((char*)EX_RUN_TIME_CACHE() + (num)))[0] = (ptr); \
347 	} while (0)
348 
349 #define CACHED_POLYMORPHIC_PTR(num, ce) \
350 	(EXPECTED(((void**)((char*)EX_RUN_TIME_CACHE() + (num)))[0] == (void*)(ce)) ? \
351 		((void**)((char*)EX_RUN_TIME_CACHE() + (num)))[1] : \
352 		NULL)
353 
354 #define CACHE_POLYMORPHIC_PTR(num, ce, ptr) do { \
355 		void **slot = (void**)((char*)EX_RUN_TIME_CACHE() + (num)); \
356 		slot[0] = (ce); \
357 		slot[1] = (ptr); \
358 	} while (0)
359 
360 #define CACHED_PTR_EX(slot) \
361 	(slot)[0]
362 
363 #define CACHE_PTR_EX(slot, ptr) do { \
364 		(slot)[0] = (ptr); \
365 	} while (0)
366 
367 #define CACHED_POLYMORPHIC_PTR_EX(slot, ce) \
368 	(EXPECTED((slot)[0] == (ce)) ? (slot)[1] : NULL)
369 
370 #define CACHE_POLYMORPHIC_PTR_EX(slot, ce, ptr) do { \
371 		(slot)[0] = (ce); \
372 		(slot)[1] = (ptr); \
373 	} while (0)
374 
375 #define CACHE_SPECIAL (1<<0)
376 
377 #define IS_SPECIAL_CACHE_VAL(ptr) \
378 	(((uintptr_t)(ptr)) & CACHE_SPECIAL)
379 
380 #define ENCODE_SPECIAL_CACHE_NUM(num) \
381 	((void*)((((uintptr_t)(num)) << 1) | CACHE_SPECIAL))
382 
383 #define DECODE_SPECIAL_CACHE_NUM(ptr) \
384 	(((uintptr_t)(ptr)) >> 1)
385 
386 #define ENCODE_SPECIAL_CACHE_PTR(ptr) \
387 	((void*)(((uintptr_t)(ptr)) | CACHE_SPECIAL))
388 
389 #define DECODE_SPECIAL_CACHE_PTR(ptr) \
390 	((void*)(((uintptr_t)(ptr)) & ~CACHE_SPECIAL))
391 
392 #define SKIP_EXT_OPLINE(opline) do { \
393 		while (UNEXPECTED((opline)->opcode >= ZEND_EXT_STMT \
394 			&& (opline)->opcode <= ZEND_TICKS)) {     \
395 			(opline)--;                                  \
396 		}                                                \
397 	} while (0)
398 
399 END_EXTERN_C()
400 
401 #endif /* ZEND_EXECUTE_H */
402 
403 /*
404  * Local variables:
405  * tab-width: 4
406  * c-basic-offset: 4
407  * indent-tabs-mode: t
408  * End:
409  * vim600: sw=4 ts=4 fdm=marker
410  * vim<600: sw=4 ts=4
411  */
412