Skip to content

Fix allocator abort in validateColumnOfFixedSize for huge index_granularity - #108650

Merged
alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
groeneai:fix-validate-fixed-size-granularity-overflow
Aug 25, 2026
Merged

alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
groeneai:fix-validate-fixed-size-granularity-overflow

Conversation

@groeneai

@groeneai groeneai commented Jun 26, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Fix a server abort (Too large size passed to allocator) in debug builds when writing a non-adaptive MergeTree part (index_granularity_bytes = 0) created with an extremely large index_granularity.

Description

The debug-only writer validator MergeTreeDataPartWriterWide::validateColumnOfFixedSize re-reads each granule via deserializeBinaryBulk, passing the per-mark granularity as the row count to read. For a non-adaptive part (index_granularity_bytes = 0) the per-mark granularity is the index_granularity setting itself, which is only rejected when < 1, so a value near the unsigned 64-bit range is accepted. deserializeBinaryBulk eagerly resizes the column to that count before reading, which trips the allocator overflow guard (Too large size (...) passed to allocator) and aborts the server in debug builds, long before reaching EOF.

Found by the AST fuzzer (STID 1367-1ab3) on AST fuzzer (amd_debug, targeted, old_compatibility), reproduced across several unrelated PR runs, always with the same value 9223372036854775933 (2^63 + 125):

Stack (key frames):

checkSize(9223372036854775933) / Allocator::alloc / PODArray::resize
SerializationNumber<char8_t>::deserializeBinaryBulk (SerializationNumber.cpp:232)
MergeTreeDataPartWriterWide::validateColumnOfFixedSize (MergeTreeDataPartWriterWide.cpp:710)
MergeTreeDataPartWriterWide::finishDataSerialization / finish
MergedBlockOutputStream::Finalizer::Impl::finish

Minimal deterministic reproducer (debug build):

CREATE TABLE t (x UInt8) ENGINE = MergeTree ORDER BY tuple()
SETTINGS index_granularity_bytes = 0, index_granularity = 9223372036854775933, min_bytes_for_wide_part = 0;
INSERT INTO t SELECT number FROM numbers(100000);

Fix: read the column in bounded batches (grow on demand) instead of resizing to the untrusted count up front. The column ends up holding only the rows actually present, which the existing column->size() != index_granularity_rows check still uses to detect a real mark/data mismatch, so the validator's behavior is unchanged for valid parts. Regression test added in 04412_validate_fixed_size_column_huge_granularity.

No related open issue found (the previous validator fix for non-adaptive parts was #103538, issue #98585, a distinct mismatch).


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

…larity

The debug-only writer validator MergeTreeDataPartWriterWide::validateColumnOfFixedSize
re-reads each granule via deserializeBinaryBulk, passing the per-mark granularity as the
row count. For a non-adaptive part (index_granularity_bytes = 0) created with an absurd
index_granularity (the setting is only rejected when < 1, so a value near the unsigned
64-bit range is accepted), that count is the huge granularity. deserializeBinaryBulk
eagerly resizes the column to it, tripping the allocator overflow guard
("Too large size passed to allocator") and aborting the server in debug builds, long
before reaching EOF.

Found by the AST fuzzer (STID 1367-1ab3) across several unrelated PR runs, always with
the same value 9223372036854775933 (2^63 + 125), e.g.
  CREATE TABLE t (x UInt8) ENGINE = MergeTree ORDER BY tuple()
    SETTINGS index_granularity_bytes = 0, index_granularity = 9223372036854775933;
  INSERT INTO t SELECT number FROM numbers(100000);

Read the column in bounded batches (grow on demand) instead of resizing to the untrusted
count up front. The column ends up holding only the rows actually present, which the
existing column->size() != index_granularity_rows check still uses to detect a real
mark/data mismatch, so the validator's behavior is unchanged for valid parts.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. CREATE TABLE t (x UInt8) ENGINE=MergeTree ORDER BY tuple() SETTINGS index_granularity_bytes=0, index_granularity=9223372036854775933, min_bytes_for_wide_part=0; INSERT INTO t SELECT number FROM numbers(100000); aborts a debug server every run.
b Root cause explained? validateColumnOfFixedSize (debug-only) reads each granule via deserializeBinaryBulk using the per-mark granularity as the row count. For a non-adaptive part that count is the index_granularity setting (only rejected when < 1), so a value near 2^63 is accepted. deserializeBinaryBulk eagerly resizes the column to that count before reading, tripping the allocator overflow guard (size >= 2^63) and aborting.
c Fix matches root cause? Yes. The read is now done in bounded batches (grow on demand) so the eager allocation can never reach the absurd count; it stops at EOF with only the rows actually present.
d Test intent preserved / new tests added? Yes. The validator's post-read check column->size() != index_granularity_rows is unchanged and still detects a real mark/data mismatch. New regression test 04412_validate_fixed_size_column_huge_granularity.
e Both directions demonstrated? Yes. Pre-fix master binary aborts deterministically; post-fix binary runs the exact repro 3/3 with correct data, and the new test passes.
f Fix is general across code paths? Covers all three deserializeBinaryBulk read sites inside validateColumnOfFixedSize via one helper. The sibling serializations (Number/Decimal/UUID/IP) share the same eager-resize pattern but are only ever fed an untrusted count by this debug validator; the normal read path passes trusted mark-derived counts (verified: reads of such tables work).
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. The validator runs for all fixed-size numeric types; the helper is type-agnostic and the huge-granularity trigger is type-independent. Verified with UInt8 and String (String table writes fine; validator only re-reads fixed-size numeric columns).
h Backward compatible? Yes. No setting, on-disk, or wire format change; the patch only changes how a debug-only validator reads back data.
i Invariants and contracts preserved? Yes. deserializeBinaryBulk's contract (read up to limit, shrink to actual) is respected per batch; the column's final size equals what a single unbounded read would have produced for any valid part, so the granularity-equality invariant the validator checks is unchanged.

Session id: cron:clickhouse-worker-slot-3:20260626-191500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @CurtizJ @alesapin — could you review this? The debug-only validateColumnOfFixedSize writer validator aborts the server (Too large size passed to allocator) for a non-adaptive part with an absurd index_granularity, because it passes the huge per-mark granularity straight to deserializeBinaryBulk, which eagerly resizes the column to ~2^63 before reading. The fix reads in bounded batches; the post-read row-count check is unchanged. Found by the AST fuzzer (STID 1367-1ab3).

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ee26965]

Summary: ✅


AI Review

Summary

This PR changes the debug-only wide-part validator to read fixed-size columns in bounded batches instead of passing the raw mark granularity straight into deserializeBinaryBulk, which avoids the allocator overflow on absurd non-adaptive index_granularity values. I traced the changed validator path, checked the relevant fixed-size bulk-deserialization implementations, and the new stateless test covers both the huge-granularity repro and the multi-batch case. I did not find a remaining correctness issue in the current diff.

Final Verdict

✅ Approve

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.00% 87.00% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 1/22 (4.55%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 26, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 1ee4ef4

CI fully finished (Finish Workflow and Mergeable Check passed; no checks queued or in progress; >20 min since the last check completed). Every failure below has an owner.

Check / test Reason Owner / fixing PR
Stress test (arm_release) / Hung check failed, possible deadlock found deadlock (chronic server-shutdown hang family; arm_release is a release build so this PR's debug-only #ifndef NDEBUG validator change is not even compiled in - cannot be the cause) #101680 / #105905 (ours, open)
CH Inc sync private sync CH Inc sync (private, not actionable)

Session id: cron:our-pr-ci-monitor:20260627-003000

@clickhouse-gh

clickhouse-gh Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.40% +0.00%
Functions 92.60% 92.60% +0.00%
Branches 77.60% 77.60% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 1/22 (4.55%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@azat azat self-assigned this Jul 1, 2026
@groeneai

groeneai commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — a1568e1

CI fully finished (Finish Workflow pass). The reds are an infra env-setup error and the chronic hung-check family, none PR-caused.

Check / test Reason Owner / fixing PR
Bugfix validation (functional tests, amd64) / Environment setup (amd_msan) infra (msan environment setup error, not a test failure) a fix task is created (CI infra env-setup — fixing-PR link to follow here)
Stress test (arm_debug) / Hung check deadlock (chronic hung-check family) #108212 (ours, merged) / #105905 (ours, merged)

Session id: cron:our-pr-ci-monitor:20260703-053000

@groeneai

groeneai commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — a1568e1

CI fully finished on this head (Finish Workflow completed 2026-06-29T02:18:06Z, 0 checks queued/in_progress). Every failure has an owner.

Check / test Reason Owner / fixing PR
Stress test (arm_debug) :: Hung check failed, possible deadlock deadlock (chronic hung-check family, 77 hits / 68 PRs / 6 master in 30d; not PR-caused) #108212 (ours, merged) / #105905 (ours, open)
Bugfix validation (functional tests, amd64) :: Environment setup (amd_msan) infra (msan re-prepare env-setup flake, 5 hits / 4 PRs / 0 master in 30d; not PR-caused) a fix task is moved to pending (investigating — fixing PR link to follow here)

This PR (Fix allocator abort in validateColumnOfFixedSize for huge input) touches only the debug-only fixed-size-column validator; neither failure is in its code path.

Session id: cron:our-pr-ci-monitor:20260704-170000

@groeneai

groeneai commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator Author

Correction to the ledger above: the Environment setup (amd_msan) line has a merged fixing PR, so it links directly (the "fix task moved to pending" wording was imprecise):

Check / test Reason Owner / fixing PR
Bugfix validation (functional tests, amd64) :: Environment setup (amd_msan) infra (msan re-prepare env-setup flake; not PR-caused) #108593 (ours, merged 2026-07-01) — this build (2026-06-29) predates the fix

Session id: cron:our-pr-ci-monitor:20260704-170000

@groeneai

Copy link
Copy Markdown
Collaborator Author

Status update (drive-to-merge, per the request on #110144):

  • The AST fuzzer that catches this abort, AST fuzzer (amd_debug, targeted, old_compatibility) (STID 1367-1ab3), is green on the head, and all AST fuzzer variants pass. Bot AI review: Approve.
  • Changelog category switched to Not for changelog (the validator is #ifndef NDEBUG, so no stable-release behavior changes), which also drops the Bugfix-validation requirement.
  • The bug is still present on master (MergeTreeDataPartWriterWide.cpp:746 passes the untrusted per-mark count straight into deserializeBinaryBulk); the fix reads in bounded 1Mi-row batches.
  • Remaining reds are unrelated: chronic Stress (arm_debug) hung-check, and a Bugfix validation (amd64) msan env-setup infra ERROR where the regression test itself passed (Failed: 0, Passed: 1).

Mergeable and ready.

@clickhouse-gh

clickhouse-gh Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Dear @azat, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

The .sql regression test carried a 10-line preamble naming C++ internals
(MergeTreeDataPartWriterWide::validateColumnOfFixedSize, deserializeBinaryBulk)
and the fuzzer STID. A test comment should say what the statement asserts; the
class and function that were broken belong here in the commit message.

Context that used to sit in the test: a non-adaptive MergeTree
(index_granularity_bytes = 0) created with an index_granularity near the
unsigned 64-bit range (the setting is only rejected when < 1) aborted the
server in debug builds with "Too large size (...) passed to allocator". The
debug-only validator re-read each granule using the per-mark granularity as the
row count, which for such a table is that huge index_granularity, so the column
was resized to ~2^63 elements before reaching EOF.

Also dropped the helper's first comment line, which restated its signature.
The test could pass with the fix reverted. finishDataSerialization only
validates a column whose kind stack is exactly {DEFAULT}, and the test runner
randomizes ratio_of_defaults_for_sparse_serialization down to 0. The inserted
UInt8 data contains 391 default values, so the column could be written sparse,
the validator was skipped, and the check became vacuous. Measured on a pre-fix
binary: with the ratio forced to 0 the query returns normally, with the ratio
pinned to 1 it aborts as expected. Pin the setting.

Also add a granule larger than the read batch (2000000 rows against a
1048576-row batch). The first case only ever needs one bounded read, so an
implementation that stopped after a single batch would still pass it while
rejecting valid marks that span several batches.
@clickhouse-gh clickhouse-gh Bot removed the pr-bugfix Pull request with bugfix, not backported by default label Aug 19, 2026
@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Aug 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ee2696590 with master 2797514bd (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
Binary Master PR Δ
programs/clickhouse-stripped 709.23 MiB 706.19 MiB -3.04 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - db03a99

Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (amd_msan, flaky check) infra, not PR-caused and no test ran: the job died pulling its container. The image pull retry that #113611 added is present in this build and declined to retry, because the pull failed with server misbehaving (a DNS resolution failure against the registry proxy) and that phrase is not in its retry list, so 2 of the 3 attempts were forfeited. a fix task is moved to pending (investigating at full effort, fixing PR link to follow on this PR)

Details, since the shape of this one is easy to misread:

  • praktika reports the job as ERROR, not FAIL, with a 1175-byte report and zero test children.
    No test executed, so this is not a flakiness datum about any test in this PR.
  • The four sibling flaky checks (amd_asan_ubsan, amd_binary, amd_debug, amd_tsan) are all
    green on this same head.
  • Retry the job image pull so a registry reset cannot kill the whole job #113611 is merged and _IMAGE_PULL_RETRY_ERRORS is present in the tree that ran, so it is not
    linked as the owner here: it was in place and did not cover this error string. The follow-up is
    to complete that list, and its link will be posted here.

The rest of the matrix is green: 172 unique jobs, Config Workflow, Style check, Fast test
and Finish Workflow all successful.

Session id: cron:our-pr-ci-monitor:20260820-030000

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the ledger line above (Stateless tests (amd_msan, flaky check), the job that died pulling its container): #115555.

It adds server misbehaving to the job image pull's retry list, so that DNS failure now gets all 3 attempts instead of forfeiting 2. Measured 4 such jobs across 4 pull requests in 30 days, none on master.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A little bit excessive, but LGTM.

…size-granularity-overflow

Earlier criss-cross merges on the branch had mangled unrelated files, so all
conflicts outside the PR's own files were resolved by taking master's version;
the resulting tree differs from master by exactly the intended fix in
`MergeTreeDataPartWriterWide.cpp` plus the regression test 04412.
@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Aug 24, 2026
The merge-conflict resolution accidentally kept the branch's stale submodule
pointer; this PR does not intend to change any submodule.
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 25, 2026
Merged via the queue into ClickHouse:master with commit d285633 Aug 25, 2026
178 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 25, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@groeneai @alexey-milovidov — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors groeneai-origin-unknown PR origin: unclassified (legacy task) manual approve Manual approve required to run CI pr-not-for-changelog This PR should not be mentioned in the changelog 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.

5 participants