Coverage Report

Created: 2025-11-16 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_list.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
   +----------------------------------------------------------------------+
18
*/
19
20
/* resource lists */
21
22
#include "zend.h"
23
#include "zend_list.h"
24
#include "zend_API.h"
25
#include "zend_globals.h"
26
27
ZEND_API int le_index_ptr;
28
29
/* true global */
30
static HashTable list_destructors;
31
32
ZEND_API zval* ZEND_FASTCALL zend_list_insert(void *ptr, int type)
33
7.30k
{
34
7.30k
  zval zv;
35
36
7.30k
  zend_long index = zend_hash_next_free_element(&EG(regular_list));
37
7.30k
  if (index == 0) {
38
6.21k
    index = 1;
39
6.21k
  } else if (index == ZEND_LONG_MAX) {
40
0
    zend_error_noreturn(E_ERROR, "Resource ID space overflow");
41
0
  }
42
7.30k
  ZVAL_NEW_RES(&zv, index, ptr, type);
43
7.30k
  return zend_hash_index_add_new(&EG(regular_list), index, &zv);
44
7.30k
}
45
46
ZEND_API zend_result ZEND_FASTCALL zend_list_delete(zend_resource *res)
47
595
{
48
595
  if (GC_DELREF(res) <= 0) {
49
263
    return zend_hash_index_del(&EG(regular_list), res->handle);
50
332
  } else {
51
332
    return SUCCESS;
52
332
  }
53
595
}
54
55
ZEND_API void ZEND_FASTCALL zend_list_free(zend_resource *res)
56
6.23k
{
57
6.23k
  ZEND_ASSERT(GC_REFCOUNT(res) == 0);
58
6.23k
  zend_hash_index_del(&EG(regular_list), res->handle);
59
6.23k
}
60
61
static void zend_resource_dtor(zend_resource *res)
62
7.30k
{
63
7.30k
  zend_rsrc_list_dtors_entry *ld;
64
7.30k
  zend_resource r = *res;
65
66
7.30k
  res->type = -1;
67
7.30k
  res->ptr = NULL;
68
69
7.30k
  ld = zend_hash_index_find_ptr(&list_destructors, r.type);
70
7.30k
  ZEND_ASSERT(ld && "Unknown list entry type");
71
72
7.30k
  if (ld->list_dtor_ex) {
73
7.25k
    ld->list_dtor_ex(&r);
74
7.25k
  }
75
7.30k
}
76
77
78
ZEND_API void ZEND_FASTCALL zend_list_close(zend_resource *res)
79
254
{
80
254
  if (GC_REFCOUNT(res) <= 0) {
81
0
    zend_list_free(res);
82
254
  } else if (res->type >= 0) {
83
254
    zend_resource_dtor(res);
84
254
  }
85
254
}
86
87
ZEND_API zend_resource* zend_register_resource(void *rsrc_pointer, int rsrc_type)
88
7.30k
{
89
7.30k
  zval *zv;
90
91
7.30k
  zv = zend_list_insert(rsrc_pointer, rsrc_type);
92
93
7.30k
  return Z_RES_P(zv);
94
7.30k
}
95
96
ZEND_API void *zend_fetch_resource2(zend_resource *res, const char *resource_type_name, int resource_type1, int resource_type2)
97
6.02k
{
98
6.02k
  if (res) {
99
6.02k
    if (resource_type1 == res->type) {
100
6.02k
      return res->ptr;
101
6.02k
    }
102
103
0
    if (resource_type2 == res->type) {
104
0
      return res->ptr;
105
0
    }
106
0
  }
107
108
0
  if (resource_type_name) {
109
0
    const char *space;
110
0
    const char *class_name = get_active_class_name(&space);
111
0
    zend_type_error("%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
112
0
  }
113
114
0
  return NULL;
115
6.02k
}
116
117
ZEND_API void *zend_fetch_resource(zend_resource *res, const char *resource_type_name, int resource_type)
118
47
{
119
47
  if (resource_type == res->type) {
120
47
    return res->ptr;
121
47
  }
122
123
0
  if (resource_type_name) {
124
0
    const char *space;
125
0
    const char *class_name = get_active_class_name(&space);
126
0
    zend_type_error("%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
127
0
  }
128
129
0
  return NULL;
130
47
}
131
132
ZEND_API void *zend_fetch_resource_ex(zval *res, const char *resource_type_name, int resource_type)
133
13
{
134
13
  const char *space, *class_name;
135
13
  if (res == NULL) {
136
0
    if (resource_type_name) {
137
0
      class_name = get_active_class_name(&space);
138
0
      zend_type_error("%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
139
0
    }
140
0
    return NULL;
141
0
  }
142
13
  if (Z_TYPE_P(res) != IS_RESOURCE) {
143
0
    if (resource_type_name) {
144
0
      class_name = get_active_class_name(&space);
145
0
      zend_type_error("%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
146
0
    }
147
0
    return NULL;
148
0
  }
149
150
13
  return zend_fetch_resource(Z_RES_P(res), resource_type_name, resource_type);
151
13
}
152
153
ZEND_API void *zend_fetch_resource2_ex(zval *res, const char *resource_type_name, int resource_type1, int resource_type2)
154
0
{
155
0
  const char *space, *class_name;
156
0
  if (res == NULL) {
157
0
    if (resource_type_name) {
158
0
      class_name = get_active_class_name(&space);
159
0
      zend_type_error("%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
160
0
    }
161
0
    return NULL;
162
0
  }
163
0
  if (Z_TYPE_P(res) != IS_RESOURCE) {
164
0
    if (resource_type_name) {
165
0
      class_name = get_active_class_name(&space);
166
0
      zend_type_error("%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
167
0
    }
168
0
    return NULL;
169
0
  }
170
171
0
  return zend_fetch_resource2(Z_RES_P(res), resource_type_name, resource_type1, resource_type2);
172
0
}
173
174
void list_entry_destructor(zval *zv)
175
7.30k
{
176
7.30k
  zend_resource *res = Z_RES_P(zv);
177
178
7.30k
  ZVAL_UNDEF(zv);
179
7.30k
  if (res->type >= 0) {
180
6.24k
    zend_resource_dtor(res);
181
6.24k
  }
182
7.30k
  efree_size(res, sizeof(zend_resource));
183
7.30k
}
184
185
void plist_entry_destructor(zval *zv)
186
0
{
187
0
  zend_resource *res = Z_RES_P(zv);
188
189
0
  if (res->type >= 0) {
190
0
    zend_rsrc_list_dtors_entry *ld;
191
192
0
    ld = zend_hash_index_find_ptr(&list_destructors, res->type);
193
0
    ZEND_ASSERT(ld && "Unknown list entry type");
194
195
0
    if (ld->plist_dtor_ex) {
196
0
      ld->plist_dtor_ex(res);
197
0
    }
198
0
  }
199
0
  free(res);
200
0
}
201
202
ZEND_API void zend_init_rsrc_list(void)
203
247k
{
204
247k
  zend_hash_init(&EG(regular_list), 8, NULL, list_entry_destructor, 0);
205
247k
  EG(regular_list).nNextFreeElement = 0;
206
247k
}
207
208
209
void zend_init_rsrc_plist(void)
210
16
{
211
16
  zend_hash_init(&EG(persistent_list), 8, NULL, plist_entry_destructor, 1);
212
16
}
213
214
215
void zend_close_rsrc_list(HashTable *ht)
216
247k
{
217
  /* Reload ht->arData on each iteration, as it may be reallocated. */
218
247k
  uint32_t i = ht->nNumUsed;
219
220
248k
retry:
221
248k
  zend_try {
222
359k
    while (i-- > 0) {
223
110k
      zval *p = ZEND_HASH_ELEMENT(ht, i);
224
110k
      if (Z_TYPE_P(p) != IS_UNDEF) {
225
110k
        zend_resource *res = Z_PTR_P(p);
226
110k
        if (res->type >= 0) {
227
810
          zend_resource_dtor(res);
228
810
        }
229
110k
      }
230
110k
    }
231
248k
  } zend_catch {
232
    /* If we have bailed, we probably executed user code (e.g. user stream
233
     * API). Keep closing resources so they don't leak. User handlers must be
234
     * called now so they aren't called in zend_deactivate() on
235
     * zend_destroy_rsrc_list(&EG(regular_list)). At that point, the executor
236
     * has already shut down and the process would crash. */
237
0
    goto retry;
238
248k
  } zend_end_try();
239
248k
}
240
241
242
void zend_destroy_rsrc_list(HashTable *ht)
243
247k
{
244
247k
  zend_hash_graceful_reverse_destroy(ht);
245
247k
}
246
247
/* int return due to HashTable API */
248
static int clean_module_resource(zval *zv, void *arg)
249
0
{
250
0
  int resource_id = *(int *)arg;
251
252
0
  return Z_RES_TYPE_P(zv) == resource_id;
253
0
}
254
255
/* int return due to HashTable API */
256
static int zend_clean_module_rsrc_dtors_cb(zval *zv, void *arg)
257
0
{
258
0
  zend_rsrc_list_dtors_entry *ld = (zend_rsrc_list_dtors_entry *)Z_PTR_P(zv);
259
0
  int module_number = *(int *)arg;
260
0
  if (ld->module_number == module_number) {
261
0
    zend_hash_apply_with_argument(&EG(persistent_list), clean_module_resource, (void *) &(ld->resource_id));
262
0
    return 1;
263
0
  } else {
264
0
    return 0;
265
0
  }
266
0
}
267
268
269
void zend_clean_module_rsrc_dtors(int module_number)
270
0
{
271
0
  zend_hash_apply_with_argument(&list_destructors, zend_clean_module_rsrc_dtors_cb, (void *) &module_number);
272
0
}
273
274
275
ZEND_API int zend_register_list_destructors_ex(rsrc_dtor_func_t ld, rsrc_dtor_func_t pld, const char *type_name, int module_number)
276
144
{
277
144
  zend_rsrc_list_dtors_entry *lde;
278
144
  zval zv;
279
280
144
  lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
281
144
  lde->list_dtor_ex = ld;
282
144
  lde->plist_dtor_ex = pld;
283
144
  lde->module_number = module_number;
284
144
  lde->resource_id = list_destructors.nNextFreeElement;
285
144
  lde->type_name = type_name;
286
144
  ZVAL_PTR(&zv, lde);
287
288
144
  if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
289
0
    free(lde);
290
0
    return FAILURE;
291
0
  }
292
144
  return list_destructors.nNextFreeElement-1;
293
144
}
294
295
ZEND_API int zend_fetch_list_dtor_id(const char *type_name)
296
0
{
297
0
  zend_rsrc_list_dtors_entry *lde;
298
299
0
  ZEND_HASH_PACKED_FOREACH_PTR(&list_destructors, lde) {
300
0
    if (lde->type_name && (strcmp(type_name, lde->type_name) == 0)) {
301
0
      return lde->resource_id;
302
0
    }
303
0
  } ZEND_HASH_FOREACH_END();
304
305
0
  return 0;
306
0
}
307
308
static void list_destructors_dtor(zval *zv)
309
0
{
310
0
  free(Z_PTR_P(zv));
311
0
}
312
313
void zend_init_rsrc_list_dtors(void)
314
16
{
315
16
  zend_hash_init(&list_destructors, 64, NULL, list_destructors_dtor, 1);
316
16
  list_destructors.nNextFreeElement=1;  /* we don't want resource type 0 */
317
16
}
318
319
320
void zend_destroy_rsrc_list_dtors(void)
321
0
{
322
0
  zend_hash_destroy(&list_destructors);
323
0
}
324
325
326
const char *zend_rsrc_list_get_rsrc_type(zend_resource *res)
327
0
{
328
0
  zend_rsrc_list_dtors_entry *lde;
329
330
0
  lde = zend_hash_index_find_ptr(&list_destructors, res->type);
331
0
  if (lde) {
332
0
    return lde->type_name;
333
0
  } else {
334
0
    return NULL;
335
0
  }
336
0
}
337
338
ZEND_API zend_resource* zend_register_persistent_resource_ex(zend_string *key, void *rsrc_pointer, int rsrc_type)
339
0
{
340
0
  zval *zv;
341
0
  zval tmp;
342
343
0
  ZVAL_NEW_PERSISTENT_RES(&tmp, -1, rsrc_pointer, rsrc_type);
344
0
  GC_MAKE_PERSISTENT_LOCAL(Z_COUNTED(tmp));
345
0
  GC_MAKE_PERSISTENT_LOCAL(key);
346
347
0
  zv = zend_hash_update(&EG(persistent_list), key, &tmp);
348
349
0
  return Z_RES_P(zv);
350
0
}
351
352
ZEND_API zend_resource* zend_register_persistent_resource(const char *key, size_t key_len, void *rsrc_pointer, int rsrc_type)
353
0
{
354
0
  zend_string *str = zend_string_init(key, key_len, 1);
355
0
  zend_resource *ret  = zend_register_persistent_resource_ex(str, rsrc_pointer, rsrc_type);
356
357
0
  zend_string_release_ex(str, 1);
358
0
  return ret;
359
0
}