Coverage Report

Created: 2025-09-27 06:26

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