Skip to content

Commit ed774ca

Browse files
ondrejmirtesclaude
andcommitted
Check the argument classes of the native ExpressionResultStorage methods
The native ExpressionResultStorage and ExpressionResultStorageStack glue parsed its object arguments without a class check: mergeResults() with a foreign object read its property slots as the storage's tables and crashed, and storeExpressionResult() / findExpressionResult() / push() silently accepted what the twin's parameter types reject. mergeResults() now requires the native class (its slots are read directly); the Expr parameters are checked against the class map's Expr, and the ExpressionResult / ExpressionResultStorage parameters through the new pt_shadow_instanceof(), which also accepts the real-named PHP twin under the prefixed activation of the differential tests. The methods are registered by their generated signatures. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent e87cd63 commit ed774ca

5 files changed

Lines changed: 72 additions & 9 deletions

File tree

‎turbo-ext/src/ExpressionResultStorage.cpp‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,37 @@ void pt_register_expression_result_storage()
128128
result.intoReturnValue(return_value);
129129
});
130130

131-
cls.method("mergeResults", reg::Public, 1, { reg::any("other") }, [](INTERNAL_FUNCTION_PARAMETERS) {
131+
/* self $other: the native class exactly (its slots are read directly) */
132+
cls.method(sigs::mergeResults, [](INTERNAL_FUNCTION_PARAMETERS) {
132133
zval *other;
133-
if (!zp::parse<zp::Obj>(execute_data, other)) RETURN_THROWS();
134+
ZEND_PARSE_PARAMETERS_START(1, 1)
135+
Z_PARAM_OBJECT_OF_CLASS(other, pt_ce_expression_result_storage)
136+
ZEND_PARSE_PARAMETERS_END();
134137
ExpressionResultStorage(ZEND_THIS).mergeResults(other);
135138
});
136139

137-
cls.method("storeExpressionResult", reg::Public, 2, { reg::any("expr"), reg::any("expressionResult") }, [](INTERNAL_FUNCTION_PARAMETERS) {
140+
cls.method(sigs::storeExpressionResult, [](INTERNAL_FUNCTION_PARAMETERS) {
138141
zval *expr, *expressionResult;
139-
if (!zp::parse<zp::Obj, zp::Obj>(execute_data, expr, expressionResult)) RETURN_THROWS();
142+
zend_class_entry *exprCe = pt_class(PT_CLASS_EXPR);
143+
if (UNEXPECTED(exprCe == NULL)) RETURN_THROWS();
144+
ZEND_PARSE_PARAMETERS_START(2, 2)
145+
Z_PARAM_OBJECT_OF_CLASS(expr, exprCe)
146+
Z_PARAM_OBJECT(expressionResult)
147+
ZEND_PARSE_PARAMETERS_END();
148+
if (UNEXPECTED(!pt_shadow_instanceof(Z_OBJCE_P(expressionResult), pt_ce_expression_result, ZEND_STRL("PHPStan\\Analyser\\ExpressionResult")))) {
149+
zend_wrong_parameter_class_error(2, "PHPStan\\Analyser\\ExpressionResult", expressionResult);
150+
RETURN_THROWS();
151+
}
140152
ExpressionResultStorage(ZEND_THIS).storeExpressionResult(expr, expressionResult);
141153
});
142154

143-
cls.method("findExpressionResult", reg::Public, 1, { reg::any("expr") }, [](INTERNAL_FUNCTION_PARAMETERS) {
155+
cls.method(sigs::findExpressionResult, [](INTERNAL_FUNCTION_PARAMETERS) {
144156
zval *expr;
145-
if (!zp::parse<zp::Obj>(execute_data, expr)) RETURN_THROWS();
157+
zend_class_entry *exprCe = pt_class(PT_CLASS_EXPR);
158+
if (UNEXPECTED(exprCe == NULL)) RETURN_THROWS();
159+
ZEND_PARSE_PARAMETERS_START(1, 1)
160+
Z_PARAM_OBJECT_OF_CLASS(expr, exprCe)
161+
ZEND_PARSE_PARAMETERS_END();
146162
ExpressionResultStorage(ZEND_THIS).findExpressionResult(expr).intoReturnValue(return_value);
147163
});
148164

‎turbo-ext/src/ExpressionResultStorageStack.cpp‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "generated/ExpressionResultStorageStack.h"
1818

1919
namespace slots = ptdecl::ExpressionResultStorageStack::slot;
20+
namespace sigs = ptdecl::ExpressionResultStorageStack::sig;
2021
#include "zv.h"
2122

2223
zend_class_entry *pt_ce_expression_result_storage_stack = nullptr;
@@ -118,18 +119,22 @@ void pt_register_expression_result_storage_stack()
118119
ptdecl::ExpressionResultStorageStack::declareClass(cls);
119120
cls.privateTypedArrayPropertyDefaultEmpty("stack");
120121

121-
cls.method("push", reg::Public, 1, { reg::any("storage") }, [](INTERNAL_FUNCTION_PARAMETERS) {
122+
cls.method(sigs::push, [](INTERNAL_FUNCTION_PARAMETERS) {
122123
zval *storage;
123124
if (!zp::parse<zp::Obj>(execute_data, storage)) RETURN_THROWS();
125+
if (UNEXPECTED(!pt_shadow_instanceof(Z_OBJCE_P(storage), pt_ce_expression_result_storage, ZEND_STRL("PHPStan\\Analyser\\ExpressionResultStorage")))) {
126+
zend_wrong_parameter_class_error(1, "PHPStan\\Analyser\\ExpressionResultStorage", storage);
127+
RETURN_THROWS();
128+
}
124129
ExpressionResultStorageStack(ZEND_THIS).push(storage);
125130
});
126131

127-
cls.method("pop", reg::Public, 0, {}, [](INTERNAL_FUNCTION_PARAMETERS) {
132+
cls.method(sigs::pop, [](INTERNAL_FUNCTION_PARAMETERS) {
128133
ZEND_PARSE_PARAMETERS_NONE();
129134
if (UNEXPECTED(!ExpressionResultStorageStack(ZEND_THIS).pop())) RETURN_THROWS();
130135
});
131136

132-
cls.method("getCurrent", reg::Public, 0, {}, [](INTERNAL_FUNCTION_PARAMETERS) {
137+
cls.method(sigs::getCurrent, [](INTERNAL_FUNCTION_PARAMETERS) {
133138
ZEND_PARSE_PARAMETERS_NONE();
134139
ExpressionResultStorageStack(ZEND_THIS).getCurrent().intoReturnValue(return_value);
135140
});

‎turbo-ext/src/Shadow.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ bool pt_shadow_is_active()
5959
return pt_shadow_active;
6060
}
6161

62+
bool pt_shadow_instanceof(zend_class_entry *ce, zend_class_entry *nativeCe, const char *realName, size_t realNameLen)
63+
{
64+
if (EXPECTED(nativeCe != NULL && instanceof_function(ce, nativeCe))) return true;
65+
if (nativeCe == NULL || zend_string_equals_cstr(nativeCe->name, realName, realNameLen)) return false;
66+
zend_string *name = zend_string_init(realName, realNameLen, 0);
67+
zend_class_entry *twin = zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
68+
zend_string_release(name);
69+
return twin != NULL && instanceof_function(ce, twin);
70+
}
71+
6272
static const char *pt_short_name(const char *fqcn)
6373
{
6474
const char *slash = strrchr(fqcn, '\\');

‎turbo-ext/src/support.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,11 @@ extern zend_class_entry *pt_ce_nonexistent_parent_class_type;
563563
/* Shadow.cpp — Runtime::activateShadowing() */
564564
bool pt_shadow_activate(HashTable *twinFiles, zend_string *prefix);
565565
bool pt_shadow_is_active();
566+
/* ce is the shadowed class nativeCe (or a subclass) — under the prefixed
567+
* activation of the differential tests, where nativeCe is PHPStanTurbo\<Short>
568+
* and a PHP collaborator creates the real-named PHP twin, that twin counts
569+
* too. realName is the shadowed class's real name. */
570+
[[nodiscard]] bool pt_shadow_instanceof(zend_class_entry *ce, zend_class_entry *nativeCe, const char *realName, size_t realNameLen);
566571

567572
ZEND_COLD void pt_register_trinary_logic();
568573
ZEND_COLD void pt_register_expression_type_holder();

‎turbo-ext/tests/smoke.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4342,6 +4342,33 @@ public function getComments(): array
43424342
ini_set('pcre.backtrack_limit', $liBacktrackLimit);
43434343
check($liIdentifierResults['php'] === $liIdentifierResults['native'], 'ConstantArrayType::isValidIdentifier() on a PCRE failure: ' . json_encode($liIdentifierResults));
43444344

4345+
// ---- lane engine: misuse parity ----
4346+
// wrong arguments to the native glue: the twin's TypeError (or other
4347+
// Throwable) must come out, not a crash or a silently accepted value
4348+
$lemOutcome = static function (callable $callback) use ($turboNorm): array {
4349+
try {
4350+
$value = $callback();
4351+
return ['ok', is_object($value) ? $turboNorm(get_class($value)) : $value];
4352+
} catch (\Throwable $e) {
4353+
return [get_class($e), $turboNorm(preg_replace('~, called in .*$~', '', $e->getMessage()))];
4354+
}
4355+
};
4356+
$lemResults = [];
4357+
foreach (['php' => '\\PHPStan\\Analyser\\', 'native' => '\\PHPStanTurbo\\'] as $lemSide => $lemNs) {
4358+
$lemStorageClass = $lemNs . 'ExpressionResultStorage';
4359+
$lemStackClass = $lemNs . 'ExpressionResultStorageStack';
4360+
$lemResults[$lemSide] = [
4361+
'storage mergeResults(stdClass)' => $lemOutcome(static fn () => (new $lemStorageClass())->mergeResults(new \stdClass())),
4362+
'storage storeExpressionResult(expr, stdClass)' => $lemOutcome(static fn () => (new $lemStorageClass())->storeExpressionResult(new \PhpParser\Node\Scalar\Int_(1), new \stdClass())),
4363+
'storage storeExpressionResult(stmt, result)' => $lemOutcome(static fn () => (new $lemStorageClass())->storeExpressionResult(new \PhpParser\Node\Stmt\Nop(), $makeResult())),
4364+
'storage findExpressionResult(stdClass)' => $lemOutcome(static fn () => (new $lemStorageClass())->findExpressionResult(new \stdClass())),
4365+
'stack push(stdClass)' => $lemOutcome(static fn () => (new $lemStackClass())->push(new \stdClass())),
4366+
];
4367+
}
4368+
foreach ($lemResults['php'] as $lemLabel => $lemPhp) {
4369+
check($lemPhp === $lemResults['native'][$lemLabel], "misuse parity ($lemLabel): " . json_encode($lemPhp) . ' vs ' . json_encode($lemResults['native'][$lemLabel]));
4370+
}
4371+
43454372
// ---- differential coverage completeness ----
43464373
// Every shadowed class must be exercised by one of the tests/ scripts; the
43474374
// classes not covered above have their own dedicated script.

0 commit comments

Comments
 (0)