Coverage Report

Created: 2026-04-01 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_objects_API.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_objects_API.h"
26
#include "zend_fibers.h"
27
28
ZEND_API void ZEND_FASTCALL zend_objects_store_init(zend_objects_store *objects, uint32_t init_size)
29
224k
{
30
224k
  objects->object_buckets = (zend_object **) emalloc(init_size * sizeof(zend_object*));
31
224k
  objects->top = 1; /* Skip 0 so that handles are true */
32
224k
  objects->size = init_size;
33
224k
  objects->free_list_head = -1;
34
224k
  memset(&objects->object_buckets[0], 0, sizeof(zend_object*));
35
224k
}
36
37
ZEND_API void ZEND_FASTCALL zend_objects_store_destroy(zend_objects_store *objects)
38
224k
{
39
224k
  efree(objects->object_buckets);
40
224k
  objects->object_buckets = NULL;
41
224k
}
42
43
ZEND_API void ZEND_FASTCALL zend_objects_store_call_destructors(zend_objects_store *objects)
44
224k
{
45
224k
  EG(flags) |= EG_FLAGS_OBJECT_STORE_NO_REUSE;
46
224k
  if (objects->top > 1) {
47
3.08M
    for (uint32_t i = 1; i < objects->top; i++) {
48
2.93M
      zend_object *obj = objects->object_buckets[i];
49
2.93M
      if (IS_OBJ_VALID(obj)) {
50
36.2k
        if (!(OBJ_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
51
30.1k
          GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
52
53
30.1k
          if (obj->handlers->dtor_obj != zend_objects_destroy_object
54
28.2k
              || obj->ce->destructor) {
55
3.00k
            GC_ADDREF(obj);
56
3.00k
            obj->handlers->dtor_obj(obj);
57
3.00k
            GC_DELREF(obj);
58
3.00k
          }
59
30.1k
        }
60
36.2k
      }
61
2.93M
    }
62
148k
  }
63
224k
}
64
65
ZEND_API void ZEND_FASTCALL zend_objects_store_mark_destructed(zend_objects_store *objects)
66
7.64k
{
67
7.64k
  if (objects->object_buckets && objects->top > 1) {
68
1.19k
    zend_object **obj_ptr = objects->object_buckets + 1;
69
1.19k
    zend_object **end = objects->object_buckets + objects->top;
70
71
103k
    do {
72
103k
      zend_object *obj = *obj_ptr;
73
74
103k
      if (IS_OBJ_VALID(obj)) {
75
101k
        GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
76
101k
      }
77
103k
      obj_ptr++;
78
103k
    } while (obj_ptr != end);
79
1.19k
  }
80
7.64k
}
81
82
ZEND_API void ZEND_FASTCALL zend_objects_store_free_object_storage(zend_objects_store *objects, bool fast_shutdown)
83
224k
{
84
224k
  zend_object **obj_ptr, **end, *obj;
85
86
224k
  if (objects->top <= 1) {
87
75.5k
    return;
88
75.5k
  }
89
90
  /* Free object contents, but don't free objects themselves, so they show up as leaks.
91
   * Also add a ref to all objects, so the object can't be freed by something else later. */
92
149k
  end = objects->object_buckets + 1;
93
149k
  obj_ptr = objects->object_buckets + objects->top;
94
95
149k
  if (fast_shutdown) {
96
0
    do {
97
0
      obj_ptr--;
98
0
      obj = *obj_ptr;
99
0
      if (IS_OBJ_VALID(obj)) {
100
0
        if (!(OBJ_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
101
0
          GC_ADD_FLAGS(obj, IS_OBJ_FREE_CALLED);
102
0
          if (obj->handlers->free_obj != zend_object_std_dtor
103
0
           || (OBJ_FLAGS(obj) & IS_OBJ_WEAKLY_REFERENCED)
104
0
          ) {
105
0
            GC_ADDREF(obj);
106
0
            obj->handlers->free_obj(obj);
107
0
          }
108
0
        }
109
0
      }
110
0
    } while (obj_ptr != end);
111
149k
  } else {
112
3.03M
    do {
113
3.03M
      obj_ptr--;
114
3.03M
      obj = *obj_ptr;
115
3.03M
      if (IS_OBJ_VALID(obj)) {
116
118k
        if (!(OBJ_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
117
118k
          GC_ADD_FLAGS(obj, IS_OBJ_FREE_CALLED);
118
118k
          GC_ADDREF(obj);
119
118k
          obj->handlers->free_obj(obj);
120
118k
        }
121
118k
      }
122
3.03M
    } while (obj_ptr != end);
123
149k
  }
124
149k
}
125
126
127
/* Store objects API */
128
static ZEND_COLD zend_never_inline void ZEND_FASTCALL zend_objects_store_put_cold(zend_object *object)
129
524
{
130
524
  uint32_t new_size = 2 * EG(objects_store).size;
131
132
524
  EG(objects_store).object_buckets = (zend_object **) erealloc(EG(objects_store).object_buckets, new_size * sizeof(zend_object*));
133
  /* Assign size after realloc, in case it fails */
134
524
  EG(objects_store).size = new_size;
135
524
  uint32_t handle = EG(objects_store).top++;
136
524
  object->handle = handle;
137
524
  EG(objects_store).object_buckets[handle] = object;
138
524
}
139
140
ZEND_API void ZEND_FASTCALL zend_objects_store_put(zend_object *object)
141
3.98M
{
142
3.98M
  uint32_t handle;
143
144
  /* When in shutdown sequence - do not reuse previously freed handles, to make sure
145
   * the dtors for newly created objects are called in zend_objects_store_call_destructors() loop
146
   */
147
3.98M
  if (EG(objects_store).free_list_head != -1 && EXPECTED(!(EG(flags) & EG_FLAGS_OBJECT_STORE_NO_REUSE))) {
148
952k
    handle = EG(objects_store).free_list_head;
149
952k
    EG(objects_store).free_list_head = GET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle]);
150
3.03M
  } else if (UNEXPECTED(EG(objects_store).top == EG(objects_store).size)) {
151
524
    zend_objects_store_put_cold(object);
152
524
    return;
153
3.03M
  } else {
154
3.03M
    handle = EG(objects_store).top++;
155
3.03M
  }
156
3.98M
  object->handle = handle;
157
3.98M
  EG(objects_store).object_buckets[handle] = object;
158
3.98M
}
159
160
ZEND_API void ZEND_FASTCALL zend_objects_store_del(zend_object *object) /* {{{ */
161
3.87M
{
162
3.87M
  ZEND_ASSERT(GC_REFCOUNT(object) == 0);
163
164
  /* GC might have released this object already. */
165
3.87M
  if (UNEXPECTED(GC_TYPE(object) == IS_NULL)) {
166
1.76k
    return;
167
1.76k
  }
168
169
  /*  Make sure we hold a reference count during the destructor call
170
    otherwise, when the destructor ends the storage might be freed
171
    when the refcount reaches 0 a second time
172
   */
173
3.86M
  if (!(OBJ_FLAGS(object) & IS_OBJ_DESTRUCTOR_CALLED)) {
174
3.35M
    GC_ADD_FLAGS(object, IS_OBJ_DESTRUCTOR_CALLED);
175
176
3.35M
    if (object->handlers->dtor_obj != zend_objects_destroy_object
177
3.34M
        || object->ce->destructor) {
178
58.0k
      GC_SET_REFCOUNT(object, 1);
179
58.0k
      object->handlers->dtor_obj(object);
180
58.0k
      GC_DELREF(object);
181
58.0k
    }
182
3.35M
  }
183
184
3.86M
  if (GC_REFCOUNT(object) == 0) {
185
3.82M
    uint32_t handle = object->handle;
186
3.82M
    void *ptr;
187
188
3.82M
    ZEND_ASSERT(EG(objects_store).object_buckets != NULL);
189
3.82M
    ZEND_ASSERT(IS_OBJ_VALID(EG(objects_store).object_buckets[handle]));
190
3.82M
    EG(objects_store).object_buckets[handle] = SET_OBJ_INVALID(object);
191
3.82M
    if (!(OBJ_FLAGS(object) & IS_OBJ_FREE_CALLED)) {
192
3.82M
      GC_ADD_FLAGS(object, IS_OBJ_FREE_CALLED);
193
3.82M
      GC_SET_REFCOUNT(object, 1);
194
3.82M
      object->handlers->free_obj(object);
195
3.82M
    }
196
3.82M
    ptr = ((char*)object) - object->handlers->offset;
197
3.82M
    GC_REMOVE_FROM_BUFFER(object);
198
3.82M
    efree(ptr);
199
3.82M
    ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
200
3.82M
  }
201
3.86M
}
202
/* }}} */
203
204
ZEND_API ZEND_COLD zend_property_info *zend_get_property_info_for_slot_slow(zend_object *obj, zval *slot)
205
155
{
206
155
  uintptr_t offset = OBJ_PROP_SLOT_TO_OFFSET(obj, slot);
207
155
  zend_property_info *prop_info;
208
620
  ZEND_HASH_MAP_FOREACH_PTR(&obj->ce->properties_info, prop_info) {
209
620
    if (prop_info->offset == offset) {
210
155
      return prop_info;
211
155
    }
212
620
  } ZEND_HASH_FOREACH_END();
213
0
  return NULL;
214
155
}