Coverage Report

Created: 2025-07-23 06:33

/src/php-src/Zend/zend_objects.c
Line
Count
Source (jump to first uncovered line)
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.07M
{
32
4.07M
  GC_SET_REFCOUNT(object, 1);
33
4.07M
  GC_TYPE_INFO(object) = GC_OBJECT;
34
4.07M
  object->ce = ce;
35
4.07M
  object->extra_flags = 0;
36
4.07M
  object->handlers = ce->default_object_handlers;
37
4.07M
  object->properties = NULL;
38
4.07M
  zend_objects_store_put(object);
39
4.07M
  if (UNEXPECTED(ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
40
264k
    zval *guard_value = object->properties_table + object->ce->default_properties_count;
41
264k
    ZVAL_UNDEF(guard_value);
42
264k
    Z_GUARD_P(guard_value) = 0;
43
264k
  }
44
4.07M
}
45
46
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
47
1.22M
{
48
1.22M
  _zend_object_std_init(object, ce);
49
1.22M
}
50
51
void zend_object_dtor_dynamic_properties(zend_object *object)
52
4.06M
{
53
4.06M
  if (object->properties) {
54
661k
    if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) {
55
661k
      if (EXPECTED(GC_DELREF(object->properties) == 0)
56
661k
          && EXPECTED(GC_TYPE(object->properties) != IS_NULL)) {
57
661k
        zend_array_destroy(object->properties);
58
661k
      }
59
661k
    }
60
661k
  }
61
4.06M
}
62
63
void zend_object_dtor_property(zend_object *object, zval *p)
64
8.72M
{
65
8.72M
  if (Z_REFCOUNTED_P(p)) {
66
3.61M
    if (UNEXPECTED(Z_ISREF_P(p)) &&
67
3.61M
        (ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(p)))) {
68
18.0k
      zend_property_info *prop_info = zend_get_property_info_for_slot_self(object, p);
69
18.0k
      if (ZEND_TYPE_IS_SET(prop_info->type)) {
70
17.6k
        ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info);
71
17.6k
      }
72
18.0k
    }
73
3.61M
    i_zval_ptr_dtor(p);
74
3.61M
  }
75
8.72M
}
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
373
    zend_weakrefs_notify(object);
83
373
  }
84
85
4.06M
  if (UNEXPECTED(zend_object_is_lazy(object))) {
86
1.43k
    zend_lazy_object_del_info(object);
87
1.43k
  }
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.26M
    end = p + object->ce->default_properties_count;
94
8.72M
    do {
95
8.72M
      zend_object_dtor_property(object, p);
96
8.72M
      p++;
97
8.72M
    } while (p != end);
98
1.26M
  }
99
100
4.06M
  if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
101
264k
    if (EXPECTED(Z_TYPE_P(p) == IS_STRING)) {
102
937
      zval_ptr_dtor_str(p);
103
263k
    } else if (Z_TYPE_P(p) == IS_ARRAY) {
104
89
      HashTable *guards;
105
106
89
      guards = Z_ARRVAL_P(p);
107
89
      ZEND_ASSERT(guards != NULL);
108
89
      zend_hash_destroy(guards);
109
89
      FREE_HASHTABLE(guards);
110
89
    }
111
264k
  }
112
4.06M
}
113
114
ZEND_API void zend_objects_destroy_object(zend_object *object)
115
279k
{
116
279k
  zend_function *destructor = object->ce->destructor;
117
118
279k
  if (destructor) {
119
279k
    if (UNEXPECTED(zend_object_is_lazy(object))) {
120
141
      return;
121
141
    }
122
123
279k
    zend_object *old_exception;
124
279k
    const zend_op *old_opline_before_exception;
125
126
279k
    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
279k
    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
279k
    old_exception = NULL;
155
279k
    if (EG(exception)) {
156
263k
      if (EG(exception) == object) {
157
0
        zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception");
158
263k
      } else {
159
263k
        if (EG(current_execute_data)
160
263k
         && EG(current_execute_data)->func
161
263k
         && ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) {
162
108k
          zend_rethrow_exception(EG(current_execute_data));
163
108k
        }
164
263k
        old_exception = EG(exception);
165
263k
        old_opline_before_exception = EG(opline_before_exception);
166
263k
        EG(exception) = NULL;
167
263k
      }
168
263k
    }
169
170
279k
    zend_call_known_instance_method_with_0_params(destructor, object, NULL);
171
172
279k
    if (old_exception) {
173
234
      EG(opline_before_exception) = old_opline_before_exception;
174
234
      if (EG(exception)) {
175
106
        zend_exception_set_previous(EG(exception), old_exception);
176
128
      } else {
177
128
        EG(exception) = old_exception;
178
128
      }
179
234
    }
180
279k
    OBJ_RELEASE(object);
181
279k
  }
182
279k
}
183
184
ZEND_API zend_object* ZEND_FASTCALL zend_objects_new(zend_class_entry *ce)
185
2.84M
{
186
2.84M
  zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce));
187
188
2.84M
  _zend_object_std_init(object, ce);
189
2.84M
  return object;
190
2.84M
}
191
192
ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
193
804
{
194
804
  bool has_clone_method = old_object->ce->clone != NULL;
195
196
804
  if (old_object->ce->default_properties_count) {
197
351
    zval *src = old_object->properties_table;
198
351
    zval *dst = new_object->properties_table;
199
351
    zval *end = src + old_object->ce->default_properties_count;
200
201
625
    do {
202
625
      i_zval_ptr_dtor(dst);
203
625
      ZVAL_COPY_VALUE_PROP(dst, src);
204
625
      zval_add_ref(dst);
205
625
      if (has_clone_method) {
206
        /* Unconditionally add the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */
207
177
        Z_PROP_FLAG_P(dst) |= IS_PROP_REINITABLE;
208
177
      }
209
210
625
      if (UNEXPECTED(Z_ISREF_P(dst)) &&
211
625
          (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
625
      src++;
218
625
      dst++;
219
625
    } while (src != end);
220
453
  } else if (old_object->properties && !has_clone_method) {
221
    /* fast copy */
222
166
    if (EXPECTED(old_object->handlers == &std_object_handlers)) {
223
166
      if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) {
224
166
        GC_ADDREF(old_object->properties);
225
166
      }
226
166
      new_object->properties = old_object->properties;
227
166
      return;
228
166
    }
229
166
  }
230
231
638
  if (old_object->properties &&
232
638
      EXPECTED(zend_hash_num_elements(old_object->properties))) {
233
14
    zval *prop, new_prop;
234
14
    zend_ulong num_key;
235
14
    zend_string *key;
236
237
14
    if (!new_object->properties) {
238
14
      new_object->properties = zend_new_array(zend_hash_num_elements(old_object->properties));
239
14
      zend_hash_real_init_mixed(new_object->properties);
240
14
    } 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
14
    HT_FLAGS(new_object->properties) |=
245
14
      HT_FLAGS(old_object->properties) & HASH_FLAG_HAS_EMPTY_IND;
246
247
64
    ZEND_HASH_MAP_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) {
248
64
      if (Z_TYPE_P(prop) == IS_INDIRECT) {
249
16
        ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table));
250
16
      } else {
251
2
        ZVAL_COPY_VALUE(&new_prop, prop);
252
2
        zval_add_ref(&new_prop);
253
2
      }
254
64
      if (has_clone_method) {
255
        /* Unconditionally add the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */
256
10
        Z_PROP_FLAG_P(&new_prop) |= IS_PROP_REINITABLE;
257
10
      }
258
64
      if (EXPECTED(key)) {
259
18
        _zend_hash_append(new_object->properties, key, &new_prop);
260
18
      } else {
261
0
        zend_hash_index_add_new(new_object->properties, num_key, &new_prop);
262
0
      }
263
64
    } ZEND_HASH_FOREACH_END();
264
14
  }
265
266
638
  if (has_clone_method) {
267
201
    zend_call_known_instance_method_with_0_params(new_object->ce->clone, new_object, NULL);
268
269
201
    if (ZEND_CLASS_HAS_READONLY_PROPS(new_object->ce)) {
270
243
      for (uint32_t i = 0; i < new_object->ce->default_properties_count; i++) {
271
133
        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
133
        Z_PROP_FLAG_P(prop) &= ~IS_PROP_REINITABLE;
274
133
      }
275
110
    }
276
201
  }
277
638
}
278
279
ZEND_API zend_object *zend_objects_clone_obj_with(zend_object *old_object, const zend_class_entry *scope, const HashTable *properties)
280
141
{
281
141
  zend_object *new_object = old_object->handlers->clone_obj(old_object);
282
283
141
  if (EXPECTED(!EG(exception))) {
284
    /* Unlock readonly properties once more. */
285
141
    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
141
    const zend_class_entry *old_scope = EG(fake_scope);
293
294
141
    EG(fake_scope) = scope;
295
296
635
    ZEND_HASH_FOREACH_KEY_VAL(properties, zend_ulong num_key, zend_string *key, zval *val) {
297
635
      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
242
      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
227
      } else {
311
227
        new_object->handlers->write_property(new_object, key, val, NULL);
312
227
      }
313
314
242
      if (UNEXPECTED(EG(exception))) {
315
45
        break;
316
45
      }
317
242
    } ZEND_HASH_FOREACH_END();
318
319
141
    EG(fake_scope) = old_scope;
320
141
  }
321
322
141
  return new_object;
323
141
}
324
325
ZEND_API zend_object *zend_objects_clone_obj(zend_object *old_object)
326
863
{
327
863
  zend_object *new_object;
328
329
863
  if (UNEXPECTED(zend_object_is_lazy(old_object))) {
330
72
    return zend_lazy_object_clone(old_object);
331
72
  }
332
333
  /* assume that create isn't overwritten, so when clone depends on the
334
   * overwritten one then it must itself be overwritten */
335
791
  new_object = zend_objects_new(old_object->ce);
336
337
  /* zend_objects_clone_members() expect the properties to be initialized. */
338
791
  if (new_object->ce->default_properties_count) {
339
351
    zval *p = new_object->properties_table;
340
351
    zval *end = p + new_object->ce->default_properties_count;
341
625
    do {
342
625
      ZVAL_UNDEF(p);
343
625
      p++;
344
625
    } while (p != end);
345
351
  }
346
347
791
  zend_objects_clone_members(new_object, old_object);
348
349
791
  return new_object;
350
863
}