Skip to content

Cherry pick #110263 to 25.8: Fix heap-buffer-overflow when serializing a shared quantileTDigest state - #110472

Merged
robot-ch-test-poll2 merged 7 commits into
backport/25.8/110263from
cherrypick/25.8/110263
Jul 15, 2026
Merged

robot-ch-test-poll2 merged 7 commits into
backport/25.8/110263from
cherrypick/25.8/110263

Conversation

@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

Original pull-request #110263

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

groeneai and others added 6 commits July 13, 2026 17:25
QuantileTDigest::serialize() called compress(), which sorts the centroids
buffer in place, so serialize() mutated the aggregate state. But
IAggregateFunction::serialize takes a ConstAggregateDataPtr, and the same
state can be shared between rows (a replicated / const aggregate-state
column used as a GROUP BY key shares one state pointer across rows). When
such a column is a parallel GROUP BY key, several pipeline threads serialize
the same state concurrently, racing inside compress()/RadixSort and writing
one element past the centroids buffer (ASAN heap-buffer-overflow in
RadixSort::radixSortLSDInternal).

serialize() is now const and never touches the shared buffer: if the digest
is already compacted it writes as is, otherwise it compresses a local copy.
The const_cast in AggregateFunctionQuantile::serialize is dropped.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The flaky check reruns changed tests ~50x and FAILs any single run over
180s (TEST_MAX_RUN_TIME_IN_SECONDS) unless the test carries the long tag.
The 30-iteration x 200k-row re-aggregation loop blew past that on the
slower sanitizer builds (msan/debug/tsan): 'Test runs too long (> 180s)'
plus hard-timeout process-group kills.

Add the long tag (exempts the 180s flaky-check FAIL and cuts the rerun
count to ~10%) and shorten the loop to 10 iterations. The pre-fix binary
trips the heap-buffer-overflow within the first 1-2 iterations under
ASan, so 10 iterations keeps the failure reliable with 5x margin while
each run finishes in a few seconds.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
SequenceNextNodeImpl::serialize took a ConstAggregateDataPtr, const_cast-ed it
and sorted the value buffer in place. A shared/const sequenceNextNodeState
column (replicated or used as a parallel GROUP BY key) is then reordered
concurrently by several pipeline threads, which is a data race and can yield
wrong results, the same class of bug fixed for quantileTDigest in this PR. Sort
a local copy of the node pointers instead. Serialization stays byte-identical
(values are always written in sorted order).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Style check forbids raw std::vector in server code. Use the
memory-tracked drop-in for the local sorted node-pointer copy.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…t-serialize-shared-state

Fix heap-buffer-overflow when serializing a shared quantileTDigest state
@robot-ch-test-poll3 robot-ch-test-poll3 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-bugfix Pull request with bugfix, not backported by default labels Jul 15, 2026
@Algunenano Algunenano removed their assignment Jul 15, 2026
…ypick/25.8/110263

# Conflicts:
#	src/AggregateFunctions/AggregateFunctionSequenceNextNode.cpp
@alexey-milovidov

Copy link
Copy Markdown
Member

Resolved the conflict with backport/25.8/110263 and adapted the change to the 25.8 APIs: Common/VectorWithMemoryTracking.h and stableSort from base/sort.h do not exist on 25.8, so the local copy of the node pointers in SequenceNextNodeImpl::serialize uses PODArray<Node *> (memory-tracked via the default allocator) and std::stable_sort instead. The rest of the backport (QuantileTDigest.h, AggregateFunctionQuantile.h, and the test) applied cleanly; all other quantile data structures on 25.8 already have a const serialize, so dropping the const_cast compiles. The affected translation units were syntax-checked against the 25.8 tree.

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-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants