Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions Zend/tests/oom_stale_opline_001.phpt
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions Zend/tests/oom_stale_opline_002.phpt
Original file line number Diff line number Diff line change
@@ -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
14 changes: 13 additions & 1 deletion Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
34 changes: 34 additions & 0 deletions ext/opcache/tests/jit/oom_stale_opline_runtime_jit.phpt
Original file line number Diff line number Diff line change
@@ -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
Loading