Coverage Report

Created: 2026-06-02 06:39

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