xref: /php-src/Zend/zend_API.c (revision f2e199e8)
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 #include "zend.h"
23 #include "zend_execute.h"
24 #include "zend_API.h"
25 #include "zend_modules.h"
26 #include "zend_extensions.h"
27 #include "zend_constants.h"
28 #include "zend_interfaces.h"
29 #include "zend_exceptions.h"
30 #include "zend_closures.h"
31 #include "zend_inheritance.h"
32 #include "zend_ini.h"
33 #include "zend_enum.h"
34 #include "zend_observer.h"
35 
36 #include <stdarg.h>
37 
38 /* these variables are true statics/globals, and have to be mutex'ed on every access */
39 ZEND_API HashTable module_registry;
40 
41 static zend_module_entry **module_request_startup_handlers;
42 static zend_module_entry **module_request_shutdown_handlers;
43 static zend_module_entry **module_post_deactivate_handlers;
44 static zend_module_entry **modules_dl_loaded;
45 
46 static zend_class_entry  **class_cleanup_handlers;
47 
zend_get_parameters_array_ex(uint32_t param_count,zval * argument_array)48 ZEND_API zend_result zend_get_parameters_array_ex(uint32_t param_count, zval *argument_array) /* {{{ */
49 {
50 	zval *param_ptr;
51 	uint32_t arg_count;
52 
53 	param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
54 	arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
55 
56 	if (param_count>arg_count) {
57 		return FAILURE;
58 	}
59 
60 	while (param_count-->0) {
61 		ZVAL_COPY_VALUE(argument_array, param_ptr);
62 		argument_array++;
63 		param_ptr++;
64 	}
65 
66 	return SUCCESS;
67 }
68 /* }}} */
69 
zend_copy_parameters_array(uint32_t param_count,zval * argument_array)70 ZEND_API zend_result zend_copy_parameters_array(uint32_t param_count, zval *argument_array) /* {{{ */
71 {
72 	zval *param_ptr;
73 	uint32_t arg_count;
74 
75 	param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
76 	arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
77 
78 	if (param_count>arg_count) {
79 		return FAILURE;
80 	}
81 
82 	while (param_count-->0) {
83 		Z_TRY_ADDREF_P(param_ptr);
84 		zend_hash_next_index_insert_new(Z_ARRVAL_P(argument_array), param_ptr);
85 		param_ptr++;
86 	}
87 
88 	return SUCCESS;
89 }
90 /* }}} */
91 
zend_wrong_param_count(void)92 ZEND_API ZEND_COLD void zend_wrong_param_count(void) /* {{{ */
93 {
94 	const char *space;
95 	const char *class_name = get_active_class_name(&space);
96 
97 	zend_argument_count_error("Wrong parameter count for %s%s%s()", class_name, space, get_active_function_name());
98 }
99 /* }}} */
100 
zend_wrong_property_read(zval * object,zval * property)101 ZEND_API ZEND_COLD void zend_wrong_property_read(zval *object, zval *property)
102 {
103 	zend_string *tmp_property_name;
104 	zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name);
105 	zend_error(E_WARNING, "Attempt to read property \"%s\" on %s", ZSTR_VAL(property_name), zend_zval_value_name(object));
106 	zend_tmp_string_release(tmp_property_name);
107 }
108 
109 /* Argument parsing API -- andrei */
zend_get_type_by_const(int type)110 ZEND_API const char *zend_get_type_by_const(int type) /* {{{ */
111 {
112 	switch(type) {
113 		case IS_FALSE:
114 		case IS_TRUE:
115 		case _IS_BOOL:
116 			return "bool";
117 		case IS_LONG:
118 			return "int";
119 		case IS_DOUBLE:
120 			return "float";
121 		case IS_STRING:
122 			return "string";
123 		case IS_OBJECT:
124 			return "object";
125 		case IS_RESOURCE:
126 			return "resource";
127 		case IS_NULL:
128 			return "null";
129 		case IS_CALLABLE:
130 			return "callable";
131 		case IS_ITERABLE:
132 			return "iterable";
133 		case IS_ARRAY:
134 			return "array";
135 		case IS_VOID:
136 			return "void";
137 		case IS_MIXED:
138 			return "mixed";
139 		case _IS_NUMBER:
140 			return "int|float";
141 		EMPTY_SWITCH_DEFAULT_CASE()
142 	}
143 }
144 /* }}} */
145 
zend_zval_value_name(const zval * arg)146 ZEND_API const char *zend_zval_value_name(const zval *arg)
147 {
148 	ZVAL_DEREF(arg);
149 
150 	if (Z_ISUNDEF_P(arg)) {
151 		return "null";
152 	}
153 
154 	if (Z_TYPE_P(arg) == IS_OBJECT) {
155 		return ZSTR_VAL(Z_OBJCE_P(arg)->name);
156 	} else if (Z_TYPE_P(arg) == IS_FALSE) {
157 		return "false";
158 	} else if  (Z_TYPE_P(arg) == IS_TRUE) {
159 		return "true";
160 	}
161 
162 	return zend_get_type_by_const(Z_TYPE_P(arg));
163 }
164 
zend_zval_type_name(const zval * arg)165 ZEND_API const char *zend_zval_type_name(const zval *arg)
166 {
167 	ZVAL_DEREF(arg);
168 
169 	if (Z_ISUNDEF_P(arg)) {
170 		return "null";
171 	}
172 
173 	if (Z_TYPE_P(arg) == IS_OBJECT) {
174 		return ZSTR_VAL(Z_OBJCE_P(arg)->name);
175 	}
176 
177 	return zend_get_type_by_const(Z_TYPE_P(arg));
178 }
179 
180 /* This API exists *only* for use in gettype().
181  * For anything else, you likely want zend_zval_type_name(). */
zend_zval_get_legacy_type(const zval * arg)182 ZEND_API zend_string *zend_zval_get_legacy_type(const zval *arg) /* {{{ */
183 {
184 	switch (Z_TYPE_P(arg)) {
185 		case IS_NULL:
186 			return ZSTR_KNOWN(ZEND_STR_NULL);
187 		case IS_FALSE:
188 		case IS_TRUE:
189 			return ZSTR_KNOWN(ZEND_STR_BOOLEAN);
190 		case IS_LONG:
191 			return ZSTR_KNOWN(ZEND_STR_INTEGER);
192 		case IS_DOUBLE:
193 			return ZSTR_KNOWN(ZEND_STR_DOUBLE);
194 		case IS_STRING:
195 			return ZSTR_KNOWN(ZEND_STR_STRING);
196 		case IS_ARRAY:
197 			return ZSTR_KNOWN(ZEND_STR_ARRAY);
198 		case IS_OBJECT:
199 			return ZSTR_KNOWN(ZEND_STR_OBJECT);
200 		case IS_RESOURCE:
201 			if (zend_rsrc_list_get_rsrc_type(Z_RES_P(arg))) {
202 				return ZSTR_KNOWN(ZEND_STR_RESOURCE);
203 			} else {
204 				return ZSTR_KNOWN(ZEND_STR_CLOSED_RESOURCE);
205 			}
206 		default:
207 			return NULL;
208 	}
209 }
210 /* }}} */
211 
zend_wrong_parameters_none_error(void)212 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_none_error(void) /* {{{ */
213 {
214 	int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
215 	zend_string *func_name = get_active_function_or_method_name();
216 
217 	zend_argument_count_error("%s() expects exactly 0 arguments, %d given", ZSTR_VAL(func_name), num_args);
218 
219 	zend_string_release(func_name);
220 }
221 /* }}} */
222 
zend_wrong_parameters_count_error(uint32_t min_num_args,uint32_t max_num_args)223 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_error(uint32_t min_num_args, uint32_t max_num_args) /* {{{ */
224 {
225 	uint32_t num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
226 	zend_string *func_name = get_active_function_or_method_name();
227 
228 	zend_argument_count_error(
229 		"%s() expects %s %d argument%s, %d given",
230 		ZSTR_VAL(func_name),
231 		min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
232 		num_args < min_num_args ? min_num_args : max_num_args,
233 		(num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
234 		num_args
235 	);
236 
237 	zend_string_release(func_name);
238 }
239 /* }}} */
240 
zend_wrong_parameter_error(int error_code,uint32_t num,char * name,zend_expected_type expected_type,zval * arg)241 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) /* {{{ */
242 {
243 	switch (error_code) {
244 		case ZPP_ERROR_WRONG_CALLBACK:
245 			zend_wrong_callback_error(num, name);
246 			break;
247 		case ZPP_ERROR_WRONG_CALLBACK_OR_NULL:
248 			zend_wrong_callback_or_null_error(num, name);
249 			break;
250 		case ZPP_ERROR_WRONG_CLASS:
251 			zend_wrong_parameter_class_error(num, name, arg);
252 			break;
253 		case ZPP_ERROR_WRONG_CLASS_OR_NULL:
254 			zend_wrong_parameter_class_or_null_error(num, name, arg);
255 			break;
256 		case ZPP_ERROR_WRONG_CLASS_OR_STRING:
257 			zend_wrong_parameter_class_or_string_error(num, name, arg);
258 			break;
259 		case ZPP_ERROR_WRONG_CLASS_OR_STRING_OR_NULL:
260 			zend_wrong_parameter_class_or_string_or_null_error(num, name, arg);
261 			break;
262 		case ZPP_ERROR_WRONG_CLASS_OR_LONG:
263 			zend_wrong_parameter_class_or_long_error(num, name, arg);
264 			break;
265 		case ZPP_ERROR_WRONG_CLASS_OR_LONG_OR_NULL:
266 			zend_wrong_parameter_class_or_long_or_null_error(num, name, arg);
267 			break;
268 		case ZPP_ERROR_WRONG_ARG:
269 			zend_wrong_parameter_type_error(num, expected_type, arg);
270 			break;
271 		case ZPP_ERROR_UNEXPECTED_EXTRA_NAMED:
272 			zend_unexpected_extra_named_error();
273 			break;
274 		case ZPP_ERROR_FAILURE:
275 			ZEND_ASSERT(EG(exception) && "Should have produced an error already");
276 			break;
277 		EMPTY_SWITCH_DEFAULT_CASE()
278 	}
279 }
280 /* }}} */
281 
zend_wrong_parameter_type_error(uint32_t num,zend_expected_type expected_type,zval * arg)282 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_type_error(uint32_t num, zend_expected_type expected_type, zval *arg) /* {{{ */
283 {
284 	static const char * const expected_error[] = {
285 		Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR)
286 		NULL
287 	};
288 
289 	if (EG(exception)) {
290 		return;
291 	}
292 
293 	if ((expected_type == Z_EXPECTED_PATH || expected_type == Z_EXPECTED_PATH_OR_NULL)
294 			&& Z_TYPE_P(arg) == IS_STRING) {
295 		zend_argument_value_error(num, "must not contain any null bytes");
296 		return;
297 	}
298 
299 	zend_argument_type_error(num, "must be %s, %s given", expected_error[expected_type], zend_zval_value_name(arg));
300 }
301 /* }}} */
302 
zend_wrong_parameter_class_error(uint32_t num,const char * name,zval * arg)303 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_error(uint32_t num, const char *name, zval *arg) /* {{{ */
304 {
305 	if (EG(exception)) {
306 		return;
307 	}
308 
309 	zend_argument_type_error(num, "must be of type %s, %s given", name, zend_zval_value_name(arg));
310 }
311 /* }}} */
312 
zend_wrong_parameter_class_or_null_error(uint32_t num,const char * name,zval * arg)313 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_null_error(uint32_t num, const char *name, zval *arg) /* {{{ */
314 {
315 	if (EG(exception)) {
316 		return;
317 	}
318 
319 	zend_argument_type_error(num, "must be of type ?%s, %s given", name, zend_zval_value_name(arg));
320 }
321 /* }}} */
322 
zend_wrong_parameter_class_or_long_error(uint32_t num,const char * name,zval * arg)323 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_long_error(uint32_t num, const char *name, zval *arg) /* {{{ */
324 {
325 	if (EG(exception)) {
326 		return;
327 	}
328 
329 	zend_argument_type_error(num, "must be of type %s|int, %s given", name, zend_zval_value_name(arg));
330 }
331 /* }}} */
332 
zend_wrong_parameter_class_or_long_or_null_error(uint32_t num,const char * name,zval * arg)333 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_long_or_null_error(uint32_t num, const char *name, zval *arg) /* {{{ */
334 {
335 	if (EG(exception)) {
336 		return;
337 	}
338 
339 	zend_argument_type_error(num, "must be of type %s|int|null, %s given", name, zend_zval_value_name(arg));
340 }
341 /* }}} */
342 
zend_wrong_parameter_class_or_string_error(uint32_t num,const char * name,zval * arg)343 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_string_error(uint32_t num, const char *name, zval *arg) /* {{{ */
344 {
345 	if (EG(exception)) {
346 		return;
347 	}
348 
349 	zend_argument_type_error(num, "must be of type %s|string, %s given", name, zend_zval_value_name(arg));
350 }
351 /* }}} */
352 
zend_wrong_parameter_class_or_string_or_null_error(uint32_t num,const char * name,zval * arg)353 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_string_or_null_error(uint32_t num, const char *name, zval *arg) /* {{{ */
354 {
355 	if (EG(exception)) {
356 		return;
357 	}
358 
359 	zend_argument_type_error(num, "must be of type %s|string|null, %s given", name, zend_zval_value_name(arg));
360 }
361 /* }}} */
362 
zend_wrong_callback_error(uint32_t num,char * error)363 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_error(uint32_t num, char *error) /* {{{ */
364 {
365 	if (!EG(exception)) {
366 		zend_argument_type_error(num, "must be a valid callback, %s", error);
367 	}
368 	efree(error);
369 }
370 /* }}} */
371 
zend_wrong_callback_or_null_error(uint32_t num,char * error)372 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_or_null_error(uint32_t num, char *error) /* {{{ */
373 {
374 	if (!EG(exception)) {
375 		zend_argument_type_error(num, "must be a valid callback or null, %s", error);
376 	}
377 	efree(error);
378 }
379 /* }}} */
380 
zend_unexpected_extra_named_error(void)381 ZEND_API ZEND_COLD void ZEND_FASTCALL zend_unexpected_extra_named_error(void)
382 {
383 	const char *space;
384 	const char *class_name = get_active_class_name(&space);
385 	zend_argument_count_error("%s%s%s() does not accept unknown named parameters",
386 		class_name, space, get_active_function_name());
387 }
388 
zend_argument_error_variadic(zend_class_entry * error_ce,uint32_t arg_num,const char * format,va_list va)389 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) /* {{{ */
390 {
391 	zend_string *func_name;
392 	const char *arg_name;
393 	char *message = NULL;
394 	if (EG(exception)) {
395 		return;
396 	}
397 
398 	func_name = get_active_function_or_method_name();
399 	arg_name = get_active_function_arg_name(arg_num);
400 
401 	zend_vspprintf(&message, 0, format, va);
402 	zend_throw_error(error_ce, "%s(): Argument #%d%s%s%s %s",
403 		ZSTR_VAL(func_name), arg_num,
404 		arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "", message
405 	);
406 	efree(message);
407 	zend_string_release(func_name);
408 }
409 /* }}} */
410 
zend_argument_error(zend_class_entry * error_ce,uint32_t arg_num,const char * format,...)411 ZEND_API ZEND_COLD void zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format, ...) /* {{{ */
412 {
413 	va_list va;
414 
415 	va_start(va, format);
416 	zend_argument_error_variadic(error_ce, arg_num, format, va);
417 	va_end(va);
418 }
419 /* }}} */
420 
zend_argument_type_error(uint32_t arg_num,const char * format,...)421 ZEND_API ZEND_COLD void zend_argument_type_error(uint32_t arg_num, const char *format, ...) /* {{{ */
422 {
423 	va_list va;
424 
425 	va_start(va, format);
426 	zend_argument_error_variadic(zend_ce_type_error, arg_num, format, va);
427 	va_end(va);
428 }
429 /* }}} */
430 
zend_argument_value_error(uint32_t arg_num,const char * format,...)431 ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format, ...) /* {{{ */
432 {
433 	va_list va;
434 
435 	va_start(va, format);
436 	zend_argument_error_variadic(zend_ce_value_error, arg_num, format, va);
437 	va_end(va);
438 }
439 /* }}} */
440 
zend_parse_arg_class(zval * arg,zend_class_entry ** pce,uint32_t num,bool check_null)441 ZEND_API bool ZEND_FASTCALL zend_parse_arg_class(zval *arg, zend_class_entry **pce, uint32_t num, bool check_null) /* {{{ */
442 {
443 	zend_class_entry *ce_base = *pce;
444 
445 	if (check_null && Z_TYPE_P(arg) == IS_NULL) {
446 		*pce = NULL;
447 		return 1;
448 	}
449 	if (!try_convert_to_string(arg)) {
450 		*pce = NULL;
451 		return 0;
452 	}
453 
454 	*pce = zend_lookup_class(Z_STR_P(arg));
455 	if (ce_base) {
456 		if ((!*pce || !instanceof_function(*pce, ce_base))) {
457 			zend_argument_type_error(num, "must be a class name derived from %s, %s given", ZSTR_VAL(ce_base->name), Z_STRVAL_P(arg));
458 			*pce = NULL;
459 			return 0;
460 		}
461 	}
462 	if (!*pce) {
463 		zend_argument_type_error(num, "must be a valid class name, %s given", Z_STRVAL_P(arg));
464 		return 0;
465 	}
466 	return 1;
467 }
468 /* }}} */
469 
zend_null_arg_deprecated(const char * fallback_type,uint32_t arg_num)470 static ZEND_COLD bool zend_null_arg_deprecated(const char *fallback_type, uint32_t arg_num) {
471 	zend_function *func = zend_active_function();
472 	ZEND_ASSERT(arg_num > 0);
473 	uint32_t arg_offset = arg_num - 1;
474 	if (arg_offset >= func->common.num_args) {
475 		ZEND_ASSERT(func->common.fn_flags & ZEND_ACC_VARIADIC);
476 		arg_offset = func->common.num_args;
477 	}
478 
479 	zend_arg_info *arg_info = &func->common.arg_info[arg_offset];
480 	zend_string *func_name = get_active_function_or_method_name();
481 	const char *arg_name = get_active_function_arg_name(arg_num);
482 
483 	/* If no type is specified in arginfo, use the specified fallback_type determined through
484 	 * zend_parse_parameters instead. */
485 	zend_string *type_str = zend_type_to_string(arg_info->type);
486 	const char *type = type_str ? ZSTR_VAL(type_str) : fallback_type;
487 	zend_error(E_DEPRECATED,
488 		"%s(): Passing null to parameter #%" PRIu32 "%s%s%s of type %s is deprecated",
489 		ZSTR_VAL(func_name), arg_num,
490 		arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "",
491 		type);
492 	zend_string_release(func_name);
493 	if (type_str) {
494 		zend_string_release(type_str);
495 	}
496 	return !EG(exception);
497 }
498 
zend_parse_arg_bool_weak(const zval * arg,bool * dest,uint32_t arg_num)499 ZEND_API bool ZEND_FASTCALL zend_parse_arg_bool_weak(const zval *arg, bool *dest, uint32_t arg_num) /* {{{ */
500 {
501 	if (EXPECTED(Z_TYPE_P(arg) <= IS_STRING)) {
502 		if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("bool", arg_num)) {
503 			return 0;
504 		}
505 		*dest = zend_is_true(arg);
506 	} else {
507 		return 0;
508 	}
509 	return 1;
510 }
511 /* }}} */
512 
zend_parse_arg_bool_slow(const zval * arg,bool * dest,uint32_t arg_num)513 ZEND_API bool ZEND_FASTCALL zend_parse_arg_bool_slow(const zval *arg, bool *dest, uint32_t arg_num) /* {{{ */
514 {
515 	if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
516 		return 0;
517 	}
518 	return zend_parse_arg_bool_weak(arg, dest, arg_num);
519 }
520 /* }}} */
521 
zend_flf_parse_arg_bool_slow(const zval * arg,bool * dest,uint32_t arg_num)522 ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_bool_slow(const zval *arg, bool *dest, uint32_t arg_num)
523 {
524 	if (UNEXPECTED(ZEND_FLF_ARG_USES_STRICT_TYPES())) {
525 		return 0;
526 	}
527 	return zend_parse_arg_bool_weak(arg, dest, arg_num);
528 }
529 
zend_parse_arg_long_weak(const zval * arg,zend_long * dest,uint32_t arg_num)530 ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_weak(const zval *arg, zend_long *dest, uint32_t arg_num) /* {{{ */
531 {
532 	if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
533 		if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
534 			return 0;
535 		}
536 		if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
537 			return 0;
538 		} else {
539 			zend_long lval = zend_dval_to_lval(Z_DVAL_P(arg));
540 			if (UNEXPECTED(!zend_is_long_compatible(Z_DVAL_P(arg), lval))) {
541 				/* Check arg_num is not (uint32_t)-1, as otherwise its called by
542 				 * zend_verify_weak_scalar_type_hint_no_sideeffect() */
543 				if (arg_num != (uint32_t)-1) {
544 					zend_incompatible_double_to_long_error(Z_DVAL_P(arg));
545 				}
546 				if (UNEXPECTED(EG(exception))) {
547 					return 0;
548 				}
549 			}
550 			*dest = lval;
551 		}
552 	} else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
553 		double d;
554 		uint8_t type;
555 
556 		if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
557 			if (EXPECTED(type != 0)) {
558 				zend_long lval;
559 				if (UNEXPECTED(zend_isnan(d))) {
560 					return 0;
561 				}
562 				if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
563 					return 0;
564 				}
565 
566 				lval = zend_dval_to_lval(d);
567 				/* This only checks for a fractional part as if doesn't fit it already throws a TypeError */
568 				if (UNEXPECTED(!zend_is_long_compatible(d, lval))) {
569 					/* Check arg_num is not (uint32_t)-1, as otherwise its called by
570 					 * zend_verify_weak_scalar_type_hint_no_sideeffect() */
571 					if (arg_num != (uint32_t)-1) {
572 						zend_incompatible_string_to_long_error(Z_STR_P(arg));
573 					}
574 					if (UNEXPECTED(EG(exception))) {
575 						return 0;
576 					}
577 				}
578 				*dest = lval;
579 			} else {
580 				return 0;
581 			}
582 		}
583 		if (UNEXPECTED(EG(exception))) {
584 			return 0;
585 		}
586 	} else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
587 		if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("int", arg_num)) {
588 			return 0;
589 		}
590 		*dest = 0;
591 	} else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
592 		*dest = 1;
593 	} else {
594 		return 0;
595 	}
596 	return 1;
597 }
598 /* }}} */
599 
zend_parse_arg_long_slow(const zval * arg,zend_long * dest,uint32_t arg_num)600 ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_slow(const zval *arg, zend_long *dest, uint32_t arg_num) /* {{{ */
601 {
602 	if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
603 		return 0;
604 	}
605 	return zend_parse_arg_long_weak(arg, dest, arg_num);
606 }
607 /* }}} */
608 
zend_flf_parse_arg_long_slow(const zval * arg,zend_long * dest,uint32_t arg_num)609 ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_long_slow(const zval *arg, zend_long *dest, uint32_t arg_num)
610 {
611 	if (UNEXPECTED(ZEND_FLF_ARG_USES_STRICT_TYPES())) {
612 		return 0;
613 	}
614 	return zend_parse_arg_long_weak(arg, dest, arg_num);
615 }
616 
zend_parse_arg_double_weak(const zval * arg,double * dest,uint32_t arg_num)617 ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_weak(const zval *arg, double *dest, uint32_t arg_num) /* {{{ */
618 {
619 	if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
620 		*dest = (double)Z_LVAL_P(arg);
621 	} else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
622 		zend_long l;
623 		uint8_t type;
624 
625 		if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), &l, dest)) != IS_DOUBLE)) {
626 			if (EXPECTED(type != 0)) {
627 				*dest = (double)(l);
628 			} else {
629 				return 0;
630 			}
631 		}
632 		if (UNEXPECTED(EG(exception))) {
633 			return 0;
634 		}
635 	} else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
636 		if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("float", arg_num)) {
637 			return 0;
638 		}
639 		*dest = 0.0;
640 	} else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
641 		*dest = 1.0;
642 	} else {
643 		return 0;
644 	}
645 	return 1;
646 }
647 /* }}} */
648 
zend_parse_arg_double_slow(const zval * arg,double * dest,uint32_t arg_num)649 ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_slow(const zval *arg, double *dest, uint32_t arg_num) /* {{{ */
650 {
651 	if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
652 		/* SSTH Exception: IS_LONG may be accepted instead as IS_DOUBLE */
653 		*dest = (double)Z_LVAL_P(arg);
654 	} else if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
655 		return 0;
656 	}
657 	return zend_parse_arg_double_weak(arg, dest, arg_num);
658 }
659 /* }}} */
660 
zend_parse_arg_number_slow(zval * arg,zval ** dest,uint32_t arg_num)661 ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_slow(zval *arg, zval **dest, uint32_t arg_num) /* {{{ */
662 {
663 	if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
664 		return 0;
665 	}
666 	if (Z_TYPE_P(arg) == IS_STRING) {
667 		zend_string *str = Z_STR_P(arg);
668 		zend_long lval;
669 		double dval;
670 		uint8_t type = is_numeric_str_function(str, &lval, &dval);
671 		if (type == IS_LONG) {
672 			ZVAL_LONG(arg, lval);
673 		} else if (type == IS_DOUBLE) {
674 			ZVAL_DOUBLE(arg, dval);
675 		} else {
676 			return 0;
677 		}
678 		zend_string_release(str);
679 	} else if (Z_TYPE_P(arg) < IS_TRUE) {
680 		if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("int|float", arg_num)) {
681 			return 0;
682 		}
683 		ZVAL_LONG(arg, 0);
684 	} else if (Z_TYPE_P(arg) == IS_TRUE) {
685 		ZVAL_LONG(arg, 1);
686 	} else {
687 		return 0;
688 	}
689 	*dest = arg;
690 	return 1;
691 }
692 /* }}} */
693 
694 
zend_parse_arg_number_or_str_slow(zval * arg,zval ** dest,uint32_t arg_num)695 ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_or_str_slow(zval *arg, zval **dest, uint32_t arg_num) /* {{{ */
696 {
697 	if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
698 		return false;
699 	}
700 	if (Z_TYPE_P(arg) < IS_TRUE) {
701 		if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("string|int|float", arg_num)) {
702 			return false;
703 		}
704 		ZVAL_LONG(arg, 0);
705 	} else if (Z_TYPE_P(arg) == IS_TRUE) {
706 		ZVAL_LONG(arg, 1);
707 	} else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
708 		zend_object *zobj = Z_OBJ_P(arg);
709 		zval obj;
710 		if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) {
711 			OBJ_RELEASE(zobj);
712 			ZVAL_COPY_VALUE(arg, &obj);
713 			*dest = arg;
714 			return true;
715 		}
716 		return false;
717 	} else {
718 		return false;
719 	}
720 	*dest = arg;
721 	return true;
722 }
723 
zend_parse_arg_str_weak(zval * arg,zend_string ** dest,uint32_t arg_num)724 ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest, uint32_t arg_num) /* {{{ */
725 {
726 	if (EXPECTED(Z_TYPE_P(arg) < IS_STRING)) {
727 		if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("string", arg_num)) {
728 			return 0;
729 		}
730 		convert_to_string(arg);
731 		*dest = Z_STR_P(arg);
732 	} else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
733 		zend_object *zobj = Z_OBJ_P(arg);
734 		zval obj;
735 		if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) {
736 			OBJ_RELEASE(zobj);
737 			ZVAL_COPY_VALUE(arg, &obj);
738 			*dest = Z_STR_P(arg);
739 			return 1;
740 		}
741 		return 0;
742 	} else {
743 		return 0;
744 	}
745 	return 1;
746 }
747 /* }}} */
748 
zend_parse_arg_str_slow(zval * arg,zend_string ** dest,uint32_t arg_num)749 ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest, uint32_t arg_num) /* {{{ */
750 {
751 	if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
752 		return 0;
753 	}
754 	return zend_parse_arg_str_weak(arg, dest, arg_num);
755 }
756 /* }}} */
757 
zend_flf_parse_arg_str_slow(zval * arg,zend_string ** dest,uint32_t arg_num)758 ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_str_slow(zval *arg, zend_string **dest, uint32_t arg_num)
759 {
760 	if (UNEXPECTED(ZEND_FLF_ARG_USES_STRICT_TYPES())) {
761 		return 0;
762 	}
763 	return zend_parse_arg_str_weak(arg, dest, arg_num);
764 }
765 
zend_parse_arg_str_or_long_slow(zval * arg,zend_string ** dest_str,zend_long * dest_long,uint32_t arg_num)766 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) /* {{{ */
767 {
768 	if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
769 		return 0;
770 	}
771 	if (zend_parse_arg_long_weak(arg, dest_long, arg_num)) {
772 		*dest_str = NULL;
773 		return 1;
774 	} else if (zend_parse_arg_str_weak(arg, dest_str, arg_num)) {
775 		*dest_long = 0;
776 		return 1;
777 	} else {
778 		return 0;
779 	}
780 }
781 /* }}} */
782 
zend_parse_arg_impl(zval * arg,va_list * va,const char ** spec,char ** error,uint32_t arg_num)783 static const char *zend_parse_arg_impl(zval *arg, va_list *va, const char **spec, char **error, uint32_t arg_num) /* {{{ */
784 {
785 	const char *spec_walk = *spec;
786 	char c = *spec_walk++;
787 	bool check_null = 0;
788 	bool separate = 0;
789 	zval *real_arg = arg;
790 
791 	/* scan through modifiers */
792 	ZVAL_DEREF(arg);
793 	while (1) {
794 		if (*spec_walk == '/') {
795 			SEPARATE_ZVAL_NOREF(arg);
796 			real_arg = arg;
797 			separate = 1;
798 		} else if (*spec_walk == '!') {
799 			check_null = 1;
800 		} else {
801 			break;
802 		}
803 		spec_walk++;
804 	}
805 
806 	switch (c) {
807 		case 'l':
808 			{
809 				zend_long *p = va_arg(*va, zend_long *);
810 				bool *is_null = NULL;
811 
812 				if (check_null) {
813 					is_null = va_arg(*va, bool *);
814 				}
815 
816 				if (!zend_parse_arg_long(arg, p, is_null, check_null, arg_num)) {
817 					return check_null ? "?int" : "int";
818 				}
819 			}
820 			break;
821 
822 		case 'd':
823 			{
824 				double *p = va_arg(*va, double *);
825 				bool *is_null = NULL;
826 
827 				if (check_null) {
828 					is_null = va_arg(*va, bool *);
829 				}
830 
831 				if (!zend_parse_arg_double(arg, p, is_null, check_null, arg_num)) {
832 					return check_null ? "?float" : "float";
833 				}
834 			}
835 			break;
836 
837 		case 'n':
838 			{
839 				zval **p = va_arg(*va, zval **);
840 
841 				if (!zend_parse_arg_number(arg, p, check_null, arg_num)) {
842 					return check_null ? "int|float|null" : "int|float";
843 				}
844 			}
845 			break;
846 
847 		case 's':
848 			{
849 				char **p = va_arg(*va, char **);
850 				size_t *pl = va_arg(*va, size_t *);
851 				if (!zend_parse_arg_string(arg, p, pl, check_null, arg_num)) {
852 					return check_null ? "?string" : "string";
853 				}
854 			}
855 			break;
856 
857 		case 'p':
858 			{
859 				char **p = va_arg(*va, char **);
860 				size_t *pl = va_arg(*va, size_t *);
861 				if (!zend_parse_arg_path(arg, p, pl, check_null, arg_num)) {
862 					if (Z_TYPE_P(arg) == IS_STRING) {
863 						zend_spprintf(error, 0, "must not contain any null bytes");
864 						return "";
865 					} else {
866 						return check_null ? "?string" : "string";
867 					}
868 				}
869 			}
870 			break;
871 
872 		case 'P':
873 			{
874 				zend_string **str = va_arg(*va, zend_string **);
875 				if (!zend_parse_arg_path_str(arg, str, check_null, arg_num)) {
876 					if (Z_TYPE_P(arg) == IS_STRING) {
877 						zend_spprintf(error, 0, "must not contain any null bytes");
878 						return "";
879 					} else {
880 						return check_null ? "?string" : "string";
881 					}
882 				}
883 			}
884 			break;
885 
886 		case 'S':
887 			{
888 				zend_string **str = va_arg(*va, zend_string **);
889 				if (!zend_parse_arg_str(arg, str, check_null, arg_num)) {
890 					return check_null ? "?string" : "string";
891 				}
892 			}
893 			break;
894 
895 		case 'b':
896 			{
897 				bool *p = va_arg(*va, bool *);
898 				bool *is_null = NULL;
899 
900 				if (check_null) {
901 					is_null = va_arg(*va, bool *);
902 				}
903 
904 				if (!zend_parse_arg_bool(arg, p, is_null, check_null, arg_num)) {
905 					return check_null ? "?bool" : "bool";
906 				}
907 			}
908 			break;
909 
910 		case 'r':
911 			{
912 				zval **p = va_arg(*va, zval **);
913 
914 				if (!zend_parse_arg_resource(arg, p, check_null)) {
915 					return check_null ? "resource or null" : "resource";
916 				}
917 			}
918 			break;
919 
920 		case 'A':
921 		case 'a':
922 			{
923 				zval **p = va_arg(*va, zval **);
924 
925 				if (!zend_parse_arg_array(arg, p, check_null, c == 'A')) {
926 					return check_null ? "?array" : "array";
927 				}
928 			}
929 			break;
930 
931 		case 'H':
932 		case 'h':
933 			{
934 				HashTable **p = va_arg(*va, HashTable **);
935 
936 				if (!zend_parse_arg_array_ht(arg, p, check_null, c == 'H', separate)) {
937 					return check_null ? "?array" : "array";
938 				}
939 			}
940 			break;
941 
942 		case 'o':
943 			{
944 				zval **p = va_arg(*va, zval **);
945 
946 				if (!zend_parse_arg_object(arg, p, NULL, check_null)) {
947 					return check_null ? "?object" : "object";
948 				}
949 			}
950 			break;
951 
952 		case 'O':
953 			{
954 				zval **p = va_arg(*va, zval **);
955 				zend_class_entry *ce = va_arg(*va, zend_class_entry *);
956 
957 				if (!zend_parse_arg_object(arg, p, ce, check_null)) {
958 					if (ce) {
959 						if (check_null) {
960 							zend_spprintf(error, 0, "must be of type ?%s, %s given", ZSTR_VAL(ce->name), zend_zval_value_name(arg));
961 							return "";
962 						} else {
963 							return ZSTR_VAL(ce->name);
964 						}
965 					} else {
966 						return check_null ? "?object" : "object";
967 					}
968 				}
969 			}
970 			break;
971 
972 		case 'C':
973 			{
974 				zend_class_entry *lookup, **pce = va_arg(*va, zend_class_entry **);
975 				zend_class_entry *ce_base = *pce;
976 
977 				if (check_null && Z_TYPE_P(arg) == IS_NULL) {
978 					*pce = NULL;
979 					break;
980 				}
981 				if (!try_convert_to_string(arg)) {
982 					*pce = NULL;
983 					return ""; /* try_convert_to_string() throws an exception */
984 				}
985 
986 				if ((lookup = zend_lookup_class(Z_STR_P(arg))) == NULL) {
987 					*pce = NULL;
988 				} else {
989 					*pce = lookup;
990 				}
991 				if (ce_base) {
992 					if ((!*pce || !instanceof_function(*pce, ce_base))) {
993 						zend_spprintf(error, 0, "must be a class name derived from %s%s, %s given",
994 							ZSTR_VAL(ce_base->name), check_null ? " or null" : "", Z_STRVAL_P(arg));
995 						*pce = NULL;
996 						return "";
997 					}
998 				}
999 				if (!*pce) {
1000 					zend_spprintf(error, 0, "must be a valid class name%s, %s given",
1001 						check_null ? " or null" : "", Z_STRVAL_P(arg));
1002 					return "";
1003 				}
1004 				break;
1005 
1006 			}
1007 			break;
1008 
1009 		case 'F':
1010 		case 'f':
1011 			{
1012 				zend_fcall_info *fci = va_arg(*va, zend_fcall_info *);
1013 				zend_fcall_info_cache *fcc = va_arg(*va, zend_fcall_info_cache *);
1014 				char *is_callable_error = NULL;
1015 
1016 				if (check_null && Z_TYPE_P(arg) == IS_NULL) {
1017 					fci->size = 0;
1018 					fcc->function_handler = 0;
1019 					break;
1020 				}
1021 
1022 				if (zend_fcall_info_init(arg, 0, fci, fcc, NULL, &is_callable_error) == SUCCESS) {
1023 					ZEND_ASSERT(!is_callable_error);
1024 					if (c == 'f') {
1025 						/* Release call trampolines: The function may not get called, in which case
1026 						 * the trampoline will leak. Force it to be refetched during
1027 						 * zend_call_function instead. */
1028 						zend_release_fcall_info_cache(fcc);
1029 					}
1030 					break;
1031 				}
1032 
1033 				if (is_callable_error) {
1034 					zend_spprintf(error, 0, "must be a valid callback%s, %s", check_null ? " or null" : "", is_callable_error);
1035 					efree(is_callable_error);
1036 					return "";
1037 				} else {
1038 					return check_null ? "a valid callback or null" : "a valid callback";
1039 				}
1040 			}
1041 
1042 		case 'z':
1043 			{
1044 				zval **p = va_arg(*va, zval **);
1045 
1046 				zend_parse_arg_zval_deref(real_arg, p, check_null);
1047 			}
1048 			break;
1049 
1050 		case 'Z': /* replace with 'z' */
1051 		case 'L': /* replace with 'l' */
1052 			ZEND_ASSERT(0 && "ZPP modifier no longer supported");
1053 			ZEND_FALLTHROUGH;
1054 		default:
1055 			return "unknown";
1056 	}
1057 
1058 	*spec = spec_walk;
1059 
1060 	return NULL;
1061 }
1062 /* }}} */
1063 
zend_parse_arg(uint32_t arg_num,zval * arg,va_list * va,const char ** spec,int flags)1064 static zend_result zend_parse_arg(uint32_t arg_num, zval *arg, va_list *va, const char **spec, int flags) /* {{{ */
1065 {
1066 	const char *expected_type = NULL;
1067 	char *error = NULL;
1068 
1069 	expected_type = zend_parse_arg_impl(arg, va, spec, &error, arg_num);
1070 	if (expected_type) {
1071 		if (EG(exception)) {
1072 			return FAILURE;
1073 		}
1074 		if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) {
1075 			if (error) {
1076 				if (strcmp(error, "must not contain any null bytes") == 0) {
1077 					zend_argument_value_error(arg_num, "%s", error);
1078 				} else {
1079 					zend_argument_type_error(arg_num, "%s", error);
1080 				}
1081 				efree(error);
1082 			} else {
1083 				zend_argument_type_error(arg_num, "must be of type %s, %s given", expected_type, zend_zval_value_name(arg));
1084 			}
1085 		} else if (error) {
1086 			efree(error);
1087 		}
1088 
1089 		return FAILURE;
1090 	}
1091 
1092 	return SUCCESS;
1093 }
1094 /* }}} */
1095 
zend_parse_parameter(int flags,uint32_t arg_num,zval * arg,const char * spec,...)1096 ZEND_API zend_result zend_parse_parameter(int flags, uint32_t arg_num, zval *arg, const char *spec, ...)
1097 {
1098 	va_list va;
1099 	zend_result ret;
1100 
1101 	va_start(va, spec);
1102 	ret = zend_parse_arg(arg_num, arg, &va, &spec, flags);
1103 	va_end(va);
1104 
1105 	return ret;
1106 }
1107 
zend_parse_parameters_debug_error(const char * msg)1108 static ZEND_COLD void zend_parse_parameters_debug_error(const char *msg) {
1109 	zend_function *active_function = EG(current_execute_data)->func;
1110 	const char *class_name = active_function->common.scope
1111 		? ZSTR_VAL(active_function->common.scope->name) : "";
1112 	zend_error_noreturn(E_CORE_ERROR, "%s%s%s(): %s",
1113 		class_name, class_name[0] ? "::" : "",
1114 		ZSTR_VAL(active_function->common.function_name), msg);
1115 }
1116 
zend_parse_va_args(uint32_t num_args,const char * type_spec,va_list * va,int flags)1117 static zend_result zend_parse_va_args(uint32_t num_args, const char *type_spec, va_list *va, int flags) /* {{{ */
1118 {
1119 	const  char *spec_walk;
1120 	char c;
1121 	uint32_t i;
1122 	uint32_t min_num_args = 0;
1123 	uint32_t max_num_args = 0;
1124 	uint32_t post_varargs = 0;
1125 	zval *arg;
1126 	bool have_varargs = 0;
1127 	bool have_optional_args = 0;
1128 	zval **varargs = NULL;
1129 	uint32_t *n_varargs = NULL;
1130 
1131 	for (spec_walk = type_spec; *spec_walk; spec_walk++) {
1132 		c = *spec_walk;
1133 		switch (c) {
1134 			case 'l': case 'd':
1135 			case 's': case 'b':
1136 			case 'r': case 'a':
1137 			case 'o': case 'O':
1138 			case 'z': case 'Z':
1139 			case 'C': case 'h':
1140 			case 'f': case 'F': case 'A':
1141 			case 'H': case 'p':
1142 			case 'S': case 'P':
1143 			case 'L': case 'n':
1144 				max_num_args++;
1145 				break;
1146 
1147 			case '|':
1148 				min_num_args = max_num_args;
1149 				have_optional_args = 1;
1150 				break;
1151 
1152 			case '/':
1153 			case '!':
1154 				/* Pass */
1155 				break;
1156 
1157 			case '*':
1158 			case '+':
1159 				if (have_varargs) {
1160 					zend_parse_parameters_debug_error(
1161 						"only one varargs specifier (* or +) is permitted");
1162 					return FAILURE;
1163 				}
1164 				have_varargs = 1;
1165 				/* we expect at least one parameter in varargs */
1166 				if (c == '+') {
1167 					max_num_args++;
1168 				}
1169 				/* mark the beginning of varargs */
1170 				post_varargs = max_num_args;
1171 
1172 				if (ZEND_CALL_INFO(EG(current_execute_data)) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
1173 					zend_unexpected_extra_named_error();
1174 					return FAILURE;
1175 				}
1176 				break;
1177 
1178 			default:
1179 				zend_parse_parameters_debug_error("bad type specifier while parsing parameters");
1180 				return FAILURE;
1181 		}
1182 	}
1183 
1184 	/* with no optional arguments the minimum number of arguments must be the same as the maximum */
1185 	if (!have_optional_args) {
1186 		min_num_args = max_num_args;
1187 	}
1188 
1189 	if (have_varargs) {
1190 		/* calculate how many required args are at the end of the specifier list */
1191 		post_varargs = max_num_args - post_varargs;
1192 		max_num_args = UINT32_MAX;
1193 	}
1194 
1195 	if (num_args < min_num_args || num_args > max_num_args) {
1196 		if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
1197 			zend_string *func_name = get_active_function_or_method_name();
1198 
1199 			zend_argument_count_error("%s() expects %s %d argument%s, %d given",
1200 				ZSTR_VAL(func_name),
1201 				min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
1202 				num_args < min_num_args ? min_num_args : max_num_args,
1203 				(num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
1204 				num_args
1205 			);
1206 
1207 			zend_string_release(func_name);
1208 		}
1209 		return FAILURE;
1210 	}
1211 
1212 	if (num_args > ZEND_CALL_NUM_ARGS(EG(current_execute_data))) {
1213 		zend_parse_parameters_debug_error("could not obtain parameters for parsing");
1214 		return FAILURE;
1215 	}
1216 
1217 	i = 0;
1218 	while (num_args-- > 0) {
1219 		if (*type_spec == '|') {
1220 			type_spec++;
1221 		}
1222 
1223 		if (*type_spec == '*' || *type_spec == '+') {
1224 			uint32_t num_varargs = num_args + 1 - post_varargs;
1225 
1226 			/* eat up the passed in storage even if it won't be filled in with varargs */
1227 			varargs = va_arg(*va, zval **);
1228 			n_varargs = va_arg(*va, uint32_t *);
1229 			type_spec++;
1230 
1231 			if (num_varargs > 0) {
1232 				*n_varargs = num_varargs;
1233 				*varargs = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
1234 				/* adjust how many args we have left and restart loop */
1235 				num_args += 1 - num_varargs;
1236 				i += num_varargs;
1237 				continue;
1238 			} else {
1239 				*varargs = NULL;
1240 				*n_varargs = 0;
1241 			}
1242 		}
1243 
1244 		arg = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
1245 
1246 		if (zend_parse_arg(i+1, arg, va, &type_spec, flags) == FAILURE) {
1247 			/* clean up varargs array if it was used */
1248 			if (varargs && *varargs) {
1249 				*varargs = NULL;
1250 			}
1251 			return FAILURE;
1252 		}
1253 		i++;
1254 	}
1255 
1256 	return SUCCESS;
1257 }
1258 /* }}} */
1259 
zend_parse_parameters_ex(int flags,uint32_t num_args,const char * type_spec,...)1260 ZEND_API zend_result zend_parse_parameters_ex(int flags, uint32_t num_args, const char *type_spec, ...) /* {{{ */
1261 {
1262 	va_list va;
1263 	zend_result retval;
1264 
1265 	va_start(va, type_spec);
1266 	retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1267 	va_end(va);
1268 
1269 	return retval;
1270 }
1271 /* }}} */
1272 
zend_parse_parameters(uint32_t num_args,const char * type_spec,...)1273 ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec, ...) /* {{{ */
1274 {
1275 	va_list va;
1276 	zend_result retval;
1277 	int flags = 0;
1278 
1279 	va_start(va, type_spec);
1280 	retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1281 	va_end(va);
1282 
1283 	return retval;
1284 }
1285 /* }}} */
1286 
zend_parse_method_parameters(uint32_t num_args,zval * this_ptr,const char * type_spec,...)1287 ZEND_API zend_result zend_parse_method_parameters(uint32_t num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
1288 {
1289 	va_list va;
1290 	zend_result retval;
1291 	int flags = 0;
1292 	const char *p = type_spec;
1293 	zval **object;
1294 	zend_class_entry *ce;
1295 
1296 	/* Just checking this_ptr is not enough, because fcall_common_helper does not set
1297 	 * Z_OBJ(EG(This)) to NULL when calling an internal function with common.scope == NULL.
1298 	 * In that case EG(This) would still be the $this from the calling code and we'd take the
1299 	 * wrong branch here. */
1300 	bool is_method = EG(current_execute_data)->func->common.scope != NULL;
1301 
1302 	if (!is_method || !this_ptr || Z_TYPE_P(this_ptr) != IS_OBJECT) {
1303 		va_start(va, type_spec);
1304 		retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1305 		va_end(va);
1306 	} else {
1307 		p++;
1308 
1309 		va_start(va, type_spec);
1310 
1311 		object = va_arg(va, zval **);
1312 		ce = va_arg(va, zend_class_entry *);
1313 		*object = this_ptr;
1314 
1315 		if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
1316 			zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s()",
1317 				ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name(), ZSTR_VAL(ce->name), get_active_function_name());
1318 		}
1319 
1320 		retval = zend_parse_va_args(num_args, p, &va, flags);
1321 		va_end(va);
1322 	}
1323 	return retval;
1324 }
1325 /* }}} */
1326 
zend_parse_method_parameters_ex(int flags,uint32_t num_args,zval * this_ptr,const char * type_spec,...)1327 ZEND_API zend_result zend_parse_method_parameters_ex(int flags, uint32_t num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
1328 {
1329 	va_list va;
1330 	zend_result retval;
1331 	const char *p = type_spec;
1332 	zval **object;
1333 	zend_class_entry *ce;
1334 
1335 	if (!this_ptr) {
1336 		va_start(va, type_spec);
1337 		retval = zend_parse_va_args(num_args, type_spec, &va, flags);
1338 		va_end(va);
1339 	} else {
1340 		p++;
1341 		va_start(va, type_spec);
1342 
1343 		object = va_arg(va, zval **);
1344 		ce = va_arg(va, zend_class_entry *);
1345 		*object = this_ptr;
1346 
1347 		if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
1348 			if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
1349 				zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s()",
1350 					ZSTR_VAL(ce->name), get_active_function_name(), ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name());
1351 			}
1352 			va_end(va);
1353 			return FAILURE;
1354 		}
1355 
1356 		retval = zend_parse_va_args(num_args, p, &va, flags);
1357 		va_end(va);
1358 	}
1359 	return retval;
1360 }
1361 /* }}} */
1362 
1363 /* This function should be called after the constructor has been called
1364  * because it may call __set from the uninitialized object otherwise. */
zend_merge_properties(zval * obj,HashTable * properties)1365 ZEND_API void zend_merge_properties(zval *obj, HashTable *properties) /* {{{ */
1366 {
1367 	zend_object *zobj = Z_OBJ_P(obj);
1368 	zend_object_write_property_t write_property = zobj->handlers->write_property;
1369 	zend_class_entry *old_scope = EG(fake_scope);
1370 	zend_string *key;
1371 	zval *value;
1372 
1373 	if (HT_IS_PACKED(properties)) {
1374 		return;
1375 	}
1376 	EG(fake_scope) = Z_OBJCE_P(obj);
1377 	ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(properties, key, value) {
1378 		if (key) {
1379 			write_property(zobj, key, value, NULL);
1380 		}
1381 	} ZEND_HASH_FOREACH_END();
1382 	EG(fake_scope) = old_scope;
1383 }
1384 /* }}} */
1385 
zend_allocate_mutable_data(zend_class_entry * class_type)1386 static zend_class_mutable_data *zend_allocate_mutable_data(zend_class_entry *class_type) /* {{{ */
1387 {
1388 	zend_class_mutable_data *mutable_data;
1389 
1390 	ZEND_ASSERT(ZEND_MAP_PTR(class_type->mutable_data) != NULL);
1391 	ZEND_ASSERT(ZEND_MAP_PTR_GET_IMM(class_type->mutable_data) == NULL);
1392 
1393 	mutable_data = zend_arena_alloc(&CG(arena), sizeof(zend_class_mutable_data));
1394 	memset(mutable_data, 0, sizeof(zend_class_mutable_data));
1395 	mutable_data->ce_flags = class_type->ce_flags;
1396 	ZEND_MAP_PTR_SET_IMM(class_type->mutable_data, mutable_data);
1397 
1398 	return mutable_data;
1399 }
1400 /* }}} */
1401 
zend_separate_class_constants_table(zend_class_entry * class_type)1402 ZEND_API HashTable *zend_separate_class_constants_table(zend_class_entry *class_type) /* {{{ */
1403 {
1404 	zend_class_mutable_data *mutable_data;
1405 	HashTable *constants_table;
1406 	zend_string *key;
1407 	zend_class_constant *new_c, *c;
1408 
1409 	constants_table = zend_arena_alloc(&CG(arena), sizeof(HashTable));
1410 	zend_hash_init(constants_table, zend_hash_num_elements(&class_type->constants_table), NULL, NULL, 0);
1411 	zend_hash_extend(constants_table, zend_hash_num_elements(&class_type->constants_table), 0);
1412 
1413 	ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&class_type->constants_table, key, c) {
1414 		if (c->ce == class_type) {
1415 			if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
1416 				new_c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
1417 				memcpy(new_c, c, sizeof(zend_class_constant));
1418 				c = new_c;
1419 			}
1420 			Z_TRY_ADDREF(c->value);
1421 		} else {
1422 			if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
1423 				c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(c->ce), key);
1424 				ZEND_ASSERT(c);
1425 			}
1426 		}
1427 		_zend_hash_append_ptr(constants_table, key, c);
1428 	} ZEND_HASH_FOREACH_END();
1429 
1430 	ZEND_ASSERT(ZEND_MAP_PTR(class_type->mutable_data) != NULL);
1431 
1432 	mutable_data = ZEND_MAP_PTR_GET_IMM(class_type->mutable_data);
1433 	if (!mutable_data) {
1434 		mutable_data = zend_allocate_mutable_data(class_type);
1435 	}
1436 
1437 	mutable_data->constants_table = constants_table;
1438 
1439 	return constants_table;
1440 }
1441 
update_property(zval * val,zend_property_info * prop_info)1442 static zend_result update_property(zval *val, zend_property_info *prop_info) {
1443 	if (ZEND_TYPE_IS_SET(prop_info->type)) {
1444 		zval tmp;
1445 
1446 		ZVAL_COPY(&tmp, val);
1447 		if (UNEXPECTED(zval_update_constant_ex(&tmp, prop_info->ce) != SUCCESS)) {
1448 			zval_ptr_dtor(&tmp);
1449 			return FAILURE;
1450 		}
1451 		/* property initializers must always be evaluated with strict types */;
1452 		if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, /* strict */ 1))) {
1453 			zval_ptr_dtor(&tmp);
1454 			return FAILURE;
1455 		}
1456 		zval_ptr_dtor(val);
1457 		ZVAL_COPY_VALUE(val, &tmp);
1458 		return SUCCESS;
1459 	}
1460 	return zval_update_constant_ex(val, prop_info->ce);
1461 }
1462 
zend_update_class_constant(zend_class_constant * c,const zend_string * name,zend_class_entry * scope)1463 ZEND_API zend_result zend_update_class_constant(zend_class_constant *c, const zend_string *name, zend_class_entry *scope)
1464 {
1465 	ZEND_ASSERT(Z_TYPE(c->value) == IS_CONSTANT_AST);
1466 
1467 	if (EXPECTED(!ZEND_TYPE_IS_SET(c->type) || ZEND_TYPE_PURE_MASK(c->type) == MAY_BE_ANY)) {
1468 		return zval_update_constant_ex(&c->value, scope);
1469 	}
1470 
1471 	zval tmp;
1472 
1473 	ZVAL_COPY(&tmp, &c->value);
1474 	zend_result result = zval_update_constant_ex(&tmp, scope);
1475 	if (result == FAILURE) {
1476 		zval_ptr_dtor(&tmp);
1477 		return FAILURE;
1478 	}
1479 
1480 	if (UNEXPECTED(!zend_verify_class_constant_type(c, name, &tmp))) {
1481 		zval_ptr_dtor(&tmp);
1482 		return FAILURE;
1483 	}
1484 
1485 	zval_ptr_dtor(&c->value);
1486 	ZVAL_COPY_VALUE(&c->value, &tmp);
1487 
1488 	return SUCCESS;
1489 }
1490 
zend_update_class_constants(zend_class_entry * class_type)1491 ZEND_API zend_result zend_update_class_constants(zend_class_entry *class_type) /* {{{ */
1492 {
1493 	zend_class_mutable_data *mutable_data = NULL;
1494 	zval *default_properties_table = NULL;
1495 	zval *static_members_table = NULL;
1496 	zend_class_constant *c;
1497 	zval *val;
1498 	uint32_t ce_flags;
1499 
1500 	ce_flags = class_type->ce_flags;
1501 
1502 	if (ce_flags & ZEND_ACC_CONSTANTS_UPDATED) {
1503 		return SUCCESS;
1504 	}
1505 
1506 	bool uses_mutable_data = ZEND_MAP_PTR(class_type->mutable_data) != NULL;
1507 	if (uses_mutable_data) {
1508 		mutable_data = ZEND_MAP_PTR_GET_IMM(class_type->mutable_data);
1509 		if (mutable_data) {
1510 			ce_flags = mutable_data->ce_flags;
1511 			if (ce_flags & ZEND_ACC_CONSTANTS_UPDATED) {
1512 				return SUCCESS;
1513 			}
1514 		} else {
1515 			mutable_data = zend_allocate_mutable_data(class_type);
1516 		}
1517 	}
1518 
1519 	if (class_type->parent) {
1520 		if (UNEXPECTED(zend_update_class_constants(class_type->parent) != SUCCESS)) {
1521 			return FAILURE;
1522 		}
1523 	}
1524 
1525 	if (ce_flags & ZEND_ACC_HAS_AST_CONSTANTS) {
1526 		HashTable *constants_table;
1527 
1528 		if (uses_mutable_data) {
1529 			constants_table = mutable_data->constants_table;
1530 			if (!constants_table) {
1531 				constants_table = zend_separate_class_constants_table(class_type);
1532 			}
1533 		} else {
1534 			constants_table = &class_type->constants_table;
1535 		}
1536 
1537 		zend_string *name;
1538 		ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(constants_table, name, val) {
1539 			c = Z_PTR_P(val);
1540 			if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
1541 				if (c->ce != class_type) {
1542 					Z_PTR_P(val) = c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(c->ce), name);
1543 					if (Z_TYPE(c->value) != IS_CONSTANT_AST) {
1544 						continue;
1545 					}
1546 				}
1547 
1548 				val = &c->value;
1549 				if (UNEXPECTED(zend_update_class_constant(c, name, c->ce) != SUCCESS)) {
1550 					return FAILURE;
1551 				}
1552 			}
1553 		} ZEND_HASH_FOREACH_END();
1554 	}
1555 
1556 	if (class_type->default_static_members_count) {
1557 		static_members_table = CE_STATIC_MEMBERS(class_type);
1558 		if (!static_members_table) {
1559 			zend_class_init_statics(class_type);
1560 			static_members_table = CE_STATIC_MEMBERS(class_type);
1561 		}
1562 	}
1563 
1564 	default_properties_table = class_type->default_properties_table;
1565 	if (uses_mutable_data && (ce_flags & ZEND_ACC_HAS_AST_PROPERTIES)) {
1566 		zval *src, *dst, *end;
1567 
1568 		default_properties_table = mutable_data->default_properties_table;
1569 		if (!default_properties_table) {
1570 			default_properties_table = zend_arena_alloc(&CG(arena), sizeof(zval) * class_type->default_properties_count);
1571 			src = class_type->default_properties_table;
1572 			dst = default_properties_table;
1573 			end = dst + class_type->default_properties_count;
1574 			do {
1575 				ZVAL_COPY_PROP(dst, src);
1576 				src++;
1577 				dst++;
1578 			} while (dst != end);
1579 			mutable_data->default_properties_table = default_properties_table;
1580 		}
1581 	}
1582 
1583 	if (ce_flags & (ZEND_ACC_HAS_AST_PROPERTIES|ZEND_ACC_HAS_AST_STATICS)) {
1584 		zend_property_info *prop_info;
1585 
1586 		/* Use the default properties table to also update initializers of private properties
1587 		 * that have been shadowed in a child class. */
1588 		for (uint32_t i = 0; i < class_type->default_properties_count; i++) {
1589 			val = &default_properties_table[i];
1590 			prop_info = class_type->properties_info_table[i];
1591 			if (Z_TYPE_P(val) == IS_CONSTANT_AST
1592 					&& UNEXPECTED(update_property(val, prop_info) != SUCCESS)) {
1593 				return FAILURE;
1594 			}
1595 		}
1596 
1597 		if (class_type->default_static_members_count) {
1598 			ZEND_HASH_MAP_FOREACH_PTR(&class_type->properties_info, prop_info) {
1599 				if (prop_info->flags & ZEND_ACC_STATIC) {
1600 					val = static_members_table + prop_info->offset;
1601 					if (Z_TYPE_P(val) == IS_CONSTANT_AST
1602 							&& UNEXPECTED(update_property(val, prop_info) != SUCCESS)) {
1603 						return FAILURE;
1604 					}
1605 				}
1606 			} ZEND_HASH_FOREACH_END();
1607 		}
1608 	}
1609 
1610 	if (class_type->type == ZEND_USER_CLASS && class_type->ce_flags & ZEND_ACC_ENUM && class_type->enum_backing_type != IS_UNDEF) {
1611 		if (zend_enum_build_backed_enum_table(class_type) == FAILURE) {
1612 			return FAILURE;
1613 		}
1614 	}
1615 
1616 	ce_flags |= ZEND_ACC_CONSTANTS_UPDATED;
1617 	ce_flags &= ~ZEND_ACC_HAS_AST_CONSTANTS;
1618 	ce_flags &= ~ZEND_ACC_HAS_AST_PROPERTIES;
1619 	ce_flags &= ~ZEND_ACC_HAS_AST_STATICS;
1620 	if (uses_mutable_data) {
1621 		mutable_data->ce_flags = ce_flags;
1622 	} else {
1623 		class_type->ce_flags = ce_flags;
1624 	}
1625 
1626 	return SUCCESS;
1627 }
1628 /* }}} */
1629 
_object_properties_init(zend_object * object,zend_class_entry * class_type)1630 static zend_always_inline void _object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1631 {
1632 	if (class_type->default_properties_count) {
1633 		zval *src = CE_DEFAULT_PROPERTIES_TABLE(class_type);
1634 		zval *dst = object->properties_table;
1635 		zval *end = src + class_type->default_properties_count;
1636 
1637 		if (UNEXPECTED(class_type->type == ZEND_INTERNAL_CLASS)) {
1638 			/* We don't have to account for refcounting because
1639 			 * zend_declare_typed_property() disallows refcounted defaults for internal classes. */
1640 			do {
1641 				ZEND_ASSERT(!Z_REFCOUNTED_P(src));
1642 				ZVAL_COPY_VALUE_PROP(dst, src);
1643 				src++;
1644 				dst++;
1645 			} while (src != end);
1646 		} else {
1647 			do {
1648 				ZVAL_COPY_PROP(dst, src);
1649 				src++;
1650 				dst++;
1651 			} while (src != end);
1652 		}
1653 	}
1654 }
1655 /* }}} */
1656 
object_properties_init(zend_object * object,zend_class_entry * class_type)1657 ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
1658 {
1659 	object->properties = NULL;
1660 	_object_properties_init(object, class_type);
1661 }
1662 /* }}} */
1663 
object_properties_init_ex(zend_object * object,HashTable * properties)1664 ZEND_API void object_properties_init_ex(zend_object *object, HashTable *properties) /* {{{ */
1665 {
1666 	object->properties = properties;
1667 	if (object->ce->default_properties_count) {
1668 		zval *prop;
1669 		zend_string *key;
1670 		zend_property_info *property_info;
1671 
1672 		ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(properties, key, prop) {
1673 			property_info = zend_get_property_info(object->ce, key, 1);
1674 			if (property_info != ZEND_WRONG_PROPERTY_INFO &&
1675 			    property_info &&
1676 			    (property_info->flags & ZEND_ACC_STATIC) == 0) {
1677 				zval *slot = OBJ_PROP(object, property_info->offset);
1678 
1679 				if (ZEND_TYPE_IS_SET(property_info->type)) {
1680 					zval tmp;
1681 
1682 					ZVAL_COPY_VALUE(&tmp, prop);
1683 					if (UNEXPECTED(!zend_verify_property_type(property_info, &tmp, 0))) {
1684 						continue;
1685 					}
1686 					ZVAL_COPY_VALUE(slot, &tmp);
1687 				} else {
1688 					ZVAL_COPY_VALUE(slot, prop);
1689 				}
1690 				ZVAL_INDIRECT(prop, slot);
1691 			}
1692 		} ZEND_HASH_FOREACH_END();
1693 	}
1694 }
1695 /* }}} */
1696 
object_properties_load(zend_object * object,HashTable * properties)1697 ZEND_API void object_properties_load(zend_object *object, HashTable *properties) /* {{{ */
1698 {
1699 	zval *prop, tmp;
1700 	zend_string *key;
1701 	zend_long h;
1702 	zend_property_info *property_info;
1703 
1704 	ZEND_HASH_FOREACH_KEY_VAL(properties, h, key, prop) {
1705 		if (key) {
1706 			if (ZSTR_VAL(key)[0] == '\0') {
1707 				const char *class_name, *prop_name;
1708 				size_t prop_name_len;
1709 				if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) {
1710 					zend_string *pname = zend_string_init(prop_name, prop_name_len, 0);
1711 					zend_class_entry *prev_scope = EG(fake_scope);
1712 					if (class_name && class_name[0] != '*') {
1713 						zend_string *cname = zend_string_init(class_name, strlen(class_name), 0);
1714 						EG(fake_scope) = zend_lookup_class(cname);
1715 						zend_string_release_ex(cname, 0);
1716 					}
1717 					property_info = zend_get_property_info(object->ce, pname, 1);
1718 					zend_string_release_ex(pname, 0);
1719 					EG(fake_scope) = prev_scope;
1720 				} else {
1721 					property_info = ZEND_WRONG_PROPERTY_INFO;
1722 				}
1723 			} else {
1724 				property_info = zend_get_property_info(object->ce, key, 1);
1725 			}
1726 			if (property_info != ZEND_WRONG_PROPERTY_INFO &&
1727 				property_info &&
1728 				(property_info->flags & ZEND_ACC_STATIC) == 0) {
1729 				zval *slot = OBJ_PROP(object, property_info->offset);
1730 				zval_ptr_dtor(slot);
1731 				ZVAL_COPY_VALUE(slot, prop);
1732 				zval_add_ref(slot);
1733 				if (object->properties) {
1734 					ZVAL_INDIRECT(&tmp, slot);
1735 					zend_hash_update(object->properties, key, &tmp);
1736 				}
1737 			} else {
1738 				if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) {
1739 					zend_throw_error(NULL, "Cannot create dynamic property %s::$%s",
1740 						ZSTR_VAL(object->ce->name), property_info != ZEND_WRONG_PROPERTY_INFO ? zend_get_unmangled_property_name(key): "");
1741 					return;
1742 				} else if (!(object->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES)) {
1743 					zend_error(E_DEPRECATED, "Creation of dynamic property %s::$%s is deprecated",
1744 						ZSTR_VAL(object->ce->name), property_info != ZEND_WRONG_PROPERTY_INFO ? zend_get_unmangled_property_name(key): "");
1745 				}
1746 
1747 				if (!object->properties) {
1748 					rebuild_object_properties(object);
1749 				}
1750 				prop = zend_hash_update(object->properties, key, prop);
1751 				zval_add_ref(prop);
1752 			}
1753 		} else {
1754 			if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) {
1755 				zend_throw_error(NULL, "Cannot create dynamic property %s::$" ZEND_LONG_FMT, ZSTR_VAL(object->ce->name), h);
1756 				return;
1757 			} else if (!(object->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES)) {
1758 				zend_error(E_DEPRECATED, "Creation of dynamic property %s::$" ZEND_LONG_FMT " is deprecated",
1759 					ZSTR_VAL(object->ce->name), h);
1760 			}
1761 
1762 			if (!object->properties) {
1763 				rebuild_object_properties(object);
1764 			}
1765 			prop = zend_hash_index_update(object->properties, h, prop);
1766 			zval_add_ref(prop);
1767 		}
1768 	} ZEND_HASH_FOREACH_END();
1769 }
1770 /* }}} */
1771 
1772 /* This function requires 'properties' to contain all props declared in the
1773  * class and all props being public. If only a subset is given or the class
1774  * has protected members then you need to merge the properties separately by
1775  * calling zend_merge_properties(). */
_object_and_properties_init(zval * arg,zend_class_entry * class_type,HashTable * properties)1776 static zend_always_inline zend_result _object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
1777 {
1778 	if (UNEXPECTED(class_type->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_ENUM))) {
1779 		if (class_type->ce_flags & ZEND_ACC_INTERFACE) {
1780 			zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(class_type->name));
1781 		} else if (class_type->ce_flags & ZEND_ACC_TRAIT) {
1782 			zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(class_type->name));
1783 		} else if (class_type->ce_flags & ZEND_ACC_ENUM) {
1784 			zend_throw_error(NULL, "Cannot instantiate enum %s", ZSTR_VAL(class_type->name));
1785 		} else {
1786 			zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name));
1787 		}
1788 		ZVAL_NULL(arg);
1789 		Z_OBJ_P(arg) = NULL;
1790 		return FAILURE;
1791 	}
1792 
1793 	if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
1794 		if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
1795 			ZVAL_NULL(arg);
1796 			Z_OBJ_P(arg) = NULL;
1797 			return FAILURE;
1798 		}
1799 	}
1800 
1801 	if (class_type->create_object == NULL) {
1802 		zend_object *obj = zend_objects_new(class_type);
1803 
1804 		ZVAL_OBJ(arg, obj);
1805 		if (properties) {
1806 			object_properties_init_ex(obj, properties);
1807 		} else {
1808 			_object_properties_init(obj, class_type);
1809 		}
1810 	} else {
1811 		ZVAL_OBJ(arg, class_type->create_object(class_type));
1812 	}
1813 	return SUCCESS;
1814 }
1815 /* }}} */
1816 
object_and_properties_init(zval * arg,zend_class_entry * class_type,HashTable * properties)1817 ZEND_API zend_result object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
1818 {
1819 	return _object_and_properties_init(arg, class_type, properties);
1820 }
1821 /* }}} */
1822 
object_init_ex(zval * arg,zend_class_entry * class_type)1823 ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *class_type) /* {{{ */
1824 {
1825 	return _object_and_properties_init(arg, class_type, NULL);
1826 }
1827 /* }}} */
1828 
object_init(zval * arg)1829 ZEND_API void object_init(zval *arg) /* {{{ */
1830 {
1831 	ZVAL_OBJ(arg, zend_objects_new(zend_standard_class_def));
1832 }
1833 /* }}} */
1834 
add_assoc_long_ex(zval * arg,const char * key,size_t key_len,zend_long n)1835 ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
1836 {
1837 	zval tmp;
1838 
1839 	ZVAL_LONG(&tmp, n);
1840 	zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1841 }
1842 /* }}} */
1843 
add_assoc_null_ex(zval * arg,const char * key,size_t key_len)1844 ZEND_API void add_assoc_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
1845 {
1846 	zval tmp;
1847 
1848 	ZVAL_NULL(&tmp);
1849 	zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1850 }
1851 /* }}} */
1852 
add_assoc_bool_ex(zval * arg,const char * key,size_t key_len,bool b)1853 ZEND_API void add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, bool b) /* {{{ */
1854 {
1855 	zval tmp;
1856 
1857 	ZVAL_BOOL(&tmp, b);
1858 	zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1859 }
1860 /* }}} */
1861 
add_assoc_resource_ex(zval * arg,const char * key,size_t key_len,zend_resource * r)1862 ZEND_API void add_assoc_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
1863 {
1864 	zval tmp;
1865 
1866 	ZVAL_RES(&tmp, r);
1867 	zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1868 }
1869 /* }}} */
1870 
add_assoc_double_ex(zval * arg,const char * key,size_t key_len,double d)1871 ZEND_API void add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
1872 {
1873 	zval tmp;
1874 
1875 	ZVAL_DOUBLE(&tmp, d);
1876 	zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1877 }
1878 /* }}} */
1879 
add_assoc_str_ex(zval * arg,const char * key,size_t key_len,zend_string * str)1880 ZEND_API void add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
1881 {
1882 	zval tmp;
1883 
1884 	ZVAL_STR(&tmp, str);
1885 	zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1886 }
1887 /* }}} */
1888 
add_assoc_string_ex(zval * arg,const char * key,size_t key_len,const char * str)1889 ZEND_API void add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
1890 {
1891 	zval tmp;
1892 
1893 	ZVAL_STRING(&tmp, str);
1894 	zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1895 }
1896 /* }}} */
1897 
add_assoc_stringl_ex(zval * arg,const char * key,size_t key_len,const char * str,size_t length)1898 ZEND_API void add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
1899 {
1900 	zval tmp;
1901 
1902 	ZVAL_STRINGL(&tmp, str, length);
1903 	zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1904 }
1905 /* }}} */
1906 
add_assoc_array_ex(zval * arg,const char * key,size_t key_len,zend_array * arr)1907 ZEND_API void add_assoc_array_ex(zval *arg, const char *key, size_t key_len, zend_array *arr) /* {{{ */
1908 {
1909 	zval tmp;
1910 
1911 	ZVAL_ARR(&tmp, arr);
1912 	zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1913 }
1914 /* }}} */
1915 
add_assoc_object_ex(zval * arg,const char * key,size_t key_len,zend_object * obj)1916 ZEND_API void add_assoc_object_ex(zval *arg, const char *key, size_t key_len, zend_object *obj) /* {{{ */
1917 {
1918 	zval tmp;
1919 
1920 	ZVAL_OBJ(&tmp, obj);
1921 	zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1922 }
1923 /* }}} */
1924 
add_assoc_reference_ex(zval * arg,const char * key,size_t key_len,zend_reference * ref)1925 ZEND_API void add_assoc_reference_ex(zval *arg, const char *key, size_t key_len, zend_reference *ref) /* {{{ */
1926 {
1927 	zval tmp;
1928 
1929 	ZVAL_REF(&tmp, ref);
1930 	zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1931 }
1932 /* }}} */
1933 
add_assoc_zval_ex(zval * arg,const char * key,size_t key_len,zval * value)1934 ZEND_API void add_assoc_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
1935 {
1936 	zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value);
1937 }
1938 /* }}} */
1939 
add_index_long(zval * arg,zend_ulong index,zend_long n)1940 ZEND_API void add_index_long(zval *arg, zend_ulong index, zend_long n) /* {{{ */
1941 {
1942 	zval tmp;
1943 
1944 	ZVAL_LONG(&tmp, n);
1945 	zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1946 }
1947 /* }}} */
1948 
add_index_null(zval * arg,zend_ulong index)1949 ZEND_API void add_index_null(zval *arg, zend_ulong index) /* {{{ */
1950 {
1951 	zval tmp;
1952 
1953 	ZVAL_NULL(&tmp);
1954 	zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1955 }
1956 /* }}} */
1957 
add_index_bool(zval * arg,zend_ulong index,bool b)1958 ZEND_API void add_index_bool(zval *arg, zend_ulong index, bool b) /* {{{ */
1959 {
1960 	zval tmp;
1961 
1962 	ZVAL_BOOL(&tmp, b);
1963 	zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1964 }
1965 /* }}} */
1966 
add_index_resource(zval * arg,zend_ulong index,zend_resource * r)1967 ZEND_API void add_index_resource(zval *arg, zend_ulong index, zend_resource *r) /* {{{ */
1968 {
1969 	zval tmp;
1970 
1971 	ZVAL_RES(&tmp, r);
1972 	zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1973 }
1974 /* }}} */
1975 
add_index_double(zval * arg,zend_ulong index,double d)1976 ZEND_API void add_index_double(zval *arg, zend_ulong index, double d) /* {{{ */
1977 {
1978 	zval tmp;
1979 
1980 	ZVAL_DOUBLE(&tmp, d);
1981 	zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1982 }
1983 /* }}} */
1984 
add_index_str(zval * arg,zend_ulong index,zend_string * str)1985 ZEND_API void add_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */
1986 {
1987 	zval tmp;
1988 
1989 	ZVAL_STR(&tmp, str);
1990 	zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
1991 }
1992 /* }}} */
1993 
add_index_string(zval * arg,zend_ulong index,const char * str)1994 ZEND_API void add_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */
1995 {
1996 	zval tmp;
1997 
1998 	ZVAL_STRING(&tmp, str);
1999 	zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2000 }
2001 /* }}} */
2002 
add_index_stringl(zval * arg,zend_ulong index,const char * str,size_t length)2003 ZEND_API void add_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */
2004 {
2005 	zval tmp;
2006 
2007 	ZVAL_STRINGL(&tmp, str, length);
2008 	zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2009 }
2010 /* }}} */
2011 
add_index_array(zval * arg,zend_ulong index,zend_array * arr)2012 ZEND_API void add_index_array(zval *arg, zend_ulong index, zend_array *arr) /* {{{ */
2013 {
2014 	zval tmp;
2015 
2016 	ZVAL_ARR(&tmp, arr);
2017 	zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2018 }
2019 /* }}} */
2020 
add_index_object(zval * arg,zend_ulong index,zend_object * obj)2021 ZEND_API void add_index_object(zval *arg, zend_ulong index, zend_object *obj) /* {{{ */
2022 {
2023 	zval tmp;
2024 
2025 	ZVAL_OBJ(&tmp, obj);
2026 	zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2027 }
2028 /* }}} */
2029 
add_index_reference(zval * arg,zend_ulong index,zend_reference * ref)2030 ZEND_API void add_index_reference(zval *arg, zend_ulong index, zend_reference *ref) /* {{{ */
2031 {
2032 	zval tmp;
2033 
2034 	ZVAL_REF(&tmp, ref);
2035 	zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2036 }
2037 /* }}} */
2038 
add_next_index_long(zval * arg,zend_long n)2039 ZEND_API zend_result add_next_index_long(zval *arg, zend_long n) /* {{{ */
2040 {
2041 	zval tmp;
2042 
2043 	ZVAL_LONG(&tmp, n);
2044 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2045 }
2046 /* }}} */
2047 
add_next_index_null(zval * arg)2048 ZEND_API zend_result add_next_index_null(zval *arg) /* {{{ */
2049 {
2050 	zval tmp;
2051 
2052 	ZVAL_NULL(&tmp);
2053 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2054 }
2055 /* }}} */
2056 
add_next_index_bool(zval * arg,bool b)2057 ZEND_API zend_result add_next_index_bool(zval *arg, bool b) /* {{{ */
2058 {
2059 	zval tmp;
2060 
2061 	ZVAL_BOOL(&tmp, b);
2062 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2063 }
2064 /* }}} */
2065 
add_next_index_resource(zval * arg,zend_resource * r)2066 ZEND_API zend_result add_next_index_resource(zval *arg, zend_resource *r) /* {{{ */
2067 {
2068 	zval tmp;
2069 
2070 	ZVAL_RES(&tmp, r);
2071 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2072 }
2073 /* }}} */
2074 
add_next_index_double(zval * arg,double d)2075 ZEND_API zend_result add_next_index_double(zval *arg, double d) /* {{{ */
2076 {
2077 	zval tmp;
2078 
2079 	ZVAL_DOUBLE(&tmp, d);
2080 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2081 }
2082 /* }}} */
2083 
add_next_index_str(zval * arg,zend_string * str)2084 ZEND_API zend_result add_next_index_str(zval *arg, zend_string *str) /* {{{ */
2085 {
2086 	zval tmp;
2087 
2088 	ZVAL_STR(&tmp, str);
2089 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2090 }
2091 /* }}} */
2092 
add_next_index_string(zval * arg,const char * str)2093 ZEND_API zend_result add_next_index_string(zval *arg, const char *str) /* {{{ */
2094 {
2095 	zval tmp;
2096 
2097 	ZVAL_STRING(&tmp, str);
2098 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2099 }
2100 /* }}} */
2101 
add_next_index_stringl(zval * arg,const char * str,size_t length)2102 ZEND_API zend_result add_next_index_stringl(zval *arg, const char *str, size_t length) /* {{{ */
2103 {
2104 	zval tmp;
2105 
2106 	ZVAL_STRINGL(&tmp, str, length);
2107 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2108 }
2109 /* }}} */
2110 
add_next_index_array(zval * arg,zend_array * arr)2111 ZEND_API zend_result add_next_index_array(zval *arg, zend_array *arr) /* {{{ */
2112 {
2113 	zval tmp;
2114 
2115 	ZVAL_ARR(&tmp, arr);
2116 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2117 }
2118 /* }}} */
2119 
add_next_index_object(zval * arg,zend_object * obj)2120 ZEND_API zend_result add_next_index_object(zval *arg, zend_object *obj) /* {{{ */
2121 {
2122 	zval tmp;
2123 
2124 	ZVAL_OBJ(&tmp, obj);
2125 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2126 }
2127 /* }}} */
2128 
add_next_index_reference(zval * arg,zend_reference * ref)2129 ZEND_API zend_result add_next_index_reference(zval *arg, zend_reference *ref) /* {{{ */
2130 {
2131 	zval tmp;
2132 
2133 	ZVAL_REF(&tmp, ref);
2134 	return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2135 }
2136 /* }}} */
2137 
array_set_zval_key(HashTable * ht,zval * key,zval * value)2138 ZEND_API zend_result array_set_zval_key(HashTable *ht, zval *key, zval *value) /* {{{ */
2139 {
2140 	zval *result;
2141 
2142 	switch (Z_TYPE_P(key)) {
2143 		case IS_STRING:
2144 			result = zend_symtable_update(ht, Z_STR_P(key), value);
2145 			break;
2146 		case IS_NULL:
2147 			result = zend_hash_update(ht, ZSTR_EMPTY_ALLOC(), value);
2148 			break;
2149 		case IS_RESOURCE:
2150 			zend_use_resource_as_offset(key);
2151 			result = zend_hash_index_update(ht, Z_RES_HANDLE_P(key), value);
2152 			break;
2153 		case IS_FALSE:
2154 			result = zend_hash_index_update(ht, 0, value);
2155 			break;
2156 		case IS_TRUE:
2157 			result = zend_hash_index_update(ht, 1, value);
2158 			break;
2159 		case IS_LONG:
2160 			result = zend_hash_index_update(ht, Z_LVAL_P(key), value);
2161 			break;
2162 		case IS_DOUBLE:
2163 			result = zend_hash_index_update(ht, zend_dval_to_lval_safe(Z_DVAL_P(key)), value);
2164 			break;
2165 		default:
2166 			zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), key, BP_VAR_W);
2167 			result = NULL;
2168 	}
2169 
2170 	if (result) {
2171 		Z_TRY_ADDREF_P(result);
2172 		return SUCCESS;
2173 	} else {
2174 		return FAILURE;
2175 	}
2176 }
2177 /* }}} */
2178 
add_property_long_ex(zval * arg,const char * key,size_t key_len,zend_long n)2179 ZEND_API void add_property_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
2180 {
2181 	zval tmp;
2182 
2183 	ZVAL_LONG(&tmp, n);
2184 	add_property_zval_ex(arg, key, key_len, &tmp);
2185 }
2186 /* }}} */
2187 
add_property_bool_ex(zval * arg,const char * key,size_t key_len,zend_long b)2188 ZEND_API void add_property_bool_ex(zval *arg, const char *key, size_t key_len, zend_long b) /* {{{ */
2189 {
2190 	zval tmp;
2191 
2192 	ZVAL_BOOL(&tmp, b);
2193 	add_property_zval_ex(arg, key, key_len, &tmp);
2194 }
2195 /* }}} */
2196 
add_property_null_ex(zval * arg,const char * key,size_t key_len)2197 ZEND_API void add_property_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
2198 {
2199 	zval tmp;
2200 
2201 	ZVAL_NULL(&tmp);
2202 	add_property_zval_ex(arg, key, key_len, &tmp);
2203 }
2204 /* }}} */
2205 
add_property_resource_ex(zval * arg,const char * key,size_t key_len,zend_resource * r)2206 ZEND_API void add_property_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
2207 {
2208 	zval tmp;
2209 
2210 	ZVAL_RES(&tmp, r);
2211 	add_property_zval_ex(arg, key, key_len, &tmp);
2212 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2213 }
2214 /* }}} */
2215 
add_property_double_ex(zval * arg,const char * key,size_t key_len,double d)2216 ZEND_API void add_property_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
2217 {
2218 	zval tmp;
2219 
2220 	ZVAL_DOUBLE(&tmp, d);
2221 	add_property_zval_ex(arg, key, key_len, &tmp);
2222 }
2223 /* }}} */
2224 
add_property_str_ex(zval * arg,const char * key,size_t key_len,zend_string * str)2225 ZEND_API void add_property_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
2226 {
2227 	zval tmp;
2228 
2229 	ZVAL_STR(&tmp, str);
2230 	add_property_zval_ex(arg, key, key_len, &tmp);
2231 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2232 }
2233 /* }}} */
2234 
add_property_string_ex(zval * arg,const char * key,size_t key_len,const char * str)2235 ZEND_API void add_property_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
2236 {
2237 	zval tmp;
2238 
2239 	ZVAL_STRING(&tmp, str);
2240 	add_property_zval_ex(arg, key, key_len, &tmp);
2241 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2242 }
2243 /* }}} */
2244 
add_property_stringl_ex(zval * arg,const char * key,size_t key_len,const char * str,size_t length)2245 ZEND_API void add_property_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
2246 {
2247 	zval tmp;
2248 
2249 	ZVAL_STRINGL(&tmp, str, length);
2250 	add_property_zval_ex(arg, key, key_len, &tmp);
2251 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2252 }
2253 /* }}} */
2254 
add_property_array_ex(zval * arg,const char * key,size_t key_len,zend_array * arr)2255 ZEND_API void add_property_array_ex(zval *arg, const char *key, size_t key_len, zend_array *arr) /* {{{ */
2256 {
2257 	zval tmp;
2258 
2259 	ZVAL_ARR(&tmp, arr);
2260 	add_property_zval_ex(arg, key, key_len, &tmp);
2261 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2262 }
2263 /* }}} */
2264 
add_property_object_ex(zval * arg,const char * key,size_t key_len,zend_object * obj)2265 ZEND_API void add_property_object_ex(zval *arg, const char *key, size_t key_len, zend_object *obj) /* {{{ */
2266 {
2267 	zval tmp;
2268 
2269 	ZVAL_OBJ(&tmp, obj);
2270 	add_property_zval_ex(arg, key, key_len, &tmp);
2271 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2272 }
2273 /* }}} */
2274 
add_property_reference_ex(zval * arg,const char * key,size_t key_len,zend_reference * ref)2275 ZEND_API void add_property_reference_ex(zval *arg, const char *key, size_t key_len, zend_reference *ref) /* {{{ */
2276 {
2277 	zval tmp;
2278 
2279 	ZVAL_REF(&tmp, ref);
2280 	add_property_zval_ex(arg, key, key_len, &tmp);
2281 	zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2282 }
2283 /* }}} */
2284 
add_property_zval_ex(zval * arg,const char * key,size_t key_len,zval * value)2285 ZEND_API void add_property_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
2286 {
2287 	zend_string *str;
2288 
2289 	str = zend_string_init(key, key_len, 0);
2290 	Z_OBJ_HANDLER_P(arg, write_property)(Z_OBJ_P(arg), str, value, NULL);
2291 	zend_string_release_ex(str, 0);
2292 }
2293 /* }}} */
2294 
zend_startup_module_ex(zend_module_entry * module)2295 ZEND_API zend_result zend_startup_module_ex(zend_module_entry *module) /* {{{ */
2296 {
2297 	size_t name_len;
2298 	zend_string *lcname;
2299 
2300 	if (module->module_started) {
2301 		return SUCCESS;
2302 	}
2303 	module->module_started = 1;
2304 
2305 	/* Check module dependencies */
2306 	if (module->deps) {
2307 		const zend_module_dep *dep = module->deps;
2308 
2309 		while (dep->name) {
2310 			if (dep->type == MODULE_DEP_REQUIRED) {
2311 				zend_module_entry *req_mod;
2312 
2313 				name_len = strlen(dep->name);
2314 				lcname = zend_string_alloc(name_len, 0);
2315 				zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
2316 
2317 				if ((req_mod = zend_hash_find_ptr(&module_registry, lcname)) == NULL || !req_mod->module_started) {
2318 					zend_string_efree(lcname);
2319 					/* TODO: Check version relationship */
2320 					zend_error(E_CORE_WARNING, "Cannot load module \"%s\" because required module \"%s\" is not loaded", module->name, dep->name);
2321 					module->module_started = 0;
2322 					return FAILURE;
2323 				}
2324 				zend_string_efree(lcname);
2325 			}
2326 			++dep;
2327 		}
2328 	}
2329 
2330 	/* Initialize module globals */
2331 	if (module->globals_size) {
2332 #ifdef ZTS
2333 		ts_allocate_id(module->globals_id_ptr, module->globals_size, (ts_allocate_ctor) module->globals_ctor, (ts_allocate_dtor) module->globals_dtor);
2334 #else
2335 		if (module->globals_ctor) {
2336 			module->globals_ctor(module->globals_ptr);
2337 		}
2338 #endif
2339 	}
2340 	if (module->module_startup_func) {
2341 		EG(current_module) = module;
2342 		if (module->module_startup_func(module->type, module->module_number)==FAILURE) {
2343 			zend_error_noreturn(E_CORE_ERROR,"Unable to start %s module", module->name);
2344 			EG(current_module) = NULL;
2345 			return FAILURE;
2346 		}
2347 		EG(current_module) = NULL;
2348 	}
2349 	return SUCCESS;
2350 }
2351 /* }}} */
2352 
zend_startup_module_zval(zval * zv)2353 static int zend_startup_module_zval(zval *zv) /* {{{ */
2354 {
2355 	zend_module_entry *module = Z_PTR_P(zv);
2356 
2357 	return (zend_startup_module_ex(module) == SUCCESS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
2358 }
2359 /* }}} */
2360 
zend_sort_modules(void * base,size_t count,size_t siz,compare_func_t compare,swap_func_t swp)2361 static void zend_sort_modules(void *base, size_t count, size_t siz, compare_func_t compare, swap_func_t swp) /* {{{ */
2362 {
2363 	Bucket *b1 = base;
2364 	Bucket *b2;
2365 	Bucket *end = b1 + count;
2366 	Bucket tmp;
2367 	zend_module_entry *m, *r;
2368 
2369 	while (b1 < end) {
2370 try_again:
2371 		m = (zend_module_entry*)Z_PTR(b1->val);
2372 		if (!m->module_started && m->deps) {
2373 			const zend_module_dep *dep = m->deps;
2374 			while (dep->name) {
2375 				if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) {
2376 					b2 = b1 + 1;
2377 					while (b2 < end) {
2378 						r = (zend_module_entry*)Z_PTR(b2->val);
2379 						if (strcasecmp(dep->name, r->name) == 0) {
2380 							tmp = *b1;
2381 							*b1 = *b2;
2382 							*b2 = tmp;
2383 							goto try_again;
2384 						}
2385 						b2++;
2386 					}
2387 				}
2388 				dep++;
2389 			}
2390 		}
2391 		b1++;
2392 	}
2393 }
2394 /* }}} */
2395 
zend_collect_module_handlers(void)2396 ZEND_API void zend_collect_module_handlers(void) /* {{{ */
2397 {
2398 	zend_module_entry *module;
2399 	int startup_count = 0;
2400 	int shutdown_count = 0;
2401 	int post_deactivate_count = 0;
2402 	int dl_loaded_count = 0;
2403 	zend_class_entry *ce;
2404 	int class_count = 0;
2405 
2406 	/* Collect extensions with request startup/shutdown handlers */
2407 	ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
2408 		if (module->request_startup_func) {
2409 			startup_count++;
2410 		}
2411 		if (module->request_shutdown_func) {
2412 			shutdown_count++;
2413 		}
2414 		if (module->post_deactivate_func) {
2415 			post_deactivate_count++;
2416 		}
2417 		if (module->handle) {
2418 			dl_loaded_count++;
2419 		}
2420 	} ZEND_HASH_FOREACH_END();
2421 	module_request_startup_handlers = (zend_module_entry**)realloc(
2422 		module_request_startup_handlers,
2423 	    sizeof(zend_module_entry*) *
2424 		(startup_count + 1 +
2425 		 shutdown_count + 1 +
2426 		 post_deactivate_count + 1));
2427 	module_request_startup_handlers[startup_count] = NULL;
2428 	module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
2429 	module_request_shutdown_handlers[shutdown_count] = NULL;
2430 	module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
2431 	module_post_deactivate_handlers[post_deactivate_count] = NULL;
2432 	/* Cannot reuse module_request_startup_handlers because it is freed in zend_destroy_modules, which happens before zend_unload_modules. */
2433 	modules_dl_loaded = realloc(modules_dl_loaded, sizeof(zend_module_entry*) * (dl_loaded_count + 1));
2434 	modules_dl_loaded[dl_loaded_count] = NULL;
2435 	startup_count = 0;
2436 
2437 	ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
2438 		if (module->request_startup_func) {
2439 			module_request_startup_handlers[startup_count++] = module;
2440 		}
2441 		if (module->request_shutdown_func) {
2442 			module_request_shutdown_handlers[--shutdown_count] = module;
2443 		}
2444 		if (module->post_deactivate_func) {
2445 			module_post_deactivate_handlers[--post_deactivate_count] = module;
2446 		}
2447 		if (module->handle) {
2448 			modules_dl_loaded[--dl_loaded_count] = module;
2449 		}
2450 	} ZEND_HASH_FOREACH_END();
2451 
2452 	/* Collect internal classes with static members */
2453 	ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
2454 		if (ce->type == ZEND_INTERNAL_CLASS &&
2455 		    ce->default_static_members_count > 0) {
2456 		    class_count++;
2457 		}
2458 	} ZEND_HASH_FOREACH_END();
2459 
2460 	class_cleanup_handlers = (zend_class_entry**)realloc(
2461 		class_cleanup_handlers,
2462 		sizeof(zend_class_entry*) *
2463 		(class_count + 1));
2464 	class_cleanup_handlers[class_count] = NULL;
2465 
2466 	if (class_count) {
2467 		ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
2468 			if (ce->type == ZEND_INTERNAL_CLASS &&
2469 			    ce->default_static_members_count > 0) {
2470 			    class_cleanup_handlers[--class_count] = ce;
2471 			}
2472 		} ZEND_HASH_FOREACH_END();
2473 	}
2474 }
2475 /* }}} */
2476 
zend_startup_modules(void)2477 ZEND_API void zend_startup_modules(void) /* {{{ */
2478 {
2479 	zend_hash_sort_ex(&module_registry, zend_sort_modules, NULL, 0);
2480 	zend_hash_apply(&module_registry, zend_startup_module_zval);
2481 }
2482 /* }}} */
2483 
zend_destroy_modules(void)2484 ZEND_API void zend_destroy_modules(void) /* {{{ */
2485 {
2486 	free(class_cleanup_handlers);
2487 	class_cleanup_handlers = NULL;
2488 	free(module_request_startup_handlers);
2489 	module_request_startup_handlers = NULL;
2490 	zend_hash_graceful_reverse_destroy(&module_registry);
2491 }
2492 /* }}} */
2493 
zend_register_module_ex(zend_module_entry * module,int module_type)2494 ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module, int module_type) /* {{{ */
2495 {
2496 	size_t name_len;
2497 	zend_string *lcname;
2498 	zend_module_entry *module_ptr;
2499 
2500 	if (!module) {
2501 		return NULL;
2502 	}
2503 
2504 #if 0
2505 	zend_printf("%s: Registering module %d\n", module->name, module->module_number);
2506 #endif
2507 
2508 	/* Check module dependencies */
2509 	if (module->deps) {
2510 		const zend_module_dep *dep = module->deps;
2511 
2512 		while (dep->name) {
2513 			if (dep->type == MODULE_DEP_CONFLICTS) {
2514 				name_len = strlen(dep->name);
2515 				lcname = zend_string_alloc(name_len, 0);
2516 				zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
2517 
2518 				if (zend_hash_exists(&module_registry, lcname) || zend_get_extension(dep->name)) {
2519 					zend_string_efree(lcname);
2520 					/* TODO: Check version relationship */
2521 					zend_error(E_CORE_WARNING, "Cannot load module \"%s\" because conflicting module \"%s\" is already loaded", module->name, dep->name);
2522 					return NULL;
2523 				}
2524 				zend_string_efree(lcname);
2525 			}
2526 			++dep;
2527 		}
2528 	}
2529 
2530 	name_len = strlen(module->name);
2531 	lcname = zend_string_alloc(name_len, module_type == MODULE_PERSISTENT);
2532 	zend_str_tolower_copy(ZSTR_VAL(lcname), module->name, name_len);
2533 
2534 	int module_number = zend_next_free_module();
2535 
2536 	lcname = zend_new_interned_string(lcname);
2537 	if ((module_ptr = zend_hash_add_ptr(&module_registry, lcname, module)) == NULL) {
2538 		zend_error(E_CORE_WARNING, "Module \"%s\" is already loaded", module->name);
2539 		zend_string_release(lcname);
2540 		return NULL;
2541 	}
2542 	module = module_ptr;
2543 	EG(current_module) = module;
2544 
2545 	module->module_number = module_number;
2546 	module->type = module_type;
2547 
2548 	if (module->functions && zend_register_functions(NULL, module->functions, NULL, module_type)==FAILURE) {
2549 		zend_hash_del(&module_registry, lcname);
2550 		zend_string_release(lcname);
2551 		EG(current_module) = NULL;
2552 		zend_error(E_CORE_WARNING,"%s: Unable to register functions, unable to load", module->name);
2553 		return NULL;
2554 	}
2555 
2556 	EG(current_module) = NULL;
2557 	zend_string_release(lcname);
2558 	return module;
2559 }
2560 /* }}} */
2561 
zend_register_internal_module(zend_module_entry * module)2562 ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module) /* {{{ */
2563 {
2564 	return zend_register_module_ex(module, MODULE_PERSISTENT);
2565 }
2566 /* }}} */
2567 
zend_check_magic_method_args(uint32_t num_args,const zend_class_entry * ce,const zend_function * fptr,int error_type)2568 static void zend_check_magic_method_args(
2569 		uint32_t num_args, const zend_class_entry *ce, const zend_function *fptr, int error_type)
2570 {
2571 	if (fptr->common.num_args != num_args) {
2572 		if (num_args == 0) {
2573 			zend_error(error_type, "Method %s::%s() cannot take arguments",
2574 				ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2575 		} else if (num_args == 1) {
2576 			zend_error(error_type, "Method %s::%s() must take exactly 1 argument",
2577 				ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2578 		} else {
2579 			zend_error(error_type, "Method %s::%s() must take exactly %" PRIu32 " arguments",
2580 				ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name), num_args);
2581 		}
2582 		return;
2583 	}
2584 	for (uint32_t i = 0; i < num_args; i++) {
2585 		if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, i + 1)) {
2586 			zend_error(error_type, "Method %s::%s() cannot take arguments by reference",
2587 				ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2588 			return;
2589 		}
2590 	}
2591 }
2592 
zend_check_magic_method_arg_type(uint32_t arg_num,const zend_class_entry * ce,const zend_function * fptr,int error_type,int arg_type)2593 static void zend_check_magic_method_arg_type(uint32_t arg_num, const zend_class_entry *ce, const zend_function *fptr, int error_type, int arg_type)
2594 {
2595 		if (
2596 			ZEND_TYPE_IS_SET(fptr->common.arg_info[arg_num].type)
2597 			 && !(ZEND_TYPE_FULL_MASK(fptr->common.arg_info[arg_num].type) & arg_type)
2598 		) {
2599 			zend_error(error_type, "%s::%s(): Parameter #%d ($%s) must be of type %s when declared",
2600 				ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2601 				arg_num + 1, ZSTR_VAL(fptr->common.arg_info[arg_num].name),
2602 				ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(arg_type))));
2603 		}
2604 }
2605 
zend_check_magic_method_return_type(const zend_class_entry * ce,const zend_function * fptr,int error_type,int return_type)2606 static void zend_check_magic_method_return_type(const zend_class_entry *ce, const zend_function *fptr, int error_type, int return_type)
2607 {
2608 	if (!(fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
2609 		/* For backwards compatibility reasons, do not enforce the return type if it is not set. */
2610 		return;
2611 	}
2612 
2613 	if (ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & MAY_BE_NEVER) {
2614 		/* It is always legal to specify the never type. */
2615 		return;
2616 	}
2617 
2618 	bool is_complex_type = ZEND_TYPE_IS_COMPLEX(fptr->common.arg_info[-1].type);
2619 	uint32_t extra_types = ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & ~return_type;
2620 	if (extra_types & MAY_BE_STATIC) {
2621 		extra_types &= ~MAY_BE_STATIC;
2622 		is_complex_type = true;
2623 	}
2624 
2625 	if (extra_types || (is_complex_type && return_type != MAY_BE_OBJECT)) {
2626 		zend_error(error_type, "%s::%s(): Return type must be %s when declared",
2627 			ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2628 			ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(return_type))));
2629 	}
2630 }
2631 
zend_check_magic_method_non_static(const zend_class_entry * ce,const zend_function * fptr,int error_type)2632 static void zend_check_magic_method_non_static(
2633 		const zend_class_entry *ce, const zend_function *fptr, int error_type)
2634 {
2635 	if (fptr->common.fn_flags & ZEND_ACC_STATIC) {
2636 		zend_error(error_type, "Method %s::%s() cannot be static",
2637 			ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2638 	}
2639 }
2640 
zend_check_magic_method_static(const zend_class_entry * ce,const zend_function * fptr,int error_type)2641 static void zend_check_magic_method_static(
2642 		const zend_class_entry *ce, const zend_function *fptr, int error_type)
2643 {
2644 	if (!(fptr->common.fn_flags & ZEND_ACC_STATIC)) {
2645 		zend_error(error_type, "Method %s::%s() must be static",
2646 			ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2647 	}
2648 }
2649 
zend_check_magic_method_public(const zend_class_entry * ce,const zend_function * fptr,int error_type)2650 static void zend_check_magic_method_public(
2651 		const zend_class_entry *ce, const zend_function *fptr, int error_type)
2652 {
2653 	// TODO: Remove this warning after adding proper visibility handling.
2654 	if (!(fptr->common.fn_flags & ZEND_ACC_PUBLIC)) {
2655 		zend_error(E_WARNING, "The magic method %s::%s() must have public visibility",
2656 			ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2657 	}
2658 }
2659 
zend_check_magic_method_no_return_type(const zend_class_entry * ce,const zend_function * fptr,int error_type)2660 static void zend_check_magic_method_no_return_type(
2661 		const zend_class_entry *ce, const zend_function *fptr, int error_type)
2662 {
2663 	if (fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
2664 		zend_error_noreturn(error_type, "Method %s::%s() cannot declare a return type",
2665 			ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2666 	}
2667 }
2668 
zend_check_magic_method_implementation(const zend_class_entry * ce,const zend_function * fptr,zend_string * lcname,int error_type)2669 ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, zend_string *lcname, int error_type) /* {{{ */
2670 {
2671 	if (ZSTR_VAL(fptr->common.function_name)[0] != '_'
2672 	 || ZSTR_VAL(fptr->common.function_name)[1] != '_') {
2673 		return;
2674 	}
2675 
2676 	if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2677 		zend_check_magic_method_non_static(ce, fptr, error_type);
2678 		zend_check_magic_method_no_return_type(ce, fptr, error_type);
2679 	} else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2680 		zend_check_magic_method_args(0, ce, fptr, error_type);
2681 		zend_check_magic_method_non_static(ce, fptr, error_type);
2682 		zend_check_magic_method_no_return_type(ce, fptr, error_type);
2683 	} else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2684 		zend_check_magic_method_args(0, ce, fptr, error_type);
2685 		zend_check_magic_method_non_static(ce, fptr, error_type);
2686 		zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2687 	} else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2688 		zend_check_magic_method_args(1, ce, fptr, error_type);
2689 		zend_check_magic_method_non_static(ce, fptr, error_type);
2690 		zend_check_magic_method_public(ce, fptr, error_type);
2691 		zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2692 	} else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2693 		zend_check_magic_method_args(2, ce, fptr, error_type);
2694 		zend_check_magic_method_non_static(ce, fptr, error_type);
2695 		zend_check_magic_method_public(ce, fptr, error_type);
2696 		zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2697 		zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2698 	} else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
2699 		zend_check_magic_method_args(1, ce, fptr, error_type);
2700 		zend_check_magic_method_non_static(ce, fptr, error_type);
2701 		zend_check_magic_method_public(ce, fptr, error_type);
2702 		zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2703 		zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2704 	} else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2705 		zend_check_magic_method_args(1, ce, fptr, error_type);
2706 		zend_check_magic_method_non_static(ce, fptr, error_type);
2707 		zend_check_magic_method_public(ce, fptr, error_type);
2708 		zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2709 		zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_BOOL);
2710 	} else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2711 		zend_check_magic_method_args(2, ce, fptr, error_type);
2712 		zend_check_magic_method_non_static(ce, fptr, error_type);
2713 		zend_check_magic_method_public(ce, fptr, error_type);
2714 		zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2715 		zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2716 	} else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
2717 		zend_check_magic_method_args(2, ce, fptr, error_type);
2718 		zend_check_magic_method_static(ce, fptr, error_type);
2719 		zend_check_magic_method_public(ce, fptr, error_type);
2720 		zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2721 		zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2722 	} else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
2723 		zend_check_magic_method_args(0, ce, fptr, error_type);
2724 		zend_check_magic_method_non_static(ce, fptr, error_type);
2725 		zend_check_magic_method_public(ce, fptr, error_type);
2726 		zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_STRING);
2727 	} else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
2728 		zend_check_magic_method_args(0, ce, fptr, error_type);
2729 		zend_check_magic_method_non_static(ce, fptr, error_type);
2730 		zend_check_magic_method_public(ce, fptr, error_type);
2731 		zend_check_magic_method_return_type(ce, fptr, error_type, (MAY_BE_ARRAY | MAY_BE_NULL));
2732 	} else if (zend_string_equals_literal(lcname, "__serialize")) {
2733 		zend_check_magic_method_args(0, ce, fptr, error_type);
2734 		zend_check_magic_method_non_static(ce, fptr, error_type);
2735 		zend_check_magic_method_public(ce, fptr, error_type);
2736 		zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2737 	} else if (zend_string_equals_literal(lcname, "__unserialize")) {
2738 		zend_check_magic_method_args(1, ce, fptr, error_type);
2739 		zend_check_magic_method_non_static(ce, fptr, error_type);
2740 		zend_check_magic_method_public(ce, fptr, error_type);
2741 		zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2742 		zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2743 	} else if (zend_string_equals_literal(lcname, "__set_state")) {
2744 		zend_check_magic_method_args(1, ce, fptr, error_type);
2745 		zend_check_magic_method_static(ce, fptr, error_type);
2746 		zend_check_magic_method_public(ce, fptr, error_type);
2747 		zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2748 		zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_OBJECT);
2749 	} else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) {
2750 		zend_check_magic_method_non_static(ce, fptr, error_type);
2751 		zend_check_magic_method_public(ce, fptr, error_type);
2752 	} else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_SLEEP))) {
2753 		zend_check_magic_method_args(0, ce, fptr, error_type);
2754 		zend_check_magic_method_non_static(ce, fptr, error_type);
2755 		zend_check_magic_method_public(ce, fptr, error_type);
2756 		zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2757 	} else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_WAKEUP))) {
2758 		zend_check_magic_method_args(0, ce, fptr, error_type);
2759 		zend_check_magic_method_non_static(ce, fptr, error_type);
2760 		zend_check_magic_method_public(ce, fptr, error_type);
2761 		zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2762 	}
2763 }
2764 /* }}} */
2765 
zend_add_magic_method(zend_class_entry * ce,zend_function * fptr,zend_string * lcname)2766 ZEND_API void zend_add_magic_method(zend_class_entry *ce, zend_function *fptr, zend_string *lcname)
2767 {
2768 	if (ZSTR_VAL(lcname)[0] != '_' || ZSTR_VAL(lcname)[1] != '_') {
2769 		/* pass */
2770 	} else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2771 		ce->clone = fptr;
2772 	} else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2773 		ce->constructor = fptr;
2774 		ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
2775 	} else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2776 		ce->destructor = fptr;
2777 	} else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2778 		ce->__get = fptr;
2779 		ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2780 	} else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2781 		ce->__set = fptr;
2782 		ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2783 	} else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2784 		ce->__call = fptr;
2785 	} else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
2786 		ce->__unset = fptr;
2787 		ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2788 	} else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2789 		ce->__isset = fptr;
2790 		ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2791 	} else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
2792 		ce->__callstatic = fptr;
2793 	} else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
2794 		ce->__tostring = fptr;
2795 	} else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
2796 		ce->__debugInfo = fptr;
2797 		ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2798 	} else if (zend_string_equals_literal(lcname, "__serialize")) {
2799 		ce->__serialize = fptr;
2800 	} else if (zend_string_equals_literal(lcname, "__unserialize")) {
2801 		ce->__unserialize = fptr;
2802 	}
2803 }
2804 
2805 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arg_info_toString, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()2806 ZEND_END_ARG_INFO()
2807 
2808 static zend_always_inline void zend_normalize_internal_type(zend_type *type) {
2809 	ZEND_ASSERT(!ZEND_TYPE_HAS_LITERAL_NAME(*type));
2810 	if (ZEND_TYPE_PURE_MASK(*type) != MAY_BE_ANY) {
2811 		ZEND_ASSERT(!ZEND_TYPE_CONTAINS_CODE(*type, IS_RESOURCE) && "resource is not allowed in a zend_type");
2812 	}
2813 	zend_type *current;
2814 	ZEND_TYPE_FOREACH(*type, current) {
2815 		if (ZEND_TYPE_HAS_NAME(*current)) {
2816 			zend_string *name = zend_new_interned_string(ZEND_TYPE_NAME(*current));
2817 			zend_alloc_ce_cache(name);
2818 			ZEND_TYPE_SET_PTR(*current, name);
2819 		} else if (ZEND_TYPE_HAS_LIST(*current)) {
2820 			zend_type *inner;
2821 			ZEND_TYPE_FOREACH(*current, inner) {
2822 				ZEND_ASSERT(!ZEND_TYPE_HAS_LITERAL_NAME(*inner) && !ZEND_TYPE_HAS_LIST(*inner));
2823 				if (ZEND_TYPE_HAS_NAME(*inner)) {
2824 					zend_string *name = zend_new_interned_string(ZEND_TYPE_NAME(*inner));
2825 					zend_alloc_ce_cache(name);
2826 					ZEND_TYPE_SET_PTR(*inner, name);
2827 				}
2828 			} ZEND_TYPE_FOREACH_END();
2829 		}
2830 	} ZEND_TYPE_FOREACH_END();
2831 }
2832 
2833 /* registers all functions in *library_functions in the function hash */
zend_register_functions(zend_class_entry * scope,const zend_function_entry * functions,HashTable * function_table,int type)2834 ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type) /* {{{ */
2835 {
2836 	const zend_function_entry *ptr = functions;
2837 	zend_function function;
2838 	zend_internal_function *reg_function, *internal_function = (zend_internal_function *)&function;
2839 	int count=0, unload=0;
2840 	HashTable *target_function_table = function_table;
2841 	int error_type;
2842 	zend_string *lowercase_name;
2843 	size_t fname_len;
2844 
2845 	if (type==MODULE_PERSISTENT) {
2846 		error_type = E_CORE_WARNING;
2847 	} else {
2848 		error_type = E_WARNING;
2849 	}
2850 
2851 	if (!target_function_table) {
2852 		target_function_table = CG(function_table);
2853 	}
2854 	internal_function->type = ZEND_INTERNAL_FUNCTION;
2855 	internal_function->module = EG(current_module);
2856 	internal_function->T = 0;
2857 	memset(internal_function->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
2858 
2859 	while (ptr->fname) {
2860 		fname_len = strlen(ptr->fname);
2861 		internal_function->handler = ptr->handler;
2862 		internal_function->doc_comment = ptr->doc_comment ? zend_string_init_interned(ptr->doc_comment, strlen(ptr->doc_comment), 1) : NULL;
2863 		internal_function->function_name = zend_string_init_interned(ptr->fname, fname_len, 1);
2864 		internal_function->scope = scope;
2865 		internal_function->prototype = NULL;
2866 		internal_function->attributes = NULL;
2867 		internal_function->frameless_function_infos = ptr->frameless_function_infos;
2868 		if (EG(active)) { // at run-time: this ought to only happen if registered with dl() or somehow temporarily at runtime
2869 			ZEND_MAP_PTR_INIT(internal_function->run_time_cache, zend_arena_calloc(&CG(arena), 1, zend_internal_run_time_cache_reserved_size()));
2870 		} else {
2871 			ZEND_MAP_PTR_NEW(internal_function->run_time_cache);
2872 		}
2873 		if (ptr->flags) {
2874 			if (!(ptr->flags & ZEND_ACC_PPP_MASK)) {
2875 				if (ptr->flags != ZEND_ACC_DEPRECATED && scope) {
2876 					zend_error(error_type, "Invalid access level for %s::%s() - access must be exactly one of public, protected or private", ZSTR_VAL(scope->name), ptr->fname);
2877 				}
2878 				internal_function->fn_flags = ZEND_ACC_PUBLIC | ptr->flags;
2879 			} else {
2880 				internal_function->fn_flags = ptr->flags;
2881 			}
2882 		} else {
2883 			internal_function->fn_flags = ZEND_ACC_PUBLIC;
2884 		}
2885 
2886 		if (ptr->arg_info) {
2887 			zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info;
2888 			internal_function->arg_info = (zend_internal_arg_info*)ptr->arg_info+1;
2889 			internal_function->num_args = ptr->num_args;
2890 			/* Currently you cannot denote that the function can accept less arguments than num_args */
2891 			if (info->required_num_args == (uintptr_t)-1) {
2892 				internal_function->required_num_args = ptr->num_args;
2893 			} else {
2894 				internal_function->required_num_args = info->required_num_args;
2895 			}
2896 			if (ZEND_ARG_SEND_MODE(info)) {
2897 				internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE;
2898 			}
2899 			if (ZEND_ARG_IS_VARIADIC(&ptr->arg_info[ptr->num_args])) {
2900 				internal_function->fn_flags |= ZEND_ACC_VARIADIC;
2901 				/* Don't count the variadic argument */
2902 				internal_function->num_args--;
2903 			}
2904 			if (ZEND_TYPE_IS_SET(info->type)) {
2905 				if (ZEND_TYPE_HAS_NAME(info->type)) {
2906 					const char *type_name = ZEND_TYPE_LITERAL_NAME(info->type);
2907 					if (!scope && (!strcasecmp(type_name, "self") || !strcasecmp(type_name, "parent"))) {
2908 						zend_error_noreturn(E_CORE_ERROR, "Cannot declare a return type of %s outside of a class scope", type_name);
2909 					}
2910 				}
2911 
2912 				internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
2913 			}
2914 		} else {
2915 			zend_error(E_CORE_WARNING, "Missing arginfo for %s%s%s()",
2916 				 scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2917 
2918 			internal_function->arg_info = NULL;
2919 			internal_function->num_args = 0;
2920 			internal_function->required_num_args = 0;
2921 		}
2922 
2923 		/* If not specified, add __toString() return type for compatibility with Stringable
2924 		 * interface. */
2925 		if (scope && zend_string_equals_literal_ci(internal_function->function_name, "__tostring") &&
2926 				!(internal_function->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
2927 			zend_error(E_CORE_WARNING, "%s::__toString() implemented without string return type",
2928 				ZSTR_VAL(scope->name));
2929 			internal_function->arg_info = (zend_internal_arg_info *) arg_info_toString + 1;
2930 			internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
2931 			internal_function->num_args = internal_function->required_num_args = 0;
2932 		}
2933 
2934 
2935 		zend_set_function_arg_flags((zend_function*)internal_function);
2936 		if (ptr->flags & ZEND_ACC_ABSTRACT) {
2937 			if (scope) {
2938 				/* This is a class that must be abstract itself. Here we set the check info. */
2939 				scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
2940 				if (!(scope->ce_flags & ZEND_ACC_INTERFACE)) {
2941 					/* Since the class is not an interface it needs to be declared as a abstract class. */
2942 					/* Since here we are handling internal functions only we can add the keyword flag. */
2943 					/* This time we set the flag for the keyword 'abstract'. */
2944 					scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
2945 				}
2946 			}
2947 			if ((ptr->flags & ZEND_ACC_STATIC) && (!scope || !(scope->ce_flags & ZEND_ACC_INTERFACE))) {
2948 				zend_error(error_type, "Static function %s%s%s() cannot be abstract", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2949 			}
2950 		} else {
2951 			if (scope && (scope->ce_flags & ZEND_ACC_INTERFACE)) {
2952 				zend_error(error_type, "Interface %s cannot contain non abstract method %s()", ZSTR_VAL(scope->name), ptr->fname);
2953 				return FAILURE;
2954 			}
2955 			if (!internal_function->handler) {
2956 				zend_error(error_type, "Method %s%s%s() cannot be a NULL function", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
2957 				zend_unregister_functions(functions, count, target_function_table);
2958 				return FAILURE;
2959 			}
2960 		}
2961 		lowercase_name = zend_string_tolower_ex(internal_function->function_name, type == MODULE_PERSISTENT);
2962 		lowercase_name = zend_new_interned_string(lowercase_name);
2963 		reg_function = malloc(sizeof(zend_internal_function));
2964 		memcpy(reg_function, &function, sizeof(zend_internal_function));
2965 		if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) {
2966 			unload=1;
2967 			free(reg_function);
2968 			zend_string_release(lowercase_name);
2969 			break;
2970 		}
2971 		if (reg_function->frameless_function_infos) {
2972 			const zend_frameless_function_info *flf_info = reg_function->frameless_function_infos;
2973 			while (flf_info->handler) {
2974 				if (zend_flf_count == zend_flf_capacity) {
2975 					if (!zend_flf_capacity) {
2976 						zend_flf_capacity = 8;
2977 					} else {
2978 						zend_flf_capacity *= 2;
2979 					}
2980 					/* +1 for NULL terminator */
2981 					zend_flf_handlers = realloc(zend_flf_handlers, (zend_flf_capacity + 1) * sizeof(void *));
2982 					zend_flf_functions = realloc(zend_flf_functions, (zend_flf_capacity + 1) * sizeof(zend_function *));
2983 				}
2984 				zend_flf_handlers[zend_flf_count] = flf_info->handler;
2985 				zend_flf_functions[zend_flf_count] = (zend_function *)reg_function;
2986 				zend_flf_count++;
2987 				flf_info++;
2988 			}
2989 			zend_flf_handlers[zend_flf_count] = NULL;
2990 			zend_flf_functions[zend_flf_count] = NULL;
2991 		}
2992 
2993 		/* Get parameter count including variadic parameter. */
2994 		uint32_t num_args = reg_function->num_args;
2995 		if (reg_function->fn_flags & ZEND_ACC_VARIADIC) {
2996 			num_args++;
2997 		}
2998 
2999 		/* If types of arguments have to be checked */
3000 		if (reg_function->arg_info && num_args) {
3001 			uint32_t i;
3002 			for (i = 0; i < num_args; i++) {
3003 				zend_internal_arg_info *arg_info = &reg_function->arg_info[i];
3004 				ZEND_ASSERT(arg_info->name && "Parameter must have a name");
3005 				if (ZEND_TYPE_IS_SET(arg_info->type)) {
3006 				    reg_function->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
3007 				}
3008 #if ZEND_DEBUG
3009 				for (uint32_t j = 0; j < i; j++) {
3010 					if (!strcmp(arg_info->name, reg_function->arg_info[j].name)) {
3011 						zend_error_noreturn(E_CORE_ERROR,
3012 							"Duplicate parameter name $%s for function %s%s%s()", arg_info->name,
3013 							scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3014 					}
3015 				}
3016 #endif
3017 			}
3018 		}
3019 
3020 		/* Rebuild arginfos if parameter/property types and/or a return type are used */
3021 		if (reg_function->arg_info &&
3022 		    (reg_function->fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS))) {
3023 			/* convert "const char*" class type names into "zend_string*" */
3024 			uint32_t i;
3025 			zend_internal_arg_info *arg_info = reg_function->arg_info - 1;
3026 			zend_internal_arg_info *new_arg_info;
3027 
3028 			/* Treat return type as an extra argument */
3029 			num_args++;
3030 			new_arg_info = malloc(sizeof(zend_internal_arg_info) * num_args);
3031 			memcpy(new_arg_info, arg_info, sizeof(zend_internal_arg_info) * num_args);
3032 			reg_function->arg_info = new_arg_info + 1;
3033 			for (i = 0; i < num_args; i++) {
3034 				if (ZEND_TYPE_HAS_LITERAL_NAME(new_arg_info[i].type)) {
3035 					// gen_stubs.php does not support codegen for DNF types in arg infos.
3036 					// As a temporary workaround, we split the type name on `|` characters,
3037 					// converting it to an union type if necessary.
3038 					const char *class_name = ZEND_TYPE_LITERAL_NAME(new_arg_info[i].type);
3039 					new_arg_info[i].type.type_mask &= ~_ZEND_TYPE_LITERAL_NAME_BIT;
3040 
3041 					size_t num_types = 1;
3042 					const char *p = class_name;
3043 					while ((p = strchr(p, '|'))) {
3044 						num_types++;
3045 						p++;
3046 					}
3047 
3048 					if (num_types == 1) {
3049 						/* Simple class type */
3050 						zend_string *str = zend_string_init_interned(class_name, strlen(class_name), 1);
3051 						zend_alloc_ce_cache(str);
3052 						ZEND_TYPE_SET_PTR(new_arg_info[i].type, str);
3053 						new_arg_info[i].type.type_mask |= _ZEND_TYPE_NAME_BIT;
3054 					} else {
3055 						/* Union type */
3056 						zend_type_list *list = malloc(ZEND_TYPE_LIST_SIZE(num_types));
3057 						list->num_types = num_types;
3058 						ZEND_TYPE_SET_LIST(new_arg_info[i].type, list);
3059 						ZEND_TYPE_FULL_MASK(new_arg_info[i].type) |= _ZEND_TYPE_UNION_BIT;
3060 
3061 						const char *start = class_name;
3062 						uint32_t j = 0;
3063 						while (true) {
3064 							const char *end = strchr(start, '|');
3065 							zend_string *str = zend_string_init_interned(start, end ? end - start : strlen(start), 1);
3066 							zend_alloc_ce_cache(str);
3067 							list->types[j] = (zend_type) ZEND_TYPE_INIT_CLASS(str, 0, 0);
3068 							if (!end) {
3069 								break;
3070 							}
3071 							start = end + 1;
3072 							j++;
3073 						}
3074 					}
3075 				}
3076 				if (ZEND_TYPE_IS_ITERABLE_FALLBACK(new_arg_info[i].type)) {
3077 					/* Warning generated an extension load warning which is emitted for every test
3078 					zend_error(E_CORE_WARNING, "iterable type is now a compile time alias for array|Traversable,"
3079 						" regenerate the argument info via the php-src gen_stub build script");
3080 					*/
3081 					zend_type legacy_iterable = ZEND_TYPE_INIT_CLASS_MASK(
3082 						ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
3083 						(new_arg_info[i].type.type_mask | MAY_BE_ARRAY)
3084 					);
3085 					new_arg_info[i].type = legacy_iterable;
3086 				}
3087 
3088 				zend_normalize_internal_type(&new_arg_info[i].type);
3089 			}
3090 		}
3091 
3092 		if (scope) {
3093 			zend_check_magic_method_implementation(
3094 				scope, (zend_function *)reg_function, lowercase_name, E_CORE_ERROR);
3095 			zend_add_magic_method(scope, (zend_function *)reg_function, lowercase_name);
3096 		}
3097 		ptr++;
3098 		count++;
3099 		zend_string_release(lowercase_name);
3100 	}
3101 	if (unload) { /* before unloading, display all remaining bad function in the module */
3102 		while (ptr->fname) {
3103 			fname_len = strlen(ptr->fname);
3104 			lowercase_name = zend_string_alloc(fname_len, 0);
3105 			zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
3106 			if (zend_hash_exists(target_function_table, lowercase_name)) {
3107 				zend_error(error_type, "Function registration failed - duplicate name - %s%s%s", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3108 			}
3109 			zend_string_efree(lowercase_name);
3110 			ptr++;
3111 		}
3112 		zend_unregister_functions(functions, count, target_function_table);
3113 		return FAILURE;
3114 	}
3115 	return SUCCESS;
3116 }
3117 /* }}} */
3118 
3119 /* count=-1 means erase all functions, otherwise,
3120  * erase the first count functions
3121  */
zend_unregister_functions(const zend_function_entry * functions,int count,HashTable * function_table)3122 ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table) /* {{{ */
3123 {
3124 	const zend_function_entry *ptr = functions;
3125 	int i=0;
3126 	HashTable *target_function_table = function_table;
3127 	zend_string *lowercase_name;
3128 	size_t fname_len;
3129 
3130 	if (!target_function_table) {
3131 		target_function_table = CG(function_table);
3132 	}
3133 	while (ptr->fname) {
3134 		if (count!=-1 && i>=count) {
3135 			break;
3136 		}
3137 		fname_len = strlen(ptr->fname);
3138 		lowercase_name = zend_string_alloc(fname_len, 0);
3139 		zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
3140 		zend_hash_del(target_function_table, lowercase_name);
3141 		zend_string_efree(lowercase_name);
3142 		ptr++;
3143 		i++;
3144 	}
3145 }
3146 /* }}} */
3147 
zend_startup_module(zend_module_entry * module)3148 ZEND_API zend_result zend_startup_module(zend_module_entry *module) /* {{{ */
3149 {
3150 	if ((module = zend_register_internal_module(module)) != NULL && zend_startup_module_ex(module) == SUCCESS) {
3151 		return SUCCESS;
3152 	}
3153 	return FAILURE;
3154 }
3155 /* }}} */
3156 
zend_get_module_started(const char * module_name)3157 ZEND_API zend_result zend_get_module_started(const char *module_name) /* {{{ */
3158 {
3159 	zend_module_entry *module;
3160 
3161 	module = zend_hash_str_find_ptr(&module_registry, module_name, strlen(module_name));
3162 	return (module && module->module_started) ? SUCCESS : FAILURE;
3163 }
3164 /* }}} */
3165 
clean_module_class(zval * el,void * arg)3166 static int clean_module_class(zval *el, void *arg) /* {{{ */
3167 {
3168 	zend_class_entry *ce = (zend_class_entry *)Z_PTR_P(el);
3169 	int module_number = *(int *)arg;
3170 	if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->module_number == module_number) {
3171 		return ZEND_HASH_APPLY_REMOVE;
3172 	} else {
3173 		return ZEND_HASH_APPLY_KEEP;
3174 	}
3175 }
3176 /* }}} */
3177 
clean_module_classes(int module_number)3178 static void clean_module_classes(int module_number) /* {{{ */
3179 {
3180 	zend_hash_apply_with_argument(EG(class_table), clean_module_class, (void *) &module_number);
3181 }
3182 /* }}} */
3183 
clean_module_function(zval * el,void * arg)3184 static int clean_module_function(zval *el, void *arg) /* {{{ */
3185 {
3186 	zend_function *fe = (zend_function *) Z_PTR_P(el);
3187 	zend_module_entry *module = (zend_module_entry *) arg;
3188 	if (fe->common.type == ZEND_INTERNAL_FUNCTION && fe->internal_function.module == module) {
3189 		return ZEND_HASH_APPLY_REMOVE;
3190 	} else {
3191 		return ZEND_HASH_APPLY_KEEP;
3192 	}
3193 }
3194 /* }}} */
3195 
clean_module_functions(zend_module_entry * module)3196 static void clean_module_functions(zend_module_entry *module) /* {{{ */
3197 {
3198 	zend_hash_apply_with_argument(CG(function_table), clean_module_function, module);
3199 }
3200 /* }}} */
3201 
module_destructor(zend_module_entry * module)3202 void module_destructor(zend_module_entry *module) /* {{{ */
3203 {
3204 #if ZEND_RC_DEBUG
3205 	bool orig_rc_debug = zend_rc_debug;
3206 #endif
3207 
3208 	if (module->type == MODULE_TEMPORARY) {
3209 #if ZEND_RC_DEBUG
3210 		/* FIXME: Loading extensions during the request breaks some invariants.
3211 		 * In particular, it will create persistent interned strings, which is
3212 		 * not allowed at this stage. */
3213 		zend_rc_debug = false;
3214 #endif
3215 		zend_clean_module_rsrc_dtors(module->module_number);
3216 		clean_module_constants(module->module_number);
3217 		clean_module_classes(module->module_number);
3218 	}
3219 
3220 	if (module->module_started && module->module_shutdown_func) {
3221 #if 0
3222 		zend_printf("%s: Module shutdown\n", module->name);
3223 #endif
3224 		module->module_shutdown_func(module->type, module->module_number);
3225 	}
3226 
3227 	if (module->module_started
3228 	 && !module->module_shutdown_func
3229 	 && module->type == MODULE_TEMPORARY) {
3230 		zend_unregister_ini_entries_ex(module->module_number, module->type);
3231 	}
3232 
3233 	/* Deinitialize module globals */
3234 	if (module->globals_size) {
3235 #ifdef ZTS
3236 		if (*module->globals_id_ptr) {
3237 			ts_free_id(*module->globals_id_ptr);
3238 		}
3239 #else
3240 		if (module->globals_dtor) {
3241 			module->globals_dtor(module->globals_ptr);
3242 		}
3243 #endif
3244 	}
3245 
3246 	module->module_started=0;
3247 	if (module->type == MODULE_TEMPORARY && module->functions) {
3248 		zend_unregister_functions(module->functions, -1, NULL);
3249 		/* Clean functions registered separately from module->functions */
3250 		clean_module_functions(module);
3251 	}
3252 
3253 #if ZEND_RC_DEBUG
3254 	zend_rc_debug = orig_rc_debug;
3255 #endif
3256 }
3257 /* }}} */
3258 
module_registry_unload(const zend_module_entry * module)3259 void module_registry_unload(const zend_module_entry *module)
3260 {
3261 #if HAVE_LIBDL
3262 	if (!getenv("ZEND_DONT_UNLOAD_MODULES")) {
3263 		DL_UNLOAD(module->handle);
3264 	}
3265 #else
3266 	ZEND_IGNORE_VALUE(module);
3267 #endif
3268 }
3269 
zend_activate_modules(void)3270 ZEND_API void zend_activate_modules(void) /* {{{ */
3271 {
3272 	zend_module_entry **p = module_request_startup_handlers;
3273 
3274 	while (*p) {
3275 		zend_module_entry *module = *p;
3276 
3277 		if (module->request_startup_func(module->type, module->module_number)==FAILURE) {
3278 			zend_error(E_WARNING, "request_startup() for %s module failed", module->name);
3279 			exit(1);
3280 		}
3281 		p++;
3282 	}
3283 }
3284 /* }}} */
3285 
zend_deactivate_modules(void)3286 ZEND_API void zend_deactivate_modules(void) /* {{{ */
3287 {
3288 	EG(current_execute_data) = NULL; /* we're no longer executing anything */
3289 
3290 	if (EG(full_tables_cleanup)) {
3291 		zend_module_entry *module;
3292 
3293 		ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) {
3294 			if (module->request_shutdown_func) {
3295 				zend_try {
3296 					module->request_shutdown_func(module->type, module->module_number);
3297 				} zend_end_try();
3298 			}
3299 		} ZEND_HASH_FOREACH_END();
3300 	} else {
3301 		zend_module_entry **p = module_request_shutdown_handlers;
3302 
3303 		while (*p) {
3304 			zend_module_entry *module = *p;
3305 			zend_try {
3306 				module->request_shutdown_func(module->type, module->module_number);
3307 			} zend_end_try();
3308 			p++;
3309 		}
3310 	}
3311 }
3312 /* }}} */
3313 
zend_unload_modules(void)3314 void zend_unload_modules(void) /* {{{ */
3315 {
3316 	zend_module_entry **modules = modules_dl_loaded;
3317 	while (*modules) {
3318 		module_registry_unload(*modules);
3319 		modules++;
3320 	}
3321 	free(modules_dl_loaded);
3322 	modules_dl_loaded = NULL;
3323 }
3324 /* }}} */
3325 
zend_post_deactivate_modules(void)3326 ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
3327 {
3328 	if (EG(full_tables_cleanup)) {
3329 		zend_module_entry *module;
3330 		zval *zv;
3331 		zend_string *key;
3332 
3333 		ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
3334 			if (module->post_deactivate_func) {
3335 				module->post_deactivate_func();
3336 			}
3337 		} ZEND_HASH_FOREACH_END();
3338 		ZEND_HASH_MAP_REVERSE_FOREACH_STR_KEY_VAL(&module_registry, key, zv) {
3339 			module = Z_PTR_P(zv);
3340 			if (module->type != MODULE_TEMPORARY) {
3341 				break;
3342 			}
3343 			module_destructor(module);
3344 			if (module->handle) {
3345 				module_registry_unload(module);
3346 			}
3347 			zend_string_release_ex(key, 0);
3348 		} ZEND_HASH_MAP_FOREACH_END_DEL();
3349 	} else {
3350 		zend_module_entry **p = module_post_deactivate_handlers;
3351 
3352 		while (*p) {
3353 			zend_module_entry *module = *p;
3354 
3355 			module->post_deactivate_func();
3356 			p++;
3357 		}
3358 	}
3359 }
3360 /* }}} */
3361 
3362 /* return the next free module number */
zend_next_free_module(void)3363 ZEND_API int zend_next_free_module(void) /* {{{ */
3364 {
3365 	return zend_hash_num_elements(&module_registry);
3366 }
3367 /* }}} */
3368 
do_register_internal_class(zend_class_entry * orig_class_entry,uint32_t ce_flags)3369 static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
3370 {
3371 	zend_class_entry *class_entry = malloc(sizeof(zend_class_entry));
3372 	zend_string *lowercase_name;
3373 	*class_entry = *orig_class_entry;
3374 
3375 	class_entry->type = ZEND_INTERNAL_CLASS;
3376 	zend_initialize_class_data(class_entry, 0);
3377 	zend_alloc_ce_cache(class_entry->name);
3378 	class_entry->ce_flags = orig_class_entry->ce_flags | ce_flags | ZEND_ACC_CONSTANTS_UPDATED | ZEND_ACC_LINKED | ZEND_ACC_RESOLVED_PARENT | ZEND_ACC_RESOLVED_INTERFACES;
3379 	class_entry->info.internal.module = EG(current_module);
3380 
3381 	if (class_entry->info.internal.builtin_functions) {
3382 		zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, EG(current_module)->type);
3383 	}
3384 
3385 	lowercase_name = zend_string_tolower_ex(orig_class_entry->name, EG(current_module)->type == MODULE_PERSISTENT);
3386 	lowercase_name = zend_new_interned_string(lowercase_name);
3387 	zend_hash_update_ptr(CG(class_table), lowercase_name, class_entry);
3388 	zend_string_release_ex(lowercase_name, 1);
3389 
3390 	if (class_entry->__tostring && !zend_string_equals_literal(class_entry->name, "Stringable")
3391 			&& !(class_entry->ce_flags & ZEND_ACC_TRAIT)) {
3392 		ZEND_ASSERT(zend_ce_stringable
3393 			&& "Should be registered before first class using __toString()");
3394 		zend_do_implement_interface(class_entry, zend_ce_stringable);
3395 	}
3396 	return class_entry;
3397 }
3398 /* }}} */
3399 
3400 /* If parent_ce is not NULL then it inherits from parent_ce
3401  * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it
3402  * If both parent_ce and parent_name are NULL it does a regular class registration
3403  * If parent_name is specified but not found NULL is returned
3404  */
zend_register_internal_class_ex(zend_class_entry * class_entry,zend_class_entry * parent_ce)3405 ZEND_API zend_class_entry *zend_register_internal_class_ex(zend_class_entry *class_entry, zend_class_entry *parent_ce) /* {{{ */
3406 {
3407 	zend_class_entry *register_class;
3408 
3409 	register_class = zend_register_internal_class(class_entry);
3410 
3411 	if (parent_ce) {
3412 		zend_do_inheritance(register_class, parent_ce);
3413 		zend_build_properties_info_table(register_class);
3414 	}
3415 
3416 	return register_class;
3417 }
3418 /* }}} */
3419 
zend_class_implements(zend_class_entry * class_entry,int num_interfaces,...)3420 ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...) /* {{{ */
3421 {
3422 	zend_class_entry *interface_entry;
3423 	va_list interface_list;
3424 	va_start(interface_list, num_interfaces);
3425 
3426 	while (num_interfaces--) {
3427 		interface_entry = va_arg(interface_list, zend_class_entry *);
3428 		if (interface_entry == zend_ce_stringable
3429 				&& zend_class_implements_interface(class_entry, zend_ce_stringable)) {
3430 			/* Stringable is implemented automatically,
3431 			 * silently ignore an explicit implementation. */
3432 			continue;
3433 		}
3434 
3435 		zend_do_implement_interface(class_entry, interface_entry);
3436 	}
3437 
3438 	va_end(interface_list);
3439 }
3440 /* }}} */
3441 
3442 /* A class that contains at least one abstract method automatically becomes an abstract class.
3443  */
zend_register_internal_class(zend_class_entry * orig_class_entry)3444 ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *orig_class_entry) /* {{{ */
3445 {
3446 	return do_register_internal_class(orig_class_entry, 0);
3447 }
3448 /* }}} */
3449 
zend_register_internal_interface(zend_class_entry * orig_class_entry)3450 ZEND_API zend_class_entry *zend_register_internal_interface(zend_class_entry *orig_class_entry) /* {{{ */
3451 {
3452 	return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE);
3453 }
3454 /* }}} */
3455 
zend_register_class_alias_ex(const char * name,size_t name_len,zend_class_entry * ce,bool persistent)3456 ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce, bool persistent) /* {{{ */
3457 {
3458 	zend_string *lcname;
3459 	zval zv, *ret;
3460 
3461 	/* TODO: Move this out of here in 7.4. */
3462 	if (persistent && EG(current_module) && EG(current_module)->type == MODULE_TEMPORARY) {
3463 		persistent = 0;
3464 	}
3465 
3466 	if (name[0] == '\\') {
3467 		lcname = zend_string_alloc(name_len-1, persistent);
3468 		zend_str_tolower_copy(ZSTR_VAL(lcname), name+1, name_len-1);
3469 	} else {
3470 		lcname = zend_string_alloc(name_len, persistent);
3471 		zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len);
3472 	}
3473 
3474 	zend_assert_valid_class_name(lcname);
3475 
3476 	lcname = zend_new_interned_string(lcname);
3477 
3478 	/* We cannot increase the refcount of an internal class during request time.
3479 	 * Instead of having to deal with differentiating between class types and lifetimes,
3480 	 * we simply don't increase the refcount of a class entry for aliases.
3481 	 */
3482 	ZVAL_ALIAS_PTR(&zv, ce);
3483 
3484 	ret = zend_hash_add(CG(class_table), lcname, &zv);
3485 	zend_string_release_ex(lcname, 0);
3486 	if (ret) {
3487 		// avoid notifying at MINIT time
3488 		if (ce->type == ZEND_USER_CLASS) {
3489 			zend_observer_class_linked_notify(ce, lcname);
3490 		}
3491 		return SUCCESS;
3492 	}
3493 	return FAILURE;
3494 }
3495 /* }}} */
3496 
3497 // TODO num_symbol_tables as unsigned int?
zend_set_hash_symbol(zval * symbol,const char * name,size_t name_length,bool is_ref,int num_symbol_tables,...)3498 ZEND_API zend_result zend_set_hash_symbol(zval *symbol, const char *name, size_t name_length, bool is_ref, int num_symbol_tables, ...) /* {{{ */
3499 {
3500 	HashTable *symbol_table;
3501 	va_list symbol_table_list;
3502 
3503 	if (num_symbol_tables <= 0) return FAILURE;
3504 
3505 	if (is_ref) {
3506 		ZVAL_MAKE_REF(symbol);
3507 	}
3508 
3509 	va_start(symbol_table_list, num_symbol_tables);
3510 	while (num_symbol_tables-- > 0) {
3511 		symbol_table = va_arg(symbol_table_list, HashTable *);
3512 		zend_hash_str_update(symbol_table, name, name_length, symbol);
3513 		Z_TRY_ADDREF_P(symbol);
3514 	}
3515 	va_end(symbol_table_list);
3516 	return SUCCESS;
3517 }
3518 /* }}} */
3519 
3520 /* Disabled functions support */
3521 
zend_disable_function(const char * function_name,size_t function_name_length)3522 static void zend_disable_function(const char *function_name, size_t function_name_length)
3523 {
3524 	zend_hash_str_del(CG(function_table), function_name, function_name_length);
3525 }
3526 
zend_disable_functions(const char * function_list)3527 ZEND_API void zend_disable_functions(const char *function_list) /* {{{ */
3528 {
3529 	if (!function_list || !*function_list) {
3530 		return;
3531 	}
3532 
3533 	const char *s = NULL, *e = function_list;
3534 	while (*e) {
3535 		switch (*e) {
3536 			case ' ':
3537 			case ',':
3538 				if (s) {
3539 					zend_disable_function(s, e - s);
3540 					s = NULL;
3541 				}
3542 				break;
3543 			default:
3544 				if (!s) {
3545 					s = e;
3546 				}
3547 				break;
3548 		}
3549 		e++;
3550 	}
3551 	if (s) {
3552 		zend_disable_function(s, e - s);
3553 	}
3554 
3555 	/* Rehash the function table after deleting functions. This ensures that all internal
3556 	 * functions are contiguous, which means we don't need to perform full table cleanup
3557 	 * on shutdown. */
3558 	zend_hash_rehash(CG(function_table));
3559 }
3560 /* }}} */
3561 
3562 #ifdef ZEND_WIN32
3563 #pragma optimize("", off)
3564 #endif
display_disabled_class(zend_class_entry * class_type)3565 static ZEND_COLD zend_object *display_disabled_class(zend_class_entry *class_type) /* {{{ */
3566 {
3567 	zend_object *intern;
3568 
3569 	intern = zend_objects_new(class_type);
3570 
3571 	/* Initialize default properties */
3572 	if (EXPECTED(class_type->default_properties_count != 0)) {
3573 		zval *p = intern->properties_table;
3574 		zval *end = p + class_type->default_properties_count;
3575 		do {
3576 			ZVAL_UNDEF(p);
3577 			p++;
3578 		} while (p != end);
3579 	}
3580 
3581 	zend_error(E_WARNING, "%s() has been disabled for security reasons", ZSTR_VAL(class_type->name));
3582 	return intern;
3583 }
3584 #ifdef ZEND_WIN32
3585 #pragma optimize("", on)
3586 #endif
3587 /* }}} */
3588 
3589 static const zend_function_entry disabled_class_new[] = {
3590 	ZEND_FE_END
3591 };
3592 
zend_disable_class(const char * class_name,size_t class_name_length)3593 ZEND_API zend_result zend_disable_class(const char *class_name, size_t class_name_length) /* {{{ */
3594 {
3595 	zend_class_entry *disabled_class;
3596 	zend_string *key;
3597 	zend_function *fn;
3598 	zend_property_info *prop;
3599 
3600 	key = zend_string_alloc(class_name_length, 0);
3601 	zend_str_tolower_copy(ZSTR_VAL(key), class_name, class_name_length);
3602 	disabled_class = zend_hash_find_ptr(CG(class_table), key);
3603 	zend_string_release_ex(key, 0);
3604 	if (!disabled_class) {
3605 		return FAILURE;
3606 	}
3607 
3608 	/* Will be reset by INIT_CLASS_ENTRY. */
3609 	free(disabled_class->interfaces);
3610 
3611 	INIT_CLASS_ENTRY_INIT_METHODS((*disabled_class), disabled_class_new);
3612 	disabled_class->create_object = display_disabled_class;
3613 
3614 	ZEND_HASH_MAP_FOREACH_PTR(&disabled_class->function_table, fn) {
3615 		if ((fn->common.fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS)) &&
3616 			fn->common.scope == disabled_class) {
3617 			zend_free_internal_arg_info(&fn->internal_function);
3618 		}
3619 	} ZEND_HASH_FOREACH_END();
3620 	zend_hash_clean(&disabled_class->function_table);
3621 	ZEND_HASH_MAP_FOREACH_PTR(&disabled_class->properties_info, prop) {
3622 		if (prop->ce == disabled_class) {
3623 			zend_string_release(prop->name);
3624 			zend_type_release(prop->type, /* persistent */ 1);
3625 			free(prop);
3626 		}
3627 	} ZEND_HASH_FOREACH_END();
3628 	zend_hash_clean(&disabled_class->properties_info);
3629 	return SUCCESS;
3630 }
3631 /* }}} */
3632 
get_scope(zend_execute_data * frame)3633 static zend_always_inline zend_class_entry *get_scope(zend_execute_data *frame)
3634 {
3635 	return frame && frame->func ? frame->func->common.scope : NULL;
3636 }
3637 
zend_is_callable_check_class(zend_string * name,zend_class_entry * scope,zend_execute_data * frame,zend_fcall_info_cache * fcc,bool * strict_class,char ** error,bool suppress_deprecation)3638 static bool zend_is_callable_check_class(zend_string *name, zend_class_entry *scope, zend_execute_data *frame, zend_fcall_info_cache *fcc, bool *strict_class, char **error, bool suppress_deprecation) /* {{{ */
3639 {
3640 	bool ret = 0;
3641 	zend_class_entry *ce;
3642 	size_t name_len = ZSTR_LEN(name);
3643 	zend_string *lcname;
3644 	ALLOCA_FLAG(use_heap);
3645 
3646 	ZSTR_ALLOCA_ALLOC(lcname, name_len, use_heap);
3647 	zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name), name_len);
3648 
3649 	*strict_class = 0;
3650 	if (zend_string_equals_literal(lcname, "self")) {
3651 		if (!scope) {
3652 			if (error) *error = estrdup("cannot access \"self\" when no class scope is active");
3653 		} else {
3654 			if (!suppress_deprecation) {
3655 				zend_error(E_DEPRECATED, "Use of \"self\" in callables is deprecated");
3656 			}
3657 			fcc->called_scope = zend_get_called_scope(frame);
3658 			if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope)) {
3659 				fcc->called_scope = scope;
3660 			}
3661 			fcc->calling_scope = scope;
3662 			if (!fcc->object) {
3663 				fcc->object = zend_get_this_object(frame);
3664 			}
3665 			ret = 1;
3666 		}
3667 	} else if (zend_string_equals_literal(lcname, "parent")) {
3668 		if (!scope) {
3669 			if (error) *error = estrdup("cannot access \"parent\" when no class scope is active");
3670 		} else if (!scope->parent) {
3671 			if (error) *error = estrdup("cannot access \"parent\" when current class scope has no parent");
3672 		} else {
3673 			if (!suppress_deprecation) {
3674 				zend_error(E_DEPRECATED, "Use of \"parent\" in callables is deprecated");
3675 			}
3676 			fcc->called_scope = zend_get_called_scope(frame);
3677 			if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope->parent)) {
3678 				fcc->called_scope = scope->parent;
3679 			}
3680 			fcc->calling_scope = scope->parent;
3681 			if (!fcc->object) {
3682 				fcc->object = zend_get_this_object(frame);
3683 			}
3684 			*strict_class = 1;
3685 			ret = 1;
3686 		}
3687 	} else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_STATIC))) {
3688 		zend_class_entry *called_scope = zend_get_called_scope(frame);
3689 
3690 		if (!called_scope) {
3691 			if (error) *error = estrdup("cannot access \"static\" when no class scope is active");
3692 		} else {
3693 			if (!suppress_deprecation) {
3694 				zend_error(E_DEPRECATED, "Use of \"static\" in callables is deprecated");
3695 			}
3696 			fcc->called_scope = called_scope;
3697 			fcc->calling_scope = called_scope;
3698 			if (!fcc->object) {
3699 				fcc->object = zend_get_this_object(frame);
3700 			}
3701 			*strict_class = 1;
3702 			ret = 1;
3703 		}
3704 	} else if ((ce = zend_lookup_class(name)) != NULL) {
3705 		zend_class_entry *scope = get_scope(frame);
3706 		fcc->calling_scope = ce;
3707 		if (scope && !fcc->object) {
3708 			zend_object *object = zend_get_this_object(frame);
3709 
3710 			if (object &&
3711 			    instanceof_function(object->ce, scope) &&
3712 			    instanceof_function(scope, ce)) {
3713 				fcc->object = object;
3714 				fcc->called_scope = object->ce;
3715 			} else {
3716 				fcc->called_scope = ce;
3717 			}
3718 		} else {
3719 			fcc->called_scope = fcc->object ? fcc->object->ce : ce;
3720 		}
3721 		*strict_class = 1;
3722 		ret = 1;
3723 	} else {
3724 		if (error) zend_spprintf(error, 0, "class \"%.*s\" not found", (int)name_len, ZSTR_VAL(name));
3725 	}
3726 	ZSTR_ALLOCA_FREE(lcname, use_heap);
3727 	return ret;
3728 }
3729 /* }}} */
3730 
zend_release_fcall_info_cache(zend_fcall_info_cache * fcc)3731 ZEND_API void zend_release_fcall_info_cache(zend_fcall_info_cache *fcc) {
3732 	if (fcc->function_handler &&
3733 		(fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
3734 		if (fcc->function_handler->common.function_name) {
3735 			zend_string_release_ex(fcc->function_handler->common.function_name, 0);
3736 		}
3737 		zend_free_trampoline(fcc->function_handler);
3738 		fcc->function_handler = NULL;
3739 	}
3740 }
3741 
zend_is_callable_check_func(zval * callable,zend_execute_data * frame,zend_fcall_info_cache * fcc,bool strict_class,char ** error,bool suppress_deprecation)3742 static zend_always_inline bool zend_is_callable_check_func(zval *callable, zend_execute_data *frame, zend_fcall_info_cache *fcc, bool strict_class, char **error, bool suppress_deprecation) /* {{{ */
3743 {
3744 	zend_class_entry *ce_org = fcc->calling_scope;
3745 	bool retval = 0;
3746 	zend_string *mname, *cname;
3747 	zend_string *lmname;
3748 	const char *colon;
3749 	size_t clen;
3750 	HashTable *ftable;
3751 	int call_via_handler = 0;
3752 	zend_class_entry *scope;
3753 	zval *zv;
3754 	ALLOCA_FLAG(use_heap)
3755 
3756 	fcc->calling_scope = NULL;
3757 
3758 	if (!ce_org) {
3759 		zend_function *func;
3760 		zend_string *lmname;
3761 
3762 		/* Check if function with given name exists.
3763 		 * This may be a compound name that includes namespace name */
3764 		if (UNEXPECTED(Z_STRVAL_P(callable)[0] == '\\')) {
3765 			/* Skip leading \ */
3766 			ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable) - 1, use_heap);
3767 			zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable) + 1, Z_STRLEN_P(callable) - 1);
3768 			func = zend_fetch_function(lmname);
3769 			ZSTR_ALLOCA_FREE(lmname, use_heap);
3770 		} else {
3771 			lmname = Z_STR_P(callable);
3772 			func = zend_fetch_function(lmname);
3773 			if (!func) {
3774 				ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable), use_heap);
3775 				zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable), Z_STRLEN_P(callable));
3776 				func = zend_fetch_function(lmname);
3777 				ZSTR_ALLOCA_FREE(lmname, use_heap);
3778 			}
3779 		}
3780 		if (EXPECTED(func != NULL)) {
3781 			fcc->function_handler = func;
3782 			return 1;
3783 		}
3784 	}
3785 
3786 	/* Split name into class/namespace and method/function names */
3787 	if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL &&
3788 		colon > Z_STRVAL_P(callable) &&
3789 		*(colon-1) == ':'
3790 	) {
3791 		size_t mlen;
3792 
3793 		colon--;
3794 		clen = colon - Z_STRVAL_P(callable);
3795 		mlen = Z_STRLEN_P(callable) - clen - 2;
3796 
3797 		if (colon == Z_STRVAL_P(callable)) {
3798 			if (error) *error = estrdup("invalid function name");
3799 			return 0;
3800 		}
3801 
3802 		/* This is a compound name.
3803 		 * Try to fetch class and then find static method. */
3804 		if (ce_org) {
3805 			scope = ce_org;
3806 		} else {
3807 			scope = get_scope(frame);
3808 		}
3809 
3810 		cname = zend_string_init_interned(Z_STRVAL_P(callable), clen, 0);
3811 		if (ZSTR_HAS_CE_CACHE(cname) && ZSTR_GET_CE_CACHE(cname)) {
3812 			fcc->calling_scope = ZSTR_GET_CE_CACHE(cname);
3813 			if (scope && !fcc->object) {
3814 				zend_object *object = zend_get_this_object(frame);
3815 
3816 				if (object &&
3817 				    instanceof_function(object->ce, scope) &&
3818 				    instanceof_function(scope, fcc->calling_scope)) {
3819 					fcc->object = object;
3820 					fcc->called_scope = object->ce;
3821 				} else {
3822 					fcc->called_scope = fcc->calling_scope;
3823 				}
3824 			} else {
3825 				fcc->called_scope = fcc->object ? fcc->object->ce : fcc->calling_scope;
3826 			}
3827 			strict_class = 1;
3828 		} else if (!zend_is_callable_check_class(cname, scope, frame, fcc, &strict_class, error, suppress_deprecation || ce_org != NULL)) {
3829 			zend_string_release_ex(cname, 0);
3830 			return 0;
3831 		}
3832 		zend_string_release_ex(cname, 0);
3833 
3834 		ftable = &fcc->calling_scope->function_table;
3835 		if (ce_org && !instanceof_function(ce_org, fcc->calling_scope)) {
3836 			if (error) zend_spprintf(error, 0, "class %s is not a subclass of %s", ZSTR_VAL(ce_org->name), ZSTR_VAL(fcc->calling_scope->name));
3837 			return 0;
3838 		}
3839 		if (ce_org && !suppress_deprecation) {
3840 			zend_error(E_DEPRECATED,
3841 				"Callables of the form [\"%s\", \"%s\"] are deprecated",
3842 				ZSTR_VAL(ce_org->name), Z_STRVAL_P(callable));
3843 		}
3844 		mname = zend_string_init(Z_STRVAL_P(callable) + clen + 2, mlen, 0);
3845 	} else if (ce_org) {
3846 		/* Try to fetch find static method of given class. */
3847 		mname = Z_STR_P(callable);
3848 		zend_string_addref(mname);
3849 		ftable = &ce_org->function_table;
3850 		fcc->calling_scope = ce_org;
3851 	} else {
3852 		/* We already checked for plain function before. */
3853 		if (error) {
3854 			zend_spprintf(error, 0, "function \"%s\" not found or invalid function name", Z_STRVAL_P(callable));
3855 		}
3856 		return 0;
3857 	}
3858 
3859 	lmname = zend_string_tolower(mname);
3860 	if (strict_class &&
3861 	    fcc->calling_scope &&
3862 		zend_string_equals_literal(lmname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
3863 		fcc->function_handler = fcc->calling_scope->constructor;
3864 		if (fcc->function_handler) {
3865 			retval = 1;
3866 		}
3867 	} else if ((zv = zend_hash_find(ftable, lmname)) != NULL) {
3868 		fcc->function_handler = Z_PTR_P(zv);
3869 		retval = 1;
3870 		if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) &&
3871 		    !strict_class) {
3872 			scope = get_scope(frame);
3873 			if (scope &&
3874 			    instanceof_function(fcc->function_handler->common.scope, scope)) {
3875 
3876 				zv = zend_hash_find(&scope->function_table, lmname);
3877 				if (zv != NULL) {
3878 					zend_function *priv_fbc = Z_PTR_P(zv);
3879 
3880 					if ((priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE)
3881 					 && priv_fbc->common.scope == scope) {
3882 						fcc->function_handler = priv_fbc;
3883 					}
3884 				}
3885 			}
3886 		}
3887 		if (!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC) &&
3888 		    (fcc->calling_scope &&
3889 		     ((fcc->object && fcc->calling_scope->__call) ||
3890 		      (!fcc->object && fcc->calling_scope->__callstatic)))) {
3891 			scope = get_scope(frame);
3892 			if (fcc->function_handler->common.scope != scope) {
3893 				if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
3894 				 || !zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope)) {
3895 					retval = 0;
3896 					fcc->function_handler = NULL;
3897 					goto get_function_via_handler;
3898 				}
3899 			}
3900 		}
3901 	} else {
3902 get_function_via_handler:
3903 		if (fcc->object && fcc->calling_scope == ce_org) {
3904 			if (strict_class && ce_org->__call) {
3905 				fcc->function_handler = zend_get_call_trampoline_func(ce_org, mname, 0);
3906 				call_via_handler = 1;
3907 				retval = 1;
3908 			} else {
3909 				fcc->function_handler = fcc->object->handlers->get_method(&fcc->object, mname, NULL);
3910 				if (fcc->function_handler) {
3911 					if (strict_class &&
3912 					    (!fcc->function_handler->common.scope ||
3913 					     !instanceof_function(ce_org, fcc->function_handler->common.scope))) {
3914 						zend_release_fcall_info_cache(fcc);
3915 					} else {
3916 						retval = 1;
3917 						call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3918 					}
3919 				}
3920 			}
3921 		} else if (fcc->calling_scope) {
3922 			if (fcc->calling_scope->get_static_method) {
3923 				fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname);
3924 			} else {
3925 				fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, NULL);
3926 			}
3927 			if (fcc->function_handler) {
3928 				retval = 1;
3929 				call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
3930 				if (call_via_handler && !fcc->object) {
3931 					zend_object *object = zend_get_this_object(frame);
3932 					if (object &&
3933 					    instanceof_function(object->ce, fcc->calling_scope)) {
3934 						fcc->object = object;
3935 					}
3936 				}
3937 			}
3938 		}
3939 	}
3940 
3941 	if (retval) {
3942 		if (fcc->calling_scope && !call_via_handler) {
3943 			if (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT) {
3944 				retval = 0;
3945 				if (error) {
3946 					zend_spprintf(error, 0, "cannot call abstract method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
3947 				}
3948 			} else if (!fcc->object && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
3949 				retval = 0;
3950 				if (error) {
3951 					zend_spprintf(error, 0, "non-static method %s::%s() cannot be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
3952 				}
3953 			}
3954 			if (retval
3955 			 && !(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC)) {
3956 				scope = get_scope(frame);
3957 				if (fcc->function_handler->common.scope != scope) {
3958 					if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
3959 					 || (!zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope))) {
3960 						if (error) {
3961 							if (*error) {
3962 								efree(*error);
3963 							}
3964 							zend_spprintf(error, 0, "cannot access %s method %s::%s()", zend_visibility_string(fcc->function_handler->common.fn_flags), ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
3965 						}
3966 						retval = 0;
3967 					}
3968 				}
3969 			}
3970 		}
3971 	} else if (error) {
3972 		if (fcc->calling_scope) {
3973 			zend_spprintf(error, 0, "class %s does not have a method \"%s\"", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(mname));
3974 		} else {
3975 			zend_spprintf(error, 0, "function %s() does not exist", ZSTR_VAL(mname));
3976 		}
3977 	}
3978 	zend_string_release_ex(lmname, 0);
3979 	zend_string_release_ex(mname, 0);
3980 
3981 	if (fcc->object) {
3982 		fcc->called_scope = fcc->object->ce;
3983 		if (fcc->function_handler
3984 		 && (fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
3985 			fcc->object = NULL;
3986 		}
3987 	}
3988 	return retval;
3989 }
3990 /* }}} */
3991 
zend_get_callable_name_ex(zval * callable,zend_object * object)3992 ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *object) /* {{{ */
3993 {
3994 try_again:
3995 	switch (Z_TYPE_P(callable)) {
3996 		case IS_STRING:
3997 			if (object) {
3998 				return zend_create_member_string(object->ce->name, Z_STR_P(callable));
3999 			}
4000 			return zend_string_copy(Z_STR_P(callable));
4001 
4002 		case IS_ARRAY:
4003 		{
4004 			zval *method = NULL;
4005 			zval *obj = NULL;
4006 
4007 			if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
4008 				obj = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 0);
4009 				method = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 1);
4010 			}
4011 
4012 			if (obj == NULL || method == NULL || Z_TYPE_P(method) != IS_STRING) {
4013 				return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
4014 			}
4015 
4016 			if (Z_TYPE_P(obj) == IS_STRING) {
4017 				return zend_create_member_string(Z_STR_P(obj), Z_STR_P(method));
4018 			} else if (Z_TYPE_P(obj) == IS_OBJECT) {
4019 				return zend_create_member_string(Z_OBJCE_P(obj)->name, Z_STR_P(method));
4020 			} else {
4021 				return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
4022 			}
4023 		}
4024 		case IS_OBJECT:
4025 		{
4026 			zend_class_entry *ce = Z_OBJCE_P(callable);
4027 			return zend_string_concat2(
4028 				ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
4029 				"::__invoke", sizeof("::__invoke") - 1);
4030 		}
4031 		case IS_REFERENCE:
4032 			callable = Z_REFVAL_P(callable);
4033 			goto try_again;
4034 		default:
4035 			return zval_get_string_func(callable);
4036 	}
4037 }
4038 /* }}} */
4039 
zend_get_callable_name(zval * callable)4040 ZEND_API zend_string *zend_get_callable_name(zval *callable) /* {{{ */
4041 {
4042 	return zend_get_callable_name_ex(callable, NULL);
4043 }
4044 /* }}} */
4045 
zend_is_callable_at_frame(zval * callable,zend_object * object,zend_execute_data * frame,uint32_t check_flags,zend_fcall_info_cache * fcc,char ** error)4046 ZEND_API bool zend_is_callable_at_frame(
4047 		zval *callable, zend_object *object, zend_execute_data *frame,
4048 		uint32_t check_flags, zend_fcall_info_cache *fcc, char **error) /* {{{ */
4049 {
4050 	bool ret;
4051 	zend_fcall_info_cache fcc_local;
4052 	bool strict_class = 0;
4053 
4054 	if (fcc == NULL) {
4055 		fcc = &fcc_local;
4056 	}
4057 	if (error) {
4058 		*error = NULL;
4059 	}
4060 
4061 	fcc->calling_scope = NULL;
4062 	fcc->called_scope = NULL;
4063 	fcc->function_handler = NULL;
4064 	fcc->object = NULL;
4065 	fcc->closure = NULL;
4066 
4067 again:
4068 	switch (Z_TYPE_P(callable)) {
4069 		case IS_STRING:
4070 			if (object) {
4071 				fcc->object = object;
4072 				fcc->calling_scope = object->ce;
4073 			}
4074 
4075 			if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4076 				fcc->called_scope = fcc->calling_scope;
4077 				return 1;
4078 			}
4079 
4080 check_func:
4081 			ret = zend_is_callable_check_func(callable, frame, fcc, strict_class, error, check_flags & IS_CALLABLE_SUPPRESS_DEPRECATIONS);
4082 			if (fcc == &fcc_local) {
4083 				zend_release_fcall_info_cache(fcc);
4084 			}
4085 			return ret;
4086 
4087 		case IS_ARRAY:
4088 			{
4089 				if (zend_hash_num_elements(Z_ARRVAL_P(callable)) != 2) {
4090 					if (error) *error = estrdup("array callback must have exactly two members");
4091 					return 0;
4092 				}
4093 
4094 				zval *obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0);
4095 				zval *method = zend_hash_index_find(Z_ARRVAL_P(callable), 1);
4096 				if (!obj || !method) {
4097 					if (error) *error = estrdup("array callback has to contain indices 0 and 1");
4098 					return 0;
4099 				}
4100 
4101 				ZVAL_DEREF(obj);
4102 				if (Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT) {
4103 					if (error) *error = estrdup("first array member is not a valid class name or object");
4104 					return 0;
4105 				}
4106 
4107 				ZVAL_DEREF(method);
4108 				if (Z_TYPE_P(method) != IS_STRING) {
4109 					if (error) *error = estrdup("second array member is not a valid method");
4110 					return 0;
4111 				}
4112 
4113 				if (Z_TYPE_P(obj) == IS_STRING) {
4114 					if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4115 						return 1;
4116 					}
4117 
4118 					if (!zend_is_callable_check_class(Z_STR_P(obj), get_scope(frame), frame, fcc, &strict_class, error, check_flags & IS_CALLABLE_SUPPRESS_DEPRECATIONS)) {
4119 						return 0;
4120 					}
4121 				} else {
4122 					ZEND_ASSERT(Z_TYPE_P(obj) == IS_OBJECT);
4123 					fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */
4124 					fcc->object = Z_OBJ_P(obj);
4125 
4126 					if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4127 						fcc->called_scope = fcc->calling_scope;
4128 						return 1;
4129 					}
4130 				}
4131 
4132 				callable = method;
4133 				goto check_func;
4134 			}
4135 			return 0;
4136 		case IS_OBJECT:
4137 			if (Z_OBJ_HANDLER_P(callable, get_closure) && Z_OBJ_HANDLER_P(callable, get_closure)(Z_OBJ_P(callable), &fcc->calling_scope, &fcc->function_handler, &fcc->object, 1) == SUCCESS) {
4138 				fcc->called_scope = fcc->calling_scope;
4139 				fcc->closure = Z_OBJ_P(callable);
4140 				if (fcc == &fcc_local) {
4141 					zend_release_fcall_info_cache(fcc);
4142 				}
4143 				return 1;
4144 			}
4145 			if (error) *error = estrdup("no array or string given");
4146 			return 0;
4147 		case IS_REFERENCE:
4148 			callable = Z_REFVAL_P(callable);
4149 			goto again;
4150 		default:
4151 			if (error) *error = estrdup("no array or string given");
4152 			return 0;
4153 	}
4154 }
4155 /* }}} */
4156 
zend_is_callable_ex(zval * callable,zend_object * object,uint32_t check_flags,zend_string ** callable_name,zend_fcall_info_cache * fcc,char ** error)4157 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) /* {{{ */
4158 {
4159 	/* Determine callability at the first parent user frame. */
4160 	zend_execute_data *frame = EG(current_execute_data);
4161 	while (frame && (!frame->func || !ZEND_USER_CODE(frame->func->type))) {
4162 		frame = frame->prev_execute_data;
4163 	}
4164 
4165 	bool ret = zend_is_callable_at_frame(callable, object, frame, check_flags, fcc, error);
4166 	if (callable_name) {
4167 		*callable_name = zend_get_callable_name_ex(callable, object);
4168 	}
4169 	return ret;
4170 }
4171 
zend_is_callable(zval * callable,uint32_t check_flags,zend_string ** callable_name)4172 ZEND_API bool zend_is_callable(zval *callable, uint32_t check_flags, zend_string **callable_name) /* {{{ */
4173 {
4174 	return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL);
4175 }
4176 /* }}} */
4177 
zend_make_callable(zval * callable,zend_string ** callable_name)4178 ZEND_API bool zend_make_callable(zval *callable, zend_string **callable_name) /* {{{ */
4179 {
4180 	zend_fcall_info_cache fcc;
4181 
4182 	if (zend_is_callable_ex(callable, NULL, IS_CALLABLE_SUPPRESS_DEPRECATIONS, callable_name, &fcc, NULL)) {
4183 		if (Z_TYPE_P(callable) == IS_STRING && fcc.calling_scope) {
4184 			zval_ptr_dtor_str(callable);
4185 			array_init(callable);
4186 			add_next_index_str(callable, zend_string_copy(fcc.calling_scope->name));
4187 			add_next_index_str(callable, zend_string_copy(fcc.function_handler->common.function_name));
4188 		}
4189 		zend_release_fcall_info_cache(&fcc);
4190 		return 1;
4191 	}
4192 	return 0;
4193 }
4194 /* }}} */
4195 
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)4196 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) /* {{{ */
4197 {
4198 	if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, fcc, error)) {
4199 		return FAILURE;
4200 	}
4201 
4202 	fci->size = sizeof(*fci);
4203 	fci->object = fcc->object;
4204 	ZVAL_COPY_VALUE(&fci->function_name, callable);
4205 	fci->retval = NULL;
4206 	fci->param_count = 0;
4207 	fci->params = NULL;
4208 	fci->named_params = NULL;
4209 
4210 	return SUCCESS;
4211 }
4212 /* }}} */
4213 
zend_fcall_info_args_clear(zend_fcall_info * fci,bool free_mem)4214 ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, bool free_mem) /* {{{ */
4215 {
4216 	if (fci->params) {
4217 		zval *p = fci->params;
4218 		zval *end = p + fci->param_count;
4219 
4220 		while (p != end) {
4221 			i_zval_ptr_dtor(p);
4222 			p++;
4223 		}
4224 		if (free_mem) {
4225 			efree(fci->params);
4226 			fci->params = NULL;
4227 		}
4228 	}
4229 	fci->param_count = 0;
4230 }
4231 /* }}} */
4232 
zend_fcall_info_args_save(zend_fcall_info * fci,uint32_t * param_count,zval ** params)4233 ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, uint32_t *param_count, zval **params) /* {{{ */
4234 {
4235 	*param_count = fci->param_count;
4236 	*params = fci->params;
4237 	fci->param_count = 0;
4238 	fci->params = NULL;
4239 }
4240 /* }}} */
4241 
zend_fcall_info_args_restore(zend_fcall_info * fci,uint32_t param_count,zval * params)4242 ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, uint32_t param_count, zval *params) /* {{{ */
4243 {
4244 	zend_fcall_info_args_clear(fci, 1);
4245 	fci->param_count = param_count;
4246 	fci->params = params;
4247 }
4248 /* }}} */
4249 
zend_fcall_info_args_ex(zend_fcall_info * fci,zend_function * func,zval * args)4250 ZEND_API zend_result zend_fcall_info_args_ex(zend_fcall_info *fci, zend_function *func, zval *args) /* {{{ */
4251 {
4252 	zval *arg, *params;
4253 	uint32_t n = 1;
4254 
4255 	zend_fcall_info_args_clear(fci, !args);
4256 
4257 	if (!args) {
4258 		return SUCCESS;
4259 	}
4260 
4261 	if (Z_TYPE_P(args) != IS_ARRAY) {
4262 		return FAILURE;
4263 	}
4264 
4265 	fci->param_count = zend_hash_num_elements(Z_ARRVAL_P(args));
4266 	fci->params = params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4267 
4268 	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), arg) {
4269 		if (func && !Z_ISREF_P(arg) && ARG_SHOULD_BE_SENT_BY_REF(func, n)) {
4270 			ZVAL_NEW_REF(params, arg);
4271 			Z_TRY_ADDREF_P(arg);
4272 		} else {
4273 			ZVAL_COPY(params, arg);
4274 		}
4275 		params++;
4276 		n++;
4277 	} ZEND_HASH_FOREACH_END();
4278 
4279 	return SUCCESS;
4280 }
4281 /* }}} */
4282 
zend_fcall_info_args(zend_fcall_info * fci,zval * args)4283 ZEND_API zend_result zend_fcall_info_args(zend_fcall_info *fci, zval *args) /* {{{ */
4284 {
4285 	return zend_fcall_info_args_ex(fci, NULL, args);
4286 }
4287 /* }}} */
4288 
zend_fcall_info_argp(zend_fcall_info * fci,uint32_t argc,zval * argv)4289 ZEND_API void zend_fcall_info_argp(zend_fcall_info *fci, uint32_t argc, zval *argv) /* {{{ */
4290 {
4291 	zend_fcall_info_args_clear(fci, !argc);
4292 
4293 	if (argc) {
4294 		fci->param_count = argc;
4295 		fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4296 
4297 		for (uint32_t i = 0; i < argc; ++i) {
4298 			ZVAL_COPY(&fci->params[i], &argv[i]);
4299 		}
4300 	}
4301 }
4302 /* }}} */
4303 
zend_fcall_info_argv(zend_fcall_info * fci,uint32_t argc,va_list * argv)4304 ZEND_API void zend_fcall_info_argv(zend_fcall_info *fci, uint32_t argc, va_list *argv) /* {{{ */
4305 {
4306 	zend_fcall_info_args_clear(fci, !argc);
4307 
4308 	if (argc) {
4309 		zval *arg;
4310 		fci->param_count = argc;
4311 		fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4312 
4313 		for (uint32_t i = 0; i < argc; ++i) {
4314 			arg = va_arg(*argv, zval *);
4315 			ZVAL_COPY(&fci->params[i], arg);
4316 		}
4317 	}
4318 }
4319 /* }}} */
4320 
zend_fcall_info_argn(zend_fcall_info * fci,uint32_t argc,...)4321 ZEND_API void zend_fcall_info_argn(zend_fcall_info *fci, uint32_t argc, ...) /* {{{ */
4322 {
4323 	va_list argv;
4324 
4325 	va_start(argv, argc);
4326 	zend_fcall_info_argv(fci, argc, &argv);
4327 	va_end(argv);
4328 }
4329 /* }}} */
4330 
zend_fcall_info_call(zend_fcall_info * fci,zend_fcall_info_cache * fcc,zval * retval_ptr,zval * args)4331 ZEND_API zend_result zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval *retval_ptr, zval *args) /* {{{ */
4332 {
4333 	zval retval, *org_params = NULL;
4334 	uint32_t org_count = 0;
4335 	zend_result result;
4336 
4337 	fci->retval = retval_ptr ? retval_ptr : &retval;
4338 	if (args) {
4339 		zend_fcall_info_args_save(fci, &org_count, &org_params);
4340 		zend_fcall_info_args(fci, args);
4341 	}
4342 	result = zend_call_function(fci, fcc);
4343 
4344 	if (!retval_ptr && Z_TYPE(retval) != IS_UNDEF) {
4345 		zval_ptr_dtor(&retval);
4346 	}
4347 	if (args) {
4348 		zend_fcall_info_args_restore(fci, org_count, org_params);
4349 	}
4350 	return result;
4351 }
4352 /* }}} */
4353 
zend_get_callable_zval_from_fcc(const zend_fcall_info_cache * fcc,zval * callable)4354 ZEND_API void zend_get_callable_zval_from_fcc(const zend_fcall_info_cache *fcc, zval *callable)
4355 {
4356 	if (fcc->closure) {
4357 		ZVAL_OBJ_COPY(callable, fcc->closure);
4358 	} else if (fcc->function_handler->common.scope) {
4359 		array_init(callable);
4360 		if (fcc->object) {
4361 			GC_ADDREF(fcc->object);
4362 			add_next_index_object(callable, fcc->object);
4363 		} else {
4364 			add_next_index_str(callable, zend_string_copy(fcc->calling_scope->name));
4365 		}
4366 		add_next_index_str(callable, zend_string_copy(fcc->function_handler->common.function_name));
4367 	} else {
4368 		ZVAL_STR_COPY(callable, fcc->function_handler->common.function_name);
4369 	}
4370 }
4371 
zend_get_module_version(const char * module_name)4372 ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */
4373 {
4374 	zend_string *lname;
4375 	size_t name_len = strlen(module_name);
4376 	zend_module_entry *module;
4377 
4378 	lname = zend_string_alloc(name_len, 0);
4379 	zend_str_tolower_copy(ZSTR_VAL(lname), module_name, name_len);
4380 	module = zend_hash_find_ptr(&module_registry, lname);
4381 	zend_string_efree(lname);
4382 	return module ? module->version : NULL;
4383 }
4384 /* }}} */
4385 
is_persistent_class(zend_class_entry * ce)4386 static zend_always_inline bool is_persistent_class(zend_class_entry *ce) {
4387 	return (ce->type & ZEND_INTERNAL_CLASS)
4388 		&& ce->info.internal.module->type == MODULE_PERSISTENT;
4389 }
4390 
zend_declare_typed_property(zend_class_entry * ce,zend_string * name,zval * property,int access_type,zend_string * doc_comment,zend_type type)4391 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) /* {{{ */
4392 {
4393 	zend_property_info *property_info, *property_info_ptr;
4394 
4395 	if (ZEND_TYPE_IS_SET(type)) {
4396 		ce->ce_flags |= ZEND_ACC_HAS_TYPE_HINTS;
4397 
4398 		if (access_type & ZEND_ACC_READONLY) {
4399 			ce->ce_flags |= ZEND_ACC_HAS_READONLY_PROPS;
4400 		}
4401 	}
4402 
4403 	if (ce->type == ZEND_INTERNAL_CLASS) {
4404 		property_info = pemalloc(sizeof(zend_property_info), 1);
4405 	} else {
4406 		property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
4407 		if (Z_TYPE_P(property) == IS_CONSTANT_AST) {
4408 			ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
4409 			if (access_type & ZEND_ACC_STATIC) {
4410 				ce->ce_flags |= ZEND_ACC_HAS_AST_STATICS;
4411 			} else {
4412 				ce->ce_flags |= ZEND_ACC_HAS_AST_PROPERTIES;
4413 			}
4414 		}
4415 	}
4416 
4417 	if (Z_TYPE_P(property) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(property))) {
4418 		zval_make_interned_string(property);
4419 	}
4420 
4421 	if (!(access_type & ZEND_ACC_PPP_MASK)) {
4422 		access_type |= ZEND_ACC_PUBLIC;
4423 	}
4424 	if (access_type & ZEND_ACC_STATIC) {
4425 		if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
4426 			ZEND_ASSERT(property_info_ptr->flags & ZEND_ACC_STATIC);
4427 			property_info->offset = property_info_ptr->offset;
4428 			zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]);
4429 			if (property_info_ptr->doc_comment && property_info_ptr->ce == ce) {
4430 				zend_string_release(property_info_ptr->doc_comment);
4431 			}
4432 			zend_hash_del(&ce->properties_info, name);
4433 		} else {
4434 			property_info->offset = ce->default_static_members_count++;
4435 			ce->default_static_members_table = perealloc(ce->default_static_members_table, sizeof(zval) * ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS);
4436 		}
4437 		ZVAL_COPY_VALUE(&ce->default_static_members_table[property_info->offset], property);
4438 		if (!ZEND_MAP_PTR(ce->static_members_table)) {
4439 			if (ce->type == ZEND_INTERNAL_CLASS &&
4440 					ce->info.internal.module->type == MODULE_PERSISTENT) {
4441 				ZEND_MAP_PTR_NEW(ce->static_members_table);
4442 			}
4443 		}
4444 	} else {
4445 		zval *property_default_ptr;
4446 		if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
4447 			ZEND_ASSERT(!(property_info_ptr->flags & ZEND_ACC_STATIC));
4448 			property_info->offset = property_info_ptr->offset;
4449 			zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]);
4450 			if (property_info_ptr->doc_comment && property_info_ptr->ce == ce) {
4451 				zend_string_release_ex(property_info_ptr->doc_comment, 1);
4452 			}
4453 			zend_hash_del(&ce->properties_info, name);
4454 
4455 			ZEND_ASSERT(ce->type == ZEND_INTERNAL_CLASS);
4456 			ZEND_ASSERT(ce->properties_info_table != NULL);
4457 			ce->properties_info_table[OBJ_PROP_TO_NUM(property_info->offset)] = property_info;
4458 		} else {
4459 			property_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count);
4460 			ce->default_properties_count++;
4461 			ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
4462 
4463 			/* For user classes this is handled during linking */
4464 			if (ce->type == ZEND_INTERNAL_CLASS) {
4465 				ce->properties_info_table = perealloc(ce->properties_info_table, sizeof(zend_property_info *) * ce->default_properties_count, 1);
4466 				ce->properties_info_table[ce->default_properties_count - 1] = property_info;
4467 			}
4468 		}
4469 		property_default_ptr = &ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
4470 		ZVAL_COPY_VALUE(property_default_ptr, property);
4471 		Z_PROP_FLAG_P(property_default_ptr) = Z_ISUNDEF_P(property) ? IS_PROP_UNINIT : 0;
4472 	}
4473 	if (ce->type & ZEND_INTERNAL_CLASS) {
4474 		/* Must be interned to avoid ZTS data races */
4475 		if (is_persistent_class(ce)) {
4476 			name = zend_new_interned_string(zend_string_copy(name));
4477 		}
4478 
4479 		if (Z_REFCOUNTED_P(property)) {
4480 			zend_error_noreturn(E_CORE_ERROR, "Internal zvals cannot be refcounted");
4481 		}
4482 	}
4483 
4484 	if (access_type & ZEND_ACC_PUBLIC) {
4485 		property_info->name = zend_string_copy(name);
4486 	} else if (access_type & ZEND_ACC_PRIVATE) {
4487 		property_info->name = zend_mangle_property_name(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
4488 	} else {
4489 		ZEND_ASSERT(access_type & ZEND_ACC_PROTECTED);
4490 		property_info->name = zend_mangle_property_name("*", 1, ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
4491 	}
4492 
4493 	property_info->name = zend_new_interned_string(property_info->name);
4494 	property_info->flags = access_type;
4495 	property_info->doc_comment = doc_comment;
4496 	property_info->attributes = NULL;
4497 	property_info->ce = ce;
4498 	property_info->type = type;
4499 
4500 	if (is_persistent_class(ce)) {
4501 		zend_normalize_internal_type(&property_info->type);
4502 	}
4503 
4504 	zend_hash_update_ptr(&ce->properties_info, name, property_info);
4505 
4506 	return property_info;
4507 }
4508 /* }}} */
4509 
zend_try_assign_typed_ref_ex(zend_reference * ref,zval * val,bool strict)4510 ZEND_API zend_result zend_try_assign_typed_ref_ex(zend_reference *ref, zval *val, bool strict) /* {{{ */
4511 {
4512 	if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, val, strict))) {
4513 		zval_ptr_dtor(val);
4514 		return FAILURE;
4515 	} else {
4516 		zval_ptr_dtor(&ref->val);
4517 		ZVAL_COPY_VALUE(&ref->val, val);
4518 		return SUCCESS;
4519 	}
4520 }
4521 /* }}} */
4522 
zend_try_assign_typed_ref(zend_reference * ref,zval * val)4523 ZEND_API zend_result zend_try_assign_typed_ref(zend_reference *ref, zval *val) /* {{{ */
4524 {
4525 	return zend_try_assign_typed_ref_ex(ref, val, ZEND_ARG_USES_STRICT_TYPES());
4526 }
4527 /* }}} */
4528 
zend_try_assign_typed_ref_null(zend_reference * ref)4529 ZEND_API zend_result zend_try_assign_typed_ref_null(zend_reference *ref) /* {{{ */
4530 {
4531 	zval tmp;
4532 
4533 	ZVAL_NULL(&tmp);
4534 	return zend_try_assign_typed_ref(ref, &tmp);
4535 }
4536 /* }}} */
4537 
zend_try_assign_typed_ref_bool(zend_reference * ref,bool val)4538 ZEND_API zend_result zend_try_assign_typed_ref_bool(zend_reference *ref, bool val) /* {{{ */
4539 {
4540 	zval tmp;
4541 
4542 	ZVAL_BOOL(&tmp, val);
4543 	return zend_try_assign_typed_ref(ref, &tmp);
4544 }
4545 /* }}} */
4546 
zend_try_assign_typed_ref_long(zend_reference * ref,zend_long lval)4547 ZEND_API zend_result zend_try_assign_typed_ref_long(zend_reference *ref, zend_long lval) /* {{{ */
4548 {
4549 	zval tmp;
4550 
4551 	ZVAL_LONG(&tmp, lval);
4552 	return zend_try_assign_typed_ref(ref, &tmp);
4553 }
4554 /* }}} */
4555 
zend_try_assign_typed_ref_double(zend_reference * ref,double dval)4556 ZEND_API zend_result zend_try_assign_typed_ref_double(zend_reference *ref, double dval) /* {{{ */
4557 {
4558 	zval tmp;
4559 
4560 	ZVAL_DOUBLE(&tmp, dval);
4561 	return zend_try_assign_typed_ref(ref, &tmp);
4562 }
4563 /* }}} */
4564 
zend_try_assign_typed_ref_empty_string(zend_reference * ref)4565 ZEND_API zend_result zend_try_assign_typed_ref_empty_string(zend_reference *ref) /* {{{ */
4566 {
4567 	zval tmp;
4568 
4569 	ZVAL_EMPTY_STRING(&tmp);
4570 	return zend_try_assign_typed_ref(ref, &tmp);
4571 }
4572 /* }}} */
4573 
zend_try_assign_typed_ref_str(zend_reference * ref,zend_string * str)4574 ZEND_API zend_result zend_try_assign_typed_ref_str(zend_reference *ref, zend_string *str) /* {{{ */
4575 {
4576 	zval tmp;
4577 
4578 	ZVAL_STR(&tmp, str);
4579 	return zend_try_assign_typed_ref(ref, &tmp);
4580 }
4581 /* }}} */
4582 
zend_try_assign_typed_ref_string(zend_reference * ref,const char * string)4583 ZEND_API zend_result zend_try_assign_typed_ref_string(zend_reference *ref, const char *string) /* {{{ */
4584 {
4585 	zval tmp;
4586 
4587 	ZVAL_STRING(&tmp, string);
4588 	return zend_try_assign_typed_ref(ref, &tmp);
4589 }
4590 /* }}} */
4591 
zend_try_assign_typed_ref_stringl(zend_reference * ref,const char * string,size_t len)4592 ZEND_API zend_result zend_try_assign_typed_ref_stringl(zend_reference *ref, const char *string, size_t len) /* {{{ */
4593 {
4594 	zval tmp;
4595 
4596 	ZVAL_STRINGL(&tmp, string, len);
4597 	return zend_try_assign_typed_ref(ref, &tmp);
4598 }
4599 /* }}} */
4600 
zend_try_assign_typed_ref_arr(zend_reference * ref,zend_array * arr)4601 ZEND_API zend_result zend_try_assign_typed_ref_arr(zend_reference *ref, zend_array *arr) /* {{{ */
4602 {
4603 	zval tmp;
4604 
4605 	ZVAL_ARR(&tmp, arr);
4606 	return zend_try_assign_typed_ref(ref, &tmp);
4607 }
4608 /* }}} */
4609 
zend_try_assign_typed_ref_res(zend_reference * ref,zend_resource * res)4610 ZEND_API zend_result zend_try_assign_typed_ref_res(zend_reference *ref, zend_resource *res) /* {{{ */
4611 {
4612 	zval tmp;
4613 
4614 	ZVAL_RES(&tmp, res);
4615 	return zend_try_assign_typed_ref(ref, &tmp);
4616 }
4617 /* }}} */
4618 
zend_try_assign_typed_ref_zval(zend_reference * ref,zval * zv)4619 ZEND_API zend_result zend_try_assign_typed_ref_zval(zend_reference *ref, zval *zv) /* {{{ */
4620 {
4621 	zval tmp;
4622 
4623 	ZVAL_COPY_VALUE(&tmp, zv);
4624 	return zend_try_assign_typed_ref(ref, &tmp);
4625 }
4626 /* }}} */
4627 
zend_try_assign_typed_ref_zval_ex(zend_reference * ref,zval * zv,bool strict)4628 ZEND_API zend_result zend_try_assign_typed_ref_zval_ex(zend_reference *ref, zval *zv, bool strict) /* {{{ */
4629 {
4630 	zval tmp;
4631 
4632 	ZVAL_COPY_VALUE(&tmp, zv);
4633 	return zend_try_assign_typed_ref_ex(ref, &tmp, strict);
4634 }
4635 /* }}} */
4636 
zend_declare_property_ex(zend_class_entry * ce,zend_string * name,zval * property,int access_type,zend_string * doc_comment)4637 ZEND_API void zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */
4638 {
4639 	zend_declare_typed_property(ce, name, property, access_type, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
4640 }
4641 /* }}} */
4642 
zend_declare_property(zend_class_entry * ce,const char * name,size_t name_length,zval * property,int access_type)4643 ZEND_API void zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type) /* {{{ */
4644 {
4645 	zend_string *key = zend_string_init(name, name_length, is_persistent_class(ce));
4646 	zend_declare_property_ex(ce, key, property, access_type, NULL);
4647 	zend_string_release(key);
4648 }
4649 /* }}} */
4650 
zend_declare_property_null(zend_class_entry * ce,const char * name,size_t name_length,int access_type)4651 ZEND_API void zend_declare_property_null(zend_class_entry *ce, const char *name, size_t name_length, int access_type) /* {{{ */
4652 {
4653 	zval property;
4654 
4655 	ZVAL_NULL(&property);
4656 	zend_declare_property(ce, name, name_length, &property, access_type);
4657 }
4658 /* }}} */
4659 
zend_declare_property_bool(zend_class_entry * ce,const char * name,size_t name_length,zend_long value,int access_type)4660 ZEND_API void zend_declare_property_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
4661 {
4662 	zval property;
4663 
4664 	ZVAL_BOOL(&property, value);
4665 	zend_declare_property(ce, name, name_length, &property, access_type);
4666 }
4667 /* }}} */
4668 
zend_declare_property_long(zend_class_entry * ce,const char * name,size_t name_length,zend_long value,int access_type)4669 ZEND_API void zend_declare_property_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
4670 {
4671 	zval property;
4672 
4673 	ZVAL_LONG(&property, value);
4674 	zend_declare_property(ce, name, name_length, &property, access_type);
4675 }
4676 /* }}} */
4677 
zend_declare_property_double(zend_class_entry * ce,const char * name,size_t name_length,double value,int access_type)4678 ZEND_API void zend_declare_property_double(zend_class_entry *ce, const char *name, size_t name_length, double value, int access_type) /* {{{ */
4679 {
4680 	zval property;
4681 
4682 	ZVAL_DOUBLE(&property, value);
4683 	zend_declare_property(ce, name, name_length, &property, access_type);
4684 }
4685 /* }}} */
4686 
zend_declare_property_string(zend_class_entry * ce,const char * name,size_t name_length,const char * value,int access_type)4687 ZEND_API void zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type) /* {{{ */
4688 {
4689 	zval property;
4690 
4691 	ZVAL_NEW_STR(&property, zend_string_init(value, strlen(value), ce->type & ZEND_INTERNAL_CLASS));
4692 	zend_declare_property(ce, name, name_length, &property, access_type);
4693 }
4694 /* }}} */
4695 
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)4696 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) /* {{{ */
4697 {
4698 	zval property;
4699 
4700 	ZVAL_NEW_STR(&property, zend_string_init(value, value_len, ce->type & ZEND_INTERNAL_CLASS));
4701 	zend_declare_property(ce, name, name_length, &property, access_type);
4702 }
4703 /* }}} */
4704 
zend_declare_typed_class_constant(zend_class_entry * ce,zend_string * name,zval * value,int flags,zend_string * doc_comment,zend_type type)4705 ZEND_API zend_class_constant *zend_declare_typed_class_constant(zend_class_entry *ce, zend_string *name, zval *value, int flags, zend_string *doc_comment, zend_type type) /* {{{ */
4706 {
4707 	zend_class_constant *c;
4708 
4709 	if (ce->ce_flags & ZEND_ACC_INTERFACE) {
4710 		if (!(flags & ZEND_ACC_PUBLIC)) {
4711 			zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface constant %s::%s must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4712 		}
4713 	}
4714 
4715 	if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_CLASS))) {
4716 		zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4717 				"A class constant must not be called 'class'; it is reserved for class name fetching");
4718 	}
4719 
4720 	if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) {
4721 		zval_make_interned_string(value);
4722 	}
4723 
4724 	if (ce->type == ZEND_INTERNAL_CLASS) {
4725 		c = pemalloc(sizeof(zend_class_constant), 1);
4726 		if (ZEND_TYPE_PURE_MASK(type) != MAY_BE_ANY) {
4727 			ZEND_ASSERT(!ZEND_TYPE_CONTAINS_CODE(type, IS_RESOURCE) && "resource is not allowed in a zend_type");
4728 		}
4729 	} else {
4730 		c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
4731 	}
4732 	ZVAL_COPY_VALUE(&c->value, value);
4733 	ZEND_CLASS_CONST_FLAGS(c) = flags;
4734 	c->doc_comment = doc_comment;
4735 	c->attributes = NULL;
4736 	c->ce = ce;
4737 	c->type = type;
4738 
4739 	if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
4740 		ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
4741 		ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
4742 		if (ce->type == ZEND_INTERNAL_CLASS && !ZEND_MAP_PTR(ce->mutable_data)) {
4743 			ZEND_MAP_PTR_NEW(ce->mutable_data);
4744 		}
4745 	}
4746 
4747 	if (!zend_hash_add_ptr(&ce->constants_table, name, c)) {
4748 		zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4749 			"Cannot redefine class constant %s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4750 	}
4751 
4752 	return c;
4753 }
4754 
zend_declare_class_constant_ex(zend_class_entry * ce,zend_string * name,zval * value,int flags,zend_string * doc_comment)4755 ZEND_API zend_class_constant *zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int flags, zend_string *doc_comment)
4756 {
4757 	return zend_declare_typed_class_constant(ce, name, value, flags, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
4758 }
4759 
zend_declare_class_constant(zend_class_entry * ce,const char * name,size_t name_length,zval * value)4760 ZEND_API void zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
4761 {
4762 	zend_string *key;
4763 
4764 	if (ce->type == ZEND_INTERNAL_CLASS) {
4765 		key = zend_string_init_interned(name, name_length, 1);
4766 	} else {
4767 		key = zend_string_init(name, name_length, 0);
4768 	}
4769 	zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
4770 	zend_string_release(key);
4771 }
4772 /* }}} */
4773 
zend_declare_class_constant_null(zend_class_entry * ce,const char * name,size_t name_length)4774 ZEND_API void zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length) /* {{{ */
4775 {
4776 	zval constant;
4777 
4778 	ZVAL_NULL(&constant);
4779 	zend_declare_class_constant(ce, name, name_length, &constant);
4780 }
4781 /* }}} */
4782 
zend_declare_class_constant_long(zend_class_entry * ce,const char * name,size_t name_length,zend_long value)4783 ZEND_API void zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value) /* {{{ */
4784 {
4785 	zval constant;
4786 
4787 	ZVAL_LONG(&constant, value);
4788 	zend_declare_class_constant(ce, name, name_length, &constant);
4789 }
4790 /* }}} */
4791 
zend_declare_class_constant_bool(zend_class_entry * ce,const char * name,size_t name_length,bool value)4792 ZEND_API void zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, bool value) /* {{{ */
4793 {
4794 	zval constant;
4795 
4796 	ZVAL_BOOL(&constant, value);
4797 	zend_declare_class_constant(ce, name, name_length, &constant);
4798 }
4799 /* }}} */
4800 
zend_declare_class_constant_double(zend_class_entry * ce,const char * name,size_t name_length,double value)4801 ZEND_API void zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value) /* {{{ */
4802 {
4803 	zval constant;
4804 
4805 	ZVAL_DOUBLE(&constant, value);
4806 	zend_declare_class_constant(ce, name, name_length, &constant);
4807 }
4808 /* }}} */
4809 
zend_declare_class_constant_stringl(zend_class_entry * ce,const char * name,size_t name_length,const char * value,size_t value_length)4810 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) /* {{{ */
4811 {
4812 	zval constant;
4813 
4814 	ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type & ZEND_INTERNAL_CLASS));
4815 	zend_declare_class_constant(ce, name, name_length, &constant);
4816 }
4817 /* }}} */
4818 
zend_declare_class_constant_string(zend_class_entry * ce,const char * name,size_t name_length,const char * value)4819 ZEND_API void zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value) /* {{{ */
4820 {
4821 	zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value));
4822 }
4823 /* }}} */
4824 
zend_update_property_ex(zend_class_entry * scope,zend_object * object,zend_string * name,zval * value)4825 ZEND_API void zend_update_property_ex(zend_class_entry *scope, zend_object *object, zend_string *name, zval *value) /* {{{ */
4826 {
4827 	zend_class_entry *old_scope = EG(fake_scope);
4828 
4829 	EG(fake_scope) = scope;
4830 
4831 	object->handlers->write_property(object, name, value, NULL);
4832 
4833 	EG(fake_scope) = old_scope;
4834 }
4835 /* }}} */
4836 
zend_update_property(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,zval * value)4837 ZEND_API void zend_update_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zval *value) /* {{{ */
4838 {
4839 	zend_string *property;
4840 	zend_class_entry *old_scope = EG(fake_scope);
4841 
4842 	EG(fake_scope) = scope;
4843 
4844 	property = zend_string_init(name, name_length, 0);
4845 	object->handlers->write_property(object, property, value, NULL);
4846 	zend_string_release_ex(property, 0);
4847 
4848 	EG(fake_scope) = old_scope;
4849 }
4850 /* }}} */
4851 
zend_update_property_null(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length)4852 ZEND_API void zend_update_property_null(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */
4853 {
4854 	zval tmp;
4855 
4856 	ZVAL_NULL(&tmp);
4857 	zend_update_property(scope, object, name, name_length, &tmp);
4858 }
4859 /* }}} */
4860 
zend_unset_property(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length)4861 ZEND_API void zend_unset_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */
4862 {
4863 	zend_string *property;
4864 	zend_class_entry *old_scope = EG(fake_scope);
4865 
4866 	EG(fake_scope) = scope;
4867 
4868 	property = zend_string_init(name, name_length, 0);
4869 	object->handlers->unset_property(object, property, 0);
4870 	zend_string_release_ex(property, 0);
4871 
4872 	EG(fake_scope) = old_scope;
4873 }
4874 /* }}} */
4875 
zend_update_property_bool(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,zend_long value)4876 ZEND_API void zend_update_property_bool(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value) /* {{{ */
4877 {
4878 	zval tmp;
4879 
4880 	ZVAL_BOOL(&tmp, value);
4881 	zend_update_property(scope, object, name, name_length, &tmp);
4882 }
4883 /* }}} */
4884 
zend_update_property_long(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,zend_long value)4885 ZEND_API void zend_update_property_long(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value) /* {{{ */
4886 {
4887 	zval tmp;
4888 
4889 	ZVAL_LONG(&tmp, value);
4890 	zend_update_property(scope, object, name, name_length, &tmp);
4891 }
4892 /* }}} */
4893 
zend_update_property_double(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,double value)4894 ZEND_API void zend_update_property_double(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, double value) /* {{{ */
4895 {
4896 	zval tmp;
4897 
4898 	ZVAL_DOUBLE(&tmp, value);
4899 	zend_update_property(scope, object, name, name_length, &tmp);
4900 }
4901 /* }}} */
4902 
zend_update_property_str(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,zend_string * value)4903 ZEND_API void zend_update_property_str(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_string *value) /* {{{ */
4904 {
4905 	zval tmp;
4906 
4907 	ZVAL_STR(&tmp, value);
4908 	zend_update_property(scope, object, name, name_length, &tmp);
4909 }
4910 /* }}} */
4911 
zend_update_property_string(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,const char * value)4912 ZEND_API void zend_update_property_string(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, const char *value) /* {{{ */
4913 {
4914 	zval tmp;
4915 
4916 	ZVAL_STRING(&tmp, value);
4917 	Z_SET_REFCOUNT(tmp, 0);
4918 	zend_update_property(scope, object, name, name_length, &tmp);
4919 }
4920 /* }}} */
4921 
zend_update_property_stringl(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,const char * value,size_t value_len)4922 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_len) /* {{{ */
4923 {
4924 	zval tmp;
4925 
4926 	ZVAL_STRINGL(&tmp, value, value_len);
4927 	Z_SET_REFCOUNT(tmp, 0);
4928 	zend_update_property(scope, object, name, name_length, &tmp);
4929 }
4930 /* }}} */
4931 
zend_update_static_property_ex(zend_class_entry * scope,zend_string * name,zval * value)4932 ZEND_API zend_result zend_update_static_property_ex(zend_class_entry *scope, zend_string *name, zval *value) /* {{{ */
4933 {
4934 	zval *property, tmp;
4935 	zend_property_info *prop_info;
4936 	zend_class_entry *old_scope = EG(fake_scope);
4937 
4938 	if (UNEXPECTED(!(scope->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
4939 		if (UNEXPECTED(zend_update_class_constants(scope) != SUCCESS)) {
4940 			return FAILURE;
4941 		}
4942 	}
4943 
4944 	EG(fake_scope) = scope;
4945 	property = zend_std_get_static_property_with_info(scope, name, BP_VAR_W, &prop_info);
4946 	EG(fake_scope) = old_scope;
4947 
4948 	if (!property) {
4949 		return FAILURE;
4950 	}
4951 
4952 	ZEND_ASSERT(!Z_ISREF_P(value));
4953 	Z_TRY_ADDREF_P(value);
4954 	if (ZEND_TYPE_IS_SET(prop_info->type)) {
4955 		ZVAL_COPY_VALUE(&tmp, value);
4956 		if (!zend_verify_property_type(prop_info, &tmp, /* strict */ 0)) {
4957 			Z_TRY_DELREF_P(value);
4958 			return FAILURE;
4959 		}
4960 		value = &tmp;
4961 	}
4962 
4963 	zend_assign_to_variable(property, value, IS_TMP_VAR, /* strict */ 0);
4964 	return SUCCESS;
4965 }
4966 /* }}} */
4967 
zend_update_static_property(zend_class_entry * scope,const char * name,size_t name_length,zval * value)4968 ZEND_API zend_result zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value) /* {{{ */
4969 {
4970 	zend_string *key = zend_string_init(name, name_length, 0);
4971 	zend_result retval = zend_update_static_property_ex(scope, key, value);
4972 	zend_string_efree(key);
4973 	return retval;
4974 }
4975 /* }}} */
4976 
zend_update_static_property_null(zend_class_entry * scope,const char * name,size_t name_length)4977 ZEND_API zend_result zend_update_static_property_null(zend_class_entry *scope, const char *name, size_t name_length) /* {{{ */
4978 {
4979 	zval tmp;
4980 
4981 	ZVAL_NULL(&tmp);
4982 	return zend_update_static_property(scope, name, name_length, &tmp);
4983 }
4984 /* }}} */
4985 
zend_update_static_property_bool(zend_class_entry * scope,const char * name,size_t name_length,zend_long value)4986 ZEND_API zend_result zend_update_static_property_bool(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
4987 {
4988 	zval tmp;
4989 
4990 	ZVAL_BOOL(&tmp, value);
4991 	return zend_update_static_property(scope, name, name_length, &tmp);
4992 }
4993 /* }}} */
4994 
zend_update_static_property_long(zend_class_entry * scope,const char * name,size_t name_length,zend_long value)4995 ZEND_API zend_result zend_update_static_property_long(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
4996 {
4997 	zval tmp;
4998 
4999 	ZVAL_LONG(&tmp, value);
5000 	return zend_update_static_property(scope, name, name_length, &tmp);
5001 }
5002 /* }}} */
5003 
zend_update_static_property_double(zend_class_entry * scope,const char * name,size_t name_length,double value)5004 ZEND_API zend_result zend_update_static_property_double(zend_class_entry *scope, const char *name, size_t name_length, double value) /* {{{ */
5005 {
5006 	zval tmp;
5007 
5008 	ZVAL_DOUBLE(&tmp, value);
5009 	return zend_update_static_property(scope, name, name_length, &tmp);
5010 }
5011 /* }}} */
5012 
zend_update_static_property_string(zend_class_entry * scope,const char * name,size_t name_length,const char * value)5013 ZEND_API zend_result zend_update_static_property_string(zend_class_entry *scope, const char *name, size_t name_length, const char *value) /* {{{ */
5014 {
5015 	zval tmp;
5016 
5017 	ZVAL_STRING(&tmp, value);
5018 	Z_SET_REFCOUNT(tmp, 0);
5019 	return zend_update_static_property(scope, name, name_length, &tmp);
5020 }
5021 /* }}} */
5022 
zend_update_static_property_stringl(zend_class_entry * scope,const char * name,size_t name_length,const char * value,size_t value_len)5023 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_len) /* {{{ */
5024 {
5025 	zval tmp;
5026 
5027 	ZVAL_STRINGL(&tmp, value, value_len);
5028 	Z_SET_REFCOUNT(tmp, 0);
5029 	return zend_update_static_property(scope, name, name_length, &tmp);
5030 }
5031 /* }}} */
5032 
zend_read_property_ex(zend_class_entry * scope,zend_object * object,zend_string * name,bool silent,zval * rv)5033 ZEND_API zval *zend_read_property_ex(zend_class_entry *scope, zend_object *object, zend_string *name, bool silent, zval *rv) /* {{{ */
5034 {
5035 	zval *value;
5036 	zend_class_entry *old_scope = EG(fake_scope);
5037 
5038 	EG(fake_scope) = scope;
5039 
5040 	value = object->handlers->read_property(object, name, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
5041 
5042 	EG(fake_scope) = old_scope;
5043 	return value;
5044 }
5045 /* }}} */
5046 
zend_read_property(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,bool silent,zval * rv)5047 ZEND_API zval *zend_read_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, bool silent, zval *rv) /* {{{ */
5048 {
5049 	zval *value;
5050 	zend_string *str;
5051 
5052 	str = zend_string_init(name, name_length, 0);
5053 	value = zend_read_property_ex(scope, object, str, silent, rv);
5054 	zend_string_release_ex(str, 0);
5055 	return value;
5056 }
5057 /* }}} */
5058 
zend_read_static_property_ex(zend_class_entry * scope,zend_string * name,bool silent)5059 ZEND_API zval *zend_read_static_property_ex(zend_class_entry *scope, zend_string *name, bool silent) /* {{{ */
5060 {
5061 	zval *property;
5062 	zend_class_entry *old_scope = EG(fake_scope);
5063 
5064 	EG(fake_scope) = scope;
5065 	property = zend_std_get_static_property(scope, name, silent ? BP_VAR_IS : BP_VAR_R);
5066 	EG(fake_scope) = old_scope;
5067 
5068 	return property;
5069 }
5070 /* }}} */
5071 
zend_read_static_property(zend_class_entry * scope,const char * name,size_t name_length,bool silent)5072 ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, size_t name_length, bool silent) /* {{{ */
5073 {
5074 	zend_string *key = zend_string_init(name, name_length, 0);
5075 	zval *property = zend_read_static_property_ex(scope, key, silent);
5076 	zend_string_efree(key);
5077 	return property;
5078 }
5079 /* }}} */
5080 
zend_save_error_handling(zend_error_handling * current)5081 ZEND_API void zend_save_error_handling(zend_error_handling *current) /* {{{ */
5082 {
5083 	current->handling = EG(error_handling);
5084 	current->exception = EG(exception_class);
5085 }
5086 /* }}} */
5087 
zend_replace_error_handling(zend_error_handling_t error_handling,zend_class_entry * exception_class,zend_error_handling * current)5088 ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current) /* {{{ */
5089 {
5090 	if (current) {
5091 		zend_save_error_handling(current);
5092 	}
5093 	ZEND_ASSERT(error_handling == EH_THROW || exception_class == NULL);
5094 	EG(error_handling) = error_handling;
5095 	EG(exception_class) = exception_class;
5096 }
5097 /* }}} */
5098 
zend_restore_error_handling(zend_error_handling * saved)5099 ZEND_API void zend_restore_error_handling(zend_error_handling *saved) /* {{{ */
5100 {
5101 	EG(error_handling) = saved->handling;
5102 	EG(exception_class) = saved->exception;
5103 }
5104 /* }}} */
5105 
zend_get_object_type_case(const zend_class_entry * ce,bool upper_case)5106 ZEND_API ZEND_COLD const char *zend_get_object_type_case(const zend_class_entry *ce, bool upper_case) /* {{{ */
5107 {
5108 	if (ce->ce_flags & ZEND_ACC_TRAIT) {
5109 		return upper_case ? "Trait" : "trait";
5110 	} else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
5111 		return upper_case ? "Interface" : "interface";
5112 	} else if (ce->ce_flags & ZEND_ACC_ENUM) {
5113 		return upper_case ? "Enum" : "enum";
5114 	} else {
5115 		return upper_case ? "Class" : "class";
5116 	}
5117 }
5118 /* }}} */
5119 
zend_is_iterable(const zval * iterable)5120 ZEND_API bool zend_is_iterable(const zval *iterable) /* {{{ */
5121 {
5122 	switch (Z_TYPE_P(iterable)) {
5123 		case IS_ARRAY:
5124 			return 1;
5125 		case IS_OBJECT:
5126 			return zend_class_implements_interface(Z_OBJCE_P(iterable), zend_ce_traversable);
5127 		default:
5128 			return 0;
5129 	}
5130 }
5131 /* }}} */
5132 
zend_is_countable(const zval * countable)5133 ZEND_API bool zend_is_countable(const zval *countable) /* {{{ */
5134 {
5135 	switch (Z_TYPE_P(countable)) {
5136 		case IS_ARRAY:
5137 			return 1;
5138 		case IS_OBJECT:
5139 			if (Z_OBJ_HT_P(countable)->count_elements) {
5140 				return 1;
5141 			}
5142 
5143 			return zend_class_implements_interface(Z_OBJCE_P(countable), zend_ce_countable);
5144 		default:
5145 			return 0;
5146 	}
5147 }
5148 /* }}} */
5149 
get_default_via_ast(zval * default_value_zval,const char * default_value)5150 static zend_result get_default_via_ast(zval *default_value_zval, const char *default_value) {
5151 	zend_ast *ast;
5152 	zend_arena *ast_arena;
5153 
5154 	zend_string *code = zend_string_concat3(
5155 		"<?php ", sizeof("<?php ") - 1, default_value, strlen(default_value), ";", 1);
5156 
5157 	ast = zend_compile_string_to_ast(code, &ast_arena, ZSTR_EMPTY_ALLOC());
5158 	zend_string_release(code);
5159 
5160 	if (!ast) {
5161 		return FAILURE;
5162 	}
5163 
5164 	zend_ast_list *statement_list = zend_ast_get_list(ast);
5165 	zend_ast **const_expr_ast_ptr = &statement_list->child[0];
5166 
5167 	zend_arena *original_ast_arena = CG(ast_arena);
5168 	uint32_t original_compiler_options = CG(compiler_options);
5169 	zend_file_context original_file_context;
5170 	CG(ast_arena) = ast_arena;
5171 	/* Disable constant substitution, to make getDefaultValueConstant() work. */
5172 	CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
5173 	zend_file_context_begin(&original_file_context);
5174 	zend_const_expr_to_zval(default_value_zval, const_expr_ast_ptr, /* allow_dynamic */ true);
5175 	CG(ast_arena) = original_ast_arena;
5176 	CG(compiler_options) = original_compiler_options;
5177 	zend_file_context_end(&original_file_context);
5178 
5179 	zend_ast_destroy(ast);
5180 	zend_arena_destroy(ast_arena);
5181 
5182 	return SUCCESS;
5183 }
5184 
try_parse_string(const char * str,size_t len,char quote)5185 static zend_string *try_parse_string(const char *str, size_t len, char quote) {
5186 	if (len == 0) {
5187 		return ZSTR_EMPTY_ALLOC();
5188 	}
5189 
5190 	for (size_t i = 0; i < len; i++) {
5191 		if (str[i] == '\\' || str[i] == quote) {
5192 			return NULL;
5193 		}
5194 	}
5195 	return zend_string_init(str, len, 0);
5196 }
5197 
zend_get_default_from_internal_arg_info(zval * default_value_zval,zend_internal_arg_info * arg_info)5198 ZEND_API zend_result zend_get_default_from_internal_arg_info(
5199 		zval *default_value_zval, zend_internal_arg_info *arg_info)
5200 {
5201 	const char *default_value = arg_info->default_value;
5202 	if (!default_value) {
5203 		return FAILURE;
5204 	}
5205 
5206 	/* Avoid going through the full AST machinery for some simple and common cases. */
5207 	size_t default_value_len = strlen(default_value);
5208 	zend_ulong lval;
5209 	if (default_value_len == sizeof("null")-1
5210 			&& !memcmp(default_value, "null", sizeof("null")-1)) {
5211 		ZVAL_NULL(default_value_zval);
5212 		return SUCCESS;
5213 	} else if (default_value_len == sizeof("true")-1
5214 			&& !memcmp(default_value, "true", sizeof("true")-1)) {
5215 		ZVAL_TRUE(default_value_zval);
5216 		return SUCCESS;
5217 	} else if (default_value_len == sizeof("false")-1
5218 			&& !memcmp(default_value, "false", sizeof("false")-1)) {
5219 		ZVAL_FALSE(default_value_zval);
5220 		return SUCCESS;
5221 	} else if (default_value_len >= 2
5222 			&& (default_value[0] == '\'' || default_value[0] == '"')
5223 			&& default_value[default_value_len - 1] == default_value[0]) {
5224 		zend_string *str = try_parse_string(
5225 			default_value + 1, default_value_len - 2, default_value[0]);
5226 		if (str) {
5227 			ZVAL_STR(default_value_zval, str);
5228 			return SUCCESS;
5229 		}
5230 	} else if (default_value_len == sizeof("[]")-1
5231 			&& !memcmp(default_value, "[]", sizeof("[]")-1)) {
5232 		ZVAL_EMPTY_ARRAY(default_value_zval);
5233 		return SUCCESS;
5234 	} else if (ZEND_HANDLE_NUMERIC_STR(default_value, default_value_len, lval)) {
5235 		ZVAL_LONG(default_value_zval, lval);
5236 		return SUCCESS;
5237 	}
5238 
5239 #if 0
5240 	fprintf(stderr, "Evaluating %s via AST\n", default_value);
5241 #endif
5242 	return get_default_via_ast(default_value_zval, default_value);
5243 }
5244