Coverage Report

Created: 2025-09-27 06:26

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