Skip to content

Commit 40c6861

Browse files
zonuexeclaude
authored andcommitted
Pass immutable array arguments non-refcounted through zv::Args
zv::Args stored a HashTable argument with a bare ZVAL_ARR, which types an immutable table (the shared zend_empty_array of a PHP [] literal or zv::Arr::empty()) as refcounted. When the argument vector reached a PHP method, the addref in zend_call_function() wrote into read-only memory (SIGBUS on macOS arm64, SIGSEGV on Linux). ForeachHandler hit this for a constant array without keys: its conditional holder tables stay empty and are handed to a PHP override of MutatingScope::addConditionalExpressions(). Wrap immutable tables as plain IS_ARRAY, like zv::Arr::adoptTable() and copyOfTable() do. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c5e486c commit 40c6861

4 files changed

Lines changed: 122 additions & 1 deletion

File tree

‎build/php-85.neon‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ parameters:
1212
count: 1
1313
path: ../src/Testing/TestCaseSourceLocatorFactory.php
1414

15+
-
16+
rawMessage: 'Call to deprecated method setAccessible() of class ReflectionProperty.'
17+
identifier: method.deprecated
18+
count: 1
19+
path: ../tests/PHPStan/Analyser/StmtHandler/ForeachHandlerTest.php
20+
1521
-
1622
rawMessage: 'Call to deprecated method setAccessible() of class ReflectionProperty.'
1723
identifier: method.deprecated
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\StmtHandler;
4+
5+
use PhpParser\Node\Expr;
6+
use PHPStan\Analyser\ConditionalExpressionHolder;
7+
use PHPStan\Analyser\MutatingScope;
8+
use PHPStan\Type\Type;
9+
use function count;
10+
11+
/**
12+
* A MutatingScope subclass overriding addConditionalExpressions(), so the
13+
* native engine has to hand the holder tables over through a PHP call.
14+
* enterForeach() and mergeWith() answer $this to keep this subclass the
15+
* scope ForeachHandler narrows.
16+
*/
17+
final class ConditionalExpressionsRecordingScope extends MutatingScope
18+
{
19+
20+
/** @var list<array{string, int}> */
21+
public array $added = [];
22+
23+
public function enterForeach(MutatingScope $originalScope, Expr $iteratee, Type $iterateeType, Type $nativeIterateeType, string $valueName, ?string $keyName, bool $valueByRef): MutatingScope
24+
{
25+
return $this;
26+
}
27+
28+
public function mergeWith(?MutatingScope $otherScope, bool $preserveVacuousConditionals = false): MutatingScope
29+
{
30+
return $this;
31+
}
32+
33+
/**
34+
* @param ConditionalExpressionHolder[] $conditionalExpressionHolders
35+
*/
36+
public function addConditionalExpressions(string $exprString, array $conditionalExpressionHolders): MutatingScope
37+
{
38+
$this->added[] = [$exprString, count($conditionalExpressionHolders)];
39+
40+
return $this;
41+
}
42+
43+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\StmtHandler;
4+
5+
use PhpParser\Node\Expr\Variable;
6+
use PhpParser\Node\Stmt\Foreach_;
7+
use PHPStan\Analyser\ExpressionResultStorage;
8+
use PHPStan\Analyser\MutatingScope;
9+
use PHPStan\Analyser\NodeScopeResolver;
10+
use PHPStan\Analyser\NoopNodeCallback;
11+
use PHPStan\Analyser\ScopeContext;
12+
use PHPStan\Analyser\StatementContext;
13+
use PHPStan\Testing\PHPStanTestCase;
14+
use PHPStan\TrinaryLogic;
15+
use PHPStan\Type\Constant\ConstantArrayType;
16+
use PHPStan\Type\Constant\ConstantIntegerType;
17+
use PHPStan\Type\Constant\ConstantStringType;
18+
use PHPUnit\Framework\Attributes\DataProvider;
19+
use ReflectionClass;
20+
21+
class ForeachHandlerTest extends PHPStanTestCase
22+
{
23+
24+
public static function dataConstantArrayConditionalHolders(): iterable
25+
{
26+
// no keys: both holder tables stay the empty array literal
27+
yield [new ConstantArrayType([], []), 0];
28+
yield [new ConstantArrayType([new ConstantStringType('a')], [new ConstantIntegerType(1)]), 1];
29+
}
30+
31+
#[DataProvider('dataConstantArrayConditionalHolders')]
32+
public function testConstantArrayConditionalHoldersReachScopeOverride(ConstantArrayType $iterateeType, int $expectedHolders): void
33+
{
34+
$container = self::getContainer();
35+
$scope = self::createScopeFactory(self::createReflectionProvider(), $container->getService('typeSpecifier'))
36+
->create(ScopeContext::create(__FILE__))
37+
->assignVariable('a', $iterateeType, $iterateeType, TrinaryLogic::createYes());
38+
// the scope factory only creates MutatingScope: rebuild the scope as
39+
// the subclass from its constructor arguments
40+
$args = [];
41+
$reflection = new ReflectionClass(MutatingScope::class);
42+
foreach ($reflection->getMethod('__construct')->getParameters() as $parameter) {
43+
$property = $reflection->getProperty($parameter->getName());
44+
$property->setAccessible(true);
45+
$args[] = $property->getValue($scope);
46+
}
47+
$scope = new ConditionalExpressionsRecordingScope(...$args);
48+
49+
$container->getByType(ForeachHandler::class)->processStmt(
50+
$container->getByType(NodeScopeResolver::class),
51+
new Foreach_(new Variable('a'), new Variable('v'), ['keyVar' => new Variable('k')]),
52+
$scope,
53+
new ExpressionResultStorage(),
54+
new NoopNodeCallback(),
55+
StatementContext::createDeep(),
56+
);
57+
58+
$this->assertSame([
59+
['$v', $expectedHolders],
60+
['$a[$k]', $expectedHolders],
61+
], $scope->added);
62+
}
63+
64+
}

‎turbo-ext/src/zv.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,15 @@ class Args
589589
static zend_always_inline void set(zval *slot, const zval *value) { ZVAL_COPY_VALUE(slot, value); }
590590
static zend_always_inline void set(zval *slot, zend_object *value) { ZVAL_OBJ(slot, value); }
591591
static zend_always_inline void set(zval *slot, zend_string *value) { ZVAL_STR(slot, value); }
592-
static zend_always_inline void set(zval *slot, HashTable *value) { ZVAL_ARR(slot, value); }
592+
/* immutable tables (a PHP [] literal) are wrapped non-refcounted, like
593+
* ZVAL_EMPTY_ARRAY: the call's addref of its arguments must not touch them */
594+
static zend_always_inline void set(zval *slot, HashTable *value)
595+
{
596+
ZVAL_ARR(slot, value);
597+
if (GC_FLAGS(value) & IS_ARRAY_IMMUTABLE) {
598+
Z_TYPE_INFO_P(slot) = IS_ARRAY;
599+
}
600+
}
593601
static zend_always_inline void set(zval *slot, bool value) { ZVAL_BOOL(slot, value); }
594602
static zend_always_inline void set(zval *slot, zend_long value) { ZVAL_LONG(slot, value); }
595603
static zend_always_inline void set(zval *slot, double value) { ZVAL_DOUBLE(slot, value); }

0 commit comments

Comments
 (0)