xref: /PHP-8.4/Zend/zend_execute.c (revision 67318e91)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    |          Dmitry Stogov <dmitry@php.net>                              |
18    +----------------------------------------------------------------------+
19 */
20 
21 #define ZEND_INTENSIVE_DEBUGGING 0
22 
23 #include <stdio.h>
24 #include <signal.h>
25 
26 #include "zend.h"
27 #include "zend_compile.h"
28 #include "zend_execute.h"
29 #include "zend_API.h"
30 #include "zend_ptr_stack.h"
31 #include "zend_constants.h"
32 #include "zend_extensions.h"
33 #include "zend_ini.h"
34 #include "zend_exceptions.h"
35 #include "zend_interfaces.h"
36 #include "zend_closures.h"
37 #include "zend_generators.h"
38 #include "zend_vm.h"
39 #include "zend_dtrace.h"
40 #include "zend_inheritance.h"
41 #include "zend_type_info.h"
42 #include "zend_smart_str.h"
43 #include "zend_observer.h"
44 #include "zend_system_id.h"
45 #include "zend_call_stack.h"
46 #include "zend_attributes.h"
47 #include "Optimizer/zend_func_info.h"
48 
49 /* Virtual current working directory support */
50 #include "zend_virtual_cwd.h"
51 
52 #ifdef HAVE_GCC_GLOBAL_REGS
53 # if defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(i386)
54 #  define ZEND_VM_FP_GLOBAL_REG "%esi"
55 #  define ZEND_VM_IP_GLOBAL_REG "%edi"
56 # elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__x86_64__)
57 #  define ZEND_VM_FP_GLOBAL_REG "%r14"
58 #  define ZEND_VM_IP_GLOBAL_REG "%r15"
59 # elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__powerpc64__)
60 #  define ZEND_VM_FP_GLOBAL_REG "r14"
61 #  define ZEND_VM_IP_GLOBAL_REG "r15"
62 # elif defined(__IBMC__) && ZEND_GCC_VERSION >= 4002 && defined(__powerpc64__)
63 #  define ZEND_VM_FP_GLOBAL_REG "r14"
64 #  define ZEND_VM_IP_GLOBAL_REG "r15"
65 # elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__aarch64__)
66 #  define ZEND_VM_FP_GLOBAL_REG "x27"
67 #  define ZEND_VM_IP_GLOBAL_REG "x28"
68 #elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__riscv) && __riscv_xlen == 64
69 #  define ZEND_VM_FP_GLOBAL_REG "x18"
70 #  define ZEND_VM_IP_GLOBAL_REG "x19"
71 # endif
72 #endif
73 
74 #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
75 # pragma GCC diagnostic ignored "-Wvolatile-register-var"
76   register zend_execute_data* volatile execute_data __asm__(ZEND_VM_FP_GLOBAL_REG);
77 # pragma GCC diagnostic warning "-Wvolatile-register-var"
78 #endif
79 
80 #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
81 # define EXECUTE_DATA_D     void
82 # define EXECUTE_DATA_C
83 # define EXECUTE_DATA_DC
84 # define EXECUTE_DATA_CC
85 # define NO_EXECUTE_DATA_CC
86 #else
87 # define EXECUTE_DATA_D     zend_execute_data* execute_data
88 # define EXECUTE_DATA_C     execute_data
89 # define EXECUTE_DATA_DC    , EXECUTE_DATA_D
90 # define EXECUTE_DATA_CC    , EXECUTE_DATA_C
91 # define NO_EXECUTE_DATA_CC , NULL
92 #endif
93 
94 #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
95 # define OPLINE_D           void
96 # define OPLINE_C
97 # define OPLINE_DC
98 # define OPLINE_CC
99 #else
100 # define OPLINE_D           const zend_op* opline
101 # define OPLINE_C           opline
102 # define OPLINE_DC          , OPLINE_D
103 # define OPLINE_CC          , OPLINE_C
104 #endif
105 
106 #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
107 # pragma GCC diagnostic ignored "-Wvolatile-register-var"
108   register const zend_op* volatile opline __asm__(ZEND_VM_IP_GLOBAL_REG);
109 # pragma GCC diagnostic warning "-Wvolatile-register-var"
110 #else
111 #endif
112 
113 #define _CONST_CODE  0
114 #define _TMP_CODE    1
115 #define _VAR_CODE    2
116 #define _UNUSED_CODE 3
117 #define _CV_CODE     4
118 
119 typedef int (ZEND_FASTCALL *incdec_t)(zval *);
120 
121 #define get_zval_ptr(op_type, node, type) _get_zval_ptr(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
122 #define get_zval_ptr_deref(op_type, node, type) _get_zval_ptr_deref(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
123 #define get_zval_ptr_undef(op_type, node, type) _get_zval_ptr_undef(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
124 #define get_op_data_zval_ptr_r(op_type, node) _get_op_data_zval_ptr_r(op_type, node EXECUTE_DATA_CC OPLINE_CC)
125 #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)
126 #define get_zval_ptr_ptr(op_type, node, type) _get_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC)
127 #define get_zval_ptr_ptr_undef(op_type, node, type) _get_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC)
128 #define get_obj_zval_ptr(op_type, node, type) _get_obj_zval_ptr(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
129 #define get_obj_zval_ptr_undef(op_type, node, type) _get_obj_zval_ptr_undef(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
130 #define get_obj_zval_ptr_ptr(op_type, node, type) _get_obj_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC)
131 
132 #define RETURN_VALUE_USED(opline) ((opline)->result_type != IS_UNUSED)
133 
ZEND_FUNCTION(pass)134 static ZEND_FUNCTION(pass)
135 {
136 }
137 
138 ZEND_BEGIN_ARG_INFO_EX(zend_pass_function_arg_info, 0, 0, 0)
139 ZEND_END_ARG_INFO()
140 
141 ZEND_API const zend_internal_function zend_pass_function = {
142 	ZEND_INTERNAL_FUNCTION, /* type              */
143 	{0, 0, 0},              /* arg_flags         */
144 	0,                      /* fn_flags          */
145 	NULL,                   /* name              */
146 	NULL,                   /* scope             */
147 	NULL,                   /* prototype         */
148 	0,                      /* num_args          */
149 	0,                      /* required_num_args */
150 	(zend_internal_arg_info *) zend_pass_function_arg_info + 1, /* arg_info */
151 	NULL,                   /* attributes        */
152 	NULL,                   /* run_time_cache    */
153 	NULL,                   /* doc_comment       */
154 	0,                      /* T                 */
155 	NULL,                   /* prop_info */
156 	ZEND_FN(pass),          /* handler           */
157 	NULL,                   /* module            */
158 	NULL,                   /* frameless_function_infos */
159 	{NULL,NULL,NULL,NULL}   /* reserved          */
160 };
161 
162 #define FREE_VAR_PTR_AND_EXTRACT_RESULT_IF_NECESSARY(free_var) do {			\
163 	zval *__container_to_free = EX_VAR(free_var);							\
164 	if (UNEXPECTED(Z_REFCOUNTED_P(__container_to_free))) {					\
165 		zend_refcounted *__ref = Z_COUNTED_P(__container_to_free);			\
166 		if (UNEXPECTED(!GC_DELREF(__ref))) {								\
167 			zval *__zv = EX_VAR(opline->result.var);						\
168 			if (EXPECTED(Z_TYPE_P(__zv) == IS_INDIRECT)) {					\
169 				ZVAL_COPY(__zv, Z_INDIRECT_P(__zv));						\
170 			}																\
171 			rc_dtor_func(__ref);											\
172 		}																	\
173 	}																		\
174 } while (0)
175 
176 #define FREE_OP(type, var) \
177 	if ((type) & (IS_TMP_VAR|IS_VAR)) { \
178 		zval_ptr_dtor_nogc(EX_VAR(var)); \
179 	}
180 
181 #define CV_DEF_OF(i) (EX(func)->op_array.vars[i])
182 
183 #define ZEND_VM_STACK_PAGE_SLOTS (16 * 1024) /* should be a power of 2 */
184 
185 #define ZEND_VM_STACK_PAGE_SIZE  (ZEND_VM_STACK_PAGE_SLOTS * sizeof(zval))
186 
187 #define ZEND_VM_STACK_PAGE_ALIGNED_SIZE(size, page_size) \
188 	(((size) + ZEND_VM_STACK_HEADER_SLOTS * sizeof(zval) \
189 	  + ((page_size) - 1)) & ~((page_size) - 1))
190 
zend_vm_stack_init(void)191 ZEND_API void zend_vm_stack_init(void)
192 {
193 	EG(vm_stack_page_size) = ZEND_VM_STACK_PAGE_SIZE;
194 	EG(vm_stack) = zend_vm_stack_new_page(ZEND_VM_STACK_PAGE_SIZE, NULL);
195 	EG(vm_stack_top) = EG(vm_stack)->top;
196 	EG(vm_stack_end) = EG(vm_stack)->end;
197 }
198 
zend_vm_stack_init_ex(size_t page_size)199 ZEND_API void zend_vm_stack_init_ex(size_t page_size)
200 {
201 	/* page_size must be a power of 2 */
202 	ZEND_ASSERT(page_size > 0 && (page_size & (page_size - 1)) == 0);
203 	EG(vm_stack_page_size) = page_size;
204 	EG(vm_stack) = zend_vm_stack_new_page(page_size, NULL);
205 	EG(vm_stack_top) = EG(vm_stack)->top;
206 	EG(vm_stack_end) = EG(vm_stack)->end;
207 }
208 
zend_vm_stack_destroy(void)209 ZEND_API void zend_vm_stack_destroy(void)
210 {
211 	zend_vm_stack stack = EG(vm_stack);
212 
213 	while (stack != NULL) {
214 		zend_vm_stack p = stack->prev;
215 		efree(stack);
216 		stack = p;
217 	}
218 }
219 
zend_vm_stack_extend(size_t size)220 ZEND_API void* zend_vm_stack_extend(size_t size)
221 {
222 	zend_vm_stack stack;
223 	void *ptr;
224 
225 	stack = EG(vm_stack);
226 	stack->top = EG(vm_stack_top);
227 	EG(vm_stack) = stack = zend_vm_stack_new_page(
228 		EXPECTED(size < EG(vm_stack_page_size) - (ZEND_VM_STACK_HEADER_SLOTS * sizeof(zval))) ?
229 			EG(vm_stack_page_size) : ZEND_VM_STACK_PAGE_ALIGNED_SIZE(size, EG(vm_stack_page_size)),
230 		stack);
231 	ptr = stack->top;
232 	EG(vm_stack_top) = (void*)(((char*)ptr) + size);
233 	EG(vm_stack_end) = stack->end;
234 	return ptr;
235 }
236 
zend_get_compiled_variable_value(const zend_execute_data * execute_data,uint32_t var)237 ZEND_API zval* zend_get_compiled_variable_value(const zend_execute_data *execute_data, uint32_t var)
238 {
239 	return EX_VAR(var);
240 }
241 
zend_gcc_global_regs(void)242 ZEND_API bool zend_gcc_global_regs(void)
243 {
244   #if defined(HAVE_GCC_GLOBAL_REGS)
245         return 1;
246   #else
247         return 0;
248   #endif
249 }
250 
_get_zval_ptr_tmp(uint32_t var EXECUTE_DATA_DC)251 static zend_always_inline zval *_get_zval_ptr_tmp(uint32_t var EXECUTE_DATA_DC)
252 {
253 	zval *ret = EX_VAR(var);
254 
255 	ZEND_ASSERT(Z_TYPE_P(ret) != IS_REFERENCE);
256 
257 	return ret;
258 }
259 
_get_zval_ptr_var(uint32_t var EXECUTE_DATA_DC)260 static zend_always_inline zval *_get_zval_ptr_var(uint32_t var EXECUTE_DATA_DC)
261 {
262 	zval *ret = EX_VAR(var);
263 
264 	return ret;
265 }
266 
_get_zval_ptr_var_deref(uint32_t var EXECUTE_DATA_DC)267 static zend_always_inline zval *_get_zval_ptr_var_deref(uint32_t var EXECUTE_DATA_DC)
268 {
269 	zval *ret = EX_VAR(var);
270 
271 	ZVAL_DEREF(ret);
272 	return ret;
273 }
274 
zval_undefined_cv(uint32_t var EXECUTE_DATA_DC)275 static zend_never_inline ZEND_COLD zval* zval_undefined_cv(uint32_t var EXECUTE_DATA_DC)
276 {
277 	if (EXPECTED(EG(exception) == NULL)) {
278 		zend_string *cv = CV_DEF_OF(EX_VAR_TO_NUM(var));
279 		zend_error_unchecked(E_WARNING, "Undefined variable $%S", cv);
280 	}
281 	return &EG(uninitialized_zval);
282 }
283 
_zval_undefined_op1(EXECUTE_DATA_D)284 static zend_never_inline ZEND_COLD zval* ZEND_FASTCALL _zval_undefined_op1(EXECUTE_DATA_D)
285 {
286 	return zval_undefined_cv(EX(opline)->op1.var EXECUTE_DATA_CC);
287 }
288 
_zval_undefined_op2(EXECUTE_DATA_D)289 static zend_never_inline ZEND_COLD zval* ZEND_FASTCALL _zval_undefined_op2(EXECUTE_DATA_D)
290 {
291 	return zval_undefined_cv(EX(opline)->op2.var EXECUTE_DATA_CC);
292 }
293 
294 #define ZVAL_UNDEFINED_OP1() _zval_undefined_op1(EXECUTE_DATA_C)
295 #define ZVAL_UNDEFINED_OP2() _zval_undefined_op2(EXECUTE_DATA_C)
296 
_get_zval_cv_lookup(zval * ptr,uint32_t var,int type EXECUTE_DATA_DC)297 static zend_never_inline ZEND_COLD zval *_get_zval_cv_lookup(zval *ptr, uint32_t var, int type EXECUTE_DATA_DC)
298 {
299 	switch (type) {
300 		case BP_VAR_R:
301 		case BP_VAR_UNSET:
302 			ptr = zval_undefined_cv(var EXECUTE_DATA_CC);
303 			break;
304 		case BP_VAR_IS:
305 			ptr = &EG(uninitialized_zval);
306 			break;
307 		case BP_VAR_RW:
308 			zval_undefined_cv(var EXECUTE_DATA_CC);
309 			ZEND_FALLTHROUGH;
310 		case BP_VAR_W:
311 			ZVAL_NULL(ptr);
312 			break;
313 	}
314 	return ptr;
315 }
316 
_get_zval_ptr_cv(uint32_t var,int type EXECUTE_DATA_DC)317 static zend_always_inline zval *_get_zval_ptr_cv(uint32_t var, int type EXECUTE_DATA_DC)
318 {
319 	zval *ret = EX_VAR(var);
320 
321 	if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
322 		if (type == BP_VAR_W) {
323 			ZVAL_NULL(ret);
324 		} else {
325 			return _get_zval_cv_lookup(ret, var, type EXECUTE_DATA_CC);
326 		}
327 	}
328 	return ret;
329 }
330 
_get_zval_ptr_cv_deref(uint32_t var,int type EXECUTE_DATA_DC)331 static zend_always_inline zval *_get_zval_ptr_cv_deref(uint32_t var, int type EXECUTE_DATA_DC)
332 {
333 	zval *ret = EX_VAR(var);
334 
335 	if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
336 		if (type == BP_VAR_W) {
337 			ZVAL_NULL(ret);
338 			return ret;
339 		} else {
340 			return _get_zval_cv_lookup(ret, var, type EXECUTE_DATA_CC);
341 		}
342 	}
343 	ZVAL_DEREF(ret);
344 	return ret;
345 }
346 
_get_zval_ptr_cv_BP_VAR_R(uint32_t var EXECUTE_DATA_DC)347 static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_R(uint32_t var EXECUTE_DATA_DC)
348 {
349 	zval *ret = EX_VAR(var);
350 
351 	if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
352 		return zval_undefined_cv(var EXECUTE_DATA_CC);
353 	}
354 	return ret;
355 }
356 
_get_zval_ptr_cv_deref_BP_VAR_R(uint32_t var EXECUTE_DATA_DC)357 static zend_always_inline zval *_get_zval_ptr_cv_deref_BP_VAR_R(uint32_t var EXECUTE_DATA_DC)
358 {
359 	zval *ret = EX_VAR(var);
360 
361 	if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
362 		return zval_undefined_cv(var EXECUTE_DATA_CC);
363 	}
364 	ZVAL_DEREF(ret);
365 	return ret;
366 }
367 
_get_zval_ptr_cv_BP_VAR_IS(uint32_t var EXECUTE_DATA_DC)368 static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_IS(uint32_t var EXECUTE_DATA_DC)
369 {
370 	zval *ret = EX_VAR(var);
371 
372 	return ret;
373 }
374 
_get_zval_ptr_cv_BP_VAR_RW(uint32_t var EXECUTE_DATA_DC)375 static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_RW(uint32_t var EXECUTE_DATA_DC)
376 {
377 	zval *ret = EX_VAR(var);
378 
379 	if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
380 		zval_undefined_cv(var EXECUTE_DATA_CC);
381 		ZVAL_NULL(ret);
382 		return ret;
383 	}
384 	return ret;
385 }
386 
_get_zval_ptr_cv_BP_VAR_W(uint32_t var EXECUTE_DATA_DC)387 static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_W(uint32_t var EXECUTE_DATA_DC)
388 {
389 	zval *ret = EX_VAR(var);
390 
391 	if (Z_TYPE_P(ret) == IS_UNDEF) {
392 		ZVAL_NULL(ret);
393 	}
394 	return ret;
395 }
396 
_get_zval_ptr_tmpvarcv(int op_type,znode_op node,int type EXECUTE_DATA_DC)397 static zend_always_inline zval *_get_zval_ptr_tmpvarcv(int op_type, znode_op node, int type EXECUTE_DATA_DC)
398 {
399 	if (op_type & (IS_TMP_VAR|IS_VAR)) {
400 		if (op_type == IS_TMP_VAR) {
401 			return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
402 		} else {
403 			ZEND_ASSERT(op_type == IS_VAR);
404 			return _get_zval_ptr_var_deref(node.var EXECUTE_DATA_CC);
405 		}
406 	} else {
407 		ZEND_ASSERT(op_type == IS_CV);
408 		return _get_zval_ptr_cv_deref(node.var, type EXECUTE_DATA_CC);
409 	}
410 }
411 
_get_zval_ptr(int op_type,znode_op node,int type EXECUTE_DATA_DC OPLINE_DC)412 static zend_always_inline zval *_get_zval_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC)
413 {
414 	if (op_type & (IS_TMP_VAR|IS_VAR)) {
415 		if (!ZEND_DEBUG || op_type == IS_VAR) {
416 			return _get_zval_ptr_var(node.var EXECUTE_DATA_CC);
417 		} else {
418 			ZEND_ASSERT(op_type == IS_TMP_VAR);
419 			return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
420 		}
421 	} else {
422 		if (op_type == IS_CONST) {
423 			return RT_CONSTANT(opline, node);
424 		} else if (op_type == IS_CV) {
425 			return _get_zval_ptr_cv(node.var, type EXECUTE_DATA_CC);
426 		} else {
427 			return NULL;
428 		}
429 	}
430 }
431 
_get_op_data_zval_ptr_r(int op_type,znode_op node EXECUTE_DATA_DC OPLINE_DC)432 static zend_always_inline zval *_get_op_data_zval_ptr_r(int op_type, znode_op node EXECUTE_DATA_DC OPLINE_DC)
433 {
434 	if (op_type & (IS_TMP_VAR|IS_VAR)) {
435 		if (!ZEND_DEBUG || op_type == IS_VAR) {
436 			return _get_zval_ptr_var(node.var EXECUTE_DATA_CC);
437 		} else {
438 			ZEND_ASSERT(op_type == IS_TMP_VAR);
439 			return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
440 		}
441 	} else {
442 		if (op_type == IS_CONST) {
443 			return RT_CONSTANT(opline + 1, node);
444 		} else if (op_type == IS_CV) {
445 			return _get_zval_ptr_cv_BP_VAR_R(node.var EXECUTE_DATA_CC);
446 		} else {
447 			return NULL;
448 		}
449 	}
450 }
451 
_get_zval_ptr_deref(int op_type,znode_op node,int type EXECUTE_DATA_DC OPLINE_DC)452 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)
453 {
454 	if (op_type & (IS_TMP_VAR|IS_VAR)) {
455 		if (op_type == IS_TMP_VAR) {
456 			return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
457 		} else {
458 			ZEND_ASSERT(op_type == IS_VAR);
459 			return _get_zval_ptr_var_deref(node.var EXECUTE_DATA_CC);
460 		}
461 	} else {
462 		if (op_type == IS_CONST) {
463 			return RT_CONSTANT(opline, node);
464 		} else if (op_type == IS_CV) {
465 			return _get_zval_ptr_cv_deref(node.var, type EXECUTE_DATA_CC);
466 		} else {
467 			return NULL;
468 		}
469 	}
470 }
471 
_get_op_data_zval_ptr_deref_r(int op_type,znode_op node EXECUTE_DATA_DC OPLINE_DC)472 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)
473 {
474 	if (op_type & (IS_TMP_VAR|IS_VAR)) {
475 		if (op_type == IS_TMP_VAR) {
476 			return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
477 		} else {
478 			ZEND_ASSERT(op_type == IS_VAR);
479 			return _get_zval_ptr_var_deref(node.var EXECUTE_DATA_CC);
480 		}
481 	} else {
482 		if (op_type == IS_CONST) {
483 			return RT_CONSTANT(opline + 1, node);
484 		} else if (op_type == IS_CV) {
485 			return _get_zval_ptr_cv_deref_BP_VAR_R(node.var EXECUTE_DATA_CC);
486 		} else {
487 			return NULL;
488 		}
489 	}
490 }
491 
_get_zval_ptr_undef(int op_type,znode_op node,int type EXECUTE_DATA_DC OPLINE_DC)492 static zend_always_inline zval *_get_zval_ptr_undef(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC)
493 {
494 	if (op_type & (IS_TMP_VAR|IS_VAR)) {
495 		if (!ZEND_DEBUG || op_type == IS_VAR) {
496 			return _get_zval_ptr_var(node.var EXECUTE_DATA_CC);
497 		} else {
498 			ZEND_ASSERT(op_type == IS_TMP_VAR);
499 			return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
500 		}
501 	} else {
502 		if (op_type == IS_CONST) {
503 			return RT_CONSTANT(opline, node);
504 		} else if (op_type == IS_CV) {
505 			return EX_VAR(node.var);
506 		} else {
507 			return NULL;
508 		}
509 	}
510 }
511 
_get_zval_ptr_ptr_var(uint32_t var EXECUTE_DATA_DC)512 static zend_always_inline zval *_get_zval_ptr_ptr_var(uint32_t var EXECUTE_DATA_DC)
513 {
514 	zval *ret = EX_VAR(var);
515 
516 	if (EXPECTED(Z_TYPE_P(ret) == IS_INDIRECT)) {
517 		ret = Z_INDIRECT_P(ret);
518 	}
519 	return ret;
520 }
521 
_get_zval_ptr_ptr(int op_type,znode_op node,int type EXECUTE_DATA_DC)522 static inline zval *_get_zval_ptr_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC)
523 {
524 	if (op_type == IS_CV) {
525 		return _get_zval_ptr_cv(node.var, type EXECUTE_DATA_CC);
526 	} else /* if (op_type == IS_VAR) */ {
527 		ZEND_ASSERT(op_type == IS_VAR);
528 		return _get_zval_ptr_ptr_var(node.var EXECUTE_DATA_CC);
529 	}
530 }
531 
_get_obj_zval_ptr(int op_type,znode_op op,int type EXECUTE_DATA_DC OPLINE_DC)532 static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr(int op_type, znode_op op, int type EXECUTE_DATA_DC OPLINE_DC)
533 {
534 	if (op_type == IS_UNUSED) {
535 		return &EX(This);
536 	}
537 	return get_zval_ptr(op_type, op, type);
538 }
539 
_get_obj_zval_ptr_undef(int op_type,znode_op op,int type EXECUTE_DATA_DC OPLINE_DC)540 static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_undef(int op_type, znode_op op, int type EXECUTE_DATA_DC OPLINE_DC)
541 {
542 	if (op_type == IS_UNUSED) {
543 		return &EX(This);
544 	}
545 	return get_zval_ptr_undef(op_type, op, type);
546 }
547 
_get_obj_zval_ptr_ptr(int op_type,znode_op node,int type EXECUTE_DATA_DC)548 static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC)
549 {
550 	if (op_type == IS_UNUSED) {
551 		return &EX(This);
552 	}
553 	return get_zval_ptr_ptr(op_type, node, type);
554 }
555 
zend_assign_to_variable_reference(zval * variable_ptr,zval * value_ptr,zend_refcounted ** garbage_ptr)556 static inline void zend_assign_to_variable_reference(zval *variable_ptr, zval *value_ptr, zend_refcounted **garbage_ptr)
557 {
558 	zend_reference *ref;
559 
560 	if (EXPECTED(!Z_ISREF_P(value_ptr))) {
561 		ZVAL_NEW_REF(value_ptr, value_ptr);
562 	} else if (UNEXPECTED(variable_ptr == value_ptr)) {
563 		return;
564 	}
565 
566 	ref = Z_REF_P(value_ptr);
567 	GC_ADDREF(ref);
568 	if (Z_REFCOUNTED_P(variable_ptr)) {
569 		*garbage_ptr = Z_COUNTED_P(variable_ptr);
570 	}
571 	ZVAL_REF(variable_ptr, ref);
572 }
573 
zend_assign_to_typed_property_reference(zend_property_info * prop_info,zval * prop,zval * value_ptr,zend_refcounted ** garbage_ptr EXECUTE_DATA_DC)574 static zend_never_inline zval* zend_assign_to_typed_property_reference(zend_property_info *prop_info, zval *prop, zval *value_ptr, zend_refcounted **garbage_ptr EXECUTE_DATA_DC)
575 {
576 	if (!zend_verify_prop_assignable_by_ref(prop_info, value_ptr, EX_USES_STRICT_TYPES())) {
577 		return &EG(uninitialized_zval);
578 	}
579 	if (Z_ISREF_P(prop)) {
580 		ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(prop), prop_info);
581 	}
582 	zend_assign_to_variable_reference(prop, value_ptr, garbage_ptr);
583 	ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(prop), prop_info);
584 	return prop;
585 }
586 
zend_wrong_assign_to_variable_reference(zval * variable_ptr,zval * value_ptr,zend_refcounted ** garbage_ptr OPLINE_DC EXECUTE_DATA_DC)587 static zend_never_inline ZEND_COLD zval *zend_wrong_assign_to_variable_reference(zval *variable_ptr, zval *value_ptr, zend_refcounted **garbage_ptr OPLINE_DC EXECUTE_DATA_DC)
588 {
589 	zend_error(E_NOTICE, "Only variables should be assigned by reference");
590 	if (UNEXPECTED(EG(exception) != NULL)) {
591 		return &EG(uninitialized_zval);
592 	}
593 
594 	/* Use IS_TMP_VAR instead of IS_VAR to avoid ISREF check */
595 	Z_TRY_ADDREF_P(value_ptr);
596 	return zend_assign_to_variable_ex(variable_ptr, value_ptr, IS_TMP_VAR, EX_USES_STRICT_TYPES(), garbage_ptr);
597 }
598 
zend_cannot_pass_by_reference(uint32_t arg_num)599 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_cannot_pass_by_reference(uint32_t arg_num)
600 {
601 	const zend_execute_data *execute_data = EG(current_execute_data);
602 	zend_string *func_name = get_function_or_method_name(EX(call)->func);
603 	const char *param_name = get_function_arg_name(EX(call)->func, arg_num);
604 
605 	zend_throw_error(NULL, "%s(): Argument #%d%s%s%s could not be passed by reference",
606 		ZSTR_VAL(func_name), arg_num, param_name ? " ($" : "", param_name ? param_name : "", param_name ? ")" : ""
607 	);
608 
609 	zend_string_release(func_name);
610 }
611 
zend_throw_auto_init_in_prop_error(zend_property_info * prop)612 static zend_never_inline ZEND_COLD void zend_throw_auto_init_in_prop_error(zend_property_info *prop) {
613 	zend_string *type_str = zend_type_to_string(prop->type);
614 	zend_type_error(
615 		"Cannot auto-initialize an array inside property %s::$%s of type %s",
616 		ZSTR_VAL(prop->ce->name), zend_get_unmangled_property_name(prop->name),
617 		ZSTR_VAL(type_str)
618 	);
619 	zend_string_release(type_str);
620 }
621 
zend_throw_auto_init_in_ref_error(zend_property_info * prop)622 static zend_never_inline ZEND_COLD void zend_throw_auto_init_in_ref_error(zend_property_info *prop) {
623 	zend_string *type_str = zend_type_to_string(prop->type);
624 	zend_type_error(
625 		"Cannot auto-initialize an array inside a reference held by property %s::$%s of type %s",
626 		ZSTR_VAL(prop->ce->name), zend_get_unmangled_property_name(prop->name),
627 		ZSTR_VAL(type_str)
628 	);
629 	zend_string_release(type_str);
630 }
631 
zend_throw_access_uninit_prop_by_ref_error(zend_property_info * prop)632 static zend_never_inline ZEND_COLD void zend_throw_access_uninit_prop_by_ref_error(
633 		zend_property_info *prop) {
634 	zend_throw_error(NULL,
635 		"Cannot access uninitialized non-nullable property %s::$%s by reference",
636 		ZSTR_VAL(prop->ce->name),
637 		zend_get_unmangled_property_name(prop->name));
638 }
639 
640 /* this should modify object only if it's empty */
zend_throw_non_object_error(zval * object,zval * property OPLINE_DC EXECUTE_DATA_DC)641 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_throw_non_object_error(zval *object, zval *property OPLINE_DC EXECUTE_DATA_DC)
642 {
643 	zend_string *tmp_property_name;
644 	zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name);
645 
646 	if (opline->opcode == ZEND_PRE_INC_OBJ
647 	 || opline->opcode == ZEND_PRE_DEC_OBJ
648 	 || opline->opcode == ZEND_POST_INC_OBJ
649 	 || opline->opcode == ZEND_POST_DEC_OBJ) {
650 		zend_throw_error(NULL,
651 			"Attempt to increment/decrement property \"%s\" on %s",
652 			ZSTR_VAL(property_name), zend_zval_value_name(object)
653 		);
654 	} else if (opline->opcode == ZEND_FETCH_OBJ_W
655 			|| opline->opcode == ZEND_FETCH_OBJ_RW
656 			|| opline->opcode == ZEND_FETCH_OBJ_FUNC_ARG
657 			|| opline->opcode == ZEND_ASSIGN_OBJ_REF) {
658 		zend_throw_error(NULL,
659 			"Attempt to modify property \"%s\" on %s",
660 			ZSTR_VAL(property_name), zend_zval_value_name(object)
661 		);
662 	} else {
663 		zend_throw_error(NULL,
664 			"Attempt to assign property \"%s\" on %s",
665 			ZSTR_VAL(property_name), zend_zval_value_name(object)
666 		);
667 	}
668 	zend_tmp_string_release(tmp_property_name);
669 
670 	if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
671 		ZVAL_NULL(EX_VAR(opline->result.var));
672 	}
673 }
674 
zend_verify_type_error_common(const zend_function * zf,const zend_arg_info * arg_info,zval * value,const char ** fname,const char ** fsep,const char ** fclass,zend_string ** need_msg,const char ** given_kind)675 static ZEND_COLD void zend_verify_type_error_common(
676 		const zend_function *zf, const zend_arg_info *arg_info, zval *value,
677 		const char **fname, const char **fsep, const char **fclass,
678 		zend_string **need_msg, const char **given_kind)
679 {
680 	*fname = ZSTR_VAL(zf->common.function_name);
681 	if (zf->common.scope) {
682 		*fsep =  "::";
683 		*fclass = ZSTR_VAL(zf->common.scope->name);
684 	} else {
685 		*fsep =  "";
686 		*fclass = "";
687 	}
688 
689 	*need_msg = zend_type_to_string_resolved(arg_info->type, zf->common.scope);
690 
691 	if (value) {
692 		*given_kind = zend_zval_value_name(value);
693 	} else {
694 		*given_kind = "none";
695 	}
696 }
697 
zend_verify_arg_error(const zend_function * zf,const zend_arg_info * arg_info,uint32_t arg_num,zval * value)698 ZEND_API ZEND_COLD void zend_verify_arg_error(
699 		const zend_function *zf, const zend_arg_info *arg_info, uint32_t arg_num, zval *value)
700 {
701 	zend_execute_data *ptr = EG(current_execute_data)->prev_execute_data;
702 	const char *fname, *fsep, *fclass;
703 	zend_string *need_msg;
704 	const char *given_msg;
705 
706 	zend_verify_type_error_common(
707 		zf, arg_info, value, &fname, &fsep, &fclass, &need_msg, &given_msg);
708 
709 	ZEND_ASSERT(zf->common.type == ZEND_USER_FUNCTION
710 		&& "Arginfo verification is not performed for internal functions");
711 	if (ptr && ptr->func && ZEND_USER_CODE(ptr->func->common.type)) {
712 		zend_argument_type_error(arg_num, "must be of type %s, %s given, called in %s on line %d",
713 			ZSTR_VAL(need_msg), given_msg,
714 			ZSTR_VAL(ptr->func->op_array.filename), ptr->opline->lineno
715 		);
716 	} else {
717 		zend_argument_type_error(arg_num,
718 			"must be of type %s, %s given", ZSTR_VAL(need_msg), given_msg);
719 	}
720 
721 	zend_string_release(need_msg);
722 }
723 
zend_verify_weak_scalar_type_hint(uint32_t type_mask,zval * arg)724 static bool zend_verify_weak_scalar_type_hint(uint32_t type_mask, zval *arg)
725 {
726 	zend_long lval;
727 	double dval;
728 	zend_string *str;
729 	bool bval;
730 
731 	/* Type preference order: int -> float -> string -> bool */
732 	if (type_mask & MAY_BE_LONG) {
733 		/* For an int|float union type and string value,
734 		 * determine chosen type by is_numeric_string() semantics. */
735 		if ((type_mask & MAY_BE_DOUBLE) && Z_TYPE_P(arg) == IS_STRING) {
736 			uint8_t type = is_numeric_str_function(Z_STR_P(arg), &lval, &dval);
737 			if (type == IS_LONG) {
738 				zend_string_release(Z_STR_P(arg));
739 				ZVAL_LONG(arg, lval);
740 				return 1;
741 			}
742 			if (type == IS_DOUBLE) {
743 				zend_string_release(Z_STR_P(arg));
744 				ZVAL_DOUBLE(arg, dval);
745 				return 1;
746 			}
747 		} else if (zend_parse_arg_long_weak(arg, &lval, 0)) {
748 			zval_ptr_dtor(arg);
749 			ZVAL_LONG(arg, lval);
750 			return 1;
751 		} else if (UNEXPECTED(EG(exception))) {
752 			return 0;
753 		}
754 	}
755 	if ((type_mask & MAY_BE_DOUBLE) && zend_parse_arg_double_weak(arg, &dval, 0)) {
756 		zval_ptr_dtor(arg);
757 		ZVAL_DOUBLE(arg, dval);
758 		return 1;
759 	}
760 	if ((type_mask & MAY_BE_STRING) && zend_parse_arg_str_weak(arg, &str, 0)) {
761 		/* on success "arg" is converted to IS_STRING */
762 		return 1;
763 	}
764 	if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL && zend_parse_arg_bool_weak(arg, &bval, 0)) {
765 		zval_ptr_dtor(arg);
766 		ZVAL_BOOL(arg, bval);
767 		return 1;
768 	}
769 	return 0;
770 }
771 
772 #if ZEND_DEBUG
can_convert_to_string(const zval * zv)773 static bool can_convert_to_string(const zval *zv) {
774 	/* We don't call cast_object here, because this check must be side-effect free. As this
775 	 * is only used for a sanity check of arginfo/zpp consistency, it's okay if we accept
776 	 * more than actually allowed here. */
777 	if (Z_TYPE_P(zv) == IS_OBJECT) {
778 		return Z_OBJ_HT_P(zv)->cast_object != zend_std_cast_object_tostring
779 			|| Z_OBJCE_P(zv)->__tostring;
780 	}
781 	return Z_TYPE_P(zv) <= IS_STRING;
782 }
783 
784 /* Used to sanity-check internal arginfo types without performing any actual type conversions. */
zend_verify_weak_scalar_type_hint_no_sideeffect(uint32_t type_mask,const zval * arg)785 static bool zend_verify_weak_scalar_type_hint_no_sideeffect(uint32_t type_mask, const zval *arg)
786 {
787 	zend_long lval;
788 	double dval;
789 	bool bval;
790 
791 	/* Pass (uint32_t)-1 as arg_num to indicate to ZPP not to emit any deprecation notice,
792 	 * this is needed because the version with side effects also uses 0 (e.g. for typed properties) */
793 	if ((type_mask & MAY_BE_LONG) && zend_parse_arg_long_weak(arg, &lval, (uint32_t)-1)) {
794 		return 1;
795 	}
796 	if ((type_mask & MAY_BE_DOUBLE) && zend_parse_arg_double_weak(arg, &dval, (uint32_t)-1)) {
797 		return 1;
798 	}
799 	if ((type_mask & MAY_BE_STRING) && can_convert_to_string(arg)) {
800 		return 1;
801 	}
802 	if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL && zend_parse_arg_bool_weak(arg, &bval, (uint32_t)-1)) {
803 		return 1;
804 	}
805 	return 0;
806 }
807 #endif
808 
zend_verify_scalar_type_hint(uint32_t type_mask,zval * arg,bool strict,bool is_internal_arg)809 ZEND_API bool zend_verify_scalar_type_hint(uint32_t type_mask, zval *arg, bool strict, bool is_internal_arg)
810 {
811 	if (UNEXPECTED(strict)) {
812 		/* SSTH Exception: IS_LONG may be accepted as IS_DOUBLE (converted) */
813 		if (!(type_mask & MAY_BE_DOUBLE) || Z_TYPE_P(arg) != IS_LONG) {
814 			return 0;
815 		}
816 	} else if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
817 		/* NULL may be accepted only by nullable hints (this is already checked).
818 		 * As an exception for internal functions, null is allowed for scalar types in weak mode. */
819 		return is_internal_arg
820 			&& (type_mask & (MAY_BE_TRUE|MAY_BE_FALSE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING));
821 	}
822 #if ZEND_DEBUG
823 	if (is_internal_arg) {
824 		return zend_verify_weak_scalar_type_hint_no_sideeffect(type_mask, arg);
825 	}
826 #endif
827 	return zend_verify_weak_scalar_type_hint(type_mask, arg);
828 }
829 
zend_verify_class_constant_type_error(const zend_class_constant * c,const zend_string * name,const zval * constant)830 ZEND_COLD zend_never_inline void zend_verify_class_constant_type_error(const zend_class_constant *c, const zend_string *name, const zval *constant)
831 {
832 	zend_string *type_str = zend_type_to_string(c->type);
833 
834 	zend_type_error("Cannot assign %s to class constant %s::%s of type %s",
835 		zend_zval_type_name(constant), ZSTR_VAL(c->ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
836 
837 	zend_string_release(type_str);
838 }
839 
zend_verify_property_type_error(const zend_property_info * info,const zval * property)840 ZEND_COLD zend_never_inline void zend_verify_property_type_error(const zend_property_info *info, const zval *property)
841 {
842 	zend_string *type_str;
843 
844 	/* 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) */
845 	if (EG(exception)) {
846 		return;
847 	}
848 
849 	type_str = zend_type_to_string(info->type);
850 	zend_type_error("Cannot assign %s to property %s::$%s of type %s",
851 		zend_zval_value_name(property),
852 		ZSTR_VAL(info->ce->name),
853 		zend_get_unmangled_property_name(info->name),
854 		ZSTR_VAL(type_str));
855 	zend_string_release(type_str);
856 }
857 
zend_magic_get_property_type_inconsistency_error(const zend_property_info * info,const zval * property)858 ZEND_COLD zend_never_inline void zend_magic_get_property_type_inconsistency_error(const zend_property_info *info, const zval *property)
859 {
860 	/* 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) */
861 	if (EG(exception)) {
862 		return;
863 	}
864 
865 	zend_string *type_str = zend_type_to_string(info->type);
866 	zend_type_error("Value of type %s returned from %s::__get() must be compatible with unset property %s::$%s of type %s",
867 		zend_zval_type_name(property),
868 		ZSTR_VAL(info->ce->name),
869 		ZSTR_VAL(info->ce->name),
870 		zend_get_unmangled_property_name(info->name),
871 		ZSTR_VAL(type_str));
872 	zend_string_release(type_str);
873 }
874 
zend_match_unhandled_error(const zval * value)875 ZEND_COLD void zend_match_unhandled_error(const zval *value)
876 {
877 	smart_str msg = {0};
878 	if (smart_str_append_zval(&msg, value, EG(exception_string_param_max_len)) != SUCCESS) {
879 		smart_str_appendl(&msg, "of type ", sizeof("of type ")-1);
880 		smart_str_appends(&msg, zend_zval_type_name(value));
881 	}
882 	smart_str_0(&msg);
883 
884 	zend_throw_exception_ex(
885 		zend_ce_unhandled_match_error, 0, "Unhandled match case %s", ZSTR_VAL(msg.s));
886 
887 	smart_str_free(&msg);
888 }
889 
zend_readonly_property_modification_error(const zend_property_info * info)890 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_readonly_property_modification_error(
891 		const zend_property_info *info) {
892 	zend_readonly_property_modification_error_ex(
893 		ZSTR_VAL(info->ce->name), zend_get_unmangled_property_name(info->name));
894 }
895 
zend_readonly_property_modification_error_ex(const char * class_name,const char * prop_name)896 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_readonly_property_modification_error_ex(
897 		const char *class_name, const char *prop_name) {
898 	zend_throw_error(NULL, "Cannot modify readonly property %s::$%s", class_name, prop_name);
899 }
900 
zend_readonly_property_indirect_modification_error(const zend_property_info * info)901 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_readonly_property_indirect_modification_error(const zend_property_info *info)
902 {
903 	zend_throw_error(NULL, "Cannot indirectly modify readonly property %s::$%s",
904 		ZSTR_VAL(info->ce->name), zend_get_unmangled_property_name(info->name));
905 }
906 
zend_invalid_class_constant_type_error(uint8_t type)907 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_invalid_class_constant_type_error(uint8_t type)
908 {
909 	zend_type_error("Cannot use value of type %s as class constant name", zend_get_type_by_const(type));
910 }
911 
zend_object_released_while_assigning_to_property_error(const zend_property_info * info)912 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_object_released_while_assigning_to_property_error(const zend_property_info *info)
913 {
914 	zend_throw_error(NULL, "Object was released while assigning to property %s::$%s",
915 		ZSTR_VAL(info->ce->name), zend_get_unmangled_property_name(info->name));
916 }
917 
zend_asymmetric_visibility_property_modification_error(const zend_property_info * prop_info,const char * operation)918 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_asymmetric_visibility_property_modification_error(
919 	const zend_property_info *prop_info, const char *operation
920 ) {
921 	zend_class_entry *scope;
922 	if (EG(fake_scope)) {
923 		scope = EG(fake_scope);
924 	} else {
925 		scope = zend_get_called_scope(EG(current_execute_data));
926 	}
927 
928 	const char *visibility;
929 	if (prop_info->flags & ZEND_ACC_PRIVATE_SET) {
930 		visibility = "private(set)";
931 	} else {
932 		ZEND_ASSERT(prop_info->flags & ZEND_ACC_PROTECTED_SET);
933 		if (prop_info->flags & ZEND_ACC_READONLY) {
934 			visibility = "protected(set) readonly";
935 		} else {
936 			visibility = "protected(set)";
937 		}
938 	}
939 
940 	zend_throw_error(NULL, "Cannot %s %s property %s::$%s from %s%s",
941 		operation,
942 		visibility,
943 		ZSTR_VAL(prop_info->ce->name),
944 		ZSTR_VAL(prop_info->name),
945 		scope ? "scope " : "global scope", scope ? ZSTR_VAL(scope->name) : "");
946 }
947 
resolve_single_class_type(zend_string * name,const zend_class_entry * self_ce)948 static const zend_class_entry *resolve_single_class_type(zend_string *name, const zend_class_entry *self_ce) {
949 	if (zend_string_equals_literal_ci(name, "self")) {
950 		return self_ce;
951 	} else if (zend_string_equals_literal_ci(name, "parent")) {
952 		return self_ce->parent;
953 	} else {
954 		return zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
955 	}
956 }
957 
zend_ce_from_type(const zend_class_entry * scope,const zend_type * type)958 static zend_always_inline const zend_class_entry *zend_ce_from_type(
959 		const zend_class_entry *scope, const zend_type *type) {
960 	ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*type));
961 	zend_string *name = ZEND_TYPE_NAME(*type);
962 	if (ZSTR_HAS_CE_CACHE(name)) {
963 		zend_class_entry *ce = ZSTR_GET_CE_CACHE(name);
964 		if (!ce) {
965 			ce = zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
966 		}
967 		return ce;
968 	}
969 	return resolve_single_class_type(name, scope);
970 }
971 
zend_check_intersection_for_property_or_class_constant_class_type(const zend_class_entry * scope,zend_type_list * intersection_type_list,const zend_class_entry * value_ce)972 static bool zend_check_intersection_for_property_or_class_constant_class_type(
973 	const zend_class_entry *scope, zend_type_list *intersection_type_list, const zend_class_entry *value_ce)
974 {
975 	zend_type *list_type;
976 
977 	ZEND_TYPE_LIST_FOREACH(intersection_type_list, list_type) {
978 		ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
979 		const zend_class_entry *ce = zend_ce_from_type(scope, list_type);
980 		if (!ce || !instanceof_function(value_ce, ce)) {
981 			return false;
982 		}
983 	} ZEND_TYPE_LIST_FOREACH_END();
984 	return true;
985 }
986 
zend_check_and_resolve_property_or_class_constant_class_type(const zend_class_entry * scope,zend_type member_type,const zend_class_entry * value_ce)987 static bool zend_check_and_resolve_property_or_class_constant_class_type(
988 	const zend_class_entry *scope, zend_type member_type, const zend_class_entry *value_ce) {
989 	if (ZEND_TYPE_HAS_LIST(member_type)) {
990 		zend_type *list_type;
991 		if (ZEND_TYPE_IS_INTERSECTION(member_type)) {
992 			return zend_check_intersection_for_property_or_class_constant_class_type(
993 				scope, ZEND_TYPE_LIST(member_type), value_ce);
994 		} else {
995 			ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(member_type), list_type) {
996 				if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
997 					if (zend_check_intersection_for_property_or_class_constant_class_type(
998 							scope, ZEND_TYPE_LIST(*list_type), value_ce)) {
999 						return true;
1000 					}
1001 					continue;
1002 				}
1003 				ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
1004 				const zend_class_entry *ce = zend_ce_from_type(scope, list_type);
1005 				if (ce && instanceof_function(value_ce, ce)) {
1006 					return true;
1007 				}
1008 			} ZEND_TYPE_LIST_FOREACH_END();
1009 
1010 			if ((ZEND_TYPE_PURE_MASK(member_type) & MAY_BE_STATIC)) {
1011 				return value_ce == scope;
1012 			}
1013 
1014 			return false;
1015 		}
1016 	} else if ((ZEND_TYPE_PURE_MASK(member_type) & MAY_BE_STATIC) && value_ce == scope) {
1017 		return true;
1018 	} else if (ZEND_TYPE_HAS_NAME(member_type)) {
1019 		const zend_class_entry *ce = zend_ce_from_type(scope, &member_type);
1020 		return ce && instanceof_function(value_ce, ce);
1021 	}
1022 
1023 	return false;
1024 }
1025 
i_zend_check_property_type(const zend_property_info * info,zval * property,bool strict)1026 static zend_always_inline bool i_zend_check_property_type(const zend_property_info *info, zval *property, bool strict)
1027 {
1028 	ZEND_ASSERT(!Z_ISREF_P(property));
1029 	if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(info->type, Z_TYPE_P(property)))) {
1030 		return 1;
1031 	}
1032 
1033 	if (ZEND_TYPE_IS_COMPLEX(info->type) && Z_TYPE_P(property) == IS_OBJECT
1034 			&& zend_check_and_resolve_property_or_class_constant_class_type(info->ce, info->type, Z_OBJCE_P(property))) {
1035 		return 1;
1036 	}
1037 
1038 	uint32_t type_mask = ZEND_TYPE_FULL_MASK(info->type);
1039 	ZEND_ASSERT(!(type_mask & (MAY_BE_CALLABLE|MAY_BE_STATIC|MAY_BE_NEVER|MAY_BE_VOID)));
1040 	return zend_verify_scalar_type_hint(type_mask, property, strict, 0);
1041 }
1042 
i_zend_verify_property_type(const zend_property_info * info,zval * property,bool strict)1043 static zend_always_inline bool i_zend_verify_property_type(const zend_property_info *info, zval *property, bool strict)
1044 {
1045 	if (i_zend_check_property_type(info, property, strict)) {
1046 		return 1;
1047 	}
1048 
1049 	zend_verify_property_type_error(info, property);
1050 	return 0;
1051 }
1052 
zend_verify_property_type(const zend_property_info * info,zval * property,bool strict)1053 ZEND_API bool zend_never_inline zend_verify_property_type(const zend_property_info *info, zval *property, bool strict) {
1054 	return i_zend_verify_property_type(info, property, strict);
1055 }
1056 
zend_assign_to_typed_prop(zend_property_info * info,zval * property_val,zval * value,zend_refcounted ** garbage_ptr EXECUTE_DATA_DC)1057 static zend_never_inline zval* zend_assign_to_typed_prop(zend_property_info *info, zval *property_val, zval *value, zend_refcounted **garbage_ptr EXECUTE_DATA_DC)
1058 {
1059 	zval tmp;
1060 
1061 	if (UNEXPECTED(info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))) {
1062 		if ((info->flags & ZEND_ACC_READONLY) && !(Z_PROP_FLAG_P(property_val) & IS_PROP_REINITABLE)) {
1063 			zend_readonly_property_modification_error(info);
1064 			return &EG(uninitialized_zval);
1065 		}
1066 		if (info->flags & ZEND_ACC_PPP_SET_MASK && !zend_asymmetric_property_has_set_access(info)) {
1067 			zend_asymmetric_visibility_property_modification_error(info, "modify");
1068 			return &EG(uninitialized_zval);
1069 		}
1070 	}
1071 
1072 	ZVAL_DEREF(value);
1073 	ZVAL_COPY(&tmp, value);
1074 
1075 	if (UNEXPECTED(!i_zend_verify_property_type(info, &tmp, EX_USES_STRICT_TYPES()))) {
1076 		zval_ptr_dtor(&tmp);
1077 		return &EG(uninitialized_zval);
1078 	}
1079 
1080 	Z_PROP_FLAG_P(property_val) &= ~IS_PROP_REINITABLE;
1081 
1082 	return zend_assign_to_variable_ex(property_val, &tmp, IS_TMP_VAR, EX_USES_STRICT_TYPES(), garbage_ptr);
1083 }
1084 
zend_value_instanceof_static(zval * zv)1085 static zend_always_inline bool zend_value_instanceof_static(zval *zv) {
1086 	if (Z_TYPE_P(zv) != IS_OBJECT) {
1087 		return 0;
1088 	}
1089 
1090 	zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1091 	if (!called_scope) {
1092 		return 0;
1093 	}
1094 	return instanceof_function(Z_OBJCE_P(zv), called_scope);
1095 }
1096 
1097 /* The cache_slot may only be NULL in debug builds, where arginfo verification of
1098  * internal functions is enabled. Avoid unnecessary checks in release builds. */
1099 #if ZEND_DEBUG
1100 # define HAVE_CACHE_SLOT (cache_slot != NULL)
1101 #else
1102 # define HAVE_CACHE_SLOT 1
1103 #endif
1104 
1105 #define PROGRESS_CACHE_SLOT() if (HAVE_CACHE_SLOT) {cache_slot++;}
1106 
zend_fetch_ce_from_cache_slot(void ** cache_slot,zend_type * type)1107 static zend_always_inline zend_class_entry *zend_fetch_ce_from_cache_slot(
1108 		void **cache_slot, zend_type *type)
1109 {
1110 	if (EXPECTED(HAVE_CACHE_SLOT && *cache_slot)) {
1111 		return (zend_class_entry *) *cache_slot;
1112 	}
1113 
1114 	zend_string *name = ZEND_TYPE_NAME(*type);
1115 	zend_class_entry *ce;
1116 	if (ZSTR_HAS_CE_CACHE(name)) {
1117 		ce = ZSTR_GET_CE_CACHE(name);
1118 		if (!ce) {
1119 			ce = zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
1120 			if (UNEXPECTED(!ce)) {
1121 				/* Cannot resolve */
1122 				return NULL;
1123 			}
1124 		}
1125 	} else {
1126 		ce = zend_fetch_class(name,
1127 			ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_SILENT);
1128 		if (UNEXPECTED(!ce)) {
1129 			return NULL;
1130 		}
1131 	}
1132 	if (HAVE_CACHE_SLOT) {
1133 		*cache_slot = (void *) ce;
1134 	}
1135 	return ce;
1136 }
1137 
zend_check_intersection_type_from_cache_slot(zend_type_list * intersection_type_list,zend_class_entry * arg_ce,void *** cache_slot_ptr)1138 static bool zend_check_intersection_type_from_cache_slot(zend_type_list *intersection_type_list,
1139 	zend_class_entry *arg_ce, void ***cache_slot_ptr)
1140 {
1141 	void **cache_slot = *cache_slot_ptr;
1142 	zend_class_entry *ce;
1143 	zend_type *list_type;
1144 	bool status = true;
1145 	ZEND_TYPE_LIST_FOREACH(intersection_type_list, list_type) {
1146 		/* Only check classes if the type might be valid */
1147 		if (status) {
1148 			ce = zend_fetch_ce_from_cache_slot(cache_slot, list_type);
1149 			/* If type is not an instance of one of the types taking part in the
1150 			 * intersection it cannot be a valid instance of the whole intersection type. */
1151 			if (!ce || !instanceof_function(arg_ce, ce)) {
1152 				status = false;
1153 			}
1154 		}
1155 		PROGRESS_CACHE_SLOT();
1156 	} ZEND_TYPE_LIST_FOREACH_END();
1157 	if (HAVE_CACHE_SLOT) {
1158 		*cache_slot_ptr = cache_slot;
1159 	}
1160 	return status;
1161 }
1162 
zend_check_type_slow(zend_type * type,zval * arg,zend_reference * ref,void ** cache_slot,bool is_return_type,bool is_internal)1163 static zend_always_inline bool zend_check_type_slow(
1164 		zend_type *type, zval *arg, zend_reference *ref, void **cache_slot,
1165 		bool is_return_type, bool is_internal)
1166 {
1167 	uint32_t type_mask;
1168 	if (ZEND_TYPE_IS_COMPLEX(*type) && EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
1169 		zend_class_entry *ce;
1170 		if (UNEXPECTED(ZEND_TYPE_HAS_LIST(*type))) {
1171 			zend_type *list_type;
1172 			if (ZEND_TYPE_IS_INTERSECTION(*type)) {
1173 				return zend_check_intersection_type_from_cache_slot(ZEND_TYPE_LIST(*type), Z_OBJCE_P(arg), &cache_slot);
1174 			} else {
1175 				ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(*type), list_type) {
1176 					if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
1177 						if (zend_check_intersection_type_from_cache_slot(ZEND_TYPE_LIST(*list_type), Z_OBJCE_P(arg), &cache_slot)) {
1178 							return true;
1179 						}
1180 						/* The cache_slot is progressed in zend_check_intersection_type_from_cache_slot() */
1181 					} else {
1182 						ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
1183 						ce = zend_fetch_ce_from_cache_slot(cache_slot, list_type);
1184 						/* Instance of a single type part of a union is sufficient to pass the type check */
1185 						if (ce && instanceof_function(Z_OBJCE_P(arg), ce)) {
1186 							return true;
1187 						}
1188 						PROGRESS_CACHE_SLOT();
1189 					}
1190 				} ZEND_TYPE_LIST_FOREACH_END();
1191 			}
1192 		} else {
1193 			ce = zend_fetch_ce_from_cache_slot(cache_slot, type);
1194 			/* If we have a CE we check if it satisfies the type constraint,
1195 			 * otherwise it will check if a standard type satisfies it. */
1196 			if (ce && instanceof_function(Z_OBJCE_P(arg), ce)) {
1197 				return true;
1198 			}
1199 		}
1200 	}
1201 
1202 	type_mask = ZEND_TYPE_FULL_MASK(*type);
1203 	if ((type_mask & MAY_BE_CALLABLE) &&
1204 		zend_is_callable(arg, is_internal ? IS_CALLABLE_SUPPRESS_DEPRECATIONS : 0, NULL)) {
1205 		return 1;
1206 	}
1207 	if ((type_mask & MAY_BE_STATIC) && zend_value_instanceof_static(arg)) {
1208 		return 1;
1209 	}
1210 	if (ref && ZEND_REF_HAS_TYPE_SOURCES(ref)) {
1211 		/* We cannot have conversions for typed refs. */
1212 		return 0;
1213 	}
1214 	if (is_internal && is_return_type) {
1215 		/* For internal returns, the type has to match exactly, because we're not
1216 		 * going to check it for non-debug builds, and there will be no chance to
1217 		 * apply coercions. */
1218 		return 0;
1219 	}
1220 
1221 	return zend_verify_scalar_type_hint(type_mask, arg,
1222 		is_return_type ? ZEND_RET_USES_STRICT_TYPES() : ZEND_ARG_USES_STRICT_TYPES(),
1223 		is_internal);
1224 
1225 	/* Special handling for IS_VOID is not necessary (for return types),
1226 	 * because this case is already checked at compile-time. */
1227 }
1228 
zend_check_type(zend_type * type,zval * arg,void ** cache_slot,zend_class_entry * scope,bool is_return_type,bool is_internal)1229 static zend_always_inline bool zend_check_type(
1230 		zend_type *type, zval *arg, void **cache_slot, zend_class_entry *scope,
1231 		bool is_return_type, bool is_internal)
1232 {
1233 	zend_reference *ref = NULL;
1234 	ZEND_ASSERT(ZEND_TYPE_IS_SET(*type));
1235 
1236 	if (UNEXPECTED(Z_ISREF_P(arg))) {
1237 		ref = Z_REF_P(arg);
1238 		arg = Z_REFVAL_P(arg);
1239 	}
1240 
1241 	if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(*type, Z_TYPE_P(arg)))) {
1242 		return 1;
1243 	}
1244 
1245 	return zend_check_type_slow(type, arg, ref, cache_slot, is_return_type, is_internal);
1246 }
1247 
zend_check_user_type_slow(zend_type * type,zval * arg,zend_reference * ref,void ** cache_slot,bool is_return_type)1248 ZEND_API bool zend_check_user_type_slow(
1249 		zend_type *type, zval *arg, zend_reference *ref, void **cache_slot, bool is_return_type)
1250 {
1251 	return zend_check_type_slow(
1252 		type, arg, ref, cache_slot, is_return_type, /* is_internal */ false);
1253 }
1254 
zend_verify_recv_arg_type(zend_function * zf,uint32_t arg_num,zval * arg,void ** cache_slot)1255 static zend_always_inline bool zend_verify_recv_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, void **cache_slot)
1256 {
1257 	zend_arg_info *cur_arg_info;
1258 
1259 	ZEND_ASSERT(arg_num <= zf->common.num_args);
1260 	cur_arg_info = &zf->common.arg_info[arg_num-1];
1261 
1262 	if (ZEND_TYPE_IS_SET(cur_arg_info->type)
1263 			&& UNEXPECTED(!zend_check_type(&cur_arg_info->type, arg, cache_slot, zf->common.scope, 0, 0))) {
1264 		zend_verify_arg_error(zf, cur_arg_info, arg_num, arg);
1265 		return 0;
1266 	}
1267 
1268 	return 1;
1269 }
1270 
zend_verify_variadic_arg_type(zend_function * zf,zend_arg_info * arg_info,uint32_t arg_num,zval * arg,void ** cache_slot)1271 static zend_always_inline bool zend_verify_variadic_arg_type(
1272 		zend_function *zf, zend_arg_info *arg_info, uint32_t arg_num, zval *arg, void **cache_slot)
1273 {
1274 	ZEND_ASSERT(ZEND_TYPE_IS_SET(arg_info->type));
1275 	if (UNEXPECTED(!zend_check_type(&arg_info->type, arg, cache_slot, zf->common.scope, 0, 0))) {
1276 		zend_verify_arg_error(zf, arg_info, arg_num, arg);
1277 		return 0;
1278 	}
1279 
1280 	return 1;
1281 }
1282 
zend_verify_internal_arg_types(zend_function * fbc,zend_execute_data * call)1283 static zend_never_inline ZEND_ATTRIBUTE_UNUSED bool zend_verify_internal_arg_types(zend_function *fbc, zend_execute_data *call)
1284 {
1285 	uint32_t i;
1286 	uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
1287 	zval *arg = ZEND_CALL_ARG(call, 1);
1288 
1289 	for (i = 0; i < num_args; ++i) {
1290 		zend_arg_info *cur_arg_info;
1291 		if (EXPECTED(i < fbc->common.num_args)) {
1292 			cur_arg_info = &fbc->common.arg_info[i];
1293 		} else if (UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
1294 			cur_arg_info = &fbc->common.arg_info[fbc->common.num_args];
1295 		} else {
1296 			break;
1297 		}
1298 
1299 		if (ZEND_TYPE_IS_SET(cur_arg_info->type)
1300 				&& UNEXPECTED(!zend_check_type(&cur_arg_info->type, arg, /* cache_slot */ NULL, fbc->common.scope, 0, /* is_internal */ 1))) {
1301 			return 0;
1302 		}
1303 		arg++;
1304 	}
1305 	return 1;
1306 }
1307 
1308 #if ZEND_DEBUG
1309 /* Determine whether an internal call should throw, because the passed arguments violate
1310  * an arginfo constraint. This is only checked in debug builds. In release builds, we
1311  * trust that arginfo matches what is enforced by zend_parse_parameters. */
zend_internal_call_should_throw(zend_function * fbc,zend_execute_data * call)1312 ZEND_API bool zend_internal_call_should_throw(zend_function *fbc, zend_execute_data *call)
1313 {
1314 	if (fbc->internal_function.handler == ZEND_FN(pass) || (fbc->internal_function.fn_flags & ZEND_ACC_FAKE_CLOSURE)) {
1315 		/* Be lenient about the special pass function and about fake closures. */
1316 		return 0;
1317 	}
1318 
1319 	if (fbc->common.required_num_args > ZEND_CALL_NUM_ARGS(call)) {
1320 		/* Required argument not passed. */
1321 		return 1;
1322 	}
1323 
1324 	if (fbc->common.num_args < ZEND_CALL_NUM_ARGS(call)
1325 			&& !(fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
1326 		/* Too many arguments passed. For internal functions (unlike userland functions),
1327 		 * this should always throw. */
1328 		return 1;
1329 	}
1330 
1331 	if ((fbc->common.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) &&
1332 			!zend_verify_internal_arg_types(fbc, call)) {
1333 		return 1;
1334 	}
1335 
1336 	return 0;
1337 }
1338 
zend_internal_call_arginfo_violation(zend_function * fbc)1339 ZEND_API ZEND_COLD void zend_internal_call_arginfo_violation(zend_function *fbc)
1340 {
1341 	zend_error_noreturn(E_ERROR, "Arginfo / zpp mismatch during call of %s%s%s()",
1342 		fbc->common.scope ? ZSTR_VAL(fbc->common.scope->name) : "",
1343 		fbc->common.scope ? "::" : "",
1344 		ZSTR_VAL(fbc->common.function_name));
1345 }
1346 
1347 #ifndef ZEND_VERIFY_FUNC_INFO
1348 # define ZEND_VERIFY_FUNC_INFO 0
1349 #endif
1350 
zend_verify_internal_func_info(zend_function * fn,zval * retval)1351 static void zend_verify_internal_func_info(zend_function *fn, zval *retval) {
1352 #if ZEND_VERIFY_FUNC_INFO
1353 	zend_string *name = fn->common.function_name;
1354 	uint32_t type_mask = zend_get_internal_func_info(fn, NULL, NULL);
1355 	if (!type_mask) {
1356 		return;
1357 	}
1358 
1359 	/* Always check refcount of arrays, as immutable arrays are RCN. */
1360 	if (Z_REFCOUNTED_P(retval) || Z_TYPE_P(retval) == IS_ARRAY) {
1361 		if (!(type_mask & MAY_BE_RC1)) {
1362 			zend_error_noreturn(E_CORE_ERROR, "%s() missing rc1", ZSTR_VAL(name));
1363 		}
1364 		if (Z_REFCOUNT_P(retval) > 1 && !(type_mask & MAY_BE_RCN)) {
1365 			zend_error_noreturn(E_CORE_ERROR, "%s() missing rcn", ZSTR_VAL(name));
1366 		}
1367 	}
1368 
1369 	uint32_t type = 1u << Z_TYPE_P(retval);
1370 	if (!(type_mask & type)) {
1371 		zend_error_noreturn(E_CORE_ERROR, "%s() missing type %s",
1372 			ZSTR_VAL(name), zend_get_type_by_const(Z_TYPE_P(retval)));
1373 	}
1374 
1375 	if (Z_TYPE_P(retval) == IS_ARRAY) {
1376 		HashTable *ht = Z_ARRVAL_P(retval);
1377 		uint32_t num_checked = 0;
1378 		zend_string *str;
1379 		zval *val;
1380 		ZEND_HASH_FOREACH_STR_KEY_VAL(ht, str, val) {
1381 			if (str) {
1382 				if (!(type_mask & MAY_BE_ARRAY_KEY_STRING)) {
1383 					zend_error_noreturn(E_CORE_ERROR,
1384 						"%s() missing array_key_string", ZSTR_VAL(name));
1385 				}
1386 			} else {
1387 				if (!(type_mask & MAY_BE_ARRAY_KEY_LONG)) {
1388 					zend_error_noreturn(E_CORE_ERROR,
1389 						"%s() missing array_key_long", ZSTR_VAL(name));
1390 				}
1391 			}
1392 
1393 			uint32_t array_type = 1u << (Z_TYPE_P(val) + MAY_BE_ARRAY_SHIFT);
1394 			if (!(type_mask & array_type)) {
1395 				zend_error_noreturn(E_CORE_ERROR,
1396 					"%s() missing array element type %s",
1397 					ZSTR_VAL(name), zend_get_type_by_const(Z_TYPE_P(retval)));
1398 			}
1399 
1400 			/* Don't check all elements of large arrays. */
1401 			if (++num_checked > 16) {
1402 				break;
1403 			}
1404 		} ZEND_HASH_FOREACH_END();
1405 	}
1406 #endif
1407 }
1408 #endif
1409 
zend_missing_arg_error(zend_execute_data * execute_data)1410 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_missing_arg_error(zend_execute_data *execute_data)
1411 {
1412 	zend_execute_data *ptr = EX(prev_execute_data);
1413 
1414 	if (ptr && ptr->func && ZEND_USER_CODE(ptr->func->common.type)) {
1415 		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",
1416 			EX(func)->common.scope ? ZSTR_VAL(EX(func)->common.scope->name) : "",
1417 			EX(func)->common.scope ? "::" : "",
1418 			ZSTR_VAL(EX(func)->common.function_name),
1419 			EX_NUM_ARGS(),
1420 			ZSTR_VAL(ptr->func->op_array.filename),
1421 			ptr->opline->lineno,
1422 			EX(func)->common.required_num_args == EX(func)->common.num_args ? "exactly" : "at least",
1423 			EX(func)->common.required_num_args);
1424 	} else {
1425 		zend_throw_error(zend_ce_argument_count_error, "Too few arguments to function %s%s%s(), %d passed and %s %d expected",
1426 			EX(func)->common.scope ? ZSTR_VAL(EX(func)->common.scope->name) : "",
1427 			EX(func)->common.scope ? "::" : "",
1428 			ZSTR_VAL(EX(func)->common.function_name),
1429 			EX_NUM_ARGS(),
1430 			EX(func)->common.required_num_args == EX(func)->common.num_args ? "exactly" : "at least",
1431 			EX(func)->common.required_num_args);
1432 	}
1433 }
1434 
zend_verify_return_error(const zend_function * zf,zval * value)1435 ZEND_API ZEND_COLD void zend_verify_return_error(const zend_function *zf, zval *value)
1436 {
1437 	const zend_arg_info *arg_info = &zf->common.arg_info[-1];
1438 	const char *fname, *fsep, *fclass;
1439 	zend_string *need_msg;
1440 	const char *given_msg;
1441 
1442 	zend_verify_type_error_common(
1443 		zf, arg_info, value, &fname, &fsep, &fclass, &need_msg, &given_msg);
1444 
1445 	zend_type_error("%s%s%s(): Return value must be of type %s, %s returned",
1446 		fclass, fsep, fname, ZSTR_VAL(need_msg), given_msg);
1447 
1448 	zend_string_release(need_msg);
1449 }
1450 
zend_verify_never_error(const zend_function * zf)1451 ZEND_API ZEND_COLD void zend_verify_never_error(const zend_function *zf)
1452 {
1453 	zend_string *func_name = get_function_or_method_name(zf);
1454 
1455 	zend_type_error("%s(): never-returning %s must not implicitly return",
1456 		ZSTR_VAL(func_name), zf->common.scope ? "method" : "function");
1457 
1458 	zend_string_release(func_name);
1459 }
1460 
1461 #if ZEND_DEBUG
zend_verify_internal_return_error(const zend_function * zf,zval * value)1462 static ZEND_COLD void zend_verify_internal_return_error(const zend_function *zf, zval *value)
1463 {
1464 	const zend_arg_info *arg_info = &zf->common.arg_info[-1];
1465 	const char *fname, *fsep, *fclass;
1466 	zend_string *need_msg;
1467 	const char *given_msg;
1468 
1469 	zend_verify_type_error_common(
1470 		zf, arg_info, value, &fname, &fsep, &fclass, &need_msg, &given_msg);
1471 
1472 	zend_error_noreturn(E_CORE_ERROR, "%s%s%s(): Return value must be of type %s, %s returned",
1473 		fclass, fsep, fname, ZSTR_VAL(need_msg), given_msg);
1474 }
1475 
zend_verify_void_return_error(const zend_function * zf,const char * returned_msg,const char * returned_kind)1476 static ZEND_COLD void zend_verify_void_return_error(const zend_function *zf, const char *returned_msg, const char *returned_kind)
1477 {
1478 	const char *fname = ZSTR_VAL(zf->common.function_name);
1479 	const char *fsep;
1480 	const char *fclass;
1481 
1482 	if (zf->common.scope) {
1483 		fsep =  "::";
1484 		fclass = ZSTR_VAL(zf->common.scope->name);
1485 	} else {
1486 		fsep =  "";
1487 		fclass = "";
1488 	}
1489 
1490 	zend_type_error("%s%s%s() must not return a value, %s%s returned",
1491 		fclass, fsep, fname, returned_msg, returned_kind);
1492 }
1493 
zend_verify_internal_return_type(zend_function * zf,zval * ret)1494 ZEND_API bool zend_verify_internal_return_type(zend_function *zf, zval *ret)
1495 {
1496 	zend_internal_arg_info *ret_info = zf->internal_function.arg_info - 1;
1497 
1498 	if (ZEND_TYPE_FULL_MASK(ret_info->type) & MAY_BE_VOID) {
1499 		if (UNEXPECTED(Z_TYPE_P(ret) != IS_NULL)) {
1500 			zend_verify_void_return_error(zf, zend_zval_value_name(ret), "");
1501 			return 0;
1502 		}
1503 		return 1;
1504 	}
1505 
1506 	if (UNEXPECTED(!zend_check_type(&ret_info->type, ret, /* cache_slot */ NULL, NULL, 1, /* is_internal */ 1))) {
1507 		zend_verify_internal_return_error(zf, ret);
1508 		return 0;
1509 	}
1510 
1511 	return 1;
1512 }
1513 #endif
1514 
zend_verify_missing_return_type(const zend_function * zf)1515 static ZEND_COLD void zend_verify_missing_return_type(const zend_function *zf)
1516 {
1517 	/* VERIFY_RETURN_TYPE is not emitted for "void" functions, so this is always an error. */
1518 	zend_verify_return_error(zf, NULL);
1519 }
1520 
zend_check_class_constant_type(zend_class_constant * c,zval * constant)1521 static zend_always_inline bool zend_check_class_constant_type(zend_class_constant *c, zval *constant)
1522 {
1523 	ZEND_ASSERT(!Z_ISREF_P(constant));
1524 	if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(c->type, Z_TYPE_P(constant)))) {
1525 		return 1;
1526 	}
1527 
1528 	if (((ZEND_TYPE_PURE_MASK(c->type) & MAY_BE_STATIC) || ZEND_TYPE_IS_COMPLEX(c->type)) && Z_TYPE_P(constant) == IS_OBJECT
1529 		&& zend_check_and_resolve_property_or_class_constant_class_type(c->ce, c->type, Z_OBJCE_P(constant))) {
1530 		return 1;
1531 	}
1532 
1533 	uint32_t type_mask = ZEND_TYPE_FULL_MASK(c->type);
1534 	ZEND_ASSERT(!(type_mask & (MAY_BE_CALLABLE|MAY_BE_NEVER|MAY_BE_VOID)));
1535 	return zend_verify_scalar_type_hint(type_mask, constant, true, false);
1536 }
1537 
zend_verify_class_constant_type(zend_class_constant * c,const zend_string * name,zval * constant)1538 ZEND_API bool zend_never_inline zend_verify_class_constant_type(zend_class_constant *c, const zend_string *name, zval *constant)
1539 {
1540 	if (!zend_check_class_constant_type(c, constant)) {
1541 		zend_verify_class_constant_type_error(c, name, constant);
1542 		return 0;
1543 	}
1544 
1545 	return 1;
1546 }
1547 
zend_use_object_as_array(const zend_object * object)1548 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_object_as_array(const zend_object *object)
1549 {
1550 	zend_throw_error(NULL, "Cannot use object of type %s as array", ZSTR_VAL(object->ce->name));
1551 }
1552 
zend_illegal_array_offset_access(const zval * offset)1553 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_array_offset_access(const zval *offset)
1554 {
1555 	zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), offset, BP_VAR_RW);
1556 }
1557 
zend_illegal_array_offset_isset(const zval * offset)1558 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_array_offset_isset(const zval *offset)
1559 {
1560 	zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), offset, BP_VAR_IS);
1561 }
1562 
zend_illegal_array_offset_unset(const zval * offset)1563 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_array_offset_unset(const zval *offset)
1564 {
1565 	zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), offset, BP_VAR_UNSET);
1566 }
1567 
zend_illegal_string_offset(const zval * offset,int type)1568 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_string_offset(const zval *offset, int type)
1569 {
1570 	zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_STRING), offset, type);
1571 }
1572 
zend_assign_to_object_dim(zend_object * obj,zval * dim,zval * value OPLINE_DC EXECUTE_DATA_DC)1573 static zend_never_inline void zend_assign_to_object_dim(zend_object *obj, zval *dim, zval *value OPLINE_DC EXECUTE_DATA_DC)
1574 {
1575 	obj->handlers->write_dimension(obj, dim, value);
1576 
1577 	if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
1578 		ZVAL_COPY(EX_VAR(opline->result.var), value);
1579 	}
1580 }
1581 
frameless_observed_call_copy(zend_execute_data * call,uint32_t arg,zval * zv)1582 static void frameless_observed_call_copy(zend_execute_data *call, uint32_t arg, zval *zv)
1583 {
1584 	if (Z_ISUNDEF_P(zv)) {
1585 		ZVAL_NULL(ZEND_CALL_VAR_NUM(call, arg));
1586 	} else {
1587 		ZVAL_COPY_DEREF(ZEND_CALL_VAR_NUM(call, arg), zv);
1588 	}
1589 }
1590 
zend_frameless_observed_call(zend_execute_data * execute_data)1591 ZEND_API void zend_frameless_observed_call(zend_execute_data *execute_data)
1592 {
1593 	const zend_op *opline = EX(opline);
1594 	uint8_t num_args = ZEND_FLF_NUM_ARGS(opline->opcode);
1595 	zend_function *fbc = ZEND_FLF_FUNC(opline);
1596 	zval *result = EX_VAR(opline->result.var);
1597 
1598 	zend_execute_data *call = zend_vm_stack_push_call_frame_ex(zend_vm_calc_used_stack(num_args, fbc), ZEND_CALL_NESTED_FUNCTION, fbc, num_args, NULL);
1599 	call->prev_execute_data = execute_data;
1600 
1601 	switch (num_args) {
1602 		case 3: frameless_observed_call_copy(call, 2, zend_get_zval_ptr(opline+1, (opline+1)->op1_type, &(opline+1)->op1, execute_data)); ZEND_FALLTHROUGH;
1603 		case 2: frameless_observed_call_copy(call, 1, zend_get_zval_ptr(opline, opline->op2_type, &opline->op2, execute_data)); ZEND_FALLTHROUGH;
1604 		case 1: frameless_observed_call_copy(call, 0, zend_get_zval_ptr(opline, opline->op1_type, &opline->op1, execute_data));
1605 	}
1606 
1607 	EG(current_execute_data) = call;
1608 
1609 	zend_observer_fcall_begin_prechecked(call, ZEND_OBSERVER_DATA(fbc));
1610 	fbc->internal_function.handler(call, result);
1611 	zend_observer_fcall_end(call, result);
1612 
1613 	EG(current_execute_data) = execute_data;
1614 
1615 	if (UNEXPECTED(EG(exception) != NULL)) {
1616 		zend_rethrow_exception(execute_data);
1617 	}
1618 
1619 	zend_vm_stack_free_args(call);
1620 
1621 	uint32_t call_info = ZEND_CALL_INFO(call);
1622 	if (UNEXPECTED(call_info & ZEND_CALL_ALLOCATED)) {
1623 		zend_vm_stack_free_call_frame_ex(call_info, call);
1624 	} else {
1625 		EG(vm_stack_top) = (zval*)call;
1626 	}
1627 }
1628 
1629 
zend_binary_op(zval * ret,zval * op1,zval * op2 OPLINE_DC)1630 static zend_always_inline int zend_binary_op(zval *ret, zval *op1, zval *op2 OPLINE_DC)
1631 {
1632 	static const binary_op_type zend_binary_ops[] = {
1633 		add_function,
1634 		sub_function,
1635 		mul_function,
1636 		div_function,
1637 		mod_function,
1638 		shift_left_function,
1639 		shift_right_function,
1640 		concat_function,
1641 		bitwise_or_function,
1642 		bitwise_and_function,
1643 		bitwise_xor_function,
1644 		pow_function
1645 	};
1646 	/* size_t cast makes GCC to better optimize 64-bit PIC code */
1647 	size_t opcode = (size_t)opline->extended_value;
1648 
1649 	return zend_binary_ops[opcode - ZEND_ADD](ret, op1, op2);
1650 }
1651 
zend_binary_assign_op_obj_dim(zend_object * obj,zval * property OPLINE_DC EXECUTE_DATA_DC)1652 static zend_never_inline void zend_binary_assign_op_obj_dim(zend_object *obj, zval *property OPLINE_DC EXECUTE_DATA_DC)
1653 {
1654 	zval *value;
1655 	zval *z;
1656 	zval rv, res;
1657 
1658 	GC_ADDREF(obj);
1659 	if (property && UNEXPECTED(Z_ISUNDEF_P(property))) {
1660 		property = ZVAL_UNDEFINED_OP2();
1661 	}
1662 	value = get_op_data_zval_ptr_r((opline+1)->op1_type, (opline+1)->op1);
1663 	if ((z = obj->handlers->read_dimension(obj, property, BP_VAR_R, &rv)) != NULL) {
1664 
1665 		if (zend_binary_op(&res, z, value OPLINE_CC) == SUCCESS) {
1666 			obj->handlers->write_dimension(obj, property, &res);
1667 		}
1668 		if (z == &rv) {
1669 			zval_ptr_dtor(&rv);
1670 		}
1671 		if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
1672 			ZVAL_COPY(EX_VAR(opline->result.var), &res);
1673 		}
1674 		zval_ptr_dtor(&res);
1675 	} else {
1676 		zend_use_object_as_array(obj);
1677 		if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
1678 			ZVAL_NULL(EX_VAR(opline->result.var));
1679 		}
1680 	}
1681 	FREE_OP((opline+1)->op1_type, (opline+1)->op1.var);
1682 	if (UNEXPECTED(GC_DELREF(obj) == 0)) {
1683 		zend_objects_store_del(obj);
1684 	}
1685 }
1686 
zend_binary_assign_op_typed_ref(zend_reference * ref,zval * value OPLINE_DC EXECUTE_DATA_DC)1687 static zend_never_inline void zend_binary_assign_op_typed_ref(zend_reference *ref, zval *value OPLINE_DC EXECUTE_DATA_DC)
1688 {
1689 	zval z_copy;
1690 
1691 	/* Make sure that in-place concatenation is used if the LHS is a string. */
1692 	if (opline->extended_value == ZEND_CONCAT && Z_TYPE(ref->val) == IS_STRING) {
1693 		concat_function(&ref->val, &ref->val, value);
1694 		ZEND_ASSERT(Z_TYPE(ref->val) == IS_STRING && "Concat should return string");
1695 		return;
1696 	}
1697 
1698 	zend_binary_op(&z_copy, &ref->val, value OPLINE_CC);
1699 	if (EXPECTED(zend_verify_ref_assignable_zval(ref, &z_copy, EX_USES_STRICT_TYPES()))) {
1700 		zval_ptr_dtor(&ref->val);
1701 		ZVAL_COPY_VALUE(&ref->val, &z_copy);
1702 	} else {
1703 		zval_ptr_dtor(&z_copy);
1704 	}
1705 }
1706 
zend_binary_assign_op_typed_prop(zend_property_info * prop_info,zval * zptr,zval * value OPLINE_DC EXECUTE_DATA_DC)1707 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)
1708 {
1709 	zval z_copy;
1710 
1711 	/* Make sure that in-place concatenation is used if the LHS is a string. */
1712 	if (opline->extended_value == ZEND_CONCAT && Z_TYPE_P(zptr) == IS_STRING) {
1713 		concat_function(zptr, zptr, value);
1714 		ZEND_ASSERT(Z_TYPE_P(zptr) == IS_STRING && "Concat should return string");
1715 		return;
1716 	}
1717 
1718 	zend_binary_op(&z_copy, zptr, value OPLINE_CC);
1719 	if (EXPECTED(zend_verify_property_type(prop_info, &z_copy, EX_USES_STRICT_TYPES()))) {
1720 		zval_ptr_dtor(zptr);
1721 		ZVAL_COPY_VALUE(zptr, &z_copy);
1722 	} else {
1723 		zval_ptr_dtor(&z_copy);
1724 	}
1725 }
1726 
zend_check_string_offset(zval * dim,int type EXECUTE_DATA_DC)1727 static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type EXECUTE_DATA_DC)
1728 {
1729 	zend_long offset;
1730 
1731 try_again:
1732 	switch(Z_TYPE_P(dim)) {
1733 		case IS_LONG:
1734 			return Z_LVAL_P(dim);
1735 		case IS_STRING:
1736 		{
1737 			bool trailing_data = false;
1738 			/* For BC reasons we allow errors so that we can warn on leading numeric string */
1739 			if (IS_LONG == is_numeric_string_ex(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL,
1740 					/* allow errors */ true, NULL, &trailing_data)) {
1741 				if (UNEXPECTED(trailing_data) && type != BP_VAR_UNSET) {
1742 					zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
1743 				}
1744 				return offset;
1745 			}
1746 			zend_illegal_string_offset(dim, type);
1747 			return 0;
1748 		}
1749 		case IS_UNDEF:
1750 			ZVAL_UNDEFINED_OP2();
1751 			ZEND_FALLTHROUGH;
1752 		case IS_DOUBLE:
1753 		case IS_NULL:
1754 		case IS_FALSE:
1755 		case IS_TRUE:
1756 			zend_error(E_WARNING, "String offset cast occurred");
1757 			break;
1758 		case IS_REFERENCE:
1759 			dim = Z_REFVAL_P(dim);
1760 			goto try_again;
1761 		default:
1762 			zend_illegal_string_offset(dim, type);
1763 			return 0;
1764 	}
1765 
1766 	return zval_get_long_func(dim, /* is_strict */ false);
1767 }
1768 
zend_wrong_string_offset_error(void)1769 ZEND_API ZEND_COLD void zend_wrong_string_offset_error(void)
1770 {
1771 	const char *msg = NULL;
1772 	const zend_execute_data *execute_data = EG(current_execute_data);
1773 	const zend_op *opline = execute_data->opline;
1774 
1775 	if (UNEXPECTED(EG(exception) != NULL)) {
1776 		return;
1777 	}
1778 
1779 	switch (opline->opcode) {
1780 		case ZEND_ASSIGN_DIM_OP:
1781 			msg = "Cannot use assign-op operators with string offsets";
1782 			break;
1783 		case ZEND_FETCH_LIST_W:
1784 			msg = "Cannot create references to/from string offsets";
1785 			break;
1786 		case ZEND_FETCH_DIM_W:
1787 		case ZEND_FETCH_DIM_RW:
1788 		case ZEND_FETCH_DIM_FUNC_ARG:
1789 		case ZEND_FETCH_DIM_UNSET:
1790 			switch (opline->extended_value) {
1791 				case ZEND_FETCH_DIM_REF:
1792 					msg = "Cannot create references to/from string offsets";
1793 					break;
1794 				case ZEND_FETCH_DIM_DIM:
1795 					msg = "Cannot use string offset as an array";
1796 					break;
1797 				case ZEND_FETCH_DIM_OBJ:
1798 					msg = "Cannot use string offset as an object";
1799 					break;
1800 				case ZEND_FETCH_DIM_INCDEC:
1801 					msg = "Cannot increment/decrement string offsets";
1802 					break;
1803 				EMPTY_SWITCH_DEFAULT_CASE();
1804 			}
1805 			break;
1806 		EMPTY_SWITCH_DEFAULT_CASE();
1807 	}
1808 	ZEND_ASSERT(msg != NULL);
1809 	zend_throw_error(NULL, "%s", msg);
1810 }
1811 
get_deprecation_suffix_from_attribute(HashTable * attributes,zend_class_entry * scope,zend_string ** message_suffix)1812 ZEND_COLD static zend_result ZEND_FASTCALL get_deprecation_suffix_from_attribute(HashTable *attributes, zend_class_entry* scope, zend_string **message_suffix)
1813 {
1814 	*message_suffix = ZSTR_EMPTY_ALLOC();
1815 
1816 	if (!attributes) {
1817 		return SUCCESS;
1818 	}
1819 
1820 	zend_attribute *deprecated = zend_get_attribute_str(attributes, "deprecated", sizeof("deprecated")-1);
1821 
1822 	if (!deprecated) {
1823 		return SUCCESS;
1824 	}
1825 
1826 	if (deprecated->argc == 0) {
1827 		return SUCCESS;
1828 	}
1829 
1830 	zend_result result = FAILURE;
1831 
1832 	zend_string *message = ZSTR_EMPTY_ALLOC();
1833 	zend_string *since = ZSTR_EMPTY_ALLOC();
1834 
1835 	zval obj;
1836 	ZVAL_UNDEF(&obj);
1837 	zval *z;
1838 
1839 	/* Construct the Deprecated object to correctly handle parameter processing. */
1840 	if (FAILURE == zend_get_attribute_object(&obj, zend_ce_deprecated, deprecated, scope, NULL)) {
1841 		goto out;
1842 	}
1843 
1844 	/* Extract the $message property. */
1845 	z = zend_read_property_ex(zend_ce_deprecated, Z_OBJ_P(&obj), ZSTR_KNOWN(ZEND_STR_MESSAGE), false, NULL);
1846 	ZEND_ASSERT(z != &EG(uninitialized_zval));
1847 	if (Z_TYPE_P(z) == IS_STRING) {
1848 		message = zend_string_copy(Z_STR_P(z));
1849 	}
1850 
1851 	/* Extract the $since property. */
1852 	z = zend_read_property_ex(zend_ce_deprecated, Z_OBJ_P(&obj), ZSTR_KNOWN(ZEND_STR_SINCE), false, NULL);
1853 	ZEND_ASSERT(z != &EG(uninitialized_zval));
1854 	if (Z_TYPE_P(z) == IS_STRING) {
1855 		since = zend_string_copy(Z_STR_P(z));
1856 	}
1857 
1858 	/* Construct the suffix. */
1859 	*message_suffix = zend_strpprintf_unchecked(
1860 		0,
1861 		"%s%S%s%S",
1862 		ZSTR_LEN(since) > 0 ? " since " : "",
1863 		since,
1864 		ZSTR_LEN(message) > 0 ? ", " : "",
1865 		message
1866 	);
1867 
1868 	result = SUCCESS;
1869 
1870  out:
1871 
1872 	zend_string_release(since);
1873 	zend_string_release(message);
1874 	zval_ptr_dtor(&obj);
1875 
1876 	return result;
1877 }
1878 
zend_deprecated_function(const zend_function * fbc)1879 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_deprecated_function(const zend_function *fbc)
1880 {
1881 	zend_string *message_suffix = ZSTR_EMPTY_ALLOC();
1882 
1883 	if (get_deprecation_suffix_from_attribute(fbc->common.attributes, fbc->common.scope, &message_suffix) == FAILURE) {
1884 		return;
1885 	}
1886 
1887 	int code = fbc->type == ZEND_INTERNAL_FUNCTION ? E_DEPRECATED : E_USER_DEPRECATED;
1888 
1889 	if (fbc->common.scope) {
1890 		zend_error_unchecked(code, "Method %s::%s() is deprecated%S",
1891 			ZSTR_VAL(fbc->common.scope->name),
1892 			ZSTR_VAL(fbc->common.function_name),
1893 			message_suffix
1894 		);
1895 	} else {
1896 		zend_error_unchecked(code, "Function %s() is deprecated%S",
1897 			ZSTR_VAL(fbc->common.function_name),
1898 			message_suffix
1899 		);
1900 	}
1901 
1902 	zend_string_release(message_suffix);
1903 }
1904 
zend_deprecated_class_constant(const zend_class_constant * c,const zend_string * constant_name)1905 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_deprecated_class_constant(const zend_class_constant *c, const zend_string *constant_name)
1906 {
1907 	zend_string *message_suffix = ZSTR_EMPTY_ALLOC();
1908 
1909 	if (get_deprecation_suffix_from_attribute(c->attributes, c->ce, &message_suffix) == FAILURE) {
1910 		return;
1911 	}
1912 
1913 	int code = c->ce->type == ZEND_INTERNAL_CLASS ? E_DEPRECATED : E_USER_DEPRECATED;
1914 	char *type = (ZEND_CLASS_CONST_FLAGS(c) & ZEND_CLASS_CONST_IS_CASE) ? "Enum case" : "Constant";
1915 
1916 	zend_error_unchecked(code, "%s %s::%s is deprecated%S",
1917 		type,
1918 		ZSTR_VAL(c->ce->name),
1919 		ZSTR_VAL(constant_name),
1920 		message_suffix
1921 	);
1922 
1923 	zend_string_release(message_suffix);
1924 }
1925 
zend_false_to_array_deprecated(void)1926 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_false_to_array_deprecated(void)
1927 {
1928 	zend_error(E_DEPRECATED, "Automatic conversion of false to array is deprecated");
1929 }
1930 
zend_assign_to_string_offset(zval * str,zval * dim,zval * value OPLINE_DC EXECUTE_DATA_DC)1931 static zend_never_inline void zend_assign_to_string_offset(zval *str, zval *dim, zval *value OPLINE_DC EXECUTE_DATA_DC)
1932 {
1933 	zend_uchar c;
1934 	size_t string_len;
1935 	zend_long offset;
1936 	zend_string *s;
1937 
1938 	/* separate string */
1939 	if (Z_REFCOUNTED_P(str) && Z_REFCOUNT_P(str) == 1) {
1940 		s = Z_STR_P(str);
1941 	} else {
1942 		s = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
1943 		ZSTR_H(s) = ZSTR_H(Z_STR_P(str));
1944 		if (Z_REFCOUNTED_P(str)) {
1945 			GC_DELREF(Z_STR_P(str));
1946 		}
1947 		ZVAL_NEW_STR(str, s);
1948 	}
1949 
1950 	if (EXPECTED(Z_TYPE_P(dim) == IS_LONG)) {
1951 		offset = Z_LVAL_P(dim);
1952 	} else {
1953 		/* The string may be destroyed while throwing the notice.
1954 		 * Temporarily increase the refcount to detect this situation. */
1955 		GC_ADDREF(s);
1956 		offset = zend_check_string_offset(dim, BP_VAR_W EXECUTE_DATA_CC);
1957 		if (UNEXPECTED(GC_DELREF(s) == 0)) {
1958 			zend_string_efree(s);
1959 			if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
1960 				ZVAL_NULL(EX_VAR(opline->result.var));
1961 			}
1962 			return;
1963 		}
1964 		/* Illegal offset assignment */
1965 		if (UNEXPECTED(EG(exception) != NULL)) {
1966 			if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
1967 				ZVAL_UNDEF(EX_VAR(opline->result.var));
1968 			}
1969 			return;
1970 		}
1971 	}
1972 
1973 	if (UNEXPECTED(offset < -(zend_long)ZSTR_LEN(s))) {
1974 		/* Error on negative offset */
1975 		zend_error(E_WARNING, "Illegal string offset " ZEND_LONG_FMT, offset);
1976 		if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
1977 			ZVAL_NULL(EX_VAR(opline->result.var));
1978 		}
1979 		return;
1980 	}
1981 
1982 	if (offset < 0) { /* Handle negative offset */
1983 		offset += (zend_long)ZSTR_LEN(s);
1984 	}
1985 
1986 	if (UNEXPECTED(Z_TYPE_P(value) != IS_STRING)) {
1987 		zend_string *tmp;
1988 
1989 		/* The string may be destroyed while throwing the notice.
1990 		 * Temporarily increase the refcount to detect this situation. */
1991 		GC_ADDREF(s);
1992 		if (UNEXPECTED(Z_TYPE_P(value) == IS_UNDEF)) {
1993 			zval_undefined_cv((opline+1)->op1.var EXECUTE_DATA_CC);
1994 		}
1995 		/* Convert to string, just the time to pick the 1st byte */
1996 		tmp = zval_try_get_string_func(value);
1997 		if (UNEXPECTED(GC_DELREF(s) == 0)) {
1998 			zend_string_efree(s);
1999 			if (tmp) {
2000 				zend_string_release_ex(tmp, 0);
2001 			}
2002 			if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
2003 				ZVAL_NULL(EX_VAR(opline->result.var));
2004 			}
2005 			return;
2006 		}
2007 		if (UNEXPECTED(!tmp)) {
2008 			if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
2009 				ZVAL_UNDEF(EX_VAR(opline->result.var));
2010 			}
2011 			return;
2012 		}
2013 
2014 		string_len = ZSTR_LEN(tmp);
2015 		c = (zend_uchar)ZSTR_VAL(tmp)[0];
2016 		zend_string_release_ex(tmp, 0);
2017 	} else {
2018 		string_len = Z_STRLEN_P(value);
2019 		c = (zend_uchar)Z_STRVAL_P(value)[0];
2020 	}
2021 
2022 	if (UNEXPECTED(string_len != 1)) {
2023 		if (string_len == 0) {
2024 			/* Error on empty input string */
2025 			zend_throw_error(NULL, "Cannot assign an empty string to a string offset");
2026 			if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
2027 				ZVAL_NULL(EX_VAR(opline->result.var));
2028 			}
2029 			return;
2030 		}
2031 
2032 		/* The string may be destroyed while throwing the notice.
2033 		 * Temporarily increase the refcount to detect this situation. */
2034 		GC_ADDREF(s);
2035 		zend_error(E_WARNING, "Only the first byte will be assigned to the string offset");
2036 		if (UNEXPECTED(GC_DELREF(s) == 0)) {
2037 			zend_string_efree(s);
2038 			if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
2039 				ZVAL_NULL(EX_VAR(opline->result.var));
2040 			}
2041 			return;
2042 		}
2043 		/* Illegal offset assignment */
2044 		if (UNEXPECTED(EG(exception) != NULL)) {
2045 			if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
2046 				ZVAL_UNDEF(EX_VAR(opline->result.var));
2047 			}
2048 			return;
2049 		}
2050 	}
2051 
2052 	if ((size_t)offset >= ZSTR_LEN(s)) {
2053 		/* Extend string if needed */
2054 		zend_long old_len = ZSTR_LEN(s);
2055 		ZVAL_NEW_STR(str, zend_string_extend(s, (size_t)offset + 1, 0));
2056 		memset(Z_STRVAL_P(str) + old_len, ' ', offset - old_len);
2057 		Z_STRVAL_P(str)[offset+1] = 0;
2058 	} else {
2059 		zend_string_forget_hash_val(Z_STR_P(str));
2060 	}
2061 
2062 	Z_STRVAL_P(str)[offset] = c;
2063 
2064 	if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
2065 		/* Return the new character */
2066 		ZVAL_CHAR(EX_VAR(opline->result.var), c);
2067 	}
2068 }
2069 
zend_get_prop_not_accepting_double(zend_reference * ref)2070 static zend_property_info *zend_get_prop_not_accepting_double(zend_reference *ref)
2071 {
2072 	zend_property_info *prop;
2073 	ZEND_REF_FOREACH_TYPE_SOURCES(ref, prop) {
2074 		if (!(ZEND_TYPE_FULL_MASK(prop->type) & MAY_BE_DOUBLE)) {
2075 			return prop;
2076 		}
2077 	} ZEND_REF_FOREACH_TYPE_SOURCES_END();
2078 	return NULL;
2079 }
2080 
zend_throw_incdec_ref_error(zend_reference * ref,zend_property_info * error_prop OPLINE_DC)2081 static ZEND_COLD zend_long zend_throw_incdec_ref_error(
2082 		zend_reference *ref, zend_property_info *error_prop OPLINE_DC)
2083 {
2084 	zend_string *type_str = zend_type_to_string(error_prop->type);
2085 	if (ZEND_IS_INCREMENT(opline->opcode)) {
2086 		zend_type_error(
2087 			"Cannot increment a reference held by property %s::$%s of type %s past its maximal value",
2088 			ZSTR_VAL(error_prop->ce->name),
2089 			zend_get_unmangled_property_name(error_prop->name),
2090 			ZSTR_VAL(type_str));
2091 		zend_string_release(type_str);
2092 		return ZEND_LONG_MAX;
2093 	} else {
2094 		zend_type_error(
2095 			"Cannot decrement a reference held by property %s::$%s of type %s past its minimal value",
2096 			ZSTR_VAL(error_prop->ce->name),
2097 			zend_get_unmangled_property_name(error_prop->name),
2098 			ZSTR_VAL(type_str));
2099 		zend_string_release(type_str);
2100 		return ZEND_LONG_MIN;
2101 	}
2102 }
2103 
zend_throw_incdec_prop_error(zend_property_info * prop OPLINE_DC)2104 static ZEND_COLD zend_long zend_throw_incdec_prop_error(zend_property_info *prop OPLINE_DC) {
2105 	zend_string *type_str = zend_type_to_string(prop->type);
2106 	if (ZEND_IS_INCREMENT(opline->opcode)) {
2107 		zend_type_error("Cannot increment property %s::$%s of type %s past its maximal value",
2108 			ZSTR_VAL(prop->ce->name),
2109 			zend_get_unmangled_property_name(prop->name),
2110 			ZSTR_VAL(type_str));
2111 		zend_string_release(type_str);
2112 		return ZEND_LONG_MAX;
2113 	} else {
2114 		zend_type_error("Cannot decrement property %s::$%s of type %s past its minimal value",
2115 			ZSTR_VAL(prop->ce->name),
2116 			zend_get_unmangled_property_name(prop->name),
2117 			ZSTR_VAL(type_str));
2118 		zend_string_release(type_str);
2119 		return ZEND_LONG_MIN;
2120 	}
2121 }
2122 
zend_incdec_typed_ref(zend_reference * ref,zval * copy OPLINE_DC EXECUTE_DATA_DC)2123 static void zend_incdec_typed_ref(zend_reference *ref, zval *copy OPLINE_DC EXECUTE_DATA_DC)
2124 {
2125 	zval tmp;
2126 	zval *var_ptr = &ref->val;
2127 
2128 	if (!copy) {
2129 		copy = &tmp;
2130 	}
2131 
2132 	ZVAL_COPY(copy, var_ptr);
2133 
2134 	if (ZEND_IS_INCREMENT(opline->opcode)) {
2135 		increment_function(var_ptr);
2136 	} else {
2137 		decrement_function(var_ptr);
2138 	}
2139 
2140 	if (UNEXPECTED(Z_TYPE_P(var_ptr) == IS_DOUBLE) && Z_TYPE_P(copy) == IS_LONG) {
2141 		zend_property_info *error_prop = zend_get_prop_not_accepting_double(ref);
2142 		if (UNEXPECTED(error_prop)) {
2143 			zend_long val = zend_throw_incdec_ref_error(ref, error_prop OPLINE_CC);
2144 			ZVAL_LONG(var_ptr, val);
2145 		}
2146 	} else if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, var_ptr, EX_USES_STRICT_TYPES()))) {
2147 		zval_ptr_dtor(var_ptr);
2148 		ZVAL_COPY_VALUE(var_ptr, copy);
2149 		ZVAL_UNDEF(copy);
2150 	} else if (copy == &tmp) {
2151 		zval_ptr_dtor(&tmp);
2152 	}
2153 }
2154 
zend_incdec_typed_prop(zend_property_info * prop_info,zval * var_ptr,zval * copy OPLINE_DC EXECUTE_DATA_DC)2155 static void zend_incdec_typed_prop(zend_property_info *prop_info, zval *var_ptr, zval *copy OPLINE_DC EXECUTE_DATA_DC)
2156 {
2157 	zval tmp;
2158 
2159 	if (!copy) {
2160 		copy = &tmp;
2161 	}
2162 
2163 	ZVAL_COPY(copy, var_ptr);
2164 
2165 	if (ZEND_IS_INCREMENT(opline->opcode)) {
2166 		increment_function(var_ptr);
2167 	} else {
2168 		decrement_function(var_ptr);
2169 	}
2170 
2171 	if (UNEXPECTED(Z_TYPE_P(var_ptr) == IS_DOUBLE) && Z_TYPE_P(copy) == IS_LONG) {
2172 		if (!(ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_DOUBLE)) {
2173 			zend_long val = zend_throw_incdec_prop_error(prop_info OPLINE_CC);
2174 			ZVAL_LONG(var_ptr, val);
2175 		}
2176 	} else if (UNEXPECTED(!zend_verify_property_type(prop_info, var_ptr, EX_USES_STRICT_TYPES()))) {
2177 		zval_ptr_dtor(var_ptr);
2178 		ZVAL_COPY_VALUE(var_ptr, copy);
2179 		ZVAL_UNDEF(copy);
2180 	} else if (copy == &tmp) {
2181 		zval_ptr_dtor(&tmp);
2182 	}
2183 }
2184 
zend_pre_incdec_property_zval(zval * prop,zend_property_info * prop_info OPLINE_DC EXECUTE_DATA_DC)2185 static void zend_pre_incdec_property_zval(zval *prop, zend_property_info *prop_info OPLINE_DC EXECUTE_DATA_DC)
2186 {
2187 	if (EXPECTED(Z_TYPE_P(prop) == IS_LONG)) {
2188 		if (ZEND_IS_INCREMENT(opline->opcode)) {
2189 			fast_long_increment_function(prop);
2190 		} else {
2191 			fast_long_decrement_function(prop);
2192 		}
2193 		if (UNEXPECTED(Z_TYPE_P(prop) != IS_LONG) && prop_info
2194 				&& !(ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_DOUBLE)) {
2195 			zend_long val = zend_throw_incdec_prop_error(prop_info OPLINE_CC);
2196 			ZVAL_LONG(prop, val);
2197 		}
2198 	} else {
2199 		do {
2200 			if (Z_ISREF_P(prop)) {
2201 				zend_reference *ref = Z_REF_P(prop);
2202 				prop = Z_REFVAL_P(prop);
2203 				if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) {
2204 					zend_incdec_typed_ref(ref, NULL OPLINE_CC EXECUTE_DATA_CC);
2205 					break;
2206 				}
2207 			}
2208 
2209 			if (prop_info) {
2210 				zend_incdec_typed_prop(prop_info, prop, NULL OPLINE_CC EXECUTE_DATA_CC);
2211 			} else if (ZEND_IS_INCREMENT(opline->opcode)) {
2212 				increment_function(prop);
2213 			} else {
2214 				decrement_function(prop);
2215 			}
2216 		} while (0);
2217 	}
2218 	if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
2219 		ZVAL_COPY(EX_VAR(opline->result.var), prop);
2220 	}
2221 }
2222 
zend_post_incdec_property_zval(zval * prop,zend_property_info * prop_info OPLINE_DC EXECUTE_DATA_DC)2223 static void zend_post_incdec_property_zval(zval *prop, zend_property_info *prop_info OPLINE_DC EXECUTE_DATA_DC)
2224 {
2225 	if (EXPECTED(Z_TYPE_P(prop) == IS_LONG)) {
2226 		ZVAL_LONG(EX_VAR(opline->result.var), Z_LVAL_P(prop));
2227 		if (ZEND_IS_INCREMENT(opline->opcode)) {
2228 			fast_long_increment_function(prop);
2229 		} else {
2230 			fast_long_decrement_function(prop);
2231 		}
2232 		if (UNEXPECTED(Z_TYPE_P(prop) != IS_LONG) && prop_info
2233 				&& !(ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_DOUBLE)) {
2234 			zend_long val = zend_throw_incdec_prop_error(prop_info OPLINE_CC);
2235 			ZVAL_LONG(prop, val);
2236 		}
2237 	} else {
2238 		if (Z_ISREF_P(prop)) {
2239 			zend_reference *ref = Z_REF_P(prop);
2240 			prop = Z_REFVAL_P(prop);
2241 			if (ZEND_REF_HAS_TYPE_SOURCES(ref)) {
2242 				zend_incdec_typed_ref(ref, EX_VAR(opline->result.var) OPLINE_CC EXECUTE_DATA_CC);
2243 				return;
2244 			}
2245 		}
2246 
2247 		if (prop_info) {
2248 			zend_incdec_typed_prop(prop_info, prop, EX_VAR(opline->result.var) OPLINE_CC EXECUTE_DATA_CC);
2249 		} else {
2250 			ZVAL_COPY(EX_VAR(opline->result.var), prop);
2251 			if (ZEND_IS_INCREMENT(opline->opcode)) {
2252 				increment_function(prop);
2253 			} else {
2254 				decrement_function(prop);
2255 			}
2256 		}
2257 	}
2258 }
2259 
zend_post_incdec_overloaded_property(zend_object * object,zend_string * name,void ** cache_slot OPLINE_DC EXECUTE_DATA_DC)2260 static zend_never_inline void zend_post_incdec_overloaded_property(zend_object *object, zend_string *name, void **cache_slot OPLINE_DC EXECUTE_DATA_DC)
2261 {
2262 	zval rv;
2263 	zval *z;
2264 	zval z_copy;
2265 
2266 	GC_ADDREF(object);
2267 	z =object->handlers->read_property(object, name, BP_VAR_R, cache_slot, &rv);
2268 	if (UNEXPECTED(EG(exception))) {
2269 		OBJ_RELEASE(object);
2270 		ZVAL_UNDEF(EX_VAR(opline->result.var));
2271 		return;
2272 	}
2273 
2274 	ZVAL_COPY_DEREF(&z_copy, z);
2275 	ZVAL_COPY(EX_VAR(opline->result.var), &z_copy);
2276 	if (ZEND_IS_INCREMENT(opline->opcode)) {
2277 		increment_function(&z_copy);
2278 	} else {
2279 		decrement_function(&z_copy);
2280 	}
2281 	object->handlers->write_property(object, name, &z_copy, cache_slot);
2282 	OBJ_RELEASE(object);
2283 	zval_ptr_dtor(&z_copy);
2284 	if (z == &rv) {
2285 		zval_ptr_dtor(z);
2286 	}
2287 }
2288 
zend_pre_incdec_overloaded_property(zend_object * object,zend_string * name,void ** cache_slot OPLINE_DC EXECUTE_DATA_DC)2289 static zend_never_inline void zend_pre_incdec_overloaded_property(zend_object *object, zend_string *name, void **cache_slot OPLINE_DC EXECUTE_DATA_DC)
2290 {
2291 	zval rv;
2292 	zval *z;
2293 	zval z_copy;
2294 
2295 	GC_ADDREF(object);
2296 	z = object->handlers->read_property(object, name, BP_VAR_R, cache_slot, &rv);
2297 	if (UNEXPECTED(EG(exception))) {
2298 		OBJ_RELEASE(object);
2299 		if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
2300 			ZVAL_NULL(EX_VAR(opline->result.var));
2301 		}
2302 		return;
2303 	}
2304 
2305 	ZVAL_COPY_DEREF(&z_copy, z);
2306 	if (ZEND_IS_INCREMENT(opline->opcode)) {
2307 		increment_function(&z_copy);
2308 	} else {
2309 		decrement_function(&z_copy);
2310 	}
2311 	if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
2312 		ZVAL_COPY(EX_VAR(opline->result.var), &z_copy);
2313 	}
2314 	object->handlers->write_property(object, name, &z_copy, cache_slot);
2315 	OBJ_RELEASE(object);
2316 	zval_ptr_dtor(&z_copy);
2317 	if (z == &rv) {
2318 		zval_ptr_dtor(z);
2319 	}
2320 }
2321 
zend_assign_op_overloaded_property(zend_object * object,zend_string * name,void ** cache_slot,zval * value OPLINE_DC EXECUTE_DATA_DC)2322 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)
2323 {
2324 	zval *z;
2325 	zval rv, res;
2326 
2327 	GC_ADDREF(object);
2328 	z = object->handlers->read_property(object, name, BP_VAR_R, cache_slot, &rv);
2329 	if (UNEXPECTED(EG(exception))) {
2330 		OBJ_RELEASE(object);
2331 		if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
2332 			ZVAL_UNDEF(EX_VAR(opline->result.var));
2333 		}
2334 		return;
2335 	}
2336 	if (zend_binary_op(&res, z, value OPLINE_CC) == SUCCESS) {
2337 		object->handlers->write_property(object, name, &res, cache_slot);
2338 	}
2339 	if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
2340 		ZVAL_COPY(EX_VAR(opline->result.var), &res);
2341 	}
2342 	if (z == &rv) {
2343 		zval_ptr_dtor(z);
2344 	}
2345 	zval_ptr_dtor(&res);
2346 	OBJ_RELEASE(object);
2347 }
2348 
2349 /* Utility Functions for Extensions */
zend_extension_statement_handler(const zend_extension * extension,zend_execute_data * frame)2350 static void zend_extension_statement_handler(const zend_extension *extension, zend_execute_data *frame)
2351 {
2352 	if (extension->statement_handler) {
2353 		extension->statement_handler(frame);
2354 	}
2355 }
2356 
2357 
zend_extension_fcall_begin_handler(const zend_extension * extension,zend_execute_data * frame)2358 static void zend_extension_fcall_begin_handler(const zend_extension *extension, zend_execute_data *frame)
2359 {
2360 	if (extension->fcall_begin_handler) {
2361 		extension->fcall_begin_handler(frame);
2362 	}
2363 }
2364 
2365 
zend_extension_fcall_end_handler(const zend_extension * extension,zend_execute_data * frame)2366 static void zend_extension_fcall_end_handler(const zend_extension *extension, zend_execute_data *frame)
2367 {
2368 	if (extension->fcall_end_handler) {
2369 		extension->fcall_end_handler(frame);
2370 	}
2371 }
2372 
2373 
zend_get_target_symbol_table(int fetch_type EXECUTE_DATA_DC)2374 static zend_always_inline HashTable *zend_get_target_symbol_table(int fetch_type EXECUTE_DATA_DC)
2375 {
2376 	HashTable *ht;
2377 
2378 	if (EXPECTED(fetch_type & (ZEND_FETCH_GLOBAL_LOCK | ZEND_FETCH_GLOBAL))) {
2379 		ht = &EG(symbol_table);
2380 	} else {
2381 		ZEND_ASSERT(fetch_type & ZEND_FETCH_LOCAL);
2382 		if (!(EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE)) {
2383 			zend_rebuild_symbol_table();
2384 		}
2385 		ht = EX(symbol_table);
2386 	}
2387 	return ht;
2388 }
2389 
zend_undefined_offset(zend_long lval)2390 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_undefined_offset(zend_long lval)
2391 {
2392 	zend_error(E_WARNING, "Undefined array key " ZEND_LONG_FMT, lval);
2393 }
2394 
zend_undefined_index(const zend_string * offset)2395 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_undefined_index(const zend_string *offset)
2396 {
2397 	zend_error(E_WARNING, "Undefined array key \"%s\"", ZSTR_VAL(offset));
2398 }
2399 
zend_undefined_offset_write(HashTable * ht,zend_long lval)2400 ZEND_API ZEND_COLD zval* ZEND_FASTCALL zend_undefined_offset_write(HashTable *ht, zend_long lval)
2401 {
2402 	/* The array may be destroyed while throwing the notice.
2403 	 * Temporarily increase the refcount to detect this situation. */
2404 	if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
2405 		GC_ADDREF(ht);
2406 	}
2407 	zend_undefined_offset(lval);
2408 	if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) {
2409 		if (!GC_REFCOUNT(ht)) {
2410 			zend_array_destroy(ht);
2411 		}
2412 		return NULL;
2413 	}
2414 	if (EG(exception)) {
2415 		return NULL;
2416 	}
2417 	return zend_hash_index_add_new(ht, lval, &EG(uninitialized_zval));
2418 }
2419 
zend_undefined_index_write(HashTable * ht,zend_string * offset)2420 ZEND_API ZEND_COLD zval* ZEND_FASTCALL zend_undefined_index_write(HashTable *ht, zend_string *offset)
2421 {
2422 	zval *retval;
2423 
2424 	/* The array may be destroyed while throwing the notice.
2425 	 * Temporarily increase the refcount to detect this situation. */
2426 	if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
2427 		GC_ADDREF(ht);
2428 	}
2429 	/* Key may be released while throwing the undefined index warning. */
2430 	zend_string_addref(offset);
2431 	zend_undefined_index(offset);
2432 	if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) {
2433 		if (!GC_REFCOUNT(ht)) {
2434 			zend_array_destroy(ht);
2435 		}
2436 		retval = NULL;
2437 	} else if (EG(exception)) {
2438 		retval = NULL;
2439 	} else {
2440 		retval = zend_hash_add_new(ht, offset, &EG(uninitialized_zval));
2441 	}
2442 	zend_string_release(offset);
2443 	return retval;
2444 }
2445 
zend_undefined_method(const zend_class_entry * ce,const zend_string * method)2446 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_undefined_method(const zend_class_entry *ce, const zend_string *method)
2447 {
2448 	zend_throw_error(NULL, "Call to undefined method %s::%s()", ZSTR_VAL(ce->name), ZSTR_VAL(method));
2449 }
2450 
zend_invalid_method_call(zval * object,zval * function_name)2451 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_invalid_method_call(zval *object, zval *function_name)
2452 {
2453 	zend_throw_error(NULL, "Call to a member function %s() on %s",
2454 		Z_STRVAL_P(function_name), zend_zval_value_name(object));
2455 }
2456 
zend_non_static_method_call(const zend_function * fbc)2457 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_non_static_method_call(const zend_function *fbc)
2458 {
2459 	zend_throw_error(
2460 		zend_ce_error,
2461 		"Non-static method %s::%s() cannot be called statically",
2462 		ZSTR_VAL(fbc->common.scope->name), ZSTR_VAL(fbc->common.function_name));
2463 }
2464 
zend_param_must_be_ref(const zend_function * func,uint32_t arg_num)2465 ZEND_COLD void ZEND_FASTCALL zend_param_must_be_ref(const zend_function *func, uint32_t arg_num)
2466 {
2467 	const char *arg_name = get_function_arg_name(func, arg_num);
2468 
2469 	zend_error(E_WARNING, "%s%s%s(): Argument #%d%s%s%s must be passed by reference, value given",
2470 		func->common.scope ? ZSTR_VAL(func->common.scope->name) : "",
2471 		func->common.scope ? "::" : "",
2472 		ZSTR_VAL(func->common.function_name),
2473 		arg_num,
2474 		arg_name ? " ($" : "",
2475 		arg_name ? arg_name : "",
2476 		arg_name ? ")" : ""
2477 	);
2478 }
2479 
zend_use_scalar_as_array(void)2480 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_scalar_as_array(void)
2481 {
2482 	zend_throw_error(NULL, "Cannot use a scalar value as an array");
2483 }
2484 
zend_cannot_add_element(void)2485 ZEND_API zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(void)
2486 {
2487 	zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
2488 }
2489 
zend_use_resource_as_offset(const zval * dim)2490 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_use_resource_as_offset(const zval *dim)
2491 {
2492 	zend_error(E_WARNING,
2493 		"Resource ID#" ZEND_LONG_FMT " used as offset, casting to integer (" ZEND_LONG_FMT ")",
2494 		Z_RES_HANDLE_P(dim), Z_RES_HANDLE_P(dim));
2495 }
2496 
zend_use_new_element_for_string(void)2497 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_new_element_for_string(void)
2498 {
2499 	zend_throw_error(NULL, "[] operator not supported for strings");
2500 }
2501 
2502 #ifdef ZEND_CHECK_STACK_LIMIT
zend_call_stack_size_error(void)2503 ZEND_API zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_call_stack_size_error(void)
2504 {
2505 	size_t max_stack_size = 0;
2506 	if ((uintptr_t) EG(stack_base) > (uintptr_t) EG(stack_limit)) {
2507 		max_stack_size = (size_t) ((uintptr_t) EG(stack_base) - (uintptr_t) EG(stack_limit));
2508 	}
2509 
2510 	zend_throw_error(NULL, "Maximum call stack size of %zu bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?",
2511 		max_stack_size);
2512 }
2513 #endif /* ZEND_CHECK_STACK_LIMIT */
2514 
zend_binary_assign_op_dim_slow(zval * container,zval * dim OPLINE_DC EXECUTE_DATA_DC)2515 static ZEND_COLD void zend_binary_assign_op_dim_slow(zval *container, zval *dim OPLINE_DC EXECUTE_DATA_DC)
2516 {
2517 	if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) {
2518 		if (opline->op2_type == IS_UNUSED) {
2519 			zend_use_new_element_for_string();
2520 		} else {
2521 			zend_check_string_offset(dim, BP_VAR_RW EXECUTE_DATA_CC);
2522 			zend_wrong_string_offset_error();
2523 		}
2524 	} else {
2525 		zend_use_scalar_as_array();
2526 	}
2527 }
2528 
slow_index_convert(HashTable * ht,const zval * dim,zend_value * value EXECUTE_DATA_DC)2529 static zend_never_inline uint8_t slow_index_convert(HashTable *ht, const zval *dim, zend_value *value EXECUTE_DATA_DC)
2530 {
2531 	switch (Z_TYPE_P(dim)) {
2532 		case IS_UNDEF: {
2533 			/* The array may be destroyed while throwing the notice.
2534 			 * Temporarily increase the refcount to detect this situation. */
2535 			if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
2536 				GC_ADDREF(ht);
2537 			}
2538 			ZVAL_UNDEFINED_OP2();
2539 			if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) {
2540 				zend_array_destroy(ht);
2541 				return IS_NULL;
2542 			}
2543 			if (EG(exception)) {
2544 				return IS_NULL;
2545 			}
2546 			ZEND_FALLTHROUGH;
2547 		}
2548 		case IS_NULL:
2549 			value->str = ZSTR_EMPTY_ALLOC();
2550 			return IS_STRING;
2551 		case IS_DOUBLE:
2552 			value->lval = zend_dval_to_lval(Z_DVAL_P(dim));
2553 			if (!zend_is_long_compatible(Z_DVAL_P(dim), value->lval)) {
2554 				/* The array may be destroyed while throwing the notice.
2555 				 * Temporarily increase the refcount to detect this situation. */
2556 				if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
2557 					GC_ADDREF(ht);
2558 				}
2559 				zend_incompatible_double_to_long_error(Z_DVAL_P(dim));
2560 				if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) {
2561 					zend_array_destroy(ht);
2562 					return IS_NULL;
2563 				}
2564 				if (EG(exception)) {
2565 					return IS_NULL;
2566 				}
2567 			}
2568 			return IS_LONG;
2569 		case IS_RESOURCE:
2570 			/* The array may be destroyed while throwing the notice.
2571 			 * Temporarily increase the refcount to detect this situation. */
2572 			if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
2573 				GC_ADDREF(ht);
2574 			}
2575 			zend_use_resource_as_offset(dim);
2576 			if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) {
2577 				zend_array_destroy(ht);
2578 				return IS_NULL;
2579 			}
2580 			if (EG(exception)) {
2581 				return IS_NULL;
2582 			}
2583 			value->lval = Z_RES_HANDLE_P(dim);
2584 			return IS_LONG;
2585 		case IS_FALSE:
2586 			value->lval = 0;
2587 			return IS_LONG;
2588 		case IS_TRUE:
2589 			value->lval = 1;
2590 			return IS_LONG;
2591 		default:
2592 			zend_illegal_array_offset_access(dim);
2593 			return IS_NULL;
2594 	}
2595 }
2596 
slow_index_convert_w(HashTable * ht,const zval * dim,zend_value * value EXECUTE_DATA_DC)2597 static zend_never_inline uint8_t slow_index_convert_w(HashTable *ht, const zval *dim, zend_value *value EXECUTE_DATA_DC)
2598 {
2599 	switch (Z_TYPE_P(dim)) {
2600 		case IS_UNDEF: {
2601 			/* The array may be destroyed while throwing the notice.
2602 			 * Temporarily increase the refcount to detect this situation. */
2603 			if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
2604 				GC_ADDREF(ht);
2605 			}
2606 			ZVAL_UNDEFINED_OP2();
2607 			if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) {
2608 				if (!GC_REFCOUNT(ht)) {
2609 					zend_array_destroy(ht);
2610 				}
2611 				return IS_NULL;
2612 			}
2613 			if (EG(exception)) {
2614 				return IS_NULL;
2615 			}
2616 			ZEND_FALLTHROUGH;
2617 		}
2618 		case IS_NULL:
2619 			value->str = ZSTR_EMPTY_ALLOC();
2620 			return IS_STRING;
2621 		case IS_DOUBLE:
2622 			value->lval = zend_dval_to_lval(Z_DVAL_P(dim));
2623 			if (!zend_is_long_compatible(Z_DVAL_P(dim), value->lval)) {
2624 				/* The array may be destroyed while throwing the notice.
2625 				 * Temporarily increase the refcount to detect this situation. */
2626 				if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
2627 					GC_ADDREF(ht);
2628 				}
2629 				zend_incompatible_double_to_long_error(Z_DVAL_P(dim));
2630 				if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) {
2631 					if (!GC_REFCOUNT(ht)) {
2632 						zend_array_destroy(ht);
2633 					}
2634 					return IS_NULL;
2635 				}
2636 				if (EG(exception)) {
2637 					return IS_NULL;
2638 				}
2639 			}
2640 			return IS_LONG;
2641 		case IS_RESOURCE:
2642 			/* The array may be destroyed while throwing the notice.
2643 			 * Temporarily increase the refcount to detect this situation. */
2644 			if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
2645 				GC_ADDREF(ht);
2646 			}
2647 			zend_use_resource_as_offset(dim);
2648 			if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) {
2649 				if (!GC_REFCOUNT(ht)) {
2650 					zend_array_destroy(ht);
2651 				}
2652 				return IS_NULL;
2653 			}
2654 			if (EG(exception)) {
2655 				return IS_NULL;
2656 			}
2657 			value->lval = Z_RES_HANDLE_P(dim);
2658 			return IS_LONG;
2659 		case IS_FALSE:
2660 			value->lval = 0;
2661 			return IS_LONG;
2662 		case IS_TRUE:
2663 			value->lval = 1;
2664 			return IS_LONG;
2665 		default:
2666 			zend_illegal_array_offset_access(dim);
2667 			return IS_NULL;
2668 	}
2669 }
2670 
zend_fetch_dimension_address_inner(HashTable * ht,const zval * dim,int dim_type,int type EXECUTE_DATA_DC)2671 static zend_always_inline zval *zend_fetch_dimension_address_inner(HashTable *ht, const zval *dim, int dim_type, int type EXECUTE_DATA_DC)
2672 {
2673 	zval *retval = NULL;
2674 	zend_string *offset_key;
2675 	zend_ulong hval;
2676 
2677 try_again:
2678 	if (EXPECTED(Z_TYPE_P(dim) == IS_LONG)) {
2679 		hval = Z_LVAL_P(dim);
2680 num_index:
2681 		if (type != BP_VAR_W) {
2682 			ZEND_HASH_INDEX_FIND(ht, hval, retval, num_undef);
2683 			return retval;
2684 num_undef:
2685 			switch (type) {
2686 				case BP_VAR_R:
2687 					zend_undefined_offset(hval);
2688 					ZEND_FALLTHROUGH;
2689 				case BP_VAR_UNSET:
2690 				case BP_VAR_IS:
2691 					retval = &EG(uninitialized_zval);
2692 					break;
2693 				case BP_VAR_RW:
2694 					retval = zend_undefined_offset_write(ht, hval);
2695 					break;
2696 				}
2697 		} else {
2698 			ZEND_HASH_INDEX_LOOKUP(ht, hval, retval);
2699 		}
2700 	} else if (EXPECTED(Z_TYPE_P(dim) == IS_STRING)) {
2701 		offset_key = Z_STR_P(dim);
2702 		if (ZEND_CONST_COND(dim_type != IS_CONST, 1)) {
2703 			if (ZEND_HANDLE_NUMERIC(offset_key, hval)) {
2704 				goto num_index;
2705 			}
2706 		}
2707 str_index:
2708 		if (type != BP_VAR_W) {
2709 			retval = zend_hash_find_ex(ht, offset_key, ZEND_CONST_COND(dim_type == IS_CONST, 0));
2710 			if (!retval) {
2711 				switch (type) {
2712 					case BP_VAR_R:
2713 						zend_undefined_index(offset_key);
2714 						ZEND_FALLTHROUGH;
2715 					case BP_VAR_UNSET:
2716 					case BP_VAR_IS:
2717 						retval = &EG(uninitialized_zval);
2718 						break;
2719 					case BP_VAR_RW:
2720 						retval = zend_undefined_index_write(ht, offset_key);
2721 						break;
2722 				}
2723 			}
2724 		} else {
2725 			retval = zend_hash_lookup(ht, offset_key);
2726 		}
2727 	} else if (EXPECTED(Z_TYPE_P(dim) == IS_REFERENCE)) {
2728 		dim = Z_REFVAL_P(dim);
2729 		goto try_again;
2730 	} else {
2731 		zend_value val;
2732 		uint8_t t;
2733 
2734 		if (type != BP_VAR_W && type != BP_VAR_RW) {
2735 			t = slow_index_convert(ht, dim, &val EXECUTE_DATA_CC);
2736 		} else {
2737 			t = slow_index_convert_w(ht, dim, &val EXECUTE_DATA_CC);
2738 		}
2739 		if (t == IS_STRING) {
2740 			offset_key = val.str;
2741 			goto str_index;
2742 		} else if (t == IS_LONG) {
2743 			hval = val.lval;
2744 			goto num_index;
2745 		} else {
2746 			retval = (type == BP_VAR_W || type == BP_VAR_RW) ?
2747 					NULL : &EG(uninitialized_zval);
2748 		}
2749 	}
2750 	return retval;
2751 }
2752 
zend_fetch_dimension_address_inner_W(HashTable * ht,const zval * dim EXECUTE_DATA_DC)2753 static zend_never_inline zval* ZEND_FASTCALL zend_fetch_dimension_address_inner_W(HashTable *ht, const zval *dim EXECUTE_DATA_DC)
2754 {
2755 	return zend_fetch_dimension_address_inner(ht, dim, IS_TMP_VAR, BP_VAR_W EXECUTE_DATA_CC);
2756 }
2757 
zend_fetch_dimension_address_inner_W_CONST(HashTable * ht,const zval * dim EXECUTE_DATA_DC)2758 static zend_never_inline zval* ZEND_FASTCALL zend_fetch_dimension_address_inner_W_CONST(HashTable *ht, const zval *dim EXECUTE_DATA_DC)
2759 {
2760 	return zend_fetch_dimension_address_inner(ht, dim, IS_CONST, BP_VAR_W EXECUTE_DATA_CC);
2761 }
2762 
zend_fetch_dimension_address_inner_RW(HashTable * ht,const zval * dim EXECUTE_DATA_DC)2763 static zend_never_inline zval* ZEND_FASTCALL zend_fetch_dimension_address_inner_RW(HashTable *ht, const zval *dim EXECUTE_DATA_DC)
2764 {
2765 	return zend_fetch_dimension_address_inner(ht, dim, IS_TMP_VAR, BP_VAR_RW EXECUTE_DATA_CC);
2766 }
2767 
zend_fetch_dimension_address_inner_RW_CONST(HashTable * ht,const zval * dim EXECUTE_DATA_DC)2768 static zend_never_inline zval* ZEND_FASTCALL zend_fetch_dimension_address_inner_RW_CONST(HashTable *ht, const zval *dim EXECUTE_DATA_DC)
2769 {
2770 	return zend_fetch_dimension_address_inner(ht, dim, IS_CONST, BP_VAR_RW EXECUTE_DATA_CC);
2771 }
2772 
zend_fetch_dimension_address(zval * result,zval * container,zval * dim,int dim_type,int type EXECUTE_DATA_DC)2773 static zend_always_inline void zend_fetch_dimension_address(zval *result, zval *container, zval *dim, int dim_type, int type EXECUTE_DATA_DC)
2774 {
2775 	zval *retval;
2776 
2777 	if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
2778 try_array:
2779 		SEPARATE_ARRAY(container);
2780 fetch_from_array:
2781 		if (dim == NULL) {
2782 			retval = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval));
2783 			if (UNEXPECTED(retval == NULL)) {
2784 				zend_cannot_add_element();
2785 				ZVAL_UNDEF(result);
2786 				return;
2787 			}
2788 		} else {
2789 			retval = zend_fetch_dimension_address_inner(Z_ARRVAL_P(container), dim, dim_type, type EXECUTE_DATA_CC);
2790 			if (UNEXPECTED(!retval)) {
2791 				/* This may fail without throwing if the array was modified while throwing an
2792 				 * undefined index error. */
2793 				ZVAL_NULL(result);
2794 				return;
2795 			}
2796 		}
2797 		ZVAL_INDIRECT(result, retval);
2798 		return;
2799 	} else if (EXPECTED(Z_TYPE_P(container) == IS_REFERENCE)) {
2800 		zend_reference *ref = Z_REF_P(container);
2801 		container = Z_REFVAL_P(container);
2802 		if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
2803 			goto try_array;
2804 		} else if (EXPECTED(Z_TYPE_P(container) <= IS_FALSE)) {
2805 			if (type != BP_VAR_UNSET) {
2806 				if (ZEND_REF_HAS_TYPE_SOURCES(ref)) {
2807 					if (UNEXPECTED(!zend_verify_ref_array_assignable(ref))) {
2808 						ZVAL_UNDEF(result);
2809 						return;
2810 					}
2811 				}
2812 				array_init(container);
2813 				goto fetch_from_array;
2814 			} else {
2815 				goto return_null;
2816 			}
2817 		}
2818 	}
2819 	if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) {
2820 		if (dim == NULL) {
2821 			zend_use_new_element_for_string();
2822 		} else {
2823 			zend_check_string_offset(dim, type EXECUTE_DATA_CC);
2824 			zend_wrong_string_offset_error();
2825 		}
2826 		ZVAL_UNDEF(result);
2827 	} else if (EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
2828 		zend_object *obj = Z_OBJ_P(container);
2829 		GC_ADDREF(obj);
2830 		if (ZEND_CONST_COND(dim_type == IS_CV, dim != NULL) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
2831 			dim = ZVAL_UNDEFINED_OP2();
2832 		} else if (dim_type == IS_CONST && Z_EXTRA_P(dim) == ZEND_EXTRA_VALUE) {
2833 			dim++;
2834 		}
2835 		retval = obj->handlers->read_dimension(obj, dim, type, result);
2836 
2837 		if (UNEXPECTED(retval == &EG(uninitialized_zval))) {
2838 			zend_class_entry *ce = obj->ce;
2839 
2840 			ZVAL_NULL(result);
2841 			zend_error(E_NOTICE, "Indirect modification of overloaded element of %s has no effect", ZSTR_VAL(ce->name));
2842 		} else if (EXPECTED(retval && Z_TYPE_P(retval) != IS_UNDEF)) {
2843 			if (!Z_ISREF_P(retval)) {
2844 				if (result != retval) {
2845 					ZVAL_COPY(result, retval);
2846 					retval = result;
2847 				}
2848 				if (Z_TYPE_P(retval) != IS_OBJECT) {
2849 					zend_class_entry *ce = obj->ce;
2850 					zend_error(E_NOTICE, "Indirect modification of overloaded element of %s has no effect", ZSTR_VAL(ce->name));
2851 				}
2852 			} else if (UNEXPECTED(Z_REFCOUNT_P(retval) == 1)) {
2853 				ZVAL_UNREF(retval);
2854 			}
2855 			if (result != retval) {
2856 				ZVAL_INDIRECT(result, retval);
2857 			}
2858 		} else {
2859 			ZEND_ASSERT(EG(exception) && "read_dimension() returned NULL without exception");
2860 			ZVAL_UNDEF(result);
2861 		}
2862 		if (UNEXPECTED(GC_DELREF(obj) == 0)) {
2863 			zend_objects_store_del(obj);
2864 		}
2865 	} else {
2866 		if (EXPECTED(Z_TYPE_P(container) <= IS_FALSE)) {
2867 			if (type != BP_VAR_W && UNEXPECTED(Z_TYPE_P(container) == IS_UNDEF)) {
2868 				ZVAL_UNDEFINED_OP1();
2869 			}
2870 			if (type != BP_VAR_UNSET) {
2871 				HashTable *ht = zend_new_array(0);
2872 				uint8_t old_type = Z_TYPE_P(container);
2873 
2874 				ZVAL_ARR(container, ht);
2875 				if (UNEXPECTED(old_type == IS_FALSE)) {
2876 					GC_ADDREF(ht);
2877 					zend_false_to_array_deprecated();
2878 					if (UNEXPECTED(GC_DELREF(ht) == 0)) {
2879 						zend_array_destroy(ht);
2880 						goto return_null;
2881 					}
2882 				}
2883 				goto fetch_from_array;
2884 			} else {
2885 				if (UNEXPECTED(Z_TYPE_P(container) == IS_FALSE)) {
2886 					zend_false_to_array_deprecated();
2887 				}
2888 return_null:
2889 				/* for read-mode only */
2890 				if (ZEND_CONST_COND(dim_type == IS_CV, dim != NULL) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
2891 					ZVAL_UNDEFINED_OP2();
2892 				}
2893 				ZVAL_NULL(result);
2894 			}
2895 		} else {
2896 			if (type == BP_VAR_UNSET) {
2897 				zend_throw_error(NULL, "Cannot unset offset in a non-array variable");
2898 				ZVAL_UNDEF(result);
2899 			} else {
2900 				zend_use_scalar_as_array();
2901 				ZVAL_UNDEF(result);
2902 			}
2903 		}
2904 	}
2905 }
2906 
zend_fetch_dimension_address_W(zval * container_ptr,zval * dim,int dim_type OPLINE_DC EXECUTE_DATA_DC)2907 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)
2908 {
2909 	zval *result = EX_VAR(opline->result.var);
2910 	zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_W EXECUTE_DATA_CC);
2911 }
2912 
zend_fetch_dimension_address_RW(zval * container_ptr,zval * dim,int dim_type OPLINE_DC EXECUTE_DATA_DC)2913 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)
2914 {
2915 	zval *result = EX_VAR(opline->result.var);
2916 	zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_RW EXECUTE_DATA_CC);
2917 }
2918 
zend_fetch_dimension_address_UNSET(zval * container_ptr,zval * dim,int dim_type OPLINE_DC EXECUTE_DATA_DC)2919 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)
2920 {
2921 	zval *result = EX_VAR(opline->result.var);
2922 	zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_UNSET EXECUTE_DATA_CC);
2923 }
2924 
zend_fetch_dimension_address_read(zval * result,zval * container,zval * dim,int dim_type,int type,bool is_list,int slow EXECUTE_DATA_DC)2925 static zend_always_inline void zend_fetch_dimension_address_read(zval *result, zval *container, zval *dim, int dim_type, int type, bool is_list, int slow EXECUTE_DATA_DC)
2926 {
2927 	zval *retval;
2928 
2929 	if (!slow) {
2930 		if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
2931 try_array:
2932 			retval = zend_fetch_dimension_address_inner(Z_ARRVAL_P(container), dim, dim_type, type EXECUTE_DATA_CC);
2933 			ZVAL_COPY_DEREF(result, retval);
2934 			return;
2935 		} else if (EXPECTED(Z_TYPE_P(container) == IS_REFERENCE)) {
2936 			container = Z_REFVAL_P(container);
2937 			if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
2938 				goto try_array;
2939 			}
2940 		}
2941 	}
2942 	if (!is_list && EXPECTED(Z_TYPE_P(container) == IS_STRING)) {
2943 		zend_string *str = Z_STR_P(container);
2944 		zend_long offset;
2945 
2946 try_string_offset:
2947 		if (UNEXPECTED(Z_TYPE_P(dim) != IS_LONG)) {
2948 			switch (Z_TYPE_P(dim)) {
2949 				case IS_STRING:
2950 				{
2951 					bool trailing_data = false;
2952 					/* For BC reasons we allow errors so that we can warn on leading numeric string */
2953 					if (IS_LONG == is_numeric_string_ex(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset,
2954 							NULL, /* allow errors */ true, NULL, &trailing_data)) {
2955 						if (UNEXPECTED(trailing_data)) {
2956 							zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
2957 						}
2958 						goto out;
2959 					}
2960 					if (type == BP_VAR_IS) {
2961 						ZVAL_NULL(result);
2962 						return;
2963 					}
2964 					zend_illegal_string_offset(dim, BP_VAR_R);
2965 					ZVAL_NULL(result);
2966 					return;
2967 				}
2968 				case IS_UNDEF:
2969 					/* The string may be destroyed while throwing the notice.
2970 					 * Temporarily increase the refcount to detect this situation. */
2971 					if (!(GC_FLAGS(str) & IS_STR_INTERNED)) {
2972 						GC_ADDREF(str);
2973 					}
2974 					ZVAL_UNDEFINED_OP2();
2975 					if (!(GC_FLAGS(str) & IS_STR_INTERNED) && UNEXPECTED(GC_DELREF(str) == 0)) {
2976 						zend_string_efree(str);
2977 						ZVAL_NULL(result);
2978 						return;
2979 					}
2980 					ZEND_FALLTHROUGH;
2981 				case IS_DOUBLE:
2982 				case IS_NULL:
2983 				case IS_FALSE:
2984 				case IS_TRUE:
2985 					if (type != BP_VAR_IS) {
2986 						/* The string may be destroyed while throwing the notice.
2987 						 * Temporarily increase the refcount to detect this situation. */
2988 						if (!(GC_FLAGS(str) & IS_STR_INTERNED)) {
2989 							GC_ADDREF(str);
2990 						}
2991 						zend_error(E_WARNING, "String offset cast occurred");
2992 						if (!(GC_FLAGS(str) & IS_STR_INTERNED) && UNEXPECTED(GC_DELREF(str) == 0)) {
2993 							zend_string_efree(str);
2994 							ZVAL_NULL(result);
2995 							return;
2996 						}
2997 					}
2998 					break;
2999 				case IS_REFERENCE:
3000 					dim = Z_REFVAL_P(dim);
3001 					goto try_string_offset;
3002 				default:
3003 					zend_illegal_string_offset(dim, BP_VAR_R);
3004 					ZVAL_NULL(result);
3005 					return;
3006 			}
3007 
3008 			offset = zval_get_long_func(dim, /* is_strict */ false);
3009 		} else {
3010 			offset = Z_LVAL_P(dim);
3011 		}
3012 		out:
3013 
3014 		if (UNEXPECTED(ZSTR_LEN(str) < ((offset < 0) ? -(size_t)offset : ((size_t)offset + 1)))) {
3015 			if (type != BP_VAR_IS) {
3016 				zend_error(E_WARNING, "Uninitialized string offset " ZEND_LONG_FMT, offset);
3017 				ZVAL_EMPTY_STRING(result);
3018 			} else {
3019 				ZVAL_NULL(result);
3020 			}
3021 		} else {
3022 			zend_uchar c;
3023 			zend_long real_offset;
3024 
3025 			real_offset = (UNEXPECTED(offset < 0)) /* Handle negative offset */
3026 				? (zend_long)ZSTR_LEN(str) + offset : offset;
3027 			c = (zend_uchar)ZSTR_VAL(str)[real_offset];
3028 
3029 			ZVAL_CHAR(result, c);
3030 		}
3031 	} else if (EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
3032 		zend_object *obj = Z_OBJ_P(container);
3033 
3034 		GC_ADDREF(obj);
3035 		if (ZEND_CONST_COND(dim_type == IS_CV, 1) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
3036 			dim = ZVAL_UNDEFINED_OP2();
3037 		}
3038 		if (dim_type == IS_CONST && Z_EXTRA_P(dim) == ZEND_EXTRA_VALUE) {
3039 			dim++;
3040 		}
3041 		retval = obj->handlers->read_dimension(obj, dim, type, result);
3042 
3043 		ZEND_ASSERT(result != NULL);
3044 		if (retval) {
3045 			if (result != retval) {
3046 				ZVAL_COPY_DEREF(result, retval);
3047 			} else if (UNEXPECTED(Z_ISREF_P(retval))) {
3048 				zend_unwrap_reference(result);
3049 			}
3050 		} else {
3051 			ZVAL_NULL(result);
3052 		}
3053 		if (UNEXPECTED(GC_DELREF(obj) == 0)) {
3054 			zend_objects_store_del(obj);
3055 		}
3056 	} else {
3057 		if (type != BP_VAR_IS && UNEXPECTED(Z_TYPE_P(container) == IS_UNDEF)) {
3058 			container = ZVAL_UNDEFINED_OP1();
3059 		}
3060 		if (ZEND_CONST_COND(dim_type == IS_CV, 1) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
3061 			ZVAL_UNDEFINED_OP2();
3062 		}
3063 		if (!is_list && type != BP_VAR_IS) {
3064 			zend_error(E_WARNING, "Trying to access array offset on %s",
3065 				zend_zval_value_name(container));
3066 		}
3067 		ZVAL_NULL(result);
3068 	}
3069 }
3070 
zend_fetch_dimension_address_read_R(zval * container,zval * dim,int dim_type OPLINE_DC EXECUTE_DATA_DC)3071 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)
3072 {
3073 	zval *result = EX_VAR(opline->result.var);
3074 	zend_fetch_dimension_address_read(result, container, dim, dim_type, BP_VAR_R, 0, 0 EXECUTE_DATA_CC);
3075 }
3076 
zend_fetch_dimension_address_read_R_slow(zval * container,zval * dim OPLINE_DC EXECUTE_DATA_DC)3077 static zend_never_inline void zend_fetch_dimension_address_read_R_slow(zval *container, zval *dim OPLINE_DC EXECUTE_DATA_DC)
3078 {
3079 	zval *result = EX_VAR(opline->result.var);
3080 	zend_fetch_dimension_address_read(result, container, dim, IS_CV, BP_VAR_R, 0, 1 EXECUTE_DATA_CC);
3081 }
3082 
zend_fetch_dimension_address_read_IS(zval * container,zval * dim,int dim_type OPLINE_DC EXECUTE_DATA_DC)3083 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)
3084 {
3085 	zval *result = EX_VAR(opline->result.var);
3086 	zend_fetch_dimension_address_read(result, container, dim, dim_type, BP_VAR_IS, 0, 0 EXECUTE_DATA_CC);
3087 }
3088 
zend_fetch_dimension_address_LIST_r(zval * container,zval * dim,int dim_type OPLINE_DC EXECUTE_DATA_DC)3089 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)
3090 {
3091 	zval *result = EX_VAR(opline->result.var);
3092 	zend_fetch_dimension_address_read(result, container, dim, dim_type, BP_VAR_R, 1, 0 EXECUTE_DATA_CC);
3093 }
3094 
zend_fetch_dimension_const(zval * result,zval * container,zval * dim,int type)3095 ZEND_API void zend_fetch_dimension_const(zval *result, zval *container, zval *dim, int type)
3096 {
3097 	zend_fetch_dimension_address_read(result, container, dim, IS_TMP_VAR, type, 0, 0 NO_EXECUTE_DATA_CC);
3098 }
3099 
zend_find_array_dim_slow(HashTable * ht,zval * offset EXECUTE_DATA_DC)3100 static zend_never_inline zval* ZEND_FASTCALL zend_find_array_dim_slow(HashTable *ht, zval *offset EXECUTE_DATA_DC)
3101 {
3102 	zend_ulong hval;
3103 
3104 	if (Z_TYPE_P(offset) == IS_DOUBLE) {
3105 		hval = zend_dval_to_lval_safe(Z_DVAL_P(offset));
3106 num_idx:
3107 		return zend_hash_index_find(ht, hval);
3108 	} else if (Z_TYPE_P(offset) == IS_NULL) {
3109 str_idx:
3110 		return zend_hash_find_known_hash(ht, ZSTR_EMPTY_ALLOC());
3111 	} else if (Z_TYPE_P(offset) == IS_FALSE) {
3112 		hval = 0;
3113 		goto num_idx;
3114 	} else if (Z_TYPE_P(offset) == IS_TRUE) {
3115 		hval = 1;
3116 		goto num_idx;
3117 	} else if (Z_TYPE_P(offset) == IS_RESOURCE) {
3118 		zend_use_resource_as_offset(offset);
3119 		hval = Z_RES_HANDLE_P(offset);
3120 		goto num_idx;
3121 	} else if (/*OP2_TYPE == IS_CV &&*/ Z_TYPE_P(offset) == IS_UNDEF) {
3122 		ZVAL_UNDEFINED_OP2();
3123 		goto str_idx;
3124 	} else {
3125 		zend_illegal_array_offset_isset(offset);
3126 		return NULL;
3127 	}
3128 }
3129 
zend_isset_dim_slow(zval * container,zval * offset EXECUTE_DATA_DC)3130 static zend_never_inline bool ZEND_FASTCALL zend_isset_dim_slow(zval *container, zval *offset EXECUTE_DATA_DC)
3131 {
3132 	if (/*OP2_TYPE == IS_CV &&*/ UNEXPECTED(Z_TYPE_P(offset) == IS_UNDEF)) {
3133 		offset = ZVAL_UNDEFINED_OP2();
3134 	}
3135 
3136 	if (/*OP1_TYPE != IS_CONST &&*/ EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
3137 		return Z_OBJ_HT_P(container)->has_dimension(Z_OBJ_P(container), offset, 0);
3138 	} else if (EXPECTED(Z_TYPE_P(container) == IS_STRING)) { /* string offsets */
3139 		zend_long lval;
3140 
3141 		if (EXPECTED(Z_TYPE_P(offset) == IS_LONG)) {
3142 			lval = Z_LVAL_P(offset);
3143 str_offset:
3144 			if (UNEXPECTED(lval < 0)) { /* Handle negative offset */
3145 				lval += (zend_long)Z_STRLEN_P(container);
3146 			}
3147 			if (EXPECTED(lval >= 0) && (size_t)lval < Z_STRLEN_P(container)) {
3148 				return 1;
3149 			} else {
3150 				return 0;
3151 			}
3152 		} else {
3153 			/*if (OP2_TYPE & (IS_CV|IS_VAR)) {*/
3154 				ZVAL_DEREF(offset);
3155 			/*}*/
3156 			if (Z_TYPE_P(offset) < IS_STRING /* simple scalar types */
3157 					|| (Z_TYPE_P(offset) == IS_STRING /* or numeric string */
3158 						&& IS_LONG == is_numeric_string(Z_STRVAL_P(offset), Z_STRLEN_P(offset), NULL, NULL, 0))) {
3159 				lval = zval_get_long_ex(offset, /* is_strict */ true);
3160 				goto str_offset;
3161 			}
3162 			return 0;
3163 		}
3164 	} else {
3165 		return 0;
3166 	}
3167 }
3168 
zend_isempty_dim_slow(zval * container,zval * offset EXECUTE_DATA_DC)3169 static zend_never_inline bool ZEND_FASTCALL zend_isempty_dim_slow(zval *container, zval *offset EXECUTE_DATA_DC)
3170 {
3171 	if (/*OP2_TYPE == IS_CV &&*/ UNEXPECTED(Z_TYPE_P(offset) == IS_UNDEF)) {
3172 		offset = ZVAL_UNDEFINED_OP2();
3173 	}
3174 
3175 	if (/*OP1_TYPE != IS_CONST &&*/ EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
3176 		return !Z_OBJ_HT_P(container)->has_dimension(Z_OBJ_P(container), offset, 1);
3177 	} else if (EXPECTED(Z_TYPE_P(container) == IS_STRING)) { /* string offsets */
3178 		zend_long lval;
3179 
3180 		if (EXPECTED(Z_TYPE_P(offset) == IS_LONG)) {
3181 			lval = Z_LVAL_P(offset);
3182 str_offset:
3183 			if (UNEXPECTED(lval < 0)) { /* Handle negative offset */
3184 				lval += (zend_long)Z_STRLEN_P(container);
3185 			}
3186 			if (EXPECTED(lval >= 0) && (size_t)lval < Z_STRLEN_P(container)) {
3187 				return (Z_STRVAL_P(container)[lval] == '0');
3188 			} else {
3189 				return 1;
3190 			}
3191 		} else {
3192 			/*if (OP2_TYPE & (IS_CV|IS_VAR)) {*/
3193 				ZVAL_DEREF(offset);
3194 			/*}*/
3195 			if (Z_TYPE_P(offset) < IS_STRING /* simple scalar types */
3196 					|| (Z_TYPE_P(offset) == IS_STRING /* or numeric string */
3197 						&& IS_LONG == is_numeric_string(Z_STRVAL_P(offset), Z_STRLEN_P(offset), NULL, NULL, 0))) {
3198 				lval = zval_get_long_ex(offset, /* is_strict */ true);
3199 				goto str_offset;
3200 			}
3201 			return 1;
3202 		}
3203 	} else {
3204 		return 1;
3205 	}
3206 }
3207 
zend_array_key_exists_fast(HashTable * ht,zval * key OPLINE_DC EXECUTE_DATA_DC)3208 static zend_never_inline bool ZEND_FASTCALL zend_array_key_exists_fast(HashTable *ht, zval *key OPLINE_DC EXECUTE_DATA_DC)
3209 {
3210 	zend_string *str;
3211 	zend_ulong hval;
3212 
3213 try_again:
3214 	if (EXPECTED(Z_TYPE_P(key) == IS_STRING)) {
3215 		str = Z_STR_P(key);
3216 		if (ZEND_HANDLE_NUMERIC(str, hval)) {
3217 			goto num_key;
3218 		}
3219 str_key:
3220 		return zend_hash_exists(ht, str);
3221 	} else if (EXPECTED(Z_TYPE_P(key) == IS_LONG)) {
3222 		hval = Z_LVAL_P(key);
3223 num_key:
3224 		return zend_hash_index_exists(ht, hval);
3225 	} else if (EXPECTED(Z_ISREF_P(key))) {
3226 		key = Z_REFVAL_P(key);
3227 		goto try_again;
3228 	} else if (Z_TYPE_P(key) == IS_DOUBLE) {
3229 		hval = zend_dval_to_lval_safe(Z_DVAL_P(key));
3230 		goto num_key;
3231 	} else if (Z_TYPE_P(key) == IS_FALSE) {
3232 		hval = 0;
3233 		goto num_key;
3234 	} else if (Z_TYPE_P(key) == IS_TRUE) {
3235 		hval = 1;
3236 		goto num_key;
3237 	} else if (Z_TYPE_P(key) == IS_RESOURCE) {
3238 		zend_use_resource_as_offset(key);
3239 		hval = Z_RES_HANDLE_P(key);
3240 		goto num_key;
3241 	} else if (Z_TYPE_P(key) <= IS_NULL) {
3242 		if (UNEXPECTED(Z_TYPE_P(key) == IS_UNDEF)) {
3243 			ZVAL_UNDEFINED_OP1();
3244 		}
3245 		str = ZSTR_EMPTY_ALLOC();
3246 		goto str_key;
3247 	} else {
3248 		zend_illegal_array_offset_access(key);
3249 		return 0;
3250 	}
3251 }
3252 
zend_array_key_exists_error(zval * subject,zval * key OPLINE_DC EXECUTE_DATA_DC)3253 static ZEND_COLD void ZEND_FASTCALL zend_array_key_exists_error(
3254 		zval *subject, zval *key OPLINE_DC EXECUTE_DATA_DC)
3255 {
3256 	if (Z_TYPE_P(key) == IS_UNDEF) {
3257 		ZVAL_UNDEFINED_OP1();
3258 	}
3259 	if (Z_TYPE_P(subject) == IS_UNDEF) {
3260 		ZVAL_UNDEFINED_OP2();
3261 	}
3262 	if (!EG(exception)) {
3263 		zend_type_error("array_key_exists(): Argument #2 ($array) must be of type array, %s given",
3264 			zend_zval_value_name(subject));
3265 	}
3266 }
3267 
promotes_to_array(zval * val)3268 static zend_always_inline bool promotes_to_array(zval *val) {
3269 	return Z_TYPE_P(val) <= IS_FALSE
3270 		|| (Z_ISREF_P(val) && Z_TYPE_P(Z_REFVAL_P(val)) <= IS_FALSE);
3271 }
3272 
check_type_array_assignable(zend_type type)3273 static zend_always_inline bool check_type_array_assignable(zend_type type) {
3274 	if (!ZEND_TYPE_IS_SET(type)) {
3275 		return 1;
3276 	}
3277 	return (ZEND_TYPE_FULL_MASK(type) & MAY_BE_ARRAY) != 0;
3278 }
3279 
3280 /* Checks whether an array can be assigned to the reference. Throws error if not assignable. */
zend_verify_ref_array_assignable(zend_reference * ref)3281 ZEND_API bool zend_verify_ref_array_assignable(zend_reference *ref) {
3282 	zend_property_info *prop;
3283 	ZEND_ASSERT(ZEND_REF_HAS_TYPE_SOURCES(ref));
3284 	ZEND_REF_FOREACH_TYPE_SOURCES(ref, prop) {
3285 		if (!check_type_array_assignable(prop->type)) {
3286 			zend_throw_auto_init_in_ref_error(prop);
3287 			return 0;
3288 		}
3289 	} ZEND_REF_FOREACH_TYPE_SOURCES_END();
3290 	return 1;
3291 }
3292 
zend_handle_fetch_obj_flags(zval * result,zval * ptr,zend_object * obj,zend_property_info * prop_info,uint32_t flags)3293 static zend_never_inline bool zend_handle_fetch_obj_flags(
3294 		zval *result, zval *ptr, zend_object *obj, zend_property_info *prop_info, uint32_t flags)
3295 {
3296 	switch (flags) {
3297 		case ZEND_FETCH_DIM_WRITE:
3298 			if (promotes_to_array(ptr)) {
3299 				if (!prop_info) {
3300 					break;
3301 				}
3302 				if (!check_type_array_assignable(prop_info->type)) {
3303 					zend_throw_auto_init_in_prop_error(prop_info);
3304 					if (result) ZVAL_ERROR(result);
3305 					return 0;
3306 				}
3307 			}
3308 			break;
3309 		case ZEND_FETCH_REF:
3310 			if (Z_TYPE_P(ptr) != IS_REFERENCE) {
3311 				if (!prop_info) {
3312 					break;
3313 				}
3314 				if (Z_TYPE_P(ptr) == IS_UNDEF) {
3315 					if (!ZEND_TYPE_ALLOW_NULL(prop_info->type)) {
3316 						zend_throw_access_uninit_prop_by_ref_error(prop_info);
3317 						if (result) ZVAL_ERROR(result);
3318 						return 0;
3319 					}
3320 					ZVAL_NULL(ptr);
3321 				}
3322 
3323 				ZVAL_NEW_REF(ptr, ptr);
3324 				ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(ptr), prop_info);
3325 			}
3326 			break;
3327 		EMPTY_SWITCH_DEFAULT_CASE()
3328 	}
3329 	return 1;
3330 }
3331 
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_property_info ** prop_info_p OPLINE_DC EXECUTE_DATA_DC)3332 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_property_info **prop_info_p OPLINE_DC EXECUTE_DATA_DC)
3333 {
3334 	zval *ptr;
3335 	zend_object *zobj;
3336 	zend_string *name, *tmp_name;
3337 	void *_cache_slot[3] = {0};
3338 	if (prop_op_type != IS_CONST) {
3339 		cache_slot = _cache_slot;
3340 	} else {
3341 		ZEND_ASSERT(cache_slot);
3342 	}
3343 
3344 	if (container_op_type != IS_UNUSED && UNEXPECTED(Z_TYPE_P(container) != IS_OBJECT)) {
3345 		do {
3346 			if (Z_ISREF_P(container) && Z_TYPE_P(Z_REFVAL_P(container)) == IS_OBJECT) {
3347 				container = Z_REFVAL_P(container);
3348 				break;
3349 			}
3350 
3351 			if (container_op_type == IS_CV
3352 			 && type != BP_VAR_W
3353 			 && UNEXPECTED(Z_TYPE_P(container) == IS_UNDEF)) {
3354 				ZVAL_UNDEFINED_OP1();
3355 			}
3356 
3357 			/* this should modify object only if it's empty */
3358 			if (type == BP_VAR_UNSET) {
3359 				ZVAL_NULL(result);
3360 				return;
3361 			}
3362 
3363 			zend_throw_non_object_error(container, prop_ptr OPLINE_CC EXECUTE_DATA_CC);
3364 			ZVAL_ERROR(result);
3365 			return;
3366 		} while (0);
3367 	}
3368 
3369 	zobj = Z_OBJ_P(container);
3370 	if (prop_op_type == IS_CONST &&
3371 	    EXPECTED(zobj->ce == CACHED_PTR_EX(cache_slot))) {
3372 		uintptr_t prop_offset = (uintptr_t)CACHED_PTR_EX(cache_slot + 1);
3373 		if (prop_info_p) {
3374 			*prop_info_p = CACHED_PTR_EX(cache_slot + 2);
3375 		}
3376 
3377 		if (EXPECTED(IS_VALID_PROPERTY_OFFSET(prop_offset))) {
3378 			ptr = OBJ_PROP(zobj, prop_offset);
3379 			if (EXPECTED(Z_TYPE_P(ptr) != IS_UNDEF)) {
3380 				ZVAL_INDIRECT(result, ptr);
3381 				zend_property_info *prop_info = CACHED_PTR_EX(cache_slot + 2);
3382 				if (prop_info) {
3383 					if (UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))
3384 					 && ((prop_info->flags & ZEND_ACC_READONLY) || !zend_asymmetric_property_has_set_access(prop_info))) {
3385 						/* For objects, W/RW/UNSET fetch modes might not actually modify object.
3386 						 * Similar as with magic __get() allow them, but return the value as a copy
3387 						 * to make sure no actual modification is possible. */
3388 						ZEND_ASSERT(type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET);
3389 						if (Z_TYPE_P(ptr) == IS_OBJECT) {
3390 							ZVAL_COPY(result, ptr);
3391 						} else {
3392 							if (prop_info->flags & ZEND_ACC_READONLY) {
3393 								zend_readonly_property_indirect_modification_error(prop_info);
3394 							} else {
3395 								zend_asymmetric_visibility_property_modification_error(prop_info, "indirectly modify");
3396 							}
3397 							ZVAL_ERROR(result);
3398 						}
3399 						return;
3400 					}
3401 					flags &= ZEND_FETCH_OBJ_FLAGS;
3402 					if (flags) {
3403 						zend_handle_fetch_obj_flags(result, ptr, NULL, prop_info, flags);
3404 					}
3405 				}
3406 				return;
3407 			}
3408 		} else if (UNEXPECTED(IS_HOOKED_PROPERTY_OFFSET(prop_offset))) {
3409 			/* Fall through to read_property for hooks. */
3410 		} else if (EXPECTED(zobj->properties != NULL)) {
3411 			ZEND_ASSERT(IS_DYNAMIC_PROPERTY_OFFSET(prop_offset));
3412 			if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
3413 				if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
3414 					GC_DELREF(zobj->properties);
3415 				}
3416 				zobj->properties = zend_array_dup(zobj->properties);
3417 			}
3418 			ptr = zend_hash_find_known_hash(zobj->properties, Z_STR_P(prop_ptr));
3419 			if (EXPECTED(ptr)) {
3420 				ZVAL_INDIRECT(result, ptr);
3421 				return;
3422 			}
3423 		}
3424 	}
3425 
3426 	/* Pointer on property callback is required */
3427 	ZEND_ASSERT(zobj->handlers->get_property_ptr_ptr != NULL);
3428 
3429 	if (prop_op_type == IS_CONST) {
3430 		name = Z_STR_P(prop_ptr);
3431 	} else {
3432 		name = zval_get_tmp_string(prop_ptr, &tmp_name);
3433 	}
3434 	ptr = zobj->handlers->get_property_ptr_ptr(zobj, name, type, cache_slot);
3435 	if (NULL == ptr) {
3436 		ptr = zobj->handlers->read_property(zobj, name, type, cache_slot, result);
3437 		if (ptr == result) {
3438 			if (UNEXPECTED(Z_ISREF_P(ptr) && Z_REFCOUNT_P(ptr) == 1)) {
3439 				ZVAL_UNREF(ptr);
3440 			}
3441 			goto end;
3442 		}
3443 		if (UNEXPECTED(EG(exception))) {
3444 			ZVAL_ERROR(result);
3445 			goto end;
3446 		}
3447 	} else if (UNEXPECTED(Z_ISERROR_P(ptr))) {
3448 		ZVAL_ERROR(result);
3449 		goto end;
3450 	}
3451 
3452 	ZVAL_INDIRECT(result, ptr);
3453 	flags &= ZEND_FETCH_OBJ_FLAGS;
3454 	if (flags) {
3455 		zend_property_info *prop_info = CACHED_PTR_EX(cache_slot + 2);
3456 		if (prop_info && ZEND_TYPE_IS_SET(prop_info->type)) {
3457 			if (UNEXPECTED(!zend_handle_fetch_obj_flags(result, ptr, NULL, prop_info, flags))) {
3458 				goto end;
3459 			}
3460 		}
3461 	}
3462 
3463 end:
3464 	if (prop_info_p) {
3465 		*prop_info_p = CACHED_PTR_EX(cache_slot + 2);
3466 	}
3467 	if (prop_op_type != IS_CONST) {
3468 		zend_tmp_string_release(tmp_name);
3469 	}
3470 }
3471 
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)3472 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)
3473 {
3474 	zval variable, *variable_ptr = &variable;
3475 	void **cache_addr = (prop_op_type == IS_CONST) ? CACHE_ADDR(opline->extended_value & ~ZEND_RETURNS_FUNCTION) : NULL;
3476 	zend_refcounted *garbage = NULL;
3477 	zend_property_info *prop_info = NULL;
3478 
3479 	zend_fetch_property_address(variable_ptr, container, container_op_type, prop_ptr, prop_op_type,
3480 		cache_addr, BP_VAR_W, 0, &prop_info OPLINE_CC EXECUTE_DATA_CC);
3481 
3482 	if (EXPECTED(Z_TYPE_P(variable_ptr) == IS_INDIRECT)) {
3483 		variable_ptr = Z_INDIRECT_P(variable_ptr);
3484 		if (/*OP_DATA_TYPE == IS_VAR &&*/
3485 				   (opline->extended_value & ZEND_RETURNS_FUNCTION) &&
3486 				   UNEXPECTED(!Z_ISREF_P(value_ptr))) {
3487 
3488 			variable_ptr = zend_wrong_assign_to_variable_reference(
3489 				variable_ptr, value_ptr, &garbage OPLINE_CC EXECUTE_DATA_CC);
3490 		} else if (prop_info) {
3491 			variable_ptr = zend_assign_to_typed_property_reference(prop_info, variable_ptr, value_ptr, &garbage EXECUTE_DATA_CC);
3492 		} else {
3493 			zend_assign_to_variable_reference(variable_ptr, value_ptr, &garbage);
3494 		}
3495 	} else if (Z_ISERROR_P(variable_ptr)) {
3496 		variable_ptr = &EG(uninitialized_zval);
3497 	} else {
3498 		zend_throw_error(NULL, "Cannot assign by reference to overloaded object");
3499 		zval_ptr_dtor(&variable);
3500 		variable_ptr = &EG(uninitialized_zval);
3501 	}
3502 
3503 	if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
3504 		ZVAL_COPY(EX_VAR(opline->result.var), variable_ptr);
3505 	}
3506 	if (garbage) {
3507 		GC_DTOR(garbage);
3508 	}
3509 }
3510 
zend_assign_to_property_reference_this_const(zval * container,zval * prop_ptr,zval * value_ptr OPLINE_DC EXECUTE_DATA_DC)3511 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)
3512 {
3513 	zend_assign_to_property_reference(container, IS_UNUSED, prop_ptr, IS_CONST, value_ptr
3514 		OPLINE_CC EXECUTE_DATA_CC);
3515 }
3516 
zend_assign_to_property_reference_var_const(zval * container,zval * prop_ptr,zval * value_ptr OPLINE_DC EXECUTE_DATA_DC)3517 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)
3518 {
3519 	zend_assign_to_property_reference(container, IS_VAR, prop_ptr, IS_CONST, value_ptr
3520 		OPLINE_CC EXECUTE_DATA_CC);
3521 }
3522 
zend_assign_to_property_reference_this_var(zval * container,zval * prop_ptr,zval * value_ptr OPLINE_DC EXECUTE_DATA_DC)3523 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)
3524 {
3525 	zend_assign_to_property_reference(container, IS_UNUSED, prop_ptr, IS_VAR, value_ptr
3526 		OPLINE_CC EXECUTE_DATA_CC);
3527 }
3528 
zend_assign_to_property_reference_var_var(zval * container,zval * prop_ptr,zval * value_ptr OPLINE_DC EXECUTE_DATA_DC)3529 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)
3530 {
3531 	zend_assign_to_property_reference(container, IS_VAR, prop_ptr, IS_VAR, value_ptr
3532 		OPLINE_CC EXECUTE_DATA_CC);
3533 }
3534 
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)3535 static zend_never_inline zend_result 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) {
3536 	zend_string *name;
3537 	zend_class_entry *ce;
3538 	zend_property_info *property_info;
3539 
3540 	uint8_t op1_type = opline->op1_type, op2_type = opline->op2_type;
3541 
3542 	if (EXPECTED(op2_type == IS_CONST)) {
3543 		zval *class_name = RT_CONSTANT(opline, opline->op2);
3544 
3545 		ZEND_ASSERT(op1_type != IS_CONST || CACHED_PTR(cache_slot) == NULL);
3546 
3547 		if (EXPECTED((ce = CACHED_PTR(cache_slot)) == NULL)) {
3548 			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);
3549 			if (UNEXPECTED(ce == NULL)) {
3550 				FREE_OP(op1_type, opline->op1.var);
3551 				return FAILURE;
3552 			}
3553 			if (UNEXPECTED(op1_type != IS_CONST)) {
3554 				CACHE_PTR(cache_slot, ce);
3555 			}
3556 		}
3557 	} else {
3558 		if (EXPECTED(op2_type == IS_UNUSED)) {
3559 			ce = zend_fetch_class(NULL, opline->op2.num);
3560 			if (UNEXPECTED(ce == NULL)) {
3561 				FREE_OP(op1_type, opline->op1.var);
3562 				return FAILURE;
3563 			}
3564 		} else {
3565 			ce = Z_CE_P(EX_VAR(opline->op2.var));
3566 		}
3567 		if (EXPECTED(op1_type == IS_CONST) && EXPECTED(CACHED_PTR(cache_slot) == ce)) {
3568 			*retval = CACHED_PTR(cache_slot + sizeof(void *));
3569 			*prop_info = CACHED_PTR(cache_slot + sizeof(void *) * 2);
3570 			return SUCCESS;
3571 		}
3572 	}
3573 
3574 	if (EXPECTED(op1_type == IS_CONST)) {
3575 		name = Z_STR_P(RT_CONSTANT(opline, opline->op1));
3576 		*retval = zend_std_get_static_property_with_info(ce, name, fetch_type, &property_info);
3577 	} else {
3578 		zend_string *tmp_name;
3579 		zval *varname = get_zval_ptr_undef(opline->op1_type, opline->op1, BP_VAR_R);
3580 		if (EXPECTED(Z_TYPE_P(varname) == IS_STRING)) {
3581 			name = Z_STR_P(varname);
3582 			tmp_name = NULL;
3583 		} else {
3584 			if (op1_type == IS_CV && UNEXPECTED(Z_TYPE_P(varname) == IS_UNDEF)) {
3585 				zval_undefined_cv(opline->op1.var EXECUTE_DATA_CC);
3586 			}
3587 			name = zval_get_tmp_string(varname, &tmp_name);
3588 		}
3589 		*retval = zend_std_get_static_property_with_info(ce, name, fetch_type, &property_info);
3590 
3591 		zend_tmp_string_release(tmp_name);
3592 
3593 		FREE_OP(op1_type, opline->op1.var);
3594 	}
3595 
3596 	if (UNEXPECTED(*retval == NULL)) {
3597 		return FAILURE;
3598 	}
3599 
3600 	*prop_info = property_info;
3601 
3602 	if (EXPECTED(op1_type == IS_CONST)
3603 			&& EXPECTED(!(property_info->ce->ce_flags & ZEND_ACC_TRAIT))) {
3604 		CACHE_POLYMORPHIC_PTR(cache_slot, ce, *retval);
3605 		CACHE_PTR(cache_slot + sizeof(void *) * 2, property_info);
3606 	}
3607 
3608 	return SUCCESS;
3609 }
3610 
3611 
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)3612 static zend_always_inline zend_result 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) {
3613 	zend_property_info *property_info;
3614 
3615 	if (opline->op1_type == IS_CONST
3616 	 && (opline->op2_type == IS_CONST
3617 	  || (opline->op2_type == IS_UNUSED
3618 	   && ((opline->op2.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
3619 	    || (opline->op2.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_PARENT)))
3620 	 && EXPECTED(CACHED_PTR(cache_slot) != NULL)) {
3621 		*retval = CACHED_PTR(cache_slot + sizeof(void *));
3622 		property_info = CACHED_PTR(cache_slot + sizeof(void *) * 2);
3623 
3624 		if ((fetch_type == BP_VAR_R || fetch_type == BP_VAR_RW)
3625 				&& UNEXPECTED(Z_TYPE_P(*retval) == IS_UNDEF)
3626 				&& ZEND_TYPE_IS_SET(property_info->type)) {
3627 			zend_throw_error(NULL, "Typed static property %s::$%s must not be accessed before initialization",
3628 				ZSTR_VAL(property_info->ce->name),
3629 				zend_get_unmangled_property_name(property_info->name));
3630 			return FAILURE;
3631 		}
3632 	} else {
3633 		zend_result success;
3634 		success = zend_fetch_static_property_address_ex(retval, &property_info, cache_slot, fetch_type OPLINE_CC EXECUTE_DATA_CC);
3635 		if (UNEXPECTED(success != SUCCESS)) {
3636 			return FAILURE;
3637 		}
3638 	}
3639 
3640 	flags &= ZEND_FETCH_OBJ_FLAGS;
3641 	if (flags && ZEND_TYPE_IS_SET(property_info->type)) {
3642 		zend_handle_fetch_obj_flags(NULL, *retval, NULL, property_info, flags);
3643 	}
3644 
3645 	if (prop_info) {
3646 		*prop_info = property_info;
3647 	}
3648 
3649 	return SUCCESS;
3650 }
3651 
zend_throw_ref_type_error_type(const zend_property_info * prop1,const zend_property_info * prop2,const zval * zv)3652 ZEND_API ZEND_COLD void zend_throw_ref_type_error_type(const zend_property_info *prop1, const zend_property_info *prop2, const zval *zv) {
3653 	zend_string *type1_str = zend_type_to_string(prop1->type);
3654 	zend_string *type2_str = zend_type_to_string(prop2->type);
3655 	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",
3656 		zend_zval_type_name(zv),
3657 		ZSTR_VAL(prop1->ce->name),
3658 		zend_get_unmangled_property_name(prop1->name),
3659 		ZSTR_VAL(type1_str),
3660 		ZSTR_VAL(prop2->ce->name),
3661 		zend_get_unmangled_property_name(prop2->name),
3662 		ZSTR_VAL(type2_str)
3663 	);
3664 	zend_string_release(type1_str);
3665 	zend_string_release(type2_str);
3666 }
3667 
zend_throw_ref_type_error_zval(const zend_property_info * prop,const zval * zv)3668 ZEND_API ZEND_COLD void zend_throw_ref_type_error_zval(const zend_property_info *prop, const zval *zv) {
3669 	zend_string *type_str = zend_type_to_string(prop->type);
3670 	zend_type_error("Cannot assign %s to reference held by property %s::$%s of type %s",
3671 		zend_zval_value_name(zv),
3672 		ZSTR_VAL(prop->ce->name),
3673 		zend_get_unmangled_property_name(prop->name),
3674 		ZSTR_VAL(type_str)
3675 	);
3676 	zend_string_release(type_str);
3677 }
3678 
zend_throw_conflicting_coercion_error(const zend_property_info * prop1,const zend_property_info * prop2,const zval * zv)3679 ZEND_API ZEND_COLD void zend_throw_conflicting_coercion_error(const zend_property_info *prop1, const zend_property_info *prop2, const zval *zv) {
3680 	zend_string *type1_str = zend_type_to_string(prop1->type);
3681 	zend_string *type2_str = zend_type_to_string(prop2->type);
3682 	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",
3683 		zend_zval_value_name(zv),
3684 		ZSTR_VAL(prop1->ce->name),
3685 		zend_get_unmangled_property_name(prop1->name),
3686 		ZSTR_VAL(type1_str),
3687 		ZSTR_VAL(prop2->ce->name),
3688 		zend_get_unmangled_property_name(prop2->name),
3689 		ZSTR_VAL(type2_str)
3690 	);
3691 	zend_string_release(type1_str);
3692 	zend_string_release(type2_str);
3693 }
3694 
3695 /* 1: valid, 0: invalid, -1: may be valid after type coercion */
i_zend_verify_type_assignable_zval(const zend_property_info * info,const zval * zv,bool strict)3696 static zend_always_inline int i_zend_verify_type_assignable_zval(
3697 		const zend_property_info *info, const zval *zv, bool strict) {
3698 	zend_type type = info->type;
3699 	uint32_t type_mask;
3700 	uint8_t zv_type = Z_TYPE_P(zv);
3701 
3702 	if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(type, zv_type))) {
3703 		return 1;
3704 	}
3705 
3706 	if (ZEND_TYPE_IS_COMPLEX(type) && zv_type == IS_OBJECT
3707 			&& zend_check_and_resolve_property_or_class_constant_class_type(info->ce, info->type, Z_OBJCE_P(zv))) {
3708 		return 1;
3709 	}
3710 
3711 	type_mask = ZEND_TYPE_FULL_MASK(type);
3712 	ZEND_ASSERT(!(type_mask & (MAY_BE_CALLABLE|MAY_BE_STATIC)));
3713 
3714 	/* SSTH Exception: IS_LONG may be accepted as IS_DOUBLE (converted) */
3715 	if (strict) {
3716 		if ((type_mask & MAY_BE_DOUBLE) && zv_type == IS_LONG) {
3717 			return -1;
3718 		}
3719 		return 0;
3720 	}
3721 
3722 	/* NULL may be accepted only by nullable hints (this is already checked) */
3723 	if (zv_type == IS_NULL) {
3724 		return 0;
3725 	}
3726 
3727 	/* Does not contain any type to which a coercion is possible */
3728 	if (!(type_mask & (MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING))
3729 			&& (type_mask & MAY_BE_BOOL) != MAY_BE_BOOL) {
3730 		return 0;
3731 	}
3732 
3733 	/* Coercion may be necessary, check separately */
3734 	return -1;
3735 }
3736 
zend_verify_ref_assignable_zval(zend_reference * ref,zval * zv,bool strict)3737 ZEND_API bool ZEND_FASTCALL zend_verify_ref_assignable_zval(zend_reference *ref, zval *zv, bool strict)
3738 {
3739 	const zend_property_info *prop;
3740 
3741 	/* The value must satisfy each property type, and coerce to the same value for each property
3742 	 * type. Remember the first coerced type and value we've seen for this purpose. */
3743 	const zend_property_info *first_prop = NULL;
3744 	zval coerced_value;
3745 	ZVAL_UNDEF(&coerced_value);
3746 
3747 	ZEND_ASSERT(Z_TYPE_P(zv) != IS_REFERENCE);
3748 	ZEND_REF_FOREACH_TYPE_SOURCES(ref, prop) {
3749 		int result = i_zend_verify_type_assignable_zval(prop, zv, strict);
3750 		if (result == 0) {
3751 type_error:
3752 			zend_throw_ref_type_error_zval(prop, zv);
3753 			zval_ptr_dtor(&coerced_value);
3754 			return 0;
3755 		}
3756 
3757 		if (result < 0) {
3758 			if (!first_prop) {
3759 				first_prop = prop;
3760 				ZVAL_COPY(&coerced_value, zv);
3761 				if (!zend_verify_weak_scalar_type_hint(
3762 						ZEND_TYPE_FULL_MASK(prop->type), &coerced_value)) {
3763 					goto type_error;
3764 				}
3765 			} else if (Z_ISUNDEF(coerced_value)) {
3766 				/* A previous property did not require coercion, but this one does,
3767 				 * so they are incompatible. */
3768 				goto conflicting_coercion_error;
3769 			} else {
3770 				zval tmp;
3771 				ZVAL_COPY(&tmp, zv);
3772 				if (!zend_verify_weak_scalar_type_hint(ZEND_TYPE_FULL_MASK(prop->type), &tmp)) {
3773 					zval_ptr_dtor(&tmp);
3774 					goto type_error;
3775 				}
3776 				if (!zend_is_identical(&coerced_value, &tmp)) {
3777 					zval_ptr_dtor(&tmp);
3778 					goto conflicting_coercion_error;
3779 				}
3780 				zval_ptr_dtor(&tmp);
3781 			}
3782 		} else {
3783 			if (!first_prop) {
3784 				first_prop = prop;
3785 			} else if (!Z_ISUNDEF(coerced_value)) {
3786 				/* A previous property required coercion, but this one doesn't,
3787 				 * so they are incompatible. */
3788 conflicting_coercion_error:
3789 				zend_throw_conflicting_coercion_error(first_prop, prop, zv);
3790 				zval_ptr_dtor(&coerced_value);
3791 				return 0;
3792 			}
3793 		}
3794 	} ZEND_REF_FOREACH_TYPE_SOURCES_END();
3795 
3796 	if (!Z_ISUNDEF(coerced_value)) {
3797 		zval_ptr_dtor(zv);
3798 		ZVAL_COPY_VALUE(zv, &coerced_value);
3799 	}
3800 
3801 	return 1;
3802 }
3803 
i_zval_ptr_dtor_noref(zval * zval_ptr)3804 static zend_always_inline void i_zval_ptr_dtor_noref(zval *zval_ptr) {
3805 	if (Z_REFCOUNTED_P(zval_ptr)) {
3806 		zend_refcounted *ref = Z_COUNTED_P(zval_ptr);
3807 		ZEND_ASSERT(Z_TYPE_P(zval_ptr) != IS_REFERENCE);
3808 		GC_DTOR_NO_REF(ref);
3809 	}
3810 }
3811 
zend_assign_to_typed_ref_ex(zval * variable_ptr,zval * orig_value,uint8_t value_type,bool strict,zend_refcounted ** garbage_ptr)3812 ZEND_API zval* zend_assign_to_typed_ref_ex(zval *variable_ptr, zval *orig_value, uint8_t value_type, bool strict, zend_refcounted **garbage_ptr)
3813 {
3814 	bool ret;
3815 	zval value;
3816 	zend_refcounted *ref = NULL;
3817 
3818 	if (Z_ISREF_P(orig_value)) {
3819 		ref = Z_COUNTED_P(orig_value);
3820 		orig_value = Z_REFVAL_P(orig_value);
3821 	}
3822 
3823 	ZVAL_COPY(&value, orig_value);
3824 	ret = zend_verify_ref_assignable_zval(Z_REF_P(variable_ptr), &value, strict);
3825 	variable_ptr = Z_REFVAL_P(variable_ptr);
3826 	if (EXPECTED(ret)) {
3827 		if (Z_REFCOUNTED_P(variable_ptr)) {
3828 			*garbage_ptr = Z_COUNTED_P(variable_ptr);
3829 		}
3830 		ZVAL_COPY_VALUE(variable_ptr, &value);
3831 	} else {
3832 		zval_ptr_dtor_nogc(&value);
3833 	}
3834 	if (value_type & (IS_VAR|IS_TMP_VAR)) {
3835 		if (UNEXPECTED(ref)) {
3836 			if (UNEXPECTED(GC_DELREF(ref) == 0)) {
3837 				zval_ptr_dtor(orig_value);
3838 				efree_size(ref, sizeof(zend_reference));
3839 			}
3840 		} else {
3841 			i_zval_ptr_dtor_noref(orig_value);
3842 		}
3843 	}
3844 	return variable_ptr;
3845 }
3846 
zend_assign_to_typed_ref(zval * variable_ptr,zval * orig_value,uint8_t value_type,bool strict)3847 ZEND_API zval* zend_assign_to_typed_ref(zval *variable_ptr, zval *orig_value, uint8_t value_type, bool strict)
3848 {
3849 	zend_refcounted *garbage = NULL;
3850 	zval *result = zend_assign_to_typed_ref_ex(variable_ptr, orig_value, value_type, strict, &garbage);
3851 	if (garbage) {
3852 		GC_DTOR_NO_REF(garbage);
3853 	}
3854 	return result;
3855 }
3856 
zend_verify_prop_assignable_by_ref_ex(const zend_property_info * prop_info,zval * orig_val,bool strict,zend_verify_prop_assignable_by_ref_context context)3857 ZEND_API bool ZEND_FASTCALL zend_verify_prop_assignable_by_ref_ex(const zend_property_info *prop_info, zval *orig_val, bool strict, zend_verify_prop_assignable_by_ref_context context) {
3858 	zval *val = orig_val;
3859 	if (Z_ISREF_P(val) && ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(val))) {
3860 		int result;
3861 
3862 		val = Z_REFVAL_P(val);
3863 		result = i_zend_verify_type_assignable_zval(prop_info, val, strict);
3864 		if (result > 0) {
3865 			return 1;
3866 		}
3867 
3868 		if (result < 0) {
3869 			/* This is definitely an error, but we still need to determined why: Either because
3870 			 * the value is simply illegal for the type, or because or a conflicting coercion. */
3871 			zval tmp;
3872 			ZVAL_COPY(&tmp, val);
3873 			if (zend_verify_weak_scalar_type_hint(ZEND_TYPE_FULL_MASK(prop_info->type), &tmp)) {
3874 				const zend_property_info *ref_prop = ZEND_REF_FIRST_SOURCE(Z_REF_P(orig_val));
3875 				zend_throw_ref_type_error_type(ref_prop, prop_info, val);
3876 				zval_ptr_dtor(&tmp);
3877 				return 0;
3878 			}
3879 			zval_ptr_dtor(&tmp);
3880 		}
3881 	} else {
3882 		ZVAL_DEREF(val);
3883 		if (i_zend_check_property_type(prop_info, val, strict)) {
3884 			return 1;
3885 		}
3886 	}
3887 
3888 	if (EXPECTED(context == ZEND_VERIFY_PROP_ASSIGNABLE_BY_REF_CONTEXT_ASSIGNMENT)) {
3889 		zend_verify_property_type_error(prop_info, val);
3890 	} else {
3891 		ZEND_ASSERT(context == ZEND_VERIFY_PROP_ASSIGNABLE_BY_REF_CONTEXT_MAGIC_GET);
3892 		zend_magic_get_property_type_inconsistency_error(prop_info, val);
3893 	}
3894 
3895 	return 0;
3896 }
3897 
zend_verify_prop_assignable_by_ref(const zend_property_info * prop_info,zval * orig_val,bool strict)3898 ZEND_API bool ZEND_FASTCALL zend_verify_prop_assignable_by_ref(const zend_property_info *prop_info, zval *orig_val, bool strict) {
3899 	return zend_verify_prop_assignable_by_ref_ex(prop_info, orig_val, strict, ZEND_VERIFY_PROP_ASSIGNABLE_BY_REF_CONTEXT_ASSIGNMENT);
3900 }
3901 
zend_ref_add_type_source(zend_property_info_source_list * source_list,zend_property_info * prop)3902 ZEND_API void ZEND_FASTCALL zend_ref_add_type_source(zend_property_info_source_list *source_list, zend_property_info *prop)
3903 {
3904 	zend_property_info_list *list;
3905 	if (source_list->ptr == NULL) {
3906 		source_list->ptr = prop;
3907 		return;
3908 	}
3909 
3910 	list = ZEND_PROPERTY_INFO_SOURCE_TO_LIST(source_list->list);
3911 	if (!ZEND_PROPERTY_INFO_SOURCE_IS_LIST(source_list->list)) {
3912 		list = emalloc(sizeof(zend_property_info_list) + (4 - 1) * sizeof(zend_property_info *));
3913 		list->ptr[0] = source_list->ptr;
3914 		list->num_allocated = 4;
3915 		list->num = 1;
3916 	} else if (list->num_allocated == list->num) {
3917 		list->num_allocated = list->num * 2;
3918 		list = erealloc(list, sizeof(zend_property_info_list) + (list->num_allocated - 1) * sizeof(zend_property_info *));
3919 	}
3920 
3921 	list->ptr[list->num++] = prop;
3922 	source_list->list = ZEND_PROPERTY_INFO_SOURCE_FROM_LIST(list);
3923 }
3924 
zend_ref_del_type_source(zend_property_info_source_list * source_list,const zend_property_info * prop)3925 ZEND_API void ZEND_FASTCALL zend_ref_del_type_source(zend_property_info_source_list *source_list, const zend_property_info *prop)
3926 {
3927 	zend_property_info_list *list = ZEND_PROPERTY_INFO_SOURCE_TO_LIST(source_list->list);
3928 	zend_property_info **ptr, **end;
3929 
3930 	ZEND_ASSERT(prop);
3931 	if (!ZEND_PROPERTY_INFO_SOURCE_IS_LIST(source_list->list)) {
3932 		ZEND_ASSERT(source_list->ptr == prop);
3933 		source_list->ptr = NULL;
3934 		return;
3935 	}
3936 
3937 	if (list->num == 1) {
3938 		ZEND_ASSERT(*list->ptr == prop);
3939 		efree(list);
3940 		source_list->ptr = NULL;
3941 		return;
3942 	}
3943 
3944 	/* Checking against end here to get a more graceful failure mode if we missed adding a type
3945 	 * source at some point. */
3946 	ptr = list->ptr;
3947 	end = ptr + list->num;
3948 	while (ptr < end && *ptr != prop) {
3949 		ptr++;
3950 	}
3951 	ZEND_ASSERT(*ptr == prop);
3952 
3953 	/* Copy the last list element into the deleted slot. */
3954 	*ptr = list->ptr[--list->num];
3955 
3956 	if (list->num >= 4 && list->num * 4 == list->num_allocated) {
3957 		list->num_allocated = list->num * 2;
3958 		source_list->list = ZEND_PROPERTY_INFO_SOURCE_FROM_LIST(erealloc(list, sizeof(zend_property_info_list) + (list->num_allocated - 1) * sizeof(zend_property_info *)));
3959 	}
3960 }
3961 
zend_fetch_this_var(int type OPLINE_DC EXECUTE_DATA_DC)3962 static zend_never_inline void zend_fetch_this_var(int type OPLINE_DC EXECUTE_DATA_DC)
3963 {
3964 	zval *result = EX_VAR(opline->result.var);
3965 
3966 	switch (type) {
3967 		case BP_VAR_R:
3968 			if (EXPECTED(Z_TYPE(EX(This)) == IS_OBJECT)) {
3969 				ZVAL_OBJ(result, Z_OBJ(EX(This)));
3970 				Z_ADDREF_P(result);
3971 			} else {
3972 				ZVAL_NULL(result);
3973 				zend_error_unchecked(E_WARNING, "Undefined variable $this");
3974 			}
3975 			break;
3976 		case BP_VAR_IS:
3977 			if (EXPECTED(Z_TYPE(EX(This)) == IS_OBJECT)) {
3978 				ZVAL_OBJ(result, Z_OBJ(EX(This)));
3979 				Z_ADDREF_P(result);
3980 			} else {
3981 				ZVAL_NULL(result);
3982 			}
3983 			break;
3984 		case BP_VAR_RW:
3985 		case BP_VAR_W:
3986 			ZVAL_UNDEF(result);
3987 			zend_throw_error(NULL, "Cannot re-assign $this");
3988 			break;
3989 		case BP_VAR_UNSET:
3990 			ZVAL_UNDEF(result);
3991 			zend_throw_error(NULL, "Cannot unset $this");
3992 			break;
3993 		EMPTY_SWITCH_DEFAULT_CASE()
3994 	}
3995 }
3996 
zend_wrong_clone_call(zend_function * clone,zend_class_entry * scope)3997 static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_wrong_clone_call(zend_function *clone, zend_class_entry *scope)
3998 {
3999 	zend_throw_error(NULL, "Call to %s %s::__clone() from %s%s",
4000 		zend_visibility_string(clone->common.fn_flags), ZSTR_VAL(clone->common.scope->name),
4001 		scope ? "scope " : "global scope",
4002 		scope ? ZSTR_VAL(scope->name) : ""
4003 	);
4004 }
4005 
4006 #if ZEND_INTENSIVE_DEBUGGING
4007 
4008 #define CHECK_SYMBOL_TABLES()													\
4009 	zend_hash_apply(&EG(symbol_table), zend_check_symbol);			\
4010 	if (&EG(symbol_table)!=EX(symbol_table)) {							\
4011 		zend_hash_apply(EX(symbol_table), zend_check_symbol);	\
4012 	}
4013 
zend_check_symbol(zval * pz)4014 static void zend_check_symbol(zval *pz)
4015 {
4016 	if (Z_TYPE_P(pz) == IS_INDIRECT) {
4017 		pz = Z_INDIRECT_P(pz);
4018 	}
4019 	if (Z_TYPE_P(pz) > 10) {
4020 		fprintf(stderr, "Warning!  %x has invalid type!\n", *pz);
4021 /* See http://support.microsoft.com/kb/190351 */
4022 #ifdef ZEND_WIN32
4023 		fflush(stderr);
4024 #endif
4025 	} else if (Z_TYPE_P(pz) == IS_ARRAY) {
4026 		zend_hash_apply(Z_ARRVAL_P(pz), zend_check_symbol);
4027 	} else if (Z_TYPE_P(pz) == IS_OBJECT) {
4028 		/* OBJ-TBI - doesn't support new object model! */
4029 		zend_hash_apply(Z_OBJPROP_P(pz), zend_check_symbol);
4030 	}
4031 }
4032 
4033 
4034 #else
4035 #define CHECK_SYMBOL_TABLES()
4036 #endif
4037 
execute_internal(zend_execute_data * execute_data,zval * return_value)4038 ZEND_API void execute_internal(zend_execute_data *execute_data, zval *return_value)
4039 {
4040 	execute_data->func->internal_function.handler(execute_data, return_value);
4041 }
4042 
zend_clean_and_cache_symbol_table(zend_array * symbol_table)4043 ZEND_API void zend_clean_and_cache_symbol_table(zend_array *symbol_table) /* {{{ */
4044 {
4045 	/* Clean before putting into the cache, since clean could call dtors,
4046 	 * which could use the cached hash. Also do this before the check for
4047 	 * available cache slots, as those may be used by a dtor as well. */
4048 	zend_symtable_clean(symbol_table);
4049 	if (EG(symtable_cache_ptr) >= EG(symtable_cache_limit)) {
4050 		zend_array_destroy(symbol_table);
4051 	} else {
4052 		*(EG(symtable_cache_ptr)++) = symbol_table;
4053 	}
4054 }
4055 /* }}} */
4056 
i_free_compiled_variables(zend_execute_data * execute_data)4057 static zend_always_inline void i_free_compiled_variables(zend_execute_data *execute_data) /* {{{ */
4058 {
4059 	zval *cv = EX_VAR_NUM(0);
4060 	int count = EX(func)->op_array.last_var;
4061 	while (EXPECTED(count != 0)) {
4062 		i_zval_ptr_dtor(cv);
4063 		cv++;
4064 		count--;
4065 	}
4066 }
4067 /* }}} */
4068 
zend_free_compiled_variables(zend_execute_data * execute_data)4069 ZEND_API void ZEND_FASTCALL zend_free_compiled_variables(zend_execute_data *execute_data) /* {{{ */
4070 {
4071 	i_free_compiled_variables(execute_data);
4072 }
4073 /* }}} */
4074 
zend_fcall_interrupt(zend_execute_data * call)4075 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_fcall_interrupt(zend_execute_data *call)
4076 {
4077 	zend_atomic_bool_store_ex(&EG(vm_interrupt), false);
4078 	if (zend_atomic_bool_load_ex(&EG(timed_out))) {
4079 		zend_timeout();
4080 	} else if (zend_interrupt_function) {
4081 		zend_interrupt_function(call);
4082 	}
4083 }
4084 
4085 #define ZEND_VM_INTERRUPT_CHECK() do { \
4086 		if (UNEXPECTED(zend_atomic_bool_load_ex(&EG(vm_interrupt)))) { \
4087 			ZEND_VM_INTERRUPT(); \
4088 		} \
4089 	} while (0)
4090 
4091 #define ZEND_VM_LOOP_INTERRUPT_CHECK() do { \
4092 		if (UNEXPECTED(zend_atomic_bool_load_ex(&EG(vm_interrupt)))) { \
4093 			ZEND_VM_LOOP_INTERRUPT(); \
4094 		} \
4095 	} while (0)
4096 
4097 #define ZEND_VM_FCALL_INTERRUPT_CHECK(call) do { \
4098 		if (UNEXPECTED(zend_atomic_bool_load_ex(&EG(vm_interrupt)))) { \
4099 			zend_fcall_interrupt(call); \
4100 		} \
4101 	} while (0)
4102 
4103 /*
4104  * Stack Frame Layout (the whole stack frame is allocated at once)
4105  * ==================
4106  *
4107  *                             +========================================+
4108  * EG(current_execute_data) -> | zend_execute_data                      |
4109  *                             +----------------------------------------+
4110  *     EX_VAR_NUM(0) --------> | VAR[0] = ARG[1]                        |
4111  *                             | ...                                    |
4112  *                             | VAR[op_array->num_args-1] = ARG[N]     |
4113  *                             | ...                                    |
4114  *                             | VAR[op_array->last_var-1]              |
4115  *                             | VAR[op_array->last_var] = TMP[0]       |
4116  *                             | ...                                    |
4117  *                             | VAR[op_array->last_var+op_array->T-1]  |
4118  *                             | ARG[N+1] (extra_args)                  |
4119  *                             | ...                                    |
4120  *                             +----------------------------------------+
4121  */
4122 
4123 /* zend_copy_extra_args is used when the actually passed number of arguments
4124  * (EX_NUM_ARGS) is greater than what the function defined (op_array->num_args).
4125  *
4126  * The extra arguments will be copied into the call frame after all the compiled variables.
4127  *
4128  * If there are extra arguments copied, a flag "ZEND_CALL_FREE_EXTRA_ARGS" will be set
4129  * on the zend_execute_data, and when the executor leaves the function, the
4130  * args will be freed in zend_leave_helper.
4131  */
zend_copy_extra_args(EXECUTE_DATA_D)4132 static zend_never_inline void zend_copy_extra_args(EXECUTE_DATA_D)
4133 {
4134 	zend_op_array *op_array = &EX(func)->op_array;
4135 	uint32_t first_extra_arg = op_array->num_args;
4136 	uint32_t num_args = EX_NUM_ARGS();
4137 	zval *src;
4138 	size_t delta;
4139 	uint32_t count;
4140 	uint32_t type_flags = 0;
4141 
4142 	if (EXPECTED((op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0)) {
4143 		/* Skip useless ZEND_RECV and ZEND_RECV_INIT opcodes */
4144 #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
4145 		opline += first_extra_arg;
4146 #else
4147 		EX(opline) += first_extra_arg;
4148 #endif
4149 
4150 	}
4151 
4152 	/* move extra args into separate array after all CV and TMP vars */
4153 	src = EX_VAR_NUM(num_args - 1);
4154 	delta = op_array->last_var + op_array->T - first_extra_arg;
4155 	count = num_args - first_extra_arg;
4156 	if (EXPECTED(delta != 0)) {
4157 		delta *= sizeof(zval);
4158 		do {
4159 			type_flags |= Z_TYPE_INFO_P(src);
4160 			ZVAL_COPY_VALUE((zval*)(((char*)src) + delta), src);
4161 			ZVAL_UNDEF(src);
4162 			src--;
4163 		} while (--count);
4164 		if (Z_TYPE_INFO_REFCOUNTED(type_flags)) {
4165 			ZEND_ADD_CALL_FLAG(execute_data, ZEND_CALL_FREE_EXTRA_ARGS);
4166 		}
4167 	} else {
4168 		do {
4169 			if (Z_REFCOUNTED_P(src)) {
4170 				ZEND_ADD_CALL_FLAG(execute_data, ZEND_CALL_FREE_EXTRA_ARGS);
4171 				break;
4172 			}
4173 			src--;
4174 		} while (--count);
4175 	}
4176 }
4177 
zend_init_cvs(uint32_t first,uint32_t last EXECUTE_DATA_DC)4178 static zend_always_inline void zend_init_cvs(uint32_t first, uint32_t last EXECUTE_DATA_DC)
4179 {
4180 	if (EXPECTED(first < last)) {
4181 		uint32_t count = last - first;
4182 		zval *var = EX_VAR_NUM(first);
4183 
4184 		do {
4185 			ZVAL_UNDEF(var);
4186 			var++;
4187 		} while (--count);
4188 	}
4189 }
4190 
i_init_func_execute_data(zend_op_array * op_array,zval * return_value,bool may_be_trampoline EXECUTE_DATA_DC)4191 static zend_always_inline void i_init_func_execute_data(zend_op_array *op_array, zval *return_value, bool may_be_trampoline EXECUTE_DATA_DC) /* {{{ */
4192 {
4193 	uint32_t first_extra_arg, num_args;
4194 	ZEND_ASSERT(EX(func) == (zend_function*)op_array);
4195 
4196 #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
4197 	opline = op_array->opcodes;
4198 #else
4199 	EX(opline) = op_array->opcodes;
4200 #endif
4201 	EX(call) = NULL;
4202 	EX(return_value) = return_value;
4203 
4204 	/* Handle arguments */
4205 	first_extra_arg = op_array->num_args;
4206 	num_args = EX_NUM_ARGS();
4207 	if (UNEXPECTED(num_args > first_extra_arg)) {
4208 		if (!may_be_trampoline || EXPECTED(!(op_array->fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))) {
4209 			zend_copy_extra_args(EXECUTE_DATA_C);
4210 		}
4211 	} else if (EXPECTED((op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0)) {
4212 		/* Skip useless ZEND_RECV and ZEND_RECV_INIT opcodes */
4213 #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
4214 		opline += num_args;
4215 #else
4216 		EX(opline) += num_args;
4217 #endif
4218 	}
4219 
4220 	/* Initialize CV variables (skip arguments) */
4221 	zend_init_cvs(num_args, op_array->last_var EXECUTE_DATA_CC);
4222 
4223 	EX(run_time_cache) = RUN_TIME_CACHE(op_array);
4224 
4225 	EG(current_execute_data) = execute_data;
4226 }
4227 /* }}} */
4228 
init_func_run_time_cache_i(zend_op_array * op_array)4229 static zend_always_inline void init_func_run_time_cache_i(zend_op_array *op_array) /* {{{ */
4230 {
4231 	void **run_time_cache;
4232 
4233 	ZEND_ASSERT(RUN_TIME_CACHE(op_array) == NULL);
4234 	run_time_cache = zend_arena_alloc(&CG(arena), op_array->cache_size);
4235 	memset(run_time_cache, 0, op_array->cache_size);
4236 	ZEND_MAP_PTR_SET(op_array->run_time_cache, run_time_cache);
4237 }
4238 /* }}} */
4239 
init_func_run_time_cache(zend_op_array * op_array)4240 static zend_never_inline void ZEND_FASTCALL init_func_run_time_cache(zend_op_array *op_array) /* {{{ */
4241 {
4242 	init_func_run_time_cache_i(op_array);
4243 }
4244 /* }}} */
4245 
zend_fetch_function(zend_string * name)4246 ZEND_API zend_function * ZEND_FASTCALL zend_fetch_function(zend_string *name) /* {{{ */
4247 {
4248 	zval *zv = zend_hash_find(EG(function_table), name);
4249 
4250 	if (EXPECTED(zv != NULL)) {
4251 		zend_function *fbc = Z_FUNC_P(zv);
4252 
4253 		if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
4254 			init_func_run_time_cache_i(&fbc->op_array);
4255 		}
4256 		return fbc;
4257 	}
4258 	return NULL;
4259 } /* }}} */
4260 
zend_fetch_function_str(const char * name,size_t len)4261 ZEND_API zend_function * ZEND_FASTCALL zend_fetch_function_str(const char *name, size_t len) /* {{{ */
4262 {
4263 	zval *zv = zend_hash_str_find(EG(function_table), name, len);
4264 
4265 	if (EXPECTED(zv != NULL)) {
4266 		zend_function *fbc = Z_FUNC_P(zv);
4267 
4268 		if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
4269 			init_func_run_time_cache_i(&fbc->op_array);
4270 		}
4271 		return fbc;
4272 	}
4273 	return NULL;
4274 } /* }}} */
4275 
zend_init_func_run_time_cache(zend_op_array * op_array)4276 ZEND_API void ZEND_FASTCALL zend_init_func_run_time_cache(zend_op_array *op_array) /* {{{ */
4277 {
4278 	if (!RUN_TIME_CACHE(op_array)) {
4279 		init_func_run_time_cache_i(op_array);
4280 	}
4281 } /* }}} */
4282 
i_init_code_execute_data(zend_execute_data * execute_data,zend_op_array * op_array,zval * return_value)4283 static zend_always_inline void i_init_code_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value) /* {{{ */
4284 {
4285 	ZEND_ASSERT(EX(func) == (zend_function*)op_array);
4286 
4287 	EX(opline) = op_array->opcodes;
4288 	EX(call) = NULL;
4289 	EX(return_value) = return_value;
4290 
4291 	if (op_array->last_var) {
4292 		zend_attach_symbol_table(execute_data);
4293 	}
4294 
4295 	if (!ZEND_MAP_PTR(op_array->run_time_cache)) {
4296 		void *ptr;
4297 
4298 		ZEND_ASSERT(op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE);
4299 		ptr = emalloc(op_array->cache_size);
4300 		ZEND_MAP_PTR_INIT(op_array->run_time_cache, ptr);
4301 		memset(ptr, 0, op_array->cache_size);
4302 	}
4303 	EX(run_time_cache) = RUN_TIME_CACHE(op_array);
4304 
4305 	EG(current_execute_data) = execute_data;
4306 }
4307 /* }}} */
4308 
zend_init_func_execute_data(zend_execute_data * ex,zend_op_array * op_array,zval * return_value)4309 ZEND_API void zend_init_func_execute_data(zend_execute_data *ex, zend_op_array *op_array, zval *return_value) /* {{{ */
4310 {
4311 #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
4312 	zend_execute_data *orig_execute_data = execute_data;
4313 #endif
4314 #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
4315 	const zend_op *orig_opline = opline;
4316 #endif
4317 #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
4318 	execute_data = ex;
4319 #else
4320 	zend_execute_data *execute_data = ex;
4321 #endif
4322 
4323 	EX(prev_execute_data) = EG(current_execute_data);
4324 	if (!RUN_TIME_CACHE(op_array)) {
4325 		init_func_run_time_cache(op_array);
4326 	}
4327 	i_init_func_execute_data(op_array, return_value, 1 EXECUTE_DATA_CC);
4328 
4329 #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
4330 	EX(opline) = opline;
4331 	opline = orig_opline;
4332 #endif
4333 #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
4334 	execute_data = orig_execute_data;
4335 #endif
4336 }
4337 /* }}} */
4338 
zend_init_code_execute_data(zend_execute_data * execute_data,zend_op_array * op_array,zval * return_value)4339 ZEND_API void zend_init_code_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value) /* {{{ */
4340 {
4341 	EX(prev_execute_data) = EG(current_execute_data);
4342 	i_init_code_execute_data(execute_data, op_array, return_value);
4343 }
4344 /* }}} */
4345 
zend_init_execute_data(zend_execute_data * execute_data,zend_op_array * op_array,zval * return_value)4346 ZEND_API void zend_init_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value) /* {{{ */
4347 {
4348 	if (EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE) {
4349 		zend_init_code_execute_data(execute_data, op_array, return_value);
4350 	} else {
4351 		zend_init_func_execute_data(execute_data, op_array, return_value);
4352 	}
4353 }
4354 /* }}} */
4355 
zend_vm_stack_copy_call_frame(zend_execute_data * call,uint32_t passed_args,uint32_t additional_args)4356 zend_execute_data *zend_vm_stack_copy_call_frame(zend_execute_data *call, uint32_t passed_args, uint32_t additional_args) /* {{{ */
4357 {
4358 	zend_execute_data *new_call;
4359 	int used_stack = (EG(vm_stack_top) - (zval*)call) + additional_args;
4360 
4361 	/* copy call frame into new stack segment */
4362 	new_call = zend_vm_stack_extend(used_stack * sizeof(zval));
4363 	*new_call = *call;
4364 	ZEND_ADD_CALL_FLAG(new_call, ZEND_CALL_ALLOCATED);
4365 
4366 	if (passed_args) {
4367 		zval *src = ZEND_CALL_ARG(call, 1);
4368 		zval *dst = ZEND_CALL_ARG(new_call, 1);
4369 		do {
4370 			ZVAL_COPY_VALUE(dst, src);
4371 			passed_args--;
4372 			src++;
4373 			dst++;
4374 		} while (passed_args);
4375 	}
4376 
4377 	/* delete old call_frame from previous stack segment */
4378 	EG(vm_stack)->prev->top = (zval*)call;
4379 
4380 	/* delete previous stack segment if it became empty */
4381 	if (UNEXPECTED(EG(vm_stack)->prev->top == ZEND_VM_STACK_ELEMENTS(EG(vm_stack)->prev))) {
4382 		zend_vm_stack r = EG(vm_stack)->prev;
4383 
4384 		EG(vm_stack)->prev = r->prev;
4385 		efree(r);
4386 	}
4387 
4388 	return new_call;
4389 }
4390 /* }}} */
4391 
zend_get_running_generator(EXECUTE_DATA_D)4392 static zend_always_inline zend_generator *zend_get_running_generator(EXECUTE_DATA_D) /* {{{ */
4393 {
4394 	/* The generator object is stored in EX(return_value) */
4395 	zend_generator *generator = (zend_generator *) EX(return_value);
4396 	/* However control may currently be delegated to another generator.
4397 	 * That's the one we're interested in. */
4398 	return generator;
4399 }
4400 /* }}} */
4401 
zend_unfinished_calls_gc(zend_execute_data * execute_data,zend_execute_data * call,uint32_t op_num,zend_get_gc_buffer * buf)4402 ZEND_API void zend_unfinished_calls_gc(zend_execute_data *execute_data, zend_execute_data *call, uint32_t op_num, zend_get_gc_buffer *buf) /* {{{ */
4403 {
4404 	zend_op *opline = EX(func)->op_array.opcodes + op_num;
4405 	int level;
4406 	int do_exit;
4407 	uint32_t num_args;
4408 
4409 	if (UNEXPECTED(opline->opcode == ZEND_INIT_FCALL ||
4410 		opline->opcode == ZEND_INIT_FCALL_BY_NAME ||
4411 		opline->opcode == ZEND_INIT_NS_FCALL_BY_NAME ||
4412 		opline->opcode == ZEND_INIT_DYNAMIC_CALL ||
4413 		opline->opcode == ZEND_INIT_USER_CALL ||
4414 		opline->opcode == ZEND_INIT_METHOD_CALL ||
4415 		opline->opcode == ZEND_INIT_STATIC_METHOD_CALL ||
4416 		opline->opcode == ZEND_NEW)) {
4417 		ZEND_ASSERT(op_num);
4418 		opline--;
4419 	}
4420 
4421 	do {
4422 		/* find the number of actually passed arguments */
4423 		level = 0;
4424 		do_exit = 0;
4425 		num_args = ZEND_CALL_NUM_ARGS(call);
4426 		do {
4427 			switch (opline->opcode) {
4428 				case ZEND_DO_FCALL:
4429 				case ZEND_DO_ICALL:
4430 				case ZEND_DO_UCALL:
4431 				case ZEND_DO_FCALL_BY_NAME:
4432 				case ZEND_CALLABLE_CONVERT:
4433 					level++;
4434 					break;
4435 				case ZEND_INIT_FCALL:
4436 				case ZEND_INIT_FCALL_BY_NAME:
4437 				case ZEND_INIT_NS_FCALL_BY_NAME:
4438 				case ZEND_INIT_DYNAMIC_CALL:
4439 				case ZEND_INIT_USER_CALL:
4440 				case ZEND_INIT_METHOD_CALL:
4441 				case ZEND_INIT_STATIC_METHOD_CALL:
4442 				case ZEND_NEW:
4443 					if (level == 0) {
4444 						num_args = 0;
4445 						do_exit = 1;
4446 					}
4447 					level--;
4448 					break;
4449 				case ZEND_SEND_VAL:
4450 				case ZEND_SEND_VAL_EX:
4451 				case ZEND_SEND_VAR:
4452 				case ZEND_SEND_VAR_EX:
4453 				case ZEND_SEND_FUNC_ARG:
4454 				case ZEND_SEND_REF:
4455 				case ZEND_SEND_VAR_NO_REF:
4456 				case ZEND_SEND_VAR_NO_REF_EX:
4457 				case ZEND_SEND_USER:
4458 					if (level == 0) {
4459 						/* For named args, the number of arguments is up to date. */
4460 						if (opline->op2_type != IS_CONST) {
4461 							num_args = opline->op2.num;
4462 						}
4463 						do_exit = 1;
4464 					}
4465 					break;
4466 				case ZEND_SEND_ARRAY:
4467 				case ZEND_SEND_UNPACK:
4468 				case ZEND_CHECK_UNDEF_ARGS:
4469 					if (level == 0) {
4470 						do_exit = 1;
4471 					}
4472 					break;
4473 			}
4474 			if (!do_exit) {
4475 				opline--;
4476 			}
4477 		} while (!do_exit);
4478 		if (call->prev_execute_data) {
4479 			/* skip current call region */
4480 			level = 0;
4481 			do_exit = 0;
4482 			do {
4483 				switch (opline->opcode) {
4484 					case ZEND_DO_FCALL:
4485 					case ZEND_DO_ICALL:
4486 					case ZEND_DO_UCALL:
4487 					case ZEND_DO_FCALL_BY_NAME:
4488 					case ZEND_CALLABLE_CONVERT:
4489 						level++;
4490 						break;
4491 					case ZEND_INIT_FCALL:
4492 					case ZEND_INIT_FCALL_BY_NAME:
4493 					case ZEND_INIT_NS_FCALL_BY_NAME:
4494 					case ZEND_INIT_DYNAMIC_CALL:
4495 					case ZEND_INIT_USER_CALL:
4496 					case ZEND_INIT_METHOD_CALL:
4497 					case ZEND_INIT_STATIC_METHOD_CALL:
4498 					case ZEND_NEW:
4499 						if (level == 0) {
4500 							do_exit = 1;
4501 						}
4502 						level--;
4503 						break;
4504 				}
4505 				opline--;
4506 			} while (!do_exit);
4507 		}
4508 
4509 		if (EXPECTED(num_args > 0)) {
4510 			zval *p = ZEND_CALL_ARG(call, 1);
4511 			do {
4512 				zend_get_gc_buffer_add_zval(buf, p);
4513 				p++;
4514 			} while (--num_args);
4515 		}
4516 		if (ZEND_CALL_INFO(call) & ZEND_CALL_RELEASE_THIS) {
4517 			zend_get_gc_buffer_add_obj(buf, Z_OBJ(call->This));
4518 		}
4519 		if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
4520 			zval *val;
4521 			ZEND_HASH_FOREACH_VAL(call->extra_named_params, val) {
4522 				zend_get_gc_buffer_add_zval(buf, val);
4523 			} ZEND_HASH_FOREACH_END();
4524 		}
4525 		if (call->func->common.fn_flags & ZEND_ACC_CLOSURE) {
4526 			zend_get_gc_buffer_add_obj(buf, ZEND_CLOSURE_OBJECT(call->func));
4527 		}
4528 
4529 		call = call->prev_execute_data;
4530 	} while (call);
4531 }
4532 /* }}} */
4533 
cleanup_unfinished_calls(zend_execute_data * execute_data,uint32_t op_num)4534 static void cleanup_unfinished_calls(zend_execute_data *execute_data, uint32_t op_num) /* {{{ */
4535 {
4536 	if (UNEXPECTED(EX(call))) {
4537 		zend_execute_data *call = EX(call);
4538 		zend_op *opline = EX(func)->op_array.opcodes + op_num;
4539 		int level;
4540 		int do_exit;
4541 
4542 		if (UNEXPECTED(opline->opcode == ZEND_INIT_FCALL ||
4543 			opline->opcode == ZEND_INIT_FCALL_BY_NAME ||
4544 			opline->opcode == ZEND_INIT_NS_FCALL_BY_NAME ||
4545 			opline->opcode == ZEND_INIT_DYNAMIC_CALL ||
4546 			opline->opcode == ZEND_INIT_USER_CALL ||
4547 			opline->opcode == ZEND_INIT_METHOD_CALL ||
4548 			opline->opcode == ZEND_INIT_STATIC_METHOD_CALL ||
4549 			opline->opcode == ZEND_INIT_PARENT_PROPERTY_HOOK_CALL ||
4550 			opline->opcode == ZEND_NEW)) {
4551 			ZEND_ASSERT(op_num);
4552 			opline--;
4553 		}
4554 
4555 		do {
4556 			/* If the exception was thrown during a function call there might be
4557 			 * arguments pushed to the stack that have to be dtor'ed. */
4558 
4559 			/* find the number of actually passed arguments */
4560 			level = 0;
4561 			do_exit = 0;
4562 			do {
4563 				switch (opline->opcode) {
4564 					case ZEND_DO_FCALL:
4565 					case ZEND_DO_ICALL:
4566 					case ZEND_DO_UCALL:
4567 					case ZEND_DO_FCALL_BY_NAME:
4568 					case ZEND_CALLABLE_CONVERT:
4569 						level++;
4570 						break;
4571 					case ZEND_INIT_FCALL:
4572 					case ZEND_INIT_FCALL_BY_NAME:
4573 					case ZEND_INIT_NS_FCALL_BY_NAME:
4574 					case ZEND_INIT_DYNAMIC_CALL:
4575 					case ZEND_INIT_USER_CALL:
4576 					case ZEND_INIT_METHOD_CALL:
4577 					case ZEND_INIT_STATIC_METHOD_CALL:
4578 					case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL:
4579 					case ZEND_NEW:
4580 						if (level == 0) {
4581 							ZEND_CALL_NUM_ARGS(call) = 0;
4582 							do_exit = 1;
4583 						}
4584 						level--;
4585 						break;
4586 					case ZEND_SEND_VAL:
4587 					case ZEND_SEND_VAL_EX:
4588 					case ZEND_SEND_VAR:
4589 					case ZEND_SEND_VAR_EX:
4590 					case ZEND_SEND_FUNC_ARG:
4591 					case ZEND_SEND_REF:
4592 					case ZEND_SEND_VAR_NO_REF:
4593 					case ZEND_SEND_VAR_NO_REF_EX:
4594 					case ZEND_SEND_USER:
4595 						if (level == 0) {
4596 							/* For named args, the number of arguments is up to date. */
4597 							if (opline->op2_type != IS_CONST) {
4598 								ZEND_CALL_NUM_ARGS(call) = opline->op2.num;
4599 							}
4600 							do_exit = 1;
4601 						}
4602 						break;
4603 					case ZEND_SEND_ARRAY:
4604 					case ZEND_SEND_UNPACK:
4605 					case ZEND_CHECK_UNDEF_ARGS:
4606 						if (level == 0) {
4607 							do_exit = 1;
4608 						}
4609 						break;
4610 				}
4611 				if (!do_exit) {
4612 					opline--;
4613 				}
4614 			} while (!do_exit);
4615 			if (call->prev_execute_data) {
4616 				/* skip current call region */
4617 				level = 0;
4618 				do_exit = 0;
4619 				do {
4620 					switch (opline->opcode) {
4621 						case ZEND_DO_FCALL:
4622 						case ZEND_DO_ICALL:
4623 						case ZEND_DO_UCALL:
4624 						case ZEND_DO_FCALL_BY_NAME:
4625 						case ZEND_CALLABLE_CONVERT:
4626 							level++;
4627 							break;
4628 						case ZEND_INIT_FCALL:
4629 						case ZEND_INIT_FCALL_BY_NAME:
4630 						case ZEND_INIT_NS_FCALL_BY_NAME:
4631 						case ZEND_INIT_DYNAMIC_CALL:
4632 						case ZEND_INIT_USER_CALL:
4633 						case ZEND_INIT_METHOD_CALL:
4634 						case ZEND_INIT_STATIC_METHOD_CALL:
4635 						case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL:
4636 						case ZEND_NEW:
4637 							if (level == 0) {
4638 								do_exit = 1;
4639 							}
4640 							level--;
4641 							break;
4642 					}
4643 					opline--;
4644 				} while (!do_exit);
4645 			}
4646 
4647 			zend_vm_stack_free_args(EX(call));
4648 
4649 			if (ZEND_CALL_INFO(call) & ZEND_CALL_RELEASE_THIS) {
4650 				OBJ_RELEASE(Z_OBJ(call->This));
4651 			}
4652 			if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
4653 				zend_free_extra_named_params(call->extra_named_params);
4654 			}
4655 			if (call->func->common.fn_flags & ZEND_ACC_CLOSURE) {
4656 				zend_object_release(ZEND_CLOSURE_OBJECT(call->func));
4657 			} else if (call->func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
4658 				zend_string_release_ex(call->func->common.function_name, 0);
4659 				zend_free_trampoline(call->func);
4660 			}
4661 
4662 			EX(call) = call->prev_execute_data;
4663 			zend_vm_stack_free_call_frame(call);
4664 			call = EX(call);
4665 		} while (call);
4666 	}
4667 }
4668 /* }}} */
4669 
find_live_range(const zend_op_array * op_array,uint32_t op_num,uint32_t var_num)4670 static const zend_live_range *find_live_range(const zend_op_array *op_array, uint32_t op_num, uint32_t var_num) /* {{{ */
4671 {
4672 	int i;
4673 	for (i = 0; i < op_array->last_live_range; i++) {
4674 		const zend_live_range *range = &op_array->live_range[i];
4675 		if (op_num >= range->start && op_num < range->end
4676 				&& var_num == (range->var & ~ZEND_LIVE_MASK)) {
4677 			return range;
4678 		}
4679 	}
4680 	return NULL;
4681 }
4682 /* }}} */
4683 
cleanup_live_vars(zend_execute_data * execute_data,uint32_t op_num,uint32_t catch_op_num)4684 static void cleanup_live_vars(zend_execute_data *execute_data, uint32_t op_num, uint32_t catch_op_num) /* {{{ */
4685 {
4686 	int i;
4687 
4688 	for (i = 0; i < EX(func)->op_array.last_live_range; i++) {
4689 		const zend_live_range *range = &EX(func)->op_array.live_range[i];
4690 		if (range->start > op_num) {
4691 			/* further blocks will not be relevant... */
4692 			break;
4693 		} else if (op_num < range->end) {
4694 			if (!catch_op_num || catch_op_num >= range->end) {
4695 				uint32_t kind = range->var & ZEND_LIVE_MASK;
4696 				uint32_t var_num = range->var & ~ZEND_LIVE_MASK;
4697 				zval *var = EX_VAR(var_num);
4698 
4699 				if (kind == ZEND_LIVE_TMPVAR) {
4700 					zval_ptr_dtor_nogc(var);
4701 				} else if (kind == ZEND_LIVE_NEW) {
4702 					zend_object *obj;
4703 					ZEND_ASSERT(Z_TYPE_P(var) == IS_OBJECT);
4704 					obj = Z_OBJ_P(var);
4705 					zend_object_store_ctor_failed(obj);
4706 					OBJ_RELEASE(obj);
4707 				} else if (kind == ZEND_LIVE_LOOP) {
4708 					if (Z_TYPE_P(var) != IS_ARRAY && Z_FE_ITER_P(var) != (uint32_t)-1) {
4709 						zend_hash_iterator_del(Z_FE_ITER_P(var));
4710 					}
4711 					zval_ptr_dtor_nogc(var);
4712 				} else if (kind == ZEND_LIVE_ROPE) {
4713 					zend_string **rope = (zend_string **)var;
4714 					zend_op *last = EX(func)->op_array.opcodes + op_num;
4715 					while ((last->opcode != ZEND_ROPE_ADD && last->opcode != ZEND_ROPE_INIT)
4716 							|| last->result.var != var_num) {
4717 						ZEND_ASSERT(last >= EX(func)->op_array.opcodes);
4718 						last--;
4719 					}
4720 					if (last->opcode == ZEND_ROPE_INIT) {
4721 						zend_string_release_ex(*rope, 0);
4722 					} else {
4723 						int j = last->extended_value;
4724 						do {
4725 							zend_string_release_ex(rope[j], 0);
4726 						} while (j--);
4727 					}
4728 				} else if (kind == ZEND_LIVE_SILENCE) {
4729 					/* restore previous error_reporting value */
4730 					if (E_HAS_ONLY_FATAL_ERRORS(EG(error_reporting))
4731 							&& !E_HAS_ONLY_FATAL_ERRORS(Z_LVAL_P(var))) {
4732 						EG(error_reporting) = Z_LVAL_P(var);
4733 					}
4734 				}
4735 			}
4736 		}
4737 	}
4738 }
4739 /* }}} */
4740 
zend_cleanup_unfinished_execution(zend_execute_data * execute_data,uint32_t op_num,uint32_t catch_op_num)4741 ZEND_API void zend_cleanup_unfinished_execution(zend_execute_data *execute_data, uint32_t op_num, uint32_t catch_op_num) {
4742 	cleanup_unfinished_calls(execute_data, op_num);
4743 	cleanup_live_vars(execute_data, op_num, catch_op_num);
4744 }
4745 
zend_unfinished_execution_gc(zend_execute_data * execute_data,zend_execute_data * call,zend_get_gc_buffer * gc_buffer)4746 ZEND_API ZEND_ATTRIBUTE_DEPRECATED HashTable *zend_unfinished_execution_gc(zend_execute_data *execute_data, zend_execute_data *call, zend_get_gc_buffer *gc_buffer)
4747 {
4748 	return zend_unfinished_execution_gc_ex(execute_data, call, gc_buffer, false);
4749 }
4750 
zend_unfinished_execution_gc_ex(zend_execute_data * execute_data,zend_execute_data * call,zend_get_gc_buffer * gc_buffer,bool suspended_by_yield)4751 ZEND_API HashTable *zend_unfinished_execution_gc_ex(zend_execute_data *execute_data, zend_execute_data *call, zend_get_gc_buffer *gc_buffer, bool suspended_by_yield)
4752 {
4753 	if (!EX(func)) {
4754 		return NULL;
4755 	}
4756 
4757 	if (EX_CALL_INFO() & ZEND_CALL_RELEASE_THIS) {
4758 		zend_get_gc_buffer_add_obj(gc_buffer, Z_OBJ(execute_data->This));
4759 	}
4760 
4761 	if (EX_CALL_INFO() & ZEND_CALL_CLOSURE) {
4762 		zend_get_gc_buffer_add_obj(gc_buffer, ZEND_CLOSURE_OBJECT(EX(func)));
4763 	}
4764 
4765 	if (!ZEND_USER_CODE(EX(func)->common.type)) {
4766 		ZEND_ASSERT(!(EX_CALL_INFO() & (ZEND_CALL_HAS_SYMBOL_TABLE|ZEND_CALL_FREE_EXTRA_ARGS|ZEND_CALL_HAS_EXTRA_NAMED_PARAMS)));
4767 		return NULL;
4768 	}
4769 
4770 	zend_op_array *op_array = &EX(func)->op_array;
4771 
4772 	if (!(EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE)) {
4773 		uint32_t i, num_cvs = EX(func)->op_array.last_var;
4774 		for (i = 0; i < num_cvs; i++) {
4775 			zend_get_gc_buffer_add_zval(gc_buffer, EX_VAR_NUM(i));
4776 		}
4777 	}
4778 
4779 	if (EX_CALL_INFO() & ZEND_CALL_FREE_EXTRA_ARGS) {
4780 		zval *zv = EX_VAR_NUM(op_array->last_var + op_array->T);
4781 		zval *end = zv + (EX_NUM_ARGS() - op_array->num_args);
4782 		while (zv != end) {
4783 			zend_get_gc_buffer_add_zval(gc_buffer, zv++);
4784 		}
4785 	}
4786 
4787 	if (EX_CALL_INFO() & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
4788 		zval extra_named_params;
4789 		ZVAL_ARR(&extra_named_params, EX(extra_named_params));
4790 		zend_get_gc_buffer_add_zval(gc_buffer, &extra_named_params);
4791 	}
4792 
4793 	uint32_t op_num;
4794 	if (UNEXPECTED(execute_data->opline->opcode == ZEND_HANDLE_EXCEPTION)) {
4795 		op_num = EG(opline_before_exception) - op_array->opcodes;
4796 	} else {
4797 		op_num = execute_data->opline - op_array->opcodes;
4798 	}
4799 	ZEND_ASSERT(op_num < op_array->last);
4800 
4801 	if (call) {
4802 		zend_unfinished_calls_gc(execute_data, call, op_num, gc_buffer);
4803 	}
4804 
4805 	if (execute_data->opline != op_array->opcodes) {
4806 		uint32_t i;
4807 		for (i = 0; i < op_array->last_live_range; i++) {
4808 			const zend_live_range *range = &op_array->live_range[i];
4809 			if (range->start > op_num) {
4810 				break;
4811 			} else if (op_num < range->end) {
4812 				uint32_t kind = range->var & ZEND_LIVE_MASK;
4813 				uint32_t var_num = range->var & ~ZEND_LIVE_MASK;
4814 				zval *var = EX_VAR(var_num);
4815 				if (kind == ZEND_LIVE_TMPVAR || kind == ZEND_LIVE_LOOP) {
4816 					zend_get_gc_buffer_add_zval(gc_buffer, var);
4817 				}
4818 			}
4819 		}
4820 	}
4821 
4822 	if (EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE) {
4823 		return execute_data->symbol_table;
4824 	} else {
4825 		return NULL;
4826 	}
4827 }
4828 
4829 #if ZEND_VM_SPEC
zend_swap_operands(zend_op * op)4830 static void zend_swap_operands(zend_op *op) /* {{{ */
4831 {
4832 	znode_op     tmp;
4833 	uint8_t   tmp_type;
4834 
4835 	tmp          = op->op1;
4836 	tmp_type     = op->op1_type;
4837 	op->op1      = op->op2;
4838 	op->op1_type = op->op2_type;
4839 	op->op2      = tmp;
4840 	op->op2_type = tmp_type;
4841 
4842 #ifdef ZEND_VERIFY_TYPE_INFERENCE
4843 	uint32_t tmp_info;
4844 	tmp_info = op->op1_use_type;
4845 	op->op1_use_type = op->op2_use_type;
4846 	op->op2_use_type = tmp_info;
4847 	tmp_info = op->op1_def_type;
4848 	op->op1_def_type = op->op2_def_type;
4849 	op->op2_def_type = tmp_info;
4850 #endif
4851 }
4852 /* }}} */
4853 #endif
4854 
zend_init_dynamic_call_string(zend_string * function,uint32_t num_args)4855 static zend_never_inline zend_execute_data *zend_init_dynamic_call_string(zend_string *function, uint32_t num_args) /* {{{ */
4856 {
4857 	zend_function *fbc;
4858 	zval *func;
4859 	zend_class_entry *called_scope;
4860 	zend_string *lcname;
4861 	const char *colon;
4862 
4863 	if ((colon = zend_memrchr(ZSTR_VAL(function), ':', ZSTR_LEN(function))) != NULL &&
4864 		colon > ZSTR_VAL(function) &&
4865 		*(colon-1) == ':'
4866 	) {
4867 		zend_string *mname;
4868 		size_t cname_length = colon - ZSTR_VAL(function) - 1;
4869 		size_t mname_length = ZSTR_LEN(function) - cname_length - (sizeof("::") - 1);
4870 
4871 		lcname = zend_string_init(ZSTR_VAL(function), cname_length, 0);
4872 
4873 		called_scope = zend_fetch_class_by_name(lcname, NULL, ZEND_FETCH_CLASS_DEFAULT | ZEND_FETCH_CLASS_EXCEPTION);
4874 		if (UNEXPECTED(called_scope == NULL)) {
4875 			zend_string_release_ex(lcname, 0);
4876 			return NULL;
4877 		}
4878 
4879 		mname = zend_string_init(ZSTR_VAL(function) + (cname_length + sizeof("::") - 1), mname_length, 0);
4880 
4881 		if (called_scope->get_static_method) {
4882 			fbc = called_scope->get_static_method(called_scope, mname);
4883 		} else {
4884 			fbc = zend_std_get_static_method(called_scope, mname, NULL);
4885 		}
4886 		if (UNEXPECTED(fbc == NULL)) {
4887 			if (EXPECTED(!EG(exception))) {
4888 				zend_undefined_method(called_scope, mname);
4889 			}
4890 			zend_string_release_ex(lcname, 0);
4891 			zend_string_release_ex(mname, 0);
4892 			return NULL;
4893 		}
4894 
4895 		zend_string_release_ex(lcname, 0);
4896 		zend_string_release_ex(mname, 0);
4897 
4898 		if (UNEXPECTED(!(fbc->common.fn_flags & ZEND_ACC_STATIC))) {
4899 			zend_non_static_method_call(fbc);
4900 			if (fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
4901 				zend_string_release_ex(fbc->common.function_name, 0);
4902 				zend_free_trampoline(fbc);
4903 			}
4904 			return NULL;
4905 		}
4906 		if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
4907 			init_func_run_time_cache(&fbc->op_array);
4908 		}
4909 	} else {
4910 		if (ZSTR_VAL(function)[0] == '\\') {
4911 			lcname = zend_string_alloc(ZSTR_LEN(function) - 1, 0);
4912 			zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(function) + 1, ZSTR_LEN(function) - 1);
4913 		} else {
4914 			lcname = zend_string_tolower(function);
4915 		}
4916 		if (UNEXPECTED((func = zend_hash_find(EG(function_table), lcname)) == NULL)) {
4917 			zend_throw_error(NULL, "Call to undefined function %s()", ZSTR_VAL(function));
4918 			zend_string_release_ex(lcname, 0);
4919 			return NULL;
4920 		}
4921 		zend_string_release_ex(lcname, 0);
4922 
4923 		fbc = Z_FUNC_P(func);
4924 		if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
4925 			init_func_run_time_cache(&fbc->op_array);
4926 		}
4927 		called_scope = NULL;
4928 	}
4929 
4930 	return zend_vm_stack_push_call_frame(ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC,
4931 		fbc, num_args, called_scope);
4932 }
4933 /* }}} */
4934 
zend_init_dynamic_call_object(zend_object * function,uint32_t num_args)4935 static zend_never_inline zend_execute_data *zend_init_dynamic_call_object(zend_object *function, uint32_t num_args) /* {{{ */
4936 {
4937 	zend_function *fbc;
4938 	void *object_or_called_scope;
4939 	zend_class_entry *called_scope;
4940 	zend_object *object;
4941 	uint32_t call_info;
4942 
4943 	if (EXPECTED(function->handlers->get_closure) &&
4944 	    EXPECTED(function->handlers->get_closure(function, &called_scope, &fbc, &object, 0) == SUCCESS)) {
4945 
4946 		object_or_called_scope = called_scope;
4947 		if (EXPECTED(fbc->common.fn_flags & ZEND_ACC_CLOSURE)) {
4948 			/* Delay closure destruction until its invocation */
4949 			GC_ADDREF(ZEND_CLOSURE_OBJECT(fbc));
4950 			ZEND_ASSERT(ZEND_ACC_FAKE_CLOSURE == ZEND_CALL_FAKE_CLOSURE);
4951 			call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_CLOSURE |
4952 				(fbc->common.fn_flags & ZEND_ACC_FAKE_CLOSURE);
4953 			if (object) {
4954 				call_info |= ZEND_CALL_HAS_THIS;
4955 				object_or_called_scope = object;
4956 			}
4957 		} else {
4958 			call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC;
4959 			if (object) {
4960 				call_info |= ZEND_CALL_RELEASE_THIS | ZEND_CALL_HAS_THIS;
4961 				GC_ADDREF(object); /* For $this pointer */
4962 				object_or_called_scope = object;
4963 			}
4964 		}
4965 	} else {
4966 		zend_throw_error(NULL, "Object of type %s is not callable", ZSTR_VAL(function->ce->name));
4967 		return NULL;
4968 	}
4969 
4970 	if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
4971 		init_func_run_time_cache(&fbc->op_array);
4972 	}
4973 
4974 	return zend_vm_stack_push_call_frame(call_info,
4975 		fbc, num_args, object_or_called_scope);
4976 }
4977 /* }}} */
4978 
zend_init_dynamic_call_array(zend_array * function,uint32_t num_args)4979 static zend_never_inline zend_execute_data *zend_init_dynamic_call_array(zend_array *function, uint32_t num_args) /* {{{ */
4980 {
4981 	zend_function *fbc;
4982 	void *object_or_called_scope;
4983 	uint32_t call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC;
4984 
4985 	if (zend_hash_num_elements(function) == 2) {
4986 		zval *obj;
4987 		zval *method;
4988 		obj = zend_hash_index_find(function, 0);
4989 		method = zend_hash_index_find(function, 1);
4990 
4991 		if (UNEXPECTED(!obj) || UNEXPECTED(!method)) {
4992 			zend_throw_error(NULL, "Array callback has to contain indices 0 and 1");
4993 			return NULL;
4994 		}
4995 
4996 		ZVAL_DEREF(obj);
4997 		if (UNEXPECTED(Z_TYPE_P(obj) != IS_STRING) && UNEXPECTED(Z_TYPE_P(obj) != IS_OBJECT)) {
4998 			zend_throw_error(NULL, "First array member is not a valid class name or object");
4999 			return NULL;
5000 		}
5001 
5002 		ZVAL_DEREF(method);
5003 		if (UNEXPECTED(Z_TYPE_P(method) != IS_STRING)) {
5004 			zend_throw_error(NULL, "Second array member is not a valid method");
5005 			return NULL;
5006 		}
5007 
5008 		if (Z_TYPE_P(obj) == IS_STRING) {
5009 			zend_class_entry *called_scope = zend_fetch_class_by_name(Z_STR_P(obj), NULL, ZEND_FETCH_CLASS_DEFAULT | ZEND_FETCH_CLASS_EXCEPTION);
5010 
5011 			if (UNEXPECTED(called_scope == NULL)) {
5012 				return NULL;
5013 			}
5014 
5015 			if (called_scope->get_static_method) {
5016 				fbc = called_scope->get_static_method(called_scope, Z_STR_P(method));
5017 			} else {
5018 				fbc = zend_std_get_static_method(called_scope, Z_STR_P(method), NULL);
5019 			}
5020 			if (UNEXPECTED(fbc == NULL)) {
5021 				if (EXPECTED(!EG(exception))) {
5022 					zend_undefined_method(called_scope, Z_STR_P(method));
5023 				}
5024 				return NULL;
5025 			}
5026 			if (!(fbc->common.fn_flags & ZEND_ACC_STATIC)) {
5027 				zend_non_static_method_call(fbc);
5028 				if (fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
5029 					zend_string_release_ex(fbc->common.function_name, 0);
5030 					zend_free_trampoline(fbc);
5031 				}
5032 				return NULL;
5033 			}
5034 			object_or_called_scope = called_scope;
5035 		} else {
5036 			zend_object *object = Z_OBJ_P(obj);
5037 
5038 			fbc = Z_OBJ_HT_P(obj)->get_method(&object, Z_STR_P(method), NULL);
5039 			if (UNEXPECTED(fbc == NULL)) {
5040 				if (EXPECTED(!EG(exception))) {
5041 					zend_undefined_method(object->ce, Z_STR_P(method));
5042 				}
5043 				return NULL;
5044 			}
5045 
5046 			if ((fbc->common.fn_flags & ZEND_ACC_STATIC) != 0) {
5047 				object_or_called_scope = object->ce;
5048 			} else {
5049 				call_info |= ZEND_CALL_RELEASE_THIS | ZEND_CALL_HAS_THIS;
5050 				GC_ADDREF(object); /* For $this pointer */
5051 				object_or_called_scope = object;
5052 			}
5053 		}
5054 	} else {
5055 		zend_throw_error(NULL, "Array callback must have exactly two elements");
5056 		return NULL;
5057 	}
5058 
5059 	if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
5060 		init_func_run_time_cache(&fbc->op_array);
5061 	}
5062 
5063 	return zend_vm_stack_push_call_frame(call_info,
5064 		fbc, num_args, object_or_called_scope);
5065 }
5066 /* }}} */
5067 
5068 #define ZEND_FAKE_OP_ARRAY ((zend_op_array*)(intptr_t)-1)
5069 
zend_include_or_eval(zval * inc_filename_zv,int type)5070 static zend_never_inline zend_op_array* ZEND_FASTCALL zend_include_or_eval(zval *inc_filename_zv, int type) /* {{{ */
5071 {
5072 	zend_op_array *new_op_array = NULL;
5073 	zend_string *tmp_inc_filename;
5074 	zend_string *inc_filename = zval_try_get_tmp_string(inc_filename_zv, &tmp_inc_filename);
5075 	if (UNEXPECTED(!inc_filename)) {
5076 		return NULL;
5077 	}
5078 
5079 	switch (type) {
5080 		case ZEND_INCLUDE_ONCE:
5081 		case ZEND_REQUIRE_ONCE: {
5082 				zend_file_handle file_handle;
5083 				zend_string *resolved_path;
5084 
5085 				resolved_path = zend_resolve_path(inc_filename);
5086 				if (EXPECTED(resolved_path)) {
5087 					if (zend_hash_exists(&EG(included_files), resolved_path)) {
5088 						new_op_array = ZEND_FAKE_OP_ARRAY;
5089 						zend_string_release_ex(resolved_path, 0);
5090 						break;
5091 					}
5092 				} else if (UNEXPECTED(EG(exception))) {
5093 					break;
5094 				} else if (UNEXPECTED(strlen(ZSTR_VAL(inc_filename)) != ZSTR_LEN(inc_filename))) {
5095 					zend_message_dispatcher(
5096 						(type == ZEND_INCLUDE_ONCE) ?
5097 							ZMSG_FAILED_INCLUDE_FOPEN : ZMSG_FAILED_REQUIRE_FOPEN,
5098 							ZSTR_VAL(inc_filename));
5099 					break;
5100 				} else {
5101 					resolved_path = zend_string_copy(inc_filename);
5102 				}
5103 
5104 				zend_stream_init_filename_ex(&file_handle, resolved_path);
5105 				if (SUCCESS == zend_stream_open(&file_handle)) {
5106 
5107 					if (!file_handle.opened_path) {
5108 						file_handle.opened_path = zend_string_copy(resolved_path);
5109 					}
5110 
5111 					if (zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path)) {
5112 						new_op_array = zend_compile_file(&file_handle, (type==ZEND_INCLUDE_ONCE?ZEND_INCLUDE:ZEND_REQUIRE));
5113 					} else {
5114 						new_op_array = ZEND_FAKE_OP_ARRAY;
5115 					}
5116 				} else if (!EG(exception)) {
5117 					zend_message_dispatcher(
5118 						(type == ZEND_INCLUDE_ONCE) ?
5119 							ZMSG_FAILED_INCLUDE_FOPEN : ZMSG_FAILED_REQUIRE_FOPEN,
5120 							ZSTR_VAL(inc_filename));
5121 				}
5122 				zend_destroy_file_handle(&file_handle);
5123 				zend_string_release_ex(resolved_path, 0);
5124 			}
5125 			break;
5126 		case ZEND_INCLUDE:
5127 		case ZEND_REQUIRE:
5128 			if (UNEXPECTED(strlen(ZSTR_VAL(inc_filename)) != ZSTR_LEN(inc_filename))) {
5129 				zend_message_dispatcher(
5130 					(type == ZEND_INCLUDE) ?
5131 						ZMSG_FAILED_INCLUDE_FOPEN : ZMSG_FAILED_REQUIRE_FOPEN,
5132 						ZSTR_VAL(inc_filename));
5133 				break;
5134 			}
5135 			new_op_array = compile_filename(type, inc_filename);
5136 			break;
5137 		case ZEND_EVAL: {
5138 				char *eval_desc = zend_make_compiled_string_description("eval()'d code");
5139 				new_op_array = zend_compile_string(inc_filename, eval_desc, ZEND_COMPILE_POSITION_AFTER_OPEN_TAG);
5140 				efree(eval_desc);
5141 			}
5142 			break;
5143 		EMPTY_SWITCH_DEFAULT_CASE()
5144 	}
5145 
5146 	zend_tmp_string_release(tmp_inc_filename);
5147 	return new_op_array;
5148 }
5149 /* }}} */
5150 
zend_fe_reset_iterator(zval * array_ptr,int by_ref OPLINE_DC EXECUTE_DATA_DC)5151 static zend_never_inline bool ZEND_FASTCALL zend_fe_reset_iterator(zval *array_ptr, int by_ref OPLINE_DC EXECUTE_DATA_DC) /* {{{ */
5152 {
5153 	zend_class_entry *ce = Z_OBJCE_P(array_ptr);
5154 	zend_object_iterator *iter = ce->get_iterator(ce, array_ptr, by_ref);
5155 	bool is_empty;
5156 
5157 	if (UNEXPECTED(!iter) || UNEXPECTED(EG(exception))) {
5158 		if (iter) {
5159 			OBJ_RELEASE(&iter->std);
5160 		}
5161 		if (!EG(exception)) {
5162 			zend_throw_exception_ex(NULL, 0, "Object of type %s did not create an Iterator", ZSTR_VAL(ce->name));
5163 		}
5164 		ZVAL_UNDEF(EX_VAR(opline->result.var));
5165 		return 1;
5166 	}
5167 
5168 	iter->index = 0;
5169 	if (iter->funcs->rewind) {
5170 		iter->funcs->rewind(iter);
5171 		if (UNEXPECTED(EG(exception) != NULL)) {
5172 			OBJ_RELEASE(&iter->std);
5173 			ZVAL_UNDEF(EX_VAR(opline->result.var));
5174 			return 1;
5175 		}
5176 	}
5177 
5178 	is_empty = iter->funcs->valid(iter) != SUCCESS;
5179 
5180 	if (UNEXPECTED(EG(exception) != NULL)) {
5181 		OBJ_RELEASE(&iter->std);
5182 		ZVAL_UNDEF(EX_VAR(opline->result.var));
5183 		return 1;
5184 	}
5185 	iter->index = -1; /* will be set to 0 before using next handler */
5186 
5187 	ZVAL_OBJ(EX_VAR(opline->result.var), &iter->std);
5188 	Z_FE_ITER_P(EX_VAR(opline->result.var)) = (uint32_t)-1;
5189 
5190 	return is_empty;
5191 }
5192 /* }}} */
5193 
_zend_quick_get_constant(const zval * key,uint32_t flags,bool check_defined_only OPLINE_DC EXECUTE_DATA_DC)5194 static zend_always_inline zend_result _zend_quick_get_constant(
5195 		const zval *key, uint32_t flags, bool check_defined_only OPLINE_DC EXECUTE_DATA_DC) /* {{{ */
5196 {
5197 	zval *zv;
5198 	zend_constant *c = NULL;
5199 
5200 	/* null/true/false are resolved during compilation, so don't check for them here. */
5201 	zv = zend_hash_find_known_hash(EG(zend_constants), Z_STR_P(key));
5202 	if (zv) {
5203 		c = (zend_constant*)Z_PTR_P(zv);
5204 	} else if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
5205 		key++;
5206 		zv = zend_hash_find_known_hash(EG(zend_constants), Z_STR_P(key));
5207 		if (zv) {
5208 			c = (zend_constant*)Z_PTR_P(zv);
5209 		}
5210 	}
5211 
5212 	if (!c) {
5213 		if (!check_defined_only) {
5214 			zend_throw_error(NULL, "Undefined constant \"%s\"", Z_STRVAL_P(RT_CONSTANT(opline, opline->op2)));
5215 			ZVAL_UNDEF(EX_VAR(opline->result.var));
5216 		}
5217 		return FAILURE;
5218 	}
5219 
5220 	if (!check_defined_only) {
5221 		ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value);
5222 		if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
5223 			zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
5224 			return SUCCESS;
5225 		}
5226 	}
5227 
5228 	CACHE_PTR(opline->extended_value, c);
5229 	return SUCCESS;
5230 }
5231 /* }}} */
5232 
zend_quick_get_constant(const zval * key,uint32_t flags OPLINE_DC EXECUTE_DATA_DC)5233 static zend_never_inline void ZEND_FASTCALL zend_quick_get_constant(
5234 		const zval *key, uint32_t flags OPLINE_DC EXECUTE_DATA_DC) /* {{{ */
5235 {
5236 	_zend_quick_get_constant(key, flags, 0 OPLINE_CC EXECUTE_DATA_CC);
5237 } /* }}} */
5238 
zend_quick_check_constant(const zval * key OPLINE_DC EXECUTE_DATA_DC)5239 static zend_never_inline zend_result ZEND_FASTCALL zend_quick_check_constant(
5240 		const zval *key OPLINE_DC EXECUTE_DATA_DC) /* {{{ */
5241 {
5242 	return _zend_quick_get_constant(key, 0, 1 OPLINE_CC EXECUTE_DATA_CC);
5243 } /* }}} */
5244 
zend_get_arg_offset_by_name(zend_function * fbc,zend_string * arg_name,void ** cache_slot)5245 static zend_always_inline uint32_t zend_get_arg_offset_by_name(
5246 		zend_function *fbc, zend_string *arg_name, void **cache_slot) {
5247 	if (EXPECTED(*cache_slot == fbc)) {
5248 		return *(uintptr_t *)(cache_slot + 1);
5249 	}
5250 
5251 	// TODO: Use a hash table?
5252 	uint32_t num_args = fbc->common.num_args;
5253 	if (EXPECTED(fbc->type == ZEND_USER_FUNCTION)
5254 			|| EXPECTED(fbc->common.fn_flags & ZEND_ACC_USER_ARG_INFO)) {
5255 		for (uint32_t i = 0; i < num_args; i++) {
5256 			zend_arg_info *arg_info = &fbc->op_array.arg_info[i];
5257 			if (zend_string_equals(arg_name, arg_info->name)) {
5258 				*cache_slot = fbc;
5259 				*(uintptr_t *)(cache_slot + 1) = i;
5260 				return i;
5261 			}
5262 		}
5263 	} else {
5264 		for (uint32_t i = 0; i < num_args; i++) {
5265 			zend_internal_arg_info *arg_info = &fbc->internal_function.arg_info[i];
5266 			size_t len = strlen(arg_info->name);
5267 			if (zend_string_equals_cstr(arg_name, arg_info->name, len)) {
5268 				*cache_slot = fbc;
5269 				*(uintptr_t *)(cache_slot + 1) = i;
5270 				return i;
5271 			}
5272 		}
5273 	}
5274 
5275 	if (fbc->common.fn_flags & ZEND_ACC_VARIADIC) {
5276 		*cache_slot = fbc;
5277 		*(uintptr_t *)(cache_slot + 1) = fbc->common.num_args;
5278 		return fbc->common.num_args;
5279 	}
5280 
5281 	return (uint32_t) -1;
5282 }
5283 
zend_handle_named_arg(zend_execute_data ** call_ptr,zend_string * arg_name,uint32_t * arg_num_ptr,void ** cache_slot)5284 zval * ZEND_FASTCALL zend_handle_named_arg(
5285 		zend_execute_data **call_ptr, zend_string *arg_name,
5286 		uint32_t *arg_num_ptr, void **cache_slot) {
5287 	zend_execute_data *call = *call_ptr;
5288 	zend_function *fbc = call->func;
5289 	uint32_t arg_offset = zend_get_arg_offset_by_name(fbc, arg_name, cache_slot);
5290 	if (UNEXPECTED(arg_offset == (uint32_t) -1)) {
5291 		zend_throw_error(NULL, "Unknown named parameter $%s", ZSTR_VAL(arg_name));
5292 		return NULL;
5293 	}
5294 
5295 	zval *arg;
5296 	if (UNEXPECTED(arg_offset == fbc->common.num_args)) {
5297 		/* Unknown named parameter that will be collected into a variadic. */
5298 		if (!(ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS)) {
5299 			ZEND_ADD_CALL_FLAG(call, ZEND_CALL_HAS_EXTRA_NAMED_PARAMS);
5300 			call->extra_named_params = zend_new_array(0);
5301 		}
5302 
5303 		arg = zend_hash_add_empty_element(call->extra_named_params, arg_name);
5304 		if (!arg) {
5305 			zend_throw_error(NULL, "Named parameter $%s overwrites previous argument",
5306 				ZSTR_VAL(arg_name));
5307 			return NULL;
5308 		}
5309 		*arg_num_ptr = arg_offset + 1;
5310 		return arg;
5311 	}
5312 
5313 	uint32_t current_num_args = ZEND_CALL_NUM_ARGS(call);
5314 	// TODO: We may wish to optimize the arg_offset == current_num_args case,
5315 	// which is probably common (if the named parameters are in order of declaration).
5316 	if (arg_offset >= current_num_args) {
5317 		uint32_t new_num_args = arg_offset + 1;
5318 		ZEND_CALL_NUM_ARGS(call) = new_num_args;
5319 
5320 		uint32_t num_extra_args = new_num_args - current_num_args;
5321 		zend_vm_stack_extend_call_frame(call_ptr, current_num_args, num_extra_args);
5322 		call = *call_ptr;
5323 
5324 		arg = ZEND_CALL_VAR_NUM(call, arg_offset);
5325 		if (num_extra_args > 1) {
5326 			zval *zv = ZEND_CALL_VAR_NUM(call, current_num_args);
5327 			do {
5328 				ZVAL_UNDEF(zv);
5329 				zv++;
5330 			} while (zv != arg);
5331 			ZEND_ADD_CALL_FLAG(call, ZEND_CALL_MAY_HAVE_UNDEF);
5332 		}
5333 	} else {
5334 		arg = ZEND_CALL_VAR_NUM(call, arg_offset);
5335 		if (UNEXPECTED(!Z_ISUNDEF_P(arg))) {
5336 			zend_throw_error(NULL, "Named parameter $%s overwrites previous argument",
5337 				ZSTR_VAL(arg_name));
5338 			return NULL;
5339 		}
5340 	}
5341 
5342 	*arg_num_ptr = arg_offset + 1;
5343 	return arg;
5344 }
5345 
start_fake_frame(zend_execute_data * call,const zend_op * opline)5346 static zend_execute_data *start_fake_frame(zend_execute_data *call, const zend_op *opline) {
5347 	zend_execute_data *old_prev_execute_data = call->prev_execute_data;
5348 	call->prev_execute_data = EG(current_execute_data);
5349 	call->opline = opline;
5350 	EG(current_execute_data) = call;
5351 	return old_prev_execute_data;
5352 }
5353 
end_fake_frame(zend_execute_data * call,zend_execute_data * old_prev_execute_data)5354 static void end_fake_frame(zend_execute_data *call, zend_execute_data *old_prev_execute_data) {
5355 	zend_execute_data *prev_execute_data = call->prev_execute_data;
5356 	EG(current_execute_data) = prev_execute_data;
5357 	call->prev_execute_data = old_prev_execute_data;
5358 	if (UNEXPECTED(EG(exception)) && ZEND_USER_CODE(prev_execute_data->func->common.type)) {
5359 		zend_rethrow_exception(prev_execute_data);
5360 	}
5361 }
5362 
zend_handle_undef_args(zend_execute_data * call)5363 ZEND_API zend_result ZEND_FASTCALL zend_handle_undef_args(zend_execute_data *call) {
5364 	zend_function *fbc = call->func;
5365 	if (fbc->type == ZEND_USER_FUNCTION) {
5366 		zend_op_array *op_array = &fbc->op_array;
5367 		uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
5368 		for (uint32_t i = 0; i < num_args; i++) {
5369 			zval *arg = ZEND_CALL_VAR_NUM(call, i);
5370 			if (!Z_ISUNDEF_P(arg)) {
5371 				continue;
5372 			}
5373 
5374 			zend_op *opline = &op_array->opcodes[i];
5375 			if (EXPECTED(opline->opcode == ZEND_RECV_INIT)) {
5376 				zval *default_value = RT_CONSTANT(opline, opline->op2);
5377 				if (Z_OPT_TYPE_P(default_value) == IS_CONSTANT_AST) {
5378 					if (UNEXPECTED(!RUN_TIME_CACHE(op_array))) {
5379 						init_func_run_time_cache(op_array);
5380 					}
5381 
5382 					void *run_time_cache = RUN_TIME_CACHE(op_array);
5383 					zval *cache_val =
5384 						(zval *) ((char *) run_time_cache + Z_CACHE_SLOT_P(default_value));
5385 
5386 					if (Z_TYPE_P(cache_val) != IS_UNDEF) {
5387 						/* We keep in cache only not refcounted values */
5388 						ZVAL_COPY_VALUE(arg, cache_val);
5389 					} else {
5390 						/* Update constant inside a temporary zval, to make sure the CONSTANT_AST
5391 						 * value is not accessible through back traces. */
5392 						zval tmp;
5393 						ZVAL_COPY(&tmp, default_value);
5394 						zend_execute_data *old = start_fake_frame(call, opline);
5395 						zend_result ret = zval_update_constant_ex(&tmp, fbc->op_array.scope);
5396 						end_fake_frame(call, old);
5397 						if (UNEXPECTED(ret == FAILURE)) {
5398 							zval_ptr_dtor_nogc(&tmp);
5399 							return FAILURE;
5400 						}
5401 						ZVAL_COPY_VALUE(arg, &tmp);
5402 						if (!Z_REFCOUNTED(tmp)) {
5403 							ZVAL_COPY_VALUE(cache_val, &tmp);
5404 						}
5405 					}
5406 				} else {
5407 					ZVAL_COPY(arg, default_value);
5408 				}
5409 			} else {
5410 				ZEND_ASSERT(opline->opcode == ZEND_RECV);
5411 				zend_execute_data *old = start_fake_frame(call, opline);
5412 				zend_argument_error(zend_ce_argument_count_error, i + 1, "not passed");
5413 				end_fake_frame(call, old);
5414 				return FAILURE;
5415 			}
5416 		}
5417 
5418 		return SUCCESS;
5419 	} else {
5420 		if (fbc->common.fn_flags & ZEND_ACC_USER_ARG_INFO) {
5421 			/* Magic function, let it deal with it. */
5422 			return SUCCESS;
5423 		}
5424 
5425 		uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
5426 		for (uint32_t i = 0; i < num_args; i++) {
5427 			zval *arg = ZEND_CALL_VAR_NUM(call, i);
5428 			if (!Z_ISUNDEF_P(arg)) {
5429 				continue;
5430 			}
5431 
5432 			zend_internal_arg_info *arg_info = &fbc->internal_function.arg_info[i];
5433 			if (i < fbc->common.required_num_args) {
5434 				zend_execute_data *old = start_fake_frame(call, NULL);
5435 				zend_argument_error(zend_ce_argument_count_error, i + 1, "not passed");
5436 				end_fake_frame(call, old);
5437 				return FAILURE;
5438 			}
5439 
5440 			zval default_value;
5441 			if (zend_get_default_from_internal_arg_info(&default_value, arg_info) == FAILURE) {
5442 				zend_execute_data *old = start_fake_frame(call, NULL);
5443 				zend_argument_error(zend_ce_argument_count_error, i + 1,
5444 					"must be passed explicitly, because the default value is not known");
5445 				end_fake_frame(call, old);
5446 				return FAILURE;
5447 			}
5448 
5449 			if (Z_TYPE(default_value) == IS_CONSTANT_AST) {
5450 				zend_execute_data *old = start_fake_frame(call, NULL);
5451 				zend_result ret = zval_update_constant_ex(&default_value, fbc->common.scope);
5452 				end_fake_frame(call, old);
5453 				if (ret == FAILURE) {
5454 					return FAILURE;
5455 				}
5456 			}
5457 
5458 			ZVAL_COPY_VALUE(arg, &default_value);
5459 			if (ZEND_ARG_SEND_MODE(arg_info) & ZEND_SEND_BY_REF) {
5460 				ZVAL_NEW_REF(arg, arg);
5461 			}
5462 		}
5463 	}
5464 
5465 	return SUCCESS;
5466 }
5467 
zend_free_extra_named_params(zend_array * extra_named_params)5468 ZEND_API void ZEND_FASTCALL zend_free_extra_named_params(zend_array *extra_named_params)
5469 {
5470 	/* Extra named params may be shared. */
5471 	zend_array_release(extra_named_params);
5472 }
5473 
5474 #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
5475 /* Special versions of functions that sets EX(opline) before calling zend_vm_stack_extend() */
_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)5476 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) /* {{{ */
5477 {
5478 	zend_execute_data *call = (zend_execute_data*)EG(vm_stack_top);
5479 
5480 	ZEND_ASSERT_VM_STACK_GLOBAL;
5481 
5482 	if (UNEXPECTED(used_stack > (size_t)(((char*)EG(vm_stack_end)) - (char*)call))) {
5483 		EX(opline) = opline; /* this is the only difference */
5484 		call = (zend_execute_data*)zend_vm_stack_extend(used_stack);
5485 		ZEND_ASSERT_VM_STACK_GLOBAL;
5486 		zend_vm_init_call_frame(call, call_info | ZEND_CALL_ALLOCATED, func, num_args, object_or_called_scope);
5487 		return call;
5488 	} else {
5489 		EG(vm_stack_top) = (zval*)((char*)call + used_stack);
5490 		zend_vm_init_call_frame(call, call_info, func, num_args, object_or_called_scope);
5491 		return call;
5492 	}
5493 } /* }}} */
5494 
_zend_vm_stack_push_call_frame(uint32_t call_info,zend_function * func,uint32_t num_args,void * object_or_called_scope)5495 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) /* {{{ */
5496 {
5497 	uint32_t used_stack = zend_vm_calc_used_stack(num_args, func);
5498 
5499 	return _zend_vm_stack_push_call_frame_ex(used_stack, call_info,
5500 		func, num_args, object_or_called_scope);
5501 } /* }}} */
5502 #else
5503 # define _zend_vm_stack_push_call_frame_ex zend_vm_stack_push_call_frame_ex
5504 # define _zend_vm_stack_push_call_frame    zend_vm_stack_push_call_frame
5505 #endif
5506 
5507 #ifdef ZEND_VM_TRACE_HANDLERS
5508 # include "zend_vm_trace_handlers.h"
5509 #elif defined(ZEND_VM_TRACE_LINES)
5510 # include "zend_vm_trace_lines.h"
5511 #elif defined(ZEND_VM_TRACE_MAP)
5512 # include "zend_vm_trace_map.h"
5513 #elif defined(ZEND_VERIFY_TYPE_INFERENCE)
5514 # include "zend_verify_type_inference.h"
5515 #endif
5516 
5517 #define ZEND_VM_NEXT_OPCODE_EX(check_exception, skip) \
5518 	CHECK_SYMBOL_TABLES() \
5519 	if (check_exception) { \
5520 		OPLINE = EX(opline) + (skip); \
5521 	} else { \
5522 		ZEND_ASSERT(!EG(exception)); \
5523 		OPLINE = opline + (skip); \
5524 	} \
5525 	ZEND_VM_CONTINUE()
5526 
5527 #define ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION() \
5528 	ZEND_VM_NEXT_OPCODE_EX(1, 1)
5529 
5530 #define ZEND_VM_NEXT_OPCODE() \
5531 	ZEND_VM_NEXT_OPCODE_EX(0, 1)
5532 
5533 #define ZEND_VM_SET_NEXT_OPCODE(new_op) \
5534 	CHECK_SYMBOL_TABLES() \
5535 	OPLINE = new_op
5536 
5537 #define ZEND_VM_SET_OPCODE_NO_INTERRUPT(new_op) \
5538 	CHECK_SYMBOL_TABLES() \
5539 	OPLINE = new_op
5540 
5541 #define ZEND_VM_SET_OPCODE(new_op) \
5542 	ZEND_VM_SET_OPCODE_NO_INTERRUPT(new_op); \
5543 	ZEND_VM_INTERRUPT_CHECK()
5544 
5545 #define ZEND_VM_SET_RELATIVE_OPCODE(opline, offset) \
5546 	ZEND_VM_SET_OPCODE(ZEND_OFFSET_TO_OPLINE(opline, offset))
5547 
5548 #define ZEND_VM_JMP_EX(new_op, check_exception) do { \
5549 		if (check_exception && UNEXPECTED(EG(exception))) { \
5550 			HANDLE_EXCEPTION(); \
5551 		} \
5552 		ZEND_VM_SET_OPCODE(new_op); \
5553 		ZEND_VM_CONTINUE(); \
5554 	} while (0)
5555 
5556 #define ZEND_VM_JMP(new_op) \
5557 	ZEND_VM_JMP_EX(new_op, 1)
5558 
5559 #define ZEND_VM_INC_OPCODE() \
5560 	OPLINE++
5561 
5562 
5563 #define ZEND_VM_REPEATABLE_OPCODE \
5564 	do {
5565 #define ZEND_VM_REPEAT_OPCODE(_opcode) \
5566 	} while (UNEXPECTED((++opline)->opcode == _opcode)); \
5567 	OPLINE = opline; \
5568 	ZEND_VM_CONTINUE()
5569 #define ZEND_VM_SMART_BRANCH(_result, _check) do { \
5570 		if ((_check) && UNEXPECTED(EG(exception))) { \
5571 			OPLINE = EX(opline); \
5572 		} else if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPZ|IS_TMP_VAR))) { \
5573 			if (_result) { \
5574 				ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
5575 			} else { \
5576 				ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
5577 			} \
5578 		} else if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPNZ|IS_TMP_VAR))) { \
5579 			if (!(_result)) { \
5580 				ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
5581 			} else { \
5582 				ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
5583 			} \
5584 		} else { \
5585 			ZVAL_BOOL(EX_VAR(opline->result.var), _result); \
5586 			ZEND_VM_SET_NEXT_OPCODE(opline + 1); \
5587 		} \
5588 		ZEND_VM_CONTINUE(); \
5589 	} while (0)
5590 #define ZEND_VM_SMART_BRANCH_JMPZ(_result, _check) do { \
5591 		if ((_check) && UNEXPECTED(EG(exception))) { \
5592 			OPLINE = EX(opline); \
5593 		} else if (_result) { \
5594 			ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
5595 		} else { \
5596 			ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
5597 		} \
5598 		ZEND_VM_CONTINUE(); \
5599 	} while (0)
5600 #define ZEND_VM_SMART_BRANCH_JMPNZ(_result, _check) do { \
5601 		if ((_check) && UNEXPECTED(EG(exception))) { \
5602 			OPLINE = EX(opline); \
5603 		} else if (!(_result)) { \
5604 			ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
5605 		} else { \
5606 			ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
5607 		} \
5608 		ZEND_VM_CONTINUE(); \
5609 	} while (0)
5610 #define ZEND_VM_SMART_BRANCH_NONE(_result, _check) do { \
5611 		ZVAL_BOOL(EX_VAR(opline->result.var), _result); \
5612 		ZEND_VM_NEXT_OPCODE_EX(_check, 1); \
5613 		ZEND_VM_CONTINUE(); \
5614 	} while (0)
5615 #define ZEND_VM_SMART_BRANCH_TRUE() do { \
5616 		if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPNZ|IS_TMP_VAR))) { \
5617 			ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
5618 		} else if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPZ|IS_TMP_VAR))) { \
5619 			ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
5620 		} else { \
5621 			ZVAL_TRUE(EX_VAR(opline->result.var)); \
5622 			ZEND_VM_SET_NEXT_OPCODE(opline + 1); \
5623 		} \
5624 		ZEND_VM_CONTINUE(); \
5625 	} while (0)
5626 #define ZEND_VM_SMART_BRANCH_TRUE_JMPZ() do { \
5627 		ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
5628 		ZEND_VM_CONTINUE(); \
5629 	} while (0)
5630 #define ZEND_VM_SMART_BRANCH_TRUE_JMPNZ() do { \
5631 		ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
5632 		ZEND_VM_CONTINUE(); \
5633 	} while (0)
5634 #define ZEND_VM_SMART_BRANCH_TRUE_NONE() do { \
5635 		ZVAL_TRUE(EX_VAR(opline->result.var)); \
5636 		ZEND_VM_NEXT_OPCODE(); \
5637 	} while (0)
5638 #define ZEND_VM_SMART_BRANCH_FALSE() do { \
5639 		if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPNZ|IS_TMP_VAR))) { \
5640 			ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
5641 		} else if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPZ|IS_TMP_VAR))) { \
5642 			ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
5643 		} else { \
5644 			ZVAL_FALSE(EX_VAR(opline->result.var)); \
5645 			ZEND_VM_SET_NEXT_OPCODE(opline + 1); \
5646 		} \
5647 		ZEND_VM_CONTINUE(); \
5648 	} while (0)
5649 #define ZEND_VM_SMART_BRANCH_FALSE_JMPZ() do { \
5650 		ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
5651 		ZEND_VM_CONTINUE(); \
5652 	} while (0)
5653 #define ZEND_VM_SMART_BRANCH_FALSE_JMPNZ() do { \
5654 		ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
5655 		ZEND_VM_CONTINUE(); \
5656 	} while (0)
5657 #define ZEND_VM_SMART_BRANCH_FALSE_NONE() do { \
5658 		ZVAL_FALSE(EX_VAR(opline->result.var)); \
5659 		ZEND_VM_NEXT_OPCODE(); \
5660 	} while (0)
5661 
5662 #ifdef __GNUC__
5663 # define ZEND_VM_GUARD(name) __asm__("#" #name)
5664 #else
5665 # define ZEND_VM_GUARD(name)
5666 #endif
5667 
5668 #define UNDEF_RESULT() do { \
5669 		if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { \
5670 			ZVAL_UNDEF(EX_VAR(opline->result.var)); \
5671 		} \
5672 	} while (0)
5673 
5674 /* This callback disables optimization of "vm_stack_data" variable in VM */
5675 ZEND_API void (ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data) = NULL;
5676 
5677 #include "zend_vm_execute.h"
5678 
zend_set_user_opcode_handler(zend_uchar opcode,user_opcode_handler_t handler)5679 ZEND_API zend_result zend_set_user_opcode_handler(zend_uchar opcode, user_opcode_handler_t handler)
5680 {
5681 	if (opcode != ZEND_USER_OPCODE) {
5682 		if (handler == NULL) {
5683 			/* restore the original handler */
5684 			zend_user_opcodes[opcode] = opcode;
5685 		} else {
5686 			zend_user_opcodes[opcode] = ZEND_USER_OPCODE;
5687 		}
5688 		zend_user_opcode_handlers[opcode] = handler;
5689 		return SUCCESS;
5690 	}
5691 	return FAILURE;
5692 }
5693 
zend_get_user_opcode_handler(zend_uchar opcode)5694 ZEND_API user_opcode_handler_t zend_get_user_opcode_handler(zend_uchar opcode)
5695 {
5696 	return zend_user_opcode_handlers[opcode];
5697 }
5698 
zend_get_zval_ptr(const zend_op * opline,int op_type,const znode_op * node,const zend_execute_data * execute_data)5699 ZEND_API zval *zend_get_zval_ptr(const zend_op *opline, int op_type, const znode_op *node, const zend_execute_data *execute_data)
5700 {
5701 	zval *ret;
5702 
5703 	switch (op_type) {
5704 		case IS_CONST:
5705 			ret = RT_CONSTANT(opline, *node);
5706 			break;
5707 		case IS_TMP_VAR:
5708 		case IS_VAR:
5709 		case IS_CV:
5710 			ret = EX_VAR(node->var);
5711 			break;
5712 		default:
5713 			ret = NULL;
5714 			break;
5715 	}
5716 	return ret;
5717 }
5718