Coverage Report

Created: 2026-02-09 07:07

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