From 7faf1b332c3aa5ea5afb2ef63ac6605816cc159a Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Wed, 2 Dec 2026 21:01:47 +0100 Subject: [PATCH] Fix crash on memory limit error before the opline is saved 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. --- NEWS | 4 +++ Zend/tests/oom_stale_opline_001.phpt | 28 +++++++++++++++ Zend/tests/oom_stale_opline_002.phpt | 27 +++++++++++++++ Zend/zend_execute_API.c | 14 +++++++- .../jit/oom_stale_opline_runtime_jit.phpt | 34 +++++++++++++++++++ 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 Zend/tests/oom_stale_opline_001.phpt create mode 100644 Zend/tests/oom_stale_opline_002.phpt create mode 100644 ext/opcache/tests/jit/oom_stale_opline_runtime_jit.phpt diff --git a/NEWS b/NEWS index 1b3b63932f95..e8996332e1ec 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 000000000000..cdb8eb96e26d --- /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-- + +--INI-- +memory_limit=8M +--FILE-- + +--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 000000000000..4159cdf8a5b2 --- /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-- + +--INI-- +memory_limit=32M +--FILE-- + +--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 aeb68bdd6be5..38fdc02ab1c9 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 000000000000..b47343dee1b0 --- /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-- + +--INI-- +memory_limit=8M +opcache.enable_cli=1 +opcache.jit=1215 +--FILE-- + +--CLEAN-- + +--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