Coverage Report

Created: 2022-02-19 20:27

/src/php-src/Zend/zend_objects.c
Line
Count
Source (jump to first uncovered line)
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
29
static zend_always_inline void _zend_object_std_init(zend_object *object, zend_class_entry *ce)
30
1.65M
{
31
1.65M
  GC_SET_REFCOUNT(object, 1);
32
1.65M
  GC_TYPE_INFO(object) = GC_OBJECT;
33
1.65M
  object->ce = ce;
34
1.65M
  object->properties = NULL;
35
1.65M
  zend_objects_store_put(object);
36
1.65M
  if (UNEXPECTED(ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
37
13.8k
    ZVAL_UNDEF(object->properties_table + object->ce->default_properties_count);
38
13.8k
  }
39
1.65M
}
40
41
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
42
529k
{
43
529k
  _zend_object_std_init(object, ce);
44
529k
}
45
46
ZEND_API void zend_object_std_dtor(zend_object *object)
47
1.63M
{
48
1.63M
  zval *p, *end;
49
50
1.63M
  if (object->properties) {
51
411k
    if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) {
52
411k
      if (EXPECTED(GC_DELREF(object->properties) == 0)
53
409k
          && EXPECTED(GC_TYPE(object->properties) != IS_NULL)) {
54
409k
        zend_array_destroy(object->properties);
55
409k
      }
56
411k
    }
57
411k
  }
58
1.63M
  p = object->properties_table;
59
1.63M
  if (EXPECTED(object->ce->default_properties_count)) {
60
953k
    end = p + object->ce->default_properties_count;
61
5.90M
    do {
62
5.90M
      if (Z_REFCOUNTED_P(p)) {
63
2.28M
        if (UNEXPECTED(Z_ISREF_P(p)) &&
64
23.2k
            (ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(p)))) {
65
23.2k
          zend_property_info *prop_info = zend_get_property_info_for_slot(object, p);
66
23.2k
          if (ZEND_TYPE_IS_SET(prop_info->type)) {
67
20.4k
            ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info);
68
20.4k
          }
69
23.2k
        }
70
2.28M
        i_zval_ptr_dtor(p);
71
2.28M
      }
72
5.90M
      p++;
73
5.90M
    } while (p != end);
74
953k
  }
75
76
1.63M
  if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
77
13.8k
    if (EXPECTED(Z_TYPE_P(p) == IS_STRING)) {
78
9.39k
      zval_ptr_dtor_str(p);
79
4.49k
    } else if (Z_TYPE_P(p) == IS_ARRAY) {
80
3.25k
      HashTable *guards;
81
82
3.25k
      guards = Z_ARRVAL_P(p);
83
3.25k
      ZEND_ASSERT(guards != NULL);
84
3.25k
      zend_hash_destroy(guards);
85
3.25k
      FREE_HASHTABLE(guards);
86
3.25k
    }
87
13.8k
  }
88
89
1.63M
  if (UNEXPECTED(GC_FLAGS(object) & IS_OBJ_WEAKLY_REFERENCED)) {
90
2.45k
    zend_weakrefs_notify(object);
91
2.45k
  }
92
1.63M
}
93
94
ZEND_API void zend_objects_destroy_object(zend_object *object)
95
66.2k
{
96
66.2k
  zend_function *destructor = object->ce->destructor;
97
98
66.2k
  if (destructor) {
99
64.9k
    zend_object *old_exception;
100
101
64.9k
    if (destructor->op_array.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
102
0
      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
0
        if (EG(current_execute_data)) {
106
0
          zend_class_entry *scope = zend_get_executed_scope();
107
108
0
          if (object->ce != scope) {
109
0
            zend_throw_error(NULL,
110
0
              "Call to private %s::__destruct() from %s%s",
111
0
              ZSTR_VAL(object->ce->name),
112
0
              scope ? "scope " : "global scope",
113
0
              scope ? ZSTR_VAL(scope->name) : ""
114
0
            );
115
0
            return;
116
0
          }
117
0
        } else {
118
0
          zend_error(E_WARNING,
119
0
            "Call to private %s::__destruct() from global scope during shutdown ignored",
120
0
            ZSTR_VAL(object->ce->name));
121
0
          return;
122
0
        }
123
0
      } else {
124
        /* Ensure that if we're calling a protected function, we're allowed to do so.
125
         */
126
0
        if (EG(current_execute_data)) {
127
0
          zend_class_entry *scope = zend_get_executed_scope();
128
129
0
          if (!zend_check_protected(zend_get_function_root_class(destructor), scope)) {
130
0
            zend_throw_error(NULL,
131
0
              "Call to protected %s::__destruct() from %s%s",
132
0
              ZSTR_VAL(object->ce->name),
133
0
              scope ? "scope " : "global scope",
134
0
              scope ? ZSTR_VAL(scope->name) : ""
135
0
            );
136
0
            return;
137
0
          }
138
0
        } else {
139
0
          zend_error(E_WARNING,
140
0
            "Call to protected %s::__destruct() from global scope during shutdown ignored",
141
0
            ZSTR_VAL(object->ce->name));
142
0
          return;
143
0
        }
144
64.9k
      }
145
0
    }
146
147
64.9k
    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
64.9k
    old_exception = NULL;
154
64.9k
    if (EG(exception)) {
155
4.65k
      if (EG(exception) == object) {
156
6
        zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception");
157
4.64k
      } else {
158
4.64k
        old_exception = EG(exception);
159
4.64k
        EG(exception) = NULL;
160
4.64k
      }
161
4.65k
    }
162
163
64.9k
    zend_call_known_instance_method_with_0_params(destructor, object, NULL);
164
165
64.9k
    if (old_exception) {
166
3.15k
      if (EG(exception)) {
167
2.59k
        zend_exception_set_previous(EG(exception), old_exception);
168
561
      } else {
169
561
        EG(exception) = old_exception;
170
561
      }
171
3.15k
    }
172
64.9k
    OBJ_RELEASE(object);
173
64.9k
  }
174
66.2k
}
175
176
ZEND_API zend_object* ZEND_FASTCALL zend_objects_new(zend_class_entry *ce)
177
1.12M
{
178
1.12M
  zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce));
179
180
1.12M
  _zend_object_std_init(object, ce);
181
1.12M
  object->handlers = &std_object_handlers;
182
1.12M
  return object;
183
1.12M
}
184
185
ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
186
3.92k
{
187
3.92k
  if (old_object->ce->default_properties_count) {
188
372
    zval *src = old_object->properties_table;
189
372
    zval *dst = new_object->properties_table;
190
372
    zval *end = src + old_object->ce->default_properties_count;
191
192
372
    do {
193
372
      i_zval_ptr_dtor(dst);
194
372
      ZVAL_COPY_VALUE_PROP(dst, src);
195
372
      zval_add_ref(dst);
196
372
      if (UNEXPECTED(Z_ISREF_P(dst)) &&
197
51
          (ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(dst)))) {
198
51
        zend_property_info *prop_info = zend_get_property_info_for_slot(new_object, dst);
199
51
        if (ZEND_TYPE_IS_SET(prop_info->type)) {
200
28
          ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(dst), prop_info);
201
28
        }
202
51
      }
203
372
      src++;
204
372
      dst++;
205
372
    } while (src != end);
206
3.55k
  } else if (old_object->properties && !old_object->ce->clone) {
207
    /* fast copy */
208
2.38k
    if (EXPECTED(old_object->handlers == &std_object_handlers)) {
209
2.38k
      if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) {
210
2.38k
        GC_ADDREF(old_object->properties);
211
2.38k
      }
212
2.38k
      new_object->properties = old_object->properties;
213
2.38k
      return;
214
2.38k
    }
215
1.54k
  }
216
217
1.54k
  if (old_object->properties &&
218
122
      EXPECTED(zend_hash_num_elements(old_object->properties))) {
219
122
    zval *prop, new_prop;
220
122
    zend_ulong num_key;
221
122
    zend_string *key;
222
223
122
    if (!new_object->properties) {
224
122
      new_object->properties = zend_new_array(zend_hash_num_elements(old_object->properties));
225
122
      zend_hash_real_init_mixed(new_object->properties);
226
0
    } else {
227
0
      zend_hash_extend(new_object->properties, new_object->properties->nNumUsed + zend_hash_num_elements(old_object->properties), 0);
228
0
    }
229
230
122
    HT_FLAGS(new_object->properties) |=
231
122
      HT_FLAGS(old_object->properties) & HASH_FLAG_HAS_EMPTY_IND;
232
233
662
    ZEND_HASH_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) {
234
270
      if (Z_TYPE_P(prop) == IS_INDIRECT) {
235
122
        ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table));
236
148
      } else {
237
148
        ZVAL_COPY_VALUE(&new_prop, prop);
238
148
        zval_add_ref(&new_prop);
239
148
      }
240
270
      if (EXPECTED(key)) {
241
270
        _zend_hash_append(new_object->properties, key, &new_prop);
242
0
      } else {
243
0
        zend_hash_index_add_new(new_object->properties, num_key, &new_prop);
244
0
      }
245
270
    } ZEND_HASH_FOREACH_END();
246
122
  }
247
248
1.54k
  if (old_object->ce->clone) {
249
273
    GC_ADDREF(new_object);
250
273
    zend_call_known_instance_method_with_0_params(new_object->ce->clone, new_object, NULL);
251
273
    OBJ_RELEASE(new_object);
252
273
  }
253
1.54k
}
254
255
ZEND_API zend_object *zend_objects_clone_obj(zend_object *old_object)
256
3.92k
{
257
3.92k
  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
3.92k
  new_object = zend_objects_new(old_object->ce);
262
263
  /* zend_objects_clone_members() expect the properties to be initialized. */
264
3.92k
  if (new_object->ce->default_properties_count) {
265
372
    zval *p = new_object->properties_table;
266
372
    zval *end = p + new_object->ce->default_properties_count;
267
372
    do {
268
372
      ZVAL_UNDEF(p);
269
372
      p++;
270
372
    } while (p != end);
271
372
  }
272
273
3.92k
  zend_objects_clone_members(new_object, old_object);
274
275
3.92k
  return new_object;
276
3.92k
}