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_init_func_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value);
41 ZEND_API void zend_init_code_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value);
42 ZEND_API void zend_execute(zend_op_array *op_array, zval *return_value);
43 ZEND_API void execute_ex(zend_execute_data *execute_data);
44 ZEND_API void execute_internal(zend_execute_data *execute_data, zval *return_value);
45 ZEND_API zend_class_entry *zend_lookup_class(zend_string *name);
46 ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, const zval *key, int use_autoload);
47 ZEND_API zend_class_entry *zend_get_called_scope(zend_execute_data *ex);
48 ZEND_API zend_object *zend_get_this_object(zend_execute_data *ex);
49 ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name);
50 ZEND_API int zend_eval_stringl(char *str, size_t str_len, zval *retval_ptr, char *string_name);
51 ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions);
52 ZEND_API int zend_eval_stringl_ex(char *str, size_t str_len, zval *retval_ptr, char *string_name, int handle_exceptions);
53
54 /* export zend_pass_function to allow comparisons against it */
55 extern ZEND_API const zend_internal_function zend_pass_function;
56
57 ZEND_API void ZEND_FASTCALL zend_check_internal_arg_type(zend_function *zf, uint32_t arg_num, zval *arg);
58 ZEND_API int ZEND_FASTCALL zend_check_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, zval *default_value, void **cache_slot);
59 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_missing_arg_error(zend_execute_data *execute_data);
60
zend_assign_to_variable(zval * variable_ptr,zval * value,zend_uchar value_type)61 static zend_always_inline zval* zend_assign_to_variable(zval *variable_ptr, zval *value, zend_uchar value_type)
62 {
63 zend_refcounted *ref = NULL;
64
65 if (ZEND_CONST_COND(value_type & (IS_VAR|IS_CV), 1) && Z_ISREF_P(value)) {
66 ref = Z_COUNTED_P(value);
67 value = Z_REFVAL_P(value);
68 }
69
70 do {
71 if (UNEXPECTED(Z_REFCOUNTED_P(variable_ptr))) {
72 zend_refcounted *garbage;
73
74 if (Z_ISREF_P(variable_ptr)) {
75 variable_ptr = Z_REFVAL_P(variable_ptr);
76 if (EXPECTED(!Z_REFCOUNTED_P(variable_ptr))) {
77 break;
78 }
79 }
80 if (Z_TYPE_P(variable_ptr) == IS_OBJECT &&
81 UNEXPECTED(Z_OBJ_HANDLER_P(variable_ptr, set) != NULL)) {
82 Z_OBJ_HANDLER_P(variable_ptr, set)(variable_ptr, value);
83 return variable_ptr;
84 }
85 if (ZEND_CONST_COND(value_type & (IS_VAR|IS_CV), 1) && variable_ptr == value) {
86 if (value_type == IS_VAR && ref) {
87 ZEND_ASSERT(GC_REFCOUNT(ref) > 1);
88 --GC_REFCOUNT(ref);
89 }
90 return variable_ptr;
91 }
92 garbage = Z_COUNTED_P(variable_ptr);
93 if (--GC_REFCOUNT(garbage) == 0) {
94 ZVAL_COPY_VALUE(variable_ptr, value);
95 if (value_type & (IS_CONST|IS_CV)) {
96 if (UNEXPECTED(Z_OPT_REFCOUNTED_P(variable_ptr))) {
97 Z_ADDREF_P(variable_ptr);
98 }
99 } else if (ZEND_CONST_COND(value_type == IS_VAR, 1) && UNEXPECTED(ref)) {
100 if (UNEXPECTED(--GC_REFCOUNT(ref) == 0)) {
101 efree_size(ref, sizeof(zend_reference));
102 } else if (Z_OPT_REFCOUNTED_P(variable_ptr)) {
103 Z_ADDREF_P(variable_ptr);
104 }
105 }
106 zval_dtor_func(garbage);
107 return variable_ptr;
108 } else { /* we need to split */
109 /* optimized version of GC_ZVAL_CHECK_POSSIBLE_ROOT(variable_ptr) */
110 if (UNEXPECTED(GC_MAY_LEAK(garbage))) {
111 gc_possible_root(garbage);
112 }
113 }
114 }
115 } while (0);
116
117 ZVAL_COPY_VALUE(variable_ptr, value);
118 if (value_type & (IS_CONST|IS_CV)) {
119 if (UNEXPECTED(Z_OPT_REFCOUNTED_P(variable_ptr))) {
120 Z_ADDREF_P(variable_ptr);
121 }
122 } else if (ZEND_CONST_COND(value_type == IS_VAR, 1) && UNEXPECTED(ref)) {
123 if (UNEXPECTED(--GC_REFCOUNT(ref) == 0)) {
124 efree_size(ref, sizeof(zend_reference));
125 } else if (Z_OPT_REFCOUNTED_P(variable_ptr)) {
126 Z_ADDREF_P(variable_ptr);
127 }
128 }
129 return variable_ptr;
130 }
131
132 ZEND_API int zval_update_constant(zval *pp);
133 ZEND_API int zval_update_constant_ex(zval *pp, zend_class_entry *scope);
134
135 /* dedicated Zend executor functions - do not use! */
136 struct _zend_vm_stack {
137 zval *top;
138 zval *end;
139 zend_vm_stack prev;
140 };
141
142 #define ZEND_VM_STACK_HEADER_SLOTS \
143 ((ZEND_MM_ALIGNED_SIZE(sizeof(struct _zend_vm_stack)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval)) - 1) / ZEND_MM_ALIGNED_SIZE(sizeof(zval)))
144
145 #define ZEND_VM_STACK_ELEMENTS(stack) \
146 (((zval*)(stack)) + ZEND_VM_STACK_HEADER_SLOTS)
147
148 /*
149 * In general in RELEASE build ZEND_ASSERT() must be zero-cost, but for some
150 * reason, GCC generated worse code, performing CSE on assertion code and the
151 * following "slow path" and moving memory read operatins from slow path into
152 * common header. This made a degradation for the fast path.
153 * The following "#if ZEND_DEBUG" eliminates it.
154 */
155 #if ZEND_DEBUG
156 # define ZEND_ASSERT_VM_STACK(stack) ZEND_ASSERT(stack->top > (zval *) stack && stack->end > (zval *) stack && stack->top <= stack->end)
157 # 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))
158 #else
159 # define ZEND_ASSERT_VM_STACK(stack)
160 # define ZEND_ASSERT_VM_STACK_GLOBAL
161 #endif
162
163 ZEND_API void zend_vm_stack_init(void);
164 ZEND_API void zend_vm_stack_destroy(void);
165 ZEND_API void* zend_vm_stack_extend(size_t size);
166
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 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)
168 {
169 call->func = func;
170 if (object) {
171 Z_OBJ(call->This) = object;
172 ZEND_SET_CALL_INFO(call, 1, call_info);
173 } else {
174 Z_CE(call->This) = called_scope;
175 ZEND_SET_CALL_INFO(call, 0, call_info);
176 }
177 ZEND_CALL_NUM_ARGS(call) = num_args;
178 }
179
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 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)
181 {
182 zend_execute_data *call = (zend_execute_data*)EG(vm_stack_top);
183
184 ZEND_ASSERT_VM_STACK_GLOBAL;
185
186 if (UNEXPECTED(used_stack > (size_t)(((char*)EG(vm_stack_end)) - (char*)call))) {
187 call = (zend_execute_data*)zend_vm_stack_extend(used_stack);
188 ZEND_ASSERT_VM_STACK_GLOBAL;
189 zend_vm_init_call_frame(call, call_info | ZEND_CALL_ALLOCATED, func, num_args, called_scope, object);
190 return call;
191 } else {
192 EG(vm_stack_top) = (zval*)((char*)call + used_stack);
193 zend_vm_init_call_frame(call, call_info, func, num_args, called_scope, object);
194 return call;
195 }
196 }
197
zend_vm_calc_used_stack(uint32_t num_args,zend_function * func)198 static zend_always_inline uint32_t zend_vm_calc_used_stack(uint32_t num_args, zend_function *func)
199 {
200 uint32_t used_stack = ZEND_CALL_FRAME_SLOT + num_args;
201
202 if (EXPECTED(ZEND_USER_CODE(func->type))) {
203 used_stack += func->op_array.last_var + func->op_array.T - MIN(func->op_array.num_args, num_args);
204 }
205 return used_stack * sizeof(zval);
206 }
207
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 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)
209 {
210 uint32_t used_stack = zend_vm_calc_used_stack(num_args, func);
211
212 return zend_vm_stack_push_call_frame_ex(used_stack, call_info,
213 func, num_args, called_scope, object);
214 }
215
zend_vm_stack_free_extra_args_ex(uint32_t call_info,zend_execute_data * call)216 static zend_always_inline void zend_vm_stack_free_extra_args_ex(uint32_t call_info, zend_execute_data *call)
217 {
218 if (UNEXPECTED(call_info & ZEND_CALL_FREE_EXTRA_ARGS)) {
219 zval *end = ZEND_CALL_VAR_NUM(call, call->func->op_array.last_var + call->func->op_array.T);
220 zval *p = end + (ZEND_CALL_NUM_ARGS(call) - call->func->op_array.num_args);
221 do {
222 p--;
223 if (Z_REFCOUNTED_P(p)) {
224 zend_refcounted *r = Z_COUNTED_P(p);
225 if (!--GC_REFCOUNT(r)) {
226 ZVAL_NULL(p);
227 zval_dtor_func(r);
228 } else {
229 gc_check_possible_root(r);
230 }
231 }
232 } while (p != end);
233 }
234 }
235
zend_vm_stack_free_extra_args(zend_execute_data * call)236 static zend_always_inline void zend_vm_stack_free_extra_args(zend_execute_data *call)
237 {
238 zend_vm_stack_free_extra_args_ex(ZEND_CALL_INFO(call), call);
239 }
240
zend_vm_stack_free_args(zend_execute_data * call)241 static zend_always_inline void zend_vm_stack_free_args(zend_execute_data *call)
242 {
243 uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
244
245 if (EXPECTED(num_args > 0)) {
246 zval *end = ZEND_CALL_ARG(call, 1);
247 zval *p = end + num_args;
248
249 do {
250 p--;
251 if (Z_REFCOUNTED_P(p)) {
252 if (!Z_DELREF_P(p)) {
253 zend_refcounted *r = Z_COUNTED_P(p);
254 ZVAL_NULL(p);
255 zval_dtor_func(r);
256 }
257 }
258 } while (p != end);
259 }
260 }
261
zend_vm_stack_free_call_frame_ex(uint32_t call_info,zend_execute_data * call)262 static zend_always_inline void zend_vm_stack_free_call_frame_ex(uint32_t call_info, zend_execute_data *call)
263 {
264 ZEND_ASSERT_VM_STACK_GLOBAL;
265
266 if (UNEXPECTED(call_info & ZEND_CALL_ALLOCATED)) {
267 zend_vm_stack p = EG(vm_stack);
268 zend_vm_stack prev = p->prev;
269
270 ZEND_ASSERT(call == (zend_execute_data*)ZEND_VM_STACK_ELEMENTS(EG(vm_stack)));
271 EG(vm_stack_top) = prev->top;
272 EG(vm_stack_end) = prev->end;
273 EG(vm_stack) = prev;
274 efree(p);
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 uint32_t zend_get_executed_lineno(void);
293 ZEND_API zend_class_entry *zend_get_executed_scope(void);
294 ZEND_API zend_bool zend_is_executing(void);
295
296 ZEND_API void zend_set_timeout(zend_long seconds, int reset_signals);
297 ZEND_API void zend_unset_timeout(void);
298 ZEND_API ZEND_NORETURN void zend_timeout(int dummy);
299 ZEND_API zend_class_entry *zend_fetch_class(zend_string *class_name, int fetch_type);
300 ZEND_API zend_class_entry *zend_fetch_class_by_name(zend_string *class_name, const zval *key, int fetch_type);
301 void zend_verify_abstract_class(zend_class_entry *ce);
302
303 ZEND_API void zend_fetch_dimension_const(zval *result, zval *container, zval *dim, int 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 ZEND_API int ZEND_FASTCALL zend_do_fcall_overloaded(zend_execute_data *call, zval *ret);
328
329 #define CACHE_ADDR(num) \
330 ((void**)((char*)EX_RUN_TIME_CACHE() + (num)))
331
332 #define CACHED_PTR(num) \
333 ((void**)((char*)EX_RUN_TIME_CACHE() + (num)))[0]
334
335 #define CACHE_PTR(num, ptr) do { \
336 ((void**)((char*)EX_RUN_TIME_CACHE() + (num)))[0] = (ptr); \
337 } while (0)
338
339 #define CACHED_POLYMORPHIC_PTR(num, ce) \
340 (EXPECTED(((void**)((char*)EX_RUN_TIME_CACHE() + (num)))[0] == (void*)(ce)) ? \
341 ((void**)((char*)EX_RUN_TIME_CACHE() + (num)))[1] : \
342 NULL)
343
344 #define CACHE_POLYMORPHIC_PTR(num, ce, ptr) do { \
345 void **slot = (void**)((char*)EX_RUN_TIME_CACHE() + (num)); \
346 slot[0] = (ce); \
347 slot[1] = (ptr); \
348 } while (0)
349
350 #define CACHED_PTR_EX(slot) \
351 (slot)[0]
352
353 #define CACHE_PTR_EX(slot, ptr) do { \
354 (slot)[0] = (ptr); \
355 } while (0)
356
357 #define CACHED_POLYMORPHIC_PTR_EX(slot, ce) \
358 (EXPECTED((slot)[0] == (ce)) ? (slot)[1] : NULL)
359
360 #define CACHE_POLYMORPHIC_PTR_EX(slot, ce, ptr) do { \
361 (slot)[0] = (ce); \
362 (slot)[1] = (ptr); \
363 } while (0)
364
365 #define SKIP_EXT_OPLINE(opline) do { \
366 while (UNEXPECTED((opline)->opcode >= ZEND_EXT_STMT \
367 && (opline)->opcode <= ZEND_TICKS)) { \
368 (opline)--; \
369 } \
370 } while (0)
371
372 END_EXTERN_C()
373
374 #endif /* ZEND_EXECUTE_H */
375
376 /*
377 * Local variables:
378 * tab-width: 4
379 * c-basic-offset: 4
380 * indent-tabs-mode: t
381 * End:
382 * vim600: sw=4 ts=4 fdm=marker
383 * vim<600: sw=4 ts=4
384 */
385