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