Skip to content

Commit e87cd63

Browse files
ondrejmirtesclaude
andcommitted
Continue the native recursions over deep chains on a fresh C stack
ExpressionResult's collectReadVariableNames(), IssetabilityResolution's isSet()/isSetUndefined() down the inner chain and IssetabilityDescriptor's inner resolution recursed on the C stack, where their PHP twins recurse on the VM stack: a deep concatenation chain segfaulted, and a deep resolution chain threw "Maximum call stack size reached" where the twin answers. Each recursive step now goes through pt_engine_with_stack(), which continues on a fresh C stack segment when the current one runs low. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 78d7959 commit e87cd63

4 files changed

Lines changed: 69 additions & 8 deletions

File tree

‎turbo-ext/src/ExpressionResult.cpp‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,10 +1051,13 @@ class ExpressionResult
10511051
return zv::Val(std::move(result));
10521052
}
10531053

1054-
/* foreach (self::collectReadVariableNames($subNode) as $name) $names[$name] = true */
1054+
/* foreach (self::collectReadVariableNames($subNode) as $name) $names[$name] = true
1055+
* — the recursion continues on a fresh C stack segment when the current
1056+
* one runs low (the twin recursed on the VM stack) */
10551057
static bool mergeNames(zv::Arr &names, zend_object *subNode)
10561058
{
1057-
zv::Val subNames = collectReadVariableNames(subNode);
1059+
zv::Val subNames;
1060+
pt_engine_with_stack([&]() { subNames = collectReadVariableNames(subNode); });
10581061
if (UNEXPECTED(subNames.isUndef())) return false;
10591062
for (auto entry : zv::TableRef(Z_ARRVAL_P(subNames.raw()))) {
10601063
zval trueValue;

‎turbo-ext/src/IssetabilityDescriptor.cpp‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace sigs = ptdecl::IssetabilityDescriptor::sig;
2626
#include "TypeTraits.h"
2727
#include "TypeOps.h"
2828
#include "AnalyserValues.h"
29+
#include "Engine.h"
2930

3031
#pragma GCC diagnostic push
3132
#pragma GCC diagnostic ignored "-Wpragmas"
@@ -146,6 +147,16 @@ class IssetabilityDescriptor
146147
pt_write_slot(self, index, &null);
147148
}
148149

150+
/* $result->getIssetabilityResolution(...) of the inner link's result: the
151+
* native recursion down the chain continues on a fresh C stack segment
152+
* when the current one runs low (the twin recursed on the VM stack) */
153+
static zv::Val innerResolution(zval *result, zval *scope, bool useNativeTypes, bool reprocessUntrackedLinks)
154+
{
155+
zv::Val inner;
156+
pt_engine_with_stack([&]() { inner = pt_expression_result_get_issetability_resolution(result, scope, useNativeTypes, reprocessUntrackedLinks); });
157+
return inner;
158+
}
159+
149160
/* a nullable slot, borrowed; NULL with the uninitialized-read Error pending */
150161
zval *slot(uint32_t index, const char *name) const
151162
{
@@ -253,7 +264,7 @@ class IssetabilityDescriptor
253264
if (UNEXPECTED(isExprTracked < 0)) return zv::Val();
254265
zv::Val link = pt_issetability_link_info_offset(isOffsetAccessible.raw(), hasOffsetValue.raw(), isExprTracked == PT_TRI_YES, varType.raw(), dimType.raw(), valueType.raw());
255266
if (UNEXPECTED(link.isUndef())) return zv::Val();
256-
zv::Val inner = pt_expression_result_get_issetability_resolution(varResult, scope, useNativeTypes, reprocessUntrackedLinks);
267+
zv::Val inner = innerResolution(varResult, scope, useNativeTypes, reprocessUntrackedLinks);
257268
return newResolution(std::move(link), std::move(inner));
258269
}
259270

@@ -270,7 +281,7 @@ class IssetabilityDescriptor
270281
zval *innerResult = slot(slots::innerResult, "innerResult");
271282
if (UNEXPECTED(innerResult == NULL)) return zv::Val();
272283
zv::Val inner = Z_TYPE_P(innerResult) != IS_NULL
273-
? pt_expression_result_get_issetability_resolution(innerResult, scope, useNativeTypes, reprocessUntrackedLinks)
284+
? innerResolution(innerResult, scope, useNativeTypes, reprocessUntrackedLinks)
274285
: zv::Val::null();
275286
if (UNEXPECTED(inner.isUndef())) return zv::Val();
276287

‎turbo-ext/src/IssetabilityResolution.cpp‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,16 @@ class IssetabilityResolution
261261
return isSet(typeCallback.raw(), NULL);
262262
}
263263

264-
/* $inner->isSet($typeCallback, $result) of an inner resolution */
264+
/* $inner->isSet($typeCallback, $result) of an inner resolution; the
265+
* native recursion down the chain continues on a fresh C stack segment
266+
* when the current one runs low (the twin recursed on the VM stack) */
265267
static zv::Val isSetOf(zval *resolution, zval *typeCallback, zval *result)
266268
{
267269
if (EXPECTED(Z_TYPE_P(resolution) == IS_OBJECT && Z_OBJCE_P(resolution) == pt_ce_issetability_resolution)) {
268270
zv::Val held = zv::Val::copyOf(zv::Ref(resolution));
269-
return IssetabilityResolution(Z_OBJ_P(held.raw())).isSet(typeCallback, result);
271+
zv::Val isSetResult;
272+
pt_engine_with_stack([&]() { isSetResult = IssetabilityResolution(Z_OBJ_P(held.raw())).isSet(typeCallback, result); });
273+
return isSetResult;
270274
}
271275
if (UNEXPECTED(Z_TYPE_P(resolution) != IS_OBJECT)) {
272276
zend_throw_error(NULL, "Call to a member function isSet() on %s", zend_zval_value_name(resolution));
@@ -278,12 +282,15 @@ class IssetabilityResolution
278282
return pt_type_call(Z_OBJ_P(resolution), PT_LC("isset"), 2, argv);
279283
}
280284

281-
/* $inner->isSetUndefined() of an inner resolution */
285+
/* $inner->isSetUndefined() of an inner resolution (on a fresh C stack
286+
* segment when the current one runs low, like isSetOf()) */
282287
static zv::Val isSetUndefinedOf(zval *resolution)
283288
{
284289
if (EXPECTED(Z_TYPE_P(resolution) == IS_OBJECT && Z_OBJCE_P(resolution) == pt_ce_issetability_resolution)) {
285290
zv::Val held = zv::Val::copyOf(zv::Ref(resolution));
286-
return IssetabilityResolution(Z_OBJ_P(held.raw())).isSetUndefined();
291+
zv::Val isSetResult;
292+
pt_engine_with_stack([&]() { isSetResult = IssetabilityResolution(Z_OBJ_P(held.raw())).isSetUndefined(); });
293+
return isSetResult;
287294
}
288295
if (UNEXPECTED(Z_TYPE_P(resolution) != IS_OBJECT)) {
289296
zend_throw_error(NULL, "Call to a member function isSetUndefined() on %s", zend_zval_value_name(resolution));

‎turbo-ext/tests/analyser-values.php‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ function check(bool $cond, string $msg): void
4747
$avContainer = $avContainerFactory->create(sys_get_temp_dir() . '/phpstan-turbo-smoke', [$avContainerFactory->getConfigDirectory() . '/config.level8.neon'], []);
4848
$avReflectionProvider = $avContainer->getByType(\PHPStan\Reflection\ReflectionProvider::class);
4949
$avPrecise = \PHPStan\Type\VerbosityLevel::precise();
50+
// the length of the deep chains below: the native recursions over them run
51+
// out of the C stack without pt_engine_with_stack(), the twins recurse on the
52+
// VM stack and do not
53+
$avDeepChainDepth = 100000;
54+
// the deep chains stay referenced until shutdown: releasing one mid-run frees
55+
// it recursively, which runs out of the C stack in the engine itself
56+
$avDeepChainsKeptAlive = [];
5057

5158
// PHP types for the scopes and the issetability chains: the PHP twins of the
5259
// links and resolutions type-hint the PHP TrinaryLogic the PHP types answer
@@ -337,6 +344,26 @@ function check(bool $cond, string $msg): void
337344
(new \ReflectionProperty($c['IssetabilityDescriptor'], 'kind'))->setValue($broken, 'something else');
338345
$r['issetability broken property'] = $avCatch(static fn () => $broken->resolve($avScope, false, $avN['a']));
339346

347+
// deep chains (a fresh tree per side: the read variable names are cached
348+
// on the nodes)
349+
$deepConcat = new \PhpParser\Node\Expr\Variable('a0');
350+
for ($i = 1; $i < $avDeepChainDepth; $i++) {
351+
$deepConcat = new \PhpParser\Node\Expr\BinaryOp\Concat($deepConcat, new \PhpParser\Node\Expr\Variable('a' . ($i % 3)));
352+
}
353+
$r['deep read variable names'] = $avCatch(static fn () => $newResult($deepConcat, $avInt)->askScopeVariableStateMatches($avOtherScope, false));
354+
$deepDescriptor = $c['IssetabilityDescriptor']::variable('arr');
355+
for ($i = 1; $i < $avDeepChainDepth; $i++) {
356+
$deepDescriptor = $i % 2 === 0
357+
? $c['IssetabilityDescriptor']::offset($newResult($avN['arr'], $avArray, descriptor: $deepDescriptor), $newResult($avN['one'], $avInt))
358+
: $c['IssetabilityDescriptor']::property($newResult($avN['arr'], $avArray, descriptor: $deepDescriptor), $resolverFor('parent'), $avN['thisParent']);
359+
}
360+
$r['deep issetability descriptor'] = $avCatch(static function () use ($deepDescriptor, $avScope, $avN, &$avDeepChainsKeptAlive): bool {
361+
$resolution = $deepDescriptor->resolve($avScope, false, $avN['dimA']);
362+
$avDeepChainsKeptAlive[] = $resolution;
363+
return $resolution->getLink()->isProperty();
364+
});
365+
$avDeepChainsKeptAlive[] = [$deepConcat, $deepDescriptor];
366+
340367
$avResults[$side] = $r;
341368
}
342369
foreach ($avResults['php'] as $label => $described) {
@@ -915,6 +942,19 @@ public function resolveConditionalTypes(\PHPStan\Type\Type $type): \PHPStan\Type
915942
$r['resolution uninitialized not empty'] = $avCatch(static fn () => (new \ReflectionClass($R))->newInstanceWithoutConstructor()->notEmpty());
916943
$r['resolution uninitialized link'] = $avCatch(static fn () => (new \ReflectionClass($R))->newInstanceWithoutConstructor()->getLink());
917944
$r['resolution named'] = $avDescribe(new $R(inner: null, link: $links['leaf']));
945+
// every link asks the next one: through isSet() (an accessible offset
946+
// holding a verdict) and through isSetUndefined() (inaccessible offsets)
947+
$deepResolution = null;
948+
$deepUndefinedResolution = null;
949+
for ($i = 0; $i < $avDeepChainDepth; $i++) {
950+
$deepResolution = new $R($links['offset accessible yes'], $deepResolution);
951+
$deepUndefinedResolution = new $R($links['offset not accessible'], $deepUndefinedResolution);
952+
}
953+
$r['deep resolution'] = [
954+
$avCatch(static fn () => $deepResolution->isSet(static fn (): bool => true)),
955+
$avCatch(static fn () => $deepUndefinedResolution->isSet(static fn (): bool => true)),
956+
];
957+
$avDeepChainsKeptAlive[] = [$deepResolution, $deepUndefinedResolution];
918958

919959
$E = $c['EnsuredNonNullabilityResultExpression'];
920960
$expression = new $E($avN['a'], $avNullableInt, $avInt, $avMaybe);

0 commit comments

Comments
 (0)