Coverage Report

Created: 2025-12-31 07:28

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