Coverage Report

Created: 2026-10-28 06:20

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