xref: /PHP-8.1/Zend/zend_objects.c (revision 08dafda1)
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->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 (ZEND_TYPE_IS_SET(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 		const zend_op *old_opline_before_exception;
101 
102 		if (destructor->op_array.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
103 			if (destructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
104 				/* Ensure that if we're calling a private function, we're allowed to do so.
105 				 */
106 				if (EG(current_execute_data)) {
107 					zend_class_entry *scope = zend_get_executed_scope();
108 
109 					if (object->ce != scope) {
110 						zend_throw_error(NULL,
111 							"Call to private %s::__destruct() from %s%s",
112 							ZSTR_VAL(object->ce->name),
113 							scope ? "scope " : "global scope",
114 							scope ? ZSTR_VAL(scope->name) : ""
115 						);
116 						return;
117 					}
118 				} else {
119 					zend_error(E_WARNING,
120 						"Call to private %s::__destruct() from global scope during shutdown ignored",
121 						ZSTR_VAL(object->ce->name));
122 					return;
123 				}
124 			} else {
125 				/* Ensure that if we're calling a protected function, we're allowed to do so.
126 				 */
127 				if (EG(current_execute_data)) {
128 					zend_class_entry *scope = zend_get_executed_scope();
129 
130 					if (!zend_check_protected(zend_get_function_root_class(destructor), scope)) {
131 						zend_throw_error(NULL,
132 							"Call to protected %s::__destruct() from %s%s",
133 							ZSTR_VAL(object->ce->name),
134 							scope ? "scope " : "global scope",
135 							scope ? ZSTR_VAL(scope->name) : ""
136 						);
137 						return;
138 					}
139 				} else {
140 					zend_error(E_WARNING,
141 						"Call to protected %s::__destruct() from global scope during shutdown ignored",
142 						ZSTR_VAL(object->ce->name));
143 					return;
144 				}
145 			}
146 		}
147 
148 		GC_ADDREF(object);
149 
150 		/* Make sure that destructors are protected from previously thrown exceptions.
151 		 * For example, if an exception was thrown in a function and when the function's
152 		 * local variable destruction results in a destructor being called.
153 		 */
154 		old_exception = NULL;
155 		if (EG(exception)) {
156 			if (EG(exception) == object) {
157 				zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception");
158 			} else {
159 				if (EG(current_execute_data)
160 				 && EG(current_execute_data)->func
161 				 && ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) {
162 					zend_rethrow_exception(EG(current_execute_data));
163 				}
164 				old_exception = EG(exception);
165 				old_opline_before_exception = EG(opline_before_exception);
166 				EG(exception) = NULL;
167 			}
168 		}
169 
170 		zend_call_known_instance_method_with_0_params(destructor, object, NULL);
171 
172 		if (old_exception) {
173 			EG(opline_before_exception) = old_opline_before_exception;
174 			if (EG(exception)) {
175 				zend_exception_set_previous(EG(exception), old_exception);
176 			} else {
177 				EG(exception) = old_exception;
178 			}
179 		}
180 		OBJ_RELEASE(object);
181 	}
182 }
183 
zend_objects_new(zend_class_entry * ce)184 ZEND_API zend_object* ZEND_FASTCALL zend_objects_new(zend_class_entry *ce)
185 {
186 	zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce));
187 
188 	_zend_object_std_init(object, ce);
189 	object->handlers = &std_object_handlers;
190 	return object;
191 }
192 
zend_objects_clone_members(zend_object * new_object,zend_object * old_object)193 ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
194 {
195 	if (old_object->ce->default_properties_count) {
196 		zval *src = old_object->properties_table;
197 		zval *dst = new_object->properties_table;
198 		zval *end = src + old_object->ce->default_properties_count;
199 
200 		do {
201 			i_zval_ptr_dtor(dst);
202 			ZVAL_COPY_VALUE_PROP(dst, src);
203 			zval_add_ref(dst);
204 			if (UNEXPECTED(Z_ISREF_P(dst)) &&
205 					(ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(dst)))) {
206 				zend_property_info *prop_info = zend_get_property_info_for_slot(new_object, dst);
207 				if (ZEND_TYPE_IS_SET(prop_info->type)) {
208 					ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(dst), prop_info);
209 				}
210 			}
211 			src++;
212 			dst++;
213 		} while (src != end);
214 	} else if (old_object->properties && !old_object->ce->clone) {
215 		/* fast copy */
216 		if (EXPECTED(old_object->handlers == &std_object_handlers)) {
217 			if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) {
218 				GC_ADDREF(old_object->properties);
219 			}
220 			new_object->properties = old_object->properties;
221 			return;
222 		}
223 	}
224 
225 	if (old_object->properties &&
226 	    EXPECTED(zend_hash_num_elements(old_object->properties))) {
227 		zval *prop, new_prop;
228 		zend_ulong num_key;
229 		zend_string *key;
230 
231 		if (!new_object->properties) {
232 			new_object->properties = zend_new_array(zend_hash_num_elements(old_object->properties));
233 			zend_hash_real_init_mixed(new_object->properties);
234 		} else {
235 			zend_hash_extend(new_object->properties, new_object->properties->nNumUsed + zend_hash_num_elements(old_object->properties), 0);
236 		}
237 
238 		HT_FLAGS(new_object->properties) |=
239 			HT_FLAGS(old_object->properties) & HASH_FLAG_HAS_EMPTY_IND;
240 
241 		ZEND_HASH_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) {
242 			if (Z_TYPE_P(prop) == IS_INDIRECT) {
243 				ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table));
244 			} else {
245 				ZVAL_COPY_VALUE(&new_prop, prop);
246 				zval_add_ref(&new_prop);
247 			}
248 			if (EXPECTED(key)) {
249 				_zend_hash_append(new_object->properties, key, &new_prop);
250 			} else {
251 				zend_hash_index_add_new(new_object->properties, num_key, &new_prop);
252 			}
253 		} ZEND_HASH_FOREACH_END();
254 	}
255 
256 	if (old_object->ce->clone) {
257 		GC_ADDREF(new_object);
258 		zend_call_known_instance_method_with_0_params(new_object->ce->clone, new_object, NULL);
259 		OBJ_RELEASE(new_object);
260 	}
261 }
262 
zend_objects_clone_obj(zend_object * old_object)263 ZEND_API zend_object *zend_objects_clone_obj(zend_object *old_object)
264 {
265 	zend_object *new_object;
266 
267 	/* assume that create isn't overwritten, so when clone depends on the
268 	 * overwritten one then it must itself be overwritten */
269 	new_object = zend_objects_new(old_object->ce);
270 
271 	/* zend_objects_clone_members() expect the properties to be initialized. */
272 	if (new_object->ce->default_properties_count) {
273 		zval *p = new_object->properties_table;
274 		zval *end = p + new_object->ce->default_properties_count;
275 		do {
276 			ZVAL_UNDEF(p);
277 			p++;
278 		} while (p != end);
279 	}
280 
281 	zend_objects_clone_members(new_object, old_object);
282 
283 	return new_object;
284 }
285