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