Coverage Report

Created: 2026-09-14 06:25

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.82M
{
31
3.82M
  GC_SET_REFCOUNT(object, 1);
32
3.82M
  GC_TYPE_INFO(object) = GC_OBJECT;
33
3.82M
  object->ce = ce;
34
3.82M
  object->extra_flags = 0;
35
3.82M
  object->handlers = ce->default_object_handlers;
36
3.82M
  object->properties = NULL;
37
3.82M
  zend_objects_store_put(object);
38
3.82M
  if (UNEXPECTED(ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
39
229k
    zval *guard_value = object->properties_table + object->ce->default_properties_count;
40
229k
    ZVAL_UNDEF(guard_value);
41
229k
    Z_GUARD_P(guard_value) = 0;
42
229k
  }
43
3.82M
}
44
45
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
46
973k
{
47
973k
  _zend_object_std_init(object, ce);
48
973k
}
49
50
void zend_object_dtor_dynamic_properties(zend_object *object)
51
3.81M
{
52
3.81M
  if (object->properties) {
53
620k
    if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) {
54
620k
      if (EXPECTED(GC_DELREF(object->properties) == 0)
55
619k
          && EXPECTED(GC_TYPE(object->properties) != IS_NULL)) {
56
619k
        zend_array_destroy(object->properties);
57
619k
      }
58
620k
    }
59
620k
  }
60
3.81M
}
61
62
void zend_object_dtor_property(zend_object *object, zval *p)
63
6.73M
{
64
6.73M
  if (Z_REFCOUNTED_P(p)) {
65
2.82M
    if (UNEXPECTED(Z_ISREF_P(p)) &&
66
37.9k
        (ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(p)))) {
67
37.9k
      zend_property_info *prop_info = zend_get_property_info_for_slot_self(object, p);
68
37.9k
      if (ZEND_TYPE_IS_SET(prop_info->type)) {
69
37.1k
        ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info);
70
37.1k
      }
71
37.9k
    }
72
2.82M
    i_zval_ptr_dtor(p);
73
2.82M
  }
74
6.73M
}
75
76
ZEND_API void zend_object_std_dtor(zend_object *object)
77
3.81M
{
78
3.81M
  zval *p, *end;
79
80
3.81M
  if (UNEXPECTED(GC_FLAGS(object) & IS_OBJ_WEAKLY_REFERENCED)) {
81
756
    zend_weakrefs_notify(object);
82
756
  }
83
84
3.81M
  if (UNEXPECTED(zend_object_is_lazy(object))) {
85
1.87k
    zend_lazy_object_del_info(object);
86
1.87k
  }
87
88
3.81M
  zend_object_dtor_dynamic_properties(object);
89
90
3.81M
  p = object->properties_table;
91
3.81M
  if (EXPECTED(object->ce->default_properties_count)) {
92
997k
    end = p + object->ce->default_properties_count;
93
6.73M
    do {
94
6.73M
      zend_object_dtor_property(object, p);
95
6.73M
      p++;
96
6.73M
    } while (p != end);
97
997k
  }
98
99
3.81M
  if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
100
229k
    if (EXPECTED(Z_TYPE_P(p) == IS_STRING)) {
101
1.43k
      zval_ptr_dtor_str(p);
102
228k
    } else if (Z_TYPE_P(p) == IS_ARRAY) {
103
438
      HashTable *guards;
104
105
438
      guards = Z_ARRVAL_P(p);
106
438
      ZEND_ASSERT(guards != NULL);
107
438
      zend_hash_destroy(guards);
108
438
      FREE_HASHTABLE(guards);
109
438
    }
110
229k
  }
111
3.81M
}
112
113
ZEND_API void zend_objects_destroy_object(zend_object *object)
114
68.8k
{
115
68.8k
  zend_function *destructor = object->ce->destructor;
116
117
68.8k
  if (destructor) {
118
68.8k
    if (UNEXPECTED(zend_object_is_lazy(object))) {
119
191
      return;
120
191
    }
121
122
68.6k
    zend_object *old_exception;
123
68.6k
    const zend_op *old_opline_before_exception = NULL;
124
125
68.6k
    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
68.6k
    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
68.6k
    old_exception = NULL;
154
68.6k
    if (EG(exception)) {
155
56.9k
      if (EG(exception) == object) {
156
0
        zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception");
157
56.9k
      } else {
158
56.9k
        if (EG(current_execute_data)) {
159
14.4k
          if (EG(current_execute_data)->func
160
14.4k
           && ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) {
161
14.3k
            zend_rethrow_exception(EG(current_execute_data));
162
14.3k
          }
163
14.4k
          EG(current_execute_data)->opline = EG(opline_before_exception);
164
14.4k
          old_opline_before_exception = EG(opline_before_exception);
165
14.4k
        }
166
56.9k
        old_exception = EG(exception);
167
56.9k
        EG(exception) = NULL;
168
56.9k
      }
169
56.9k
    }
170
171
68.6k
    zend_call_known_instance_method_with_0_params(destructor, object, NULL);
172
173
68.6k
    if (old_exception) {
174
540
      if (EG(current_execute_data)) {
175
532
        EG(current_execute_data)->opline = EG(exception_op);
176
532
        EG(opline_before_exception) = old_opline_before_exception;
177
532
      }
178
540
      if (EG(exception)) {
179
406
        zend_exception_set_previous(EG(exception), old_exception);
180
406
      } else {
181
134
        EG(exception) = old_exception;
182
134
      }
183
540
    }
184
68.6k
    OBJ_RELEASE(object);
185
68.6k
  }
186
68.8k
}
187
188
ZEND_API zend_object* ZEND_FASTCALL zend_objects_new(zend_class_entry *ce)
189
2.84M
{
190
2.84M
  zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce));
191
192
2.84M
  _zend_object_std_init(object, ce);
193
2.84M
  return object;
194
2.84M
}
195
196
ZEND_API void ZEND_FASTCALL zend_object_set_properties_reinitable(zend_object *object, bool reinitable)
197
725k
{
198
725k
  if (!ZEND_CLASS_HAS_READONLY_PROPS(object->ce)) {
199
724k
    return;
200
724k
  }
201
202
1.05k
  for (uint32_t i = 0; i < object->ce->default_properties_count; i++) {
203
586
    zval *prop = OBJ_PROP_NUM(object, i);
204
586
    if (reinitable) {
205
293
      if (!Z_ISUNDEF_P(prop)) {
206
263
        Z_PROP_FLAG_P(prop) |= IS_PROP_REINITABLE;
207
263
      }
208
293
    } else {
209
293
      Z_PROP_FLAG_P(prop) &= ~IS_PROP_REINITABLE;
210
293
    }
211
586
  }
212
464
}
213
214
ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, const zend_object *old_object)
215
1.06k
{
216
1.06k
  bool has_clone_method = old_object->ce->clone != NULL;
217
218
1.06k
  if (old_object->ce->default_properties_count) {
219
499
    const zval *src = old_object->properties_table;
220
499
    zval *dst = new_object->properties_table;
221
499
    const zval *end = src + old_object->ce->default_properties_count;
222
223
908
    do {
224
908
      i_zval_ptr_dtor(dst);
225
908
      ZVAL_COPY_VALUE_PROP(dst, src);
226
908
      zval_add_ref(dst);
227
228
908
      if (UNEXPECTED(Z_ISREF_P(dst)) &&
229
10
          (ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(dst)))) {
230
10
        zend_property_info *prop_info = zend_get_property_info_for_slot_self(new_object, dst);
231
10
        if (ZEND_TYPE_IS_SET(prop_info->type)) {
232
8
          ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(dst), prop_info);
233
8
        }
234
10
      }
235
908
      src++;
236
908
      dst++;
237
908
    } while (src != end);
238
567
  } else if (old_object->properties && !has_clone_method) {
239
    /* fast copy */
240
150
    if (EXPECTED(old_object->handlers == &std_object_handlers)) {
241
150
      if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) {
242
150
        GC_ADDREF(old_object->properties);
243
150
      }
244
150
      new_object->properties = old_object->properties;
245
150
      return;
246
150
    }
247
150
  }
248
249
916
  if (old_object->properties &&
250
58
      EXPECTED(zend_hash_num_elements(old_object->properties))) {
251
57
    zval *prop, new_prop;
252
57
    zend_ulong num_key;
253
57
    zend_string *key;
254
255
57
    if (!new_object->properties) {
256
57
      new_object->properties = zend_new_array(zend_hash_num_elements(old_object->properties));
257
57
      zend_hash_real_init_mixed(new_object->properties);
258
57
    } else {
259
0
      zend_hash_extend(new_object->properties, new_object->properties->nNumUsed + zend_hash_num_elements(old_object->properties), 0);
260
0
    }
261
262
57
    HT_FLAGS(new_object->properties) |=
263
57
      HT_FLAGS(old_object->properties) & HASH_FLAG_HAS_EMPTY_IND;
264
265
264
    ZEND_HASH_MAP_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) {
266
264
      if (Z_TYPE_P(prop) == IS_INDIRECT) {
267
54
        ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table));
268
54
      } else {
269
21
        ZVAL_COPY_VALUE(&new_prop, prop);
270
21
        zval_add_ref(&new_prop);
271
21
      }
272
273
264
      if (EXPECTED(key)) {
274
75
        _zend_hash_append(new_object->properties, key, &new_prop);
275
75
      } else {
276
0
        zend_hash_index_add_new(new_object->properties, num_key, &new_prop);
277
0
      }
278
264
    } ZEND_HASH_FOREACH_END();
279
57
  }
280
281
916
  if (has_clone_method) {
282
290
    zend_object_set_properties_reinitable(new_object, /* reinitable */ true);
283
290
    zend_call_known_instance_method_with_0_params(new_object->ce->clone, new_object, NULL);
284
290
    zend_object_set_properties_reinitable(new_object, /* reinitable */ false);
285
290
  }
286
916
}
287
288
ZEND_API zend_object *zend_objects_clone_obj_with(zend_object *old_object, const zend_class_entry *scope, const HashTable *properties)
289
250
{
290
250
  zend_object *new_object = old_object->handlers->clone_obj(old_object);
291
292
250
  if (EXPECTED(!EG(exception))) {
293
294
250
    zend_object_set_properties_reinitable(new_object, /* reinitable */ true);
295
296
250
    const zend_class_entry *old_scope = EG(fake_scope);
297
298
250
    EG(fake_scope) = scope;
299
300
1.11k
    ZEND_HASH_FOREACH_KEY_VAL(properties, zend_ulong num_key, zend_string *key, zval *val) {
301
1.11k
      if (UNEXPECTED(Z_ISREF_P(val))) {
302
18
        if (Z_REFCOUNT_P(val) == 1) {
303
6
          val = Z_REFVAL_P(val);
304
12
        } else {
305
12
          zend_throw_error(NULL, "Cannot assign by reference when cloning with updated properties");
306
12
          break;
307
12
        }
308
18
      }
309
310
421
      if (UNEXPECTED(key == NULL)) {
311
39
        key = zend_long_to_str(num_key);
312
39
        new_object->handlers->write_property(new_object, key, val, NULL);
313
39
        zend_string_release_ex(key, false);
314
382
      } else {
315
382
        new_object->handlers->write_property(new_object, key, val, NULL);
316
382
      }
317
318
421
      if (UNEXPECTED(EG(exception))) {
319
64
        break;
320
64
      }
321
421
    } ZEND_HASH_FOREACH_END();
322
323
250
    EG(fake_scope) = old_scope;
324
325
250
    zend_object_set_properties_reinitable(new_object, /* reinitable */ false);
326
250
  }
327
328
250
  return new_object;
329
250
}
330
331
ZEND_API zend_object *zend_objects_clone_obj(zend_object *old_object)
332
1.12k
{
333
1.12k
  zend_object *new_object;
334
335
1.12k
  if (UNEXPECTED(zend_object_is_lazy(old_object))) {
336
70
    return zend_lazy_object_clone(old_object);
337
70
  }
338
339
  /* assume that create isn't overwritten, so when clone depends on the
340
   * overwritten one then it must itself be overwritten */
341
1.05k
  new_object = zend_objects_new(old_object->ce);
342
343
  /* zend_objects_clone_members() expect the properties to be initialized. */
344
1.05k
  if (new_object->ce->default_properties_count) {
345
499
    zval *p = new_object->properties_table;
346
499
    zval *end = p + new_object->ce->default_properties_count;
347
908
    do {
348
908
      ZVAL_UNDEF(p);
349
908
      p++;
350
908
    } while (p != end);
351
499
  }
352
353
1.05k
  zend_objects_clone_members(new_object, old_object);
354
355
1.05k
  return new_object;
356
1.12k
}