/src/php-src/Zend/zend_object_handlers.c
Line | Count | Source |
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_lazy_objects.h" |
24 | | #include "zend_variables.h" |
25 | | #include "zend_API.h" |
26 | | #include "zend_objects.h" |
27 | | #include "zend_objects_API.h" |
28 | | #include "zend_object_handlers.h" |
29 | | #include "zend_interfaces.h" |
30 | | #include "zend_exceptions.h" |
31 | | #include "zend_closures.h" |
32 | | #include "zend_compile.h" |
33 | | #include "zend_hash.h" |
34 | | #include "zend_property_hooks.h" |
35 | | #include "zend_observer.h" |
36 | | |
37 | | #define DEBUG_OBJECT_HANDLERS 0 |
38 | | |
39 | 52 | #define ZEND_WRONG_PROPERTY_OFFSET 0 |
40 | 906 | #define ZEND_HOOKED_PROPERTY_OFFSET 1 |
41 | | |
42 | | /* guard flags */ |
43 | 363 | #define IN_GET ZEND_GUARD_PROPERTY_GET |
44 | 264 | #define IN_SET ZEND_GUARD_PROPERTY_SET |
45 | 22 | #define IN_UNSET ZEND_GUARD_PROPERTY_UNSET |
46 | 0 | #define IN_ISSET ZEND_GUARD_PROPERTY_ISSET |
47 | | #define IN_HOOK ZEND_GUARD_PROPERTY_HOOK |
48 | | |
49 | | static zend_arg_info zend_call_trampoline_arginfo[1] = {{0}}; |
50 | | static zend_arg_info zend_property_hook_arginfo[1] = {{0}}; |
51 | | |
52 | | static zend_always_inline bool zend_objects_check_stack_limit(void) |
53 | 298 | { |
54 | 298 | #ifdef ZEND_CHECK_STACK_LIMIT |
55 | 298 | return zend_call_stack_overflowed(EG(stack_limit)); |
56 | | #else |
57 | | return false; |
58 | | #endif |
59 | 298 | } |
60 | | |
61 | | /* |
62 | | __X accessors explanation: |
63 | | |
64 | | if we have __get and property that is not part of the properties array is |
65 | | requested, we call __get handler. If it fails, we return uninitialized. |
66 | | |
67 | | if we have __set and property that is not part of the properties array is |
68 | | set, we call __set handler. If it fails, we do not change the array. |
69 | | |
70 | | for both handlers above, when we are inside __get/__set, no further calls for |
71 | | __get/__set for this property of this object will be made, to prevent endless |
72 | | recursion and enable accessors to change properties array. |
73 | | |
74 | | if we have __call and method which is not part of the class function table is |
75 | | called, we cal __call handler. |
76 | | */ |
77 | | |
78 | | ZEND_API HashTable *rebuild_object_properties_internal(zend_object *zobj) /* {{{ */ |
79 | 668k | { |
80 | 668k | if (!zobj->properties) { |
81 | 668k | zend_property_info *prop_info; |
82 | 668k | zend_class_entry *ce = zobj->ce; |
83 | 668k | int i; |
84 | | |
85 | 668k | zobj->properties = zend_new_array(ce->default_properties_count); |
86 | 668k | if (ce->default_properties_count) { |
87 | 99.3k | zend_hash_real_init_mixed(zobj->properties); |
88 | 788k | for (i = 0; i < ce->default_properties_count; i++) { |
89 | 689k | prop_info = ce->properties_info_table[i]; |
90 | | |
91 | 689k | if (!prop_info) { |
92 | 38 | continue; |
93 | 38 | } |
94 | | |
95 | 689k | if (UNEXPECTED(Z_TYPE_P(OBJ_PROP(zobj, prop_info->offset)) == IS_UNDEF)) { |
96 | 347 | HT_FLAGS(zobj->properties) |= HASH_FLAG_HAS_EMPTY_IND; |
97 | 347 | } |
98 | | |
99 | 689k | _zend_hash_append_ind(zobj->properties, prop_info->name, |
100 | 689k | OBJ_PROP(zobj, prop_info->offset)); |
101 | 689k | } |
102 | 99.3k | } |
103 | 668k | } |
104 | | |
105 | 668k | return zobj->properties; |
106 | 668k | } |
107 | | /* }}} */ |
108 | | |
109 | | /* Implements the fast path for array cast */ |
110 | | ZEND_API HashTable *zend_std_build_object_properties_array(zend_object *zobj) /* {{{ */ |
111 | 0 | { |
112 | 0 | const zend_class_entry *ce = zobj->ce; |
113 | 0 | HashTable *ht; |
114 | 0 | zval* prop; |
115 | 0 | int i; |
116 | |
|
117 | 0 | ZEND_ASSERT(!(zend_object_is_lazy_proxy(zobj) && zend_lazy_object_initialized(zobj))); |
118 | 0 | ZEND_ASSERT(!zobj->properties); |
119 | 0 | ht = zend_new_array(ce->default_properties_count); |
120 | 0 | if (ce->default_properties_count) { |
121 | 0 | zend_hash_real_init_mixed(ht); |
122 | 0 | for (i = 0; i < ce->default_properties_count; i++) { |
123 | 0 | const zend_property_info *prop_info = ce->properties_info_table[i]; |
124 | |
|
125 | 0 | if (!prop_info) { |
126 | 0 | continue; |
127 | 0 | } |
128 | | |
129 | 0 | prop = OBJ_PROP(zobj, prop_info->offset); |
130 | 0 | if (UNEXPECTED(Z_TYPE_P(prop) == IS_UNDEF)) { |
131 | 0 | continue; |
132 | 0 | } |
133 | | |
134 | 0 | if (Z_ISREF_P(prop) && Z_REFCOUNT_P(prop) == 1) { |
135 | 0 | prop = Z_REFVAL_P(prop); |
136 | 0 | } |
137 | |
|
138 | 0 | Z_TRY_ADDREF_P(prop); |
139 | 0 | _zend_hash_append(ht, prop_info->name, prop); |
140 | 0 | } |
141 | 0 | } |
142 | 0 | return ht; |
143 | 0 | } |
144 | | /* }}} */ |
145 | | |
146 | | ZEND_API HashTable *zend_std_get_properties(zend_object *zobj) /* {{{ */ |
147 | 1.51M | { |
148 | 1.51M | return zend_std_get_properties_ex(zobj); |
149 | 1.51M | } |
150 | | /* }}} */ |
151 | | |
152 | | /* Fetch properties HashTable without triggering lazy initialization */ |
153 | | ZEND_API HashTable *zend_get_properties_no_lazy_init(zend_object *zobj) |
154 | 121 | { |
155 | 121 | if (zobj->handlers->get_properties == zend_std_get_properties) { |
156 | 121 | if (UNEXPECTED(zend_object_is_lazy_proxy(zobj) |
157 | 121 | && zend_lazy_object_initialized(zobj))) { |
158 | 0 | zend_object *instance = zend_lazy_object_get_instance(zobj); |
159 | 0 | return zend_get_properties_no_lazy_init(instance); |
160 | 0 | } |
161 | | |
162 | 121 | if (!zobj->properties) { |
163 | 46 | rebuild_object_properties_internal(zobj); |
164 | 46 | } |
165 | 121 | return zobj->properties; |
166 | 121 | } |
167 | | |
168 | 0 | ZEND_ASSERT(!zend_object_is_lazy(zobj)); |
169 | |
|
170 | 0 | return zobj->handlers->get_properties(zobj); |
171 | 0 | } |
172 | | |
173 | | ZEND_API HashTable *zend_std_get_gc(zend_object *zobj, zval **table, int *n) /* {{{ */ |
174 | 567k | { |
175 | 567k | if (zobj->handlers->get_properties != zend_std_get_properties) { |
176 | 0 | *table = NULL; |
177 | 0 | *n = 0; |
178 | 0 | return zobj->handlers->get_properties(zobj); |
179 | 567k | } else { |
180 | 567k | if (UNEXPECTED(zend_object_is_lazy(zobj))) { |
181 | 142 | return zend_lazy_object_get_gc(zobj, table, n); |
182 | 567k | } else if (zobj->properties) { |
183 | 549k | *table = NULL; |
184 | 549k | *n = 0; |
185 | 549k | return zobj->properties; |
186 | 549k | } else { |
187 | 17.9k | *table = zobj->properties_table; |
188 | 17.9k | *n = zobj->ce->default_properties_count; |
189 | 17.9k | return NULL; |
190 | 17.9k | } |
191 | 567k | } |
192 | 567k | } |
193 | | /* }}} */ |
194 | | |
195 | | ZEND_API HashTable *zend_std_get_debug_info(zend_object *object, int *is_temp) /* {{{ */ |
196 | 1.40k | { |
197 | 1.40k | const zend_class_entry *ce = object->ce; |
198 | 1.40k | zval retval; |
199 | 1.40k | HashTable *ht; |
200 | | |
201 | 1.40k | if (!ce->__debugInfo) { |
202 | 1.38k | if (UNEXPECTED(zend_object_is_lazy(object))) { |
203 | 69 | return zend_lazy_object_debug_info(object, is_temp); |
204 | 69 | } |
205 | | |
206 | 1.31k | *is_temp = 0; |
207 | 1.31k | return object->handlers->get_properties(object); |
208 | 1.38k | } |
209 | | |
210 | 19 | zend_call_known_instance_method_with_0_params(ce->__debugInfo, object, &retval); |
211 | 19 | if (UNEXPECTED(Z_ISREF(retval))) { |
212 | 1 | zend_unwrap_reference(&retval); |
213 | 1 | } |
214 | 19 | if (Z_TYPE(retval) == IS_ARRAY) { |
215 | 17 | if (!Z_REFCOUNTED(retval)) { |
216 | 0 | *is_temp = 1; |
217 | 0 | return zend_array_dup(Z_ARRVAL(retval)); |
218 | 17 | } else if (Z_REFCOUNT(retval) <= 1) { |
219 | 15 | *is_temp = 1; |
220 | 15 | ht = Z_ARR(retval); |
221 | 15 | return ht; |
222 | 15 | } else { |
223 | 2 | *is_temp = 0; |
224 | 2 | zval_ptr_dtor(&retval); |
225 | 2 | return Z_ARRVAL(retval); |
226 | 2 | } |
227 | 17 | } else if (Z_TYPE(retval) == IS_NULL) { |
228 | 0 | zend_error(E_DEPRECATED, "Returning null from %s::__debugInfo() is deprecated, return an empty array instead", |
229 | 0 | ZSTR_VAL(ce->name)); |
230 | 0 | *is_temp = 1; |
231 | 0 | ht = zend_new_array(0); |
232 | 0 | return ht; |
233 | 0 | } |
234 | | |
235 | 2 | zend_error_noreturn(E_ERROR, ZEND_DEBUGINFO_FUNC_NAME "() must return an array"); |
236 | | |
237 | 0 | return NULL; /* Compilers are dumb and don't understand that noreturn means that the function does NOT need a return value... */ |
238 | 19 | } |
239 | | /* }}} */ |
240 | | |
241 | | static void zend_std_call_getter(zend_object *zobj, zend_string *prop_name, zval *retval) /* {{{ */ |
242 | 119 | { |
243 | 119 | zval member; |
244 | 119 | ZVAL_STR(&member, prop_name); |
245 | 119 | zend_call_known_instance_method_with_1_params(zobj->ce->__get, zobj, retval, &member); |
246 | 119 | } |
247 | | /* }}} */ |
248 | | |
249 | | static void zend_std_call_setter(zend_object *zobj, zend_string *prop_name, zval *value) /* {{{ */ |
250 | 87 | { |
251 | 87 | zval args[2]; |
252 | 87 | ZVAL_STR(&args[0], prop_name); |
253 | 87 | ZVAL_COPY_VALUE(&args[1], value); |
254 | 87 | zend_call_known_instance_method(zobj->ce->__set, zobj, NULL, 2, args); |
255 | 87 | } |
256 | | /* }}} */ |
257 | | |
258 | | static void zend_std_call_unsetter(zend_object *zobj, zend_string *prop_name) /* {{{ */ |
259 | 7 | { |
260 | 7 | zval member; |
261 | 7 | ZVAL_STR(&member, prop_name); |
262 | 7 | zend_call_known_instance_method_with_1_params(zobj->ce->__unset, zobj, NULL, &member); |
263 | 7 | } |
264 | | /* }}} */ |
265 | | |
266 | | static void zend_std_call_issetter(zend_object *zobj, zend_string *prop_name, zval *retval) /* {{{ */ |
267 | 0 | { |
268 | 0 | zval member; |
269 | 0 | ZVAL_STR(&member, prop_name); |
270 | 0 | zend_call_known_instance_method_with_1_params(zobj->ce->__isset, zobj, retval, &member); |
271 | 0 | } |
272 | | /* }}} */ |
273 | | |
274 | | |
275 | | static zend_always_inline bool is_derived_class(const zend_class_entry *child_class, const zend_class_entry *parent_class) /* {{{ */ |
276 | 741k | { |
277 | 741k | child_class = child_class->parent; |
278 | 1.10M | while (child_class) { |
279 | 738k | if (child_class == parent_class) { |
280 | 370k | return 1; |
281 | 370k | } |
282 | 367k | child_class = child_class->parent; |
283 | 367k | } |
284 | | |
285 | 370k | return 0; |
286 | 741k | } |
287 | | /* }}} */ |
288 | | |
289 | | static zend_never_inline int is_protected_compatible_scope(const zend_class_entry *ce, const zend_class_entry *scope) /* {{{ */ |
290 | 370k | { |
291 | 370k | return scope && |
292 | 370k | (ce == scope || is_derived_class(ce, scope) || is_derived_class(scope, ce)); |
293 | 370k | } |
294 | | /* }}} */ |
295 | | |
296 | | static zend_never_inline zend_property_info *zend_get_parent_private_property(const zend_class_entry *scope, const zend_class_entry *ce, zend_string *member) /* {{{ */ |
297 | 105 | { |
298 | 105 | zval *zv; |
299 | 105 | zend_property_info *prop_info; |
300 | | |
301 | 105 | if (scope != ce && scope && is_derived_class(ce, scope)) { |
302 | 87 | zv = zend_hash_find(&scope->properties_info, member); |
303 | 87 | if (zv != NULL) { |
304 | 87 | prop_info = (zend_property_info*)Z_PTR_P(zv); |
305 | 87 | if ((prop_info->flags & ZEND_ACC_PRIVATE) |
306 | 87 | && prop_info->ce == scope) { |
307 | 87 | return prop_info; |
308 | 87 | } |
309 | 87 | } |
310 | 87 | } |
311 | 18 | return NULL; |
312 | 105 | } |
313 | | /* }}} */ |
314 | | |
315 | | static ZEND_COLD zend_never_inline void zend_bad_property_access(const zend_property_info *property_info, const zend_class_entry *ce, const zend_string *member) /* {{{ */ |
316 | 11 | { |
317 | 11 | zend_throw_error(NULL, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ZSTR_VAL(ce->name), ZSTR_VAL(member)); |
318 | 11 | } |
319 | | /* }}} */ |
320 | | |
321 | | static ZEND_COLD zend_never_inline void zend_bad_property_name(void) /* {{{ */ |
322 | 23 | { |
323 | 23 | zend_throw_error(NULL, "Cannot access property starting with \"\\0\""); |
324 | 23 | } |
325 | | /* }}} */ |
326 | | |
327 | | static ZEND_COLD zend_never_inline void zend_forbidden_dynamic_property( |
328 | 3 | const zend_class_entry *ce, const zend_string *member) { |
329 | 3 | zend_throw_error(NULL, "Cannot create dynamic property %s::$%s", |
330 | 3 | ZSTR_VAL(ce->name), ZSTR_VAL(member)); |
331 | 3 | } |
332 | | |
333 | | static ZEND_COLD zend_never_inline bool zend_deprecated_dynamic_property( |
334 | 20.6k | zend_object *obj, const zend_string *member) { |
335 | 20.6k | GC_ADDREF(obj); |
336 | 20.6k | zend_error(E_DEPRECATED, "Creation of dynamic property %s::$%s is deprecated", |
337 | 20.6k | ZSTR_VAL(obj->ce->name), ZSTR_VAL(member)); |
338 | 20.6k | if (UNEXPECTED(GC_DELREF(obj) == 0)) { |
339 | 0 | const zend_class_entry *ce = obj->ce; |
340 | 0 | zend_objects_store_del(obj); |
341 | 0 | if (!EG(exception)) { |
342 | | /* We cannot continue execution and have to throw an exception */ |
343 | 0 | zend_throw_error(NULL, "Cannot create dynamic property %s::$%s", |
344 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(member)); |
345 | 0 | } |
346 | 0 | return 0; |
347 | 0 | } |
348 | 20.6k | return 1; |
349 | 20.6k | } |
350 | | |
351 | | static ZEND_COLD zend_never_inline void zend_readonly_property_unset_error( |
352 | 6 | const zend_class_entry *ce, const zend_string *member) { |
353 | 6 | zend_throw_error(NULL, "Cannot unset readonly property %s::$%s", |
354 | 6 | ZSTR_VAL(ce->name), ZSTR_VAL(member)); |
355 | 6 | } |
356 | | |
357 | | static zend_always_inline const zend_class_entry *get_fake_or_executed_scope(void) |
358 | 2.46M | { |
359 | 2.46M | if (UNEXPECTED(EG(fake_scope))) { |
360 | 2.46M | return EG(fake_scope); |
361 | 2.46M | } else { |
362 | 1.41k | return zend_get_executed_scope(); |
363 | 1.41k | } |
364 | 2.46M | } |
365 | | |
366 | | static zend_always_inline uintptr_t zend_get_property_offset(zend_class_entry *ce, zend_string *member, int silent, void **cache_slot, const zend_property_info **info_ptr) /* {{{ */ |
367 | 840k | { |
368 | 840k | zval *zv; |
369 | 840k | zend_property_info *property_info; |
370 | 840k | uint32_t flags; |
371 | 840k | uintptr_t offset; |
372 | | |
373 | 840k | if (cache_slot && EXPECTED(ce == CACHED_PTR_EX(cache_slot))) { |
374 | 1.10k | *info_ptr = CACHED_PTR_EX(cache_slot + 2); |
375 | 1.10k | return (uintptr_t)CACHED_PTR_EX(cache_slot + 1); |
376 | 1.10k | } |
377 | | |
378 | 839k | if (UNEXPECTED(zend_hash_num_elements(&ce->properties_info) == 0) |
379 | 787k | || UNEXPECTED((zv = zend_hash_find(&ce->properties_info, member)) == NULL)) { |
380 | 55.9k | if (UNEXPECTED(ZSTR_VAL(member)[0] == '\0') && ZSTR_LEN(member) != 0) { |
381 | 33 | if (!silent) { |
382 | 23 | zend_bad_property_name(); |
383 | 23 | } |
384 | 33 | return ZEND_WRONG_PROPERTY_OFFSET; |
385 | 33 | } |
386 | 55.9k | dynamic: |
387 | 55.9k | if (cache_slot) { |
388 | 5.06k | CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET); |
389 | 5.06k | CACHE_PTR_EX(cache_slot + 2, NULL); |
390 | 5.06k | } |
391 | 55.9k | return ZEND_DYNAMIC_PROPERTY_OFFSET; |
392 | 55.9k | } |
393 | | |
394 | 783k | property_info = (zend_property_info*)Z_PTR_P(zv); |
395 | 783k | flags = property_info->flags; |
396 | | |
397 | 783k | if (flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) { |
398 | 780k | const zend_class_entry *scope = get_fake_or_executed_scope(); |
399 | | |
400 | 780k | if (property_info->ce != scope) { |
401 | 370k | if (flags & ZEND_ACC_CHANGED) { |
402 | 51 | zend_property_info *p = zend_get_parent_private_property(scope, ce, member); |
403 | | |
404 | | /* If there is a public/protected instance property on ce, don't try to use a |
405 | | * private static property on scope. If both are static, prefer the static |
406 | | * property on scope. This will throw a static property notice, rather than |
407 | | * a visibility error. */ |
408 | 51 | if (p && (!(p->flags & ZEND_ACC_STATIC) || (flags & ZEND_ACC_STATIC))) { |
409 | 51 | property_info = p; |
410 | 51 | flags = property_info->flags; |
411 | 51 | goto found; |
412 | 51 | } else if (flags & ZEND_ACC_PUBLIC) { |
413 | 0 | goto found; |
414 | 0 | } |
415 | 51 | } |
416 | 370k | if (flags & ZEND_ACC_PRIVATE) { |
417 | 23 | if (property_info->ce != ce) { |
418 | 16 | goto dynamic; |
419 | 16 | } else { |
420 | 19 | wrong: |
421 | | /* Information was available, but we were denied access. Error out. */ |
422 | 19 | if (!silent) { |
423 | 11 | zend_bad_property_access(property_info, ce, member); |
424 | 11 | } |
425 | 19 | return ZEND_WRONG_PROPERTY_OFFSET; |
426 | 7 | } |
427 | 370k | } else { |
428 | 370k | ZEND_ASSERT(flags & ZEND_ACC_PROTECTED); |
429 | 370k | if (UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope))) { |
430 | 12 | goto wrong; |
431 | 12 | } |
432 | 370k | } |
433 | 370k | } |
434 | 780k | } |
435 | | |
436 | 783k | found: |
437 | 783k | if (UNEXPECTED(flags & ZEND_ACC_STATIC)) { |
438 | 1 | if (!silent) { |
439 | 1 | zend_error(E_NOTICE, "Accessing static property %s::$%s as non static", ZSTR_VAL(ce->name), ZSTR_VAL(member)); |
440 | 1 | } |
441 | 1 | return ZEND_DYNAMIC_PROPERTY_OFFSET; |
442 | 1 | } |
443 | | |
444 | 783k | if (property_info->hooks) { |
445 | 906 | *info_ptr = property_info; |
446 | 906 | if (cache_slot) { |
447 | 215 | CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)ZEND_HOOKED_PROPERTY_OFFSET); |
448 | 215 | CACHE_PTR_EX(cache_slot + 2, property_info); |
449 | 215 | } |
450 | 906 | return ZEND_HOOKED_PROPERTY_OFFSET; |
451 | 906 | } |
452 | | |
453 | 782k | offset = property_info->offset; |
454 | 782k | if (EXPECTED(!ZEND_TYPE_IS_SET(property_info->type))) { |
455 | 506k | property_info = NULL; |
456 | 506k | } else { |
457 | 276k | *info_ptr = property_info; |
458 | 276k | } |
459 | 782k | if (cache_slot) { |
460 | 2.82k | CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)(uintptr_t)offset); |
461 | 2.82k | CACHE_PTR_EX(cache_slot + 2, property_info); |
462 | 2.82k | } |
463 | 782k | return offset; |
464 | 783k | } |
465 | | /* }}} */ |
466 | | |
467 | | static ZEND_COLD void zend_wrong_offset(zend_class_entry *ce, zend_string *member) /* {{{ */ |
468 | 5 | { |
469 | 5 | const zend_property_info *dummy; |
470 | | |
471 | | /* Trigger the correct error */ |
472 | 5 | zend_get_property_offset(ce, member, 0, NULL, &dummy); |
473 | 5 | } |
474 | | /* }}} */ |
475 | | |
476 | | ZEND_API zend_property_info *zend_get_property_info(const zend_class_entry *ce, zend_string *member, int silent) /* {{{ */ |
477 | 1.69M | { |
478 | 1.69M | zval *zv; |
479 | 1.69M | zend_property_info *property_info; |
480 | 1.69M | uint32_t flags; |
481 | | |
482 | 1.69M | if (UNEXPECTED(zend_hash_num_elements(&ce->properties_info) == 0) |
483 | 1.68M | || EXPECTED((zv = zend_hash_find(&ce->properties_info, member)) == NULL)) { |
484 | 6.36k | if (UNEXPECTED(ZSTR_VAL(member)[0] == '\0') && ZSTR_LEN(member) != 0) { |
485 | 1.53k | if (!silent) { |
486 | 0 | zend_bad_property_name(); |
487 | 0 | } |
488 | 1.53k | return ZEND_WRONG_PROPERTY_INFO; |
489 | 1.53k | } |
490 | 4.87k | dynamic: |
491 | 4.87k | return NULL; |
492 | 6.36k | } |
493 | | |
494 | 1.68M | property_info = (zend_property_info*)Z_PTR_P(zv); |
495 | 1.68M | flags = property_info->flags; |
496 | | |
497 | 1.68M | if (flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) { |
498 | 1.68M | const zend_class_entry *scope = get_fake_or_executed_scope(); |
499 | 1.68M | if (property_info->ce != scope) { |
500 | 216 | if (flags & ZEND_ACC_CHANGED) { |
501 | 54 | zend_property_info *p = zend_get_parent_private_property(scope, ce, member); |
502 | | |
503 | 54 | if (p) { |
504 | 36 | property_info = p; |
505 | 36 | flags = property_info->flags; |
506 | 36 | goto found; |
507 | 36 | } else if (flags & ZEND_ACC_PUBLIC) { |
508 | 0 | goto found; |
509 | 0 | } |
510 | 54 | } |
511 | 180 | if (flags & ZEND_ACC_PRIVATE) { |
512 | 180 | if (property_info->ce != ce) { |
513 | 45 | goto dynamic; |
514 | 135 | } else { |
515 | 135 | wrong: |
516 | | /* Information was available, but we were denied access. Error out. */ |
517 | 135 | if (!silent) { |
518 | 0 | zend_bad_property_access(property_info, ce, member); |
519 | 0 | } |
520 | 135 | return ZEND_WRONG_PROPERTY_INFO; |
521 | 135 | } |
522 | 180 | } else { |
523 | 0 | ZEND_ASSERT(flags & ZEND_ACC_PROTECTED); |
524 | 0 | if (UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope))) { |
525 | 0 | goto wrong; |
526 | 0 | } |
527 | 0 | } |
528 | 180 | } |
529 | 1.68M | } |
530 | | |
531 | 1.68M | found: |
532 | 1.68M | if (UNEXPECTED(flags & ZEND_ACC_STATIC)) { |
533 | 534 | if (!silent) { |
534 | 0 | zend_error(E_NOTICE, "Accessing static property %s::$%s as non static", ZSTR_VAL(ce->name), ZSTR_VAL(member)); |
535 | 0 | } |
536 | 534 | } |
537 | 1.68M | return property_info; |
538 | 1.68M | } |
539 | | /* }}} */ |
540 | | |
541 | | ZEND_API zend_result zend_check_property_access(const zend_object *zobj, zend_string *prop_info_name, bool is_dynamic) /* {{{ */ |
542 | 902 | { |
543 | 902 | zend_property_info *property_info; |
544 | 902 | const char *class_name = NULL; |
545 | 902 | const char *prop_name; |
546 | 902 | zend_string *member; |
547 | 902 | size_t prop_name_len; |
548 | | |
549 | 902 | if (ZSTR_VAL(prop_info_name)[0] == 0) { |
550 | 264 | if (is_dynamic) { |
551 | 0 | return SUCCESS; |
552 | 0 | } |
553 | | |
554 | 264 | zend_unmangle_property_name_ex(prop_info_name, &class_name, &prop_name, &prop_name_len); |
555 | 264 | member = zend_string_init(prop_name, prop_name_len, 0); |
556 | 264 | property_info = zend_get_property_info(zobj->ce, member, 1); |
557 | 264 | zend_string_release_ex(member, 0); |
558 | 264 | if (property_info == NULL || property_info == ZEND_WRONG_PROPERTY_INFO) { |
559 | 180 | return FAILURE; |
560 | 180 | } |
561 | | |
562 | 84 | if (class_name[0] != '*') { |
563 | 66 | if (!(property_info->flags & ZEND_ACC_PRIVATE)) { |
564 | | /* we we're looking for a private prop but found a non private one of the same name */ |
565 | 0 | return FAILURE; |
566 | 66 | } else if (strcmp(ZSTR_VAL(prop_info_name)+1, ZSTR_VAL(property_info->name)+1)) { |
567 | | /* we we're looking for a private prop but found a private one of the same name but another class */ |
568 | 15 | return FAILURE; |
569 | 15 | } |
570 | 66 | } else { |
571 | | /* We were looking for a protected property but found a private one |
572 | | * belonging to the parent class. */ |
573 | 18 | if (property_info->flags & ZEND_ACC_PRIVATE) { |
574 | 0 | return FAILURE; |
575 | 0 | } |
576 | 18 | ZEND_ASSERT(property_info->flags & ZEND_ACC_PROTECTED); |
577 | 18 | } |
578 | 69 | return SUCCESS; |
579 | 638 | } else { |
580 | 638 | property_info = zend_get_property_info(zobj->ce, prop_info_name, 1); |
581 | 638 | if (property_info == NULL) { |
582 | 75 | ZEND_ASSERT(is_dynamic); |
583 | 75 | return SUCCESS; |
584 | 563 | } else if (property_info == ZEND_WRONG_PROPERTY_INFO) { |
585 | 0 | return FAILURE; |
586 | 0 | } |
587 | 563 | return (property_info->flags & ZEND_ACC_PUBLIC) ? SUCCESS : FAILURE; |
588 | 638 | } |
589 | 902 | } |
590 | | /* }}} */ |
591 | | |
592 | 635 | ZEND_API bool ZEND_FASTCALL zend_asymmetric_property_has_set_access(const zend_property_info *prop_info) { |
593 | 635 | ZEND_ASSERT(prop_info->flags & ZEND_ACC_PPP_SET_MASK); |
594 | 635 | ZEND_ASSERT(!(prop_info->flags & ZEND_ACC_PUBLIC_SET)); |
595 | 635 | const zend_class_entry *scope = get_fake_or_executed_scope(); |
596 | 635 | if (prop_info->ce == scope) { |
597 | 482 | return true; |
598 | 482 | } |
599 | 153 | return EXPECTED((prop_info->flags & ZEND_ACC_PROTECTED_SET) |
600 | 635 | && is_protected_compatible_scope(prop_info->prototype->ce, scope)); |
601 | 635 | } |
602 | | |
603 | 14 | static void zend_property_guard_dtor(zval *el) /* {{{ */ { |
604 | 14 | uint32_t *ptr = (uint32_t*)Z_PTR_P(el); |
605 | 14 | if (EXPECTED(!(((uintptr_t)ptr) & 1))) { |
606 | 7 | efree_size(ptr, sizeof(uint32_t)); |
607 | 7 | } |
608 | 14 | } |
609 | | /* }}} */ |
610 | | |
611 | | static zend_always_inline zval *zend_get_guard_value(zend_object *zobj) |
612 | 274 | { |
613 | 274 | return zobj->properties_table + zobj->ce->default_properties_count; |
614 | 274 | } |
615 | | |
616 | | ZEND_API uint32_t *zend_get_property_guard(zend_object *zobj, zend_string *member) /* {{{ */ |
617 | 240 | { |
618 | 240 | HashTable *guards; |
619 | 240 | zval *zv; |
620 | 240 | uint32_t *ptr; |
621 | | |
622 | | |
623 | 240 | ZEND_ASSERT(zobj->ce->ce_flags & ZEND_ACC_USE_GUARDS); |
624 | 240 | zv = zend_get_guard_value(zobj); |
625 | 240 | if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) { |
626 | 162 | zend_string *str = Z_STR_P(zv); |
627 | 162 | if (EXPECTED(str == member) || |
628 | | /* str and member don't necessarily have a pre-calculated hash value here */ |
629 | 125 | EXPECTED(zend_string_equal_content(str, member))) { |
630 | 40 | return &Z_GUARD_P(zv); |
631 | 122 | } else if (EXPECTED(Z_GUARD_P(zv) == 0)) { |
632 | 115 | zval_ptr_dtor_str(zv); |
633 | 115 | ZVAL_STR_COPY(zv, member); |
634 | 115 | return &Z_GUARD_P(zv); |
635 | 115 | } else { |
636 | 7 | ALLOC_HASHTABLE(guards); |
637 | 7 | zend_hash_init(guards, 8, NULL, zend_property_guard_dtor, 0); |
638 | | /* mark pointer as "special" using low bit */ |
639 | 7 | zend_hash_add_new_ptr(guards, str, |
640 | 7 | (void*)(((uintptr_t)&Z_GUARD_P(zv)) | 1)); |
641 | 7 | zval_ptr_dtor_str(zv); |
642 | 7 | ZVAL_ARR(zv, guards); |
643 | 7 | } |
644 | 162 | } else if (EXPECTED(Z_TYPE_P(zv) == IS_ARRAY)) { |
645 | 20 | guards = Z_ARRVAL_P(zv); |
646 | 20 | ZEND_ASSERT(guards != NULL); |
647 | 20 | zv = zend_hash_find(guards, member); |
648 | 20 | if (zv != NULL) { |
649 | 20 | return (uint32_t*)(((uintptr_t)Z_PTR_P(zv)) & ~1); |
650 | 20 | } |
651 | 58 | } else { |
652 | 58 | ZEND_ASSERT(Z_TYPE_P(zv) == IS_UNDEF); |
653 | 58 | ZVAL_STR_COPY(zv, member); |
654 | 58 | Z_GUARD_P(zv) &= ~ZEND_GUARD_PROPERTY_MASK; |
655 | 58 | return &Z_GUARD_P(zv); |
656 | 58 | } |
657 | | /* we have to allocate uint32_t separately because ht->arData may be reallocated */ |
658 | 7 | ptr = (uint32_t*)emalloc(sizeof(uint32_t)); |
659 | 7 | *ptr = 0; |
660 | 7 | return (uint32_t*)zend_hash_add_new_ptr(guards, member, ptr); |
661 | 240 | } |
662 | | /* }}} */ |
663 | | |
664 | | ZEND_API uint32_t *zend_get_recursion_guard(zend_object *zobj) |
665 | 2.15k | { |
666 | 2.15k | if (!(zobj->ce->ce_flags & ZEND_ACC_USE_GUARDS)) { |
667 | 2.12k | return NULL; |
668 | 2.12k | } |
669 | 34 | zval *zv = zend_get_guard_value(zobj); |
670 | 34 | return &Z_GUARD_P(zv); |
671 | 2.15k | } |
672 | | |
673 | | ZEND_COLD static void zend_typed_property_uninitialized_access(const zend_property_info *prop_info, zend_string *name) |
674 | 38 | { |
675 | 38 | zend_throw_error(NULL, "Typed property %s::$%s must not be accessed before initialization", |
676 | 38 | ZSTR_VAL(prop_info->ce->name), |
677 | 38 | ZSTR_VAL(name)); |
678 | 38 | } |
679 | | |
680 | | static ZEND_FUNCTION(zend_parent_hook_get_trampoline); |
681 | | static ZEND_FUNCTION(zend_parent_hook_set_trampoline); |
682 | | |
683 | | static bool zend_is_in_hook(const zend_property_info *prop_info) |
684 | 934 | { |
685 | 934 | const zend_execute_data *execute_data = EG(current_execute_data); |
686 | 934 | if (!execute_data || !EX(func) || !EX(func)->common.prop_info) { |
687 | 688 | return false; |
688 | 688 | } |
689 | | |
690 | 246 | const zend_property_info *parent_info = EX(func)->common.prop_info; |
691 | 246 | ZEND_ASSERT(prop_info->prototype && parent_info->prototype); |
692 | 246 | return prop_info->prototype == parent_info->prototype; |
693 | 246 | } |
694 | | |
695 | | static bool zend_should_call_hook(const zend_property_info *prop_info, const zend_object *obj) |
696 | 924 | { |
697 | 924 | if (!zend_is_in_hook(prop_info)) { |
698 | 678 | return true; |
699 | 678 | } |
700 | | |
701 | | /* execute_data and This are guaranteed to be set if zend_is_in_hook() returns true. */ |
702 | 246 | zend_object *parent_obj = Z_OBJ(EG(current_execute_data)->This); |
703 | 246 | if (parent_obj == obj) { |
704 | 243 | return false; |
705 | 243 | } |
706 | | |
707 | 3 | if (zend_object_is_lazy_proxy(parent_obj) |
708 | 3 | && zend_lazy_object_initialized(parent_obj) |
709 | 3 | && zend_lazy_object_get_instance(parent_obj) == obj) { |
710 | 3 | return false; |
711 | 3 | } |
712 | | |
713 | 0 | return true; |
714 | 3 | } |
715 | | |
716 | | static ZEND_COLD void zend_throw_no_prop_backing_value_access(const zend_string *class_name, const zend_string *prop_name, bool is_read) |
717 | 0 | { |
718 | 0 | zend_throw_error(NULL, "Must not %s virtual property %s::$%s", |
719 | 0 | is_read ? "read from" : "write to", |
720 | 0 | ZSTR_VAL(class_name), ZSTR_VAL(prop_name)); |
721 | 0 | } |
722 | | |
723 | | static bool zend_call_get_hook( |
724 | | const zend_property_info *prop_info, const zend_string *prop_name, |
725 | | zend_function *get, zend_object *zobj, zval *rv) |
726 | 792 | { |
727 | 792 | if (!zend_should_call_hook(prop_info, zobj)) { |
728 | 195 | if (UNEXPECTED(prop_info->flags & ZEND_ACC_VIRTUAL)) { |
729 | 0 | zend_throw_no_prop_backing_value_access(zobj->ce->name, prop_name, /* is_read */ true); |
730 | 0 | } |
731 | 195 | return false; |
732 | 195 | } |
733 | | |
734 | 597 | GC_ADDREF(zobj); |
735 | 597 | zend_call_known_instance_method_with_0_params(get, zobj, rv); |
736 | 597 | OBJ_RELEASE(zobj); |
737 | | |
738 | 597 | return true; |
739 | 792 | } |
740 | | |
741 | | ZEND_API zval *zend_std_read_property(zend_object *zobj, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */ |
742 | 312k | { |
743 | 312k | zval *retval; |
744 | 312k | uintptr_t property_offset; |
745 | 312k | const zend_property_info *prop_info = NULL; |
746 | 312k | uint32_t *guard = NULL; |
747 | | |
748 | | #if DEBUG_OBJECT_HANDLERS |
749 | | fprintf(stderr, "Read object #%d property: %s\n", zobj->handle, ZSTR_VAL(name)); |
750 | | #endif |
751 | | |
752 | | /* make zend_get_property_info silent if we have getter - we may want to use it */ |
753 | 312k | property_offset = zend_get_property_offset(zobj->ce, name, (type == BP_VAR_IS) || (zobj->ce->__get != NULL), cache_slot, &prop_info); |
754 | | |
755 | 312k | if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) { |
756 | 307k | try_again: |
757 | 307k | retval = OBJ_PROP(zobj, property_offset); |
758 | | |
759 | 307k | if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK)) |
760 | 234 | && (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) |
761 | 52 | && ((prop_info->flags & ZEND_ACC_READONLY) || !zend_asymmetric_property_has_set_access(prop_info))) { |
762 | 52 | if (Z_TYPE_P(retval) == IS_OBJECT) { |
763 | | /* For objects, W/RW/UNSET fetch modes might not actually modify object. |
764 | | * Similar as with magic __get() allow them, but return the value as a copy |
765 | | * to make sure no actual modification is possible. */ |
766 | 6 | ZVAL_COPY(rv, retval); |
767 | 6 | retval = rv; |
768 | 6 | goto exit; |
769 | 46 | } else if (Z_TYPE_P(retval) == IS_UNDEF && type == BP_VAR_UNSET) { |
770 | 9 | retval = &EG(uninitialized_zval); |
771 | 9 | goto exit; |
772 | 9 | } |
773 | 37 | if (prop_info->flags & ZEND_ACC_READONLY) { |
774 | 22 | zend_readonly_property_indirect_modification_error(prop_info); |
775 | 22 | } else { |
776 | 15 | zend_asymmetric_visibility_property_modification_error(prop_info, "indirectly modify"); |
777 | 15 | } |
778 | 37 | retval = &EG(uninitialized_zval); |
779 | 37 | goto exit; |
780 | 52 | } |
781 | 307k | if (EXPECTED(Z_TYPE_P(retval) != IS_UNDEF)) { |
782 | 307k | goto exit; |
783 | 307k | } |
784 | 80 | if (UNEXPECTED(Z_PROP_FLAG_P(retval) & IS_PROP_UNINIT)) { |
785 | | /* Skip __get() for uninitialized typed properties */ |
786 | 72 | goto uninit_error; |
787 | 72 | } |
788 | 5.53k | } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) { |
789 | 4.72k | if (EXPECTED(zobj->properties != NULL)) { |
790 | 603 | if (!IS_UNKNOWN_DYNAMIC_PROPERTY_OFFSET(property_offset)) { |
791 | 0 | uintptr_t idx = ZEND_DECODE_DYN_PROP_OFFSET(property_offset); |
792 | |
|
793 | 0 | if (EXPECTED(idx < zobj->properties->nNumUsed * sizeof(Bucket))) { |
794 | 0 | Bucket *p = (Bucket*)((char*)zobj->properties->arData + idx); |
795 | |
|
796 | 0 | if (EXPECTED(p->key == name) || |
797 | 0 | (EXPECTED(p->h == ZSTR_H(name)) && |
798 | 0 | EXPECTED(p->key != NULL) && |
799 | 0 | EXPECTED(zend_string_equal_content(p->key, name)))) { |
800 | 0 | retval = &p->val; |
801 | 0 | goto exit; |
802 | 0 | } |
803 | 0 | } |
804 | 0 | CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET); |
805 | 0 | } |
806 | 603 | retval = zend_hash_find(zobj->properties, name); |
807 | 603 | if (EXPECTED(retval)) { |
808 | 238 | if (cache_slot) { |
809 | 32 | uintptr_t idx = (char*)retval - (char*)zobj->properties->arData; |
810 | 32 | CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_ENCODE_DYN_PROP_OFFSET(idx)); |
811 | 32 | } |
812 | 238 | goto exit; |
813 | 238 | } |
814 | 603 | } |
815 | 4.72k | } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) { |
816 | 802 | zend_function *get = prop_info->hooks[ZEND_PROPERTY_HOOK_GET]; |
817 | 802 | if (!get) { |
818 | 10 | if (prop_info->flags & ZEND_ACC_VIRTUAL) { |
819 | 0 | zend_throw_error(NULL, "Cannot read from set-only virtual property %s::$%s", |
820 | 0 | ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name)); |
821 | 0 | return &EG(uninitialized_zval); |
822 | 0 | } |
823 | | /* Cache the fact that this hook has trivial read. This only applies to |
824 | | * BP_VAR_R and BP_VAR_IS fetches. */ |
825 | 10 | ZEND_SET_PROPERTY_HOOK_SIMPLE_READ(cache_slot); |
826 | | |
827 | 10 | retval = OBJ_PROP(zobj, prop_info->offset); |
828 | 10 | if (UNEXPECTED(Z_TYPE_P(retval) == IS_UNDEF)) { |
829 | | /* As hooked properties can't be unset, the only way to end up with an undef |
830 | | * value is via an uninitialized property. */ |
831 | 3 | ZEND_ASSERT(Z_PROP_FLAG_P(retval) & IS_PROP_UNINIT); |
832 | 3 | goto uninit_error; |
833 | 3 | } |
834 | | |
835 | 7 | if (UNEXPECTED(type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)) { |
836 | 0 | if (UNEXPECTED(Z_TYPE_P(retval) != IS_OBJECT)) { |
837 | 0 | zend_throw_error(NULL, "Indirect modification of %s::$%s is not allowed", |
838 | 0 | ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name)); |
839 | 0 | goto exit; |
840 | 0 | } |
841 | 0 | ZVAL_COPY(rv, retval); |
842 | 0 | retval = rv; |
843 | 0 | } |
844 | 7 | goto exit; |
845 | 7 | } |
846 | | |
847 | 792 | const zend_class_entry *ce = zobj->ce; |
848 | | |
849 | 792 | if (!zend_call_get_hook(prop_info, name, get, zobj, rv)) { |
850 | 195 | if (EG(exception)) { |
851 | 0 | return &EG(uninitialized_zval); |
852 | 0 | } |
853 | | |
854 | | /* Reads from backing store can only occur in hooks, and hence will always remain simple. */ |
855 | 195 | const zend_execute_data *execute_data = EG(current_execute_data); |
856 | 195 | if (cache_slot && EX(opline) && EX(opline)->opcode == ZEND_FETCH_OBJ_R && EX(opline)->op1_type == IS_UNUSED) { |
857 | 117 | ZEND_SET_PROPERTY_HOOK_SIMPLE_READ(cache_slot); |
858 | 117 | } |
859 | | |
860 | 195 | property_offset = prop_info->offset; |
861 | 195 | if (!ZEND_TYPE_IS_SET(prop_info->type)) { |
862 | 156 | prop_info = NULL; |
863 | 156 | } |
864 | 195 | goto try_again; |
865 | 195 | } |
866 | | |
867 | 597 | if (EXPECTED(cache_slot |
868 | 597 | && zend_execute_ex == execute_ex |
869 | 597 | && ce->default_object_handlers->read_property == zend_std_read_property |
870 | 597 | && !ce->create_object |
871 | 597 | && !zend_is_in_hook(prop_info) |
872 | 597 | && !(prop_info->hooks[ZEND_PROPERTY_HOOK_GET]->common.fn_flags & ZEND_ACC_RETURN_REFERENCE))) { |
873 | 10 | ZEND_SET_PROPERTY_HOOK_SIMPLE_GET(cache_slot); |
874 | 10 | } |
875 | | |
876 | 597 | if (Z_TYPE_P(rv) != IS_UNDEF) { |
877 | 589 | retval = rv; |
878 | 589 | if (!Z_ISREF_P(rv) |
879 | 494 | && (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) |
880 | 0 | && UNEXPECTED(Z_TYPE_P(rv) != IS_OBJECT)) { |
881 | 0 | zend_throw_error(NULL, "Indirect modification of %s::$%s is not allowed", |
882 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
883 | 0 | } |
884 | 589 | } else { |
885 | 8 | retval = &EG(uninitialized_zval); |
886 | 8 | } |
887 | | |
888 | 597 | goto exit; |
889 | 792 | } else if (UNEXPECTED(EG(exception))) { |
890 | 0 | retval = &EG(uninitialized_zval); |
891 | 0 | goto exit; |
892 | 0 | } |
893 | | |
894 | 4.50k | retval = &EG(uninitialized_zval); |
895 | | |
896 | | /* magic isset */ |
897 | 4.50k | if ((type == BP_VAR_IS) && zobj->ce->__isset) { |
898 | 0 | zval tmp_result; |
899 | 0 | guard = zend_get_property_guard(zobj, name); |
900 | |
|
901 | 0 | if (!((*guard) & IN_ISSET)) { |
902 | 0 | GC_ADDREF(zobj); |
903 | |
|
904 | 0 | *guard |= IN_ISSET; |
905 | 0 | zend_std_call_issetter(zobj, name, &tmp_result); |
906 | 0 | *guard &= ~IN_ISSET; |
907 | |
|
908 | 0 | if (!zend_is_true(&tmp_result)) { |
909 | 0 | retval = &EG(uninitialized_zval); |
910 | 0 | OBJ_RELEASE(zobj); |
911 | 0 | zval_ptr_dtor(&tmp_result); |
912 | 0 | goto exit; |
913 | 0 | } |
914 | | |
915 | 0 | zval_ptr_dtor(&tmp_result); |
916 | 0 | if (zobj->ce->__get && !((*guard) & IN_GET)) { |
917 | 0 | goto call_getter; |
918 | 0 | } |
919 | 0 | OBJ_RELEASE(zobj); |
920 | 0 | } else if (zobj->ce->__get && !((*guard) & IN_GET)) { |
921 | 0 | goto call_getter_addref; |
922 | 0 | } |
923 | 4.50k | } else if (zobj->ce->__get) { |
924 | | /* magic get */ |
925 | 125 | guard = zend_get_property_guard(zobj, name); |
926 | 125 | if (!((*guard) & IN_GET)) { |
927 | | /* have getter - try with it! */ |
928 | 119 | call_getter_addref: |
929 | 119 | GC_ADDREF(zobj); |
930 | 119 | call_getter: |
931 | 119 | *guard |= IN_GET; /* prevent circular getting */ |
932 | 119 | zend_std_call_getter(zobj, name, rv); |
933 | 119 | *guard &= ~IN_GET; |
934 | | |
935 | 119 | if (Z_TYPE_P(rv) != IS_UNDEF) { |
936 | 115 | retval = rv; |
937 | 115 | if (!Z_ISREF_P(rv) && |
938 | 110 | (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)) { |
939 | 10 | if (UNEXPECTED(Z_TYPE_P(rv) != IS_OBJECT)) { |
940 | 0 | zend_error(E_NOTICE, "Indirect modification of overloaded property %s::$%s has no effect", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name)); |
941 | 0 | } |
942 | 10 | } |
943 | 115 | } else { |
944 | 4 | retval = &EG(uninitialized_zval); |
945 | 4 | } |
946 | | |
947 | 119 | if (prop_info) { |
948 | 2 | zend_verify_prop_assignable_by_ref_ex(prop_info, retval, (zobj->ce->__get->common.fn_flags & ZEND_ACC_STRICT_TYPES) != 0, ZEND_VERIFY_PROP_ASSIGNABLE_BY_REF_CONTEXT_MAGIC_GET); |
949 | 2 | } |
950 | | |
951 | 119 | OBJ_RELEASE(zobj); |
952 | 119 | goto exit; |
953 | 119 | } else if (UNEXPECTED(IS_WRONG_PROPERTY_OFFSET(property_offset))) { |
954 | | /* Trigger the correct error */ |
955 | 2 | zend_wrong_offset(zobj->ce, name); |
956 | 2 | ZEND_ASSERT(EG(exception)); |
957 | 2 | retval = &EG(uninitialized_zval); |
958 | 2 | goto exit; |
959 | 2 | } |
960 | 125 | } |
961 | | |
962 | 4.45k | uninit_error: |
963 | 4.45k | if (UNEXPECTED(zend_lazy_object_must_init(zobj))) { |
964 | 65 | if (!prop_info || (Z_PROP_FLAG_P(retval) & IS_PROP_LAZY)) { |
965 | 65 | zobj = zend_lazy_object_init(zobj); |
966 | 65 | if (!zobj) { |
967 | 33 | retval = &EG(uninitialized_zval); |
968 | 33 | goto exit; |
969 | 33 | } |
970 | | |
971 | 32 | if (UNEXPECTED(guard)) { |
972 | 0 | uint32_t guard_type = (type == BP_VAR_IS) && zobj->ce->__isset |
973 | 0 | ? IN_ISSET : IN_GET; |
974 | 0 | guard = zend_get_property_guard(zobj, name); |
975 | 0 | if (!((*guard) & guard_type)) { |
976 | 0 | (*guard) |= guard_type; |
977 | 0 | retval = zend_std_read_property(zobj, name, type, cache_slot, rv); |
978 | 0 | (*guard) &= ~guard_type; |
979 | 0 | return retval; |
980 | 0 | } |
981 | 0 | } |
982 | | |
983 | 32 | return zend_std_read_property(zobj, name, type, cache_slot, rv); |
984 | 32 | } |
985 | 65 | } |
986 | 4.39k | if (type != BP_VAR_IS) { |
987 | 4.26k | if (prop_info) { |
988 | 20 | zend_typed_property_uninitialized_access(prop_info, name); |
989 | 4.24k | } else { |
990 | 4.24k | zend_error(E_WARNING, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name)); |
991 | 4.24k | } |
992 | 4.26k | } |
993 | 4.39k | retval = &EG(uninitialized_zval); |
994 | | |
995 | 312k | exit: |
996 | 312k | return retval; |
997 | 4.39k | } |
998 | | /* }}} */ |
999 | | |
1000 | 499k | static zend_always_inline bool property_uses_strict_types(void) { |
1001 | 499k | const zend_execute_data *execute_data = EG(current_execute_data); |
1002 | 499k | return execute_data |
1003 | 328k | && execute_data->func |
1004 | 328k | && ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)); |
1005 | 499k | } |
1006 | | |
1007 | | static zval *forward_write_to_lazy_object(zend_object *zobj, |
1008 | | zend_string *name, zval *value, void **cache_slot, bool guarded) |
1009 | 15 | { |
1010 | 15 | zval *variable_ptr; |
1011 | | |
1012 | | /* backup value as it may change during initialization */ |
1013 | 15 | zval backup; |
1014 | 15 | ZVAL_COPY(&backup, value); |
1015 | | |
1016 | 15 | zend_object *instance = zend_lazy_object_init(zobj); |
1017 | 15 | if (UNEXPECTED(!instance)) { |
1018 | 12 | zval_ptr_dtor(&backup); |
1019 | 12 | return &EG(error_zval); |
1020 | 12 | } |
1021 | | |
1022 | 3 | if (UNEXPECTED(guarded)) { |
1023 | 0 | uint32_t *guard = zend_get_property_guard(instance, name); |
1024 | 0 | if (!((*guard) & IN_SET)) { |
1025 | 0 | (*guard) |= IN_SET; |
1026 | 0 | variable_ptr = zend_std_write_property(instance, name, &backup, cache_slot); |
1027 | 0 | (*guard) &= ~IN_SET; |
1028 | 0 | goto exit; |
1029 | 0 | } |
1030 | 0 | } |
1031 | | |
1032 | 3 | variable_ptr = zend_std_write_property(instance, name, &backup, cache_slot); |
1033 | | |
1034 | 3 | exit: |
1035 | 3 | zval_ptr_dtor(&backup); |
1036 | | |
1037 | 3 | if (variable_ptr == &backup) { |
1038 | 0 | variable_ptr = value; |
1039 | 0 | } |
1040 | | |
1041 | 3 | return variable_ptr; |
1042 | 3 | } |
1043 | | |
1044 | | ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zval *value, void **cache_slot) /* {{{ */ |
1045 | 526k | { |
1046 | 526k | zval *variable_ptr, tmp; |
1047 | 526k | uintptr_t property_offset; |
1048 | 526k | const zend_property_info *prop_info = NULL; |
1049 | 526k | uint32_t *guard = NULL; |
1050 | 526k | ZEND_ASSERT(!Z_ISREF_P(value)); |
1051 | | |
1052 | 526k | property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__set != NULL), cache_slot, &prop_info); |
1053 | | |
1054 | 526k | if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) { |
1055 | 475k | try_again: |
1056 | 475k | variable_ptr = OBJ_PROP(zobj, property_offset); |
1057 | | |
1058 | 475k | if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))) { |
1059 | 523 | bool error; |
1060 | 523 | if (Z_TYPE_P(variable_ptr) != IS_UNDEF || (Z_PROP_FLAG_P(variable_ptr) & IS_PROP_UNINIT) || !zobj->ce->__set) { |
1061 | 523 | error = true; |
1062 | 523 | } else { |
1063 | 0 | guard = zend_get_property_guard(zobj, name); |
1064 | 0 | error = (*guard) & IN_SET; |
1065 | 0 | } |
1066 | 523 | if (error) { |
1067 | 523 | if ((prop_info->flags & ZEND_ACC_READONLY) |
1068 | 300 | && Z_TYPE_P(variable_ptr) != IS_UNDEF |
1069 | 17 | && !(Z_PROP_FLAG_P(variable_ptr) & IS_PROP_REINITABLE)) { |
1070 | 17 | zend_readonly_property_modification_error(prop_info); |
1071 | 17 | variable_ptr = &EG(error_zval); |
1072 | 17 | goto exit; |
1073 | 17 | } |
1074 | 506 | if ((prop_info->flags & ZEND_ACC_PPP_SET_MASK) && !zend_asymmetric_property_has_set_access(prop_info)) { |
1075 | 33 | zend_asymmetric_visibility_property_modification_error(prop_info, "modify"); |
1076 | 33 | variable_ptr = &EG(error_zval); |
1077 | 33 | goto exit; |
1078 | 33 | } |
1079 | 506 | } |
1080 | 523 | } |
1081 | | |
1082 | 475k | if (Z_TYPE_P(variable_ptr) != IS_UNDEF) { |
1083 | 474k | Z_TRY_ADDREF_P(value); |
1084 | | |
1085 | 474k | if (prop_info) { |
1086 | 14.7k | typed_property: |
1087 | 14.7k | ZVAL_COPY_VALUE(&tmp, value); |
1088 | | // Increase refcount to prevent object from being released in __toString() |
1089 | 14.7k | GC_ADDREF(zobj); |
1090 | 14.7k | bool type_matched = zend_verify_property_type(prop_info, &tmp, property_uses_strict_types()); |
1091 | 14.7k | if (UNEXPECTED(GC_DELREF(zobj) == 0)) { |
1092 | 2 | zend_object_released_while_assigning_to_property_error(prop_info); |
1093 | 2 | zend_objects_store_del(zobj); |
1094 | 2 | zval_ptr_dtor(&tmp); |
1095 | 2 | variable_ptr = &EG(error_zval); |
1096 | 2 | goto exit; |
1097 | 2 | } |
1098 | 14.7k | if (UNEXPECTED(!type_matched)) { |
1099 | 77 | zval_ptr_dtor(&tmp); |
1100 | 77 | variable_ptr = &EG(error_zval); |
1101 | 77 | goto exit; |
1102 | 77 | } |
1103 | 14.6k | Z_PROP_FLAG_P(variable_ptr) &= ~(IS_PROP_UNINIT|IS_PROP_REINITABLE); |
1104 | 14.6k | value = &tmp; |
1105 | 14.6k | } |
1106 | | |
1107 | 484k | found:; |
1108 | 484k | zend_refcounted *garbage = NULL; |
1109 | | |
1110 | 484k | variable_ptr = zend_assign_to_variable_ex( |
1111 | 484k | variable_ptr, value, IS_TMP_VAR, property_uses_strict_types(), &garbage); |
1112 | | |
1113 | 484k | if (garbage) { |
1114 | 3.35k | if (GC_DELREF(garbage) == 0) { |
1115 | 2.33k | zend_execute_data *execute_data = EG(current_execute_data); |
1116 | | // Assign to result variable before calling the destructor as it may release the object |
1117 | 2.33k | if (execute_data |
1118 | 56 | && EX(func) |
1119 | 56 | && ZEND_USER_CODE(EX(func)->common.type) |
1120 | 53 | && EX(opline) |
1121 | 53 | && EX(opline)->opcode == ZEND_ASSIGN_OBJ |
1122 | 53 | && EX(opline)->result_type) { |
1123 | 21 | ZVAL_COPY_DEREF(EX_VAR(EX(opline)->result.var), variable_ptr); |
1124 | 21 | variable_ptr = NULL; |
1125 | 21 | } |
1126 | 2.33k | rc_dtor_func(garbage); |
1127 | 2.33k | } else { |
1128 | 1.01k | gc_check_possible_root_no_ref(garbage); |
1129 | 1.01k | } |
1130 | 3.35k | } |
1131 | 484k | goto exit; |
1132 | 474k | } |
1133 | 1.00k | if (Z_PROP_FLAG_P(variable_ptr) & IS_PROP_UNINIT) { |
1134 | 991 | if (UNEXPECTED(zend_lazy_object_must_init(zobj))) { |
1135 | 99 | if (Z_PROP_FLAG_P(variable_ptr) & IS_PROP_LAZY) { |
1136 | 15 | goto lazy_init; |
1137 | 15 | } |
1138 | 99 | } |
1139 | | /* Writes to uninitialized typed properties bypass __set(). */ |
1140 | 976 | goto write_std_property; |
1141 | 991 | } |
1142 | 51.0k | } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) { |
1143 | 50.9k | if (EXPECTED(zobj->properties != NULL)) { |
1144 | 20.7k | if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) { |
1145 | 6 | if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) { |
1146 | 6 | GC_DELREF(zobj->properties); |
1147 | 6 | } |
1148 | 6 | zobj->properties = zend_array_dup(zobj->properties); |
1149 | 6 | } |
1150 | 20.7k | if ((variable_ptr = zend_hash_find(zobj->properties, name)) != NULL) { |
1151 | 9.28k | Z_TRY_ADDREF_P(value); |
1152 | 9.28k | goto found; |
1153 | 9.28k | } |
1154 | 20.7k | } |
1155 | 50.9k | } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) { |
1156 | 134 | zend_function *set = prop_info->hooks[ZEND_PROPERTY_HOOK_SET]; |
1157 | | |
1158 | 134 | if (!set) { |
1159 | 2 | if (prop_info->flags & ZEND_ACC_VIRTUAL) { |
1160 | 1 | zend_throw_error(NULL, "Cannot write to get-only virtual property %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name)); |
1161 | 1 | variable_ptr = &EG(error_zval); |
1162 | 1 | goto exit; |
1163 | 1 | } |
1164 | 1 | ZEND_SET_PROPERTY_HOOK_SIMPLE_WRITE(cache_slot); |
1165 | 1 | property_offset = prop_info->offset; |
1166 | 1 | if (!ZEND_TYPE_IS_SET(prop_info->type)) { |
1167 | 1 | prop_info = NULL; |
1168 | 1 | } |
1169 | 1 | goto try_again; |
1170 | 2 | } |
1171 | | |
1172 | 132 | if (!zend_should_call_hook(prop_info, zobj)) { |
1173 | 51 | if (prop_info->flags & ZEND_ACC_VIRTUAL) { |
1174 | 0 | zend_throw_no_prop_backing_value_access(zobj->ce->name, name, /* is_read */ false); |
1175 | 0 | variable_ptr = &EG(error_zval); |
1176 | 0 | goto exit; |
1177 | 0 | } |
1178 | | |
1179 | | /* Writes to backing store can only occur in hooks, and hence will always remain simple. */ |
1180 | 51 | zend_execute_data *execute_data = EG(current_execute_data); |
1181 | 51 | if (cache_slot && EX(opline) && EX(opline)->opcode == ZEND_ASSIGN_OBJ && EX(opline)->op1_type == IS_UNUSED) { |
1182 | 50 | ZEND_SET_PROPERTY_HOOK_SIMPLE_WRITE(cache_slot); |
1183 | 50 | } |
1184 | | |
1185 | 51 | property_offset = prop_info->offset; |
1186 | 51 | if (!ZEND_TYPE_IS_SET(prop_info->type)) { |
1187 | 12 | prop_info = NULL; |
1188 | 12 | } |
1189 | 51 | goto try_again; |
1190 | 51 | } |
1191 | | |
1192 | 81 | if (UNEXPECTED(prop_info->flags & ZEND_ACC_PPP_SET_MASK |
1193 | 81 | && !zend_asymmetric_property_has_set_access(prop_info))) { |
1194 | 0 | zend_asymmetric_visibility_property_modification_error(prop_info, "modify"); |
1195 | 0 | variable_ptr = &EG(error_zval); |
1196 | 0 | goto exit; |
1197 | 0 | } |
1198 | | |
1199 | 81 | GC_ADDREF(zobj); |
1200 | 81 | zend_call_known_instance_method_with_1_params(set, zobj, NULL, value); |
1201 | 81 | OBJ_RELEASE(zobj); |
1202 | | |
1203 | 81 | variable_ptr = value; |
1204 | 81 | goto exit; |
1205 | 81 | } else if (UNEXPECTED(EG(exception))) { |
1206 | 28 | variable_ptr = &EG(error_zval); |
1207 | 28 | goto exit; |
1208 | 28 | } |
1209 | | |
1210 | | /* magic set */ |
1211 | 41.6k | if (zobj->ce->__set) { |
1212 | 90 | if (!guard) { |
1213 | 90 | guard = zend_get_property_guard(zobj, name); |
1214 | 90 | } |
1215 | | |
1216 | 90 | if (!((*guard) & IN_SET)) { |
1217 | 87 | GC_ADDREF(zobj); |
1218 | 87 | (*guard) |= IN_SET; /* prevent circular setting */ |
1219 | 87 | zend_std_call_setter(zobj, name, value); |
1220 | 87 | (*guard) &= ~IN_SET; |
1221 | 87 | OBJ_RELEASE(zobj); |
1222 | 87 | variable_ptr = value; |
1223 | 87 | } else if (EXPECTED(!IS_WRONG_PROPERTY_OFFSET(property_offset))) { |
1224 | 1 | if (UNEXPECTED(zend_lazy_object_must_init(zobj))) { |
1225 | 0 | return forward_write_to_lazy_object(zobj, name, value, |
1226 | 0 | cache_slot, /* guarded */ true); |
1227 | 0 | } |
1228 | | |
1229 | 1 | goto write_std_property; |
1230 | 2 | } else { |
1231 | | /* Trigger the correct error */ |
1232 | 2 | zend_wrong_offset(zobj->ce, name); |
1233 | 2 | ZEND_ASSERT(EG(exception)); |
1234 | 2 | variable_ptr = &EG(error_zval); |
1235 | 2 | goto exit; |
1236 | 2 | } |
1237 | 41.5k | } else { |
1238 | 41.5k | ZEND_ASSERT(!IS_WRONG_PROPERTY_OFFSET(property_offset)); |
1239 | 41.5k | if (UNEXPECTED(zend_lazy_object_must_init(zobj))) { |
1240 | 0 | goto lazy_init; |
1241 | 0 | } |
1242 | 42.5k | write_std_property: |
1243 | 42.5k | if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) { |
1244 | 985 | variable_ptr = OBJ_PROP(zobj, property_offset); |
1245 | | |
1246 | 985 | Z_TRY_ADDREF_P(value); |
1247 | 985 | if (prop_info) { |
1248 | 979 | goto typed_property; |
1249 | 979 | } |
1250 | | |
1251 | 6 | ZVAL_COPY_VALUE(variable_ptr, value); |
1252 | 41.5k | } else { |
1253 | 41.5k | if (UNEXPECTED(zobj->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) { |
1254 | 3 | zend_forbidden_dynamic_property(zobj->ce, name); |
1255 | 3 | variable_ptr = &EG(error_zval); |
1256 | 3 | goto exit; |
1257 | 3 | } |
1258 | 41.5k | if (UNEXPECTED(!(zobj->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES))) { |
1259 | 20.5k | if (UNEXPECTED(!zend_deprecated_dynamic_property(zobj, name))) { |
1260 | 0 | variable_ptr = &EG(error_zval); |
1261 | 0 | goto exit; |
1262 | 0 | } |
1263 | 20.5k | } |
1264 | | |
1265 | 41.5k | Z_TRY_ADDREF_P(value); |
1266 | 41.5k | variable_ptr = zend_hash_add_new(zend_std_get_properties(zobj), name, value); |
1267 | 41.5k | } |
1268 | 42.5k | } |
1269 | | |
1270 | 526k | exit: |
1271 | 526k | return variable_ptr; |
1272 | | |
1273 | 15 | lazy_init: |
1274 | 15 | return forward_write_to_lazy_object(zobj, name, value, cache_slot, |
1275 | 15 | /* guarded */ false); |
1276 | 41.6k | } |
1277 | | /* }}} */ |
1278 | | |
1279 | | static ZEND_COLD zend_never_inline void zend_bad_array_access(const zend_class_entry *ce) /* {{{ */ |
1280 | 14 | { |
1281 | 14 | zend_throw_error(NULL, "Cannot use object of type %s as array", ZSTR_VAL(ce->name)); |
1282 | 14 | } |
1283 | | /* }}} */ |
1284 | | |
1285 | | ZEND_API zval *zend_std_read_dimension(zend_object *object, zval *offset, int type, zval *rv) /* {{{ */ |
1286 | 106 | { |
1287 | 106 | const zend_class_entry *ce = object->ce; |
1288 | 106 | zval tmp_offset; |
1289 | | |
1290 | | /* arrayaccess_funcs_ptr is set if (and only if) the class implements zend_ce_arrayaccess */ |
1291 | 106 | zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr; |
1292 | 106 | if (EXPECTED(funcs)) { |
1293 | 96 | if (offset == NULL) { |
1294 | | /* [] construct */ |
1295 | 0 | ZVAL_NULL(&tmp_offset); |
1296 | 96 | } else { |
1297 | 96 | ZVAL_COPY_DEREF(&tmp_offset, offset); |
1298 | 96 | } |
1299 | | |
1300 | 96 | GC_ADDREF(object); |
1301 | 96 | if (type == BP_VAR_IS) { |
1302 | 12 | zend_call_known_instance_method_with_1_params(funcs->zf_offsetexists, object, rv, &tmp_offset); |
1303 | 12 | if (UNEXPECTED(Z_ISUNDEF_P(rv))) { |
1304 | 0 | OBJ_RELEASE(object); |
1305 | 0 | zval_ptr_dtor(&tmp_offset); |
1306 | 0 | return NULL; |
1307 | 0 | } |
1308 | 12 | if (!i_zend_is_true(rv)) { |
1309 | 3 | OBJ_RELEASE(object); |
1310 | 3 | zval_ptr_dtor(&tmp_offset); |
1311 | 3 | zval_ptr_dtor(rv); |
1312 | 3 | return &EG(uninitialized_zval); |
1313 | 3 | } |
1314 | 9 | zval_ptr_dtor(rv); |
1315 | 9 | } |
1316 | | |
1317 | 93 | zend_call_known_instance_method_with_1_params(funcs->zf_offsetget, object, rv, &tmp_offset); |
1318 | | |
1319 | 93 | OBJ_RELEASE(object); |
1320 | 93 | zval_ptr_dtor(&tmp_offset); |
1321 | | |
1322 | 93 | if (UNEXPECTED(Z_TYPE_P(rv) == IS_UNDEF)) { |
1323 | 0 | if (UNEXPECTED(!EG(exception))) { |
1324 | 0 | zend_throw_error(NULL, "Undefined offset for object of type %s used as array", ZSTR_VAL(ce->name)); |
1325 | 0 | } |
1326 | 0 | return NULL; |
1327 | 0 | } |
1328 | 93 | return rv; |
1329 | 93 | } else { |
1330 | 10 | zend_bad_array_access(ce); |
1331 | 10 | return NULL; |
1332 | 10 | } |
1333 | 106 | } |
1334 | | /* }}} */ |
1335 | | |
1336 | | ZEND_API void zend_std_write_dimension(zend_object *object, zval *offset, zval *value) /* {{{ */ |
1337 | 52 | { |
1338 | 52 | const zend_class_entry *ce = object->ce; |
1339 | 52 | zval tmp_offset; |
1340 | | |
1341 | 52 | zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr; |
1342 | 52 | if (EXPECTED(funcs)) { |
1343 | 51 | if (!offset) { |
1344 | 0 | ZVAL_NULL(&tmp_offset); |
1345 | 51 | } else { |
1346 | 51 | ZVAL_COPY_DEREF(&tmp_offset, offset); |
1347 | 51 | } |
1348 | 51 | GC_ADDREF(object); |
1349 | 51 | zend_call_known_instance_method_with_2_params(funcs->zf_offsetset, object, NULL, &tmp_offset, value); |
1350 | 51 | OBJ_RELEASE(object); |
1351 | 51 | zval_ptr_dtor(&tmp_offset); |
1352 | 51 | } else { |
1353 | 1 | zend_bad_array_access(ce); |
1354 | 1 | } |
1355 | 52 | } |
1356 | | /* }}} */ |
1357 | | |
1358 | | // todo: make zend_std_has_dimension return bool as well |
1359 | | ZEND_API int zend_std_has_dimension(zend_object *object, zval *offset, int check_empty) /* {{{ */ |
1360 | 66 | { |
1361 | 66 | const zend_class_entry *ce = object->ce; |
1362 | 66 | zval retval, tmp_offset; |
1363 | 66 | bool result; |
1364 | | |
1365 | 66 | zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr; |
1366 | 66 | if (EXPECTED(funcs)) { |
1367 | 66 | ZVAL_COPY_DEREF(&tmp_offset, offset); |
1368 | 66 | GC_ADDREF(object); |
1369 | 66 | zend_call_known_instance_method_with_1_params(funcs->zf_offsetexists, object, &retval, &tmp_offset); |
1370 | 66 | result = i_zend_is_true(&retval); |
1371 | 66 | zval_ptr_dtor(&retval); |
1372 | 66 | if (check_empty && result && EXPECTED(!EG(exception))) { |
1373 | 0 | zend_call_known_instance_method_with_1_params(funcs->zf_offsetget, object, &retval, &tmp_offset); |
1374 | 0 | result = i_zend_is_true(&retval); |
1375 | 0 | zval_ptr_dtor(&retval); |
1376 | 0 | } |
1377 | 66 | OBJ_RELEASE(object); |
1378 | 66 | zval_ptr_dtor(&tmp_offset); |
1379 | 66 | } else { |
1380 | 0 | zend_bad_array_access(ce); |
1381 | 0 | return 0; |
1382 | 0 | } |
1383 | | |
1384 | 66 | return result; |
1385 | 66 | } |
1386 | | /* }}} */ |
1387 | | |
1388 | | ZEND_API zval *zend_std_get_property_ptr_ptr(zend_object *zobj, zend_string *name, int type, void **cache_slot) /* {{{ */ |
1389 | 1.51k | { |
1390 | 1.51k | zval *retval = NULL; |
1391 | 1.51k | uintptr_t property_offset; |
1392 | 1.51k | const zend_property_info *prop_info = NULL; |
1393 | | |
1394 | | #if DEBUG_OBJECT_HANDLERS |
1395 | | fprintf(stderr, "Ptr object #%d property: %s\n", zobj->handle, ZSTR_VAL(name)); |
1396 | | #endif |
1397 | | |
1398 | 1.51k | property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__get != NULL), cache_slot, &prop_info); |
1399 | | |
1400 | 1.51k | if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) { |
1401 | 935 | retval = OBJ_PROP(zobj, property_offset); |
1402 | 935 | if (UNEXPECTED(Z_TYPE_P(retval) == IS_UNDEF)) { |
1403 | 77 | if (EXPECTED(!zobj->ce->__get) || |
1404 | 0 | UNEXPECTED((*zend_get_property_guard(zobj, name)) & IN_GET) || |
1405 | 77 | UNEXPECTED(prop_info && (Z_PROP_FLAG_P(retval) & IS_PROP_UNINIT))) { |
1406 | 77 | if (UNEXPECTED(zend_lazy_object_must_init(zobj) && (Z_PROP_FLAG_P(retval) & IS_PROP_LAZY))) { |
1407 | 0 | zobj = zend_lazy_object_init(zobj); |
1408 | 0 | if (!zobj) { |
1409 | 0 | return &EG(error_zval); |
1410 | 0 | } |
1411 | | |
1412 | 0 | return zend_std_get_property_ptr_ptr(zobj, name, type, cache_slot); |
1413 | 0 | } |
1414 | 77 | if (UNEXPECTED(type == BP_VAR_RW || type == BP_VAR_R)) { |
1415 | 19 | if (prop_info) { |
1416 | 18 | zend_typed_property_uninitialized_access(prop_info, name); |
1417 | 18 | retval = &EG(error_zval); |
1418 | 18 | } else { |
1419 | 1 | zend_error(E_WARNING, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name)); |
1420 | | /* An error handler may set the property */ |
1421 | 1 | if (EXPECTED(Z_TYPE_P(retval) == IS_UNDEF)) { |
1422 | 1 | ZVAL_NULL(retval); |
1423 | 1 | } |
1424 | 1 | } |
1425 | 58 | } else if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))) { |
1426 | 30 | if ((prop_info->flags & ZEND_ACC_READONLY) || !zend_asymmetric_property_has_set_access(prop_info)) { |
1427 | 21 | retval = NULL; |
1428 | 21 | } |
1429 | 30 | } else if (!prop_info || !ZEND_TYPE_IS_SET(prop_info->type)) { |
1430 | 1 | ZVAL_NULL(retval); |
1431 | 1 | } |
1432 | 77 | } else { |
1433 | | /* we do have getter - fail and let it try again with usual get/set */ |
1434 | 0 | retval = NULL; |
1435 | 0 | } |
1436 | 858 | } else if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))) { |
1437 | 58 | if ((prop_info->flags & ZEND_ACC_READONLY) || !zend_asymmetric_property_has_set_access(prop_info)) { |
1438 | 43 | retval = NULL; |
1439 | 43 | } |
1440 | 58 | } |
1441 | 935 | } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) { |
1442 | 572 | if (EXPECTED(zobj->properties)) { |
1443 | 440 | if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) { |
1444 | 8 | if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) { |
1445 | 8 | GC_DELREF(zobj->properties); |
1446 | 8 | } |
1447 | 8 | zobj->properties = zend_array_dup(zobj->properties); |
1448 | 8 | } |
1449 | 440 | if (EXPECTED((retval = zend_hash_find(zobj->properties, name)) != NULL)) { |
1450 | 245 | return retval; |
1451 | 245 | } |
1452 | 440 | } |
1453 | 327 | if (EXPECTED(!zobj->ce->__get) || |
1454 | 311 | UNEXPECTED((*zend_get_property_guard(zobj, name)) & IN_GET)) { |
1455 | 311 | if (UNEXPECTED(zobj->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) { |
1456 | 0 | zend_forbidden_dynamic_property(zobj->ce, name); |
1457 | 0 | return &EG(error_zval); |
1458 | 0 | } |
1459 | 311 | if (UNEXPECTED(!(zobj->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES))) { |
1460 | 66 | if (UNEXPECTED(!zend_deprecated_dynamic_property(zobj, name))) { |
1461 | 0 | return &EG(error_zval); |
1462 | 0 | } |
1463 | 66 | } |
1464 | 311 | if (UNEXPECTED(zend_lazy_object_must_init(zobj))) { |
1465 | 0 | zobj = zend_lazy_object_init(zobj); |
1466 | 0 | if (!zobj) { |
1467 | 0 | return &EG(error_zval); |
1468 | 0 | } |
1469 | | |
1470 | 0 | return zend_std_get_property_ptr_ptr(zobj, name, type, cache_slot); |
1471 | 0 | } |
1472 | 311 | if (UNEXPECTED(!zobj->properties)) { |
1473 | 117 | rebuild_object_properties_internal(zobj); |
1474 | 117 | } |
1475 | 311 | if (UNEXPECTED(type == BP_VAR_RW || type == BP_VAR_R)) { |
1476 | 122 | zend_error(E_WARNING, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name)); |
1477 | 122 | } |
1478 | 311 | retval = zend_hash_add(zobj->properties, name, &EG(uninitialized_zval)); |
1479 | 311 | } |
1480 | 327 | } else if (!IS_HOOKED_PROPERTY_OFFSET(property_offset) && zobj->ce->__get == NULL) { |
1481 | 0 | retval = &EG(error_zval); |
1482 | 0 | } |
1483 | | |
1484 | 1.26k | return retval; |
1485 | 1.51k | } |
1486 | | /* }}} */ |
1487 | | |
1488 | | ZEND_API void zend_std_unset_property(zend_object *zobj, zend_string *name, void **cache_slot) /* {{{ */ |
1489 | 164 | { |
1490 | 164 | uintptr_t property_offset; |
1491 | 164 | const zend_property_info *prop_info = NULL; |
1492 | 164 | uint32_t *guard = NULL; |
1493 | | |
1494 | 164 | property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__unset != NULL), cache_slot, &prop_info); |
1495 | | |
1496 | 164 | if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) { |
1497 | 89 | zval *slot = OBJ_PROP(zobj, property_offset); |
1498 | | |
1499 | 89 | if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))) { |
1500 | 36 | bool error; |
1501 | 36 | if (Z_TYPE_P(slot) != IS_UNDEF || Z_PROP_FLAG_P(slot) & IS_PROP_UNINIT || !zobj->ce->__unset) { |
1502 | 36 | error = true; |
1503 | 36 | } else { |
1504 | 0 | guard = zend_get_property_guard(zobj, name); |
1505 | 0 | error = (*guard) & IN_UNSET; |
1506 | 0 | } |
1507 | 36 | if (error) { |
1508 | 36 | if ((prop_info->flags & ZEND_ACC_READONLY) |
1509 | 12 | && Z_TYPE_P(slot) != IS_UNDEF |
1510 | 6 | && !(Z_PROP_FLAG_P(slot) & IS_PROP_REINITABLE)) { |
1511 | 6 | zend_readonly_property_unset_error(prop_info->ce, name); |
1512 | 6 | return; |
1513 | 6 | } |
1514 | 30 | if ((prop_info->flags & ZEND_ACC_PPP_SET_MASK) && !zend_asymmetric_property_has_set_access(prop_info)) { |
1515 | 15 | zend_asymmetric_visibility_property_modification_error(prop_info, "unset"); |
1516 | 15 | return; |
1517 | 15 | } |
1518 | 30 | } |
1519 | 36 | } |
1520 | | |
1521 | 68 | if (Z_TYPE_P(slot) != IS_UNDEF) { |
1522 | 48 | if (UNEXPECTED(Z_ISREF_P(slot)) && |
1523 | 28 | (ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(slot)))) { |
1524 | 28 | if (prop_info) { |
1525 | 27 | ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(slot), prop_info); |
1526 | 27 | } |
1527 | 28 | } |
1528 | 48 | zval tmp; |
1529 | 48 | ZVAL_COPY_VALUE(&tmp, slot); |
1530 | 48 | ZVAL_UNDEF(slot); |
1531 | 48 | zval_ptr_dtor(&tmp); |
1532 | 48 | if (zobj->properties) { |
1533 | 34 | HT_FLAGS(zobj->properties) |= HASH_FLAG_HAS_EMPTY_IND; |
1534 | 34 | } |
1535 | 48 | return; |
1536 | 48 | } |
1537 | 20 | if (UNEXPECTED(Z_PROP_FLAG_P(slot) & IS_PROP_UNINIT)) { |
1538 | 20 | if (UNEXPECTED(zend_lazy_object_must_init(zobj) && (Z_PROP_FLAG_P(slot) & IS_PROP_LAZY))) { |
1539 | 3 | zobj = zend_lazy_object_init(zobj); |
1540 | 3 | if (!zobj) { |
1541 | 0 | return; |
1542 | 0 | } |
1543 | 3 | zend_std_unset_property(zobj, name, cache_slot); |
1544 | 3 | return; |
1545 | 3 | } |
1546 | | |
1547 | | /* Reset the IS_PROP_UNINIT flag, if it exists and bypass __unset(). */ |
1548 | 17 | Z_PROP_FLAG_P(slot) = 0; |
1549 | 17 | return; |
1550 | 20 | } |
1551 | 75 | } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset)) |
1552 | 72 | && EXPECTED(zobj->properties != NULL)) { |
1553 | 66 | if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) { |
1554 | 0 | if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) { |
1555 | 0 | GC_DELREF(zobj->properties); |
1556 | 0 | } |
1557 | 0 | zobj->properties = zend_array_dup(zobj->properties); |
1558 | 0 | } |
1559 | 66 | if (EXPECTED(zend_hash_del(zobj->properties, name) != FAILURE)) { |
1560 | 66 | return; |
1561 | 66 | } |
1562 | 66 | } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) { |
1563 | 0 | zend_throw_error(NULL, "Cannot unset hooked property %s::$%s", |
1564 | 0 | ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name)); |
1565 | 0 | return; |
1566 | 9 | } else if (UNEXPECTED(EG(exception))) { |
1567 | 1 | return; |
1568 | 1 | } |
1569 | | |
1570 | | /* magic unset */ |
1571 | 8 | if (zobj->ce->__unset) { |
1572 | 8 | if (!guard) { |
1573 | 8 | guard = zend_get_property_guard(zobj, name); |
1574 | 8 | } |
1575 | 8 | if (!((*guard) & IN_UNSET)) { |
1576 | | /* have unsetter - try with it! */ |
1577 | 7 | (*guard) |= IN_UNSET; /* prevent circular unsetting */ |
1578 | 7 | zend_std_call_unsetter(zobj, name); |
1579 | 7 | (*guard) &= ~IN_UNSET; |
1580 | 7 | return; |
1581 | 7 | } else if (UNEXPECTED(IS_WRONG_PROPERTY_OFFSET(property_offset))) { |
1582 | | /* Trigger the correct error */ |
1583 | 1 | zend_wrong_offset(zobj->ce, name); |
1584 | 1 | ZEND_ASSERT(EG(exception)); |
1585 | 1 | return; |
1586 | 1 | } else { |
1587 | | /* Nothing to do: The property already does not exist. */ |
1588 | 0 | } |
1589 | 8 | } |
1590 | | |
1591 | 0 | if (UNEXPECTED(zend_lazy_object_must_init(zobj))) { |
1592 | 0 | zobj = zend_lazy_object_init(zobj); |
1593 | 0 | if (!zobj) { |
1594 | 0 | return; |
1595 | 0 | } |
1596 | | |
1597 | 0 | if (UNEXPECTED(guard)) { |
1598 | 0 | guard = zend_get_property_guard(zobj, name); |
1599 | 0 | if (!((*guard) & IN_UNSET)) { |
1600 | 0 | (*guard) |= IN_UNSET; |
1601 | 0 | zend_std_unset_property(zobj, name, cache_slot); |
1602 | 0 | (*guard) &= ~IN_UNSET; |
1603 | 0 | return; |
1604 | 0 | } |
1605 | 0 | } |
1606 | | |
1607 | 0 | zend_std_unset_property(zobj, name, cache_slot); |
1608 | 0 | return; |
1609 | 0 | } |
1610 | 0 | } |
1611 | | /* }}} */ |
1612 | | |
1613 | | ZEND_API void zend_std_unset_dimension(zend_object *object, zval *offset) /* {{{ */ |
1614 | 60 | { |
1615 | 60 | const zend_class_entry *ce = object->ce; |
1616 | 60 | zval tmp_offset; |
1617 | | |
1618 | 60 | zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr; |
1619 | 60 | if (EXPECTED(funcs)) { |
1620 | 57 | ZVAL_COPY_DEREF(&tmp_offset, offset); |
1621 | 57 | GC_ADDREF(object); |
1622 | 57 | zend_call_known_instance_method_with_1_params(funcs->zf_offsetunset, object, NULL, &tmp_offset); |
1623 | 57 | OBJ_RELEASE(object); |
1624 | 57 | zval_ptr_dtor(&tmp_offset); |
1625 | 57 | } else { |
1626 | 3 | zend_bad_array_access(ce); |
1627 | 3 | } |
1628 | 60 | } |
1629 | | /* }}} */ |
1630 | | |
1631 | | static zend_never_inline zend_function *zend_get_parent_private_method(const zend_class_entry *scope, const zend_class_entry *ce, zend_string *function_name) /* {{{ */ |
1632 | 3 | { |
1633 | 3 | zval *func; |
1634 | 3 | zend_function *fbc; |
1635 | | |
1636 | 3 | if (scope != ce && scope && is_derived_class(ce, scope)) { |
1637 | 3 | func = zend_hash_find(&scope->function_table, function_name); |
1638 | 3 | if (func != NULL) { |
1639 | 3 | fbc = Z_FUNC_P(func); |
1640 | 3 | if ((fbc->common.fn_flags & ZEND_ACC_PRIVATE) |
1641 | 3 | && fbc->common.scope == scope) { |
1642 | 3 | return fbc; |
1643 | 3 | } |
1644 | 3 | } |
1645 | 3 | } |
1646 | 0 | return NULL; |
1647 | 3 | } |
1648 | | /* }}} */ |
1649 | | |
1650 | | /* Ensures that we're allowed to call a protected method. |
1651 | | */ |
1652 | | ZEND_API bool zend_check_protected(const zend_class_entry *ce, const zend_class_entry *scope) /* {{{ */ |
1653 | 48 | { |
1654 | 48 | const zend_class_entry *fbc_scope = ce; |
1655 | | |
1656 | | /* Is the context that's calling the function, the same as one of |
1657 | | * the function's parents? |
1658 | | */ |
1659 | 100 | while (fbc_scope) { |
1660 | 52 | if (fbc_scope==scope) { |
1661 | 0 | return 1; |
1662 | 0 | } |
1663 | 52 | fbc_scope = fbc_scope->parent; |
1664 | 52 | } |
1665 | | |
1666 | | /* Is the function's scope the same as our current object context, |
1667 | | * or any of the parents of our context? |
1668 | | */ |
1669 | 74 | while (scope) { |
1670 | 44 | if (scope==ce) { |
1671 | 18 | return 1; |
1672 | 18 | } |
1673 | 26 | scope = scope->parent; |
1674 | 26 | } |
1675 | 30 | return 0; |
1676 | 48 | } |
1677 | | /* }}} */ |
1678 | | |
1679 | | ZEND_API ZEND_ATTRIBUTE_NONNULL zend_function *zend_get_call_trampoline_func( |
1680 | | const zend_function *fbc, zend_string *method_name) /* {{{ */ |
1681 | 1.48k | { |
1682 | 1.48k | size_t mname_len; |
1683 | 1.48k | zend_op_array *func; |
1684 | | /* We use non-NULL value to avoid useless run_time_cache allocation. |
1685 | | * The low bit must be zero, to not be interpreted as a MAP_PTR offset. |
1686 | | */ |
1687 | 1.48k | static const void *dummy = (void*)(intptr_t)2; |
1688 | 1.48k | static const zend_arg_info arg_info[1] = {{0}}; |
1689 | | |
1690 | 1.48k | if (EXPECTED(EG(trampoline).common.function_name == NULL)) { |
1691 | 160 | func = &EG(trampoline).op_array; |
1692 | 1.32k | } else { |
1693 | 1.32k | func = ecalloc(1, sizeof(zend_op_array)); |
1694 | 1.32k | } |
1695 | | |
1696 | 1.48k | func->type = ZEND_USER_FUNCTION; |
1697 | 1.48k | func->arg_flags[0] = 0; |
1698 | 1.48k | func->arg_flags[1] = 0; |
1699 | 1.48k | func->arg_flags[2] = 0; |
1700 | 1.48k | func->fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE |
1701 | 1.48k | | ZEND_ACC_PUBLIC |
1702 | 1.48k | | ZEND_ACC_VARIADIC |
1703 | 1.48k | | (fbc->common.fn_flags & (ZEND_ACC_RETURN_REFERENCE|ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED|ZEND_ACC_NODISCARD|ZEND_ACC_STATIC)); |
1704 | 1.48k | func->fn_flags2 = 0; |
1705 | | /* Attributes outlive the trampoline because they are created by the compiler. */ |
1706 | 1.48k | func->attributes = fbc->common.attributes; |
1707 | 1.48k | func->opcodes = &EG(call_trampoline_op); |
1708 | 1.48k | ZEND_MAP_PTR_INIT(func->run_time_cache, (void**)dummy); |
1709 | 1.48k | func->scope = fbc->common.scope; |
1710 | | /* reserve space for arguments, local and temporary variables */ |
1711 | | /* EG(trampoline) is reused from other places, like FFI (e.g. zend_ffi_cdata_get_closure()) where |
1712 | | * it is used as an internal function. It may set fields that don't belong to common, thus |
1713 | | * modifying zend_op_array specific data, most significantly last_var. We need to reset this |
1714 | | * value so that it doesn't contain garbage when the engine allocates space for the next stack |
1715 | | * frame. This didn't cause any issues until now due to "lucky" structure layout. */ |
1716 | 1.48k | func->last_var = 0; |
1717 | 1.48k | uint32_t min_T = 2 + ZEND_OBSERVER_ENABLED; |
1718 | 1.48k | func->T = (fbc->type == ZEND_USER_FUNCTION)? MAX(fbc->op_array.last_var + fbc->op_array.T, min_T) : min_T; |
1719 | 1.48k | func->filename = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.filename : ZSTR_EMPTY_ALLOC(); |
1720 | 1.48k | func->line_start = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.line_start : 0; |
1721 | 1.48k | func->line_end = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.line_end : 0; |
1722 | | |
1723 | | //??? keep compatibility for "\0" characters |
1724 | | //??? see: Zend/tests/bug46238.phpt |
1725 | 1.48k | if (UNEXPECTED((mname_len = strlen(ZSTR_VAL(method_name))) != ZSTR_LEN(method_name))) { |
1726 | 4 | func->function_name = zend_string_init(ZSTR_VAL(method_name), mname_len, 0); |
1727 | 1.47k | } else { |
1728 | 1.47k | func->function_name = zend_string_copy(method_name); |
1729 | 1.47k | } |
1730 | | |
1731 | 1.48k | func->prototype = NULL; |
1732 | 1.48k | func->prop_info = NULL; |
1733 | 1.48k | func->num_args = 0; |
1734 | 1.48k | func->required_num_args = 0; |
1735 | 1.48k | func->arg_info = (zend_arg_info *) arg_info; |
1736 | | |
1737 | 1.48k | return (zend_function*)func; |
1738 | 1.48k | } |
1739 | | /* }}} */ |
1740 | | |
1741 | | static ZEND_FUNCTION(zend_parent_hook_get_trampoline) |
1742 | 67 | { |
1743 | 67 | zend_object *obj = Z_PTR_P(ZEND_THIS); |
1744 | 67 | zend_string *prop_name = EX(func)->internal_function.reserved[0]; |
1745 | | |
1746 | 67 | if (UNEXPECTED(ZEND_NUM_ARGS() != 0)) { |
1747 | 0 | zend_wrong_parameters_none_error(); |
1748 | 0 | goto clean; |
1749 | 0 | } |
1750 | | |
1751 | 67 | zval rv; |
1752 | 67 | const zval *retval = obj->handlers->read_property(obj, prop_name, BP_VAR_R, NULL, &rv); |
1753 | 67 | if (retval == &rv) { |
1754 | 0 | RETVAL_COPY_VALUE(retval); |
1755 | 67 | } else { |
1756 | 67 | RETVAL_COPY(retval); |
1757 | 67 | } |
1758 | | |
1759 | 67 | clean: |
1760 | 67 | zend_string_release(EX(func)->common.function_name); |
1761 | 67 | zend_free_trampoline(EX(func)); |
1762 | 67 | EX(func) = NULL; |
1763 | 67 | } |
1764 | | |
1765 | | static ZEND_FUNCTION(zend_parent_hook_set_trampoline) |
1766 | 1 | { |
1767 | 1 | zend_object *obj = Z_PTR_P(ZEND_THIS); |
1768 | 1 | zend_string *prop_name = EX(func)->internal_function.reserved[0]; |
1769 | | |
1770 | 1 | zval *value; |
1771 | | |
1772 | 3 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1773 | 4 | Z_PARAM_ZVAL(value) |
1774 | 4 | ZEND_PARSE_PARAMETERS_END_EX(goto clean); |
1775 | | |
1776 | 1 | RETVAL_COPY(obj->handlers->write_property(obj, prop_name, value, NULL)); |
1777 | | |
1778 | 1 | clean: |
1779 | 1 | zend_string_release(EX(func)->common.function_name); |
1780 | 1 | zend_free_trampoline(EX(func)); |
1781 | 1 | EX(func) = NULL; |
1782 | 1 | } |
1783 | | |
1784 | | ZEND_API zend_function *zend_get_property_hook_trampoline( |
1785 | | const zend_property_info *prop_info, |
1786 | | zend_property_hook_kind kind, zend_string *prop_name) |
1787 | 68 | { |
1788 | 68 | zend_function *func; |
1789 | 68 | if (EXPECTED(EG(trampoline).common.function_name == NULL)) { |
1790 | 68 | func = &EG(trampoline); |
1791 | 68 | } else { |
1792 | 0 | func = (zend_function *)(uintptr_t)ecalloc(1, sizeof(zend_internal_function)); |
1793 | 0 | } |
1794 | 68 | func->type = ZEND_INTERNAL_FUNCTION; |
1795 | | /* This trampoline does not use the call_trampoline_op, so it won't reuse the call frame, |
1796 | | * which means we don't even need to reserve a temporary for observers. */ |
1797 | 68 | func->common.T = 0; |
1798 | 68 | func->common.arg_flags[0] = 0; |
1799 | 68 | func->common.arg_flags[1] = 0; |
1800 | 68 | func->common.arg_flags[2] = 0; |
1801 | 68 | func->common.fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE; |
1802 | 68 | func->common.fn_flags2 = 0; |
1803 | 68 | func->common.function_name = zend_string_concat3( |
1804 | 68 | "$", 1, ZSTR_VAL(prop_name), ZSTR_LEN(prop_name), |
1805 | 68 | kind == ZEND_PROPERTY_HOOK_GET ? "::get" : "::set", 5); |
1806 | | /* set to 0 to avoid arg_info[] allocation, because all values are passed by value anyway */ |
1807 | 68 | uint32_t args = kind == ZEND_PROPERTY_HOOK_GET ? 0 : 1; |
1808 | 68 | func->common.num_args = args; |
1809 | 68 | func->common.required_num_args = args; |
1810 | 68 | func->common.scope = prop_info->ce; |
1811 | 68 | func->common.prototype = NULL; |
1812 | 68 | func->common.prop_info = prop_info; |
1813 | 68 | func->common.arg_info = zend_property_hook_arginfo; |
1814 | 68 | func->internal_function.handler = kind == ZEND_PROPERTY_HOOK_GET |
1815 | 68 | ? ZEND_FN(zend_parent_hook_get_trampoline) |
1816 | 68 | : ZEND_FN(zend_parent_hook_set_trampoline); |
1817 | 68 | func->internal_function.module = NULL; |
1818 | | |
1819 | 68 | func->internal_function.reserved[0] = prop_name; |
1820 | 68 | func->internal_function.reserved[1] = NULL; |
1821 | | |
1822 | 68 | return func; |
1823 | 68 | } |
1824 | | |
1825 | | ZEND_API ZEND_COLD zend_never_inline void zend_bad_method_call(const zend_function *fbc, const zend_string *method_name, const zend_class_entry *scope) /* {{{ */ |
1826 | 2 | { |
1827 | 2 | zend_throw_error(NULL, "Call to %s method %s::%s() from %s%s", |
1828 | 2 | zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), ZSTR_VAL(method_name), |
1829 | 2 | scope ? "scope " : "global scope", |
1830 | 2 | scope ? ZSTR_VAL(scope->name) : "" |
1831 | 2 | ); |
1832 | 2 | } |
1833 | | /* }}} */ |
1834 | | |
1835 | | ZEND_API ZEND_COLD zend_never_inline void zend_abstract_method_call(const zend_function *fbc) /* {{{ */ |
1836 | 2 | { |
1837 | 2 | zend_throw_error(NULL, "Cannot call abstract method %s::%s()", |
1838 | 2 | ZSTR_VAL(fbc->common.scope->name), ZSTR_VAL(fbc->common.function_name)); |
1839 | 2 | } |
1840 | | /* }}} */ |
1841 | | |
1842 | | ZEND_API zend_function *zend_std_get_method(zend_object **obj_ptr, zend_string *method_name, const zval *key) /* {{{ */ |
1843 | 39.7k | { |
1844 | 39.7k | zend_object *zobj = *obj_ptr; |
1845 | 39.7k | zval *func; |
1846 | 39.7k | zend_function *fbc; |
1847 | 39.7k | zend_string *lc_method_name; |
1848 | 39.7k | ALLOCA_FLAG(use_heap); |
1849 | | |
1850 | 39.7k | if (EXPECTED(key != NULL)) { |
1851 | 38.1k | lc_method_name = Z_STR_P(key); |
1852 | | #ifdef ZEND_ALLOCA_MAX_SIZE |
1853 | | use_heap = 0; |
1854 | | #endif |
1855 | 38.1k | } else { |
1856 | 1.59k | ZSTR_ALLOCA_ALLOC(lc_method_name, ZSTR_LEN(method_name), use_heap); |
1857 | 1.59k | zend_str_tolower_copy(ZSTR_VAL(lc_method_name), ZSTR_VAL(method_name), ZSTR_LEN(method_name)); |
1858 | 1.59k | } |
1859 | | |
1860 | 39.7k | if (UNEXPECTED((func = zend_hash_find(&zobj->ce->function_table, lc_method_name)) == NULL)) { |
1861 | 1.50k | if (UNEXPECTED(!key)) { |
1862 | 1.44k | ZSTR_ALLOCA_FREE(lc_method_name, use_heap); |
1863 | 1.44k | } |
1864 | 1.50k | if (zobj->ce->__call) { |
1865 | 1.44k | return zend_get_call_trampoline_func(zobj->ce->__call, method_name); |
1866 | 1.44k | } else { |
1867 | 63 | return NULL; |
1868 | 63 | } |
1869 | 1.50k | } |
1870 | | |
1871 | 38.2k | fbc = Z_FUNC_P(func); |
1872 | | |
1873 | | /* Check access level */ |
1874 | 38.2k | if (fbc->op_array.fn_flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) { |
1875 | 44 | const zend_class_entry *scope = zend_get_executed_scope(); |
1876 | | |
1877 | 44 | if (fbc->common.scope != scope) { |
1878 | 14 | if (fbc->op_array.fn_flags & ZEND_ACC_CHANGED) { |
1879 | 3 | zend_function *updated_fbc = zend_get_parent_private_method(scope, zobj->ce, lc_method_name); |
1880 | | |
1881 | 3 | if (EXPECTED(updated_fbc != NULL)) { |
1882 | 3 | fbc = updated_fbc; |
1883 | 3 | goto exit; |
1884 | 3 | } else if (fbc->op_array.fn_flags & ZEND_ACC_PUBLIC) { |
1885 | 0 | goto exit; |
1886 | 0 | } |
1887 | 3 | } |
1888 | 11 | if (UNEXPECTED(fbc->op_array.fn_flags & ZEND_ACC_PRIVATE) |
1889 | 10 | || UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), scope))) { |
1890 | 2 | if (zobj->ce->__call) { |
1891 | 0 | fbc = zend_get_call_trampoline_func(zobj->ce->__call, method_name); |
1892 | 2 | } else { |
1893 | 2 | zend_bad_method_call(fbc, method_name, scope); |
1894 | 2 | fbc = NULL; |
1895 | 2 | } |
1896 | 2 | } |
1897 | 11 | } |
1898 | 44 | } |
1899 | | |
1900 | 38.2k | exit: |
1901 | 38.2k | if (fbc && UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_ABSTRACT)) { |
1902 | 0 | zend_abstract_method_call(fbc); |
1903 | 0 | fbc = NULL; |
1904 | 0 | } |
1905 | 38.2k | if (UNEXPECTED(!key)) { |
1906 | 148 | ZSTR_ALLOCA_FREE(lc_method_name, use_heap); |
1907 | 148 | } |
1908 | 38.2k | return fbc; |
1909 | 38.2k | } |
1910 | | /* }}} */ |
1911 | | |
1912 | | static zend_always_inline zend_function *get_static_method_fallback( |
1913 | | const zend_class_entry *ce, zend_string *function_name) |
1914 | 60 | { |
1915 | 60 | zend_object *object; |
1916 | 60 | if (ce->__call && |
1917 | 20 | (object = zend_get_this_object(EG(current_execute_data))) != NULL && |
1918 | 2 | instanceof_function(object->ce, ce)) { |
1919 | | /* Call the top-level defined __call(). |
1920 | | * see: tests/classes/__call_004.phpt */ |
1921 | | |
1922 | 2 | ZEND_ASSERT(object->ce->__call); |
1923 | 2 | return zend_get_call_trampoline_func(object->ce->__call, function_name); |
1924 | 58 | } else if (ce->__callstatic) { |
1925 | 37 | return zend_get_call_trampoline_func(ce->__callstatic, function_name); |
1926 | 37 | } else { |
1927 | 21 | return NULL; |
1928 | 21 | } |
1929 | 60 | } |
1930 | | |
1931 | | ZEND_API zend_function *zend_std_get_static_method(const zend_class_entry *ce, zend_string *function_name, const zval *key) /* {{{ */ |
1932 | 737 | { |
1933 | 737 | zend_string *lc_function_name; |
1934 | 737 | if (EXPECTED(key != NULL)) { |
1935 | 694 | lc_function_name = Z_STR_P(key); |
1936 | 694 | } else { |
1937 | 43 | lc_function_name = zend_string_tolower(function_name); |
1938 | 43 | } |
1939 | | |
1940 | 737 | zend_function *fbc; |
1941 | 737 | zval *func = zend_hash_find(&ce->function_table, lc_function_name); |
1942 | 737 | if (EXPECTED(func)) { |
1943 | 677 | fbc = Z_FUNC_P(func); |
1944 | 677 | if (!(fbc->common.fn_flags & ZEND_ACC_PUBLIC)) { |
1945 | 12 | const zend_class_entry *scope = zend_get_executed_scope(); |
1946 | 12 | ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_PUBLIC)); |
1947 | 12 | if (!zend_check_method_accessible(fbc, scope)) { |
1948 | 0 | zend_function *fallback_fbc = get_static_method_fallback(ce, function_name); |
1949 | 0 | if (!fallback_fbc) { |
1950 | 0 | zend_bad_method_call(fbc, function_name, scope); |
1951 | 0 | } |
1952 | 0 | fbc = fallback_fbc; |
1953 | 0 | } |
1954 | 12 | } |
1955 | 677 | } else { |
1956 | 60 | fbc = get_static_method_fallback(ce, function_name); |
1957 | 60 | } |
1958 | | |
1959 | 737 | if (UNEXPECTED(!key)) { |
1960 | 43 | zend_string_release_ex(lc_function_name, 0); |
1961 | 43 | } |
1962 | | |
1963 | 737 | if (EXPECTED(fbc)) { |
1964 | 716 | if (UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_ABSTRACT)) { |
1965 | 1 | zend_abstract_method_call(fbc); |
1966 | 1 | goto fail; |
1967 | 715 | } else if (UNEXPECTED(fbc->common.scope->ce_flags & ZEND_ACC_TRAIT)) { |
1968 | 0 | zend_error(E_DEPRECATED, |
1969 | 0 | "Calling static trait method %s::%s is deprecated, " |
1970 | 0 | "it should only be called on a class using the trait", |
1971 | 0 | ZSTR_VAL(fbc->common.scope->name), ZSTR_VAL(fbc->common.function_name)); |
1972 | 0 | if (EG(exception)) { |
1973 | 0 | goto fail; |
1974 | 0 | } |
1975 | 0 | } |
1976 | 716 | } |
1977 | | |
1978 | 736 | return fbc; |
1979 | | |
1980 | 1 | fail: |
1981 | 1 | if (UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) { |
1982 | 1 | zend_string_release_ex(fbc->common.function_name, 0); |
1983 | 1 | zend_free_trampoline(fbc); |
1984 | 1 | } |
1985 | | |
1986 | 1 | return NULL; |
1987 | 737 | } |
1988 | | /* }}} */ |
1989 | | |
1990 | | ZEND_API void zend_class_init_statics(zend_class_entry *class_type) /* {{{ */ |
1991 | 135 | { |
1992 | 135 | zval *p; |
1993 | | |
1994 | 135 | if (class_type->default_static_members_count && !CE_STATIC_MEMBERS(class_type)) { |
1995 | 132 | if (class_type->parent) { |
1996 | 3 | zend_class_init_statics(class_type->parent); |
1997 | 3 | } |
1998 | | |
1999 | 132 | ZEND_MAP_PTR_SET(class_type->static_members_table, emalloc(sizeof(zval) * class_type->default_static_members_count)); |
2000 | 869 | for (uint32_t i = 0; i < class_type->default_static_members_count; i++) { |
2001 | 737 | p = &class_type->default_static_members_table[i]; |
2002 | 737 | if (Z_TYPE_P(p) == IS_INDIRECT) { |
2003 | 3 | zval *q = &CE_STATIC_MEMBERS(class_type->parent)[i]; |
2004 | 3 | ZVAL_DEINDIRECT(q); |
2005 | 3 | ZVAL_INDIRECT(&CE_STATIC_MEMBERS(class_type)[i], q); |
2006 | 734 | } else { |
2007 | 734 | ZVAL_COPY_OR_DUP(&CE_STATIC_MEMBERS(class_type)[i], p); |
2008 | 734 | } |
2009 | 737 | } |
2010 | 132 | } |
2011 | 135 | } /* }}} */ |
2012 | | |
2013 | | 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) /* {{{ */ |
2014 | 738 | { |
2015 | 738 | zval *ret; |
2016 | 738 | zend_property_info *property_info = zend_hash_find_ptr(&ce->properties_info, property_name); |
2017 | 738 | *property_info_ptr = property_info; |
2018 | | |
2019 | 738 | if (UNEXPECTED(property_info == NULL)) { |
2020 | 10 | goto undeclared_property; |
2021 | 10 | } |
2022 | | |
2023 | 728 | if (!(property_info->flags & ZEND_ACC_PUBLIC)) { |
2024 | 132 | const zend_class_entry *scope = get_fake_or_executed_scope(); |
2025 | 132 | if (property_info->ce != scope) { |
2026 | 39 | if (UNEXPECTED(property_info->flags & ZEND_ACC_PRIVATE) |
2027 | 39 | || UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope))) { |
2028 | 39 | if (type != BP_VAR_IS) { |
2029 | 0 | zend_bad_property_access(property_info, ce, property_name); |
2030 | 0 | } |
2031 | 39 | return NULL; |
2032 | 39 | } |
2033 | 39 | } |
2034 | 132 | } |
2035 | | |
2036 | 689 | if (UNEXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0)) { |
2037 | 22 | undeclared_property: |
2038 | 22 | if (type != BP_VAR_IS) { |
2039 | 16 | zend_throw_error(NULL, "Access to undeclared static property %s::$%s", ZSTR_VAL(ce->name), ZSTR_VAL(property_name)); |
2040 | 16 | } |
2041 | 22 | return NULL; |
2042 | 12 | } |
2043 | | |
2044 | 677 | if (UNEXPECTED(!(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) { |
2045 | 128 | if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) { |
2046 | 1 | return NULL; |
2047 | 1 | } |
2048 | 128 | } |
2049 | | |
2050 | | /* Ensure static properties are initialized. */ |
2051 | 676 | if (UNEXPECTED(CE_STATIC_MEMBERS(ce) == NULL)) { |
2052 | 107 | zend_class_init_statics(ce); |
2053 | 107 | } |
2054 | | |
2055 | 676 | ret = CE_STATIC_MEMBERS(ce) + property_info->offset; |
2056 | 676 | ZVAL_DEINDIRECT(ret); |
2057 | | |
2058 | 676 | if (UNEXPECTED((type == BP_VAR_R || type == BP_VAR_RW) |
2059 | 676 | && Z_TYPE_P(ret) == IS_UNDEF && ZEND_TYPE_IS_SET(property_info->type))) { |
2060 | 0 | zend_throw_error(NULL, "Typed static property %s::$%s must not be accessed before initialization", |
2061 | 0 | ZSTR_VAL(property_info->ce->name), ZSTR_VAL(property_name)); |
2062 | 0 | return NULL; |
2063 | 0 | } |
2064 | | |
2065 | 676 | if (UNEXPECTED(ce->ce_flags & ZEND_ACC_TRAIT)) { |
2066 | 0 | zend_error(E_DEPRECATED, |
2067 | 0 | "Accessing static trait property %s::$%s is deprecated, " |
2068 | 0 | "it should only be accessed on a class using the trait", |
2069 | 0 | ZSTR_VAL(property_info->ce->name), ZSTR_VAL(property_name)); |
2070 | 0 | } |
2071 | | |
2072 | 676 | return ret; |
2073 | 676 | } |
2074 | | /* }}} */ |
2075 | | |
2076 | | ZEND_API zval *zend_std_get_static_property(zend_class_entry *ce, zend_string *property_name, int type) /* {{{ */ |
2077 | 0 | { |
2078 | 0 | zend_property_info *prop_info; |
2079 | 0 | return zend_std_get_static_property_with_info(ce, property_name, type, &prop_info); |
2080 | 0 | } |
2081 | | |
2082 | | ZEND_API ZEND_COLD bool zend_std_unset_static_property(const zend_class_entry *ce, const zend_string *property_name) /* {{{ */ |
2083 | 0 | { |
2084 | 0 | zend_throw_error(NULL, "Attempt to unset static property %s::$%s", ZSTR_VAL(ce->name), ZSTR_VAL(property_name)); |
2085 | 0 | return 0; |
2086 | 0 | } |
2087 | | /* }}} */ |
2088 | | |
2089 | | static ZEND_COLD zend_never_inline void zend_bad_constructor_call(const zend_function *constructor, const zend_class_entry *scope) /* {{{ */ |
2090 | 19 | { |
2091 | 19 | if (scope) { |
2092 | 3 | zend_throw_error(NULL, "Call to %s %s::%s() from scope %s", |
2093 | 3 | zend_visibility_string(constructor->common.fn_flags), ZSTR_VAL(constructor->common.scope->name), |
2094 | 3 | ZSTR_VAL(constructor->common.function_name), ZSTR_VAL(scope->name) |
2095 | 3 | ); |
2096 | 16 | } else { |
2097 | 16 | 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)); |
2098 | 16 | } |
2099 | 19 | } |
2100 | | /* }}} */ |
2101 | | |
2102 | | ZEND_API zend_function *zend_std_get_constructor(zend_object *zobj) /* {{{ */ |
2103 | 264k | { |
2104 | 264k | zend_function *constructor = zobj->ce->constructor; |
2105 | | |
2106 | 264k | if (constructor) { |
2107 | 230k | if (UNEXPECTED(!(constructor->common.fn_flags & ZEND_ACC_PUBLIC))) { |
2108 | 25 | const zend_class_entry *scope = get_fake_or_executed_scope(); |
2109 | 25 | ZEND_ASSERT(!(constructor->common.fn_flags & ZEND_ACC_PUBLIC)); |
2110 | 25 | if (!zend_check_method_accessible(constructor, scope)) { |
2111 | 19 | zend_bad_constructor_call(constructor, scope); |
2112 | 19 | zend_object_store_ctor_failed(zobj); |
2113 | 19 | constructor = NULL; |
2114 | 19 | } |
2115 | 25 | } |
2116 | 230k | } |
2117 | | |
2118 | 264k | return constructor; |
2119 | 264k | } |
2120 | | /* }}} */ |
2121 | | |
2122 | | ZEND_API int zend_std_compare_objects(zval *o1, zval *o2) /* {{{ */ |
2123 | 298 | { |
2124 | 298 | zend_object *zobj1, *zobj2; |
2125 | | |
2126 | 298 | if (zend_objects_check_stack_limit()) { |
2127 | 0 | zend_throw_error(NULL, "Maximum call stack size reached during object comparison"); |
2128 | 0 | return ZEND_UNCOMPARABLE; |
2129 | 0 | } |
2130 | | |
2131 | 298 | if (Z_TYPE_P(o1) != Z_TYPE_P(o2)) { |
2132 | | /* Object and non-object */ |
2133 | 295 | zval *object; |
2134 | 295 | zval *value; |
2135 | 295 | zval casted; |
2136 | 295 | bool object_lhs; |
2137 | 295 | if (Z_TYPE_P(o1) == IS_OBJECT) { |
2138 | 283 | object = o1; |
2139 | 283 | value = o2; |
2140 | 283 | object_lhs = true; |
2141 | 283 | } else { |
2142 | 12 | object = o2; |
2143 | 12 | value = o1; |
2144 | 12 | object_lhs = false; |
2145 | 12 | } |
2146 | 295 | ZEND_ASSERT(Z_TYPE_P(value) != IS_OBJECT); |
2147 | 295 | uint8_t target_type = Z_TYPE_P(value); |
2148 | | /* Should be handled in zend_compare(). */ |
2149 | 295 | ZEND_ASSERT(target_type != IS_FALSE && target_type != IS_TRUE); |
2150 | 295 | if (Z_OBJ_HT_P(object)->cast_object(Z_OBJ_P(object), &casted, target_type) == FAILURE) { |
2151 | | // TODO: Less crazy. |
2152 | 295 | if (target_type == IS_LONG || target_type == IS_DOUBLE) { |
2153 | 265 | zend_error(E_NOTICE, "Object of class %s could not be converted to %s", |
2154 | 265 | ZSTR_VAL(Z_OBJCE_P(object)->name), zend_get_type_by_const(target_type)); |
2155 | 265 | if (target_type == IS_LONG) { |
2156 | 247 | ZVAL_LONG(&casted, 1); |
2157 | 247 | } else { |
2158 | 18 | ZVAL_DOUBLE(&casted, 1.0); |
2159 | 18 | } |
2160 | 265 | } else { |
2161 | 30 | return object_lhs ? 1 : -1; |
2162 | 30 | } |
2163 | 295 | } |
2164 | 265 | int ret = object_lhs ? zend_compare(&casted, value) : zend_compare(value, &casted); |
2165 | 265 | zval_ptr_dtor(&casted); |
2166 | 265 | return ret; |
2167 | 295 | } |
2168 | | |
2169 | 3 | zobj1 = Z_OBJ_P(o1); |
2170 | 3 | zobj2 = Z_OBJ_P(o2); |
2171 | | |
2172 | 3 | if (zobj1 == zobj2) { |
2173 | 0 | return 0; /* the same object */ |
2174 | 0 | } |
2175 | 3 | if (zobj1->ce != zobj2->ce) { |
2176 | 0 | return ZEND_UNCOMPARABLE; /* different classes */ |
2177 | 0 | } |
2178 | 3 | if (!zobj1->properties && !zobj2->properties |
2179 | 3 | && !zend_object_is_lazy(zobj1) && !zend_object_is_lazy(zobj2)) { |
2180 | 3 | zend_property_info *info; |
2181 | 3 | int i; |
2182 | | |
2183 | 3 | if (!zobj1->ce->default_properties_count) { |
2184 | 3 | return 0; |
2185 | 3 | } |
2186 | | |
2187 | | /* It's enough to protect only one of the objects. |
2188 | | * The second one may be referenced from the first and this may cause |
2189 | | * false recursion detection. |
2190 | | */ |
2191 | | /* use bitwise OR to make only one conditional jump */ |
2192 | 0 | if (UNEXPECTED(Z_IS_RECURSIVE_P(o1))) { |
2193 | 0 | zend_throw_error(NULL, "Nesting level too deep - recursive dependency?"); |
2194 | 0 | return ZEND_UNCOMPARABLE; |
2195 | 0 | } |
2196 | 0 | Z_PROTECT_RECURSION_P(o1); |
2197 | |
|
2198 | 0 | GC_ADDREF(zobj1); |
2199 | 0 | GC_ADDREF(zobj2); |
2200 | 0 | int ret; |
2201 | |
|
2202 | 0 | for (i = 0; i < zobj1->ce->default_properties_count; i++) { |
2203 | 0 | zval *p1, *p2; |
2204 | |
|
2205 | 0 | info = zobj1->ce->properties_info_table[i]; |
2206 | |
|
2207 | 0 | if (!info) { |
2208 | 0 | continue; |
2209 | 0 | } |
2210 | | |
2211 | 0 | p1 = OBJ_PROP(zobj1, info->offset); |
2212 | 0 | p2 = OBJ_PROP(zobj2, info->offset); |
2213 | |
|
2214 | 0 | if (Z_TYPE_P(p1) != IS_UNDEF) { |
2215 | 0 | if (Z_TYPE_P(p2) != IS_UNDEF) { |
2216 | 0 | ret = zend_compare(p1, p2); |
2217 | 0 | if (ret != 0) { |
2218 | 0 | Z_UNPROTECT_RECURSION_P(o1); |
2219 | 0 | goto done; |
2220 | 0 | } |
2221 | 0 | } else { |
2222 | 0 | Z_UNPROTECT_RECURSION_P(o1); |
2223 | 0 | ret = 1; |
2224 | 0 | goto done; |
2225 | 0 | } |
2226 | 0 | } else { |
2227 | 0 | if (Z_TYPE_P(p2) != IS_UNDEF) { |
2228 | 0 | Z_UNPROTECT_RECURSION_P(o1); |
2229 | 0 | ret = 1; |
2230 | 0 | goto done; |
2231 | 0 | } |
2232 | 0 | } |
2233 | 0 | } |
2234 | | |
2235 | 0 | Z_UNPROTECT_RECURSION_P(o1); |
2236 | 0 | ret = 0; |
2237 | |
|
2238 | 0 | done: |
2239 | 0 | OBJ_RELEASE(zobj1); |
2240 | 0 | OBJ_RELEASE(zobj2); |
2241 | |
|
2242 | 0 | return ret; |
2243 | 0 | } else { |
2244 | 0 | GC_ADDREF(zobj1); |
2245 | 0 | GC_ADDREF(zobj2); |
2246 | |
|
2247 | 0 | int ret = zend_compare_symbol_tables( |
2248 | 0 | zend_std_get_properties_ex(zobj1), |
2249 | 0 | zend_std_get_properties_ex(zobj2)); |
2250 | |
|
2251 | 0 | OBJ_RELEASE(zobj1); |
2252 | 0 | OBJ_RELEASE(zobj2); |
2253 | |
|
2254 | 0 | return ret; |
2255 | 0 | } |
2256 | 3 | } |
2257 | | /* }}} */ |
2258 | | |
2259 | | ZEND_API int zend_objects_not_comparable(zval *o1, zval *o2) |
2260 | 3 | { |
2261 | 3 | return ZEND_UNCOMPARABLE; |
2262 | 3 | } |
2263 | | |
2264 | | // todo: make zend_std_has_property return bool as well |
2265 | | ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has_set_exists, void **cache_slot) /* {{{ */ |
2266 | 56 | { |
2267 | 56 | bool result; |
2268 | 56 | zval *value = NULL; |
2269 | 56 | uintptr_t property_offset; |
2270 | 56 | const zend_property_info *prop_info = NULL; |
2271 | | |
2272 | 56 | property_offset = zend_get_property_offset(zobj->ce, name, 1, cache_slot, &prop_info); |
2273 | | |
2274 | 56 | if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) { |
2275 | 37 | try_again: |
2276 | 37 | value = OBJ_PROP(zobj, property_offset); |
2277 | 37 | if (Z_TYPE_P(value) != IS_UNDEF) { |
2278 | 19 | goto found; |
2279 | 19 | } |
2280 | 18 | if (UNEXPECTED(Z_PROP_FLAG_P(value) & IS_PROP_UNINIT)) { |
2281 | | /* Skip __isset() for uninitialized typed properties */ |
2282 | 18 | goto lazy_init; |
2283 | 18 | } |
2284 | 19 | } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) { |
2285 | 18 | if (EXPECTED(zobj->properties != NULL)) { |
2286 | 0 | if (!IS_UNKNOWN_DYNAMIC_PROPERTY_OFFSET(property_offset)) { |
2287 | 0 | uintptr_t idx = ZEND_DECODE_DYN_PROP_OFFSET(property_offset); |
2288 | |
|
2289 | 0 | if (EXPECTED(idx < zobj->properties->nNumUsed * sizeof(Bucket))) { |
2290 | 0 | Bucket *p = (Bucket*)((char*)zobj->properties->arData + idx); |
2291 | |
|
2292 | 0 | if (EXPECTED(p->key == name) || |
2293 | 0 | (EXPECTED(p->h == ZSTR_H(name)) && |
2294 | 0 | EXPECTED(p->key != NULL) && |
2295 | 0 | EXPECTED(zend_string_equal_content(p->key, name)))) { |
2296 | 0 | value = &p->val; |
2297 | 0 | goto found; |
2298 | 0 | } |
2299 | 0 | } |
2300 | 0 | CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET); |
2301 | 0 | } |
2302 | 0 | value = zend_hash_find(zobj->properties, name); |
2303 | 0 | if (value) { |
2304 | 0 | if (cache_slot) { |
2305 | 0 | uintptr_t idx = (char*)value - (char*)zobj->properties->arData; |
2306 | 0 | CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_ENCODE_DYN_PROP_OFFSET(idx)); |
2307 | 0 | } |
2308 | 19 | found: |
2309 | 19 | if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY) { |
2310 | 0 | result = zend_is_true(value); |
2311 | 19 | } else if (has_set_exists < ZEND_PROPERTY_NOT_EMPTY) { |
2312 | 18 | ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_ISSET); |
2313 | 18 | ZVAL_DEREF(value); |
2314 | 18 | result = (Z_TYPE_P(value) != IS_NULL); |
2315 | 18 | } else { |
2316 | 1 | ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_EXISTS); |
2317 | 1 | result = true; |
2318 | 1 | } |
2319 | 19 | goto exit; |
2320 | 19 | } |
2321 | 0 | } |
2322 | 18 | } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) { |
2323 | 0 | zend_function *get = prop_info->hooks[ZEND_PROPERTY_HOOK_GET]; |
2324 | |
|
2325 | 0 | if (has_set_exists == ZEND_PROPERTY_EXISTS) { |
2326 | 0 | if (prop_info->flags & ZEND_ACC_VIRTUAL) { |
2327 | 0 | return true; |
2328 | 0 | } |
2329 | 0 | property_offset = prop_info->offset; |
2330 | 0 | goto try_again; |
2331 | 0 | } |
2332 | | |
2333 | 0 | if (!get) { |
2334 | 0 | if (prop_info->flags & ZEND_ACC_VIRTUAL) { |
2335 | 0 | zend_throw_error(NULL, "Cannot read from set-only virtual property %s::$%s", |
2336 | 0 | ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name)); |
2337 | 0 | return 0; |
2338 | 0 | } else { |
2339 | 0 | property_offset = prop_info->offset; |
2340 | 0 | goto try_again; |
2341 | 0 | } |
2342 | 0 | } |
2343 | | |
2344 | 0 | zval rv; |
2345 | 0 | if (!zend_call_get_hook(prop_info, name, get, zobj, &rv)) { |
2346 | 0 | if (EG(exception)) { |
2347 | 0 | return 0; |
2348 | 0 | } |
2349 | 0 | property_offset = prop_info->offset; |
2350 | 0 | goto try_again; |
2351 | 0 | } |
2352 | | |
2353 | 0 | if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY) { |
2354 | 0 | result = zend_is_true(&rv); |
2355 | 0 | } else { |
2356 | 0 | ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_ISSET); |
2357 | 0 | result = Z_TYPE(rv) != IS_NULL |
2358 | 0 | && (Z_TYPE(rv) != IS_REFERENCE || Z_TYPE_P(Z_REFVAL(rv)) != IS_NULL); |
2359 | 0 | } |
2360 | 0 | zval_ptr_dtor(&rv); |
2361 | 0 | return result; |
2362 | 1 | } else if (UNEXPECTED(EG(exception))) { |
2363 | 0 | result = false; |
2364 | 0 | goto exit; |
2365 | 0 | } |
2366 | | |
2367 | 19 | if (!zobj->ce->__isset) { |
2368 | 19 | goto lazy_init; |
2369 | 19 | } |
2370 | | |
2371 | 0 | result = false; |
2372 | 0 | if (has_set_exists != ZEND_PROPERTY_EXISTS) { |
2373 | 0 | uint32_t *guard = zend_get_property_guard(zobj, name); |
2374 | |
|
2375 | 0 | if (!((*guard) & IN_ISSET)) { |
2376 | 0 | zval rv; |
2377 | | |
2378 | | /* have issetter - try with it! */ |
2379 | 0 | GC_ADDREF(zobj); |
2380 | 0 | (*guard) |= IN_ISSET; /* prevent circular getting */ |
2381 | 0 | zend_std_call_issetter(zobj, name, &rv); |
2382 | 0 | result = zend_is_true(&rv); |
2383 | 0 | zval_ptr_dtor(&rv); |
2384 | 0 | if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY && result) { |
2385 | 0 | if (EXPECTED(!EG(exception)) && zobj->ce->__get && !((*guard) & IN_GET)) { |
2386 | 0 | (*guard) |= IN_GET; |
2387 | 0 | zend_std_call_getter(zobj, name, &rv); |
2388 | 0 | (*guard) &= ~IN_GET; |
2389 | 0 | result = i_zend_is_true(&rv); |
2390 | 0 | zval_ptr_dtor(&rv); |
2391 | 0 | } else { |
2392 | 0 | result = false; |
2393 | 0 | } |
2394 | 0 | } |
2395 | 0 | (*guard) &= ~IN_ISSET; |
2396 | 0 | OBJ_RELEASE(zobj); |
2397 | 0 | } else { |
2398 | 0 | goto lazy_init; |
2399 | 0 | } |
2400 | 0 | } |
2401 | | |
2402 | 56 | exit: |
2403 | 56 | return result; |
2404 | | |
2405 | 37 | lazy_init: |
2406 | 37 | if (UNEXPECTED(zend_lazy_object_must_init(zobj))) { |
2407 | 0 | if (!value || (Z_PROP_FLAG_P(value) & IS_PROP_LAZY)) { |
2408 | 0 | zobj = zend_lazy_object_init(zobj); |
2409 | 0 | if (!zobj) { |
2410 | 0 | result = false; |
2411 | 0 | goto exit; |
2412 | 0 | } |
2413 | | |
2414 | 0 | if (UNEXPECTED(zobj->ce->__isset)) { |
2415 | 0 | uint32_t *guard = zend_get_property_guard(zobj, name); |
2416 | 0 | if (!((*guard) & IN_ISSET)) { |
2417 | 0 | (*guard) |= IN_ISSET; |
2418 | 0 | result = zend_std_has_property(zobj, name, has_set_exists, cache_slot); |
2419 | 0 | (*guard) &= ~IN_ISSET; |
2420 | 0 | return result; |
2421 | 0 | } |
2422 | 0 | } |
2423 | | |
2424 | 0 | return zend_std_has_property(zobj, name, has_set_exists, cache_slot); |
2425 | 0 | } |
2426 | 0 | } |
2427 | | |
2428 | 37 | result = false; |
2429 | 37 | goto exit; |
2430 | 37 | } |
2431 | | /* }}} */ |
2432 | | |
2433 | | ZEND_API zend_string *zend_std_get_class_name(const zend_object *zobj) /* {{{ */ |
2434 | 2.81k | { |
2435 | 2.81k | return zend_string_copy(zobj->ce->name); |
2436 | 2.81k | } |
2437 | | /* }}} */ |
2438 | | |
2439 | | ZEND_API zend_result zend_std_cast_object_tostring(zend_object *readobj, zval *writeobj, int type) /* {{{ */ |
2440 | 5.90k | { |
2441 | 5.90k | switch (type) { |
2442 | 5.21k | case IS_STRING: { |
2443 | 5.21k | const zend_class_entry *ce = readobj->ce; |
2444 | 5.21k | if (ce->__tostring) { |
2445 | 4.47k | zval retval; |
2446 | 4.47k | GC_ADDREF(readobj); |
2447 | 4.47k | zend_call_known_instance_method_with_0_params(ce->__tostring, readobj, &retval); |
2448 | 4.47k | zend_object_release(readobj); |
2449 | 4.47k | if (EXPECTED(Z_TYPE(retval) == IS_STRING)) { |
2450 | 2.38k | is_string: |
2451 | 2.38k | ZVAL_COPY_VALUE(writeobj, &retval); |
2452 | 2.38k | return SUCCESS; |
2453 | 2.38k | } else if (Z_ISREF(retval)) { |
2454 | 1 | zend_unwrap_reference(&retval); |
2455 | 1 | goto is_string; |
2456 | 1 | } |
2457 | 2.08k | zval_ptr_dtor(&retval); |
2458 | 2.08k | if (!EG(exception)) { |
2459 | 0 | zend_throw_error(NULL, "Method %s::__toString() must return a string value", ZSTR_VAL(ce->name)); |
2460 | 0 | } |
2461 | 2.08k | } |
2462 | 2.83k | return FAILURE; |
2463 | 5.21k | } |
2464 | 1 | case _IS_BOOL: |
2465 | 1 | ZVAL_TRUE(writeobj); |
2466 | 1 | return SUCCESS; |
2467 | 685 | default: |
2468 | 685 | return FAILURE; |
2469 | 5.90k | } |
2470 | 5.90k | } |
2471 | | /* }}} */ |
2472 | | |
2473 | | ZEND_API zend_result zend_std_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only) /* {{{ */ |
2474 | 49 | { |
2475 | 49 | zend_class_entry *ce = obj->ce; |
2476 | 49 | const zval *func = zend_hash_find_known_hash(&ce->function_table, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE)); |
2477 | | |
2478 | 49 | if (func == NULL) { |
2479 | 1 | return FAILURE; |
2480 | 1 | } |
2481 | 48 | *fptr_ptr = Z_FUNC_P(func); |
2482 | 48 | *ce_ptr = ce; |
2483 | 48 | *obj_ptr = obj; |
2484 | | |
2485 | 48 | return SUCCESS; |
2486 | 49 | } |
2487 | | /* }}} */ |
2488 | | |
2489 | 2.20k | ZEND_API HashTable *zend_std_get_properties_for(zend_object *obj, zend_prop_purpose purpose) { |
2490 | 2.20k | HashTable *ht; |
2491 | 2.20k | switch (purpose) { |
2492 | 2.01k | case ZEND_PROP_PURPOSE_DEBUG: |
2493 | 2.01k | if (obj->handlers->get_debug_info) { |
2494 | 2.01k | int is_temp; |
2495 | 2.01k | ht = obj->handlers->get_debug_info(obj, &is_temp); |
2496 | 2.01k | if (ht && !is_temp) { |
2497 | 1.37k | GC_TRY_ADDREF(ht); |
2498 | 1.37k | } |
2499 | 2.01k | return ht; |
2500 | 2.01k | } |
2501 | 0 | ZEND_FALLTHROUGH; |
2502 | 68 | case ZEND_PROP_PURPOSE_JSON: |
2503 | 108 | case ZEND_PROP_PURPOSE_GET_OBJECT_VARS: |
2504 | 146 | case ZEND_PROP_PURPOSE_VAR_EXPORT: |
2505 | 146 | if (obj->ce->num_hooked_props) { |
2506 | 140 | return zend_hooked_object_build_properties(obj); |
2507 | 140 | } |
2508 | 6 | ht = obj->handlers->get_properties(obj); |
2509 | 6 | if (ht) { |
2510 | 6 | GC_TRY_ADDREF(ht); |
2511 | 6 | } |
2512 | 6 | return ht; |
2513 | 43 | case ZEND_PROP_PURPOSE_ARRAY_CAST: |
2514 | 43 | ht = zend_get_properties_no_lazy_init(obj); |
2515 | 43 | if (ht) { |
2516 | 43 | GC_TRY_ADDREF(ht); |
2517 | 43 | } |
2518 | 43 | return ht; |
2519 | 4 | case ZEND_PROP_PURPOSE_SERIALIZE: { |
2520 | 4 | if (zend_object_is_lazy(obj) |
2521 | 0 | && !zend_lazy_object_initialize_on_serialize(obj)) { |
2522 | 0 | ht = zend_get_properties_no_lazy_init(obj); |
2523 | 4 | } else { |
2524 | 4 | ht = obj->handlers->get_properties(obj); |
2525 | 4 | } |
2526 | 4 | if (ht) { |
2527 | 4 | GC_TRY_ADDREF(ht); |
2528 | 4 | } |
2529 | 4 | return ht; |
2530 | 146 | } |
2531 | 0 | default: |
2532 | 0 | ZEND_UNREACHABLE(); |
2533 | 0 | return NULL; |
2534 | 2.20k | } |
2535 | 2.20k | } |
2536 | | |
2537 | 2.29k | ZEND_API HashTable *zend_get_properties_for(zval *obj, zend_prop_purpose purpose) { |
2538 | 2.29k | zend_object *zobj = Z_OBJ_P(obj); |
2539 | | |
2540 | 2.29k | if (zobj->handlers->get_properties_for) { |
2541 | 100 | return zobj->handlers->get_properties_for(zobj, purpose); |
2542 | 100 | } |
2543 | | |
2544 | 2.19k | return zend_std_get_properties_for(zobj, purpose); |
2545 | 2.29k | } |
2546 | | |
2547 | | ZEND_API const zend_object_handlers std_object_handlers = { |
2548 | | 0, /* offset */ |
2549 | | |
2550 | | zend_object_std_dtor, /* free_obj */ |
2551 | | zend_objects_destroy_object, /* dtor_obj */ |
2552 | | zend_objects_clone_obj, /* clone_obj */ |
2553 | | zend_objects_clone_obj_with, /* clone_obj_with */ |
2554 | | |
2555 | | zend_std_read_property, /* read_property */ |
2556 | | zend_std_write_property, /* write_property */ |
2557 | | zend_std_read_dimension, /* read_dimension */ |
2558 | | zend_std_write_dimension, /* write_dimension */ |
2559 | | zend_std_get_property_ptr_ptr, /* get_property_ptr_ptr */ |
2560 | | zend_std_has_property, /* has_property */ |
2561 | | zend_std_unset_property, /* unset_property */ |
2562 | | zend_std_has_dimension, /* has_dimension */ |
2563 | | zend_std_unset_dimension, /* unset_dimension */ |
2564 | | zend_std_get_properties, /* get_properties */ |
2565 | | zend_std_get_method, /* get_method */ |
2566 | | zend_std_get_constructor, /* get_constructor */ |
2567 | | zend_std_get_class_name, /* get_class_name */ |
2568 | | zend_std_cast_object_tostring, /* cast_object */ |
2569 | | NULL, /* count_elements */ |
2570 | | zend_std_get_debug_info, /* get_debug_info */ |
2571 | | zend_std_get_closure, /* get_closure */ |
2572 | | zend_std_get_gc, /* get_gc */ |
2573 | | NULL, /* do_operation */ |
2574 | | zend_std_compare_objects, /* compare */ |
2575 | | NULL, /* get_properties_for */ |
2576 | | }; |
2577 | | |
2578 | 14 | void zend_object_handlers_startup(void) { |
2579 | 14 | zend_call_trampoline_arginfo[0].name = ZSTR_KNOWN(ZEND_STR_ARGS); |
2580 | 14 | zend_property_hook_arginfo[0].name = ZSTR_KNOWN(ZEND_STR_VALUE); |
2581 | 14 | } |