Skip to content
Merged
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
52 changes: 50 additions & 2 deletions lib/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8192,7 +8192,10 @@ private function inheritErrorSuppressExpressionSlots(Block $suppressCompiled, Bl
) {
continue;
}
if ($this->errorSuppressEndBlockCallArgHasTrailingHoistedScalarProducer($endCompiled, $endChild, (int) $argIndex)) {
if (
$this->errorSuppressEndBlockCallArgHasTrailingHoistedScalarProducer($endCompiled, $endChild, (int) $argIndex)
|| $this->errorSuppressEndBlockCallArgHasTrailingHoistedArrayProducer($endCompiled, $endChild, (int) $argIndex)
) {
continue;
}
// First dead temp in the outer call is the @ inner value (#15916, #10302).
Expand Down Expand Up @@ -28693,10 +28696,55 @@ private function errorSuppressEndBlockInnerResultSlotForCallArg(
if ($this->errorSuppressEndBlockCallArgHasTrailingHoistedScalarProducer($block, $cfgCallOp, $argIndex)) {
return null;
}
if ($this->errorSuppressEndBlockCallArgHasTrailingHoistedArrayProducer($block, $cfgCallOp, $argIndex)) {
return null;
}

return $this->errorSuppressEndBlockInnerResultSlot($block);
}

/**
* Trailing hoisted Array_ before a post-@ call feeds this dead-temp arg (#16205).
*/
private function errorSuppressEndBlockCallArgHasTrailingHoistedArrayProducer(
Block $block,
Op $cfgCallOp,
int $argIndex
): bool {
if (null === $block->orig || !property_exists($cfgCallOp, 'args') || !\is_array($cfgCallOp->args)) {
return false;
}
$callArg = $cfgCallOp->args[$argIndex] ?? null;
if (!$callArg instanceof Operand || $this->isEmbeddedCallLiteralArg($callArg)) {
return false;
}
if (!$this->callArgIsDeadInlineTemporary($callArg)) {
return false;
}
$arrayProducer = $this->inlineArrayProducerImmediatelyBeforeCfgCall($cfgCallOp, $block);
if (!$arrayProducer instanceof Op\Expr\Array_) {
return false;
}
if (
null !== $arrayProducer->result
&& $this->operandsReferToSameVariable($arrayProducer->result, $callArg)
) {
return true;
}
$nonEmbeddedArgIndices = [];
foreach ($cfgCallOp->args as $i => $candidate) {
if (!$this->isEmbeddedCallLiteralArg($candidate)) {
$nonEmbeddedArgIndices[] = (int) $i;
}
}
$producerOrdinal = array_search($argIndex, $nonEmbeddedArgIndices, true);
if (false === $producerOrdinal) {
return false;
}

return 0 === $producerOrdinal;
}

/**
* Trailing hoisted true/false/null before a post-@ call feeds this dead-temp arg (#15916).
*
Expand Down Expand Up @@ -28779,7 +28827,7 @@ private function errorSuppressEndBlockCallArgHasTrailingHoistedScalarProducer(
/**
* True when an outer call in a post-@ block consumes the suppressed inner expression (#10336, #15916).
*
* Standalone `@mkdir(); stream_context_create(null)` must not wire hoisted null to mkdir's return slot.
* Standalone `@mkdir(); stream_context_create(null|[])` must not wire hoisted literal to mkdir's return slot.
*/
private function callInErrorSuppressEndBlockUsesInnerResultAsArg(Block $block, Op $cfgCallOp): bool
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
stream_context_create([]) after @mkdir — empty literal stays array (#16205, ext/standard/streams.c)
--FILE--
<?php
$dir = sys_get_temp_dir() . '/phpc_stream_ctx_' . getmypid();
@mkdir($dir);
$ctx = stream_context_create([]);
echo is_resource($ctx) ? "ok\n" : "fail\n";
--EXPECT--
ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$dir = sys_get_temp_dir() . '/phpc_stream_ctx_' . getmypid();
@mkdir($dir);
$ctx = stream_context_create([]);
echo is_resource($ctx) ? "ok\n" : "fail\n";
53 changes: 53 additions & 0 deletions test/unit/ErrorSuppressInlineReturnSlotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,57 @@ public function testStandaloneSuppressStatementDoesNotAliasHoistedNullCallArg():
'hoisted null must not alias unrelated @mkdir return slot'
);
}

public function testStandaloneSuppressStatementDoesNotAliasHoistedEmptyArrayCallArg(): void
{
$code = <<<'PHP'
<?php
@mkdir('/tmp/phpc_suppress_hoist_gap');
$ctx = stream_context_create([]);
PHP;
$runtime = new Runtime();
$block = $runtime->parseAndCompile($code, 'suppress_then_hoisted_empty_array.php');

$suppressReturnSlot = null;
$streamContextSendSlot = null;
$walk = static function (Block $b) use (&$walk, &$suppressReturnSlot, &$streamContextSendSlot): void {
$inSuppress = null !== $b->orig && $b->orig instanceof \PHPCfg\ErrorSuppressBlock;
$inEnd = null !== $b->orig && 1 === \count($b->orig->parents) && $b->orig->parents[0] instanceof \PHPCfg\ErrorSuppressBlock;
$pendingInit = false;
foreach ($b->opCodes as $op) {
if (OpCode::TYPE_FUNCCALL_INIT === $op->type) {
$pendingInit = true;
continue;
}
if ($pendingInit && OpCode::TYPE_FUNCCALL_EXEC_RETURN === $op->type) {
if ($inSuppress) {
$suppressReturnSlot = $op->arg1;
}
$pendingInit = false;
continue;
}
if ($pendingInit && OpCode::TYPE_FUNCCALL_EXEC_NORETURN === $op->type) {
$pendingInit = false;
continue;
}
if (OpCode::TYPE_ARG_SEND === $op->type && $inEnd && null === $streamContextSendSlot) {
$streamContextSendSlot = $op->arg1;
}
foreach ([$op->block1, $op->block2, $op->block3] as $sub) {
if ($sub instanceof Block) {
$walk($sub);
}
}
}
};
$walk($block);

self::assertNotNull($suppressReturnSlot, 'suppress mkdir return slot');
self::assertNotNull($streamContextSendSlot, 'stream_context_create arg send slot');
self::assertNotSame(
$suppressReturnSlot,
$streamContextSendSlot,
'hoisted [] must not alias unrelated @mkdir return slot'
);
}
}