1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 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, zend_string *lcname, uint32_t flags);
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 ZEND_COLD void ZEND_FASTCALL zend_missing_arg_error(zend_execute_data *execute_data);
56
57 ZEND_API zend_bool ZEND_FASTCALL zend_verify_ref_assignable_zval(zend_reference *ref, zval *zv, zend_bool strict);
58 ZEND_API zend_bool ZEND_FASTCALL zend_verify_prop_assignable_by_ref(zend_property_info *prop_info, zval *orig_val, zend_bool strict);
59
60 ZEND_API ZEND_COLD void zend_throw_ref_type_error_zval(zend_property_info *prop, zval *zv);
61 ZEND_API ZEND_COLD void zend_throw_ref_type_error_type(zend_property_info *prop1, zend_property_info *prop2, zval *zv);
62
63 #define ZEND_REF_TYPE_SOURCES(ref) \
64 (ref)->sources
65
66 #define ZEND_REF_HAS_TYPE_SOURCES(ref) \
67 (ZEND_REF_TYPE_SOURCES(ref).ptr != NULL)
68
69 #define ZEND_REF_FIRST_SOURCE(ref) \
70 (ZEND_PROPERTY_INFO_SOURCE_IS_LIST((ref)->sources.list) \
71 ? ZEND_PROPERTY_INFO_SOURCE_TO_LIST((ref)->sources.list)->ptr[0] \
72 : (ref)->sources.ptr)
73
74
75 ZEND_API void ZEND_FASTCALL zend_ref_add_type_source(zend_property_info_source_list *source_list, zend_property_info *prop);
76 ZEND_API void ZEND_FASTCALL zend_ref_del_type_source(zend_property_info_source_list *source_list, zend_property_info *prop);
77
78 ZEND_API zval* zend_assign_to_typed_ref(zval *variable_ptr, zval *value, zend_uchar value_type, zend_bool strict, zend_refcounted *ref);
79
zend_copy_to_variable(zval * variable_ptr,zval * value,zend_uchar value_type,zend_refcounted * ref)80 static zend_always_inline void zend_copy_to_variable(zval *variable_ptr, zval *value, zend_uchar value_type, zend_refcounted *ref)
81 {
82 ZVAL_COPY_VALUE(variable_ptr, value);
83 if (ZEND_CONST_COND(value_type == IS_CONST, 0)) {
84 if (UNEXPECTED(Z_OPT_REFCOUNTED_P(variable_ptr))) {
85 Z_ADDREF_P(variable_ptr);
86 }
87 } else if (value_type & (IS_CONST|IS_CV)) {
88 if (Z_OPT_REFCOUNTED_P(variable_ptr)) {
89 Z_ADDREF_P(variable_ptr);
90 }
91 } else if (ZEND_CONST_COND(value_type == IS_VAR, 1) && UNEXPECTED(ref)) {
92 if (UNEXPECTED(GC_DELREF(ref) == 0)) {
93 efree_size(ref, sizeof(zend_reference));
94 } else if (Z_OPT_REFCOUNTED_P(variable_ptr)) {
95 Z_ADDREF_P(variable_ptr);
96 }
97 }
98 }
99
zend_assign_to_variable(zval * variable_ptr,zval * value,zend_uchar value_type,zend_bool strict)100 static zend_always_inline zval* zend_assign_to_variable(zval *variable_ptr, zval *value, zend_uchar value_type, zend_bool strict)
101 {
102 zend_refcounted *ref = NULL;
103
104 if (ZEND_CONST_COND(value_type & (IS_VAR|IS_CV), 1) && Z_ISREF_P(value)) {
105 ref = Z_COUNTED_P(value);
106 value = Z_REFVAL_P(value);
107 }
108
109 do {
110 if (UNEXPECTED(Z_REFCOUNTED_P(variable_ptr))) {
111 zend_refcounted *garbage;
112
113 if (Z_ISREF_P(variable_ptr)) {
114 if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(variable_ptr)))) {
115 return zend_assign_to_typed_ref(variable_ptr, value, value_type, strict, ref);
116 }
117
118 variable_ptr = Z_REFVAL_P(variable_ptr);
119 if (EXPECTED(!Z_REFCOUNTED_P(variable_ptr))) {
120 break;
121 }
122 }
123 if (Z_TYPE_P(variable_ptr) == IS_OBJECT &&
124 UNEXPECTED(Z_OBJ_HANDLER_P(variable_ptr, set) != NULL)) {
125 Z_OBJ_HANDLER_P(variable_ptr, set)(variable_ptr, value);
126 return variable_ptr;
127 }
128 garbage = Z_COUNTED_P(variable_ptr);
129 zend_copy_to_variable(variable_ptr, value, value_type, ref);
130 if (GC_DELREF(garbage) == 0) {
131 rc_dtor_func(garbage);
132 } else { /* we need to split */
133 /* optimized version of GC_ZVAL_CHECK_POSSIBLE_ROOT(variable_ptr) */
134 if (UNEXPECTED(GC_MAY_LEAK(garbage))) {
135 gc_possible_root(garbage);
136 }
137 }
138 return variable_ptr;
139 }
140 } while (0);
141
142 zend_copy_to_variable(variable_ptr, value, value_type, ref);
143 return variable_ptr;
144 }
145
146 ZEND_API int zval_update_constant(zval *pp);
147 ZEND_API int zval_update_constant_ex(zval *pp, zend_class_entry *scope);
148 ZEND_API ZEND_COLD int zend_use_undefined_constant(zend_string *name, zend_ast_attr attr, zval *result);
149
150 /* dedicated Zend executor functions - do not use! */
151 struct _zend_vm_stack {
152 zval *top;
153 zval *end;
154 zend_vm_stack prev;
155 };
156
157 #define ZEND_VM_STACK_HEADER_SLOTS \
158 ((ZEND_MM_ALIGNED_SIZE(sizeof(struct _zend_vm_stack)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval)) - 1) / ZEND_MM_ALIGNED_SIZE(sizeof(zval)))
159
160 #define ZEND_VM_STACK_ELEMENTS(stack) \
161 (((zval*)(stack)) + ZEND_VM_STACK_HEADER_SLOTS)
162
163 /*
164 * In general in RELEASE build ZEND_ASSERT() must be zero-cost, but for some
165 * reason, GCC generated worse code, performing CSE on assertion code and the
166 * following "slow path" and moving memory read operatins from slow path into
167 * common header. This made a degradation for the fast path.
168 * The following "#if ZEND_DEBUG" eliminates it.
169 */
170 #if ZEND_DEBUG
171 # define ZEND_ASSERT_VM_STACK(stack) ZEND_ASSERT(stack->top > (zval *) stack && stack->end > (zval *) stack && stack->top <= stack->end)
172 # 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))
173 #else
174 # define ZEND_ASSERT_VM_STACK(stack)
175 # define ZEND_ASSERT_VM_STACK_GLOBAL
176 #endif
177
178 ZEND_API void zend_vm_stack_init(void);
179 ZEND_API void zend_vm_stack_init_ex(size_t page_size);
180 ZEND_API void zend_vm_stack_destroy(void);
181 ZEND_API void* zend_vm_stack_extend(size_t size);
182
zend_vm_init_call_frame(zend_execute_data * call,uint32_t call_info,zend_function * func,uint32_t num_args,void * object_or_called_scope)183 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, void *object_or_called_scope)
184 {
185 call->func = func;
186 Z_PTR(call->This) = object_or_called_scope;
187 ZEND_CALL_INFO(call) = call_info;
188 ZEND_CALL_NUM_ARGS(call) = num_args;
189 }
190
zend_vm_stack_push_call_frame_ex(uint32_t used_stack,uint32_t call_info,zend_function * func,uint32_t num_args,void * object_or_called_scope)191 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, void *object_or_called_scope)
192 {
193 zend_execute_data *call = (zend_execute_data*)EG(vm_stack_top);
194
195 ZEND_ASSERT_VM_STACK_GLOBAL;
196
197 if (UNEXPECTED(used_stack > (size_t)(((char*)EG(vm_stack_end)) - (char*)call))) {
198 call = (zend_execute_data*)zend_vm_stack_extend(used_stack);
199 ZEND_ASSERT_VM_STACK_GLOBAL;
200 zend_vm_init_call_frame(call, call_info | ZEND_CALL_ALLOCATED, func, num_args, object_or_called_scope);
201 return call;
202 } else {
203 EG(vm_stack_top) = (zval*)((char*)call + used_stack);
204 zend_vm_init_call_frame(call, call_info, func, num_args, object_or_called_scope);
205 return call;
206 }
207 }
208
zend_vm_calc_used_stack(uint32_t num_args,zend_function * func)209 static zend_always_inline uint32_t zend_vm_calc_used_stack(uint32_t num_args, zend_function *func)
210 {
211 uint32_t used_stack = ZEND_CALL_FRAME_SLOT + num_args;
212
213 if (EXPECTED(ZEND_USER_CODE(func->type))) {
214 used_stack += func->op_array.last_var + func->op_array.T - MIN(func->op_array.num_args, num_args);
215 }
216 return used_stack * sizeof(zval);
217 }
218
zend_vm_stack_push_call_frame(uint32_t call_info,zend_function * func,uint32_t num_args,void * object_or_called_scope)219 static zend_always_inline zend_execute_data *zend_vm_stack_push_call_frame(uint32_t call_info, zend_function *func, uint32_t num_args, void *object_or_called_scope)
220 {
221 uint32_t used_stack = zend_vm_calc_used_stack(num_args, func);
222
223 return zend_vm_stack_push_call_frame_ex(used_stack, call_info,
224 func, num_args, object_or_called_scope);
225 }
226
zend_vm_stack_free_extra_args_ex(uint32_t call_info,zend_execute_data * call)227 static zend_always_inline void zend_vm_stack_free_extra_args_ex(uint32_t call_info, zend_execute_data *call)
228 {
229 if (UNEXPECTED(call_info & ZEND_CALL_FREE_EXTRA_ARGS)) {
230 uint32_t count = ZEND_CALL_NUM_ARGS(call) - call->func->op_array.num_args;
231 zval *p = ZEND_CALL_VAR_NUM(call, call->func->op_array.last_var + call->func->op_array.T);
232 do {
233 if (Z_REFCOUNTED_P(p)) {
234 zend_refcounted *r = Z_COUNTED_P(p);
235 if (!GC_DELREF(r)) {
236 ZVAL_NULL(p);
237 rc_dtor_func(r);
238 } else {
239 gc_check_possible_root(r);
240 }
241 }
242 p++;
243 } while (--count);
244 }
245 }
246
zend_vm_stack_free_extra_args(zend_execute_data * call)247 static zend_always_inline void zend_vm_stack_free_extra_args(zend_execute_data *call)
248 {
249 zend_vm_stack_free_extra_args_ex(ZEND_CALL_INFO(call), call);
250 }
251
zend_vm_stack_free_args(zend_execute_data * call)252 static zend_always_inline void zend_vm_stack_free_args(zend_execute_data *call)
253 {
254 uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
255
256 if (EXPECTED(num_args > 0)) {
257 zval *p = ZEND_CALL_ARG(call, 1);
258
259 do {
260 if (Z_REFCOUNTED_P(p)) {
261 zend_refcounted *r = Z_COUNTED_P(p);
262 if (!GC_DELREF(r)) {
263 ZVAL_NULL(p);
264 rc_dtor_func(r);
265 }
266 }
267 p++;
268 } while (--num_args);
269 }
270 }
271
zend_vm_stack_free_call_frame_ex(uint32_t call_info,zend_execute_data * call)272 static zend_always_inline void zend_vm_stack_free_call_frame_ex(uint32_t call_info, zend_execute_data *call)
273 {
274 ZEND_ASSERT_VM_STACK_GLOBAL;
275
276 if (UNEXPECTED(call_info & ZEND_CALL_ALLOCATED)) {
277 zend_vm_stack p = EG(vm_stack);
278 zend_vm_stack prev = p->prev;
279
280 ZEND_ASSERT(call == (zend_execute_data*)ZEND_VM_STACK_ELEMENTS(EG(vm_stack)));
281 EG(vm_stack_top) = prev->top;
282 EG(vm_stack_end) = prev->end;
283 EG(vm_stack) = prev;
284 efree(p);
285 } else {
286 EG(vm_stack_top) = (zval*)call;
287 }
288
289 ZEND_ASSERT_VM_STACK_GLOBAL;
290 }
291
zend_vm_stack_free_call_frame(zend_execute_data * call)292 static zend_always_inline void zend_vm_stack_free_call_frame(zend_execute_data *call)
293 {
294 zend_vm_stack_free_call_frame_ex(ZEND_CALL_INFO(call), call);
295 }
296
297 /* services */
298 ZEND_API const char *get_active_class_name(const char **space);
299 ZEND_API const char *get_active_function_name(void);
300 ZEND_API const char *zend_get_executed_filename(void);
301 ZEND_API zend_string *zend_get_executed_filename_ex(void);
302 ZEND_API uint32_t zend_get_executed_lineno(void);
303 ZEND_API zend_class_entry *zend_get_executed_scope(void);
304 ZEND_API zend_bool zend_is_executing(void);
305
306 ZEND_API void zend_set_timeout(zend_long seconds, int reset_signals);
307 ZEND_API void zend_unset_timeout(void);
308 ZEND_API ZEND_NORETURN void ZEND_FASTCALL zend_timeout(int dummy);
309 ZEND_API zend_class_entry *zend_fetch_class(zend_string *class_name, int fetch_type);
310 ZEND_API zend_class_entry *zend_fetch_class_by_name(zend_string *class_name, zend_string *lcname, int fetch_type);
311
312 ZEND_API zend_function * ZEND_FASTCALL zend_fetch_function(zend_string *name);
313 ZEND_API zend_function * ZEND_FASTCALL zend_fetch_function_str(const char *name, size_t len);
314 ZEND_API void ZEND_FASTCALL zend_init_func_run_time_cache(zend_op_array *op_array);
315
316 ZEND_API void zend_fetch_dimension_const(zval *result, zval *container, zval *dim, int type);
317
318 ZEND_API zval* zend_get_compiled_variable_value(const zend_execute_data *execute_data_ptr, uint32_t var);
319
320 #define ZEND_USER_OPCODE_CONTINUE 0 /* execute next opcode */
321 #define ZEND_USER_OPCODE_RETURN 1 /* exit from executor (return from function) */
322 #define ZEND_USER_OPCODE_DISPATCH 2 /* call original opcode handler */
323 #define ZEND_USER_OPCODE_ENTER 3 /* enter into new op_array without recursion */
324 #define ZEND_USER_OPCODE_LEAVE 4 /* return to calling op_array within the same executor */
325
326 #define ZEND_USER_OPCODE_DISPATCH_TO 0x100 /* call original handler of returned opcode */
327
328 ZEND_API int zend_set_user_opcode_handler(zend_uchar opcode, user_opcode_handler_t handler);
329 ZEND_API user_opcode_handler_t zend_get_user_opcode_handler(zend_uchar opcode);
330
331 /* former zend_execute_locks.h */
332 typedef zval* zend_free_op;
333
334 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);
335
336 ZEND_API void zend_clean_and_cache_symbol_table(zend_array *symbol_table);
337 ZEND_API void zend_free_compiled_variables(zend_execute_data *execute_data);
338 ZEND_API void zend_cleanup_unfinished_execution(zend_execute_data *execute_data, uint32_t op_num, uint32_t catch_op_num);
339
340 #define CACHE_ADDR(num) \
341 ((void**)((char*)EX(run_time_cache) + (num)))
342
343 #define CACHED_PTR(num) \
344 ((void**)((char*)EX(run_time_cache) + (num)))[0]
345
346 #define CACHE_PTR(num, ptr) do { \
347 ((void**)((char*)EX(run_time_cache) + (num)))[0] = (ptr); \
348 } while (0)
349
350 #define CACHED_POLYMORPHIC_PTR(num, ce) \
351 (EXPECTED(((void**)((char*)EX(run_time_cache) + (num)))[0] == (void*)(ce)) ? \
352 ((void**)((char*)EX(run_time_cache) + (num)))[1] : \
353 NULL)
354
355 #define CACHE_POLYMORPHIC_PTR(num, ce, ptr) do { \
356 void **slot = (void**)((char*)EX(run_time_cache) + (num)); \
357 slot[0] = (ce); \
358 slot[1] = (ptr); \
359 } while (0)
360
361 #define CACHED_PTR_EX(slot) \
362 (slot)[0]
363
364 #define CACHE_PTR_EX(slot, ptr) do { \
365 (slot)[0] = (ptr); \
366 } while (0)
367
368 #define CACHED_POLYMORPHIC_PTR_EX(slot, ce) \
369 (EXPECTED((slot)[0] == (ce)) ? (slot)[1] : NULL)
370
371 #define CACHE_POLYMORPHIC_PTR_EX(slot, ce, ptr) do { \
372 (slot)[0] = (ce); \
373 (slot)[1] = (ptr); \
374 } while (0)
375
376 #define CACHE_SPECIAL (1<<0)
377
378 #define IS_SPECIAL_CACHE_VAL(ptr) \
379 (((uintptr_t)(ptr)) & CACHE_SPECIAL)
380
381 #define ENCODE_SPECIAL_CACHE_NUM(num) \
382 ((void*)((((uintptr_t)(num)) << 1) | CACHE_SPECIAL))
383
384 #define DECODE_SPECIAL_CACHE_NUM(ptr) \
385 (((uintptr_t)(ptr)) >> 1)
386
387 #define ENCODE_SPECIAL_CACHE_PTR(ptr) \
388 ((void*)(((uintptr_t)(ptr)) | CACHE_SPECIAL))
389
390 #define DECODE_SPECIAL_CACHE_PTR(ptr) \
391 ((void*)(((uintptr_t)(ptr)) & ~CACHE_SPECIAL))
392
393 #define SKIP_EXT_OPLINE(opline) do { \
394 while (UNEXPECTED((opline)->opcode >= ZEND_EXT_STMT \
395 && (opline)->opcode <= ZEND_TICKS)) { \
396 (opline)--; \
397 } \
398 } while (0)
399
400 #define ZEND_CLASS_HAS_TYPE_HINTS(ce) ((ce->ce_flags & ZEND_ACC_HAS_TYPE_HINTS) == ZEND_ACC_HAS_TYPE_HINTS)
401
402 zend_bool zend_verify_property_type(zend_property_info *info, zval *property, zend_bool strict);
403 ZEND_COLD void zend_verify_property_type_error(zend_property_info *info, zval *property);
404
405 #define ZEND_REF_ADD_TYPE_SOURCE(ref, source) \
406 zend_ref_add_type_source(&ZEND_REF_TYPE_SOURCES(ref), source)
407
408 #define ZEND_REF_DEL_TYPE_SOURCE(ref, source) \
409 zend_ref_del_type_source(&ZEND_REF_TYPE_SOURCES(ref), source)
410
411 #define ZEND_REF_FOREACH_TYPE_SOURCES(ref, prop) do { \
412 zend_property_info_source_list *_source_list = &ZEND_REF_TYPE_SOURCES(ref); \
413 zend_property_info **_prop, **_end; \
414 zend_property_info_list *_list; \
415 if (_source_list->ptr) { \
416 if (ZEND_PROPERTY_INFO_SOURCE_IS_LIST(_source_list->list)) { \
417 _list = ZEND_PROPERTY_INFO_SOURCE_TO_LIST(_source_list->list); \
418 _prop = _list->ptr; \
419 _end = _list->ptr + _list->num; \
420 } else { \
421 _prop = &_source_list->ptr; \
422 _end = _prop + 1; \
423 } \
424 for (; _prop < _end; _prop++) { \
425 prop = *_prop; \
426
427 #define ZEND_REF_FOREACH_TYPE_SOURCES_END() \
428 } \
429 } \
430 } while (0)
431
432
433 END_EXTERN_C()
434
435 #endif /* ZEND_EXECUTE_H */
436