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 zend_string_release(arg_name);
1883 /* Do not call destructor, free object, and set arg to IS_UNDEF */
1884 zend_object_store_ctor_failed(obj);
1885 zval_ptr_dtor(arg);
1886 ZVAL_UNDEF(arg);
1887 return FAILURE;
1888 } else {
1889 return SUCCESS;
1890 }
1891 }
1892 /* A constructor should not return a value, however if an exception is thrown
1893 * zend_call_known_function() will set the retval to IS_UNDEF */
1894 zval retval;
1895 zend_call_known_function(
1896 constructor,
1897 obj,
1898 class_type,
1899 &retval,
1900 param_count,
1901 params,
1902 named_params
1903 );
1904 if (Z_TYPE(retval) == IS_UNDEF) {
1905 /* Do not call destructor, free object, and set arg to IS_UNDEF */
1906 zend_object_store_ctor_failed(obj);
1907 zval_ptr_dtor(arg);
1908 ZVAL_UNDEF(arg);
1909 return FAILURE;
1910 } else {
1911 /* Unlikely, but user constructors may return any value they want */
1912 zval_ptr_dtor(&retval);
1913 return SUCCESS;
1914 }
1915 }
1916 /* }}} */
1917
object_init(zval * arg)1918 ZEND_API void object_init(zval *arg) /* {{{ */
1919 {
1920 ZVAL_OBJ(arg, zend_objects_new(zend_standard_class_def));
1921 }
1922 /* }}} */
1923
add_assoc_long_ex(zval * arg,const char * key,size_t key_len,zend_long n)1924 ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
1925 {
1926 zval tmp;
1927
1928 ZVAL_LONG(&tmp, n);
1929 zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1930 }
1931 /* }}} */
1932
add_assoc_null_ex(zval * arg,const char * key,size_t key_len)1933 ZEND_API void add_assoc_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
1934 {
1935 zval tmp;
1936
1937 ZVAL_NULL(&tmp);
1938 zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1939 }
1940 /* }}} */
1941
add_assoc_bool_ex(zval * arg,const char * key,size_t key_len,bool b)1942 ZEND_API void add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, bool b) /* {{{ */
1943 {
1944 zval tmp;
1945
1946 ZVAL_BOOL(&tmp, b);
1947 zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1948 }
1949 /* }}} */
1950
add_assoc_resource_ex(zval * arg,const char * key,size_t key_len,zend_resource * r)1951 ZEND_API void add_assoc_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
1952 {
1953 zval tmp;
1954
1955 ZVAL_RES(&tmp, r);
1956 zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1957 }
1958 /* }}} */
1959
add_assoc_double_ex(zval * arg,const char * key,size_t key_len,double d)1960 ZEND_API void add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
1961 {
1962 zval tmp;
1963
1964 ZVAL_DOUBLE(&tmp, d);
1965 zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1966 }
1967 /* }}} */
1968
add_assoc_str_ex(zval * arg,const char * key,size_t key_len,zend_string * str)1969 ZEND_API void add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
1970 {
1971 zval tmp;
1972
1973 ZVAL_STR(&tmp, str);
1974 zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1975 }
1976 /* }}} */
1977
add_assoc_string_ex(zval * arg,const char * key,size_t key_len,const char * str)1978 ZEND_API void add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
1979 {
1980 zval tmp;
1981
1982 ZVAL_STRING(&tmp, str);
1983 zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1984 }
1985 /* }}} */
1986
add_assoc_stringl_ex(zval * arg,const char * key,size_t key_len,const char * str,size_t length)1987 ZEND_API void add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
1988 {
1989 zval tmp;
1990
1991 ZVAL_STRINGL(&tmp, str, length);
1992 zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
1993 }
1994 /* }}} */
1995
add_assoc_array_ex(zval * arg,const char * key,size_t key_len,zend_array * arr)1996 ZEND_API void add_assoc_array_ex(zval *arg, const char *key, size_t key_len, zend_array *arr) /* {{{ */
1997 {
1998 zval tmp;
1999
2000 ZVAL_ARR(&tmp, arr);
2001 zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
2002 }
2003 /* }}} */
2004
add_assoc_object_ex(zval * arg,const char * key,size_t key_len,zend_object * obj)2005 ZEND_API void add_assoc_object_ex(zval *arg, const char *key, size_t key_len, zend_object *obj) /* {{{ */
2006 {
2007 zval tmp;
2008
2009 ZVAL_OBJ(&tmp, obj);
2010 zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
2011 }
2012 /* }}} */
2013
add_assoc_reference_ex(zval * arg,const char * key,size_t key_len,zend_reference * ref)2014 ZEND_API void add_assoc_reference_ex(zval *arg, const char *key, size_t key_len, zend_reference *ref) /* {{{ */
2015 {
2016 zval tmp;
2017
2018 ZVAL_REF(&tmp, ref);
2019 zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
2020 }
2021 /* }}} */
2022
add_assoc_zval_ex(zval * arg,const char * key,size_t key_len,zval * value)2023 ZEND_API void add_assoc_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
2024 {
2025 zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value);
2026 }
2027 /* }}} */
2028
add_index_long(zval * arg,zend_ulong index,zend_long n)2029 ZEND_API void add_index_long(zval *arg, zend_ulong index, zend_long n) /* {{{ */
2030 {
2031 zval tmp;
2032
2033 ZVAL_LONG(&tmp, n);
2034 zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2035 }
2036 /* }}} */
2037
add_index_null(zval * arg,zend_ulong index)2038 ZEND_API void add_index_null(zval *arg, zend_ulong index) /* {{{ */
2039 {
2040 zval tmp;
2041
2042 ZVAL_NULL(&tmp);
2043 zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2044 }
2045 /* }}} */
2046
add_index_bool(zval * arg,zend_ulong index,bool b)2047 ZEND_API void add_index_bool(zval *arg, zend_ulong index, bool b) /* {{{ */
2048 {
2049 zval tmp;
2050
2051 ZVAL_BOOL(&tmp, b);
2052 zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2053 }
2054 /* }}} */
2055
add_index_resource(zval * arg,zend_ulong index,zend_resource * r)2056 ZEND_API void add_index_resource(zval *arg, zend_ulong index, zend_resource *r) /* {{{ */
2057 {
2058 zval tmp;
2059
2060 ZVAL_RES(&tmp, r);
2061 zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2062 }
2063 /* }}} */
2064
add_index_double(zval * arg,zend_ulong index,double d)2065 ZEND_API void add_index_double(zval *arg, zend_ulong index, double d) /* {{{ */
2066 {
2067 zval tmp;
2068
2069 ZVAL_DOUBLE(&tmp, d);
2070 zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2071 }
2072 /* }}} */
2073
add_index_str(zval * arg,zend_ulong index,zend_string * str)2074 ZEND_API void add_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */
2075 {
2076 zval tmp;
2077
2078 ZVAL_STR(&tmp, str);
2079 zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2080 }
2081 /* }}} */
2082
add_index_string(zval * arg,zend_ulong index,const char * str)2083 ZEND_API void add_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */
2084 {
2085 zval tmp;
2086
2087 ZVAL_STRING(&tmp, str);
2088 zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2089 }
2090 /* }}} */
2091
add_index_stringl(zval * arg,zend_ulong index,const char * str,size_t length)2092 ZEND_API void add_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */
2093 {
2094 zval tmp;
2095
2096 ZVAL_STRINGL(&tmp, str, length);
2097 zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2098 }
2099 /* }}} */
2100
add_index_array(zval * arg,zend_ulong index,zend_array * arr)2101 ZEND_API void add_index_array(zval *arg, zend_ulong index, zend_array *arr) /* {{{ */
2102 {
2103 zval tmp;
2104
2105 ZVAL_ARR(&tmp, arr);
2106 zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2107 }
2108 /* }}} */
2109
add_index_object(zval * arg,zend_ulong index,zend_object * obj)2110 ZEND_API void add_index_object(zval *arg, zend_ulong index, zend_object *obj) /* {{{ */
2111 {
2112 zval tmp;
2113
2114 ZVAL_OBJ(&tmp, obj);
2115 zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2116 }
2117 /* }}} */
2118
add_index_reference(zval * arg,zend_ulong index,zend_reference * ref)2119 ZEND_API void add_index_reference(zval *arg, zend_ulong index, zend_reference *ref) /* {{{ */
2120 {
2121 zval tmp;
2122
2123 ZVAL_REF(&tmp, ref);
2124 zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
2125 }
2126 /* }}} */
2127
add_next_index_long(zval * arg,zend_long n)2128 ZEND_API zend_result add_next_index_long(zval *arg, zend_long n) /* {{{ */
2129 {
2130 zval tmp;
2131
2132 ZVAL_LONG(&tmp, n);
2133 return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2134 }
2135 /* }}} */
2136
add_next_index_null(zval * arg)2137 ZEND_API zend_result add_next_index_null(zval *arg) /* {{{ */
2138 {
2139 zval tmp;
2140
2141 ZVAL_NULL(&tmp);
2142 return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2143 }
2144 /* }}} */
2145
add_next_index_bool(zval * arg,bool b)2146 ZEND_API zend_result add_next_index_bool(zval *arg, bool b) /* {{{ */
2147 {
2148 zval tmp;
2149
2150 ZVAL_BOOL(&tmp, b);
2151 return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2152 }
2153 /* }}} */
2154
add_next_index_resource(zval * arg,zend_resource * r)2155 ZEND_API zend_result add_next_index_resource(zval *arg, zend_resource *r) /* {{{ */
2156 {
2157 zval tmp;
2158
2159 ZVAL_RES(&tmp, r);
2160 return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2161 }
2162 /* }}} */
2163
add_next_index_double(zval * arg,double d)2164 ZEND_API zend_result add_next_index_double(zval *arg, double d) /* {{{ */
2165 {
2166 zval tmp;
2167
2168 ZVAL_DOUBLE(&tmp, d);
2169 return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2170 }
2171 /* }}} */
2172
add_next_index_str(zval * arg,zend_string * str)2173 ZEND_API zend_result add_next_index_str(zval *arg, zend_string *str) /* {{{ */
2174 {
2175 zval tmp;
2176
2177 ZVAL_STR(&tmp, str);
2178 return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2179 }
2180 /* }}} */
2181
add_next_index_string(zval * arg,const char * str)2182 ZEND_API zend_result add_next_index_string(zval *arg, const char *str) /* {{{ */
2183 {
2184 zval tmp;
2185
2186 ZVAL_STRING(&tmp, str);
2187 return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2188 }
2189 /* }}} */
2190
add_next_index_stringl(zval * arg,const char * str,size_t length)2191 ZEND_API zend_result add_next_index_stringl(zval *arg, const char *str, size_t length) /* {{{ */
2192 {
2193 zval tmp;
2194
2195 ZVAL_STRINGL(&tmp, str, length);
2196 return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2197 }
2198 /* }}} */
2199
add_next_index_array(zval * arg,zend_array * arr)2200 ZEND_API zend_result add_next_index_array(zval *arg, zend_array *arr) /* {{{ */
2201 {
2202 zval tmp;
2203
2204 ZVAL_ARR(&tmp, arr);
2205 return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2206 }
2207 /* }}} */
2208
add_next_index_object(zval * arg,zend_object * obj)2209 ZEND_API zend_result add_next_index_object(zval *arg, zend_object *obj) /* {{{ */
2210 {
2211 zval tmp;
2212
2213 ZVAL_OBJ(&tmp, obj);
2214 return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2215 }
2216 /* }}} */
2217
add_next_index_reference(zval * arg,zend_reference * ref)2218 ZEND_API zend_result add_next_index_reference(zval *arg, zend_reference *ref) /* {{{ */
2219 {
2220 zval tmp;
2221
2222 ZVAL_REF(&tmp, ref);
2223 return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
2224 }
2225 /* }}} */
2226
array_set_zval_key(HashTable * ht,zval * key,zval * value)2227 ZEND_API zend_result array_set_zval_key(HashTable *ht, zval *key, zval *value) /* {{{ */
2228 {
2229 zval *result;
2230
2231 switch (Z_TYPE_P(key)) {
2232 case IS_STRING:
2233 result = zend_symtable_update(ht, Z_STR_P(key), value);
2234 break;
2235 case IS_NULL:
2236 result = zend_hash_update(ht, ZSTR_EMPTY_ALLOC(), value);
2237 break;
2238 case IS_RESOURCE:
2239 zend_use_resource_as_offset(key);
2240 result = zend_hash_index_update(ht, Z_RES_HANDLE_P(key), value);
2241 break;
2242 case IS_FALSE:
2243 result = zend_hash_index_update(ht, 0, value);
2244 break;
2245 case IS_TRUE:
2246 result = zend_hash_index_update(ht, 1, value);
2247 break;
2248 case IS_LONG:
2249 result = zend_hash_index_update(ht, Z_LVAL_P(key), value);
2250 break;
2251 case IS_DOUBLE:
2252 result = zend_hash_index_update(ht, zend_dval_to_lval_safe(Z_DVAL_P(key)), value);
2253 break;
2254 default:
2255 zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), key, BP_VAR_W);
2256 result = NULL;
2257 }
2258
2259 if (result) {
2260 Z_TRY_ADDREF_P(result);
2261 return SUCCESS;
2262 } else {
2263 return FAILURE;
2264 }
2265 }
2266 /* }}} */
2267
add_property_long_ex(zval * arg,const char * key,size_t key_len,zend_long n)2268 ZEND_API void add_property_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
2269 {
2270 zval tmp;
2271
2272 ZVAL_LONG(&tmp, n);
2273 add_property_zval_ex(arg, key, key_len, &tmp);
2274 }
2275 /* }}} */
2276
add_property_bool_ex(zval * arg,const char * key,size_t key_len,zend_long b)2277 ZEND_API void add_property_bool_ex(zval *arg, const char *key, size_t key_len, zend_long b) /* {{{ */
2278 {
2279 zval tmp;
2280
2281 ZVAL_BOOL(&tmp, b);
2282 add_property_zval_ex(arg, key, key_len, &tmp);
2283 }
2284 /* }}} */
2285
add_property_null_ex(zval * arg,const char * key,size_t key_len)2286 ZEND_API void add_property_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
2287 {
2288 zval tmp;
2289
2290 ZVAL_NULL(&tmp);
2291 add_property_zval_ex(arg, key, key_len, &tmp);
2292 }
2293 /* }}} */
2294
add_property_resource_ex(zval * arg,const char * key,size_t key_len,zend_resource * r)2295 ZEND_API void add_property_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
2296 {
2297 zval tmp;
2298
2299 ZVAL_RES(&tmp, r);
2300 add_property_zval_ex(arg, key, key_len, &tmp);
2301 zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2302 }
2303 /* }}} */
2304
add_property_double_ex(zval * arg,const char * key,size_t key_len,double d)2305 ZEND_API void add_property_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
2306 {
2307 zval tmp;
2308
2309 ZVAL_DOUBLE(&tmp, d);
2310 add_property_zval_ex(arg, key, key_len, &tmp);
2311 }
2312 /* }}} */
2313
add_property_str_ex(zval * arg,const char * key,size_t key_len,zend_string * str)2314 ZEND_API void add_property_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
2315 {
2316 zval tmp;
2317
2318 ZVAL_STR(&tmp, str);
2319 add_property_zval_ex(arg, key, key_len, &tmp);
2320 zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2321 }
2322 /* }}} */
2323
add_property_string_ex(zval * arg,const char * key,size_t key_len,const char * str)2324 ZEND_API void add_property_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
2325 {
2326 zval tmp;
2327
2328 ZVAL_STRING(&tmp, str);
2329 add_property_zval_ex(arg, key, key_len, &tmp);
2330 zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2331 }
2332 /* }}} */
2333
add_property_stringl_ex(zval * arg,const char * key,size_t key_len,const char * str,size_t length)2334 ZEND_API void add_property_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
2335 {
2336 zval tmp;
2337
2338 ZVAL_STRINGL(&tmp, str, length);
2339 add_property_zval_ex(arg, key, key_len, &tmp);
2340 zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2341 }
2342 /* }}} */
2343
add_property_array_ex(zval * arg,const char * key,size_t key_len,zend_array * arr)2344 ZEND_API void add_property_array_ex(zval *arg, const char *key, size_t key_len, zend_array *arr) /* {{{ */
2345 {
2346 zval tmp;
2347
2348 ZVAL_ARR(&tmp, arr);
2349 add_property_zval_ex(arg, key, key_len, &tmp);
2350 zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2351 }
2352 /* }}} */
2353
add_property_object_ex(zval * arg,const char * key,size_t key_len,zend_object * obj)2354 ZEND_API void add_property_object_ex(zval *arg, const char *key, size_t key_len, zend_object *obj) /* {{{ */
2355 {
2356 zval tmp;
2357
2358 ZVAL_OBJ(&tmp, obj);
2359 add_property_zval_ex(arg, key, key_len, &tmp);
2360 zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2361 }
2362 /* }}} */
2363
add_property_reference_ex(zval * arg,const char * key,size_t key_len,zend_reference * ref)2364 ZEND_API void add_property_reference_ex(zval *arg, const char *key, size_t key_len, zend_reference *ref) /* {{{ */
2365 {
2366 zval tmp;
2367
2368 ZVAL_REF(&tmp, ref);
2369 add_property_zval_ex(arg, key, key_len, &tmp);
2370 zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
2371 }
2372 /* }}} */
2373
add_property_zval_ex(zval * arg,const char * key,size_t key_len,zval * value)2374 ZEND_API void add_property_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
2375 {
2376 zend_string *str;
2377
2378 str = zend_string_init(key, key_len, 0);
2379 Z_OBJ_HANDLER_P(arg, write_property)(Z_OBJ_P(arg), str, value, NULL);
2380 zend_string_release_ex(str, 0);
2381 }
2382 /* }}} */
2383
zend_startup_module_ex(zend_module_entry * module)2384 ZEND_API zend_result zend_startup_module_ex(zend_module_entry *module) /* {{{ */
2385 {
2386 size_t name_len;
2387 zend_string *lcname;
2388
2389 if (module->module_started) {
2390 return SUCCESS;
2391 }
2392 module->module_started = 1;
2393
2394 /* Check module dependencies */
2395 if (module->deps) {
2396 const zend_module_dep *dep = module->deps;
2397
2398 while (dep->name) {
2399 if (dep->type == MODULE_DEP_REQUIRED) {
2400 zend_module_entry *req_mod;
2401
2402 name_len = strlen(dep->name);
2403 lcname = zend_string_alloc(name_len, 0);
2404 zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
2405
2406 if ((req_mod = zend_hash_find_ptr(&module_registry, lcname)) == NULL || !req_mod->module_started) {
2407 zend_string_efree(lcname);
2408 /* TODO: Check version relationship */
2409 zend_error(E_CORE_WARNING, "Cannot load module \"%s\" because required module \"%s\" is not loaded", module->name, dep->name);
2410 module->module_started = 0;
2411 return FAILURE;
2412 }
2413 zend_string_efree(lcname);
2414 }
2415 ++dep;
2416 }
2417 }
2418
2419 /* Initialize module globals */
2420 if (module->globals_size) {
2421 #ifdef ZTS
2422 ts_allocate_id(module->globals_id_ptr, module->globals_size, (ts_allocate_ctor) module->globals_ctor, (ts_allocate_dtor) module->globals_dtor);
2423 #else
2424 if (module->globals_ctor) {
2425 module->globals_ctor(module->globals_ptr);
2426 }
2427 #endif
2428 }
2429 if (module->module_startup_func) {
2430 EG(current_module) = module;
2431 if (module->module_startup_func(module->type, module->module_number)==FAILURE) {
2432 zend_error_noreturn(E_CORE_ERROR,"Unable to start %s module", module->name);
2433 EG(current_module) = NULL;
2434 return FAILURE;
2435 }
2436 EG(current_module) = NULL;
2437 }
2438 return SUCCESS;
2439 }
2440 /* }}} */
2441
zend_startup_module_zval(zval * zv)2442 static int zend_startup_module_zval(zval *zv) /* {{{ */
2443 {
2444 zend_module_entry *module = Z_PTR_P(zv);
2445
2446 return (zend_startup_module_ex(module) == SUCCESS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
2447 }
2448 /* }}} */
2449
zend_sort_modules(void * base,size_t count,size_t siz,compare_func_t compare,swap_func_t swp)2450 static void zend_sort_modules(void *base, size_t count, size_t siz, compare_func_t compare, swap_func_t swp) /* {{{ */
2451 {
2452 Bucket *b1 = base;
2453 Bucket *b2;
2454 Bucket *end = b1 + count;
2455 Bucket tmp;
2456 zend_module_entry *m, *r;
2457
2458 while (b1 < end) {
2459 try_again:
2460 m = (zend_module_entry*)Z_PTR(b1->val);
2461 if (!m->module_started && m->deps) {
2462 const zend_module_dep *dep = m->deps;
2463 while (dep->name) {
2464 if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) {
2465 b2 = b1 + 1;
2466 while (b2 < end) {
2467 r = (zend_module_entry*)Z_PTR(b2->val);
2468 if (strcasecmp(dep->name, r->name) == 0) {
2469 tmp = *b1;
2470 *b1 = *b2;
2471 *b2 = tmp;
2472 goto try_again;
2473 }
2474 b2++;
2475 }
2476 }
2477 dep++;
2478 }
2479 }
2480 b1++;
2481 }
2482 }
2483 /* }}} */
2484
zend_collect_module_handlers(void)2485 ZEND_API void zend_collect_module_handlers(void) /* {{{ */
2486 {
2487 zend_module_entry *module;
2488 int startup_count = 0;
2489 int shutdown_count = 0;
2490 int post_deactivate_count = 0;
2491 int dl_loaded_count = 0;
2492 zend_class_entry *ce;
2493 int class_count = 0;
2494
2495 /* Collect extensions with request startup/shutdown handlers */
2496 ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
2497 if (module->request_startup_func) {
2498 startup_count++;
2499 }
2500 if (module->request_shutdown_func) {
2501 shutdown_count++;
2502 }
2503 if (module->post_deactivate_func) {
2504 post_deactivate_count++;
2505 }
2506 if (module->handle) {
2507 dl_loaded_count++;
2508 }
2509 } ZEND_HASH_FOREACH_END();
2510 module_request_startup_handlers = (zend_module_entry**)realloc(
2511 module_request_startup_handlers,
2512 sizeof(zend_module_entry*) *
2513 (startup_count + 1 +
2514 shutdown_count + 1 +
2515 post_deactivate_count + 1));
2516 module_request_startup_handlers[startup_count] = NULL;
2517 module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
2518 module_request_shutdown_handlers[shutdown_count] = NULL;
2519 module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
2520 module_post_deactivate_handlers[post_deactivate_count] = NULL;
2521 /* Cannot reuse module_request_startup_handlers because it is freed in zend_destroy_modules, which happens before zend_unload_modules. */
2522 modules_dl_loaded = realloc(modules_dl_loaded, sizeof(zend_module_entry*) * (dl_loaded_count + 1));
2523 modules_dl_loaded[dl_loaded_count] = NULL;
2524 startup_count = 0;
2525
2526 ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
2527 if (module->request_startup_func) {
2528 module_request_startup_handlers[startup_count++] = module;
2529 }
2530 if (module->request_shutdown_func) {
2531 module_request_shutdown_handlers[--shutdown_count] = module;
2532 }
2533 if (module->post_deactivate_func) {
2534 module_post_deactivate_handlers[--post_deactivate_count] = module;
2535 }
2536 if (module->handle) {
2537 modules_dl_loaded[--dl_loaded_count] = module;
2538 }
2539 } ZEND_HASH_FOREACH_END();
2540
2541 /* Collect internal classes with static members */
2542 ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
2543 if (ce->type == ZEND_INTERNAL_CLASS &&
2544 ce->default_static_members_count > 0) {
2545 class_count++;
2546 }
2547 } ZEND_HASH_FOREACH_END();
2548
2549 class_cleanup_handlers = (zend_class_entry**)realloc(
2550 class_cleanup_handlers,
2551 sizeof(zend_class_entry*) *
2552 (class_count + 1));
2553 class_cleanup_handlers[class_count] = NULL;
2554
2555 if (class_count) {
2556 ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
2557 if (ce->type == ZEND_INTERNAL_CLASS &&
2558 ce->default_static_members_count > 0) {
2559 class_cleanup_handlers[--class_count] = ce;
2560 }
2561 } ZEND_HASH_FOREACH_END();
2562 }
2563 }
2564 /* }}} */
2565
zend_startup_modules(void)2566 ZEND_API void zend_startup_modules(void) /* {{{ */
2567 {
2568 zend_hash_sort_ex(&module_registry, zend_sort_modules, NULL, 0);
2569 zend_hash_apply(&module_registry, zend_startup_module_zval);
2570 }
2571 /* }}} */
2572
zend_destroy_modules(void)2573 ZEND_API void zend_destroy_modules(void) /* {{{ */
2574 {
2575 free(class_cleanup_handlers);
2576 class_cleanup_handlers = NULL;
2577 free(module_request_startup_handlers);
2578 module_request_startup_handlers = NULL;
2579 zend_hash_graceful_reverse_destroy(&module_registry);
2580 }
2581 /* }}} */
2582
zend_register_module_ex(zend_module_entry * module,int module_type)2583 ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module, int module_type) /* {{{ */
2584 {
2585 size_t name_len;
2586 zend_string *lcname;
2587 zend_module_entry *module_ptr;
2588
2589 if (!module) {
2590 return NULL;
2591 }
2592
2593 #if 0
2594 zend_printf("%s: Registering module %d\n", module->name, module->module_number);
2595 #endif
2596
2597 /* Check module dependencies */
2598 if (module->deps) {
2599 const zend_module_dep *dep = module->deps;
2600
2601 while (dep->name) {
2602 if (dep->type == MODULE_DEP_CONFLICTS) {
2603 name_len = strlen(dep->name);
2604 lcname = zend_string_alloc(name_len, 0);
2605 zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
2606
2607 if (zend_hash_exists(&module_registry, lcname) || zend_get_extension(dep->name)) {
2608 zend_string_efree(lcname);
2609 /* TODO: Check version relationship */
2610 zend_error(E_CORE_WARNING, "Cannot load module \"%s\" because conflicting module \"%s\" is already loaded", module->name, dep->name);
2611 return NULL;
2612 }
2613 zend_string_efree(lcname);
2614 }
2615 ++dep;
2616 }
2617 }
2618
2619 name_len = strlen(module->name);
2620 lcname = zend_string_alloc(name_len, module_type == MODULE_PERSISTENT);
2621 zend_str_tolower_copy(ZSTR_VAL(lcname), module->name, name_len);
2622
2623 int module_number = zend_next_free_module();
2624
2625 lcname = zend_new_interned_string(lcname);
2626 if ((module_ptr = zend_hash_add_ptr(&module_registry, lcname, module)) == NULL) {
2627 zend_error(E_CORE_WARNING, "Module \"%s\" is already loaded", module->name);
2628 zend_string_release(lcname);
2629 return NULL;
2630 }
2631 module = module_ptr;
2632 EG(current_module) = module;
2633
2634 module->module_number = module_number;
2635 module->type = module_type;
2636
2637 if (module->functions && zend_register_functions(NULL, module->functions, NULL, module_type)==FAILURE) {
2638 zend_hash_del(&module_registry, lcname);
2639 zend_string_release(lcname);
2640 EG(current_module) = NULL;
2641 zend_error(E_CORE_WARNING,"%s: Unable to register functions, unable to load", module->name);
2642 return NULL;
2643 }
2644
2645 EG(current_module) = NULL;
2646 zend_string_release(lcname);
2647 return module;
2648 }
2649 /* }}} */
2650
zend_register_internal_module(zend_module_entry * module)2651 ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module) /* {{{ */
2652 {
2653 return zend_register_module_ex(module, MODULE_PERSISTENT);
2654 }
2655 /* }}} */
2656
zend_check_magic_method_args(uint32_t num_args,const zend_class_entry * ce,const zend_function * fptr,int error_type)2657 static void zend_check_magic_method_args(
2658 uint32_t num_args, const zend_class_entry *ce, const zend_function *fptr, int error_type)
2659 {
2660 if (fptr->common.num_args != num_args) {
2661 if (num_args == 0) {
2662 zend_error(error_type, "Method %s::%s() cannot take arguments",
2663 ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2664 } else if (num_args == 1) {
2665 zend_error(error_type, "Method %s::%s() must take exactly 1 argument",
2666 ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2667 } else {
2668 zend_error(error_type, "Method %s::%s() must take exactly %" PRIu32 " arguments",
2669 ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name), num_args);
2670 }
2671 return;
2672 }
2673 for (uint32_t i = 0; i < num_args; i++) {
2674 if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, i + 1)) {
2675 zend_error(error_type, "Method %s::%s() cannot take arguments by reference",
2676 ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2677 return;
2678 }
2679 }
2680 }
2681
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 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)
2683 {
2684 if (
2685 ZEND_TYPE_IS_SET(fptr->common.arg_info[arg_num].type)
2686 && !(ZEND_TYPE_FULL_MASK(fptr->common.arg_info[arg_num].type) & arg_type)
2687 ) {
2688 zend_error(error_type, "%s::%s(): Parameter #%d ($%s) must be of type %s when declared",
2689 ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2690 arg_num + 1, ZSTR_VAL(fptr->common.arg_info[arg_num].name),
2691 ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(arg_type))));
2692 }
2693 }
2694
zend_check_magic_method_return_type(const zend_class_entry * ce,const zend_function * fptr,int error_type,int return_type)2695 static void zend_check_magic_method_return_type(const zend_class_entry *ce, const zend_function *fptr, int error_type, int return_type)
2696 {
2697 if (!(fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
2698 /* For backwards compatibility reasons, do not enforce the return type if it is not set. */
2699 return;
2700 }
2701
2702 if (ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & MAY_BE_NEVER) {
2703 /* It is always legal to specify the never type. */
2704 return;
2705 }
2706
2707 bool is_complex_type = ZEND_TYPE_IS_COMPLEX(fptr->common.arg_info[-1].type);
2708 uint32_t extra_types = ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & ~return_type;
2709 if (extra_types & MAY_BE_STATIC) {
2710 extra_types &= ~MAY_BE_STATIC;
2711 is_complex_type = true;
2712 }
2713
2714 if (extra_types || (is_complex_type && return_type != MAY_BE_OBJECT)) {
2715 zend_error(error_type, "%s::%s(): Return type must be %s when declared",
2716 ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
2717 ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(return_type))));
2718 }
2719 }
2720
zend_check_magic_method_non_static(const zend_class_entry * ce,const zend_function * fptr,int error_type)2721 static void zend_check_magic_method_non_static(
2722 const zend_class_entry *ce, const zend_function *fptr, int error_type)
2723 {
2724 if (fptr->common.fn_flags & ZEND_ACC_STATIC) {
2725 zend_error(error_type, "Method %s::%s() cannot be static",
2726 ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2727 }
2728 }
2729
zend_check_magic_method_static(const zend_class_entry * ce,const zend_function * fptr,int error_type)2730 static void zend_check_magic_method_static(
2731 const zend_class_entry *ce, const zend_function *fptr, int error_type)
2732 {
2733 if (!(fptr->common.fn_flags & ZEND_ACC_STATIC)) {
2734 zend_error(error_type, "Method %s::%s() must be static",
2735 ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2736 }
2737 }
2738
zend_check_magic_method_public(const zend_class_entry * ce,const zend_function * fptr,int error_type)2739 static void zend_check_magic_method_public(
2740 const zend_class_entry *ce, const zend_function *fptr, int error_type)
2741 {
2742 // TODO: Remove this warning after adding proper visibility handling.
2743 if (!(fptr->common.fn_flags & ZEND_ACC_PUBLIC)) {
2744 zend_error(E_WARNING, "The magic method %s::%s() must have public visibility",
2745 ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2746 }
2747 }
2748
zend_check_magic_method_no_return_type(const zend_class_entry * ce,const zend_function * fptr,int error_type)2749 static void zend_check_magic_method_no_return_type(
2750 const zend_class_entry *ce, const zend_function *fptr, int error_type)
2751 {
2752 if (fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
2753 zend_error_noreturn(error_type, "Method %s::%s() cannot declare a return type",
2754 ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
2755 }
2756 }
2757
zend_check_magic_method_implementation(const zend_class_entry * ce,const zend_function * fptr,zend_string * lcname,int error_type)2758 ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, zend_string *lcname, int error_type) /* {{{ */
2759 {
2760 if (ZSTR_VAL(lcname)[0] != '_'
2761 || ZSTR_VAL(lcname)[1] != '_') {
2762 return;
2763 }
2764
2765 if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2766 zend_check_magic_method_non_static(ce, fptr, error_type);
2767 zend_check_magic_method_no_return_type(ce, fptr, error_type);
2768 } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2769 zend_check_magic_method_args(0, ce, fptr, error_type);
2770 zend_check_magic_method_non_static(ce, fptr, error_type);
2771 zend_check_magic_method_no_return_type(ce, fptr, error_type);
2772 } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2773 zend_check_magic_method_args(0, ce, fptr, error_type);
2774 zend_check_magic_method_non_static(ce, fptr, error_type);
2775 zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2776 } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2777 zend_check_magic_method_args(1, ce, fptr, error_type);
2778 zend_check_magic_method_non_static(ce, fptr, error_type);
2779 zend_check_magic_method_public(ce, fptr, error_type);
2780 zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2781 } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2782 zend_check_magic_method_args(2, ce, fptr, error_type);
2783 zend_check_magic_method_non_static(ce, fptr, error_type);
2784 zend_check_magic_method_public(ce, fptr, error_type);
2785 zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2786 zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2787 } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
2788 zend_check_magic_method_args(1, ce, fptr, error_type);
2789 zend_check_magic_method_non_static(ce, fptr, error_type);
2790 zend_check_magic_method_public(ce, fptr, error_type);
2791 zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2792 zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2793 } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2794 zend_check_magic_method_args(1, ce, fptr, error_type);
2795 zend_check_magic_method_non_static(ce, fptr, error_type);
2796 zend_check_magic_method_public(ce, fptr, error_type);
2797 zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2798 zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_BOOL);
2799 } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2800 zend_check_magic_method_args(2, ce, fptr, error_type);
2801 zend_check_magic_method_non_static(ce, fptr, error_type);
2802 zend_check_magic_method_public(ce, fptr, error_type);
2803 zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2804 zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2805 } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
2806 zend_check_magic_method_args(2, ce, fptr, error_type);
2807 zend_check_magic_method_static(ce, fptr, error_type);
2808 zend_check_magic_method_public(ce, fptr, error_type);
2809 zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
2810 zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
2811 } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
2812 zend_check_magic_method_args(0, ce, fptr, error_type);
2813 zend_check_magic_method_non_static(ce, fptr, error_type);
2814 zend_check_magic_method_public(ce, fptr, error_type);
2815 zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_STRING);
2816 } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
2817 zend_check_magic_method_args(0, ce, fptr, error_type);
2818 zend_check_magic_method_non_static(ce, fptr, error_type);
2819 zend_check_magic_method_public(ce, fptr, error_type);
2820 zend_check_magic_method_return_type(ce, fptr, error_type, (MAY_BE_ARRAY | MAY_BE_NULL));
2821 } else if (zend_string_equals_literal(lcname, "__serialize")) {
2822 zend_check_magic_method_args(0, ce, fptr, error_type);
2823 zend_check_magic_method_non_static(ce, fptr, error_type);
2824 zend_check_magic_method_public(ce, fptr, error_type);
2825 zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2826 } else if (zend_string_equals_literal(lcname, "__unserialize")) {
2827 zend_check_magic_method_args(1, ce, fptr, error_type);
2828 zend_check_magic_method_non_static(ce, fptr, error_type);
2829 zend_check_magic_method_public(ce, fptr, error_type);
2830 zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2831 zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2832 } else if (zend_string_equals_literal(lcname, "__set_state")) {
2833 zend_check_magic_method_args(1, ce, fptr, error_type);
2834 zend_check_magic_method_static(ce, fptr, error_type);
2835 zend_check_magic_method_public(ce, fptr, error_type);
2836 zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
2837 zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_OBJECT);
2838 } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) {
2839 zend_check_magic_method_non_static(ce, fptr, error_type);
2840 zend_check_magic_method_public(ce, fptr, error_type);
2841 } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_SLEEP))) {
2842 zend_check_magic_method_args(0, ce, fptr, error_type);
2843 zend_check_magic_method_non_static(ce, fptr, error_type);
2844 zend_check_magic_method_public(ce, fptr, error_type);
2845 zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
2846 } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_WAKEUP))) {
2847 zend_check_magic_method_args(0, ce, fptr, error_type);
2848 zend_check_magic_method_non_static(ce, fptr, error_type);
2849 zend_check_magic_method_public(ce, fptr, error_type);
2850 zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
2851 }
2852 }
2853 /* }}} */
2854
zend_add_magic_method(zend_class_entry * ce,zend_function * fptr,zend_string * lcname)2855 ZEND_API void zend_add_magic_method(zend_class_entry *ce, zend_function *fptr, zend_string *lcname)
2856 {
2857 if (ZSTR_VAL(lcname)[0] != '_' || ZSTR_VAL(lcname)[1] != '_') {
2858 /* pass */
2859 } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
2860 ce->clone = fptr;
2861 } else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
2862 ce->constructor = fptr;
2863 ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
2864 } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
2865 ce->destructor = fptr;
2866 } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
2867 ce->__get = fptr;
2868 ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2869 } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
2870 ce->__set = fptr;
2871 ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2872 } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
2873 ce->__call = fptr;
2874 } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
2875 ce->__unset = fptr;
2876 ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2877 } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
2878 ce->__isset = fptr;
2879 ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2880 } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
2881 ce->__callstatic = fptr;
2882 } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
2883 ce->__tostring = fptr;
2884 } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
2885 ce->__debugInfo = fptr;
2886 ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2887 } else if (zend_string_equals_literal(lcname, "__serialize")) {
2888 ce->__serialize = fptr;
2889 } else if (zend_string_equals_literal(lcname, "__unserialize")) {
2890 ce->__unserialize = fptr;
2891 }
2892 }
2893
2894 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arg_info_toString, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()2895 ZEND_END_ARG_INFO()
2896
2897 static zend_always_inline void zend_normalize_internal_type(zend_type *type) {
2898 ZEND_ASSERT(!ZEND_TYPE_HAS_LITERAL_NAME(*type));
2899 if (ZEND_TYPE_PURE_MASK(*type) != MAY_BE_ANY) {
2900 ZEND_ASSERT(!ZEND_TYPE_CONTAINS_CODE(*type, IS_RESOURCE) && "resource is not allowed in a zend_type");
2901 }
2902 zend_type *current;
2903 ZEND_TYPE_FOREACH(*type, current) {
2904 if (ZEND_TYPE_HAS_NAME(*current)) {
2905 zend_string *name = zend_new_interned_string(ZEND_TYPE_NAME(*current));
2906 zend_alloc_ce_cache(name);
2907 ZEND_TYPE_SET_PTR(*current, name);
2908 } else if (ZEND_TYPE_HAS_LIST(*current)) {
2909 zend_type *inner;
2910 ZEND_TYPE_FOREACH(*current, inner) {
2911 ZEND_ASSERT(!ZEND_TYPE_HAS_LITERAL_NAME(*inner) && !ZEND_TYPE_HAS_LIST(*inner));
2912 if (ZEND_TYPE_HAS_NAME(*inner)) {
2913 zend_string *name = zend_new_interned_string(ZEND_TYPE_NAME(*inner));
2914 zend_alloc_ce_cache(name);
2915 ZEND_TYPE_SET_PTR(*inner, name);
2916 }
2917 } ZEND_TYPE_FOREACH_END();
2918 }
2919 } ZEND_TYPE_FOREACH_END();
2920 }
2921
2922 /* 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)2923 ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type) /* {{{ */
2924 {
2925 const zend_function_entry *ptr = functions;
2926 zend_function function;
2927 zend_internal_function *reg_function, *internal_function = (zend_internal_function *)&function;
2928 int count=0, unload=0;
2929 HashTable *target_function_table = function_table;
2930 int error_type;
2931 zend_string *lowercase_name;
2932 size_t fname_len;
2933
2934 if (type==MODULE_PERSISTENT) {
2935 error_type = E_CORE_WARNING;
2936 } else {
2937 error_type = E_WARNING;
2938 }
2939
2940 if (!target_function_table) {
2941 target_function_table = CG(function_table);
2942 }
2943 internal_function->type = ZEND_INTERNAL_FUNCTION;
2944 internal_function->module = EG(current_module);
2945 internal_function->T = 0;
2946 memset(internal_function->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
2947
2948 while (ptr->fname) {
2949 fname_len = strlen(ptr->fname);
2950 internal_function->handler = ptr->handler;
2951 internal_function->doc_comment = ptr->doc_comment ? zend_string_init_interned(ptr->doc_comment, strlen(ptr->doc_comment), 1) : NULL;
2952 internal_function->function_name = zend_string_init_interned(ptr->fname, fname_len, 1);
2953 internal_function->scope = scope;
2954 internal_function->prototype = NULL;
2955 internal_function->prop_info = NULL;
2956 internal_function->attributes = NULL;
2957 internal_function->frameless_function_infos = ptr->frameless_function_infos;
2958 if (EG(active)) { // at run-time: this ought to only happen if registered with dl() or somehow temporarily at runtime
2959 ZEND_MAP_PTR_INIT(internal_function->run_time_cache, zend_arena_calloc(&CG(arena), 1, zend_internal_run_time_cache_reserved_size()));
2960 } else {
2961 #ifdef ZTS
2962 ZEND_MAP_PTR_NEW_STATIC(internal_function->run_time_cache);
2963 #else
2964 ZEND_MAP_PTR_INIT(internal_function->run_time_cache, NULL);
2965 #endif
2966 }
2967 if (ptr->flags) {
2968 if (!(ptr->flags & ZEND_ACC_PPP_MASK)) {
2969 if (ptr->flags != ZEND_ACC_DEPRECATED && scope) {
2970 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);
2971 }
2972 internal_function->fn_flags = ZEND_ACC_PUBLIC | ptr->flags;
2973 } else {
2974 internal_function->fn_flags = ptr->flags;
2975 }
2976 } else {
2977 internal_function->fn_flags = ZEND_ACC_PUBLIC;
2978 }
2979
2980 if (ptr->arg_info) {
2981 zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info;
2982 internal_function->arg_info = (zend_internal_arg_info*)ptr->arg_info+1;
2983 internal_function->num_args = ptr->num_args;
2984 /* Currently you cannot denote that the function can accept less arguments than num_args */
2985 if (info->required_num_args == (uintptr_t)-1) {
2986 internal_function->required_num_args = ptr->num_args;
2987 } else {
2988 internal_function->required_num_args = info->required_num_args;
2989 }
2990 if (ZEND_ARG_SEND_MODE(info)) {
2991 internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE;
2992 }
2993 if (ZEND_ARG_IS_VARIADIC(&ptr->arg_info[ptr->num_args])) {
2994 internal_function->fn_flags |= ZEND_ACC_VARIADIC;
2995 /* Don't count the variadic argument */
2996 internal_function->num_args--;
2997 }
2998 if (ZEND_TYPE_IS_SET(info->type)) {
2999 if (ZEND_TYPE_HAS_NAME(info->type)) {
3000 const char *type_name = ZEND_TYPE_LITERAL_NAME(info->type);
3001 if (!scope && (!strcasecmp(type_name, "self") || !strcasecmp(type_name, "parent"))) {
3002 zend_error_noreturn(E_CORE_ERROR, "Cannot declare a return type of %s outside of a class scope", type_name);
3003 }
3004 }
3005
3006 internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
3007 }
3008 } else {
3009 zend_error(E_CORE_WARNING, "Missing arginfo for %s%s%s()",
3010 scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3011
3012 internal_function->arg_info = NULL;
3013 internal_function->num_args = 0;
3014 internal_function->required_num_args = 0;
3015 }
3016
3017 /* If not specified, add __toString() return type for compatibility with Stringable
3018 * interface. */
3019 if (scope && zend_string_equals_literal_ci(internal_function->function_name, "__tostring") &&
3020 !(internal_function->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
3021 zend_error(E_CORE_WARNING, "%s::__toString() implemented without string return type",
3022 ZSTR_VAL(scope->name));
3023 internal_function->arg_info = (zend_internal_arg_info *) arg_info_toString + 1;
3024 internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
3025 internal_function->num_args = internal_function->required_num_args = 0;
3026 }
3027
3028
3029 zend_set_function_arg_flags((zend_function*)internal_function);
3030 if (ptr->flags & ZEND_ACC_ABSTRACT) {
3031 if (scope) {
3032 /* This is a class that must be abstract itself. Here we set the check info. */
3033 scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
3034 if (!(scope->ce_flags & ZEND_ACC_INTERFACE)) {
3035 /* Since the class is not an interface it needs to be declared as a abstract class. */
3036 /* Since here we are handling internal functions only we can add the keyword flag. */
3037 /* This time we set the flag for the keyword 'abstract'. */
3038 scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
3039 }
3040 }
3041 if ((ptr->flags & ZEND_ACC_STATIC) && (!scope || !(scope->ce_flags & ZEND_ACC_INTERFACE))) {
3042 zend_error(error_type, "Static function %s%s%s() cannot be abstract", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3043 }
3044 } else {
3045 if (scope && (scope->ce_flags & ZEND_ACC_INTERFACE)) {
3046 zend_error(error_type, "Interface %s cannot contain non abstract method %s()", ZSTR_VAL(scope->name), ptr->fname);
3047 return FAILURE;
3048 }
3049 if (!internal_function->handler) {
3050 zend_error(error_type, "Method %s%s%s() cannot be a NULL function", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3051 zend_unregister_functions(functions, count, target_function_table);
3052 return FAILURE;
3053 }
3054 }
3055 lowercase_name = zend_string_tolower_ex(internal_function->function_name, type == MODULE_PERSISTENT);
3056 lowercase_name = zend_new_interned_string(lowercase_name);
3057 reg_function = malloc(sizeof(zend_internal_function));
3058 memcpy(reg_function, &function, sizeof(zend_internal_function));
3059 if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) {
3060 unload=1;
3061 free(reg_function);
3062 zend_string_release(lowercase_name);
3063 break;
3064 }
3065 if (reg_function->frameless_function_infos) {
3066 const zend_frameless_function_info *flf_info = reg_function->frameless_function_infos;
3067 while (flf_info->handler) {
3068 if (zend_flf_count == zend_flf_capacity) {
3069 if (!zend_flf_capacity) {
3070 zend_flf_capacity = 8;
3071 } else {
3072 zend_flf_capacity *= 2;
3073 }
3074 /* +1 for NULL terminator */
3075 zend_flf_handlers = realloc(zend_flf_handlers, (zend_flf_capacity + 1) * sizeof(void *));
3076 zend_flf_functions = realloc(zend_flf_functions, (zend_flf_capacity + 1) * sizeof(zend_function *));
3077 }
3078 zend_flf_handlers[zend_flf_count] = flf_info->handler;
3079 zend_flf_functions[zend_flf_count] = (zend_function *)reg_function;
3080 zend_flf_count++;
3081 flf_info++;
3082 }
3083 zend_flf_handlers[zend_flf_count] = NULL;
3084 zend_flf_functions[zend_flf_count] = NULL;
3085 }
3086
3087 /* Get parameter count including variadic parameter. */
3088 uint32_t num_args = reg_function->num_args;
3089 if (reg_function->fn_flags & ZEND_ACC_VARIADIC) {
3090 num_args++;
3091 }
3092
3093 /* If types of arguments have to be checked */
3094 if (reg_function->arg_info && num_args) {
3095 uint32_t i;
3096 for (i = 0; i < num_args; i++) {
3097 zend_internal_arg_info *arg_info = ®_function->arg_info[i];
3098 ZEND_ASSERT(arg_info->name && "Parameter must have a name");
3099 if (ZEND_TYPE_IS_SET(arg_info->type)) {
3100 reg_function->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
3101 }
3102 #if ZEND_DEBUG
3103 for (uint32_t j = 0; j < i; j++) {
3104 if (!strcmp(arg_info->name, reg_function->arg_info[j].name)) {
3105 zend_error_noreturn(E_CORE_ERROR,
3106 "Duplicate parameter name $%s for function %s%s%s()", arg_info->name,
3107 scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3108 }
3109 }
3110 #endif
3111 }
3112 }
3113
3114 /* Rebuild arginfos if parameter/property types and/or a return type are used */
3115 if (reg_function->arg_info &&
3116 (reg_function->fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS))) {
3117 /* convert "const char*" class type names into "zend_string*" */
3118 uint32_t i;
3119 zend_internal_arg_info *arg_info = reg_function->arg_info - 1;
3120 zend_internal_arg_info *new_arg_info;
3121
3122 /* Treat return type as an extra argument */
3123 num_args++;
3124 new_arg_info = malloc(sizeof(zend_internal_arg_info) * num_args);
3125 memcpy(new_arg_info, arg_info, sizeof(zend_internal_arg_info) * num_args);
3126 reg_function->arg_info = new_arg_info + 1;
3127 for (i = 0; i < num_args; i++) {
3128 if (ZEND_TYPE_HAS_LITERAL_NAME(new_arg_info[i].type)) {
3129 // gen_stubs.php does not support codegen for DNF types in arg infos.
3130 // As a temporary workaround, we split the type name on `|` characters,
3131 // converting it to an union type if necessary.
3132 const char *class_name = ZEND_TYPE_LITERAL_NAME(new_arg_info[i].type);
3133 new_arg_info[i].type.type_mask &= ~_ZEND_TYPE_LITERAL_NAME_BIT;
3134
3135 size_t num_types = 1;
3136 const char *p = class_name;
3137 while ((p = strchr(p, '|'))) {
3138 num_types++;
3139 p++;
3140 }
3141
3142 if (num_types == 1) {
3143 /* Simple class type */
3144 zend_string *str = zend_string_init_interned(class_name, strlen(class_name), 1);
3145 zend_alloc_ce_cache(str);
3146 ZEND_TYPE_SET_PTR(new_arg_info[i].type, str);
3147 new_arg_info[i].type.type_mask |= _ZEND_TYPE_NAME_BIT;
3148 } else {
3149 /* Union type */
3150 zend_type_list *list = malloc(ZEND_TYPE_LIST_SIZE(num_types));
3151 list->num_types = num_types;
3152 ZEND_TYPE_SET_LIST(new_arg_info[i].type, list);
3153 ZEND_TYPE_FULL_MASK(new_arg_info[i].type) |= _ZEND_TYPE_UNION_BIT;
3154
3155 const char *start = class_name;
3156 uint32_t j = 0;
3157 while (true) {
3158 const char *end = strchr(start, '|');
3159 zend_string *str = zend_string_init_interned(start, end ? end - start : strlen(start), 1);
3160 zend_alloc_ce_cache(str);
3161 list->types[j] = (zend_type) ZEND_TYPE_INIT_CLASS(str, 0, 0);
3162 if (!end) {
3163 break;
3164 }
3165 start = end + 1;
3166 j++;
3167 }
3168 }
3169 }
3170 if (ZEND_TYPE_IS_ITERABLE_FALLBACK(new_arg_info[i].type)) {
3171 /* Warning generated an extension load warning which is emitted for every test
3172 zend_error(E_CORE_WARNING, "iterable type is now a compile time alias for array|Traversable,"
3173 " regenerate the argument info via the php-src gen_stub build script");
3174 */
3175 zend_type legacy_iterable = ZEND_TYPE_INIT_CLASS_MASK(
3176 ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
3177 (new_arg_info[i].type.type_mask | MAY_BE_ARRAY)
3178 );
3179 new_arg_info[i].type = legacy_iterable;
3180 }
3181
3182 zend_normalize_internal_type(&new_arg_info[i].type);
3183 }
3184 }
3185
3186 if (scope) {
3187 zend_check_magic_method_implementation(
3188 scope, (zend_function *)reg_function, lowercase_name, E_CORE_ERROR);
3189 zend_add_magic_method(scope, (zend_function *)reg_function, lowercase_name);
3190 }
3191 ptr++;
3192 count++;
3193 zend_string_release(lowercase_name);
3194 }
3195 if (unload) { /* before unloading, display all remaining bad function in the module */
3196 while (ptr->fname) {
3197 fname_len = strlen(ptr->fname);
3198 lowercase_name = zend_string_alloc(fname_len, 0);
3199 zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
3200 if (zend_hash_exists(target_function_table, lowercase_name)) {
3201 zend_error(error_type, "Function registration failed - duplicate name - %s%s%s", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
3202 }
3203 zend_string_efree(lowercase_name);
3204 ptr++;
3205 }
3206 zend_unregister_functions(functions, count, target_function_table);
3207 return FAILURE;
3208 }
3209 return SUCCESS;
3210 }
3211 /* }}} */
3212
3213 /* count=-1 means erase all functions, otherwise,
3214 * erase the first count functions
3215 */
zend_unregister_functions(const zend_function_entry * functions,int count,HashTable * function_table)3216 ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table) /* {{{ */
3217 {
3218 const zend_function_entry *ptr = functions;
3219 int i=0;
3220 HashTable *target_function_table = function_table;
3221 zend_string *lowercase_name;
3222 size_t fname_len;
3223
3224 if (!target_function_table) {
3225 target_function_table = CG(function_table);
3226 }
3227 while (ptr->fname) {
3228 if (count!=-1 && i>=count) {
3229 break;
3230 }
3231 fname_len = strlen(ptr->fname);
3232 lowercase_name = zend_string_alloc(fname_len, 0);
3233 zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
3234 zend_hash_del(target_function_table, lowercase_name);
3235 zend_string_efree(lowercase_name);
3236 ptr++;
3237 i++;
3238 }
3239 }
3240 /* }}} */
3241
zend_startup_module(zend_module_entry * module)3242 ZEND_API zend_result zend_startup_module(zend_module_entry *module) /* {{{ */
3243 {
3244 if ((module = zend_register_internal_module(module)) != NULL && zend_startup_module_ex(module) == SUCCESS) {
3245 return SUCCESS;
3246 }
3247 return FAILURE;
3248 }
3249 /* }}} */
3250
zend_get_module_started(const char * module_name)3251 ZEND_API zend_result zend_get_module_started(const char *module_name) /* {{{ */
3252 {
3253 zend_module_entry *module;
3254
3255 module = zend_hash_str_find_ptr(&module_registry, module_name, strlen(module_name));
3256 return (module && module->module_started) ? SUCCESS : FAILURE;
3257 }
3258 /* }}} */
3259
clean_module_class(zval * el,void * arg)3260 static int clean_module_class(zval *el, void *arg) /* {{{ */
3261 {
3262 zend_class_entry *ce = (zend_class_entry *)Z_PTR_P(el);
3263 int module_number = *(int *)arg;
3264 if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->module_number == module_number) {
3265 return ZEND_HASH_APPLY_REMOVE;
3266 } else {
3267 return ZEND_HASH_APPLY_KEEP;
3268 }
3269 }
3270 /* }}} */
3271
clean_module_classes(int module_number)3272 static void clean_module_classes(int module_number) /* {{{ */
3273 {
3274 zend_hash_apply_with_argument(EG(class_table), clean_module_class, (void *) &module_number);
3275 }
3276 /* }}} */
3277
clean_module_function(zval * el,void * arg)3278 static int clean_module_function(zval *el, void *arg) /* {{{ */
3279 {
3280 zend_function *fe = (zend_function *) Z_PTR_P(el);
3281 zend_module_entry *module = (zend_module_entry *) arg;
3282 if (fe->common.type == ZEND_INTERNAL_FUNCTION && fe->internal_function.module == module) {
3283 return ZEND_HASH_APPLY_REMOVE;
3284 } else {
3285 return ZEND_HASH_APPLY_KEEP;
3286 }
3287 }
3288 /* }}} */
3289
clean_module_functions(zend_module_entry * module)3290 static void clean_module_functions(zend_module_entry *module) /* {{{ */
3291 {
3292 zend_hash_apply_with_argument(CG(function_table), clean_module_function, module);
3293 }
3294 /* }}} */
3295
module_destructor(zend_module_entry * module)3296 void module_destructor(zend_module_entry *module) /* {{{ */
3297 {
3298 #if ZEND_RC_DEBUG
3299 bool orig_rc_debug = zend_rc_debug;
3300 #endif
3301
3302 if (module->type == MODULE_TEMPORARY) {
3303 #if ZEND_RC_DEBUG
3304 /* FIXME: Loading extensions during the request breaks some invariants.
3305 * In particular, it will create persistent interned strings, which is
3306 * not allowed at this stage. */
3307 zend_rc_debug = false;
3308 #endif
3309 zend_clean_module_rsrc_dtors(module->module_number);
3310 clean_module_constants(module->module_number);
3311 clean_module_classes(module->module_number);
3312 }
3313
3314 if (module->module_started && module->module_shutdown_func) {
3315 #if 0
3316 zend_printf("%s: Module shutdown\n", module->name);
3317 #endif
3318 module->module_shutdown_func(module->type, module->module_number);
3319 }
3320
3321 if (module->module_started
3322 && !module->module_shutdown_func
3323 && module->type == MODULE_TEMPORARY) {
3324 zend_unregister_ini_entries_ex(module->module_number, module->type);
3325 }
3326
3327 /* Deinitialize module globals */
3328 if (module->globals_size) {
3329 #ifdef ZTS
3330 if (*module->globals_id_ptr) {
3331 ts_free_id(*module->globals_id_ptr);
3332 }
3333 #else
3334 if (module->globals_dtor) {
3335 module->globals_dtor(module->globals_ptr);
3336 }
3337 #endif
3338 }
3339
3340 module->module_started=0;
3341 if (module->type == MODULE_TEMPORARY && module->functions) {
3342 zend_unregister_functions(module->functions, -1, NULL);
3343 /* Clean functions registered separately from module->functions */
3344 clean_module_functions(module);
3345 }
3346
3347 #if ZEND_RC_DEBUG
3348 zend_rc_debug = orig_rc_debug;
3349 #endif
3350 }
3351 /* }}} */
3352
module_registry_unload(const zend_module_entry * module)3353 void module_registry_unload(const zend_module_entry *module)
3354 {
3355 #ifdef HAVE_LIBDL
3356 if (!getenv("ZEND_DONT_UNLOAD_MODULES")) {
3357 DL_UNLOAD(module->handle);
3358 }
3359 #else
3360 ZEND_IGNORE_VALUE(module);
3361 #endif
3362 }
3363
zend_activate_modules(void)3364 ZEND_API void zend_activate_modules(void) /* {{{ */
3365 {
3366 zend_module_entry **p = module_request_startup_handlers;
3367
3368 while (*p) {
3369 zend_module_entry *module = *p;
3370
3371 if (module->request_startup_func(module->type, module->module_number)==FAILURE) {
3372 zend_error(E_WARNING, "request_startup() for %s module failed", module->name);
3373 exit(1);
3374 }
3375 p++;
3376 }
3377 }
3378 /* }}} */
3379
zend_deactivate_modules(void)3380 ZEND_API void zend_deactivate_modules(void) /* {{{ */
3381 {
3382 EG(current_execute_data) = NULL; /* we're no longer executing anything */
3383
3384 if (EG(full_tables_cleanup)) {
3385 zend_module_entry *module;
3386
3387 ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) {
3388 if (module->request_shutdown_func) {
3389 zend_try {
3390 module->request_shutdown_func(module->type, module->module_number);
3391 } zend_end_try();
3392 }
3393 } ZEND_HASH_FOREACH_END();
3394 } else {
3395 zend_module_entry **p = module_request_shutdown_handlers;
3396
3397 while (*p) {
3398 zend_module_entry *module = *p;
3399 zend_try {
3400 module->request_shutdown_func(module->type, module->module_number);
3401 } zend_end_try();
3402 p++;
3403 }
3404 }
3405 }
3406 /* }}} */
3407
zend_unload_modules(void)3408 void zend_unload_modules(void) /* {{{ */
3409 {
3410 zend_module_entry **modules = modules_dl_loaded;
3411 while (*modules) {
3412 module_registry_unload(*modules);
3413 modules++;
3414 }
3415 free(modules_dl_loaded);
3416 modules_dl_loaded = NULL;
3417 }
3418 /* }}} */
3419
zend_post_deactivate_modules(void)3420 ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
3421 {
3422 if (EG(full_tables_cleanup)) {
3423 zend_module_entry *module;
3424 zval *zv;
3425 zend_string *key;
3426
3427 ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
3428 if (module->post_deactivate_func) {
3429 module->post_deactivate_func();
3430 }
3431 } ZEND_HASH_FOREACH_END();
3432 ZEND_HASH_MAP_REVERSE_FOREACH_STR_KEY_VAL(&module_registry, key, zv) {
3433 module = Z_PTR_P(zv);
3434 if (module->type != MODULE_TEMPORARY) {
3435 break;
3436 }
3437 module_destructor(module);
3438 if (module->handle) {
3439 module_registry_unload(module);
3440 }
3441 zend_string_release_ex(key, 0);
3442 } ZEND_HASH_MAP_FOREACH_END_DEL();
3443 } else {
3444 zend_module_entry **p = module_post_deactivate_handlers;
3445
3446 while (*p) {
3447 zend_module_entry *module = *p;
3448
3449 module->post_deactivate_func();
3450 p++;
3451 }
3452 }
3453 }
3454 /* }}} */
3455
3456 /* return the next free module number */
zend_next_free_module(void)3457 ZEND_API int zend_next_free_module(void) /* {{{ */
3458 {
3459 return zend_hash_num_elements(&module_registry);
3460 }
3461 /* }}} */
3462
do_register_internal_class(zend_class_entry * orig_class_entry,uint32_t ce_flags)3463 static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
3464 {
3465 zend_class_entry *class_entry = malloc(sizeof(zend_class_entry));
3466 zend_string *lowercase_name;
3467 *class_entry = *orig_class_entry;
3468
3469 class_entry->type = ZEND_INTERNAL_CLASS;
3470 zend_initialize_class_data(class_entry, 0);
3471 zend_alloc_ce_cache(class_entry->name);
3472 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;
3473 class_entry->info.internal.module = EG(current_module);
3474
3475 if (class_entry->info.internal.builtin_functions) {
3476 zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, EG(current_module)->type);
3477 }
3478
3479 lowercase_name = zend_string_tolower_ex(orig_class_entry->name, EG(current_module)->type == MODULE_PERSISTENT);
3480 lowercase_name = zend_new_interned_string(lowercase_name);
3481 zend_hash_update_ptr(CG(class_table), lowercase_name, class_entry);
3482 zend_string_release_ex(lowercase_name, 1);
3483
3484 if (class_entry->__tostring && !zend_string_equals_literal(class_entry->name, "Stringable")
3485 && !(class_entry->ce_flags & ZEND_ACC_TRAIT)) {
3486 ZEND_ASSERT(zend_ce_stringable
3487 && "Should be registered before first class using __toString()");
3488 zend_do_implement_interface(class_entry, zend_ce_stringable);
3489 }
3490 return class_entry;
3491 }
3492 /* }}} */
3493
3494 /* If parent_ce is not NULL then it inherits from parent_ce
3495 * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it
3496 * If both parent_ce and parent_name are NULL it does a regular class registration
3497 * If parent_name is specified but not found NULL is returned
3498 */
zend_register_internal_class_ex(zend_class_entry * class_entry,zend_class_entry * parent_ce)3499 ZEND_API zend_class_entry *zend_register_internal_class_ex(zend_class_entry *class_entry, zend_class_entry *parent_ce) /* {{{ */
3500 {
3501 return zend_register_internal_class_with_flags(class_entry, parent_ce, 0);
3502 }
3503 /* }}} */
3504
zend_register_internal_class_with_flags(zend_class_entry * class_entry,zend_class_entry * parent_ce,uint32_t ce_flags)3505 ZEND_API zend_class_entry *zend_register_internal_class_with_flags(
3506 zend_class_entry *class_entry,
3507 zend_class_entry *parent_ce,
3508 uint32_t ce_flags
3509 ) {
3510 zend_class_entry *register_class = do_register_internal_class(class_entry, ce_flags);
3511
3512 if (parent_ce) {
3513 zend_do_inheritance(register_class, parent_ce);
3514 zend_build_properties_info_table(register_class);
3515 }
3516
3517 return register_class;
3518 }
3519
zend_class_implements(zend_class_entry * class_entry,int num_interfaces,...)3520 ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...) /* {{{ */
3521 {
3522 zend_class_entry *interface_entry;
3523 va_list interface_list;
3524 va_start(interface_list, num_interfaces);
3525
3526 while (num_interfaces--) {
3527 interface_entry = va_arg(interface_list, zend_class_entry *);
3528 if (interface_entry == zend_ce_stringable
3529 && zend_class_implements_interface(class_entry, zend_ce_stringable)) {
3530 /* Stringable is implemented automatically,
3531 * silently ignore an explicit implementation. */
3532 continue;
3533 }
3534
3535 zend_do_implement_interface(class_entry, interface_entry);
3536 }
3537
3538 va_end(interface_list);
3539 }
3540 /* }}} */
3541
3542 /* A class that contains at least one abstract method automatically becomes an abstract class.
3543 */
zend_register_internal_class(zend_class_entry * orig_class_entry)3544 ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *orig_class_entry) /* {{{ */
3545 {
3546 return do_register_internal_class(orig_class_entry, 0);
3547 }
3548 /* }}} */
3549
zend_register_internal_interface(zend_class_entry * orig_class_entry)3550 ZEND_API zend_class_entry *zend_register_internal_interface(zend_class_entry *orig_class_entry) /* {{{ */
3551 {
3552 return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE);
3553 }
3554 /* }}} */
3555
zend_register_class_alias_ex(const char * name,size_t name_len,zend_class_entry * ce,bool persistent)3556 ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce, bool persistent) /* {{{ */
3557 {
3558 zend_string *lcname;
3559 zval zv, *ret;
3560
3561 /* TODO: Move this out of here in 7.4. */
3562 if (persistent && EG(current_module) && EG(current_module)->type == MODULE_TEMPORARY) {
3563 persistent = 0;
3564 }
3565
3566 if (name[0] == '\\') {
3567 lcname = zend_string_alloc(name_len-1, persistent);
3568 zend_str_tolower_copy(ZSTR_VAL(lcname), name+1, name_len-1);
3569 } else {
3570 lcname = zend_string_alloc(name_len, persistent);
3571 zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len);
3572 }
3573
3574 zend_assert_valid_class_name(lcname, "a class alias");
3575
3576 lcname = zend_new_interned_string(lcname);
3577
3578 /* We cannot increase the refcount of an internal class during request time.
3579 * Instead of having to deal with differentiating between class types and lifetimes,
3580 * we simply don't increase the refcount of a class entry for aliases.
3581 */
3582 ZVAL_ALIAS_PTR(&zv, ce);
3583
3584 ret = zend_hash_add(CG(class_table), lcname, &zv);
3585 zend_string_release_ex(lcname, 0);
3586 if (ret) {
3587 // avoid notifying at MINIT time
3588 if (ce->type == ZEND_USER_CLASS) {
3589 zend_observer_class_linked_notify(ce, lcname);
3590 }
3591 return SUCCESS;
3592 }
3593 return FAILURE;
3594 }
3595 /* }}} */
3596
3597 // 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,...)3598 ZEND_API zend_result zend_set_hash_symbol(zval *symbol, const char *name, size_t name_length, bool is_ref, int num_symbol_tables, ...) /* {{{ */
3599 {
3600 HashTable *symbol_table;
3601 va_list symbol_table_list;
3602
3603 if (num_symbol_tables <= 0) return FAILURE;
3604
3605 if (is_ref) {
3606 ZVAL_MAKE_REF(symbol);
3607 }
3608
3609 va_start(symbol_table_list, num_symbol_tables);
3610 while (num_symbol_tables-- > 0) {
3611 symbol_table = va_arg(symbol_table_list, HashTable *);
3612 zend_hash_str_update(symbol_table, name, name_length, symbol);
3613 Z_TRY_ADDREF_P(symbol);
3614 }
3615 va_end(symbol_table_list);
3616 return SUCCESS;
3617 }
3618 /* }}} */
3619
3620 /* Disabled functions support */
3621
zend_disable_function(const char * function_name,size_t function_name_length)3622 static void zend_disable_function(const char *function_name, size_t function_name_length)
3623 {
3624 if (UNEXPECTED(
3625 (function_name_length == strlen("exit") && !memcmp(function_name, "exit", strlen("exit")))
3626 || (function_name_length == strlen("die") && !memcmp(function_name, "die", strlen("die")))
3627 )) {
3628 zend_error(E_WARNING, "Cannot disable function %s()", function_name);
3629 return;
3630 }
3631 zend_hash_str_del(CG(function_table), function_name, function_name_length);
3632 }
3633
zend_disable_functions(const char * function_list)3634 ZEND_API void zend_disable_functions(const char *function_list) /* {{{ */
3635 {
3636 if (!function_list || !*function_list) {
3637 return;
3638 }
3639
3640 const char *s = NULL, *e = function_list;
3641 while (*e) {
3642 switch (*e) {
3643 case ' ':
3644 case ',':
3645 if (s) {
3646 zend_disable_function(s, e - s);
3647 s = NULL;
3648 }
3649 break;
3650 default:
3651 if (!s) {
3652 s = e;
3653 }
3654 break;
3655 }
3656 e++;
3657 }
3658 if (s) {
3659 zend_disable_function(s, e - s);
3660 }
3661
3662 /* Rehash the function table after deleting functions. This ensures that all internal
3663 * functions are contiguous, which means we don't need to perform full table cleanup
3664 * on shutdown. */
3665 zend_hash_rehash(CG(function_table));
3666 }
3667 /* }}} */
3668
3669 #ifdef ZEND_WIN32
3670 #pragma optimize("", off)
3671 #endif
display_disabled_class(zend_class_entry * class_type)3672 static ZEND_COLD zend_object *display_disabled_class(zend_class_entry *class_type) /* {{{ */
3673 {
3674 zend_object *intern;
3675
3676 intern = zend_objects_new(class_type);
3677
3678 /* Initialize default properties */
3679 if (EXPECTED(class_type->default_properties_count != 0)) {
3680 zval *p = intern->properties_table;
3681 zval *end = p + class_type->default_properties_count;
3682 do {
3683 ZVAL_UNDEF(p);
3684 p++;
3685 } while (p != end);
3686 }
3687
3688 zend_error(E_WARNING, "%s() has been disabled for security reasons", ZSTR_VAL(class_type->name));
3689 return intern;
3690 }
3691 #ifdef ZEND_WIN32
3692 #pragma optimize("", on)
3693 #endif
3694 /* }}} */
3695
3696 static const zend_function_entry disabled_class_new[] = {
3697 ZEND_FE_END
3698 };
3699
zend_disable_class(const char * class_name,size_t class_name_length)3700 ZEND_API zend_result zend_disable_class(const char *class_name, size_t class_name_length) /* {{{ */
3701 {
3702 zend_class_entry *disabled_class;
3703 zend_string *key;
3704 zend_function *fn;
3705 zend_property_info *prop;
3706
3707 key = zend_string_alloc(class_name_length, 0);
3708 zend_str_tolower_copy(ZSTR_VAL(key), class_name, class_name_length);
3709 disabled_class = zend_hash_find_ptr(CG(class_table), key);
3710 zend_string_release_ex(key, 0);
3711 if (!disabled_class) {
3712 return FAILURE;
3713 }
3714
3715 /* Will be reset by INIT_CLASS_ENTRY. */
3716 free(disabled_class->interfaces);
3717
3718 INIT_CLASS_ENTRY_INIT_METHODS((*disabled_class), disabled_class_new);
3719 disabled_class->create_object = display_disabled_class;
3720
3721 ZEND_HASH_MAP_FOREACH_PTR(&disabled_class->function_table, fn) {
3722 if ((fn->common.fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS)) &&
3723 fn->common.scope == disabled_class) {
3724 zend_free_internal_arg_info(&fn->internal_function);
3725 }
3726 } ZEND_HASH_FOREACH_END();
3727 zend_hash_clean(&disabled_class->function_table);
3728 ZEND_HASH_MAP_FOREACH_PTR(&disabled_class->properties_info, prop) {
3729 if (prop->ce == disabled_class) {
3730 zend_string_release(prop->name);
3731 zend_type_release(prop->type, /* persistent */ 1);
3732 free(prop);
3733 }
3734 } ZEND_HASH_FOREACH_END();
3735 zend_hash_clean(&disabled_class->properties_info);
3736 return SUCCESS;
3737 }
3738 /* }}} */
3739
get_scope(zend_execute_data * frame)3740 static zend_always_inline zend_class_entry *get_scope(zend_execute_data *frame)
3741 {
3742 return frame && frame->func ? frame->func->common.scope : NULL;
3743 }
3744
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 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) /* {{{ */
3746 {
3747 bool ret = 0;
3748 zend_class_entry *ce;
3749 size_t name_len = ZSTR_LEN(name);
3750 zend_string *lcname;
3751 ALLOCA_FLAG(use_heap);
3752
3753 ZSTR_ALLOCA_ALLOC(lcname, name_len, use_heap);
3754 zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name), name_len);
3755
3756 *strict_class = 0;
3757 if (zend_string_equals_literal(lcname, "self")) {
3758 if (!scope) {
3759 if (error) *error = estrdup("cannot access \"self\" when no class scope is active");
3760 } else {
3761 if (!suppress_deprecation) {
3762 zend_error(E_DEPRECATED, "Use of \"self\" in callables is deprecated");
3763 }
3764 fcc->called_scope = zend_get_called_scope(frame);
3765 if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope)) {
3766 fcc->called_scope = scope;
3767 }
3768 fcc->calling_scope = scope;
3769 if (!fcc->object) {
3770 fcc->object = zend_get_this_object(frame);
3771 }
3772 ret = 1;
3773 }
3774 } else if (zend_string_equals_literal(lcname, "parent")) {
3775 if (!scope) {
3776 if (error) *error = estrdup("cannot access \"parent\" when no class scope is active");
3777 } else if (!scope->parent) {
3778 if (error) *error = estrdup("cannot access \"parent\" when current class scope has no parent");
3779 } else {
3780 if (!suppress_deprecation) {
3781 zend_error(E_DEPRECATED, "Use of \"parent\" in callables is deprecated");
3782 }
3783 fcc->called_scope = zend_get_called_scope(frame);
3784 if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope->parent)) {
3785 fcc->called_scope = scope->parent;
3786 }
3787 fcc->calling_scope = scope->parent;
3788 if (!fcc->object) {
3789 fcc->object = zend_get_this_object(frame);
3790 }
3791 *strict_class = 1;
3792 ret = 1;
3793 }
3794 } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_STATIC))) {
3795 zend_class_entry *called_scope = zend_get_called_scope(frame);
3796
3797 if (!called_scope) {
3798 if (error) *error = estrdup("cannot access \"static\" when no class scope is active");
3799 } else {
3800 if (!suppress_deprecation) {
3801 zend_error(E_DEPRECATED, "Use of \"static\" in callables is deprecated");
3802 }
3803 fcc->called_scope = called_scope;
3804 fcc->calling_scope = called_scope;
3805 if (!fcc->object) {
3806 fcc->object = zend_get_this_object(frame);
3807 }
3808 *strict_class = 1;
3809 ret = 1;
3810 }
3811 } else if ((ce = zend_lookup_class(name)) != NULL) {
3812 zend_class_entry *scope = get_scope(frame);
3813 fcc->calling_scope = ce;
3814 if (scope && !fcc->object) {
3815 zend_object *object = zend_get_this_object(frame);
3816
3817 if (object &&
3818 instanceof_function(object->ce, scope) &&
3819 instanceof_function(scope, ce)) {
3820 fcc->object = object;
3821 fcc->called_scope = object->ce;
3822 } else {
3823 fcc->called_scope = ce;
3824 }
3825 } else {
3826 fcc->called_scope = fcc->object ? fcc->object->ce : ce;
3827 }
3828 *strict_class = 1;
3829 ret = 1;
3830 } else {
3831 if (error) zend_spprintf(error, 0, "class \"%.*s\" not found", (int)name_len, ZSTR_VAL(name));
3832 }
3833 ZSTR_ALLOCA_FREE(lcname, use_heap);
3834 return ret;
3835 }
3836 /* }}} */
3837
zend_release_fcall_info_cache(zend_fcall_info_cache * fcc)3838 ZEND_API void zend_release_fcall_info_cache(zend_fcall_info_cache *fcc) {
3839 if (fcc->function_handler &&
3840 (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
3841 if (fcc->function_handler->common.function_name) {
3842 zend_string_release_ex(fcc->function_handler->common.function_name, 0);
3843 }
3844 zend_free_trampoline(fcc->function_handler);
3845 fcc->function_handler = NULL;
3846 }
3847 }
3848
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 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) /* {{{ */
3850 {
3851 zend_class_entry *ce_org = fcc->calling_scope;
3852 bool retval = 0;
3853 zend_string *mname, *cname;
3854 zend_string *lmname;
3855 const char *colon;
3856 size_t clen;
3857 HashTable *ftable;
3858 int call_via_handler = 0;
3859 zend_class_entry *scope;
3860 zval *zv;
3861 ALLOCA_FLAG(use_heap)
3862
3863 fcc->calling_scope = NULL;
3864
3865 if (!ce_org) {
3866 zend_function *func;
3867 zend_string *lmname;
3868
3869 /* Check if function with given name exists.
3870 * This may be a compound name that includes namespace name */
3871 if (UNEXPECTED(Z_STRVAL_P(callable)[0] == '\\')) {
3872 /* Skip leading \ */
3873 ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable) - 1, use_heap);
3874 zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable) + 1, Z_STRLEN_P(callable) - 1);
3875 func = zend_fetch_function(lmname);
3876 ZSTR_ALLOCA_FREE(lmname, use_heap);
3877 } else {
3878 lmname = Z_STR_P(callable);
3879 func = zend_fetch_function(lmname);
3880 if (!func) {
3881 ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable), use_heap);
3882 zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable), Z_STRLEN_P(callable));
3883 func = zend_fetch_function(lmname);
3884 ZSTR_ALLOCA_FREE(lmname, use_heap);
3885 }
3886 }
3887 if (EXPECTED(func != NULL)) {
3888 fcc->function_handler = func;
3889 return 1;
3890 }
3891 }
3892
3893 /* Split name into class/namespace and method/function names */
3894 if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL &&
3895 colon > Z_STRVAL_P(callable) &&
3896 *(colon-1) == ':'
3897 ) {
3898 size_t mlen;
3899
3900 colon--;
3901 clen = colon - Z_STRVAL_P(callable);
3902 mlen = Z_STRLEN_P(callable) - clen - 2;
3903
3904 if (colon == Z_STRVAL_P(callable)) {
3905 if (error) *error = estrdup("invalid function name");
3906 return 0;
3907 }
3908
3909 /* This is a compound name.
3910 * Try to fetch class and then find static method. */
3911 if (ce_org) {
3912 scope = ce_org;
3913 } else {
3914 scope = get_scope(frame);
3915 }
3916
3917 cname = zend_string_init_interned(Z_STRVAL_P(callable), clen, 0);
3918 if (ZSTR_HAS_CE_CACHE(cname) && ZSTR_GET_CE_CACHE(cname)) {
3919 fcc->calling_scope = ZSTR_GET_CE_CACHE(cname);
3920 if (scope && !fcc->object) {
3921 zend_object *object = zend_get_this_object(frame);
3922
3923 if (object &&
3924 instanceof_function(object->ce, scope) &&
3925 instanceof_function(scope, fcc->calling_scope)) {
3926 fcc->object = object;
3927 fcc->called_scope = object->ce;
3928 } else {
3929 fcc->called_scope = fcc->calling_scope;
3930 }
3931 } else {
3932 fcc->called_scope = fcc->object ? fcc->object->ce : fcc->calling_scope;
3933 }
3934 strict_class = 1;
3935 } else if (!zend_is_callable_check_class(cname, scope, frame, fcc, &strict_class, error, suppress_deprecation || ce_org != NULL)) {
3936 zend_string_release_ex(cname, 0);
3937 return 0;
3938 }
3939 zend_string_release_ex(cname, 0);
3940
3941 ftable = &fcc->calling_scope->function_table;
3942 if (ce_org && !instanceof_function(ce_org, fcc->calling_scope)) {
3943 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));
3944 return 0;
3945 }
3946 if (ce_org && !suppress_deprecation) {
3947 zend_error(E_DEPRECATED,
3948 "Callables of the form [\"%s\", \"%s\"] are deprecated",
3949 ZSTR_VAL(ce_org->name), Z_STRVAL_P(callable));
3950 }
3951 mname = zend_string_init(Z_STRVAL_P(callable) + clen + 2, mlen, 0);
3952 } else if (ce_org) {
3953 /* Try to fetch find static method of given class. */
3954 mname = Z_STR_P(callable);
3955 zend_string_addref(mname);
3956 ftable = &ce_org->function_table;
3957 fcc->calling_scope = ce_org;
3958 } else {
3959 /* We already checked for plain function before. */
3960 if (error) {
3961 zend_spprintf(error, 0, "function \"%s\" not found or invalid function name", Z_STRVAL_P(callable));
3962 }
3963 return 0;
3964 }
3965
3966 lmname = zend_string_tolower(mname);
3967 if (strict_class &&
3968 fcc->calling_scope &&
3969 zend_string_equals_literal(lmname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
3970 fcc->function_handler = fcc->calling_scope->constructor;
3971 if (fcc->function_handler) {
3972 retval = 1;
3973 }
3974 } else if ((zv = zend_hash_find(ftable, lmname)) != NULL) {
3975 fcc->function_handler = Z_PTR_P(zv);
3976 retval = 1;
3977 if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) &&
3978 !strict_class) {
3979 scope = get_scope(frame);
3980 if (scope &&
3981 instanceof_function(fcc->function_handler->common.scope, scope)) {
3982
3983 zv = zend_hash_find(&scope->function_table, lmname);
3984 if (zv != NULL) {
3985 zend_function *priv_fbc = Z_PTR_P(zv);
3986
3987 if ((priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE)
3988 && priv_fbc->common.scope == scope) {
3989 fcc->function_handler = priv_fbc;
3990 }
3991 }
3992 }
3993 }
3994 if (!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC) &&
3995 (fcc->calling_scope &&
3996 ((fcc->object && fcc->calling_scope->__call) ||
3997 (!fcc->object && fcc->calling_scope->__callstatic)))) {
3998 scope = get_scope(frame);
3999 if (fcc->function_handler->common.scope != scope) {
4000 if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
4001 || !zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope)) {
4002 retval = 0;
4003 fcc->function_handler = NULL;
4004 goto get_function_via_handler;
4005 }
4006 }
4007 }
4008 } else {
4009 get_function_via_handler:
4010 if (fcc->object && fcc->calling_scope == ce_org) {
4011 if (strict_class && ce_org->__call) {
4012 fcc->function_handler = zend_get_call_trampoline_func(ce_org, mname, 0);
4013 call_via_handler = 1;
4014 retval = 1;
4015 } else {
4016 fcc->function_handler = fcc->object->handlers->get_method(&fcc->object, mname, NULL);
4017 if (fcc->function_handler) {
4018 if (strict_class &&
4019 (!fcc->function_handler->common.scope ||
4020 !instanceof_function(ce_org, fcc->function_handler->common.scope))) {
4021 zend_release_fcall_info_cache(fcc);
4022 } else {
4023 retval = 1;
4024 call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
4025 }
4026 }
4027 }
4028 } else if (fcc->calling_scope) {
4029 if (fcc->calling_scope->get_static_method) {
4030 fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname);
4031 } else {
4032 fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, NULL);
4033 }
4034 if (fcc->function_handler) {
4035 retval = 1;
4036 call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
4037 if (call_via_handler && !fcc->object) {
4038 zend_object *object = zend_get_this_object(frame);
4039 if (object &&
4040 instanceof_function(object->ce, fcc->calling_scope)) {
4041 fcc->object = object;
4042 }
4043 }
4044 }
4045 }
4046 }
4047
4048 if (retval) {
4049 if (fcc->calling_scope && !call_via_handler) {
4050 if (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT) {
4051 retval = 0;
4052 if (error) {
4053 zend_spprintf(error, 0, "cannot call abstract method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
4054 }
4055 } else if (!fcc->object && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
4056 retval = 0;
4057 if (error) {
4058 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));
4059 }
4060 }
4061 if (retval
4062 && !(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC)) {
4063 scope = get_scope(frame);
4064 if (fcc->function_handler->common.scope != scope) {
4065 if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
4066 || (!zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope))) {
4067 if (error) {
4068 if (*error) {
4069 efree(*error);
4070 }
4071 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));
4072 }
4073 retval = 0;
4074 }
4075 }
4076 }
4077 }
4078 } else if (error) {
4079 if (fcc->calling_scope) {
4080 zend_spprintf(error, 0, "class %s does not have a method \"%s\"", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(mname));
4081 } else {
4082 zend_spprintf(error, 0, "function %s() does not exist", ZSTR_VAL(mname));
4083 }
4084 }
4085 zend_string_release_ex(lmname, 0);
4086 zend_string_release_ex(mname, 0);
4087
4088 if (fcc->object) {
4089 fcc->called_scope = fcc->object->ce;
4090 if (fcc->function_handler
4091 && (fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
4092 fcc->object = NULL;
4093 }
4094 }
4095 return retval;
4096 }
4097 /* }}} */
4098
zend_get_callable_name_ex(zval * callable,zend_object * object)4099 ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *object) /* {{{ */
4100 {
4101 try_again:
4102 switch (Z_TYPE_P(callable)) {
4103 case IS_STRING:
4104 if (object) {
4105 return zend_create_member_string(object->ce->name, Z_STR_P(callable));
4106 }
4107 return zend_string_copy(Z_STR_P(callable));
4108
4109 case IS_ARRAY:
4110 {
4111 zval *method = NULL;
4112 zval *obj = NULL;
4113
4114 if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
4115 obj = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 0);
4116 method = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 1);
4117 }
4118
4119 if (obj == NULL || method == NULL || Z_TYPE_P(method) != IS_STRING) {
4120 return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
4121 }
4122
4123 if (Z_TYPE_P(obj) == IS_STRING) {
4124 return zend_create_member_string(Z_STR_P(obj), Z_STR_P(method));
4125 } else if (Z_TYPE_P(obj) == IS_OBJECT) {
4126 return zend_create_member_string(Z_OBJCE_P(obj)->name, Z_STR_P(method));
4127 } else {
4128 return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
4129 }
4130 }
4131 case IS_OBJECT:
4132 {
4133 zend_class_entry *ce = Z_OBJCE_P(callable);
4134 return zend_string_concat2(
4135 ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
4136 "::__invoke", sizeof("::__invoke") - 1);
4137 }
4138 case IS_REFERENCE:
4139 callable = Z_REFVAL_P(callable);
4140 goto try_again;
4141 default:
4142 return zval_get_string_func(callable);
4143 }
4144 }
4145 /* }}} */
4146
zend_get_callable_name(zval * callable)4147 ZEND_API zend_string *zend_get_callable_name(zval *callable) /* {{{ */
4148 {
4149 return zend_get_callable_name_ex(callable, NULL);
4150 }
4151 /* }}} */
4152
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)4153 ZEND_API bool zend_is_callable_at_frame(
4154 zval *callable, zend_object *object, zend_execute_data *frame,
4155 uint32_t check_flags, zend_fcall_info_cache *fcc, char **error) /* {{{ */
4156 {
4157 bool ret;
4158 zend_fcall_info_cache fcc_local;
4159 bool strict_class = 0;
4160
4161 if (fcc == NULL) {
4162 fcc = &fcc_local;
4163 }
4164 if (error) {
4165 *error = NULL;
4166 }
4167
4168 fcc->calling_scope = NULL;
4169 fcc->called_scope = NULL;
4170 fcc->function_handler = NULL;
4171 fcc->object = NULL;
4172 fcc->closure = NULL;
4173
4174 again:
4175 switch (Z_TYPE_P(callable)) {
4176 case IS_STRING:
4177 if (object) {
4178 fcc->object = object;
4179 fcc->calling_scope = object->ce;
4180 }
4181
4182 if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4183 fcc->called_scope = fcc->calling_scope;
4184 return 1;
4185 }
4186
4187 check_func:
4188 ret = zend_is_callable_check_func(callable, frame, fcc, strict_class, error, check_flags & IS_CALLABLE_SUPPRESS_DEPRECATIONS);
4189 if (fcc == &fcc_local) {
4190 zend_release_fcall_info_cache(fcc);
4191 }
4192 return ret;
4193
4194 case IS_ARRAY:
4195 {
4196 if (zend_hash_num_elements(Z_ARRVAL_P(callable)) != 2) {
4197 if (error) *error = estrdup("array callback must have exactly two members");
4198 return 0;
4199 }
4200
4201 zval *obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0);
4202 zval *method = zend_hash_index_find(Z_ARRVAL_P(callable), 1);
4203 if (!obj || !method) {
4204 if (error) *error = estrdup("array callback has to contain indices 0 and 1");
4205 return 0;
4206 }
4207
4208 ZVAL_DEREF(obj);
4209 if (Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT) {
4210 if (error) *error = estrdup("first array member is not a valid class name or object");
4211 return 0;
4212 }
4213
4214 ZVAL_DEREF(method);
4215 if (Z_TYPE_P(method) != IS_STRING) {
4216 if (error) *error = estrdup("second array member is not a valid method");
4217 return 0;
4218 }
4219
4220 if (Z_TYPE_P(obj) == IS_STRING) {
4221 if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4222 return 1;
4223 }
4224
4225 if (!zend_is_callable_check_class(Z_STR_P(obj), get_scope(frame), frame, fcc, &strict_class, error, check_flags & IS_CALLABLE_SUPPRESS_DEPRECATIONS)) {
4226 return 0;
4227 }
4228 } else {
4229 ZEND_ASSERT(Z_TYPE_P(obj) == IS_OBJECT);
4230 fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */
4231 fcc->object = Z_OBJ_P(obj);
4232
4233 if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
4234 fcc->called_scope = fcc->calling_scope;
4235 return 1;
4236 }
4237 }
4238
4239 callable = method;
4240 goto check_func;
4241 }
4242 return 0;
4243 case IS_OBJECT:
4244 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) {
4245 fcc->called_scope = fcc->calling_scope;
4246 fcc->closure = Z_OBJ_P(callable);
4247 if (fcc == &fcc_local) {
4248 zend_release_fcall_info_cache(fcc);
4249 }
4250 return 1;
4251 }
4252 if (error) *error = estrdup("no array or string given");
4253 return 0;
4254 case IS_REFERENCE:
4255 callable = Z_REFVAL_P(callable);
4256 goto again;
4257 default:
4258 if (error) *error = estrdup("no array or string given");
4259 return 0;
4260 }
4261 }
4262 /* }}} */
4263
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 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) /* {{{ */
4265 {
4266 /* Determine callability at the first parent user frame. */
4267 zend_execute_data *frame = EG(current_execute_data);
4268 while (frame && (!frame->func || !ZEND_USER_CODE(frame->func->type))) {
4269 frame = frame->prev_execute_data;
4270 }
4271
4272 bool ret = zend_is_callable_at_frame(callable, object, frame, check_flags, fcc, error);
4273 if (callable_name) {
4274 *callable_name = zend_get_callable_name_ex(callable, object);
4275 }
4276 return ret;
4277 }
4278
zend_is_callable(zval * callable,uint32_t check_flags,zend_string ** callable_name)4279 ZEND_API bool zend_is_callable(zval *callable, uint32_t check_flags, zend_string **callable_name) /* {{{ */
4280 {
4281 return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL);
4282 }
4283 /* }}} */
4284
zend_make_callable(zval * callable,zend_string ** callable_name)4285 ZEND_API bool zend_make_callable(zval *callable, zend_string **callable_name) /* {{{ */
4286 {
4287 zend_fcall_info_cache fcc;
4288
4289 if (zend_is_callable_ex(callable, NULL, IS_CALLABLE_SUPPRESS_DEPRECATIONS, callable_name, &fcc, NULL)) {
4290 if (Z_TYPE_P(callable) == IS_STRING && fcc.calling_scope) {
4291 zval_ptr_dtor_str(callable);
4292 array_init(callable);
4293 add_next_index_str(callable, zend_string_copy(fcc.calling_scope->name));
4294 add_next_index_str(callable, zend_string_copy(fcc.function_handler->common.function_name));
4295 }
4296 zend_release_fcall_info_cache(&fcc);
4297 return 1;
4298 }
4299 return 0;
4300 }
4301 /* }}} */
4302
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 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) /* {{{ */
4304 {
4305 if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, fcc, error)) {
4306 return FAILURE;
4307 }
4308
4309 fci->size = sizeof(*fci);
4310 fci->object = fcc->object;
4311 ZVAL_COPY_VALUE(&fci->function_name, callable);
4312 fci->retval = NULL;
4313 fci->param_count = 0;
4314 fci->params = NULL;
4315 fci->named_params = NULL;
4316
4317 return SUCCESS;
4318 }
4319 /* }}} */
4320
zend_fcall_info_args_clear(zend_fcall_info * fci,bool free_mem)4321 ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, bool free_mem) /* {{{ */
4322 {
4323 if (fci->params) {
4324 zval *p = fci->params;
4325 zval *end = p + fci->param_count;
4326
4327 while (p != end) {
4328 i_zval_ptr_dtor(p);
4329 p++;
4330 }
4331 if (free_mem) {
4332 efree(fci->params);
4333 fci->params = NULL;
4334 }
4335 }
4336 fci->param_count = 0;
4337 }
4338 /* }}} */
4339
zend_fcall_info_args_save(zend_fcall_info * fci,uint32_t * param_count,zval ** params)4340 ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, uint32_t *param_count, zval **params) /* {{{ */
4341 {
4342 *param_count = fci->param_count;
4343 *params = fci->params;
4344 fci->param_count = 0;
4345 fci->params = NULL;
4346 }
4347 /* }}} */
4348
zend_fcall_info_args_restore(zend_fcall_info * fci,uint32_t param_count,zval * params)4349 ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, uint32_t param_count, zval *params) /* {{{ */
4350 {
4351 zend_fcall_info_args_clear(fci, 1);
4352 fci->param_count = param_count;
4353 fci->params = params;
4354 }
4355 /* }}} */
4356
zend_fcall_info_args_ex(zend_fcall_info * fci,zend_function * func,zval * args)4357 ZEND_API zend_result zend_fcall_info_args_ex(zend_fcall_info *fci, zend_function *func, zval *args) /* {{{ */
4358 {
4359 zval *arg, *params;
4360 uint32_t n = 1;
4361
4362 zend_fcall_info_args_clear(fci, !args);
4363
4364 if (!args) {
4365 return SUCCESS;
4366 }
4367
4368 if (Z_TYPE_P(args) != IS_ARRAY) {
4369 return FAILURE;
4370 }
4371
4372 fci->param_count = zend_hash_num_elements(Z_ARRVAL_P(args));
4373 fci->params = params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4374
4375 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), arg) {
4376 if (func && !Z_ISREF_P(arg) && ARG_SHOULD_BE_SENT_BY_REF(func, n)) {
4377 ZVAL_NEW_REF(params, arg);
4378 Z_TRY_ADDREF_P(arg);
4379 } else {
4380 ZVAL_COPY(params, arg);
4381 }
4382 params++;
4383 n++;
4384 } ZEND_HASH_FOREACH_END();
4385
4386 return SUCCESS;
4387 }
4388 /* }}} */
4389
zend_fcall_info_args(zend_fcall_info * fci,zval * args)4390 ZEND_API zend_result zend_fcall_info_args(zend_fcall_info *fci, zval *args) /* {{{ */
4391 {
4392 return zend_fcall_info_args_ex(fci, NULL, args);
4393 }
4394 /* }}} */
4395
zend_fcall_info_argp(zend_fcall_info * fci,uint32_t argc,zval * argv)4396 ZEND_API void zend_fcall_info_argp(zend_fcall_info *fci, uint32_t argc, zval *argv) /* {{{ */
4397 {
4398 zend_fcall_info_args_clear(fci, !argc);
4399
4400 if (argc) {
4401 fci->param_count = argc;
4402 fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4403
4404 for (uint32_t i = 0; i < argc; ++i) {
4405 ZVAL_COPY(&fci->params[i], &argv[i]);
4406 }
4407 }
4408 }
4409 /* }}} */
4410
zend_fcall_info_argv(zend_fcall_info * fci,uint32_t argc,va_list * argv)4411 ZEND_API void zend_fcall_info_argv(zend_fcall_info *fci, uint32_t argc, va_list *argv) /* {{{ */
4412 {
4413 zend_fcall_info_args_clear(fci, !argc);
4414
4415 if (argc) {
4416 zval *arg;
4417 fci->param_count = argc;
4418 fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
4419
4420 for (uint32_t i = 0; i < argc; ++i) {
4421 arg = va_arg(*argv, zval *);
4422 ZVAL_COPY(&fci->params[i], arg);
4423 }
4424 }
4425 }
4426 /* }}} */
4427
zend_fcall_info_argn(zend_fcall_info * fci,uint32_t argc,...)4428 ZEND_API void zend_fcall_info_argn(zend_fcall_info *fci, uint32_t argc, ...) /* {{{ */
4429 {
4430 va_list argv;
4431
4432 va_start(argv, argc);
4433 zend_fcall_info_argv(fci, argc, &argv);
4434 va_end(argv);
4435 }
4436 /* }}} */
4437
zend_fcall_info_call(zend_fcall_info * fci,zend_fcall_info_cache * fcc,zval * retval_ptr,zval * args)4438 ZEND_API zend_result zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval *retval_ptr, zval *args) /* {{{ */
4439 {
4440 zval retval, *org_params = NULL;
4441 uint32_t org_count = 0;
4442 zend_result result;
4443
4444 fci->retval = retval_ptr ? retval_ptr : &retval;
4445 if (args) {
4446 zend_fcall_info_args_save(fci, &org_count, &org_params);
4447 zend_fcall_info_args(fci, args);
4448 }
4449 result = zend_call_function(fci, fcc);
4450
4451 if (!retval_ptr && Z_TYPE(retval) != IS_UNDEF) {
4452 zval_ptr_dtor(&retval);
4453 }
4454 if (args) {
4455 zend_fcall_info_args_restore(fci, org_count, org_params);
4456 }
4457 return result;
4458 }
4459 /* }}} */
4460
zend_get_callable_zval_from_fcc(const zend_fcall_info_cache * fcc,zval * callable)4461 ZEND_API void zend_get_callable_zval_from_fcc(const zend_fcall_info_cache *fcc, zval *callable)
4462 {
4463 if (fcc->closure) {
4464 ZVAL_OBJ_COPY(callable, fcc->closure);
4465 } else if (fcc->function_handler->common.scope) {
4466 array_init(callable);
4467 if (fcc->object) {
4468 GC_ADDREF(fcc->object);
4469 add_next_index_object(callable, fcc->object);
4470 } else {
4471 add_next_index_str(callable, zend_string_copy(fcc->calling_scope->name));
4472 }
4473 add_next_index_str(callable, zend_string_copy(fcc->function_handler->common.function_name));
4474 } else {
4475 ZVAL_STR_COPY(callable, fcc->function_handler->common.function_name);
4476 }
4477 }
4478
zend_get_module_version(const char * module_name)4479 ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */
4480 {
4481 zend_string *lname;
4482 size_t name_len = strlen(module_name);
4483 zend_module_entry *module;
4484
4485 lname = zend_string_alloc(name_len, 0);
4486 zend_str_tolower_copy(ZSTR_VAL(lname), module_name, name_len);
4487 module = zend_hash_find_ptr(&module_registry, lname);
4488 zend_string_efree(lname);
4489 return module ? module->version : NULL;
4490 }
4491 /* }}} */
4492
is_persistent_class(zend_class_entry * ce)4493 static zend_always_inline bool is_persistent_class(zend_class_entry *ce) {
4494 return (ce->type & ZEND_INTERNAL_CLASS)
4495 && ce->info.internal.module->type == MODULE_PERSISTENT;
4496 }
4497
zend_declare_typed_property(zend_class_entry * ce,zend_string * name,zval * property,int access_type,zend_string * doc_comment,zend_type type)4498 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) /* {{{ */
4499 {
4500 zend_property_info *property_info, *property_info_ptr;
4501
4502 if (ZEND_TYPE_IS_SET(type)) {
4503 ce->ce_flags |= ZEND_ACC_HAS_TYPE_HINTS;
4504
4505 if (access_type & ZEND_ACC_READONLY) {
4506 ce->ce_flags |= ZEND_ACC_HAS_READONLY_PROPS;
4507 }
4508 }
4509
4510 if (ce->type == ZEND_INTERNAL_CLASS) {
4511 property_info = pemalloc(sizeof(zend_property_info), 1);
4512 } else {
4513 property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
4514 if (Z_TYPE_P(property) == IS_CONSTANT_AST) {
4515 ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
4516 if (access_type & ZEND_ACC_STATIC) {
4517 ce->ce_flags |= ZEND_ACC_HAS_AST_STATICS;
4518 } else {
4519 ce->ce_flags |= ZEND_ACC_HAS_AST_PROPERTIES;
4520 }
4521 }
4522 }
4523
4524 if (Z_TYPE_P(property) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(property))) {
4525 zval_make_interned_string(property);
4526 }
4527
4528 if (!(access_type & ZEND_ACC_PPP_MASK)) {
4529 access_type |= ZEND_ACC_PUBLIC;
4530 }
4531 /* Add the protected(set) bit for public readonly properties with no set visibility. */
4532 if ((access_type & (ZEND_ACC_PUBLIC|ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK)) == (ZEND_ACC_PUBLIC|ZEND_ACC_READONLY)) {
4533 access_type |= ZEND_ACC_PROTECTED_SET;
4534 } else if (UNEXPECTED(access_type & ZEND_ACC_PPP_SET_MASK)) {
4535 if (!ZEND_TYPE_IS_SET(type)) {
4536 zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4537 "Property with asymmetric visibility %s::$%s must have type",
4538 ZSTR_VAL(ce->name), ZSTR_VAL(name));
4539 }
4540 uint32_t get_visibility = zend_visibility_to_set_visibility(access_type & ZEND_ACC_PPP_MASK);
4541 uint32_t set_visibility = access_type & ZEND_ACC_PPP_SET_MASK;
4542 if (get_visibility > set_visibility) {
4543 zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4544 "Visibility of property %s::$%s must not be weaker than set visibility",
4545 ZSTR_VAL(ce->name), ZSTR_VAL(name));
4546 }
4547 /* Remove equivalent set visibility. */
4548 if (((access_type & (ZEND_ACC_PUBLIC|ZEND_ACC_PUBLIC_SET)) == (ZEND_ACC_PUBLIC|ZEND_ACC_PUBLIC_SET))
4549 || ((access_type & (ZEND_ACC_PROTECTED|ZEND_ACC_PROTECTED_SET)) == (ZEND_ACC_PROTECTED|ZEND_ACC_PROTECTED_SET))
4550 || ((access_type & (ZEND_ACC_PRIVATE|ZEND_ACC_PRIVATE_SET)) == (ZEND_ACC_PRIVATE|ZEND_ACC_PRIVATE_SET))) {
4551 access_type &= ~ZEND_ACC_PPP_SET_MASK;
4552 }
4553 /* private(set) properties are implicitly final. */
4554 if (access_type & ZEND_ACC_PRIVATE_SET) {
4555 access_type |= ZEND_ACC_FINAL;
4556 }
4557 }
4558
4559 /* Virtual properties have no backing storage, the offset should never be used. However, the
4560 * virtual flag cannot be definitively determined at compile time. Allow using default values
4561 * anyway, and assert after inheritance that the property is not actually virtual. */
4562 if (access_type & ZEND_ACC_VIRTUAL) {
4563 if (Z_TYPE_P(property) == IS_UNDEF) {
4564 property_info->offset = (uint32_t)-1;
4565 goto skip_property_storage;
4566 }
4567 }
4568 if (access_type & ZEND_ACC_STATIC) {
4569 if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
4570 ZEND_ASSERT(property_info_ptr->flags & ZEND_ACC_STATIC);
4571 property_info->offset = property_info_ptr->offset;
4572 zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]);
4573 if (property_info_ptr->doc_comment && property_info_ptr->ce == ce) {
4574 zend_string_release(property_info_ptr->doc_comment);
4575 }
4576 zend_hash_del(&ce->properties_info, name);
4577 } else {
4578 property_info->offset = ce->default_static_members_count++;
4579 ce->default_static_members_table = perealloc(ce->default_static_members_table, sizeof(zval) * ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS);
4580 }
4581 ZVAL_COPY_VALUE(&ce->default_static_members_table[property_info->offset], property);
4582 if (!ZEND_MAP_PTR(ce->static_members_table)) {
4583 if (ce->type == ZEND_INTERNAL_CLASS &&
4584 ce->info.internal.module->type == MODULE_PERSISTENT) {
4585 ZEND_MAP_PTR_NEW(ce->static_members_table);
4586 }
4587 }
4588 } else {
4589 zval *property_default_ptr;
4590 if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
4591 ZEND_ASSERT(!(property_info_ptr->flags & ZEND_ACC_STATIC));
4592 property_info->offset = property_info_ptr->offset;
4593 zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]);
4594 if (property_info_ptr->doc_comment && property_info_ptr->ce == ce) {
4595 zend_string_release_ex(property_info_ptr->doc_comment, 1);
4596 }
4597 zend_hash_del(&ce->properties_info, name);
4598
4599 ZEND_ASSERT(ce->type == ZEND_INTERNAL_CLASS);
4600 ZEND_ASSERT(ce->properties_info_table != NULL);
4601 ce->properties_info_table[OBJ_PROP_TO_NUM(property_info->offset)] = property_info;
4602 } else {
4603 property_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count);
4604 ce->default_properties_count++;
4605 ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
4606
4607 /* For user classes this is handled during linking */
4608 if (ce->type == ZEND_INTERNAL_CLASS) {
4609 ce->properties_info_table = perealloc(ce->properties_info_table, sizeof(zend_property_info *) * ce->default_properties_count, 1);
4610 ce->properties_info_table[ce->default_properties_count - 1] = property_info;
4611 }
4612 }
4613 property_default_ptr = &ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
4614 ZVAL_COPY_VALUE(property_default_ptr, property);
4615 Z_PROP_FLAG_P(property_default_ptr) = Z_ISUNDEF_P(property) ? IS_PROP_UNINIT : 0;
4616 }
4617 skip_property_storage:
4618 if (ce->type & ZEND_INTERNAL_CLASS) {
4619 /* Must be interned to avoid ZTS data races */
4620 if (is_persistent_class(ce)) {
4621 name = zend_new_interned_string(zend_string_copy(name));
4622 }
4623
4624 if (Z_REFCOUNTED_P(property)) {
4625 zend_error_noreturn(E_CORE_ERROR, "Internal zvals cannot be refcounted");
4626 }
4627 }
4628
4629 if (access_type & ZEND_ACC_PUBLIC) {
4630 property_info->name = zend_string_copy(name);
4631 } else if (access_type & ZEND_ACC_PRIVATE) {
4632 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));
4633 } else {
4634 ZEND_ASSERT(access_type & ZEND_ACC_PROTECTED);
4635 property_info->name = zend_mangle_property_name("*", 1, ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
4636 }
4637
4638 property_info->name = zend_new_interned_string(property_info->name);
4639 property_info->flags = access_type;
4640 property_info->doc_comment = doc_comment;
4641 property_info->attributes = NULL;
4642 property_info->prototype = property_info;
4643 property_info->hooks = NULL;
4644 property_info->ce = ce;
4645 property_info->type = type;
4646
4647 if (is_persistent_class(ce)) {
4648 zend_normalize_internal_type(&property_info->type);
4649 }
4650
4651 zend_hash_update_ptr(&ce->properties_info, name, property_info);
4652
4653 return property_info;
4654 }
4655 /* }}} */
4656
zend_try_assign_typed_ref_ex(zend_reference * ref,zval * val,bool strict)4657 ZEND_API zend_result zend_try_assign_typed_ref_ex(zend_reference *ref, zval *val, bool strict) /* {{{ */
4658 {
4659 if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, val, strict))) {
4660 zval_ptr_dtor(val);
4661 return FAILURE;
4662 } else {
4663 zval_ptr_dtor(&ref->val);
4664 ZVAL_COPY_VALUE(&ref->val, val);
4665 return SUCCESS;
4666 }
4667 }
4668 /* }}} */
4669
zend_try_assign_typed_ref(zend_reference * ref,zval * val)4670 ZEND_API zend_result zend_try_assign_typed_ref(zend_reference *ref, zval *val) /* {{{ */
4671 {
4672 return zend_try_assign_typed_ref_ex(ref, val, ZEND_ARG_USES_STRICT_TYPES());
4673 }
4674 /* }}} */
4675
zend_try_assign_typed_ref_null(zend_reference * ref)4676 ZEND_API zend_result zend_try_assign_typed_ref_null(zend_reference *ref) /* {{{ */
4677 {
4678 zval tmp;
4679
4680 ZVAL_NULL(&tmp);
4681 return zend_try_assign_typed_ref(ref, &tmp);
4682 }
4683 /* }}} */
4684
zend_try_assign_typed_ref_bool(zend_reference * ref,bool val)4685 ZEND_API zend_result zend_try_assign_typed_ref_bool(zend_reference *ref, bool val) /* {{{ */
4686 {
4687 zval tmp;
4688
4689 ZVAL_BOOL(&tmp, val);
4690 return zend_try_assign_typed_ref(ref, &tmp);
4691 }
4692 /* }}} */
4693
zend_try_assign_typed_ref_long(zend_reference * ref,zend_long lval)4694 ZEND_API zend_result zend_try_assign_typed_ref_long(zend_reference *ref, zend_long lval) /* {{{ */
4695 {
4696 zval tmp;
4697
4698 ZVAL_LONG(&tmp, lval);
4699 return zend_try_assign_typed_ref(ref, &tmp);
4700 }
4701 /* }}} */
4702
zend_try_assign_typed_ref_double(zend_reference * ref,double dval)4703 ZEND_API zend_result zend_try_assign_typed_ref_double(zend_reference *ref, double dval) /* {{{ */
4704 {
4705 zval tmp;
4706
4707 ZVAL_DOUBLE(&tmp, dval);
4708 return zend_try_assign_typed_ref(ref, &tmp);
4709 }
4710 /* }}} */
4711
zend_try_assign_typed_ref_empty_string(zend_reference * ref)4712 ZEND_API zend_result zend_try_assign_typed_ref_empty_string(zend_reference *ref) /* {{{ */
4713 {
4714 zval tmp;
4715
4716 ZVAL_EMPTY_STRING(&tmp);
4717 return zend_try_assign_typed_ref(ref, &tmp);
4718 }
4719 /* }}} */
4720
zend_try_assign_typed_ref_str(zend_reference * ref,zend_string * str)4721 ZEND_API zend_result zend_try_assign_typed_ref_str(zend_reference *ref, zend_string *str) /* {{{ */
4722 {
4723 zval tmp;
4724
4725 ZVAL_STR(&tmp, str);
4726 return zend_try_assign_typed_ref(ref, &tmp);
4727 }
4728 /* }}} */
4729
zend_try_assign_typed_ref_string(zend_reference * ref,const char * string)4730 ZEND_API zend_result zend_try_assign_typed_ref_string(zend_reference *ref, const char *string) /* {{{ */
4731 {
4732 zval tmp;
4733
4734 ZVAL_STRING(&tmp, string);
4735 return zend_try_assign_typed_ref(ref, &tmp);
4736 }
4737 /* }}} */
4738
zend_try_assign_typed_ref_stringl(zend_reference * ref,const char * string,size_t len)4739 ZEND_API zend_result zend_try_assign_typed_ref_stringl(zend_reference *ref, const char *string, size_t len) /* {{{ */
4740 {
4741 zval tmp;
4742
4743 ZVAL_STRINGL(&tmp, string, len);
4744 return zend_try_assign_typed_ref(ref, &tmp);
4745 }
4746 /* }}} */
4747
zend_try_assign_typed_ref_arr(zend_reference * ref,zend_array * arr)4748 ZEND_API zend_result zend_try_assign_typed_ref_arr(zend_reference *ref, zend_array *arr) /* {{{ */
4749 {
4750 zval tmp;
4751
4752 ZVAL_ARR(&tmp, arr);
4753 return zend_try_assign_typed_ref(ref, &tmp);
4754 }
4755 /* }}} */
4756
zend_try_assign_typed_ref_res(zend_reference * ref,zend_resource * res)4757 ZEND_API zend_result zend_try_assign_typed_ref_res(zend_reference *ref, zend_resource *res) /* {{{ */
4758 {
4759 zval tmp;
4760
4761 ZVAL_RES(&tmp, res);
4762 return zend_try_assign_typed_ref(ref, &tmp);
4763 }
4764 /* }}} */
4765
zend_try_assign_typed_ref_zval(zend_reference * ref,zval * zv)4766 ZEND_API zend_result zend_try_assign_typed_ref_zval(zend_reference *ref, zval *zv) /* {{{ */
4767 {
4768 zval tmp;
4769
4770 ZVAL_COPY_VALUE(&tmp, zv);
4771 return zend_try_assign_typed_ref(ref, &tmp);
4772 }
4773 /* }}} */
4774
zend_try_assign_typed_ref_zval_ex(zend_reference * ref,zval * zv,bool strict)4775 ZEND_API zend_result zend_try_assign_typed_ref_zval_ex(zend_reference *ref, zval *zv, bool strict) /* {{{ */
4776 {
4777 zval tmp;
4778
4779 ZVAL_COPY_VALUE(&tmp, zv);
4780 return zend_try_assign_typed_ref_ex(ref, &tmp, strict);
4781 }
4782 /* }}} */
4783
zend_declare_property_ex(zend_class_entry * ce,zend_string * name,zval * property,int access_type,zend_string * doc_comment)4784 ZEND_API void zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */
4785 {
4786 zend_declare_typed_property(ce, name, property, access_type, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
4787 }
4788 /* }}} */
4789
zend_declare_property(zend_class_entry * ce,const char * name,size_t name_length,zval * property,int access_type)4790 ZEND_API void zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type) /* {{{ */
4791 {
4792 zend_string *key = zend_string_init(name, name_length, is_persistent_class(ce));
4793 zend_declare_property_ex(ce, key, property, access_type, NULL);
4794 zend_string_release(key);
4795 }
4796 /* }}} */
4797
zend_declare_property_null(zend_class_entry * ce,const char * name,size_t name_length,int access_type)4798 ZEND_API void zend_declare_property_null(zend_class_entry *ce, const char *name, size_t name_length, int access_type) /* {{{ */
4799 {
4800 zval property;
4801
4802 ZVAL_NULL(&property);
4803 zend_declare_property(ce, name, name_length, &property, access_type);
4804 }
4805 /* }}} */
4806
zend_declare_property_bool(zend_class_entry * ce,const char * name,size_t name_length,zend_long value,int access_type)4807 ZEND_API void zend_declare_property_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
4808 {
4809 zval property;
4810
4811 ZVAL_BOOL(&property, value);
4812 zend_declare_property(ce, name, name_length, &property, access_type);
4813 }
4814 /* }}} */
4815
zend_declare_property_long(zend_class_entry * ce,const char * name,size_t name_length,zend_long value,int access_type)4816 ZEND_API void zend_declare_property_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
4817 {
4818 zval property;
4819
4820 ZVAL_LONG(&property, value);
4821 zend_declare_property(ce, name, name_length, &property, access_type);
4822 }
4823 /* }}} */
4824
zend_declare_property_double(zend_class_entry * ce,const char * name,size_t name_length,double value,int access_type)4825 ZEND_API void zend_declare_property_double(zend_class_entry *ce, const char *name, size_t name_length, double value, int access_type) /* {{{ */
4826 {
4827 zval property;
4828
4829 ZVAL_DOUBLE(&property, value);
4830 zend_declare_property(ce, name, name_length, &property, access_type);
4831 }
4832 /* }}} */
4833
zend_declare_property_string(zend_class_entry * ce,const char * name,size_t name_length,const char * value,int access_type)4834 ZEND_API void zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type) /* {{{ */
4835 {
4836 zval property;
4837
4838 ZVAL_NEW_STR(&property, zend_string_init(value, strlen(value), ce->type & ZEND_INTERNAL_CLASS));
4839 zend_declare_property(ce, name, name_length, &property, access_type);
4840 }
4841 /* }}} */
4842
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 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) /* {{{ */
4844 {
4845 zval property;
4846
4847 ZVAL_NEW_STR(&property, zend_string_init(value, value_len, ce->type & ZEND_INTERNAL_CLASS));
4848 zend_declare_property(ce, name, name_length, &property, access_type);
4849 }
4850 /* }}} */
4851
zend_declare_typed_class_constant(zend_class_entry * ce,zend_string * name,zval * value,int flags,zend_string * doc_comment,zend_type type)4852 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) /* {{{ */
4853 {
4854 zend_class_constant *c;
4855
4856 if (ce->ce_flags & ZEND_ACC_INTERFACE) {
4857 if (!(flags & ZEND_ACC_PUBLIC)) {
4858 zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface constant %s::%s must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4859 }
4860 }
4861
4862 if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_CLASS))) {
4863 zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4864 "A class constant must not be called 'class'; it is reserved for class name fetching");
4865 }
4866
4867 if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) {
4868 zval_make_interned_string(value);
4869 }
4870
4871 if (ce->type == ZEND_INTERNAL_CLASS) {
4872 c = pemalloc(sizeof(zend_class_constant), 1);
4873 if (ZEND_TYPE_PURE_MASK(type) != MAY_BE_ANY) {
4874 ZEND_ASSERT(!ZEND_TYPE_CONTAINS_CODE(type, IS_RESOURCE) && "resource is not allowed in a zend_type");
4875 }
4876 } else {
4877 c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
4878 }
4879 ZVAL_COPY_VALUE(&c->value, value);
4880 ZEND_CLASS_CONST_FLAGS(c) = flags;
4881 c->doc_comment = doc_comment;
4882 c->attributes = NULL;
4883 c->ce = ce;
4884 c->type = type;
4885
4886 if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
4887 ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
4888 ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
4889 if (ce->type == ZEND_INTERNAL_CLASS && !ZEND_MAP_PTR(ce->mutable_data)) {
4890 ZEND_MAP_PTR_NEW(ce->mutable_data);
4891 }
4892 }
4893
4894 if (!zend_hash_add_ptr(&ce->constants_table, name, c)) {
4895 zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
4896 "Cannot redefine class constant %s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4897 }
4898
4899 return c;
4900 }
4901
zend_declare_class_constant_ex(zend_class_entry * ce,zend_string * name,zval * value,int flags,zend_string * doc_comment)4902 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)
4903 {
4904 return zend_declare_typed_class_constant(ce, name, value, flags, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
4905 }
4906
zend_declare_class_constant(zend_class_entry * ce,const char * name,size_t name_length,zval * value)4907 ZEND_API void zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
4908 {
4909 zend_string *key;
4910
4911 if (ce->type == ZEND_INTERNAL_CLASS) {
4912 key = zend_string_init_interned(name, name_length, 1);
4913 } else {
4914 key = zend_string_init(name, name_length, 0);
4915 }
4916 zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
4917 zend_string_release(key);
4918 }
4919 /* }}} */
4920
zend_declare_class_constant_null(zend_class_entry * ce,const char * name,size_t name_length)4921 ZEND_API void zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length) /* {{{ */
4922 {
4923 zval constant;
4924
4925 ZVAL_NULL(&constant);
4926 zend_declare_class_constant(ce, name, name_length, &constant);
4927 }
4928 /* }}} */
4929
zend_declare_class_constant_long(zend_class_entry * ce,const char * name,size_t name_length,zend_long value)4930 ZEND_API void zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value) /* {{{ */
4931 {
4932 zval constant;
4933
4934 ZVAL_LONG(&constant, value);
4935 zend_declare_class_constant(ce, name, name_length, &constant);
4936 }
4937 /* }}} */
4938
zend_declare_class_constant_bool(zend_class_entry * ce,const char * name,size_t name_length,bool value)4939 ZEND_API void zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, bool value) /* {{{ */
4940 {
4941 zval constant;
4942
4943 ZVAL_BOOL(&constant, value);
4944 zend_declare_class_constant(ce, name, name_length, &constant);
4945 }
4946 /* }}} */
4947
zend_declare_class_constant_double(zend_class_entry * ce,const char * name,size_t name_length,double value)4948 ZEND_API void zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value) /* {{{ */
4949 {
4950 zval constant;
4951
4952 ZVAL_DOUBLE(&constant, value);
4953 zend_declare_class_constant(ce, name, name_length, &constant);
4954 }
4955 /* }}} */
4956
zend_declare_class_constant_stringl(zend_class_entry * ce,const char * name,size_t name_length,const char * value,size_t value_length)4957 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) /* {{{ */
4958 {
4959 zval constant;
4960
4961 ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type & ZEND_INTERNAL_CLASS));
4962 zend_declare_class_constant(ce, name, name_length, &constant);
4963 }
4964 /* }}} */
4965
zend_declare_class_constant_string(zend_class_entry * ce,const char * name,size_t name_length,const char * value)4966 ZEND_API void zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value) /* {{{ */
4967 {
4968 zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value));
4969 }
4970 /* }}} */
4971
zend_update_property_ex(zend_class_entry * scope,zend_object * object,zend_string * name,zval * value)4972 ZEND_API void zend_update_property_ex(zend_class_entry *scope, zend_object *object, zend_string *name, zval *value) /* {{{ */
4973 {
4974 zend_class_entry *old_scope = EG(fake_scope);
4975
4976 EG(fake_scope) = scope;
4977
4978 object->handlers->write_property(object, name, value, NULL);
4979
4980 EG(fake_scope) = old_scope;
4981 }
4982 /* }}} */
4983
zend_update_property(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,zval * value)4984 ZEND_API void zend_update_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zval *value) /* {{{ */
4985 {
4986 zend_string *property;
4987 zend_class_entry *old_scope = EG(fake_scope);
4988
4989 EG(fake_scope) = scope;
4990
4991 property = zend_string_init(name, name_length, 0);
4992 object->handlers->write_property(object, property, value, NULL);
4993 zend_string_release_ex(property, 0);
4994
4995 EG(fake_scope) = old_scope;
4996 }
4997 /* }}} */
4998
zend_update_property_null(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length)4999 ZEND_API void zend_update_property_null(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */
5000 {
5001 zval tmp;
5002
5003 ZVAL_NULL(&tmp);
5004 zend_update_property(scope, object, name, name_length, &tmp);
5005 }
5006 /* }}} */
5007
zend_unset_property(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length)5008 ZEND_API void zend_unset_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */
5009 {
5010 zend_string *property;
5011 zend_class_entry *old_scope = EG(fake_scope);
5012
5013 EG(fake_scope) = scope;
5014
5015 property = zend_string_init(name, name_length, 0);
5016 object->handlers->unset_property(object, property, 0);
5017 zend_string_release_ex(property, 0);
5018
5019 EG(fake_scope) = old_scope;
5020 }
5021 /* }}} */
5022
zend_update_property_bool(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,zend_long value)5023 ZEND_API void zend_update_property_bool(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value) /* {{{ */
5024 {
5025 zval tmp;
5026
5027 ZVAL_BOOL(&tmp, value);
5028 zend_update_property(scope, object, name, name_length, &tmp);
5029 }
5030 /* }}} */
5031
zend_update_property_long(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,zend_long value)5032 ZEND_API void zend_update_property_long(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value) /* {{{ */
5033 {
5034 zval tmp;
5035
5036 ZVAL_LONG(&tmp, value);
5037 zend_update_property(scope, object, name, name_length, &tmp);
5038 }
5039 /* }}} */
5040
zend_update_property_double(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,double value)5041 ZEND_API void zend_update_property_double(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, double value) /* {{{ */
5042 {
5043 zval tmp;
5044
5045 ZVAL_DOUBLE(&tmp, value);
5046 zend_update_property(scope, object, name, name_length, &tmp);
5047 }
5048 /* }}} */
5049
zend_update_property_str(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,zend_string * value)5050 ZEND_API void zend_update_property_str(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_string *value) /* {{{ */
5051 {
5052 zval tmp;
5053
5054 ZVAL_STR(&tmp, value);
5055 zend_update_property(scope, object, name, name_length, &tmp);
5056 }
5057 /* }}} */
5058
zend_update_property_string(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,const char * value)5059 ZEND_API void zend_update_property_string(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, const char *value) /* {{{ */
5060 {
5061 zval tmp;
5062
5063 ZVAL_STRING(&tmp, value);
5064 Z_SET_REFCOUNT(tmp, 0);
5065 zend_update_property(scope, object, name, name_length, &tmp);
5066 }
5067 /* }}} */
5068
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 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) /* {{{ */
5070 {
5071 zval tmp;
5072
5073 ZVAL_STRINGL(&tmp, value, value_len);
5074 Z_SET_REFCOUNT(tmp, 0);
5075 zend_update_property(scope, object, name, name_length, &tmp);
5076 }
5077 /* }}} */
5078
zend_update_static_property_ex(zend_class_entry * scope,zend_string * name,zval * value)5079 ZEND_API zend_result zend_update_static_property_ex(zend_class_entry *scope, zend_string *name, zval *value) /* {{{ */
5080 {
5081 zval *property, tmp;
5082 zend_property_info *prop_info;
5083 zend_class_entry *old_scope = EG(fake_scope);
5084
5085 if (UNEXPECTED(!(scope->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
5086 if (UNEXPECTED(zend_update_class_constants(scope) != SUCCESS)) {
5087 return FAILURE;
5088 }
5089 }
5090
5091 EG(fake_scope) = scope;
5092 property = zend_std_get_static_property_with_info(scope, name, BP_VAR_W, &prop_info);
5093 EG(fake_scope) = old_scope;
5094
5095 if (!property) {
5096 return FAILURE;
5097 }
5098
5099 ZEND_ASSERT(!Z_ISREF_P(value));
5100 Z_TRY_ADDREF_P(value);
5101 if (ZEND_TYPE_IS_SET(prop_info->type)) {
5102 ZVAL_COPY_VALUE(&tmp, value);
5103 if (!zend_verify_property_type(prop_info, &tmp, /* strict */ 0)) {
5104 Z_TRY_DELREF_P(value);
5105 return FAILURE;
5106 }
5107 value = &tmp;
5108 }
5109
5110 zend_assign_to_variable(property, value, IS_TMP_VAR, /* strict */ 0);
5111 return SUCCESS;
5112 }
5113 /* }}} */
5114
zend_update_static_property(zend_class_entry * scope,const char * name,size_t name_length,zval * value)5115 ZEND_API zend_result zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value) /* {{{ */
5116 {
5117 zend_string *key = zend_string_init(name, name_length, 0);
5118 zend_result retval = zend_update_static_property_ex(scope, key, value);
5119 zend_string_efree(key);
5120 return retval;
5121 }
5122 /* }}} */
5123
zend_update_static_property_null(zend_class_entry * scope,const char * name,size_t name_length)5124 ZEND_API zend_result zend_update_static_property_null(zend_class_entry *scope, const char *name, size_t name_length) /* {{{ */
5125 {
5126 zval tmp;
5127
5128 ZVAL_NULL(&tmp);
5129 return zend_update_static_property(scope, name, name_length, &tmp);
5130 }
5131 /* }}} */
5132
zend_update_static_property_bool(zend_class_entry * scope,const char * name,size_t name_length,zend_long value)5133 ZEND_API zend_result zend_update_static_property_bool(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
5134 {
5135 zval tmp;
5136
5137 ZVAL_BOOL(&tmp, value);
5138 return zend_update_static_property(scope, name, name_length, &tmp);
5139 }
5140 /* }}} */
5141
zend_update_static_property_long(zend_class_entry * scope,const char * name,size_t name_length,zend_long value)5142 ZEND_API zend_result zend_update_static_property_long(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
5143 {
5144 zval tmp;
5145
5146 ZVAL_LONG(&tmp, value);
5147 return zend_update_static_property(scope, name, name_length, &tmp);
5148 }
5149 /* }}} */
5150
zend_update_static_property_double(zend_class_entry * scope,const char * name,size_t name_length,double value)5151 ZEND_API zend_result zend_update_static_property_double(zend_class_entry *scope, const char *name, size_t name_length, double value) /* {{{ */
5152 {
5153 zval tmp;
5154
5155 ZVAL_DOUBLE(&tmp, value);
5156 return zend_update_static_property(scope, name, name_length, &tmp);
5157 }
5158 /* }}} */
5159
zend_update_static_property_string(zend_class_entry * scope,const char * name,size_t name_length,const char * value)5160 ZEND_API zend_result zend_update_static_property_string(zend_class_entry *scope, const char *name, size_t name_length, const char *value) /* {{{ */
5161 {
5162 zval tmp;
5163
5164 ZVAL_STRING(&tmp, value);
5165 Z_SET_REFCOUNT(tmp, 0);
5166 return zend_update_static_property(scope, name, name_length, &tmp);
5167 }
5168 /* }}} */
5169
zend_update_static_property_stringl(zend_class_entry * scope,const char * name,size_t name_length,const char * value,size_t value_len)5170 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) /* {{{ */
5171 {
5172 zval tmp;
5173
5174 ZVAL_STRINGL(&tmp, value, value_len);
5175 Z_SET_REFCOUNT(tmp, 0);
5176 return zend_update_static_property(scope, name, name_length, &tmp);
5177 }
5178 /* }}} */
5179
zend_read_property_ex(zend_class_entry * scope,zend_object * object,zend_string * name,bool silent,zval * rv)5180 ZEND_API zval *zend_read_property_ex(zend_class_entry *scope, zend_object *object, zend_string *name, bool silent, zval *rv) /* {{{ */
5181 {
5182 zval *value;
5183 zend_class_entry *old_scope = EG(fake_scope);
5184
5185 EG(fake_scope) = scope;
5186
5187 value = object->handlers->read_property(object, name, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
5188
5189 EG(fake_scope) = old_scope;
5190 return value;
5191 }
5192 /* }}} */
5193
zend_read_property(zend_class_entry * scope,zend_object * object,const char * name,size_t name_length,bool silent,zval * rv)5194 ZEND_API zval *zend_read_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, bool silent, zval *rv) /* {{{ */
5195 {
5196 zval *value;
5197 zend_string *str;
5198
5199 str = zend_string_init(name, name_length, 0);
5200 value = zend_read_property_ex(scope, object, str, silent, rv);
5201 zend_string_release_ex(str, 0);
5202 return value;
5203 }
5204 /* }}} */
5205
zend_read_static_property_ex(zend_class_entry * scope,zend_string * name,bool silent)5206 ZEND_API zval *zend_read_static_property_ex(zend_class_entry *scope, zend_string *name, bool silent) /* {{{ */
5207 {
5208 zval *property;
5209 zend_class_entry *old_scope = EG(fake_scope);
5210
5211 EG(fake_scope) = scope;
5212 property = zend_std_get_static_property(scope, name, silent ? BP_VAR_IS : BP_VAR_R);
5213 EG(fake_scope) = old_scope;
5214
5215 return property;
5216 }
5217 /* }}} */
5218
zend_read_static_property(zend_class_entry * scope,const char * name,size_t name_length,bool silent)5219 ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, size_t name_length, bool silent) /* {{{ */
5220 {
5221 zend_string *key = zend_string_init(name, name_length, 0);
5222 zval *property = zend_read_static_property_ex(scope, key, silent);
5223 zend_string_efree(key);
5224 return property;
5225 }
5226 /* }}} */
5227
zend_save_error_handling(zend_error_handling * current)5228 ZEND_API void zend_save_error_handling(zend_error_handling *current) /* {{{ */
5229 {
5230 current->handling = EG(error_handling);
5231 current->exception = EG(exception_class);
5232 }
5233 /* }}} */
5234
zend_replace_error_handling(zend_error_handling_t error_handling,zend_class_entry * exception_class,zend_error_handling * current)5235 ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current) /* {{{ */
5236 {
5237 if (current) {
5238 zend_save_error_handling(current);
5239 }
5240 ZEND_ASSERT(error_handling == EH_THROW || exception_class == NULL);
5241 EG(error_handling) = error_handling;
5242 EG(exception_class) = exception_class;
5243 }
5244 /* }}} */
5245
zend_restore_error_handling(zend_error_handling * saved)5246 ZEND_API void zend_restore_error_handling(zend_error_handling *saved) /* {{{ */
5247 {
5248 EG(error_handling) = saved->handling;
5249 EG(exception_class) = saved->exception;
5250 }
5251 /* }}} */
5252
zend_get_object_type_case(const zend_class_entry * ce,bool upper_case)5253 ZEND_API ZEND_COLD const char *zend_get_object_type_case(const zend_class_entry *ce, bool upper_case) /* {{{ */
5254 {
5255 if (ce->ce_flags & ZEND_ACC_TRAIT) {
5256 return upper_case ? "Trait" : "trait";
5257 } else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
5258 return upper_case ? "Interface" : "interface";
5259 } else if (ce->ce_flags & ZEND_ACC_ENUM) {
5260 return upper_case ? "Enum" : "enum";
5261 } else {
5262 return upper_case ? "Class" : "class";
5263 }
5264 }
5265 /* }}} */
5266
zend_is_iterable(const zval * iterable)5267 ZEND_API bool zend_is_iterable(const zval *iterable) /* {{{ */
5268 {
5269 switch (Z_TYPE_P(iterable)) {
5270 case IS_ARRAY:
5271 return 1;
5272 case IS_OBJECT:
5273 return zend_class_implements_interface(Z_OBJCE_P(iterable), zend_ce_traversable);
5274 default:
5275 return 0;
5276 }
5277 }
5278 /* }}} */
5279
zend_is_countable(const zval * countable)5280 ZEND_API bool zend_is_countable(const zval *countable) /* {{{ */
5281 {
5282 switch (Z_TYPE_P(countable)) {
5283 case IS_ARRAY:
5284 return 1;
5285 case IS_OBJECT:
5286 if (Z_OBJ_HT_P(countable)->count_elements) {
5287 return 1;
5288 }
5289
5290 return zend_class_implements_interface(Z_OBJCE_P(countable), zend_ce_countable);
5291 default:
5292 return 0;
5293 }
5294 }
5295 /* }}} */
5296
get_default_via_ast(zval * default_value_zval,const char * default_value)5297 static zend_result get_default_via_ast(zval *default_value_zval, const char *default_value) {
5298 zend_ast *ast;
5299 zend_arena *ast_arena;
5300
5301 zend_string *code = zend_string_concat3(
5302 "<?php ", sizeof("<?php ") - 1, default_value, strlen(default_value), ";", 1);
5303
5304 ast = zend_compile_string_to_ast(code, &ast_arena, ZSTR_EMPTY_ALLOC());
5305 zend_string_release(code);
5306
5307 if (!ast) {
5308 return FAILURE;
5309 }
5310
5311 zend_ast_list *statement_list = zend_ast_get_list(ast);
5312 zend_ast **const_expr_ast_ptr = &statement_list->child[0];
5313
5314 zend_arena *original_ast_arena = CG(ast_arena);
5315 uint32_t original_compiler_options = CG(compiler_options);
5316 zend_file_context original_file_context;
5317 CG(ast_arena) = ast_arena;
5318 /* Disable constant substitution, to make getDefaultValueConstant() work. */
5319 CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
5320 zend_file_context_begin(&original_file_context);
5321 zend_const_expr_to_zval(default_value_zval, const_expr_ast_ptr, /* allow_dynamic */ true);
5322 CG(ast_arena) = original_ast_arena;
5323 CG(compiler_options) = original_compiler_options;
5324 zend_file_context_end(&original_file_context);
5325
5326 zend_ast_destroy(ast);
5327 zend_arena_destroy(ast_arena);
5328
5329 return SUCCESS;
5330 }
5331
try_parse_string(const char * str,size_t len,char quote)5332 static zend_string *try_parse_string(const char *str, size_t len, char quote) {
5333 if (len == 0) {
5334 return ZSTR_EMPTY_ALLOC();
5335 }
5336
5337 for (size_t i = 0; i < len; i++) {
5338 if (str[i] == '\\' || str[i] == quote) {
5339 return NULL;
5340 }
5341 }
5342 return zend_string_init(str, len, 0);
5343 }
5344
zend_get_default_from_internal_arg_info(zval * default_value_zval,zend_internal_arg_info * arg_info)5345 ZEND_API zend_result zend_get_default_from_internal_arg_info(
5346 zval *default_value_zval, zend_internal_arg_info *arg_info)
5347 {
5348 const char *default_value = arg_info->default_value;
5349 if (!default_value) {
5350 return FAILURE;
5351 }
5352
5353 /* Avoid going through the full AST machinery for some simple and common cases. */
5354 size_t default_value_len = strlen(default_value);
5355 zend_ulong lval;
5356 if (default_value_len == sizeof("null")-1
5357 && !memcmp(default_value, "null", sizeof("null")-1)) {
5358 ZVAL_NULL(default_value_zval);
5359 return SUCCESS;
5360 } else if (default_value_len == sizeof("true")-1
5361 && !memcmp(default_value, "true", sizeof("true")-1)) {
5362 ZVAL_TRUE(default_value_zval);
5363 return SUCCESS;
5364 } else if (default_value_len == sizeof("false")-1
5365 && !memcmp(default_value, "false", sizeof("false")-1)) {
5366 ZVAL_FALSE(default_value_zval);
5367 return SUCCESS;
5368 } else if (default_value_len >= 2
5369 && (default_value[0] == '\'' || default_value[0] == '"')
5370 && default_value[default_value_len - 1] == default_value[0]) {
5371 zend_string *str = try_parse_string(
5372 default_value + 1, default_value_len - 2, default_value[0]);
5373 if (str) {
5374 ZVAL_STR(default_value_zval, str);
5375 return SUCCESS;
5376 }
5377 } else if (default_value_len == sizeof("[]")-1
5378 && !memcmp(default_value, "[]", sizeof("[]")-1)) {
5379 ZVAL_EMPTY_ARRAY(default_value_zval);
5380 return SUCCESS;
5381 } else if (ZEND_HANDLE_NUMERIC_STR(default_value, default_value_len, lval)) {
5382 ZVAL_LONG(default_value_zval, lval);
5383 return SUCCESS;
5384 }
5385
5386 #if 0
5387 fprintf(stderr, "Evaluating %s via AST\n", default_value);
5388 #endif
5389 return get_default_via_ast(default_value_zval, default_value);
5390 }
5391