2006-05-21 05:02:44 +08:00
|
|
|
/*
|
2003-02-01 09:49:15 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 17:23:29 +08:00
|
|
|
| Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
|
2003-02-01 09:49:15 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2003-02-01 09:49:15 +08:00
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
|
|
|
| If you did not receive a copy of the Zend license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@zend.com so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
2018-11-01 23:20:07 +08:00
|
|
|
| Authors: Andi Gutmans <andi@php.net> |
|
|
|
|
| Zeev Suraski <zeev@php.net> |
|
|
|
|
| Dmitry Stogov <dmitry@php.net> |
|
2003-02-01 09:49:15 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2001-08-07 11:17:33 +08:00
|
|
|
#include "zend.h"
|
|
|
|
#include "zend_globals.h"
|
2001-08-12 02:04:07 +08:00
|
|
|
#include "zend_variables.h"
|
2001-12-27 03:54:20 +08:00
|
|
|
#include "zend_API.h"
|
2003-12-28 04:33:14 +08:00
|
|
|
#include "zend_interfaces.h"
|
2006-05-21 05:02:44 +08:00
|
|
|
#include "zend_exceptions.h"
|
2018-05-10 20:58:10 +08:00
|
|
|
#include "zend_weakrefs.h"
|
2001-08-07 11:17:33 +08:00
|
|
|
|
2018-10-23 17:46:44 +08:00
|
|
|
static zend_always_inline void _zend_object_std_init(zend_object *object, zend_class_entry *ce)
|
2006-03-29 22:28:43 +08:00
|
|
|
{
|
2017-10-27 06:28:58 +08:00
|
|
|
GC_SET_REFCOUNT(object, 1);
|
2016-10-21 22:47:30 +08:00
|
|
|
GC_TYPE_INFO(object) = IS_OBJECT | (GC_COLLECTABLE << GC_FLAGS_SHIFT);
|
2012-05-13 13:12:48 +08:00
|
|
|
object->ce = ce;
|
2010-05-24 22:11:39 +08:00
|
|
|
object->properties = NULL;
|
2014-12-14 06:06:14 +08:00
|
|
|
zend_objects_store_put(object);
|
2015-04-27 23:30:33 +08:00
|
|
|
if (UNEXPECTED(ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
|
2017-05-03 04:16:41 +08:00
|
|
|
ZVAL_UNDEF(object->properties_table + object->ce->default_properties_count);
|
2015-02-04 20:24:13 +08:00
|
|
|
}
|
2006-03-29 22:28:43 +08:00
|
|
|
}
|
|
|
|
|
2018-10-23 17:46:44 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
|
|
|
|
{
|
|
|
|
_zend_object_std_init(object, ce);
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API void zend_object_std_dtor(zend_object *object)
|
2006-03-29 22:28:43 +08:00
|
|
|
{
|
2015-04-27 23:30:33 +08:00
|
|
|
zval *p, *end;
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2006-03-29 22:28:43 +08:00
|
|
|
if (object->properties) {
|
2015-06-17 17:50:16 +08:00
|
|
|
if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) {
|
2017-10-27 06:28:58 +08:00
|
|
|
if (EXPECTED(GC_DELREF(object->properties) == 0)) {
|
2015-06-17 17:50:16 +08:00
|
|
|
zend_array_destroy(object->properties);
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2015-04-27 23:30:33 +08:00
|
|
|
p = object->properties_table;
|
|
|
|
if (EXPECTED(object->ce->default_properties_count)) {
|
|
|
|
end = p + object->ce->default_properties_count;
|
|
|
|
do {
|
2019-01-07 19:28:51 +08:00
|
|
|
if (Z_REFCOUNTED_P(p)) {
|
|
|
|
if (UNEXPECTED(Z_ISREF_P(p)) &&
|
|
|
|
(ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(p)))) {
|
|
|
|
zend_property_info *prop_info = zend_get_property_info_for_slot(object, p);
|
|
|
|
if (prop_info->type) {
|
|
|
|
ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i_zval_ptr_dtor(p);
|
|
|
|
}
|
2015-04-27 23:30:33 +08:00
|
|
|
p++;
|
|
|
|
} while (p != end);
|
2006-03-29 22:28:43 +08:00
|
|
|
}
|
2018-05-10 20:58:10 +08:00
|
|
|
|
2018-02-27 22:38:56 +08:00
|
|
|
if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
|
2016-04-27 05:24:20 +08:00
|
|
|
if (EXPECTED(Z_TYPE_P(p) == IS_STRING)) {
|
2018-07-04 17:08:07 +08:00
|
|
|
zval_ptr_dtor_str(p);
|
2018-02-27 22:38:56 +08:00
|
|
|
} else if (Z_TYPE_P(p) == IS_ARRAY) {
|
2016-04-27 05:24:20 +08:00
|
|
|
HashTable *guards;
|
2015-02-04 20:24:13 +08:00
|
|
|
|
2016-04-27 05:24:20 +08:00
|
|
|
guards = Z_ARRVAL_P(p);
|
|
|
|
ZEND_ASSERT(guards != NULL);
|
|
|
|
zend_hash_destroy(guards);
|
|
|
|
FREE_HASHTABLE(guards);
|
|
|
|
}
|
2015-02-04 20:24:13 +08:00
|
|
|
}
|
2018-05-10 20:58:10 +08:00
|
|
|
|
|
|
|
if (UNEXPECTED(GC_FLAGS(object) & IS_OBJ_WEAKLY_REFERENCED)) {
|
|
|
|
zend_weakrefs_notify(object);
|
|
|
|
}
|
2006-03-29 22:28:43 +08:00
|
|
|
}
|
2003-07-03 07:53:53 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API void zend_objects_destroy_object(zend_object *object)
|
2001-12-27 22:35:09 +08:00
|
|
|
{
|
2017-06-27 17:03:06 +08:00
|
|
|
zend_function *destructor = object->ce->destructor;
|
2003-07-02 03:13:50 +08:00
|
|
|
|
|
|
|
if (destructor) {
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_object *old_exception;
|
2016-06-12 15:17:03 +08:00
|
|
|
zend_class_entry *orig_fake_scope;
|
2018-06-22 19:29:54 +08:00
|
|
|
zend_fcall_info fci;
|
|
|
|
zend_fcall_info_cache fcic;
|
|
|
|
zval ret;
|
2006-05-21 05:02:44 +08:00
|
|
|
|
2003-07-03 07:53:53 +08:00
|
|
|
if (destructor->op_array.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
|
|
|
|
if (destructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
|
|
|
|
/* Ensure that if we're calling a private function, we're allowed to do so.
|
|
|
|
*/
|
2016-04-28 09:13:34 +08:00
|
|
|
if (EG(current_execute_data)) {
|
|
|
|
zend_class_entry *scope = zend_get_executed_scope();
|
2003-08-09 22:32:33 +08:00
|
|
|
|
2016-04-28 09:13:34 +08:00
|
|
|
if (object->ce != scope) {
|
2015-07-08 01:10:22 +08:00
|
|
|
zend_throw_error(NULL,
|
2015-07-04 05:04:33 +08:00
|
|
|
"Call to private %s::__destruct() from context '%s'",
|
2016-04-28 09:13:34 +08:00
|
|
|
ZSTR_VAL(object->ce->name),
|
|
|
|
scope ? ZSTR_VAL(scope->name) : "");
|
|
|
|
return;
|
2015-07-04 05:04:33 +08:00
|
|
|
}
|
2016-04-28 09:13:34 +08:00
|
|
|
} else {
|
|
|
|
zend_error(E_WARNING,
|
|
|
|
"Call to private %s::__destruct() from context '' during shutdown ignored",
|
|
|
|
ZSTR_VAL(object->ce->name));
|
2003-07-03 07:53:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Ensure that if we're calling a protected function, we're allowed to do so.
|
|
|
|
*/
|
2016-04-28 09:13:34 +08:00
|
|
|
if (EG(current_execute_data)) {
|
|
|
|
zend_class_entry *scope = zend_get_executed_scope();
|
2003-08-09 22:32:33 +08:00
|
|
|
|
2016-04-28 09:13:34 +08:00
|
|
|
if (!zend_check_protected(zend_get_function_root_class(destructor), scope)) {
|
2015-07-08 01:10:22 +08:00
|
|
|
zend_throw_error(NULL,
|
2015-07-04 05:04:33 +08:00
|
|
|
"Call to protected %s::__destruct() from context '%s'",
|
2016-04-28 09:13:34 +08:00
|
|
|
ZSTR_VAL(object->ce->name),
|
|
|
|
scope ? ZSTR_VAL(scope->name) : "");
|
|
|
|
return;
|
2015-07-04 05:04:33 +08:00
|
|
|
}
|
2016-04-28 09:13:34 +08:00
|
|
|
} else {
|
|
|
|
zend_error(E_WARNING,
|
|
|
|
"Call to protected %s::__destruct() from context '' during shutdown ignored",
|
|
|
|
ZSTR_VAL(object->ce->name));
|
2003-07-03 07:53:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 06:28:58 +08:00
|
|
|
GC_ADDREF(object);
|
2001-12-27 22:35:09 +08:00
|
|
|
|
2004-03-02 16:13:15 +08:00
|
|
|
/* Make sure that destructors are protected from previously thrown exceptions.
|
|
|
|
* For example, if an exception was thrown in a function and when the function's
|
|
|
|
* local variable destruction results in a destructor being called.
|
|
|
|
*/
|
2010-05-12 00:09:43 +08:00
|
|
|
old_exception = NULL;
|
|
|
|
if (EG(exception)) {
|
2014-02-10 14:04:30 +08:00
|
|
|
if (EG(exception) == object) {
|
2015-04-02 19:19:52 +08:00
|
|
|
zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception");
|
2010-05-12 00:09:43 +08:00
|
|
|
} else {
|
|
|
|
old_exception = EG(exception);
|
2010-08-16 17:20:46 +08:00
|
|
|
EG(exception) = NULL;
|
2010-05-12 00:09:43 +08:00
|
|
|
}
|
2008-08-14 18:24:52 +08:00
|
|
|
}
|
2016-06-12 15:17:03 +08:00
|
|
|
orig_fake_scope = EG(fake_scope);
|
|
|
|
EG(fake_scope) = NULL;
|
2018-06-22 19:29:54 +08:00
|
|
|
|
|
|
|
ZVAL_UNDEF(&ret);
|
|
|
|
|
|
|
|
fci.size = sizeof(fci);
|
|
|
|
fci.object = object;
|
|
|
|
fci.retval = &ret;
|
|
|
|
fci.param_count = 0;
|
|
|
|
fci.params = NULL;
|
|
|
|
fci.no_separation = 1;
|
|
|
|
ZVAL_UNDEF(&fci.function_name); /* Unused */
|
|
|
|
|
|
|
|
fcic.function_handler = destructor;
|
|
|
|
fcic.called_scope = object->ce;
|
|
|
|
fcic.object = object;
|
|
|
|
|
|
|
|
zend_call_function(&fci, &fcic);
|
|
|
|
zval_ptr_dtor(&ret);
|
|
|
|
|
2010-05-12 00:09:43 +08:00
|
|
|
if (old_exception) {
|
|
|
|
if (EG(exception)) {
|
2014-12-14 06:06:14 +08:00
|
|
|
zend_exception_set_previous(EG(exception), old_exception);
|
2010-05-12 00:09:43 +08:00
|
|
|
} else {
|
|
|
|
EG(exception) = old_exception;
|
|
|
|
}
|
|
|
|
}
|
2018-06-22 19:29:54 +08:00
|
|
|
OBJ_RELEASE(object);
|
2016-05-13 16:55:09 +08:00
|
|
|
EG(fake_scope) = orig_fake_scope;
|
2001-12-27 22:35:09 +08:00
|
|
|
}
|
2004-02-04 17:56:20 +08:00
|
|
|
}
|
|
|
|
|
2017-12-14 18:50:39 +08:00
|
|
|
ZEND_API zend_object* ZEND_FASTCALL zend_objects_new(zend_class_entry *ce)
|
2012-05-13 13:12:48 +08:00
|
|
|
{
|
2015-02-04 20:24:13 +08:00
|
|
|
zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce));
|
2001-08-07 11:17:33 +08:00
|
|
|
|
2018-10-23 17:46:44 +08:00
|
|
|
_zend_object_std_init(object, ce);
|
2014-02-10 14:04:30 +08:00
|
|
|
object->handlers = &std_object_handlers;
|
|
|
|
return object;
|
2001-08-07 11:17:33 +08:00
|
|
|
}
|
2001-08-16 22:04:04 +08:00
|
|
|
|
2017-12-14 18:50:39 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
|
2001-08-16 22:04:04 +08:00
|
|
|
{
|
2014-03-26 22:07:31 +08:00
|
|
|
if (old_object->ce->default_properties_count) {
|
2015-04-29 00:11:45 +08:00
|
|
|
zval *src = old_object->properties_table;
|
|
|
|
zval *dst = new_object->properties_table;
|
|
|
|
zval *end = src + old_object->ce->default_properties_count;
|
|
|
|
|
|
|
|
do {
|
2018-09-16 23:12:44 +08:00
|
|
|
i_zval_ptr_dtor(dst);
|
2015-04-29 00:11:45 +08:00
|
|
|
ZVAL_COPY_VALUE(dst, src);
|
|
|
|
zval_add_ref(dst);
|
2019-01-07 19:28:51 +08:00
|
|
|
if (UNEXPECTED(Z_ISREF_P(dst)) &&
|
|
|
|
(ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(dst)))) {
|
|
|
|
zend_property_info *prop_info = zend_get_property_info_for_slot(new_object, dst);
|
|
|
|
if (prop_info->type) {
|
|
|
|
ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(dst), prop_info);
|
|
|
|
}
|
|
|
|
}
|
2015-04-29 00:11:45 +08:00
|
|
|
src++;
|
|
|
|
dst++;
|
|
|
|
} while (src != end);
|
2015-06-17 17:50:16 +08:00
|
|
|
} else if (old_object->properties && !old_object->ce->clone) {
|
|
|
|
/* fast copy */
|
|
|
|
if (EXPECTED(old_object->handlers == &std_object_handlers)) {
|
|
|
|
if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) {
|
2017-10-27 06:28:58 +08:00
|
|
|
GC_ADDREF(old_object->properties);
|
2015-06-17 17:50:16 +08:00
|
|
|
}
|
|
|
|
new_object->properties = old_object->properties;
|
|
|
|
return;
|
|
|
|
}
|
2014-03-26 22:07:31 +08:00
|
|
|
}
|
2015-06-17 17:50:16 +08:00
|
|
|
|
2015-05-19 18:23:13 +08:00
|
|
|
if (old_object->properties &&
|
|
|
|
EXPECTED(zend_hash_num_elements(old_object->properties))) {
|
2014-03-26 22:07:31 +08:00
|
|
|
zval *prop, new_prop;
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_ulong num_key;
|
2014-03-26 22:07:31 +08:00
|
|
|
zend_string *key;
|
|
|
|
|
2010-05-24 22:11:39 +08:00
|
|
|
if (!new_object->properties) {
|
2017-09-20 07:25:56 +08:00
|
|
|
new_object->properties = zend_new_array(zend_hash_num_elements(old_object->properties));
|
2018-03-23 05:13:45 +08:00
|
|
|
zend_hash_real_init_mixed(new_object->properties);
|
2015-05-19 18:23:13 +08:00
|
|
|
} else {
|
|
|
|
zend_hash_extend(new_object->properties, new_object->properties->nNumUsed + zend_hash_num_elements(old_object->properties), 0);
|
2010-05-24 22:11:39 +08:00
|
|
|
}
|
2014-03-26 22:07:31 +08:00
|
|
|
|
2018-01-22 19:58:16 +08:00
|
|
|
HT_FLAGS(new_object->properties) |=
|
|
|
|
HT_FLAGS(old_object->properties) & HASH_FLAG_HAS_EMPTY_IND;
|
2015-08-26 08:27:05 +08:00
|
|
|
|
2014-04-18 23:18:11 +08:00
|
|
|
ZEND_HASH_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) {
|
2014-03-26 22:07:31 +08:00
|
|
|
if (Z_TYPE_P(prop) == IS_INDIRECT) {
|
|
|
|
ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table));
|
|
|
|
} else {
|
|
|
|
ZVAL_COPY_VALUE(&new_prop, prop);
|
2014-04-14 17:24:43 +08:00
|
|
|
zval_add_ref(&new_prop);
|
2014-03-26 22:07:31 +08:00
|
|
|
}
|
2015-05-19 18:23:13 +08:00
|
|
|
if (EXPECTED(key)) {
|
|
|
|
_zend_hash_append(new_object->properties, key, &new_prop);
|
2014-04-18 23:18:11 +08:00
|
|
|
} else {
|
2014-12-15 19:43:16 +08:00
|
|
|
zend_hash_index_add_new(new_object->properties, num_key, &new_prop);
|
2010-05-24 22:11:39 +08:00
|
|
|
}
|
2014-04-18 23:18:11 +08:00
|
|
|
} ZEND_HASH_FOREACH_END();
|
2010-05-24 22:11:39 +08:00
|
|
|
}
|
2008-03-18 22:10:45 +08:00
|
|
|
|
2001-12-27 03:54:20 +08:00
|
|
|
if (old_object->ce->clone) {
|
2018-06-22 19:29:54 +08:00
|
|
|
zend_fcall_info fci;
|
|
|
|
zend_fcall_info_cache fcic;
|
|
|
|
zval ret;
|
|
|
|
|
|
|
|
GC_ADDREF(new_object);
|
|
|
|
|
|
|
|
ZVAL_UNDEF(&ret);
|
|
|
|
|
|
|
|
fci.size = sizeof(fci);
|
|
|
|
fci.object = new_object;
|
|
|
|
fci.retval = &ret;
|
|
|
|
fci.param_count = 0;
|
|
|
|
fci.params = NULL;
|
|
|
|
fci.no_separation = 1;
|
|
|
|
ZVAL_UNDEF(&fci.function_name); /* Unused */
|
|
|
|
|
|
|
|
fcic.function_handler = new_object->ce->clone;
|
|
|
|
fcic.called_scope = new_object->ce;
|
|
|
|
fcic.object = new_object;
|
2001-12-27 03:54:20 +08:00
|
|
|
|
2018-06-22 19:29:54 +08:00
|
|
|
zend_call_function(&fci, &fcic);
|
|
|
|
zval_ptr_dtor(&ret);
|
|
|
|
OBJ_RELEASE(new_object);
|
2001-12-27 03:54:20 +08:00
|
|
|
}
|
2003-07-19 17:47:00 +08:00
|
|
|
}
|
2001-08-16 22:04:04 +08:00
|
|
|
|
2019-01-31 23:47:58 +08:00
|
|
|
ZEND_API zend_object *zend_objects_clone_obj(zend_object *old_object)
|
2003-07-19 17:47:00 +08:00
|
|
|
{
|
|
|
|
zend_object *new_object;
|
|
|
|
|
2012-05-13 13:12:48 +08:00
|
|
|
/* assume that create isn't overwritten, so when clone depends on the
|
2003-07-19 17:47:00 +08:00
|
|
|
* overwritten one then it must itself be overwritten */
|
2014-12-14 06:06:14 +08:00
|
|
|
new_object = zend_objects_new(old_object->ce);
|
2003-07-19 17:47:00 +08:00
|
|
|
|
2017-05-03 04:16:41 +08:00
|
|
|
/* zend_objects_clone_members() expect the properties to be initialized. */
|
|
|
|
if (new_object->ce->default_properties_count) {
|
|
|
|
zval *p = new_object->properties_table;
|
|
|
|
zval *end = p + new_object->ce->default_properties_count;
|
|
|
|
do {
|
|
|
|
ZVAL_UNDEF(p);
|
|
|
|
p++;
|
|
|
|
} while (p != end);
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
zend_objects_clone_members(new_object, old_object);
|
2003-07-19 17:47:00 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
return new_object;
|
2001-08-16 22:04:04 +08:00
|
|
|
}
|