Coverage Report

Created: 2026-06-02 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_objects.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright © Zend Technologies Ltd., a subsidiary company of          |
6
   |     Perforce Software, Inc., and Contributors.                       |
7
   +----------------------------------------------------------------------+
8
   | This source file is subject to the Modified BSD License that is      |
9
   | bundled with this package in the file LICENSE, and is available      |
10
   | through the World Wide Web at <https://www.php.net/license/>.        |
11
   |                                                                      |
12
   | SPDX-License-Identifier: BSD-3-Clause                                |
13
   +----------------------------------------------------------------------+
14
   | Authors: Andi Gutmans <andi@php.net>                                 |
15
   |          Zeev Suraski <zeev@php.net>                                 |
16
   |          Dmitry Stogov <dmitry@php.net>                              |
17
   +----------------------------------------------------------------------+
18
*/
19
20
#include "zend.h"
21
#include "zend_globals.h"
22
#include "zend_variables.h"
23
#include "zend_API.h"
24
#include "zend_interfaces.h"
25
#include "zend_exceptions.h"
26
#include "zend_weakrefs.h"
27
#include "zend_lazy_objects.h"
28
29
static zend_always_inline void _zend_object_std_init(zend_object *object, zend_class_entry *ce)
30
3.87M
{
31
3.87M
  GC_SET_REFCOUNT(object, 1);
32
3.87M
  GC_TYPE_INFO(object) = GC_OBJECT;
33
3.87M
  object->ce = ce;
34
3.87M
  object->extra_flags = 0;
35
3.87M
  object->handlers = ce->default_object_handlers;
36
3.87M
  object->properties = NULL;
37
3.87M
  zend_objects_store_put(object);
38
3.87M
  if (UNEXPECTED(ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
39
288k
    zval *guard_value = object->properties_table + object->ce->default_properties_count;
40
288k
    ZVAL_UNDEF(guard_value);
41
288k
    Z_GUARD_P(guard_value) = 0;
42
288k
  }
43
3.87M
}
44
45
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
46
996k
{
47
996k
  _zend_object_std_init(object, ce);
48
996k
}
49
50
void zend_object_dtor_dynamic_properties(zend_object *object)
51
3.87M
{
52
3.87M
  if (object->properties) {
53
731k
    if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) {
54
731k
      if (EXPECTED(GC_DELREF(object->properties) == 0)
55
731k
          && EXPECTED(GC_TYPE(object->properties) != IS_NULL)) {
56
731k
        zend_array_destroy(object->properties);
57
731k
      }
58
731k
    }
59
731k
  }
60
3.87M
}
61
62
void zend_object_dtor_property(zend_object *object, zval *p)
63
6.46M
{
64
6.46M
  if (Z_REFCOUNTED_P(p)) {
65
2.69M
    if (UNEXPECTED(Z_ISREF_P(p)) &&
66
32.2k
        (ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(p)))) {
67
32.2k
      zend_property_info *prop_info = zend_get_property_info_for_slot_self(object, p);
68
32.2k
      if (ZEND_TYPE_IS_SET(prop_info->type)) {
69
31.7k
        ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info);
70
31.7k
      }
71
32.2k
    }
72
2.69M
    i_zval_ptr_dtor(p);
73
2.69M
  }
74
6.46M
}
75
76
ZEND_API void zend_object_std_dtor(zend_object *object)
77
3.87M
{
78
3.87M
  zval *p, *end;
79
80
3.87M
  if (UNEXPECTED(GC_FLAGS(object) & IS_OBJ_WEAKLY_REFERENCED)) {
81
431
    zend_weakrefs_notify(object);
82
431
  }
83
84
3.87M
  if (UNEXPECTED(zend_object_is_lazy(object))) {
85
1.60k
    zend_lazy_object_del_info(object);
86
1.60k
  }
87
88
3.87M
  zend_object_dtor_dynamic_properties(object);
89
90
3.87M
  p = object->properties_table;
91
3.87M
  if (EXPECTED(object->ce->default_properties_count)) {
92
949k
    end = p + object->ce->default_properties_count;
93
6.46M
    do {
94
6.46M
      zend_object_dtor_property(object, p);
95
6.46M
      p++;
96
6.46M
    } while (p != end);
97
949k
  }
98
99
3.87M
  if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
100
288k
    if (EXPECTED(Z_TYPE_P(p) == IS_STRING)) {
101
1.15k
      zval_ptr_dtor_str(p);
102
287k
    } else if (Z_TYPE_P(p) == IS_ARRAY) {
103
232
      HashTable *guards;
104
105
232
      guards = Z_ARRVAL_P(p);
106
232
      ZEND_ASSERT(guards != NULL);
107
232
      zend_hash_destroy(guards);
108
232
      FREE_HASHTABLE(guards);
109
232
    }
110
288k
  }
111
3.87M
}
112
113
ZEND_API void zend_objects_destroy_object(zend_object *object)
114
80.1k
{
115
80.1k
  zend_function *destructor = object->ce->destructor;
116
117
80.1k
  if (destructor) {
118
80.1k
    if (UNEXPECTED(zend_object_is_lazy(object))) {
119
142
      return;
120
142
    }
121
122
80.0k
    zend_object *old_exception;
123
80.0k
    const zend_op *old_opline_before_exception = NULL;
124
125
80.0k
    if (destructor->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
126
0
      if (EG(current_execute_data)) {
127
0
        zend_class_entry *scope = zend_get_executed_scope();
128
        /* Ensure that if we're calling a protected or private function, we're allowed to do so. */
129
0
        ZEND_ASSERT(!(destructor->common.fn_flags & ZEND_ACC_PUBLIC));
130
0
        if (!zend_check_method_accessible(destructor, scope)) {
131
0
          zend_throw_error(NULL,
132
0
            "Call to %s %s::__destruct() from %s%s",
133
0
            zend_visibility_string(destructor->common.fn_flags), ZSTR_VAL(object->ce->name),
134
0
            scope ? "scope " : "global scope",
135
0
            scope ? ZSTR_VAL(scope->name) : ""
136
0
          );
137
0
          return;
138
0
        }
139
0
      } else {
140
0
        zend_error(E_WARNING,
141
0
          "Call to %s %s::__destruct() from global scope during shutdown ignored",
142
0
          zend_visibility_string(destructor->common.fn_flags), ZSTR_VAL(object->ce->name));
143
0
        return;
144
0
      }
145
0
    }
146
147
80.0k
    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
80.0k
    old_exception = NULL;
154
80.0k
    if (EG(exception)) {
155
69.6k
      if (EG(exception) == object) {
156
0
        zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception");
157
69.6k
      } else {
158
69.6k
        if (EG(current_execute_data)) {
159
12.4k
          if (EG(current_execute_data)->func
160
12.4k
           && ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) {
161
12.3k
            zend_rethrow_exception(EG(current_execute_data));
162
12.3k
          }
163
12.4k
          EG(current_execute_data)->opline = EG(opline_before_exception);
164
12.4k
          old_opline_before_exception = EG(opline_before_exception);
165
12.4k
        }
166
69.6k
        old_exception = EG(exception);
167
69.6k
        EG(exception) = NULL;
168
69.6k
      }
169
69.6k
    }
170
171
80.0k
    zend_call_known_instance_method_with_0_params(destructor, object, NULL);
172
173
80.0k
    if (old_exception) {
174
290
      if (EG(current_execute_data)) {
175
287
        EG(current_execute_data)->opline = EG(exception_op);
176
287
        EG(opline_before_exception) = old_opline_before_exception;
177
287
      }
178
290
      if (EG(exception)) {
179
163
        zend_exception_set_previous(EG(exception), old_exception);
180
163
      } else {
181
127
        EG(exception) = old_exception;
182
127
      }
183
290
    }
184
80.0k
    OBJ_RELEASE(object);
185
80.0k
  }
186
80.1k
}
187
188
ZEND_API zend_object* ZEND_FASTCALL zend_objects_new(zend_class_entry *ce)
189
2.88M
{
190
2.88M
  zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce));
191
192
2.88M
  _zend_object_std_init(object, ce);
193
2.88M
  return object;
194
2.88M
}
195
196
ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, const zend_object *old_object)
197
872
{
198
872
  bool has_clone_method = old_object->ce->clone != NULL;
199
200
872
  if (old_object->ce->default_properties_count) {
201
410
    const zval *src = old_object->properties_table;
202
410
    zval *dst = new_object->properties_table;
203
410
    const zval *end = src + old_object->ce->default_properties_count;
204
205
801
    do {
206
801
      i_zval_ptr_dtor(dst);
207
801
      ZVAL_COPY_VALUE_PROP(dst, src);
208
801
      zval_add_ref(dst);
209
801
      if (has_clone_method) {
210
        /* Unconditionally add the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */
211
183
        Z_PROP_FLAG_P(dst) |= IS_PROP_REINITABLE;
212
183
      }
213
214
801
      if (UNEXPECTED(Z_ISREF_P(dst)) &&
215
5
          (ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(dst)))) {
216
5
        zend_property_info *prop_info = zend_get_property_info_for_slot_self(new_object, dst);
217
5
        if (ZEND_TYPE_IS_SET(prop_info->type)) {
218
5
          ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(dst), prop_info);
219
5
        }
220
5
      }
221
801
      src++;
222
801
      dst++;
223
801
    } while (src != end);
224
462
  } else if (old_object->properties && !has_clone_method) {
225
    /* fast copy */
226
164
    if (EXPECTED(old_object->handlers == &std_object_handlers)) {
227
164
      if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) {
228
164
        GC_ADDREF(old_object->properties);
229
164
      }
230
164
      new_object->properties = old_object->properties;
231
164
      return;
232
164
    }
233
164
  }
234
235
708
  if (old_object->properties &&
236
35
      EXPECTED(zend_hash_num_elements(old_object->properties))) {
237
35
    zval *prop, new_prop;
238
35
    zend_ulong num_key;
239
35
    zend_string *key;
240
241
35
    if (!new_object->properties) {
242
35
      new_object->properties = zend_new_array(zend_hash_num_elements(old_object->properties));
243
35
      zend_hash_real_init_mixed(new_object->properties);
244
35
    } else {
245
0
      zend_hash_extend(new_object->properties, new_object->properties->nNumUsed + zend_hash_num_elements(old_object->properties), 0);
246
0
    }
247
248
35
    HT_FLAGS(new_object->properties) |=
249
35
      HT_FLAGS(old_object->properties) & HASH_FLAG_HAS_EMPTY_IND;
250
251
166
    ZEND_HASH_MAP_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) {
252
166
      if (Z_TYPE_P(prop) == IS_INDIRECT) {
253
34
        ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table));
254
34
      } else {
255
14
        ZVAL_COPY_VALUE(&new_prop, prop);
256
14
        zval_add_ref(&new_prop);
257
14
      }
258
166
      if (has_clone_method) {
259
        /* Unconditionally add the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */
260
37
        Z_PROP_FLAG_P(&new_prop) |= IS_PROP_REINITABLE;
261
37
      }
262
166
      if (EXPECTED(key)) {
263
48
        _zend_hash_append(new_object->properties, key, &new_prop);
264
48
      } else {
265
0
        zend_hash_index_add_new(new_object->properties, num_key, &new_prop);
266
0
      }
267
166
    } ZEND_HASH_FOREACH_END();
268
35
  }
269
270
708
  if (has_clone_method) {
271
214
    zend_call_known_instance_method_with_0_params(new_object->ce->clone, new_object, NULL);
272
273
214
    if (ZEND_CLASS_HAS_READONLY_PROPS(new_object->ce)) {
274
245
      for (uint32_t i = 0; i < new_object->ce->default_properties_count; i++) {
275
135
        zval* prop = OBJ_PROP_NUM(new_object, i);
276
        /* Unconditionally remove the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */
277
135
        Z_PROP_FLAG_P(prop) &= ~IS_PROP_REINITABLE;
278
135
      }
279
110
    }
280
214
  }
281
708
}
282
283
ZEND_API zend_object *zend_objects_clone_obj_with(zend_object *old_object, const zend_class_entry *scope, const HashTable *properties)
284
186
{
285
186
  zend_object *new_object = old_object->handlers->clone_obj(old_object);
286
287
186
  if (EXPECTED(!EG(exception))) {
288
    /* Unlock readonly properties once more. */
289
186
    if (ZEND_CLASS_HAS_READONLY_PROPS(new_object->ce)) {
290
66
      for (uint32_t i = 0; i < new_object->ce->default_properties_count; i++) {
291
44
        zval* prop = OBJ_PROP_NUM(new_object, i);
292
44
        Z_PROP_FLAG_P(prop) |= IS_PROP_REINITABLE;
293
44
      }
294
22
    }
295
296
186
    const zend_class_entry *old_scope = EG(fake_scope);
297
298
186
    EG(fake_scope) = scope;
299
300
856
    ZEND_HASH_FOREACH_KEY_VAL(properties, zend_ulong num_key, zend_string *key, zval *val) {
301
856
      if (UNEXPECTED(Z_ISREF_P(val))) {
302
10
        if (Z_REFCOUNT_P(val) == 1) {
303
5
          val = Z_REFVAL_P(val);
304
5
        } else {
305
5
          zend_throw_error(NULL, "Cannot assign by reference when cloning with updated properties");
306
5
          break;
307
5
        }
308
10
      }
309
310
330
      if (UNEXPECTED(key == NULL)) {
311
15
        key = zend_long_to_str(num_key);
312
15
        new_object->handlers->write_property(new_object, key, val, NULL);
313
15
        zend_string_release_ex(key, false);
314
315
      } else {
315
315
        new_object->handlers->write_property(new_object, key, val, NULL);
316
315
      }
317
318
330
      if (UNEXPECTED(EG(exception))) {
319
73
        break;
320
73
      }
321
330
    } ZEND_HASH_FOREACH_END();
322
323
186
    EG(fake_scope) = old_scope;
324
186
  }
325
326
186
  return new_object;
327
186
}
328
329
ZEND_API zend_object *zend_objects_clone_obj(zend_object *old_object)
330
938
{
331
938
  zend_object *new_object;
332
333
938
  if (UNEXPECTED(zend_object_is_lazy(old_object))) {
334
80
    return zend_lazy_object_clone(old_object);
335
80
  }
336
337
  /* assume that create isn't overwritten, so when clone depends on the
338
   * overwritten one then it must itself be overwritten */
339
858
  new_object = zend_objects_new(old_object->ce);
340
341
  /* zend_objects_clone_members() expect the properties to be initialized. */
342
858
  if (new_object->ce->default_properties_count) {
343
410
    zval *p = new_object->properties_table;
344
410
    zval *end = p + new_object->ce->default_properties_count;
345
801
    do {
346
801
      ZVAL_UNDEF(p);
347
801
      p++;
348
801
    } while (p != end);
349
410
  }
350
351
858
  zend_objects_clone_members(new_object, old_object);
352
353
858
  return new_object;
354
938
}