Skip to content

Reject serialized query plan steps without their inputs - #131736

Merged
alexey-milovidov merged 6 commits into
masterfrom
fix-query-plan-step-missing-inputs
Nov 17, 2026
Merged

alexey-milovidov merged 6 commits into
masterfrom
fix-query-plan-step-missing-inputs

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

The number of children of a step in a serialized query plan comes from the stream, but the step deserializers read their input headers (ctx.input_headers.front() and similar) without checking it, so a plan with a step lacking its children made the server read out of bounds. Make QueryPlanStepDeserialization::input_headers a checked view that throws INCORRECT_DATA for a missing input, which covers all steps at once, and reject a UnionStep without inputs. Also check that every deserialized step has exactly as many inputs as its node has children (a step with extra children made plan optimizations read out of bounds), and replace the chassert in BlocksMarshallingStep::deserialize with an exception.

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix out-of-bounds reads when deserializing a query plan with a step lacking some of its inputs or having extra children. Now such a plan is rejected with INCORRECT_DATA.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/131736]

Version info

  • Merged into: 26.11.1.2542-master (included in 26.11 and later)
  • Backported to: 26.10.5.57, 26.9.31.20, 26.8.39.17, 26.3.66.13

The number of children of a step in a serialized query plan comes from the stream, but the step deserializers read their input headers (`ctx.input_headers.front()` and similar) without checking it, so a plan with a step lacking its children made the server read out of bounds. Make `QueryPlanStepDeserialization::input_headers` a checked view that throws `INCORRECT_DATA` for a missing input, which covers all steps at once, and reject a `UnionStep` without inputs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov alexey-milovidov self-assigned this Nov 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a932375]

Summary: ✅


AI Review

Summary

The PR rejects serialized query plan steps with missing inputs, extra children, or children without output streams using INCORRECT_DATA. Reviewed head a9323753d6b4215d5599881ca93f2acd6bbeee33, including the current code, regression coverage, and all prior discussion. All four previous findings are addressed; no outstanding findings remain.

Missing context / blind spots
  • ⚠️ No runnable ClickHouse build is available locally, so runtime verification relies on the author's reported regression and unit test runs. Completed CI results for those tests would provide independent confirmation.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Nov 15, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Nov 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a9323753d with master e53b32344 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

57 translation units recompiled, 438 s compile time in total, 57 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.11 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +44.9 s (+11%) in total before that adjustment.

Job report

alexey-milovidov and others added 2 commits November 16, 2026 19:58
`DeserializedInputHeaders` stores a reference, so the temporary `SharedHeaders{header}`
used to initialize the deserialization context in the gtest was destroyed right after the
initialization. This made `LimitByStepSerialization` fail in unit tests, with
`stack-use-after-scope` under ASan. Use a named vector, and delete the rvalue constructor
of `DeserializedInputHeaders` so other callers cannot get the same dangling reference.

Address review feedback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A step may take fewer inputs than its node has children, e.g. `Extremes` with two
children stores one input header, while optimizations iterate over the children and
read the input header of each of them out of bounds. Check that the number of input
headers of each deserialized step equals the number of its children.

Also replace the `chassert` in `BlocksMarshallingStep::deserialize` with an
`INCORRECT_DATA` exception: a childless step aborted debug and sanitizer servers.
Extend the test with both cases.

Address review feedback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov and others added 3 commits November 16, 2026 21:50
A sink such as `GatherSend` has no output header, so calling `getOutputHeader`
on it while collecting the input headers of its parent raised a `LOGICAL_ERROR`
exception. Throw `INCORRECT_DATA` instead, and extend the test with a sink
beneath another step.

Also mark the accessors of `DeserializedInputHeaders` `[[nodiscard]]` as
required by `modernize-use-nodiscard`.

Address review: #131736 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolved the test conflict by keeping the remote's `buildNode` helper and the
new cases, and adding the `Extremes` over `GatherSend` case on top of it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@clickhouse-gh clickhouse-gh Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review approves this commit, see the AI Review comment.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 17, 2026
Merged via the queue into master with commit e03ba31 Nov 17, 2026
193 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-query-plan-step-missing-inputs branch November 17, 2026 06:20
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 17, 2026
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 17, 2026
alexey-milovidov added a commit that referenced this pull request Nov 17, 2026
Resolve conflicts of the cherry-pick of #131736 onto 26.3:
- `Serialization.cpp`: keep the input-count and sink-child checks; 26.3 has no
  `MAX_QUERY_PLAN_STEP_CHILDREN`, step versions, or the relaxed header check.
- `UnionStep.cpp`: keep the empty-input check; 26.3's `UnionStep::deserialize`
  reads no flags.
- `BlocksMarshallingStep.cpp`: keep the input-count check; 26.3 has no
  `contextAllowsBlocksMarshalling` here.
- Drop `gtest_limit_by_step_serialization.cpp`, which does not exist on 26.3.
- Adapt the test `05775_query_plan_step_missing_inputs`: 26.3 does not register
  `ReadNothing`, `GatherSend`, or `BlocksMarshalling` steps, so use
  `ReadFromTable` children and drop the cases with the missing steps.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-ch-test-poll2 added a commit that referenced this pull request Nov 17, 2026
Cherry pick #131736 to 26.3: Reject serialized query plan steps without their inputs
robot-clickhouse added a commit that referenced this pull request Nov 17, 2026
robot-ch-test-poll added a commit that referenced this pull request Nov 17, 2026
Cherry pick #131736 to 26.8: Reject serialized query plan steps without their inputs
robot-clickhouse added a commit that referenced this pull request Nov 17, 2026
robot-ch-test-poll3 added a commit that referenced this pull request Nov 17, 2026
Cherry pick #131736 to 26.9: Reject serialized query plan steps without their inputs
robot-clickhouse added a commit that referenced this pull request Nov 17, 2026
robot-ch-test-poll3 added a commit that referenced this pull request Nov 17, 2026
Cherry pick #131736 to 26.10: Reject serialized query plan steps without their inputs
robot-clickhouse added a commit that referenced this pull request Nov 17, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 17, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 17, 2026
Backport #131736 to 26.10: Reject serialized query plan steps without their inputs
clickhouse-gh Bot added a commit that referenced this pull request Nov 17, 2026
Backport #131736 to 26.9: Reject serialized query plan steps without their inputs
clickhouse-gh Bot added a commit that referenced this pull request Nov 17, 2026
Backport #131736 to 26.8: Reject serialized query plan steps without their inputs
PedroTadim added a commit that referenced this pull request Nov 17, 2026
Backport #131736 to 26.3: Reject serialized query plan steps without their inputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants