Skip to content

Commit 31aa1ce

Browse files
ondrejmirtesclaude
andcommitted
Check the statement type in the native StatementsHandler::processStatementStep()
The native processStatementStep() used the elements of the caller's statement list as objects without a check and crashed on anything else, where the twin's `Node\Stmt $stmt` parameter throws a TypeError. Throw the same TypeError. The misuse-parity section of smoke.php gains a child-process harness for the engine classes (they cannot be declared next to their twins): the same probes run once over the PHP classes and once over the native ones under their real names, and their outcomes are compared. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 9dae9e4 commit 31aa1ce

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

‎turbo-ext/src/StatementsHandler.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,13 @@ class StatementsHandler
11061106
/* the private processStatementStep() */
11071107
[[nodiscard]] bool processStatementStep(zval *nodeScopeResolver, zval *parentNode, zval *stmts, zend_long i, zval *stmt, zval *state, zval *storage, zval *nodeCallback, zval *context, bool shouldCheckLastStatement)
11081108
{
1109+
/* the twin's `Node\Stmt $stmt` parameter: the caller's list may hold anything */
1110+
zend_class_entry *stmtCe = pt_class(PT_CLASS_STMT);
1111+
if (UNEXPECTED(stmtCe == NULL)) return false;
1112+
if (UNEXPECTED(Z_TYPE_P(stmt) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(stmt), stmtCe))) {
1113+
zend_type_error("PHPStan\\Analyser\\StatementsHandler::processStatementStep(): Argument #5 ($stmt) must be of type PhpParser\\Node\\Stmt, %s given", zend_zval_value_name(stmt));
1114+
return false;
1115+
}
11091116
zend_object *stateObject = Z_OBJ_P(state);
11101117
zval *stateScope = OBJ_PROP_NUM(stateObject, stateSlots::scope);
11111118
if (Z_TYPE_P(OBJ_PROP_NUM(stateObject, stateSlots::alreadyTerminated)) == IS_TRUE) {

‎turbo-ext/tests/smoke.php‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4380,6 +4380,73 @@ public function getComments(): array
43804380
check($lemPhp === $lemResults['native'][$lemLabel], "misuse parity ($lemLabel): " . json_encode($lemPhp) . ' vs ' . json_encode($lemResults['native'][$lemLabel]));
43814381
}
43824382

4383+
$lemExtension = getenv('TURBO_DLL');
4384+
if (!is_string($lemExtension) || $lemExtension === '') {
4385+
$lemExtension = __DIR__ . '/../phpstan_turbo.so';
4386+
}
4387+
// misuse of the engine classes, which cannot be declared next to their twins:
4388+
// the probes run in a child process per implementation (the native classes
4389+
// under their real names, or none), each answering the outcome of every probe
4390+
$lemProbeTemplate = <<<'CHILD'
4391+
<?php declare(strict_types = 1);
4392+
$root = __ROOT__;
4393+
require $root . '/vendor/autoload.php';
4394+
if (__NATIVE__) {
4395+
$twinFiles = [];
4396+
foreach (json_decode(file_get_contents($root . '/vendor/turbo-shadowed-classes.json'), true) as $className => $entry) {
4397+
$twinFiles[$className] = $root . '/' . $entry['php'];
4398+
}
4399+
\PHPStanTurbo\Runtime::configure(require $root . '/vendor/turbo-class-map.php');
4400+
\PHPStanTurbo\Runtime::activateShadowing($twinFiles);
4401+
}
4402+
$containerFactory = new \PHPStan\DependencyInjection\ContainerFactory($root);
4403+
$container = $containerFactory->create(sys_get_temp_dir() . '/phpstan-turbo-smoke-lane-engine', [$containerFactory->getConfigDirectory() . '/config.level8.neon'], []);
4404+
$outcome = static function (callable $callback): array {
4405+
try {
4406+
$value = $callback();
4407+
return ['ok', is_object($value) ? get_class($value) : $value];
4408+
} catch (\Throwable $e) {
4409+
return [get_class($e), preg_replace('~, called in .*$~', '', $e->getMessage())];
4410+
}
4411+
};
4412+
$scope = $container->getByType(\PHPStan\Analyser\ScopeFactory::class)->create(\PHPStan\Analyser\ScopeContext::create(__FILE__));
4413+
$probes = (static function () use ($container, $outcome, $scope): array {
4414+
__BODY__
4415+
})();
4416+
$probes['native'] = (new \ReflectionMethod(\PHPStan\Analyser\StatementsHandler::class, 'doProcessStmtNodes'))->isInternal();
4417+
echo json_encode($probes), "\n";
4418+
CHILD;
4419+
$lemProbe = static function (string $body) use ($lemProbeTemplate, $lemExtension): array {
4420+
$outcomes = [];
4421+
foreach (['php' => false, 'native' => true] as $mode => $native) {
4422+
$script = tempnam(sys_get_temp_dir(), 'phpstan-turbo-lane-engine-');
4423+
file_put_contents($script, strtr($lemProbeTemplate, ['__ROOT__' => var_export(dirname(__DIR__, 2), true), '__NATIVE__' => $native ? 'true' : 'false', '__BODY__' => $body]));
4424+
$output = [];
4425+
exec(sprintf('%s -d memory_limit=-1 -d extension=%s %s 2>&1', escapeshellarg(PHP_BINARY), escapeshellarg($lemExtension), escapeshellarg($script)), $output, $exitCode);
4426+
unlink($script);
4427+
$decoded = $exitCode === 0 ? json_decode((string) end($output), true) : null;
4428+
$outcomes[$mode] = is_array($decoded) ? $decoded : ['exit code' => $exitCode, 'output' => implode("\n", $output)];
4429+
}
4430+
return $outcomes;
4431+
};
4432+
$lemEngineProbes = $lemProbe(<<<'BODY'
4433+
$handler = $container->getByType(\PHPStan\Analyser\StatementsHandler::class);
4434+
$nodeScopeResolver = $container->getByType(\PHPStan\Analyser\NodeScopeResolver::class);
4435+
$doProcess = static fn (array $stmts) => $handler->doProcessStmtNodes($nodeScopeResolver, new \PhpParser\Node\Stmt\Nop(), $stmts, $scope, new \PHPStan\Analyser\ExpressionResultStorage(), static function (): void {
4436+
}, \PHPStan\Analyser\StatementContext::createTopLevel());
4437+
return [
4438+
'doProcessStmtNodes(stdClass)' => $outcome(static fn () => $doProcess([new \stdClass()])),
4439+
'doProcessStmtNodes(string)' => $outcome(static fn () => $doProcess(['echo'])),
4440+
'doProcessStmtNodes(expr)' => $outcome(static fn () => $doProcess([new \PhpParser\Node\Expr\Variable('x')])),
4441+
];
4442+
BODY);
4443+
$lemEngineNative = $lemEngineProbes['native']['native'] ?? null;
4444+
unset($lemEngineProbes['native']['native'], $lemEngineProbes['php']['native']);
4445+
check($lemEngineNative === true, 'misuse parity: the engine probes ran the native classes: ' . json_encode($lemEngineProbes['native']));
4446+
foreach ($lemEngineProbes['php'] as $lemLabel => $lemPhp) {
4447+
check($lemPhp === ($lemEngineProbes['native'][$lemLabel] ?? null), "misuse parity ($lemLabel): " . json_encode($lemPhp) . ' vs ' . json_encode($lemEngineProbes['native'][$lemLabel] ?? $lemEngineProbes['native']));
4448+
}
4449+
43834450
// ---- differential coverage completeness ----
43844451
// Every shadowed class must be exercised by one of the tests/ scripts; the
43854452
// classes not covered above have their own dedicated script.

0 commit comments

Comments
 (0)