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