Commit 42f293689db for php

commit 42f293689db8676ed4b1d36dd86a829a43a4f611
Author: Jorg Sowa <jorg.sowa@gmail.com>
Date:   Sat Dec 5 23:39:46 2026 +0100

    Fix crash on memory limit error before the opline is saved (GH-26073)

    The memory limit error reads EX(opline) for its line number. At function
    entry it is uninitialized with global register VMs and JIT-compiled
    callers, so an allocation before SAVE_OPLINE() dereferenced garbage.
    Fall back to the first line of the function when EX(opline) does not
    point into the frame's opcodes.

    Closes GH-26073

diff --git a/NEWS b/NEWS
index e2984094444..adb9038c4eb 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,10 @@ PHP                                                                        NEWS
 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 ?? ??? ????, PHP 8.4.30

+- Core:
+  . Fixed crash on memory limit error before a function saved its current
+    opline. (Jorg Sowa)
+
 - Enchant:
   . Fixed enchant_broker_set_dict_path() and enchant_broker_get_dict_path()
     returning null with libenchant 2. Both now return false. (lacatoire)
diff --git a/Zend/tests/oom_stale_opline_001.phpt b/Zend/tests/oom_stale_opline_001.phpt
new file mode 100644
index 00000000000..cdb8eb96e26
--- /dev/null
+++ b/Zend/tests/oom_stale_opline_001.phpt
@@ -0,0 +1,28 @@
+--TEST--
+Memory limit exceeded before a function saved its opline must not read a stale opline
+--SKIPIF--
+<?php
+if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
+if (PHP_INT_SIZE != 8) die("skip the VM stack layout below assumes 64-bit");
+?>
+--INI--
+memory_limit=8M
+--FILE--
+<?php
+function f($a, $b) {
+    // CONCAT allocates the result before saving the opline
+    return $a . $b;
+}
+
+$a = str_repeat('a', 2 * 1024 * 1024);
+$b = 'b';
+// Leave ~1M below the limit so the concatenation exceeds it
+$ballast = str_repeat('x', 8 * 1024 * 1024 - memory_get_usage(true) - 1024 * 1024);
+
+// The 4th argument lands in the VM stack slot later used as EX(opline) of f()
+$bogus_opline = 1;
+$unused = max(0, 0, 0, $bogus_opline);
+$unused = max(0, 0, f($a, $b));
+?>
+--EXPECTF--
+Fatal error: Allowed memory size of %d bytes exhausted%S(tried to allocate %d bytes) in %s on line %r(2|4)%r
diff --git a/Zend/tests/oom_stale_opline_002.phpt b/Zend/tests/oom_stale_opline_002.phpt
new file mode 100644
index 00000000000..4159cdf8a5b
--- /dev/null
+++ b/Zend/tests/oom_stale_opline_002.phpt
@@ -0,0 +1,27 @@
+--TEST--
+Hard memory limit exceeded before a function saved its opline must not read a stale opline
+--SKIPIF--
+<?php
+if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
+if (PHP_INT_SIZE != 8) die("skip the VM stack layout below assumes 64-bit");
+?>
+--INI--
+memory_limit=32M
+--FILE--
+<?php
+function f($a) {
+    return $a . $a;
+}
+
+// The concatenation exceeds twice the limit
+$a = str_repeat('a', 24 * 1024 * 1024);
+
+// The 4th argument lands in the VM stack slot later used as EX(opline) of f()
+$bogus_opline = 1;
+$unused = max(0, 0, 0, $bogus_opline);
+$unused = max(0, 0, f($a));
+?>
+--EXPECTF--
+Hard memory limit of %d bytes exhausted %S(tried to allocate %d bytes) in %s on line %r(2|3)%r
+
+Termsig=9
diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c
index aeb68bdd6be..38fdc02ab1c 100644
--- a/Zend/zend_execute_API.c
+++ b/Zend/zend_execute_API.c
@@ -684,6 +684,18 @@ ZEND_API zend_string *zend_get_executed_filename_ex(void) /* {{{ */
 }
 /* }}} */

+static bool zend_is_frame_opline(const zend_op_array *op_array, const zend_op *opline)
+{
+	/* Trampolines and dummy frames (e.g. for attributes) point outside their opcodes */
+	if (op_array->fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
+		return opline != NULL;
+	}
+	if ((uintptr_t)opline - (uintptr_t)op_array->opcodes < op_array->last * sizeof(zend_op)) {
+		return true;
+	}
+	return (uintptr_t)opline - (uintptr_t)EG(exception_op) < sizeof(EG(exception_op));
+}
+
 ZEND_API uint32_t zend_get_executed_lineno(void) /* {{{ */
 {
 	zend_long lineno_override = EG(lineno_override);
@@ -697,7 +709,7 @@ ZEND_API uint32_t zend_get_executed_lineno(void) /* {{{ */
 		ex = ex->prev_execute_data;
 	}
 	if (ex) {
-		if (!ex->opline) {
+		if (!zend_is_frame_opline(&ex->func->op_array, ex->opline)) {
 			/* Missing SAVE_OPLINE()? Falling back to first line of function */
 			return ex->func->op_array.opcodes[0].lineno;
 		}
diff --git a/ext/opcache/tests/jit/oom_stale_opline_runtime_jit.phpt b/ext/opcache/tests/jit/oom_stale_opline_runtime_jit.phpt
new file mode 100644
index 00000000000..b47343dee1b
--- /dev/null
+++ b/ext/opcache/tests/jit/oom_stale_opline_runtime_jit.phpt
@@ -0,0 +1,34 @@
+--TEST--
+JIT: OOM during first-exec compilation must not read a stale opline
+--EXTENSIONS--
+opcache
+--SKIPIF--
+<?php
+if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
+if (PHP_INT_SIZE != 8) die("skip the VM stack layout below assumes 64-bit");
+?>
+--INI--
+memory_limit=8M
+opcache.enable_cli=1
+opcache.jit=1215
+--FILE--
+<?php
+// Eval'd code is not JIT-compiled, so the function has to come from a file
+$file = __DIR__ . '/oom_stale_opline_runtime_jit.inc';
+file_put_contents($file, "<?php\nfunction test(\$a) {\n" . str_repeat("    \$a += 1;\n", 500) . "    return \$a;\n}\n");
+include $file;
+
+// Leave ~1M below the limit so JIT compilation of test() exceeds it
+$ballast = str_repeat('x', 8 * 1024 * 1024 - memory_get_usage(true) - 1024 * 1024);
+
+// The 4th argument lands in the VM stack slot later used as EX(opline) of test()
+$bogus_opline = 1;
+$unused = max(0, 0, 0, $bogus_opline);
+$unused = max(0, 0, test(1));
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/oom_stale_opline_runtime_jit.inc');
+?>
+--EXPECTF--
+Fatal error: Allowed memory size of %d bytes exhausted%S(tried to allocate %d bytes) in %soom_stale_opline_runtime_jit.inc on line %r(2|3)%r