diff --git a/NEWS b/NEWS index 1fd03816816e..95fcee8ba439 100644 --- a/NEWS +++ b/NEWS @@ -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 diff --git a/Zend/Optimizer/optimize_func_calls.c b/Zend/Optimizer/optimize_func_calls.c index 4275bdb4071c..375e9d523f9c 100644 --- a/Zend/Optimizer/optimize_func_calls.c +++ b/Zend/Optimizer/optimize_func_calls.c @@ -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; @@ -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 { diff --git a/ext/opcache/tests/opt/gh25449.phpt b/ext/opcache/tests/opt/gh25449.phpt new file mode 100644 index 000000000000..00aa3eda5a7f --- /dev/null +++ b/ext/opcache/tests/opt/gh25449.phpt @@ -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-- + '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