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