Skip to content

Commit c6f02a4

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: Differenciate WeakMaps from bare HashTables used as weak maps for GC purposes
2 parents ba5305d + 62e30ec commit c6f02a4

File tree

6 files changed

+73
-17
lines changed

6 files changed

+73
-17
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ PHP NEWS
1717
https://wiki.php.net/rfc/url_parsing_api#plugability. (kocsismate)
1818
. Fixed bug GH-19548 (Shared memory violation on property inheritance).
1919
(alexandre-daubois)
20+
. Fixed bug GH-19544 (GC treats ZEND_WEAKREF_TAG_MAP references as WeakMap
21+
references). (Arnaud, timwolla)
2022

2123
- Filter:
2224
. Added support for configuring the URI parser for FILTER_VALIDATE_URL

Zend/tests/gh19543-001.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
GH-19543 001: GC treats ZEND_WEAKREF_TAG_MAP references as WeakMap references
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
$e = new Exception();
9+
$a = new stdClass();
10+
zend_weakmap_attach($e, $a);
11+
unset($a);
12+
gc_collect_cycles();
13+
14+
?>
15+
==DONE==
16+
--EXPECT--
17+
==DONE==

Zend/tests/gh19543-002.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-19543 002: GC treats ZEND_WEAKREF_TAG_MAP references as WeakMap references
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
$e = new Exception();
9+
$a = new stdClass();
10+
zend_weakmap_attach($e, $a);
11+
unset($a);
12+
$e2 = $e;
13+
unset($e2); // add to roots
14+
gc_collect_cycles();
15+
16+
?>
17+
==DONE==
18+
--EXPECT--
19+
==DONE==

Zend/zend_weakrefs.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,21 @@ typedef struct _zend_weakmap_iterator {
3636
uint32_t ht_iter;
3737
} zend_weakmap_iterator;
3838

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.
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.
4040
*
4141
* 1. For a single WeakReference,
4242
* 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()).
4343
* 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.
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.
4647
*
4748
* 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),
4849
* 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
50+
#define ZEND_WEAKREF_TAG_REF 0
51+
#define ZEND_WEAKREF_TAG_MAP 1
52+
#define ZEND_WEAKREF_TAG_HT 2
53+
#define ZEND_WEAKREF_TAG_BARE_HT 3
5254
#define ZEND_WEAKREF_GET_TAG(p) (((uintptr_t) (p)) & 3)
5355
#define ZEND_WEAKREF_GET_PTR(p) ((void *) (((uintptr_t) (p)) & ~3))
5456
#define ZEND_WEAKREF_ENCODE(p, t) ((void *) (((uintptr_t) (p)) | (t)))
@@ -72,8 +74,8 @@ static inline void zend_weakref_unref_single(
7274
zend_weakref *wr = ptr;
7375
wr->referent = NULL;
7476
} else {
75-
/* unreferencing WeakMap entry (at ptr) with a key of object. */
76-
ZEND_ASSERT(tag == ZEND_WEAKREF_TAG_MAP);
77+
/* unreferencing WeakMap or bare HashTable entry (at ptr) with a key of object. */
78+
ZEND_ASSERT(tag == ZEND_WEAKREF_TAG_MAP || tag == ZEND_WEAKREF_TAG_BARE_HT);
7779
zend_hash_index_del((HashTable *) ptr, zend_object_to_weakref_key(object));
7880
}
7981
}
@@ -166,18 +168,20 @@ static void zend_weakref_unregister(zend_object *object, void *payload, bool wea
166168
}
167169
}
168170

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. */
169173
ZEND_API zval *zend_weakrefs_hash_add(HashTable *ht, zend_object *key, zval *pData) {
170174
zval *zv = zend_hash_index_add(ht, zend_object_to_weakref_key(key), pData);
171175
if (zv) {
172-
zend_weakref_register(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_MAP));
176+
zend_weakref_register(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_BARE_HT));
173177
}
174178
return zv;
175179
}
176180

177181
ZEND_API zend_result zend_weakrefs_hash_del(HashTable *ht, zend_object *key) {
178182
zval *zv = zend_hash_index_find(ht, zend_object_to_weakref_key(key));
179183
if (zv) {
180-
zend_weakref_unregister(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_MAP), 1);
184+
zend_weakref_unregister(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_BARE_HT), 1);
181185
return SUCCESS;
182186
}
183187
return FAILURE;
@@ -547,6 +551,10 @@ HashTable *zend_weakmap_get_object_key_entry_gc(zend_object *object, zval **tabl
547551
ZEND_ASSERT(zv);
548552
zend_get_gc_buffer_add_ptr(gc_buffer, zv);
549553
zend_get_gc_buffer_add_obj(gc_buffer, &wm->std);
554+
} else if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_BARE_HT) {
555+
/* Bare HashTables are intentionally ignored, since they are
556+
* intended for internal usage by extensions and might not be
557+
* collectable. */
550558
}
551559
} ZEND_HASH_FOREACH_END();
552560
} else if (tag == ZEND_WEAKREF_TAG_MAP) {
@@ -555,6 +563,8 @@ HashTable *zend_weakmap_get_object_key_entry_gc(zend_object *object, zval **tabl
555563
ZEND_ASSERT(zv);
556564
zend_get_gc_buffer_add_ptr(gc_buffer, zv);
557565
zend_get_gc_buffer_add_obj(gc_buffer, &wm->std);
566+
} else if (tag == ZEND_WEAKREF_TAG_BARE_HT) {
567+
/* Bare HashTables are intentionally ignored (see above) */
558568
}
559569

560570
zend_get_gc_buffer_use(gc_buffer, table, n);
@@ -581,13 +591,19 @@ HashTable *zend_weakmap_get_object_entry_gc(zend_object *object, zval **table, i
581591
zval *zv = zend_hash_index_find(&wm->ht, obj_key);
582592
ZEND_ASSERT(zv);
583593
zend_get_gc_buffer_add_ptr(gc_buffer, zv);
594+
} else if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_BARE_HT) {
595+
/* Bare HashTables are intentionally ignored
596+
* (see zend_weakmap_get_object_key_entry_gc) */
584597
}
585598
} ZEND_HASH_FOREACH_END();
586599
} else if (tag == ZEND_WEAKREF_TAG_MAP) {
587600
zend_weakmap *wm = (zend_weakmap*) ptr;
588601
zval *zv = zend_hash_index_find(&wm->ht, obj_key);
589602
ZEND_ASSERT(zv);
590603
zend_get_gc_buffer_add_ptr(gc_buffer, zv);
604+
} else if (tag == ZEND_WEAKREF_TAG_BARE_HT) {
605+
/* Bare HashTables are intentionally ignored
606+
* (see zend_weakmap_get_object_key_entry_gc) */
591607
}
592608

593609
zend_get_gc_buffer_use(gc_buffer, table, n);

ext/zend_test/php_test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ ZEND_BEGIN_MODULE_GLOBALS(zend_test)
5050
int observer_fiber_switch;
5151
int observer_fiber_destroy;
5252
int observer_execute_internal;
53-
HashTable global_weakmap;
53+
HashTable *global_weakmap;
5454
int replace_zend_execute_ex;
5555
int register_passes;
5656
bool print_stderr_mshutdown;

ext/zend_test/test.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ static ZEND_FUNCTION(zend_weakmap_attach)
381381
Z_PARAM_ZVAL(value)
382382
ZEND_PARSE_PARAMETERS_END();
383383

384-
if (zend_weakrefs_hash_add(&ZT_G(global_weakmap), obj, value)) {
384+
if (zend_weakrefs_hash_add(ZT_G(global_weakmap), obj, value)) {
385385
Z_TRY_ADDREF_P(value);
386386
RETURN_TRUE;
387387
}
@@ -396,13 +396,13 @@ static ZEND_FUNCTION(zend_weakmap_remove)
396396
Z_PARAM_OBJ(obj)
397397
ZEND_PARSE_PARAMETERS_END();
398398

399-
RETURN_BOOL(zend_weakrefs_hash_del(&ZT_G(global_weakmap), obj) == SUCCESS);
399+
RETURN_BOOL(zend_weakrefs_hash_del(ZT_G(global_weakmap), obj) == SUCCESS);
400400
}
401401

402402
static ZEND_FUNCTION(zend_weakmap_dump)
403403
{
404404
ZEND_PARSE_PARAMETERS_NONE();
405-
RETURN_ARR(zend_array_dup(&ZT_G(global_weakmap)));
405+
RETURN_ARR(zend_array_dup(ZT_G(global_weakmap)));
406406
}
407407

408408
static ZEND_FUNCTION(zend_get_current_func_name)
@@ -1424,15 +1424,17 @@ PHP_MSHUTDOWN_FUNCTION(zend_test)
14241424

14251425
PHP_RINIT_FUNCTION(zend_test)
14261426
{
1427-
zend_hash_init(&ZT_G(global_weakmap), 8, NULL, ZVAL_PTR_DTOR, 0);
1427+
ALLOC_HASHTABLE(ZT_G(global_weakmap));
1428+
zend_hash_init(ZT_G(global_weakmap), 8, NULL, ZVAL_PTR_DTOR, 0);
14281429
ZT_G(observer_nesting_depth) = 0;
14291430
zend_test_mm_custom_handlers_rinit();
14301431
return SUCCESS;
14311432
}
14321433

14331434
PHP_RSHUTDOWN_FUNCTION(zend_test)
14341435
{
1435-
zend_weakrefs_hash_destroy(&ZT_G(global_weakmap));
1436+
zend_weakrefs_hash_destroy(ZT_G(global_weakmap));
1437+
FREE_HASHTABLE(ZT_G(global_weakmap));
14361438

14371439
if (ZT_G(zend_test_heap)) {
14381440
free(ZT_G(zend_test_heap));
@@ -1637,7 +1639,7 @@ static PHP_FUNCTION(zend_test_compile_to_ast)
16371639

16381640
zend_arena *ast_arena;
16391641
zend_ast *ast = zend_compile_string_to_ast(str, &ast_arena, ZSTR_EMPTY_ALLOC());
1640-
1642+
16411643
zend_string *result = zend_ast_export("", ast, "");
16421644

16431645
zend_ast_destroy(ast);

0 commit comments

Comments
 (0)