php-src icon indicating copy to clipboard operation
php-src copied to clipboard

WeakMap: write/reference fetch with a temporary object key returns a dangling slot (UNKNOWN:0, leaks)

Open jorgsowa opened this issue 3 weeks ago • 3 comments

Description

Since GH-20097 (aaed3ccb479), zend_weakmap_read_dimension() inserts a missing key for BP_VAR_W/BP_VAR_RW. With a temporary key, the FETCH_DIM_W handler then frees the TMP key, the weakref notification removes the just-inserted entry, and the returned INDIRECT points at a dead bucket. If the key's destructor frees the WeakMap, the slot points into freed memory.

The following code:

<?php
$wm = new WeakMap;
$r = &$wm[new stdClass];
var_dump($r);
$wm[new stdClass][] = 1;
var_dump(count($wm));

Resulted in this output (debug build):

UNKNOWN:0
int(0)
Zend/zend_hash.c(159) :  Freeing 0x0000000105e02780 (136 bytes), script=t.php
Zend/zend_hash.c(352) :  Freeing 0x0000000105e06ae0 (56 bytes), script=t.php
Zend/zend_execute.c(589) :  Freeing 0x0000000105e5d700 (32 bytes), script=t.php
=== Total 3 memory leaks detected ===

But I expected this output instead (either an error, as in PHP 8.5.12, or a write into a discarded entry without reading freed memory):

Fatal error: Uncaught Error: Object stdClass#2 not contained in WeakMap in t.php:3

Variant where the key's destructor frees the map, which crashes (exit code 139) under guard malloc (USE_ZEND_ALLOC=0):

<?php
class K { function __destruct() { $GLOBALS['wm'] = null; } }
$wm = new WeakMap;
$wm[new K][] = str_repeat("x", 10);

The underlying ordering issue also exists in 8.5 when the entry already exists and the temporary holds the last reference to the key, which prints UNKNOWN:0 on 8.5.12 as well:

<?php
$wm = new WeakMap;
$k = new stdClass; $wm[$k] = 1; $a = [$k]; unset($k);
$r = &$wm[array_pop($a)];
var_dump($r);

8.6 makes it reachable with a plain new X key that 8.5 rejected. Reproduces with opcache off, on, and with tracing JIT.

PHP Version

PHP 8.6.1-dev (cli) (NTS DEBUG), PHP-8.6 @ dfc3219f341

Operating System

macOS (arm64)

jorgsowa avatar Nov 10 '26 22:11 jorgsowa

The core issue actually reproduces on 8.4 too.

ndossche avatar Nov 11 '26 10:11 ndossche

This is a general engine problem, but only WeakMap (for now) can trigger this. FETCH_DIM_W/RW (& friends) give the next opcode an INDIRECT pointing into the container, and only then free op2. If freeing op2 runs code or invalidates the slot the INDIRECT becomes invalid.

ndossche avatar Nov 11 '26 10:11 ndossche

This would work but this is of course not nice:

diff --git a/Zend/zend_weakrefs.c b/Zend/zend_weakrefs.c
index 1178c1656c9..b1351ac6a8d 100644
--- a/Zend/zend_weakrefs.c
+++ b/Zend/zend_weakrefs.c
@@ -347,6 +347,8 @@ static void zend_weakmap_free_obj(zend_object *object)
 
 static zval *zend_weakmap_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
 {
+	zval *orig_offset = offset;
+
 	if (offset == NULL) {
 		zend_throw_error(NULL, "Cannot append to WeakMap");
 		return NULL;
@@ -372,6 +374,19 @@ static zval *zend_weakmap_read_dimension(zend_object *object, zval *offset, int
 
 	if (type == BP_VAR_W || type == BP_VAR_RW) {
 		ZVAL_MAKE_REF(zv);
+
+		/* If offset is a TMPVAR of the current fetch and holds the last recount,
+		 * the VM releases it after the call at which point the INDIRECT becomes invalid.
+		 * The next opcode then reads a stale iNIDRECT.
+		 * To circumvent this issue, we give a copy rather than an indirect. */
+		const zend_execute_data *ex = EG(current_execute_data);
+		if (UNEXPECTED(GC_REFCOUNT(obj_addr) == 1 && Z_REFCOUNT_P(orig_offset) == 1)
+		 && ex && ZEND_USER_CODE(ex->func->type)
+		 && (ex->opline->op2_type & (IS_TMP_VAR|IS_VAR))
+		 && orig_offset == ZEND_CALL_VAR(ex, ex->opline->op2.var)) {
+			ZVAL_COPY(rv, zv);
+			return rv;
+		}
 	}
 	return zv;
 }

ndossche avatar Nov 11 '26 11:11 ndossche