You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1825 lines
56 KiB

27 years ago
12 years ago
27 years ago
24 years ago
24 years ago
27 years ago
27 years ago
27 years ago
27 years ago
16 years ago
27 years ago
27 years ago
27 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
12 years ago
12 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2014 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. #define ZEND_INTENSIVE_DEBUGGING 0
  21. #include <stdio.h>
  22. #include <signal.h>
  23. #include "zend.h"
  24. #include "zend_compile.h"
  25. #include "zend_execute.h"
  26. #include "zend_API.h"
  27. #include "zend_ptr_stack.h"
  28. #include "zend_constants.h"
  29. #include "zend_extensions.h"
  30. #include "zend_ini.h"
  31. #include "zend_exceptions.h"
  32. #include "zend_interfaces.h"
  33. #include "zend_closures.h"
  34. #include "zend_generators.h"
  35. #include "zend_vm.h"
  36. #include "zend_dtrace.h"
  37. #include "zend_inheritance.h"
  38. /* Virtual current working directory support */
  39. #include "zend_virtual_cwd.h"
  40. #define _CONST_CODE 0
  41. #define _TMP_CODE 1
  42. #define _VAR_CODE 2
  43. #define _UNUSED_CODE 3
  44. #define _CV_CODE 4
  45. typedef int (*incdec_t)(zval *);
  46. #define get_zval_ptr(op_type, node, ex, should_free, type) _get_zval_ptr(op_type, node, ex, should_free, type TSRMLS_CC)
  47. #define get_zval_ptr_deref(op_type, node, ex, should_free, type) _get_zval_ptr_deref(op_type, node, ex, should_free, type TSRMLS_CC)
  48. #define get_zval_ptr_ptr(op_type, node, ex, should_free, type) _get_zval_ptr_ptr(op_type, node, ex, should_free, type TSRMLS_CC)
  49. #define get_zval_ptr_ptr_undef(op_type, node, ex, should_free, type) _get_zval_ptr_ptr(op_type, node, ex, should_free, type TSRMLS_CC)
  50. #define get_obj_zval_ptr(op_type, node, ex, should_free, type) _get_obj_zval_ptr(op_type, node, ex, should_free, type TSRMLS_CC)
  51. #define get_obj_zval_ptr_ptr(op_type, node, ex, should_free, type) _get_obj_zval_ptr_ptr(op_type, node, ex, should_free, type TSRMLS_CC)
  52. /* Prototypes */
  53. static void zend_extension_statement_handler(const zend_extension *extension, zend_op_array *op_array TSRMLS_DC);
  54. static void zend_extension_fcall_begin_handler(const zend_extension *extension, zend_op_array *op_array TSRMLS_DC);
  55. static void zend_extension_fcall_end_handler(const zend_extension *extension, zend_op_array *op_array TSRMLS_DC);
  56. #define RETURN_VALUE_USED(opline) (!((opline)->result_type & EXT_TYPE_UNUSED))
  57. static ZEND_FUNCTION(pass)
  58. {
  59. }
  60. static const zend_internal_function zend_pass_function = {
  61. ZEND_INTERNAL_FUNCTION, /* type */
  62. 0, /* fn_flags */
  63. NULL, /* name */
  64. NULL, /* scope */
  65. NULL, /* prototype */
  66. 0, /* num_args */
  67. 0, /* required_num_args */
  68. NULL, /* arg_info */
  69. ZEND_FN(pass), /* handler */
  70. NULL /* module */
  71. };
  72. #undef zval_ptr_dtor
  73. #define zval_ptr_dtor(zv) i_zval_ptr_dtor(zv ZEND_FILE_LINE_CC TSRMLS_CC)
  74. #define PZVAL_LOCK(z) if (Z_REFCOUNTED_P(z)) Z_ADDREF_P((z))
  75. #define SELECTIVE_PZVAL_LOCK(pzv, opline) if (RETURN_VALUE_USED(opline)) { PZVAL_LOCK(pzv); }
  76. #define READY_TO_DESTROY(zv) \
  77. (zv && Z_REFCOUNTED_P(zv) && Z_REFCOUNT_P(zv) == 1)
  78. #define EXTRACT_ZVAL_PTR(zv) do { \
  79. zval *__zv = (zv); \
  80. if (Z_TYPE_P(__zv) == IS_INDIRECT) { \
  81. ZVAL_COPY(__zv, Z_INDIRECT_P(__zv)); \
  82. } \
  83. } while (0)
  84. #define FREE_OP(should_free) \
  85. if (should_free.var) { \
  86. zval_ptr_dtor_nogc(should_free.var); \
  87. }
  88. #define FREE_OP_VAR_PTR(should_free) \
  89. if (should_free.var) { \
  90. zval_ptr_dtor_nogc(should_free.var); \
  91. }
  92. /* End of zend_execute_locks.h */
  93. #define CV_DEF_OF(i) (EX(func)->op_array.vars[i])
  94. #define CTOR_CALL_BIT 0x1
  95. #define CTOR_USED_BIT 0x2
  96. #define IS_CTOR_CALL(ce) (((zend_uintptr_t)(ce)) & CTOR_CALL_BIT)
  97. #define IS_CTOR_USED(ce) (((zend_uintptr_t)(ce)) & CTOR_USED_BIT)
  98. #define ENCODE_CTOR(ce, used) \
  99. ((zend_class_entry*)(((zend_uintptr_t)(ce)) | CTOR_CALL_BIT | ((used) ? CTOR_USED_BIT : 0)))
  100. #define DECODE_CTOR(ce) \
  101. ((zend_class_entry*)(((zend_uintptr_t)(ce)) & ~(CTOR_CALL_BIT|CTOR_USED_BIT)))
  102. #define ZEND_VM_STACK_PAGE_SLOTS (16 * 1024) /* should be a power of 2 */
  103. #define ZEND_VM_STACK_PAGE_SIZE (ZEND_VM_STACK_PAGE_SLOTS * sizeof(zval))
  104. #define ZEND_VM_STACK_FREE_PAGE_SIZE \
  105. ((ZEND_VM_STACK_PAGE_SLOTS - ZEND_VM_STACK_HEADER_SLOTS) * sizeof(zval))
  106. #define ZEND_VM_STACK_PAGE_ALIGNED_SIZE(size) \
  107. (((size) + (ZEND_VM_STACK_FREE_PAGE_SIZE - 1)) & ~ZEND_VM_STACK_PAGE_SIZE)
  108. static zend_always_inline zend_vm_stack zend_vm_stack_new_page(size_t size, zend_vm_stack prev) {
  109. zend_vm_stack page = (zend_vm_stack)emalloc(size);
  110. page->top = ZEND_VM_STACK_ELEMETS(page);
  111. page->end = (zval*)((char*)page + size);
  112. page->prev = prev;
  113. return page;
  114. }
  115. ZEND_API void zend_vm_stack_init(TSRMLS_D)
  116. {
  117. EG(vm_stack) = zend_vm_stack_new_page(ZEND_VM_STACK_PAGE_SIZE, NULL);
  118. EG(vm_stack)->top++;
  119. EG(vm_stack_top) = EG(vm_stack)->top;
  120. EG(vm_stack_end) = EG(vm_stack)->end;
  121. }
  122. ZEND_API void zend_vm_stack_destroy(TSRMLS_D)
  123. {
  124. zend_vm_stack stack = EG(vm_stack);
  125. while (stack != NULL) {
  126. zend_vm_stack p = stack->prev;
  127. efree(stack);
  128. stack = p;
  129. }
  130. }
  131. ZEND_API void* zend_vm_stack_extend(size_t size TSRMLS_DC)
  132. {
  133. zend_vm_stack stack;
  134. void *ptr;
  135. stack = EG(vm_stack);
  136. stack->top = EG(vm_stack_top);
  137. EG(vm_stack) = stack = zend_vm_stack_new_page(
  138. EXPECTED(size < ZEND_VM_STACK_FREE_PAGE_SIZE) ?
  139. ZEND_VM_STACK_PAGE_SIZE : ZEND_VM_STACK_PAGE_ALIGNED_SIZE(size),
  140. stack);
  141. ptr = stack->top;
  142. EG(vm_stack_top) = (void*)(((char*)ptr) + size);
  143. EG(vm_stack_end) = stack->end;
  144. return ptr;
  145. }
  146. ZEND_API zval* zend_get_compiled_variable_value(const zend_execute_data *execute_data, uint32_t var)
  147. {
  148. return EX_VAR(var);
  149. }
  150. static zend_always_inline zval *_get_zval_ptr_tmp(uint32_t var, const zend_execute_data *execute_data, zend_free_op *should_free TSRMLS_DC)
  151. {
  152. zval *ret = EX_VAR(var);
  153. should_free->var = ret;
  154. ZEND_ASSERT(Z_TYPE_P(ret) != IS_REFERENCE);
  155. return ret;
  156. }
  157. static zend_always_inline zval *_get_zval_ptr_var(uint32_t var, const zend_execute_data *execute_data, zend_free_op *should_free TSRMLS_DC)
  158. {
  159. zval *ret = EX_VAR(var);
  160. should_free->var = ret;
  161. return ret;
  162. }
  163. static zend_always_inline zval *_get_zval_ptr_var_deref(uint32_t var, const zend_execute_data *execute_data, zend_free_op *should_free TSRMLS_DC)
  164. {
  165. zval *ret = EX_VAR(var);
  166. should_free->var = ret;
  167. ZVAL_DEREF(ret);
  168. return ret;
  169. }
  170. static zend_never_inline zval *_get_zval_cv_lookup(zval *ptr, uint32_t var, int type, const zend_execute_data *execute_data TSRMLS_DC)
  171. {
  172. zend_string *cv;
  173. switch (type) {
  174. case BP_VAR_R:
  175. case BP_VAR_UNSET:
  176. cv = CV_DEF_OF(EX_VAR_TO_NUM(var));
  177. zend_error(E_NOTICE, "Undefined variable: %s", cv->val);
  178. /* break missing intentionally */
  179. case BP_VAR_IS:
  180. ptr = &EG(uninitialized_zval);
  181. break;
  182. case BP_VAR_RW:
  183. cv = CV_DEF_OF(EX_VAR_TO_NUM(var));
  184. zend_error(E_NOTICE, "Undefined variable: %s", cv->val);
  185. /* break missing intentionally */
  186. case BP_VAR_W:
  187. ZVAL_NULL(ptr);
  188. break;
  189. }
  190. return ptr;
  191. }
  192. static zend_always_inline zval *_get_zval_cv_lookup_BP_VAR_R(zval *ptr, uint32_t var, const zend_execute_data *execute_data TSRMLS_DC)
  193. {
  194. zend_string *cv = CV_DEF_OF(EX_VAR_TO_NUM(var));
  195. zend_error(E_NOTICE, "Undefined variable: %s", cv->val);
  196. return &EG(uninitialized_zval);
  197. }
  198. static zend_always_inline zval *_get_zval_cv_lookup_BP_VAR_UNSET(zval *ptr, uint32_t var, const zend_execute_data *execute_data TSRMLS_DC)
  199. {
  200. zend_string *cv = CV_DEF_OF(EX_VAR_TO_NUM(var));
  201. zend_error(E_NOTICE, "Undefined variable: %s", cv->val);
  202. return &EG(uninitialized_zval);
  203. }
  204. static zend_always_inline zval *_get_zval_cv_lookup_BP_VAR_RW(zval *ptr, uint32_t var, const zend_execute_data *execute_data TSRMLS_DC)
  205. {
  206. zend_string *cv = CV_DEF_OF(EX_VAR_TO_NUM(var));
  207. ZVAL_NULL(ptr);
  208. zend_error(E_NOTICE, "Undefined variable: %s", cv->val);
  209. return ptr;
  210. }
  211. static zend_always_inline zval *_get_zval_cv_lookup_BP_VAR_W(zval *ptr, uint32_t var, const zend_execute_data *execute_data TSRMLS_DC)
  212. {
  213. ZVAL_NULL(ptr);
  214. return ptr;
  215. }
  216. static zend_always_inline zval *_get_zval_ptr_cv(const zend_execute_data *execute_data, uint32_t var, int type TSRMLS_DC)
  217. {
  218. zval *ret = EX_VAR(var);
  219. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  220. return _get_zval_cv_lookup(ret, var, type, execute_data TSRMLS_CC);
  221. }
  222. return ret;
  223. }
  224. static zend_always_inline zval *_get_zval_ptr_cv_deref(const zend_execute_data *execute_data, uint32_t var, int type TSRMLS_DC)
  225. {
  226. zval *ret = EX_VAR(var);
  227. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  228. return _get_zval_cv_lookup(ret, var, type, execute_data TSRMLS_CC);
  229. }
  230. ZVAL_DEREF(ret);
  231. return ret;
  232. }
  233. static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_R(const zend_execute_data *execute_data, uint32_t var TSRMLS_DC)
  234. {
  235. zval *ret = EX_VAR(var);
  236. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  237. return _get_zval_cv_lookup_BP_VAR_R(ret, var, execute_data TSRMLS_CC);
  238. }
  239. return ret;
  240. }
  241. static zend_always_inline zval *_get_zval_ptr_cv_deref_BP_VAR_R(const zend_execute_data *execute_data, uint32_t var TSRMLS_DC)
  242. {
  243. zval *ret = EX_VAR(var);
  244. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  245. return _get_zval_cv_lookup_BP_VAR_R(ret, var, execute_data TSRMLS_CC);
  246. }
  247. ZVAL_DEREF(ret);
  248. return ret;
  249. }
  250. static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_UNSET(const zend_execute_data *execute_data, uint32_t var TSRMLS_DC)
  251. {
  252. zval *ret = EX_VAR(var);
  253. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  254. return _get_zval_cv_lookup_BP_VAR_UNSET(ret, var, execute_data TSRMLS_CC);
  255. }
  256. return ret;
  257. }
  258. static zend_always_inline zval *_get_zval_ptr_cv_deref_BP_VAR_UNSET(const zend_execute_data *execute_data, uint32_t var TSRMLS_DC)
  259. {
  260. zval *ret = EX_VAR(var);
  261. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  262. return _get_zval_cv_lookup_BP_VAR_UNSET(ret, var, execute_data TSRMLS_CC);
  263. }
  264. ZVAL_DEREF(ret);
  265. return ret;
  266. }
  267. static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_IS(const zend_execute_data *execute_data, uint32_t var TSRMLS_DC)
  268. {
  269. zval *ret = EX_VAR(var);
  270. return ret;
  271. }
  272. static zend_always_inline zval *_get_zval_ptr_cv_deref_BP_VAR_IS(const zend_execute_data *execute_data, uint32_t var TSRMLS_DC)
  273. {
  274. zval *ret = EX_VAR(var);
  275. ZVAL_DEREF(ret);
  276. return ret;
  277. }
  278. static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_RW(const zend_execute_data *execute_data, uint32_t var TSRMLS_DC)
  279. {
  280. zval *ret = EX_VAR(var);
  281. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  282. return _get_zval_cv_lookup_BP_VAR_RW(ret, var, execute_data TSRMLS_CC);
  283. }
  284. return ret;
  285. }
  286. static zend_always_inline zval *_get_zval_ptr_cv_deref_BP_VAR_RW(const zend_execute_data *execute_data, uint32_t var TSRMLS_DC)
  287. {
  288. zval *ret = EX_VAR(var);
  289. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  290. return _get_zval_cv_lookup_BP_VAR_RW(ret, var, execute_data TSRMLS_CC);
  291. }
  292. ZVAL_DEREF(ret);
  293. return ret;
  294. }
  295. static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_W(const zend_execute_data *execute_data, uint32_t var TSRMLS_DC)
  296. {
  297. zval *ret = EX_VAR(var);
  298. if (Z_TYPE_P(ret) == IS_UNDEF) {
  299. return _get_zval_cv_lookup_BP_VAR_W(ret, var, execute_data TSRMLS_CC);
  300. }
  301. return ret;
  302. }
  303. static zend_always_inline zval *_get_zval_ptr_cv_undef_BP_VAR_W(const zend_execute_data *execute_data, uint32_t var TSRMLS_DC)
  304. {
  305. return EX_VAR(var);
  306. }
  307. static zend_always_inline zval *_get_zval_ptr_cv_deref_BP_VAR_W(const zend_execute_data *execute_data, uint32_t var TSRMLS_DC)
  308. {
  309. zval *ret = EX_VAR(var);
  310. if (Z_TYPE_P(ret) == IS_UNDEF) {
  311. return _get_zval_cv_lookup_BP_VAR_W(ret, var, execute_data TSRMLS_CC);
  312. }
  313. ZVAL_DEREF(ret);
  314. return ret;
  315. }
  316. static inline zval *_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)
  317. {
  318. zval *ret;
  319. switch (op_type) {
  320. case IS_CONST:
  321. should_free->var = NULL;
  322. return node->zv;
  323. break;
  324. case IS_TMP_VAR:
  325. ret = EX_VAR(node->var);
  326. should_free->var = ret;
  327. return ret;
  328. break;
  329. case IS_VAR:
  330. return _get_zval_ptr_var(node->var, execute_data, should_free TSRMLS_CC);
  331. break;
  332. case IS_UNUSED:
  333. should_free->var = NULL;
  334. return NULL;
  335. break;
  336. case IS_CV:
  337. default:
  338. should_free->var = NULL;
  339. return _get_zval_ptr_cv(execute_data, node->var, type TSRMLS_CC);
  340. break;
  341. }
  342. return NULL;
  343. }
  344. static inline zval *_get_zval_ptr_deref(int op_type, const znode_op *node, const zend_execute_data *execute_data, zend_free_op *should_free, int type TSRMLS_DC)
  345. {
  346. zval *ret;
  347. switch (op_type) {
  348. case IS_CONST:
  349. should_free->var = NULL;
  350. return node->zv;
  351. break;
  352. case IS_TMP_VAR:
  353. ret = EX_VAR(node->var);
  354. should_free->var = ret;
  355. return ret;
  356. break;
  357. case IS_VAR:
  358. return _get_zval_ptr_var_deref(node->var, execute_data, should_free TSRMLS_CC);
  359. break;
  360. case IS_UNUSED:
  361. should_free->var = NULL;
  362. return NULL;
  363. break;
  364. case IS_CV:
  365. default:
  366. should_free->var = NULL;
  367. return _get_zval_ptr_cv_deref(execute_data, node->var, type TSRMLS_CC);
  368. break;
  369. }
  370. return NULL;
  371. }
  372. static zend_always_inline zval *_get_zval_ptr_ptr_var(uint32_t var, const zend_execute_data *execute_data, zend_free_op *should_free TSRMLS_DC)
  373. {
  374. zval *ret = EX_VAR(var);
  375. if (EXPECTED(Z_TYPE_P(ret) == IS_INDIRECT)) {
  376. should_free->var = NULL;
  377. return Z_INDIRECT_P(ret);
  378. } else if (!Z_REFCOUNTED_P(ret) || Z_REFCOUNT_P(ret) == 1) {
  379. should_free->var = ret;
  380. return ret;
  381. } else {
  382. Z_DELREF_P(ret);
  383. should_free->var = NULL;
  384. return ret;
  385. }
  386. }
  387. static inline zval *_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)
  388. {
  389. if (op_type == IS_CV) {
  390. should_free->var = NULL;
  391. return _get_zval_ptr_cv(execute_data, node->var, type TSRMLS_CC);
  392. } else /* if (op_type == IS_VAR) */ {
  393. ZEND_ASSERT(op_type == IS_VAR);
  394. return _get_zval_ptr_ptr_var(node->var, execute_data, should_free TSRMLS_CC);
  395. }
  396. }
  397. static zend_always_inline zval *_get_obj_zval_ptr_unused(zend_execute_data *execute_data TSRMLS_DC)
  398. {
  399. if (EXPECTED(Z_OBJ(EX(This)) != NULL)) {
  400. return &EX(This);
  401. } else {
  402. zend_error_noreturn(E_ERROR, "Using $this when not in object context");
  403. return NULL;
  404. }
  405. }
  406. static inline zval *_get_obj_zval_ptr(int op_type, znode_op *op, zend_execute_data *execute_data, zend_free_op *should_free, int type TSRMLS_DC)
  407. {
  408. if (op_type == IS_UNUSED) {
  409. if (EXPECTED(Z_OBJ(EX(This)) != NULL)) {
  410. should_free->var = NULL;
  411. return &EX(This);
  412. } else {
  413. zend_error_noreturn(E_ERROR, "Using $this when not in object context");
  414. }
  415. }
  416. return get_zval_ptr(op_type, op, execute_data, should_free, type);
  417. }
  418. static inline zval *_get_obj_zval_ptr_ptr(int op_type, const znode_op *node, zend_execute_data *execute_data, zend_free_op *should_free, int type TSRMLS_DC)
  419. {
  420. if (op_type == IS_UNUSED) {
  421. if (EXPECTED(Z_OBJ(EX(This)) != NULL)) {
  422. should_free->var = NULL;
  423. return &EX(This);
  424. } else {
  425. zend_error_noreturn(E_ERROR, "Using $this when not in object context");
  426. }
  427. }
  428. return get_zval_ptr_ptr(op_type, node, execute_data, should_free, type);
  429. }
  430. static inline void zend_assign_to_variable_reference(zval *variable_ptr, zval *value_ptr TSRMLS_DC)
  431. {
  432. if (EXPECTED(variable_ptr != value_ptr)) {
  433. zend_reference *ref;
  434. ZVAL_MAKE_REF(value_ptr);
  435. Z_ADDREF_P(value_ptr);
  436. ref = Z_REF_P(value_ptr);
  437. zval_ptr_dtor(variable_ptr);
  438. ZVAL_REF(variable_ptr, ref);
  439. } else {
  440. ZVAL_MAKE_REF(variable_ptr);
  441. }
  442. }
  443. /* this should modify object only if it's empty */
  444. static inline zval* make_real_object(zval *object_ptr TSRMLS_DC)
  445. {
  446. zval *object = object_ptr;
  447. ZVAL_DEREF(object);
  448. if (UNEXPECTED(Z_TYPE_P(object) != IS_OBJECT)) {
  449. if (Z_TYPE_P(object) == IS_NULL
  450. || Z_TYPE_P(object) == IS_FALSE
  451. || (Z_TYPE_P(object) == IS_STRING && Z_STRLEN_P(object) == 0)) {
  452. zval_ptr_dtor_nogc(object);
  453. object_init(object);
  454. zend_error(E_WARNING, "Creating default object from empty value");
  455. }
  456. }
  457. return object;
  458. }
  459. ZEND_API char * zend_verify_arg_class_kind(const zend_arg_info *cur_arg_info, zend_ulong fetch_type, char **class_name, zend_class_entry **pce TSRMLS_DC)
  460. {
  461. zend_string *key;
  462. ALLOCA_FLAG(use_heap);
  463. STR_ALLOCA_INIT(key, cur_arg_info->class_name, cur_arg_info->class_name_len, use_heap);
  464. *pce = zend_fetch_class(key, (fetch_type | ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_NO_AUTOLOAD) TSRMLS_CC);
  465. STR_ALLOCA_FREE(key, use_heap);
  466. *class_name = (*pce) ? (*pce)->name->val : (char*)cur_arg_info->class_name;
  467. if (*pce && (*pce)->ce_flags & ZEND_ACC_INTERFACE) {
  468. return "implement interface ";
  469. } else {
  470. return "be an instance of ";
  471. }
  472. }
  473. ZEND_API void zend_verify_arg_error(int error_type, const zend_function *zf, uint32_t arg_num, const char *need_msg, const char *need_kind, const char *given_msg, const char *given_kind, zval *arg TSRMLS_DC)
  474. {
  475. zend_execute_data *ptr = EG(current_execute_data)->prev_execute_data;
  476. const char *fname = zf->common.function_name->val;
  477. const char *fsep;
  478. const char *fclass;
  479. zval old_arg;
  480. if (zf->common.scope) {
  481. fsep = "::";
  482. fclass = zf->common.scope->name->val;
  483. } else {
  484. fsep = "";
  485. fclass = "";
  486. }
  487. if (arg && zf->common.type == ZEND_USER_FUNCTION) {
  488. ZVAL_COPY_VALUE(&old_arg, arg);
  489. ZVAL_UNDEF(arg);
  490. }
  491. if (zf->common.type == ZEND_USER_FUNCTION && ptr && ptr->func && ZEND_USER_CODE(ptr->func->common.type)) {
  492. zend_error(error_type, "Argument %d passed to %s%s%s() must %s%s, %s%s given, called in %s on line %d and defined", arg_num, fclass, fsep, fname, need_msg, need_kind, given_msg, given_kind, ptr->func->op_array.filename->val, ptr->opline->lineno);
  493. } else {
  494. zend_error(error_type, "Argument %d passed to %s%s%s() must %s%s, %s%s given", arg_num, fclass, fsep, fname, need_msg, need_kind, given_msg, given_kind);
  495. }
  496. if (arg && zf->common.type == ZEND_USER_FUNCTION) {
  497. ZVAL_COPY_VALUE(arg, &old_arg);
  498. }
  499. }
  500. static void zend_verify_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, zend_ulong fetch_type TSRMLS_DC)
  501. {
  502. zend_arg_info *cur_arg_info;
  503. char *need_msg;
  504. zend_class_entry *ce;
  505. if (UNEXPECTED(!zf->common.arg_info)) {
  506. return;
  507. }
  508. if (EXPECTED(arg_num <= zf->common.num_args)) {
  509. cur_arg_info = &zf->common.arg_info[arg_num-1];
  510. } else if (zf->common.fn_flags & ZEND_ACC_VARIADIC) {
  511. cur_arg_info = &zf->common.arg_info[zf->common.num_args-1];
  512. } else {
  513. return;
  514. }
  515. if (cur_arg_info->class_name) {
  516. char *class_name;
  517. ZVAL_DEREF(arg);
  518. if (Z_TYPE_P(arg) == IS_OBJECT) {
  519. need_msg = zend_verify_arg_class_kind(cur_arg_info, fetch_type, &class_name, &ce TSRMLS_CC);
  520. if (!ce || !instanceof_function(Z_OBJCE_P(arg), ce TSRMLS_CC)) {
  521. zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, "instance of ", Z_OBJCE_P(arg)->name->val, arg TSRMLS_CC);
  522. }
  523. } else if (Z_TYPE_P(arg) != IS_NULL || !cur_arg_info->allow_null) {
  524. need_msg = zend_verify_arg_class_kind(cur_arg_info, fetch_type, &class_name, &ce TSRMLS_CC);
  525. zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, zend_zval_type_name(arg), "", arg TSRMLS_CC);
  526. }
  527. } else if (cur_arg_info->type_hint) {
  528. if (cur_arg_info->type_hint == IS_ARRAY) {
  529. ZVAL_DEREF(arg);
  530. if (Z_TYPE_P(arg) != IS_ARRAY && (Z_TYPE_P(arg) != IS_NULL || !cur_arg_info->allow_null)) {
  531. zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be of the type array", "", zend_zval_type_name(arg), "", arg TSRMLS_CC);
  532. }
  533. } else if (cur_arg_info->type_hint == IS_CALLABLE) {
  534. if (!zend_is_callable(arg, IS_CALLABLE_CHECK_SILENT, NULL TSRMLS_CC) && (Z_TYPE_P(arg) != IS_NULL || !cur_arg_info->allow_null)) {
  535. zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be callable", "", zend_zval_type_name(arg), "", arg TSRMLS_CC);
  536. }
  537. #if ZEND_DEBUG
  538. } else {
  539. zend_error(E_ERROR, "Unknown typehint");
  540. #endif
  541. }
  542. }
  543. }
  544. static inline int zend_verify_missing_arg_type(zend_function *zf, uint32_t arg_num, zend_ulong fetch_type TSRMLS_DC)
  545. {
  546. zend_arg_info *cur_arg_info;
  547. char *need_msg;
  548. zend_class_entry *ce;
  549. if (UNEXPECTED(!zf->common.arg_info)) {
  550. return 1;
  551. }
  552. if (EXPECTED(arg_num <= zf->common.num_args)) {
  553. cur_arg_info = &zf->common.arg_info[arg_num-1];
  554. } else if (zf->common.fn_flags & ZEND_ACC_VARIADIC) {
  555. cur_arg_info = &zf->common.arg_info[zf->common.num_args-1];
  556. } else {
  557. return 1;
  558. }
  559. if (cur_arg_info->class_name) {
  560. char *class_name;
  561. need_msg = zend_verify_arg_class_kind(cur_arg_info, fetch_type, &class_name, &ce TSRMLS_CC);
  562. zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, "none", "", NULL TSRMLS_CC);
  563. return 0;
  564. } else if (cur_arg_info->type_hint) {
  565. if (cur_arg_info->type_hint == IS_ARRAY) {
  566. zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be of the type array", "", "none", "", NULL TSRMLS_CC);
  567. } else if (cur_arg_info->type_hint == IS_CALLABLE) {
  568. zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be callable", "", "none", "", NULL TSRMLS_CC);
  569. #if ZEND_DEBUG
  570. } else {
  571. zend_error(E_ERROR, "Unknown typehint");
  572. #endif
  573. }
  574. return 0;
  575. }
  576. return 1;
  577. }
  578. static void zend_verify_missing_arg(zend_execute_data *execute_data, uint32_t arg_num TSRMLS_DC)
  579. {
  580. if (EXPECTED(!(EX(func)->common.fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) ||
  581. zend_verify_missing_arg_type(EX(func), arg_num, EX(opline)->extended_value TSRMLS_CC)) {
  582. const char *class_name = EX(func)->common.scope ? EX(func)->common.scope->name->val : "";
  583. const char *space = EX(func)->common.scope ? "::" : "";
  584. const char *func_name = EX(func)->common.function_name ? EX(func)->common.function_name->val : "main";
  585. zend_execute_data *ptr = EX(prev_execute_data);
  586. if (ptr && ptr->func && ZEND_USER_CODE(ptr->func->common.type)) {
  587. zend_error(E_WARNING, "Missing argument %u for %s%s%s(), called in %s on line %d and defined", arg_num, class_name, space, func_name, ptr->func->op_array.filename->val, ptr->opline->lineno);
  588. } else {
  589. zend_error(E_WARNING, "Missing argument %u for %s%s%s()", arg_num, class_name, space, func_name);
  590. }
  591. }
  592. }
  593. static zend_always_inline void zend_assign_to_object(zval *retval, zval *object, uint32_t object_op_type, zval *property_name, int value_type, const znode_op *value_op, const zend_execute_data *execute_data, int opcode, void **cache_slot TSRMLS_DC)
  594. {
  595. zend_free_op free_value;
  596. zval *value = get_zval_ptr(value_type, value_op, execute_data, &free_value, BP_VAR_R);
  597. zval tmp;
  598. if (object_op_type != IS_UNUSED) {
  599. ZVAL_DEREF(object);
  600. if (UNEXPECTED(Z_TYPE_P(object) != IS_OBJECT)) {
  601. if (UNEXPECTED(object == &EG(error_zval))) {
  602. if (retval) {
  603. ZVAL_NULL(retval);
  604. }
  605. FREE_OP(free_value);
  606. return;
  607. }
  608. if (EXPECTED(Z_TYPE_P(object) == IS_NULL ||
  609. Z_TYPE_P(object) == IS_FALSE ||
  610. (Z_TYPE_P(object) == IS_STRING && Z_STRLEN_P(object) == 0))) {
  611. zend_object *obj;
  612. zval_ptr_dtor(object);
  613. object_init(object);
  614. Z_ADDREF_P(object);
  615. obj = Z_OBJ_P(object);
  616. zend_error(E_WARNING, "Creating default object from empty value");
  617. if (GC_REFCOUNT(obj) == 1) {
  618. /* the enclosing container was deleted, obj is unreferenced */
  619. if (retval) {
  620. ZVAL_NULL(retval);
  621. }
  622. FREE_OP(free_value);
  623. OBJ_RELEASE(obj);
  624. return;
  625. }
  626. Z_DELREF_P(object);
  627. } else {
  628. zend_error(E_WARNING, "Attempt to assign property of non-object");
  629. if (retval) {
  630. ZVAL_NULL(retval);
  631. }
  632. FREE_OP(free_value);
  633. return;
  634. }
  635. }
  636. }
  637. /* separate our value if necessary */
  638. if (value_type == IS_TMP_VAR) {
  639. ZVAL_COPY_VALUE(&tmp, value);
  640. value = &tmp;
  641. } else if (value_type == IS_CONST) {
  642. if (UNEXPECTED(Z_OPT_COPYABLE_P(value))) {
  643. ZVAL_COPY_VALUE(&tmp, value);
  644. zval_copy_ctor_func(&tmp);
  645. value = &tmp;
  646. }
  647. } else if (Z_REFCOUNTED_P(value)) {
  648. Z_ADDREF_P(value);
  649. }
  650. if (opcode == ZEND_ASSIGN_OBJ) {
  651. if (!Z_OBJ_HT_P(object)->write_property) {
  652. zend_error(E_WARNING, "Attempt to assign property of non-object");
  653. if (retval) {
  654. ZVAL_NULL(retval);
  655. }
  656. if (value_type == IS_CONST) {
  657. zval_ptr_dtor(value);
  658. }
  659. FREE_OP(free_value);
  660. return;
  661. }
  662. Z_OBJ_HT_P(object)->write_property(object, property_name, value, cache_slot TSRMLS_CC);
  663. } else {
  664. /* Note: property_name in this case is really the array index! */
  665. if (!Z_OBJ_HT_P(object)->write_dimension) {
  666. zend_error_noreturn(E_ERROR, "Cannot use object as array");
  667. }
  668. Z_OBJ_HT_P(object)->write_dimension(object, property_name, value TSRMLS_CC);
  669. }
  670. if (retval && !EG(exception)) {
  671. ZVAL_COPY(retval, value);
  672. }
  673. zval_ptr_dtor(value);
  674. if (value_type == IS_VAR) {
  675. FREE_OP(free_value);
  676. }
  677. }
  678. static void zend_assign_to_string_offset(zval *str, zend_long offset, zval *value, zval *result TSRMLS_DC)
  679. {
  680. zend_string *old_str;
  681. if (offset < 0) {
  682. zend_error(E_WARNING, "Illegal string offset: " ZEND_LONG_FMT, offset);
  683. zend_string_release(Z_STR_P(str));
  684. if (result) {
  685. ZVAL_NULL(result);
  686. }
  687. return;
  688. }
  689. old_str = Z_STR_P(str);
  690. if ((size_t)offset >= Z_STRLEN_P(str)) {
  691. zend_long old_len = Z_STRLEN_P(str);
  692. Z_STR_P(str) = zend_string_realloc(Z_STR_P(str), offset + 1, 0);
  693. Z_TYPE_INFO_P(str) = IS_STRING_EX;
  694. memset(Z_STRVAL_P(str) + old_len, ' ', offset - old_len);
  695. Z_STRVAL_P(str)[offset+1] = 0;
  696. } else if (!Z_REFCOUNTED_P(str)) {
  697. Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
  698. Z_TYPE_INFO_P(str) = IS_STRING_EX;
  699. }
  700. if (Z_TYPE_P(value) != IS_STRING) {
  701. zend_string *tmp = zval_get_string(value);
  702. Z_STRVAL_P(str)[offset] = tmp->val[0];
  703. zend_string_release(tmp);
  704. } else {
  705. Z_STRVAL_P(str)[offset] = Z_STRVAL_P(value)[0];
  706. }
  707. /*
  708. * the value of an assignment to a string offset is undefined
  709. T(result->u.var).var = &T->str_offset.str;
  710. */
  711. zend_string_release(old_str);
  712. if (result) {
  713. zend_uchar c = (zend_uchar)Z_STRVAL_P(str)[offset];
  714. if (CG(one_char_string)[c]) {
  715. ZVAL_INTERNED_STR(result, CG(one_char_string)[c]);
  716. } else {
  717. ZVAL_NEW_STR(result, zend_string_init(Z_STRVAL_P(str) + offset, 1, 0));
  718. }
  719. }
  720. }
  721. static zend_always_inline zval* zend_assign_to_variable(zval *variable_ptr, zval *value, zend_uchar value_type TSRMLS_DC)
  722. {
  723. do {
  724. if (UNEXPECTED(Z_REFCOUNTED_P(variable_ptr))) {
  725. zend_refcounted *garbage;
  726. if (Z_ISREF_P(variable_ptr)) {
  727. variable_ptr = Z_REFVAL_P(variable_ptr);
  728. if (EXPECTED(!Z_REFCOUNTED_P(variable_ptr))) {
  729. break;
  730. }
  731. }
  732. if (Z_TYPE_P(variable_ptr) == IS_OBJECT &&
  733. UNEXPECTED(Z_OBJ_HANDLER_P(variable_ptr, set) != NULL)) {
  734. Z_OBJ_HANDLER_P(variable_ptr, set)(variable_ptr, value TSRMLS_CC);
  735. return variable_ptr;
  736. }
  737. if ((value_type & (IS_VAR|IS_CV)) && variable_ptr == value) {
  738. return variable_ptr;
  739. }
  740. garbage = Z_COUNTED_P(variable_ptr);
  741. if (--GC_REFCOUNT(garbage) == 0) {
  742. ZVAL_COPY_VALUE(variable_ptr, value);
  743. if (value_type == IS_CONST) {
  744. /* IS_CONST can't be IS_OBJECT, IS_RESOURCE or IS_REFERENCE */
  745. if (UNEXPECTED(Z_OPT_COPYABLE_P(variable_ptr))) {
  746. zval_copy_ctor_func(variable_ptr);
  747. }
  748. } else if (value_type != IS_TMP_VAR) {
  749. if (UNEXPECTED(Z_OPT_REFCOUNTED_P(variable_ptr))) {
  750. Z_ADDREF_P(variable_ptr);
  751. }
  752. }
  753. _zval_dtor_func_for_ptr(garbage ZEND_FILE_LINE_CC);
  754. return variable_ptr;
  755. } else { /* we need to split */
  756. /* optimized version of GC_ZVAL_CHECK_POSSIBLE_ROOT(variable_ptr) */
  757. if ((Z_COLLECTABLE_P(variable_ptr)) &&
  758. UNEXPECTED(!GC_INFO(garbage))) {
  759. gc_possible_root(garbage TSRMLS_CC);
  760. }
  761. }
  762. }
  763. } while (0);
  764. ZVAL_COPY_VALUE(variable_ptr, value);
  765. if (value_type == IS_CONST) {
  766. /* IS_CONST can't be IS_OBJECT, IS_RESOURCE or IS_REFERENCE */
  767. if (UNEXPECTED(Z_OPT_COPYABLE_P(variable_ptr))) {
  768. zval_copy_ctor_func(variable_ptr);
  769. }
  770. } else if (value_type != IS_TMP_VAR) {
  771. if (UNEXPECTED(Z_OPT_REFCOUNTED_P(variable_ptr))) {
  772. Z_ADDREF_P(variable_ptr);
  773. }
  774. }
  775. return variable_ptr;
  776. }
  777. /* Utility Functions for Extensions */
  778. static void zend_extension_statement_handler(const zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
  779. {
  780. if (extension->statement_handler) {
  781. extension->statement_handler(op_array);
  782. }
  783. }
  784. static void zend_extension_fcall_begin_handler(const zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
  785. {
  786. if (extension->fcall_begin_handler) {
  787. extension->fcall_begin_handler(op_array);
  788. }
  789. }
  790. static void zend_extension_fcall_end_handler(const zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
  791. {
  792. if (extension->fcall_end_handler) {
  793. extension->fcall_end_handler(op_array);
  794. }
  795. }
  796. static zend_always_inline HashTable *zend_get_target_symbol_table(zend_execute_data *execute_data, int fetch_type TSRMLS_DC)
  797. {
  798. HashTable *ht;
  799. if (EXPECTED(fetch_type == ZEND_FETCH_GLOBAL_LOCK) ||
  800. EXPECTED(fetch_type == ZEND_FETCH_GLOBAL)) {
  801. ht = &EG(symbol_table).ht;
  802. } else if (EXPECTED(fetch_type == ZEND_FETCH_STATIC)) {
  803. ZEND_ASSERT(EX(func)->op_array.static_variables != NULL);
  804. ht = EX(func)->op_array.static_variables;
  805. } else {
  806. ZEND_ASSERT(fetch_type == ZEND_FETCH_LOCAL);
  807. if (!EX(symbol_table)) {
  808. zend_rebuild_symbol_table(TSRMLS_C);
  809. }
  810. ht = &EX(symbol_table)->ht;
  811. }
  812. return ht;
  813. }
  814. static zend_always_inline zval *zend_fetch_dimension_address_inner(HashTable *ht, const zval *dim, int dim_type, int type TSRMLS_DC)
  815. {
  816. zval *retval;
  817. zend_string *offset_key;
  818. zend_ulong hval;
  819. if (EXPECTED(Z_TYPE_P(dim) == IS_LONG)) {
  820. hval = Z_LVAL_P(dim);
  821. num_index:
  822. retval = zend_hash_index_find(ht, hval);
  823. if (retval == NULL) {
  824. switch (type) {
  825. case BP_VAR_R:
  826. zend_error(E_NOTICE,"Undefined offset: " ZEND_ULONG_FMT, hval);
  827. /* break missing intentionally */
  828. case BP_VAR_UNSET:
  829. case BP_VAR_IS:
  830. retval = &EG(uninitialized_zval);
  831. break;
  832. case BP_VAR_RW:
  833. zend_error(E_NOTICE,"Undefined offset: " ZEND_ULONG_FMT, hval);
  834. /* break missing intentionally */
  835. case BP_VAR_W:
  836. retval = zend_hash_index_add_new(ht, hval, &EG(uninitialized_zval));
  837. break;
  838. }
  839. }
  840. } else if (EXPECTED(Z_TYPE_P(dim) == IS_STRING)) {
  841. offset_key = Z_STR_P(dim);
  842. if (dim_type != IS_CONST) {
  843. if (ZEND_HANDLE_NUMERIC(offset_key, hval)) {
  844. goto num_index;
  845. }
  846. }
  847. str_index:
  848. retval = zend_hash_find(ht, offset_key);
  849. if (retval) {
  850. /* support for $GLOBALS[...] */
  851. if (UNEXPECTED(Z_TYPE_P(retval) == IS_INDIRECT)) {
  852. retval = Z_INDIRECT_P(retval);
  853. if (UNEXPECTED(Z_TYPE_P(retval) == IS_UNDEF)) {
  854. switch (type) {
  855. case BP_VAR_R:
  856. zend_error(E_NOTICE, "Undefined index: %s", offset_key->val);
  857. /* break missing intentionally */
  858. case BP_VAR_UNSET:
  859. case BP_VAR_IS:
  860. retval = &EG(uninitialized_zval);
  861. break;
  862. case BP_VAR_RW:
  863. zend_error(E_NOTICE,"Undefined index: %s", offset_key->val);
  864. /* break missing intentionally */
  865. case BP_VAR_W:
  866. ZVAL_NULL(retval);
  867. break;
  868. }
  869. }
  870. }
  871. } else {
  872. switch (type) {
  873. case BP_VAR_R:
  874. zend_error(E_NOTICE, "Undefined index: %s", offset_key->val);
  875. /* break missing intentionally */
  876. case BP_VAR_UNSET:
  877. case BP_VAR_IS:
  878. retval = &EG(uninitialized_zval);
  879. break;
  880. case BP_VAR_RW:
  881. zend_error(E_NOTICE,"Undefined index: %s", offset_key->val);
  882. /* break missing intentionally */
  883. case BP_VAR_W:
  884. retval = zend_hash_add_new(ht, offset_key, &EG(uninitialized_zval));
  885. break;
  886. }
  887. }
  888. } else {
  889. switch (Z_TYPE_P(dim)) {
  890. case IS_NULL:
  891. offset_key = STR_EMPTY_ALLOC();
  892. goto str_index;
  893. case IS_DOUBLE:
  894. hval = zend_dval_to_lval(Z_DVAL_P(dim));
  895. goto num_index;
  896. case IS_RESOURCE:
  897. zend_error(E_STRICT, "Resource ID#%pd used as offset, casting to integer (%pd)", Z_RES_HANDLE_P(dim), Z_RES_HANDLE_P(dim));
  898. hval = Z_RES_HANDLE_P(dim);
  899. goto num_index;
  900. case IS_FALSE:
  901. hval = 0;
  902. goto num_index;
  903. case IS_TRUE:
  904. hval = 1;
  905. goto num_index;
  906. default:
  907. zend_error(E_WARNING, "Illegal offset type");
  908. retval = (type == BP_VAR_W || type == BP_VAR_RW) ?
  909. &EG(error_zval) : &EG(uninitialized_zval);
  910. }
  911. }
  912. return retval;
  913. }
  914. static zend_always_inline zval *zend_fetch_dimension_address(zval *result, zval *container_ptr, zval *dim, int dim_type, int type, int is_ref, int allow_str_offset TSRMLS_DC)
  915. {
  916. zval *retval;
  917. zval *container = container_ptr;
  918. ZVAL_DEREF(container);
  919. if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
  920. SEPARATE_ARRAY(container);
  921. fetch_from_array:
  922. if (dim == NULL) {
  923. retval = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval));
  924. if (UNEXPECTED(retval == NULL)) {
  925. zend_error(E_WARNING, "Cannot add element to the array as the next element is already occupied");
  926. retval = &EG(error_zval);
  927. }
  928. } else {
  929. retval = zend_fetch_dimension_address_inner(Z_ARRVAL_P(container), dim, dim_type, type TSRMLS_CC);
  930. }
  931. if (is_ref) {
  932. ZVAL_MAKE_REF(retval);
  933. }
  934. ZVAL_INDIRECT(result, retval);
  935. } else if (EXPECTED(Z_TYPE_P(container) == IS_STRING)) {
  936. zend_long offset;
  937. if (type != BP_VAR_UNSET && UNEXPECTED(Z_STRLEN_P(container) == 0)) {
  938. zval_ptr_dtor_nogc(container);
  939. convert_to_array:
  940. ZVAL_NEW_ARR(container);
  941. zend_hash_init(Z_ARRVAL_P(container), 8, NULL, ZVAL_PTR_DTOR, 0);
  942. goto fetch_from_array;
  943. }
  944. if (dim == NULL) {
  945. zend_error_noreturn(E_ERROR, "[] operator not supported for strings");
  946. }
  947. if (UNEXPECTED(Z_TYPE_P(dim) != IS_LONG)) {
  948. switch(Z_TYPE_P(dim)) {
  949. case IS_STRING:
  950. if (IS_LONG == is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), NULL, NULL, -1)) {
  951. break;
  952. }
  953. if (type != BP_VAR_UNSET) {
  954. zend_error(E_WARNING, "Illegal string offset '%s'", Z_STRVAL_P(dim));
  955. }
  956. break;
  957. case IS_DOUBLE:
  958. case IS_NULL:
  959. case IS_FALSE:
  960. case IS_TRUE:
  961. zend_error(E_NOTICE, "String offset cast occurred");
  962. break;
  963. default:
  964. zend_error(E_WARNING, "Illegal offset type");
  965. break;
  966. }
  967. offset = zval_get_long(dim);
  968. } else {
  969. offset = Z_LVAL_P(dim);
  970. }
  971. if (allow_str_offset) {
  972. if (Z_REFCOUNTED_P(container)) {
  973. if (Z_REFCOUNT_P(container) > 1) {
  974. Z_DELREF_P(container);
  975. zval_copy_ctor_func(container);
  976. }
  977. Z_ADDREF_P(container);
  978. }
  979. ZVAL_LONG(result, offset);
  980. return container; /* assignment to string offset */
  981. } else {
  982. ZVAL_INDIRECT(result, NULL); /* wrong string offset */
  983. }
  984. } else if (EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
  985. if (!Z_OBJ_HT_P(container)->read_dimension) {
  986. zend_error_noreturn(E_ERROR, "Cannot use object as array");
  987. } else {
  988. retval = Z_OBJ_HT_P(container)->read_dimension(container, dim, type, result TSRMLS_CC);
  989. if (UNEXPECTED(retval == &EG(uninitialized_zval))) {
  990. zend_class_entry *ce = Z_OBJCE_P(container);
  991. ZVAL_NULL(result);
  992. zend_error(E_NOTICE, "Indirect modification of overloaded element of %s has no effect", ce->name->val);
  993. } else if (EXPECTED(retval && Z_TYPE_P(retval) != IS_UNDEF)) {
  994. if (!Z_ISREF_P(retval)) {
  995. if (Z_REFCOUNTED_P(retval) &&
  996. Z_REFCOUNT_P(retval) > 1) {
  997. if (Z_TYPE_P(retval) != IS_OBJECT) {
  998. Z_DELREF_P(retval);
  999. ZVAL_DUP(result, retval);
  1000. retval = result;
  1001. } else {
  1002. ZVAL_COPY(result, retval);
  1003. retval = result;
  1004. }
  1005. }
  1006. if (Z_TYPE_P(retval) != IS_OBJECT) {
  1007. zend_class_entry *ce = Z_OBJCE_P(container);
  1008. zend_error(E_NOTICE, "Indirect modification of overloaded element of %s has no effect", ce->name->val);
  1009. }
  1010. }
  1011. if (result != retval) {
  1012. if (is_ref) {
  1013. ZVAL_MAKE_REF(retval);
  1014. }
  1015. ZVAL_INDIRECT(result, retval);
  1016. }
  1017. } else {
  1018. ZVAL_INDIRECT(result, &EG(error_zval));
  1019. }
  1020. }
  1021. } else if (EXPECTED(Z_TYPE_P(container) == IS_NULL)) {
  1022. if (UNEXPECTED(container == &EG(error_zval))) {
  1023. ZVAL_INDIRECT(result, &EG(error_zval));
  1024. } else if (type != BP_VAR_UNSET) {
  1025. goto convert_to_array;
  1026. } else {
  1027. /* for read-mode only */
  1028. ZVAL_NULL(result);
  1029. }
  1030. } else {
  1031. if (type != BP_VAR_UNSET &&
  1032. Z_TYPE_P(container) == IS_FALSE) {
  1033. goto convert_to_array;
  1034. }
  1035. if (type == BP_VAR_UNSET) {
  1036. zend_error(E_WARNING, "Cannot unset offset in a non-array variable");
  1037. ZVAL_NULL(result);
  1038. } else {
  1039. zend_error(E_WARNING, "Cannot use a scalar value as an array");
  1040. ZVAL_INDIRECT(result, &EG(error_zval));
  1041. }
  1042. }
  1043. return NULL; /* not an assignment to string offset */
  1044. }
  1045. static zend_never_inline void zend_fetch_dimension_address_W(zval *result, zval *container_ptr, zval *dim, int dim_type TSRMLS_DC)
  1046. {
  1047. zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_W, 0, 0 TSRMLS_CC);
  1048. }
  1049. static zend_never_inline zval *zend_fetch_dimension_address_W_str(zval *result, zval *container_ptr, zval *dim, int dim_type TSRMLS_DC)
  1050. {
  1051. return zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_W, 0, 1 TSRMLS_CC);
  1052. }
  1053. static zend_never_inline void zend_fetch_dimension_address_W_ref(zval *result, zval *container_ptr, zval *dim, int dim_type TSRMLS_DC)
  1054. {
  1055. zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_W, 1, 0 TSRMLS_CC);
  1056. }
  1057. static zend_never_inline void zend_fetch_dimension_address_RW(zval *result, zval *container_ptr, zval *dim, int dim_type TSRMLS_DC)
  1058. {
  1059. zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_RW, 0, 0 TSRMLS_CC);
  1060. }
  1061. static zend_never_inline void zend_fetch_dimension_address_UNSET(zval *result, zval *container_ptr, zval *dim, int dim_type TSRMLS_DC)
  1062. {
  1063. zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_UNSET, 0, 0 TSRMLS_CC);
  1064. }
  1065. static zend_always_inline void zend_fetch_dimension_address_read(zval *result, zval *container, zval *dim, int dim_type, int type TSRMLS_DC)
  1066. {
  1067. zval *retval;
  1068. ZVAL_DEREF(container);
  1069. if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
  1070. retval = zend_fetch_dimension_address_inner(Z_ARRVAL_P(container), dim, dim_type, type TSRMLS_CC);
  1071. ZVAL_COPY(result, retval);
  1072. } else if (EXPECTED(Z_TYPE_P(container) == IS_STRING)) {
  1073. zend_long offset;
  1074. if (UNEXPECTED(Z_TYPE_P(dim) != IS_LONG)) {
  1075. switch(Z_TYPE_P(dim)) {
  1076. /* case IS_LONG: */
  1077. case IS_STRING:
  1078. if (IS_LONG == is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), NULL, NULL, -1)) {
  1079. break;
  1080. }
  1081. if (type != BP_VAR_IS) {
  1082. zend_error(E_WARNING, "Illegal string offset '%s'", Z_STRVAL_P(dim));
  1083. }
  1084. break;
  1085. case IS_DOUBLE:
  1086. case IS_NULL:
  1087. case IS_FALSE:
  1088. case IS_TRUE:
  1089. if (type != BP_VAR_IS) {
  1090. zend_error(E_NOTICE, "String offset cast occurred");
  1091. }
  1092. break;
  1093. default:
  1094. zend_error(E_WARNING, "Illegal offset type");
  1095. break;
  1096. }
  1097. offset = zval_get_long(dim);
  1098. } else {
  1099. offset = Z_LVAL_P(dim);
  1100. }
  1101. if (UNEXPECTED(offset < 0) || UNEXPECTED(Z_STRLEN_P(container) <= (size_t)offset)) {
  1102. if (type != BP_VAR_IS) {
  1103. zend_error(E_NOTICE, "Uninitialized string offset: %pd", offset);
  1104. }
  1105. ZVAL_EMPTY_STRING(result);
  1106. } else {
  1107. zend_uchar c = (zend_uchar)Z_STRVAL_P(container)[offset];
  1108. if (CG(one_char_string)[c]) {
  1109. ZVAL_INTERNED_STR(result, CG(one_char_string)[c]);
  1110. } else {
  1111. ZVAL_NEW_STR(result, zend_string_init(Z_STRVAL_P(container) + offset, 1, 0));
  1112. }
  1113. }
  1114. } else if (EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
  1115. if (!Z_OBJ_HT_P(container)->read_dimension) {
  1116. zend_error_noreturn(E_ERROR, "Cannot use object as array");
  1117. } else {
  1118. retval = Z_OBJ_HT_P(container)->read_dimension(container, dim, type, result TSRMLS_CC);
  1119. ZEND_ASSERT(result != NULL);
  1120. if (retval) {
  1121. if (result != retval) {
  1122. ZVAL_COPY(result, retval);
  1123. }
  1124. } else {
  1125. ZVAL_NULL(result);
  1126. }
  1127. }
  1128. } else {
  1129. ZVAL_NULL(result);
  1130. }
  1131. }
  1132. static zend_never_inline void zend_fetch_dimension_address_read_R(zval *result, zval *container, zval *dim, int dim_type TSRMLS_DC)
  1133. {
  1134. zend_fetch_dimension_address_read(result, container, dim, dim_type, BP_VAR_R TSRMLS_CC);
  1135. }
  1136. static zend_never_inline void zend_fetch_dimension_address_read_IS(zval *result, zval *container, zval *dim, int dim_type TSRMLS_DC)
  1137. {
  1138. zend_fetch_dimension_address_read(result, container, dim, dim_type, BP_VAR_IS TSRMLS_CC);
  1139. }
  1140. ZEND_API void zend_fetch_dimension_by_zval(zval *result, zval *container, zval *dim TSRMLS_DC)
  1141. {
  1142. zend_fetch_dimension_address_read_R(result, container, dim, IS_TMP_VAR TSRMLS_CC);
  1143. }
  1144. static zend_always_inline void zend_fetch_property_address(zval *result, zval *container, uint32_t container_op_type, zval *prop_ptr, void **cache_slot, int type, int is_ref TSRMLS_DC)
  1145. {
  1146. if (container_op_type != IS_UNUSED) {
  1147. ZVAL_DEREF(container);
  1148. if (UNEXPECTED(Z_TYPE_P(container) != IS_OBJECT)) {
  1149. if (UNEXPECTED(container == &EG(error_zval))) {
  1150. ZVAL_INDIRECT(result, &EG(error_zval));
  1151. return;
  1152. }
  1153. /* this should modify object only if it's empty */
  1154. if (type != BP_VAR_UNSET &&
  1155. EXPECTED((Z_TYPE_P(container) == IS_NULL ||
  1156. Z_TYPE_P(container) == IS_FALSE ||
  1157. (Z_TYPE_P(container) == IS_STRING && Z_STRLEN_P(container)==0)))) {
  1158. zval_ptr_dtor_nogc(container);
  1159. object_init(container);
  1160. } else {
  1161. zend_error(E_WARNING, "Attempt to modify property of non-object");
  1162. ZVAL_INDIRECT(result, &EG(error_zval));
  1163. return;
  1164. }
  1165. }
  1166. }
  1167. if (EXPECTED(Z_OBJ_HT_P(container)->get_property_ptr_ptr)) {
  1168. zval *ptr = Z_OBJ_HT_P(container)->get_property_ptr_ptr(container, prop_ptr, type, cache_slot TSRMLS_CC);
  1169. if (NULL == ptr) {
  1170. if (Z_OBJ_HT_P(container)->read_property &&
  1171. (ptr = Z_OBJ_HT_P(container)->read_property(container, prop_ptr, type, cache_slot, result TSRMLS_CC)) != NULL) {
  1172. if (ptr != result) {
  1173. if (is_ref && ptr != &EG(uninitialized_zval)) {
  1174. ZVAL_MAKE_REF(ptr);
  1175. }
  1176. ZVAL_INDIRECT(result, ptr);
  1177. }
  1178. } else {
  1179. zend_error_noreturn(E_ERROR, "Cannot access undefined property for object with overloaded property access");
  1180. }
  1181. } else {
  1182. if (is_ref) {
  1183. ZVAL_MAKE_REF(ptr);
  1184. }
  1185. ZVAL_INDIRECT(result, ptr);
  1186. }
  1187. } else if (EXPECTED(Z_OBJ_HT_P(container)->read_property)) {
  1188. zval *ptr = Z_OBJ_HT_P(container)->read_property(container, prop_ptr, type, cache_slot, result TSRMLS_CC);
  1189. if (ptr != result) {
  1190. if (is_ref && ptr != &EG(uninitialized_zval)) {
  1191. ZVAL_MAKE_REF(ptr);
  1192. }
  1193. ZVAL_INDIRECT(result, ptr);
  1194. }
  1195. } else {
  1196. zend_error(E_WARNING, "This object doesn't support property references");
  1197. ZVAL_INDIRECT(result, &EG(error_zval));
  1198. }
  1199. }
  1200. static inline zend_brk_cont_element* zend_brk_cont(int nest_levels, int array_offset, const zend_op_array *op_array, const zend_execute_data *execute_data TSRMLS_DC)
  1201. {
  1202. int original_nest_levels = nest_levels;
  1203. zend_brk_cont_element *jmp_to;
  1204. do {
  1205. if (array_offset==-1) {
  1206. zend_error_noreturn(E_ERROR, "Cannot break/continue %d level%s", original_nest_levels, (original_nest_levels == 1) ? "" : "s");
  1207. }
  1208. jmp_to = &op_array->brk_cont_array[array_offset];
  1209. if (nest_levels>1) {
  1210. zend_op *brk_opline = &op_array->opcodes[jmp_to->brk];
  1211. if (brk_opline->opcode == ZEND_FREE) {
  1212. if (!(brk_opline->extended_value & EXT_TYPE_FREE_ON_RETURN)) {
  1213. zval_ptr_dtor_nogc(EX_VAR(brk_opline->op1.var));
  1214. }
  1215. }
  1216. }
  1217. array_offset = jmp_to->parent;
  1218. } while (--nest_levels > 0);
  1219. return jmp_to;
  1220. }
  1221. #if ZEND_INTENSIVE_DEBUGGING
  1222. #define CHECK_SYMBOL_TABLES() \
  1223. zend_hash_apply(&EG(symbol_table), zend_check_symbol TSRMLS_CC); \
  1224. if (&EG(symbol_table)!=EX(symbol_table)) { \
  1225. zend_hash_apply(EX(symbol_table), zend_check_symbol TSRMLS_CC); \
  1226. }
  1227. static int zend_check_symbol(zval *pz TSRMLS_DC)
  1228. {
  1229. if (Z_TYPE_P(pz) == IS_INDIRECT) {
  1230. pz = Z_INDIRECT_P(pz);
  1231. }
  1232. if (Z_TYPE_P(pz) > 10) {
  1233. fprintf(stderr, "Warning! %x has invalid type!\n", *pz);
  1234. /* See http://support.microsoft.com/kb/190351 */
  1235. #ifdef PHP_WIN32
  1236. fflush(stderr);
  1237. #endif
  1238. } else if (Z_TYPE_P(pz) == IS_ARRAY) {
  1239. zend_hash_apply(Z_ARRVAL_P(pz), zend_check_symbol TSRMLS_CC);
  1240. } else if (Z_TYPE_P(pz) == IS_OBJECT) {
  1241. /* OBJ-TBI - doesn't support new object model! */
  1242. zend_hash_apply(Z_OBJPROP_P(pz), zend_check_symbol TSRMLS_CC);
  1243. }
  1244. return 0;
  1245. }
  1246. #else
  1247. #define CHECK_SYMBOL_TABLES()
  1248. #endif
  1249. ZEND_API opcode_handler_t *zend_opcode_handlers;
  1250. ZEND_API void execute_internal(zend_execute_data *execute_data, zval *return_value TSRMLS_DC)
  1251. {
  1252. execute_data->func->internal_function.handler(execute_data, return_value TSRMLS_CC);
  1253. }
  1254. ZEND_API void zend_clean_and_cache_symbol_table(zend_array *symbol_table TSRMLS_DC) /* {{{ */
  1255. {
  1256. if (EG(symtable_cache_ptr) >= EG(symtable_cache_limit)) {
  1257. zend_hash_destroy(&symbol_table->ht);
  1258. efree_size(symbol_table, sizeof(zend_array));
  1259. } else {
  1260. /* clean before putting into the cache, since clean
  1261. could call dtors, which could use cached hash */
  1262. zend_hash_clean(&symbol_table->ht);
  1263. *(++EG(symtable_cache_ptr)) = symbol_table;
  1264. }
  1265. }
  1266. /* }}} */
  1267. static zend_always_inline void i_free_compiled_variables(zend_execute_data *execute_data TSRMLS_DC) /* {{{ */
  1268. {
  1269. if (EXPECTED(EX(func)->op_array.last_var > 0)) {
  1270. zval *cv = EX_VAR_NUM(0);
  1271. zval *end = cv + EX(func)->op_array.last_var;
  1272. do {
  1273. zval_ptr_dtor(cv);
  1274. cv++;
  1275. } while (cv != end);
  1276. }
  1277. }
  1278. /* }}} */
  1279. void zend_free_compiled_variables(zend_execute_data *execute_data TSRMLS_DC) /* {{{ */
  1280. {
  1281. i_free_compiled_variables(execute_data TSRMLS_CC);
  1282. }
  1283. /* }}} */
  1284. /*
  1285. * Stack Frame Layout (the whole stack frame is allocated at once)
  1286. * ==================
  1287. *
  1288. * +========================================+
  1289. * EG(current_execute_data) -> | zend_execute_data |
  1290. * +----------------------------------------+
  1291. * EX_CV_NUM(0) ---------> | VAR[0] = ARG[1] |
  1292. * | ... |
  1293. * | VAR[op_array->num_args-1] = ARG[N] |
  1294. * | ... |
  1295. * | VAR[op_array->last_var-1] |
  1296. * | VAR[op_array->last_var] = TMP[0] |
  1297. * | ... |
  1298. * | VAR[op_array->last_var+op_array->T-1] |
  1299. * | ARG[N+1] (extra_args) |
  1300. * | ... |
  1301. * +----------------------------------------+
  1302. */
  1303. static zend_always_inline void i_init_func_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value TSRMLS_DC) /* {{{ */
  1304. {
  1305. uint32_t first_extra_arg, num_args;
  1306. ZEND_ASSERT(EX(func) == (zend_function*)op_array);
  1307. ZEND_ASSERT(EX(scope) == EG(scope));
  1308. EX(opline) = op_array->opcodes;
  1309. EX(call) = NULL;
  1310. EX(return_value) = return_value;
  1311. EX(delayed_exception) = NULL;
  1312. EX(silence_op_num) = -1;
  1313. /* Handle arguments */
  1314. first_extra_arg = op_array->num_args;
  1315. if (UNEXPECTED((op_array->fn_flags & ZEND_ACC_VARIADIC) != 0)) {
  1316. first_extra_arg--;
  1317. }
  1318. num_args = EX(num_args);
  1319. if (UNEXPECTED(num_args > first_extra_arg)) {
  1320. zval *end, *src, *dst;
  1321. if (EXPECTED((op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0)) {
  1322. /* Skip useless ZEND_RECV and ZEND_RECV_INIT opcodes */
  1323. EX(opline) += first_extra_arg;
  1324. }
  1325. /* move extra args into separate array after all CV and TMP vars */
  1326. end = EX_VAR_NUM(first_extra_arg - 1);
  1327. src = end + (num_args - first_extra_arg);
  1328. dst = src + (op_array->last_var + op_array->T - first_extra_arg);
  1329. if (EXPECTED(src != dst)) {
  1330. do {
  1331. ZVAL_COPY_VALUE(dst, src);
  1332. ZVAL_UNDEF(src);
  1333. src--;
  1334. dst--;
  1335. } while (src != end);
  1336. }
  1337. } else if (EXPECTED((op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0)) {
  1338. /* Skip useless ZEND_RECV and ZEND_RECV_INIT opcodes */
  1339. EX(opline) += num_args;
  1340. }
  1341. /* Initialize CV variables (skip arguments) */
  1342. if (EXPECTED((int)num_args < op_array->last_var)) {
  1343. zval *var = EX_VAR_NUM(num_args);
  1344. zval *end = EX_VAR_NUM(op_array->last_var);
  1345. do {
  1346. ZVAL_UNDEF(var);
  1347. var++;
  1348. } while (var != end);
  1349. }
  1350. if (op_array->this_var != -1 && Z_OBJ(EX(This))) {
  1351. ZVAL_OBJ(EX_VAR(op_array->this_var), Z_OBJ(EX(This)));
  1352. GC_REFCOUNT(Z_OBJ(EX(This)))++;
  1353. }
  1354. if (!op_array->run_time_cache && op_array->last_cache_slot) {
  1355. op_array->run_time_cache = zend_arena_calloc(&CG(arena), op_array->last_cache_slot, sizeof(void*));
  1356. }
  1357. EX(run_time_cache) = op_array->run_time_cache;
  1358. EG(current_execute_data) = execute_data;
  1359. }
  1360. /* }}} */
  1361. static zend_always_inline void i_init_code_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value TSRMLS_DC) /* {{{ */
  1362. {
  1363. ZEND_ASSERT(EX(func) == (zend_function*)op_array);
  1364. EX(opline) = op_array->opcodes;
  1365. EX(call) = NULL;
  1366. EX(return_value) = return_value;
  1367. EX(scope) = EG(scope);
  1368. EX(delayed_exception) = NULL;
  1369. EX(silence_op_num) = -1;
  1370. zend_attach_symbol_table(execute_data);
  1371. if (op_array->this_var != -1 && Z_OBJ(EX(This))) {
  1372. ZVAL_OBJ(EX_VAR(op_array->this_var), Z_OBJ(EX(This)));
  1373. GC_REFCOUNT(Z_OBJ(EX(This)))++;
  1374. }
  1375. if (!op_array->run_time_cache && op_array->last_cache_slot) {
  1376. op_array->run_time_cache = ecalloc(op_array->last_cache_slot, sizeof(void*));
  1377. }
  1378. EX(run_time_cache) = op_array->run_time_cache;
  1379. EG(current_execute_data) = execute_data;
  1380. }
  1381. /* }}} */
  1382. static zend_always_inline void i_init_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value TSRMLS_DC) /* {{{ */
  1383. {
  1384. ZEND_ASSERT(EX(func) == (zend_function*)op_array);
  1385. EX(opline) = op_array->opcodes;
  1386. EX(call) = NULL;
  1387. EX(return_value) = return_value;
  1388. EX(scope) = EG(scope);
  1389. EX(delayed_exception) = NULL;
  1390. EX(silence_op_num) = -1;
  1391. if (UNEXPECTED(EX(symbol_table) != NULL)) {
  1392. zend_attach_symbol_table(execute_data);
  1393. } else {
  1394. uint32_t first_extra_arg, num_args;
  1395. /* Handle arguments */
  1396. first_extra_arg = op_array->num_args;
  1397. if (UNEXPECTED((op_array->fn_flags & ZEND_ACC_VARIADIC) != 0)) {
  1398. first_extra_arg--;
  1399. }
  1400. num_args = EX(num_args);
  1401. if (UNEXPECTED(num_args > first_extra_arg)) {
  1402. zval *end, *src, *dst;
  1403. if (EXPECTED((op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0)) {
  1404. /* Skip useless ZEND_RECV and ZEND_RECV_INIT opcodes */
  1405. EX(opline) += first_extra_arg;
  1406. }
  1407. /* move extra args into separate array after all CV and TMP vars */
  1408. end = EX_VAR_NUM(first_extra_arg - 1);
  1409. src = end + (num_args - first_extra_arg);
  1410. dst = src + (op_array->last_var + op_array->T - first_extra_arg);
  1411. if (EXPECTED(src != dst)) {
  1412. do {
  1413. ZVAL_COPY_VALUE(dst, src);
  1414. ZVAL_UNDEF(src);
  1415. src--;
  1416. dst--;
  1417. } while (src != end);
  1418. }
  1419. } else if (EXPECTED((op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0)) {
  1420. /* Skip useless ZEND_RECV and ZEND_RECV_INIT opcodes */
  1421. EX(opline) += num_args;
  1422. }
  1423. /* Initialize CV variables (skip arguments) */
  1424. if (EXPECTED((int)num_args < op_array->last_var)) {
  1425. zval *var = EX_VAR_NUM(num_args);
  1426. zval *end = EX_VAR_NUM(op_array->last_var);
  1427. do {
  1428. ZVAL_UNDEF(var);
  1429. var++;
  1430. } while (var != end);
  1431. }
  1432. }
  1433. if (op_array->this_var != -1 && Z_OBJ(EX(This))) {
  1434. ZVAL_OBJ(EX_VAR(op_array->this_var), Z_OBJ(EX(This)));
  1435. GC_REFCOUNT(Z_OBJ(EX(This)))++;
  1436. }
  1437. if (!op_array->run_time_cache && op_array->last_cache_slot) {
  1438. if (op_array->function_name) {
  1439. op_array->run_time_cache = zend_arena_calloc(&CG(arena), op_array->last_cache_slot, sizeof(void*));
  1440. } else {
  1441. op_array->run_time_cache = ecalloc(op_array->last_cache_slot, sizeof(void*));
  1442. }
  1443. }
  1444. EX(run_time_cache) = op_array->run_time_cache;
  1445. EG(current_execute_data) = execute_data;
  1446. }
  1447. /* }}} */
  1448. ZEND_API zend_execute_data *zend_create_generator_execute_data(zend_execute_data *call, zend_op_array *op_array, zval *return_value TSRMLS_DC) /* {{{ */
  1449. {
  1450. /*
  1451. * Normally the execute_data is allocated on the VM stack (because it does
  1452. * not actually do any allocation and thus is faster). For generators
  1453. * though this behavior would be suboptimal, because the (rather large)
  1454. * structure would have to be copied back and forth every time execution is
  1455. * suspended or resumed. That's why for generators the execution context
  1456. * is allocated using a separate VM stack, thus allowing to save and
  1457. * restore it simply by replacing a pointer.
  1458. */
  1459. zend_execute_data *execute_data;
  1460. uint32_t num_args = call->num_args;
  1461. size_t stack_size = (ZEND_CALL_FRAME_SLOT + MAX(op_array->last_var + op_array->T, num_args)) * sizeof(zval);
  1462. EG(vm_stack) = zend_vm_stack_new_page(
  1463. EXPECTED(stack_size < ZEND_VM_STACK_FREE_PAGE_SIZE) ?
  1464. ZEND_VM_STACK_PAGE_SIZE :
  1465. ZEND_VM_STACK_PAGE_ALIGNED_SIZE(stack_size),
  1466. NULL);
  1467. EG(vm_stack_top) = EG(vm_stack)->top;
  1468. EG(vm_stack_end) = EG(vm_stack)->end;
  1469. execute_data = zend_vm_stack_push_call_frame(
  1470. VM_FRAME_TOP_FUNCTION,
  1471. (zend_function*)op_array,
  1472. num_args,
  1473. call->called_scope,
  1474. Z_OBJ(call->This),
  1475. NULL TSRMLS_CC);
  1476. EX(num_args) = num_args;
  1477. /* copy arguments */
  1478. if (num_args > 0) {
  1479. zval *arg_src = ZEND_CALL_ARG(call, 1);
  1480. zval *arg_dst = ZEND_CALL_ARG(execute_data, 1);
  1481. uint32_t i;
  1482. for (i = 0; i < num_args; i++) {
  1483. ZVAL_COPY_VALUE(arg_dst + i, arg_src + i);
  1484. }
  1485. }
  1486. EX(symbol_table) = NULL;
  1487. EX(scope) = EG(scope);
  1488. i_init_func_execute_data(execute_data, op_array, return_value TSRMLS_CC);
  1489. return execute_data;
  1490. }
  1491. /* }}} */
  1492. ZEND_API void zend_init_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value TSRMLS_DC) /* {{{ */
  1493. {
  1494. EX(prev_execute_data) = EG(current_execute_data);
  1495. i_init_execute_data(execute_data, op_array, return_value TSRMLS_CC);
  1496. }
  1497. /* }}} */
  1498. static zend_always_inline zend_bool zend_is_by_ref_func_arg_fetch(const zend_op *opline, zend_execute_data *call TSRMLS_DC) /* {{{ */
  1499. {
  1500. uint32_t arg_num = opline->extended_value & ZEND_FETCH_ARG_MASK;
  1501. return ARG_SHOULD_BE_SENT_BY_REF(call->func, arg_num);
  1502. }
  1503. /* }}} */
  1504. static zend_execute_data *zend_vm_stack_copy_call_frame(zend_execute_data *call, uint32_t passed_args, uint32_t additional_args TSRMLS_DC) /* {{{ */
  1505. {
  1506. zend_execute_data *new_call;
  1507. int used_stack = (EG(vm_stack_top) - (zval*)call) + additional_args;
  1508. /* copy call frame into new stack segment */
  1509. new_call = zend_vm_stack_extend(used_stack * sizeof(zval) TSRMLS_CC);
  1510. *new_call = *call;
  1511. if (passed_args) {
  1512. zval *src = ZEND_CALL_ARG(call, 1);
  1513. zval *dst = ZEND_CALL_ARG(new_call, 1);
  1514. do {
  1515. ZVAL_COPY_VALUE(dst, src);
  1516. passed_args--;
  1517. src++;
  1518. dst++;
  1519. } while (passed_args);
  1520. }
  1521. /* delete old call_frame from previous stack segment */
  1522. EG(vm_stack)->prev->top = (zval*)call;
  1523. /* delete previous stack segment if it becames empty */
  1524. if (UNEXPECTED(EG(vm_stack)->prev->top == ZEND_VM_STACK_ELEMETS(EG(vm_stack)->prev))) {
  1525. zend_vm_stack r = EG(vm_stack)->prev;
  1526. EG(vm_stack)->prev = r->prev;
  1527. efree(r);
  1528. }
  1529. return new_call;
  1530. }
  1531. /* }}} */
  1532. static zend_always_inline void zend_vm_stack_extend_call_frame(zend_execute_data **call, uint32_t passed_args, uint32_t additional_args TSRMLS_DC) /* {{{ */
  1533. {
  1534. if (EXPECTED(EG(vm_stack_end) - EG(vm_stack_top) > additional_args)) {
  1535. EG(vm_stack_top) += additional_args;
  1536. } else {
  1537. *call = zend_vm_stack_copy_call_frame(*call, passed_args, additional_args TSRMLS_CC);
  1538. }
  1539. }
  1540. /* }}} */
  1541. #define ZEND_VM_NEXT_OPCODE() \
  1542. CHECK_SYMBOL_TABLES() \
  1543. ZEND_VM_INC_OPCODE(); \
  1544. ZEND_VM_CONTINUE()
  1545. #define ZEND_VM_SET_OPCODE(new_op) \
  1546. CHECK_SYMBOL_TABLES() \
  1547. OPLINE = new_op
  1548. #define ZEND_VM_SET_RELATIVE_OPCODE(opline, offset) \
  1549. CHECK_SYMBOL_TABLES() \
  1550. OPLINE = ((zend_op*)(((char*)opline)+(offset)))
  1551. #define ZEND_VM_JMP(new_op) \
  1552. if (EXPECTED(!EG(exception))) { \
  1553. ZEND_VM_SET_OPCODE(new_op); \
  1554. } else { \
  1555. LOAD_OPLINE(); \
  1556. } \
  1557. ZEND_VM_CONTINUE()
  1558. #define ZEND_VM_INC_OPCODE() \
  1559. OPLINE++
  1560. #ifdef __GNUC__
  1561. # define ZEND_VM_GUARD(name) __asm__("#" #name)
  1562. #else
  1563. # define ZEND_VM_GUARD(name)
  1564. #endif
  1565. #include "zend_vm_execute.h"
  1566. ZEND_API int zend_set_user_opcode_handler(zend_uchar opcode, user_opcode_handler_t handler)
  1567. {
  1568. if (opcode != ZEND_USER_OPCODE) {
  1569. if (handler == NULL) {
  1570. /* restore the original handler */
  1571. zend_user_opcodes[opcode] = opcode;
  1572. } else {
  1573. zend_user_opcodes[opcode] = ZEND_USER_OPCODE;
  1574. }
  1575. zend_user_opcode_handlers[opcode] = handler;
  1576. return SUCCESS;
  1577. }
  1578. return FAILURE;
  1579. }
  1580. ZEND_API user_opcode_handler_t zend_get_user_opcode_handler(zend_uchar opcode)
  1581. {
  1582. return zend_user_opcode_handlers[opcode];
  1583. }
  1584. 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) {
  1585. return get_zval_ptr(op_type, node, execute_data, should_free, type);
  1586. }
  1587. /*
  1588. * Local variables:
  1589. * tab-width: 4
  1590. * c-basic-offset: 4
  1591. * indent-tabs-mode: t
  1592. * End:
  1593. */