xref: /PHP-8.0/Zend/zend_object_handlers.c (revision ebd3a210)
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    |          Dmitry Stogov <dmitry@php.net>                              |
18    +----------------------------------------------------------------------+
19 */
20 
21 #include "zend.h"
22 #include "zend_globals.h"
23 #include "zend_variables.h"
24 #include "zend_API.h"
25 #include "zend_objects.h"
26 #include "zend_objects_API.h"
27 #include "zend_object_handlers.h"
28 #include "zend_interfaces.h"
29 #include "zend_exceptions.h"
30 #include "zend_closures.h"
31 #include "zend_compile.h"
32 #include "zend_hash.h"
33 
34 #define DEBUG_OBJECT_HANDLERS 0
35 
36 #define ZEND_WRONG_PROPERTY_OFFSET   0
37 
38 /* guard flags */
39 #define IN_GET		(1<<0)
40 #define IN_SET		(1<<1)
41 #define IN_UNSET	(1<<2)
42 #define IN_ISSET	(1<<3)
43 
44 /*
45   __X accessors explanation:
46 
47   if we have __get and property that is not part of the properties array is
48   requested, we call __get handler. If it fails, we return uninitialized.
49 
50   if we have __set and property that is not part of the properties array is
51   set, we call __set handler. If it fails, we do not change the array.
52 
53   for both handlers above, when we are inside __get/__set, no further calls for
54   __get/__set for this property of this object will be made, to prevent endless
55   recursion and enable accessors to change properties array.
56 
57   if we have __call and method which is not part of the class function table is
58   called, we cal __call handler.
59 */
60 
rebuild_object_properties(zend_object * zobj)61 ZEND_API void rebuild_object_properties(zend_object *zobj) /* {{{ */
62 {
63 	if (!zobj->properties) {
64 		zend_property_info *prop_info;
65 		zend_class_entry *ce = zobj->ce;
66 		uint32_t flags = 0;
67 
68 		zobj->properties = zend_new_array(ce->default_properties_count);
69 		if (ce->default_properties_count) {
70 			zend_hash_real_init_mixed(zobj->properties);
71 			ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop_info) {
72 				if (!(prop_info->flags & ZEND_ACC_STATIC)) {
73 					flags |= prop_info->flags;
74 
75 					if (UNEXPECTED(Z_TYPE_P(OBJ_PROP(zobj, prop_info->offset)) == IS_UNDEF)) {
76 						HT_FLAGS(zobj->properties) |= HASH_FLAG_HAS_EMPTY_IND;
77 					}
78 
79 					_zend_hash_append_ind(zobj->properties, prop_info->name,
80 						OBJ_PROP(zobj, prop_info->offset));
81 				}
82 			} ZEND_HASH_FOREACH_END();
83 			if (flags & ZEND_ACC_CHANGED) {
84 				while (ce->parent && ce->parent->default_properties_count) {
85 					ce = ce->parent;
86 					ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop_info) {
87 						if (prop_info->ce == ce &&
88 						    !(prop_info->flags & ZEND_ACC_STATIC) &&
89 						    (prop_info->flags & ZEND_ACC_PRIVATE)) {
90 							zval zv;
91 
92 							if (UNEXPECTED(Z_TYPE_P(OBJ_PROP(zobj, prop_info->offset)) == IS_UNDEF)) {
93 								HT_FLAGS(zobj->properties) |= HASH_FLAG_HAS_EMPTY_IND;
94 							}
95 
96 							ZVAL_INDIRECT(&zv, OBJ_PROP(zobj, prop_info->offset));
97 							zend_hash_add(zobj->properties, prop_info->name, &zv);
98 						}
99 					} ZEND_HASH_FOREACH_END();
100 				}
101 			}
102 		}
103 	}
104 }
105 /* }}} */
106 
zend_std_get_properties(zend_object * zobj)107 ZEND_API HashTable *zend_std_get_properties(zend_object *zobj) /* {{{ */
108 {
109 	if (!zobj->properties) {
110 		rebuild_object_properties(zobj);
111 	}
112 	return zobj->properties;
113 }
114 /* }}} */
115 
zend_std_get_gc(zend_object * zobj,zval ** table,int * n)116 ZEND_API HashTable *zend_std_get_gc(zend_object *zobj, zval **table, int *n) /* {{{ */
117 {
118 	if (zobj->handlers->get_properties != zend_std_get_properties) {
119 		*table = NULL;
120 		*n = 0;
121 		return zobj->handlers->get_properties(zobj);
122 	} else {
123 		if (zobj->properties) {
124 			*table = NULL;
125 			*n = 0;
126 			if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)
127 			 && EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
128 				GC_DELREF(zobj->properties);
129 				zobj->properties = zend_array_dup(zobj->properties);
130 			}
131 			return zobj->properties;
132 		} else {
133 			*table = zobj->properties_table;
134 			*n = zobj->ce->default_properties_count;
135 			return NULL;
136 		}
137 	}
138 }
139 /* }}} */
140 
zend_std_get_debug_info(zend_object * object,int * is_temp)141 ZEND_API HashTable *zend_std_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
142 {
143 	zend_class_entry *ce = object->ce;
144 	zval retval;
145 	HashTable *ht;
146 
147 	if (!ce->__debugInfo) {
148 		*is_temp = 0;
149 		return object->handlers->get_properties(object);
150 	}
151 
152 	zend_call_known_instance_method_with_0_params(ce->__debugInfo, object, &retval);
153 	if (Z_TYPE(retval) == IS_ARRAY) {
154 		if (!Z_REFCOUNTED(retval)) {
155 			*is_temp = 1;
156 			return zend_array_dup(Z_ARRVAL(retval));
157 		} else if (Z_REFCOUNT(retval) <= 1) {
158 			*is_temp = 1;
159 			ht = Z_ARR(retval);
160 			return ht;
161 		} else {
162 			*is_temp = 0;
163 			zval_ptr_dtor(&retval);
164 			return Z_ARRVAL(retval);
165 		}
166 	} else if (Z_TYPE(retval) == IS_NULL) {
167 		*is_temp = 1;
168 		ht = zend_new_array(0);
169 		return ht;
170 	}
171 
172 	zend_error_noreturn(E_ERROR, ZEND_DEBUGINFO_FUNC_NAME "() must return an array");
173 
174 	return NULL; /* Compilers are dumb and don't understand that noreturn means that the function does NOT need a return value... */
175 }
176 /* }}} */
177 
zend_std_call_getter(zend_object * zobj,zend_string * prop_name,zval * retval)178 static void zend_std_call_getter(zend_object *zobj, zend_string *prop_name, zval *retval) /* {{{ */
179 {
180 	zval member;
181 	ZVAL_STR(&member, prop_name);
182 	zend_call_known_instance_method_with_1_params(zobj->ce->__get, zobj, retval, &member);
183 }
184 /* }}} */
185 
zend_std_call_setter(zend_object * zobj,zend_string * prop_name,zval * value)186 static void zend_std_call_setter(zend_object *zobj, zend_string *prop_name, zval *value) /* {{{ */
187 {
188 	zval args[2];
189 	ZVAL_STR(&args[0], prop_name);
190 	ZVAL_COPY_VALUE(&args[1], value);
191 	zend_call_known_instance_method(zobj->ce->__set, zobj, NULL, 2, args);
192 }
193 /* }}} */
194 
zend_std_call_unsetter(zend_object * zobj,zend_string * prop_name)195 static void zend_std_call_unsetter(zend_object *zobj, zend_string *prop_name) /* {{{ */
196 {
197 	zval member;
198 	ZVAL_STR(&member, prop_name);
199 	zend_call_known_instance_method_with_1_params(zobj->ce->__unset, zobj, NULL, &member);
200 }
201 /* }}} */
202 
zend_std_call_issetter(zend_object * zobj,zend_string * prop_name,zval * retval)203 static void zend_std_call_issetter(zend_object *zobj, zend_string *prop_name, zval *retval) /* {{{ */
204 {
205 	zval member;
206 	ZVAL_STR(&member, prop_name);
207 	zend_call_known_instance_method_with_1_params(zobj->ce->__isset, zobj, retval, &member);
208 }
209 /* }}} */
210 
211 
is_derived_class(zend_class_entry * child_class,zend_class_entry * parent_class)212 static zend_always_inline zend_bool is_derived_class(zend_class_entry *child_class, zend_class_entry *parent_class) /* {{{ */
213 {
214 	child_class = child_class->parent;
215 	while (child_class) {
216 		if (child_class == parent_class) {
217 			return 1;
218 		}
219 		child_class = child_class->parent;
220 	}
221 
222 	return 0;
223 }
224 /* }}} */
225 
is_protected_compatible_scope(zend_class_entry * ce,zend_class_entry * scope)226 static zend_never_inline int is_protected_compatible_scope(zend_class_entry *ce, zend_class_entry *scope) /* {{{ */
227 {
228 	return scope &&
229 		(is_derived_class(ce, scope) || is_derived_class(scope, ce));
230 }
231 /* }}} */
232 
zend_get_parent_private_property(zend_class_entry * scope,zend_class_entry * ce,zend_string * member)233 static zend_never_inline zend_property_info *zend_get_parent_private_property(zend_class_entry *scope, zend_class_entry *ce, zend_string *member) /* {{{ */
234 {
235 	zval *zv;
236 	zend_property_info *prop_info;
237 
238 	if (scope != ce && scope && is_derived_class(ce, scope)) {
239 		zv = zend_hash_find(&scope->properties_info, member);
240 		if (zv != NULL) {
241 			prop_info = (zend_property_info*)Z_PTR_P(zv);
242 			if ((prop_info->flags & ZEND_ACC_PRIVATE)
243 			 && prop_info->ce == scope) {
244 				return prop_info;
245 			}
246 		}
247 	}
248 	return NULL;
249 }
250 /* }}} */
251 
zend_bad_property_access(zend_property_info * property_info,zend_class_entry * ce,zend_string * member)252 static ZEND_COLD zend_never_inline void zend_bad_property_access(zend_property_info *property_info, zend_class_entry *ce, zend_string *member) /* {{{ */
253 {
254 	zend_throw_error(NULL, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ZSTR_VAL(ce->name), ZSTR_VAL(member));
255 }
256 /* }}} */
257 
zend_bad_property_name(void)258 static ZEND_COLD zend_never_inline void zend_bad_property_name(void) /* {{{ */
259 {
260 	zend_throw_error(NULL, "Cannot access property starting with \"\\0\"");
261 }
262 /* }}} */
263 
zend_forbidden_dynamic_property(zend_class_entry * ce,zend_string * member)264 static ZEND_COLD zend_never_inline void zend_forbidden_dynamic_property(
265 		zend_class_entry *ce, zend_string *member) {
266 	zend_throw_error(NULL, "Cannot create dynamic property %s::$%s",
267 		ZSTR_VAL(ce->name), ZSTR_VAL(member));
268 }
269 
zend_get_property_offset(zend_class_entry * ce,zend_string * member,int silent,void ** cache_slot,zend_property_info ** info_ptr)270 static zend_always_inline uintptr_t zend_get_property_offset(zend_class_entry *ce, zend_string *member, int silent, void **cache_slot, zend_property_info **info_ptr) /* {{{ */
271 {
272 	zval *zv;
273 	zend_property_info *property_info;
274 	uint32_t flags;
275 	zend_class_entry *scope;
276 	uintptr_t offset;
277 
278 	if (cache_slot && EXPECTED(ce == CACHED_PTR_EX(cache_slot))) {
279 		*info_ptr = CACHED_PTR_EX(cache_slot + 2);
280 		return (uintptr_t)CACHED_PTR_EX(cache_slot + 1);
281 	}
282 
283 	if (UNEXPECTED(zend_hash_num_elements(&ce->properties_info) == 0)
284 	 || UNEXPECTED((zv = zend_hash_find(&ce->properties_info, member)) == NULL)) {
285 		if (UNEXPECTED(ZSTR_VAL(member)[0] == '\0') && ZSTR_LEN(member) != 0) {
286 			if (!silent) {
287 				zend_bad_property_name();
288 			}
289 			return ZEND_WRONG_PROPERTY_OFFSET;
290 		}
291 dynamic:
292 		if (cache_slot) {
293 			CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET);
294 			CACHE_PTR_EX(cache_slot + 2, NULL);
295 		}
296 		return ZEND_DYNAMIC_PROPERTY_OFFSET;
297 	}
298 
299 	property_info = (zend_property_info*)Z_PTR_P(zv);
300 	flags = property_info->flags;
301 
302 	if (flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
303 		if (UNEXPECTED(EG(fake_scope))) {
304 			scope = EG(fake_scope);
305 		} else {
306 			scope = zend_get_executed_scope();
307 		}
308 
309 		if (property_info->ce != scope) {
310 			if (flags & ZEND_ACC_CHANGED) {
311 				zend_property_info *p = zend_get_parent_private_property(scope, ce, member);
312 
313 				/* If there is a public/protected instance property on ce, don't try to use a
314 				 * private static property on scope. If both are static, prefer the static
315 				 * property on scope. This will throw a static property notice, rather than
316 				 * a visibility error. */
317 				if (p && (!(p->flags & ZEND_ACC_STATIC) || (flags & ZEND_ACC_STATIC))) {
318 					property_info = p;
319 					flags = property_info->flags;
320 					goto found;
321 				} else if (flags & ZEND_ACC_PUBLIC) {
322 					goto found;
323 				}
324 			}
325 			if (flags & ZEND_ACC_PRIVATE) {
326 				if (property_info->ce != ce) {
327 					goto dynamic;
328 				} else {
329 wrong:
330 					/* Information was available, but we were denied access.  Error out. */
331 					if (!silent) {
332 						zend_bad_property_access(property_info, ce, member);
333 					}
334 					return ZEND_WRONG_PROPERTY_OFFSET;
335 				}
336 			} else {
337 				ZEND_ASSERT(flags & ZEND_ACC_PROTECTED);
338 				if (UNEXPECTED(!is_protected_compatible_scope(property_info->ce, scope))) {
339 					goto wrong;
340 				}
341 			}
342 		}
343 	}
344 
345 found:
346 	if (UNEXPECTED(flags & ZEND_ACC_STATIC)) {
347 		if (!silent) {
348 			zend_error(E_NOTICE, "Accessing static property %s::$%s as non static", ZSTR_VAL(ce->name), ZSTR_VAL(member));
349 		}
350 		return ZEND_DYNAMIC_PROPERTY_OFFSET;
351 	}
352 
353 	offset = property_info->offset;
354 	if (EXPECTED(!ZEND_TYPE_IS_SET(property_info->type))) {
355 		property_info = NULL;
356 	} else {
357 		*info_ptr = property_info;
358 	}
359 	if (cache_slot) {
360 		CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)(uintptr_t)offset);
361 		CACHE_PTR_EX(cache_slot + 2, property_info);
362 	}
363 	return offset;
364 }
365 /* }}} */
366 
zend_wrong_offset(zend_class_entry * ce,zend_string * member)367 static ZEND_COLD void zend_wrong_offset(zend_class_entry *ce, zend_string *member) /* {{{ */
368 {
369 	zend_property_info *dummy;
370 
371 	/* Trigger the correct error */
372 	zend_get_property_offset(ce, member, 0, NULL, &dummy);
373 }
374 /* }}} */
375 
zend_get_property_info(zend_class_entry * ce,zend_string * member,int silent)376 ZEND_API zend_property_info *zend_get_property_info(zend_class_entry *ce, zend_string *member, int silent) /* {{{ */
377 {
378 	zval *zv;
379 	zend_property_info *property_info;
380 	uint32_t flags;
381 	zend_class_entry *scope;
382 
383 	if (UNEXPECTED(zend_hash_num_elements(&ce->properties_info) == 0)
384 	 || EXPECTED((zv = zend_hash_find(&ce->properties_info, member)) == NULL)) {
385 		if (UNEXPECTED(ZSTR_VAL(member)[0] == '\0') && ZSTR_LEN(member) != 0) {
386 			if (!silent) {
387 				zend_bad_property_name();
388 			}
389 			return ZEND_WRONG_PROPERTY_INFO;
390 		}
391 dynamic:
392 		return NULL;
393 	}
394 
395 	property_info = (zend_property_info*)Z_PTR_P(zv);
396 	flags = property_info->flags;
397 
398 	if (flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
399 		if (UNEXPECTED(EG(fake_scope))) {
400 			scope = EG(fake_scope);
401 		} else {
402 			scope = zend_get_executed_scope();
403 		}
404 		if (property_info->ce != scope) {
405 			if (flags & ZEND_ACC_CHANGED) {
406 				zend_property_info *p = zend_get_parent_private_property(scope, ce, member);
407 
408 				if (p) {
409 					property_info = p;
410 					flags = property_info->flags;
411 					goto found;
412 				} else if (flags & ZEND_ACC_PUBLIC) {
413 					goto found;
414 				}
415 			}
416 			if (flags & ZEND_ACC_PRIVATE) {
417 				if (property_info->ce != ce) {
418 					goto dynamic;
419 				} else {
420 wrong:
421 					/* Information was available, but we were denied access.  Error out. */
422 					if (!silent) {
423 						zend_bad_property_access(property_info, ce, member);
424 					}
425 					return ZEND_WRONG_PROPERTY_INFO;
426 				}
427 			} else {
428 				ZEND_ASSERT(flags & ZEND_ACC_PROTECTED);
429 				if (UNEXPECTED(!is_protected_compatible_scope(property_info->ce, scope))) {
430 					goto wrong;
431 				}
432 			}
433 		}
434 	}
435 
436 found:
437 	if (UNEXPECTED(flags & ZEND_ACC_STATIC)) {
438 		if (!silent) {
439 			zend_error(E_NOTICE, "Accessing static property %s::$%s as non static", ZSTR_VAL(ce->name), ZSTR_VAL(member));
440 		}
441 	}
442 	return property_info;
443 }
444 /* }}} */
445 
zend_check_property_access(zend_object * zobj,zend_string * prop_info_name,zend_bool is_dynamic)446 ZEND_API int zend_check_property_access(zend_object *zobj, zend_string *prop_info_name, zend_bool is_dynamic) /* {{{ */
447 {
448 	zend_property_info *property_info;
449 	const char *class_name = NULL;
450 	const char *prop_name;
451 	zend_string *member;
452 	size_t prop_name_len;
453 
454 	if (ZSTR_VAL(prop_info_name)[0] == 0) {
455 		if (is_dynamic) {
456 			return SUCCESS;
457 		}
458 
459 		zend_unmangle_property_name_ex(prop_info_name, &class_name, &prop_name, &prop_name_len);
460 		member = zend_string_init(prop_name, prop_name_len, 0);
461 		property_info = zend_get_property_info(zobj->ce, member, 1);
462 		zend_string_release_ex(member, 0);
463 		if (property_info == NULL || property_info == ZEND_WRONG_PROPERTY_INFO) {
464 			return FAILURE;
465 		}
466 
467 		if (class_name[0] != '*') {
468 			if (!(property_info->flags & ZEND_ACC_PRIVATE)) {
469 				/* we we're looking for a private prop but found a non private one of the same name */
470 				return FAILURE;
471 			} else if (strcmp(ZSTR_VAL(prop_info_name)+1, ZSTR_VAL(property_info->name)+1)) {
472 				/* we we're looking for a private prop but found a private one of the same name but another class */
473 				return FAILURE;
474 			}
475 		} else {
476 			ZEND_ASSERT(property_info->flags & ZEND_ACC_PROTECTED);
477 		}
478 		return SUCCESS;
479 	} else {
480 		property_info = zend_get_property_info(zobj->ce, prop_info_name, 1);
481 		if (property_info == NULL) {
482 			ZEND_ASSERT(is_dynamic);
483 			return SUCCESS;
484 		} else if (property_info == ZEND_WRONG_PROPERTY_INFO) {
485 			return FAILURE;
486 		}
487 		return (property_info->flags & ZEND_ACC_PUBLIC) ? SUCCESS : FAILURE;
488 	}
489 }
490 /* }}} */
491 
zend_property_guard_dtor(zval * el)492 static void zend_property_guard_dtor(zval *el) /* {{{ */ {
493 	uint32_t *ptr = (uint32_t*)Z_PTR_P(el);
494 	if (EXPECTED(!(((zend_uintptr_t)ptr) & 1))) {
495 		efree_size(ptr, sizeof(uint32_t));
496 	}
497 }
498 /* }}} */
499 
zend_get_property_guard(zend_object * zobj,zend_string * member)500 ZEND_API uint32_t *zend_get_property_guard(zend_object *zobj, zend_string *member) /* {{{ */
501 {
502 	HashTable *guards;
503 	zval *zv;
504 	uint32_t *ptr;
505 
506 	ZEND_ASSERT(zobj->ce->ce_flags & ZEND_ACC_USE_GUARDS);
507 	zv = zobj->properties_table + zobj->ce->default_properties_count;
508 	if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
509 		zend_string *str = Z_STR_P(zv);
510 		if (EXPECTED(str == member) ||
511 		     /* "str" always has a pre-calculated hash value here */
512 		    (EXPECTED(ZSTR_H(str) == zend_string_hash_val(member)) &&
513 		     EXPECTED(zend_string_equal_content(str, member)))) {
514 			return &Z_PROPERTY_GUARD_P(zv);
515 		} else if (EXPECTED(Z_PROPERTY_GUARD_P(zv) == 0)) {
516 			zval_ptr_dtor_str(zv);
517 			ZVAL_STR_COPY(zv, member);
518 			return &Z_PROPERTY_GUARD_P(zv);
519 		} else {
520 			ALLOC_HASHTABLE(guards);
521 			zend_hash_init(guards, 8, NULL, zend_property_guard_dtor, 0);
522 			/* mark pointer as "special" using low bit */
523 			zend_hash_add_new_ptr(guards, str,
524 				(void*)(((zend_uintptr_t)&Z_PROPERTY_GUARD_P(zv)) | 1));
525 			zval_ptr_dtor_str(zv);
526 			ZVAL_ARR(zv, guards);
527 		}
528 	} else if (EXPECTED(Z_TYPE_P(zv) == IS_ARRAY)) {
529 		guards = Z_ARRVAL_P(zv);
530 		ZEND_ASSERT(guards != NULL);
531 		zv = zend_hash_find(guards, member);
532 		if (zv != NULL) {
533 			return (uint32_t*)(((zend_uintptr_t)Z_PTR_P(zv)) & ~1);
534 		}
535 	} else {
536 		ZEND_ASSERT(Z_TYPE_P(zv) == IS_UNDEF);
537 		ZVAL_STR_COPY(zv, member);
538 		Z_PROPERTY_GUARD_P(zv) = 0;
539 		return &Z_PROPERTY_GUARD_P(zv);
540 	}
541 	/* we have to allocate uint32_t separately because ht->arData may be reallocated */
542 	ptr = (uint32_t*)emalloc(sizeof(uint32_t));
543 	*ptr = 0;
544 	return (uint32_t*)zend_hash_add_new_ptr(guards, member, ptr);
545 }
546 /* }}} */
547 
zend_std_read_property(zend_object * zobj,zend_string * name,int type,void ** cache_slot,zval * rv)548 ZEND_API zval *zend_std_read_property(zend_object *zobj, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */
549 {
550 	zval *retval;
551 	uintptr_t property_offset;
552 	zend_property_info *prop_info = NULL;
553 	uint32_t *guard = NULL;
554 	zend_string *tmp_name = NULL;
555 
556 #if DEBUG_OBJECT_HANDLERS
557 	fprintf(stderr, "Read object #%d property: %s\n", zobj->handle, ZSTR_VAL(name));
558 #endif
559 
560 	/* make zend_get_property_info silent if we have getter - we may want to use it */
561 	property_offset = zend_get_property_offset(zobj->ce, name, (type == BP_VAR_IS) || (zobj->ce->__get != NULL), cache_slot, &prop_info);
562 
563 	if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
564 		retval = OBJ_PROP(zobj, property_offset);
565 		if (EXPECTED(Z_TYPE_P(retval) != IS_UNDEF)) {
566 			goto exit;
567 		}
568 		if (UNEXPECTED(Z_PROP_FLAG_P(retval) == IS_PROP_UNINIT)) {
569 			/* Skip __get() for uninitialized typed properties */
570 			goto uninit_error;
571 		}
572 	} else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
573 		if (EXPECTED(zobj->properties != NULL)) {
574 			if (!IS_UNKNOWN_DYNAMIC_PROPERTY_OFFSET(property_offset)) {
575 				uintptr_t idx = ZEND_DECODE_DYN_PROP_OFFSET(property_offset);
576 
577 				if (EXPECTED(idx < zobj->properties->nNumUsed * sizeof(Bucket))) {
578 					Bucket *p = (Bucket*)((char*)zobj->properties->arData + idx);
579 
580 					if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF) &&
581 				        (EXPECTED(p->key == name) ||
582 				         (EXPECTED(p->h == ZSTR_H(name)) &&
583 				          EXPECTED(p->key != NULL) &&
584 				          EXPECTED(zend_string_equal_content(p->key, name))))) {
585 						retval = &p->val;
586 						goto exit;
587 					}
588 				}
589 				CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET);
590 			}
591 			retval = zend_hash_find(zobj->properties, name);
592 			if (EXPECTED(retval)) {
593 				if (cache_slot) {
594 					uintptr_t idx = (char*)retval - (char*)zobj->properties->arData;
595 					CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_ENCODE_DYN_PROP_OFFSET(idx));
596 				}
597 				goto exit;
598 			}
599 		}
600 	} else if (UNEXPECTED(EG(exception))) {
601 		retval = &EG(uninitialized_zval);
602 		goto exit;
603 	}
604 
605 	/* magic isset */
606 	if ((type == BP_VAR_IS) && zobj->ce->__isset) {
607 		zval tmp_result;
608 		guard = zend_get_property_guard(zobj, name);
609 
610 		if (!((*guard) & IN_ISSET)) {
611 			if (!tmp_name && !ZSTR_IS_INTERNED(name)) {
612 				tmp_name = zend_string_copy(name);
613 			}
614 			GC_ADDREF(zobj);
615 			ZVAL_UNDEF(&tmp_result);
616 
617 			*guard |= IN_ISSET;
618 			zend_std_call_issetter(zobj, name, &tmp_result);
619 			*guard &= ~IN_ISSET;
620 
621 			if (!zend_is_true(&tmp_result)) {
622 				retval = &EG(uninitialized_zval);
623 				OBJ_RELEASE(zobj);
624 				zval_ptr_dtor(&tmp_result);
625 				goto exit;
626 			}
627 
628 			zval_ptr_dtor(&tmp_result);
629 			if (zobj->ce->__get && !((*guard) & IN_GET)) {
630 				goto call_getter;
631 			}
632 			OBJ_RELEASE(zobj);
633 		} else if (zobj->ce->__get && !((*guard) & IN_GET)) {
634 			goto call_getter_addref;
635 		}
636 	} else if (zobj->ce->__get) {
637 		/* magic get */
638 		guard = zend_get_property_guard(zobj, name);
639 		if (!((*guard) & IN_GET)) {
640 			/* have getter - try with it! */
641 call_getter_addref:
642 			GC_ADDREF(zobj);
643 call_getter:
644 			*guard |= IN_GET; /* prevent circular getting */
645 			zend_std_call_getter(zobj, name, rv);
646 			*guard &= ~IN_GET;
647 
648 			if (Z_TYPE_P(rv) != IS_UNDEF) {
649 				retval = rv;
650 				if (!Z_ISREF_P(rv) &&
651 				    (type == BP_VAR_W || type == BP_VAR_RW  || type == BP_VAR_UNSET)) {
652 					if (UNEXPECTED(Z_TYPE_P(rv) != IS_OBJECT)) {
653 						zend_error(E_NOTICE, "Indirect modification of overloaded property %s::$%s has no effect", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
654 					}
655 				}
656 			} else {
657 				retval = &EG(uninitialized_zval);
658 			}
659 
660 			if (UNEXPECTED(prop_info)) {
661 				zend_verify_prop_assignable_by_ref(prop_info, retval, (zobj->ce->__get->common.fn_flags & ZEND_ACC_STRICT_TYPES) != 0);
662 			}
663 
664 			OBJ_RELEASE(zobj);
665 			goto exit;
666 		} else if (UNEXPECTED(IS_WRONG_PROPERTY_OFFSET(property_offset))) {
667 			/* Trigger the correct error */
668 			zend_get_property_offset(zobj->ce, name, 0, NULL, &prop_info);
669 			ZEND_ASSERT(EG(exception));
670 			retval = &EG(uninitialized_zval);
671 			goto exit;
672 		}
673 	}
674 
675 uninit_error:
676 	if (type != BP_VAR_IS) {
677 		if (UNEXPECTED(prop_info)) {
678 			zend_throw_error(NULL, "Typed property %s::$%s must not be accessed before initialization",
679 				ZSTR_VAL(prop_info->ce->name),
680 				ZSTR_VAL(name));
681 		} else {
682 			zend_error(E_WARNING, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
683 		}
684 	}
685 	retval = &EG(uninitialized_zval);
686 
687 exit:
688 	zend_tmp_string_release(tmp_name);
689 
690 	return retval;
691 }
692 /* }}} */
693 
property_uses_strict_types()694 static zend_always_inline zend_bool property_uses_strict_types() {
695 	zend_execute_data *execute_data = EG(current_execute_data);
696 	return execute_data
697 		&& execute_data->func
698 		&& ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data));
699 }
700 
zend_std_write_property(zend_object * zobj,zend_string * name,zval * value,void ** cache_slot)701 ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zval *value, void **cache_slot) /* {{{ */
702 {
703 	zval *variable_ptr, tmp;
704 	uintptr_t property_offset;
705 	zend_property_info *prop_info = NULL;
706 	ZEND_ASSERT(!Z_ISREF_P(value));
707 
708 	property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__set != NULL), cache_slot, &prop_info);
709 
710 	if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
711 		variable_ptr = OBJ_PROP(zobj, property_offset);
712 		if (Z_TYPE_P(variable_ptr) != IS_UNDEF) {
713 			Z_TRY_ADDREF_P(value);
714 
715 			if (UNEXPECTED(prop_info)) {
716 				ZVAL_COPY_VALUE(&tmp, value);
717 				if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, property_uses_strict_types()))) {
718 					Z_TRY_DELREF_P(value);
719 					variable_ptr = &EG(error_zval);
720 					goto exit;
721 				}
722 				value = &tmp;
723 			}
724 
725 found:
726 			variable_ptr = zend_assign_to_variable(
727 				variable_ptr, value, IS_TMP_VAR, property_uses_strict_types());
728 			goto exit;
729 		}
730 		if (Z_PROP_FLAG_P(variable_ptr) == IS_PROP_UNINIT) {
731 			/* Writes to uninitializde typed properties bypass __set(). */
732 			goto write_std_property;
733 		}
734 	} else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
735 		if (EXPECTED(zobj->properties != NULL)) {
736 			if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
737 				if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
738 					GC_DELREF(zobj->properties);
739 				}
740 				zobj->properties = zend_array_dup(zobj->properties);
741 			}
742 			if ((variable_ptr = zend_hash_find(zobj->properties, name)) != NULL) {
743 				Z_TRY_ADDREF_P(value);
744 				goto found;
745 			}
746 		}
747 	} else if (UNEXPECTED(EG(exception))) {
748 		variable_ptr = &EG(error_zval);
749 		goto exit;
750 	}
751 
752 	/* magic set */
753 	if (zobj->ce->__set) {
754 		uint32_t *guard = zend_get_property_guard(zobj, name);
755 
756 		if (!((*guard) & IN_SET)) {
757 			GC_ADDREF(zobj);
758 			(*guard) |= IN_SET; /* prevent circular setting */
759 			zend_std_call_setter(zobj, name, value);
760 			(*guard) &= ~IN_SET;
761 			OBJ_RELEASE(zobj);
762 			variable_ptr = value;
763 		} else if (EXPECTED(!IS_WRONG_PROPERTY_OFFSET(property_offset))) {
764 			goto write_std_property;
765 		} else {
766 			/* Trigger the correct error */
767 			zend_wrong_offset(zobj->ce, name);
768 			ZEND_ASSERT(EG(exception));
769 			variable_ptr = &EG(error_zval);
770 			goto exit;
771 		}
772 	} else {
773 		ZEND_ASSERT(!IS_WRONG_PROPERTY_OFFSET(property_offset));
774 write_std_property:
775 		if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
776 			variable_ptr = OBJ_PROP(zobj, property_offset);
777 
778 			Z_TRY_ADDREF_P(value);
779 			if (UNEXPECTED(prop_info)) {
780 				ZVAL_COPY_VALUE(&tmp, value);
781 				if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, property_uses_strict_types()))) {
782 					zval_ptr_dtor(value);
783 					goto exit;
784 				}
785 				value = &tmp;
786 				Z_PROP_FLAG_P(variable_ptr) = 0;
787 				goto found; /* might have been updated via e.g. __toString() */
788 			}
789 
790 			ZVAL_COPY_VALUE(variable_ptr, value);
791 		} else {
792 			if (UNEXPECTED(zobj->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) {
793 				zend_forbidden_dynamic_property(zobj->ce, name);
794 				variable_ptr = &EG(error_zval);
795 				goto exit;
796 			}
797 
798 			Z_TRY_ADDREF_P(value);
799 			if (!zobj->properties) {
800 				rebuild_object_properties(zobj);
801 			}
802 			variable_ptr = zend_hash_add_new(zobj->properties, name, value);
803 		}
804 	}
805 
806 exit:
807 	return variable_ptr;
808 }
809 /* }}} */
810 
zend_bad_array_access(zend_class_entry * ce)811 static ZEND_COLD zend_never_inline void zend_bad_array_access(zend_class_entry *ce) /* {{{ */
812 {
813 	zend_throw_error(NULL, "Cannot use object of type %s as array", ZSTR_VAL(ce->name));
814 }
815 /* }}} */
816 
zend_std_read_dimension(zend_object * object,zval * offset,int type,zval * rv)817 ZEND_API zval *zend_std_read_dimension(zend_object *object, zval *offset, int type, zval *rv) /* {{{ */
818 {
819 	zend_class_entry *ce = object->ce;
820 	zval tmp_offset;
821 
822 	if (EXPECTED(zend_class_implements_interface(ce, zend_ce_arrayaccess) != 0)) {
823 		if (offset == NULL) {
824 			/* [] construct */
825 			ZVAL_NULL(&tmp_offset);
826 		} else {
827 			ZVAL_COPY_DEREF(&tmp_offset, offset);
828 		}
829 
830 		GC_ADDREF(object);
831 		if (type == BP_VAR_IS) {
832 			zend_call_method_with_1_params(object, ce, NULL, "offsetexists", rv, &tmp_offset);
833 			if (UNEXPECTED(Z_ISUNDEF_P(rv))) {
834 				OBJ_RELEASE(object);
835 				zval_ptr_dtor(&tmp_offset);
836 				return NULL;
837 			}
838 			if (!i_zend_is_true(rv)) {
839 				OBJ_RELEASE(object);
840 				zval_ptr_dtor(&tmp_offset);
841 				zval_ptr_dtor(rv);
842 				return &EG(uninitialized_zval);
843 			}
844 			zval_ptr_dtor(rv);
845 		}
846 
847 		zend_call_method_with_1_params(object, ce, NULL, "offsetget", rv, &tmp_offset);
848 
849 		OBJ_RELEASE(object);
850 		zval_ptr_dtor(&tmp_offset);
851 
852 		if (UNEXPECTED(Z_TYPE_P(rv) == IS_UNDEF)) {
853 			if (UNEXPECTED(!EG(exception))) {
854 				zend_throw_error(NULL, "Undefined offset for object of type %s used as array", ZSTR_VAL(ce->name));
855 			}
856 			return NULL;
857 		}
858 		return rv;
859 	} else {
860 	    zend_bad_array_access(ce);
861 		return NULL;
862 	}
863 }
864 /* }}} */
865 
zend_std_write_dimension(zend_object * object,zval * offset,zval * value)866 ZEND_API void zend_std_write_dimension(zend_object *object, zval *offset, zval *value) /* {{{ */
867 {
868 	zend_class_entry *ce = object->ce;
869 	zval tmp_offset;
870 
871 	if (EXPECTED(zend_class_implements_interface(ce, zend_ce_arrayaccess) != 0)) {
872 		if (!offset) {
873 			ZVAL_NULL(&tmp_offset);
874 		} else {
875 			ZVAL_COPY_DEREF(&tmp_offset, offset);
876 		}
877 		GC_ADDREF(object);
878 		zend_call_method_with_2_params(object, ce, NULL, "offsetset", NULL, &tmp_offset, value);
879 		OBJ_RELEASE(object);
880 		zval_ptr_dtor(&tmp_offset);
881 	} else {
882 	    zend_bad_array_access(ce);
883 	}
884 }
885 /* }}} */
886 
zend_std_has_dimension(zend_object * object,zval * offset,int check_empty)887 ZEND_API int zend_std_has_dimension(zend_object *object, zval *offset, int check_empty) /* {{{ */
888 {
889 	zend_class_entry *ce = object->ce;
890 	zval retval, tmp_offset;
891 	int result;
892 
893 	if (EXPECTED(zend_class_implements_interface(ce, zend_ce_arrayaccess) != 0)) {
894 		ZVAL_COPY_DEREF(&tmp_offset, offset);
895 		GC_ADDREF(object);
896 		zend_call_method_with_1_params(object, ce, NULL, "offsetexists", &retval, &tmp_offset);
897 		result = i_zend_is_true(&retval);
898 		zval_ptr_dtor(&retval);
899 		if (check_empty && result && EXPECTED(!EG(exception))) {
900 			zend_call_method_with_1_params(object, ce, NULL, "offsetget", &retval, &tmp_offset);
901 			result = i_zend_is_true(&retval);
902 			zval_ptr_dtor(&retval);
903 		}
904 		OBJ_RELEASE(object);
905 		zval_ptr_dtor(&tmp_offset);
906 	} else {
907 	    zend_bad_array_access(ce);
908 		return 0;
909 	}
910 	return result;
911 }
912 /* }}} */
913 
zend_std_get_property_ptr_ptr(zend_object * zobj,zend_string * name,int type,void ** cache_slot)914 ZEND_API zval *zend_std_get_property_ptr_ptr(zend_object *zobj, zend_string *name, int type, void **cache_slot) /* {{{ */
915 {
916 	zval *retval = NULL;
917 	uintptr_t property_offset;
918 	zend_property_info *prop_info = NULL;
919 
920 #if DEBUG_OBJECT_HANDLERS
921 	fprintf(stderr, "Ptr object #%d property: %s\n", zobj->handle, ZSTR_VAL(name));
922 #endif
923 
924 	property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__get != NULL), cache_slot, &prop_info);
925 
926 	if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
927 		retval = OBJ_PROP(zobj, property_offset);
928 		if (UNEXPECTED(Z_TYPE_P(retval) == IS_UNDEF)) {
929 			if (EXPECTED(!zobj->ce->__get) ||
930 			    UNEXPECTED((*zend_get_property_guard(zobj, name)) & IN_GET) ||
931 			    UNEXPECTED(prop_info && Z_PROP_FLAG_P(retval) == IS_PROP_UNINIT)) {
932 				if (UNEXPECTED(type == BP_VAR_RW || type == BP_VAR_R)) {
933 					if (UNEXPECTED(prop_info)) {
934 						zend_throw_error(NULL,
935 							"Typed property %s::$%s must not be accessed before initialization",
936 							ZSTR_VAL(prop_info->ce->name),
937 							ZSTR_VAL(name));
938 						retval = &EG(error_zval);
939 					} else {
940 						ZVAL_NULL(retval);
941 						zend_error(E_WARNING, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
942 					}
943 				}
944 			} else {
945 				/* we do have getter - fail and let it try again with usual get/set */
946 				retval = NULL;
947 			}
948 		}
949 	} else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
950 		if (EXPECTED(zobj->properties)) {
951 			if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
952 				if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
953 					GC_DELREF(zobj->properties);
954 				}
955 				zobj->properties = zend_array_dup(zobj->properties);
956 			}
957 		    if (EXPECTED((retval = zend_hash_find(zobj->properties, name)) != NULL)) {
958 				return retval;
959 		    }
960 		}
961 		if (EXPECTED(!zobj->ce->__get) ||
962 		    UNEXPECTED((*zend_get_property_guard(zobj, name)) & IN_GET)) {
963 			if (UNEXPECTED(zobj->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) {
964 				zend_forbidden_dynamic_property(zobj->ce, name);
965 				return &EG(error_zval);
966 			}
967 			if (UNEXPECTED(!zobj->properties)) {
968 				rebuild_object_properties(zobj);
969 			}
970 			retval = zend_hash_update(zobj->properties, name, &EG(uninitialized_zval));
971 			/* Notice is thrown after creation of the property, to avoid EG(std_property_info)
972 			 * being overwritten in an error handler. */
973 			if (UNEXPECTED(type == BP_VAR_RW || type == BP_VAR_R)) {
974 				zend_error(E_WARNING, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
975 			}
976 		}
977 	} else if (zobj->ce->__get == NULL) {
978 		retval = &EG(error_zval);
979 	}
980 
981 	return retval;
982 }
983 /* }}} */
984 
zend_std_unset_property(zend_object * zobj,zend_string * name,void ** cache_slot)985 ZEND_API void zend_std_unset_property(zend_object *zobj, zend_string *name, void **cache_slot) /* {{{ */
986 {
987 	uintptr_t property_offset;
988 	zend_property_info *prop_info = NULL;
989 
990 	property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__unset != NULL), cache_slot, &prop_info);
991 
992 	if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
993 		zval *slot = OBJ_PROP(zobj, property_offset);
994 
995 		if (Z_TYPE_P(slot) != IS_UNDEF) {
996 			if (UNEXPECTED(Z_ISREF_P(slot)) &&
997 					(ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(slot)))) {
998 				if (prop_info) {
999 					ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(slot), prop_info);
1000 				}
1001 			}
1002 			zval tmp;
1003 			ZVAL_COPY_VALUE(&tmp, slot);
1004 			ZVAL_UNDEF(slot);
1005 			zval_ptr_dtor(&tmp);
1006 			if (zobj->properties) {
1007 				HT_FLAGS(zobj->properties) |= HASH_FLAG_HAS_EMPTY_IND;
1008 			}
1009 			return;
1010 		}
1011 		if (UNEXPECTED(Z_PROP_FLAG_P(slot) == IS_PROP_UNINIT)) {
1012 			/* Reset the IS_PROP_UNINIT flag, if it exists and bypass __unset(). */
1013 			Z_PROP_FLAG_P(slot) = 0;
1014 			return;
1015 		}
1016 	} else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))
1017 	 && EXPECTED(zobj->properties != NULL)) {
1018 		if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
1019 			if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
1020 				GC_DELREF(zobj->properties);
1021 			}
1022 			zobj->properties = zend_array_dup(zobj->properties);
1023 		}
1024 		if (EXPECTED(zend_hash_del(zobj->properties, name) != FAILURE)) {
1025 			return;
1026 		}
1027 	} else if (UNEXPECTED(EG(exception))) {
1028 		return;
1029 	}
1030 
1031 	/* magic unset */
1032 	if (zobj->ce->__unset) {
1033 		uint32_t *guard = zend_get_property_guard(zobj, name);
1034 		if (!((*guard) & IN_UNSET)) {
1035 			/* have unseter - try with it! */
1036 			(*guard) |= IN_UNSET; /* prevent circular unsetting */
1037 			zend_std_call_unsetter(zobj, name);
1038 			(*guard) &= ~IN_UNSET;
1039 		} else if (UNEXPECTED(IS_WRONG_PROPERTY_OFFSET(property_offset))) {
1040 			/* Trigger the correct error */
1041 			zend_wrong_offset(zobj->ce, name);
1042 			ZEND_ASSERT(EG(exception));
1043 			return;
1044 		} else {
1045 			/* Nothing to do: The property already does not exist. */
1046 		}
1047 	}
1048 }
1049 /* }}} */
1050 
zend_std_unset_dimension(zend_object * object,zval * offset)1051 ZEND_API void zend_std_unset_dimension(zend_object *object, zval *offset) /* {{{ */
1052 {
1053 	zend_class_entry *ce = object->ce;
1054 	zval tmp_offset;
1055 
1056 	if (zend_class_implements_interface(ce, zend_ce_arrayaccess)) {
1057 		ZVAL_COPY_DEREF(&tmp_offset, offset);
1058 		GC_ADDREF(object);
1059 		zend_call_method_with_1_params(object, ce, NULL, "offsetunset", NULL, &tmp_offset);
1060 		OBJ_RELEASE(object);
1061 		zval_ptr_dtor(&tmp_offset);
1062 	} else {
1063 	    zend_bad_array_access(ce);
1064 	}
1065 }
1066 /* }}} */
1067 
zend_get_parent_private_method(zend_class_entry * scope,zend_class_entry * ce,zend_string * function_name)1068 static zend_never_inline zend_function *zend_get_parent_private_method(zend_class_entry *scope, zend_class_entry *ce, zend_string *function_name) /* {{{ */
1069 {
1070 	zval *func;
1071 	zend_function *fbc;
1072 
1073 	if (scope != ce && scope && is_derived_class(ce, scope)) {
1074 		func = zend_hash_find(&scope->function_table, function_name);
1075 		if (func != NULL) {
1076 			fbc = Z_FUNC_P(func);
1077 			if ((fbc->common.fn_flags & ZEND_ACC_PRIVATE)
1078 			 && fbc->common.scope == scope) {
1079 				return fbc;
1080 			}
1081 		}
1082 	}
1083 	return NULL;
1084 }
1085 /* }}} */
1086 
1087 /* Ensures that we're allowed to call a protected method.
1088  */
zend_check_protected(zend_class_entry * ce,zend_class_entry * scope)1089 ZEND_API int zend_check_protected(zend_class_entry *ce, zend_class_entry *scope) /* {{{ */
1090 {
1091 	zend_class_entry *fbc_scope = ce;
1092 
1093 	/* Is the context that's calling the function, the same as one of
1094 	 * the function's parents?
1095 	 */
1096 	while (fbc_scope) {
1097 		if (fbc_scope==scope) {
1098 			return 1;
1099 		}
1100 		fbc_scope = fbc_scope->parent;
1101 	}
1102 
1103 	/* Is the function's scope the same as our current object context,
1104 	 * or any of the parents of our context?
1105 	 */
1106 	while (scope) {
1107 		if (scope==ce) {
1108 			return 1;
1109 		}
1110 		scope = scope->parent;
1111 	}
1112 	return 0;
1113 }
1114 /* }}} */
1115 
zend_get_call_trampoline_func(zend_class_entry * ce,zend_string * method_name,int is_static)1116 ZEND_API zend_function *zend_get_call_trampoline_func(zend_class_entry *ce, zend_string *method_name, int is_static) /* {{{ */
1117 {
1118 	size_t mname_len;
1119 	zend_op_array *func;
1120 	zend_function *fbc = is_static ? ce->__callstatic : ce->__call;
1121 	/* We use non-NULL value to avoid useless run_time_cache allocation.
1122 	 * The low bit must be zero, to not be interpreted as a MAP_PTR offset.
1123 	 */
1124 	static const void *dummy = (void*)(intptr_t)2;
1125 	static const zend_arg_info arg_info[1] = {{0}};
1126 
1127 	ZEND_ASSERT(fbc);
1128 
1129 	if (EXPECTED(EG(trampoline).common.function_name == NULL)) {
1130 		func = &EG(trampoline).op_array;
1131 	} else {
1132 		func = ecalloc(1, sizeof(zend_op_array));
1133 	}
1134 
1135 	func->type = ZEND_USER_FUNCTION;
1136 	func->arg_flags[0] = 0;
1137 	func->arg_flags[1] = 0;
1138 	func->arg_flags[2] = 0;
1139 	func->fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_PUBLIC | ZEND_ACC_VARIADIC;
1140 	if (is_static) {
1141 		func->fn_flags |= ZEND_ACC_STATIC;
1142 	}
1143 	func->opcodes = &EG(call_trampoline_op);
1144 	ZEND_MAP_PTR_INIT(func->run_time_cache, (void***)&dummy);
1145 	func->scope = fbc->common.scope;
1146 	/* reserve space for arguments, local and temporary variables */
1147 	func->T = (fbc->type == ZEND_USER_FUNCTION)? MAX(fbc->op_array.last_var + fbc->op_array.T, 2) : 2;
1148 	func->filename = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.filename : ZSTR_EMPTY_ALLOC();
1149 	func->line_start = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.line_start : 0;
1150 	func->line_end = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.line_end : 0;
1151 
1152 	//??? keep compatibility for "\0" characters
1153 	//??? see: Zend/tests/bug46238.phpt
1154 	if (UNEXPECTED((mname_len = strlen(ZSTR_VAL(method_name))) != ZSTR_LEN(method_name))) {
1155 		func->function_name = zend_string_init(ZSTR_VAL(method_name), mname_len, 0);
1156 	} else {
1157 		func->function_name = zend_string_copy(method_name);
1158 	}
1159 
1160 	func->prototype = NULL;
1161 	func->num_args = 0;
1162 	func->required_num_args = 0;
1163 	func->arg_info = (zend_arg_info *) arg_info;
1164 
1165 	return (zend_function*)func;
1166 }
1167 /* }}} */
1168 
zend_get_user_call_function(zend_class_entry * ce,zend_string * method_name)1169 static zend_always_inline zend_function *zend_get_user_call_function(zend_class_entry *ce, zend_string *method_name) /* {{{ */
1170 {
1171 	return zend_get_call_trampoline_func(ce, method_name, 0);
1172 }
1173 /* }}} */
1174 
zend_bad_method_call(zend_function * fbc,zend_string * method_name,zend_class_entry * scope)1175 static ZEND_COLD zend_never_inline void zend_bad_method_call(zend_function *fbc, zend_string *method_name, zend_class_entry *scope) /* {{{ */
1176 {
1177 	zend_throw_error(NULL, "Call to %s method %s::%s() from %s%s",
1178 		zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), ZSTR_VAL(method_name),
1179 		scope ? "scope " : "global scope",
1180 		scope ? ZSTR_VAL(scope->name) : ""
1181 	);
1182 }
1183 /* }}} */
1184 
zend_abstract_method_call(zend_function * fbc)1185 static ZEND_COLD zend_never_inline void zend_abstract_method_call(zend_function *fbc) /* {{{ */
1186 {
1187 	zend_throw_error(NULL, "Cannot call abstract method %s::%s()",
1188 		ZSTR_VAL(fbc->common.scope->name), ZSTR_VAL(fbc->common.function_name));
1189 }
1190 /* }}} */
1191 
zend_std_get_method(zend_object ** obj_ptr,zend_string * method_name,const zval * key)1192 ZEND_API zend_function *zend_std_get_method(zend_object **obj_ptr, zend_string *method_name, const zval *key) /* {{{ */
1193 {
1194 	zend_object *zobj = *obj_ptr;
1195 	zval *func;
1196 	zend_function *fbc;
1197 	zend_string *lc_method_name;
1198 	zend_class_entry *scope;
1199 	ALLOCA_FLAG(use_heap);
1200 
1201 	if (EXPECTED(key != NULL)) {
1202 		lc_method_name = Z_STR_P(key);
1203 #ifdef ZEND_ALLOCA_MAX_SIZE
1204 		use_heap = 0;
1205 #endif
1206 	} else {
1207 		ZSTR_ALLOCA_ALLOC(lc_method_name, ZSTR_LEN(method_name), use_heap);
1208 		zend_str_tolower_copy(ZSTR_VAL(lc_method_name), ZSTR_VAL(method_name), ZSTR_LEN(method_name));
1209 	}
1210 
1211 	if (UNEXPECTED((func = zend_hash_find(&zobj->ce->function_table, lc_method_name)) == NULL)) {
1212 		if (UNEXPECTED(!key)) {
1213 			ZSTR_ALLOCA_FREE(lc_method_name, use_heap);
1214 		}
1215 		if (zobj->ce->__call) {
1216 			return zend_get_user_call_function(zobj->ce, method_name);
1217 		} else {
1218 			return NULL;
1219 		}
1220 	}
1221 
1222 	fbc = Z_FUNC_P(func);
1223 
1224 	/* Check access level */
1225 	if (fbc->op_array.fn_flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
1226 		scope = zend_get_executed_scope();
1227 
1228 		if (fbc->common.scope != scope) {
1229 			if (fbc->op_array.fn_flags & ZEND_ACC_CHANGED) {
1230 				zend_function *updated_fbc = zend_get_parent_private_method(scope, zobj->ce, lc_method_name);
1231 
1232 				if (EXPECTED(updated_fbc != NULL)) {
1233 					fbc = updated_fbc;
1234 					goto exit;
1235 				} else if (fbc->op_array.fn_flags & ZEND_ACC_PUBLIC) {
1236 					goto exit;
1237 				}
1238 			}
1239 			if (UNEXPECTED(fbc->op_array.fn_flags & ZEND_ACC_PRIVATE)
1240 			 || UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), scope))) {
1241 				if (zobj->ce->__call) {
1242 					fbc = zend_get_user_call_function(zobj->ce, method_name);
1243 				} else {
1244 					zend_bad_method_call(fbc, method_name, scope);
1245 					fbc = NULL;
1246 				}
1247 			}
1248 		}
1249 	}
1250 
1251 exit:
1252 	if (fbc && UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_ABSTRACT)) {
1253 		zend_abstract_method_call(fbc);
1254 		fbc = NULL;
1255 	}
1256 	if (UNEXPECTED(!key)) {
1257 		ZSTR_ALLOCA_FREE(lc_method_name, use_heap);
1258 	}
1259 	return fbc;
1260 }
1261 /* }}} */
1262 
zend_get_user_callstatic_function(zend_class_entry * ce,zend_string * method_name)1263 static zend_always_inline zend_function *zend_get_user_callstatic_function(zend_class_entry *ce, zend_string *method_name) /* {{{ */
1264 {
1265 	return zend_get_call_trampoline_func(ce, method_name, 1);
1266 }
1267 /* }}} */
1268 
zend_std_get_static_method(zend_class_entry * ce,zend_string * function_name,const zval * key)1269 ZEND_API zend_function *zend_std_get_static_method(zend_class_entry *ce, zend_string *function_name, const zval *key) /* {{{ */
1270 {
1271 	zend_function *fbc = NULL;
1272 	zend_string *lc_function_name;
1273 	zend_object *object;
1274 	zend_class_entry *scope;
1275 
1276 	if (EXPECTED(key != NULL)) {
1277 		lc_function_name = Z_STR_P(key);
1278 	} else {
1279 		lc_function_name = zend_string_tolower(function_name);
1280 	}
1281 
1282 	do {
1283 		zval *func = zend_hash_find(&ce->function_table, lc_function_name);
1284 		if (EXPECTED(func != NULL)) {
1285 			fbc = Z_FUNC_P(func);
1286 		} else if (ce->constructor
1287 			&& ZSTR_LEN(lc_function_name) == ZSTR_LEN(ce->name)
1288 			&& zend_binary_strncasecmp(ZSTR_VAL(lc_function_name), ZSTR_LEN(lc_function_name), ZSTR_VAL(ce->name), ZSTR_LEN(lc_function_name), ZSTR_LEN(lc_function_name)) == 0
1289 			/* Only change the method to the constructor if the constructor isn't called __construct
1290 			 * we check for __ so we can be binary safe for lowering, we should use ZEND_CONSTRUCTOR_FUNC_NAME
1291 			 */
1292 			&& (ZSTR_VAL(ce->constructor->common.function_name)[0] != '_'
1293 				|| ZSTR_VAL(ce->constructor->common.function_name)[1] != '_')) {
1294 			fbc = ce->constructor;
1295 		} else {
1296 			if (UNEXPECTED(!key)) {
1297 				zend_string_release_ex(lc_function_name, 0);
1298 			}
1299 			if (ce->__call &&
1300 				(object = zend_get_this_object(EG(current_execute_data))) != NULL &&
1301 			    instanceof_function(object->ce, ce)) {
1302 				/* Call the top-level defined __call().
1303 				 * see: tests/classes/__call_004.phpt  */
1304 
1305 				ZEND_ASSERT(object->ce->__call);
1306 				return zend_get_user_call_function(object->ce, function_name);
1307 			} else if (ce->__callstatic) {
1308 				return zend_get_user_callstatic_function(ce, function_name);
1309 			} else {
1310 	   			return NULL;
1311 			}
1312 		}
1313 	} while (0);
1314 
1315 #ifdef MBO_0
1316 	/* right now this function is used for non static method lookup too */
1317 	/* Is the function static */
1318 	if (UNEXPECTED(!(fbc->common.fn_flags & ZEND_ACC_STATIC))) {
1319 		zend_error_noreturn(E_ERROR, "Cannot call non-static method %s::%s() without object", ZEND_FN_SCOPE_NAME(fbc), ZSTR_VAL(fbc->common.function_name));
1320 	}
1321 #endif
1322 	if (!(fbc->op_array.fn_flags & ZEND_ACC_PUBLIC)) {
1323 		scope = zend_get_executed_scope();
1324 		if (UNEXPECTED(fbc->common.scope != scope)) {
1325 			if (UNEXPECTED(fbc->op_array.fn_flags & ZEND_ACC_PRIVATE)
1326 			 || UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), scope))) {
1327 				if (ce->__callstatic) {
1328 					fbc = zend_get_user_callstatic_function(ce, function_name);
1329 				} else {
1330 					zend_bad_method_call(fbc, function_name, scope);
1331 					fbc = NULL;
1332 				}
1333 			}
1334 		}
1335 	}
1336 
1337 	if (fbc && UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_ABSTRACT)) {
1338 		zend_abstract_method_call(fbc);
1339 		fbc = NULL;
1340 	}
1341 
1342 	if (UNEXPECTED(!key)) {
1343 		zend_string_release_ex(lc_function_name, 0);
1344 	}
1345 
1346 	return fbc;
1347 }
1348 /* }}} */
1349 
zend_class_init_statics(zend_class_entry * class_type)1350 ZEND_API void zend_class_init_statics(zend_class_entry *class_type) /* {{{ */
1351 {
1352 	int i;
1353 	zval *p;
1354 
1355 	if (class_type->default_static_members_count && !CE_STATIC_MEMBERS(class_type)) {
1356 		if (class_type->parent) {
1357 			zend_class_init_statics(class_type->parent);
1358 		}
1359 
1360 		ZEND_MAP_PTR_SET(class_type->static_members_table, emalloc(sizeof(zval) * class_type->default_static_members_count));
1361 		for (i = 0; i < class_type->default_static_members_count; i++) {
1362 			p = &class_type->default_static_members_table[i];
1363 			if (Z_TYPE_P(p) == IS_INDIRECT) {
1364 				zval *q = &CE_STATIC_MEMBERS(class_type->parent)[i];
1365 				ZVAL_DEINDIRECT(q);
1366 				ZVAL_INDIRECT(&CE_STATIC_MEMBERS(class_type)[i], q);
1367 			} else {
1368 				ZVAL_COPY_OR_DUP(&CE_STATIC_MEMBERS(class_type)[i], p);
1369 			}
1370 		}
1371 	}
1372 } /* }}} */
1373 
zend_std_get_static_property_with_info(zend_class_entry * ce,zend_string * property_name,int type,zend_property_info ** property_info_ptr)1374 ZEND_API zval *zend_std_get_static_property_with_info(zend_class_entry *ce, zend_string *property_name, int type, zend_property_info **property_info_ptr) /* {{{ */
1375 {
1376 	zval *ret;
1377 	zend_class_entry *scope;
1378 	zend_property_info *property_info = zend_hash_find_ptr(&ce->properties_info, property_name);
1379 	*property_info_ptr = property_info;
1380 
1381 	if (UNEXPECTED(property_info == NULL)) {
1382 		goto undeclared_property;
1383 	}
1384 
1385 	if (!(property_info->flags & ZEND_ACC_PUBLIC)) {
1386 		if (UNEXPECTED(EG(fake_scope))) {
1387 			scope = EG(fake_scope);
1388 		} else {
1389 			scope = zend_get_executed_scope();
1390 		}
1391 		if (property_info->ce != scope) {
1392 			if (UNEXPECTED(property_info->flags & ZEND_ACC_PRIVATE)
1393 			 || UNEXPECTED(!is_protected_compatible_scope(property_info->ce, scope))) {
1394 				if (type != BP_VAR_IS) {
1395 					zend_bad_property_access(property_info, ce, property_name);
1396 				}
1397 				return NULL;
1398 			}
1399 		}
1400 	}
1401 
1402 	if (UNEXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0)) {
1403 		goto undeclared_property;
1404 	}
1405 
1406 	if (UNEXPECTED(!(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
1407 		if (UNEXPECTED(zend_update_class_constants(ce)) != SUCCESS) {
1408 			return NULL;
1409 		}
1410 	}
1411 
1412 	/* check if static properties were destroyed */
1413 	if (UNEXPECTED(CE_STATIC_MEMBERS(ce) == NULL)) {
1414 		if (ce->type == ZEND_INTERNAL_CLASS || (ce->ce_flags & (ZEND_ACC_IMMUTABLE|ZEND_ACC_PRELOADED))) {
1415 			zend_class_init_statics(ce);
1416 		} else {
1417 undeclared_property:
1418 			if (type != BP_VAR_IS) {
1419 				zend_throw_error(NULL, "Access to undeclared static property %s::$%s", ZSTR_VAL(ce->name), ZSTR_VAL(property_name));
1420 			}
1421 			return NULL;
1422 		}
1423 	}
1424 
1425 	ret = CE_STATIC_MEMBERS(ce) + property_info->offset;
1426 	ZVAL_DEINDIRECT(ret);
1427 
1428 	if (UNEXPECTED((type == BP_VAR_R || type == BP_VAR_RW)
1429 				&& Z_TYPE_P(ret) == IS_UNDEF && ZEND_TYPE_IS_SET(property_info->type))) {
1430 		zend_throw_error(NULL, "Typed static property %s::$%s must not be accessed before initialization",
1431 			ZSTR_VAL(property_info->ce->name),
1432 			zend_get_unmangled_property_name(property_name));
1433 		return NULL;
1434 	}
1435 
1436 	return ret;
1437 }
1438 /* }}} */
1439 
zend_std_get_static_property(zend_class_entry * ce,zend_string * property_name,int type)1440 ZEND_API zval *zend_std_get_static_property(zend_class_entry *ce, zend_string *property_name, int type) /* {{{ */
1441 {
1442 	zend_property_info *prop_info;
1443 	return zend_std_get_static_property_with_info(ce, property_name, type, &prop_info);
1444 }
1445 
zend_std_unset_static_property(zend_class_entry * ce,zend_string * property_name)1446 ZEND_API ZEND_COLD zend_bool zend_std_unset_static_property(zend_class_entry *ce, zend_string *property_name) /* {{{ */
1447 {
1448 	zend_throw_error(NULL, "Attempt to unset static property %s::$%s", ZSTR_VAL(ce->name), ZSTR_VAL(property_name));
1449 	return 0;
1450 }
1451 /* }}} */
1452 
zend_bad_constructor_call(zend_function * constructor,zend_class_entry * scope)1453 static ZEND_COLD zend_never_inline void zend_bad_constructor_call(zend_function *constructor, zend_class_entry *scope) /* {{{ */
1454 {
1455 	if (scope) {
1456 		zend_throw_error(NULL, "Call to %s %s::%s() from scope %s",
1457 			zend_visibility_string(constructor->common.fn_flags), ZSTR_VAL(constructor->common.scope->name),
1458 			ZSTR_VAL(constructor->common.function_name), ZSTR_VAL(scope->name)
1459 		);
1460 	} else {
1461 		zend_throw_error(NULL, "Call to %s %s::%s() from global scope", zend_visibility_string(constructor->common.fn_flags), ZSTR_VAL(constructor->common.scope->name), ZSTR_VAL(constructor->common.function_name));
1462 	}
1463 }
1464 /* }}} */
1465 
zend_std_get_constructor(zend_object * zobj)1466 ZEND_API zend_function *zend_std_get_constructor(zend_object *zobj) /* {{{ */
1467 {
1468 	zend_function *constructor = zobj->ce->constructor;
1469 	zend_class_entry *scope;
1470 
1471 	if (constructor) {
1472 		if (UNEXPECTED(!(constructor->op_array.fn_flags & ZEND_ACC_PUBLIC))) {
1473 			if (UNEXPECTED(EG(fake_scope))) {
1474 				scope = EG(fake_scope);
1475 			} else {
1476 				scope = zend_get_executed_scope();
1477 			}
1478 			if (UNEXPECTED(constructor->common.scope != scope)) {
1479 				if (UNEXPECTED(constructor->op_array.fn_flags & ZEND_ACC_PRIVATE)
1480 				 || UNEXPECTED(!zend_check_protected(zend_get_function_root_class(constructor), scope))) {
1481 					zend_bad_constructor_call(constructor, scope);
1482 					constructor = NULL;
1483 				}
1484 			}
1485 		}
1486 	}
1487 
1488 	return constructor;
1489 }
1490 /* }}} */
1491 
zend_std_compare_objects(zval * o1,zval * o2)1492 ZEND_API int zend_std_compare_objects(zval *o1, zval *o2) /* {{{ */
1493 {
1494 	zend_object *zobj1, *zobj2;
1495 
1496 	if (Z_TYPE_P(o1) != Z_TYPE_P(o2)) {
1497 		/* Object and non-object */
1498 		zval casted;
1499 		if (Z_TYPE_P(o1) == IS_OBJECT) {
1500 			ZEND_ASSERT(Z_TYPE_P(o2) != IS_OBJECT);
1501 			zend_uchar target_type = (Z_TYPE_P(o2) == IS_FALSE || Z_TYPE_P(o2) == IS_TRUE)
1502 				? _IS_BOOL : Z_TYPE_P(o2);
1503 			if (Z_OBJ_HT_P(o1)->cast_object(Z_OBJ_P(o1), &casted, target_type) == FAILURE) {
1504 				// TODO: Less crazy.
1505 				if (target_type == IS_LONG || target_type == IS_DOUBLE) {
1506 					zend_error(E_NOTICE, "Object of class %s could not be converted to %s",
1507 						ZSTR_VAL(Z_OBJCE_P(o1)->name), zend_get_type_by_const(target_type));
1508 					if (target_type == IS_LONG) {
1509 						ZVAL_LONG(&casted, 1);
1510 					} else {
1511 						ZVAL_DOUBLE(&casted, 1.0);
1512 					}
1513 				} else {
1514 					return 1;
1515 				}
1516 			}
1517 			int ret = zend_compare(&casted, o2);
1518 			zval_ptr_dtor(&casted);
1519 			return ret;
1520 		} else {
1521 			ZEND_ASSERT(Z_TYPE_P(o2) == IS_OBJECT);
1522 			zend_uchar target_type = (Z_TYPE_P(o1) == IS_FALSE || Z_TYPE_P(o1) == IS_TRUE)
1523 				? _IS_BOOL : Z_TYPE_P(o1);
1524 			if (Z_OBJ_HT_P(o2)->cast_object(Z_OBJ_P(o2), &casted, target_type) == FAILURE) {
1525 				// TODO: Less crazy.
1526 				if (target_type == IS_LONG || target_type == IS_DOUBLE) {
1527 					zend_error(E_NOTICE, "Object of class %s could not be converted to %s",
1528 						ZSTR_VAL(Z_OBJCE_P(o2)->name), zend_get_type_by_const(target_type));
1529 					if (target_type == IS_LONG) {
1530 						ZVAL_LONG(&casted, 1);
1531 					} else {
1532 						ZVAL_DOUBLE(&casted, 1.0);
1533 					}
1534 				} else {
1535 					return -1;
1536 				}
1537 			}
1538 			int ret = zend_compare(o1, &casted);
1539 			zval_ptr_dtor(&casted);
1540 			return ret;
1541 		}
1542 		return ZEND_UNCOMPARABLE;
1543 	}
1544 
1545 	zobj1 = Z_OBJ_P(o1);
1546 	zobj2 = Z_OBJ_P(o2);
1547 
1548 	if (zobj1 == zobj2) {
1549 		return 0; /* the same object */
1550 	}
1551 	if (zobj1->ce != zobj2->ce) {
1552 		return ZEND_UNCOMPARABLE; /* different classes */
1553 	}
1554 	if (!zobj1->properties && !zobj2->properties) {
1555 		zend_property_info *info;
1556 
1557 		if (!zobj1->ce->default_properties_count) {
1558 			return 0;
1559 		}
1560 
1561 		/* It's enough to protect only one of the objects.
1562 		 * The second one may be referenced from the first and this may cause
1563 		 * false recursion detection.
1564 		 */
1565 		/* use bitwise OR to make only one conditional jump */
1566 		if (UNEXPECTED(Z_IS_RECURSIVE_P(o1))) {
1567 			zend_error_noreturn(E_ERROR, "Nesting level too deep - recursive dependency?");
1568 		}
1569 		Z_PROTECT_RECURSION_P(o1);
1570 
1571 		ZEND_HASH_FOREACH_PTR(&zobj1->ce->properties_info, info) {
1572 			zval *p1 = OBJ_PROP(zobj1, info->offset);
1573 			zval *p2 = OBJ_PROP(zobj2, info->offset);
1574 
1575 			if (info->flags & ZEND_ACC_STATIC) {
1576 				continue;
1577 			}
1578 
1579 			if (Z_TYPE_P(p1) != IS_UNDEF) {
1580 				if (Z_TYPE_P(p2) != IS_UNDEF) {
1581 					int ret;
1582 
1583 					ret = zend_compare(p1, p2);
1584 					if (ret != 0) {
1585 						Z_UNPROTECT_RECURSION_P(o1);
1586 						return ret;
1587 					}
1588 				} else {
1589 					Z_UNPROTECT_RECURSION_P(o1);
1590 					return 1;
1591 				}
1592 			} else {
1593 				if (Z_TYPE_P(p2) != IS_UNDEF) {
1594 					Z_UNPROTECT_RECURSION_P(o1);
1595 					return 1;
1596 				}
1597 			}
1598 		} ZEND_HASH_FOREACH_END();
1599 
1600 		Z_UNPROTECT_RECURSION_P(o1);
1601 		return 0;
1602 	} else {
1603 		if (!zobj1->properties) {
1604 			rebuild_object_properties(zobj1);
1605 		}
1606 		if (!zobj2->properties) {
1607 			rebuild_object_properties(zobj2);
1608 		}
1609 		return zend_compare_symbol_tables(zobj1->properties, zobj2->properties);
1610 	}
1611 }
1612 /* }}} */
1613 
zend_objects_not_comparable(zval * o1,zval * o2)1614 ZEND_API int zend_objects_not_comparable(zval *o1, zval *o2)
1615 {
1616 	return ZEND_UNCOMPARABLE;
1617 }
1618 
zend_std_has_property(zend_object * zobj,zend_string * name,int has_set_exists,void ** cache_slot)1619 ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has_set_exists, void **cache_slot) /* {{{ */
1620 {
1621 	int result;
1622 	zval *value = NULL;
1623 	uintptr_t property_offset;
1624 	zend_property_info *prop_info = NULL;
1625 	zend_string *tmp_name = NULL;
1626 
1627 	property_offset = zend_get_property_offset(zobj->ce, name, 1, cache_slot, &prop_info);
1628 
1629 	if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
1630 		value = OBJ_PROP(zobj, property_offset);
1631 		if (Z_TYPE_P(value) != IS_UNDEF) {
1632 			goto found;
1633 		}
1634 		if (UNEXPECTED(Z_PROP_FLAG_P(value) == IS_PROP_UNINIT)) {
1635 			/* Skip __isset() for uninitialized typed properties */
1636 			result = 0;
1637 			goto exit;
1638 		}
1639 	} else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
1640 		if (EXPECTED(zobj->properties != NULL)) {
1641 			if (!IS_UNKNOWN_DYNAMIC_PROPERTY_OFFSET(property_offset)) {
1642 				uintptr_t idx = ZEND_DECODE_DYN_PROP_OFFSET(property_offset);
1643 
1644 				if (EXPECTED(idx < zobj->properties->nNumUsed * sizeof(Bucket))) {
1645 					Bucket *p = (Bucket*)((char*)zobj->properties->arData + idx);
1646 
1647 					if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF) &&
1648 				        (EXPECTED(p->key == name) ||
1649 				         (EXPECTED(p->h == ZSTR_H(name)) &&
1650 				          EXPECTED(p->key != NULL) &&
1651 				          EXPECTED(zend_string_equal_content(p->key, name))))) {
1652 						value = &p->val;
1653 						goto found;
1654 					}
1655 				}
1656 				CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET);
1657 			}
1658 			value = zend_hash_find(zobj->properties, name);
1659 			if (value) {
1660 				if (cache_slot) {
1661 					uintptr_t idx = (char*)value - (char*)zobj->properties->arData;
1662 					CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_ENCODE_DYN_PROP_OFFSET(idx));
1663 				}
1664 found:
1665 				if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY) {
1666 					result = zend_is_true(value);
1667 				} else if (has_set_exists < ZEND_PROPERTY_NOT_EMPTY) {
1668 					ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_ISSET);
1669 					ZVAL_DEREF(value);
1670 					result = (Z_TYPE_P(value) != IS_NULL);
1671 				} else {
1672 					ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_EXISTS);
1673 					result = 1;
1674 				}
1675 				goto exit;
1676 			}
1677 		}
1678 	} else if (UNEXPECTED(EG(exception))) {
1679 		result = 0;
1680 		goto exit;
1681 	}
1682 
1683 	result = 0;
1684 	if ((has_set_exists != ZEND_PROPERTY_EXISTS) && zobj->ce->__isset) {
1685 		uint32_t *guard = zend_get_property_guard(zobj, name);
1686 
1687 		if (!((*guard) & IN_ISSET)) {
1688 			zval rv;
1689 
1690 			/* have issetter - try with it! */
1691 			if (!tmp_name && !ZSTR_IS_INTERNED(name)) {
1692 				tmp_name = zend_string_copy(name);
1693 			}
1694 			GC_ADDREF(zobj);
1695 			(*guard) |= IN_ISSET; /* prevent circular getting */
1696 			zend_std_call_issetter(zobj, name, &rv);
1697 			result = zend_is_true(&rv);
1698 			zval_ptr_dtor(&rv);
1699 			if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY && result) {
1700 				if (EXPECTED(!EG(exception)) && zobj->ce->__get && !((*guard) & IN_GET)) {
1701 					(*guard) |= IN_GET;
1702 					zend_std_call_getter(zobj, name, &rv);
1703 					(*guard) &= ~IN_GET;
1704 					result = i_zend_is_true(&rv);
1705 					zval_ptr_dtor(&rv);
1706 				} else {
1707 					result = 0;
1708 				}
1709 			}
1710 			(*guard) &= ~IN_ISSET;
1711 			OBJ_RELEASE(zobj);
1712 		}
1713 	}
1714 
1715 exit:
1716 	zend_tmp_string_release(tmp_name);
1717 	return result;
1718 }
1719 /* }}} */
1720 
zend_std_get_class_name(const zend_object * zobj)1721 ZEND_API zend_string *zend_std_get_class_name(const zend_object *zobj) /* {{{ */
1722 {
1723 	return zend_string_copy(zobj->ce->name);
1724 }
1725 /* }}} */
1726 
zend_std_cast_object_tostring(zend_object * readobj,zval * writeobj,int type)1727 ZEND_API int zend_std_cast_object_tostring(zend_object *readobj, zval *writeobj, int type) /* {{{ */
1728 {
1729 	switch (type) {
1730 		case IS_STRING: {
1731 			zend_class_entry *ce = readobj->ce;
1732 			if (ce->__tostring) {
1733 				zval retval;
1734 				GC_ADDREF(readobj);
1735 				zend_call_known_instance_method_with_0_params(ce->__tostring, readobj, &retval);
1736 				zend_object_release(readobj);
1737 				if (EXPECTED(Z_TYPE(retval) == IS_STRING)) {
1738 					ZVAL_COPY_VALUE(writeobj, &retval);
1739 					return SUCCESS;
1740 				}
1741 				zval_ptr_dtor(&retval);
1742 				if (!EG(exception)) {
1743 					zend_throw_error(NULL, "Method %s::__toString() must return a string value", ZSTR_VAL(ce->name));
1744 				}
1745 			}
1746 			return FAILURE;
1747 		}
1748 		case _IS_BOOL:
1749 			ZVAL_TRUE(writeobj);
1750 			return SUCCESS;
1751 		default:
1752 			return FAILURE;
1753 	}
1754 }
1755 /* }}} */
1756 
zend_std_get_closure(zend_object * obj,zend_class_entry ** ce_ptr,zend_function ** fptr_ptr,zend_object ** obj_ptr,zend_bool check_only)1757 ZEND_API int zend_std_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, zend_bool check_only) /* {{{ */
1758 {
1759 	zval *func;
1760 	zend_class_entry *ce = obj->ce;
1761 
1762 	if ((func = zend_hash_find_ex(&ce->function_table, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE), 1)) == NULL) {
1763 		return FAILURE;
1764 	}
1765 	*fptr_ptr = Z_FUNC_P(func);
1766 
1767 	*ce_ptr = ce;
1768 	if ((*fptr_ptr)->common.fn_flags & ZEND_ACC_STATIC) {
1769 		if (obj_ptr) {
1770 			*obj_ptr = NULL;
1771 		}
1772 	} else {
1773 		if (obj_ptr) {
1774 			*obj_ptr = obj;
1775 		}
1776 	}
1777 	return SUCCESS;
1778 }
1779 /* }}} */
1780 
zend_std_get_properties_for(zend_object * obj,zend_prop_purpose purpose)1781 ZEND_API HashTable *zend_std_get_properties_for(zend_object *obj, zend_prop_purpose purpose) {
1782 	HashTable *ht;
1783 	switch (purpose) {
1784 		case ZEND_PROP_PURPOSE_DEBUG:
1785 			if (obj->handlers->get_debug_info) {
1786 				int is_temp;
1787 				ht = obj->handlers->get_debug_info(obj, &is_temp);
1788 				if (ht && !is_temp) {
1789 					GC_TRY_ADDREF(ht);
1790 				}
1791 				return ht;
1792 			}
1793 			/* break missing intentionally */
1794 		case ZEND_PROP_PURPOSE_ARRAY_CAST:
1795 		case ZEND_PROP_PURPOSE_SERIALIZE:
1796 		case ZEND_PROP_PURPOSE_VAR_EXPORT:
1797 		case ZEND_PROP_PURPOSE_JSON:
1798 			ht = obj->handlers->get_properties(obj);
1799 			if (ht) {
1800 				GC_TRY_ADDREF(ht);
1801 			}
1802 			return ht;
1803 		default:
1804 			ZEND_UNREACHABLE();
1805 			return NULL;
1806 	}
1807 }
1808 
zend_get_properties_for(zval * obj,zend_prop_purpose purpose)1809 ZEND_API HashTable *zend_get_properties_for(zval *obj, zend_prop_purpose purpose) {
1810 	zend_object *zobj = Z_OBJ_P(obj);
1811 
1812 	if (zobj->handlers->get_properties_for) {
1813 		return zobj->handlers->get_properties_for(zobj, purpose);
1814 	}
1815 
1816 	return zend_std_get_properties_for(zobj, purpose);
1817 }
1818 
1819 ZEND_API const zend_object_handlers std_object_handlers = {
1820 	0,										/* offset */
1821 
1822 	zend_object_std_dtor,					/* free_obj */
1823 	zend_objects_destroy_object,			/* dtor_obj */
1824 	zend_objects_clone_obj,					/* clone_obj */
1825 
1826 	zend_std_read_property,					/* read_property */
1827 	zend_std_write_property,				/* write_property */
1828 	zend_std_read_dimension,				/* read_dimension */
1829 	zend_std_write_dimension,				/* write_dimension */
1830 	zend_std_get_property_ptr_ptr,			/* get_property_ptr_ptr */
1831 	zend_std_has_property,					/* has_property */
1832 	zend_std_unset_property,				/* unset_property */
1833 	zend_std_has_dimension,					/* has_dimension */
1834 	zend_std_unset_dimension,				/* unset_dimension */
1835 	zend_std_get_properties,				/* get_properties */
1836 	zend_std_get_method,					/* get_method */
1837 	zend_std_get_constructor,				/* get_constructor */
1838 	zend_std_get_class_name,				/* get_class_name */
1839 	zend_std_cast_object_tostring,			/* cast_object */
1840 	NULL,									/* count_elements */
1841 	zend_std_get_debug_info,				/* get_debug_info */
1842 	zend_std_get_closure,					/* get_closure */
1843 	zend_std_get_gc,						/* get_gc */
1844 	NULL,									/* do_operation */
1845 	zend_std_compare_objects,				/* compare */
1846 	NULL,									/* get_properties_for */
1847 };
1848