Coverage Report

Created: 2025-12-14 06:09

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