Coverage Report

Created: 2026-07-25 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_weakrefs.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Authors: krakjoe@php.net                                             |
12
   +----------------------------------------------------------------------+
13
*/
14
15
#include "zend.h"
16
#include "zend_interfaces.h"
17
#include "zend_objects_API.h"
18
#include "zend_types.h"
19
#include "zend_weakrefs.h"
20
#include "zend_weakrefs_arginfo.h"
21
22
typedef struct _zend_weakref {
23
  zend_object *referent;
24
  zend_object std;
25
} zend_weakref;
26
27
typedef struct _zend_weakmap {
28
  HashTable ht;
29
  zend_object std;
30
} zend_weakmap;
31
32
typedef struct _zend_weakmap_iterator {
33
  zend_object_iterator it;
34
  uint32_t ht_iter;
35
} zend_weakmap_iterator;
36
37
/* EG(weakrefs) is a map from a key corresponding to a zend_object pointer to all the WeakReference, WeakMap, and/or bare HashTable entries relating to that pointer.
38
 *
39
 * 1. For a single WeakReference,
40
 *    the HashTable's corresponding value's tag is a ZEND_WEAKREF_TAG_REF and the pointer is a singleton WeakReference instance (zend_weakref *) for that zend_object pointer (from WeakReference::create()).
41
 * 2. For a single WeakMap, the HashTable's corresponding value's tag is a ZEND_WEAKREF_TAG_MAP and the pointer is a WeakMap instance (zend_weakmap *).
42
 * 3. For a single bare HashTable, the HashTable's corresponding value's tag is a ZEND_WEAKREF_TAG_BARE_HT and the pointer is a HashTable*.
43
 * 4. For multiple values associated with the same zend_object pointer, the HashTable entry's tag is a ZEND_WEAKREF_TAG_HT with a HashTable mapping
44
 *    tagged pointers of at most 1 WeakReference and 1 or more WeakMap or bare HashTable to the same tagged pointer.
45
 *
46
 * ZEND_MM_ALIGNED_OFFSET_LOG2 is at least 2 on supported architectures (pointers to the objects in question are aligned to 4 bytes (1<<2) even on 32-bit systems),
47
 * i.e. the least two significant bits of the pointer can be used as a tag (ZEND_WEAKREF_TAG_*). */
48
785
#define ZEND_WEAKREF_TAG_REF     0
49
3.75k
#define ZEND_WEAKREF_TAG_MAP     1
50
4.67k
#define ZEND_WEAKREF_TAG_HT      2
51
1.46k
#define ZEND_WEAKREF_TAG_BARE_HT 3
52
4.89k
#define ZEND_WEAKREF_GET_TAG(p) (((uintptr_t) (p)) & 3)
53
4.78k
#define ZEND_WEAKREF_GET_PTR(p) ((void *) (((uintptr_t) (p)) & ~3))
54
2.11k
#define ZEND_WEAKREF_ENCODE(p, t) ((void *) (((uintptr_t) (p)) | (t)))
55
56
zend_class_entry *zend_ce_weakref;
57
static zend_class_entry *zend_ce_weakmap;
58
static zend_object_handlers zend_weakref_handlers;
59
static zend_object_handlers zend_weakmap_handlers;
60
61
1.25k
#define zend_weakref_from(o) (ZEND_CONTAINER_OF(o, zend_weakref, std))
62
781
#define zend_weakref_fetch(z) zend_weakref_from(Z_OBJ_P(z))
63
64
4.07k
#define zend_weakmap_from(o) (ZEND_CONTAINER_OF(o, zend_weakmap, std))
65
1.16k
#define zend_weakmap_fetch(z) zend_weakmap_from(Z_OBJ_P(z))
66
67
static inline void zend_weakref_unref_single(
68
    void *ptr, uintptr_t tag, zend_object *object)
69
720
{
70
720
  if (tag == ZEND_WEAKREF_TAG_REF) {
71
    /* Unreferencing WeakReference (at ptr) singleton that pointed to object. */
72
423
    zend_weakref *wr = ptr;
73
423
    wr->referent = NULL;
74
423
  } else {
75
    /* unreferencing WeakMap or bare HashTable entry (at ptr) with a key of object. */
76
297
    ZEND_ASSERT(tag == ZEND_WEAKREF_TAG_MAP || tag == ZEND_WEAKREF_TAG_BARE_HT);
77
297
    zend_hash_index_del((HashTable *) ptr, zend_object_to_weakref_key(object));
78
297
  }
79
720
}
80
81
575
static void zend_weakref_unref(zend_object *object, void *tagged_ptr) {
82
575
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
83
575
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
84
575
  if (tag == ZEND_WEAKREF_TAG_HT) {
85
28
    HashTable *ht = ptr;
86
195
    ZEND_HASH_MAP_FOREACH_PTR(ht, tagged_ptr) {
87
195
      zend_weakref_unref_single(
88
195
        ZEND_WEAKREF_GET_PTR(tagged_ptr), ZEND_WEAKREF_GET_TAG(tagged_ptr), object);
89
195
    } ZEND_HASH_FOREACH_END();
90
28
    zend_hash_destroy(ht);
91
28
    FREE_HASHTABLE(ht);
92
547
  } else {
93
547
    zend_weakref_unref_single(ptr, tag, object);
94
547
  }
95
575
}
96
97
1.36k
static void zend_weakref_register(zend_object *object, void *payload) {
98
1.36k
  GC_ADD_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
99
100
1.36k
  zend_ulong obj_key = zend_object_to_weakref_key(object);
101
1.36k
  zval *zv = zend_hash_index_lookup(&EG(weakrefs), obj_key);
102
1.36k
  if (Z_TYPE_P(zv) == IS_NULL) {
103
1.25k
    ZVAL_PTR(zv, payload);
104
1.25k
    return;
105
1.25k
  }
106
107
111
  void *tagged_ptr = Z_PTR_P(zv);
108
111
  if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_HT) {
109
57
    HashTable *ht = ZEND_WEAKREF_GET_PTR(tagged_ptr);
110
57
    zend_hash_index_add_new_ptr(ht, (zend_ulong)(uintptr_t) payload, payload);
111
57
    return;
112
57
  }
113
114
  /* Convert simple pointer to hashtable. */
115
54
  HashTable *ht = emalloc(sizeof(HashTable));
116
54
  zend_hash_init(ht, 0, NULL, NULL, 0);
117
54
  zend_hash_index_add_new_ptr(ht, (zend_ulong)(uintptr_t) tagged_ptr, tagged_ptr);
118
54
  zend_hash_index_add_new_ptr(ht, (zend_ulong)(uintptr_t) payload, payload);
119
  /* Replace the single WeakMap or WeakReference entry in EG(weakrefs) with a HashTable with 2 entries in place. */
120
54
  ZVAL_PTR(zv, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_HT));
121
54
}
122
123
755
static void zend_weakref_unregister(zend_object *object, void *payload, bool weakref_free) {
124
755
  zend_ulong obj_key = zend_object_to_weakref_key(object);
125
755
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
126
755
  ZEND_ASSERT(tagged_ptr && "Weakref not registered?");
127
128
755
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
129
755
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
130
755
  if (tag != ZEND_WEAKREF_TAG_HT) {
131
651
    ZEND_ASSERT(tagged_ptr == payload);
132
651
    zend_hash_index_del(&EG(weakrefs), obj_key);
133
651
    GC_DEL_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
134
135
    /* Do this last, as it may destroy the object. */
136
651
    if (weakref_free) {
137
83
      zend_weakref_unref_single(ptr, tag, object);
138
568
    } else {
139
      /* The optimization of skipping unref is used for zend_weakrefs_hash_clean_ex() */
140
568
      ZEND_ASSERT(ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_MAP || ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_BARE_HT);
141
568
    }
142
651
    return;
143
651
  }
144
145
104
  HashTable *ht = ptr;
146
104
#if ZEND_DEBUG
147
104
  void *old_payload = zend_hash_index_find_ptr(ht, (zend_ulong)(uintptr_t) payload);
148
104
  ZEND_ASSERT(old_payload && "Weakref not registered?");
149
104
  ZEND_ASSERT(old_payload == payload);
150
104
#endif
151
104
  zend_hash_index_del(ht, (zend_ulong)(uintptr_t) payload);
152
104
  if (zend_hash_num_elements(ht) == 0) {
153
26
    GC_DEL_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
154
26
    zend_hash_destroy(ht);
155
26
    FREE_HASHTABLE(ht);
156
26
    zend_hash_index_del(&EG(weakrefs), obj_key);
157
26
  }
158
159
  /* Do this last, as it may destroy the object. */
160
104
  if (weakref_free)  {
161
29
    zend_weakref_unref_single(
162
29
      ZEND_WEAKREF_GET_PTR(payload), ZEND_WEAKREF_GET_TAG(payload), object);
163
75
  } else {
164
    /* The optimization of skipping unref is used for zend_weakrefs_hash_clean_ex() */
165
75
    ZEND_ASSERT(ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_MAP || ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_BARE_HT);
166
75
  }
167
104
}
168
169
/* Insert 'pData' into bare HashTable 'ht', with the given 'key'. 'key' is
170
 * weakly referenced. 'ht' is considered to be a bare HashTable, not a WeakMap. */
171
0
ZEND_API zval *zend_weakrefs_hash_add(HashTable *ht, zend_object *key, zval *pData) {
172
0
  zval *zv = zend_hash_index_add(ht, zend_object_to_weakref_key(key), pData);
173
0
  if (zv) {
174
0
    zend_weakref_register(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_BARE_HT));
175
0
  }
176
0
  return zv;
177
0
}
178
179
0
ZEND_API zend_result zend_weakrefs_hash_del(HashTable *ht, zend_object *key) {
180
0
  zval *zv = zend_hash_index_find(ht, zend_object_to_weakref_key(key));
181
0
  if (zv) {
182
0
    zend_weakref_unregister(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_BARE_HT), true);
183
0
    return SUCCESS;
184
0
  }
185
0
  return FAILURE;
186
0
}
187
188
523
static void zend_weakrefs_hash_clean_ex(HashTable *ht, int type) {
189
523
  zend_ulong obj_key;
190
2.40k
  ZEND_HASH_MAP_FOREACH_NUM_KEY(ht, obj_key) {
191
    /* Optimization: Don't call zend_weakref_unref_single to free individual entries from ht when unregistering (which would do a hash table lookup, call zend_hash_index_del, and skip over any bucket collisions).
192
     * Let freeing the corresponding values for WeakMap entries be done in zend_hash_clean, freeing objects sequentially.
193
     * The performance difference is notable for larger WeakMaps with worse cache locality. */
194
2.40k
    zend_weakref_unregister(
195
2.40k
      zend_weakref_key_to_object(obj_key), ZEND_WEAKREF_ENCODE(ht, type), false);
196
2.40k
  } ZEND_HASH_FOREACH_END();
197
523
  zend_hash_clean(ht);
198
523
}
199
200
0
ZEND_API void zend_weakrefs_hash_clean(HashTable *ht) {
201
0
  zend_weakrefs_hash_clean_ex(ht, ZEND_WEAKREF_TAG_BARE_HT);
202
0
}
203
204
300k
void zend_weakrefs_init(void) {
205
300k
  zend_hash_init(&EG(weakrefs), 8, NULL, NULL, 0);
206
300k
}
207
208
/* This is called when the object is garbage collected
209
 * to remove all WeakReference and WeakMap entries weakly referencing that object. */
210
575
void zend_weakrefs_notify(zend_object *object) {
211
  /* Annoyingly we can't use the HT destructor here, because we need access to the key (which
212
   * is the object address), which is not provided to the dtor. */
213
575
  const zend_ulong obj_key = zend_object_to_weakref_key(object);
214
575
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
215
575
#if ZEND_DEBUG
216
575
  ZEND_ASSERT(tagged_ptr && "Tracking of the IS_OBJ_WEAKLY_REFERENCE flag should be precise");
217
575
#endif
218
575
  if (tagged_ptr) {
219
575
    zend_weakref_unref(object, tagged_ptr);
220
575
    zend_hash_index_del(&EG(weakrefs), obj_key);
221
575
  }
222
575
}
223
224
300k
void zend_weakrefs_shutdown(void) {
225
300k
  zend_hash_destroy(&EG(weakrefs));
226
300k
}
227
228
432
static zend_object* zend_weakref_new(zend_class_entry *ce) {
229
432
  zend_weakref *wr = zend_object_alloc(sizeof(zend_weakref), zend_ce_weakref);
230
231
432
  zend_object_std_init(&wr->std, zend_ce_weakref);
232
432
  return &wr->std;
233
432
}
234
235
451
static zend_always_inline bool zend_weakref_find(zend_object *referent, zval *return_value) {
236
451
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), zend_object_to_weakref_key(referent));
237
451
  if (!tagged_ptr) {
238
409
    return false;
239
409
  }
240
241
42
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
242
42
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
243
42
  if (tag == ZEND_WEAKREF_TAG_REF) {
244
11
    zend_weakref *wr;
245
28
found_weakref:
246
28
    wr = ptr;
247
28
    RETVAL_OBJ_COPY(&wr->std);
248
28
    return true;
249
11
  }
250
251
31
  if (tag == ZEND_WEAKREF_TAG_HT) {
252
84
    ZEND_HASH_MAP_FOREACH_PTR(ptr, tagged_ptr) {
253
84
      if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_REF) {
254
17
        ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
255
17
        goto found_weakref;
256
17
      }
257
84
    } ZEND_HASH_FOREACH_END();
258
19
  }
259
260
14
  return false;
261
31
}
262
263
423
static zend_always_inline void zend_weakref_create(zend_object *referent, zval *return_value) {
264
423
  zend_weakref *wr;
265
266
423
  object_init_ex(return_value, zend_ce_weakref);
267
268
423
  wr = zend_weakref_fetch(return_value);
269
423
  wr->referent = referent;
270
271
423
  zend_weakref_register(wr->referent, ZEND_WEAKREF_ENCODE(wr, ZEND_WEAKREF_TAG_REF));
272
423
}
273
274
358
static zend_always_inline void zend_weakref_get(zval *weakref, zval *return_value) {
275
358
  zend_weakref *wr = zend_weakref_fetch(weakref);
276
277
358
  if (wr->referent) {
278
173
    RETVAL_OBJ_COPY(wr->referent);
279
173
  }
280
358
}
281
282
432
static void zend_weakref_free(zend_object *zo) {
283
432
  zend_weakref *wr = zend_weakref_from(zo);
284
285
432
  if (wr->referent) {
286
97
    zend_weakref_unregister(wr->referent, ZEND_WEAKREF_ENCODE(wr, ZEND_WEAKREF_TAG_REF), true);
287
97
  }
288
289
432
  zend_object_std_dtor(&wr->std);
290
432
}
291
292
static HashTable *zend_weakref_get_debug_info(zend_object *object, int *is_temp)
293
45
{
294
45
  *is_temp = 1;
295
296
45
  HashTable *ht = zend_new_array(1);
297
298
45
  zend_object *referent = zend_weakref_from(object)->referent;
299
45
  zval value;
300
45
  if (referent) {
301
36
    ZVAL_OBJ_COPY(&value, referent);
302
36
  } else {
303
9
    ZVAL_NULL(&value);
304
9
  }
305
306
45
  zend_hash_update(ht, ZSTR_KNOWN(ZEND_STR_OBJECT), &value);
307
308
45
  return ht;
309
45
}
310
311
ZEND_COLD ZEND_METHOD(WeakReference, __construct)
312
9
{
313
9
  zend_throw_error(NULL, "Direct instantiation of WeakReference is not allowed, use WeakReference::create instead");
314
9
}
315
316
ZEND_METHOD(WeakReference, create)
317
457
{
318
457
  zend_object *referent;
319
320
1.37k
  ZEND_PARSE_PARAMETERS_START(1,1)
321
1.82k
    Z_PARAM_OBJ(referent)
322
457
  ZEND_PARSE_PARAMETERS_END();
323
324
451
  if (zend_weakref_find(referent, return_value)) {
325
28
      return;
326
28
  }
327
328
423
  zend_weakref_create(referent, return_value);
329
423
}
330
331
ZEND_METHOD(WeakReference, get)
332
361
{
333
361
  ZEND_PARSE_PARAMETERS_NONE();
334
335
358
  zend_weakref_get(ZEND_THIS, return_value);
336
358
}
337
338
static zend_object *zend_weakmap_create_object(zend_class_entry *ce)
339
523
{
340
523
  zend_weakmap *wm = zend_object_alloc(sizeof(zend_weakmap), ce);
341
523
  zend_object_std_init(&wm->std, ce);
342
343
523
  zend_hash_init(&wm->ht, 0, NULL, ZVAL_PTR_DTOR, 0);
344
523
  return &wm->std;
345
523
}
346
347
static void zend_weakmap_free_obj(zend_object *object)
348
523
{
349
523
  zend_weakmap *wm = zend_weakmap_from(object);
350
523
  zend_weakrefs_hash_clean_ex(&wm->ht, ZEND_WEAKREF_TAG_MAP);
351
523
  zend_hash_destroy(&wm->ht);
352
523
  zend_object_std_dtor(&wm->std);
353
523
}
354
355
static zval *zend_weakmap_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
356
166
{
357
166
  if (offset == NULL) {
358
5
    zend_throw_error(NULL, "Cannot append to WeakMap");
359
5
    return NULL;
360
5
  }
361
362
161
  ZVAL_DEREF(offset);
363
161
  if (Z_TYPE_P(offset) != IS_OBJECT) {
364
10
    zend_type_error("WeakMap key must be an object");
365
10
    return NULL;
366
10
  }
367
368
151
  zend_weakmap *wm = zend_weakmap_from(object);
369
151
  zend_object *obj_addr = Z_OBJ_P(offset);
370
151
  zval *zv = zend_hash_index_find(&wm->ht, zend_object_to_weakref_key(obj_addr));
371
151
  if (type == BP_VAR_W || type == BP_VAR_RW) {
372
30
    if (zv == NULL) {
373
4
      zval value;
374
4
      zend_weakref_register(obj_addr, ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP));
375
4
      ZVAL_NULL(&value);
376
4
      zv = zend_hash_index_add_new(&wm->ht, zend_object_to_weakref_key(obj_addr), &value);
377
4
    }
378
30
    ZVAL_MAKE_REF(zv);
379
121
  } else {
380
121
    if (zv == NULL) {
381
15
      if (type != BP_VAR_IS) {
382
15
        zend_throw_error(NULL,
383
15
          "Object %s#%d not contained in WeakMap", ZSTR_VAL(obj_addr->ce->name), obj_addr->handle);
384
15
        return NULL;
385
15
      }
386
0
      return NULL;
387
15
    }
388
121
  }
389
390
136
  return zv;
391
151
}
392
393
static void zend_weakmap_write_dimension(zend_object *object, zval *offset, zval *value)
394
1.01k
{
395
1.01k
  if (offset == NULL) {
396
5
    zend_throw_error(NULL, "Cannot append to WeakMap");
397
5
    return;
398
5
  }
399
400
1.00k
  ZVAL_DEREF(offset);
401
1.00k
  if (Z_TYPE_P(offset) != IS_OBJECT) {
402
25
    zend_type_error("WeakMap key must be an object");
403
25
    return;
404
25
  }
405
406
982
  zend_weakmap *wm = zend_weakmap_from(object);
407
982
  zend_object *obj_addr = Z_OBJ_P(offset);
408
982
  zend_ulong obj_key = zend_object_to_weakref_key(obj_addr);
409
982
  Z_TRY_ADDREF_P(value);
410
411
982
  zval *zv = zend_hash_index_find(&wm->ht, obj_key);
412
982
  if (zv) {
413
    /* Because the destructors can have side effects such as resizing or rehashing the WeakMap storage,
414
     * free the zval only after overwriting the original value. */
415
73
    zval zv_orig;
416
73
    ZVAL_COPY_VALUE(&zv_orig, zv);
417
73
    ZVAL_COPY_VALUE(zv, value);
418
73
    zval_ptr_dtor(&zv_orig);
419
73
    return;
420
73
  }
421
422
909
  zend_weakref_register(obj_addr, ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP));
423
909
  zend_hash_index_add_new(&wm->ht, obj_key, value);
424
909
}
425
426
// todo: make zend_weakmap_has_dimension return bool as well
427
/* int return and check_empty due to Object Handler API */
428
static int zend_weakmap_has_dimension(zend_object *object, zval *offset, int check_empty)
429
175
{
430
175
  ZVAL_DEREF(offset);
431
175
  if (Z_TYPE_P(offset) != IS_OBJECT) {
432
12
    zend_type_error("WeakMap key must be an object");
433
12
    return 0;
434
12
  }
435
436
163
  zend_weakmap *wm = zend_weakmap_from(object);
437
163
  zval *zv = zend_hash_index_find(&wm->ht, zend_object_to_weakref_key(Z_OBJ_P(offset)));
438
163
  if (!zv) {
439
20
    return 0;
440
20
  }
441
442
143
  if (check_empty) {
443
69
    return i_zend_is_true(zv);
444
69
  }
445
74
  return Z_TYPE_P(zv) != IS_NULL;
446
143
}
447
448
static void zend_weakmap_unset_dimension(zend_object *object, zval *offset)
449
30
{
450
30
  ZVAL_DEREF(offset);
451
30
  if (Z_TYPE_P(offset) != IS_OBJECT) {
452
5
    zend_type_error("WeakMap key must be an object");
453
5
    return;
454
5
  }
455
456
25
  zend_weakmap *wm = zend_weakmap_from(object);
457
25
  zend_object *obj_addr = Z_OBJ_P(offset);
458
25
  if (!zend_hash_index_exists(&wm->ht, zend_object_to_weakref_key(obj_addr))) {
459
    /* Object not in WeakMap, do nothing. */
460
10
    return;
461
10
  }
462
463
15
  zend_weakref_unregister(obj_addr, ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP), true);
464
15
}
465
466
static zend_result zend_weakmap_count_elements(zend_object *object, zend_long *count)
467
89
{
468
89
  zend_weakmap *wm = zend_weakmap_from(object);
469
89
  *count = zend_hash_num_elements(&wm->ht);
470
89
  return SUCCESS;
471
89
}
472
473
static HashTable *zend_weakmap_get_properties_for(zend_object *object, zend_prop_purpose purpose)
474
371
{
475
371
  if (purpose != ZEND_PROP_PURPOSE_DEBUG) {
476
0
    return NULL;
477
0
  }
478
479
371
  zend_weakmap *wm = zend_weakmap_from(object);
480
371
  HashTable *ht;
481
371
  ALLOC_HASHTABLE(ht);
482
371
  zend_hash_init(ht, zend_hash_num_elements(&wm->ht), NULL, ZVAL_PTR_DTOR, 0);
483
484
371
  zend_ulong obj_key;
485
371
  zval *val;
486
1.49k
  ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&wm->ht, obj_key, val) {
487
1.49k
    zend_object *obj = zend_weakref_key_to_object(obj_key);
488
1.49k
    zval pair;
489
1.49k
    array_init(&pair);
490
491
1.49k
    GC_ADDREF(obj);
492
1.49k
    add_assoc_object(&pair, "key", obj);
493
1.49k
    Z_TRY_ADDREF_P(val);
494
1.49k
    add_assoc_zval(&pair, "value", val);
495
496
1.49k
    zend_hash_next_index_insert_new(ht, &pair);
497
1.49k
  } ZEND_HASH_FOREACH_END();
498
499
371
  return ht;
500
371
}
501
502
HashTable *zend_weakmap_get_gc(zend_object *object, zval **table, int *n)
503
62
{
504
62
  zend_weakmap *wm = zend_weakmap_from(object);
505
62
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
506
62
  zval *val;
507
281
  ZEND_HASH_MAP_FOREACH_VAL(&wm->ht, val) {
508
281
    zend_get_gc_buffer_add_zval(gc_buffer, val);
509
281
  } ZEND_HASH_FOREACH_END();
510
62
  zend_get_gc_buffer_use(gc_buffer, table, n);
511
62
  return NULL;
512
62
}
513
514
HashTable *zend_weakmap_get_key_entry_gc(zend_object *object, zval **table, int *n)
515
182
{
516
182
  zend_weakmap *wm = zend_weakmap_from(object);
517
182
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
518
182
  zend_ulong h;
519
182
  zval *val;
520
964
  ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&wm->ht, h, val) {
521
964
    zend_object *key = zend_weakref_key_to_object(h);
522
964
    zend_get_gc_buffer_add_obj(gc_buffer, key);
523
964
    zend_get_gc_buffer_add_ptr(gc_buffer, val);
524
964
  } ZEND_HASH_FOREACH_END();
525
182
  zend_get_gc_buffer_use(gc_buffer, table, n);
526
182
  return NULL;
527
182
}
528
529
HashTable *zend_weakmap_get_entry_gc(zend_object *object, zval **table, int *n)
530
306
{
531
306
  zend_weakmap *wm = zend_weakmap_from(object);
532
306
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
533
306
  zval *val;
534
1.52k
  ZEND_HASH_MAP_FOREACH_VAL(&wm->ht, val) {
535
1.52k
    zend_get_gc_buffer_add_ptr(gc_buffer, val);
536
1.52k
  } ZEND_HASH_FOREACH_END();
537
306
  zend_get_gc_buffer_use(gc_buffer, table, n);
538
306
  return NULL;
539
306
}
540
541
HashTable *zend_weakmap_get_object_key_entry_gc(zend_object *object, zval **table, int *n)
542
2.07k
{
543
2.07k
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
544
2.07k
  const zend_ulong obj_key = zend_object_to_weakref_key(object);
545
2.07k
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
546
2.07k
#if ZEND_DEBUG
547
2.07k
  ZEND_ASSERT(tagged_ptr && "Tracking of the IS_OBJ_WEAKLY_REFERENCE flag should be precise");
548
2.07k
#endif
549
2.07k
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
550
2.07k
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
551
552
2.07k
  if (tag == ZEND_WEAKREF_TAG_HT) {
553
20
    HashTable *ht = ptr;
554
122
    ZEND_HASH_MAP_FOREACH_PTR(ht, tagged_ptr) {
555
122
      if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_MAP) {
556
28
        zend_weakmap *wm = (zend_weakmap*) ZEND_WEAKREF_GET_PTR(tagged_ptr);
557
28
        zval *zv = zend_hash_index_find(&wm->ht, obj_key);
558
28
        ZEND_ASSERT(zv);
559
28
        zend_get_gc_buffer_add_ptr(gc_buffer, zv);
560
28
        zend_get_gc_buffer_add_obj(gc_buffer, &wm->std);
561
28
      } else if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_BARE_HT) {
562
        /* Bare HashTables are intentionally ignored, since they are
563
         * intended for internal usage by extensions and might not be
564
         * collectable. */
565
0
      }
566
122
    } ZEND_HASH_FOREACH_END();
567
2.05k
  } else if (tag == ZEND_WEAKREF_TAG_MAP) {
568
1.43k
    zend_weakmap *wm = (zend_weakmap*) ptr;
569
1.43k
    zval *zv = zend_hash_index_find(&wm->ht, obj_key);
570
1.43k
    ZEND_ASSERT(zv);
571
1.43k
    zend_get_gc_buffer_add_ptr(gc_buffer, zv);
572
1.43k
    zend_get_gc_buffer_add_obj(gc_buffer, &wm->std);
573
1.43k
  } else if (tag == ZEND_WEAKREF_TAG_BARE_HT) {
574
    /* Bare HashTables are intentionally ignored (see above) */
575
0
  }
576
577
2.07k
  zend_get_gc_buffer_use(gc_buffer, table, n);
578
579
2.07k
  return NULL;
580
2.07k
}
581
582
HashTable *zend_weakmap_get_object_entry_gc(zend_object *object, zval **table, int *n)
583
1.12k
{
584
1.12k
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
585
1.12k
  const zend_ulong obj_key = zend_object_to_weakref_key(object);
586
1.12k
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
587
1.12k
#if ZEND_DEBUG
588
1.12k
  ZEND_ASSERT(tagged_ptr && "Tracking of the IS_OBJ_WEAKLY_REFERENCE flag should be precise");
589
1.12k
#endif
590
1.12k
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
591
1.12k
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
592
593
1.12k
  if (tag == ZEND_WEAKREF_TAG_HT) {
594
24
    HashTable *ht = ptr;
595
116
    ZEND_HASH_MAP_FOREACH_PTR(ht, tagged_ptr) {
596
116
      if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_MAP) {
597
24
        zend_weakmap *wm = (zend_weakmap*) ZEND_WEAKREF_GET_PTR(tagged_ptr);
598
24
        zval *zv = zend_hash_index_find(&wm->ht, obj_key);
599
24
        ZEND_ASSERT(zv);
600
24
        zend_get_gc_buffer_add_ptr(gc_buffer, zv);
601
24
      } else if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_BARE_HT) {
602
        /* Bare HashTables are intentionally ignored
603
         * (see zend_weakmap_get_object_key_entry_gc) */
604
0
      }
605
116
    } ZEND_HASH_FOREACH_END();
606
1.10k
  } else if (tag == ZEND_WEAKREF_TAG_MAP) {
607
272
    zend_weakmap *wm = (zend_weakmap*) ptr;
608
272
    zval *zv = zend_hash_index_find(&wm->ht, obj_key);
609
272
    ZEND_ASSERT(zv);
610
272
    zend_get_gc_buffer_add_ptr(gc_buffer, zv);
611
832
  } else if (tag == ZEND_WEAKREF_TAG_BARE_HT) {
612
    /* Bare HashTables are intentionally ignored
613
     * (see zend_weakmap_get_object_key_entry_gc) */
614
0
  }
615
616
1.12k
  zend_get_gc_buffer_use(gc_buffer, table, n);
617
618
1.12k
  return NULL;
619
1.12k
}
620
621
static zend_object *zend_weakmap_clone_obj(zend_object *old_object)
622
29
{
623
29
  zend_object *new_object = zend_weakmap_create_object(zend_ce_weakmap);
624
29
  zend_weakmap *old_wm = zend_weakmap_from(old_object);
625
29
  zend_weakmap *new_wm = zend_weakmap_from(new_object);
626
29
  zend_hash_copy(&new_wm->ht, &old_wm->ht, NULL);
627
628
29
  zend_ulong obj_key;
629
29
  zval *val;
630
112
  ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&new_wm->ht, obj_key, val) {
631
112
    zend_weakref_register(
632
112
      zend_weakref_key_to_object(obj_key), ZEND_WEAKREF_ENCODE(new_wm, ZEND_WEAKREF_TAG_MAP));
633
112
    zval_add_ref(val);
634
112
  } ZEND_HASH_FOREACH_END();
635
29
  return new_object;
636
29
}
637
638
1.04k
static HashPosition *zend_weakmap_iterator_get_pos_ptr(zend_weakmap_iterator *iter) {
639
1.04k
  ZEND_ASSERT(iter->ht_iter != (uint32_t) -1);
640
1.04k
  return &EG(ht_iterators)[iter->ht_iter].pos;
641
1.04k
}
642
643
static void zend_weakmap_iterator_dtor(zend_object_iterator *obj_iter)
644
121
{
645
121
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
646
121
  zend_hash_iterator_del(iter->ht_iter);
647
121
  zval_ptr_dtor(&iter->it.data);
648
121
}
649
650
static zend_result zend_weakmap_iterator_valid(zend_object_iterator *obj_iter)
651
306
{
652
306
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
653
306
  zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
654
306
  HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
655
306
  return zend_hash_has_more_elements_ex(&wm->ht, pos);
656
306
}
657
658
static zval *zend_weakmap_iterator_get_current_data(zend_object_iterator *obj_iter)
659
213
{
660
213
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
661
213
  zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
662
213
  HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
663
213
  return zend_hash_get_current_data_ex(&wm->ht, pos);
664
213
}
665
666
static void zend_weakmap_iterator_get_current_key(zend_object_iterator *obj_iter, zval *key)
667
213
{
668
213
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
669
213
  zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
670
213
  HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
671
672
213
  zend_string *string_key;
673
213
  zend_ulong num_key;
674
213
  zend_hash_key_type key_type = zend_hash_get_current_key_ex(&wm->ht, &string_key, &num_key, pos);
675
213
  if (key_type == HASH_KEY_NON_EXISTENT) {
676
16
    ZVAL_NULL(key);
677
16
    return;
678
16
  }
679
197
  if (key_type != HASH_KEY_IS_LONG) {
680
0
    ZEND_ASSERT(0 && "Must have integer key");
681
0
  }
682
683
197
  ZVAL_OBJ_COPY(key, zend_weakref_key_to_object(num_key));
684
197
}
685
686
static void zend_weakmap_iterator_move_forward(zend_object_iterator *obj_iter)
687
194
{
688
194
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
689
194
  zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
690
194
  HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
691
194
  zend_hash_move_forward_ex(&wm->ht, pos);
692
194
}
693
694
static void zend_weakmap_iterator_rewind(zend_object_iterator *obj_iter)
695
115
{
696
115
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
697
115
  zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
698
115
  HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
699
115
  zend_hash_internal_pointer_reset_ex(&wm->ht, pos);
700
115
}
701
702
static const zend_object_iterator_funcs zend_weakmap_iterator_funcs = {
703
  zend_weakmap_iterator_dtor,
704
  zend_weakmap_iterator_valid,
705
  zend_weakmap_iterator_get_current_data,
706
  zend_weakmap_iterator_get_current_key,
707
  zend_weakmap_iterator_move_forward,
708
  zend_weakmap_iterator_rewind,
709
  NULL,
710
  NULL, /* get_gc */
711
};
712
713
/* by_ref is int due to Iterator API */
714
static zend_object_iterator *zend_weakmap_get_iterator(
715
    zend_class_entry *ce, zval *object, int by_ref)
716
121
{
717
121
  zend_weakmap *wm = zend_weakmap_fetch(object);
718
121
  zend_weakmap_iterator *iter = emalloc(sizeof(zend_weakmap_iterator));
719
121
  zend_iterator_init(&iter->it);
720
121
  iter->it.funcs = &zend_weakmap_iterator_funcs;
721
121
  ZVAL_COPY(&iter->it.data, object);
722
121
  iter->ht_iter = zend_hash_iterator_add(&wm->ht, 0);
723
121
  return &iter->it;
724
121
}
725
726
ZEND_METHOD(WeakMap, offsetGet)
727
13
{
728
13
  zval *key;
729
730
13
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
731
0
    RETURN_THROWS();
732
0
  }
733
734
13
  zval *zv = zend_weakmap_read_dimension(Z_OBJ_P(ZEND_THIS), key, BP_VAR_R, NULL);
735
13
  if (!zv) {
736
0
    RETURN_THROWS();
737
0
  }
738
739
13
  RETURN_COPY_DEREF(zv);
740
13
}
741
742
ZEND_METHOD(WeakMap, offsetSet)
743
7
{
744
7
  zval *key, *value;
745
746
7
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &key, &value) == FAILURE) {
747
0
    RETURN_THROWS();
748
0
  }
749
750
7
  zend_weakmap_write_dimension(Z_OBJ_P(ZEND_THIS), key, value);
751
7
}
752
753
ZEND_METHOD(WeakMap, offsetExists)
754
7
{
755
7
  zval *key;
756
757
7
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
758
0
    RETURN_THROWS();
759
0
  }
760
761
7
  RETURN_BOOL(zend_weakmap_has_dimension(Z_OBJ_P(ZEND_THIS), key, /* check_empty */ 0));
762
7
}
763
764
ZEND_METHOD(WeakMap, offsetUnset)
765
5
{
766
5
  zval *key;
767
768
5
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
769
0
    RETURN_THROWS();
770
0
  }
771
772
5
  zend_weakmap_unset_dimension(Z_OBJ_P(ZEND_THIS), key);
773
5
}
774
775
ZEND_METHOD(WeakMap, count)
776
12
{
777
12
  ZEND_PARSE_PARAMETERS_NONE();
778
779
12
  zend_long count;
780
12
  zend_weakmap_count_elements(Z_OBJ_P(ZEND_THIS), &count);
781
12
  RETURN_LONG(count);
782
12
}
783
784
ZEND_METHOD(WeakMap, getIterator)
785
22
{
786
22
  ZEND_PARSE_PARAMETERS_NONE();
787
788
22
  zend_create_internal_iterator_zval(return_value, ZEND_THIS);
789
22
}
790
791
void zend_register_weakref_ce(void) /* {{{ */
792
16
{
793
16
  zend_ce_weakref = register_class_WeakReference();
794
795
16
  zend_ce_weakref->create_object = zend_weakref_new;
796
16
  zend_ce_weakref->default_object_handlers = &zend_weakref_handlers;
797
798
16
  memcpy(&zend_weakref_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
799
16
  zend_weakref_handlers.offset = offsetof(zend_weakref, std);
800
801
16
  zend_weakref_handlers.free_obj = zend_weakref_free;
802
16
  zend_weakref_handlers.get_debug_info = zend_weakref_get_debug_info;
803
16
  zend_weakref_handlers.clone_obj = NULL;
804
805
16
  zend_ce_weakmap = register_class_WeakMap(zend_ce_arrayaccess, zend_ce_countable, zend_ce_aggregate);
806
807
16
  zend_ce_weakmap->create_object = zend_weakmap_create_object;
808
16
  zend_ce_weakmap->get_iterator = zend_weakmap_get_iterator;
809
16
  zend_ce_weakmap->default_object_handlers = &zend_weakmap_handlers;
810
811
16
  memcpy(&zend_weakmap_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
812
  zend_weakmap_handlers.offset = offsetof(zend_weakmap, std);
813
16
  zend_weakmap_handlers.free_obj = zend_weakmap_free_obj;
814
16
  zend_weakmap_handlers.read_dimension = zend_weakmap_read_dimension;
815
16
  zend_weakmap_handlers.write_dimension = zend_weakmap_write_dimension;
816
16
  zend_weakmap_handlers.has_dimension = zend_weakmap_has_dimension;
817
16
  zend_weakmap_handlers.unset_dimension = zend_weakmap_unset_dimension;
818
16
  zend_weakmap_handlers.count_elements = zend_weakmap_count_elements;
819
16
  zend_weakmap_handlers.get_properties_for = zend_weakmap_get_properties_for;
820
16
  zend_weakmap_handlers.get_gc = zend_weakmap_get_gc;
821
16
  zend_weakmap_handlers.clone_obj = zend_weakmap_clone_obj;
822
16
}
823
/* }}} */
824