xref: /PHP-7.1/Zend/zend_objects.c (revision ccd4716e)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 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@zend.com>                                |
16    |          Zeev Suraski <zeev@zend.com>                                |
17    |          Dmitry Stogov <dmitry@zend.com>                             |
18    +----------------------------------------------------------------------+
19 */
20 
21 /* $Id$ */
22 
23 #include "zend.h"
24 #include "zend_globals.h"
25 #include "zend_variables.h"
26 #include "zend_API.h"
27 #include "zend_interfaces.h"
28 #include "zend_exceptions.h"
29 
zend_object_std_init(zend_object * object,zend_class_entry * ce)30 ZEND_API void zend_object_std_init(zend_object *object, zend_class_entry *ce)
31 {
32 	zval *p, *end;
33 
34 	GC_REFCOUNT(object) = 1;
35 	GC_TYPE_INFO(object) = IS_OBJECT;
36 	object->ce = ce;
37 	object->properties = NULL;
38 	zend_objects_store_put(object);
39 	p = object->properties_table;
40 	if (EXPECTED(ce->default_properties_count != 0)) {
41 		end = p + ce->default_properties_count;
42 		do {
43 			ZVAL_UNDEF(p);
44 			p++;
45 		} while (p != end);
46 	}
47 	if (UNEXPECTED(ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
48 		GC_FLAGS(object) |= IS_OBJ_USE_GUARDS;
49 		ZVAL_UNDEF(p);
50 	}
51 }
52 
zend_object_std_dtor(zend_object * object)53 ZEND_API void zend_object_std_dtor(zend_object *object)
54 {
55 	zval *p, *end;
56 
57 	if (object->properties) {
58 		if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) {
59 			if (EXPECTED(--GC_REFCOUNT(object->properties) == 0)) {
60 				zend_array_destroy(object->properties);
61 			}
62 		}
63 	}
64 	p = object->properties_table;
65 	if (EXPECTED(object->ce->default_properties_count)) {
66 		end = p + object->ce->default_properties_count;
67 		do {
68 			i_zval_ptr_dtor(p ZEND_FILE_LINE_CC);
69 			p++;
70 		} while (p != end);
71 	}
72 	if (UNEXPECTED(GC_FLAGS(object) & IS_OBJ_HAS_GUARDS)) {
73 		if (EXPECTED(Z_TYPE_P(p) == IS_STRING)) {
74 			zend_string_release(Z_STR_P(p));
75 		} else {
76 			HashTable *guards;
77 
78 			ZEND_ASSERT(Z_TYPE_P(p) == IS_ARRAY);
79 			guards = Z_ARRVAL_P(p);
80 			ZEND_ASSERT(guards != NULL);
81 			zend_hash_destroy(guards);
82 			FREE_HASHTABLE(guards);
83 		}
84 	}
85 }
86 
zend_objects_destroy_object(zend_object * object)87 ZEND_API void zend_objects_destroy_object(zend_object *object)
88 {
89 	zend_function *destructor = object ? object->ce->destructor : NULL;
90 
91 	if (destructor) {
92 		zend_object *old_exception;
93 		zval obj;
94 		zend_class_entry *orig_fake_scope;
95 
96 		if (destructor->op_array.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
97 			if (destructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
98 				/* Ensure that if we're calling a private function, we're allowed to do so.
99 				 */
100 				if (EG(current_execute_data)) {
101 					zend_class_entry *scope = zend_get_executed_scope();
102 
103 					if (object->ce != scope) {
104 						zend_throw_error(NULL,
105 							"Call to private %s::__destruct() from context '%s'",
106 							ZSTR_VAL(object->ce->name),
107 							scope ? ZSTR_VAL(scope->name) : "");
108 						return;
109 					}
110 				} else {
111 					zend_error(E_WARNING,
112 						"Call to private %s::__destruct() from context '' during shutdown ignored",
113 						ZSTR_VAL(object->ce->name));
114 					return;
115 				}
116 			} else {
117 				/* Ensure that if we're calling a protected function, we're allowed to do so.
118 				 */
119 				if (EG(current_execute_data)) {
120 					zend_class_entry *scope = zend_get_executed_scope();
121 
122 					if (!zend_check_protected(zend_get_function_root_class(destructor), scope)) {
123 						zend_throw_error(NULL,
124 							"Call to protected %s::__destruct() from context '%s'",
125 							ZSTR_VAL(object->ce->name),
126 							scope ? ZSTR_VAL(scope->name) : "");
127 						return;
128 					}
129 				} else {
130 					zend_error(E_WARNING,
131 						"Call to protected %s::__destruct() from context '' during shutdown ignored",
132 						ZSTR_VAL(object->ce->name));
133 					return;
134 				}
135 			}
136 		}
137 
138 		GC_REFCOUNT(object)++;
139 		ZVAL_OBJ(&obj, object);
140 
141 		/* Make sure that destructors are protected from previously thrown exceptions.
142 		 * For example, if an exception was thrown in a function and when the function's
143 		 * local variable destruction results in a destructor being called.
144 		 */
145 		old_exception = NULL;
146 		if (EG(exception)) {
147 			if (EG(exception) == object) {
148 				zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception");
149 			} else {
150 				old_exception = EG(exception);
151 				EG(exception) = NULL;
152 			}
153 		}
154 		orig_fake_scope = EG(fake_scope);
155 		EG(fake_scope) = NULL;
156 		zend_call_method_with_0_params(&obj, object->ce, &destructor, ZEND_DESTRUCTOR_FUNC_NAME, NULL);
157 		if (old_exception) {
158 			if (EG(exception)) {
159 				zend_exception_set_previous(EG(exception), old_exception);
160 			} else {
161 				EG(exception) = old_exception;
162 			}
163 		}
164 		zval_ptr_dtor(&obj);
165 		EG(fake_scope) = orig_fake_scope;
166 	}
167 }
168 
zend_objects_new(zend_class_entry * ce)169 ZEND_API zend_object *zend_objects_new(zend_class_entry *ce)
170 {
171 	zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce));
172 
173 	zend_object_std_init(object, ce);
174 	object->handlers = &std_object_handlers;
175 	return object;
176 }
177 
zend_objects_clone_members(zend_object * new_object,zend_object * old_object)178 ZEND_API void zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
179 {
180 	if (old_object->ce->default_properties_count) {
181 		zval *src = old_object->properties_table;
182 		zval *dst = new_object->properties_table;
183 		zval *end = src + old_object->ce->default_properties_count;
184 
185 		do {
186 			i_zval_ptr_dtor(dst ZEND_FILE_LINE_CC);
187 			ZVAL_COPY_VALUE(dst, src);
188 			zval_add_ref(dst);
189 			src++;
190 			dst++;
191 		} while (src != end);
192 	} else if (old_object->properties && !old_object->ce->clone) {
193 		/* fast copy */
194 		if (EXPECTED(old_object->handlers == &std_object_handlers)) {
195 			if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) {
196 				GC_REFCOUNT(old_object->properties)++;
197 			}
198 			new_object->properties = old_object->properties;
199 			return;
200 		}
201 	}
202 
203 	if (old_object->properties &&
204 	    EXPECTED(zend_hash_num_elements(old_object->properties))) {
205 		zval *prop, new_prop;
206 		zend_ulong num_key;
207 		zend_string *key;
208 
209 		if (!new_object->properties) {
210 			ALLOC_HASHTABLE(new_object->properties);
211 			zend_hash_init(new_object->properties, zend_hash_num_elements(old_object->properties), NULL, ZVAL_PTR_DTOR, 0);
212 			zend_hash_real_init(new_object->properties, 0);
213 		} else {
214 			zend_hash_extend(new_object->properties, new_object->properties->nNumUsed + zend_hash_num_elements(old_object->properties), 0);
215 		}
216 
217 		new_object->properties->u.v.flags |=
218 			old_object->properties->u.v.flags & HASH_FLAG_HAS_EMPTY_IND;
219 
220 		ZEND_HASH_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) {
221 			if (Z_TYPE_P(prop) == IS_INDIRECT) {
222 				ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table));
223 			} else {
224 				ZVAL_COPY_VALUE(&new_prop, prop);
225 				zval_add_ref(&new_prop);
226 			}
227 			if (EXPECTED(key)) {
228 				_zend_hash_append(new_object->properties, key, &new_prop);
229 			} else {
230 				zend_hash_index_add_new(new_object->properties, num_key, &new_prop);
231 			}
232 		} ZEND_HASH_FOREACH_END();
233 	}
234 
235 	if (old_object->ce->clone) {
236 		zval new_obj;
237 
238 		ZVAL_OBJ(&new_obj, new_object);
239 		Z_ADDREF(new_obj);
240 		zend_call_method_with_0_params(&new_obj, old_object->ce, &old_object->ce->clone, ZEND_CLONE_FUNC_NAME, NULL);
241 		zval_ptr_dtor(&new_obj);
242 	}
243 }
244 
zend_objects_clone_obj(zval * zobject)245 ZEND_API zend_object *zend_objects_clone_obj(zval *zobject)
246 {
247 	zend_object *old_object;
248 	zend_object *new_object;
249 
250 	/* assume that create isn't overwritten, so when clone depends on the
251 	 * overwritten one then it must itself be overwritten */
252 	old_object = Z_OBJ_P(zobject);
253 	new_object = zend_objects_new(old_object->ce);
254 
255 	zend_objects_clone_members(new_object, old_object);
256 
257 	return new_object;
258 }
259 
260 /*
261  * Local variables:
262  * tab-width: 4
263  * c-basic-offset: 4
264  * indent-tabs-mode: t
265  * End:
266  */
267