Repository navigation
Fix heap-buffer-overflow when serializing a shared quantileTDigest state - #110263
Conversation
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-0:20260713-110900 |
|
cc @rschu1ze @Algunenano — could you review this? |
|
Workflow [PR], commit [7567c40] Summary: ✅
AI ReviewSummaryThis PR makes Final VerdictStatus: ✅ Approve |
c0455c4 to
dd12979
Compare
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]>
dd12979 to
543820b
Compare
|
Pushed 543820b: the 30x loop over the 4M-row query tripped the flaky check's 180s per-run ceiling. Reduced rows 4M -> 200k (unchanged: 5000-centroid state, max_threads=16, 30 iterations) so the shared const aggregate-state is still serialized concurrently across threads. Re-verified both directions on ARM ASan via clickhouse-test:
Still reliably fails the broken code, now well under the flaky-check timeout. |
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]>
|
Fixed the flaky-check 180s timeout on the regression test (it was FAILing on the slower sanitizer builds: msan/debug/tsan). Two changes (test-only, source fix untouched):
Reliability retained: on a pristine (pre-fix, non-const |
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]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 13/36 (36.11%) · Uncovered code |
CI finish ledger — 7567c40CI complete (Finish Workflow: pass; Mergeable Check: pass; PR aggregator: pass). No test, fuzzer, sanitizer, or stress failures on this head. The Style-check
Session id: cron:our-pr-ci-monitor:20260714-113000 |
alexey-milovidov
left a comment
There was a problem hiding this comment.
This is a very good fix, bravo 👍
Even if there were some performance degradations (there are none) it would make sense.
… shared quantileTDigest state
Backport #110263 to 26.6: Fix heap-buffer-overflow when serializing a shared quantileTDigest state
|
@alexey-milovidov I was assigned because I was going to review it (and was doing so, because it's important to look for similar issues and that the solution is proper). If you are going to go around reviewers, please des-assign the issue and assign the issue to yourself, then deal with the review and backports |
Cherry pick #110263 to 25.8: Fix heap-buffer-overflow when serializing a shared quantileTDigest state
… shared quantileTDigest state
The backported fix from #110263 used the `::stableSort` helper from `base/sort.h`, which exists only on master. On the 26.4 branch this file uses `std::stable_sort` directly, so all builds failed with: `error: no member named 'stableSort' in the global namespace`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110474&sha=b837d44fa0cf617c665b5beec9a24898ea437816&name_0=BackportPR Co-Authored-By: Claude Fable 5 <[email protected]>
Backport #110263 to 25.8: Fix heap-buffer-overflow when serializing a shared quantileTDigest state
Backport #110263 to 26.5: Fix heap-buffer-overflow when serializing a shared quantileTDigest state
Backport #110263 to 26.4: Fix heap-buffer-overflow when serializing a shared quantileTDigest state
Backport #110263 to 26.3: Fix heap-buffer-overflow when serializing a shared quantileTDigest state
04516_quantile_tdigest_serialize_shared_state hit the runner's 600s hard --timeout on Stateless tests (amd_llvm_coverage_per_test, per_test_coverage, 7/8) on master: 3 FAIL in 16 runs over 30 days, with strictly bimodal durations (600120/600130/600660 ms against 5590-8790 ms on the other 13). The cause is the randomized optimize_group_by_constant_keys, drawn false with p = 0.05 (tests/clickhouse-test:1569) and present on 3 of the 3 failing runs. Aggregator::executeOnBlock gates const-key detection on it (Aggregator.cpp:1643-1648): with it on, executeImplBatch calls emplaceKey once per block (:1059-1062); with it off, convertToFullColumnIfConst materializes the const state column (:1660) and emplaceKey runs once per row (:1066-1081). The aggregation method is serialized, so the test's single broadcast quantileTDigestState is serialized 200000 times per iteration instead of about four. The cost is exactly linear in rows (0.99 / 1.89 / 3.68 / 7.24 CPU-sec at 50k / 100k / 200k / 400k) while the pinned arm stays flat at 0.12, and the coverage build's constant factor turns that into the hard-timeout kill. Pinning the setting in the test's own SETTINGS clause does not weaken the regression test. On an ASan build with QuantileTDigest::serialize and the AggregateFunctionQuantile.h const_cast reverted to their pre-ClickHouse#110263 form, the pinned test still detects the heap-buffer-overflow in 20 of 20 runs, while an unpinned control on the same binary detects it in 4 of 5: all_keys_are_const still has several pipeline threads serializing the same shared state once per block, which is the race being pinned, and only the per-row repetition goes away. The long tag is kept and its comment corrected, since long covers the 180s soft ceiling but does not raise the 600s hard --timeout (tests/clickhouse-test:6175). The underlying O(rows) serialization of a shared aggregate state is pre-existing and general, worst on quantileGKState (37.7 CPU-sec, already 25.7 on 26.2), whose serialize has had the same copy-and-compress shape since before 110263 (AggregateFunctionQuantileGK.cpp:455-466), so it belongs in the Aggregator and is tracked separately.
…ializing a shared quantileTDigest state
…ializing a shared quantileTDigest state
…ializing a shared quantileTDigest state
…ile-tdigest-serialize-shared-state Fix heap-buffer-overflow when serializing a shared quantileTDigest state
AGG: quantileTDigest state as a GROUP BY key, multi-threaded -> crash. Effect in the production build a394e92: its upstream test(s) 04516_quantile_tdigest_serialize_shared_state fail on it (the server process died in 04516_quantile_tdigest_serialize_shared_state) and pass on official 26.8.8.8, run on isolated servers (evidence/fix_ci_20260925/upstream_repro/server_triage_PROD263_vs_UP2688.tsv). Upstream patch unchanged, with its test(s): 04516_quantile_tdigest_serialize_shared_state. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a possible crash (heap-buffer-overflow) when a
quantileTDigest-family aggregate-function state column was used as aGROUP BYkey and serialized concurrently by several threads.Description
Found by the AST fuzzer (
AST fuzzer (arm_asan_ubsan)), STID1777-2d0a, report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104657&sha=5d77b4a2becb4effcae858e0ea70d6b684c723c6&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29QuantileTDigest::serialize()calledcompress(), which sorts the centroids buffer in place, soserialize()mutated the aggregate state. ButIAggregateFunction::serializetakes aConstAggregateDataPtrand the same state can be shared between rows:ColumnAggregateFunction::replicate/createViewcopies state pointers (not the states), so a replicated / const aggregate-state column used as aGROUP BYkey has all rows pointing at one state. When such a column is a parallelGROUP BYkey, several pipeline threads runserialize()on the same state concurrently, racing insidecompress()/RadixSortand writing one element past the centroids buffer (ASAN heap-buffer-overflow inRadixSort::radixSortLSDInternal,RadixSort.h:353).Reproducer (crashes reliably under ASan with
max_threads > 1, always safe withmax_threads = 1):Fix:
serialize()is nowconstand never mutates the shared buffer. If the digest is already compacted it writes as is; otherwise it compresses a local copy. The now-unnecessaryconst_castinAggregateFunctionQuantile::serializeis dropped.QuantileTDigestwas the only quantile variant whoseserializewas non-const.Version info
26.7.1.963(included in26.7and later)26.6.2.71,26.5.6.44,26.4.5.132,26.3.17.48,25.8.29.20