zend_execute.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2016 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. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  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. typedef union _temp_variable {
  27. zval tmp_var;
  28. struct {
  29. zval **ptr_ptr;
  30. zval *ptr;
  31. zend_bool fcall_returned_reference;
  32. } var;
  33. struct {
  34. zval **ptr_ptr; /* shared with var.ptr_ptr */
  35. zval *str;
  36. zend_uint offset;
  37. } str_offset;
  38. struct {
  39. zval **ptr_ptr; /* shared with var.ptr_ptr */
  40. zval *ptr; /* shared with var.ptr */
  41. HashPointer fe_pos;
  42. } fe;
  43. zend_class_entry *class_entry;
  44. } temp_variable;
  45. BEGIN_EXTERN_C()
  46. struct _zend_fcall_info;
  47. ZEND_API extern void (*zend_execute_ex)(zend_execute_data *execute_data TSRMLS_DC);
  48. ZEND_API extern void (*zend_execute_internal)(zend_execute_data *execute_data_ptr, struct _zend_fcall_info *fci, int return_value_used TSRMLS_DC);
  49. void init_executor(TSRMLS_D);
  50. void shutdown_executor(TSRMLS_D);
  51. void shutdown_destructors(TSRMLS_D);
  52. ZEND_API zend_execute_data *zend_create_execute_data_from_op_array(zend_op_array *op_array, zend_bool nested TSRMLS_DC);
  53. ZEND_API void zend_execute(zend_op_array *op_array TSRMLS_DC);
  54. ZEND_API void execute_ex(zend_execute_data *execute_data TSRMLS_DC);
  55. ZEND_API void execute_internal(zend_execute_data *execute_data_ptr, struct _zend_fcall_info *fci, int return_value_used TSRMLS_DC);
  56. ZEND_API int zend_is_true(zval *op);
  57. ZEND_API int zend_lookup_class(const char *name, int name_length, zend_class_entry ***ce TSRMLS_DC);
  58. ZEND_API int zend_lookup_class_ex(const char *name, int name_length, const zend_literal *key, int use_autoload, zend_class_entry ***ce TSRMLS_DC);
  59. ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSRMLS_DC);
  60. ZEND_API int zend_eval_stringl(char *str, int str_len, zval *retval_ptr, char *string_name TSRMLS_DC);
  61. ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC);
  62. ZEND_API int zend_eval_stringl_ex(char *str, int str_len, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC);
  63. ZEND_API char * zend_verify_arg_class_kind(const zend_arg_info *cur_arg_info, ulong fetch_type, const char **class_name, zend_class_entry **pce TSRMLS_DC);
  64. ZEND_API int zend_verify_arg_error(int error_type, const zend_function *zf, zend_uint arg_num, const char *need_msg, const char *need_kind, const char *given_msg, const char *given_kind TSRMLS_DC);
  65. static zend_always_inline void i_zval_ptr_dtor(zval *zval_ptr ZEND_FILE_LINE_DC TSRMLS_DC)
  66. {
  67. if (!Z_DELREF_P(zval_ptr)) {
  68. ZEND_ASSERT(zval_ptr != &EG(uninitialized_zval));
  69. GC_REMOVE_ZVAL_FROM_BUFFER(zval_ptr);
  70. zval_dtor(zval_ptr);
  71. efree_rel(zval_ptr);
  72. } else {
  73. if (Z_REFCOUNT_P(zval_ptr) == 1) {
  74. Z_UNSET_ISREF_P(zval_ptr);
  75. }
  76. GC_ZVAL_CHECK_POSSIBLE_ROOT(zval_ptr);
  77. }
  78. }
  79. static zend_always_inline void i_zval_ptr_dtor_nogc(zval *zval_ptr ZEND_FILE_LINE_DC TSRMLS_DC)
  80. {
  81. if (!Z_DELREF_P(zval_ptr)) {
  82. ZEND_ASSERT(zval_ptr != &EG(uninitialized_zval));
  83. GC_REMOVE_ZVAL_FROM_BUFFER(zval_ptr);
  84. zval_dtor(zval_ptr);
  85. efree_rel(zval_ptr);
  86. } else {
  87. if (Z_REFCOUNT_P(zval_ptr) == 1) {
  88. Z_UNSET_ISREF_P(zval_ptr);
  89. }
  90. }
  91. }
  92. static zend_always_inline int i_zend_is_true(zval *op)
  93. {
  94. int result;
  95. switch (Z_TYPE_P(op)) {
  96. case IS_NULL:
  97. result = 0;
  98. break;
  99. case IS_LONG:
  100. case IS_BOOL:
  101. case IS_RESOURCE:
  102. result = (Z_LVAL_P(op)?1:0);
  103. break;
  104. case IS_DOUBLE:
  105. result = (Z_DVAL_P(op) ? 1 : 0);
  106. break;
  107. case IS_STRING:
  108. if (Z_STRLEN_P(op) == 0
  109. || (Z_STRLEN_P(op)==1 && Z_STRVAL_P(op)[0]=='0')) {
  110. result = 0;
  111. } else {
  112. result = 1;
  113. }
  114. break;
  115. case IS_ARRAY:
  116. result = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
  117. break;
  118. case IS_OBJECT:
  119. if(IS_ZEND_STD_OBJECT(*op)) {
  120. TSRMLS_FETCH();
  121. if (Z_OBJ_HT_P(op)->cast_object) {
  122. zval tmp;
  123. if (Z_OBJ_HT_P(op)->cast_object(op, &tmp, IS_BOOL TSRMLS_CC) == SUCCESS) {
  124. result = Z_LVAL(tmp);
  125. break;
  126. }
  127. } else if (Z_OBJ_HT_P(op)->get) {
  128. zval *tmp = Z_OBJ_HT_P(op)->get(op TSRMLS_CC);
  129. if(Z_TYPE_P(tmp) != IS_OBJECT) {
  130. /* for safety - avoid loop */
  131. convert_to_boolean(tmp);
  132. result = Z_LVAL_P(tmp);
  133. zval_ptr_dtor(&tmp);
  134. break;
  135. }
  136. }
  137. }
  138. result = 1;
  139. break;
  140. default:
  141. result = 0;
  142. break;
  143. }
  144. return result;
  145. }
  146. ZEND_API int zval_update_constant(zval **pp, zend_bool inline_change TSRMLS_DC);
  147. ZEND_API int zval_update_constant_inline_change(zval **pp, zend_class_entry *scope TSRMLS_DC);
  148. ZEND_API int zval_update_constant_no_inline_change(zval **pp, zend_class_entry *scope TSRMLS_DC);
  149. ZEND_API int zval_update_constant_ex(zval **pp, zend_bool inline_change, zend_class_entry *scope TSRMLS_DC);
  150. /* dedicated Zend executor functions - do not use! */
  151. #define ZEND_VM_STACK_PAGE_SIZE ((16 * 1024) - 16)
  152. struct _zend_vm_stack {
  153. void **top;
  154. void **end;
  155. zend_vm_stack prev;
  156. };
  157. #define ZEND_VM_STACK_ELEMETS(stack) \
  158. ((void**)(((char*)(stack)) + ZEND_MM_ALIGNED_SIZE(sizeof(struct _zend_vm_stack))))
  159. #define ZEND_VM_STACK_GROW_IF_NEEDED(count) \
  160. do { \
  161. if (UNEXPECTED((count) > \
  162. EG(argument_stack)->end - EG(argument_stack)->top)) { \
  163. zend_vm_stack_extend((count) TSRMLS_CC); \
  164. } \
  165. } while (0)
  166. static zend_always_inline zend_vm_stack zend_vm_stack_new_page(int count) {
  167. zend_vm_stack page = (zend_vm_stack)emalloc(ZEND_MM_ALIGNED_SIZE(sizeof(*page)) + sizeof(void*) * count);
  168. page->top = ZEND_VM_STACK_ELEMETS(page);
  169. page->end = page->top + count;
  170. page->prev = NULL;
  171. return page;
  172. }
  173. static zend_always_inline void zend_vm_stack_init(TSRMLS_D)
  174. {
  175. EG(argument_stack) = zend_vm_stack_new_page(ZEND_VM_STACK_PAGE_SIZE);
  176. }
  177. static zend_always_inline void zend_vm_stack_destroy(TSRMLS_D)
  178. {
  179. zend_vm_stack stack = EG(argument_stack);
  180. while (stack != NULL) {
  181. zend_vm_stack p = stack->prev;
  182. efree(stack);
  183. stack = p;
  184. }
  185. }
  186. static zend_always_inline void zend_vm_stack_extend(int count TSRMLS_DC)
  187. {
  188. zend_vm_stack p = zend_vm_stack_new_page(count >= ZEND_VM_STACK_PAGE_SIZE ? count : ZEND_VM_STACK_PAGE_SIZE);
  189. p->prev = EG(argument_stack);
  190. EG(argument_stack) = p;
  191. }
  192. static zend_always_inline void **zend_vm_stack_top(TSRMLS_D)
  193. {
  194. return EG(argument_stack)->top;
  195. }
  196. static zend_always_inline void zend_vm_stack_push(void *ptr TSRMLS_DC)
  197. {
  198. *(EG(argument_stack)->top++) = ptr;
  199. }
  200. static zend_always_inline void *zend_vm_stack_pop(TSRMLS_D)
  201. {
  202. void *el = *(--EG(argument_stack)->top);
  203. return el;
  204. }
  205. static zend_always_inline void *zend_vm_stack_alloc(size_t size TSRMLS_DC)
  206. {
  207. void *ret;
  208. size = (size + (sizeof(void*) - 1)) / sizeof(void*);
  209. /* the following comparison must be optimized out at compile time */
  210. if (ZEND_MM_ALIGNMENT > sizeof(void*)) {
  211. int extra = (ZEND_MM_ALIGNMENT - ((zend_uintptr_t)EG(argument_stack)->top & (ZEND_MM_ALIGNMENT - 1))) / sizeof(void*);
  212. if (UNEXPECTED(size + extra + ZEND_MM_ALIGNED_SIZE(sizeof(void*)) / sizeof(void*) >
  213. (zend_uintptr_t)(EG(argument_stack)->end - EG(argument_stack)->top))) {
  214. zend_vm_stack_extend(size TSRMLS_CC);
  215. } else {
  216. void **old_top = EG(argument_stack)->top;
  217. EG(argument_stack)->top += extra;
  218. /* store old top on the stack */
  219. *EG(argument_stack)->top = (void*)old_top;
  220. EG(argument_stack)->top += ZEND_MM_ALIGNED_SIZE(sizeof(void*)) / sizeof(void*);
  221. }
  222. } else {
  223. ZEND_VM_STACK_GROW_IF_NEEDED((int)size);
  224. }
  225. ret = (void*)EG(argument_stack)->top;
  226. EG(argument_stack)->top += size;
  227. return ret;
  228. }
  229. static zend_always_inline void** zend_vm_stack_frame_base(zend_execute_data *ex)
  230. {
  231. return (void**)((char*)ex->call_slots +
  232. ZEND_MM_ALIGNED_SIZE(sizeof(call_slot)) * ex->op_array->nested_calls);
  233. }
  234. static zend_always_inline void zend_vm_stack_free_int(void *ptr TSRMLS_DC)
  235. {
  236. if (UNEXPECTED(ZEND_VM_STACK_ELEMETS(EG(argument_stack)) == (void**)ptr)) {
  237. zend_vm_stack p = EG(argument_stack);
  238. EG(argument_stack) = p->prev;
  239. efree(p);
  240. } else {
  241. EG(argument_stack)->top = (void**)ptr;
  242. }
  243. }
  244. static zend_always_inline void zend_vm_stack_free(void *ptr TSRMLS_DC)
  245. {
  246. if (UNEXPECTED(ZEND_VM_STACK_ELEMETS(EG(argument_stack)) == (void**)ptr)) {
  247. zend_vm_stack p = EG(argument_stack);
  248. EG(argument_stack) = p->prev;
  249. efree(p);
  250. } else {
  251. /* the following comparison must be optimized out at compile time */
  252. if (ZEND_MM_ALIGNMENT > sizeof(void*)) {
  253. ptr = (void*)(((char*)ptr) - ZEND_MM_ALIGNED_SIZE(sizeof(void*)));
  254. EG(argument_stack)->top = *(void***)ptr;
  255. } else {
  256. EG(argument_stack)->top = (void**)ptr;
  257. }
  258. }
  259. }
  260. static zend_always_inline void zend_vm_stack_clear_multiple(int nested TSRMLS_DC)
  261. {
  262. void **p = EG(argument_stack)->top - 1;
  263. void **end = p - (int)(zend_uintptr_t)*p;
  264. while (p != end) {
  265. zval *q = (zval *) *(--p);
  266. *p = NULL;
  267. i_zval_ptr_dtor(q ZEND_FILE_LINE_CC TSRMLS_CC);
  268. }
  269. if (nested) {
  270. EG(argument_stack)->top = p;
  271. } else {
  272. zend_vm_stack_free_int(p TSRMLS_CC);
  273. }
  274. }
  275. static zend_always_inline int zend_vm_stack_get_args_count_ex(zend_execute_data *ex)
  276. {
  277. if (ex) {
  278. void **p = ex->function_state.arguments;
  279. return (int)(zend_uintptr_t) *p;
  280. } else {
  281. return 0;
  282. }
  283. }
  284. static zend_always_inline zval** zend_vm_stack_get_arg_ex(zend_execute_data *ex, int requested_arg)
  285. {
  286. void **p = ex->function_state.arguments;
  287. int arg_count = (int)(zend_uintptr_t) *p;
  288. if (UNEXPECTED(requested_arg > arg_count)) {
  289. return NULL;
  290. }
  291. return (zval**)p - arg_count + requested_arg - 1;
  292. }
  293. static zend_always_inline int zend_vm_stack_get_args_count(TSRMLS_D)
  294. {
  295. return zend_vm_stack_get_args_count_ex(EG(current_execute_data)->prev_execute_data);
  296. }
  297. static zend_always_inline zval** zend_vm_stack_get_arg(int requested_arg TSRMLS_DC)
  298. {
  299. return zend_vm_stack_get_arg_ex(EG(current_execute_data)->prev_execute_data, requested_arg);
  300. }
  301. void execute_new_code(TSRMLS_D);
  302. /* services */
  303. ZEND_API const char *get_active_class_name(const char **space TSRMLS_DC);
  304. ZEND_API const char *get_active_function_name(TSRMLS_D);
  305. ZEND_API const char *zend_get_executed_filename(TSRMLS_D);
  306. ZEND_API uint zend_get_executed_lineno(TSRMLS_D);
  307. ZEND_API zend_bool zend_is_executing(TSRMLS_D);
  308. ZEND_API void zend_set_timeout(long seconds, int reset_signals);
  309. ZEND_API void zend_unset_timeout(TSRMLS_D);
  310. ZEND_API void zend_timeout(int dummy);
  311. ZEND_API zend_class_entry *zend_fetch_class(const char *class_name, uint class_name_len, int fetch_type TSRMLS_DC);
  312. ZEND_API zend_class_entry *zend_fetch_class_by_name(const char *class_name, uint class_name_len, const zend_literal *key, int fetch_type TSRMLS_DC);
  313. void zend_verify_abstract_class(zend_class_entry *ce TSRMLS_DC);
  314. ZEND_API void zend_fetch_dimension_by_zval(zval **result, zval *container, zval *dim TSRMLS_DC);
  315. #define zendi_zval_copy_ctor(p) zval_copy_ctor(&(p))
  316. #define zendi_zval_dtor(p) zval_dtor(&(p))
  317. #define active_opline (*EG(opline_ptr))
  318. /* The following tries to resolve the classname of a zval of type object.
  319. * Since it is slow it should be only used in error messages.
  320. */
  321. #define Z_OBJ_CLASS_NAME_P(zval) ((zval) && Z_TYPE_P(zval) == IS_OBJECT && Z_OBJ_HT_P(zval)->get_class_entry != NULL && Z_OBJ_HT_P(zval)->get_class_entry(zval TSRMLS_CC) ? Z_OBJ_HT_P(zval)->get_class_entry(zval TSRMLS_CC)->name : "")
  322. ZEND_API zval** zend_get_compiled_variable_value(const zend_execute_data *execute_data_ptr, zend_uint var);
  323. #define ZEND_USER_OPCODE_CONTINUE 0 /* execute next opcode */
  324. #define ZEND_USER_OPCODE_RETURN 1 /* exit from executor (return from function) */
  325. #define ZEND_USER_OPCODE_DISPATCH 2 /* call original opcode handler */
  326. #define ZEND_USER_OPCODE_ENTER 3 /* enter into new op_array without recursion */
  327. #define ZEND_USER_OPCODE_LEAVE 4 /* return to calling op_array within the same executor */
  328. #define ZEND_USER_OPCODE_DISPATCH_TO 0x100 /* call original handler of returned opcode */
  329. ZEND_API int zend_set_user_opcode_handler(zend_uchar opcode, user_opcode_handler_t handler);
  330. ZEND_API user_opcode_handler_t zend_get_user_opcode_handler(zend_uchar opcode);
  331. /* former zend_execute_locks.h */
  332. typedef struct _zend_free_op {
  333. zval* var;
  334. /* int is_var; */
  335. } zend_free_op;
  336. 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 TSRMLS_DC);
  337. ZEND_API zval **zend_get_zval_ptr_ptr(int op_type, const znode_op *node, const zend_execute_data *execute_data, zend_free_op *should_free, int type TSRMLS_DC);
  338. ZEND_API int zend_do_fcall(ZEND_OPCODE_HANDLER_ARGS);
  339. void zend_clean_and_cache_symbol_table(HashTable *symbol_table TSRMLS_DC);
  340. void zend_free_compiled_variables(zend_execute_data *execute_data TSRMLS_DC);
  341. #define CACHED_PTR(num) \
  342. EG(active_op_array)->run_time_cache[(num)]
  343. #define CACHE_PTR(num, ptr) do { \
  344. EG(active_op_array)->run_time_cache[(num)] = (ptr); \
  345. } while (0)
  346. #define CACHED_POLYMORPHIC_PTR(num, ce) \
  347. ((EG(active_op_array)->run_time_cache[(num)] == (ce)) ? \
  348. EG(active_op_array)->run_time_cache[(num) + 1] : \
  349. NULL)
  350. #define CACHE_POLYMORPHIC_PTR(num, ce, ptr) do { \
  351. EG(active_op_array)->run_time_cache[(num)] = (ce); \
  352. EG(active_op_array)->run_time_cache[(num) + 1] = (ptr); \
  353. } while (0)
  354. END_EXTERN_C()
  355. #endif /* ZEND_EXECUTE_H */
  356. /*
  357. * Local variables:
  358. * tab-width: 4
  359. * c-basic-offset: 4
  360. * indent-tabs-mode: t
  361. * End:
  362. */