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