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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ PHP NEWS
. Fixed use-after-free in JIT when the error handler releases a string while
an "Undefined variable" warning is raised for its offset in ?? or nested
isset()/empty(). (Jorg Sowa)
. Fixed bug GH-25449 (Element of a by-reference function result passed as
an argument arrives as null). (Ilia Alshanetsky)

- OpenSSL:
. Fixed bug GH-25172 (TLS session reuse shares request-lifetime callback
Expand Down
36 changes: 36 additions & 0 deletions Zend/Optimizer/optimize_func_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,34 @@ static void zend_delete_call_instructions(const zend_op_array *op_array, zend_op
}
}

static bool zend_fetch_func_arg_container_to_r(const zend_op_array *op_array, zend_op *opline)
{
zend_op *def = opline;

while (def > op_array->opcodes) {
def--;
if (def->result_type == IS_VAR && def->result.var == opline->op1.var) {
switch (def->opcode) {
case ZEND_DO_FCALL:
case ZEND_DO_ICALL:
case ZEND_DO_UCALL:
case ZEND_DO_FCALL_BY_NAME:
def->result_type = IS_TMP_VAR;
opline->op1_type = IS_TMP_VAR;
return true;
case ZEND_FETCH_R:
case ZEND_FETCH_DIM_R:
case ZEND_FETCH_OBJ_R:
case ZEND_FETCH_STATIC_PROP_R:
return true;
default:
return false;
}
}
}
return false;
}

static void zend_try_inline_call(zend_op_array *op_array, const zend_op *fcall, zend_op *opline, const zend_function *func)
{
const uint32_t no_discard = RETURN_VALUE_USED(opline) ? 0 : ZEND_ACC_NODISCARD;
Expand Down Expand Up @@ -274,6 +302,14 @@ void zend_optimize_func_calls(zend_op_array *op_array, zend_optimizer_ctx *ctx)
break;
}

if ((opline->opcode == ZEND_FETCH_OBJ_FUNC_ARG || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG)
&& opline->op1_type == IS_VAR
&& !zend_fetch_func_arg_container_to_r(op_array, opline)) {
call_stack[call - 1].try_inline = false;
call_stack[call - 1].last_check_func_arg_opline = NULL;
break;
}

if (opline->opcode != ZEND_FETCH_STATIC_PROP_FUNC_ARG) {
opline->opcode -= 12;
} else {
Expand Down
66 changes: 66 additions & 0 deletions ext/opcache/tests/opt/gh25449.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
--TEST--
GH-25449 (Fetch on a by-reference call result passed as a by-value argument)
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.optimization_level=-1
opcache.file_update_protection=0
--EXTENSIONS--
opcache
--FILE--
<?php
function &registry(): array {
static $r = ['k' => 'func'];
return $r;
}

class K {
public $p = ['k' => 'prop'];

public static function &staticRef(): array {
static $r = ['k' => 'static'];
return $r;
}

public function &methodRef(): array {
return $this->p;
}

public function &self(): K {
return $this;
}
}

function dim(): string { return consume(registry()['k']); }
function staticDim(): string { return consume(K::staticRef()['k']); }
function methodDim(K $o): string { return consume($o->methodRef()['k']); }
function objDim(K $o): string { return consume($o->self()->p['k']); }
function chainedDim(): string { return consume(registry()['k'][0]); }
function closureDim(Closure $c): string { return consume($c()['k']); }
function assignRefDim(): string { $x = ['k' => 'assign-ref']; return consume(($y = &$x)['k']); }
function assignRefObj(): string { $x = new K; return consume(($y = &$x)->p['k']); }
function listRefDim(): string { $a = [['k' => 'list-ref']]; return consume(([&$z] = $a)[0]['k']); }

function consume(string $s): string { return $s; }

$o = new K;
echo "dim: ", dim(), "\n";
echo "static: ", staticDim(), "\n";
echo "method: ", methodDim($o), "\n";
echo "obj: ", objDim($o), "\n";
echo "chained: ", chainedDim(), "\n";
echo "closure: ", closureDim(function &(): array { return registry(); }), "\n";
echo "assign-ref: ", assignRefDim(), "\n";
echo "assign-ref obj: ", assignRefObj(), "\n";
echo "list-ref: ", listRefDim(), "\n";
?>
--EXPECT--
dim: func
static: static
method: prop
obj: prop
chained: f
closure: func
assign-ref: assign-ref
assign-ref obj: prop
list-ref: list-ref
Loading