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