Coverage Report

Created: 2025-07-23 06:33

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