Coverage Report

Created: 2025-12-14 06:05

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