PageRenderTime 68ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/Zend/zend_execute.c

http://github.com/php/php-src
C | 4504 lines | 3832 code | 504 blank | 168 comment | 1279 complexity | 7c5eab72c2e4612efe22639a649ff2bb MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Dmitry Stogov <dmitry@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #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. #include "zend_type_info.h"
  39. #include "zend_smart_str.h"
  40. /* Virtual current working directory support */
  41. #include "zend_virtual_cwd.h"
  42. #ifdef HAVE_GCC_GLOBAL_REGS
  43. # if defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(i386)
  44. # define ZEND_VM_FP_GLOBAL_REG "%esi"
  45. # define ZEND_VM_IP_GLOBAL_REG "%edi"
  46. # elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__x86_64__)
  47. # define ZEND_VM_FP_GLOBAL_REG "%r14"
  48. # define ZEND_VM_IP_GLOBAL_REG "%r15"
  49. # elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__powerpc64__)
  50. # define ZEND_VM_FP_GLOBAL_REG "r28"
  51. # define ZEND_VM_IP_GLOBAL_REG "r29"
  52. # elif defined(__IBMC__) && ZEND_GCC_VERSION >= 4002 && defined(__powerpc64__)
  53. # define ZEND_VM_FP_GLOBAL_REG "r28"
  54. # define ZEND_VM_IP_GLOBAL_REG "r29"
  55. # elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__aarch64__)
  56. # define ZEND_VM_FP_GLOBAL_REG "x27"
  57. # define ZEND_VM_IP_GLOBAL_REG "x28"
  58. # endif
  59. #endif
  60. #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  61. # pragma GCC diagnostic ignored "-Wvolatile-register-var"
  62. register zend_execute_data* volatile execute_data __asm__(ZEND_VM_FP_GLOBAL_REG);
  63. # pragma GCC diagnostic warning "-Wvolatile-register-var"
  64. #endif
  65. #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  66. # define EXECUTE_DATA_D void
  67. # define EXECUTE_DATA_C
  68. # define EXECUTE_DATA_DC
  69. # define EXECUTE_DATA_CC
  70. # define NO_EXECUTE_DATA_CC
  71. #else
  72. # define EXECUTE_DATA_D zend_execute_data* execute_data
  73. # define EXECUTE_DATA_C execute_data
  74. # define EXECUTE_DATA_DC , EXECUTE_DATA_D
  75. # define EXECUTE_DATA_CC , EXECUTE_DATA_C
  76. # define NO_EXECUTE_DATA_CC , NULL
  77. #endif
  78. #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  79. # define OPLINE_D void
  80. # define OPLINE_C
  81. # define OPLINE_DC
  82. # define OPLINE_CC
  83. #else
  84. # define OPLINE_D const zend_op* opline
  85. # define OPLINE_C opline
  86. # define OPLINE_DC , OPLINE_D
  87. # define OPLINE_CC , OPLINE_C
  88. #endif
  89. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  90. # pragma GCC diagnostic ignored "-Wvolatile-register-var"
  91. register const zend_op* volatile opline __asm__(ZEND_VM_IP_GLOBAL_REG);
  92. # pragma GCC diagnostic warning "-Wvolatile-register-var"
  93. #else
  94. #endif
  95. #define _CONST_CODE 0
  96. #define _TMP_CODE 1
  97. #define _VAR_CODE 2
  98. #define _UNUSED_CODE 3
  99. #define _CV_CODE 4
  100. typedef int (ZEND_FASTCALL *incdec_t)(zval *);
  101. #define get_zval_ptr(op_type, node, type) _get_zval_ptr(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
  102. #define get_zval_ptr_deref(op_type, node, type) _get_zval_ptr_deref(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
  103. #define get_zval_ptr_undef(op_type, node, type) _get_zval_ptr_undef(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
  104. #define get_op_data_zval_ptr_r(op_type, node) _get_op_data_zval_ptr_r(op_type, node EXECUTE_DATA_CC OPLINE_CC)
  105. #define get_op_data_zval_ptr_deref_r(op_type, node) _get_op_data_zval_ptr_deref_r(op_type, node EXECUTE_DATA_CC OPLINE_CC)
  106. #define get_zval_ptr_ptr(op_type, node, type) _get_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC)
  107. #define get_zval_ptr_ptr_undef(op_type, node, type) _get_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC)
  108. #define get_obj_zval_ptr(op_type, node, type) _get_obj_zval_ptr(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
  109. #define get_obj_zval_ptr_undef(op_type, node, type) _get_obj_zval_ptr_undef(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
  110. #define get_obj_zval_ptr_ptr(op_type, node, type) _get_obj_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC)
  111. #define RETURN_VALUE_USED(opline) ((opline)->result_type != IS_UNUSED)
  112. static ZEND_FUNCTION(pass)
  113. {
  114. }
  115. ZEND_API const zend_internal_function zend_pass_function = {
  116. ZEND_INTERNAL_FUNCTION, /* type */
  117. {0, 0, 0}, /* arg_flags */
  118. 0, /* fn_flags */
  119. NULL, /* name */
  120. NULL, /* scope */
  121. NULL, /* prototype */
  122. 0, /* num_args */
  123. 0, /* required_num_args */
  124. NULL, /* arg_info */
  125. ZEND_FN(pass), /* handler */
  126. NULL, /* module */
  127. {NULL,NULL,NULL,NULL} /* reserved */
  128. };
  129. #define FREE_VAR_PTR_AND_EXTRACT_RESULT_IF_NECESSARY(free_var) do { \
  130. zval *__container_to_free = EX_VAR(free_var); \
  131. if (UNEXPECTED(Z_REFCOUNTED_P(__container_to_free))) { \
  132. zend_refcounted *__ref = Z_COUNTED_P(__container_to_free); \
  133. if (UNEXPECTED(!GC_DELREF(__ref))) { \
  134. zval *__zv = EX_VAR(opline->result.var); \
  135. if (EXPECTED(Z_TYPE_P(__zv) == IS_INDIRECT)) { \
  136. ZVAL_COPY(__zv, Z_INDIRECT_P(__zv)); \
  137. } \
  138. rc_dtor_func(__ref); \
  139. } \
  140. } \
  141. } while (0)
  142. #define FREE_OP(type, var) \
  143. if ((type) & (IS_TMP_VAR|IS_VAR)) { \
  144. zval_ptr_dtor_nogc(EX_VAR(var)); \
  145. }
  146. #define FREE_UNFETCHED_OP(type, var) \
  147. FREE_OP(type, var)
  148. #define FREE_OP_VAR_PTR(type, var) \
  149. FREE_OP(type, var)
  150. #define CV_DEF_OF(i) (EX(func)->op_array.vars[i])
  151. #define ZEND_VM_STACK_PAGE_SLOTS (16 * 1024) /* should be a power of 2 */
  152. #define ZEND_VM_STACK_PAGE_SIZE (ZEND_VM_STACK_PAGE_SLOTS * sizeof(zval))
  153. #define ZEND_VM_STACK_PAGE_ALIGNED_SIZE(size, page_size) \
  154. (((size) + ZEND_VM_STACK_HEADER_SLOTS * sizeof(zval) \
  155. + ((page_size) - 1)) & ~((page_size) - 1))
  156. static zend_always_inline zend_vm_stack zend_vm_stack_new_page(size_t size, zend_vm_stack prev) {
  157. zend_vm_stack page = (zend_vm_stack)emalloc(size);
  158. page->top = ZEND_VM_STACK_ELEMENTS(page);
  159. page->end = (zval*)((char*)page + size);
  160. page->prev = prev;
  161. return page;
  162. }
  163. ZEND_API void zend_vm_stack_init(void)
  164. {
  165. EG(vm_stack_page_size) = ZEND_VM_STACK_PAGE_SIZE;
  166. EG(vm_stack) = zend_vm_stack_new_page(ZEND_VM_STACK_PAGE_SIZE, NULL);
  167. EG(vm_stack_top) = EG(vm_stack)->top;
  168. EG(vm_stack_end) = EG(vm_stack)->end;
  169. }
  170. ZEND_API void zend_vm_stack_init_ex(size_t page_size)
  171. {
  172. /* page_size must be a power of 2 */
  173. ZEND_ASSERT(page_size > 0 && (page_size & (page_size - 1)) == 0);
  174. EG(vm_stack_page_size) = page_size;
  175. EG(vm_stack) = zend_vm_stack_new_page(page_size, NULL);
  176. EG(vm_stack_top) = EG(vm_stack)->top;
  177. EG(vm_stack_end) = EG(vm_stack)->end;
  178. }
  179. ZEND_API void zend_vm_stack_destroy(void)
  180. {
  181. zend_vm_stack stack = EG(vm_stack);
  182. while (stack != NULL) {
  183. zend_vm_stack p = stack->prev;
  184. efree(stack);
  185. stack = p;
  186. }
  187. }
  188. ZEND_API void* zend_vm_stack_extend(size_t size)
  189. {
  190. zend_vm_stack stack;
  191. void *ptr;
  192. stack = EG(vm_stack);
  193. stack->top = EG(vm_stack_top);
  194. EG(vm_stack) = stack = zend_vm_stack_new_page(
  195. EXPECTED(size < EG(vm_stack_page_size) - (ZEND_VM_STACK_HEADER_SLOTS * sizeof(zval))) ?
  196. EG(vm_stack_page_size) : ZEND_VM_STACK_PAGE_ALIGNED_SIZE(size, EG(vm_stack_page_size)),
  197. stack);
  198. ptr = stack->top;
  199. EG(vm_stack_top) = (void*)(((char*)ptr) + size);
  200. EG(vm_stack_end) = stack->end;
  201. return ptr;
  202. }
  203. ZEND_API zval* zend_get_compiled_variable_value(const zend_execute_data *execute_data, uint32_t var)
  204. {
  205. return EX_VAR(var);
  206. }
  207. static zend_always_inline zval *_get_zval_ptr_tmp(uint32_t var EXECUTE_DATA_DC)
  208. {
  209. zval *ret = EX_VAR(var);
  210. ZEND_ASSERT(Z_TYPE_P(ret) != IS_REFERENCE);
  211. return ret;
  212. }
  213. static zend_always_inline zval *_get_zval_ptr_var(uint32_t var EXECUTE_DATA_DC)
  214. {
  215. zval *ret = EX_VAR(var);
  216. return ret;
  217. }
  218. static zend_always_inline zval *_get_zval_ptr_var_deref(uint32_t var EXECUTE_DATA_DC)
  219. {
  220. zval *ret = EX_VAR(var);
  221. ZVAL_DEREF(ret);
  222. return ret;
  223. }
  224. static zend_never_inline ZEND_COLD zval* zval_undefined_cv(uint32_t var EXECUTE_DATA_DC)
  225. {
  226. if (EXPECTED(EG(exception) == NULL)) {
  227. zend_string *cv = CV_DEF_OF(EX_VAR_TO_NUM(var));
  228. zend_error(E_WARNING, "Undefined variable $%s", ZSTR_VAL(cv));
  229. }
  230. return &EG(uninitialized_zval);
  231. }
  232. static zend_never_inline ZEND_COLD zval* ZEND_FASTCALL _zval_undefined_op1(EXECUTE_DATA_D)
  233. {
  234. return zval_undefined_cv(EX(opline)->op1.var EXECUTE_DATA_CC);
  235. }
  236. static zend_never_inline ZEND_COLD zval* ZEND_FASTCALL _zval_undefined_op2(EXECUTE_DATA_D)
  237. {
  238. return zval_undefined_cv(EX(opline)->op2.var EXECUTE_DATA_CC);
  239. }
  240. #define ZVAL_UNDEFINED_OP1() _zval_undefined_op1(EXECUTE_DATA_C)
  241. #define ZVAL_UNDEFINED_OP2() _zval_undefined_op2(EXECUTE_DATA_C)
  242. static zend_never_inline ZEND_COLD zval *_get_zval_cv_lookup(zval *ptr, uint32_t var, int type EXECUTE_DATA_DC)
  243. {
  244. switch (type) {
  245. case BP_VAR_R:
  246. case BP_VAR_UNSET:
  247. ptr = zval_undefined_cv(var EXECUTE_DATA_CC);
  248. break;
  249. case BP_VAR_IS:
  250. ptr = &EG(uninitialized_zval);
  251. break;
  252. case BP_VAR_RW:
  253. zval_undefined_cv(var EXECUTE_DATA_CC);
  254. /* break missing intentionally */
  255. case BP_VAR_W:
  256. ZVAL_NULL(ptr);
  257. break;
  258. }
  259. return ptr;
  260. }
  261. static zend_always_inline zval *_get_zval_ptr_cv(uint32_t var, int type EXECUTE_DATA_DC)
  262. {
  263. zval *ret = EX_VAR(var);
  264. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  265. if (type == BP_VAR_W) {
  266. ZVAL_NULL(ret);
  267. } else {
  268. return _get_zval_cv_lookup(ret, var, type EXECUTE_DATA_CC);
  269. }
  270. }
  271. return ret;
  272. }
  273. static zend_always_inline zval *_get_zval_ptr_cv_deref(uint32_t var, int type EXECUTE_DATA_DC)
  274. {
  275. zval *ret = EX_VAR(var);
  276. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  277. if (type == BP_VAR_W) {
  278. ZVAL_NULL(ret);
  279. return ret;
  280. } else {
  281. return _get_zval_cv_lookup(ret, var, type EXECUTE_DATA_CC);
  282. }
  283. }
  284. ZVAL_DEREF(ret);
  285. return ret;
  286. }
  287. static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_R(uint32_t var EXECUTE_DATA_DC)
  288. {
  289. zval *ret = EX_VAR(var);
  290. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  291. return zval_undefined_cv(var EXECUTE_DATA_CC);
  292. }
  293. return ret;
  294. }
  295. static zend_always_inline zval *_get_zval_ptr_cv_deref_BP_VAR_R(uint32_t var EXECUTE_DATA_DC)
  296. {
  297. zval *ret = EX_VAR(var);
  298. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  299. return zval_undefined_cv(var EXECUTE_DATA_CC);
  300. }
  301. ZVAL_DEREF(ret);
  302. return ret;
  303. }
  304. static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_IS(uint32_t var EXECUTE_DATA_DC)
  305. {
  306. zval *ret = EX_VAR(var);
  307. return ret;
  308. }
  309. static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_RW(uint32_t var EXECUTE_DATA_DC)
  310. {
  311. zval *ret = EX_VAR(var);
  312. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  313. ZVAL_NULL(ret);
  314. zval_undefined_cv(var EXECUTE_DATA_CC);
  315. return ret;
  316. }
  317. return ret;
  318. }
  319. static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_W(uint32_t var EXECUTE_DATA_DC)
  320. {
  321. zval *ret = EX_VAR(var);
  322. if (Z_TYPE_P(ret) == IS_UNDEF) {
  323. ZVAL_NULL(ret);
  324. }
  325. return ret;
  326. }
  327. static zend_always_inline zval *_get_zval_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC)
  328. {
  329. if (op_type & (IS_TMP_VAR|IS_VAR)) {
  330. if (!ZEND_DEBUG || op_type == IS_VAR) {
  331. return _get_zval_ptr_var(node.var EXECUTE_DATA_CC);
  332. } else {
  333. ZEND_ASSERT(op_type == IS_TMP_VAR);
  334. return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
  335. }
  336. } else {
  337. if (op_type == IS_CONST) {
  338. return RT_CONSTANT(opline, node);
  339. } else if (op_type == IS_CV) {
  340. return _get_zval_ptr_cv(node.var, type EXECUTE_DATA_CC);
  341. } else {
  342. return NULL;
  343. }
  344. }
  345. }
  346. static zend_always_inline zval *_get_op_data_zval_ptr_r(int op_type, znode_op node EXECUTE_DATA_DC OPLINE_DC)
  347. {
  348. if (op_type & (IS_TMP_VAR|IS_VAR)) {
  349. if (!ZEND_DEBUG || op_type == IS_VAR) {
  350. return _get_zval_ptr_var(node.var EXECUTE_DATA_CC);
  351. } else {
  352. ZEND_ASSERT(op_type == IS_TMP_VAR);
  353. return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
  354. }
  355. } else {
  356. if (op_type == IS_CONST) {
  357. return RT_CONSTANT(opline + 1, node);
  358. } else if (op_type == IS_CV) {
  359. return _get_zval_ptr_cv_BP_VAR_R(node.var EXECUTE_DATA_CC);
  360. } else {
  361. return NULL;
  362. }
  363. }
  364. }
  365. static zend_always_inline ZEND_ATTRIBUTE_UNUSED zval *_get_zval_ptr_deref(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC)
  366. {
  367. if (op_type & (IS_TMP_VAR|IS_VAR)) {
  368. if (op_type == IS_TMP_VAR) {
  369. return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
  370. } else {
  371. ZEND_ASSERT(op_type == IS_VAR);
  372. return _get_zval_ptr_var_deref(node.var EXECUTE_DATA_CC);
  373. }
  374. } else {
  375. if (op_type == IS_CONST) {
  376. return RT_CONSTANT(opline, node);
  377. } else if (op_type == IS_CV) {
  378. return _get_zval_ptr_cv_deref(node.var, type EXECUTE_DATA_CC);
  379. } else {
  380. return NULL;
  381. }
  382. }
  383. }
  384. static zend_always_inline ZEND_ATTRIBUTE_UNUSED zval *_get_op_data_zval_ptr_deref_r(int op_type, znode_op node EXECUTE_DATA_DC OPLINE_DC)
  385. {
  386. if (op_type & (IS_TMP_VAR|IS_VAR)) {
  387. if (op_type == IS_TMP_VAR) {
  388. return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
  389. } else {
  390. ZEND_ASSERT(op_type == IS_VAR);
  391. return _get_zval_ptr_var_deref(node.var EXECUTE_DATA_CC);
  392. }
  393. } else {
  394. if (op_type == IS_CONST) {
  395. return RT_CONSTANT(opline + 1, node);
  396. } else if (op_type == IS_CV) {
  397. return _get_zval_ptr_cv_deref_BP_VAR_R(node.var EXECUTE_DATA_CC);
  398. } else {
  399. return NULL;
  400. }
  401. }
  402. }
  403. static zend_always_inline zval *_get_zval_ptr_undef(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC)
  404. {
  405. if (op_type & (IS_TMP_VAR|IS_VAR)) {
  406. if (!ZEND_DEBUG || op_type == IS_VAR) {
  407. return _get_zval_ptr_var(node.var EXECUTE_DATA_CC);
  408. } else {
  409. ZEND_ASSERT(op_type == IS_TMP_VAR);
  410. return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
  411. }
  412. } else {
  413. if (op_type == IS_CONST) {
  414. return RT_CONSTANT(opline, node);
  415. } else if (op_type == IS_CV) {
  416. return EX_VAR(node.var);
  417. } else {
  418. return NULL;
  419. }
  420. }
  421. }
  422. static zend_always_inline zval *_get_zval_ptr_ptr_var(uint32_t var EXECUTE_DATA_DC)
  423. {
  424. zval *ret = EX_VAR(var);
  425. if (EXPECTED(Z_TYPE_P(ret) == IS_INDIRECT)) {
  426. ret = Z_INDIRECT_P(ret);
  427. }
  428. return ret;
  429. }
  430. static inline zval *_get_zval_ptr_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC)
  431. {
  432. if (op_type == IS_CV) {
  433. return _get_zval_ptr_cv(node.var, type EXECUTE_DATA_CC);
  434. } else /* if (op_type == IS_VAR) */ {
  435. ZEND_ASSERT(op_type == IS_VAR);
  436. return _get_zval_ptr_ptr_var(node.var EXECUTE_DATA_CC);
  437. }
  438. }
  439. static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr(int op_type, znode_op op, int type EXECUTE_DATA_DC OPLINE_DC)
  440. {
  441. if (op_type == IS_UNUSED) {
  442. return &EX(This);
  443. }
  444. return get_zval_ptr(op_type, op, type);
  445. }
  446. static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_undef(int op_type, znode_op op, int type EXECUTE_DATA_DC OPLINE_DC)
  447. {
  448. if (op_type == IS_UNUSED) {
  449. return &EX(This);
  450. }
  451. return get_zval_ptr_undef(op_type, op, type);
  452. }
  453. static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC)
  454. {
  455. if (op_type == IS_UNUSED) {
  456. return &EX(This);
  457. }
  458. return get_zval_ptr_ptr(op_type, node, type);
  459. }
  460. static inline void zend_assign_to_variable_reference(zval *variable_ptr, zval *value_ptr)
  461. {
  462. zend_reference *ref;
  463. if (EXPECTED(!Z_ISREF_P(value_ptr))) {
  464. ZVAL_NEW_REF(value_ptr, value_ptr);
  465. } else if (UNEXPECTED(variable_ptr == value_ptr)) {
  466. return;
  467. }
  468. ref = Z_REF_P(value_ptr);
  469. GC_ADDREF(ref);
  470. if (Z_REFCOUNTED_P(variable_ptr)) {
  471. zend_refcounted *garbage = Z_COUNTED_P(variable_ptr);
  472. if (GC_DELREF(garbage) == 0) {
  473. ZVAL_REF(variable_ptr, ref);
  474. rc_dtor_func(garbage);
  475. return;
  476. } else {
  477. gc_check_possible_root(garbage);
  478. }
  479. }
  480. ZVAL_REF(variable_ptr, ref);
  481. }
  482. static zend_never_inline zval* zend_assign_to_typed_property_reference(zend_property_info *prop_info, zval *prop, zval *value_ptr EXECUTE_DATA_DC)
  483. {
  484. if (!zend_verify_prop_assignable_by_ref(prop_info, value_ptr, EX_USES_STRICT_TYPES())) {
  485. return &EG(uninitialized_zval);
  486. }
  487. if (Z_ISREF_P(prop)) {
  488. ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(prop), prop_info);
  489. }
  490. zend_assign_to_variable_reference(prop, value_ptr);
  491. ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(prop), prop_info);
  492. return prop;
  493. }
  494. static zend_never_inline ZEND_COLD int zend_wrong_assign_to_variable_reference(zval *variable_ptr, zval *value_ptr OPLINE_DC EXECUTE_DATA_DC)
  495. {
  496. zend_error(E_NOTICE, "Only variables should be assigned by reference");
  497. if (UNEXPECTED(EG(exception) != NULL)) {
  498. return 0;
  499. }
  500. /* Use IS_TMP_VAR instead of IS_VAR to avoid ISREF check */
  501. Z_TRY_ADDREF_P(value_ptr);
  502. value_ptr = zend_assign_to_variable(variable_ptr, value_ptr, IS_TMP_VAR, EX_USES_STRICT_TYPES());
  503. return 1;
  504. }
  505. static zend_never_inline ZEND_COLD void zend_throw_auto_init_in_prop_error(zend_property_info *prop, const char *type) {
  506. zend_string *type_str = zend_type_to_string(prop->type);
  507. zend_type_error(
  508. "Cannot auto-initialize an %s inside property %s::$%s of type %s",
  509. type,
  510. ZSTR_VAL(prop->ce->name), zend_get_unmangled_property_name(prop->name),
  511. ZSTR_VAL(type_str)
  512. );
  513. zend_string_release(type_str);
  514. }
  515. static zend_never_inline ZEND_COLD void zend_throw_auto_init_in_ref_error(zend_property_info *prop, const char *type) {
  516. zend_string *type_str = zend_type_to_string(prop->type);
  517. zend_type_error(
  518. "Cannot auto-initialize an %s inside a reference held by property %s::$%s of type %s",
  519. type,
  520. ZSTR_VAL(prop->ce->name), zend_get_unmangled_property_name(prop->name),
  521. ZSTR_VAL(type_str)
  522. );
  523. zend_string_release(type_str);
  524. }
  525. static zend_never_inline ZEND_COLD void zend_throw_access_uninit_prop_by_ref_error(
  526. zend_property_info *prop) {
  527. zend_throw_error(NULL,
  528. "Cannot access uninitialized non-nullable property %s::$%s by reference",
  529. ZSTR_VAL(prop->ce->name),
  530. zend_get_unmangled_property_name(prop->name));
  531. }
  532. /* this should modify object only if it's empty */
  533. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_throw_non_object_error(zval *object, zval *property OPLINE_DC EXECUTE_DATA_DC)
  534. {
  535. zend_string *tmp_property_name;
  536. zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name);
  537. if (opline->opcode == ZEND_PRE_INC_OBJ
  538. || opline->opcode == ZEND_PRE_DEC_OBJ
  539. || opline->opcode == ZEND_POST_INC_OBJ
  540. || opline->opcode == ZEND_POST_DEC_OBJ) {
  541. zend_throw_error(NULL,
  542. "Attempt to increment/decrement property '%s' of non-object",
  543. ZSTR_VAL(property_name));
  544. } else if (opline->opcode == ZEND_FETCH_OBJ_W
  545. || opline->opcode == ZEND_FETCH_OBJ_RW
  546. || opline->opcode == ZEND_FETCH_OBJ_FUNC_ARG
  547. || opline->opcode == ZEND_ASSIGN_OBJ_REF) {
  548. zend_throw_error(NULL,
  549. "Attempt to modify property '%s' of non-object", ZSTR_VAL(property_name));
  550. } else {
  551. zend_throw_error(NULL,
  552. "Attempt to assign property '%s' of non-object", ZSTR_VAL(property_name));
  553. }
  554. zend_tmp_string_release(tmp_property_name);
  555. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  556. ZVAL_NULL(EX_VAR(opline->result.var));
  557. }
  558. }
  559. static ZEND_COLD void zend_verify_type_error_common(
  560. const zend_function *zf, const zend_arg_info *arg_info,
  561. void **cache_slot, zval *value,
  562. const char **fname, const char **fsep, const char **fclass,
  563. zend_string **need_msg, const char **given_kind)
  564. {
  565. *fname = ZSTR_VAL(zf->common.function_name);
  566. if (zf->common.scope) {
  567. *fsep = "::";
  568. *fclass = ZSTR_VAL(zf->common.scope->name);
  569. } else {
  570. *fsep = "";
  571. *fclass = "";
  572. }
  573. *need_msg = zend_type_to_string_resolved(arg_info->type, zf->common.scope);
  574. if (value) {
  575. zend_bool has_class = ZEND_TYPE_HAS_CLASS(arg_info->type)
  576. || (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_STATIC);
  577. if (has_class && Z_TYPE_P(value) == IS_OBJECT) {
  578. *given_kind = ZSTR_VAL(Z_OBJCE_P(value)->name);
  579. } else {
  580. *given_kind = zend_zval_type_name(value);
  581. }
  582. } else {
  583. *given_kind = "none";
  584. }
  585. }
  586. ZEND_API ZEND_COLD void zend_verify_arg_error(
  587. const zend_function *zf, const zend_arg_info *arg_info,
  588. int arg_num, void **cache_slot, zval *value)
  589. {
  590. zend_execute_data *ptr = EG(current_execute_data)->prev_execute_data;
  591. const char *fname, *fsep, *fclass;
  592. zend_string *need_msg;
  593. const char *given_msg;
  594. if (EG(exception)) {
  595. /* The type verification itself might have already thrown an exception
  596. * through a promoted warning. */
  597. return;
  598. }
  599. if (value) {
  600. zend_verify_type_error_common(
  601. zf, arg_info, cache_slot, value,
  602. &fname, &fsep, &fclass, &need_msg, &given_msg);
  603. if (zf->common.type == ZEND_USER_FUNCTION) {
  604. if (ptr && ptr->func && ZEND_USER_CODE(ptr->func->common.type)) {
  605. zend_type_error("%s%s%s(): Argument #%d ($%s) must be of type %s, %s given, called in %s on line %d",
  606. fclass, fsep, fname,
  607. arg_num, ZSTR_VAL(arg_info->name),
  608. ZSTR_VAL(need_msg), given_msg,
  609. ZSTR_VAL(ptr->func->op_array.filename), ptr->opline->lineno
  610. );
  611. } else {
  612. zend_type_error("%s%s%s(): Argument #%d ($%s) must be of type %s, %s given",
  613. fclass, fsep, fname, arg_num, ZSTR_VAL(arg_info->name), ZSTR_VAL(need_msg), given_msg
  614. );
  615. }
  616. } else {
  617. zend_type_error("%s%s%s(): Argument #%d ($%s) must be of type %s, %s given",
  618. fclass, fsep, fname, arg_num, ((zend_internal_arg_info*) arg_info)->name, ZSTR_VAL(need_msg), given_msg
  619. );
  620. }
  621. zend_string_release(need_msg);
  622. } else {
  623. zend_missing_arg_error(ptr);
  624. }
  625. }
  626. static zend_bool zend_verify_weak_scalar_type_hint(uint32_t type_mask, zval *arg)
  627. {
  628. zend_long lval;
  629. double dval;
  630. zend_string *str;
  631. zend_bool bval;
  632. /* Type preference order: int -> float -> string -> bool */
  633. if (type_mask & MAY_BE_LONG) {
  634. /* For an int|float union type and string value,
  635. * determine chosen type by is_numeric_string() semantics. */
  636. if ((type_mask & MAY_BE_DOUBLE) && Z_TYPE_P(arg) == IS_STRING) {
  637. zend_uchar type = is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), &lval, &dval, -1);
  638. if (type == IS_LONG) {
  639. zend_string_release(Z_STR_P(arg));
  640. ZVAL_LONG(arg, lval);
  641. return 1;
  642. }
  643. if (type == IS_DOUBLE) {
  644. zend_string_release(Z_STR_P(arg));
  645. ZVAL_DOUBLE(arg, dval);
  646. return 1;
  647. }
  648. } else if (zend_parse_arg_long_weak(arg, &lval)) {
  649. zval_ptr_dtor(arg);
  650. ZVAL_LONG(arg, lval);
  651. return 1;
  652. }
  653. }
  654. if ((type_mask & MAY_BE_DOUBLE) && zend_parse_arg_double_weak(arg, &dval)) {
  655. zval_ptr_dtor(arg);
  656. ZVAL_DOUBLE(arg, dval);
  657. return 1;
  658. }
  659. if ((type_mask & MAY_BE_STRING) && zend_parse_arg_str_weak(arg, &str)) {
  660. /* on success "arg" is converted to IS_STRING */
  661. return 1;
  662. }
  663. if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL && zend_parse_arg_bool_weak(arg, &bval)) {
  664. zval_ptr_dtor(arg);
  665. ZVAL_BOOL(arg, bval);
  666. return 1;
  667. }
  668. return 0;
  669. }
  670. #if ZEND_DEBUG
  671. /* Used to sanity-check internal arginfo types without performing any actual type conversions. */
  672. static zend_bool zend_verify_weak_scalar_type_hint_no_sideeffect(uint32_t type_mask, zval *arg)
  673. {
  674. zend_long lval;
  675. double dval;
  676. zend_bool bval;
  677. if (type_mask & MAY_BE_LONG) {
  678. if (Z_TYPE_P(arg) == IS_STRING) {
  679. /* Handle this case separately to avoid the "non well-formed" warning */
  680. zend_uchar type = is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, &dval, 1);
  681. if (type == IS_LONG) {
  682. return 1;
  683. }
  684. if (type == IS_DOUBLE) {
  685. if ((type_mask & MAY_BE_DOUBLE)
  686. || (!zend_isnan(dval) && ZEND_DOUBLE_FITS_LONG(dval))) {
  687. return 1;
  688. }
  689. }
  690. }
  691. if (zend_parse_arg_long_weak(arg, &lval)) {
  692. return 1;
  693. }
  694. }
  695. if (type_mask & MAY_BE_DOUBLE) {
  696. if (Z_TYPE_P(arg) == IS_STRING) {
  697. /* Handle this case separately to avoid the "non well-formed" warning */
  698. if (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, NULL, 1) != 0) {
  699. return 1;
  700. }
  701. }
  702. if (zend_parse_arg_double_weak(arg, &dval)) {
  703. return 1;
  704. }
  705. }
  706. /* We don't call cast_object here, because this check must be side-effect free. As this
  707. * is only used for a sanity check of arginfo/zpp consistency, it's okay if we accept
  708. * more than actually allowed here. */
  709. if ((type_mask & MAY_BE_STRING) && (Z_TYPE_P(arg) < IS_STRING || Z_TYPE_P(arg) == IS_OBJECT)) {
  710. return 1;
  711. }
  712. if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL && zend_parse_arg_bool_weak(arg, &bval)) {
  713. return 1;
  714. }
  715. return 0;
  716. }
  717. #endif
  718. ZEND_API zend_bool zend_verify_scalar_type_hint(uint32_t type_mask, zval *arg, zend_bool strict, zend_bool is_internal_arg)
  719. {
  720. if (UNEXPECTED(strict)) {
  721. /* SSTH Exception: IS_LONG may be accepted as IS_DOUBLE (converted) */
  722. if (!(type_mask & MAY_BE_DOUBLE) || Z_TYPE_P(arg) != IS_LONG) {
  723. return 0;
  724. }
  725. } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
  726. /* NULL may be accepted only by nullable hints (this is already checked).
  727. * As an exception for internal functions, null is allowed for scalar types in weak mode. */
  728. return is_internal_arg
  729. && (type_mask & (MAY_BE_TRUE|MAY_BE_FALSE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING));
  730. }
  731. #if ZEND_DEBUG
  732. if (is_internal_arg) {
  733. return zend_verify_weak_scalar_type_hint_no_sideeffect(type_mask, arg);
  734. }
  735. #endif
  736. return zend_verify_weak_scalar_type_hint(type_mask, arg);
  737. }
  738. ZEND_COLD zend_never_inline void zend_verify_property_type_error(zend_property_info *info, zval *property)
  739. {
  740. zend_string *type_str;
  741. /* we _may_ land here in case reading already errored and runtime cache thus has not been updated (i.e. it contains a valid but unrelated info) */
  742. if (EG(exception)) {
  743. return;
  744. }
  745. type_str = zend_type_to_string(info->type);
  746. zend_type_error("Cannot assign %s to property %s::$%s of type %s",
  747. Z_TYPE_P(property) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(property)->name) : zend_get_type_by_const(Z_TYPE_P(property)),
  748. ZSTR_VAL(info->ce->name),
  749. zend_get_unmangled_property_name(info->name),
  750. ZSTR_VAL(type_str));
  751. zend_string_release(type_str);
  752. }
  753. static zend_class_entry *resolve_single_class_type(zend_string *name, zend_class_entry *self_ce) {
  754. if (zend_string_equals_literal_ci(name, "self")) {
  755. /* We need to explicitly check for this here, to avoid updating the type in the trait and
  756. * later using the wrong "self" when the trait is used in a class. */
  757. if (UNEXPECTED((self_ce->ce_flags & ZEND_ACC_TRAIT) != 0)) {
  758. return NULL;
  759. }
  760. return self_ce;
  761. } else if (zend_string_equals_literal_ci(name, "parent")) {
  762. return self_ce->parent;
  763. } else {
  764. return zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
  765. }
  766. }
  767. static zend_bool zend_check_and_resolve_property_class_type(
  768. zend_property_info *info, zend_class_entry *object_ce) {
  769. zend_class_entry *ce;
  770. if (ZEND_TYPE_HAS_LIST(info->type)) {
  771. zend_type *list_type;
  772. ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(info->type), list_type) {
  773. if (ZEND_TYPE_HAS_NAME(*list_type)) {
  774. zend_string *name = ZEND_TYPE_NAME(*list_type);
  775. ce = resolve_single_class_type(name, info->ce);
  776. if (!ce) {
  777. continue;
  778. }
  779. zend_string_release(name);
  780. ZEND_TYPE_SET_CE(*list_type, ce);
  781. } else {
  782. ce = ZEND_TYPE_CE(*list_type);
  783. }
  784. if (instanceof_function(object_ce, ce)) {
  785. return 1;
  786. }
  787. } ZEND_TYPE_LIST_FOREACH_END();
  788. return 0;
  789. } else {
  790. if (UNEXPECTED(ZEND_TYPE_HAS_NAME(info->type))) {
  791. zend_string *name = ZEND_TYPE_NAME(info->type);
  792. ce = resolve_single_class_type(name, info->ce);
  793. if (UNEXPECTED(!ce)) {
  794. return 0;
  795. }
  796. zend_string_release(name);
  797. ZEND_TYPE_SET_CE(info->type, ce);
  798. } else {
  799. ce = ZEND_TYPE_CE(info->type);
  800. }
  801. return instanceof_function(object_ce, ce);
  802. }
  803. }
  804. static zend_always_inline zend_bool i_zend_check_property_type(zend_property_info *info, zval *property, zend_bool strict)
  805. {
  806. ZEND_ASSERT(!Z_ISREF_P(property));
  807. if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(info->type, Z_TYPE_P(property)))) {
  808. return 1;
  809. }
  810. if (ZEND_TYPE_HAS_CLASS(info->type) && Z_TYPE_P(property) == IS_OBJECT
  811. && zend_check_and_resolve_property_class_type(info, Z_OBJCE_P(property))) {
  812. return 1;
  813. }
  814. uint32_t type_mask = ZEND_TYPE_FULL_MASK(info->type);
  815. ZEND_ASSERT(!(type_mask & (MAY_BE_CALLABLE|MAY_BE_STATIC)));
  816. if ((type_mask & MAY_BE_ITERABLE) && zend_is_iterable(property)) {
  817. return 1;
  818. }
  819. return zend_verify_scalar_type_hint(type_mask, property, strict, 0);
  820. }
  821. static zend_always_inline zend_bool i_zend_verify_property_type(zend_property_info *info, zval *property, zend_bool strict)
  822. {
  823. if (i_zend_check_property_type(info, property, strict)) {
  824. return 1;
  825. }
  826. zend_verify_property_type_error(info, property);
  827. return 0;
  828. }
  829. zend_bool zend_never_inline zend_verify_property_type(zend_property_info *info, zval *property, zend_bool strict) {
  830. return i_zend_verify_property_type(info, property, strict);
  831. }
  832. static zend_never_inline zval* zend_assign_to_typed_prop(zend_property_info *info, zval *property_val, zval *value EXECUTE_DATA_DC)
  833. {
  834. zval tmp;
  835. ZVAL_DEREF(value);
  836. ZVAL_COPY(&tmp, value);
  837. if (UNEXPECTED(!i_zend_verify_property_type(info, &tmp, EX_USES_STRICT_TYPES()))) {
  838. zval_ptr_dtor(&tmp);
  839. return &EG(uninitialized_zval);
  840. }
  841. return zend_assign_to_variable(property_val, &tmp, IS_TMP_VAR, EX_USES_STRICT_TYPES());
  842. }
  843. ZEND_API zend_bool zend_value_instanceof_static(zval *zv) {
  844. if (Z_TYPE_P(zv) != IS_OBJECT) {
  845. return 0;
  846. }
  847. zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
  848. if (!called_scope) {
  849. return 0;
  850. }
  851. return instanceof_function(Z_OBJCE_P(zv), called_scope);
  852. }
  853. static zend_always_inline zend_bool zend_check_type_slow(
  854. zend_type type, zval *arg, zend_reference *ref, void **cache_slot, zend_class_entry *scope,
  855. zend_bool is_return_type, zend_bool is_internal)
  856. {
  857. uint32_t type_mask;
  858. if (ZEND_TYPE_HAS_CLASS(type) && Z_TYPE_P(arg) == IS_OBJECT) {
  859. zend_class_entry *ce;
  860. if (ZEND_TYPE_HAS_LIST(type)) {
  861. zend_type *list_type;
  862. ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
  863. if (*cache_slot) {
  864. ce = *cache_slot;
  865. } else {
  866. ce = zend_fetch_class(ZEND_TYPE_NAME(*list_type),
  867. (ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_NO_AUTOLOAD));
  868. if (!ce) {
  869. cache_slot++;
  870. continue;
  871. }
  872. *cache_slot = ce;
  873. }
  874. if (instanceof_function(Z_OBJCE_P(arg), ce)) {
  875. return 1;
  876. }
  877. cache_slot++;
  878. } ZEND_TYPE_LIST_FOREACH_END();
  879. } else {
  880. if (EXPECTED(*cache_slot)) {
  881. ce = (zend_class_entry *) *cache_slot;
  882. } else {
  883. ce = zend_fetch_class(ZEND_TYPE_NAME(type), (ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_NO_AUTOLOAD));
  884. if (UNEXPECTED(!ce)) {
  885. goto builtin_types;
  886. }
  887. *cache_slot = (void *) ce;
  888. }
  889. if (instanceof_function(Z_OBJCE_P(arg), ce)) {
  890. return 1;
  891. }
  892. }
  893. }
  894. builtin_types:
  895. type_mask = ZEND_TYPE_FULL_MASK(type);
  896. if ((type_mask & MAY_BE_CALLABLE) && zend_is_callable(arg, 0, NULL)) {
  897. return 1;
  898. }
  899. if ((type_mask & MAY_BE_ITERABLE) && zend_is_iterable(arg)) {
  900. return 1;
  901. }
  902. if ((type_mask & MAY_BE_STATIC) && zend_value_instanceof_static(arg)) {
  903. return 1;
  904. }
  905. if (ref && ZEND_REF_HAS_TYPE_SOURCES(ref)) {
  906. /* We cannot have conversions for typed refs. */
  907. return 0;
  908. }
  909. if (is_internal && is_return_type) {
  910. /* For internal returns, the type has to match exactly, because we're not
  911. * going to check it for non-debug builds, and there will be no chance to
  912. * apply coercions. */
  913. return 0;
  914. }
  915. return zend_verify_scalar_type_hint(type_mask, arg,
  916. is_return_type ? ZEND_RET_USES_STRICT_TYPES() : ZEND_ARG_USES_STRICT_TYPES(),
  917. is_internal);
  918. /* Special handling for IS_VOID is not necessary (for return types),
  919. * because this case is already checked at compile-time. */
  920. }
  921. static zend_always_inline zend_bool zend_check_type(
  922. zend_type type, zval *arg, void **cache_slot, zend_class_entry *scope,
  923. zend_bool is_return_type, zend_bool is_internal)
  924. {
  925. zend_reference *ref = NULL;
  926. ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
  927. if (UNEXPECTED(Z_ISREF_P(arg))) {
  928. ref = Z_REF_P(arg);
  929. arg = Z_REFVAL_P(arg);
  930. }
  931. if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(arg)))) {
  932. return 1;
  933. }
  934. return zend_check_type_slow(type, arg, ref, cache_slot, scope, is_return_type, is_internal);
  935. }
  936. static zend_always_inline int zend_verify_recv_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, void **cache_slot)
  937. {
  938. zend_arg_info *cur_arg_info;
  939. ZEND_ASSERT(arg_num <= zf->common.num_args);
  940. cur_arg_info = &zf->common.arg_info[arg_num-1];
  941. if (ZEND_TYPE_IS_SET(cur_arg_info->type)
  942. && UNEXPECTED(!zend_check_type(cur_arg_info->type, arg, cache_slot, zf->common.scope, 0, 0))) {
  943. zend_verify_arg_error(zf, cur_arg_info, arg_num, cache_slot, arg);
  944. return 0;
  945. }
  946. return 1;
  947. }
  948. static zend_always_inline int zend_verify_variadic_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, void **cache_slot)
  949. {
  950. zend_arg_info *cur_arg_info;
  951. ZEND_ASSERT(arg_num > zf->common.num_args);
  952. ZEND_ASSERT(zf->common.fn_flags & ZEND_ACC_VARIADIC);
  953. cur_arg_info = &zf->common.arg_info[zf->common.num_args];
  954. if (ZEND_TYPE_IS_SET(cur_arg_info->type)
  955. && UNEXPECTED(!zend_check_type(cur_arg_info->type, arg, cache_slot, zf->common.scope, 0, 0))) {
  956. zend_verify_arg_error(zf, cur_arg_info, arg_num, cache_slot, arg);
  957. return 0;
  958. }
  959. return 1;
  960. }
  961. static zend_never_inline ZEND_ATTRIBUTE_UNUSED int zend_verify_internal_arg_types(zend_function *fbc, zend_execute_data *call)
  962. {
  963. uint32_t i;
  964. uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
  965. zval *arg = ZEND_CALL_ARG(call, 1);
  966. for (i = 0; i < num_args; ++i) {
  967. zend_arg_info *cur_arg_info;
  968. void *dummy_cache_slot = NULL;
  969. if (EXPECTED(i < fbc->common.num_args)) {
  970. cur_arg_info = &fbc->common.arg_info[i];
  971. } else if (UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
  972. cur_arg_info = &fbc->common.arg_info[fbc->common.num_args];
  973. } else {
  974. break;
  975. }
  976. if (ZEND_TYPE_IS_SET(cur_arg_info->type)
  977. && UNEXPECTED(!zend_check_type(cur_arg_info->type, arg, &dummy_cache_slot, fbc->common.scope, 0, /* is_internal */ 1))) {
  978. return 0;
  979. }
  980. arg++;
  981. }
  982. return 1;
  983. }
  984. #if ZEND_DEBUG
  985. /* Determine whether an internal call should throw, because the passed arguments violate
  986. * an arginfo constraint. This is only checked in debug builds. In release builds, we
  987. * trust that arginfo matches what is enforced by zend_parse_parameters. */
  988. static zend_always_inline zend_bool zend_internal_call_should_throw(zend_function *fbc, zend_execute_data *call)
  989. {
  990. if (fbc->common.required_num_args > ZEND_CALL_NUM_ARGS(call)) {
  991. return 1;
  992. }
  993. if ((fbc->common.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) &&
  994. !zend_verify_internal_arg_types(fbc, call)) {
  995. return 1;
  996. }
  997. return 0;
  998. }
  999. static ZEND_COLD void zend_internal_call_arginfo_violation(zend_function *fbc)
  1000. {
  1001. zend_error(E_CORE_ERROR, "Arginfo / zpp mismatch during call of %s%s%s()",
  1002. fbc->common.scope ? ZSTR_VAL(fbc->common.scope->name) : "",
  1003. fbc->common.scope ? "::" : "",
  1004. ZSTR_VAL(fbc->common.function_name));
  1005. }
  1006. #endif
  1007. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_missing_arg_error(zend_execute_data *execute_data)
  1008. {
  1009. zend_execute_data *ptr = EX(prev_execute_data);
  1010. if (ptr && ptr->func && ZEND_USER_CODE(ptr->func->common.type)) {
  1011. zend_throw_error(zend_ce_argument_count_error, "Too few arguments to function %s%s%s(), %d passed in %s on line %d and %s %d expected",
  1012. EX(func)->common.scope ? ZSTR_VAL(EX(func)->common.scope->name) : "",
  1013. EX(func)->common.scope ? "::" : "",
  1014. ZSTR_VAL(EX(func)->common.function_name),
  1015. EX_NUM_ARGS(),
  1016. ZSTR_VAL(ptr->func->op_array.filename),
  1017. ptr->opline->lineno,
  1018. EX(func)->common.required_num_args == EX(func)->common.num_args ? "exactly" : "at least",
  1019. EX(func)->common.required_num_args);
  1020. } else {
  1021. zend_throw_error(zend_ce_argument_count_error, "Too few arguments to function %s%s%s(), %d passed and %s %d expected",
  1022. EX(func)->common.scope ? ZSTR_VAL(EX(func)->common.scope->name) : "",
  1023. EX(func)->common.scope ? "::" : "",
  1024. ZSTR_VAL(EX(func)->common.function_name),
  1025. EX_NUM_ARGS(),
  1026. EX(func)->common.required_num_args == EX(func)->common.num_args ? "exactly" : "at least",
  1027. EX(func)->common.required_num_args);
  1028. }
  1029. }
  1030. ZEND_API ZEND_COLD void zend_verify_return_error(
  1031. const zend_function *zf, void **cache_slot, zval *value)
  1032. {
  1033. const zend_arg_info *arg_info = &zf->common.arg_info[-1];
  1034. const char *fname, *fsep, *fclass;
  1035. zend_string *need_msg;
  1036. const char *given_msg;
  1037. zend_verify_type_error_common(
  1038. zf, arg_info, cache_slot, value,
  1039. &fname, &fsep, &fclass, &need_msg, &given_msg);
  1040. zend_type_error("Return value of %s%s%s() must be of type %s, %s returned",
  1041. fclass, fsep, fname, ZSTR_VAL(need_msg), given_msg);
  1042. zend_string_release(need_msg);
  1043. }
  1044. #if ZEND_DEBUG
  1045. static ZEND_COLD void zend_verify_internal_return_error(
  1046. const zend_function *zf, void **cache_slot, zval *value)
  1047. {
  1048. const zend_arg_info *arg_info = &zf->common.arg_info[-1];
  1049. const char *fname, *fsep, *fclass;
  1050. zend_string *need_msg;
  1051. const char *given_msg;
  1052. zend_verify_type_error_common(
  1053. zf, arg_info, cache_slot, value,
  1054. &fname, &fsep, &fclass, &need_msg, &given_msg);
  1055. zend_error_noreturn(E_CORE_ERROR, "Return value of %s%s%s() must be of type %s, %s returned",
  1056. fclass, fsep, fname, ZSTR_VAL(need_msg), given_msg);
  1057. }
  1058. static ZEND_COLD void zend_verify_void_return_error(const zend_function *zf, const char *returned_msg, const char *returned_kind)
  1059. {
  1060. const char *fname = ZSTR_VAL(zf->common.function_name);
  1061. const char *fsep;
  1062. const char *fclass;
  1063. if (zf->common.scope) {
  1064. fsep = "::";
  1065. fclass = ZSTR_VAL(zf->common.scope->name);
  1066. } else {
  1067. fsep = "";
  1068. fclass = "";
  1069. }
  1070. zend_type_error("%s%s%s() must not return a value, %s%s returned",
  1071. fclass, fsep, fname, returned_msg, returned_kind);
  1072. }
  1073. static int zend_verify_internal_return_type(zend_function *zf, zval *ret)
  1074. {
  1075. zend_internal_arg_info *ret_info = zf->internal_function.arg_info - 1;
  1076. void *dummy_cache_slot = NULL;
  1077. if (ZEND_TYPE_FULL_MASK(ret_info->type) & MAY_BE_VOID) {
  1078. if (UNEXPECTED(Z_TYPE_P(ret) != IS_NULL)) {
  1079. zend_verify_void_return_error(zf, zend_zval_type_name(ret), "");
  1080. return 0;
  1081. }
  1082. return 1;
  1083. }
  1084. if (UNEXPECTED(!zend_check_type(ret_info->type, ret, &dummy_cache_slot, NULL, 1, /* is_internal */ 1))) {
  1085. zend_verify_internal_return_error(zf, &dummy_cache_slot, ret);
  1086. return 0;
  1087. }
  1088. return 1;
  1089. }
  1090. #endif
  1091. static ZEND_COLD int zend_verify_missing_return_type(const zend_function *zf, void **cache_slot)
  1092. {
  1093. /* VERIFY_RETURN_TYPE is not emitted for "void" functions, so this is always an error. */
  1094. zend_verify_return_error(zf, cache_slot, NULL);
  1095. return 0;
  1096. }
  1097. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_object_as_array(void)
  1098. {
  1099. zend_throw_error(NULL, "Cannot use object as array");
  1100. }
  1101. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_offset(void)
  1102. {
  1103. zend_type_error("Illegal offset type");
  1104. }
  1105. static zend_never_inline void zend_assign_to_object_dim(zval *object, zval *dim, zval *value OPLINE_DC EXECUTE_DATA_DC)
  1106. {
  1107. Z_OBJ_HT_P(object)->write_dimension(Z_OBJ_P(object), dim, value);
  1108. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1109. ZVAL_COPY(EX_VAR(opline->result.var), value);
  1110. }
  1111. }
  1112. static zend_always_inline int zend_binary_op(zval *ret, zval *op1, zval *op2 OPLINE_DC)
  1113. {
  1114. static const binary_op_type zend_binary_ops[] = {
  1115. add_function,
  1116. sub_function,
  1117. mul_function,
  1118. div_function,
  1119. mod_function,
  1120. shift_left_function,
  1121. shift_right_function,
  1122. concat_function,
  1123. bitwise_or_function,
  1124. bitwise_and_function,
  1125. bitwise_xor_function,
  1126. pow_function
  1127. };
  1128. /* size_t cast makes GCC to better optimize 64-bit PIC code */
  1129. size_t opcode = (size_t)opline->extended_value;
  1130. return zend_binary_ops[opcode - ZEND_ADD](ret, op1, op2);
  1131. }
  1132. static zend_never_inline void zend_binary_assign_op_obj_dim(zval *object, zval *property OPLINE_DC EXECUTE_DATA_DC)
  1133. {
  1134. zval *value;
  1135. zval *z;
  1136. zval rv, res;
  1137. value = get_op_data_zval_ptr_r((opline+1)->op1_type, (opline+1)->op1);
  1138. if ((z = Z_OBJ_HT_P(object)->read_dimension(Z_OBJ_P(object), property, BP_VAR_R, &rv)) != NULL) {
  1139. if (zend_binary_op(&res, z, value OPLINE_CC) == SUCCESS) {
  1140. Z_OBJ_HT_P(object)->write_dimension(Z_OBJ_P(object), property, &res);
  1141. }
  1142. if (z == &rv) {
  1143. zval_ptr_dtor(&rv);
  1144. }
  1145. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1146. ZVAL_COPY(EX_VAR(opline->result.var), &res);
  1147. }
  1148. zval_ptr_dtor(&res);
  1149. } else {
  1150. zend_use_object_as_array();
  1151. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1152. ZVAL_NULL(EX_VAR(opline->result.var));
  1153. }
  1154. }
  1155. FREE_OP((opline+1)->op1_type, (opline+1)->op1.var);
  1156. }
  1157. static zend_never_inline void zend_binary_assign_op_typed_ref(zend_reference *ref, zval *value OPLINE_DC EXECUTE_DATA_DC)
  1158. {
  1159. zval z_copy;
  1160. zend_binary_op(&z_copy, &ref->val, value OPLINE_CC);
  1161. if (EXPECTED(zend_verify_ref_assignable_zval(ref, &z_copy, EX_USES_STRICT_TYPES()))) {
  1162. zval_ptr_dtor(&ref->val);
  1163. ZVAL_COPY_VALUE(&ref->val, &z_copy);
  1164. } else {
  1165. zval_ptr_dtor(&z_copy);
  1166. }
  1167. }
  1168. static zend_never_inline void zend_binary_assign_op_typed_prop(zend_property_info *prop_info, zval *zptr, zval *value OPLINE_DC EXECUTE_DATA_DC)
  1169. {
  1170. zval z_copy;
  1171. zend_binary_op(&z_copy, zptr, value OPLINE_CC);
  1172. if (EXPECTED(zend_verify_property_type(prop_info, &z_copy, EX_USES_STRICT_TYPES()))) {
  1173. zval_ptr_dtor(zptr);
  1174. ZVAL_COPY_VALUE(zptr, &z_copy);
  1175. } else {
  1176. zval_ptr_dtor(&z_copy);
  1177. }
  1178. }
  1179. static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type EXECUTE_DATA_DC)
  1180. {
  1181. zend_long offset;
  1182. try_again:
  1183. if (UNEXPECTED(Z_TYPE_P(dim) != IS_LONG)) {
  1184. switch(Z_TYPE_P(dim)) {
  1185. case IS_STRING:
  1186. if (IS_LONG == is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), NULL, NULL, -1)) {
  1187. break;
  1188. }
  1189. if (type != BP_VAR_UNSET) {
  1190. zend_error(E_WARNING, "Illegal string offset '%s'", Z_STRVAL_P(dim));
  1191. }
  1192. break;
  1193. case IS_UNDEF:
  1194. ZVAL_UNDEFINED_OP2();
  1195. case IS_DOUBLE:
  1196. case IS_NULL:
  1197. case IS_FALSE:
  1198. case IS_TRUE:
  1199. zend_error(E_WARNING, "String offset cast occurred");
  1200. break;
  1201. case IS_REFERENCE:
  1202. dim = Z_REFVAL_P(dim);
  1203. goto try_again;
  1204. default:
  1205. zend_illegal_offset();
  1206. break;
  1207. }
  1208. offset = zval_get_long_func(dim);
  1209. } else {
  1210. offset = Z_LVAL_P(dim);
  1211. }
  1212. return offset;
  1213. }
  1214. static zend_never_inline ZEND_COLD void zend_wrong_string_offset(EXECUTE_DATA_D)
  1215. {
  1216. const char *msg = NULL;
  1217. const zend_op *opline = EX(opline);
  1218. const zend_op *end;
  1219. uint32_t var;
  1220. if (UNEXPECTED(EG(exception) != NULL)) {
  1221. return;
  1222. }
  1223. switch (opline->opcode) {
  1224. case ZEND_ASSIGN_OP:
  1225. case ZEND_ASSIGN_DIM_OP:
  1226. case ZEND_ASSIGN_OBJ_OP:
  1227. case ZEND_ASSIGN_STATIC_PROP_OP:
  1228. msg = "Cannot use assign-op operators with string offsets";
  1229. break;
  1230. case ZEND_FETCH_DIM_W:
  1231. case ZEND_FETCH_DIM_RW:
  1232. case ZEND_FETCH_DIM_FUNC_ARG:
  1233. case ZEND_FETCH_DIM_UNSET:
  1234. case ZEND_FETCH_LIST_W:
  1235. /* TODO: Encode the "reason" into opline->extended_value??? */
  1236. var = opline->result.var;
  1237. opline++;
  1238. end = EG(current_execute_data)->func->op_array.opcodes +
  1239. EG(current_execute_data)->func->op_array.last;
  1240. while (opline < end) {
  1241. if (opline->op1_type == IS_VAR && opline->op1.var == var) {
  1242. switch (opline->opcode) {
  1243. case ZEND_ASSIGN_OBJ_OP:
  1244. msg = "Cannot use string offset as an object";
  1245. break;
  1246. case ZEND_ASSIGN_DIM_OP:
  1247. msg = "Cannot use string offset as an array";
  1248. break;
  1249. case ZEND_ASSIGN_STATIC_PROP_OP:
  1250. case ZEND_ASSIGN_OP:
  1251. msg = "Cannot use assign-op operators with string offsets";
  1252. break;
  1253. case ZEND_PRE_INC_OBJ:
  1254. case ZEND_PRE_DEC_OBJ:
  1255. case ZEND_POST_INC_OBJ:
  1256. case ZEND_POST_DEC_OBJ:
  1257. case ZEND_PRE_INC:
  1258. case ZEND_PRE_DEC:
  1259. case ZEND_POST_INC:
  1260. case ZEND_POST_DEC:
  1261. msg = "Cannot increment/decrement string offsets";
  1262. break;
  1263. case ZEND_FETCH_DIM_W:
  1264. case ZEND_FETCH_DIM_RW:
  1265. case ZEND_FETCH_DIM_FUNC_ARG:
  1266. case ZEND_FETCH_DIM_UNSET:
  1267. case ZEND_FETCH_LIST_W:
  1268. case ZEND_ASSIGN_DIM:
  1269. msg = "Cannot use string offset as an array";
  1270. break;
  1271. case ZEND_FETCH_OBJ_W:
  1272. case ZEND_FETCH_OBJ_RW:
  1273. case ZEND_FETCH_OBJ_FUNC_ARG:
  1274. case ZEND_FETCH_OBJ_UNSET:
  1275. case ZEND_ASSIGN_OBJ:
  1276. msg = "Cannot use string offset as an object";
  1277. break;
  1278. case ZEND_ASSIGN_REF:
  1279. case ZEND_ADD_ARRAY_ELEMENT:
  1280. case ZEND_INIT_ARRAY:
  1281. case ZEND_MAKE_REF:
  1282. msg = "Cannot create references to/from string offsets";
  1283. break;
  1284. case ZEND_RETURN_BY_REF:
  1285. case ZEND_VERIFY_RETURN_TYPE:
  1286. msg = "Cannot return string offsets by reference";
  1287. break;
  1288. case ZEND_UNSET_DIM:
  1289. case ZEND_UNSET_OBJ:
  1290. msg = "Cannot unset string offsets";
  1291. break;
  1292. case ZEND_YIELD:
  1293. msg = "Cannot yield string offsets by reference";
  1294. break;
  1295. case ZEND_SEND_REF:
  1296. case ZEND_SEND_VAR_EX:
  1297. case ZEND_SEND_FUNC_ARG:
  1298. msg = "Only variables can be passed by reference";
  1299. break;
  1300. case ZEND_FE_RESET_RW:
  1301. msg = "Cannot iterate on string offsets by reference";
  1302. break;
  1303. EMPTY_SWITCH_DEFAULT_CASE();
  1304. }
  1305. break;
  1306. }
  1307. if (opline->op2_type == IS_VAR && opline->op2.var == var) {
  1308. ZEND_ASSERT(opline->opcode == ZEND_ASSIGN_REF);
  1309. msg = "Cannot create references to/from string offsets";
  1310. break;
  1311. }
  1312. }
  1313. break;
  1314. EMPTY_SWITCH_DEFAULT_CASE();
  1315. }
  1316. ZEND_ASSERT(msg != NULL);
  1317. zend_throw_error(NULL, "%s", msg);
  1318. }
  1319. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_wrong_property_read(zval *property)
  1320. {
  1321. zend_string *tmp_property_name;
  1322. zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name);
  1323. zend_error(E_WARNING, "Trying to get property '%s' of non-object", ZSTR_VAL(property_name));
  1324. zend_tmp_string_release(tmp_property_name);
  1325. }
  1326. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_deprecated_function(const zend_function *fbc)
  1327. {
  1328. zend_error(E_DEPRECATED, "Function %s%s%s() is deprecated",
  1329. fbc->common.scope ? ZSTR_VAL(fbc->common.scope->name) : "",
  1330. fbc->common.scope ? "::" : "",
  1331. ZSTR_VAL(fbc->common.function_name));
  1332. }
  1333. static zend_never_inline void zend_assign_to_string_offset(zval *str, zval *dim, zval *value OPLINE_DC EXECUTE_DATA_DC)
  1334. {
  1335. zend_uchar c;
  1336. size_t string_len;
  1337. zend_long offset;
  1338. offset = zend_check_string_offset(dim, BP_VAR_W EXECUTE_DATA_CC);
  1339. if (offset < -(zend_long)Z_STRLEN_P(str)) {
  1340. /* Error on negative offset */
  1341. zend_error(E_WARNING, "Illegal string offset: " ZEND_LONG_FMT, offset);
  1342. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1343. ZVAL_NULL(EX_VAR(opline->result.var));
  1344. }
  1345. return;
  1346. }
  1347. if (Z_TYPE_P(value) != IS_STRING) {
  1348. /* Convert to string, just the time to pick the 1st byte */
  1349. zend_string *tmp = zval_try_get_string_func(value);
  1350. if (UNEXPECTED(!tmp)) {
  1351. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1352. ZVAL_UNDEF(EX_VAR(opline->result.var));
  1353. }
  1354. return;
  1355. }
  1356. string_len = ZSTR_LEN(tmp);
  1357. c = (zend_uchar)ZSTR_VAL(tmp)[0];
  1358. zend_string_release_ex(tmp, 0);
  1359. } else {
  1360. string_len = Z_STRLEN_P(value);
  1361. c = (zend_uchar)Z_STRVAL_P(value)[0];
  1362. }
  1363. if (string_len != 1) {
  1364. if (string_len == 0) {
  1365. /* Error on empty input string */
  1366. zend_throw_error(NULL, "Cannot assign an empty string to a string offset");
  1367. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1368. ZVAL_NULL(EX_VAR(opline->result.var));
  1369. }
  1370. return;
  1371. }
  1372. zend_error(E_WARNING, "Only the first byte will be assigned to the string offset");
  1373. }
  1374. if (offset < 0) { /* Handle negative offset */
  1375. offset += (zend_long)Z_STRLEN_P(str);
  1376. }
  1377. if ((size_t)offset >= Z_STRLEN_P(str)) {
  1378. /* Extend string if needed */
  1379. zend_long old_len = Z_STRLEN_P(str);
  1380. ZVAL_NEW_STR(str, zend_string_extend(Z_STR_P(str), offset + 1, 0));
  1381. memset(Z_STRVAL_P(str) + old_len, ' ', offset - old_len);
  1382. Z_STRVAL_P(str)[offset+1] = 0;
  1383. } else if (!Z_REFCOUNTED_P(str)) {
  1384. ZVAL_NEW_STR(str, zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0));
  1385. } else if (Z_REFCOUNT_P(str) > 1) {
  1386. Z_DELREF_P(str);
  1387. ZVAL_NEW_STR(str, zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0));
  1388. } else {
  1389. zend_string_forget_hash_val(Z_STR_P(str));
  1390. }
  1391. Z_STRVAL_P(str)[offset] = c;
  1392. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1393. /* Return the new character */
  1394. ZVAL_INTERNED_STR(EX_VAR(opline->result.var), ZSTR_CHAR(c));
  1395. }
  1396. }
  1397. static zend_property_info *zend_get_prop_not_accepting_double(zend_reference *ref)
  1398. {
  1399. zend_property_info *prop;
  1400. ZEND_REF_FOREACH_TYPE_SOURCES(ref, prop) {
  1401. if (!(ZEND_TYPE_FULL_MASK(prop->type) & MAY_BE_DOUBLE)) {
  1402. return prop;
  1403. }
  1404. } ZEND_REF_FOREACH_TYPE_SOURCES_END();
  1405. return NULL;
  1406. }
  1407. static ZEND_COLD zend_long zend_throw_incdec_ref_error(
  1408. zend_reference *ref, zend_property_info *error_prop OPLINE_DC)
  1409. {
  1410. zend_string *type_str = zend_type_to_string(error_prop->type);
  1411. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1412. zend_type_error(
  1413. "Cannot increment a reference held by property %s::$%s of type %s past its maximal value",
  1414. ZSTR_VAL(error_prop->ce->name),
  1415. zend_get_unmangled_property_name(error_prop->name),
  1416. ZSTR_VAL(type_str));
  1417. zend_string_release(type_str);
  1418. return ZEND_LONG_MAX;
  1419. } else {
  1420. zend_type_error(
  1421. "Cannot decrement a reference held by property %s::$%s of type %s past its minimal value",
  1422. ZSTR_VAL(error_prop->ce->name),
  1423. zend_get_unmangled_property_name(error_prop->name),
  1424. ZSTR_VAL(type_str));
  1425. zend_string_release(type_str);
  1426. return ZEND_LONG_MIN;
  1427. }
  1428. }
  1429. static ZEND_COLD zend_long zend_throw_incdec_prop_error(zend_property_info *prop OPLINE_DC) {
  1430. zend_string *type_str = zend_type_to_string(prop->type);
  1431. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1432. zend_type_error("Cannot increment property %s::$%s of type %s past its maximal value",
  1433. ZSTR_VAL(prop->ce->name),
  1434. zend_get_unmangled_property_name(prop->name),
  1435. ZSTR_VAL(type_str));
  1436. zend_string_release(type_str);
  1437. return ZEND_LONG_MAX;
  1438. } else {
  1439. zend_type_error("Cannot decrement property %s::$%s of type %s past its minimal value",
  1440. ZSTR_VAL(prop->ce->name),
  1441. zend_get_unmangled_property_name(prop->name),
  1442. ZSTR_VAL(type_str));
  1443. zend_string_release(type_str);
  1444. return ZEND_LONG_MIN;
  1445. }
  1446. }
  1447. static void zend_incdec_typed_ref(zend_reference *ref, zval *copy OPLINE_DC EXECUTE_DATA_DC)
  1448. {
  1449. zval tmp;
  1450. zval *var_ptr = &ref->val;
  1451. if (!copy) {
  1452. copy = &tmp;
  1453. }
  1454. ZVAL_COPY(copy, var_ptr);
  1455. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1456. increment_function(var_ptr);
  1457. } else {
  1458. decrement_function(var_ptr);
  1459. }
  1460. if (UNEXPECTED(Z_TYPE_P(var_ptr) == IS_DOUBLE) && Z_TYPE_P(copy) == IS_LONG) {
  1461. zend_property_info *error_prop = zend_get_prop_not_accepting_double(ref);
  1462. if (UNEXPECTED(error_prop)) {
  1463. zend_long val = zend_throw_incdec_ref_error(ref, error_prop OPLINE_CC);
  1464. ZVAL_LONG(var_ptr, val);
  1465. }
  1466. } else if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, var_ptr, EX_USES_STRICT_TYPES()))) {
  1467. zval_ptr_dtor(var_ptr);
  1468. ZVAL_COPY_VALUE(var_ptr, copy);
  1469. ZVAL_UNDEF(copy);
  1470. } else if (copy == &tmp) {
  1471. zval_ptr_dtor(&tmp);
  1472. }
  1473. }
  1474. static void zend_incdec_typed_prop(zend_property_info *prop_info, zval *var_ptr, zval *copy OPLINE_DC EXECUTE_DATA_DC)
  1475. {
  1476. zval tmp;
  1477. if (!copy) {
  1478. copy = &tmp;
  1479. }
  1480. ZVAL_COPY(copy, var_ptr);
  1481. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1482. increment_function(var_ptr);
  1483. } else {
  1484. decrement_function(var_ptr);
  1485. }
  1486. if (UNEXPECTED(Z_TYPE_P(var_ptr) == IS_DOUBLE) && Z_TYPE_P(copy) == IS_LONG) {
  1487. if (!(ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_DOUBLE)) {
  1488. zend_long val = zend_throw_incdec_prop_error(prop_info OPLINE_CC);
  1489. ZVAL_LONG(var_ptr, val);
  1490. }
  1491. } else if (UNEXPECTED(!zend_verify_property_type(prop_info, var_ptr, EX_USES_STRICT_TYPES()))) {
  1492. zval_ptr_dtor(var_ptr);
  1493. ZVAL_COPY_VALUE(var_ptr, copy);
  1494. ZVAL_UNDEF(copy);
  1495. } else if (copy == &tmp) {
  1496. zval_ptr_dtor(&tmp);
  1497. }
  1498. }
  1499. static void zend_pre_incdec_property_zval(zval *prop, zend_property_info *prop_info OPLINE_DC EXECUTE_DATA_DC)
  1500. {
  1501. if (EXPECTED(Z_TYPE_P(prop) == IS_LONG)) {
  1502. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1503. fast_long_increment_function(prop);
  1504. } else {
  1505. fast_long_decrement_function(prop);
  1506. }
  1507. if (UNEXPECTED(Z_TYPE_P(prop) != IS_LONG) && UNEXPECTED(prop_info)
  1508. && !(ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_DOUBLE)) {
  1509. zend_long val = zend_throw_incdec_prop_error(prop_info OPLINE_CC);
  1510. ZVAL_LONG(prop, val);
  1511. }
  1512. } else {
  1513. do {
  1514. if (Z_ISREF_P(prop)) {
  1515. zend_reference *ref = Z_REF_P(prop);
  1516. prop = Z_REFVAL_P(prop);
  1517. if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) {
  1518. zend_incdec_typed_ref(ref, NULL OPLINE_CC EXECUTE_DATA_CC);
  1519. break;
  1520. }
  1521. }
  1522. if (UNEXPECTED(prop_info)) {
  1523. zend_incdec_typed_prop(prop_info, prop, NULL OPLINE_CC EXECUTE_DATA_CC);
  1524. } else if (ZEND_IS_INCREMENT(opline->opcode)) {
  1525. increment_function(prop);
  1526. } else {
  1527. decrement_function(prop);
  1528. }
  1529. } while (0);
  1530. }
  1531. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1532. ZVAL_COPY(EX_VAR(opline->result.var), prop);
  1533. }
  1534. }
  1535. static void zend_post_incdec_property_zval(zval *prop, zend_property_info *prop_info OPLINE_DC EXECUTE_DATA_DC)
  1536. {
  1537. if (EXPECTED(Z_TYPE_P(prop) == IS_LONG)) {
  1538. ZVAL_LONG(EX_VAR(opline->result.var), Z_LVAL_P(prop));
  1539. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1540. fast_long_increment_function(prop);
  1541. } else {
  1542. fast_long_decrement_function(prop);
  1543. }
  1544. if (UNEXPECTED(Z_TYPE_P(prop) != IS_LONG) && UNEXPECTED(prop_info)
  1545. && !(ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_DOUBLE)) {
  1546. zend_long val = zend_throw_incdec_prop_error(prop_info OPLINE_CC);
  1547. ZVAL_LONG(prop, val);
  1548. }
  1549. } else {
  1550. if (Z_ISREF_P(prop)) {
  1551. zend_reference *ref = Z_REF_P(prop);
  1552. prop = Z_REFVAL_P(prop);
  1553. if (ZEND_REF_HAS_TYPE_SOURCES(ref)) {
  1554. zend_incdec_typed_ref(ref, EX_VAR(opline->result.var) OPLINE_CC EXECUTE_DATA_CC);
  1555. return;
  1556. }
  1557. }
  1558. if (UNEXPECTED(prop_info)) {
  1559. zend_incdec_typed_prop(prop_info, prop, EX_VAR(opline->result.var) OPLINE_CC EXECUTE_DATA_CC);
  1560. } else {
  1561. ZVAL_COPY(EX_VAR(opline->result.var), prop);
  1562. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1563. increment_function(prop);
  1564. } else {
  1565. decrement_function(prop);
  1566. }
  1567. }
  1568. }
  1569. }
  1570. static zend_never_inline void zend_post_incdec_overloaded_property(zend_object *object, zend_string *name, void **cache_slot OPLINE_DC EXECUTE_DATA_DC)
  1571. {
  1572. zval rv;
  1573. zval *z;
  1574. zval z_copy;
  1575. GC_ADDREF(object);
  1576. z =object->handlers->read_property(object, name, BP_VAR_R, cache_slot, &rv);
  1577. if (UNEXPECTED(EG(exception))) {
  1578. OBJ_RELEASE(object);
  1579. ZVAL_UNDEF(EX_VAR(opline->result.var));
  1580. return;
  1581. }
  1582. ZVAL_COPY_DEREF(&z_copy, z);
  1583. ZVAL_COPY(EX_VAR(opline->result.var), &z_copy);
  1584. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1585. increment_function(&z_copy);
  1586. } else {
  1587. decrement_function(&z_copy);
  1588. }
  1589. object->handlers->write_property(object, name, &z_copy, cache_slot);
  1590. OBJ_RELEASE(object);
  1591. zval_ptr_dtor(&z_copy);
  1592. zval_ptr_dtor(z);
  1593. }
  1594. static zend_never_inline void zend_pre_incdec_overloaded_property(zend_object *object, zend_string *name, void **cache_slot OPLINE_DC EXECUTE_DATA_DC)
  1595. {
  1596. zval rv;
  1597. zval *z;
  1598. zval z_copy;
  1599. GC_ADDREF(object);
  1600. z = object->handlers->read_property(object, name, BP_VAR_R, cache_slot, &rv);
  1601. if (UNEXPECTED(EG(exception))) {
  1602. OBJ_RELEASE(object);
  1603. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1604. ZVAL_NULL(EX_VAR(opline->result.var));
  1605. }
  1606. return;
  1607. }
  1608. ZVAL_COPY_DEREF(&z_copy, z);
  1609. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1610. increment_function(&z_copy);
  1611. } else {
  1612. decrement_function(&z_copy);
  1613. }
  1614. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1615. ZVAL_COPY(EX_VAR(opline->result.var), &z_copy);
  1616. }
  1617. object->handlers->write_property(object, name, &z_copy, cache_slot);
  1618. OBJ_RELEASE(object);
  1619. zval_ptr_dtor(&z_copy);
  1620. zval_ptr_dtor(z);
  1621. }
  1622. static zend_never_inline void zend_assign_op_overloaded_property(zend_object *object, zend_string *name, void **cache_slot, zval *value OPLINE_DC EXECUTE_DATA_DC)
  1623. {
  1624. zval *z;
  1625. zval rv, res;
  1626. GC_ADDREF(object);
  1627. z = object->handlers->read_property(object, name, BP_VAR_R, cache_slot, &rv);
  1628. if (UNEXPECTED(EG(exception))) {
  1629. OBJ_RELEASE(object);
  1630. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1631. ZVAL_UNDEF(EX_VAR(opline->result.var));
  1632. }
  1633. return;
  1634. }
  1635. if (zend_binary_op(&res, z, value OPLINE_CC) == SUCCESS) {
  1636. object->handlers->write_property(object, name, &res, cache_slot);
  1637. }
  1638. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1639. ZVAL_COPY(EX_VAR(opline->result.var), &res);
  1640. }
  1641. zval_ptr_dtor(z);
  1642. zval_ptr_dtor(&res);
  1643. OBJ_RELEASE(object);
  1644. }
  1645. /* Utility Functions for Extensions */
  1646. static void zend_extension_statement_handler(const zend_extension *extension, zend_execute_data *frame)
  1647. {
  1648. if (extension->statement_handler) {
  1649. extension->statement_handler(frame);
  1650. }
  1651. }
  1652. static void zend_extension_fcall_begin_handler(const zend_extension *extension, zend_execute_data *frame)
  1653. {
  1654. if (extension->fcall_begin_handler) {
  1655. extension->fcall_begin_handler(frame);
  1656. }
  1657. }
  1658. static void zend_extension_fcall_end_handler(const zend_extension *extension, zend_execute_data *frame)
  1659. {
  1660. if (extension->fcall_end_handler) {
  1661. extension->fcall_end_handler(frame);
  1662. }
  1663. }
  1664. static zend_always_inline HashTable *zend_get_target_symbol_table(int fetch_type EXECUTE_DATA_DC)
  1665. {
  1666. HashTable *ht;
  1667. if (EXPECTED(fetch_type & (ZEND_FETCH_GLOBAL_LOCK | ZEND_FETCH_GLOBAL))) {
  1668. ht = &EG(symbol_table);
  1669. } else {
  1670. ZEND_ASSERT(fetch_type & ZEND_FETCH_LOCAL);
  1671. if (!(EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE)) {
  1672. zend_rebuild_symbol_table();
  1673. }
  1674. ht = EX(symbol_table);
  1675. }
  1676. return ht;
  1677. }
  1678. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_undefined_offset(zend_long lval)
  1679. {
  1680. zend_error(E_NOTICE, "Undefined offset: " ZEND_LONG_FMT, lval);
  1681. }
  1682. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_undefined_index(const zend_string *offset)
  1683. {
  1684. zend_error(E_NOTICE, "Undefined index: %s", ZSTR_VAL(offset));
  1685. }
  1686. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_undefined_method(const zend_class_entry *ce, const zend_string *method)
  1687. {
  1688. zend_throw_error(NULL, "Call to undefined method %s::%s()", ZSTR_VAL(ce->name), ZSTR_VAL(method));
  1689. }
  1690. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_invalid_method_call(zval *object, zval *function_name)
  1691. {
  1692. zend_throw_error(NULL, "Call to a member function %s() on %s", Z_STRVAL_P(function_name), zend_get_type_by_const(Z_TYPE_P(object)));
  1693. }
  1694. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_non_static_method_call(const zend_function *fbc)
  1695. {
  1696. zend_throw_error(
  1697. zend_ce_error,
  1698. "Non-static method %s::%s() cannot be called statically",
  1699. ZSTR_VAL(fbc->common.scope->name), ZSTR_VAL(fbc->common.function_name));
  1700. }
  1701. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_param_must_be_ref(const zend_function *func, uint32_t arg_num)
  1702. {
  1703. const char *arg_name = get_function_arg_name(func, arg_num);
  1704. zend_error(E_WARNING, "%s%s%s(): Argument #%d%s%s%s must be passed by reference, value given",
  1705. func->common.scope ? ZSTR_VAL(func->common.scope->name) : "",
  1706. func->common.scope ? "::" : "",
  1707. ZSTR_VAL(func->common.function_name),
  1708. arg_num,
  1709. arg_name ? " ($" : "",
  1710. arg_name ? arg_name : "",
  1711. arg_name ? ")" : ""
  1712. );
  1713. }
  1714. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_scalar_as_array(void)
  1715. {
  1716. zend_throw_error(NULL, "Cannot use a scalar value as an array");
  1717. }
  1718. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(void)
  1719. {
  1720. zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
  1721. }
  1722. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_resource_as_offset(const zval *dim)
  1723. {
  1724. zend_error(E_WARNING, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(dim), Z_RES_HANDLE_P(dim));
  1725. }
  1726. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_new_element_for_string(void)
  1727. {
  1728. zend_throw_error(NULL, "[] operator not supported for strings");
  1729. }
  1730. static ZEND_COLD void zend_binary_assign_op_dim_slow(zval *container, zval *dim OPLINE_DC EXECUTE_DATA_DC)
  1731. {
  1732. if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) {
  1733. if (opline->op2_type == IS_UNUSED) {
  1734. zend_use_new_element_for_string();
  1735. } else {
  1736. zend_check_string_offset(dim, BP_VAR_RW EXECUTE_DATA_CC);
  1737. zend_wrong_string_offset(EXECUTE_DATA_C);
  1738. }
  1739. } else {
  1740. zend_use_scalar_as_array();
  1741. }
  1742. FREE_OP((opline+1)->op1_type, (opline+1)->op1.var);
  1743. }
  1744. static zend_never_inline zend_uchar slow_index_convert(const zval *dim, zend_value *value EXECUTE_DATA_DC)
  1745. {
  1746. switch (Z_TYPE_P(dim)) {
  1747. case IS_UNDEF:
  1748. ZVAL_UNDEFINED_OP2();
  1749. /* break missing intentionally */
  1750. case IS_NULL:
  1751. value->str = ZSTR_EMPTY_ALLOC();
  1752. return IS_STRING;
  1753. case IS_DOUBLE:
  1754. value->lval = zend_dval_to_lval(Z_DVAL_P(dim));
  1755. return IS_LONG;
  1756. case IS_RESOURCE:
  1757. zend_use_resource_as_offset(dim);
  1758. value->lval = Z_RES_HANDLE_P(dim);
  1759. return IS_LONG;
  1760. case IS_FALSE:
  1761. value->lval = 0;
  1762. return IS_LONG;
  1763. case IS_TRUE:
  1764. value->lval = 1;
  1765. return IS_LONG;
  1766. default:
  1767. zend_illegal_offset();
  1768. return IS_NULL;
  1769. }
  1770. }
  1771. static zend_always_inline zval *zend_fetch_dimension_address_inner(HashTable *ht, const zval *dim, int dim_type, int type EXECUTE_DATA_DC)
  1772. {
  1773. zval *retval = NULL;
  1774. zend_string *offset_key;
  1775. zend_ulong hval;
  1776. try_again:
  1777. if (EXPECTED(Z_TYPE_P(dim) == IS_LONG)) {
  1778. hval = Z_LVAL_P(dim);
  1779. num_index:
  1780. ZEND_HASH_INDEX_FIND(ht, hval, retval, num_undef);
  1781. return retval;
  1782. num_undef:
  1783. switch (type) {
  1784. case BP_VAR_R:
  1785. zend_undefined_offset(hval);
  1786. /* break missing intentionally */
  1787. case BP_VAR_UNSET:
  1788. case BP_VAR_IS:
  1789. retval = &EG(uninitialized_zval);
  1790. break;
  1791. case BP_VAR_RW:
  1792. zend_undefined_offset(hval);
  1793. retval = zend_hash_index_update(ht, hval, &EG(uninitialized_zval));
  1794. break;
  1795. case BP_VAR_W:
  1796. retval = zend_hash_index_add_new(ht, hval, &EG(uninitialized_zval));
  1797. break;
  1798. }
  1799. } else if (EXPECTED(Z_TYPE_P(dim) == IS_STRING)) {
  1800. offset_key = Z_STR_P(dim);
  1801. if (ZEND_CONST_COND(dim_type != IS_CONST, 1)) {
  1802. if (ZEND_HANDLE_NUMERIC(offset_key, hval)) {
  1803. goto num_index;
  1804. }
  1805. }
  1806. str_index:
  1807. retval = zend_hash_find_ex(ht, offset_key, ZEND_CONST_COND(dim_type == IS_CONST, 0));
  1808. if (retval) {
  1809. /* support for $GLOBALS[...] */
  1810. if (UNEXPECTED(Z_TYPE_P(retval) == IS_INDIRECT)) {
  1811. retval = Z_INDIRECT_P(retval);
  1812. if (UNEXPECTED(Z_TYPE_P(retval) == IS_UNDEF)) {
  1813. switch (type) {
  1814. case BP_VAR_R:
  1815. zend_undefined_index(offset_key);
  1816. /* break missing intentionally */
  1817. case BP_VAR_UNSET:
  1818. case BP_VAR_IS:
  1819. retval = &EG(uninitialized_zval);
  1820. break;
  1821. case BP_VAR_RW:
  1822. zend_undefined_index(offset_key);
  1823. /* break missing intentionally */
  1824. case BP_VAR_W:
  1825. ZVAL_NULL(retval);
  1826. break;
  1827. }
  1828. }
  1829. }
  1830. } else {
  1831. switch (type) {
  1832. case BP_VAR_R:
  1833. zend_undefined_index(offset_key);
  1834. /* break missing intentionally */
  1835. case BP_VAR_UNSET:
  1836. case BP_VAR_IS:
  1837. retval = &EG(uninitialized_zval);
  1838. break;
  1839. case BP_VAR_RW:
  1840. zend_undefined_index(offset_key);
  1841. retval = zend_hash_update(ht, offset_key, &EG(uninitialized_zval));
  1842. break;
  1843. case BP_VAR_W:
  1844. retval = zend_hash_add_new(ht, offset_key, &EG(uninitialized_zval));
  1845. break;
  1846. }
  1847. }
  1848. } else if (EXPECTED(Z_TYPE_P(dim) == IS_REFERENCE)) {
  1849. dim = Z_REFVAL_P(dim);
  1850. goto try_again;
  1851. } else {
  1852. zend_value val;
  1853. zend_uchar t = slow_index_convert(dim, &val EXECUTE_DATA_CC);
  1854. if (t == IS_STRING) {
  1855. offset_key = val.str;
  1856. goto str_index;
  1857. } else if (t == IS_LONG) {
  1858. hval = val.lval;
  1859. goto num_index;
  1860. } else {
  1861. retval = (type == BP_VAR_W || type == BP_VAR_RW) ?
  1862. NULL : &EG(uninitialized_zval);
  1863. }
  1864. }
  1865. return retval;
  1866. }
  1867. static zend_never_inline zval* ZEND_FASTCALL zend_fetch_dimension_address_inner_W(HashTable *ht, const zval *dim EXECUTE_DATA_DC)
  1868. {
  1869. return zend_fetch_dimension_address_inner(ht, dim, IS_TMP_VAR, BP_VAR_W EXECUTE_DATA_CC);
  1870. }
  1871. static zend_never_inline zval* ZEND_FASTCALL zend_fetch_dimension_address_inner_W_CONST(HashTable *ht, const zval *dim EXECUTE_DATA_DC)
  1872. {
  1873. return zend_fetch_dimension_address_inner(ht, dim, IS_CONST, BP_VAR_W EXECUTE_DATA_CC);
  1874. }
  1875. static zend_never_inline zval* ZEND_FASTCALL zend_fetch_dimension_address_inner_RW(HashTable *ht, const zval *dim EXECUTE_DATA_DC)
  1876. {
  1877. return zend_fetch_dimension_address_inner(ht, dim, IS_TMP_VAR, BP_VAR_RW EXECUTE_DATA_CC);
  1878. }
  1879. static zend_never_inline zval* ZEND_FASTCALL zend_fetch_dimension_address_inner_RW_CONST(HashTable *ht, const zval *dim EXECUTE_DATA_DC)
  1880. {
  1881. return zend_fetch_dimension_address_inner(ht, dim, IS_CONST, BP_VAR_RW EXECUTE_DATA_CC);
  1882. }
  1883. static zend_always_inline void zend_fetch_dimension_address(zval *result, zval *container, zval *dim, int dim_type, int type EXECUTE_DATA_DC)
  1884. {
  1885. zval *retval;
  1886. if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
  1887. try_array:
  1888. SEPARATE_ARRAY(container);
  1889. fetch_from_array:
  1890. if (dim == NULL) {
  1891. retval = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval));
  1892. if (UNEXPECTED(retval == NULL)) {
  1893. zend_cannot_add_element();
  1894. ZVAL_UNDEF(result);
  1895. return;
  1896. }
  1897. } else {
  1898. retval = zend_fetch_dimension_address_inner(Z_ARRVAL_P(container), dim, dim_type, type EXECUTE_DATA_CC);
  1899. if (UNEXPECTED(!retval)) {
  1900. ZVAL_UNDEF(result);
  1901. return;
  1902. }
  1903. }
  1904. ZVAL_INDIRECT(result, retval);
  1905. return;
  1906. } else if (EXPECTED(Z_TYPE_P(container) == IS_REFERENCE)) {
  1907. zend_reference *ref = Z_REF_P(container);
  1908. container = Z_REFVAL_P(container);
  1909. if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
  1910. goto try_array;
  1911. } else if (EXPECTED(Z_TYPE_P(container) <= IS_FALSE)) {
  1912. if (type != BP_VAR_UNSET) {
  1913. if (ZEND_REF_HAS_TYPE_SOURCES(ref)) {
  1914. if (UNEXPECTED(!zend_verify_ref_array_assignable(ref))) {
  1915. ZVAL_UNDEF(result);
  1916. return;
  1917. }
  1918. }
  1919. array_init(container);
  1920. goto fetch_from_array;
  1921. } else {
  1922. goto return_null;
  1923. }
  1924. }
  1925. }
  1926. if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) {
  1927. if (dim == NULL) {
  1928. zend_use_new_element_for_string();
  1929. } else {
  1930. zend_check_string_offset(dim, type EXECUTE_DATA_CC);
  1931. zend_wrong_string_offset(EXECUTE_DATA_C);
  1932. }
  1933. ZVAL_UNDEF(result);
  1934. } else if (EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
  1935. if (ZEND_CONST_COND(dim_type == IS_CV, dim != NULL) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
  1936. dim = ZVAL_UNDEFINED_OP2();
  1937. }
  1938. if (dim_type == IS_CONST && Z_EXTRA_P(dim) == ZEND_EXTRA_VALUE) {
  1939. dim++;
  1940. }
  1941. retval = Z_OBJ_HT_P(container)->read_dimension(Z_OBJ_P(container), dim, type, result);
  1942. if (UNEXPECTED(retval == &EG(uninitialized_zval))) {
  1943. zend_class_entry *ce = Z_OBJCE_P(container);
  1944. ZVAL_NULL(result);
  1945. zend_error(E_NOTICE, "Indirect modification of overloaded element of %s has no effect", ZSTR_VAL(ce->name));
  1946. } else if (EXPECTED(retval && Z_TYPE_P(retval) != IS_UNDEF)) {
  1947. if (!Z_ISREF_P(retval)) {
  1948. if (result != retval) {
  1949. ZVAL_COPY(result, retval);
  1950. retval = result;
  1951. }
  1952. if (Z_TYPE_P(retval) != IS_OBJECT) {
  1953. zend_class_entry *ce = Z_OBJCE_P(container);
  1954. zend_error(E_NOTICE, "Indirect modification of overloaded element of %s has no effect", ZSTR_VAL(ce->name));
  1955. }
  1956. } else if (UNEXPECTED(Z_REFCOUNT_P(retval) == 1)) {
  1957. ZVAL_UNREF(retval);
  1958. }
  1959. if (result != retval) {
  1960. ZVAL_INDIRECT(result, retval);
  1961. }
  1962. } else {
  1963. ZEND_ASSERT(EG(exception) && "read_dimension() returned NULL without exception");
  1964. ZVAL_UNDEF(result);
  1965. }
  1966. } else {
  1967. if (EXPECTED(Z_TYPE_P(container) <= IS_FALSE)) {
  1968. if (type != BP_VAR_W && UNEXPECTED(Z_TYPE_P(container) == IS_UNDEF)) {
  1969. ZVAL_UNDEFINED_OP1();
  1970. }
  1971. if (type != BP_VAR_UNSET) {
  1972. array_init(container);
  1973. goto fetch_from_array;
  1974. } else {
  1975. return_null:
  1976. /* for read-mode only */
  1977. if (ZEND_CONST_COND(dim_type == IS_CV, dim != NULL) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
  1978. ZVAL_UNDEFINED_OP2();
  1979. }
  1980. ZVAL_NULL(result);
  1981. }
  1982. } else {
  1983. if (type == BP_VAR_UNSET) {
  1984. zend_throw_error(NULL, "Cannot unset offset in a non-array variable");
  1985. ZVAL_UNDEF(result);
  1986. } else {
  1987. zend_use_scalar_as_array();
  1988. ZVAL_UNDEF(result);
  1989. }
  1990. }
  1991. }
  1992. }
  1993. static zend_never_inline void ZEND_FASTCALL zend_fetch_dimension_address_W(zval *container_ptr, zval *dim, int dim_type OPLINE_DC EXECUTE_DATA_DC)
  1994. {
  1995. zval *result = EX_VAR(opline->result.var);
  1996. zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_W EXECUTE_DATA_CC);
  1997. }
  1998. static zend_never_inline void ZEND_FASTCALL zend_fetch_dimension_address_RW(zval *container_ptr, zval *dim, int dim_type OPLINE_DC EXECUTE_DATA_DC)
  1999. {
  2000. zval *result = EX_VAR(opline->result.var);
  2001. zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_RW EXECUTE_DATA_CC);
  2002. }
  2003. static zend_never_inline void ZEND_FASTCALL zend_fetch_dimension_address_UNSET(zval *container_ptr, zval *dim, int dim_type OPLINE_DC EXECUTE_DATA_DC)
  2004. {
  2005. zval *result = EX_VAR(opline->result.var);
  2006. zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_UNSET EXECUTE_DATA_CC);
  2007. }
  2008. static zend_always_inline void zend_fetch_dimension_address_read(zval *result, zval *container, zval *dim, int dim_type, int type, int is_list, int slow EXECUTE_DATA_DC)
  2009. {
  2010. zval *retval;
  2011. if (!slow) {
  2012. if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
  2013. try_array:
  2014. retval = zend_fetch_dimension_address_inner(Z_ARRVAL_P(container), dim, dim_type, type EXECUTE_DATA_CC);
  2015. ZVAL_COPY_DEREF(result, retval);
  2016. return;
  2017. } else if (EXPECTED(Z_TYPE_P(container) == IS_REFERENCE)) {
  2018. container = Z_REFVAL_P(container);
  2019. if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
  2020. goto try_array;
  2021. }
  2022. }
  2023. }
  2024. if (!is_list && EXPECTED(Z_TYPE_P(container) == IS_STRING)) {
  2025. zend_long offset;
  2026. try_string_offset:
  2027. if (UNEXPECTED(Z_TYPE_P(dim) != IS_LONG)) {
  2028. switch (Z_TYPE_P(dim)) {
  2029. /* case IS_LONG: */
  2030. case IS_STRING:
  2031. if (IS_LONG == is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), NULL, NULL, -1)) {
  2032. break;
  2033. }
  2034. if (type == BP_VAR_IS) {
  2035. ZVAL_NULL(result);
  2036. return;
  2037. }
  2038. zend_error(E_WARNING, "Illegal string offset '%s'", Z_STRVAL_P(dim));
  2039. break;
  2040. case IS_UNDEF:
  2041. ZVAL_UNDEFINED_OP2();
  2042. case IS_DOUBLE:
  2043. case IS_NULL:
  2044. case IS_FALSE:
  2045. case IS_TRUE:
  2046. if (type != BP_VAR_IS) {
  2047. zend_error(E_WARNING, "String offset cast occurred");
  2048. }
  2049. break;
  2050. case IS_REFERENCE:
  2051. dim = Z_REFVAL_P(dim);
  2052. goto try_string_offset;
  2053. default:
  2054. zend_illegal_offset();
  2055. break;
  2056. }
  2057. offset = zval_get_long_func(dim);
  2058. } else {
  2059. offset = Z_LVAL_P(dim);
  2060. }
  2061. if (UNEXPECTED(Z_STRLEN_P(container) < ((offset < 0) ? -(size_t)offset : ((size_t)offset + 1)))) {
  2062. if (type != BP_VAR_IS) {
  2063. zend_error(E_WARNING, "Uninitialized string offset: " ZEND_LONG_FMT, offset);
  2064. ZVAL_EMPTY_STRING(result);
  2065. } else {
  2066. ZVAL_NULL(result);
  2067. }
  2068. } else {
  2069. zend_uchar c;
  2070. zend_long real_offset;
  2071. real_offset = (UNEXPECTED(offset < 0)) /* Handle negative offset */
  2072. ? (zend_long)Z_STRLEN_P(container) + offset : offset;
  2073. c = (zend_uchar)Z_STRVAL_P(container)[real_offset];
  2074. ZVAL_INTERNED_STR(result, ZSTR_CHAR(c));
  2075. }
  2076. } else if (EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
  2077. if (ZEND_CONST_COND(dim_type == IS_CV, 1) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
  2078. dim = ZVAL_UNDEFINED_OP2();
  2079. }
  2080. if (dim_type == IS_CONST && Z_EXTRA_P(dim) == ZEND_EXTRA_VALUE) {
  2081. dim++;
  2082. }
  2083. retval = Z_OBJ_HT_P(container)->read_dimension(Z_OBJ_P(container), dim, type, result);
  2084. ZEND_ASSERT(result != NULL);
  2085. if (retval) {
  2086. if (result != retval) {
  2087. ZVAL_COPY_DEREF(result, retval);
  2088. } else if (UNEXPECTED(Z_ISREF_P(retval))) {
  2089. zend_unwrap_reference(result);
  2090. }
  2091. } else {
  2092. ZVAL_NULL(result);
  2093. }
  2094. } else {
  2095. if (type != BP_VAR_IS && UNEXPECTED(Z_TYPE_P(container) == IS_UNDEF)) {
  2096. container = ZVAL_UNDEFINED_OP1();
  2097. }
  2098. if (ZEND_CONST_COND(dim_type == IS_CV, 1) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
  2099. ZVAL_UNDEFINED_OP2();
  2100. }
  2101. if (!is_list && type != BP_VAR_IS) {
  2102. zend_error(E_WARNING, "Trying to access array offset on value of type %s",
  2103. zend_zval_type_name(container));
  2104. }
  2105. ZVAL_NULL(result);
  2106. }
  2107. }
  2108. static zend_never_inline void ZEND_FASTCALL zend_fetch_dimension_address_read_R(zval *container, zval *dim, int dim_type OPLINE_DC EXECUTE_DATA_DC)
  2109. {
  2110. zval *result = EX_VAR(opline->result.var);
  2111. zend_fetch_dimension_address_read(result, container, dim, dim_type, BP_VAR_R, 0, 0 EXECUTE_DATA_CC);
  2112. }
  2113. static zend_never_inline void zend_fetch_dimension_address_read_R_slow(zval *container, zval *dim OPLINE_DC EXECUTE_DATA_DC)
  2114. {
  2115. zval *result = EX_VAR(opline->result.var);
  2116. zend_fetch_dimension_address_read(result, container, dim, IS_CV, BP_VAR_R, 0, 1 EXECUTE_DATA_CC);
  2117. }
  2118. static zend_never_inline void ZEND_FASTCALL zend_fetch_dimension_address_read_IS(zval *container, zval *dim, int dim_type OPLINE_DC EXECUTE_DATA_DC)
  2119. {
  2120. zval *result = EX_VAR(opline->result.var);
  2121. zend_fetch_dimension_address_read(result, container, dim, dim_type, BP_VAR_IS, 0, 0 EXECUTE_DATA_CC);
  2122. }
  2123. static zend_never_inline void ZEND_FASTCALL zend_fetch_dimension_address_LIST_r(zval *container, zval *dim, int dim_type OPLINE_DC EXECUTE_DATA_DC)
  2124. {
  2125. zval *result = EX_VAR(opline->result.var);
  2126. zend_fetch_dimension_address_read(result, container, dim, dim_type, BP_VAR_R, 1, 0 EXECUTE_DATA_CC);
  2127. }
  2128. ZEND_API void zend_fetch_dimension_const(zval *result, zval *container, zval *dim, int type)
  2129. {
  2130. zend_fetch_dimension_address_read(result, container, dim, IS_TMP_VAR, type, 0, 0 NO_EXECUTE_DATA_CC);
  2131. }
  2132. static zend_never_inline zval* ZEND_FASTCALL zend_find_array_dim_slow(HashTable *ht, zval *offset EXECUTE_DATA_DC)
  2133. {
  2134. zend_ulong hval;
  2135. if (Z_TYPE_P(offset) == IS_DOUBLE) {
  2136. hval = zend_dval_to_lval(Z_DVAL_P(offset));
  2137. num_idx:
  2138. return zend_hash_index_find(ht, hval);
  2139. } else if (Z_TYPE_P(offset) == IS_NULL) {
  2140. str_idx:
  2141. return zend_hash_find_ex_ind(ht, ZSTR_EMPTY_ALLOC(), 1);
  2142. } else if (Z_TYPE_P(offset) == IS_FALSE) {
  2143. hval = 0;
  2144. goto num_idx;
  2145. } else if (Z_TYPE_P(offset) == IS_TRUE) {
  2146. hval = 1;
  2147. goto num_idx;
  2148. } else if (Z_TYPE_P(offset) == IS_RESOURCE) {
  2149. zend_use_resource_as_offset(offset);
  2150. hval = Z_RES_HANDLE_P(offset);
  2151. goto num_idx;
  2152. } else if (/*OP2_TYPE == IS_CV &&*/ Z_TYPE_P(offset) == IS_UNDEF) {
  2153. ZVAL_UNDEFINED_OP2();
  2154. goto str_idx;
  2155. } else {
  2156. zend_type_error("Illegal offset type in isset or empty");
  2157. return NULL;
  2158. }
  2159. }
  2160. static zend_never_inline int ZEND_FASTCALL zend_isset_dim_slow(zval *container, zval *offset EXECUTE_DATA_DC)
  2161. {
  2162. if (/*OP2_TYPE == IS_CV &&*/ UNEXPECTED(Z_TYPE_P(offset) == IS_UNDEF)) {
  2163. offset = ZVAL_UNDEFINED_OP2();
  2164. }
  2165. if (/*OP1_TYPE != IS_CONST &&*/ EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
  2166. return Z_OBJ_HT_P(container)->has_dimension(Z_OBJ_P(container), offset, 0);
  2167. } else if (EXPECTED(Z_TYPE_P(container) == IS_STRING)) { /* string offsets */
  2168. zend_long lval;
  2169. if (EXPECTED(Z_TYPE_P(offset) == IS_LONG)) {
  2170. lval = Z_LVAL_P(offset);
  2171. str_offset:
  2172. if (UNEXPECTED(lval < 0)) { /* Handle negative offset */
  2173. lval += (zend_long)Z_STRLEN_P(container);
  2174. }
  2175. if (EXPECTED(lval >= 0) && (size_t)lval < Z_STRLEN_P(container)) {
  2176. return 1;
  2177. } else {
  2178. return 0;
  2179. }
  2180. } else {
  2181. /*if (OP2_TYPE & (IS_CV|IS_VAR)) {*/
  2182. ZVAL_DEREF(offset);
  2183. /*}*/
  2184. if (Z_TYPE_P(offset) < IS_STRING /* simple scalar types */
  2185. || (Z_TYPE_P(offset) == IS_STRING /* or numeric string */
  2186. && IS_LONG == is_numeric_string(Z_STRVAL_P(offset), Z_STRLEN_P(offset), NULL, NULL, 0))) {
  2187. lval = zval_get_long(offset);
  2188. goto str_offset;
  2189. }
  2190. return 0;
  2191. }
  2192. } else {
  2193. return 0;
  2194. }
  2195. }
  2196. static zend_never_inline int ZEND_FASTCALL zend_isempty_dim_slow(zval *container, zval *offset EXECUTE_DATA_DC)
  2197. {
  2198. if (/*OP2_TYPE == IS_CV &&*/ UNEXPECTED(Z_TYPE_P(offset) == IS_UNDEF)) {
  2199. offset = ZVAL_UNDEFINED_OP2();
  2200. }
  2201. if (/*OP1_TYPE != IS_CONST &&*/ EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
  2202. return !Z_OBJ_HT_P(container)->has_dimension(Z_OBJ_P(container), offset, 1);
  2203. } else if (EXPECTED(Z_TYPE_P(container) == IS_STRING)) { /* string offsets */
  2204. zend_long lval;
  2205. if (EXPECTED(Z_TYPE_P(offset) == IS_LONG)) {
  2206. lval = Z_LVAL_P(offset);
  2207. str_offset:
  2208. if (UNEXPECTED(lval < 0)) { /* Handle negative offset */
  2209. lval += (zend_long)Z_STRLEN_P(container);
  2210. }
  2211. if (EXPECTED(lval >= 0) && (size_t)lval < Z_STRLEN_P(container)) {
  2212. return (Z_STRVAL_P(container)[lval] == '0');
  2213. } else {
  2214. return 1;
  2215. }
  2216. } else {
  2217. /*if (OP2_TYPE & (IS_CV|IS_VAR)) {*/
  2218. ZVAL_DEREF(offset);
  2219. /*}*/
  2220. if (Z_TYPE_P(offset) < IS_STRING /* simple scalar types */
  2221. || (Z_TYPE_P(offset) == IS_STRING /* or numeric string */
  2222. && IS_LONG == is_numeric_string(Z_STRVAL_P(offset), Z_STRLEN_P(offset), NULL, NULL, 0))) {
  2223. lval = zval_get_long(offset);
  2224. goto str_offset;
  2225. }
  2226. return 1;
  2227. }
  2228. } else {
  2229. return 1;
  2230. }
  2231. }
  2232. static zend_never_inline zend_bool ZEND_FASTCALL zend_array_key_exists_fast(HashTable *ht, zval *key OPLINE_DC EXECUTE_DATA_DC)
  2233. {
  2234. zend_string *str;
  2235. zend_ulong hval;
  2236. try_again:
  2237. if (EXPECTED(Z_TYPE_P(key) == IS_STRING)) {
  2238. str = Z_STR_P(key);
  2239. if (ZEND_HANDLE_NUMERIC(str, hval)) {
  2240. goto num_key;
  2241. }
  2242. str_key:
  2243. return zend_hash_find_ind(ht, str) != NULL;
  2244. } else if (EXPECTED(Z_TYPE_P(key) == IS_LONG)) {
  2245. hval = Z_LVAL_P(key);
  2246. num_key:
  2247. return zend_hash_index_find(ht, hval) != NULL;
  2248. } else if (EXPECTED(Z_ISREF_P(key))) {
  2249. key = Z_REFVAL_P(key);
  2250. goto try_again;
  2251. } else if (Z_TYPE_P(key) == IS_DOUBLE) {
  2252. hval = zend_dval_to_lval(Z_DVAL_P(key));
  2253. goto num_key;
  2254. } else if (Z_TYPE_P(key) == IS_FALSE) {
  2255. hval = 0;
  2256. goto num_key;
  2257. } else if (Z_TYPE_P(key) == IS_TRUE) {
  2258. hval = 1;
  2259. goto num_key;
  2260. } else if (Z_TYPE_P(key) == IS_RESOURCE) {
  2261. zend_use_resource_as_offset(key);
  2262. hval = Z_RES_HANDLE_P(key);
  2263. goto num_key;
  2264. } else if (Z_TYPE_P(key) <= IS_NULL) {
  2265. if (UNEXPECTED(Z_TYPE_P(key) == IS_UNDEF)) {
  2266. ZVAL_UNDEFINED_OP1();
  2267. }
  2268. str = ZSTR_EMPTY_ALLOC();
  2269. goto str_key;
  2270. } else {
  2271. zend_type_error("Illegal offset type");
  2272. return 0;
  2273. }
  2274. }
  2275. static ZEND_COLD void ZEND_FASTCALL zend_array_key_exists_error(
  2276. zval *subject, zval *key OPLINE_DC EXECUTE_DATA_DC)
  2277. {
  2278. if (Z_TYPE_P(key) == IS_UNDEF) {
  2279. ZVAL_UNDEFINED_OP1();
  2280. }
  2281. if (Z_TYPE_P(subject) == IS_UNDEF) {
  2282. ZVAL_UNDEFINED_OP2();
  2283. }
  2284. if (!EG(exception)) {
  2285. zend_type_error("array_key_exists(): Argument #2 ($array) must be of type array, %s given",
  2286. zend_get_type_by_const(Z_TYPE_P(subject))
  2287. );
  2288. }
  2289. }
  2290. static zend_always_inline zend_bool promotes_to_array(zval *val) {
  2291. return Z_TYPE_P(val) <= IS_FALSE
  2292. || (Z_ISREF_P(val) && Z_TYPE_P(Z_REFVAL_P(val)) <= IS_FALSE);
  2293. }
  2294. static zend_always_inline zend_bool check_type_array_assignable(zend_type type) {
  2295. if (!ZEND_TYPE_IS_SET(type)) {
  2296. return 1;
  2297. }
  2298. return (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_ITERABLE|MAY_BE_ARRAY)) != 0;
  2299. }
  2300. /* Checks whether an array can be assigned to the reference. Throws error if not assignable. */
  2301. ZEND_API zend_bool zend_verify_ref_array_assignable(zend_reference *ref) {
  2302. zend_property_info *prop;
  2303. ZEND_ASSERT(ZEND_REF_HAS_TYPE_SOURCES(ref));
  2304. ZEND_REF_FOREACH_TYPE_SOURCES(ref, prop) {
  2305. if (!check_type_array_assignable(prop->type)) {
  2306. zend_throw_auto_init_in_ref_error(prop, "array");
  2307. return 0;
  2308. }
  2309. } ZEND_REF_FOREACH_TYPE_SOURCES_END();
  2310. return 1;
  2311. }
  2312. static zend_property_info *zend_object_fetch_property_type_info(
  2313. zend_object *obj, zval *slot)
  2314. {
  2315. if (EXPECTED(!ZEND_CLASS_HAS_TYPE_HINTS(obj->ce))) {
  2316. return NULL;
  2317. }
  2318. /* Not a declared property */
  2319. if (UNEXPECTED(slot < obj->properties_table ||
  2320. slot >= obj->properties_table + obj->ce->default_properties_count)) {
  2321. return NULL;
  2322. }
  2323. return zend_get_typed_property_info_for_slot(obj, slot);
  2324. }
  2325. static zend_never_inline zend_bool zend_handle_fetch_obj_flags(
  2326. zval *result, zval *ptr, zend_object *obj, zend_property_info *prop_info, uint32_t flags)
  2327. {
  2328. switch (flags) {
  2329. case ZEND_FETCH_DIM_WRITE:
  2330. if (promotes_to_array(ptr)) {
  2331. if (!prop_info) {
  2332. prop_info = zend_object_fetch_property_type_info(obj, ptr);
  2333. if (!prop_info) {
  2334. break;
  2335. }
  2336. }
  2337. if (!check_type_array_assignable(prop_info->type)) {
  2338. zend_throw_auto_init_in_prop_error(prop_info, "array");
  2339. if (result) ZVAL_ERROR(result);
  2340. return 0;
  2341. }
  2342. }
  2343. break;
  2344. case ZEND_FETCH_REF:
  2345. if (Z_TYPE_P(ptr) != IS_REFERENCE) {
  2346. if (!prop_info) {
  2347. prop_info = zend_object_fetch_property_type_info(obj, ptr);
  2348. if (!prop_info) {
  2349. break;
  2350. }
  2351. }
  2352. if (Z_TYPE_P(ptr) == IS_UNDEF) {
  2353. if (!ZEND_TYPE_ALLOW_NULL(prop_info->type)) {
  2354. zend_throw_access_uninit_prop_by_ref_error(prop_info);
  2355. if (result) ZVAL_ERROR(result);
  2356. return 0;
  2357. }
  2358. ZVAL_NULL(ptr);
  2359. }
  2360. ZVAL_NEW_REF(ptr, ptr);
  2361. ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(ptr), prop_info);
  2362. }
  2363. break;
  2364. EMPTY_SWITCH_DEFAULT_CASE()
  2365. }
  2366. return 1;
  2367. }
  2368. static zend_always_inline void zend_fetch_property_address(zval *result, zval *container, uint32_t container_op_type, zval *prop_ptr, uint32_t prop_op_type, void **cache_slot, int type, uint32_t flags, zend_bool init_undef OPLINE_DC EXECUTE_DATA_DC)
  2369. {
  2370. zval *ptr;
  2371. zend_object *zobj;
  2372. zend_string *name, *tmp_name;
  2373. if (container_op_type != IS_UNUSED && UNEXPECTED(Z_TYPE_P(container) != IS_OBJECT)) {
  2374. do {
  2375. if (Z_ISREF_P(container) && Z_TYPE_P(Z_REFVAL_P(container)) == IS_OBJECT) {
  2376. container = Z_REFVAL_P(container);
  2377. break;
  2378. }
  2379. if (container_op_type == IS_CV
  2380. && type != BP_VAR_W
  2381. && UNEXPECTED(Z_TYPE_P(container) == IS_UNDEF)) {
  2382. ZVAL_UNDEFINED_OP1();
  2383. }
  2384. /* this should modify object only if it's empty */
  2385. if (type == BP_VAR_UNSET) {
  2386. ZVAL_NULL(result);
  2387. return;
  2388. }
  2389. zend_throw_non_object_error(container, prop_ptr OPLINE_CC EXECUTE_DATA_CC);
  2390. ZVAL_ERROR(result);
  2391. return;
  2392. } while (0);
  2393. }
  2394. zobj = Z_OBJ_P(container);
  2395. if (prop_op_type == IS_CONST &&
  2396. EXPECTED(zobj->ce == CACHED_PTR_EX(cache_slot))) {
  2397. uintptr_t prop_offset = (uintptr_t)CACHED_PTR_EX(cache_slot + 1);
  2398. if (EXPECTED(IS_VALID_PROPERTY_OFFSET(prop_offset))) {
  2399. ptr = OBJ_PROP(zobj, prop_offset);
  2400. if (EXPECTED(Z_TYPE_P(ptr) != IS_UNDEF)) {
  2401. ZVAL_INDIRECT(result, ptr);
  2402. if (flags) {
  2403. zend_property_info *prop_info = CACHED_PTR_EX(cache_slot + 2);
  2404. if (prop_info) {
  2405. zend_handle_fetch_obj_flags(result, ptr, NULL, prop_info, flags);
  2406. }
  2407. }
  2408. return;
  2409. }
  2410. } else if (EXPECTED(zobj->properties != NULL)) {
  2411. if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
  2412. if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
  2413. GC_DELREF(zobj->properties);
  2414. }
  2415. zobj->properties = zend_array_dup(zobj->properties);
  2416. }
  2417. ptr = zend_hash_find_ex(zobj->properties, Z_STR_P(prop_ptr), 1);
  2418. if (EXPECTED(ptr)) {
  2419. ZVAL_INDIRECT(result, ptr);
  2420. return;
  2421. }
  2422. }
  2423. }
  2424. if (prop_op_type == IS_CONST) {
  2425. name = Z_STR_P(prop_ptr);
  2426. } else {
  2427. name = zval_get_tmp_string(prop_ptr, &tmp_name);
  2428. }
  2429. ptr = zobj->handlers->get_property_ptr_ptr(zobj, name, type, cache_slot);
  2430. if (NULL == ptr) {
  2431. ptr = zobj->handlers->read_property(zobj, name, type, cache_slot, result);
  2432. if (ptr == result) {
  2433. if (UNEXPECTED(Z_ISREF_P(ptr) && Z_REFCOUNT_P(ptr) == 1)) {
  2434. ZVAL_UNREF(ptr);
  2435. }
  2436. goto end;
  2437. }
  2438. } else if (UNEXPECTED(Z_ISERROR_P(ptr))) {
  2439. ZVAL_ERROR(result);
  2440. goto end;
  2441. }
  2442. ZVAL_INDIRECT(result, ptr);
  2443. if (flags) {
  2444. zend_property_info *prop_info;
  2445. if (prop_op_type == IS_CONST) {
  2446. prop_info = CACHED_PTR_EX(cache_slot + 2);
  2447. if (prop_info) {
  2448. if (UNEXPECTED(!zend_handle_fetch_obj_flags(result, ptr, NULL, prop_info, flags))) {
  2449. goto end;
  2450. }
  2451. }
  2452. } else {
  2453. if (UNEXPECTED(!zend_handle_fetch_obj_flags(result, ptr, Z_OBJ_P(container), NULL, flags))) {
  2454. goto end;
  2455. }
  2456. }
  2457. }
  2458. if (init_undef && UNEXPECTED(Z_TYPE_P(ptr) == IS_UNDEF)) {
  2459. ZVAL_NULL(ptr);
  2460. }
  2461. end:
  2462. if (prop_op_type != IS_CONST) {
  2463. zend_tmp_string_release(tmp_name);
  2464. }
  2465. }
  2466. static zend_always_inline void zend_assign_to_property_reference(zval *container, uint32_t container_op_type, zval *prop_ptr, uint32_t prop_op_type, zval *value_ptr OPLINE_DC EXECUTE_DATA_DC)
  2467. {
  2468. zval variable, *variable_ptr = &variable;
  2469. void **cache_addr = (prop_op_type == IS_CONST) ? CACHE_ADDR(opline->extended_value & ~ZEND_RETURNS_FUNCTION) : NULL;
  2470. zend_fetch_property_address(variable_ptr, container, container_op_type, prop_ptr, prop_op_type,
  2471. cache_addr, BP_VAR_W, 0, 0 OPLINE_CC EXECUTE_DATA_CC);
  2472. if (EXPECTED(Z_TYPE_P(variable_ptr) == IS_INDIRECT)) {
  2473. variable_ptr = Z_INDIRECT_P(variable_ptr);
  2474. if (/*OP_DATA_TYPE == IS_VAR &&*/
  2475. (opline->extended_value & ZEND_RETURNS_FUNCTION) &&
  2476. UNEXPECTED(!Z_ISREF_P(value_ptr))) {
  2477. if (UNEXPECTED(!zend_wrong_assign_to_variable_reference(
  2478. variable_ptr, value_ptr OPLINE_CC EXECUTE_DATA_CC))) {
  2479. variable_ptr = &EG(uninitialized_zval);
  2480. }
  2481. } else {
  2482. zend_property_info *prop_info = NULL;
  2483. if (prop_op_type == IS_CONST) {
  2484. prop_info = (zend_property_info *) CACHED_PTR_EX(cache_addr + 2);
  2485. } else {
  2486. ZVAL_DEREF(container);
  2487. prop_info = zend_object_fetch_property_type_info(Z_OBJ_P(container), variable_ptr);
  2488. }
  2489. if (UNEXPECTED(prop_info)) {
  2490. variable_ptr = zend_assign_to_typed_property_reference(prop_info, variable_ptr, value_ptr EXECUTE_DATA_CC);
  2491. } else {
  2492. zend_assign_to_variable_reference(variable_ptr, value_ptr);
  2493. }
  2494. }
  2495. } else if (Z_ISERROR_P(variable_ptr)) {
  2496. variable_ptr = &EG(uninitialized_zval);
  2497. } else {
  2498. zend_throw_error(NULL, "Cannot assign by reference to overloaded object");
  2499. zval_ptr_dtor(&variable);
  2500. variable_ptr = &EG(uninitialized_zval);
  2501. }
  2502. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  2503. ZVAL_COPY(EX_VAR(opline->result.var), variable_ptr);
  2504. }
  2505. }
  2506. static zend_never_inline void zend_assign_to_property_reference_this_const(zval *container, zval *prop_ptr, zval *value_ptr OPLINE_DC EXECUTE_DATA_DC)
  2507. {
  2508. zend_assign_to_property_reference(container, IS_UNUSED, prop_ptr, IS_CONST, value_ptr
  2509. OPLINE_CC EXECUTE_DATA_CC);
  2510. }
  2511. static zend_never_inline void zend_assign_to_property_reference_var_const(zval *container, zval *prop_ptr, zval *value_ptr OPLINE_DC EXECUTE_DATA_DC)
  2512. {
  2513. zend_assign_to_property_reference(container, IS_VAR, prop_ptr, IS_CONST, value_ptr
  2514. OPLINE_CC EXECUTE_DATA_CC);
  2515. }
  2516. static zend_never_inline void zend_assign_to_property_reference_this_var(zval *container, zval *prop_ptr, zval *value_ptr OPLINE_DC EXECUTE_DATA_DC)
  2517. {
  2518. zend_assign_to_property_reference(container, IS_UNUSED, prop_ptr, IS_VAR, value_ptr
  2519. OPLINE_CC EXECUTE_DATA_CC);
  2520. }
  2521. static zend_never_inline void zend_assign_to_property_reference_var_var(zval *container, zval *prop_ptr, zval *value_ptr OPLINE_DC EXECUTE_DATA_DC)
  2522. {
  2523. zend_assign_to_property_reference(container, IS_VAR, prop_ptr, IS_VAR, value_ptr
  2524. OPLINE_CC EXECUTE_DATA_CC);
  2525. }
  2526. static zend_never_inline int zend_fetch_static_property_address_ex(zval **retval, zend_property_info **prop_info, uint32_t cache_slot, int fetch_type OPLINE_DC EXECUTE_DATA_DC) {
  2527. zend_string *name;
  2528. zend_class_entry *ce;
  2529. zend_property_info *property_info;
  2530. zend_uchar op1_type = opline->op1_type, op2_type = opline->op2_type;
  2531. if (EXPECTED(op2_type == IS_CONST)) {
  2532. zval *class_name = RT_CONSTANT(opline, opline->op2);
  2533. ZEND_ASSERT(op1_type != IS_CONST || CACHED_PTR(cache_slot) == NULL);
  2534. if (EXPECTED((ce = CACHED_PTR(cache_slot)) == NULL)) {
  2535. ce = zend_fetch_class_by_name(Z_STR_P(class_name), Z_STR_P(class_name + 1), ZEND_FETCH_CLASS_DEFAULT | ZEND_FETCH_CLASS_EXCEPTION);
  2536. if (UNEXPECTED(ce == NULL)) {
  2537. FREE_UNFETCHED_OP(op1_type, opline->op1.var);
  2538. return FAILURE;
  2539. }
  2540. if (UNEXPECTED(op1_type != IS_CONST)) {
  2541. CACHE_PTR(cache_slot, ce);
  2542. }
  2543. }
  2544. } else {
  2545. if (EXPECTED(op2_type == IS_UNUSED)) {
  2546. ce = zend_fetch_class(NULL, opline->op2.num);
  2547. if (UNEXPECTED(ce == NULL)) {
  2548. FREE_UNFETCHED_OP(op1_type, opline->op1.var);
  2549. return FAILURE;
  2550. }
  2551. } else {
  2552. ce = Z_CE_P(EX_VAR(opline->op2.var));
  2553. }
  2554. if (EXPECTED(op1_type == IS_CONST) && EXPECTED(CACHED_PTR(cache_slot) == ce)) {
  2555. *retval = CACHED_PTR(cache_slot + sizeof(void *));
  2556. *prop_info = CACHED_PTR(cache_slot + sizeof(void *) * 2);
  2557. return SUCCESS;
  2558. }
  2559. }
  2560. if (EXPECTED(op1_type == IS_CONST)) {
  2561. name = Z_STR_P(RT_CONSTANT(opline, opline->op1));
  2562. *retval = zend_std_get_static_property_with_info(ce, name, fetch_type, &property_info);
  2563. } else {
  2564. zend_string *tmp_name;
  2565. zval *varname = get_zval_ptr_undef(opline->op1_type, opline->op1, BP_VAR_R);
  2566. if (EXPECTED(Z_TYPE_P(varname) == IS_STRING)) {
  2567. name = Z_STR_P(varname);
  2568. tmp_name = NULL;
  2569. } else {
  2570. if (op1_type == IS_CV && UNEXPECTED(Z_TYPE_P(varname) == IS_UNDEF)) {
  2571. zval_undefined_cv(opline->op1.var EXECUTE_DATA_CC);
  2572. }
  2573. name = zval_get_tmp_string(varname, &tmp_name);
  2574. }
  2575. *retval = zend_std_get_static_property_with_info(ce, name, fetch_type, &property_info);
  2576. if (UNEXPECTED(op1_type != IS_CONST)) {
  2577. zend_tmp_string_release(tmp_name);
  2578. FREE_OP(op1_type, opline->op1.var);
  2579. }
  2580. }
  2581. if (UNEXPECTED(*retval == NULL)) {
  2582. return FAILURE;
  2583. }
  2584. *prop_info = property_info;
  2585. if (EXPECTED(op1_type == IS_CONST)) {
  2586. CACHE_POLYMORPHIC_PTR(cache_slot, ce, *retval);
  2587. CACHE_PTR(cache_slot + sizeof(void *) * 2, property_info);
  2588. }
  2589. return SUCCESS;
  2590. }
  2591. static zend_always_inline int zend_fetch_static_property_address(zval **retval, zend_property_info **prop_info, uint32_t cache_slot, int fetch_type, int flags OPLINE_DC EXECUTE_DATA_DC) {
  2592. int success;
  2593. zend_property_info *property_info;
  2594. if (opline->op1_type == IS_CONST && (opline->op2_type == IS_CONST || (opline->op2_type == IS_UNUSED && (opline->op2.num == ZEND_FETCH_CLASS_SELF || opline->op2.num == ZEND_FETCH_CLASS_PARENT))) && EXPECTED(CACHED_PTR(cache_slot) != NULL)) {
  2595. *retval = CACHED_PTR(cache_slot + sizeof(void *));
  2596. property_info = CACHED_PTR(cache_slot + sizeof(void *) * 2);
  2597. if ((fetch_type == BP_VAR_R || fetch_type == BP_VAR_RW)
  2598. && UNEXPECTED(Z_TYPE_P(*retval) == IS_UNDEF)
  2599. && UNEXPECTED(ZEND_TYPE_IS_SET(property_info->type))) {
  2600. zend_throw_error(NULL, "Typed static property %s::$%s must not be accessed before initialization",
  2601. ZSTR_VAL(property_info->ce->name),
  2602. zend_get_unmangled_property_name(property_info->name));
  2603. return FAILURE;
  2604. }
  2605. } else {
  2606. success = zend_fetch_static_property_address_ex(retval, &property_info, cache_slot, fetch_type OPLINE_CC EXECUTE_DATA_CC);
  2607. if (UNEXPECTED(success != SUCCESS)) {
  2608. return FAILURE;
  2609. }
  2610. }
  2611. if (flags && ZEND_TYPE_IS_SET(property_info->type)) {
  2612. zend_handle_fetch_obj_flags(NULL, *retval, NULL, property_info, flags);
  2613. }
  2614. if (prop_info) {
  2615. *prop_info = property_info;
  2616. }
  2617. return SUCCESS;
  2618. }
  2619. ZEND_API ZEND_COLD void zend_throw_ref_type_error_type(zend_property_info *prop1, zend_property_info *prop2, zval *zv) {
  2620. zend_string *type1_str = zend_type_to_string(prop1->type);
  2621. zend_string *type2_str = zend_type_to_string(prop2->type);
  2622. zend_type_error("Reference with value of type %s held by property %s::$%s of type %s is not compatible with property %s::$%s of type %s",
  2623. Z_TYPE_P(zv) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(zv)->name) : zend_get_type_by_const(Z_TYPE_P(zv)),
  2624. ZSTR_VAL(prop1->ce->name),
  2625. zend_get_unmangled_property_name(prop1->name),
  2626. ZSTR_VAL(type1_str),
  2627. ZSTR_VAL(prop2->ce->name),
  2628. zend_get_unmangled_property_name(prop2->name),
  2629. ZSTR_VAL(type2_str)
  2630. );
  2631. zend_string_release(type1_str);
  2632. zend_string_release(type2_str);
  2633. }
  2634. ZEND_API ZEND_COLD void zend_throw_ref_type_error_zval(zend_property_info *prop, zval *zv) {
  2635. zend_string *type_str = zend_type_to_string(prop->type);
  2636. zend_type_error("Cannot assign %s to reference held by property %s::$%s of type %s",
  2637. Z_TYPE_P(zv) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(zv)->name) : zend_get_type_by_const(Z_TYPE_P(zv)),
  2638. ZSTR_VAL(prop->ce->name),
  2639. zend_get_unmangled_property_name(prop->name),
  2640. ZSTR_VAL(type_str)
  2641. );
  2642. zend_string_release(type_str);
  2643. }
  2644. ZEND_API ZEND_COLD void zend_throw_conflicting_coercion_error(zend_property_info *prop1, zend_property_info *prop2, zval *zv) {
  2645. zend_string *type1_str = zend_type_to_string(prop1->type);
  2646. zend_string *type2_str = zend_type_to_string(prop2->type);
  2647. zend_type_error("Cannot assign %s to reference held by property %s::$%s of type %s and property %s::$%s of type %s, as this would result in an inconsistent type conversion",
  2648. Z_TYPE_P(zv) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(zv)->name) : zend_get_type_by_const(Z_TYPE_P(zv)),
  2649. ZSTR_VAL(prop1->ce->name),
  2650. zend_get_unmangled_property_name(prop1->name),
  2651. ZSTR_VAL(type1_str),
  2652. ZSTR_VAL(prop2->ce->name),
  2653. zend_get_unmangled_property_name(prop2->name),
  2654. ZSTR_VAL(type2_str)
  2655. );
  2656. zend_string_release(type1_str);
  2657. zend_string_release(type2_str);
  2658. }
  2659. /* 1: valid, 0: invalid, -1: may be valid after type coercion */
  2660. static zend_always_inline int i_zend_verify_type_assignable_zval(
  2661. zend_property_info *info, zval *zv, zend_bool strict) {
  2662. zend_type type = info->type;
  2663. uint32_t type_mask;
  2664. zend_uchar zv_type = Z_TYPE_P(zv);
  2665. if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(type, zv_type))) {
  2666. return 1;
  2667. }
  2668. if (ZEND_TYPE_HAS_CLASS(type) && zv_type == IS_OBJECT
  2669. && zend_check_and_resolve_property_class_type(info, Z_OBJCE_P(zv))) {
  2670. return 1;
  2671. }
  2672. type_mask = ZEND_TYPE_FULL_MASK(type);
  2673. ZEND_ASSERT(!(type_mask & (MAY_BE_CALLABLE|MAY_BE_STATIC)));
  2674. if (type_mask & MAY_BE_ITERABLE) {
  2675. return zend_is_iterable(zv);
  2676. }
  2677. /* SSTH Exception: IS_LONG may be accepted as IS_DOUBLE (converted) */
  2678. if (strict) {
  2679. if ((type_mask & MAY_BE_DOUBLE) && zv_type == IS_LONG) {
  2680. return -1;
  2681. }
  2682. return 0;
  2683. }
  2684. /* NULL may be accepted only by nullable hints (this is already checked) */
  2685. if (zv_type == IS_NULL) {
  2686. return 0;
  2687. }
  2688. /* Does not contain any type to which a coercion is possible */
  2689. if (!(type_mask & (MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING))
  2690. && (type_mask & MAY_BE_BOOL) != MAY_BE_BOOL) {
  2691. return 0;
  2692. }
  2693. /* Coercion may be necessary, check separately */
  2694. return -1;
  2695. }
  2696. ZEND_API zend_bool ZEND_FASTCALL zend_verify_ref_assignable_zval(zend_reference *ref, zval *zv, zend_bool strict)
  2697. {
  2698. zend_property_info *prop;
  2699. /* The value must satisfy each property type, and coerce to the same value for each property
  2700. * type. Remember the first coerced type and value we've seen for this purpose. */
  2701. zend_property_info *first_prop = NULL;
  2702. zval coerced_value;
  2703. ZVAL_UNDEF(&coerced_value);
  2704. ZEND_ASSERT(Z_TYPE_P(zv) != IS_REFERENCE);
  2705. ZEND_REF_FOREACH_TYPE_SOURCES(ref, prop) {
  2706. int result = i_zend_verify_type_assignable_zval(prop, zv, strict);
  2707. if (result == 0) {
  2708. type_error:
  2709. zend_throw_ref_type_error_zval(prop, zv);
  2710. zval_ptr_dtor(&coerced_value);
  2711. return 0;
  2712. }
  2713. if (result < 0) {
  2714. if (!first_prop) {
  2715. first_prop = prop;
  2716. ZVAL_COPY(&coerced_value, zv);
  2717. if (!zend_verify_weak_scalar_type_hint(
  2718. ZEND_TYPE_FULL_MASK(prop->type), &coerced_value)) {
  2719. goto type_error;
  2720. }
  2721. } else if (Z_ISUNDEF(coerced_value)) {
  2722. /* A previous property did not require coercion, but this one does,
  2723. * so they are incompatible. */
  2724. goto conflicting_coercion_error;
  2725. } else {
  2726. zval tmp;
  2727. ZVAL_COPY(&tmp, zv);
  2728. if (!zend_verify_weak_scalar_type_hint(ZEND_TYPE_FULL_MASK(prop->type), &tmp)) {
  2729. zval_ptr_dtor(&tmp);
  2730. goto type_error;
  2731. }
  2732. if (!zend_is_identical(&coerced_value, &tmp)) {
  2733. zval_ptr_dtor(&tmp);
  2734. goto conflicting_coercion_error;
  2735. }
  2736. }
  2737. } else {
  2738. if (!first_prop) {
  2739. first_prop = prop;
  2740. } else if (!Z_ISUNDEF(coerced_value)) {
  2741. /* A previous property required coercion, but this one doesn't,
  2742. * so they are incompatible. */
  2743. conflicting_coercion_error:
  2744. zend_throw_conflicting_coercion_error(first_prop, prop, zv);
  2745. zval_ptr_dtor(&coerced_value);
  2746. return 0;
  2747. }
  2748. }
  2749. } ZEND_REF_FOREACH_TYPE_SOURCES_END();
  2750. if (!Z_ISUNDEF(coerced_value)) {
  2751. zval_ptr_dtor(zv);
  2752. ZVAL_COPY_VALUE(zv, &coerced_value);
  2753. }
  2754. return 1;
  2755. }
  2756. ZEND_API zval* zend_assign_to_typed_ref(zval *variable_ptr, zval *value, zend_uchar value_type, zend_bool strict, zend_refcounted *ref)
  2757. {
  2758. zend_bool need_copy = ZEND_CONST_COND(value_type & (IS_CONST|IS_CV), 1) ||
  2759. ((value_type & IS_VAR) && UNEXPECTED(ref) && GC_REFCOUNT(ref) > 1);
  2760. zend_bool ret;
  2761. zval tmp;
  2762. if (need_copy) {
  2763. ZVAL_COPY(&tmp, value);
  2764. value = &tmp;
  2765. }
  2766. ret = zend_verify_ref_assignable_zval(Z_REF_P(variable_ptr), value, strict);
  2767. if (need_copy) {
  2768. Z_TRY_DELREF_P(value);
  2769. }
  2770. if (!ret) {
  2771. if (value_type & (IS_VAR|IS_TMP_VAR)) {
  2772. zval_ptr_dtor(value);
  2773. }
  2774. return Z_REFVAL_P(variable_ptr);
  2775. }
  2776. variable_ptr = Z_REFVAL_P(variable_ptr);
  2777. if (EXPECTED(Z_REFCOUNTED_P(variable_ptr))) {
  2778. zend_refcounted *garbage = Z_COUNTED_P(variable_ptr);
  2779. zend_copy_to_variable(variable_ptr, value, value_type, ref);
  2780. if (GC_DELREF(garbage) == 0) {
  2781. rc_dtor_func(garbage);
  2782. } else { /* we need to split */
  2783. /* optimized version of GC_ZVAL_CHECK_POSSIBLE_ROOT(variable_ptr) */
  2784. if (UNEXPECTED(GC_MAY_LEAK(garbage))) {
  2785. gc_possible_root(garbage);
  2786. }
  2787. }
  2788. return variable_ptr;
  2789. }
  2790. zend_copy_to_variable(variable_ptr, value, value_type, ref);
  2791. return variable_ptr;
  2792. }
  2793. ZEND_API zend_bool ZEND_FASTCALL zend_verify_prop_assignable_by_ref(zend_property_info *prop_info, zval *orig_val, zend_bool strict) {
  2794. zval *val = orig_val;
  2795. if (Z_ISREF_P(val) && ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(val))) {
  2796. int result;
  2797. val = Z_REFVAL_P(val);
  2798. result = i_zend_verify_type_assignable_zval(prop_info, val, strict);
  2799. if (result > 0) {
  2800. return 1;
  2801. }
  2802. if (result < 0) {
  2803. /* This is definitely an error, but we still need to determined why: Either because
  2804. * the value is simply illegal for the type, or because or a conflicting coercion. */
  2805. zval tmp;
  2806. ZVAL_COPY(&tmp, val);
  2807. if (zend_verify_weak_scalar_type_hint(ZEND_TYPE_FULL_MASK(prop_info->type), &tmp)) {
  2808. zend_property_info *ref_prop = ZEND_REF_FIRST_SOURCE(Z_REF_P(orig_val));
  2809. zend_throw_ref_type_error_type(ref_prop, prop_info, val);
  2810. zval_ptr_dtor(&tmp);
  2811. return 0;
  2812. }
  2813. zval_ptr_dtor(&tmp);
  2814. }
  2815. } else {
  2816. ZVAL_DEREF(val);
  2817. if (i_zend_check_property_type(prop_info, val, strict)) {
  2818. return 1;
  2819. }
  2820. }
  2821. zend_verify_property_type_error(prop_info, val);
  2822. return 0;
  2823. }
  2824. ZEND_API void ZEND_FASTCALL zend_ref_add_type_source(zend_property_info_source_list *source_list, zend_property_info *prop)
  2825. {
  2826. zend_property_info_list *list;
  2827. if (source_list->ptr == NULL) {
  2828. source_list->ptr = prop;
  2829. return;
  2830. }
  2831. list = ZEND_PROPERTY_INFO_SOURCE_TO_LIST(source_list->list);
  2832. if (!ZEND_PROPERTY_INFO_SOURCE_IS_LIST(source_list->list)) {
  2833. list = emalloc(sizeof(zend_property_info_list) + (4 - 1) * sizeof(zend_property_info *));
  2834. list->ptr[0] = source_list->ptr;
  2835. list->num_allocated = 4;
  2836. list->num = 1;
  2837. } else if (list->num_allocated == list->num) {
  2838. list->num_allocated = list->num * 2;
  2839. list = erealloc(list, sizeof(zend_property_info_list) + (list->num_allocated - 1) * sizeof(zend_property_info *));
  2840. }
  2841. list->ptr[list->num++] = prop;
  2842. source_list->list = ZEND_PROPERTY_INFO_SOURCE_FROM_LIST(list);
  2843. }
  2844. ZEND_API void ZEND_FASTCALL zend_ref_del_type_source(zend_property_info_source_list *source_list, zend_property_info *prop)
  2845. {
  2846. zend_property_info_list *list = ZEND_PROPERTY_INFO_SOURCE_TO_LIST(source_list->list);
  2847. zend_property_info **ptr, **end;
  2848. if (!ZEND_PROPERTY_INFO_SOURCE_IS_LIST(source_list->list)) {
  2849. ZEND_ASSERT(source_list->ptr == prop);
  2850. source_list->ptr = NULL;
  2851. return;
  2852. }
  2853. if (list->num == 1) {
  2854. ZEND_ASSERT(*list->ptr == prop);
  2855. efree(list);
  2856. source_list->ptr = NULL;
  2857. return;
  2858. }
  2859. /* Checking against end here to get a more graceful failure mode if we missed adding a type
  2860. * source at some point. */
  2861. ptr = list->ptr;
  2862. end = ptr + list->num;
  2863. while (ptr < end && *ptr != prop) {
  2864. ptr++;
  2865. }
  2866. ZEND_ASSERT(*ptr == prop);
  2867. /* Copy the last list element into the deleted slot. */
  2868. *ptr = list->ptr[--list->num];
  2869. if (list->num >= 4 && list->num * 4 == list->num_allocated) {
  2870. list->num_allocated = list->num * 2;
  2871. source_list->list = ZEND_PROPERTY_INFO_SOURCE_FROM_LIST(erealloc(list, sizeof(zend_property_info_list) + (list->num_allocated - 1) * sizeof(zend_property_info *)));
  2872. }
  2873. }
  2874. static zend_never_inline void zend_fetch_this_var(int type OPLINE_DC EXECUTE_DATA_DC)
  2875. {
  2876. zval *result = EX_VAR(opline->result.var);
  2877. switch (type) {
  2878. case BP_VAR_R:
  2879. if (EXPECTED(Z_TYPE(EX(This)) == IS_OBJECT)) {
  2880. ZVAL_OBJ(result, Z_OBJ(EX(This)));
  2881. Z_ADDREF_P(result);
  2882. } else {
  2883. ZVAL_NULL(result);
  2884. zend_error(E_WARNING, "Undefined variable $this");
  2885. }
  2886. break;
  2887. case BP_VAR_IS:
  2888. if (EXPECTED(Z_TYPE(EX(This)) == IS_OBJECT)) {
  2889. ZVAL_OBJ(result, Z_OBJ(EX(This)));
  2890. Z_ADDREF_P(result);
  2891. } else {
  2892. ZVAL_NULL(result);
  2893. }
  2894. break;
  2895. case BP_VAR_RW:
  2896. case BP_VAR_W:
  2897. ZVAL_UNDEF(result);
  2898. zend_throw_error(NULL, "Cannot re-assign $this");
  2899. break;
  2900. case BP_VAR_UNSET:
  2901. ZVAL_UNDEF(result);
  2902. zend_throw_error(NULL, "Cannot unset $this");
  2903. break;
  2904. EMPTY_SWITCH_DEFAULT_CASE()
  2905. }
  2906. }
  2907. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_wrong_clone_call(zend_function *clone, zend_class_entry *scope)
  2908. {
  2909. zend_throw_error(NULL, "Call to %s %s::__clone() from context '%s'", zend_visibility_string(clone->common.fn_flags), ZSTR_VAL(clone->common.scope->name), scope ? ZSTR_VAL(scope->name) : "");
  2910. }
  2911. #if ZEND_INTENSIVE_DEBUGGING
  2912. #define CHECK_SYMBOL_TABLES() \
  2913. zend_hash_apply(&EG(symbol_table), zend_check_symbol); \
  2914. if (&EG(symbol_table)!=EX(symbol_table)) { \
  2915. zend_hash_apply(EX(symbol_table), zend_check_symbol); \
  2916. }
  2917. static int zend_check_symbol(zval *pz)
  2918. {
  2919. if (Z_TYPE_P(pz) == IS_INDIRECT) {
  2920. pz = Z_INDIRECT_P(pz);
  2921. }
  2922. if (Z_TYPE_P(pz) > 10) {
  2923. fprintf(stderr, "Warning! %x has invalid type!\n", *pz);
  2924. /* See http://support.microsoft.com/kb/190351 */
  2925. #ifdef ZEND_WIN32
  2926. fflush(stderr);
  2927. #endif
  2928. } else if (Z_TYPE_P(pz) == IS_ARRAY) {
  2929. zend_hash_apply(Z_ARRVAL_P(pz), zend_check_symbol);
  2930. } else if (Z_TYPE_P(pz) == IS_OBJECT) {
  2931. /* OBJ-TBI - doesn't support new object model! */
  2932. zend_hash_apply(Z_OBJPROP_P(pz), zend_check_symbol);
  2933. }
  2934. return 0;
  2935. }
  2936. #else
  2937. #define CHECK_SYMBOL_TABLES()
  2938. #endif
  2939. ZEND_API void execute_internal(zend_execute_data *execute_data, zval *return_value)
  2940. {
  2941. execute_data->func->internal_function.handler(execute_data, return_value);
  2942. }
  2943. ZEND_API void zend_clean_and_cache_symbol_table(zend_array *symbol_table) /* {{{ */
  2944. {
  2945. if (EG(symtable_cache_ptr) >= EG(symtable_cache_limit)) {
  2946. zend_array_destroy(symbol_table);
  2947. } else {
  2948. /* clean before putting into the cache, since clean
  2949. could call dtors, which could use cached hash */
  2950. zend_symtable_clean(symbol_table);
  2951. *(EG(symtable_cache_ptr)++) = symbol_table;
  2952. }
  2953. }
  2954. /* }}} */
  2955. static zend_always_inline void i_free_compiled_variables(zend_execute_data *execute_data) /* {{{ */
  2956. {
  2957. zval *cv = EX_VAR_NUM(0);
  2958. int count = EX(func)->op_array.last_var;
  2959. while (EXPECTED(count != 0)) {
  2960. i_zval_ptr_dtor(cv);
  2961. cv++;
  2962. count--;
  2963. }
  2964. }
  2965. /* }}} */
  2966. ZEND_API void zend_free_compiled_variables(zend_execute_data *execute_data) /* {{{ */
  2967. {
  2968. i_free_compiled_variables(execute_data);
  2969. }
  2970. /* }}} */
  2971. #define ZEND_VM_INTERRUPT_CHECK() do { \
  2972. if (UNEXPECTED(EG(vm_interrupt))) { \
  2973. ZEND_VM_INTERRUPT(); \
  2974. } \
  2975. } while (0)
  2976. #define ZEND_VM_LOOP_INTERRUPT_CHECK() do { \
  2977. if (UNEXPECTED(EG(vm_interrupt))) { \
  2978. ZEND_VM_LOOP_INTERRUPT(); \
  2979. } \
  2980. } while (0)
  2981. /*
  2982. * Stack Frame Layout (the whole stack frame is allocated at once)
  2983. * ==================
  2984. *
  2985. * +========================================+
  2986. * EG(current_execute_data) -> | zend_execute_data |
  2987. * +----------------------------------------+
  2988. * EX_VAR_NUM(0) --------> | VAR[0] = ARG[1] |
  2989. * | ... |
  2990. * | VAR[op_array->num_args-1] = ARG[N] |
  2991. * | ... |
  2992. * | VAR[op_array->last_var-1] |
  2993. * | VAR[op_array->last_var] = TMP[0] |
  2994. * | ... |
  2995. * | VAR[op_array->last_var+op_array->T-1] |
  2996. * | ARG[N+1] (extra_args) |
  2997. * | ... |
  2998. * +----------------------------------------+
  2999. */
  3000. /* zend_copy_extra_args is used when the actually passed number of arguments
  3001. * (EX_NUM_ARGS) is greater than what the function defined (op_array->num_args).
  3002. *
  3003. * The extra arguments will be copied into the call frame after all the compiled variables.
  3004. *
  3005. * If there are extra arguments copied, a flag "ZEND_CALL_FREE_EXTRA_ARGS" will be set
  3006. * on the zend_execute_data, and when the executor leaves the function, the
  3007. * args will be freed in zend_leave_helper.
  3008. */
  3009. static zend_never_inline void zend_copy_extra_args(EXECUTE_DATA_D)
  3010. {
  3011. zend_op_array *op_array = &EX(func)->op_array;
  3012. uint32_t first_extra_arg = op_array->num_args;
  3013. uint32_t num_args = EX_NUM_ARGS();
  3014. zval *src;
  3015. size_t delta;
  3016. uint32_t count;
  3017. uint32_t type_flags = 0;
  3018. if (EXPECTED((op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0)) {
  3019. /* Skip useless ZEND_RECV and ZEND_RECV_INIT opcodes */
  3020. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3021. opline += first_extra_arg;
  3022. #else
  3023. EX(opline) += first_extra_arg;
  3024. #endif
  3025. }
  3026. /* move extra args into separate array after all CV and TMP vars */
  3027. src = EX_VAR_NUM(num_args - 1);
  3028. delta = op_array->last_var + op_array->T - first_extra_arg;
  3029. count = num_args - first_extra_arg;
  3030. if (EXPECTED(delta != 0)) {
  3031. delta *= sizeof(zval);
  3032. do {
  3033. type_flags |= Z_TYPE_INFO_P(src);
  3034. ZVAL_COPY_VALUE((zval*)(((char*)src) + delta), src);
  3035. ZVAL_UNDEF(src);
  3036. src--;
  3037. } while (--count);
  3038. if (Z_TYPE_INFO_REFCOUNTED(type_flags)) {
  3039. ZEND_ADD_CALL_FLAG(execute_data, ZEND_CALL_FREE_EXTRA_ARGS);
  3040. }
  3041. } else {
  3042. do {
  3043. if (Z_REFCOUNTED_P(src)) {
  3044. ZEND_ADD_CALL_FLAG(execute_data, ZEND_CALL_FREE_EXTRA_ARGS);
  3045. break;
  3046. }
  3047. src--;
  3048. } while (--count);
  3049. }
  3050. }
  3051. static zend_always_inline void zend_init_cvs(uint32_t first, uint32_t last EXECUTE_DATA_DC)
  3052. {
  3053. if (EXPECTED(first < last)) {
  3054. uint32_t count = last - first;
  3055. zval *var = EX_VAR_NUM(first);
  3056. do {
  3057. ZVAL_UNDEF(var);
  3058. var++;
  3059. } while (--count);
  3060. }
  3061. }
  3062. static zend_always_inline void i_init_func_execute_data(zend_op_array *op_array, zval *return_value, zend_bool may_be_trampoline EXECUTE_DATA_DC) /* {{{ */
  3063. {
  3064. uint32_t first_extra_arg, num_args;
  3065. ZEND_ASSERT(EX(func) == (zend_function*)op_array);
  3066. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3067. opline = op_array->opcodes;
  3068. #else
  3069. EX(opline) = op_array->opcodes;
  3070. #endif
  3071. EX(call) = NULL;
  3072. EX(return_value) = return_value;
  3073. /* Handle arguments */
  3074. first_extra_arg = op_array->num_args;
  3075. num_args = EX_NUM_ARGS();
  3076. if (UNEXPECTED(num_args > first_extra_arg)) {
  3077. if (!may_be_trampoline || EXPECTED(!(op_array->fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))) {
  3078. zend_copy_extra_args(EXECUTE_DATA_C);
  3079. }
  3080. } else if (EXPECTED((op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0)) {
  3081. /* Skip useless ZEND_RECV and ZEND_RECV_INIT opcodes */
  3082. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3083. opline += num_args;
  3084. #else
  3085. EX(opline) += num_args;
  3086. #endif
  3087. }
  3088. /* Initialize CV variables (skip arguments) */
  3089. zend_init_cvs(num_args, op_array->last_var EXECUTE_DATA_CC);
  3090. EX(run_time_cache) = RUN_TIME_CACHE(op_array);
  3091. EG(current_execute_data) = execute_data;
  3092. }
  3093. /* }}} */
  3094. static zend_always_inline void init_func_run_time_cache_i(zend_op_array *op_array) /* {{{ */
  3095. {
  3096. void **run_time_cache;
  3097. ZEND_ASSERT(RUN_TIME_CACHE(op_array) == NULL);
  3098. run_time_cache = zend_arena_alloc(&CG(arena), op_array->cache_size);
  3099. memset(run_time_cache, 0, op_array->cache_size);
  3100. ZEND_MAP_PTR_SET(op_array->run_time_cache, run_time_cache);
  3101. }
  3102. /* }}} */
  3103. static zend_never_inline void ZEND_FASTCALL init_func_run_time_cache(zend_op_array *op_array) /* {{{ */
  3104. {
  3105. init_func_run_time_cache_i(op_array);
  3106. }
  3107. /* }}} */
  3108. ZEND_API zend_function * ZEND_FASTCALL zend_fetch_function(zend_string *name) /* {{{ */
  3109. {
  3110. zval *zv = zend_hash_find(EG(function_table), name);
  3111. if (EXPECTED(zv != NULL)) {
  3112. zend_function *fbc = Z_FUNC_P(zv);
  3113. if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
  3114. init_func_run_time_cache_i(&fbc->op_array);
  3115. }
  3116. return fbc;
  3117. }
  3118. return NULL;
  3119. } /* }}} */
  3120. ZEND_API zend_function * ZEND_FASTCALL zend_fetch_function_str(const char *name, size_t len) /* {{{ */
  3121. {
  3122. zval *zv = zend_hash_str_find(EG(function_table), name, len);
  3123. if (EXPECTED(zv != NULL)) {
  3124. zend_function *fbc = Z_FUNC_P(zv);
  3125. if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
  3126. init_func_run_time_cache_i(&fbc->op_array);
  3127. }
  3128. return fbc;
  3129. }
  3130. return NULL;
  3131. } /* }}} */
  3132. ZEND_API void ZEND_FASTCALL zend_init_func_run_time_cache(zend_op_array *op_array) /* {{{ */
  3133. {
  3134. if (!RUN_TIME_CACHE(op_array)) {
  3135. init_func_run_time_cache_i(op_array);
  3136. }
  3137. } /* }}} */
  3138. static zend_always_inline void i_init_code_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value) /* {{{ */
  3139. {
  3140. ZEND_ASSERT(EX(func) == (zend_function*)op_array);
  3141. EX(opline) = op_array->opcodes;
  3142. EX(call) = NULL;
  3143. EX(return_value) = return_value;
  3144. zend_attach_symbol_table(execute_data);
  3145. if (!ZEND_MAP_PTR(op_array->run_time_cache)) {
  3146. void *ptr;
  3147. ZEND_ASSERT(op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE);
  3148. ptr = emalloc(op_array->cache_size + sizeof(void*));
  3149. ZEND_MAP_PTR_INIT(op_array->run_time_cache, ptr);
  3150. ptr = (char*)ptr + sizeof(void*);
  3151. ZEND_MAP_PTR_SET(op_array->run_time_cache, ptr);
  3152. memset(ptr, 0, op_array->cache_size);
  3153. }
  3154. EX(run_time_cache) = RUN_TIME_CACHE(op_array);
  3155. EG(current_execute_data) = execute_data;
  3156. }
  3157. /* }}} */
  3158. ZEND_API void zend_init_func_execute_data(zend_execute_data *ex, zend_op_array *op_array, zval *return_value) /* {{{ */
  3159. {
  3160. #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3161. zend_execute_data *orig_execute_data = execute_data;
  3162. #endif
  3163. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3164. const zend_op *orig_opline = opline;
  3165. #endif
  3166. #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3167. execute_data = ex;
  3168. #else
  3169. zend_execute_data *execute_data = ex;
  3170. #endif
  3171. EX(prev_execute_data) = EG(current_execute_data);
  3172. if (!RUN_TIME_CACHE(op_array)) {
  3173. init_func_run_time_cache(op_array);
  3174. }
  3175. i_init_func_execute_data(op_array, return_value, 1 EXECUTE_DATA_CC);
  3176. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3177. EX(opline) = opline;
  3178. opline = orig_opline;
  3179. #endif
  3180. #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3181. execute_data = orig_execute_data;
  3182. #endif
  3183. }
  3184. /* }}} */
  3185. ZEND_API void zend_init_code_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value) /* {{{ */
  3186. {
  3187. EX(prev_execute_data) = EG(current_execute_data);
  3188. i_init_code_execute_data(execute_data, op_array, return_value);
  3189. }
  3190. /* }}} */
  3191. ZEND_API void zend_init_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value) /* {{{ */
  3192. {
  3193. if (EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE) {
  3194. zend_init_code_execute_data(execute_data, op_array, return_value);
  3195. } else {
  3196. zend_init_func_execute_data(execute_data, op_array, return_value);
  3197. }
  3198. }
  3199. /* }}} */
  3200. static zend_execute_data *zend_vm_stack_copy_call_frame(zend_execute_data *call, uint32_t passed_args, uint32_t additional_args) /* {{{ */
  3201. {
  3202. zend_execute_data *new_call;
  3203. int used_stack = (EG(vm_stack_top) - (zval*)call) + additional_args;
  3204. /* copy call frame into new stack segment */
  3205. new_call = zend_vm_stack_extend(used_stack * sizeof(zval));
  3206. *new_call = *call;
  3207. ZEND_ADD_CALL_FLAG(new_call, ZEND_CALL_ALLOCATED);
  3208. if (passed_args) {
  3209. zval *src = ZEND_CALL_ARG(call, 1);
  3210. zval *dst = ZEND_CALL_ARG(new_call, 1);
  3211. do {
  3212. ZVAL_COPY_VALUE(dst, src);
  3213. passed_args--;
  3214. src++;
  3215. dst++;
  3216. } while (passed_args);
  3217. }
  3218. /* delete old call_frame from previous stack segment */
  3219. EG(vm_stack)->prev->top = (zval*)call;
  3220. /* delete previous stack segment if it became empty */
  3221. if (UNEXPECTED(EG(vm_stack)->prev->top == ZEND_VM_STACK_ELEMENTS(EG(vm_stack)->prev))) {
  3222. zend_vm_stack r = EG(vm_stack)->prev;
  3223. EG(vm_stack)->prev = r->prev;
  3224. efree(r);
  3225. }
  3226. return new_call;
  3227. }
  3228. /* }}} */
  3229. static zend_always_inline void zend_vm_stack_extend_call_frame(zend_execute_data **call, uint32_t passed_args, uint32_t additional_args) /* {{{ */
  3230. {
  3231. if (EXPECTED((uint32_t)(EG(vm_stack_end) - EG(vm_stack_top)) > additional_args)) {
  3232. EG(vm_stack_top) += additional_args;
  3233. } else {
  3234. *call = zend_vm_stack_copy_call_frame(*call, passed_args, additional_args);
  3235. }
  3236. }
  3237. /* }}} */
  3238. static zend_always_inline zend_generator *zend_get_running_generator(EXECUTE_DATA_D) /* {{{ */
  3239. {
  3240. /* The generator object is stored in EX(return_value) */
  3241. zend_generator *generator = (zend_generator *) EX(return_value);
  3242. /* However control may currently be delegated to another generator.
  3243. * That's the one we're interested in. */
  3244. return generator;
  3245. }
  3246. /* }}} */
  3247. static void cleanup_unfinished_calls(zend_execute_data *execute_data, uint32_t op_num) /* {{{ */
  3248. {
  3249. if (UNEXPECTED(EX(call))) {
  3250. zend_execute_data *call = EX(call);
  3251. zend_op *opline = EX(func)->op_array.opcodes + op_num;
  3252. int level;
  3253. int do_exit;
  3254. if (UNEXPECTED(opline->opcode == ZEND_INIT_FCALL ||
  3255. opline->opcode == ZEND_INIT_FCALL_BY_NAME ||
  3256. opline->opcode == ZEND_INIT_NS_FCALL_BY_NAME ||
  3257. opline->opcode == ZEND_INIT_DYNAMIC_CALL ||
  3258. opline->opcode == ZEND_INIT_USER_CALL ||
  3259. opline->opcode == ZEND_INIT_METHOD_CALL ||
  3260. opline->opcode == ZEND_INIT_STATIC_METHOD_CALL ||
  3261. opline->opcode == ZEND_NEW)) {
  3262. ZEND_ASSERT(op_num);
  3263. opline--;
  3264. }
  3265. do {
  3266. /* If the exception was thrown during a function call there might be
  3267. * arguments pushed to the stack that have to be dtor'ed. */
  3268. /* find the number of actually passed arguments */
  3269. level = 0;
  3270. do_exit = 0;
  3271. do {
  3272. switch (opline->opcode) {
  3273. case ZEND_DO_FCALL:
  3274. case ZEND_DO_ICALL:
  3275. case ZEND_DO_UCALL:
  3276. case ZEND_DO_FCALL_BY_NAME:
  3277. level++;
  3278. break;
  3279. case ZEND_INIT_FCALL:
  3280. case ZEND_INIT_FCALL_BY_NAME:
  3281. case ZEND_INIT_NS_FCALL_BY_NAME:
  3282. case ZEND_INIT_DYNAMIC_CALL:
  3283. case ZEND_INIT_USER_CALL:
  3284. case ZEND_INIT_METHOD_CALL:
  3285. case ZEND_INIT_STATIC_METHOD_CALL:
  3286. case ZEND_NEW:
  3287. if (level == 0) {
  3288. ZEND_CALL_NUM_ARGS(call) = 0;
  3289. do_exit = 1;
  3290. }
  3291. level--;
  3292. break;
  3293. case ZEND_SEND_VAL:
  3294. case ZEND_SEND_VAL_EX:
  3295. case ZEND_SEND_VAR:
  3296. case ZEND_SEND_VAR_EX:
  3297. case ZEND_SEND_FUNC_ARG:
  3298. case ZEND_SEND_REF:
  3299. case ZEND_SEND_VAR_NO_REF:
  3300. case ZEND_SEND_VAR_NO_REF_EX:
  3301. case ZEND_SEND_USER:
  3302. if (level == 0) {
  3303. ZEND_CALL_NUM_ARGS(call) = opline->op2.num;
  3304. do_exit = 1;
  3305. }
  3306. break;
  3307. case ZEND_SEND_ARRAY:
  3308. case ZEND_SEND_UNPACK:
  3309. if (level == 0) {
  3310. do_exit = 1;
  3311. }
  3312. break;
  3313. }
  3314. if (!do_exit) {
  3315. opline--;
  3316. }
  3317. } while (!do_exit);
  3318. if (call->prev_execute_data) {
  3319. /* skip current call region */
  3320. level = 0;
  3321. do_exit = 0;
  3322. do {
  3323. switch (opline->opcode) {
  3324. case ZEND_DO_FCALL:
  3325. case ZEND_DO_ICALL:
  3326. case ZEND_DO_UCALL:
  3327. case ZEND_DO_FCALL_BY_NAME:
  3328. level++;
  3329. break;
  3330. case ZEND_INIT_FCALL:
  3331. case ZEND_INIT_FCALL_BY_NAME:
  3332. case ZEND_INIT_NS_FCALL_BY_NAME:
  3333. case ZEND_INIT_DYNAMIC_CALL:
  3334. case ZEND_INIT_USER_CALL:
  3335. case ZEND_INIT_METHOD_CALL:
  3336. case ZEND_INIT_STATIC_METHOD_CALL:
  3337. case ZEND_NEW:
  3338. if (level == 0) {
  3339. do_exit = 1;
  3340. }
  3341. level--;
  3342. break;
  3343. }
  3344. opline--;
  3345. } while (!do_exit);
  3346. }
  3347. zend_vm_stack_free_args(EX(call));
  3348. if (ZEND_CALL_INFO(call) & ZEND_CALL_RELEASE_THIS) {
  3349. OBJ_RELEASE(Z_OBJ(call->This));
  3350. }
  3351. if (call->func->common.fn_flags & ZEND_ACC_CLOSURE) {
  3352. zend_object_release(ZEND_CLOSURE_OBJECT(call->func));
  3353. } else if (call->func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
  3354. zend_string_release_ex(call->func->common.function_name, 0);
  3355. zend_free_trampoline(call->func);
  3356. }
  3357. EX(call) = call->prev_execute_data;
  3358. zend_vm_stack_free_call_frame(call);
  3359. call = EX(call);
  3360. } while (call);
  3361. }
  3362. }
  3363. /* }}} */
  3364. static const zend_live_range *find_live_range(const zend_op_array *op_array, uint32_t op_num, uint32_t var_num) /* {{{ */
  3365. {
  3366. int i;
  3367. for (i = 0; i < op_array->last_live_range; i++) {
  3368. const zend_live_range *range = &op_array->live_range[i];
  3369. if (op_num >= range->start && op_num < range->end
  3370. && var_num == (range->var & ~ZEND_LIVE_MASK)) {
  3371. return range;
  3372. }
  3373. }
  3374. return NULL;
  3375. }
  3376. /* }}} */
  3377. static void cleanup_live_vars(zend_execute_data *execute_data, uint32_t op_num, uint32_t catch_op_num) /* {{{ */
  3378. {
  3379. int i;
  3380. for (i = 0; i < EX(func)->op_array.last_live_range; i++) {
  3381. const zend_live_range *range = &EX(func)->op_array.live_range[i];
  3382. if (range->start > op_num) {
  3383. /* further blocks will not be relevant... */
  3384. break;
  3385. } else if (op_num < range->end) {
  3386. if (!catch_op_num || catch_op_num >= range->end) {
  3387. uint32_t kind = range->var & ZEND_LIVE_MASK;
  3388. uint32_t var_num = range->var & ~ZEND_LIVE_MASK;
  3389. zval *var = EX_VAR(var_num);
  3390. if (kind == ZEND_LIVE_TMPVAR) {
  3391. zval_ptr_dtor_nogc(var);
  3392. } else if (kind == ZEND_LIVE_NEW) {
  3393. zend_object *obj;
  3394. ZEND_ASSERT(Z_TYPE_P(var) == IS_OBJECT);
  3395. obj = Z_OBJ_P(var);
  3396. zend_object_store_ctor_failed(obj);
  3397. OBJ_RELEASE(obj);
  3398. } else if (kind == ZEND_LIVE_LOOP) {
  3399. if (Z_TYPE_P(var) != IS_ARRAY && Z_FE_ITER_P(var) != (uint32_t)-1) {
  3400. zend_hash_iterator_del(Z_FE_ITER_P(var));
  3401. }
  3402. zval_ptr_dtor_nogc(var);
  3403. } else if (kind == ZEND_LIVE_ROPE) {
  3404. zend_string **rope = (zend_string **)var;
  3405. zend_op *last = EX(func)->op_array.opcodes + op_num;
  3406. while ((last->opcode != ZEND_ROPE_ADD && last->opcode != ZEND_ROPE_INIT)
  3407. || last->result.var != var_num) {
  3408. ZEND_ASSERT(last >= EX(func)->op_array.opcodes);
  3409. last--;
  3410. }
  3411. if (last->opcode == ZEND_ROPE_INIT) {
  3412. zend_string_release_ex(*rope, 0);
  3413. } else {
  3414. int j = last->extended_value;
  3415. do {
  3416. zend_string_release_ex(rope[j], 0);
  3417. } while (j--);
  3418. }
  3419. } else if (kind == ZEND_LIVE_SILENCE) {
  3420. /* restore previous error_reporting value */
  3421. if (E_HAS_ONLY_FATAL_ERRORS(EG(error_reporting))
  3422. && !E_HAS_ONLY_FATAL_ERRORS(Z_LVAL_P(var))) {
  3423. EG(error_reporting) = Z_LVAL_P(var);
  3424. }
  3425. }
  3426. }
  3427. }
  3428. }
  3429. }
  3430. /* }}} */
  3431. ZEND_API void zend_cleanup_unfinished_execution(zend_execute_data *execute_data, uint32_t op_num, uint32_t catch_op_num) {
  3432. cleanup_unfinished_calls(execute_data, op_num);
  3433. cleanup_live_vars(execute_data, op_num, catch_op_num);
  3434. }
  3435. #if ZEND_VM_SPEC
  3436. static void zend_swap_operands(zend_op *op) /* {{{ */
  3437. {
  3438. znode_op tmp;
  3439. zend_uchar tmp_type;
  3440. tmp = op->op1;
  3441. tmp_type = op->op1_type;
  3442. op->op1 = op->op2;
  3443. op->op1_type = op->op2_type;
  3444. op->op2 = tmp;
  3445. op->op2_type = tmp_type;
  3446. }
  3447. /* }}} */
  3448. #endif
  3449. static zend_never_inline zend_execute_data *zend_init_dynamic_call_string(zend_string *function, uint32_t num_args) /* {{{ */
  3450. {
  3451. zend_function *fbc;
  3452. zval *func;
  3453. zend_class_entry *called_scope;
  3454. zend_string *lcname;
  3455. const char *colon;
  3456. if ((colon = zend_memrchr(ZSTR_VAL(function), ':', ZSTR_LEN(function))) != NULL &&
  3457. colon > ZSTR_VAL(function) &&
  3458. *(colon-1) == ':'
  3459. ) {
  3460. zend_string *mname;
  3461. size_t cname_length = colon - ZSTR_VAL(function) - 1;
  3462. size_t mname_length = ZSTR_LEN(function) - cname_length - (sizeof("::") - 1);
  3463. lcname = zend_string_init(ZSTR_VAL(function), cname_length, 0);
  3464. called_scope = zend_fetch_class_by_name(lcname, NULL, ZEND_FETCH_CLASS_DEFAULT | ZEND_FETCH_CLASS_EXCEPTION);
  3465. if (UNEXPECTED(called_scope == NULL)) {
  3466. zend_string_release_ex(lcname, 0);
  3467. return NULL;
  3468. }
  3469. mname = zend_string_init(ZSTR_VAL(function) + (cname_length + sizeof("::") - 1), mname_length, 0);
  3470. if (called_scope->get_static_method) {
  3471. fbc = called_scope->get_static_method(called_scope, mname);
  3472. } else {
  3473. fbc = zend_std_get_static_method(called_scope, mname, NULL);
  3474. }
  3475. if (UNEXPECTED(fbc == NULL)) {
  3476. if (EXPECTED(!EG(exception))) {
  3477. zend_undefined_method(called_scope, mname);
  3478. }
  3479. zend_string_release_ex(lcname, 0);
  3480. zend_string_release_ex(mname, 0);
  3481. return NULL;
  3482. }
  3483. zend_string_release_ex(lcname, 0);
  3484. zend_string_release_ex(mname, 0);
  3485. if (UNEXPECTED(!(fbc->common.fn_flags & ZEND_ACC_STATIC))) {
  3486. zend_non_static_method_call(fbc);
  3487. return NULL;
  3488. }
  3489. if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
  3490. init_func_run_time_cache(&fbc->op_array);
  3491. }
  3492. } else {
  3493. if (ZSTR_VAL(function)[0] == '\\') {
  3494. lcname = zend_string_alloc(ZSTR_LEN(function) - 1, 0);
  3495. zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(function) + 1, ZSTR_LEN(function) - 1);
  3496. } else {
  3497. lcname = zend_string_tolower(function);
  3498. }
  3499. if (UNEXPECTED((func = zend_hash_find(EG(function_table), lcname)) == NULL)) {
  3500. zend_throw_error(NULL, "Call to undefined function %s()", ZSTR_VAL(function));
  3501. zend_string_release_ex(lcname, 0);
  3502. return NULL;
  3503. }
  3504. zend_string_release_ex(lcname, 0);
  3505. fbc = Z_FUNC_P(func);
  3506. if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
  3507. init_func_run_time_cache(&fbc->op_array);
  3508. }
  3509. called_scope = NULL;
  3510. }
  3511. return zend_vm_stack_push_call_frame(ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC,
  3512. fbc, num_args, called_scope);
  3513. }
  3514. /* }}} */
  3515. static zend_never_inline zend_execute_data *zend_init_dynamic_call_object(zend_object *function, uint32_t num_args) /* {{{ */
  3516. {
  3517. zend_function *fbc;
  3518. void *object_or_called_scope;
  3519. zend_class_entry *called_scope;
  3520. zend_object *object;
  3521. uint32_t call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC;
  3522. if (EXPECTED(function->handlers->get_closure) &&
  3523. EXPECTED(function->handlers->get_closure(function, &called_scope, &fbc, &object, 0) == SUCCESS)) {
  3524. object_or_called_scope = called_scope;
  3525. if (fbc->common.fn_flags & ZEND_ACC_CLOSURE) {
  3526. /* Delay closure destruction until its invocation */
  3527. GC_ADDREF(ZEND_CLOSURE_OBJECT(fbc));
  3528. call_info |= ZEND_CALL_CLOSURE;
  3529. if (fbc->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) {
  3530. call_info |= ZEND_CALL_FAKE_CLOSURE;
  3531. }
  3532. if (object) {
  3533. call_info |= ZEND_CALL_HAS_THIS;
  3534. object_or_called_scope = object;
  3535. }
  3536. } else if (object) {
  3537. call_info |= ZEND_CALL_RELEASE_THIS | ZEND_CALL_HAS_THIS;
  3538. GC_ADDREF(object); /* For $this pointer */
  3539. object_or_called_scope = object;
  3540. }
  3541. } else {
  3542. zend_throw_error(NULL, "Object of type %s is not callable", ZSTR_VAL(function->ce->name));
  3543. return NULL;
  3544. }
  3545. if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
  3546. init_func_run_time_cache(&fbc->op_array);
  3547. }
  3548. return zend_vm_stack_push_call_frame(call_info,
  3549. fbc, num_args, object_or_called_scope);
  3550. }
  3551. /* }}} */
  3552. static zend_never_inline zend_execute_data *zend_init_dynamic_call_array(zend_array *function, uint32_t num_args) /* {{{ */
  3553. {
  3554. zend_function *fbc;
  3555. void *object_or_called_scope;
  3556. uint32_t call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC;
  3557. if (zend_hash_num_elements(function) == 2) {
  3558. zval *obj;
  3559. zval *method;
  3560. obj = zend_hash_index_find(function, 0);
  3561. method = zend_hash_index_find(function, 1);
  3562. if (UNEXPECTED(!obj) || UNEXPECTED(!method)) {
  3563. zend_throw_error(NULL, "Array callback has to contain indices 0 and 1");
  3564. return NULL;
  3565. }
  3566. ZVAL_DEREF(obj);
  3567. if (UNEXPECTED(Z_TYPE_P(obj) != IS_STRING) && UNEXPECTED(Z_TYPE_P(obj) != IS_OBJECT)) {
  3568. zend_throw_error(NULL, "First array member is not a valid class name or object");
  3569. return NULL;
  3570. }
  3571. ZVAL_DEREF(method);
  3572. if (UNEXPECTED(Z_TYPE_P(method) != IS_STRING)) {
  3573. zend_throw_error(NULL, "Second array member is not a valid method");
  3574. return NULL;
  3575. }
  3576. if (Z_TYPE_P(obj) == IS_STRING) {
  3577. zend_class_entry *called_scope = zend_fetch_class_by_name(Z_STR_P(obj), NULL, ZEND_FETCH_CLASS_DEFAULT | ZEND_FETCH_CLASS_EXCEPTION);
  3578. if (UNEXPECTED(called_scope == NULL)) {
  3579. return NULL;
  3580. }
  3581. if (called_scope->get_static_method) {
  3582. fbc = called_scope->get_static_method(called_scope, Z_STR_P(method));
  3583. } else {
  3584. fbc = zend_std_get_static_method(called_scope, Z_STR_P(method), NULL);
  3585. }
  3586. if (UNEXPECTED(fbc == NULL)) {
  3587. if (EXPECTED(!EG(exception))) {
  3588. zend_undefined_method(called_scope, Z_STR_P(method));
  3589. }
  3590. return NULL;
  3591. }
  3592. if (!(fbc->common.fn_flags & ZEND_ACC_STATIC)) {
  3593. zend_non_static_method_call(fbc);
  3594. return NULL;
  3595. }
  3596. object_or_called_scope = called_scope;
  3597. } else {
  3598. zend_object *object = Z_OBJ_P(obj);
  3599. fbc = Z_OBJ_HT_P(obj)->get_method(&object, Z_STR_P(method), NULL);
  3600. if (UNEXPECTED(fbc == NULL)) {
  3601. if (EXPECTED(!EG(exception))) {
  3602. zend_undefined_method(object->ce, Z_STR_P(method));
  3603. }
  3604. return NULL;
  3605. }
  3606. if ((fbc->common.fn_flags & ZEND_ACC_STATIC) != 0) {
  3607. object_or_called_scope = object->ce;
  3608. } else {
  3609. call_info |= ZEND_CALL_RELEASE_THIS | ZEND_CALL_HAS_THIS;
  3610. GC_ADDREF(object); /* For $this pointer */
  3611. object_or_called_scope = object;
  3612. }
  3613. }
  3614. } else {
  3615. zend_throw_error(NULL, "Array callback must have exactly two elements");
  3616. return NULL;
  3617. }
  3618. if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
  3619. init_func_run_time_cache(&fbc->op_array);
  3620. }
  3621. return zend_vm_stack_push_call_frame(call_info,
  3622. fbc, num_args, object_or_called_scope);
  3623. }
  3624. /* }}} */
  3625. #define ZEND_FAKE_OP_ARRAY ((zend_op_array*)(zend_intptr_t)-1)
  3626. static zend_never_inline zend_op_array* ZEND_FASTCALL zend_include_or_eval(zval *inc_filename, int type) /* {{{ */
  3627. {
  3628. zend_op_array *new_op_array = NULL;
  3629. zval tmp_inc_filename;
  3630. ZVAL_UNDEF(&tmp_inc_filename);
  3631. if (Z_TYPE_P(inc_filename) != IS_STRING) {
  3632. zend_string *tmp = zval_try_get_string_func(inc_filename);
  3633. if (UNEXPECTED(!tmp)) {
  3634. return NULL;
  3635. }
  3636. ZVAL_STR(&tmp_inc_filename, tmp);
  3637. inc_filename = &tmp_inc_filename;
  3638. }
  3639. switch (type) {
  3640. case ZEND_INCLUDE_ONCE:
  3641. case ZEND_REQUIRE_ONCE: {
  3642. zend_file_handle file_handle;
  3643. zend_string *resolved_path;
  3644. resolved_path = zend_resolve_path(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename));
  3645. if (EXPECTED(resolved_path)) {
  3646. if (zend_hash_exists(&EG(included_files), resolved_path)) {
  3647. goto already_compiled;
  3648. }
  3649. } else if (UNEXPECTED(EG(exception))) {
  3650. break;
  3651. } else if (UNEXPECTED(strlen(Z_STRVAL_P(inc_filename)) != Z_STRLEN_P(inc_filename))) {
  3652. zend_message_dispatcher(
  3653. (type == ZEND_INCLUDE_ONCE) ?
  3654. ZMSG_FAILED_INCLUDE_FOPEN : ZMSG_FAILED_REQUIRE_FOPEN,
  3655. Z_STRVAL_P(inc_filename));
  3656. break;
  3657. } else {
  3658. resolved_path = zend_string_copy(Z_STR_P(inc_filename));
  3659. }
  3660. if (SUCCESS == zend_stream_open(ZSTR_VAL(resolved_path), &file_handle)) {
  3661. if (!file_handle.opened_path) {
  3662. file_handle.opened_path = zend_string_copy(resolved_path);
  3663. }
  3664. if (zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path)) {
  3665. zend_op_array *op_array = zend_compile_file(&file_handle, (type==ZEND_INCLUDE_ONCE?ZEND_INCLUDE:ZEND_REQUIRE));
  3666. zend_destroy_file_handle(&file_handle);
  3667. zend_string_release_ex(resolved_path, 0);
  3668. if (Z_TYPE(tmp_inc_filename) != IS_UNDEF) {
  3669. zval_ptr_dtor_str(&tmp_inc_filename);
  3670. }
  3671. return op_array;
  3672. } else {
  3673. zend_file_handle_dtor(&file_handle);
  3674. already_compiled:
  3675. new_op_array = ZEND_FAKE_OP_ARRAY;
  3676. }
  3677. } else {
  3678. zend_message_dispatcher(
  3679. (type == ZEND_INCLUDE_ONCE) ?
  3680. ZMSG_FAILED_INCLUDE_FOPEN : ZMSG_FAILED_REQUIRE_FOPEN,
  3681. Z_STRVAL_P(inc_filename));
  3682. }
  3683. zend_string_release_ex(resolved_path, 0);
  3684. }
  3685. break;
  3686. case ZEND_INCLUDE:
  3687. case ZEND_REQUIRE:
  3688. if (UNEXPECTED(strlen(Z_STRVAL_P(inc_filename)) != Z_STRLEN_P(inc_filename))) {
  3689. zend_message_dispatcher(
  3690. (type == ZEND_INCLUDE) ?
  3691. ZMSG_FAILED_INCLUDE_FOPEN : ZMSG_FAILED_REQUIRE_FOPEN,
  3692. Z_STRVAL_P(inc_filename));
  3693. break;
  3694. }
  3695. new_op_array = compile_filename(type, inc_filename);
  3696. break;
  3697. case ZEND_EVAL: {
  3698. char *eval_desc = zend_make_compiled_string_description("eval()'d code");
  3699. new_op_array = zend_compile_string(inc_filename, eval_desc);
  3700. efree(eval_desc);
  3701. }
  3702. break;
  3703. EMPTY_SWITCH_DEFAULT_CASE()
  3704. }
  3705. if (Z_TYPE(tmp_inc_filename) != IS_UNDEF) {
  3706. zval_ptr_dtor_str(&tmp_inc_filename);
  3707. }
  3708. return new_op_array;
  3709. }
  3710. /* }}} */
  3711. static zend_never_inline zend_bool ZEND_FASTCALL zend_fe_reset_iterator(zval *array_ptr, int by_ref OPLINE_DC EXECUTE_DATA_DC) /* {{{ */
  3712. {
  3713. zend_class_entry *ce = Z_OBJCE_P(array_ptr);
  3714. zend_object_iterator *iter = ce->get_iterator(ce, array_ptr, by_ref);
  3715. zend_bool is_empty;
  3716. if (UNEXPECTED(!iter) || UNEXPECTED(EG(exception))) {
  3717. if (iter) {
  3718. OBJ_RELEASE(&iter->std);
  3719. }
  3720. if (!EG(exception)) {
  3721. zend_throw_exception_ex(NULL, 0, "Object of type %s did not create an Iterator", ZSTR_VAL(ce->name));
  3722. }
  3723. ZVAL_UNDEF(EX_VAR(opline->result.var));
  3724. return 1;
  3725. }
  3726. iter->index = 0;
  3727. if (iter->funcs->rewind) {
  3728. iter->funcs->rewind(iter);
  3729. if (UNEXPECTED(EG(exception) != NULL)) {
  3730. OBJ_RELEASE(&iter->std);
  3731. ZVAL_UNDEF(EX_VAR(opline->result.var));
  3732. return 1;
  3733. }
  3734. }
  3735. is_empty = iter->funcs->valid(iter) != SUCCESS;
  3736. if (UNEXPECTED(EG(exception) != NULL)) {
  3737. OBJ_RELEASE(&iter->std);
  3738. ZVAL_UNDEF(EX_VAR(opline->result.var));
  3739. return 1;
  3740. }
  3741. iter->index = -1; /* will be set to 0 before using next handler */
  3742. ZVAL_OBJ(EX_VAR(opline->result.var), &iter->std);
  3743. Z_FE_ITER_P(EX_VAR(opline->result.var)) = (uint32_t)-1;
  3744. return is_empty;
  3745. }
  3746. /* }}} */
  3747. static zend_always_inline int _zend_quick_get_constant(
  3748. const zval *key, uint32_t flags, int check_defined_only OPLINE_DC EXECUTE_DATA_DC) /* {{{ */
  3749. {
  3750. zval *zv;
  3751. zend_constant *c = NULL;
  3752. /* null/true/false are resolved during compilation, so don't check for them here. */
  3753. zv = zend_hash_find_ex(EG(zend_constants), Z_STR_P(key), 1);
  3754. if (zv) {
  3755. c = (zend_constant*)Z_PTR_P(zv);
  3756. } else if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
  3757. key++;
  3758. zv = zend_hash_find_ex(EG(zend_constants), Z_STR_P(key), 1);
  3759. if (zv) {
  3760. c = (zend_constant*)Z_PTR_P(zv);
  3761. }
  3762. }
  3763. if (!c) {
  3764. if (!check_defined_only) {
  3765. zend_throw_error(NULL, "Undefined constant '%s'", Z_STRVAL_P(RT_CONSTANT(opline, opline->op2)));
  3766. ZVAL_UNDEF(EX_VAR(opline->result.var));
  3767. }
  3768. return FAILURE;
  3769. }
  3770. if (!check_defined_only) {
  3771. ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value);
  3772. if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
  3773. zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
  3774. return SUCCESS;
  3775. }
  3776. }
  3777. CACHE_PTR(opline->extended_value, c);
  3778. return SUCCESS;
  3779. }
  3780. /* }}} */
  3781. static zend_never_inline void ZEND_FASTCALL zend_quick_get_constant(
  3782. const zval *key, uint32_t flags OPLINE_DC EXECUTE_DATA_DC) /* {{{ */
  3783. {
  3784. _zend_quick_get_constant(key, flags, 0 OPLINE_CC EXECUTE_DATA_CC);
  3785. } /* }}} */
  3786. static zend_never_inline int ZEND_FASTCALL zend_quick_check_constant(
  3787. const zval *key OPLINE_DC EXECUTE_DATA_DC) /* {{{ */
  3788. {
  3789. return _zend_quick_get_constant(key, 0, 1 OPLINE_CC EXECUTE_DATA_CC);
  3790. } /* }}} */
  3791. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3792. /* Special versions of functions that sets EX(opline) before calling zend_vm_stack_extend() */
  3793. static zend_always_inline zend_execute_data *_zend_vm_stack_push_call_frame_ex(uint32_t used_stack, uint32_t call_info, zend_function *func, uint32_t num_args, void *object_or_called_scope) /* {{{ */
  3794. {
  3795. zend_execute_data *call = (zend_execute_data*)EG(vm_stack_top);
  3796. ZEND_ASSERT_VM_STACK_GLOBAL;
  3797. if (UNEXPECTED(used_stack > (size_t)(((char*)EG(vm_stack_end)) - (char*)call))) {
  3798. EX(opline) = opline; /* this is the only difference */
  3799. call = (zend_execute_data*)zend_vm_stack_extend(used_stack);
  3800. ZEND_ASSERT_VM_STACK_GLOBAL;
  3801. zend_vm_init_call_frame(call, call_info | ZEND_CALL_ALLOCATED, func, num_args, object_or_called_scope);
  3802. return call;
  3803. } else {
  3804. EG(vm_stack_top) = (zval*)((char*)call + used_stack);
  3805. zend_vm_init_call_frame(call, call_info, func, num_args, object_or_called_scope);
  3806. return call;
  3807. }
  3808. } /* }}} */
  3809. static zend_always_inline zend_execute_data *_zend_vm_stack_push_call_frame(uint32_t call_info, zend_function *func, uint32_t num_args, void *object_or_called_scope) /* {{{ */
  3810. {
  3811. uint32_t used_stack = zend_vm_calc_used_stack(num_args, func);
  3812. return _zend_vm_stack_push_call_frame_ex(used_stack, call_info,
  3813. func, num_args, object_or_called_scope);
  3814. } /* }}} */
  3815. #else
  3816. # define _zend_vm_stack_push_call_frame_ex zend_vm_stack_push_call_frame_ex
  3817. # define _zend_vm_stack_push_call_frame zend_vm_stack_push_call_frame
  3818. #endif
  3819. #ifdef ZEND_VM_TRACE_HANDLERS
  3820. # include "zend_vm_trace_handlers.h"
  3821. #elif defined(ZEND_VM_TRACE_MAP)
  3822. # include "zend_vm_trace_map.h"
  3823. #endif
  3824. #define ZEND_VM_NEXT_OPCODE_EX(check_exception, skip) \
  3825. CHECK_SYMBOL_TABLES() \
  3826. if (check_exception) { \
  3827. OPLINE = EX(opline) + (skip); \
  3828. } else { \
  3829. OPLINE = opline + (skip); \
  3830. } \
  3831. ZEND_VM_CONTINUE()
  3832. #define ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION() \
  3833. ZEND_VM_NEXT_OPCODE_EX(1, 1)
  3834. #define ZEND_VM_NEXT_OPCODE() \
  3835. ZEND_VM_NEXT_OPCODE_EX(0, 1)
  3836. #define ZEND_VM_SET_NEXT_OPCODE(new_op) \
  3837. CHECK_SYMBOL_TABLES() \
  3838. OPLINE = new_op
  3839. #define ZEND_VM_SET_OPCODE(new_op) \
  3840. CHECK_SYMBOL_TABLES() \
  3841. OPLINE = new_op; \
  3842. ZEND_VM_INTERRUPT_CHECK()
  3843. #define ZEND_VM_SET_RELATIVE_OPCODE(opline, offset) \
  3844. ZEND_VM_SET_OPCODE(ZEND_OFFSET_TO_OPLINE(opline, offset))
  3845. #define ZEND_VM_JMP_EX(new_op, check_exception) do { \
  3846. if (check_exception && UNEXPECTED(EG(exception))) { \
  3847. HANDLE_EXCEPTION(); \
  3848. } \
  3849. ZEND_VM_SET_OPCODE(new_op); \
  3850. ZEND_VM_CONTINUE(); \
  3851. } while (0)
  3852. #define ZEND_VM_JMP(new_op) \
  3853. ZEND_VM_JMP_EX(new_op, 1)
  3854. #define ZEND_VM_INC_OPCODE() \
  3855. OPLINE++
  3856. #define ZEND_VM_REPEATABLE_OPCODE \
  3857. do {
  3858. #define ZEND_VM_REPEAT_OPCODE(_opcode) \
  3859. } while (UNEXPECTED((++opline)->opcode == _opcode)); \
  3860. OPLINE = opline; \
  3861. ZEND_VM_CONTINUE()
  3862. #define ZEND_VM_SMART_BRANCH(_result, _check) do { \
  3863. if ((_check) && UNEXPECTED(EG(exception))) { \
  3864. OPLINE = EX(opline); \
  3865. } else if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPZ|IS_TMP_VAR))) { \
  3866. if (_result) { \
  3867. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  3868. } else { \
  3869. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  3870. } \
  3871. } else if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPNZ|IS_TMP_VAR))) { \
  3872. if (!(_result)) { \
  3873. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  3874. } else { \
  3875. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  3876. } \
  3877. } else { \
  3878. ZVAL_BOOL(EX_VAR(opline->result.var), _result); \
  3879. ZEND_VM_SET_NEXT_OPCODE(opline + 1); \
  3880. } \
  3881. ZEND_VM_CONTINUE(); \
  3882. } while (0)
  3883. #define ZEND_VM_SMART_BRANCH_JMPZ(_result, _check) do { \
  3884. if ((_check) && UNEXPECTED(EG(exception))) { \
  3885. OPLINE = EX(opline); \
  3886. } else if (_result) { \
  3887. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  3888. } else { \
  3889. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  3890. } \
  3891. ZEND_VM_CONTINUE(); \
  3892. } while (0)
  3893. #define ZEND_VM_SMART_BRANCH_JMPNZ(_result, _check) do { \
  3894. if ((_check) && UNEXPECTED(EG(exception))) { \
  3895. OPLINE = EX(opline); \
  3896. } else if (!(_result)) { \
  3897. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  3898. } else { \
  3899. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  3900. } \
  3901. ZEND_VM_CONTINUE(); \
  3902. } while (0)
  3903. #define ZEND_VM_SMART_BRANCH_NONE(_result, _check) do { \
  3904. ZVAL_BOOL(EX_VAR(opline->result.var), _result); \
  3905. ZEND_VM_NEXT_OPCODE_EX(_check, 1); \
  3906. ZEND_VM_CONTINUE(); \
  3907. } while (0)
  3908. #define ZEND_VM_SMART_BRANCH_TRUE() do { \
  3909. if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPNZ|IS_TMP_VAR))) { \
  3910. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  3911. } else if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPZ|IS_TMP_VAR))) { \
  3912. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  3913. } else { \
  3914. ZVAL_TRUE(EX_VAR(opline->result.var)); \
  3915. ZEND_VM_SET_NEXT_OPCODE(opline + 1); \
  3916. } \
  3917. ZEND_VM_CONTINUE(); \
  3918. } while (0)
  3919. #define ZEND_VM_SMART_BRANCH_TRUE_JMPZ() do { \
  3920. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  3921. ZEND_VM_CONTINUE(); \
  3922. } while (0)
  3923. #define ZEND_VM_SMART_BRANCH_TRUE_JMPNZ() do { \
  3924. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  3925. ZEND_VM_CONTINUE(); \
  3926. } while (0)
  3927. #define ZEND_VM_SMART_BRANCH_TRUE_NONE() do { \
  3928. ZVAL_TRUE(EX_VAR(opline->result.var)); \
  3929. ZEND_VM_NEXT_OPCODE(); \
  3930. } while (0)
  3931. #define ZEND_VM_SMART_BRANCH_FALSE() do { \
  3932. if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPNZ|IS_TMP_VAR))) { \
  3933. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  3934. } else if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPZ|IS_TMP_VAR))) { \
  3935. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  3936. } else { \
  3937. ZVAL_FALSE(EX_VAR(opline->result.var)); \
  3938. ZEND_VM_SET_NEXT_OPCODE(opline + 1); \
  3939. } \
  3940. ZEND_VM_CONTINUE(); \
  3941. } while (0)
  3942. #define ZEND_VM_SMART_BRANCH_FALSE_JMPZ() do { \
  3943. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  3944. ZEND_VM_CONTINUE(); \
  3945. } while (0)
  3946. #define ZEND_VM_SMART_BRANCH_FALSE_JMPNZ() do { \
  3947. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  3948. ZEND_VM_CONTINUE(); \
  3949. } while (0)
  3950. #define ZEND_VM_SMART_BRANCH_FALSE_NONE() do { \
  3951. ZVAL_FALSE(EX_VAR(opline->result.var)); \
  3952. ZEND_VM_NEXT_OPCODE(); \
  3953. } while (0)
  3954. #ifdef __GNUC__
  3955. # define ZEND_VM_GUARD(name) __asm__("#" #name)
  3956. #else
  3957. # define ZEND_VM_GUARD(name)
  3958. #endif
  3959. #define UNDEF_RESULT() do { \
  3960. if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { \
  3961. ZVAL_UNDEF(EX_VAR(opline->result.var)); \
  3962. } \
  3963. } while (0)
  3964. #include "zend_vm_execute.h"
  3965. ZEND_API int zend_set_user_opcode_handler(zend_uchar opcode, user_opcode_handler_t handler)
  3966. {
  3967. if (opcode != ZEND_USER_OPCODE) {
  3968. if (handler == NULL) {
  3969. /* restore the original handler */
  3970. zend_user_opcodes[opcode] = opcode;
  3971. } else {
  3972. zend_user_opcodes[opcode] = ZEND_USER_OPCODE;
  3973. }
  3974. zend_user_opcode_handlers[opcode] = handler;
  3975. return SUCCESS;
  3976. }
  3977. return FAILURE;
  3978. }
  3979. ZEND_API user_opcode_handler_t zend_get_user_opcode_handler(zend_uchar opcode)
  3980. {
  3981. return zend_user_opcode_handlers[opcode];
  3982. }
  3983. ZEND_API zval *zend_get_zval_ptr(const zend_op *opline, int op_type, const znode_op *node, const zend_execute_data *execute_data, int type)
  3984. {
  3985. zval *ret;
  3986. switch (op_type) {
  3987. case IS_CONST:
  3988. ret = RT_CONSTANT(opline, *node);
  3989. break;
  3990. case IS_TMP_VAR:
  3991. case IS_VAR:
  3992. case IS_CV:
  3993. ret = EX_VAR(node->var);
  3994. break;
  3995. default:
  3996. ret = NULL;
  3997. break;
  3998. }
  3999. return ret;
  4000. }