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