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