xref: /php-src/Zend/zend_objects.c (revision c65e042c)
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 
_zend_object_std_init(zend_object * object,zend_class_entry * ce)30 static zend_always_inline void _zend_object_std_init(zend_object *object, zend_class_entry *ce)
31 {
32 	GC_SET_REFCOUNT(object, 1);
33 	GC_TYPE_INFO(object) = GC_OBJECT;
34 	object->ce = ce;
35 	object->extra_flags = 0;
36 	object->handlers = ce->default_object_handlers;
37 	object->properties = NULL;
38 	zend_objects_store_put(object);
39 	if (UNEXPECTED(ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
40 		zval *guard_value = object->properties_table + object->ce->default_properties_count;
41 		ZVAL_UNDEF(guard_value);
42 		Z_GUARD_P(guard_value) = 0;
43 	}
44 }
45 
zend_object_std_init(zend_object * object,zend_class_entry * ce)46 ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
47 {
48 	_zend_object_std_init(object, ce);
49 }
50 
zend_object_dtor_dynamic_properties(zend_object * object)51 void zend_object_dtor_dynamic_properties(zend_object *object)
52 {
53 	if (object->properties) {
54 		if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) {
55 			if (EXPECTED(GC_DELREF(object->properties) == 0)
56 					&& EXPECTED(GC_TYPE(object->properties) != IS_NULL)) {
57 				zend_array_destroy(object->properties);
58 			}
59 		}
60 	}
61 }
62 
zend_object_dtor_property(zend_object * object,zval * p)63 void zend_object_dtor_property(zend_object *object, zval *p)
64 {
65 	if (Z_REFCOUNTED_P(p)) {
66 		if (UNEXPECTED(Z_ISREF_P(p)) &&
67 				(ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(p)))) {
68 			zend_property_info *prop_info = zend_get_property_info_for_slot_self(object, p);
69 			if (ZEND_TYPE_IS_SET(prop_info->type)) {
70 				ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info);
71 			}
72 		}
73 		i_zval_ptr_dtor(p);
74 	}
75 }
76 
zend_object_std_dtor(zend_object * object)77 ZEND_API void zend_object_std_dtor(zend_object *object)
78 {
79 	zval *p, *end;
80 
81 	if (UNEXPECTED(GC_FLAGS(object) & IS_OBJ_WEAKLY_REFERENCED)) {
82 		zend_weakrefs_notify(object);
83 	}
84 
85 	if (UNEXPECTED(zend_object_is_lazy(object))) {
86 		zend_lazy_object_del_info(object);
87 	}
88 
89 	zend_object_dtor_dynamic_properties(object);
90 
91 	p = object->properties_table;
92 	if (EXPECTED(object->ce->default_properties_count)) {
93 		end = p + object->ce->default_properties_count;
94 		do {
95 			zend_object_dtor_property(object, p);
96 			p++;
97 		} while (p != end);
98 	}
99 
100 	if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
101 		if (EXPECTED(Z_TYPE_P(p) == IS_STRING)) {
102 			zval_ptr_dtor_str(p);
103 		} else if (Z_TYPE_P(p) == IS_ARRAY) {
104 			HashTable *guards;
105 
106 			guards = Z_ARRVAL_P(p);
107 			ZEND_ASSERT(guards != NULL);
108 			zend_hash_destroy(guards);
109 			FREE_HASHTABLE(guards);
110 		}
111 	}
112 }
113 
zend_objects_destroy_object(zend_object * object)114 ZEND_API void zend_objects_destroy_object(zend_object *object)
115 {
116 	zend_function *destructor = object->ce->destructor;
117 
118 	if (destructor) {
119 		if (UNEXPECTED(zend_object_is_lazy(object))) {
120 			return;
121 		}
122 
123 		zend_object *old_exception;
124 		const zend_op *old_opline_before_exception;
125 
126 		if (destructor->op_array.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
127 			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 				if (EG(current_execute_data)) {
131 					zend_class_entry *scope = zend_get_executed_scope();
132 
133 					if (object->ce != scope) {
134 						zend_throw_error(NULL,
135 							"Call to private %s::__destruct() from %s%s",
136 							ZSTR_VAL(object->ce->name),
137 							scope ? "scope " : "global scope",
138 							scope ? ZSTR_VAL(scope->name) : ""
139 						);
140 						return;
141 					}
142 				} else {
143 					zend_error(E_WARNING,
144 						"Call to private %s::__destruct() from global scope during shutdown ignored",
145 						ZSTR_VAL(object->ce->name));
146 					return;
147 				}
148 			} else {
149 				/* Ensure that if we're calling a protected function, we're allowed to do so.
150 				 */
151 				if (EG(current_execute_data)) {
152 					zend_class_entry *scope = zend_get_executed_scope();
153 
154 					if (!zend_check_protected(zend_get_function_root_class(destructor), scope)) {
155 						zend_throw_error(NULL,
156 							"Call to protected %s::__destruct() from %s%s",
157 							ZSTR_VAL(object->ce->name),
158 							scope ? "scope " : "global scope",
159 							scope ? ZSTR_VAL(scope->name) : ""
160 						);
161 						return;
162 					}
163 				} else {
164 					zend_error(E_WARNING,
165 						"Call to protected %s::__destruct() from global scope during shutdown ignored",
166 						ZSTR_VAL(object->ce->name));
167 					return;
168 				}
169 			}
170 		}
171 
172 		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 		old_exception = NULL;
179 		if (EG(exception)) {
180 			if (EG(exception) == object) {
181 				zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception");
182 			} else {
183 				if (EG(current_execute_data)
184 				 && EG(current_execute_data)->func
185 				 && ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) {
186 					zend_rethrow_exception(EG(current_execute_data));
187 				}
188 				old_exception = EG(exception);
189 				old_opline_before_exception = EG(opline_before_exception);
190 				EG(exception) = NULL;
191 			}
192 		}
193 
194 		zend_call_known_instance_method_with_0_params(destructor, object, NULL);
195 
196 		if (old_exception) {
197 			EG(opline_before_exception) = old_opline_before_exception;
198 			if (EG(exception)) {
199 				zend_exception_set_previous(EG(exception), old_exception);
200 			} else {
201 				EG(exception) = old_exception;
202 			}
203 		}
204 		OBJ_RELEASE(object);
205 	}
206 }
207 
zend_objects_new(zend_class_entry * ce)208 ZEND_API zend_object* ZEND_FASTCALL zend_objects_new(zend_class_entry *ce)
209 {
210 	zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce));
211 
212 	_zend_object_std_init(object, ce);
213 	return object;
214 }
215 
zend_objects_clone_members(zend_object * new_object,zend_object * old_object)216 ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
217 {
218 	bool has_clone_method = old_object->ce->clone != NULL;
219 
220 	if (old_object->ce->default_properties_count) {
221 		zval *src = old_object->properties_table;
222 		zval *dst = new_object->properties_table;
223 		zval *end = src + old_object->ce->default_properties_count;
224 
225 		do {
226 			i_zval_ptr_dtor(dst);
227 			ZVAL_COPY_VALUE_PROP(dst, src);
228 			zval_add_ref(dst);
229 			if (has_clone_method) {
230 				/* Unconditionally add the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */
231 				Z_PROP_FLAG_P(dst) |= IS_PROP_REINITABLE;
232 			}
233 
234 			if (UNEXPECTED(Z_ISREF_P(dst)) &&
235 					(ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(dst)))) {
236 				zend_property_info *prop_info = zend_get_property_info_for_slot_self(new_object, dst);
237 				if (ZEND_TYPE_IS_SET(prop_info->type)) {
238 					ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(dst), prop_info);
239 				}
240 			}
241 			src++;
242 			dst++;
243 		} while (src != end);
244 	} else if (old_object->properties && !has_clone_method) {
245 		/* fast copy */
246 		if (EXPECTED(old_object->handlers == &std_object_handlers)) {
247 			if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) {
248 				GC_ADDREF(old_object->properties);
249 			}
250 			new_object->properties = old_object->properties;
251 			return;
252 		}
253 	}
254 
255 	if (old_object->properties &&
256 	    EXPECTED(zend_hash_num_elements(old_object->properties))) {
257 		zval *prop, new_prop;
258 		zend_ulong num_key;
259 		zend_string *key;
260 
261 		if (!new_object->properties) {
262 			new_object->properties = zend_new_array(zend_hash_num_elements(old_object->properties));
263 			zend_hash_real_init_mixed(new_object->properties);
264 		} else {
265 			zend_hash_extend(new_object->properties, new_object->properties->nNumUsed + zend_hash_num_elements(old_object->properties), 0);
266 		}
267 
268 		HT_FLAGS(new_object->properties) |=
269 			HT_FLAGS(old_object->properties) & HASH_FLAG_HAS_EMPTY_IND;
270 
271 		ZEND_HASH_MAP_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) {
272 			if (Z_TYPE_P(prop) == IS_INDIRECT) {
273 				ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table));
274 			} else {
275 				ZVAL_COPY_VALUE(&new_prop, prop);
276 				zval_add_ref(&new_prop);
277 			}
278 			if (has_clone_method) {
279 				/* Unconditionally add the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */
280 				Z_PROP_FLAG_P(&new_prop) |= IS_PROP_REINITABLE;
281 			}
282 			if (EXPECTED(key)) {
283 				_zend_hash_append(new_object->properties, key, &new_prop);
284 			} else {
285 				zend_hash_index_add_new(new_object->properties, num_key, &new_prop);
286 			}
287 		} ZEND_HASH_FOREACH_END();
288 	}
289 
290 	if (has_clone_method) {
291 		GC_ADDREF(new_object);
292 		zend_call_known_instance_method_with_0_params(new_object->ce->clone, new_object, NULL);
293 
294 		if (ZEND_CLASS_HAS_READONLY_PROPS(new_object->ce)) {
295 			for (uint32_t i = 0; i < new_object->ce->default_properties_count; i++) {
296 				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 				Z_PROP_FLAG_P(prop) &= ~IS_PROP_REINITABLE;
299 			}
300 		}
301 
302 		OBJ_RELEASE(new_object);
303 	}
304 }
305 
zend_objects_clone_obj(zend_object * old_object)306 ZEND_API zend_object *zend_objects_clone_obj(zend_object *old_object)
307 {
308 	zend_object *new_object;
309 
310 	if (UNEXPECTED(zend_object_is_lazy(old_object))) {
311 		return zend_lazy_object_clone(old_object);
312 	}
313 
314 	/* assume that create isn't overwritten, so when clone depends on the
315 	 * overwritten one then it must itself be overwritten */
316 	new_object = zend_objects_new(old_object->ce);
317 
318 	/* zend_objects_clone_members() expect the properties to be initialized. */
319 	if (new_object->ce->default_properties_count) {
320 		zval *p = new_object->properties_table;
321 		zval *end = p + new_object->ce->default_properties_count;
322 		do {
323 			ZVAL_UNDEF(p);
324 			p++;
325 		} while (p != end);
326 	}
327 
328 	zend_objects_clone_members(new_object, old_object);
329 
330 	return new_object;
331 }
332