Repository navigation
Cherry pick #117370 to 26.6: Check the overflow when allocating a block of aggregate function states - #117860
Merged
Conversation
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]>
…e-bulk-alloc-overflow
…lloc-overflow Check the overflow when allocating a block of aggregate function states
…rypick/26.6/117370 # Conflicts: # src/Common/Arena.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-picklocally.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:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job