Skip to content

Cherry pick #117370 to 26.3: Check the overflow when allocating a block of aggregate function states - #117859

Merged
robot-ch-test-poll2 merged 7 commits into
backport/26.3/117370from
cherrypick/26.3/117370
Sep 3, 2026
Merged

robot-ch-test-poll2 merged 7 commits into
backport/26.3/117370from
cherrypick/26.3/117370

Conversation

@robot-clickhouse

Copy link
Copy Markdown
Member

Original pull-request #117370

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

Algunenano and others added 5 commits August 31, 2026 19:20
A block of `AggregateFunction` states is deserialized into a single allocation of the size of one
state times the number of rows, and both of them come from the data, so their product could wrap
around: the block was then allocated smaller than what was written into it, and the states were
created outside of it. A `Native` block of 110 bytes that declares 2^24 rows of a state of 2^40
bytes was enough to make the server write far outside the allocation.

Every column that sizes itself by an element count is protected by `PODArray`, which checks the
multiplication, so this was the only deserialization path computing a number of bytes as a product
of two values taken from the data and handing it straight to the arena.

The number of rows also comes from the data, so the row pointers are now reserved only after the
size has been validated.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The number of rows of a block comes from the data, and so does the size of a state, so the product
of the two must not be allocated upfront: it wraps around, and a block of a hundred bytes can ask
for an allocation of any size. The states are allocated in blocks bounded by the size of the array
of pointers that is reserved for them anyway, so a block of rows of an ordinary state still takes a
single allocation, and a state larger than that bound is allocated on its own. `NativeReader` avoids
preallocating from the row count for the same reason.

The arena adds the alignment, the padding and the rounding to the page size on top of the size it is
given, and these additions wrap around for a size close to the maximum, which makes it allocate a
chunk smaller than requested. Sizes that the allocator refuses outright are cut off there as well,
so that data-driven sizes are reported as a data error instead of the logical error the allocator
reports.

Arena allocation is unchanged for a block of states of an ordinary size: `ArenaAllocBytes` is the
same before and after for a block of 65409 states of 32 bytes.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`createAndDeserializeBatch` appends the pointer to a deserialized state outside of the block that
destroys the state when the deserialization throws. Appending allocates, so it can throw as well,
and the state would then be neither destroyed nor owned by the column. This used to be unreachable
only because the callers reserved the whole range beforehand.

The batch of a state of zero size is bounded as well, so that the number of rows, which comes from
the data, is never passed to the helper as is.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…lloc-overflow

Check the overflow when allocating a block of aggregate function states
@robot-clickhouse robot-clickhouse added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-critical-bugfix labels Sep 3, 2026
…rypick/26.3/117370

# Conflicts:
#	src/Common/Arena.cpp
The previous commit only staged the declaration; the header-only Arena
on this branch has no Arena.cpp to hold the definition.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants