Coverage Report

Created: 2025-06-13 06:43

/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
495
#define ZEND_WEAKREF_TAG_REF 0
50
2.49k
#define ZEND_WEAKREF_TAG_MAP 1
51
3.70k
#define ZEND_WEAKREF_TAG_HT  2
52
3.87k
#define ZEND_WEAKREF_GET_TAG(p) (((uintptr_t) (p)) & 3)
53
3.79k
#define ZEND_WEAKREF_GET_PTR(p) ((void *) (((uintptr_t) (p)) & ~3))
54
1.94k
#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
839
#define zend_weakref_from(o) ((zend_weakref*)(((char*) o) - XtOffsetOf(zend_weakref, std)))
62
529
#define zend_weakref_fetch(z) zend_weakref_from(Z_OBJ_P(z))
63
64
2.79k
#define zend_weakmap_from(o) ((zend_weakmap*)(((char*) o) - XtOffsetOf(zend_weakmap, std)))
65
305
#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
460
{
70
460
  if (tag == ZEND_WEAKREF_TAG_REF) {
71
    /* Unreferencing WeakReference (at ptr) singleton that pointed to object. */
72
268
    zend_weakref *wr = ptr;
73
268
    wr->referent = NULL;
74
268
  } else {
75
    /* unreferencing WeakMap entry (at ptr) with a key of object. */
76
192
    ZEND_ASSERT(tag == ZEND_WEAKREF_TAG_MAP);
77
192
    zend_hash_index_del((HashTable *) ptr, zend_object_to_weakref_key(object));
78
192
  }
79
460
}
80
81
355
static void zend_weakref_unref(zend_object *object, void *tagged_ptr) {
82
355
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
83
355
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
84
355
  if (tag == ZEND_WEAKREF_TAG_HT) {
85
24
    HashTable *ht = ptr;
86
145
    ZEND_HASH_MAP_FOREACH_PTR(ht, tagged_ptr) {
87
145
      zend_weakref_unref_single(
88
145
        ZEND_WEAKREF_GET_PTR(tagged_ptr), ZEND_WEAKREF_GET_TAG(tagged_ptr), object);
89
145
    } ZEND_HASH_FOREACH_END();
90
24
    zend_hash_destroy(ht);
91
24
    FREE_HASHTABLE(ht);
92
331
  } else {
93
331
    zend_weakref_unref_single(ptr, tag, object);
94
331
  }
95
355
}
96
97
1.15k
static void zend_weakref_register(zend_object *object, void *payload) {
98
1.15k
  GC_ADD_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
99
100
1.15k
  zend_ulong obj_key = zend_object_to_weakref_key(object);
101
1.15k
  zval *zv = zend_hash_index_lookup(&EG(weakrefs), obj_key);
102
1.15k
  if (Z_TYPE_P(zv) == IS_NULL) {
103
1.08k
    ZVAL_PTR(zv, payload);
104
1.08k
    return;
105
1.08k
  }
106
107
73
  void *tagged_ptr = Z_PTR_P(zv);
108
73
  if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_HT) {
109
26
    HashTable *ht = ZEND_WEAKREF_GET_PTR(tagged_ptr);
110
26
    zend_hash_index_add_new_ptr(ht, (zend_ulong) payload, payload);
111
26
    return;
112
26
  }
113
114
  /* Convert simple pointer to hashtable. */
115
47
  HashTable *ht = emalloc(sizeof(HashTable));
116
47
  zend_hash_init(ht, 0, NULL, NULL, 0);
117
47
  zend_hash_index_add_new_ptr(ht, (zend_ulong) tagged_ptr, tagged_ptr);
118
47
  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
47
  ZVAL_PTR(zv, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_HT));
121
47
}
122
123
789
static void zend_weakref_unregister(zend_object *object, void *payload, bool weakref_free) {
124
789
  zend_ulong obj_key = zend_object_to_weakref_key(object);
125
789
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
126
789
  ZEND_ASSERT(tagged_ptr && "Weakref not registered?");
127
128
789
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
129
789
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
130
789
  if (tag != ZEND_WEAKREF_TAG_HT) {
131
708
    ZEND_ASSERT(tagged_ptr == payload);
132
708
    zend_hash_index_del(&EG(weakrefs), obj_key);
133
708
    GC_DEL_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
134
135
    /* Do this last, as it may destroy the object. */
136
708
    if (weakref_free) {
137
67
      zend_weakref_unref_single(ptr, tag, object);
138
641
    } else {
139
      /* The optimization of skipping unref is only used in the destructor of WeakMap */
140
641
      ZEND_ASSERT(ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_MAP);
141
641
    }
142
708
    return;
143
708
  }
144
145
81
  HashTable *ht = ptr;
146
81
#if ZEND_DEBUG
147
81
  void *old_payload = zend_hash_index_find_ptr(ht, (zend_ulong) payload);
148
81
  ZEND_ASSERT(old_payload && "Weakref not registered?");
149
81
  ZEND_ASSERT(old_payload == payload);
150
81
#endif
151
81
  zend_hash_index_del(ht, (zend_ulong) payload);
152
81
  if (zend_hash_num_elements(ht) == 0) {
153
23
    GC_DEL_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
154
23
    zend_hash_destroy(ht);
155
23
    FREE_HASHTABLE(ht);
156
23
    zend_hash_index_del(&EG(weakrefs), obj_key);
157
23
  }
158
159
  /* Do this last, as it may destroy the object. */
160
81
  if (weakref_free)  {
161
23
    zend_weakref_unref_single(
162
23
      ZEND_WEAKREF_GET_PTR(payload), ZEND_WEAKREF_GET_TAG(payload), object);
163
58
  } else {
164
    /* The optimization of skipping unref is only used in the destructor of WeakMap */
165
58
    ZEND_ASSERT(ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_MAP);
166
58
  }
167
81
}
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
300k
void zend_weakrefs_init(void) {
194
300k
  zend_hash_init(&EG(weakrefs), 8, NULL, NULL, 0);
195
300k
}
196
197
/* This is called when the object is garbage collected
198
 * to remove all WeakReference and WeakMap entries weakly referencing that object. */
199
355
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
355
  const zend_ulong obj_key = zend_object_to_weakref_key(object);
203
355
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
204
355
#if ZEND_DEBUG
205
355
  ZEND_ASSERT(tagged_ptr && "Tracking of the IS_OBJ_WEAKLY_REFERENCE flag should be precise");
206
355
#endif
207
355
  if (tagged_ptr) {
208
355
    zend_weakref_unref(object, tagged_ptr);
209
355
    zend_hash_index_del(&EG(weakrefs), obj_key);
210
355
  }
211
355
}
212
213
300k
void zend_weakrefs_shutdown(void) {
214
300k
  zend_hash_destroy(&EG(weakrefs));
215
300k
}
216
217
278
static zend_object* zend_weakref_new(zend_class_entry *ce) {
218
278
  zend_weakref *wr = zend_object_alloc(sizeof(zend_weakref), zend_ce_weakref);
219
220
278
  zend_object_std_init(&wr->std, zend_ce_weakref);
221
278
  return &wr->std;
222
278
}
223
224
280
static zend_always_inline bool zend_weakref_find(zend_object *referent, zval *return_value) {
225
280
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), zend_object_to_weakref_key(referent));
226
280
  if (!tagged_ptr) {
227
250
    return 0;
228
250
  }
229
230
30
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
231
30
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
232
30
  if (tag == ZEND_WEAKREF_TAG_REF) {
233
7
    zend_weakref *wr;
234
12
found_weakref:
235
12
    wr = ptr;
236
12
    RETVAL_OBJ_COPY(&wr->std);
237
12
    return 1;
238
7
  }
239
240
23
  if (tag == ZEND_WEAKREF_TAG_HT) {
241
20
    ZEND_HASH_MAP_FOREACH_PTR(ptr, tagged_ptr) {
242
20
      if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_REF) {
243
5
        ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
244
5
        goto found_weakref;
245
5
      }
246
20
    } ZEND_HASH_FOREACH_END();
247
5
  }
248
249
18
  return 0;
250
23
}
251
252
268
static zend_always_inline void zend_weakref_create(zend_object *referent, zval *return_value) {
253
268
  zend_weakref *wr;
254
255
268
  object_init_ex(return_value, zend_ce_weakref);
256
257
268
  wr = zend_weakref_fetch(return_value);
258
268
  wr->referent = referent;
259
260
268
  zend_weakref_register(wr->referent, ZEND_WEAKREF_ENCODE(wr, ZEND_WEAKREF_TAG_REF));
261
268
}
262
263
261
static zend_always_inline void zend_weakref_get(zval *weakref, zval *return_value) {
264
261
  zend_weakref *wr = zend_weakref_fetch(weakref);
265
266
261
  if (wr->referent) {
267
100
    RETVAL_OBJ_COPY(wr->referent);
268
100
  }
269
261
}
270
271
278
static void zend_weakref_free(zend_object *zo) {
272
278
  zend_weakref *wr = zend_weakref_from(zo);
273
274
278
  if (wr->referent) {
275
71
    zend_weakref_unregister(wr->referent, ZEND_WEAKREF_ENCODE(wr, ZEND_WEAKREF_TAG_REF), 1);
276
71
  }
277
278
278
  zend_object_std_dtor(&wr->std);
279
278
}
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
285
{
307
285
  zend_object *referent;
308
309
855
  ZEND_PARSE_PARAMETERS_START(1,1)
310
1.14k
    Z_PARAM_OBJ(referent)
311
285
  ZEND_PARSE_PARAMETERS_END();
312
313
280
  if (zend_weakref_find(referent, return_value)) {
314
12
      return;
315
12
  }
316
317
268
  zend_weakref_create(referent, return_value);
318
268
}
319
320
ZEND_METHOD(WeakReference, get)
321
261
{
322
261
  ZEND_PARSE_PARAMETERS_NONE();
323
324
261
  zend_weakref_get(ZEND_THIS, return_value);
325
261
}
326
327
static zend_object *zend_weakmap_create_object(zend_class_entry *ce)
328
358
{
329
358
  zend_weakmap *wm = zend_object_alloc(sizeof(zend_weakmap), ce);
330
358
  zend_object_std_init(&wm->std, ce);
331
332
358
  zend_hash_init(&wm->ht, 0, NULL, ZVAL_PTR_DTOR, 0);
333
358
  return &wm->std;
334
358
}
335
336
static void zend_weakmap_free_obj(zend_object *object)
337
358
{
338
358
  zend_weakmap *wm = zend_weakmap_from(object);
339
358
  zend_ulong obj_key;
340
2.13k
  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
2.13k
    zend_weakref_unregister(
345
2.13k
      zend_weakref_key_to_object(obj_key), ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP), 0);
346
2.13k
  } ZEND_HASH_FOREACH_END();
347
358
  zend_hash_destroy(&wm->ht);
348
358
  zend_object_std_dtor(&wm->std);
349
358
}
350
351
static zval *zend_weakmap_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
352
160
{
353
160
  if (offset == NULL) {
354
5
    zend_throw_error(NULL, "Cannot append to WeakMap");
355
5
    return NULL;
356
5
  }
357
358
155
  ZVAL_DEREF(offset);
359
155
  if (Z_TYPE_P(offset) != IS_OBJECT) {
360
7
    zend_type_error("WeakMap key must be an object");
361
7
    return NULL;
362
7
  }
363
364
148
  zend_weakmap *wm = zend_weakmap_from(object);
365
148
  zend_object *obj_addr = Z_OBJ_P(offset);
366
148
  zval *zv = zend_hash_index_find(&wm->ht, zend_object_to_weakref_key(obj_addr));
367
148
  if (zv == NULL) {
368
22
    if (type != BP_VAR_IS) {
369
22
      zend_throw_error(NULL,
370
22
        "Object %s#%d not contained in WeakMap", ZSTR_VAL(obj_addr->ce->name), obj_addr->handle);
371
22
      return NULL;
372
22
    }
373
0
    return NULL;
374
22
  }
375
376
126
  if (type == BP_VAR_W || type == BP_VAR_RW) {
377
18
    ZVAL_MAKE_REF(zv);
378
18
  }
379
126
  return zv;
380
148
}
381
382
static void zend_weakmap_write_dimension(zend_object *object, zval *offset, zval *value)
383
995
{
384
995
  if (offset == NULL) {
385
5
    zend_throw_error(NULL, "Cannot append to WeakMap");
386
5
    return;
387
5
  }
388
389
990
  ZVAL_DEREF(offset);
390
990
  if (Z_TYPE_P(offset) != IS_OBJECT) {
391
16
    zend_type_error("WeakMap key must be an object");
392
16
    return;
393
16
  }
394
395
974
  zend_weakmap *wm = zend_weakmap_from(object);
396
974
  zend_object *obj_addr = Z_OBJ_P(offset);
397
974
  zend_ulong obj_key = zend_object_to_weakref_key(obj_addr);
398
974
  Z_TRY_ADDREF_P(value);
399
400
974
  zval *zv = zend_hash_index_find(&wm->ht, obj_key);
401
974
  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
95
    zval zv_orig;
405
95
    ZVAL_COPY_VALUE(&zv_orig, zv);
406
95
    ZVAL_COPY_VALUE(zv, value);
407
95
    zval_ptr_dtor(&zv_orig);
408
95
    return;
409
95
  }
410
411
879
  zend_weakref_register(obj_addr, ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP));
412
879
  zend_hash_index_add_new(&wm->ht, obj_key, value);
413
879
}
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
230
{
419
230
  ZVAL_DEREF(offset);
420
230
  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
213
  zend_weakmap *wm = zend_weakmap_from(object);
426
213
  zval *zv = zend_hash_index_find(&wm->ht, zend_object_to_weakref_key(Z_OBJ_P(offset)));
427
213
  if (!zv) {
428
26
    return 0;
429
26
  }
430
431
187
  if (check_empty) {
432
89
    return i_zend_is_true(zv);
433
89
  }
434
98
  return Z_TYPE_P(zv) != IS_NULL;
435
187
}
436
437
static void zend_weakmap_unset_dimension(zend_object *object, zval *offset)
438
33
{
439
33
  ZVAL_DEREF(offset);
440
33
  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
28
  zend_weakmap *wm = zend_weakmap_from(object);
446
28
  zend_object *obj_addr = Z_OBJ_P(offset);
447
28
  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
19
  zend_weakref_unregister(obj_addr, ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP), 1);
453
19
}
454
455
static zend_result zend_weakmap_count_elements(zend_object *object, zend_long *count)
456
91
{
457
91
  zend_weakmap *wm = zend_weakmap_from(object);
458
91
  *count = zend_hash_num_elements(&wm->ht);
459
91
  return SUCCESS;
460
91
}
461
462
static HashTable *zend_weakmap_get_properties_for(zend_object *object, zend_prop_purpose purpose)
463
291
{
464
291
  if (purpose != ZEND_PROP_PURPOSE_DEBUG) {
465
0
    return NULL;
466
0
  }
467
468
291
  zend_weakmap *wm = zend_weakmap_from(object);
469
291
  HashTable *ht;
470
291
  ALLOC_HASHTABLE(ht);
471
291
  zend_hash_init(ht, zend_hash_num_elements(&wm->ht), NULL, ZVAL_PTR_DTOR, 0);
472
473
291
  zend_ulong obj_key;
474
291
  zval *val;
475
1.15k
  ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&wm->ht, obj_key, val) {
476
1.15k
    zend_object *obj = zend_weakref_key_to_object(obj_key);
477
1.15k
    zval pair;
478
1.15k
    array_init(&pair);
479
480
1.15k
    GC_ADDREF(obj);
481
1.15k
    add_assoc_object(&pair, "key", obj);
482
1.15k
    Z_TRY_ADDREF_P(val);
483
1.15k
    add_assoc_zval(&pair, "value", val);
484
485
1.15k
    zend_hash_next_index_insert_new(ht, &pair);
486
1.15k
  } ZEND_HASH_FOREACH_END();
487
488
291
  return ht;
489
291
}
490
491
HashTable *zend_weakmap_get_gc(zend_object *object, zval **table, int *n)
492
49
{
493
49
  zend_weakmap *wm = zend_weakmap_from(object);
494
49
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
495
49
  zval *val;
496
234
  ZEND_HASH_MAP_FOREACH_VAL(&wm->ht, val) {
497
234
    zend_get_gc_buffer_add_zval(gc_buffer, val);
498
234
  } ZEND_HASH_FOREACH_END();
499
49
  zend_get_gc_buffer_use(gc_buffer, table, n);
500
49
  return NULL;
501
49
}
502
503
HashTable *zend_weakmap_get_key_entry_gc(zend_object *object, zval **table, int *n)
504
109
{
505
109
  zend_weakmap *wm = zend_weakmap_from(object);
506
109
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
507
109
  zend_ulong h;
508
109
  zval *val;
509
615
  ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&wm->ht, h, val) {
510
615
    zend_object *key = zend_weakref_key_to_object(h);
511
615
    zend_get_gc_buffer_add_obj(gc_buffer, key);
512
615
    zend_get_gc_buffer_add_ptr(gc_buffer, val);
513
615
  } ZEND_HASH_FOREACH_END();
514
109
  zend_get_gc_buffer_use(gc_buffer, table, n);
515
109
  return NULL;
516
109
}
517
518
HashTable *zend_weakmap_get_entry_gc(zend_object *object, zval **table, int *n)
519
207
{
520
207
  zend_weakmap *wm = zend_weakmap_from(object);
521
207
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
522
207
  zval *val;
523
1.08k
  ZEND_HASH_MAP_FOREACH_VAL(&wm->ht, val) {
524
1.08k
    zend_get_gc_buffer_add_ptr(gc_buffer, val);
525
1.08k
  } ZEND_HASH_FOREACH_END();
526
207
  zend_get_gc_buffer_use(gc_buffer, table, n);
527
207
  return NULL;
528
207
}
529
530
HashTable *zend_weakmap_get_object_key_entry_gc(zend_object *object, zval **table, int *n)
531
1.84k
{
532
1.84k
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
533
1.84k
  const zend_ulong obj_key = zend_object_to_weakref_key(object);
534
1.84k
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
535
1.84k
#if ZEND_DEBUG
536
1.84k
  ZEND_ASSERT(tagged_ptr && "Tracking of the IS_OBJ_WEAKLY_REFERENCE flag should be precise");
537
1.84k
#endif
538
1.84k
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
539
1.84k
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
540
541
1.84k
  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.82k
  } else if (tag == ZEND_WEAKREF_TAG_MAP) {
553
1.52k
    zend_weakmap *wm = (zend_weakmap*) ptr;
554
1.52k
    zval *zv = zend_hash_index_find(&wm->ht, obj_key);
555
1.52k
    ZEND_ASSERT(zv);
556
1.52k
    zend_get_gc_buffer_add_ptr(gc_buffer, zv);
557
1.52k
    zend_get_gc_buffer_add_obj(gc_buffer, &wm->std);
558
1.52k
  }
559
560
1.84k
  zend_get_gc_buffer_use(gc_buffer, table, n);
561
562
1.84k
  return NULL;
563
1.84k
}
564
565
HashTable *zend_weakmap_get_object_entry_gc(zend_object *object, zval **table, int *n)
566
614
{
567
614
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
568
614
  const zend_ulong obj_key = zend_object_to_weakref_key(object);
569
614
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
570
614
#if ZEND_DEBUG
571
614
  ZEND_ASSERT(tagged_ptr && "Tracking of the IS_OBJ_WEAKLY_REFERENCE flag should be precise");
572
614
#endif
573
614
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
574
614
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
575
576
614
  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
578
  } else if (tag == ZEND_WEAKREF_TAG_MAP) {
587
249
    zend_weakmap *wm = (zend_weakmap*) ptr;
588
249
    zval *zv = zend_hash_index_find(&wm->ht, obj_key);
589
249
    ZEND_ASSERT(zv);
590
249
    zend_get_gc_buffer_add_ptr(gc_buffer, zv);
591
249
  }
592
593
614
  zend_get_gc_buffer_use(gc_buffer, table, n);
594
595
614
  return NULL;
596
614
}
597
598
static zend_object *zend_weakmap_clone_obj(zend_object *old_object)
599
12
{
600
12
  zend_object *new_object = zend_weakmap_create_object(zend_ce_weakmap);
601
12
  zend_weakmap *old_wm = zend_weakmap_from(old_object);
602
12
  zend_weakmap *new_wm = zend_weakmap_from(new_object);
603
12
  zend_hash_copy(&new_wm->ht, &old_wm->ht, NULL);
604
605
12
  zend_ulong obj_key;
606
12
  zval *val;
607
48
  ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&new_wm->ht, obj_key, val) {
608
48
    zend_weakref_register(
609
48
      zend_weakref_key_to_object(obj_key), ZEND_WEAKREF_ENCODE(new_wm, ZEND_WEAKREF_TAG_MAP));
610
48
    zval_add_ref(val);
611
48
  } ZEND_HASH_FOREACH_END();
612
12
  return new_object;
613
12
}
614
615
272
static HashPosition *zend_weakmap_iterator_get_pos_ptr(zend_weakmap_iterator *iter) {
616
272
  ZEND_ASSERT(iter->ht_iter != (uint32_t) -1);
617
272
  return &EG(ht_iterators)[iter->ht_iter].pos;
618
272
}
619
620
static void zend_weakmap_iterator_dtor(zend_object_iterator *obj_iter)
621
33
{
622
33
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
623
33
  zend_hash_iterator_del(iter->ht_iter);
624
33
  zval_ptr_dtor(&iter->it.data);
625
33
}
626
627
static zend_result zend_weakmap_iterator_valid(zend_object_iterator *obj_iter)
628
78
{
629
78
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
630
78
  zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
631
78
  HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
632
78
  return zend_hash_has_more_elements_ex(&wm->ht, pos);
633
78
}
634
635
static zval *zend_weakmap_iterator_get_current_data(zend_object_iterator *obj_iter)
636
58
{
637
58
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
638
58
  zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
639
58
  HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
640
58
  return zend_hash_get_current_data_ex(&wm->ht, pos);
641
58
}
642
643
static void zend_weakmap_iterator_get_current_key(zend_object_iterator *obj_iter, zval *key)
644
58
{
645
58
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
646
58
  zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
647
58
  HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
648
649
58
  zend_string *string_key;
650
58
  zend_ulong num_key;
651
58
  int key_type = zend_hash_get_current_key_ex(&wm->ht, &string_key, &num_key, pos);
652
58
  if (key_type == HASH_KEY_NON_EXISTENT) {
653
13
    ZVAL_NULL(key);
654
13
    return;
655
13
  }
656
45
  if (key_type != HASH_KEY_IS_LONG) {
657
0
    ZEND_ASSERT(0 && "Must have integer key");
658
0
  }
659
660
45
  ZVAL_OBJ_COPY(key, zend_weakref_key_to_object(num_key));
661
45
}
662
663
static void zend_weakmap_iterator_move_forward(zend_object_iterator *obj_iter)
664
45
{
665
45
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
666
45
  zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
667
45
  HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
668
45
  zend_hash_move_forward_ex(&wm->ht, pos);
669
45
}
670
671
static void zend_weakmap_iterator_rewind(zend_object_iterator *obj_iter)
672
33
{
673
33
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
674
33
  zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
675
33
  HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
676
33
  zend_hash_internal_pointer_reset_ex(&wm->ht, pos);
677
33
}
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
33
{
694
33
  zend_weakmap *wm = zend_weakmap_fetch(object);
695
33
  zend_weakmap_iterator *iter = emalloc(sizeof(zend_weakmap_iterator));
696
33
  zend_iterator_init(&iter->it);
697
33
  iter->it.funcs = &zend_weakmap_iterator_funcs;
698
33
  ZVAL_COPY(&iter->it.data, object);
699
33
  iter->ht_iter = zend_hash_iterator_add(&wm->ht, 0);
700
33
  return &iter->it;
701
33
}
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
13
{
765
13
  if (zend_parse_parameters_none() == FAILURE) {
766
0
    RETURN_THROWS();
767
0
  }
768
769
13
  zend_create_internal_iterator_zval(return_value, ZEND_THIS);
770
13
}
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