zend_execute.h 15 KB

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