1290ed550SMarcus Boerger/*
2333406bdSfoobar   +----------------------------------------------------------------------+
3333406bdSfoobar   | Zend Engine                                                          |
4333406bdSfoobar   +----------------------------------------------------------------------+
5a6519d05SXinchen Hui   | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
6333406bdSfoobar   +----------------------------------------------------------------------+
7333406bdSfoobar   | This source file is subject to version 2.00 of the Zend license,     |
8333406bdSfoobar   | that is bundled with this package in the file LICENSE, and is        |
9f68c7ff2SJames Cox   | available through the world-wide-web at the following url:           |
10333406bdSfoobar   | http://www.zend.com/license/2_00.txt.                                |
11333406bdSfoobar   | If you did not receive a copy of the Zend license and are unable to  |
12333406bdSfoobar   | obtain it through the world-wide-web, please send a note to          |
13333406bdSfoobar   | license@zend.com so we can mail you a copy immediately.              |
14333406bdSfoobar   +----------------------------------------------------------------------+
159afce019SZeev Suraski   | Authors: Andi Gutmans <andi@php.net>                                 |
169afce019SZeev Suraski   |          Zeev Suraski <zeev@php.net>                                 |
179afce019SZeev Suraski   |          Dmitry Stogov <dmitry@php.net>                              |
18333406bdSfoobar   +----------------------------------------------------------------------+
19333406bdSfoobar*/
20333406bdSfoobar
215af7770aSAndi Gutmans#include "zend.h"
225af7770aSAndi Gutmans#include "zend_globals.h"
23532677e7SAndi Gutmans#include "zend_variables.h"
2429ea3da2SAndi Gutmans#include "zend_API.h"
25e20f534eSMarcus Boerger#include "zend_interfaces.h"
26290ed550SMarcus Boerger#include "zend_exceptions.h"
275af7770aSAndi Gutmans
28175e4613SDmitry StogovZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
2959b8592cSAntony Dovgal{
3049ea143bSDmitry Stogov	GC_SET_REFCOUNT(object, 1);
31d6c332ebSDmitry Stogov	GC_TYPE_INFO(object) = IS_OBJECT | (GC_COLLECTABLE << GC_FLAGS_SHIFT);
32d03900dcSStanislav Malyshev	object->ce = ce;
33c5237d82SDmitry Stogov	object->properties = NULL;
34bdeb220fSAnatol Belski	zend_objects_store_put(object);
351800bed1SDmitry Stogov	if (UNEXPECTED(ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
363abef9c9SNikita Popov		ZVAL_UNDEF(object->properties_table + object->ce->default_properties_count);
379e70d767SDmitry Stogov	}
3859b8592cSAntony Dovgal}
3959b8592cSAntony Dovgal
40bdeb220fSAnatol BelskiZEND_API void zend_object_std_dtor(zend_object *object)
4159b8592cSAntony Dovgal{
421800bed1SDmitry Stogov	zval *p, *end;
43f4cfaf36SDmitry Stogov
4459b8592cSAntony Dovgal	if (object->properties) {
454a6e1345SDmitry Stogov		if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) {
467ba54b98SNikita Popov			if (EXPECTED(GC_DELREF(object->properties) == 0)
4718f2918aSNikita Popov					&& EXPECTED(GC_TYPE(object->properties) != IS_NULL)) {
484a6e1345SDmitry Stogov				zend_array_destroy(object->properties);
494a6e1345SDmitry Stogov			}
504a6e1345SDmitry Stogov		}
51f4cfaf36SDmitry Stogov	}
521800bed1SDmitry Stogov	p = object->properties_table;
531800bed1SDmitry Stogov	if (EXPECTED(object->ce->default_properties_count)) {
541800bed1SDmitry Stogov		end = p + object->ce->default_properties_count;
551800bed1SDmitry Stogov		do {
561800bed1SDmitry Stogov			i_zval_ptr_dtor(p ZEND_FILE_LINE_CC);
571800bed1SDmitry Stogov			p++;
581800bed1SDmitry Stogov		} while (p != end);
5959b8592cSAntony Dovgal	}
6058880e3fSDmitry Stogov	if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
61e88c71d3SDmitry Stogov		if (EXPECTED(Z_TYPE_P(p) == IS_STRING)) {
62af341213SDmitry Stogov			zval_ptr_dtor_str(p);
6358880e3fSDmitry Stogov		} else if (Z_TYPE_P(p) == IS_ARRAY) {
64e88c71d3SDmitry Stogov			HashTable *guards;
659e70d767SDmitry Stogov
66e88c71d3SDmitry Stogov			guards = Z_ARRVAL_P(p);
67e88c71d3SDmitry Stogov			ZEND_ASSERT(guards != NULL);
68e88c71d3SDmitry Stogov			zend_hash_destroy(guards);
69e88c71d3SDmitry Stogov			FREE_HASHTABLE(guards);
70e88c71d3SDmitry Stogov		}
719e70d767SDmitry Stogov	}
7259b8592cSAntony Dovgal}
73669016c7SMarcus Boerger
74bdeb220fSAnatol BelskiZEND_API void zend_objects_destroy_object(zend_object *object)
7573b159e0SAndi Gutmans{
76908ce66fSDmitry Stogov	zend_function *destructor = object->ce->destructor;
7735c40932SMarcus Boerger
7835c40932SMarcus Boerger	if (destructor) {
79f4cfaf36SDmitry Stogov		zend_object *old_exception;
80ae3be78cSXinchen Hui		zend_class_entry *orig_fake_scope;
8134e58a64SDmitry Stogov		zend_fcall_info fci;
8234e58a64SDmitry Stogov		zend_fcall_info_cache fcic;
8334e58a64SDmitry Stogov		zval ret;
84290ed550SMarcus Boerger
85669016c7SMarcus Boerger		if (destructor->op_array.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
86669016c7SMarcus Boerger			if (destructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
87669016c7SMarcus Boerger				/* Ensure that if we're calling a private function, we're allowed to do so.
88669016c7SMarcus Boerger				 */
896499162fSDmitry Stogov				if (EG(current_execute_data)) {
906499162fSDmitry Stogov					zend_class_entry *scope = zend_get_executed_scope();
91d4aa155dSMarcus Boerger
926499162fSDmitry Stogov					if (object->ce != scope) {
935df893ceSAaron Piotrowski						zend_throw_error(NULL,
9422c38b2eSAaron Piotrowski							"Call to private %s::__destruct() from context '%s'",
956499162fSDmitry Stogov							ZSTR_VAL(object->ce->name),
966499162fSDmitry Stogov							scope ? ZSTR_VAL(scope->name) : "");
976499162fSDmitry Stogov						return;
9822c38b2eSAaron Piotrowski					}
996499162fSDmitry Stogov				} else {
1006499162fSDmitry Stogov					zend_error(E_WARNING,
1016499162fSDmitry Stogov						"Call to private %s::__destruct() from context '' during shutdown ignored",
1026499162fSDmitry Stogov						ZSTR_VAL(object->ce->name));
103669016c7SMarcus Boerger					return;
104669016c7SMarcus Boerger				}
105669016c7SMarcus Boerger			} else {
106669016c7SMarcus Boerger				/* Ensure that if we're calling a protected function, we're allowed to do so.
107669016c7SMarcus Boerger				 */
1086499162fSDmitry Stogov				if (EG(current_execute_data)) {
1096499162fSDmitry Stogov					zend_class_entry *scope = zend_get_executed_scope();
110d4aa155dSMarcus Boerger
1116499162fSDmitry Stogov					if (!zend_check_protected(zend_get_function_root_class(destructor), scope)) {
1125df893ceSAaron Piotrowski						zend_throw_error(NULL,
11322c38b2eSAaron Piotrowski							"Call to protected %s::__destruct() from context '%s'",
1146499162fSDmitry Stogov							ZSTR_VAL(object->ce->name),
1156499162fSDmitry Stogov							scope ? ZSTR_VAL(scope->name) : "");
1166499162fSDmitry Stogov						return;
11722c38b2eSAaron Piotrowski					}
1186499162fSDmitry Stogov				} else {
1196499162fSDmitry Stogov					zend_error(E_WARNING,
1206499162fSDmitry Stogov						"Call to protected %s::__destruct() from context '' during shutdown ignored",
1216499162fSDmitry Stogov						ZSTR_VAL(object->ce->name));
122669016c7SMarcus Boerger					return;
123669016c7SMarcus Boerger				}
124669016c7SMarcus Boerger			}
125669016c7SMarcus Boerger		}
126669016c7SMarcus Boerger
12749ea143bSDmitry Stogov		GC_ADDREF(object);
12873b159e0SAndi Gutmans
1293333380bSAndi Gutmans		/* Make sure that destructors are protected from previously thrown exceptions.
1303333380bSAndi Gutmans		 * For example, if an exception was thrown in a function and when the function's
1313333380bSAndi Gutmans		 * local variable destruction results in a destructor being called.
1323333380bSAndi Gutmans		 */
133d352196bSDmitry Stogov		old_exception = NULL;
134d352196bSDmitry Stogov		if (EG(exception)) {
135f4cfaf36SDmitry Stogov			if (EG(exception) == object) {
136ad863c17SDmitry Stogov				zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception");
137d352196bSDmitry Stogov			} else {
138d352196bSDmitry Stogov				old_exception = EG(exception);
139d93cf2a2SDmitry Stogov				EG(exception) = NULL;
140d352196bSDmitry Stogov			}
14132f9d0e1SMarcus Boerger		}
142ae3be78cSXinchen Hui		orig_fake_scope = EG(fake_scope);
143ae3be78cSXinchen Hui		EG(fake_scope) = NULL;
14434e58a64SDmitry Stogov
14534e58a64SDmitry Stogov		ZVAL_UNDEF(&ret);
14634e58a64SDmitry Stogov
14734e58a64SDmitry Stogov		fci.size = sizeof(fci);
14834e58a64SDmitry Stogov		fci.object = object;
14934e58a64SDmitry Stogov		fci.retval = &ret;
15034e58a64SDmitry Stogov		fci.param_count = 0;
15134e58a64SDmitry Stogov		fci.params = NULL;
15234e58a64SDmitry Stogov		fci.no_separation = 1;
15334e58a64SDmitry Stogov		ZVAL_UNDEF(&fci.function_name); /* Unused */
15434e58a64SDmitry Stogov
15534e58a64SDmitry Stogov		fcic.function_handler = destructor;
15634e58a64SDmitry Stogov		fcic.called_scope = object->ce;
15734e58a64SDmitry Stogov		fcic.object = object;
15834e58a64SDmitry Stogov
15934e58a64SDmitry Stogov		zend_call_function(&fci, &fcic);
16034e58a64SDmitry Stogov		zval_ptr_dtor(&ret);
16134e58a64SDmitry Stogov
162d352196bSDmitry Stogov		if (old_exception) {
163d352196bSDmitry Stogov			if (EG(exception)) {
164bdeb220fSAnatol Belski				zend_exception_set_previous(EG(exception), old_exception);
165d352196bSDmitry Stogov			} else {
166d352196bSDmitry Stogov				EG(exception) = old_exception;
167d352196bSDmitry Stogov			}
168d352196bSDmitry Stogov		}
16934e58a64SDmitry Stogov		OBJ_RELEASE(object);
170e9c3f9fcSDmitry Stogov		EG(fake_scope) = orig_fake_scope;
17173b159e0SAndi Gutmans	}
172f5f7d569SZeev Suraski}
173f5f7d569SZeev Suraski
174175e4613SDmitry StogovZEND_API zend_object* ZEND_FASTCALL zend_objects_new(zend_class_entry *ce)
175d03900dcSStanislav Malyshev{
1769e70d767SDmitry Stogov	zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce));
1775af7770aSAndi Gutmans
178bdeb220fSAnatol Belski	zend_object_std_init(object, ce);
179f4cfaf36SDmitry Stogov	object->handlers = &std_object_handlers;
180f4cfaf36SDmitry Stogov	return object;
1815af7770aSAndi Gutmans}
182ea48c0c4SAndi Gutmans
183175e4613SDmitry StogovZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
184ea48c0c4SAndi Gutmans{
185887189caSDmitry Stogov	if (old_object->ce->default_properties_count) {
186388c2cbdSDmitry Stogov		zval *src = old_object->properties_table;
187388c2cbdSDmitry Stogov		zval *dst = new_object->properties_table;
188388c2cbdSDmitry Stogov		zval *end = src + old_object->ce->default_properties_count;
189388c2cbdSDmitry Stogov
190388c2cbdSDmitry Stogov		do {
191388c2cbdSDmitry Stogov			i_zval_ptr_dtor(dst ZEND_FILE_LINE_CC);
192388c2cbdSDmitry Stogov			ZVAL_COPY_VALUE(dst, src);
193388c2cbdSDmitry Stogov			zval_add_ref(dst);
194388c2cbdSDmitry Stogov			src++;
195388c2cbdSDmitry Stogov			dst++;
196388c2cbdSDmitry Stogov		} while (src != end);
1974a6e1345SDmitry Stogov	} else if (old_object->properties && !old_object->ce->clone) {
1984a6e1345SDmitry Stogov		/* fast copy */
1994a6e1345SDmitry Stogov		if (EXPECTED(old_object->handlers == &std_object_handlers)) {
2004a6e1345SDmitry Stogov			if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) {
20149ea143bSDmitry Stogov				GC_ADDREF(old_object->properties);
2024a6e1345SDmitry Stogov			}
2034a6e1345SDmitry Stogov			new_object->properties = old_object->properties;
2044a6e1345SDmitry Stogov			return;
2054a6e1345SDmitry Stogov		}
206887189caSDmitry Stogov	}
2074a6e1345SDmitry Stogov
208eebab828SDmitry Stogov	if (old_object->properties &&
209eebab828SDmitry Stogov	    EXPECTED(zend_hash_num_elements(old_object->properties))) {
210887189caSDmitry Stogov		zval *prop, new_prop;
211c3e3c98eSAnatol Belski		zend_ulong num_key;
212887189caSDmitry Stogov		zend_string *key;
213887189caSDmitry Stogov
214c5237d82SDmitry Stogov		if (!new_object->properties) {
21544e0b79aSDmitry Stogov			new_object->properties = zend_new_array(zend_hash_num_elements(old_object->properties));
216a795bd82SDmitry Stogov			zend_hash_real_init_mixed(new_object->properties);
217eebab828SDmitry Stogov		} else {
218eebab828SDmitry Stogov			zend_hash_extend(new_object->properties, new_object->properties->nNumUsed + zend_hash_num_elements(old_object->properties), 0);
219c5237d82SDmitry Stogov		}
220887189caSDmitry Stogov
2216634d5e3SDmitry Stogov		HT_FLAGS(new_object->properties) |=
2226634d5e3SDmitry Stogov			HT_FLAGS(old_object->properties) & HASH_FLAG_HAS_EMPTY_IND;
22325f9e255SDmitry Stogov
2247652a977SDmitry Stogov		ZEND_HASH_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) {
225887189caSDmitry Stogov			if (Z_TYPE_P(prop) == IS_INDIRECT) {
226887189caSDmitry Stogov				ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table));
227887189caSDmitry Stogov			} else {
228887189caSDmitry Stogov				ZVAL_COPY_VALUE(&new_prop, prop);
2295dc52e48SDmitry Stogov				zval_add_ref(&new_prop);
230887189caSDmitry Stogov			}
231eebab828SDmitry Stogov			if (EXPECTED(key)) {
232eebab828SDmitry Stogov				_zend_hash_append(new_object->properties, key, &new_prop);
2337652a977SDmitry Stogov			} else {
234dd01ade9SDmitry Stogov				zend_hash_index_add_new(new_object->properties, num_key, &new_prop);
235c5237d82SDmitry Stogov			}
2367652a977SDmitry Stogov		} ZEND_HASH_FOREACH_END();
237c5237d82SDmitry Stogov	}
2382b10c53aSFelipe Pena
23929ea3da2SAndi Gutmans	if (old_object->ce->clone) {
24034e58a64SDmitry Stogov		zend_fcall_info fci;
24134e58a64SDmitry Stogov		zend_fcall_info_cache fcic;
24234e58a64SDmitry Stogov		zval ret;
24334e58a64SDmitry Stogov
24434e58a64SDmitry Stogov		GC_ADDREF(new_object);
24534e58a64SDmitry Stogov
24634e58a64SDmitry Stogov		ZVAL_UNDEF(&ret);
24734e58a64SDmitry Stogov
24834e58a64SDmitry Stogov		fci.size = sizeof(fci);
24934e58a64SDmitry Stogov		fci.object = new_object;
25034e58a64SDmitry Stogov		fci.retval = &ret;
25134e58a64SDmitry Stogov		fci.param_count = 0;
25234e58a64SDmitry Stogov		fci.params = NULL;
25334e58a64SDmitry Stogov		fci.no_separation = 1;
25434e58a64SDmitry Stogov		ZVAL_UNDEF(&fci.function_name); /* Unused */
25534e58a64SDmitry Stogov
25634e58a64SDmitry Stogov		fcic.function_handler = new_object->ce->clone;
25734e58a64SDmitry Stogov		fcic.called_scope = new_object->ce;
25834e58a64SDmitry Stogov		fcic.object = new_object;
25929ea3da2SAndi Gutmans
26034e58a64SDmitry Stogov		zend_call_function(&fci, &fcic);
26134e58a64SDmitry Stogov		zval_ptr_dtor(&ret);
26234e58a64SDmitry Stogov		OBJ_RELEASE(new_object);
26329ea3da2SAndi Gutmans	}
2647cdc2d1fSMarcus Boerger}
265ea48c0c4SAndi Gutmans
266bdeb220fSAnatol BelskiZEND_API zend_object *zend_objects_clone_obj(zval *zobject)
2677cdc2d1fSMarcus Boerger{
2687cdc2d1fSMarcus Boerger	zend_object *old_object;
2697cdc2d1fSMarcus Boerger	zend_object *new_object;
2707cdc2d1fSMarcus Boerger
271d03900dcSStanislav Malyshev	/* assume that create isn't overwritten, so when clone depends on the
2727cdc2d1fSMarcus Boerger	 * overwritten one then it must itself be overwritten */
273f4cfaf36SDmitry Stogov	old_object = Z_OBJ_P(zobject);
274bdeb220fSAnatol Belski	new_object = zend_objects_new(old_object->ce);
2757cdc2d1fSMarcus Boerger
2763abef9c9SNikita Popov	/* zend_objects_clone_members() expect the properties to be initialized. */
2773abef9c9SNikita Popov	if (new_object->ce->default_properties_count) {
2783abef9c9SNikita Popov		zval *p = new_object->properties_table;
2793abef9c9SNikita Popov		zval *end = p + new_object->ce->default_properties_count;
2803abef9c9SNikita Popov		do {
2813abef9c9SNikita Popov			ZVAL_UNDEF(p);
2823abef9c9SNikita Popov			p++;
2833abef9c9SNikita Popov		} while (p != end);
2843abef9c9SNikita Popov	}
2853abef9c9SNikita Popov
286bdeb220fSAnatol Belski	zend_objects_clone_members(new_object, old_object);
2877cdc2d1fSMarcus Boerger
288f4cfaf36SDmitry Stogov	return new_object;
289ea48c0c4SAndi Gutmans}
290333406bdSfoobar
291333406bdSfoobar/*
292333406bdSfoobar * Local variables:
293333406bdSfoobar * tab-width: 4
294333406bdSfoobar * c-basic-offset: 4
295333406bdSfoobar * indent-tabs-mode: t
296333406bdSfoobar * End:
297bc5811f3SAnatol Belski * vim600: sw=4 ts=4 fdm=marker
298bc5811f3SAnatol Belski * vim<600: sw=4 ts=4
299333406bdSfoobar */
300