xref: /php-src/Zend/zend_API.h (revision fd73681c)
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    |          Andrei Zmievski <andrei@php.net>                            |
18    |          Dmitry Stogov <dmitry@php.net>                              |
19    +----------------------------------------------------------------------+
20 */
21 
22 #ifndef ZEND_API_H
23 #define ZEND_API_H
24 
25 #include "zend_modules.h"
26 #include "zend_list.h"
27 #include "zend_operators.h"
28 #include "zend_variables.h"
29 #include "zend_execute.h"
30 #include "zend_type_info.h"
31 #include "zend_frameless_function.h"
32 
33 BEGIN_EXTERN_C()
34 
35 typedef struct _zend_function_entry {
36 	const char *fname;
37 	zif_handler handler;
38 	const struct _zend_internal_arg_info *arg_info;
39 	uint32_t num_args;
40 	uint32_t flags;
41 	const zend_frameless_function_info *frameless_function_infos;
42 	const char *doc_comment;
43 } zend_function_entry;
44 
45 typedef struct _zend_fcall_info {
46 	size_t size;
47 	zval function_name;
48 	zval *retval;
49 	zval *params;
50 	zend_object *object;
51 	uint32_t param_count;
52 	/* This hashtable can also contain positional arguments (with integer keys),
53 	 * which will be appended to the normal params[]. This makes it easier to
54 	 * integrate APIs like call_user_func_array(). The usual restriction that
55 	 * there may not be position arguments after named arguments applies. */
56 	HashTable *named_params;
57 } zend_fcall_info;
58 
59 typedef struct _zend_fcall_info_cache {
60 	zend_function *function_handler;
61 	zend_class_entry *calling_scope;
62 	zend_class_entry *called_scope;
63 	zend_object *object; /* Instance of object for method calls */
64 	zend_object *closure; /* Closure reference, only if the callable *is* the object */
65 } zend_fcall_info_cache;
66 
67 #define ZEND_NS_NAME(ns, name)			ns "\\" name
68 
69 /* ZEND_FN/ZEND_MN are inlined below to prevent pre-scan macro expansion,
70  * which causes issues if the function name is also a macro name. */
71 #define ZEND_FN(name) zif_##name
72 #define ZEND_MN(name) zim_##name
73 
74 #define ZEND_NAMED_FUNCTION(name)		void ZEND_FASTCALL name(INTERNAL_FUNCTION_PARAMETERS)
75 #define ZEND_FUNCTION(name)				ZEND_NAMED_FUNCTION(zif_##name)
76 #define ZEND_METHOD(classname, name)	ZEND_NAMED_FUNCTION(zim_##classname##_##name)
77 
78 #define ZEND_FENTRY(zend_name, name, arg_info, flags)	{ #zend_name, name, arg_info, (uint32_t) (sizeof(arg_info)/sizeof(struct _zend_internal_arg_info)-1), flags, NULL, NULL },
79 
80 #define ZEND_RAW_FENTRY(zend_name, name, arg_info, flags, frameless_function_infos, doc_comment)   { zend_name, name, arg_info, (uint32_t) (sizeof(arg_info)/sizeof(struct _zend_internal_arg_info)-1), flags, frameless_function_infos, doc_comment },
81 
82 /* Same as ZEND_NAMED_FE */
83 #define ZEND_RAW_NAMED_FE(zend_name, name, arg_info) ZEND_RAW_FENTRY(#zend_name, name, arg_info, 0, NULL, NULL)
84 
85 #define ZEND_NAMED_FE(zend_name, name, arg_info)	ZEND_RAW_FENTRY(#zend_name, name, arg_info, 0, NULL, NULL)
86 #define ZEND_FE(name, arg_info)						ZEND_RAW_FENTRY(#name, zif_##name, arg_info, 0, NULL, NULL)
87 #define ZEND_DEP_FE(name, arg_info)                 ZEND_RAW_FENTRY(#name, zif_##name, arg_info, ZEND_ACC_DEPRECATED, NULL, NULL)
88 #define ZEND_FALIAS(name, alias, arg_info)			ZEND_RAW_FENTRY(#name, zif_##alias, arg_info, 0, NULL, NULL)
89 #define ZEND_DEP_FALIAS(name, alias, arg_info)		ZEND_RAW_FENTRY(#name, zif_##alias, arg_info, ZEND_ACC_DEPRECATED, NULL, NULL)
90 #define ZEND_NAMED_ME(zend_name, name, arg_info, flags)	ZEND_FENTRY(zend_name, name, arg_info, flags)
91 #define ZEND_ME(classname, name, arg_info, flags)	ZEND_RAW_FENTRY(#name, zim_##classname##_##name, arg_info, flags, NULL, NULL)
92 #define ZEND_DEP_ME(classname, name, arg_info, flags) ZEND_RAW_FENTRY(#name, zim_##classname##_##name, arg_info, flags | ZEND_ACC_DEPRECATED, NULL, NULL)
93 #define ZEND_ABSTRACT_ME(classname, name, arg_info)	ZEND_RAW_FENTRY(#name, NULL, arg_info, ZEND_ACC_PUBLIC|ZEND_ACC_ABSTRACT, NULL, NULL)
94 #define ZEND_ABSTRACT_ME_WITH_FLAGS(classname, name, arg_info, flags)	ZEND_RAW_FENTRY(#name, NULL, arg_info, flags, NULL, NULL)
95 #define ZEND_MALIAS(classname, name, alias, arg_info, flags) ZEND_RAW_FENTRY(#name, zim_##classname##_##alias, arg_info, flags, NULL, NULL)
96 #define ZEND_ME_MAPPING(name, func_name, arg_info, flags) ZEND_RAW_FENTRY(#name, zif_##func_name, arg_info, flags, NULL, NULL)
97 #define ZEND_FRAMELESS_FE(name, arg_info, flags, frameless_function_infos, doc_comment) \
98 	{ #name, zif_##name, arg_info, (uint32_t) (sizeof(arg_info)/sizeof(struct _zend_internal_arg_info)-1), flags, frameless_function_infos, doc_comment },
99 
100 #define ZEND_NS_FENTRY(ns, zend_name, name, arg_info, flags)		ZEND_RAW_FENTRY(ZEND_NS_NAME(ns, #zend_name), name, arg_info, flags, NULL, NULL)
101 
102 #define ZEND_NS_RAW_FENTRY(ns, zend_name, name, arg_info, flags)	ZEND_RAW_FENTRY(ZEND_NS_NAME(ns, zend_name), name, arg_info, flags, NULL, NULL)
103 /**
104  * Note that if you are asserting that a function is compile-time evaluable, you are asserting that
105  *
106  * 1. The function will always have the same result for the same arguments
107  * 2. The function does not depend on global state such as ini settings or locale (e.g. mb_strtolower), number_format(), etc.
108  * 3. The function does not have side effects. It is okay if they throw
109  *    or warn on invalid arguments, as we detect this and will discard the evaluation result.
110  * 4. The function will not take an unreasonable amount of time or memory to compute on code that may be seen in practice.
111  *    (e.g. str_repeat is special cased to check the length instead of using this)
112  */
113 #define ZEND_SUPPORTS_COMPILE_TIME_EVAL_FE(name, arg_info) ZEND_RAW_FENTRY(#name, zif_##name, arg_info, ZEND_ACC_COMPILE_TIME_EVAL, NULL, NULL)
114 
115 /* Same as ZEND_NS_NAMED_FE */
116 #define ZEND_NS_RAW_NAMED_FE(ns, zend_name, name, arg_info)			ZEND_NS_RAW_FENTRY(ns, #zend_name, name, arg_info, 0)
117 
118 #define ZEND_NS_NAMED_FE(ns, zend_name, name, arg_info)	ZEND_NS_RAW_FENTRY(ns, #zend_name, name, arg_info, 0)
119 #define ZEND_NS_FE(ns, name, arg_info)					ZEND_NS_RAW_FENTRY(ns, #name, zif_##name, arg_info, 0)
120 #define ZEND_NS_DEP_FE(ns, name, arg_info)				ZEND_NS_RAW_FENTRY(ns, #name, zif_##name, arg_info, ZEND_ACC_DEPRECATED)
121 #define ZEND_NS_FALIAS(ns, name, alias, arg_info)		ZEND_NS_RAW_FENTRY(ns, #name, zif_##alias, arg_info, 0)
122 #define ZEND_NS_DEP_FALIAS(ns, name, alias, arg_info)	ZEND_NS_RAW_FENTRY(ns, #name, zif_##alias, arg_info, ZEND_ACC_DEPRECATED)
123 
124 #define ZEND_FE_END            { NULL, NULL, NULL, 0, 0, NULL, NULL }
125 
126 #define _ZEND_ARG_INFO_FLAGS(pass_by_ref, is_variadic, is_tentative) \
127 	(((pass_by_ref) << _ZEND_SEND_MODE_SHIFT) | ((is_variadic) ? _ZEND_IS_VARIADIC_BIT : 0) | ((is_tentative) ? _ZEND_IS_TENTATIVE_BIT : 0))
128 
129 /* Arginfo structures without type information */
130 #define ZEND_ARG_INFO(pass_by_ref, name) \
131 	{ #name, ZEND_TYPE_INIT_NONE(_ZEND_ARG_INFO_FLAGS(pass_by_ref, 0, 0)), NULL },
132 #define ZEND_ARG_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, default_value) \
133 	{ #name, ZEND_TYPE_INIT_NONE(_ZEND_ARG_INFO_FLAGS(pass_by_ref, 0, 0)), default_value },
134 #define ZEND_ARG_VARIADIC_INFO(pass_by_ref, name) \
135 	{ #name, ZEND_TYPE_INIT_NONE(_ZEND_ARG_INFO_FLAGS(pass_by_ref, 1, 0)), NULL },
136 
137 /* Arginfo structures with simple type information */
138 #define ZEND_ARG_TYPE_INFO(pass_by_ref, name, type_hint, allow_null) \
139 	{ #name, ZEND_TYPE_INIT_CODE(type_hint, allow_null, _ZEND_ARG_INFO_FLAGS(pass_by_ref, 0, 0)), NULL },
140 #define ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, type_hint, allow_null, default_value) \
141 	{ #name, ZEND_TYPE_INIT_CODE(type_hint, allow_null, _ZEND_ARG_INFO_FLAGS(pass_by_ref, 0, 0)), default_value },
142 #define ZEND_ARG_VARIADIC_TYPE_INFO(pass_by_ref, name, type_hint, allow_null) \
143 	{ #name, ZEND_TYPE_INIT_CODE(type_hint, allow_null, _ZEND_ARG_INFO_FLAGS(pass_by_ref, 1, 0)), NULL },
144 
145 /* Arginfo structures with complex type information */
146 #define ZEND_ARG_TYPE_MASK(pass_by_ref, name, type_mask, default_value) \
147 	{ #name, ZEND_TYPE_INIT_MASK(type_mask | _ZEND_ARG_INFO_FLAGS(pass_by_ref, 0, 0)), default_value },
148 #define ZEND_ARG_OBJ_TYPE_MASK(pass_by_ref, name, class_name, type_mask, default_value) \
149 	{ #name, ZEND_TYPE_INIT_CLASS_CONST_MASK(#class_name, type_mask | _ZEND_ARG_INFO_FLAGS(pass_by_ref, 0, 0)), default_value },
150 #define ZEND_ARG_VARIADIC_OBJ_TYPE_MASK(pass_by_ref, name, class_name, type_mask) \
151 	{ #name, ZEND_TYPE_INIT_CLASS_CONST_MASK(#class_name, type_mask | _ZEND_ARG_INFO_FLAGS(pass_by_ref, 1, 0)), NULL },
152 
153 /* Arginfo structures with object type information */
154 #define ZEND_ARG_OBJ_INFO(pass_by_ref, name, class_name, allow_null) \
155 	{ #name, ZEND_TYPE_INIT_CLASS_CONST(#class_name, allow_null, _ZEND_ARG_INFO_FLAGS(pass_by_ref, 0, 0)), NULL },
156 #define ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, class_name, allow_null, default_value) \
157 	{ #name, ZEND_TYPE_INIT_CLASS_CONST(#class_name, allow_null, _ZEND_ARG_INFO_FLAGS(pass_by_ref, 0, 0)), default_value },
158 #define ZEND_ARG_VARIADIC_OBJ_INFO(pass_by_ref, name, class_name, allow_null) \
159 	{ #name, ZEND_TYPE_INIT_CLASS_CONST(#class_name, allow_null, _ZEND_ARG_INFO_FLAGS(pass_by_ref, 1, 0)), NULL },
160 
161 /* Legacy arginfo structures */
162 #define ZEND_ARG_ARRAY_INFO(pass_by_ref, name, allow_null) \
163 	{ #name, ZEND_TYPE_INIT_CODE(IS_ARRAY, allow_null, _ZEND_ARG_INFO_FLAGS(pass_by_ref, 0, 0)), NULL },
164 #define ZEND_ARG_CALLABLE_INFO(pass_by_ref, name, allow_null) \
165 	{ #name, ZEND_TYPE_INIT_CODE(IS_CALLABLE, allow_null, _ZEND_ARG_INFO_FLAGS(pass_by_ref, 0, 0)), NULL },
166 
167 #define ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX2(name, return_reference, required_num_args, class_name, allow_null, is_tentative_return_type) \
168 	static const zend_internal_arg_info name[] = { \
169 		{ (const char*)(uintptr_t)(required_num_args), \
170 			ZEND_TYPE_INIT_CLASS_CONST(#class_name, allow_null, _ZEND_ARG_INFO_FLAGS(return_reference, 0, is_tentative_return_type)), NULL },
171 
172 #define ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(name, return_reference, required_num_args, class_name, allow_null) \
173 	ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX2(name, return_reference, required_num_args, class_name, allow_null, 0)
174 
175 #define ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(name, return_reference, required_num_args, class_name, allow_null) \
176 	ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX2(name, return_reference, required_num_args, class_name, allow_null, 1)
177 
178 #define ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO(name, class_name, allow_null) \
179 	ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX2(name, 0, -1, class_name, allow_null, 0)
180 
181 #define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX2(name, return_reference, required_num_args, type, is_tentative_return_type) \
182 	static const zend_internal_arg_info name[] = { \
183 		{ (const char*)(uintptr_t)(required_num_args), ZEND_TYPE_INIT_MASK(type | _ZEND_ARG_INFO_FLAGS(return_reference, 0, is_tentative_return_type)), NULL },
184 
185 #define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(name, return_reference, required_num_args, type) \
186 	ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX2(name, return_reference, required_num_args, type, 0)
187 
188 #define ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_MASK_EX(name, return_reference, required_num_args, type) \
189 	ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX2(name, return_reference, required_num_args, type, 1)
190 
191 #define ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX2(name, return_reference, required_num_args, class_name, type, is_tentative_return_type) \
192 	static const zend_internal_arg_info name[] = { \
193 		{ (const char*)(uintptr_t)(required_num_args), ZEND_TYPE_INIT_CLASS_CONST_MASK(#class_name, type | _ZEND_ARG_INFO_FLAGS(return_reference, 0, is_tentative_return_type)), NULL },
194 
195 #define ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(name, return_reference, required_num_args, class_name, type) \
196 	ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX2(name, return_reference, required_num_args, class_name, type, 0)
197 
198 #define ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_TYPE_MASK_EX(name, return_reference, required_num_args, class_name, type) \
199 	ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX2(name, return_reference, required_num_args, class_name, type, 1)
200 
201 #define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX2(name, return_reference, required_num_args, type, allow_null, is_tentative_return_type) \
202 	static const zend_internal_arg_info name[] = { \
203 		{ (const char*)(uintptr_t)(required_num_args), ZEND_TYPE_INIT_CODE(type, allow_null, _ZEND_ARG_INFO_FLAGS(return_reference, 0, is_tentative_return_type)), NULL },
204 
205 #define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
206 	ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX2(name, return_reference, required_num_args, type, allow_null, 0)
207 
208 #define ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
209 	ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX2(name, return_reference, required_num_args, type, allow_null, 1)
210 
211 #define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(name, type, allow_null) \
212 	ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX2(name, 0, -1, type, allow_null, 0)
213 
214 #define ZEND_BEGIN_ARG_INFO_EX(name, _unused, return_reference, required_num_args)	\
215 	static const zend_internal_arg_info name[] = { \
216 		{ (const char*)(uintptr_t)(required_num_args), ZEND_TYPE_INIT_NONE(_ZEND_ARG_INFO_FLAGS(return_reference, 0, 0)), NULL },
217 #define ZEND_BEGIN_ARG_INFO(name, _unused)	\
218 	ZEND_BEGIN_ARG_INFO_EX(name, {}, ZEND_RETURN_VALUE, -1)
219 #define ZEND_END_ARG_INFO()		};
220 
221 /* Name macros */
222 #define ZEND_MODULE_STARTUP_N(module)       zm_startup_##module
223 #define ZEND_MODULE_SHUTDOWN_N(module)		zm_shutdown_##module
224 #define ZEND_MODULE_ACTIVATE_N(module)		zm_activate_##module
225 #define ZEND_MODULE_DEACTIVATE_N(module)	zm_deactivate_##module
226 #define ZEND_MODULE_POST_ZEND_DEACTIVATE_N(module)	zm_post_zend_deactivate_##module
227 #define ZEND_MODULE_INFO_N(module)			zm_info_##module
228 #define ZEND_MODULE_GLOBALS_CTOR_N(module)  zm_globals_ctor_##module
229 #define ZEND_MODULE_GLOBALS_DTOR_N(module)  zm_globals_dtor_##module
230 
231 /* Declaration macros */
232 #define ZEND_MODULE_STARTUP_D(module)		zend_result ZEND_MODULE_STARTUP_N(module)(INIT_FUNC_ARGS)
233 #define ZEND_MODULE_SHUTDOWN_D(module)		zend_result ZEND_MODULE_SHUTDOWN_N(module)(SHUTDOWN_FUNC_ARGS)
234 #define ZEND_MODULE_ACTIVATE_D(module)		zend_result ZEND_MODULE_ACTIVATE_N(module)(INIT_FUNC_ARGS)
235 #define ZEND_MODULE_DEACTIVATE_D(module)	zend_result ZEND_MODULE_DEACTIVATE_N(module)(SHUTDOWN_FUNC_ARGS)
236 #define ZEND_MODULE_POST_ZEND_DEACTIVATE_D(module)	zend_result ZEND_MODULE_POST_ZEND_DEACTIVATE_N(module)(void)
237 #define ZEND_MODULE_INFO_D(module)			ZEND_COLD void ZEND_MODULE_INFO_N(module)(ZEND_MODULE_INFO_FUNC_ARGS)
238 #define ZEND_MODULE_GLOBALS_CTOR_D(module)  void ZEND_MODULE_GLOBALS_CTOR_N(module)(zend_##module##_globals *module##_globals)
239 #define ZEND_MODULE_GLOBALS_DTOR_D(module)  void ZEND_MODULE_GLOBALS_DTOR_N(module)(zend_##module##_globals *module##_globals)
240 
241 #define ZEND_GET_MODULE(name) \
242     BEGIN_EXTERN_C()\
243 	ZEND_DLEXPORT zend_module_entry *get_module(void) { return &name##_module_entry; }\
244     END_EXTERN_C()
245 
246 #define ZEND_BEGIN_MODULE_GLOBALS(module_name)		\
247 	typedef struct _zend_##module_name##_globals {
248 #define ZEND_END_MODULE_GLOBALS(module_name)		\
249 	} zend_##module_name##_globals;
250 
251 #ifdef ZTS
252 
253 #define ZEND_DECLARE_MODULE_GLOBALS(module_name)							\
254 	ts_rsrc_id module_name##_globals_id;
255 #define ZEND_EXTERN_MODULE_GLOBALS(module_name)								\
256 	extern ts_rsrc_id module_name##_globals_id;
257 #define ZEND_INIT_MODULE_GLOBALS(module_name, globals_ctor, globals_dtor)	\
258 	ts_allocate_id(&module_name##_globals_id, sizeof(zend_##module_name##_globals), (ts_allocate_ctor) globals_ctor, (ts_allocate_dtor) globals_dtor);
259 #define ZEND_MODULE_GLOBALS_ACCESSOR(module_name, v) ZEND_TSRMG(module_name##_globals_id, zend_##module_name##_globals *, v)
260 #ifdef ZEND_ENABLE_STATIC_TSRMLS_CACHE
261 #define ZEND_MODULE_GLOBALS_BULK(module_name) TSRMG_BULK_STATIC(module_name##_globals_id, zend_##module_name##_globals *)
262 #else
263 #define ZEND_MODULE_GLOBALS_BULK(module_name) TSRMG_BULK(module_name##_globals_id, zend_##module_name##_globals *)
264 #endif
265 
266 #else
267 
268 #define ZEND_DECLARE_MODULE_GLOBALS(module_name)							\
269 	zend_##module_name##_globals module_name##_globals;
270 #define ZEND_EXTERN_MODULE_GLOBALS(module_name)								\
271 	extern zend_##module_name##_globals module_name##_globals;
272 #define ZEND_INIT_MODULE_GLOBALS(module_name, globals_ctor, globals_dtor)	\
273 	globals_ctor(&module_name##_globals);
274 #define ZEND_MODULE_GLOBALS_ACCESSOR(module_name, v) (module_name##_globals.v)
275 #define ZEND_MODULE_GLOBALS_BULK(module_name) (&module_name##_globals)
276 
277 #endif
278 
279 #define INIT_CLASS_ENTRY(class_container, class_name, functions) \
280 	INIT_CLASS_ENTRY_EX(class_container, class_name, strlen(class_name), functions)
281 
282 #define INIT_CLASS_ENTRY_EX(class_container, class_name, class_name_len, functions) \
283 	{															\
284 		memset(&class_container, 0, sizeof(zend_class_entry)); \
285 		class_container.name = zend_string_init_interned(class_name, class_name_len, 1); \
286 		class_container.default_object_handlers = &std_object_handlers;	\
287 		class_container.info.internal.builtin_functions = functions;	\
288 	}
289 
290 #define INIT_CLASS_ENTRY_INIT_METHODS(class_container, functions) \
291 	{															\
292 		class_container.default_object_handlers = &std_object_handlers;	\
293 		class_container.constructor = NULL;						\
294 		class_container.destructor = NULL;						\
295 		class_container.clone = NULL;							\
296 		class_container.serialize = NULL;						\
297 		class_container.unserialize = NULL;						\
298 		class_container.create_object = NULL;					\
299 		class_container.get_static_method = NULL;				\
300 		class_container.__call = NULL;							\
301 		class_container.__callstatic = NULL;					\
302 		class_container.__tostring = NULL;						\
303 		class_container.__get = NULL;							\
304 		class_container.__set = NULL;							\
305 		class_container.__unset = NULL;							\
306 		class_container.__isset = NULL;							\
307 		class_container.__debugInfo = NULL;						\
308 		class_container.__serialize = NULL;						\
309 		class_container.__unserialize = NULL;					\
310 		class_container.parent = NULL;							\
311 		class_container.num_interfaces = 0;						\
312 		class_container.trait_names = NULL;						\
313 		class_container.num_traits = 0;							\
314 		class_container.trait_aliases = NULL;					\
315 		class_container.trait_precedences = NULL;				\
316 		class_container.interfaces = NULL;						\
317 		class_container.get_iterator = NULL;					\
318 		class_container.iterator_funcs_ptr = NULL;				\
319 		class_container.arrayaccess_funcs_ptr = NULL;			\
320 		class_container.info.internal.module = NULL;			\
321 		class_container.info.internal.builtin_functions = functions;	\
322 	}
323 
324 
325 #define INIT_NS_CLASS_ENTRY(class_container, ns, class_name, functions) \
326 	INIT_CLASS_ENTRY(class_container, ZEND_NS_NAME(ns, class_name), functions)
327 
328 #define CE_STATIC_MEMBERS(ce) \
329 	((zval*)ZEND_MAP_PTR_GET((ce)->static_members_table))
330 
331 #define CE_CONSTANTS_TABLE(ce) \
332 	zend_class_constants_table(ce)
333 
334 #define CE_DEFAULT_PROPERTIES_TABLE(ce) \
335 	zend_class_default_properties_table(ce)
336 
337 #define CE_BACKED_ENUM_TABLE(ce) \
338 	zend_class_backed_enum_table(ce)
339 
340 #define ZEND_FCI_INITIALIZED(fci) ((fci).size != 0)
341 #define ZEND_FCC_INITIALIZED(fcc) ((fcc).function_handler != NULL)
342 
343 ZEND_API int zend_next_free_module(void);
344 
345 BEGIN_EXTERN_C()
346 ZEND_API zend_result zend_get_parameters_array_ex(uint32_t param_count, zval *argument_array);
347 
348 /* internal function to efficiently copy parameters when executing __call() */
349 ZEND_API zend_result zend_copy_parameters_array(uint32_t param_count, zval *argument_array);
350 
351 #define zend_get_parameters_array(ht, param_count, argument_array) \
352 	zend_get_parameters_array_ex(param_count, argument_array)
353 #define zend_parse_parameters_none() \
354 	(EXPECTED(ZEND_NUM_ARGS() == 0) ? SUCCESS : (zend_wrong_parameters_none_error(), FAILURE))
355 #define zend_parse_parameters_none_throw() \
356 	zend_parse_parameters_none()
357 
358 /* Parameter parsing API -- andrei */
359 
360 #define ZEND_PARSE_PARAMS_THROW 0 /* No longer used, zpp always uses exceptions */
361 #define ZEND_PARSE_PARAMS_QUIET (1<<1)
362 ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec, ...);
363 ZEND_API zend_result zend_parse_parameters_ex(int flags, uint32_t num_args, const char *type_spec, ...);
364 /* NOTE: This must have at least one value in __VA_ARGS__ for the expression to be valid */
365 #define zend_parse_parameters_throw(num_args, ...) \
366 	zend_parse_parameters(num_args, __VA_ARGS__)
367 ZEND_API const char *zend_zval_type_name(const zval *arg);
368 ZEND_API const char *zend_zval_value_name(const zval *arg);
369 ZEND_API zend_string *zend_zval_get_legacy_type(const zval *arg);
370 
371 ZEND_API zend_result zend_parse_method_parameters(uint32_t num_args, zval *this_ptr, const char *type_spec, ...);
372 ZEND_API zend_result zend_parse_method_parameters_ex(int flags, uint32_t num_args, zval *this_ptr, const char *type_spec, ...);
373 
374 ZEND_API zend_result zend_parse_parameter(int flags, uint32_t arg_num, zval *arg, const char *spec, ...);
375 
376 /* End of parameter parsing API -- andrei */
377 
378 ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type);
379 ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table);
380 ZEND_API zend_result zend_startup_module(zend_module_entry *module_entry);
381 ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module_entry);
382 ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module, int module_type);
383 ZEND_API zend_result zend_startup_module_ex(zend_module_entry *module);
384 ZEND_API void zend_startup_modules(void);
385 ZEND_API void zend_collect_module_handlers(void);
386 ZEND_API void zend_destroy_modules(void);
387 ZEND_API void zend_check_magic_method_implementation(
388 		const zend_class_entry *ce, const zend_function *fptr, zend_string *lcname, int error_type);
389 ZEND_API void zend_add_magic_method(zend_class_entry *ce, zend_function *fptr, zend_string *lcname);
390 
391 ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *class_entry);
392 ZEND_API zend_class_entry *zend_register_internal_class_ex(zend_class_entry *class_entry, zend_class_entry *parent_ce);
393 ZEND_API zend_class_entry *zend_register_internal_interface(zend_class_entry *orig_class_entry);
394 ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...);
395 
396 ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce, bool persistent);
397 
zend_register_class_alias(const char * name,zend_class_entry * ce)398 static zend_always_inline zend_result zend_register_class_alias(const char *name, zend_class_entry *ce) {
399 	return zend_register_class_alias_ex(name, strlen(name), ce, 1);
400 }
401 #define zend_register_ns_class_alias(ns, name, ce) \
402 	zend_register_class_alias_ex(ZEND_NS_NAME(ns, name), sizeof(ZEND_NS_NAME(ns, name))-1, ce, 1)
403 
404 ZEND_API void zend_disable_functions(const char *function_list);
405 ZEND_API zend_result zend_disable_class(const char *class_name, size_t class_name_length);
406 
407 ZEND_API ZEND_COLD void zend_wrong_param_count(void);
408 ZEND_API ZEND_COLD void zend_wrong_property_read(zval *object, zval *property);
409 
410 #define IS_CALLABLE_CHECK_SYNTAX_ONLY (1<<0)
411 #define IS_CALLABLE_SUPPRESS_DEPRECATIONS (1<<1)
412 
413 ZEND_API void zend_release_fcall_info_cache(zend_fcall_info_cache *fcc);
414 ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *object);
415 ZEND_API zend_string *zend_get_callable_name(zval *callable);
416 ZEND_API bool zend_is_callable_at_frame(
417 		zval *callable, zend_object *object, zend_execute_data *frame,
418 		uint32_t check_flags, zend_fcall_info_cache *fcc, char **error);
419 ZEND_API bool zend_is_callable_ex(zval *callable, zend_object *object, uint32_t check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error);
420 ZEND_API bool zend_is_callable(zval *callable, uint32_t check_flags, zend_string **callable_name);
421 ZEND_API bool zend_make_callable(zval *callable, zend_string **callable_name);
422 ZEND_API const char *zend_get_module_version(const char *module_name);
423 ZEND_API zend_result zend_get_module_started(const char *module_name);
424 
425 ZEND_API zend_property_info *zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_type type);
426 
427 ZEND_API void zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment);
428 ZEND_API void zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type);
429 ZEND_API void zend_declare_property_null(zend_class_entry *ce, const char *name, size_t name_length, int access_type);
430 ZEND_API void zend_declare_property_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type);
431 ZEND_API void zend_declare_property_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type);
432 ZEND_API void zend_declare_property_double(zend_class_entry *ce, const char *name, size_t name_length, double value, int access_type);
433 ZEND_API void zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type);
434 ZEND_API void zend_declare_property_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_len, int access_type);
435 
436 ZEND_API zend_class_constant *zend_declare_typed_class_constant(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment, zend_type type);
437 ZEND_API zend_class_constant *zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment);
438 ZEND_API void zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value);
439 ZEND_API void zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length);
440 ZEND_API void zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value);
441 ZEND_API void zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, bool value);
442 ZEND_API void zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value);
443 ZEND_API void zend_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length);
444 ZEND_API void zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value);
445 
446 ZEND_API zend_result zend_update_class_constant(zend_class_constant *c, const zend_string *name, zend_class_entry *scope);
447 ZEND_API zend_result zend_update_class_constants(zend_class_entry *class_type);
448 ZEND_API HashTable *zend_separate_class_constants_table(zend_class_entry *class_type);
449 
zend_class_constants_table(zend_class_entry * ce)450 static zend_always_inline HashTable *zend_class_constants_table(zend_class_entry *ce) {
451 	if ((ce->ce_flags & ZEND_ACC_HAS_AST_CONSTANTS) && ZEND_MAP_PTR(ce->mutable_data)) {
452 		zend_class_mutable_data *mutable_data =
453 			(zend_class_mutable_data*)ZEND_MAP_PTR_GET_IMM(ce->mutable_data);
454 		if (mutable_data && mutable_data->constants_table) {
455 			return mutable_data->constants_table;
456 		} else {
457 			return zend_separate_class_constants_table(ce);
458 		}
459 	} else {
460 		return &ce->constants_table;
461 	}
462 }
463 
zend_class_default_properties_table(zend_class_entry * ce)464 static zend_always_inline zval *zend_class_default_properties_table(zend_class_entry *ce) {
465 	if ((ce->ce_flags & ZEND_ACC_HAS_AST_PROPERTIES) && ZEND_MAP_PTR(ce->mutable_data)) {
466 		zend_class_mutable_data *mutable_data =
467 			(zend_class_mutable_data*)ZEND_MAP_PTR_GET_IMM(ce->mutable_data);
468 		return mutable_data->default_properties_table;
469 	} else {
470 		return ce->default_properties_table;
471 	}
472 }
473 
zend_class_set_backed_enum_table(zend_class_entry * ce,HashTable * backed_enum_table)474 static zend_always_inline void zend_class_set_backed_enum_table(zend_class_entry *ce, HashTable *backed_enum_table)
475 {
476 	if (ZEND_MAP_PTR(ce->mutable_data) && ce->type == ZEND_USER_CLASS) {
477 		zend_class_mutable_data *mutable_data = (zend_class_mutable_data*)ZEND_MAP_PTR_GET_IMM(ce->mutable_data);
478 		mutable_data->backed_enum_table = backed_enum_table;
479 	} else {
480 		ce->backed_enum_table = backed_enum_table;
481 	}
482 }
483 
zend_class_backed_enum_table(zend_class_entry * ce)484 static zend_always_inline HashTable *zend_class_backed_enum_table(zend_class_entry *ce)
485 {
486 	if (ZEND_MAP_PTR(ce->mutable_data) && ce->type == ZEND_USER_CLASS) {
487 		zend_class_mutable_data *mutable_data = (zend_class_mutable_data*)ZEND_MAP_PTR_GET_IMM(ce->mutable_data);
488 		return mutable_data->backed_enum_table;
489 	} else {
490 		return ce->backed_enum_table;
491 	}
492 }
493 
494 ZEND_API void zend_update_property_ex(zend_class_entry *scope, zend_object *object, zend_string *name, zval *value);
495 ZEND_API void zend_update_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zval *value);
496 ZEND_API void zend_update_property_null(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length);
497 ZEND_API void zend_update_property_bool(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value);
498 ZEND_API void zend_update_property_long(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value);
499 ZEND_API void zend_update_property_double(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, double value);
500 ZEND_API void zend_update_property_str(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_string *value);
501 ZEND_API void zend_update_property_string(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, const char *value);
502 ZEND_API void zend_update_property_stringl(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, const char *value, size_t value_length);
503 ZEND_API void zend_unset_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length);
504 
505 ZEND_API zend_result zend_update_static_property_ex(zend_class_entry *scope, zend_string *name, zval *value);
506 ZEND_API zend_result zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value);
507 ZEND_API zend_result zend_update_static_property_null(zend_class_entry *scope, const char *name, size_t name_length);
508 ZEND_API zend_result zend_update_static_property_bool(zend_class_entry *scope, const char *name, size_t name_length, zend_long value);
509 ZEND_API zend_result zend_update_static_property_long(zend_class_entry *scope, const char *name, size_t name_length, zend_long value);
510 ZEND_API zend_result zend_update_static_property_double(zend_class_entry *scope, const char *name, size_t name_length, double value);
511 ZEND_API zend_result zend_update_static_property_string(zend_class_entry *scope, const char *name, size_t name_length, const char *value);
512 ZEND_API zend_result zend_update_static_property_stringl(zend_class_entry *scope, const char *name, size_t name_length, const char *value, size_t value_length);
513 
514 ZEND_API zval *zend_read_property_ex(zend_class_entry *scope, zend_object *object, zend_string *name, bool silent, zval *rv);
515 ZEND_API zval *zend_read_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, bool silent, zval *rv);
516 
517 ZEND_API zval *zend_read_static_property_ex(zend_class_entry *scope, zend_string *name, bool silent);
518 ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, size_t name_length, bool silent);
519 
520 ZEND_API const char *zend_get_type_by_const(int type);
521 
522 #define ZEND_THIS                           (&EX(This))
523 
524 #define hasThis()							(Z_TYPE_P(ZEND_THIS) == IS_OBJECT)
525 #define getThis()							(hasThis() ? ZEND_THIS : NULL)
526 #define ZEND_IS_METHOD_CALL()				(EX(func)->common.scope != NULL)
527 
528 #define WRONG_PARAM_COUNT					ZEND_WRONG_PARAM_COUNT()
529 #define ZEND_NUM_ARGS()						EX_NUM_ARGS()
530 #define ZEND_WRONG_PARAM_COUNT()					{ zend_wrong_param_count(); return; }
531 
532 #ifndef ZEND_WIN32
533 #define DLEXPORT
534 #endif
535 
536 #define array_init(arg)				ZVAL_ARR((arg), zend_new_array(0))
537 #define array_init_size(arg, size)	ZVAL_ARR((arg), zend_new_array(size))
538 ZEND_API void object_init(zval *arg);
539 ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *ce);
540 ZEND_API zend_result object_and_properties_init(zval *arg, zend_class_entry *ce, HashTable *properties);
541 ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type);
542 ZEND_API void object_properties_init_ex(zend_object *object, HashTable *properties);
543 ZEND_API void object_properties_load(zend_object *object, HashTable *properties);
544 
545 ZEND_API void zend_merge_properties(zval *obj, HashTable *properties);
546 
547 ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n);
548 ZEND_API void add_assoc_null_ex(zval *arg, const char *key, size_t key_len);
549 ZEND_API void add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, bool b);
550 ZEND_API void add_assoc_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r);
551 ZEND_API void add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d);
552 ZEND_API void add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str);
553 ZEND_API void add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str);
554 ZEND_API void add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length);
555 ZEND_API void add_assoc_array_ex(zval *arg, const char *key, size_t key_len, zend_array *arr);
556 ZEND_API void add_assoc_object_ex(zval *arg, const char *key, size_t key_len, zend_object *obj);
557 ZEND_API void add_assoc_reference_ex(zval *arg, const char *key, size_t key_len, zend_reference *ref);
558 ZEND_API void add_assoc_zval_ex(zval *arg, const char *key, size_t key_len, zval *value);
559 
add_assoc_long(zval * arg,const char * key,zend_long n)560 static zend_always_inline void add_assoc_long(zval *arg, const char *key, zend_long n) {
561 	add_assoc_long_ex(arg, key, strlen(key), n);
562 }
add_assoc_null(zval * arg,const char * key)563 static zend_always_inline void add_assoc_null(zval *arg, const char *key) {
564 	add_assoc_null_ex(arg, key, strlen(key));
565 }
add_assoc_bool(zval * arg,const char * key,bool b)566 static zend_always_inline void add_assoc_bool(zval *arg, const char *key, bool b) {
567 	add_assoc_bool_ex(arg, key, strlen(key), b);
568 }
add_assoc_resource(zval * arg,const char * key,zend_resource * r)569 static zend_always_inline void add_assoc_resource(zval *arg, const char *key, zend_resource *r) {
570 	add_assoc_resource_ex(arg, key, strlen(key), r);
571 }
add_assoc_double(zval * arg,const char * key,double d)572 static zend_always_inline void add_assoc_double(zval *arg, const char *key, double d) {
573 	add_assoc_double_ex(arg, key, strlen(key), d);
574 }
add_assoc_str(zval * arg,const char * key,zend_string * str)575 static zend_always_inline void add_assoc_str(zval *arg, const char *key, zend_string *str) {
576 	add_assoc_str_ex(arg, key, strlen(key), str);
577 }
add_assoc_string(zval * arg,const char * key,const char * str)578 static zend_always_inline void add_assoc_string(zval *arg, const char *key, const char *str) {
579 	add_assoc_string_ex(arg, key, strlen(key), str);
580 }
add_assoc_stringl(zval * arg,const char * key,const char * str,size_t length)581 static zend_always_inline void add_assoc_stringl(zval *arg, const char *key, const char *str, size_t length) {
582 	add_assoc_stringl_ex(arg, key, strlen(key), str, length);
583 }
add_assoc_array(zval * arg,const char * key,zend_array * arr)584 static zend_always_inline void add_assoc_array(zval *arg, const char *key, zend_array *arr) {
585 	add_assoc_array_ex(arg, key, strlen(key), arr);
586 }
add_assoc_object(zval * arg,const char * key,zend_object * obj)587 static zend_always_inline void add_assoc_object(zval *arg, const char *key, zend_object *obj) {
588 	add_assoc_object_ex(arg, key, strlen(key), obj);
589 }
add_assoc_reference(zval * arg,const char * key,zend_reference * ref)590 static zend_always_inline void add_assoc_reference(zval *arg, const char *key, zend_reference *ref) {
591 	add_assoc_reference_ex(arg, key, strlen(key), ref);
592 }
add_assoc_zval(zval * arg,const char * key,zval * value)593 static zend_always_inline void add_assoc_zval(zval *arg, const char *key, zval *value) {
594 	add_assoc_zval_ex(arg, key, strlen(key), value);
595 }
596 
597 ZEND_API void add_index_long(zval *arg, zend_ulong index, zend_long n);
598 ZEND_API void add_index_null(zval *arg, zend_ulong index);
599 ZEND_API void add_index_bool(zval *arg, zend_ulong index, bool b);
600 ZEND_API void add_index_resource(zval *arg, zend_ulong index, zend_resource *r);
601 ZEND_API void add_index_double(zval *arg, zend_ulong index, double d);
602 ZEND_API void add_index_str(zval *arg, zend_ulong index, zend_string *str);
603 ZEND_API void add_index_string(zval *arg, zend_ulong index, const char *str);
604 ZEND_API void add_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length);
605 ZEND_API void add_index_array(zval *arg, zend_ulong index, zend_array *arr);
606 ZEND_API void add_index_object(zval *arg, zend_ulong index, zend_object *obj);
607 ZEND_API void add_index_reference(zval *arg, zend_ulong index, zend_reference *ref);
608 
add_index_zval(zval * arg,zend_ulong index,zval * value)609 static zend_always_inline zend_result add_index_zval(zval *arg, zend_ulong index, zval *value)
610 {
611 	return zend_hash_index_update(Z_ARRVAL_P(arg), index, value) ? SUCCESS : FAILURE;
612 }
613 
614 ZEND_API zend_result add_next_index_long(zval *arg, zend_long n);
615 ZEND_API zend_result add_next_index_null(zval *arg);
616 ZEND_API zend_result add_next_index_bool(zval *arg, bool b);
617 ZEND_API zend_result add_next_index_resource(zval *arg, zend_resource *r);
618 ZEND_API zend_result add_next_index_double(zval *arg, double d);
619 ZEND_API zend_result add_next_index_str(zval *arg, zend_string *str);
620 ZEND_API zend_result add_next_index_string(zval *arg, const char *str);
621 ZEND_API zend_result add_next_index_stringl(zval *arg, const char *str, size_t length);
622 ZEND_API zend_result add_next_index_array(zval *arg, zend_array *arr);
623 ZEND_API zend_result add_next_index_object(zval *arg, zend_object *obj);
624 ZEND_API zend_result add_next_index_reference(zval *arg, zend_reference *ref);
625 
add_next_index_zval(zval * arg,zval * value)626 static zend_always_inline zend_result add_next_index_zval(zval *arg, zval *value)
627 {
628 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), value) ? SUCCESS : FAILURE;
629 }
630 
631 ZEND_API zend_result array_set_zval_key(HashTable *ht, zval *key, zval *value);
632 
633 ZEND_API void add_property_long_ex(zval *arg, const char *key, size_t key_len, zend_long l);
634 ZEND_API void add_property_null_ex(zval *arg, const char *key, size_t key_len);
635 ZEND_API void add_property_bool_ex(zval *arg, const char *key, size_t key_len, zend_long b);
636 ZEND_API void add_property_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r);
637 ZEND_API void add_property_double_ex(zval *arg, const char *key, size_t key_len, double d);
638 ZEND_API void add_property_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str);
639 ZEND_API void add_property_string_ex(zval *arg, const char *key, size_t key_len, const char *str);
640 ZEND_API void add_property_stringl_ex(zval *arg, const char *key, size_t key_len,  const char *str, size_t length);
641 ZEND_API void add_property_array_ex(zval *arg, const char *key, size_t key_len, zend_array *arr);
642 ZEND_API void add_property_object_ex(zval *arg, const char *key, size_t key_len, zend_object *obj);
643 ZEND_API void add_property_reference_ex(zval *arg, const char *key, size_t key_len, zend_reference *ref);
644 ZEND_API void add_property_zval_ex(zval *arg, const char *key, size_t key_len, zval *value);
645 
add_property_long(zval * arg,const char * key,zend_long n)646 static zend_always_inline void add_property_long(zval *arg, const char *key, zend_long n) {
647 	add_property_long_ex(arg, key, strlen(key), n);
648 }
add_property_null(zval * arg,const char * key)649 static zend_always_inline void add_property_null(zval *arg, const char *key) {
650 	add_property_null_ex(arg, key, strlen(key));
651 }
add_property_bool(zval * arg,const char * key,bool b)652 static zend_always_inline void add_property_bool(zval *arg, const char *key, bool b) {
653 	add_property_bool_ex(arg, key, strlen(key), b);
654 }
add_property_resource(zval * arg,const char * key,zend_resource * r)655 static zend_always_inline void add_property_resource(zval *arg, const char *key, zend_resource *r) {
656 	add_property_resource_ex(arg, key, strlen(key), r);
657 }
add_property_double(zval * arg,const char * key,double d)658 static zend_always_inline void add_property_double(zval *arg, const char *key, double d) {
659 	add_property_double_ex(arg, key, strlen(key), d);
660 }
add_property_str(zval * arg,const char * key,zend_string * str)661 static zend_always_inline void add_property_str(zval *arg, const char *key, zend_string *str) {
662 	add_property_str_ex(arg, key, strlen(key), str);
663 }
add_property_string(zval * arg,const char * key,const char * str)664 static zend_always_inline void add_property_string(zval *arg, const char *key, const char *str) {
665 	add_property_string_ex(arg, key, strlen(key), str);
666 }
add_property_stringl(zval * arg,const char * key,const char * str,size_t length)667 static zend_always_inline void add_property_stringl(zval *arg, const char *key, const char *str, size_t length) {
668 	add_property_stringl_ex(arg, key, strlen(key), str, length);
669 }
add_property_array(zval * arg,const char * key,zend_array * arr)670 static zend_always_inline void add_property_array(zval *arg, const char *key, zend_array *arr) {
671 	add_property_array_ex(arg, key, strlen(key), arr);
672 }
add_property_object(zval * arg,const char * key,zend_object * obj)673 static zend_always_inline void add_property_object(zval *arg, const char *key, zend_object *obj) {
674 	add_property_object_ex(arg, key, strlen(key), obj);
675 }
add_property_reference(zval * arg,const char * key,zend_reference * ref)676 static zend_always_inline void add_property_reference(zval *arg, const char *key, zend_reference *ref) {
677 	add_property_reference_ex(arg, key, strlen(key), ref);
678 }
add_property_zval(zval * arg,const char * key,zval * value)679 static zend_always_inline void add_property_zval(zval *arg, const char *key, zval *value) {
680 	add_property_zval_ex(arg, key, strlen(key), value);
681 }
682 
683 ZEND_API zend_result _call_user_function_impl(zval *object, zval *function_name, zval *retval_ptr, uint32_t param_count, zval params[], HashTable *named_params);
684 
685 #define call_user_function(function_table, object, function_name, retval_ptr, param_count, params) \
686 	_call_user_function_impl(object, function_name, retval_ptr, param_count, params, NULL)
687 
688 #define call_user_function_named(function_table, object, function_name, retval_ptr, param_count, params, named_params) \
689 	_call_user_function_impl(object, function_name, retval_ptr, param_count, params, named_params)
690 
691 ZEND_API extern const zend_fcall_info empty_fcall_info;
692 ZEND_API extern const zend_fcall_info_cache empty_fcall_info_cache;
693 
694 /** Build zend_call_info/cache from a zval*
695  *
696  * Caller is responsible to provide a return value (fci->retval), otherwise the we will crash.
697  * In order to pass parameters the following members need to be set:
698  * fci->param_count = 0;
699  * fci->params = NULL;
700  * The callable_name argument may be NULL.
701  */
702 ZEND_API zend_result zend_fcall_info_init(zval *callable, uint32_t check_flags, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_string **callable_name, char **error);
703 
704 /** Clear arguments connected with zend_fcall_info *fci
705  * If free_mem is not zero then the params array gets free'd as well
706  */
707 ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, bool free_mem);
708 
709 /** Save current arguments from zend_fcall_info *fci
710  * params array will be set to NULL
711  */
712 ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, uint32_t *param_count, zval **params);
713 
714 /** Free arguments connected with zend_fcall_info *fci and set back saved ones.
715  */
716 ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, uint32_t param_count, zval *params);
717 
718 /** Set or clear the arguments in the zend_call_info struct taking care of
719  * refcount. If args is NULL and arguments are set then those are cleared.
720  */
721 ZEND_API zend_result zend_fcall_info_args(zend_fcall_info *fci, zval *args);
722 ZEND_API zend_result zend_fcall_info_args_ex(zend_fcall_info *fci, zend_function *func, zval *args);
723 
724 /** Set arguments in the zend_fcall_info struct taking care of refcount.
725  * If argc is 0 the arguments which are set will be cleared, else pass
726  * a variable amount of zval** arguments.
727  */
728 ZEND_API void zend_fcall_info_argp(zend_fcall_info *fci, uint32_t argc, zval *argv);
729 
730 /** Set arguments in the zend_fcall_info struct taking care of refcount.
731  * If argc is 0 the arguments which are set will be cleared, else pass
732  * a variable amount of zval** arguments.
733  */
734 ZEND_API void zend_fcall_info_argv(zend_fcall_info *fci, uint32_t argc, va_list *argv);
735 
736 /** Set arguments in the zend_fcall_info struct taking care of refcount.
737  * If argc is 0 the arguments which are set will be cleared, else pass
738  * a variable amount of zval** arguments.
739  */
740 ZEND_API void zend_fcall_info_argn(zend_fcall_info *fci, uint32_t argc, ...);
741 
742 /** Call a function using information created by zend_fcall_info_init()/args().
743  * If args is given then those replace the argument info in fci is temporarily.
744  */
745 ZEND_API zend_result zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval *retval, zval *args);
746 
747 /* Zend FCC API to store and handle PHP userland functions */
zend_fcc_equals(const zend_fcall_info_cache * a,const zend_fcall_info_cache * b)748 static zend_always_inline bool zend_fcc_equals(const zend_fcall_info_cache* a, const zend_fcall_info_cache* b)
749 {
750 	if (UNEXPECTED((a->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) &&
751 		(b->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))) {
752 		return a->object == b->object
753 			&& a->calling_scope == b->calling_scope
754 			&& a->closure == b->closure
755 			&& zend_string_equals(a->function_handler->common.function_name, b->function_handler->common.function_name)
756 		;
757 	}
758 	return a->function_handler == b->function_handler
759 		&& a->object == b->object
760 		&& a->calling_scope == b->calling_scope
761 		&& a->closure == b->closure
762 	;
763 }
764 
zend_fcc_addref(zend_fcall_info_cache * fcc)765 static zend_always_inline void zend_fcc_addref(zend_fcall_info_cache *fcc)
766 {
767 	ZEND_ASSERT(ZEND_FCC_INITIALIZED(*fcc) && "FCC Not initialized, possibly refetch trampoline freed by ZPP?");
768 	/* If the cached trampoline is set, free it */
769 	if (UNEXPECTED(fcc->function_handler == &EG(trampoline))) {
770 		zend_function *copy = (zend_function*)emalloc(sizeof(zend_function));
771 
772 		memcpy(copy, fcc->function_handler, sizeof(zend_function));
773 		fcc->function_handler->common.function_name = NULL;
774 		fcc->function_handler = copy;
775 	}
776 	if (fcc->object) {
777 		GC_ADDREF(fcc->object);
778 	}
779 	if (fcc->closure) {
780 		GC_ADDREF(fcc->closure);
781 	}
782 }
783 
zend_fcc_dup(zend_fcall_info_cache * dest,const zend_fcall_info_cache * src)784 static zend_always_inline void zend_fcc_dup(/* restrict */ zend_fcall_info_cache *dest, const zend_fcall_info_cache *src)
785 {
786 	memcpy(dest, src, sizeof(zend_fcall_info_cache));
787 	zend_fcc_addref(dest);
788 }
789 
zend_fcc_dtor(zend_fcall_info_cache * fcc)790 static zend_always_inline void zend_fcc_dtor(zend_fcall_info_cache *fcc)
791 {
792 	ZEND_ASSERT(fcc->function_handler);
793 	if (fcc->object) {
794 		OBJ_RELEASE(fcc->object);
795 	}
796 	/* Need to free potential trampoline (__call/__callStatic) copied function handler before releasing the closure */
797 	zend_release_fcall_info_cache(fcc);
798 	if (fcc->closure) {
799 		OBJ_RELEASE(fcc->closure);
800 	}
801 	memcpy(fcc, &empty_fcall_info_cache, sizeof(zend_fcall_info_cache));
802 }
803 
804 ZEND_API void zend_get_callable_zval_from_fcc(const zend_fcall_info_cache *fcc, zval *callable);
805 
806 /* Moved out of zend_gc.h because zend_fcall_info_cache is an unknown type in that header */
zend_get_gc_buffer_add_fcc(zend_get_gc_buffer * gc_buffer,zend_fcall_info_cache * fcc)807 static zend_always_inline void zend_get_gc_buffer_add_fcc(zend_get_gc_buffer *gc_buffer, zend_fcall_info_cache *fcc)
808 {
809 	ZEND_ASSERT(ZEND_FCC_INITIALIZED(*fcc));
810 	if (fcc->object) {
811 		zend_get_gc_buffer_add_obj(gc_buffer, fcc->object);
812 	}
813 	if (fcc->closure) {
814 		zend_get_gc_buffer_add_obj(gc_buffer, fcc->closure);
815 	}
816 }
817 
818 /* Can only return FAILURE if EG(active) is false during late engine shutdown.
819  * If the call or call setup throws, EG(exception) will be set and the retval
820  * will be UNDEF. Otherwise, the retval will be a non-UNDEF value. */
821 ZEND_API zend_result zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache);
822 
823 /* Call the FCI/FCC pair while setting the call return value to the passed zval*. */
zend_call_function_with_return_value(zend_fcall_info * fci,zend_fcall_info_cache * fci_cache,zval * retval)824 static zend_always_inline zend_result zend_call_function_with_return_value(
825 	zend_fcall_info *fci, zend_fcall_info_cache *fci_cache, zval *retval)
826 {
827 	ZEND_ASSERT(retval && "Use zend_call_function() directly if not providing a retval");
828 	fci->retval = retval;
829 	return zend_call_function(fci, fci_cache);
830 }
831 
832 /* Call the provided zend_function with the given params.
833  * If retval_ptr is NULL, the return value is discarded.
834  * If object is NULL, this must be a free function or static call.
835  * called_scope must be provided for instance and static method calls. */
836 ZEND_API void zend_call_known_function(
837 		zend_function *fn, zend_object *object, zend_class_entry *called_scope, zval *retval_ptr,
838 		uint32_t param_count, zval *params, HashTable *named_params);
839 
zend_call_known_fcc(zend_fcall_info_cache * fcc,zval * retval_ptr,uint32_t param_count,zval * params,HashTable * named_params)840 static zend_always_inline void zend_call_known_fcc(
841 	zend_fcall_info_cache *fcc, zval *retval_ptr, uint32_t param_count, zval *params, HashTable *named_params)
842 {
843 	zend_function *func = fcc->function_handler;
844 	/* Need to copy trampolines as they get released after they are called */
845 	if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
846 		func = (zend_function*) emalloc(sizeof(zend_function));
847 		memcpy(func, fcc->function_handler, sizeof(zend_function));
848 		zend_string_addref(func->op_array.function_name);
849 	}
850 	zend_call_known_function(func, fcc->object, fcc->called_scope, retval_ptr, param_count, params, named_params);
851 }
852 
853 /* Call the provided zend_function instance method on an object. */
zend_call_known_instance_method(zend_function * fn,zend_object * object,zval * retval_ptr,uint32_t param_count,zval * params)854 static zend_always_inline void zend_call_known_instance_method(
855 		zend_function *fn, zend_object *object, zval *retval_ptr,
856 		uint32_t param_count, zval *params)
857 {
858 	zend_call_known_function(fn, object, object->ce, retval_ptr, param_count, params, NULL);
859 }
860 
zend_call_known_instance_method_with_0_params(zend_function * fn,zend_object * object,zval * retval_ptr)861 static zend_always_inline void zend_call_known_instance_method_with_0_params(
862 		zend_function *fn, zend_object *object, zval *retval_ptr)
863 {
864 	zend_call_known_instance_method(fn, object, retval_ptr, 0, NULL);
865 }
866 
zend_call_known_instance_method_with_1_params(zend_function * fn,zend_object * object,zval * retval_ptr,zval * param)867 static zend_always_inline void zend_call_known_instance_method_with_1_params(
868 		zend_function *fn, zend_object *object, zval *retval_ptr, zval *param)
869 {
870 	zend_call_known_instance_method(fn, object, retval_ptr, 1, param);
871 }
872 
873 ZEND_API void zend_call_known_instance_method_with_2_params(
874 		zend_function *fn, zend_object *object, zval *retval_ptr, zval *param1, zval *param2);
875 
876 /* Call method if it exists. Return FAILURE if method does not exist or call failed.
877  * If FAILURE is returned, retval will be UNDEF. As such, destroying retval unconditionally
878  * is legal. */
879 ZEND_API zend_result zend_call_method_if_exists(
880 		zend_object *object, zend_string *method_name, zval *retval,
881 		uint32_t param_count, zval *params);
882 
883 ZEND_API zend_result zend_set_hash_symbol(zval *symbol, const char *name, size_t name_length, bool is_ref, int num_symbol_tables, ...);
884 
885 ZEND_API zend_result zend_delete_global_variable(zend_string *name);
886 
887 ZEND_API zend_array *zend_rebuild_symbol_table(void);
888 ZEND_API void zend_attach_symbol_table(zend_execute_data *execute_data);
889 ZEND_API void zend_detach_symbol_table(zend_execute_data *execute_data);
890 ZEND_API zend_result zend_set_local_var(zend_string *name, zval *value, bool force);
891 ZEND_API zend_result zend_set_local_var_str(const char *name, size_t len, zval *value, bool force);
892 
zend_forbid_dynamic_call(void)893 static zend_always_inline zend_result zend_forbid_dynamic_call(void)
894 {
895 	zend_execute_data *ex = EG(current_execute_data);
896 	ZEND_ASSERT(ex != NULL && ex->func != NULL);
897 
898 	if (ZEND_CALL_INFO(ex) & ZEND_CALL_DYNAMIC) {
899 		zend_string *function_or_method_name = get_active_function_or_method_name();
900 		zend_throw_error(NULL, "Cannot call %.*s() dynamically",
901 			(int) ZSTR_LEN(function_or_method_name), ZSTR_VAL(function_or_method_name));
902 		zend_string_release(function_or_method_name);
903 		return FAILURE;
904 	}
905 
906 	return SUCCESS;
907 }
908 
909 ZEND_API ZEND_COLD const char *zend_get_object_type_case(const zend_class_entry *ce, bool upper_case);
910 
zend_get_object_type(const zend_class_entry * ce)911 static zend_always_inline const char *zend_get_object_type(const zend_class_entry *ce)
912 {
913 	return zend_get_object_type_case(ce, false);
914 }
915 
zend_get_object_type_uc(const zend_class_entry * ce)916 static zend_always_inline const char *zend_get_object_type_uc(const zend_class_entry *ce)
917 {
918 	return zend_get_object_type_case(ce, true);
919 }
920 
921 ZEND_API bool zend_is_iterable(const zval *iterable);
922 
923 ZEND_API bool zend_is_countable(const zval *countable);
924 
925 ZEND_API zend_result zend_get_default_from_internal_arg_info(
926 		zval *default_value_zval, zend_internal_arg_info *arg_info);
927 
END_EXTERN_C()928 END_EXTERN_C()
929 
930 #if ZEND_DEBUG
931 #define CHECK_ZVAL_STRING(str) \
932 	ZEND_ASSERT(ZSTR_VAL(str)[ZSTR_LEN(str)] == '\0' && "String is not null-terminated");
933 #else
934 #define CHECK_ZVAL_STRING(z)
935 #endif
936 
937 static zend_always_inline bool zend_str_has_nul_byte(const zend_string *str)
938 {
939 	return ZSTR_LEN(str) != strlen(ZSTR_VAL(str));
940 }
zend_char_has_nul_byte(const char * s,size_t known_length)941 static zend_always_inline bool zend_char_has_nul_byte(const char *s, size_t known_length)
942 {
943 	return known_length != strlen(s);
944 }
945 
946 /* Compatibility with PHP 8.1 and below */
947 #define CHECK_ZVAL_NULL_PATH(p) zend_str_has_nul_byte(Z_STR_P(p))
948 #define CHECK_NULL_PATH(p, l) zend_char_has_nul_byte(p, l)
949 
950 #define ZVAL_STRINGL(z, s, l) do {				\
951 		ZVAL_NEW_STR(z, zend_string_init(s, l, 0));		\
952 	} while (0)
953 
954 #define ZVAL_STRING(z, s) do {					\
955 		const char *_s = (s);					\
956 		ZVAL_STRINGL(z, _s, strlen(_s));		\
957 	} while (0)
958 
959 #define ZVAL_EMPTY_STRING(z) do {				\
960 		ZVAL_INTERNED_STR(z, ZSTR_EMPTY_ALLOC());		\
961 	} while (0)
962 
963 #define ZVAL_PSTRINGL(z, s, l) do {				\
964 		ZVAL_NEW_STR(z, zend_string_init(s, l, 1));		\
965 	} while (0)
966 
967 #define ZVAL_PSTRING(z, s) do {					\
968 		const char *_s = (s);					\
969 		ZVAL_PSTRINGL(z, _s, strlen(_s));		\
970 	} while (0)
971 
972 #define ZVAL_EMPTY_PSTRING(z) do {				\
973 		ZVAL_PSTRINGL(z, "", 0);				\
974 	} while (0)
975 
976 #define ZVAL_CHAR(z, c)  do {		            \
977 		char _c = (c);                          \
978 		ZVAL_INTERNED_STR(z, ZSTR_CHAR((zend_uchar) _c));	\
979 	} while (0)
980 
981 #define ZVAL_STRINGL_FAST(z, s, l) do {			\
982 		ZVAL_STR(z, zend_string_init_fast(s, l));	\
983 	} while (0)
984 
985 #define ZVAL_STRING_FAST(z, s) do {				\
986 		const char *_s = (s);					\
987 		ZVAL_STRINGL_FAST(z, _s, strlen(_s));	\
988 	} while (0)
989 
990 #define ZVAL_ZVAL(z, zv, copy, dtor) do {		\
991 		zval *__z = (z);						\
992 		zval *__zv = (zv);						\
993 		if (EXPECTED(!Z_ISREF_P(__zv))) {		\
994 			if (copy && !dtor) {				\
995 				ZVAL_COPY(__z, __zv);			\
996 			} else {							\
997 				ZVAL_COPY_VALUE(__z, __zv);		\
998 			}									\
999 		} else {								\
1000 			ZVAL_COPY(__z, Z_REFVAL_P(__zv));	\
1001 			if (dtor || !copy) {				\
1002 				zval_ptr_dtor(__zv);			\
1003 			}									\
1004 		}										\
1005 	} while (0)
1006 
1007 #define RETVAL_BOOL(b)					ZVAL_BOOL(return_value, b)
1008 #define RETVAL_NULL()					ZVAL_NULL(return_value)
1009 #define RETVAL_LONG(l)					ZVAL_LONG(return_value, l)
1010 #define RETVAL_DOUBLE(d)				ZVAL_DOUBLE(return_value, d)
1011 #define RETVAL_STR(s)					ZVAL_STR(return_value, s)
1012 #define RETVAL_INTERNED_STR(s)			ZVAL_INTERNED_STR(return_value, s)
1013 #define RETVAL_NEW_STR(s)				ZVAL_NEW_STR(return_value, s)
1014 #define RETVAL_STR_COPY(s)				ZVAL_STR_COPY(return_value, s)
1015 #define RETVAL_STRING(s)				ZVAL_STRING(return_value, s)
1016 #define RETVAL_STRINGL(s, l)			ZVAL_STRINGL(return_value, s, l)
1017 #define RETVAL_STRING_FAST(s)			ZVAL_STRING_FAST(return_value, s)
1018 #define RETVAL_STRINGL_FAST(s, l)		ZVAL_STRINGL_FAST(return_value, s, l)
1019 #define RETVAL_EMPTY_STRING()			ZVAL_EMPTY_STRING(return_value)
1020 #define RETVAL_CHAR(c)		            ZVAL_CHAR(return_value, c)
1021 #define RETVAL_RES(r)					ZVAL_RES(return_value, r)
1022 #define RETVAL_ARR(r)					ZVAL_ARR(return_value, r)
1023 #define RETVAL_EMPTY_ARRAY()			ZVAL_EMPTY_ARRAY(return_value)
1024 #define RETVAL_OBJ(r)					ZVAL_OBJ(return_value, r)
1025 #define RETVAL_OBJ_COPY(r)				ZVAL_OBJ_COPY(return_value, r)
1026 #define RETVAL_COPY(zv)					ZVAL_COPY(return_value, zv)
1027 #define RETVAL_COPY_VALUE(zv)			ZVAL_COPY_VALUE(return_value, zv)
1028 #define RETVAL_COPY_DEREF(zv)			ZVAL_COPY_DEREF(return_value, zv)
1029 #define RETVAL_ZVAL(zv, copy, dtor)		ZVAL_ZVAL(return_value, zv, copy, dtor)
1030 #define RETVAL_FALSE					ZVAL_FALSE(return_value)
1031 #define RETVAL_TRUE						ZVAL_TRUE(return_value)
1032 
1033 #define RETURN_BOOL(b)					do { RETVAL_BOOL(b); return; } while (0)
1034 #define RETURN_NULL()					do { RETVAL_NULL(); return;} while (0)
1035 #define RETURN_LONG(l)					do { RETVAL_LONG(l); return; } while (0)
1036 #define RETURN_DOUBLE(d)				do { RETVAL_DOUBLE(d); return; } while (0)
1037 #define RETURN_STR(s) 					do { RETVAL_STR(s); return; } while (0)
1038 #define RETURN_INTERNED_STR(s)			do { RETVAL_INTERNED_STR(s); return; } while (0)
1039 #define RETURN_NEW_STR(s)				do { RETVAL_NEW_STR(s); return; } while (0)
1040 #define RETURN_STR_COPY(s)				do { RETVAL_STR_COPY(s); return; } while (0)
1041 #define RETURN_STRING(s) 				do { RETVAL_STRING(s); return; } while (0)
1042 #define RETURN_STRINGL(s, l) 			do { RETVAL_STRINGL(s, l); return; } while (0)
1043 #define RETURN_STRING_FAST(s) 			do { RETVAL_STRING_FAST(s); return; } while (0)
1044 #define RETURN_STRINGL_FAST(s, l)		do { RETVAL_STRINGL_FAST(s, l); return; } while (0)
1045 #define RETURN_EMPTY_STRING() 			do { RETVAL_EMPTY_STRING(); return; } while (0)
1046 #define RETURN_CHAR(c)		            do { RETVAL_CHAR(c); return; } while (0)
1047 #define RETURN_RES(r)					do { RETVAL_RES(r); return; } while (0)
1048 #define RETURN_ARR(r)					do { RETVAL_ARR(r); return; } while (0)
1049 #define RETURN_EMPTY_ARRAY()			do { RETVAL_EMPTY_ARRAY(); return; } while (0)
1050 #define RETURN_OBJ(r)					do { RETVAL_OBJ(r); return; } while (0)
1051 #define RETURN_OBJ_COPY(r)				do { RETVAL_OBJ_COPY(r); return; } while (0)
1052 #define RETURN_COPY(zv)					do { RETVAL_COPY(zv); return; } while (0)
1053 #define RETURN_COPY_VALUE(zv)			do { RETVAL_COPY_VALUE(zv); return; } while (0)
1054 #define RETURN_COPY_DEREF(zv)			do { RETVAL_COPY_DEREF(zv); return; } while (0)
1055 #define RETURN_ZVAL(zv, copy, dtor)		do { RETVAL_ZVAL(zv, copy, dtor); return; } while (0)
1056 #define RETURN_FALSE					do { RETVAL_FALSE; return; } while (0)
1057 #define RETURN_TRUE						do { RETVAL_TRUE; return; } while (0)
1058 #define RETURN_THROWS()					do { ZEND_ASSERT(EG(exception)); (void) return_value; return; } while (0)
1059 
1060 #define HASH_OF(p) (Z_TYPE_P(p)==IS_ARRAY ? Z_ARRVAL_P(p) : ((Z_TYPE_P(p)==IS_OBJECT ? Z_OBJ_HT_P(p)->get_properties(Z_OBJ_P(p)) : NULL)))
1061 #define ZVAL_IS_NULL(z) (Z_TYPE_P(z) == IS_NULL)
1062 
1063 /* For compatibility */
1064 #define ZEND_MINIT			ZEND_MODULE_STARTUP_N
1065 #define ZEND_MSHUTDOWN		ZEND_MODULE_SHUTDOWN_N
1066 #define ZEND_RINIT			ZEND_MODULE_ACTIVATE_N
1067 #define ZEND_RSHUTDOWN		ZEND_MODULE_DEACTIVATE_N
1068 #define ZEND_MINFO			ZEND_MODULE_INFO_N
1069 #define ZEND_GINIT(module)		((void (*)(void*))(ZEND_MODULE_GLOBALS_CTOR_N(module)))
1070 #define ZEND_GSHUTDOWN(module)	((void (*)(void*))(ZEND_MODULE_GLOBALS_DTOR_N(module)))
1071 
1072 #define ZEND_MINIT_FUNCTION			ZEND_MODULE_STARTUP_D
1073 #define ZEND_MSHUTDOWN_FUNCTION		ZEND_MODULE_SHUTDOWN_D
1074 #define ZEND_RINIT_FUNCTION			ZEND_MODULE_ACTIVATE_D
1075 #define ZEND_RSHUTDOWN_FUNCTION		ZEND_MODULE_DEACTIVATE_D
1076 #define ZEND_MINFO_FUNCTION			ZEND_MODULE_INFO_D
1077 #define ZEND_GINIT_FUNCTION			ZEND_MODULE_GLOBALS_CTOR_D
1078 #define ZEND_GSHUTDOWN_FUNCTION		ZEND_MODULE_GLOBALS_DTOR_D
1079 
1080 /* May modify arg in-place. Will free arg in failure case (and take ownership in success case).
1081  * Prefer using the ZEND_TRY_ASSIGN_* macros over these APIs. */
1082 ZEND_API zend_result zend_try_assign_typed_ref_ex(zend_reference *ref, zval *zv, bool strict);
1083 ZEND_API zend_result zend_try_assign_typed_ref(zend_reference *ref, zval *zv);
1084 
1085 ZEND_API zend_result zend_try_assign_typed_ref_null(zend_reference *ref);
1086 ZEND_API zend_result zend_try_assign_typed_ref_bool(zend_reference *ref, bool val);
1087 ZEND_API zend_result zend_try_assign_typed_ref_long(zend_reference *ref, zend_long lval);
1088 ZEND_API zend_result zend_try_assign_typed_ref_double(zend_reference *ref, double dval);
1089 ZEND_API zend_result zend_try_assign_typed_ref_empty_string(zend_reference *ref);
1090 ZEND_API zend_result zend_try_assign_typed_ref_str(zend_reference *ref, zend_string *str);
1091 ZEND_API zend_result zend_try_assign_typed_ref_string(zend_reference *ref, const char *string);
1092 ZEND_API zend_result zend_try_assign_typed_ref_stringl(zend_reference *ref, const char *string, size_t len);
1093 ZEND_API zend_result zend_try_assign_typed_ref_arr(zend_reference *ref, zend_array *arr);
1094 ZEND_API zend_result zend_try_assign_typed_ref_res(zend_reference *ref, zend_resource *res);
1095 ZEND_API zend_result zend_try_assign_typed_ref_zval(zend_reference *ref, zval *zv);
1096 ZEND_API zend_result zend_try_assign_typed_ref_zval_ex(zend_reference *ref, zval *zv, bool strict);
1097 
1098 #define _ZEND_TRY_ASSIGN_NULL(zv, is_ref) do { \
1099 	zval *_zv = zv; \
1100 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1101 		zend_reference *ref = Z_REF_P(_zv); \
1102 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1103 			zend_try_assign_typed_ref_null(ref); \
1104 			break; \
1105 		} \
1106 		_zv = &ref->val; \
1107 	} \
1108 	zval_ptr_dtor(_zv); \
1109 	ZVAL_NULL(_zv); \
1110 } while (0)
1111 
1112 #define ZEND_TRY_ASSIGN_NULL(zv) \
1113 	_ZEND_TRY_ASSIGN_NULL(zv, 0)
1114 
1115 #define ZEND_TRY_ASSIGN_REF_NULL(zv) do { \
1116 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1117 	_ZEND_TRY_ASSIGN_NULL(zv, 1); \
1118 } while (0)
1119 
1120 #define _ZEND_TRY_ASSIGN_FALSE(zv, is_ref) do { \
1121 	zval *_zv = zv; \
1122 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1123 		zend_reference *ref = Z_REF_P(_zv); \
1124 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1125 			zend_try_assign_typed_ref_bool(ref, 0); \
1126 			break; \
1127 		} \
1128 		_zv = &ref->val; \
1129 	} \
1130 	zval_ptr_dtor(_zv); \
1131 	ZVAL_FALSE(_zv); \
1132 } while (0)
1133 
1134 #define ZEND_TRY_ASSIGN_FALSE(zv) \
1135 	_ZEND_TRY_ASSIGN_FALSE(zv, 0)
1136 
1137 #define ZEND_TRY_ASSIGN_REF_FALSE(zv) do { \
1138 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1139 	_ZEND_TRY_ASSIGN_FALSE(zv, 1); \
1140 } while (0)
1141 
1142 #define _ZEND_TRY_ASSIGN_TRUE(zv, is_ref) do { \
1143 	zval *_zv = zv; \
1144 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1145 		zend_reference *ref = Z_REF_P(_zv); \
1146 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1147 			zend_try_assign_typed_ref_bool(ref, 1); \
1148 			break; \
1149 		} \
1150 		_zv = &ref->val; \
1151 	} \
1152 	zval_ptr_dtor(_zv); \
1153 	ZVAL_TRUE(_zv); \
1154 } while (0)
1155 
1156 #define ZEND_TRY_ASSIGN_TRUE(zv) \
1157 	_ZEND_TRY_ASSIGN_TRUE(zv, 0)
1158 
1159 #define ZEND_TRY_ASSIGN_REF_TRUE(zv) do { \
1160 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1161 	_ZEND_TRY_ASSIGN_TRUE(zv, 1); \
1162 } while (0)
1163 
1164 #define _ZEND_TRY_ASSIGN_BOOL(zv, bval, is_ref) do { \
1165 	zval *_zv = zv; \
1166 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1167 		zend_reference *ref = Z_REF_P(_zv); \
1168 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1169 			zend_try_assign_typed_ref_bool(ref, 1); \
1170 			break; \
1171 		} \
1172 		_zv = &ref->val; \
1173 	} \
1174 	zval_ptr_dtor(_zv); \
1175 	ZVAL_BOOL(_zv, bval); \
1176 } while (0)
1177 
1178 #define ZEND_TRY_ASSIGN_BOOL(zv, bval) \
1179 	_ZEND_TRY_ASSIGN_BOOL(zv, bval, 0)
1180 
1181 #define ZEND_TRY_ASSIGN_REF_BOOL(zv, bval) do { \
1182 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1183 	_ZEND_TRY_ASSIGN_BOOL(zv, bval, 1); \
1184 } while (0)
1185 
1186 #define _ZEND_TRY_ASSIGN_LONG(zv, lval, is_ref) do { \
1187 	zval *_zv = zv; \
1188 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1189 		zend_reference *ref = Z_REF_P(_zv); \
1190 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1191 			zend_try_assign_typed_ref_long(ref, lval); \
1192 			break; \
1193 		} \
1194 		_zv = &ref->val; \
1195 	} \
1196 	zval_ptr_dtor(_zv); \
1197 	ZVAL_LONG(_zv, lval); \
1198 } while (0)
1199 
1200 #define ZEND_TRY_ASSIGN_LONG(zv, lval) \
1201 	_ZEND_TRY_ASSIGN_LONG(zv, lval, 0)
1202 
1203 #define ZEND_TRY_ASSIGN_REF_LONG(zv, lval) do { \
1204 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1205 	_ZEND_TRY_ASSIGN_LONG(zv, lval, 1); \
1206 } while (0)
1207 
1208 #define _ZEND_TRY_ASSIGN_DOUBLE(zv, dval, is_ref) do { \
1209 	zval *_zv = zv; \
1210 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1211 		zend_reference *ref = Z_REF_P(_zv); \
1212 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1213 			zend_try_assign_typed_ref_double(ref, dval); \
1214 			break; \
1215 		} \
1216 		_zv = &ref->val; \
1217 	} \
1218 	zval_ptr_dtor(_zv); \
1219 	ZVAL_DOUBLE(_zv, dval); \
1220 } while (0)
1221 
1222 #define ZEND_TRY_ASSIGN_DOUBLE(zv, dval) \
1223 	_ZEND_TRY_ASSIGN_DOUBLE(zv, dval, 0)
1224 
1225 #define ZEND_TRY_ASSIGN_REF_DOUBLE(zv, dval) do { \
1226 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1227 	_ZEND_TRY_ASSIGN_DOUBLE(zv, dval, 1); \
1228 } while (0)
1229 
1230 #define _ZEND_TRY_ASSIGN_EMPTY_STRING(zv, is_ref) do { \
1231 	zval *_zv = zv; \
1232 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1233 		zend_reference *ref = Z_REF_P(_zv); \
1234 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1235 			zend_try_assign_typed_ref_empty_string(ref); \
1236 			break; \
1237 		} \
1238 		_zv = &ref->val; \
1239 	} \
1240 	zval_ptr_dtor(_zv); \
1241 	ZVAL_EMPTY_STRING(_zv); \
1242 } while (0)
1243 
1244 #define ZEND_TRY_ASSIGN_EMPTY_STRING(zv) \
1245 	_ZEND_TRY_ASSIGN_EMPTY_STRING(zv, 0)
1246 
1247 #define ZEND_TRY_ASSIGN_REF_EMPTY_STRING(zv) do { \
1248 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1249 	_ZEND_TRY_ASSIGN_EMPTY_STRING(zv, 1); \
1250 } while (0)
1251 
1252 #define _ZEND_TRY_ASSIGN_STR(zv, str, is_ref) do { \
1253 	zval *_zv = zv; \
1254 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1255 		zend_reference *ref = Z_REF_P(_zv); \
1256 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1257 			zend_try_assign_typed_ref_str(ref, str); \
1258 			break; \
1259 		} \
1260 		_zv = &ref->val; \
1261 	} \
1262 	zval_ptr_dtor(_zv); \
1263 	ZVAL_STR(_zv, str); \
1264 } while (0)
1265 
1266 #define ZEND_TRY_ASSIGN_STR(zv, str) \
1267 	_ZEND_TRY_ASSIGN_STR(zv, str, 0)
1268 
1269 #define ZEND_TRY_ASSIGN_REF_STR(zv, str) do { \
1270 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1271 	_ZEND_TRY_ASSIGN_STR(zv, str, 1); \
1272 } while (0)
1273 
1274 #define _ZEND_TRY_ASSIGN_NEW_STR(zv, str, is_str) do { \
1275 	zval *_zv = zv; \
1276 	if (is_str || UNEXPECTED(Z_ISREF_P(_zv))) { \
1277 		zend_reference *ref = Z_REF_P(_zv); \
1278 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1279 			zend_try_assign_typed_ref_str(ref, str); \
1280 			break; \
1281 		} \
1282 		_zv = &ref->val; \
1283 	} \
1284 	zval_ptr_dtor(_zv); \
1285 	ZVAL_NEW_STR(_zv, str); \
1286 } while (0)
1287 
1288 #define ZEND_TRY_ASSIGN_NEW_STR(zv, str) \
1289 	_ZEND_TRY_ASSIGN_NEW_STR(zv, str, 0)
1290 
1291 #define ZEND_TRY_ASSIGN_REF_NEW_STR(zv, str) do { \
1292 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1293 	_ZEND_TRY_ASSIGN_NEW_STR(zv, str, 1); \
1294 } while (0)
1295 
1296 #define _ZEND_TRY_ASSIGN_STRING(zv, string, is_ref) do { \
1297 	zval *_zv = zv; \
1298 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1299 		zend_reference *ref = Z_REF_P(_zv); \
1300 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1301 			zend_try_assign_typed_ref_string(ref, string); \
1302 			break; \
1303 		} \
1304 		_zv = &ref->val; \
1305 	} \
1306 	zval_ptr_dtor(_zv); \
1307 	ZVAL_STRING(_zv, string); \
1308 } while (0)
1309 
1310 #define ZEND_TRY_ASSIGN_STRING(zv, string) \
1311 	_ZEND_TRY_ASSIGN_STRING(zv, string, 0)
1312 
1313 #define ZEND_TRY_ASSIGN_REF_STRING(zv, string) do { \
1314 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1315 	_ZEND_TRY_ASSIGN_STRING(zv, string, 1); \
1316 } while (0)
1317 
1318 #define _ZEND_TRY_ASSIGN_STRINGL(zv, string, len, is_ref) do { \
1319 	zval *_zv = zv; \
1320 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1321 		zend_reference *ref = Z_REF_P(_zv); \
1322 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1323 			zend_try_assign_typed_ref_stringl(ref, string, len); \
1324 			break; \
1325 		} \
1326 		_zv = &ref->val; \
1327 	} \
1328 	zval_ptr_dtor(_zv); \
1329 	ZVAL_STRINGL(_zv, string, len); \
1330 } while (0)
1331 
1332 #define ZEND_TRY_ASSIGN_STRINGL(zv, string, len) \
1333 	_ZEND_TRY_ASSIGN_STRINGL(zv, string, len, 0)
1334 
1335 #define ZEND_TRY_ASSIGN_REF_STRINGL(zv, string, len) do { \
1336 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1337 	_ZEND_TRY_ASSIGN_STRINGL(zv, string, len, 1); \
1338 } while (0)
1339 
1340 #define _ZEND_TRY_ASSIGN_ARR(zv, arr, is_ref) do { \
1341 	zval *_zv = zv; \
1342 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1343 		zend_reference *ref = Z_REF_P(_zv); \
1344 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1345 			zend_try_assign_typed_ref_arr(ref, arr); \
1346 			break; \
1347 		} \
1348 		_zv = &ref->val; \
1349 	} \
1350 	zval_ptr_dtor(_zv); \
1351 	ZVAL_ARR(_zv, arr); \
1352 } while (0)
1353 
1354 #define ZEND_TRY_ASSIGN_ARR(zv, arr) \
1355 	_ZEND_TRY_ASSIGN_ARR(zv, arr, 0)
1356 
1357 #define ZEND_TRY_ASSIGN_REF_ARR(zv, arr) do { \
1358 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1359 	_ZEND_TRY_ASSIGN_ARR(zv, arr, 1); \
1360 } while (0)
1361 
1362 #define _ZEND_TRY_ASSIGN_RES(zv, res, is_ref) do { \
1363 	zval *_zv = zv; \
1364 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1365 		zend_reference *ref = Z_REF_P(_zv); \
1366 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1367 			zend_try_assign_typed_ref_res(ref, res); \
1368 			break; \
1369 		} \
1370 		_zv = &ref->val; \
1371 	} \
1372 	zval_ptr_dtor(_zv); \
1373 	ZVAL_RES(_zv, res); \
1374 } while (0)
1375 
1376 #define ZEND_TRY_ASSIGN_RES(zv, res) \
1377 	_ZEND_TRY_ASSIGN_RES(zv, res, 0)
1378 
1379 #define ZEND_TRY_ASSIGN_REF_RES(zv, res) do { \
1380 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1381 	_ZEND_TRY_ASSIGN_RES(zv, res, 1); \
1382 } while (0)
1383 
1384 #define _ZEND_TRY_ASSIGN_TMP(zv, other_zv, is_ref) do { \
1385 	zval *_zv = zv; \
1386 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1387 		zend_reference *ref = Z_REF_P(_zv); \
1388 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1389 			zend_try_assign_typed_ref(ref, other_zv); \
1390 			break; \
1391 		} \
1392 		_zv = &ref->val; \
1393 	} \
1394 	zval_ptr_dtor(_zv); \
1395 	ZVAL_COPY_VALUE(_zv, other_zv); \
1396 } while (0)
1397 
1398 #define ZEND_TRY_ASSIGN_TMP(zv, other_zv) \
1399 	_ZEND_TRY_ASSIGN_TMP(zv, other_zv, 0)
1400 
1401 #define ZEND_TRY_ASSIGN_REF_TMP(zv, other_zv) do { \
1402 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1403 	_ZEND_TRY_ASSIGN_TMP(zv, other_zv, 1); \
1404 } while (0)
1405 
1406 #define _ZEND_TRY_ASSIGN_VALUE(zv, other_zv, is_ref) do { \
1407 	zval *_zv = zv; \
1408 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1409 		zend_reference *ref = Z_REF_P(_zv); \
1410 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1411 			zend_try_assign_typed_ref_zval(ref, other_zv); \
1412 			break; \
1413 		} \
1414 		_zv = &ref->val; \
1415 	} \
1416 	zval_ptr_dtor(_zv); \
1417 	ZVAL_COPY_VALUE(_zv, other_zv); \
1418 } while (0)
1419 
1420 #define ZEND_TRY_ASSIGN_VALUE(zv, other_zv) \
1421 	_ZEND_TRY_ASSIGN_VALUE(zv, other_zv, 0)
1422 
1423 #define ZEND_TRY_ASSIGN_REF_VALUE(zv, other_zv) do { \
1424 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1425 	_ZEND_TRY_ASSIGN_VALUE(zv, other_zv, 1); \
1426 } while (0)
1427 
1428 #define ZEND_TRY_ASSIGN_COPY(zv, other_zv) do { \
1429 	Z_TRY_ADDREF_P(other_zv); \
1430 	ZEND_TRY_ASSIGN_VALUE(zv, other_zv); \
1431 } while (0)
1432 
1433 #define ZEND_TRY_ASSIGN_REF_COPY(zv, other_zv) do { \
1434 	Z_TRY_ADDREF_P(other_zv); \
1435 	ZEND_TRY_ASSIGN_REF_VALUE(zv, other_zv); \
1436 } while (0)
1437 
1438 #define _ZEND_TRY_ASSIGN_VALUE_EX(zv, other_zv, strict, is_ref) do { \
1439 	zval *_zv = zv; \
1440 	if (is_ref || UNEXPECTED(Z_ISREF_P(_zv))) { \
1441 		zend_reference *ref = Z_REF_P(_zv); \
1442 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) { \
1443 			zend_try_assign_typed_ref_zval_ex(ref, other_zv, strict); \
1444 			break; \
1445 		} \
1446 		_zv = &ref->val; \
1447 	} \
1448 	zval_ptr_dtor(_zv); \
1449 	ZVAL_COPY_VALUE(_zv, other_zv); \
1450 } while (0)
1451 
1452 #define ZEND_TRY_ASSIGN_VALUE_EX(zv, other_zv, strict) \
1453 	_ZEND_TRY_ASSIGN_VALUE_EX(zv, other_zv, strict, 0)
1454 
1455 #define ZEND_TRY_ASSIGN_REF_VALUE_EX(zv, other_zv, strict) do { \
1456 	ZEND_ASSERT(Z_ISREF_P(zv)); \
1457 	_ZEND_TRY_ASSIGN_VALUE_EX(zv, other_zv, strict, 1); \
1458 } while (0)
1459 
1460 #define ZEND_TRY_ASSIGN_COPY_EX(zv, other_zv, strict) do { \
1461 	Z_TRY_ADDREF_P(other_zv); \
1462 	ZEND_TRY_ASSIGN_VALUE_EX(zv, other_zv, strict); \
1463 } while (0)
1464 
1465 #define ZEND_TRY_ASSIGN_REF_COPY_EX(zv, other_zv, strict) do { \
1466 	Z_TRY_ADDREF_P(other_zv); \
1467 	ZEND_TRY_ASSIGN_REF_VALUE_EX(zv, other_zv, strict); \
1468 } while (0)
1469 
1470 /* Initializes a reference to an empty array and returns dereferenced zval,
1471  * or NULL if the initialization failed. */
zend_try_array_init_size(zval * zv,uint32_t size)1472 static zend_always_inline zval *zend_try_array_init_size(zval *zv, uint32_t size)
1473 {
1474 	zend_array *arr = zend_new_array(size);
1475 
1476 	if (EXPECTED(Z_ISREF_P(zv))) {
1477 		zend_reference *ref = Z_REF_P(zv);
1478 		if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) {
1479 			if (zend_try_assign_typed_ref_arr(ref, arr) == FAILURE) {
1480 				return NULL;
1481 			}
1482 			return &ref->val;
1483 		}
1484 		zv = &ref->val;
1485 	}
1486 	zval_ptr_dtor(zv);
1487 	ZVAL_ARR(zv, arr);
1488 	return zv;
1489 }
1490 
zend_try_array_init(zval * zv)1491 static zend_always_inline zval *zend_try_array_init(zval *zv)
1492 {
1493 	return zend_try_array_init_size(zv, 0);
1494 }
1495 
1496 /* Fast parameter parsing API */
1497 
1498 /* Fast ZPP is always enabled now; this define is left in for compatibility
1499  * with any existing conditional compilation blocks.
1500  */
1501 #define FAST_ZPP 1
1502 
1503 #define Z_EXPECTED_TYPES(_) \
1504 	_(Z_EXPECTED_LONG,				"of type int") \
1505 	_(Z_EXPECTED_LONG_OR_NULL,		"of type ?int") \
1506 	_(Z_EXPECTED_BOOL,				"of type bool") \
1507 	_(Z_EXPECTED_BOOL_OR_NULL,		"of type ?bool") \
1508 	_(Z_EXPECTED_STRING,			"of type string") \
1509 	_(Z_EXPECTED_STRING_OR_NULL,	"of type ?string") \
1510 	_(Z_EXPECTED_ARRAY,				"of type array") \
1511 	_(Z_EXPECTED_ARRAY_OR_NULL,		"of type ?array") \
1512 	_(Z_EXPECTED_ARRAY_OR_LONG,		"of type array|int") \
1513 	_(Z_EXPECTED_ARRAY_OR_LONG_OR_NULL, "of type array|int|null") \
1514 	_(Z_EXPECTED_ITERABLE,				"of type Traversable|array") \
1515 	_(Z_EXPECTED_ITERABLE_OR_NULL,		"of type Traversable|array|null") \
1516 	_(Z_EXPECTED_FUNC,				"a valid callback") \
1517 	_(Z_EXPECTED_FUNC_OR_NULL,		"a valid callback or null") \
1518 	_(Z_EXPECTED_RESOURCE,			"of type resource") \
1519 	_(Z_EXPECTED_RESOURCE_OR_NULL,	"of type resource or null") \
1520 	_(Z_EXPECTED_PATH,				"of type string") \
1521 	_(Z_EXPECTED_PATH_OR_NULL,		"of type ?string") \
1522 	_(Z_EXPECTED_OBJECT,			"of type object") \
1523 	_(Z_EXPECTED_OBJECT_OR_NULL,	"of type ?object") \
1524 	_(Z_EXPECTED_DOUBLE,			"of type float") \
1525 	_(Z_EXPECTED_DOUBLE_OR_NULL,	"of type ?float") \
1526 	_(Z_EXPECTED_NUMBER,			"of type int|float") \
1527 	_(Z_EXPECTED_NUMBER_OR_NULL,	"of type int|float|null") \
1528 	_(Z_EXPECTED_NUMBER_OR_STRING,			"of type string|int|float") \
1529 	_(Z_EXPECTED_NUMBER_OR_STRING_OR_NULL,	"of type string|int|float|null") \
1530 	_(Z_EXPECTED_ARRAY_OR_STRING,	"of type array|string") \
1531 	_(Z_EXPECTED_ARRAY_OR_STRING_OR_NULL, "of type array|string|null") \
1532 	_(Z_EXPECTED_STRING_OR_LONG,	"of type string|int") \
1533 	_(Z_EXPECTED_STRING_OR_LONG_OR_NULL, "of type string|int|null") \
1534 	_(Z_EXPECTED_OBJECT_OR_CLASS_NAME,	"an object or a valid class name") \
1535 	_(Z_EXPECTED_OBJECT_OR_CLASS_NAME_OR_NULL, "an object, a valid class name, or null") \
1536 	_(Z_EXPECTED_OBJECT_OR_STRING,	"of type object|string") \
1537 	_(Z_EXPECTED_OBJECT_OR_STRING_OR_NULL, "of type object|string|null") \
1538 
1539 #define Z_EXPECTED_TYPE
1540 
1541 #define Z_EXPECTED_TYPE_ENUM(id, str) id,
1542 #define Z_EXPECTED_TYPE_STR(id, str)  str,
1543 
1544 typedef enum _zend_expected_type {
1545 	Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_ENUM)
1546 	Z_EXPECTED_LAST
1547 } zend_expected_type;
1548 
1549 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_none_error(void);
1550 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_error(uint32_t min_num_args, uint32_t max_num_args);
1551 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_error(int error_code, uint32_t num, char *name, zend_expected_type expected_type, zval *arg);
1552 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_type_error(uint32_t num, zend_expected_type expected_type, zval *arg);
1553 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_error(uint32_t num, const char *name, zval *arg);
1554 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_null_error(uint32_t num, const char *name, zval *arg);
1555 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_long_error(uint32_t num, const char *name, zval *arg);
1556 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_long_or_null_error(uint32_t num, const char *name, zval *arg);
1557 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_string_error(uint32_t num, const char *name, zval *arg);
1558 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_string_or_null_error(uint32_t num, const char *name, zval *arg);
1559 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_error(uint32_t num, char *error);
1560 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_or_null_error(uint32_t num, char *error);
1561 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_unexpected_extra_named_error(void);
1562 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_error_variadic(zend_class_entry *error_ce, uint32_t arg_num, const char *format, va_list va);
1563 ZEND_API ZEND_COLD void zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format, ...);
1564 ZEND_API ZEND_COLD void zend_argument_type_error(uint32_t arg_num, const char *format, ...);
1565 ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format, ...);
1566 
1567 #define ZPP_ERROR_OK                            0
1568 #define ZPP_ERROR_FAILURE                       1
1569 #define ZPP_ERROR_WRONG_CALLBACK                2
1570 #define ZPP_ERROR_WRONG_CLASS                   3
1571 #define ZPP_ERROR_WRONG_CLASS_OR_NULL           4
1572 #define ZPP_ERROR_WRONG_CLASS_OR_STRING         5
1573 #define ZPP_ERROR_WRONG_CLASS_OR_STRING_OR_NULL 6
1574 #define ZPP_ERROR_WRONG_CLASS_OR_LONG           7
1575 #define ZPP_ERROR_WRONG_CLASS_OR_LONG_OR_NULL   8
1576 #define ZPP_ERROR_WRONG_ARG                     9
1577 #define ZPP_ERROR_WRONG_COUNT                   10
1578 #define ZPP_ERROR_UNEXPECTED_EXTRA_NAMED        11
1579 #define ZPP_ERROR_WRONG_CALLBACK_OR_NULL        12
1580 
1581 #define ZEND_PARSE_PARAMETERS_START_EX(flags, min_num_args, max_num_args) do { \
1582 		const int _flags = (flags); \
1583 		uint32_t _min_num_args = (min_num_args); \
1584 		uint32_t _max_num_args = (uint32_t) (max_num_args); \
1585 		uint32_t _num_args = EX_NUM_ARGS(); \
1586 		uint32_t _i = 0; \
1587 		zval *_real_arg, *_arg = NULL; \
1588 		zend_expected_type _expected_type = Z_EXPECTED_LONG; \
1589 		char *_error = NULL; \
1590 		bool _dummy = 0; \
1591 		bool _optional = 0; \
1592 		int _error_code = ZPP_ERROR_OK; \
1593 		((void)_i); \
1594 		((void)_real_arg); \
1595 		((void)_arg); \
1596 		((void)_expected_type); \
1597 		((void)_error); \
1598 		((void)_optional); \
1599 		((void)_dummy); \
1600 		\
1601 		do { \
1602 			if (UNEXPECTED(_num_args < _min_num_args) || \
1603 			    UNEXPECTED(_num_args > _max_num_args)) { \
1604 				if (!(_flags & ZEND_PARSE_PARAMS_QUIET)) { \
1605 					zend_wrong_parameters_count_error(_min_num_args, _max_num_args); \
1606 				} \
1607 				_error_code = ZPP_ERROR_FAILURE; \
1608 				break; \
1609 			} \
1610 			_real_arg = ZEND_CALL_ARG(execute_data, 0);
1611 
1612 #define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args) \
1613 	ZEND_PARSE_PARAMETERS_START_EX(0, min_num_args, max_num_args)
1614 
1615 #define ZEND_PARSE_PARAMETERS_NONE() do { \
1616 		if (UNEXPECTED(ZEND_NUM_ARGS() != 0)) { \
1617 			zend_wrong_parameters_none_error(); \
1618 			return; \
1619 		} \
1620 	} while (0)
1621 
1622 #define ZEND_PARSE_PARAMETERS_END_EX(failure) \
1623 			ZEND_ASSERT(_i == _max_num_args || _max_num_args == (uint32_t) -1); \
1624 		} while (0); \
1625 		if (UNEXPECTED(_error_code != ZPP_ERROR_OK)) { \
1626 			if (!(_flags & ZEND_PARSE_PARAMS_QUIET)) { \
1627 				zend_wrong_parameter_error(_error_code, _i, _error, _expected_type, _arg); \
1628 			} \
1629 			failure; \
1630 		} \
1631 	} while (0)
1632 
1633 #define ZEND_PARSE_PARAMETERS_END() \
1634 	ZEND_PARSE_PARAMETERS_END_EX(return)
1635 
1636 #define Z_PARAM_PROLOGUE(deref, separate) \
1637 	++_i; \
1638 	ZEND_ASSERT(_i <= _min_num_args || _optional==1); \
1639 	ZEND_ASSERT(_i >  _min_num_args || _optional==0); \
1640 	if (_optional) { \
1641 		if (UNEXPECTED(_i >_num_args)) break; \
1642 	} \
1643 	_real_arg++; \
1644 	_arg = _real_arg; \
1645 	if (deref) { \
1646 		if (EXPECTED(Z_ISREF_P(_arg))) { \
1647 			_arg = Z_REFVAL_P(_arg); \
1648 		} \
1649 	} \
1650 	if (separate) { \
1651 		SEPARATE_ZVAL_NOREF(_arg); \
1652 	}
1653 
1654 /* get the zval* for a previously parsed argument */
1655 #define Z_PARAM_GET_PREV_ZVAL(dest) \
1656 	zend_parse_arg_zval_deref(_arg, &dest, 0);
1657 
1658 /* old "|" */
1659 #define Z_PARAM_OPTIONAL \
1660 	_optional = 1;
1661 
1662 /* old "a" */
1663 #define Z_PARAM_ARRAY_EX2(dest, check_null, deref, separate) \
1664 		Z_PARAM_PROLOGUE(deref, separate); \
1665 		if (UNEXPECTED(!zend_parse_arg_array(_arg, &dest, check_null, 0))) { \
1666 			_expected_type = check_null ? Z_EXPECTED_ARRAY_OR_NULL : Z_EXPECTED_ARRAY; \
1667 			_error_code = ZPP_ERROR_WRONG_ARG; \
1668 			break; \
1669 		}
1670 
1671 #define Z_PARAM_ARRAY_EX(dest, check_null, separate) \
1672 	Z_PARAM_ARRAY_EX2(dest, check_null, separate, separate)
1673 
1674 #define Z_PARAM_ARRAY(dest) \
1675 	Z_PARAM_ARRAY_EX(dest, 0, 0)
1676 
1677 #define Z_PARAM_ARRAY_OR_NULL(dest) \
1678 	Z_PARAM_ARRAY_EX(dest, 1, 0)
1679 
1680 /* old "A" */
1681 #define Z_PARAM_ARRAY_OR_OBJECT_EX2(dest, check_null, deref, separate) \
1682 		Z_PARAM_PROLOGUE(deref, separate); \
1683 		if (UNEXPECTED(!zend_parse_arg_array(_arg, &dest, check_null, 1))) { \
1684 			_expected_type = check_null ? Z_EXPECTED_ARRAY_OR_NULL : Z_EXPECTED_ARRAY; \
1685 			_error_code = ZPP_ERROR_WRONG_ARG; \
1686 			break; \
1687 		}
1688 
1689 #define Z_PARAM_ARRAY_OR_OBJECT_EX(dest, check_null, separate) \
1690 	Z_PARAM_ARRAY_OR_OBJECT_EX2(dest, check_null, separate, separate)
1691 
1692 #define Z_PARAM_ARRAY_OR_OBJECT(dest) \
1693 	Z_PARAM_ARRAY_OR_OBJECT_EX(dest, 0, 0)
1694 
1695 #define Z_PARAM_ITERABLE_EX(dest, check_null) \
1696 	Z_PARAM_PROLOGUE(0, 0); \
1697 	if (UNEXPECTED(!zend_parse_arg_iterable(_arg, &dest, check_null))) { \
1698 		_expected_type = check_null ? Z_EXPECTED_ITERABLE_OR_NULL : Z_EXPECTED_ITERABLE; \
1699 		_error_code = ZPP_ERROR_WRONG_ARG; \
1700 		break; \
1701 	}
1702 
1703 #define Z_PARAM_ITERABLE(dest) \
1704 	Z_PARAM_ITERABLE_EX(dest, 0)
1705 
1706 #define Z_PARAM_ITERABLE_OR_NULL(dest) \
1707 	Z_PARAM_ITERABLE_EX(dest, 1)
1708 
1709 /* old "b" */
1710 #define Z_PARAM_BOOL_EX(dest, is_null, check_null, deref) \
1711 		Z_PARAM_PROLOGUE(deref, 0); \
1712 		if (UNEXPECTED(!zend_parse_arg_bool(_arg, &dest, &is_null, check_null, _i))) { \
1713 			_expected_type = check_null ? Z_EXPECTED_BOOL_OR_NULL : Z_EXPECTED_BOOL; \
1714 			_error_code = ZPP_ERROR_WRONG_ARG; \
1715 			break; \
1716 		}
1717 
1718 #define Z_PARAM_BOOL(dest) \
1719 	Z_PARAM_BOOL_EX(dest, _dummy, 0, 0)
1720 
1721 #define Z_PARAM_BOOL_OR_NULL(dest, is_null) \
1722 	Z_PARAM_BOOL_EX(dest, is_null, 1, 0)
1723 
1724 /* old "C" */
1725 #define Z_PARAM_CLASS_EX(dest, check_null, deref) \
1726 		Z_PARAM_PROLOGUE(deref, 0); \
1727 		if (UNEXPECTED(!zend_parse_arg_class(_arg, &dest, _i, check_null))) { \
1728 			_error_code = ZPP_ERROR_FAILURE; \
1729 			break; \
1730 		}
1731 
1732 #define Z_PARAM_CLASS(dest) \
1733 	Z_PARAM_CLASS_EX(dest, 0, 0)
1734 
1735 #define Z_PARAM_CLASS_OR_NULL(dest) \
1736 	Z_PARAM_CLASS_EX(dest, 1, 0)
1737 
1738 #define Z_PARAM_OBJ_OR_CLASS_NAME_EX(dest, allow_null) \
1739 	Z_PARAM_PROLOGUE(0, 0); \
1740 	if (UNEXPECTED(!zend_parse_arg_obj_or_class_name(_arg, &dest, allow_null))) { \
1741 		_expected_type = allow_null ? Z_EXPECTED_OBJECT_OR_CLASS_NAME_OR_NULL : Z_EXPECTED_OBJECT_OR_CLASS_NAME; \
1742 		_error_code = ZPP_ERROR_WRONG_ARG; \
1743 		break; \
1744 	}
1745 
1746 #define Z_PARAM_OBJ_OR_CLASS_NAME(dest) \
1747 	Z_PARAM_OBJ_OR_CLASS_NAME_EX(dest, 0);
1748 
1749 #define Z_PARAM_OBJ_OR_CLASS_NAME_OR_NULL(dest) \
1750 	Z_PARAM_OBJ_OR_CLASS_NAME_EX(dest, 1);
1751 
1752 #define Z_PARAM_OBJ_OR_STR_EX(destination_object, destination_string, allow_null) \
1753 	Z_PARAM_PROLOGUE(0, 0); \
1754 	if (UNEXPECTED(!zend_parse_arg_obj_or_str(_arg, &destination_object, NULL, &destination_string, allow_null, _i))) { \
1755 		_expected_type = allow_null ? Z_EXPECTED_OBJECT_OR_STRING_OR_NULL : Z_EXPECTED_OBJECT_OR_STRING; \
1756 		_error_code = ZPP_ERROR_WRONG_ARG; \
1757 		break; \
1758 	}
1759 
1760 #define Z_PARAM_OBJ_OR_STR(destination_object, destination_string) \
1761 	Z_PARAM_OBJ_OR_STR_EX(destination_object, destination_string, 0);
1762 
1763 #define Z_PARAM_OBJ_OR_STR_OR_NULL(destination_object, destination_string) \
1764 	Z_PARAM_OBJ_OR_STR_EX(destination_object, destination_string, 1);
1765 
1766 #define Z_PARAM_OBJ_OF_CLASS_OR_STR_EX(destination_object, base_ce, destination_string, allow_null) \
1767 	Z_PARAM_PROLOGUE(0, 0); \
1768 	if (UNEXPECTED(!zend_parse_arg_obj_or_str(_arg, &destination_object, base_ce, &destination_string, allow_null, _i))) { \
1769 		if (base_ce) { \
1770 			_error = ZSTR_VAL((base_ce)->name); \
1771 			_error_code = allow_null ? ZPP_ERROR_WRONG_CLASS_OR_STRING_OR_NULL : ZPP_ERROR_WRONG_CLASS_OR_STRING; \
1772 			break; \
1773 		} else { \
1774 			_expected_type = allow_null ? Z_EXPECTED_OBJECT_OR_STRING_OR_NULL : Z_EXPECTED_OBJECT_OR_STRING; \
1775 			_error_code = ZPP_ERROR_WRONG_ARG; \
1776 			break; \
1777 		} \
1778 	}
1779 
1780 #define Z_PARAM_OBJ_OF_CLASS_OR_STR(destination_object, base_ce, destination_string) \
1781 	Z_PARAM_OBJ_OF_CLASS_OR_STR_EX(destination_object, base_ce, destination_string, 0);
1782 
1783 #define Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(destination_object, base_ce, destination_string) \
1784 	Z_PARAM_OBJ_OF_CLASS_OR_STR_EX(destination_object, base_ce, destination_string, 1);
1785 
1786 /* old "d" */
1787 #define Z_PARAM_DOUBLE_EX(dest, is_null, check_null, deref) \
1788 		Z_PARAM_PROLOGUE(deref, 0); \
1789 		if (UNEXPECTED(!zend_parse_arg_double(_arg, &dest, &is_null, check_null, _i))) { \
1790 			_expected_type = check_null ? Z_EXPECTED_DOUBLE_OR_NULL : Z_EXPECTED_DOUBLE; \
1791 			_error_code = ZPP_ERROR_WRONG_ARG; \
1792 			break; \
1793 		}
1794 
1795 #define Z_PARAM_DOUBLE(dest) \
1796 	Z_PARAM_DOUBLE_EX(dest, _dummy, 0, 0)
1797 
1798 #define Z_PARAM_DOUBLE_OR_NULL(dest, is_null) \
1799 	Z_PARAM_DOUBLE_EX(dest, is_null, 1, 0)
1800 
1801 /* old "f" */
1802 #define Z_PARAM_FUNC_EX2(dest_fci, dest_fcc, check_null, deref, free_trampoline) \
1803 		Z_PARAM_PROLOGUE(deref, 0); \
1804 		if (UNEXPECTED(!zend_parse_arg_func(_arg, &dest_fci, &dest_fcc, check_null, &_error, free_trampoline))) { \
1805 			if (!_error) { \
1806 				_expected_type = check_null ? Z_EXPECTED_FUNC_OR_NULL : Z_EXPECTED_FUNC; \
1807 				_error_code = ZPP_ERROR_WRONG_ARG; \
1808 			} else { \
1809 				_error_code = check_null ? ZPP_ERROR_WRONG_CALLBACK_OR_NULL : ZPP_ERROR_WRONG_CALLBACK; \
1810 			} \
1811 			break; \
1812 		} \
1813 
1814 #define Z_PARAM_FUNC_EX(dest_fci, dest_fcc, check_null, deref) Z_PARAM_FUNC_EX2(dest_fci, dest_fcc, check_null, deref, true)
1815 
1816 #define Z_PARAM_FUNC(dest_fci, dest_fcc) \
1817 	Z_PARAM_FUNC_EX2(dest_fci, dest_fcc, 0, 0, true)
1818 
1819 #define Z_PARAM_FUNC_NO_TRAMPOLINE_FREE(dest_fci, dest_fcc) \
1820 	Z_PARAM_FUNC_EX2(dest_fci, dest_fcc, 0, 0, false)
1821 
1822 #define Z_PARAM_FUNC_OR_NULL(dest_fci, dest_fcc) \
1823 	Z_PARAM_FUNC_EX2(dest_fci, dest_fcc, 1, 0, true)
1824 
1825 #define Z_PARAM_FUNC_NO_TRAMPOLINE_FREE_OR_NULL(dest_fci, dest_fcc) \
1826 	Z_PARAM_FUNC_EX2(dest_fci, dest_fcc, 1, 0, false)
1827 
1828 #define Z_PARAM_FUNC_OR_NULL_WITH_ZVAL(dest_fci, dest_fcc, dest_zp) \
1829 	Z_PARAM_FUNC_EX2(dest_fci, dest_fcc, 1, 0, true) \
1830 	Z_PARAM_GET_PREV_ZVAL(dest_zp)
1831 
1832 /* old "h" */
1833 #define Z_PARAM_ARRAY_HT_EX2(dest, check_null, deref, separate) \
1834 		Z_PARAM_PROLOGUE(deref, separate); \
1835 		if (UNEXPECTED(!zend_parse_arg_array_ht(_arg, &dest, check_null, 0, separate))) { \
1836 			_expected_type = check_null ? Z_EXPECTED_ARRAY_OR_NULL : Z_EXPECTED_ARRAY; \
1837 			_error_code = ZPP_ERROR_WRONG_ARG; \
1838 			break; \
1839 		}
1840 
1841 #define Z_PARAM_ARRAY_HT_EX(dest, check_null, separate) \
1842 	Z_PARAM_ARRAY_HT_EX2(dest, check_null, separate, separate)
1843 
1844 #define Z_PARAM_ARRAY_HT(dest) \
1845 	Z_PARAM_ARRAY_HT_EX(dest, 0, 0)
1846 
1847 #define Z_PARAM_ARRAY_HT_OR_NULL(dest) \
1848 	Z_PARAM_ARRAY_HT_EX(dest, 1, 0)
1849 
1850 #define Z_PARAM_ARRAY_HT_OR_LONG_EX(dest_ht, dest_long, is_null, allow_null) \
1851 	Z_PARAM_PROLOGUE(0, 0); \
1852 	if (UNEXPECTED(!zend_parse_arg_array_ht_or_long(_arg, &dest_ht, &dest_long, &is_null, allow_null, _i))) { \
1853 		_expected_type = allow_null ? Z_EXPECTED_ARRAY_OR_LONG_OR_NULL : Z_EXPECTED_ARRAY_OR_LONG; \
1854 		_error_code = ZPP_ERROR_WRONG_ARG; \
1855 		break; \
1856 	}
1857 
1858 #define Z_PARAM_ARRAY_HT_OR_LONG(dest_ht, dest_long) \
1859 	Z_PARAM_ARRAY_HT_OR_LONG_EX(dest_ht, dest_long, _dummy, 0)
1860 
1861 #define Z_PARAM_ARRAY_HT_OR_LONG_OR_NULL(dest_ht, dest_long, is_null) \
1862 	Z_PARAM_ARRAY_HT_OR_LONG_EX(dest_ht, dest_long, is_null, 1)
1863 
1864 /* old "H" */
1865 #define Z_PARAM_ARRAY_OR_OBJECT_HT_EX2(dest, check_null, deref, separate) \
1866 		Z_PARAM_PROLOGUE(deref, separate); \
1867 		if (UNEXPECTED(!zend_parse_arg_array_ht(_arg, &dest, check_null, 1, separate))) { \
1868 			_expected_type = check_null ? Z_EXPECTED_ARRAY_OR_NULL : Z_EXPECTED_ARRAY; \
1869 			_error_code = ZPP_ERROR_WRONG_ARG; \
1870 			break; \
1871 		}
1872 
1873 #define Z_PARAM_ARRAY_OR_OBJECT_HT_EX(dest, check_null, separate) \
1874 	Z_PARAM_ARRAY_OR_OBJECT_HT_EX2(dest, check_null, separate, separate)
1875 
1876 #define Z_PARAM_ARRAY_OR_OBJECT_HT(dest) \
1877 	Z_PARAM_ARRAY_OR_OBJECT_HT_EX(dest, 0, 0)
1878 
1879 /* old "l" */
1880 #define Z_PARAM_LONG_EX(dest, is_null, check_null, deref) \
1881 		Z_PARAM_PROLOGUE(deref, 0); \
1882 		if (UNEXPECTED(!zend_parse_arg_long(_arg, &dest, &is_null, check_null, _i))) { \
1883 			_expected_type = check_null ? Z_EXPECTED_LONG_OR_NULL : Z_EXPECTED_LONG; \
1884 			_error_code = ZPP_ERROR_WRONG_ARG; \
1885 			break; \
1886 		}
1887 
1888 #define Z_PARAM_LONG(dest) \
1889 	Z_PARAM_LONG_EX(dest, _dummy, 0, 0)
1890 
1891 #define Z_PARAM_LONG_OR_NULL(dest, is_null) \
1892 	Z_PARAM_LONG_EX(dest, is_null, 1, 0)
1893 
1894 /* old "n" */
1895 #define Z_PARAM_NUMBER_EX(dest, check_null) \
1896 	Z_PARAM_PROLOGUE(0, 0); \
1897 	if (UNEXPECTED(!zend_parse_arg_number(_arg, &dest, check_null, _i))) { \
1898 		_expected_type = check_null ? Z_EXPECTED_NUMBER_OR_NULL : Z_EXPECTED_NUMBER; \
1899 		_error_code = ZPP_ERROR_WRONG_ARG; \
1900 		break; \
1901 	}
1902 
1903 #define Z_PARAM_NUMBER_OR_NULL(dest) \
1904 	Z_PARAM_NUMBER_EX(dest, 1)
1905 
1906 #define Z_PARAM_NUMBER(dest) \
1907 	Z_PARAM_NUMBER_EX(dest, 0)
1908 
1909 #define Z_PARAM_NUMBER_OR_STR_EX(dest, check_null) \
1910 	Z_PARAM_PROLOGUE(0, 0); \
1911 	if (UNEXPECTED(!zend_parse_arg_number_or_str(_arg, &dest, check_null, _i))) { \
1912 		_expected_type = check_null ? Z_EXPECTED_NUMBER_OR_STRING_OR_NULL : Z_EXPECTED_NUMBER_OR_STRING; \
1913 		_error_code = ZPP_ERROR_WRONG_ARG; \
1914 		break; \
1915 	}
1916 
1917 #define Z_PARAM_NUMBER_OR_STR(dest) \
1918 	Z_PARAM_NUMBER_OR_STR_EX(dest, false)
1919 
1920 #define Z_PARAM_NUMBER_OR_STR_OR_NULL(dest) \
1921 	Z_PARAM_NUMBER_OR_STR_EX(dest, true)
1922 
1923 /* old "o" */
1924 #define Z_PARAM_OBJECT_EX(dest, check_null, deref) \
1925 		Z_PARAM_PROLOGUE(deref, 0); \
1926 		if (UNEXPECTED(!zend_parse_arg_object(_arg, &dest, NULL, check_null))) { \
1927 			_expected_type = check_null ? Z_EXPECTED_OBJECT_OR_NULL : Z_EXPECTED_OBJECT; \
1928 			_error_code = ZPP_ERROR_WRONG_ARG; \
1929 			break; \
1930 		}
1931 
1932 #define Z_PARAM_OBJECT(dest) \
1933 	Z_PARAM_OBJECT_EX(dest, 0, 0)
1934 
1935 #define Z_PARAM_OBJECT_OR_NULL(dest) \
1936 	Z_PARAM_OBJECT_EX(dest, 1, 0)
1937 
1938 /* The same as Z_PARAM_OBJECT_EX except that dest is a zend_object rather than a zval */
1939 #define Z_PARAM_OBJ_EX(dest, check_null, deref) \
1940 		Z_PARAM_PROLOGUE(deref, 0); \
1941 		if (UNEXPECTED(!zend_parse_arg_obj(_arg, &dest, NULL, check_null))) { \
1942 			_expected_type = check_null ? Z_EXPECTED_OBJECT_OR_NULL : Z_EXPECTED_OBJECT; \
1943 			_error_code = ZPP_ERROR_WRONG_ARG; \
1944 			break; \
1945 		}
1946 
1947 #define Z_PARAM_OBJ(dest) \
1948 	Z_PARAM_OBJ_EX(dest, 0, 0)
1949 
1950 #define Z_PARAM_OBJ_OR_NULL(dest) \
1951 	Z_PARAM_OBJ_EX(dest, 1, 0)
1952 
1953 /* old "O" */
1954 #define Z_PARAM_OBJECT_OF_CLASS_EX(dest, _ce, check_null, deref) \
1955 		Z_PARAM_PROLOGUE(deref, 0); \
1956 		if (UNEXPECTED(!zend_parse_arg_object(_arg, &dest, _ce, check_null))) { \
1957 			if (_ce) { \
1958 				_error = ZSTR_VAL((_ce)->name); \
1959 				_error_code = check_null ? ZPP_ERROR_WRONG_CLASS_OR_NULL : ZPP_ERROR_WRONG_CLASS; \
1960 				break; \
1961 			} else { \
1962 				_expected_type = check_null ? Z_EXPECTED_OBJECT_OR_NULL : Z_EXPECTED_OBJECT; \
1963 				_error_code = ZPP_ERROR_WRONG_ARG; \
1964 				break; \
1965 			} \
1966 		}
1967 
1968 #define Z_PARAM_OBJECT_OF_CLASS(dest, _ce) \
1969 	Z_PARAM_OBJECT_OF_CLASS_EX(dest, _ce, 0, 0)
1970 
1971 #define Z_PARAM_OBJECT_OF_CLASS_OR_NULL(dest, _ce) \
1972 	Z_PARAM_OBJECT_OF_CLASS_EX(dest, _ce, 1, 0)
1973 
1974 /* The same as Z_PARAM_OBJECT_OF_CLASS_EX except that dest is a zend_object rather than a zval */
1975 #define Z_PARAM_OBJ_OF_CLASS_EX(dest, _ce, check_null, deref) \
1976 		Z_PARAM_PROLOGUE(deref, 0); \
1977 		if (UNEXPECTED(!zend_parse_arg_obj(_arg, &dest, _ce, check_null))) { \
1978 			if (_ce) { \
1979 				_error = ZSTR_VAL((_ce)->name); \
1980 				_error_code = check_null ? ZPP_ERROR_WRONG_CLASS_OR_NULL : ZPP_ERROR_WRONG_CLASS; \
1981 				break; \
1982 			} else { \
1983 				_expected_type = check_null ? Z_EXPECTED_OBJECT_OR_NULL : Z_EXPECTED_OBJECT; \
1984 				_error_code = ZPP_ERROR_WRONG_ARG; \
1985 				break; \
1986 			} \
1987 		}
1988 
1989 #define Z_PARAM_OBJ_OF_CLASS(dest, _ce) \
1990 	Z_PARAM_OBJ_OF_CLASS_EX(dest, _ce, 0, 0)
1991 
1992 #define Z_PARAM_OBJ_OF_CLASS_OR_NULL(dest, _ce) \
1993 	Z_PARAM_OBJ_OF_CLASS_EX(dest, _ce, 1, 0)
1994 
1995 #define Z_PARAM_OBJ_OF_CLASS_OR_LONG_EX(dest_obj, _ce, dest_long, is_null, allow_null) \
1996 		Z_PARAM_PROLOGUE(0, 0); \
1997 		if (UNEXPECTED(!zend_parse_arg_obj_or_long(_arg, &dest_obj, _ce, &dest_long, &is_null, allow_null, _i))) { \
1998 			_error = ZSTR_VAL((_ce)->name); \
1999 			_error_code = allow_null ? ZPP_ERROR_WRONG_CLASS_OR_LONG_OR_NULL : ZPP_ERROR_WRONG_CLASS_OR_LONG; \
2000 			break; \
2001 		}
2002 
2003 #define Z_PARAM_OBJ_OF_CLASS_OR_LONG(dest_obj, _ce, dest_long) \
2004 	Z_PARAM_OBJ_OF_CLASS_OR_LONG_EX(dest_obj, _ce, dest_long, _dummy, 0)
2005 
2006 #define Z_PARAM_OBJ_OF_CLASS_OR_LONG_OR_NULL(dest_obj, _ce, dest_long, is_null) \
2007 	Z_PARAM_OBJ_OF_CLASS_OR_LONG_EX(dest_obj, _ce, dest_long, is_null, 1)
2008 
2009 /* old "p" */
2010 #define Z_PARAM_PATH_EX(dest, dest_len, check_null, deref) \
2011 		Z_PARAM_PROLOGUE(deref, 0); \
2012 		if (UNEXPECTED(!zend_parse_arg_path(_arg, &dest, &dest_len, check_null, _i))) { \
2013 			_expected_type = check_null ? Z_EXPECTED_PATH_OR_NULL : Z_EXPECTED_PATH; \
2014 			_error_code = ZPP_ERROR_WRONG_ARG; \
2015 			break; \
2016 		}
2017 
2018 #define Z_PARAM_PATH(dest, dest_len) \
2019 	Z_PARAM_PATH_EX(dest, dest_len, 0, 0)
2020 
2021 #define Z_PARAM_PATH_OR_NULL(dest, dest_len) \
2022 	Z_PARAM_PATH_EX(dest, dest_len, 1, 0)
2023 
2024 /* old "P" */
2025 #define Z_PARAM_PATH_STR_EX(dest, check_null, deref) \
2026 		Z_PARAM_PROLOGUE(deref, 0); \
2027 		if (UNEXPECTED(!zend_parse_arg_path_str(_arg, &dest, check_null, _i))) { \
2028 			_expected_type = check_null ? Z_EXPECTED_PATH_OR_NULL : Z_EXPECTED_PATH; \
2029 			_error_code = ZPP_ERROR_WRONG_ARG; \
2030 			break; \
2031 		}
2032 
2033 #define Z_PARAM_PATH_STR(dest) \
2034 	Z_PARAM_PATH_STR_EX(dest, 0, 0)
2035 
2036 #define Z_PARAM_PATH_STR_OR_NULL(dest) \
2037 	Z_PARAM_PATH_STR_EX(dest, 1, 0)
2038 
2039 /* old "r" */
2040 #define Z_PARAM_RESOURCE_EX(dest, check_null, deref) \
2041 		Z_PARAM_PROLOGUE(deref, 0); \
2042 		if (UNEXPECTED(!zend_parse_arg_resource(_arg, &dest, check_null))) { \
2043 			_expected_type = check_null ? Z_EXPECTED_RESOURCE_OR_NULL : Z_EXPECTED_RESOURCE; \
2044 			_error_code = ZPP_ERROR_WRONG_ARG; \
2045 			break; \
2046 		}
2047 
2048 #define Z_PARAM_RESOURCE(dest) \
2049 	Z_PARAM_RESOURCE_EX(dest, 0, 0)
2050 
2051 #define Z_PARAM_RESOURCE_OR_NULL(dest) \
2052 	Z_PARAM_RESOURCE_EX(dest, 1, 0)
2053 
2054 /* old "s" */
2055 #define Z_PARAM_STRING_EX(dest, dest_len, check_null, deref) \
2056 		Z_PARAM_PROLOGUE(deref, 0); \
2057 		if (UNEXPECTED(!zend_parse_arg_string(_arg, &dest, &dest_len, check_null, _i))) { \
2058 			_expected_type = check_null ? Z_EXPECTED_STRING_OR_NULL : Z_EXPECTED_STRING; \
2059 			_error_code = ZPP_ERROR_WRONG_ARG; \
2060 			break; \
2061 		}
2062 
2063 #define Z_PARAM_STRING(dest, dest_len) \
2064 	Z_PARAM_STRING_EX(dest, dest_len, 0, 0)
2065 
2066 #define Z_PARAM_STRING_OR_NULL(dest, dest_len) \
2067 	Z_PARAM_STRING_EX(dest, dest_len, 1, 0)
2068 
2069 /* old "S" */
2070 #define Z_PARAM_STR_EX(dest, check_null, deref) \
2071 		Z_PARAM_PROLOGUE(deref, 0); \
2072 		if (UNEXPECTED(!zend_parse_arg_str(_arg, &dest, check_null, _i))) { \
2073 			_expected_type = check_null ? Z_EXPECTED_STRING_OR_NULL : Z_EXPECTED_STRING; \
2074 			_error_code = ZPP_ERROR_WRONG_ARG; \
2075 			break; \
2076 		}
2077 
2078 #define Z_PARAM_STR(dest) \
2079 	Z_PARAM_STR_EX(dest, 0, 0)
2080 
2081 #define Z_PARAM_STR_OR_NULL(dest) \
2082 	Z_PARAM_STR_EX(dest, 1, 0)
2083 
2084 /* old "z" */
2085 #define Z_PARAM_ZVAL_EX2(dest, check_null, deref, separate) \
2086 		Z_PARAM_PROLOGUE(deref, separate); \
2087 		zend_parse_arg_zval_deref(_arg, &dest, check_null);
2088 
2089 #define Z_PARAM_ZVAL_EX(dest, check_null, separate) \
2090 	Z_PARAM_ZVAL_EX2(dest, check_null, separate, separate)
2091 
2092 #define Z_PARAM_ZVAL(dest) \
2093 	Z_PARAM_ZVAL_EX(dest, 0, 0)
2094 
2095 #define Z_PARAM_ZVAL_OR_NULL(dest) \
2096 	Z_PARAM_ZVAL_EX(dest, 1, 0)
2097 
2098 /* old "+" and "*" */
2099 #define Z_PARAM_VARIADIC_EX(spec, dest, dest_num, post_varargs) do { \
2100 		uint32_t _num_varargs = _num_args - _i - (post_varargs); \
2101 		if (EXPECTED(_num_varargs > 0)) { \
2102 			dest = _real_arg + 1; \
2103 			dest_num = _num_varargs; \
2104 			_i += _num_varargs; \
2105 			_real_arg += _num_varargs; \
2106 		} else { \
2107 			dest = NULL; \
2108 			dest_num = 0; \
2109 		} \
2110 		if (UNEXPECTED(ZEND_CALL_INFO(execute_data) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS)) { \
2111 			_error_code = ZPP_ERROR_UNEXPECTED_EXTRA_NAMED; \
2112 			break; \
2113 		} \
2114 	} while (0);
2115 
2116 #define Z_PARAM_VARIADIC(spec, dest, dest_num) \
2117 	Z_PARAM_VARIADIC_EX(spec, dest, dest_num, 0)
2118 
2119 #define Z_PARAM_VARIADIC_WITH_NAMED(dest, dest_num, dest_named) do { \
2120 		uint32_t _num_varargs = _num_args - _i; \
2121 		if (EXPECTED(_num_varargs > 0)) { \
2122 			dest = _real_arg + 1; \
2123 			dest_num = _num_varargs; \
2124 		} else { \
2125 			dest = NULL; \
2126 			dest_num = 0; \
2127 		} \
2128 		if (ZEND_CALL_INFO(execute_data) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) { \
2129 			dest_named = execute_data->extra_named_params; \
2130 		} else { \
2131 			dest_named = NULL; \
2132 		} \
2133 	} while (0);
2134 
2135 #define Z_PARAM_ARRAY_HT_OR_STR_EX(dest_ht, dest_str, allow_null) \
2136 	Z_PARAM_PROLOGUE(0, 0); \
2137 	if (UNEXPECTED(!zend_parse_arg_array_ht_or_str(_arg, &dest_ht, &dest_str, allow_null, _i))) { \
2138 		_expected_type = allow_null ? Z_EXPECTED_ARRAY_OR_STRING_OR_NULL : Z_EXPECTED_ARRAY_OR_STRING; \
2139 		_error_code = ZPP_ERROR_WRONG_ARG; \
2140 		break; \
2141 	}
2142 
2143 #define Z_PARAM_ARRAY_HT_OR_STR(dest_ht, dest_str) \
2144 	Z_PARAM_ARRAY_HT_OR_STR_EX(dest_ht, dest_str, 0);
2145 
2146 #define Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(dest_ht, dest_str) \
2147 	Z_PARAM_ARRAY_HT_OR_STR_EX(dest_ht, dest_str, 1);
2148 
2149 #define Z_PARAM_STR_OR_LONG_EX(dest_str, dest_long, is_null, allow_null) \
2150 	Z_PARAM_PROLOGUE(0, 0); \
2151 	if (UNEXPECTED(!zend_parse_arg_str_or_long(_arg, &dest_str, &dest_long, &is_null, allow_null, _i))) { \
2152 		_expected_type = allow_null ? Z_EXPECTED_STRING_OR_LONG_OR_NULL : Z_EXPECTED_STRING_OR_LONG; \
2153 		_error_code = ZPP_ERROR_WRONG_ARG; \
2154 		break; \
2155 	}
2156 
2157 #define Z_PARAM_STR_OR_LONG(dest_str, dest_long) \
2158 	Z_PARAM_STR_OR_LONG_EX(dest_str, dest_long, _dummy, 0);
2159 
2160 #define Z_PARAM_STR_OR_LONG_OR_NULL(dest_str, dest_long, is_null) \
2161 	Z_PARAM_STR_OR_LONG_EX(dest_str, dest_long, is_null, 1);
2162 
2163 /* End of new parameter parsing API */
2164 
2165 /* Inlined implementations shared by new and old parameter parsing APIs */
2166 
2167 ZEND_API bool ZEND_FASTCALL zend_parse_arg_class(zval *arg, zend_class_entry **pce, uint32_t num, bool check_null);
2168 ZEND_API bool ZEND_FASTCALL zend_parse_arg_bool_slow(const zval *arg, bool *dest, uint32_t arg_num);
2169 ZEND_API bool ZEND_FASTCALL zend_parse_arg_bool_weak(const zval *arg, bool *dest, uint32_t arg_num);
2170 ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_slow(const zval *arg, zend_long *dest, uint32_t arg_num);
2171 ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_weak(const zval *arg, zend_long *dest, uint32_t arg_num);
2172 ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_slow(const zval *arg, double *dest, uint32_t arg_num);
2173 ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_weak(const zval *arg, double *dest, uint32_t arg_num);
2174 ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest, uint32_t arg_num);
2175 ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest, uint32_t arg_num);
2176 ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_slow(zval *arg, zval **dest, uint32_t arg_num);
2177 ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_or_str_slow(zval *arg, zval **dest, uint32_t arg_num);
2178 ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_or_long_slow(zval *arg, zend_string **dest_str, zend_long *dest_long, uint32_t arg_num);
2179 
2180 ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_bool_slow(const zval *arg, bool *dest, uint32_t arg_num);
2181 ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_str_slow(zval *arg, zend_string **dest, uint32_t arg_num);
2182 ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_long_slow(const zval *arg, zend_long *dest, uint32_t arg_num);
2183 
zend_parse_arg_bool_ex(const zval * arg,bool * dest,bool * is_null,bool check_null,uint32_t arg_num,bool frameless)2184 static zend_always_inline bool zend_parse_arg_bool_ex(const zval *arg, bool *dest, bool *is_null, bool check_null, uint32_t arg_num, bool frameless)
2185 {
2186 	if (check_null) {
2187 		*is_null = 0;
2188 	}
2189 	if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
2190 		*dest = 1;
2191 	} else if (EXPECTED(Z_TYPE_P(arg) == IS_FALSE)) {
2192 		*dest = 0;
2193 	} else if (check_null && Z_TYPE_P(arg) == IS_NULL) {
2194 		*is_null = 1;
2195 		*dest = 0;
2196 	} else {
2197 		if (frameless) {
2198 			return zend_flf_parse_arg_bool_slow(arg, dest, arg_num);
2199 		} else {
2200 			return zend_parse_arg_bool_slow(arg, dest, arg_num);
2201 		}
2202 	}
2203 	return 1;
2204 }
2205 
zend_parse_arg_bool(const zval * arg,bool * dest,bool * is_null,bool check_null,uint32_t arg_num)2206 static zend_always_inline bool zend_parse_arg_bool(const zval *arg, bool *dest, bool *is_null, bool check_null, uint32_t arg_num)
2207 {
2208 	return zend_parse_arg_bool_ex(arg, dest, is_null, check_null, arg_num, /* frameless */ false);
2209 }
2210 
zend_parse_arg_long_ex(zval * arg,zend_long * dest,bool * is_null,bool check_null,uint32_t arg_num,bool frameless)2211 static zend_always_inline bool zend_parse_arg_long_ex(zval *arg, zend_long *dest, bool *is_null, bool check_null, uint32_t arg_num, bool frameless)
2212 {
2213 	if (check_null) {
2214 		*is_null = 0;
2215 	}
2216 	if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
2217 		*dest = Z_LVAL_P(arg);
2218 	} else if (check_null && Z_TYPE_P(arg) == IS_NULL) {
2219 		*is_null = 1;
2220 		*dest = 0;
2221 	} else {
2222 		if (frameless) {
2223 			return zend_flf_parse_arg_long_slow(arg, dest, arg_num);
2224 		} else {
2225 			return zend_parse_arg_long_slow(arg, dest, arg_num);
2226 		}
2227 	}
2228 	return 1;
2229 }
2230 
zend_parse_arg_long(zval * arg,zend_long * dest,bool * is_null,bool check_null,uint32_t arg_num)2231 static zend_always_inline bool zend_parse_arg_long(zval *arg, zend_long *dest, bool *is_null, bool check_null, uint32_t arg_num)
2232 {
2233 	return zend_parse_arg_long_ex(arg, dest, is_null, check_null, arg_num, /* frameless */ false);
2234 }
2235 
zend_parse_arg_double(const zval * arg,double * dest,bool * is_null,bool check_null,uint32_t arg_num)2236 static zend_always_inline bool zend_parse_arg_double(const zval *arg, double *dest, bool *is_null, bool check_null, uint32_t arg_num)
2237 {
2238 	if (check_null) {
2239 		*is_null = 0;
2240 	}
2241 	if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
2242 		*dest = Z_DVAL_P(arg);
2243 	} else if (check_null && Z_TYPE_P(arg) == IS_NULL) {
2244 		*is_null = 1;
2245 		*dest = 0.0;
2246 	} else {
2247 		return zend_parse_arg_double_slow(arg, dest, arg_num);
2248 	}
2249 	return 1;
2250 }
2251 
zend_parse_arg_number(zval * arg,zval ** dest,bool check_null,uint32_t arg_num)2252 static zend_always_inline bool zend_parse_arg_number(zval *arg, zval **dest, bool check_null, uint32_t arg_num)
2253 {
2254 	if (EXPECTED(Z_TYPE_P(arg) == IS_LONG || Z_TYPE_P(arg) == IS_DOUBLE)) {
2255 		*dest = arg;
2256 	} else if (check_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2257 		*dest = NULL;
2258 	} else {
2259 		return zend_parse_arg_number_slow(arg, dest, arg_num);
2260 	}
2261 	return 1;
2262 }
2263 
zend_parse_arg_number_or_str(zval * arg,zval ** dest,bool check_null,uint32_t arg_num)2264 static zend_always_inline bool zend_parse_arg_number_or_str(zval *arg, zval **dest, bool check_null, uint32_t arg_num)
2265 {
2266 	if (EXPECTED(Z_TYPE_P(arg) == IS_LONG || Z_TYPE_P(arg) == IS_DOUBLE || Z_TYPE_P(arg) == IS_STRING)) {
2267 		*dest = arg;
2268 	} else if (check_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2269 		*dest = NULL;
2270 	} else {
2271 		return zend_parse_arg_number_or_str_slow(arg, dest, arg_num);
2272 	}
2273 	return true;
2274 }
2275 
zend_parse_arg_str_ex(zval * arg,zend_string ** dest,bool check_null,uint32_t arg_num,bool frameless)2276 static zend_always_inline bool zend_parse_arg_str_ex(zval *arg, zend_string **dest, bool check_null, uint32_t arg_num, bool frameless)
2277 {
2278 	if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
2279 		*dest = Z_STR_P(arg);
2280 	} else if (check_null && Z_TYPE_P(arg) == IS_NULL) {
2281 		*dest = NULL;
2282 	} else {
2283 		if (frameless) {
2284 			return zend_flf_parse_arg_str_slow(arg, dest, arg_num);
2285 		} else {
2286 			return zend_parse_arg_str_slow(arg, dest, arg_num);
2287 		}
2288 	}
2289 	return 1;
2290 }
2291 
zend_parse_arg_str(zval * arg,zend_string ** dest,bool check_null,uint32_t arg_num)2292 static zend_always_inline bool zend_parse_arg_str(zval *arg, zend_string **dest, bool check_null, uint32_t arg_num)
2293 {
2294 	return zend_parse_arg_str_ex(arg, dest, check_null, arg_num, /* frameless */ false);
2295 }
2296 
zend_parse_arg_string(zval * arg,char ** dest,size_t * dest_len,bool check_null,uint32_t arg_num)2297 static zend_always_inline bool zend_parse_arg_string(zval *arg, char **dest, size_t *dest_len, bool check_null, uint32_t arg_num)
2298 {
2299 	zend_string *str;
2300 
2301 	if (!zend_parse_arg_str(arg, &str, check_null, arg_num)) {
2302 		return 0;
2303 	}
2304 	if (check_null && UNEXPECTED(!str)) {
2305 		*dest = NULL;
2306 		*dest_len = 0;
2307 	} else {
2308 		*dest = ZSTR_VAL(str);
2309 		*dest_len = ZSTR_LEN(str);
2310 	}
2311 	return 1;
2312 }
2313 
zend_parse_arg_path_str(zval * arg,zend_string ** dest,bool check_null,uint32_t arg_num)2314 static zend_always_inline bool zend_parse_arg_path_str(zval *arg, zend_string **dest, bool check_null, uint32_t arg_num)
2315 {
2316 	if (!zend_parse_arg_str(arg, dest, check_null, arg_num) ||
2317 	    (*dest && UNEXPECTED(CHECK_NULL_PATH(ZSTR_VAL(*dest), ZSTR_LEN(*dest))))) {
2318 		return 0;
2319 	}
2320 	return 1;
2321 }
2322 
zend_parse_arg_path(zval * arg,char ** dest,size_t * dest_len,bool check_null,uint32_t arg_num)2323 static zend_always_inline bool zend_parse_arg_path(zval *arg, char **dest, size_t *dest_len, bool check_null, uint32_t arg_num)
2324 {
2325 	zend_string *str;
2326 
2327 	if (!zend_parse_arg_path_str(arg, &str, check_null, arg_num)) {
2328 		return 0;
2329 	}
2330 	if (check_null && UNEXPECTED(!str)) {
2331 		*dest = NULL;
2332 		*dest_len = 0;
2333 	} else {
2334 		*dest = ZSTR_VAL(str);
2335 		*dest_len = ZSTR_LEN(str);
2336 	}
2337 	return 1;
2338 }
2339 
zend_parse_arg_iterable(zval * arg,zval ** dest,bool check_null)2340 static zend_always_inline bool zend_parse_arg_iterable(zval *arg, zval **dest, bool check_null)
2341 {
2342 	if (EXPECTED(zend_is_iterable(arg))) {
2343 		*dest = arg;
2344 		return 1;
2345 	}
2346 
2347 	if (check_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2348 		*dest = NULL;
2349 		return 1;
2350 	}
2351 
2352 	return 0;
2353 }
2354 
zend_parse_arg_array(zval * arg,zval ** dest,bool check_null,bool or_object)2355 static zend_always_inline bool zend_parse_arg_array(zval *arg, zval **dest, bool check_null, bool or_object)
2356 {
2357 	if (EXPECTED(Z_TYPE_P(arg) == IS_ARRAY) ||
2358 		(or_object && EXPECTED(Z_TYPE_P(arg) == IS_OBJECT))) {
2359 		*dest = arg;
2360 	} else if (check_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2361 		*dest = NULL;
2362 	} else {
2363 		return 0;
2364 	}
2365 	return 1;
2366 }
2367 
zend_parse_arg_array_ht(const zval * arg,HashTable ** dest,bool check_null,bool or_object,bool separate)2368 static zend_always_inline bool zend_parse_arg_array_ht(const zval *arg, HashTable **dest, bool check_null, bool or_object, bool separate)
2369 {
2370 	if (EXPECTED(Z_TYPE_P(arg) == IS_ARRAY)) {
2371 		*dest = Z_ARRVAL_P(arg);
2372 	} else if (or_object && EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
2373 		zend_object *zobj = Z_OBJ_P(arg);
2374 		if (separate
2375 		 && zobj->properties
2376 		 && UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
2377 			if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
2378 				GC_DELREF(zobj->properties);
2379 			}
2380 			zobj->properties = zend_array_dup(zobj->properties);
2381 		}
2382 		*dest = zobj->handlers->get_properties(zobj);
2383 	} else if (check_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2384 		*dest = NULL;
2385 	} else {
2386 		return 0;
2387 	}
2388 	return 1;
2389 }
2390 
zend_parse_arg_array_ht_or_long(zval * arg,HashTable ** dest_ht,zend_long * dest_long,bool * is_null,bool allow_null,uint32_t arg_num)2391 static zend_always_inline bool zend_parse_arg_array_ht_or_long(
2392 	zval *arg, HashTable **dest_ht, zend_long *dest_long, bool *is_null, bool allow_null, uint32_t arg_num
2393 ) {
2394 	if (allow_null) {
2395 		*is_null = 0;
2396 	}
2397 
2398 	if (EXPECTED(Z_TYPE_P(arg) == IS_ARRAY)) {
2399 		*dest_ht = Z_ARRVAL_P(arg);
2400 	} else if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
2401 		*dest_ht = NULL;
2402 		*dest_long = Z_LVAL_P(arg);
2403 	} else if (allow_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2404 		*dest_ht = NULL;
2405 		*is_null = 1;
2406 	} else {
2407 		*dest_ht = NULL;
2408 		return zend_parse_arg_long_slow(arg, dest_long, arg_num);
2409 	}
2410 
2411 	return 1;
2412 }
2413 
zend_parse_arg_object(zval * arg,zval ** dest,zend_class_entry * ce,bool check_null)2414 static zend_always_inline bool zend_parse_arg_object(zval *arg, zval **dest, zend_class_entry *ce, bool check_null)
2415 {
2416 	if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT) &&
2417 	    (!ce || EXPECTED(instanceof_function(Z_OBJCE_P(arg), ce) != 0))) {
2418 		*dest = arg;
2419 	} else if (check_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2420 		*dest = NULL;
2421 	} else {
2422 		return 0;
2423 	}
2424 	return 1;
2425 }
2426 
zend_parse_arg_obj(const zval * arg,zend_object ** dest,zend_class_entry * ce,bool check_null)2427 static zend_always_inline bool zend_parse_arg_obj(const zval *arg, zend_object **dest, zend_class_entry *ce, bool check_null)
2428 {
2429 	if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT) &&
2430 	    (!ce || EXPECTED(instanceof_function(Z_OBJCE_P(arg), ce) != 0))) {
2431 		*dest = Z_OBJ_P(arg);
2432 	} else if (check_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2433 		*dest = NULL;
2434 	} else {
2435 		return 0;
2436 	}
2437 	return 1;
2438 }
2439 
zend_parse_arg_obj_or_long(zval * arg,zend_object ** dest_obj,zend_class_entry * ce,zend_long * dest_long,bool * is_null,bool allow_null,uint32_t arg_num)2440 static zend_always_inline bool zend_parse_arg_obj_or_long(
2441 	zval *arg, zend_object **dest_obj, zend_class_entry *ce, zend_long *dest_long, bool *is_null, bool allow_null, uint32_t arg_num
2442 ) {
2443 	if (allow_null) {
2444 		*is_null = 0;
2445 	}
2446 
2447 	if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT) && EXPECTED(instanceof_function(Z_OBJCE_P(arg), ce) != 0)) {
2448 		*dest_obj = Z_OBJ_P(arg);
2449 	} else if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
2450 		*dest_obj = NULL;
2451 		*dest_long = Z_LVAL_P(arg);
2452 	} else if (allow_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2453 		*dest_obj = NULL;
2454 		*is_null = 1;
2455 	} else {
2456 		*dest_obj = NULL;
2457 		return zend_parse_arg_long_slow(arg, dest_long, arg_num);
2458 	}
2459 
2460 	return 1;
2461 }
2462 
zend_parse_arg_resource(zval * arg,zval ** dest,bool check_null)2463 static zend_always_inline bool zend_parse_arg_resource(zval *arg, zval **dest, bool check_null)
2464 {
2465 	if (EXPECTED(Z_TYPE_P(arg) == IS_RESOURCE)) {
2466 		*dest = arg;
2467 	} else if (check_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2468 		*dest = NULL;
2469 	} else {
2470 		return 0;
2471 	}
2472 	return 1;
2473 }
2474 
zend_parse_arg_func(zval * arg,zend_fcall_info * dest_fci,zend_fcall_info_cache * dest_fcc,bool check_null,char ** error,bool free_trampoline)2475 static zend_always_inline bool zend_parse_arg_func(zval *arg, zend_fcall_info *dest_fci, zend_fcall_info_cache *dest_fcc, bool check_null, char **error, bool free_trampoline)
2476 {
2477 	if (check_null && UNEXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2478 		dest_fci->size = 0;
2479 		dest_fcc->function_handler = NULL;
2480 		*error = NULL;
2481 	} else if (UNEXPECTED(zend_fcall_info_init(arg, 0, dest_fci, dest_fcc, NULL, error) != SUCCESS)) {
2482 		return 0;
2483 	}
2484 	if (free_trampoline) {
2485 		/* Release call trampolines: The function may not get called, in which case
2486 		 * the trampoline will leak. Force it to be refetched during
2487 		 * zend_call_function instead. */
2488 		zend_release_fcall_info_cache(dest_fcc);
2489 	}
2490 	return 1;
2491 }
2492 
zend_parse_arg_zval(zval * arg,zval ** dest,bool check_null)2493 static zend_always_inline void zend_parse_arg_zval(zval *arg, zval **dest, bool check_null)
2494 {
2495 	*dest = (check_null &&
2496 	    (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) ||
2497 	     (UNEXPECTED(Z_ISREF_P(arg)) &&
2498 	      UNEXPECTED(Z_TYPE_P(Z_REFVAL_P(arg)) == IS_NULL)))) ? NULL : arg;
2499 }
2500 
zend_parse_arg_zval_deref(zval * arg,zval ** dest,bool check_null)2501 static zend_always_inline void zend_parse_arg_zval_deref(zval *arg, zval **dest, bool check_null)
2502 {
2503 	*dest = (check_null && UNEXPECTED(Z_TYPE_P(arg) == IS_NULL)) ? NULL : arg;
2504 }
2505 
zend_parse_arg_array_ht_or_str(zval * arg,HashTable ** dest_ht,zend_string ** dest_str,bool allow_null,uint32_t arg_num)2506 static zend_always_inline bool zend_parse_arg_array_ht_or_str(
2507 		zval *arg, HashTable **dest_ht, zend_string **dest_str, bool allow_null, uint32_t arg_num)
2508 {
2509 	if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
2510 		*dest_ht = NULL;
2511 		*dest_str = Z_STR_P(arg);
2512 	} else if (EXPECTED(Z_TYPE_P(arg) == IS_ARRAY)) {
2513 		*dest_ht = Z_ARRVAL_P(arg);
2514 		*dest_str = NULL;
2515 	} else if (allow_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2516 		*dest_ht = NULL;
2517 		*dest_str = NULL;
2518 	} else {
2519 		*dest_ht = NULL;
2520 		return zend_parse_arg_str_slow(arg, dest_str, arg_num);
2521 	}
2522 	return 1;
2523 }
2524 
zend_parse_arg_str_or_long(zval * arg,zend_string ** dest_str,zend_long * dest_long,bool * is_null,bool allow_null,uint32_t arg_num)2525 static zend_always_inline bool zend_parse_arg_str_or_long(zval *arg, zend_string **dest_str, zend_long *dest_long,
2526 	bool *is_null, bool allow_null, uint32_t arg_num)
2527 {
2528 	if (allow_null) {
2529 		*is_null = 0;
2530 	}
2531 	if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
2532 		*dest_str = Z_STR_P(arg);
2533 	} else if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
2534 		*dest_str = NULL;
2535 		*dest_long = Z_LVAL_P(arg);
2536 	} else if (allow_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2537 		*dest_str = NULL;
2538 		*is_null = 1;
2539 	} else {
2540 		return zend_parse_arg_str_or_long_slow(arg, dest_str, dest_long, arg_num);
2541 	}
2542 	return 1;
2543 }
2544 
zend_parse_arg_obj_or_class_name(zval * arg,zend_class_entry ** destination,bool allow_null)2545 static zend_always_inline bool zend_parse_arg_obj_or_class_name(
2546 	zval *arg, zend_class_entry **destination, bool allow_null
2547 ) {
2548 	if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
2549 		*destination = zend_lookup_class(Z_STR_P(arg));
2550 
2551 		return *destination != NULL;
2552 	} else if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
2553 		*destination = Z_OBJ_P(arg)->ce;
2554 	} else if (allow_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
2555 		*destination = NULL;
2556 	} else {
2557 		return 0;
2558 	}
2559 
2560 	return 1;
2561 }
2562 
zend_parse_arg_obj_or_str(zval * arg,zend_object ** destination_object,zend_class_entry * base_ce,zend_string ** destination_string,bool allow_null,uint32_t arg_num)2563 static zend_always_inline bool zend_parse_arg_obj_or_str(
2564 	zval *arg, zend_object **destination_object, zend_class_entry *base_ce, zend_string **destination_string, bool allow_null, uint32_t arg_num
2565 ) {
2566 	if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
2567 		if (!base_ce || EXPECTED(instanceof_function(Z_OBJCE_P(arg), base_ce))) {
2568 			*destination_object = Z_OBJ_P(arg);
2569 			*destination_string = NULL;
2570 			return 1;
2571 		}
2572 	}
2573 
2574 	*destination_object = NULL;
2575 	return zend_parse_arg_str(arg, destination_string, allow_null, arg_num);
2576 }
2577 
2578 END_EXTERN_C()
2579 
2580 #endif /* ZEND_API_H */
2581