Commit b2caaa823c0 for php

commit b2caaa823c0cb91f3db84f94cf6d7135adced29e
Author: Ilija Tovilo <ilija.tovilo@me.com>
Date:   Sun Nov 29 20:24:02 2026 +0100

    Fix TSSA type inference after FE_FETCH_R[W] with identical branches

    After FE_FETCH_R[W] with two identical branch targets is traced, the JIT can no
    longer identify whether op2 was populated.

    Closes GH-25902
    Co-authored-by: Arnaud Le Blanc <arnaud.lb@gmail.com>

diff --git a/NEWS b/NEWS
index 02cd2da67a0..bec4e23c2c1 100644
--- a/NEWS
+++ b/NEWS
@@ -14,6 +14,8 @@ PHP                                                                        NEWS
   . Fixed bug GH-25926 (Cross-request UAF read in opcache_get_configuration()
     via dangling JIT_G(options) after runtime ini_set('opcache.jit')).
     (David Carlier)
+  . Fixed type inference of JIT trace after FE_FETCH_R[W] with identical
+    branches. (ilutov, Arnaud)

 - Standard:
   . Fixed bug GH-25959 ("consumed" stream filter loses or repeats data).
diff --git a/ext/opcache/jit/zend_jit.c b/ext/opcache/jit/zend_jit.c
index 73bc8fb48d9..ce598cf4f89 100644
--- a/ext/opcache/jit/zend_jit.c
+++ b/ext/opcache/jit/zend_jit.c
@@ -2492,7 +2492,7 @@ static int zend_jit(const zend_op_array *op_array, zend_ssa *ssa, const zend_op
 						if ((op1_info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) != MAY_BE_ARRAY) {
 							break;
 						}
-						if (!zend_jit_fe_reset(&ctx, opline, op1_info)) {
+						if (!zend_jit_fe_reset(&ctx, opline, op1_info, NULL)) {
 							goto jit_failure;
 						}
 						goto done;
diff --git a/ext/opcache/jit/zend_jit_ir.c b/ext/opcache/jit/zend_jit_ir.c
index 2ba348cab28..6f4b2d7dcba 100644
--- a/ext/opcache/jit/zend_jit_ir.c
+++ b/ext/opcache/jit/zend_jit_ir.c
@@ -13724,7 +13724,7 @@ static int zend_jit_assign_dim_op(zend_jit_ctx   *jit,
 	return 1;
 }

-static int zend_jit_fe_reset(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info)
+static int zend_jit_fe_reset(zend_jit_ctx *jit, const zend_op *opline, uint32_t op1_info, zend_jit_trace_rec *trace)
 {
 	zend_jit_addr res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, opline->result.var);

@@ -13742,6 +13742,13 @@ static int zend_jit_fe_reset(zend_jit_ctx *jit, const zend_op *opline, uint32_t
 	// JIT: Z_FE_POS_P(res) = 0;
 	ir_STORE(ir_ADD_OFFSET(jit_FP(jit), opline->result.var + offsetof(zval, u2.fe_pos)), ir_CONST_U32(0));

+	if (trace && trace->op == ZEND_JIT_TRACE_END  && trace->stop == ZEND_JIT_TRACE_STOP_INTERPRETER) {
+		/* This is reachable only when FE_FETCH_R[W] ended the trace, so we know we branched to opline+1. */
+		if (!zend_jit_set_ip(jit, opline + 1)) {
+			return 0;
+		}
+	}
+
 	return 1;
 }

diff --git a/ext/opcache/jit/zend_jit_trace.c b/ext/opcache/jit/zend_jit_trace.c
index c974796ae68..2e013355dff 100644
--- a/ext/opcache/jit/zend_jit_trace.c
+++ b/ext/opcache/jit/zend_jit_trace.c
@@ -6264,7 +6264,7 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par
 						if ((op1_info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) != MAY_BE_ARRAY) {
 							break;
 						}
-						if (!zend_jit_fe_reset(&ctx, opline, op1_info)) {
+						if (!zend_jit_fe_reset(&ctx, opline, op1_info, p + 1)) {
 							goto jit_failure;
 						}
 						goto done;
diff --git a/ext/opcache/jit/zend_jit_vm_helpers.c b/ext/opcache/jit/zend_jit_vm_helpers.c
index 8b85324ef89..61587efa909 100644
--- a/ext/opcache/jit/zend_jit_vm_helpers.c
+++ b/ext/opcache/jit/zend_jit_vm_helpers.c
@@ -693,6 +693,12 @@ zend_jit_trace_stop ZEND_FASTCALL zend_jit_trace_execute(zend_execute_data  *ex,
 	}

 	while (1) {
+		if ((opline->opcode == ZEND_FE_FETCH_R || opline->opcode == ZEND_FE_FETCH_RW)
+		 /* The taken branch of a FE_FETCH_R[W] trace with two identical branches is unidentifiable. */
+		 && UNEXPECTED(ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value) == opline + 1)) {
+			stop = ZEND_JIT_TRACE_STOP_INTERPRETER;
+			break;
+		}
 		ce1 = ce2 = NULL;
 		op1_type = op2_type = op3_type = IS_UNKNOWN;
 		if ((opline->op1_type & (IS_TMP_VAR|IS_VAR|IS_CV))
diff --git a/ext/opcache/tests/jit/fe_fetch_with_identical_branches_001.phpt b/ext/opcache/tests/jit/fe_fetch_with_identical_branches_001.phpt
new file mode 100644
index 00000000000..58646b88162
--- /dev/null
+++ b/ext/opcache/tests/jit/fe_fetch_with_identical_branches_001.phpt
@@ -0,0 +1,31 @@
+--TEST--
+JIT: Incorrect TSSA type inference after FE_FETCH with identical branches
+--EXTENSIONS--
+opcache
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.jit=tracing
+opcache.jit_buffer_size=16M
+--FILE--
+<?php
+
+class C {
+    public int $prop = 0;
+}
+
+function test() {
+    foreach ([new C()] as $c) {
+        break;
+    }
+
+    echo $c->prop, "\n";
+}
+
+test();
+test();
+
+?>
+--EXPECT--
+0
+0
diff --git a/ext/opcache/tests/jit/fe_fetch_with_identical_branches_002.phpt b/ext/opcache/tests/jit/fe_fetch_with_identical_branches_002.phpt
new file mode 100644
index 00000000000..1bb5e261164
--- /dev/null
+++ b/ext/opcache/tests/jit/fe_fetch_with_identical_branches_002.phpt
@@ -0,0 +1,27 @@
+--TEST--
+JIT: Incorrect TSSA type inference after FE_FETCH with identical branches
+--EXTENSIONS--
+opcache
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.jit=tracing
+opcache.jit_buffer_size=16M
+--FILE--
+<?php
+
+function first(array $a) {
+    foreach ($a as $v) { break; }
+    return $v;
+}
+
+for ($i = 0; $i < 4; $i++) {
+    var_dump(first([$i, 2, 3]));
+}
+
+?>
+--EXPECT--
+int(0)
+int(1)
+int(2)
+int(3)