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