Coverage Report

Created: 2026-02-14 06:52

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