/src/php-src/Zend/zend_objects.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) | |
6 | | +----------------------------------------------------------------------+ |
7 | | | This source file is subject to version 2.00 of the Zend license, | |
8 | | | that is bundled with this package in the file LICENSE, and is | |
9 | | | available through the world-wide-web at the following url: | |
10 | | | http://www.zend.com/license/2_00.txt. | |
11 | | | If you did not receive a copy of the Zend license and are unable to | |
12 | | | obtain it through the world-wide-web, please send a note to | |
13 | | | license@zend.com so we can mail you a copy immediately. | |
14 | | +----------------------------------------------------------------------+ |
15 | | | Authors: Andi Gutmans <andi@php.net> | |
16 | | | Zeev Suraski <zeev@php.net> | |
17 | | | Dmitry Stogov <dmitry@php.net> | |
18 | | +----------------------------------------------------------------------+ |
19 | | */ |
20 | | |
21 | | #include "zend.h" |
22 | | #include "zend_globals.h" |
23 | | #include "zend_variables.h" |
24 | | #include "zend_API.h" |
25 | | #include "zend_interfaces.h" |
26 | | #include "zend_exceptions.h" |
27 | | #include "zend_weakrefs.h" |
28 | | #include "zend_lazy_objects.h" |
29 | | |
30 | | static zend_always_inline void _zend_object_std_init(zend_object *object, zend_class_entry *ce) |
31 | 5.40M | { |
32 | 5.40M | GC_SET_REFCOUNT(object, 1); |
33 | 5.40M | GC_TYPE_INFO(object) = GC_OBJECT; |
34 | 5.40M | object->ce = ce; |
35 | 5.40M | object->extra_flags = 0; |
36 | 5.40M | object->handlers = ce->default_object_handlers; |
37 | 5.40M | object->properties = NULL; |
38 | 5.40M | zend_objects_store_put(object); |
39 | 5.40M | if (UNEXPECTED(ce->ce_flags & ZEND_ACC_USE_GUARDS)) { |
40 | 365k | zval *guard_value = object->properties_table + object->ce->default_properties_count; |
41 | 365k | ZVAL_UNDEF(guard_value); |
42 | 365k | Z_GUARD_P(guard_value) = 0; |
43 | 365k | } |
44 | 5.40M | } |
45 | | |
46 | | ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce) |
47 | 1.44M | { |
48 | 1.44M | _zend_object_std_init(object, ce); |
49 | 1.44M | } |
50 | | |
51 | | void zend_object_dtor_dynamic_properties(zend_object *object) |
52 | 5.40M | { |
53 | 5.40M | if (object->properties) { |
54 | 790k | if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) { |
55 | 790k | if (EXPECTED(GC_DELREF(object->properties) == 0) |
56 | 790k | && EXPECTED(GC_TYPE(object->properties) != IS_NULL)) { |
57 | 790k | zend_array_destroy(object->properties); |
58 | 790k | } |
59 | 790k | } |
60 | 790k | } |
61 | 5.40M | } |
62 | | |
63 | | void zend_object_dtor_property(zend_object *object, zval *p) |
64 | 9.07M | { |
65 | 9.07M | if (Z_REFCOUNTED_P(p)) { |
66 | 3.79M | if (UNEXPECTED(Z_ISREF_P(p)) && |
67 | 3.79M | (ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(p)))) { |
68 | 40.5k | zend_property_info *prop_info = zend_get_property_info_for_slot_self(object, p); |
69 | 40.5k | if (ZEND_TYPE_IS_SET(prop_info->type)) { |
70 | 40.0k | ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info); |
71 | 40.0k | } |
72 | 40.5k | } |
73 | 3.79M | i_zval_ptr_dtor(p); |
74 | 3.79M | } |
75 | 9.07M | } |
76 | | |
77 | | ZEND_API void zend_object_std_dtor(zend_object *object) |
78 | 5.40M | { |
79 | 5.40M | zval *p, *end; |
80 | | |
81 | 5.40M | if (UNEXPECTED(GC_FLAGS(object) & IS_OBJ_WEAKLY_REFERENCED)) { |
82 | 355 | zend_weakrefs_notify(object); |
83 | 355 | } |
84 | | |
85 | 5.40M | if (UNEXPECTED(zend_object_is_lazy(object))) { |
86 | 1.51k | zend_lazy_object_del_info(object); |
87 | 1.51k | } |
88 | | |
89 | 5.40M | zend_object_dtor_dynamic_properties(object); |
90 | | |
91 | 5.40M | p = object->properties_table; |
92 | 5.40M | if (EXPECTED(object->ce->default_properties_count)) { |
93 | 1.31M | end = p + object->ce->default_properties_count; |
94 | 9.07M | do { |
95 | 9.07M | zend_object_dtor_property(object, p); |
96 | 9.07M | p++; |
97 | 9.07M | } while (p != end); |
98 | 1.31M | } |
99 | | |
100 | 5.40M | if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_USE_GUARDS)) { |
101 | 365k | if (EXPECTED(Z_TYPE_P(p) == IS_STRING)) { |
102 | 931 | zval_ptr_dtor_str(p); |
103 | 364k | } else if (Z_TYPE_P(p) == IS_ARRAY) { |
104 | 61 | HashTable *guards; |
105 | | |
106 | 61 | guards = Z_ARRVAL_P(p); |
107 | 61 | ZEND_ASSERT(guards != NULL); |
108 | 61 | zend_hash_destroy(guards); |
109 | 61 | FREE_HASHTABLE(guards); |
110 | 61 | } |
111 | 365k | } |
112 | 5.40M | } |
113 | | |
114 | | ZEND_API void zend_objects_destroy_object(zend_object *object) |
115 | 286k | { |
116 | 286k | zend_function *destructor = object->ce->destructor; |
117 | | |
118 | 286k | if (destructor) { |
119 | 286k | if (UNEXPECTED(zend_object_is_lazy(object))) { |
120 | 159 | return; |
121 | 159 | } |
122 | | |
123 | 286k | zend_object *old_exception; |
124 | 286k | const zend_op *old_opline_before_exception; |
125 | | |
126 | 286k | if (destructor->op_array.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) { |
127 | 0 | if (destructor->op_array.fn_flags & ZEND_ACC_PRIVATE) { |
128 | | /* Ensure that if we're calling a private function, we're allowed to do so. |
129 | | */ |
130 | 0 | if (EG(current_execute_data)) { |
131 | 0 | zend_class_entry *scope = zend_get_executed_scope(); |
132 | |
|
133 | 0 | if (object->ce != scope) { |
134 | 0 | zend_throw_error(NULL, |
135 | 0 | "Call to private %s::__destruct() from %s%s", |
136 | 0 | ZSTR_VAL(object->ce->name), |
137 | 0 | scope ? "scope " : "global scope", |
138 | 0 | scope ? ZSTR_VAL(scope->name) : "" |
139 | 0 | ); |
140 | 0 | return; |
141 | 0 | } |
142 | 0 | } else { |
143 | 0 | zend_error(E_WARNING, |
144 | 0 | "Call to private %s::__destruct() from global scope during shutdown ignored", |
145 | 0 | ZSTR_VAL(object->ce->name)); |
146 | 0 | return; |
147 | 0 | } |
148 | 0 | } else { |
149 | | /* Ensure that if we're calling a protected function, we're allowed to do so. |
150 | | */ |
151 | 0 | if (EG(current_execute_data)) { |
152 | 0 | zend_class_entry *scope = zend_get_executed_scope(); |
153 | |
|
154 | 0 | if (!zend_check_protected(zend_get_function_root_class(destructor), scope)) { |
155 | 0 | zend_throw_error(NULL, |
156 | 0 | "Call to protected %s::__destruct() from %s%s", |
157 | 0 | ZSTR_VAL(object->ce->name), |
158 | 0 | scope ? "scope " : "global scope", |
159 | 0 | scope ? ZSTR_VAL(scope->name) : "" |
160 | 0 | ); |
161 | 0 | return; |
162 | 0 | } |
163 | 0 | } else { |
164 | 0 | zend_error(E_WARNING, |
165 | 0 | "Call to protected %s::__destruct() from global scope during shutdown ignored", |
166 | 0 | ZSTR_VAL(object->ce->name)); |
167 | 0 | return; |
168 | 0 | } |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | 286k | GC_ADDREF(object); |
173 | | |
174 | | /* Make sure that destructors are protected from previously thrown exceptions. |
175 | | * For example, if an exception was thrown in a function and when the function's |
176 | | * local variable destruction results in a destructor being called. |
177 | | */ |
178 | 286k | old_exception = NULL; |
179 | 286k | if (EG(exception)) { |
180 | 269k | if (EG(exception) == object) { |
181 | 0 | zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception"); |
182 | 269k | } else { |
183 | 269k | if (EG(current_execute_data) |
184 | 269k | && EG(current_execute_data)->func |
185 | 269k | && ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) { |
186 | 113k | zend_rethrow_exception(EG(current_execute_data)); |
187 | 113k | } |
188 | 269k | old_exception = EG(exception); |
189 | 269k | old_opline_before_exception = EG(opline_before_exception); |
190 | 269k | EG(exception) = NULL; |
191 | 269k | } |
192 | 269k | } |
193 | | |
194 | 286k | zend_call_known_instance_method_with_0_params(destructor, object, NULL); |
195 | | |
196 | 286k | if (old_exception) { |
197 | 253 | EG(opline_before_exception) = old_opline_before_exception; |
198 | 253 | if (EG(exception)) { |
199 | 125 | zend_exception_set_previous(EG(exception), old_exception); |
200 | 128 | } else { |
201 | 128 | EG(exception) = old_exception; |
202 | 128 | } |
203 | 253 | } |
204 | 286k | OBJ_RELEASE(object); |
205 | 286k | } |
206 | 286k | } |
207 | | |
208 | | ZEND_API zend_object* ZEND_FASTCALL zend_objects_new(zend_class_entry *ce) |
209 | 3.95M | { |
210 | 3.95M | zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce)); |
211 | | |
212 | 3.95M | _zend_object_std_init(object, ce); |
213 | 3.95M | return object; |
214 | 3.95M | } |
215 | | |
216 | | ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, zend_object *old_object) |
217 | 549 | { |
218 | 549 | bool has_clone_method = old_object->ce->clone != NULL; |
219 | | |
220 | 549 | if (old_object->ce->default_properties_count) { |
221 | 239 | zval *src = old_object->properties_table; |
222 | 239 | zval *dst = new_object->properties_table; |
223 | 239 | zval *end = src + old_object->ce->default_properties_count; |
224 | | |
225 | 312 | do { |
226 | 312 | i_zval_ptr_dtor(dst); |
227 | 312 | ZVAL_COPY_VALUE_PROP(dst, src); |
228 | 312 | zval_add_ref(dst); |
229 | 312 | if (has_clone_method) { |
230 | | /* Unconditionally add the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */ |
231 | 144 | Z_PROP_FLAG_P(dst) |= IS_PROP_REINITABLE; |
232 | 144 | } |
233 | | |
234 | 312 | if (UNEXPECTED(Z_ISREF_P(dst)) && |
235 | 312 | (ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(dst)))) { |
236 | 5 | zend_property_info *prop_info = zend_get_property_info_for_slot_self(new_object, dst); |
237 | 5 | if (ZEND_TYPE_IS_SET(prop_info->type)) { |
238 | 5 | ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(dst), prop_info); |
239 | 5 | } |
240 | 5 | } |
241 | 312 | src++; |
242 | 312 | dst++; |
243 | 312 | } while (src != end); |
244 | 310 | } else if (old_object->properties && !has_clone_method) { |
245 | | /* fast copy */ |
246 | 139 | if (EXPECTED(old_object->handlers == &std_object_handlers)) { |
247 | 139 | if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) { |
248 | 139 | GC_ADDREF(old_object->properties); |
249 | 139 | } |
250 | 139 | new_object->properties = old_object->properties; |
251 | 139 | return; |
252 | 139 | } |
253 | 139 | } |
254 | | |
255 | 410 | if (old_object->properties && |
256 | 410 | EXPECTED(zend_hash_num_elements(old_object->properties))) { |
257 | 17 | zval *prop, new_prop; |
258 | 17 | zend_ulong num_key; |
259 | 17 | zend_string *key; |
260 | | |
261 | 17 | if (!new_object->properties) { |
262 | 17 | new_object->properties = zend_new_array(zend_hash_num_elements(old_object->properties)); |
263 | 17 | zend_hash_real_init_mixed(new_object->properties); |
264 | 17 | } else { |
265 | 0 | zend_hash_extend(new_object->properties, new_object->properties->nNumUsed + zend_hash_num_elements(old_object->properties), 0); |
266 | 0 | } |
267 | | |
268 | 17 | HT_FLAGS(new_object->properties) |= |
269 | 17 | HT_FLAGS(old_object->properties) & HASH_FLAG_HAS_EMPTY_IND; |
270 | | |
271 | 78 | ZEND_HASH_MAP_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) { |
272 | 78 | if (Z_TYPE_P(prop) == IS_INDIRECT) { |
273 | 17 | ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table)); |
274 | 17 | } else { |
275 | 5 | ZVAL_COPY_VALUE(&new_prop, prop); |
276 | 5 | zval_add_ref(&new_prop); |
277 | 5 | } |
278 | 78 | if (has_clone_method) { |
279 | | /* Unconditionally add the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */ |
280 | 10 | Z_PROP_FLAG_P(&new_prop) |= IS_PROP_REINITABLE; |
281 | 10 | } |
282 | 78 | if (EXPECTED(key)) { |
283 | 22 | _zend_hash_append(new_object->properties, key, &new_prop); |
284 | 22 | } else { |
285 | 0 | zend_hash_index_add_new(new_object->properties, num_key, &new_prop); |
286 | 0 | } |
287 | 78 | } ZEND_HASH_FOREACH_END(); |
288 | 17 | } |
289 | | |
290 | 410 | if (has_clone_method) { |
291 | 173 | GC_ADDREF(new_object); |
292 | 173 | zend_call_known_instance_method_with_0_params(new_object->ce->clone, new_object, NULL); |
293 | | |
294 | 173 | if (ZEND_CLASS_HAS_READONLY_PROPS(new_object->ce)) { |
295 | 219 | for (uint32_t i = 0; i < new_object->ce->default_properties_count; i++) { |
296 | 116 | zval* prop = OBJ_PROP_NUM(new_object, i); |
297 | | /* Unconditionally remove the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */ |
298 | 116 | Z_PROP_FLAG_P(prop) &= ~IS_PROP_REINITABLE; |
299 | 116 | } |
300 | 103 | } |
301 | | |
302 | 173 | OBJ_RELEASE(new_object); |
303 | 173 | } |
304 | 410 | } |
305 | | |
306 | | ZEND_API zend_object *zend_objects_clone_obj(zend_object *old_object) |
307 | 611 | { |
308 | 611 | zend_object *new_object; |
309 | | |
310 | 611 | if (UNEXPECTED(zend_object_is_lazy(old_object))) { |
311 | 67 | return zend_lazy_object_clone(old_object); |
312 | 67 | } |
313 | | |
314 | | /* assume that create isn't overwritten, so when clone depends on the |
315 | | * overwritten one then it must itself be overwritten */ |
316 | 544 | new_object = zend_objects_new(old_object->ce); |
317 | | |
318 | | /* zend_objects_clone_members() expect the properties to be initialized. */ |
319 | 544 | if (new_object->ce->default_properties_count) { |
320 | 239 | zval *p = new_object->properties_table; |
321 | 239 | zval *end = p + new_object->ce->default_properties_count; |
322 | 312 | do { |
323 | 312 | ZVAL_UNDEF(p); |
324 | 312 | p++; |
325 | 312 | } while (p != end); |
326 | 239 | } |
327 | | |
328 | 544 | zend_objects_clone_members(new_object, old_object); |
329 | | |
330 | 544 | return new_object; |
331 | 611 | } |