Coverage Report

Created: 2026-06-02 06:36

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