Coverage Report

Created: 2025-11-16 06:23

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
584
#define ZEND_WEAKREF_TAG_REF     0
51
2.63k
#define ZEND_WEAKREF_TAG_MAP     1
52
3.41k
#define ZEND_WEAKREF_TAG_HT      2
53
672
#define ZEND_WEAKREF_TAG_BARE_HT 3
54
3.68k
#define ZEND_WEAKREF_GET_TAG(p) (((uintptr_t) (p)) & 3)
55
3.54k
#define ZEND_WEAKREF_GET_PTR(p) ((void *) (((uintptr_t) (p)) & ~3))
56
1.83k
#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
898
#define zend_weakref_from(o) ((zend_weakref*)(((char*) o) - XtOffsetOf(zend_weakref, std)))
64
567
#define zend_weakref_fetch(z) zend_weakref_from(Z_OBJ_P(z))
65
66
3.25k
#define zend_weakmap_from(o) ((zend_weakmap*)(((char*) o) - XtOffsetOf(zend_weakmap, std)))
67
643
#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
531
{
72
531
  if (tag == ZEND_WEAKREF_TAG_REF) {
73
    /* Unreferencing WeakReference (at ptr) singleton that pointed to object. */
74
286
    zend_weakref *wr = ptr;
75
286
    wr->referent = NULL;
76
286
  } else {
77
    /* unreferencing WeakMap or bare HashTable entry (at ptr) with a key of object. */
78
245
    ZEND_ASSERT(tag == ZEND_WEAKREF_TAG_MAP || tag == ZEND_WEAKREF_TAG_BARE_HT);
79
245
    zend_hash_index_del((HashTable *) ptr, zend_object_to_weakref_key(object));
80
245
  }
81
531
}
82
83
385
static void zend_weakref_unref(zend_object *object, void *tagged_ptr) {
84
385
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
85
385
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
86
385
  if (tag == ZEND_WEAKREF_TAG_HT) {
87
38
    HashTable *ht = ptr;
88
268
    ZEND_HASH_MAP_FOREACH_PTR(ht, tagged_ptr) {
89
268
      zend_weakref_unref_single(
90
268
        ZEND_WEAKREF_GET_PTR(tagged_ptr), ZEND_WEAKREF_GET_TAG(tagged_ptr), object);
91
268
    } ZEND_HASH_FOREACH_END();
92
38
    zend_hash_destroy(ht);
93
38
    FREE_HASHTABLE(ht);
94
347
  } else {
95
347
    zend_weakref_unref_single(ptr, tag, object);
96
347
  }
97
385
}
98
99
1.13k
static void zend_weakref_register(zend_object *object, void *payload) {
100
1.13k
  GC_ADD_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
101
102
1.13k
  zend_ulong obj_key = zend_object_to_weakref_key(object);
103
1.13k
  zval *zv = zend_hash_index_lookup(&EG(weakrefs), obj_key);
104
1.13k
  if (Z_TYPE_P(zv) == IS_NULL) {
105
1.00k
    ZVAL_PTR(zv, payload);
106
1.00k
    return;
107
1.00k
  }
108
109
129
  void *tagged_ptr = Z_PTR_P(zv);
110
129
  if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_HT) {
111
60
    HashTable *ht = ZEND_WEAKREF_GET_PTR(tagged_ptr);
112
60
    zend_hash_index_add_new_ptr(ht, (zend_ulong) payload, payload);
113
60
    return;
114
60
  }
115
116
  /* Convert simple pointer to hashtable. */
117
69
  HashTable *ht = emalloc(sizeof(HashTable));
118
69
  zend_hash_init(ht, 0, NULL, NULL, 0);
119
69
  zend_hash_index_add_new_ptr(ht, (zend_ulong) tagged_ptr, tagged_ptr);
120
69
  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
69
  ZVAL_PTR(zv, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_HT));
123
69
}
124
125
701
static void zend_weakref_unregister(zend_object *object, void *payload, bool weakref_free) {
126
701
  zend_ulong obj_key = zend_object_to_weakref_key(object);
127
701
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
128
701
  ZEND_ASSERT(tagged_ptr && "Weakref not registered?");
129
130
701
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
131
701
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
132
701
  if (tag != ZEND_WEAKREF_TAG_HT) {
133
586
    ZEND_ASSERT(tagged_ptr == payload);
134
586
    zend_hash_index_del(&EG(weakrefs), obj_key);
135
586
    GC_DEL_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
136
137
    /* Do this last, as it may destroy the object. */
138
586
    if (weakref_free) {
139
67
      zend_weakref_unref_single(ptr, tag, object);
140
519
    } else {
141
      /* The optimization of skipping unref is used for zend_weakrefs_hash_clean_ex() */
142
519
      ZEND_ASSERT(ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_MAP || ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_BARE_HT);
143
519
    }
144
586
    return;
145
586
  }
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
429
static void zend_weakrefs_hash_clean_ex(HashTable *ht, int type) {
191
429
  zend_ulong obj_key;
192
2.09k
  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.09k
    zend_weakref_unregister(
197
2.09k
      zend_weakref_key_to_object(obj_key), ZEND_WEAKREF_ENCODE(ht, type), false);
198
2.09k
  } ZEND_HASH_FOREACH_END();
199
429
  zend_hash_clean(ht);
200
429
}
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
247k
void zend_weakrefs_init(void) {
207
247k
  zend_hash_init(&EG(weakrefs), 8, NULL, NULL, 0);
208
247k
}
209
210
/* This is called when the object is garbage collected
211
 * to remove all WeakReference and WeakMap entries weakly referencing that object. */
212
385
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
385
  const zend_ulong obj_key = zend_object_to_weakref_key(object);
216
385
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
217
385
#if ZEND_DEBUG
218
385
  ZEND_ASSERT(tagged_ptr && "Tracking of the IS_OBJ_WEAKLY_REFERENCE flag should be precise");
219
385
#endif
220
385
  if (tagged_ptr) {
221
385
    zend_weakref_unref(object, tagged_ptr);
222
385
    zend_hash_index_del(&EG(weakrefs), obj_key);
223
385
  }
224
385
}
225
226
247k
void zend_weakrefs_shutdown(void) {
227
247k
  zend_hash_destroy(&EG(weakrefs));
228
247k
}
229
230
297
static zend_object* zend_weakref_new(zend_class_entry *ce) {
231
297
  zend_weakref *wr = zend_object_alloc(sizeof(zend_weakref), zend_ce_weakref);
232
233
297
  zend_object_std_init(&wr->std, zend_ce_weakref);
234
297
  return &wr->std;
235
297
}
236
237
306
static zend_always_inline bool zend_weakref_find(zend_object *referent, zval *return_value) {
238
306
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), zend_object_to_weakref_key(referent));
239
306
  if (!tagged_ptr) {
240
268
    return false;
241
268
  }
242
243
38
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
244
38
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
245
38
  if (tag == ZEND_WEAKREF_TAG_REF) {
246
5
    zend_weakref *wr;
247
20
found_weakref:
248
20
    wr = ptr;
249
20
    RETVAL_OBJ_COPY(&wr->std);
250
20
    return true;
251
5
  }
252
253
33
  if (tag == ZEND_WEAKREF_TAG_HT) {
254
60
    ZEND_HASH_MAP_FOREACH_PTR(ptr, tagged_ptr) {
255
60
      if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_REF) {
256
15
        ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
257
15
        goto found_weakref;
258
15
      }
259
60
    } ZEND_HASH_FOREACH_END();
260
15
  }
261
262
18
  return false;
263
33
}
264
265
286
static zend_always_inline void zend_weakref_create(zend_object *referent, zval *return_value) {
266
286
  zend_weakref *wr;
267
268
286
  object_init_ex(return_value, zend_ce_weakref);
269
270
286
  wr = zend_weakref_fetch(return_value);
271
286
  wr->referent = referent;
272
273
286
  zend_weakref_register(wr->referent, ZEND_WEAKREF_ENCODE(wr, ZEND_WEAKREF_TAG_REF));
274
286
}
275
276
281
static zend_always_inline void zend_weakref_get(zval *weakref, zval *return_value) {
277
281
  zend_weakref *wr = zend_weakref_fetch(weakref);
278
279
281
  if (wr->referent) {
280
111
    RETVAL_OBJ_COPY(wr->referent);
281
111
  }
282
281
}
283
284
297
static void zend_weakref_free(zend_object *zo) {
285
297
  zend_weakref *wr = zend_weakref_from(zo);
286
287
297
  if (wr->referent) {
288
84
    zend_weakref_unregister(wr->referent, ZEND_WEAKREF_ENCODE(wr, ZEND_WEAKREF_TAG_REF), true);
289
84
  }
290
291
297
  zend_object_std_dtor(&wr->std);
292
297
}
293
294
static HashTable *zend_weakref_get_debug_info(zend_object *object, int *is_temp)
295
34
{
296
34
  *is_temp = 1;
297
298
34
  HashTable *ht = zend_new_array(1);
299
300
34
  zend_object *referent = zend_weakref_from(object)->referent;
301
34
  zval value;
302
34
  if (referent) {
303
25
    ZVAL_OBJ_COPY(&value, referent);
304
25
  } else {
305
9
    ZVAL_NULL(&value);
306
9
  }
307
308
34
  zend_hash_update(ht, ZSTR_KNOWN(ZEND_STR_OBJECT), &value);
309
310
34
  return ht;
311
34
}
312
313
ZEND_COLD ZEND_METHOD(WeakReference, __construct)
314
11
{
315
11
  zend_throw_error(NULL, "Direct instantiation of WeakReference is not allowed, use WeakReference::create instead");
316
11
}
317
318
ZEND_METHOD(WeakReference, create)
319
311
{
320
311
  zend_object *referent;
321
322
933
  ZEND_PARSE_PARAMETERS_START(1,1)
323
1.24k
    Z_PARAM_OBJ(referent)
324
311
  ZEND_PARSE_PARAMETERS_END();
325
326
306
  if (zend_weakref_find(referent, return_value)) {
327
20
      return;
328
20
  }
329
330
286
  zend_weakref_create(referent, return_value);
331
286
}
332
333
ZEND_METHOD(WeakReference, get)
334
281
{
335
281
  ZEND_PARSE_PARAMETERS_NONE();
336
337
281
  zend_weakref_get(ZEND_THIS, return_value);
338
281
}
339
340
static zend_object *zend_weakmap_create_object(zend_class_entry *ce)
341
429
{
342
429
  zend_weakmap *wm = zend_object_alloc(sizeof(zend_weakmap), ce);
343
429
  zend_object_std_init(&wm->std, ce);
344
345
429
  zend_hash_init(&wm->ht, 0, NULL, ZVAL_PTR_DTOR, 0);
346
429
  return &wm->std;
347
429
}
348
349
static void zend_weakmap_free_obj(zend_object *object)
350
429
{
351
429
  zend_weakmap *wm = zend_weakmap_from(object);
352
429
  zend_weakrefs_hash_clean_ex(&wm->ht, ZEND_WEAKREF_TAG_MAP);
353
429
  zend_hash_destroy(&wm->ht);
354
429
  zend_object_std_dtor(&wm->std);
355
429
}
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
928
{
397
928
  if (offset == NULL) {
398
8
    zend_throw_error(NULL, "Cannot append to WeakMap");
399
8
    return;
400
8
  }
401
402
920
  ZVAL_DEREF(offset);
403
920
  if (Z_TYPE_P(offset) != IS_OBJECT) {
404
22
    zend_type_error("WeakMap key must be an object");
405
22
    return;
406
22
  }
407
408
898
  zend_weakmap *wm = zend_weakmap_from(object);
409
898
  zend_object *obj_addr = Z_OBJ_P(offset);
410
898
  zend_ulong obj_key = zend_object_to_weakref_key(obj_addr);
411
898
  Z_TRY_ADDREF_P(value);
412
413
898
  zval *zv = zend_hash_index_find(&wm->ht, obj_key);
414
898
  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
814
  zend_weakref_register(obj_addr, ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP));
425
814
  zend_hash_index_add_new(&wm->ht, obj_key, value);
426
814
}
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
204
{
432
204
  ZVAL_DEREF(offset);
433
204
  if (Z_TYPE_P(offset) != IS_OBJECT) {
434
20
    zend_type_error("WeakMap key must be an object");
435
20
    return 0;
436
20
  }
437
438
184
  zend_weakmap *wm = zend_weakmap_from(object);
439
184
  zval *zv = zend_hash_index_find(&wm->ht, zend_object_to_weakref_key(Z_OBJ_P(offset)));
440
184
  if (!zv) {
441
24
    return 0;
442
24
  }
443
444
160
  if (check_empty) {
445
76
    return i_zend_is_true(zv);
446
76
  }
447
84
  return Z_TYPE_P(zv) != IS_NULL;
448
160
}
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
79
{
470
79
  zend_weakmap *wm = zend_weakmap_from(object);
471
79
  *count = zend_hash_num_elements(&wm->ht);
472
79
  return SUCCESS;
473
79
}
474
475
static HashTable *zend_weakmap_get_properties_for(zend_object *object, zend_prop_purpose purpose)
476
342
{
477
342
  if (purpose != ZEND_PROP_PURPOSE_DEBUG) {
478
0
    return NULL;
479
0
  }
480
481
342
  zend_weakmap *wm = zend_weakmap_from(object);
482
342
  HashTable *ht;
483
342
  ALLOC_HASHTABLE(ht);
484
342
  zend_hash_init(ht, zend_hash_num_elements(&wm->ht), NULL, ZVAL_PTR_DTOR, 0);
485
486
342
  zend_ulong obj_key;
487
342
  zval *val;
488
1.31k
  ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&wm->ht, obj_key, val) {
489
1.31k
    zend_object *obj = zend_weakref_key_to_object(obj_key);
490
1.31k
    zval pair;
491
1.31k
    array_init(&pair);
492
493
1.31k
    GC_ADDREF(obj);
494
1.31k
    add_assoc_object(&pair, "key", obj);
495
1.31k
    Z_TRY_ADDREF_P(val);
496
1.31k
    add_assoc_zval(&pair, "value", val);
497
498
1.31k
    zend_hash_next_index_insert_new(ht, &pair);
499
1.31k
  } ZEND_HASH_FOREACH_END();
500
501
342
  return ht;
502
342
}
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
133
{
518
133
  zend_weakmap *wm = zend_weakmap_from(object);
519
133
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
520
133
  zend_ulong h;
521
133
  zval *val;
522
720
  ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&wm->ht, h, val) {
523
720
    zend_object *key = zend_weakref_key_to_object(h);
524
720
    zend_get_gc_buffer_add_obj(gc_buffer, key);
525
720
    zend_get_gc_buffer_add_ptr(gc_buffer, val);
526
720
  } ZEND_HASH_FOREACH_END();
527
133
  zend_get_gc_buffer_use(gc_buffer, table, n);
528
133
  return NULL;
529
133
}
530
531
HashTable *zend_weakmap_get_entry_gc(zend_object *object, zval **table, int *n)
532
247
{
533
247
  zend_weakmap *wm = zend_weakmap_from(object);
534
247
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
535
247
  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
247
  zend_get_gc_buffer_use(gc_buffer, table, n);
540
247
  return NULL;
541
247
}
542
543
HashTable *zend_weakmap_get_object_key_entry_gc(zend_object *object, zval **table, int *n)
544
1.56k
{
545
1.56k
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
546
1.56k
  const zend_ulong obj_key = zend_object_to_weakref_key(object);
547
1.56k
  void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_key);
548
1.56k
#if ZEND_DEBUG
549
1.56k
  ZEND_ASSERT(tagged_ptr && "Tracking of the IS_OBJ_WEAKLY_REFERENCE flag should be precise");
550
1.56k
#endif
551
1.56k
  void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
552
1.56k
  uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
553
554
1.56k
  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.54k
  } else if (tag == ZEND_WEAKREF_TAG_MAP) {
570
1.23k
    zend_weakmap *wm = (zend_weakmap*) ptr;
571
1.23k
    zval *zv = zend_hash_index_find(&wm->ht, obj_key);
572
1.23k
    ZEND_ASSERT(zv);
573
1.23k
    zend_get_gc_buffer_add_ptr(gc_buffer, zv);
574
1.23k
    zend_get_gc_buffer_add_obj(gc_buffer, &wm->std);
575
1.23k
  } else if (tag == ZEND_WEAKREF_TAG_BARE_HT) {
576
    /* Bare HashTables are intentionally ignored (see above) */
577
0
  }
578
579
1.56k
  zend_get_gc_buffer_use(gc_buffer, table, n);
580
581
1.56k
  return NULL;
582
1.56k
}
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
25
{
625
25
  zend_object *new_object = zend_weakmap_create_object(zend_ce_weakmap);
626
25
  zend_weakmap *old_wm = zend_weakmap_from(old_object);
627
25
  zend_weakmap *new_wm = zend_weakmap_from(new_object);
628
25
  zend_hash_copy(&new_wm->ht, &old_wm->ht, NULL);
629
630
25
  zend_ulong obj_key;
631
25
  zval *val;
632
100
  ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(&new_wm->ht, obj_key, val) {
633
100
    zend_weakref_register(
634
100
      zend_weakref_key_to_object(obj_key), ZEND_WEAKREF_ENCODE(new_wm, ZEND_WEAKREF_TAG_MAP));
635
100
    zval_add_ref(val);
636
100
  } ZEND_HASH_FOREACH_END();
637
25
  return new_object;
638
25
}
639
640
572
static HashPosition *zend_weakmap_iterator_get_pos_ptr(zend_weakmap_iterator *iter) {
641
572
  ZEND_ASSERT(iter->ht_iter != (uint32_t) -1);
642
572
  return &EG(ht_iterators)[iter->ht_iter].pos;
643
572
}
644
645
static void zend_weakmap_iterator_dtor(zend_object_iterator *obj_iter)
646
71
{
647
71
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
648
71
  zend_hash_iterator_del(iter->ht_iter);
649
71
  zval_ptr_dtor(&iter->it.data);
650
71
}
651
652
static zend_result zend_weakmap_iterator_valid(zend_object_iterator *obj_iter)
653
170
{
654
170
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
655
170
  zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
656
170
  HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
657
170
  return zend_hash_has_more_elements_ex(&wm->ht, pos);
658
170
}
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
71
{
698
71
  zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
699
71
  zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
700
71
  HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
701
71
  zend_hash_internal_pointer_reset_ex(&wm->ht, pos);
702
71
}
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
71
{
719
71
  zend_weakmap *wm = zend_weakmap_fetch(object);
720
71
  zend_weakmap_iterator *iter = emalloc(sizeof(zend_weakmap_iterator));
721
71
  zend_iterator_init(&iter->it);
722
71
  iter->it.funcs = &zend_weakmap_iterator_funcs;
723
71
  ZVAL_COPY(&iter->it.data, object);
724
71
  iter->ht_iter = zend_hash_iterator_add(&wm->ht, 0);
725
71
  return &iter->it;
726
71
}
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
  if (zend_parse_parameters_none() == FAILURE) {
780
0
    RETURN_THROWS();
781
0
  }
782
783
10
  zend_long count;
784
10
  zend_weakmap_count_elements(Z_OBJ_P(ZEND_THIS), &count);
785
10
  RETURN_LONG(count);
786
10
}
787
788
ZEND_METHOD(WeakMap, getIterator)
789
10
{
790
10
  if (zend_parse_parameters_none() == FAILURE) {
791
0
    RETURN_THROWS();
792
0
  }
793
794
10
  zend_create_internal_iterator_zval(return_value, ZEND_THIS);
795
10
}
796
797
void zend_register_weakref_ce(void) /* {{{ */
798
16
{
799
16
  zend_ce_weakref = register_class_WeakReference();
800
801
16
  zend_ce_weakref->create_object = zend_weakref_new;
802
16
  zend_ce_weakref->default_object_handlers = &zend_weakref_handlers;
803
804
16
  memcpy(&zend_weakref_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
805
16
  zend_weakref_handlers.offset = XtOffsetOf(zend_weakref, std);
806
807
16
  zend_weakref_handlers.free_obj = zend_weakref_free;
808
16
  zend_weakref_handlers.get_debug_info = zend_weakref_get_debug_info;
809
16
  zend_weakref_handlers.clone_obj = NULL;
810
811
16
  zend_ce_weakmap = register_class_WeakMap(zend_ce_arrayaccess, zend_ce_countable, zend_ce_aggregate);
812
813
16
  zend_ce_weakmap->create_object = zend_weakmap_create_object;
814
16
  zend_ce_weakmap->get_iterator = zend_weakmap_get_iterator;
815
16
  zend_ce_weakmap->default_object_handlers = &zend_weakmap_handlers;
816
817
16
  memcpy(&zend_weakmap_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
818
  zend_weakmap_handlers.offset = XtOffsetOf(zend_weakmap, std);
819
16
  zend_weakmap_handlers.free_obj = zend_weakmap_free_obj;
820
16
  zend_weakmap_handlers.read_dimension = zend_weakmap_read_dimension;
821
16
  zend_weakmap_handlers.write_dimension = zend_weakmap_write_dimension;
822
16
  zend_weakmap_handlers.has_dimension = zend_weakmap_has_dimension;
823
16
  zend_weakmap_handlers.unset_dimension = zend_weakmap_unset_dimension;
824
16
  zend_weakmap_handlers.count_elements = zend_weakmap_count_elements;
825
16
  zend_weakmap_handlers.get_properties_for = zend_weakmap_get_properties_for;
826
16
  zend_weakmap_handlers.get_gc = zend_weakmap_get_gc;
827
16
  zend_weakmap_handlers.clone_obj = zend_weakmap_clone_obj;
828
16
}
829
/* }}} */
830