Skip to content

Commit 51177a6

Browse files
ondrejmirtesclaude
andcommitted
Check the parameter classes of the native TypeSpecifier and PropertyHooksProcessor entry points
The @api TypeSpecifier::specifyTypesInCondition(), create(), specifyDefaultTypes() and handleDefaultTruthyOrFalseyContext() and PropertyHooksProcessor::processPropertyHooks() parsed their object parameters without a class check (and the callable $nodeCallback as any value), so wrong arguments ran into the bodies — answering, or failing with an internal error — where the twin throws its TypeError. Check them against the class map's interfaces and the native class entries, the native type node against Identifier|Name|ComplexType|null and the node callback for callability, with the twin's messages. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 31aa1ce commit 51177a6

3 files changed

Lines changed: 126 additions & 10 deletions

File tree

‎turbo-ext/src/PropertyHooksProcessor.cpp‎

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,26 @@ bool pt_property_hooks_processor_process_property_hooks(zval *processor, zval *n
347347

348348
#include "reg.h"
349349

350+
namespace {
351+
352+
/* $nativeTypeNode is an Identifier, a Name or a ComplexType; false (with an
353+
* exception pending when a class-map lookup failed) otherwise */
354+
bool isNativeTypeNode(zval *node)
355+
{
356+
zend_class_entry *ce = Z_OBJCE_P(node);
357+
zend_class_entry *identifierCe = pt_class(PT_CLASS_IDENTIFIER);
358+
zend_class_entry *nameCe = pt_class(PT_CLASS_NAME);
359+
if (UNEXPECTED(identifierCe == NULL || nameCe == NULL)) return false;
360+
if (instanceof_function(ce, identifierCe) || instanceof_function(ce, nameCe)) return true;
361+
/* not loaded: nothing is one */
362+
zend_string *name = zend_string_init(ZEND_STRL("PhpParser\\Node\\ComplexType"), 0);
363+
zend_class_entry *complexTypeCe = zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
364+
zend_string_release(name);
365+
return complexTypeCe != NULL && instanceof_function(ce, complexTypeCe);
366+
}
367+
368+
} // namespace
369+
350370
void pt_register_property_hooks_processor()
351371
{
352372
pt_php_property_assign = zend_string_init_interned(PT_LC("propertyAssign"), 1);
@@ -367,17 +387,33 @@ void pt_register_property_hooks_processor()
367387
cls.method(sigs::processPropertyHooks, [](INTERNAL_FUNCTION_PARAMETERS) {
368388
zval *nodeScopeResolver, *stmt, *nativeTypeNode, *phpDocType, *hooks, *scope, *storage, *nodeCallback;
369389
zend_string *propertyName;
390+
/* the parameter classes of the twin's signature */
391+
zend_class_entry *stmtCe = pt_class(PT_CLASS_STMT);
392+
zend_class_entry *typeCe = pt_class(PT_CLASS_TYPE);
393+
if (UNEXPECTED(stmtCe == NULL || typeCe == NULL)) RETURN_THROWS();
370394
ZEND_PARSE_PARAMETERS_START(9, 9)
371-
Z_PARAM_OBJECT(nodeScopeResolver)
372-
Z_PARAM_OBJECT(stmt)
395+
Z_PARAM_OBJECT_OF_CLASS(nodeScopeResolver, pt_ce_node_scope_resolver)
396+
Z_PARAM_OBJECT_OF_CLASS(stmt, stmtCe)
373397
Z_PARAM_OBJECT_OR_NULL(nativeTypeNode)
374-
Z_PARAM_OBJECT_OR_NULL(phpDocType)
398+
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(phpDocType, typeCe)
375399
Z_PARAM_STR(propertyName)
376400
Z_PARAM_ARRAY(hooks)
377-
Z_PARAM_OBJECT(scope)
378-
Z_PARAM_OBJECT(storage)
401+
Z_PARAM_OBJECT_OF_CLASS(scope, pt_ce_mutating_scope)
402+
Z_PARAM_OBJECT_OF_CLASS(storage, pt_ce_expression_result_storage)
379403
Z_PARAM_ZVAL(nodeCallback)
380404
ZEND_PARSE_PARAMETERS_END();
405+
/* Identifier|Name|ComplexType|null — checked in the order the
406+
* engine checks the twin's parameters */
407+
if (nativeTypeNode != NULL && UNEXPECTED(!isNativeTypeNode(nativeTypeNode))) {
408+
if (!EG(exception)) {
409+
zend_argument_type_error(3, "must be of type PhpParser\\Node\\Identifier|PhpParser\\Node\\Name|PhpParser\\Node\\ComplexType|null, %s given", zend_zval_value_name(nativeTypeNode));
410+
}
411+
RETURN_THROWS();
412+
}
413+
if (UNEXPECTED(!zend_is_callable(nodeCallback, 0, NULL))) {
414+
zend_argument_type_error(9, "must be of type callable, %s given", zend_zval_value_name(nodeCallback));
415+
RETURN_THROWS();
416+
}
381417
zval null;
382418
ZVAL_NULL(&null);
383419
zval propertyNameZv;

‎turbo-ext/src/TypeSpecifier.cpp‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,10 +1044,58 @@ void pt_register_type_specifier()
10441044
TypeSpecifier::construct(Z_OBJ_P(ZEND_THIS), exprPrinter, reflectionProvider, functionTypeSpecifyingExtensions, methodTypeSpecifyingExtensions, staticMethodTypeSpecifyingExtensions, rememberPossiblyImpureFunctionValues, container);
10451045
});
10461046

1047-
cls.method<&TypeSpecifier::specifyTypesInCondition, zp::Obj, zp::Obj, zp::Obj>(sigs::specifyTypesInCondition);
1048-
cls.method<&TypeSpecifier::specifyDefaultTypes, zp::Obj, zp::Obj, zp::Obj>(sigs::specifyDefaultTypes);
1049-
cls.method<&TypeSpecifier::handleDefaultTruthyOrFalseyContext, zp::Obj, zp::Obj, zp::Obj>(sigs::handleDefaultTruthyOrFalseyContext);
1050-
cls.method<&TypeSpecifier::create, zp::Obj, zp::Obj, zp::Obj, zp::Obj>(sigs::create);
1047+
/* the @api entry points check their parameter classes like the twin's
1048+
* signatures (the Scope, Expr and Type interfaces through the class map) */
1049+
cls.method(sigs::specifyTypesInCondition, [](INTERNAL_FUNCTION_PARAMETERS) {
1050+
zval *scope, *expr, *context;
1051+
zend_class_entry *scopeCe = pt_class(PT_CLASS_SCOPE);
1052+
zend_class_entry *exprCe = pt_class(PT_CLASS_EXPR);
1053+
if (UNEXPECTED(scopeCe == NULL || exprCe == NULL)) RETURN_THROWS();
1054+
ZEND_PARSE_PARAMETERS_START(3, 3)
1055+
Z_PARAM_OBJECT_OF_CLASS(scope, scopeCe)
1056+
Z_PARAM_OBJECT_OF_CLASS(expr, exprCe)
1057+
Z_PARAM_OBJECT_OF_CLASS(context, pt_ce_type_specifier_context)
1058+
ZEND_PARSE_PARAMETERS_END();
1059+
PT_RETURN_VAL(TypeSpecifier(Z_OBJ_P(ZEND_THIS)).specifyTypesInCondition(scope, expr, context));
1060+
});
1061+
cls.method(sigs::specifyDefaultTypes, [](INTERNAL_FUNCTION_PARAMETERS) {
1062+
zval *scope, *expr, *context;
1063+
zend_class_entry *scopeCe = pt_class(PT_CLASS_SCOPE);
1064+
zend_class_entry *exprCe = pt_class(PT_CLASS_EXPR);
1065+
if (UNEXPECTED(scopeCe == NULL || exprCe == NULL)) RETURN_THROWS();
1066+
ZEND_PARSE_PARAMETERS_START(3, 3)
1067+
Z_PARAM_OBJECT_OF_CLASS(scope, scopeCe)
1068+
Z_PARAM_OBJECT_OF_CLASS(expr, exprCe)
1069+
Z_PARAM_OBJECT_OF_CLASS(context, pt_ce_type_specifier_context)
1070+
ZEND_PARSE_PARAMETERS_END();
1071+
PT_RETURN_VAL(TypeSpecifier(Z_OBJ_P(ZEND_THIS)).specifyDefaultTypes(scope, expr, context));
1072+
});
1073+
cls.method(sigs::handleDefaultTruthyOrFalseyContext, [](INTERNAL_FUNCTION_PARAMETERS) {
1074+
zval *context, *expr, *scope;
1075+
zend_class_entry *scopeCe = pt_class(PT_CLASS_SCOPE);
1076+
zend_class_entry *exprCe = pt_class(PT_CLASS_EXPR);
1077+
if (UNEXPECTED(scopeCe == NULL || exprCe == NULL)) RETURN_THROWS();
1078+
ZEND_PARSE_PARAMETERS_START(3, 3)
1079+
Z_PARAM_OBJECT_OF_CLASS(context, pt_ce_type_specifier_context)
1080+
Z_PARAM_OBJECT_OF_CLASS(expr, exprCe)
1081+
Z_PARAM_OBJECT_OF_CLASS(scope, scopeCe)
1082+
ZEND_PARSE_PARAMETERS_END();
1083+
PT_RETURN_VAL(TypeSpecifier(Z_OBJ_P(ZEND_THIS)).handleDefaultTruthyOrFalseyContext(context, expr, scope));
1084+
});
1085+
cls.method(sigs::create, [](INTERNAL_FUNCTION_PARAMETERS) {
1086+
zval *expr, *type, *context, *scope;
1087+
zend_class_entry *exprCe = pt_class(PT_CLASS_EXPR);
1088+
zend_class_entry *typeCe = pt_class(PT_CLASS_TYPE);
1089+
zend_class_entry *scopeCe = pt_class(PT_CLASS_SCOPE);
1090+
if (UNEXPECTED(exprCe == NULL || typeCe == NULL || scopeCe == NULL)) RETURN_THROWS();
1091+
ZEND_PARSE_PARAMETERS_START(4, 4)
1092+
Z_PARAM_OBJECT_OF_CLASS(expr, exprCe)
1093+
Z_PARAM_OBJECT_OF_CLASS(type, typeCe)
1094+
Z_PARAM_OBJECT_OF_CLASS(context, pt_ce_type_specifier_context)
1095+
Z_PARAM_OBJECT_OF_CLASS(scope, scopeCe)
1096+
ZEND_PARSE_PARAMETERS_END();
1097+
PT_RETURN_VAL(TypeSpecifier(Z_OBJ_P(ZEND_THIS)).create(expr, type, context, scope));
1098+
});
10511099
cls.method<&TypeSpecifier::getFunctionTypeSpecifyingExtensions>(sigs::getFunctionTypeSpecifyingExtensions);
10521100
cls.method<&TypeSpecifier::getMethodTypeSpecifyingExtensionsForClass, zp::Str>(sigs::getMethodTypeSpecifyingExtensionsForClass);
10531101
cls.method<&TypeSpecifier::getStaticMethodTypeSpecifyingExtensionsForClass, zp::Str>(sigs::getStaticMethodTypeSpecifyingExtensionsForClass);

‎turbo-ext/tests/smoke.php‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4438,7 +4438,39 @@ public function getComments(): array
44384438
'doProcessStmtNodes(stdClass)' => $outcome(static fn () => $doProcess([new \stdClass()])),
44394439
'doProcessStmtNodes(string)' => $outcome(static fn () => $doProcess(['echo'])),
44404440
'doProcessStmtNodes(expr)' => $outcome(static fn () => $doProcess([new \PhpParser\Node\Expr\Variable('x')])),
4441-
];
4441+
] + (static function () use ($container, $outcome, $scope, $nodeScopeResolver): array {
4442+
// the @api entry points of TypeSpecifier and PropertyHooksProcessor
4443+
$typeSpecifier = $container->getByType(\PHPStan\Analyser\TypeSpecifier::class);
4444+
$expr = new \PhpParser\Node\Expr\Variable('x');
4445+
$truthy = \PHPStan\Analyser\TypeSpecifierContext::createTruthy();
4446+
$wrong = new \stdClass();
4447+
$hooks = $container->getByType(\PHPStan\Analyser\PropertyHooksProcessor::class);
4448+
$property = new \PhpParser\Node\Stmt\Property(0, [new \PhpParser\Node\PropertyItem('p')]);
4449+
$storage = new \PHPStan\Analyser\ExpressionResultStorage();
4450+
$callback = static function (): void {
4451+
};
4452+
return [
4453+
'specifyTypesInCondition(wrong scope)' => $outcome(static fn () => $typeSpecifier->specifyTypesInCondition($wrong, $expr, $truthy)),
4454+
'specifyTypesInCondition(wrong expr)' => $outcome(static fn () => $typeSpecifier->specifyTypesInCondition($scope, $wrong, $truthy)),
4455+
'specifyTypesInCondition(wrong context)' => $outcome(static fn () => $typeSpecifier->specifyTypesInCondition($scope, $expr, $wrong)),
4456+
'specifyDefaultTypes(wrong context)' => $outcome(static fn () => $typeSpecifier->specifyDefaultTypes($scope, $expr, $wrong)),
4457+
'handleDefaultTruthyOrFalseyContext(wrong context)' => $outcome(static fn () => $typeSpecifier->handleDefaultTruthyOrFalseyContext($wrong, $expr, $scope)),
4458+
'handleDefaultTruthyOrFalseyContext(wrong scope)' => $outcome(static fn () => $typeSpecifier->handleDefaultTruthyOrFalseyContext($truthy, $expr, $wrong)),
4459+
'create(wrong expr)' => $outcome(static fn () => $typeSpecifier->create($wrong, new \PHPStan\Type\IntegerType(), $truthy, $scope)),
4460+
'create(wrong type)' => $outcome(static fn () => $typeSpecifier->create($expr, $wrong, $truthy, $scope)),
4461+
'create(wrong context)' => $outcome(static fn () => $typeSpecifier->create($expr, new \PHPStan\Type\IntegerType(), $wrong, $scope)),
4462+
'create(wrong scope)' => $outcome(static fn () => $typeSpecifier->create($expr, new \PHPStan\Type\IntegerType(), $truthy, $wrong)),
4463+
'processPropertyHooks(wrong resolver)' => $outcome(static fn () => $hooks->processPropertyHooks($wrong, $property, null, null, 'p', [], $scope, $storage, $callback)),
4464+
'processPropertyHooks(wrong stmt)' => $outcome(static fn () => $hooks->processPropertyHooks($nodeScopeResolver, $expr, null, null, 'p', [], $scope, $storage, $callback)),
4465+
'processPropertyHooks(wrong type node)' => $outcome(static fn () => $hooks->processPropertyHooks($nodeScopeResolver, $property, $wrong, null, 'p', [], $scope, $storage, $callback)),
4466+
'processPropertyHooks(type node expr)' => $outcome(static fn () => $hooks->processPropertyHooks($nodeScopeResolver, $property, $expr, null, 'p', [], $scope, $storage, $callback)),
4467+
'processPropertyHooks(wrong phpdoc type)' => $outcome(static fn () => $hooks->processPropertyHooks($nodeScopeResolver, $property, null, $wrong, 'p', [], $scope, $storage, $callback)),
4468+
'processPropertyHooks(wrong scope)' => $outcome(static fn () => $hooks->processPropertyHooks($nodeScopeResolver, $property, null, null, 'p', [], $wrong, $storage, $callback)),
4469+
'processPropertyHooks(wrong storage)' => $outcome(static fn () => $hooks->processPropertyHooks($nodeScopeResolver, $property, null, null, 'p', [], $scope, $wrong, $callback)),
4470+
'processPropertyHooks(not callable)' => $outcome(static fn () => $hooks->processPropertyHooks($nodeScopeResolver, $property, null, null, 'p', [], $scope, $storage, 'no_such_function')),
4471+
'processPropertyHooks(fine)' => $outcome(static fn () => $hooks->processPropertyHooks($nodeScopeResolver, $property, new \PhpParser\Node\Identifier('int'), null, 'p', [], $scope, $storage, $callback)),
4472+
];
4473+
})();
44424474
BODY);
44434475
$lemEngineNative = $lemEngineProbes['native']['native'] ?? null;
44444476
unset($lemEngineProbes['native']['native'], $lemEngineProbes['php']['native']);

0 commit comments

Comments
 (0)