Repository navigation
Fix segfault when a lambda is passed in a non-lambda argument position - #109840
alexey-milovidov merged 2 commits into
Conversation
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-5:20260708-201000 |
|
cc @vdimir @novikd — could you review this? In the old analyzer, a lambda passed in a non-lambda argument position (e.g. the arrayFold accumulator) left the lambda placeholder's DataTypeFunction argument types null, and the null DataTypePtr was later dereferenced while building the ActionsDAG, segfaulting the server. This rejects it with ILLEGAL_TYPE_OF_ARGUMENT, mirroring the analyzer-path guard in resolveFunction. Found by the AST fuzzer (STID 1179-32dd). |
|
Workflow [PR], commit [39754b9] Summary: ✅
AI ReviewSummaryThis PR hardens higher-order-function lambda validation in both analyzers by rejecting lambdas in non-lambda argument positions before any lambda body is traversed. I re-checked the previously reported Final Verdict
|
A lambda passed where a higher-order function expects a concrete value (for example the accumulator of arrayFold: arrayFold(lambda, arr, another_lambda)) segfaulted the server. ActionsMatcher / QueryAnalyzer create a placeholder DataTypeFunction with null argument types for each lambda child, expecting the function's getLambdaArgumentTypes to fill them in. When the function does not expect a lambda at that position it leaves those types null. Validating one lambda at a time inside the recursion loop was too late: an unresolved trailing lambda placeholder can be copied into an earlier lambda's argument type, so we recursed into the earlier lambda's body before rejecting the trailing one. Inside that body the accumulator looked like an ordinary identifier of type Function((?, ?) -> ?), the inner higher-order call took the non-lambda path, and FunctionArrayMapped::getReturnTypeImpl dereferenced the still-null return type (FunctionArrayMapped.h:229). Validate all lambda arguments in a separate pass right after getLambdaArgumentTypes and before visiting/resolving any lambda body, rejecting the null placeholders with ILLEGAL_TYPE_OF_ARGUMENT. Applied to both the old analyzer (ActionsVisitor) and the new analyzer (resolveFunction). Found by the AST fuzzer: EXPLAIN PIPELINE SELECT arrayFold((acc, x) -> plus(toString(NULL, toLowCardinality(4), 4, 'aaaa', 4, 4, 4, 1), x), range(number), ((acc, x) -> if(x % 2, arrayPushFront(acc, x), arrayPushBack(acc, x)))) FROM system.numbers LIMIT 0 Co-Authored-By: Claude Opus 4.8 <[email protected]>
e5ae70f to
c7d6899
Compare
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-2:20260708-230700 |
The pre-validation pass added by this PR rejects a lambda in a non-lambda argument position with ILLEGAL_TYPE_OF_ARGUMENT (43) before the argument-count check that previously raised NUMBER_OF_ARGUMENTS_DOESNT_MATCH (42). The query's argument count is valid; the actual defect is a lambda passed as argument 3, so 43 is the more accurate code. Accept both so the regression test is robust to validation ordering. Co-Authored-By: Claude Opus 4.8 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 24/29 (82.76%) · Uncovered code |
CI finish ledger — 39754b9Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
All other checks green (Style, Fast test, Bugfix validation both arches, full functional/stress/fuzzer matrix). The prior PR-caused Fast-test reconciliation (03913 expected error code 42 → 42 OR 43) is confirmed resolved on this SHA. Ready for review. Session id: cron:our-pr-ci-monitor:20260709-073000 |
There was a problem hiding this comment.
Shouldn't we do argument type check in arrayFold function implementation (and other IFunctions?
There was a problem hiding this comment.
Good question. I looked at pushing it into arrayFold (and the other higher-order functions) and concluded the resolver is the right layer. Two reasons:
-
getReturnTypeImplis too late. The crash is not inarrayFold's own return-type computation; it is in the nested function during resolution of arrayFold's first lambda body. ForarrayFold((acc, x) -> arrayFilter(acc, [(x, x)]), [1], ((a, b) -> b)),getLambdaArgumentTypescopies the unresolved arg-3 placeholder (aDataTypeFunctionwith null inner types) intoacc. When we then resolve the first lambda body,arrayFilter(acc, ...)takes the non-lambda path andFunctionArrayMapped::getReturnTypeImpldereferences the nullgetReturnType().arrayFold::getReturnTypeImplis never reached, so a guard there cannot help. -
The only in-function hook early enough is
getLambdaArgumentTypes(it runs before any body is resolved). A guard there is feasible, but it would have to be duplicated in every higher-order function, and that layer only sees aDataTypesvector; the fact that an AST lambda was placed in a value position lives in the resolver (function_lambda_arguments_indexes). The null-innerDataTypeFunctionplaceholder is itself a resolver construct. So the resolver is the one place that both knows a lambda was misplaced and runs before any body is visited.
The current fix is one central pre-pass right after getLambdaArgumentTypes, symmetric across the old (ActionsVisitor) and new (resolveFunction) analyzers, covering all higher-order functions with a single guard rather than N per-function copies.
That said, if you'd prefer the check pushed into each function's getLambdaArgumentTypes, I'm happy to move it. Let me know which you want.
There was a problem hiding this comment.
Then check can be done in FunctionArrayMapped?
There was a problem hiding this comment.
arrayFold is not a FunctionArrayMapped. It is a separate FunctionArrayFold (src/Functions/array/arrayFold.cpp), so a guard in FunctionArrayMapped would not cover it, and arrayFold is the only function in this reproducer that accepts a lambda in a value position (its accumulator, the last argument).
FunctionArrayMapped also cannot itself be the source of this bug. Its getLambdaArgumentTypes already requires every data argument to be an Array ("Argument N of function ... must be Array"), so it can never carry a bare lambda in a non-lambda slot. It only shows up as the nested victim: arrayFilter(acc, ...) inside arrayFold's first lambda body, whose getReturnTypeImpl dereferences the null data_type_function->getReturnType() at FunctionArrayMapped.h:229.
So a check in FunctionArrayMapped would guard the crash site, not the root:
- it does not cover the actual source (arrayFold's accumulator);
- it does not cover other victims:
arrayFoldas the nested function also dereferences a null lambda return type (accumulator_type->equals(*lambda_type)in arrayFold.cpp), and any future higher-order function would need its own guard; - it reports the error from deep inside the nested call instead of at the misplaced lambda argument.
The resolver pre-pass is the single place that runs before any lambda body is resolved, knows an AST lambda was placed in a value position, and covers every higher-order function uniformly with the error reported at the correct argument.
I can additionally add a defensive null-check at FunctionArrayMapped.h:229 (and in arrayFold) as belt-and-suspenders so the HOF machinery is self-protecting, keeping the resolver pre-pass as the primary rejection. If you would rather drop the pre-pass and rely only on per-function guards, I can do that, but it duplicates the check across each HOF and loses the correct error location.
…da-accumulator-segfault Fix segfault when a lambda is passed in a non-lambda argument position
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a server crash when a lambda expression was passed where a higher-order function expects a concrete value (for example the accumulator of
arrayFold, as inarrayFold(lambda, arr, another_lambda)). Such queries are now rejected withILLEGAL_TYPE_OF_ARGUMENTinstead of crashing whenenable_analyzer = 0.Description
Found by the AST fuzzer:
Minimal reproducer (segfault before this change, with
enable_analyzer = 0):Root cause. In the old analyzer,
ActionsMatcher::visitcreates a placeholderDataTypeFunctionwith null argument types for eachlambdachild, expecting the function'sgetLambdaArgumentTypesto fill them in. When a lambda is placed where the function does not expect one (e.g. thearrayFoldaccumulator),getLambdaArgumentTypesleaves those argument types null. The code then dereferences the nullDataTypePtrwhile building theActionsDAG(the crash surfaced inisDynamic/WhichDataTypeduringIFunctionOverloadResolver::build, and also inFunctionCaptureOverloadResolver), crashing the server.Fix. After
getLambdaArgumentTypes, if the placeholder's argument types are still null the function did not expect a lambda at this position, so throwILLEGAL_TYPE_OF_ARGUMENT. This mirrors the analyzer path, which already rejects the same case inresolveFunction(Function '...' does not expect a lambda expression as argument N).CI provenance: STID 1179-32dd,
AST fuzzer (amd_debug), https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109589&sha=3a4cec287850f25c1b5081ad08b05638056279c1&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29Version info
26.7.1.947(included in26.7and later)