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 +----------------------------------------------------------------------+
18 */
19
20 #ifndef ZEND_OBJECT_HANDLERS_H
21 #define ZEND_OBJECT_HANDLERS_H
22
23 #include <stdint.h>
24
25 #include "zend_hash.h"
26 #include "zend_types.h"
27 #include "zend_property_hooks.h"
28 #include "zend_lazy_objects.h"
29
30 struct _zend_property_info;
31
32 #define ZEND_WRONG_PROPERTY_INFO \
33 ((struct _zend_property_info*)((intptr_t)-1))
34
35 #define ZEND_DYNAMIC_PROPERTY_OFFSET ((uintptr_t)(intptr_t)(-1))
36
37 /* The first 4 bits in the property offset are used within the cache slot for
38 * storing information about the property. This value may be bumped to
39 * offsetof(zend_object, properties_table) in the future. */
40 #define ZEND_FIRST_PROPERTY_OFFSET (1 << 4)
41 #define IS_VALID_PROPERTY_OFFSET(offset) ((intptr_t)(offset) >= ZEND_FIRST_PROPERTY_OFFSET)
42 #define IS_WRONG_PROPERTY_OFFSET(offset) ((intptr_t)(offset) == 0)
43 #define IS_HOOKED_PROPERTY_OFFSET(offset) \
44 ((intptr_t)(offset) > 0 && (intptr_t)(offset) < 16)
45 #define IS_DYNAMIC_PROPERTY_OFFSET(offset) ((intptr_t)(offset) < 0)
46
47 #define ZEND_PROPERTY_HOOK_SIMPLE_READ_BIT 2u
48 #define ZEND_PROPERTY_HOOK_SIMPLE_WRITE_BIT 4u
49 #define ZEND_PROPERTY_HOOK_SIMPLE_GET_BIT 8u
50 #define ZEND_IS_PROPERTY_HOOK_SIMPLE_READ(offset) \
51 (((offset) & ZEND_PROPERTY_HOOK_SIMPLE_READ_BIT) != 0)
52 #define ZEND_IS_PROPERTY_HOOK_SIMPLE_WRITE(offset) \
53 (((offset) & ZEND_PROPERTY_HOOK_SIMPLE_WRITE_BIT) != 0)
54 #define ZEND_IS_PROPERTY_HOOK_SIMPLE_GET(offset) \
55 (((offset) & ZEND_PROPERTY_HOOK_SIMPLE_GET_BIT) != 0)
56 #define ZEND_SET_PROPERTY_HOOK_SIMPLE_READ(cache_slot) \
57 do { \
58 void **__cache_slot = (cache_slot); \
59 if (__cache_slot) { \
60 CACHE_PTR_EX(__cache_slot + 1, (void*)((uintptr_t)CACHED_PTR_EX(__cache_slot + 1) | ZEND_PROPERTY_HOOK_SIMPLE_READ_BIT)); \
61 } \
62 } while (0)
63 #define ZEND_SET_PROPERTY_HOOK_SIMPLE_WRITE(cache_slot) \
64 do { \
65 void **__cache_slot = (cache_slot); \
66 if (__cache_slot) { \
67 CACHE_PTR_EX(__cache_slot + 1, (void*)((uintptr_t)CACHED_PTR_EX(__cache_slot + 1) | ZEND_PROPERTY_HOOK_SIMPLE_WRITE_BIT)); \
68 } \
69 } while (0)
70 #define ZEND_SET_PROPERTY_HOOK_SIMPLE_GET(cache_slot) \
71 do { \
72 void **__cache_slot = (cache_slot); \
73 if (__cache_slot) { \
74 CACHE_PTR_EX(__cache_slot + 1, (void*)((uintptr_t)CACHED_PTR_EX(__cache_slot + 1) | ZEND_PROPERTY_HOOK_SIMPLE_GET_BIT)); \
75 } \
76 } while (0)
77
78 #define IS_UNKNOWN_DYNAMIC_PROPERTY_OFFSET(offset) (offset == ZEND_DYNAMIC_PROPERTY_OFFSET)
79 #define ZEND_DECODE_DYN_PROP_OFFSET(offset) ((uintptr_t)(-(intptr_t)(offset) - 2))
80 #define ZEND_ENCODE_DYN_PROP_OFFSET(offset) ((uintptr_t)(-((intptr_t)(offset) + 2)))
81
82
83 /* Used to fetch property from the object, read-only */
84 typedef zval *(*zend_object_read_property_t)(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv);
85
86 /* Used to fetch dimension from the object, read-only */
87 typedef zval *(*zend_object_read_dimension_t)(zend_object *object, zval *offset, int type, zval *rv);
88
89
90 /* Used to set property of the object
91 You must return the final value of the assigned property.
92 */
93 typedef zval *(*zend_object_write_property_t)(zend_object *object, zend_string *member, zval *value, void **cache_slot);
94
95 /* Used to set dimension of the object */
96 typedef void (*zend_object_write_dimension_t)(zend_object *object, zval *offset, zval *value);
97
98
99 /* Used to create pointer to the property of the object, for future direct r/w access.
100 * May return one of:
101 * * A zval pointer, without incrementing the reference count.
102 * * &EG(error_zval), if an exception has been thrown.
103 * * NULL, if acquiring a direct pointer is not possible.
104 * In this case, the VM will fall back to using read_property and write_property.
105 */
106 typedef zval *(*zend_object_get_property_ptr_ptr_t)(zend_object *object, zend_string *member, int type, void **cache_slot);
107
108 /* Used to check if a property of the object exists */
109 /* param has_set_exists:
110 * 0 (has) whether property exists and is not NULL
111 * 1 (set) whether property exists and is true
112 * 2 (exists) whether property exists
113 */
114 typedef int (*zend_object_has_property_t)(zend_object *object, zend_string *member, int has_set_exists, void **cache_slot);
115
116 /* Used to check if a dimension of the object exists */
117 typedef int (*zend_object_has_dimension_t)(zend_object *object, zval *member, int check_empty);
118
119 /* Used to remove a property of the object */
120 typedef void (*zend_object_unset_property_t)(zend_object *object, zend_string *member, void **cache_slot);
121
122 /* Used to remove a dimension of the object */
123 typedef void (*zend_object_unset_dimension_t)(zend_object *object, zval *offset);
124
125 /* Used to get hash of the properties of the object, as hash of zval's */
126 typedef HashTable *(*zend_object_get_properties_t)(zend_object *object);
127
128 typedef HashTable *(*zend_object_get_debug_info_t)(zend_object *object, int *is_temp);
129
130 typedef enum _zend_prop_purpose {
131 /* Used for debugging. Supersedes get_debug_info handler. */
132 ZEND_PROP_PURPOSE_DEBUG,
133 /* Used for (array) casts. */
134 ZEND_PROP_PURPOSE_ARRAY_CAST,
135 /* Used for serialization using the "O" scheme.
136 * Unserialization will use __wakeup(). */
137 ZEND_PROP_PURPOSE_SERIALIZE,
138 /* Used for var_export().
139 * The data will be passed to __set_state() when evaluated. */
140 ZEND_PROP_PURPOSE_VAR_EXPORT,
141 /* Used for json_encode(). */
142 ZEND_PROP_PURPOSE_JSON,
143 /* Used for get_object_vars(). */
144 ZEND_PROP_PURPOSE_GET_OBJECT_VARS,
145 /* Dummy member to ensure that "default" is specified. */
146 _ZEND_PROP_PURPOSE_NON_EXHAUSTIVE_ENUM
147 } zend_prop_purpose;
148
149 /* The return value must be released using zend_release_properties(). */
150 typedef zend_array *(*zend_object_get_properties_for_t)(zend_object *object, zend_prop_purpose purpose);
151
152 /* Used to call methods */
153 /* args on stack! */
154 /* Andi - EX(fbc) (function being called) needs to be initialized already in the INIT fcall opcode so that the parameters can be parsed the right way. We need to add another callback for this.
155 */
156 typedef zend_function *(*zend_object_get_method_t)(zend_object **object, zend_string *method, const zval *key);
157 typedef zend_function *(*zend_object_get_constructor_t)(zend_object *object);
158
159 /* free_obj should release any resources the object holds, without freeing the
160 * object structure itself. The object does not need to be in a valid state after
161 * free_obj finishes running.
162 *
163 * free_obj will always be invoked, even if the object leaks or a fatal error
164 * occurs. However, during shutdown it may be called once the executor is no
165 * longer active, in which case execution of user code may be skipped.
166 */
167 typedef void (*zend_object_free_obj_t)(zend_object *object);
168
169 /* dtor_obj is called before free_obj. The object must remain in a valid state
170 * after dtor_obj finishes running. Unlike free_obj, it is run prior to
171 * deactivation of the executor during shutdown, which allows user code to run.
172 *
173 * This handler is not guaranteed to be called (e.g. on fatal error), and as
174 * such should not be used to release resources or deallocate memory. Furthermore,
175 * releasing resources in this handler can break detection of memory leaks, as
176 * cycles may be broken early.
177 *
178 * dtor_obj should be used *only* to call user destruction hooks, such as __destruct.
179 */
180 typedef void (*zend_object_dtor_obj_t)(zend_object *object);
181
182 typedef zend_object* (*zend_object_clone_obj_t)(zend_object *object);
183
184 /* Get class name for display in var_dump and other debugging functions.
185 * Must be defined and must return a non-NULL value. */
186 typedef zend_string *(*zend_object_get_class_name_t)(const zend_object *object);
187
188 typedef int (*zend_object_compare_t)(zval *object1, zval *object2);
189
190 /* Cast an object to some other type.
191 * readobj and retval must point to distinct zvals.
192 */
193 typedef zend_result (*zend_object_cast_t)(zend_object *readobj, zval *retval, int type);
194
195 /* updates *count to hold the number of elements present and returns SUCCESS.
196 * Returns FAILURE if the object does not have any sense of overloaded dimensions */
197 typedef zend_result (*zend_object_count_elements_t)(zend_object *object, zend_long *count);
198
199 typedef zend_result (*zend_object_get_closure_t)(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only);
200
201 typedef HashTable *(*zend_object_get_gc_t)(zend_object *object, zval **table, int *n);
202
203 typedef zend_result (*zend_object_do_operation_t)(uint8_t opcode, zval *result, zval *op1, zval *op2);
204
205 struct _zend_object_handlers {
206 /* offset of real object header (usually zero) */
207 int offset;
208 /* object handlers */
209 zend_object_free_obj_t free_obj; /* required */
210 zend_object_dtor_obj_t dtor_obj; /* required */
211 zend_object_clone_obj_t clone_obj; /* optional */
212 zend_object_read_property_t read_property; /* required */
213 zend_object_write_property_t write_property; /* required */
214 zend_object_read_dimension_t read_dimension; /* required */
215 zend_object_write_dimension_t write_dimension; /* required */
216 zend_object_get_property_ptr_ptr_t get_property_ptr_ptr; /* required */
217 zend_object_has_property_t has_property; /* required */
218 zend_object_unset_property_t unset_property; /* required */
219 zend_object_has_dimension_t has_dimension; /* required */
220 zend_object_unset_dimension_t unset_dimension; /* required */
221 zend_object_get_properties_t get_properties; /* required */
222 zend_object_get_method_t get_method; /* required */
223 zend_object_get_constructor_t get_constructor; /* required */
224 zend_object_get_class_name_t get_class_name; /* required */
225 zend_object_cast_t cast_object; /* required */
226 zend_object_count_elements_t count_elements; /* optional */
227 zend_object_get_debug_info_t get_debug_info; /* optional */
228 zend_object_get_closure_t get_closure; /* optional */
229 zend_object_get_gc_t get_gc; /* required */
230 zend_object_do_operation_t do_operation; /* optional */
231 zend_object_compare_t compare; /* required */
232 zend_object_get_properties_for_t get_properties_for; /* optional */
233 };
234
235 BEGIN_EXTERN_C()
236 extern const ZEND_API zend_object_handlers std_object_handlers;
237
238 #define zend_get_std_object_handlers() \
239 (&std_object_handlers)
240
241 #define zend_get_function_root_class(fbc) \
242 ((fbc)->common.prototype ? (fbc)->common.prototype->common.scope : (fbc)->common.scope)
243
244 #define ZEND_PROPERTY_ISSET 0x0 /* Property exists and is not NULL */
245 #define ZEND_PROPERTY_NOT_EMPTY ZEND_ISEMPTY /* Property is not empty */
246 #define ZEND_PROPERTY_EXISTS 0x2 /* Property exists */
247
248 ZEND_API void zend_class_init_statics(zend_class_entry *ce);
249 ZEND_API zend_function *zend_std_get_static_method(zend_class_entry *ce, zend_string *function_name_strval, const zval *key);
250 ZEND_API zval *zend_std_get_static_property_with_info(zend_class_entry *ce, zend_string *property_name, int type, struct _zend_property_info **prop_info);
251 ZEND_API zval *zend_std_get_static_property(zend_class_entry *ce, zend_string *property_name, int type);
252 ZEND_API ZEND_COLD bool zend_std_unset_static_property(zend_class_entry *ce, zend_string *property_name);
253 ZEND_API zend_function *zend_std_get_constructor(zend_object *object);
254 ZEND_API struct _zend_property_info *zend_get_property_info(const zend_class_entry *ce, zend_string *member, int silent);
255 ZEND_API HashTable *zend_std_get_properties(zend_object *object);
256 ZEND_API HashTable *zend_get_properties_no_lazy_init(zend_object *zobj);
257 ZEND_API HashTable *zend_std_get_gc(zend_object *object, zval **table, int *n);
258 ZEND_API HashTable *zend_std_get_debug_info(zend_object *object, int *is_temp);
259 ZEND_API zend_result zend_std_cast_object_tostring(zend_object *object, zval *writeobj, int type);
260 ZEND_API zval *zend_std_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot);
261 ZEND_API zval *zend_std_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv);
262 ZEND_API zval *zend_std_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot);
263 ZEND_API int zend_std_has_property(zend_object *object, zend_string *member, int has_set_exists, void **cache_slot);
264 ZEND_API void zend_std_unset_property(zend_object *object, zend_string *member, void **cache_slot);
265 ZEND_API zval *zend_std_read_dimension(zend_object *object, zval *offset, int type, zval *rv);
266 ZEND_API void zend_std_write_dimension(zend_object *object, zval *offset, zval *value);
267 ZEND_API int zend_std_has_dimension(zend_object *object, zval *offset, int check_empty);
268 ZEND_API void zend_std_unset_dimension(zend_object *object, zval *offset);
269 ZEND_API zend_function *zend_std_get_method(zend_object **obj_ptr, zend_string *method_name, const zval *key);
270 ZEND_API zend_string *zend_std_get_class_name(const zend_object *zobj);
271 ZEND_API int zend_std_compare_objects(zval *o1, zval *o2);
272 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);
273 /* Use zend_std_get_properties_ex() */
274 ZEND_API HashTable *rebuild_object_properties_internal(zend_object *zobj);
275
zend_std_get_properties_ex(zend_object * object)276 static zend_always_inline HashTable *zend_std_get_properties_ex(zend_object *object)
277 {
278 if (UNEXPECTED(zend_lazy_object_must_init(object))) {
279 return zend_lazy_object_get_properties(object);
280 }
281 if (!object->properties) {
282 return rebuild_object_properties_internal(object);
283 }
284 return object->properties;
285 }
286
287 /* Implements the fast path for array cast */
288 ZEND_API HashTable *zend_std_build_object_properties_array(zend_object *zobj);
289
290 #define ZEND_STD_BUILD_OBJECT_PROPERTIES_ARRAY_COMPATIBLE(object) ( \
291 /* We can use zend_std_build_object_properties_array() for objects \
292 * without properties ht and with standard handlers */ \
293 Z_OBJ_P(object)->properties == NULL \
294 && Z_OBJ_HT_P(object)->get_properties_for == NULL \
295 && Z_OBJ_HT_P(object)->get_properties == zend_std_get_properties \
296 /* For initialized proxies we need to forward to the real instance */ \
297 && ( \
298 !zend_object_is_lazy_proxy(Z_OBJ_P(object)) \
299 || !zend_lazy_object_initialized(Z_OBJ_P(object)) \
300 ) \
301 )
302
303 /* Handler for objects that cannot be meaningfully compared.
304 * Only objects with the same identity will be considered equal. */
305 ZEND_API int zend_objects_not_comparable(zval *o1, zval *o2);
306
307 ZEND_API bool zend_check_protected(const zend_class_entry *ce, const zend_class_entry *scope);
308
309 ZEND_API zend_result zend_check_property_access(const zend_object *zobj, zend_string *prop_info_name, bool is_dynamic);
310
311 ZEND_API zend_function *zend_get_call_trampoline_func(const zend_class_entry *ce, zend_string *method_name, bool is_static);
312
313 ZEND_API uint32_t *zend_get_property_guard(zend_object *zobj, zend_string *member);
314
315 ZEND_API uint32_t *zend_get_property_guard(zend_object *zobj, zend_string *member);
316
317 ZEND_API uint32_t *zend_get_recursion_guard(zend_object *zobj);
318
319 /* Default behavior for get_properties_for. For use as a fallback in custom
320 * get_properties_for implementations. */
321 ZEND_API HashTable *zend_std_get_properties_for(zend_object *obj, zend_prop_purpose purpose);
322
323 /* Will call get_properties_for handler or use default behavior. For use by
324 * consumers of the get_properties_for API. */
325 ZEND_API HashTable *zend_get_properties_for(zval *obj, zend_prop_purpose purpose);
326
327 typedef struct _zend_property_info zend_property_info;
328
329 ZEND_API zend_function *zend_get_property_hook_trampoline(
330 const zend_property_info *prop_info,
331 zend_property_hook_kind kind, zend_string *prop_name);
332
333 ZEND_API bool ZEND_FASTCALL zend_asymmetric_property_has_set_access(const zend_property_info *prop_info);
334
335 #define zend_release_properties(ht) do { \
336 if ((ht) && !(GC_FLAGS(ht) & GC_IMMUTABLE) && !GC_DELREF(ht)) { \
337 zend_array_destroy(ht); \
338 } \
339 } while (0)
340
341 #define zend_free_trampoline(func) do { \
342 if ((func) == &EG(trampoline)) { \
343 EG(trampoline).common.function_name = NULL; \
344 } else { \
345 efree(func); \
346 } \
347 } while (0)
348
349 /* Fallback to default comparison implementation if the arguments aren't both objects
350 * and have the same compare() handler. You'll likely want to use this unless you
351 * explicitly wish to support comparisons between objects and non-objects. */
352 #define ZEND_COMPARE_OBJECTS_FALLBACK(op1, op2) \
353 if (Z_TYPE_P(op1) != IS_OBJECT || \
354 Z_TYPE_P(op2) != IS_OBJECT || \
355 Z_OBJ_HT_P(op1)->compare != Z_OBJ_HT_P(op2)->compare) { \
356 return zend_std_compare_objects(op1, op2); \
357 }
358
359 END_EXTERN_C()
360
361 #endif
362