Coverage Report

Created: 2025-12-14 06:10

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