Skip to content

Clamp extreme compress block sizes to prevent allocator crash - #101159

Merged
PedroTadim merged 8 commits into
ClickHouse:masterfrom
groeneai:fix/clamp-extreme-compress-block-sizes
May 13, 2026
Merged

PedroTadim merged 8 commits into
ClickHouse:masterfrom
groeneai:fix/clamp-extreme-compress-block-sizes

Conversation

@groeneai

@groeneai groeneai commented Mar 30, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the change):

Clamp MergeTree compress block size settings (max_compress_block_size, min_compress_block_size, marks_compress_block_size, primary_key_compress_block_size) to 256 MiB to prevent server crash when extreme values are set via CREATE TABLE SETTINGS.

What this PR does

MergeTree settings like max_compress_block_size flow directly to buffer allocation constructors (CompressedWriteBuffer, WriteBufferFromFile) without any upper-bound validation. When the AST fuzzer mutates these settings to values near INT64_MAX in CREATE TABLE SETTINGS, the buffer allocation adds ~63 bytes of SIMD padding, overflowing past the allocator's checkSize() limit of 2^63, causing a LOGICAL_ERROR abort (server crash).

Crash variants fixed: STID 0883-4864, 0883-4856, 0883-5a5b, 0883-2066, 0883-22d2, 0883-45dc — 24 hits across 3 master runs + 21 unrelated PRs in the last 30 days.

Note: The estimateCompressionRatio variant (STID 0883-248c) was separately fixed by #100298.

Changes

  • MergeTreeIOSettings.h: Add MAX_COMPRESS_BLOCK_SIZE = 256 MiB constant to MergeTreeWriterSettings
  • MergeTreeIOSettings.cpp: Clamp min_compress_block_size, max_compress_block_size, marks_compress_block_size, and primary_key_compress_block_size to the limit in the constructor
  • MergeTreeDataPartWriterWide.cpp: Clamp the column-level max_compress_block_size override
  • New test: 04070_no_crash_extreme_compress_block_size — regression test covering all five affected code paths (wide parts, compact parts, primary key index, marks, min compress)

Root cause

  1. CREATE TABLE t (x UInt64) ENGINE = MergeTree() ORDER BY x SETTINGS max_compress_block_size = 9223372036854775807 — accepted without validation
  2. INSERT INTO t SELECT ... triggers part writing
  3. MergeTreeWriterStream passes max_compress_block_size (= INT64_MAX) as buf_size to WriteBufferFromFile
  4. BufferWithOwnMemory adds 63 bytes of SIMD padding: 9223372036854775807 + 63 = 9223372036854775870
  5. Allocator::checkSize() rejects values ≥ 2^63 → LOGICAL_ERROR abort

Reproduction

Before fix:

CREATE TABLE t (x UInt64) ENGINE = MergeTree() ORDER BY x SETTINGS max_compress_block_size = 9223372036854775807;
INSERT INTO t SELECT number FROM numbers(100);
-- Crashes with: Logical error: 'Too large size (9223372036854775870) passed to allocator.'

After fix: INSERT completes successfully (block size silently clamped to 256 MiB).

Version info

  • Merged into: 26.5.1.595

MergeTree settings like max_compress_block_size, min_compress_block_size,
marks_compress_block_size, and primary_key_compress_block_size flow
directly to buffer allocation constructors (CompressedWriteBuffer,
WriteBufferFromFile) without size validation. When the AST fuzzer mutates
these to values near INT64_MAX via CREATE TABLE SETTINGS, adding SIMD
padding (~63 bytes) overflows past the allocator's checkSize() limit of
2^63, causing a LOGICAL_ERROR abort (server crash).

Fix: clamp all compress block size settings to 256 MiB (matching the
limit in PR ClickHouse#100298 for estimateCompressionRatio) in the
MergeTreeWriterSettings constructor and the column-level override in
MergeTreeDataPartWriterWide.

Affected crash variants: STID 0883-4864, 0883-4856, 0883-5a5b,
0883-2066, 0883-22d2, 0883-45dc (24 hits in 30 days, 3 on master).
The estimateCompressionRatio variant (0883-248c) was separately fixed
by PR ClickHouse#100298.

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

Copy link
Copy Markdown
Collaborator Author

Pre-PR Validation Gate (Session: cron:clickhouse-ci-task-worker:20260330-031500)

a) Deterministic repro? ✅

CREATE TABLE t (x UInt64) ENGINE = MergeTree() ORDER BY x SETTINGS max_compress_block_size = 9223372036854775807;
INSERT INTO t SELECT number FROM numbers(100);
-- Crashes every time with: Logical error: 'Too large size (9223372036854775870) passed to allocator.'

b) Root cause explained? ✅
max_compress_block_size (and min_compress_block_size, marks_compress_block_size, primary_key_compress_block_size) from MergeTree table SETTINGS flow directly to BufferWithOwnMemory constructors via MergeTreeWriterStream (wide parts), MergeTreeDataPartWriterCompact (compact parts), and MergeTreeDataPartWriterOnDisk::initPrimaryIndex() (primary key index). When set to INT64_MAX (9223372036854775807), adding ~63 bytes of SIMD padding produces 9223372036854775870, which exceeds the Allocator::checkSize() limit of 2^63, causing a LOGICAL_ERROR abort. The AST fuzzer triggers this by mutating constants in CREATE TABLE SETTINGS.

c) Fix matches root cause? ✅
Clamps all four compress block size settings to 256 MiB (consistent with PR #100298's limit for estimateCompressionRatio) at the MergeTreeWriterSettings constructor and at the column-level override in MergeTreeDataPartWriterWide::addStreams(). This prevents any extreme value from reaching the allocator.

d) Test intent preserved / New test added? ✅
New test 04070_no_crash_extreme_compress_block_size.sql covers all five affected code paths: wide parts, compact parts, primary key index, marks, and min compress block size.

e) Both directions demonstrated? ✅

  • Without fix: INSERT crashes with LOGICAL_ERROR on all five paths
  • With fix: INSERT completes successfully, returns correct row counts, data integrity verified

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @CurtizJ @alesapin — could you review this? It clamps MergeTree compress block size settings (max/min_compress_block_size, marks_compress_block_size, primary_key_compress_block_size) to 256 MiB in MergeTreeWriterSettings to prevent server crash when the AST fuzzer sets these to INT64_MAX via CREATE TABLE SETTINGS.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Mar 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b77e15d]

Summary: ⏳

job_name test_name status info comment
Config Workflow success IGNORED
Dockers Build (amd) skipped IGNORED
Dockers Build (arm) skipped IGNORED
Dockers Build (multiplatform manifest) skipped IGNORED
Style check success IGNORED
Code Review success IGNORED
Docs check skipped IGNORED
Fast test success IGNORED
Build (arm_tidy) success IGNORED
Build (amd_debug) success IGNORED

AI Review

Summary

This PR clamps extreme MergeTree compression block size settings (min/max_compress_block_size, marks_compress_block_size, primary_key_compress_block_size, plus column-level max_compress_block_size) to 256 MiB to prevent allocator-size overflow exceptions when fuzzed values near INT64_MAX are provided. The change is focused, covers the previously missed column-level path, and adds a regression test that exercises all affected write paths. High-confidence review result: no correctness, safety, or performance regressions found in the diff.

Missing context
  • ⚠️ Several CI checks are still pending, so this review is based on code + available PR context rather than final full CI completion.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 30, 2026
@alexey-milovidov alexey-milovidov self-assigned this Mar 30, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai it does not build:

Build (arm_fuzzers) failure
Build ClickHouse failure cidb
Stateless tests (amd_debug, parallel) failure
03668_shard_join_in_reverse_order FAIL cidb, #100870

@groeneai

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov thanks for reviewing — none of these failures are caused by this PR:

Build (arm_fuzzers): Linker crash (Bus error / OOM at ~30GB RSS) during final linking of clickhouse_fuzzer. All 15,470 compilation targets succeeded — only the link step crashed. This is a widespread infra issue: 8 identical failures across 6 different PRs + master in the last 48h (PRs #101104, #101128, #99581, #100724, #95181 + master on 2026-03-29 all show the same ~30GB linker OOM).

03668_shard_join_in_reverse_order: Known pre-existing flaky test, tracked in #100870.

test_overcommit_tracker/test_user_overcommit: Chronic flaky — 127 failures across 102 distinct PRs in the last 30 days.

Could this get a CI retry for the arm_fuzzers build, or would the existing results be sufficient for merge?

MergeTree settings like max_compress_block_size, min_compress_block_size,
marks_compress_block_size, and primary_key_compress_block_size flow
directly to buffer allocation constructors (CompressedWriteBuffer,
WriteBufferFromFile) without size validation. When the AST fuzzer mutates
these to values near INT64_MAX via CREATE TABLE SETTINGS, adding SIMD
padding (~63 bytes) overflows past the allocator's checkSize() limit of
2^63, causing a LOGICAL_ERROR abort (server crash).

Fix: clamp all compress block size settings to 256 MiB (matching the
limit in PR ClickHouse#100298 for estimateCompressionRatio) in the
MergeTreeWriterSettings constructor and the column-level override in
MergeTreeDataPartWriterWide.

Affected crash variants: STID 0883-4864, 0883-4856, 0883-5a5b,
0883-2066, 0883-22d2, 0883-45dc (24 hits in 30 days, 3 on master).
The estimateCompressionRatio variant (0883-248c) was separately fixed
by PR ClickHouse#100298.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@groeneai
groeneai force-pushed the fix/clamp-extreme-compress-block-sizes branch from 85b6510 to a937442 Compare March 30, 2026 17:22
@alexey-milovidov

Copy link
Copy Markdown
Member

The test 02859_replicated_db_name_zookeeper is fixed in #101952

@alexey-milovidov

Copy link
Copy Markdown
Member

The MSan stress test failure (MemorySanitizer: use-of-uninitialized-value, STID 4179-5154 or 4148-3044) is a known pre-existing issue unrelated to this PR. Fix: #102158

alexey-milovidov and others added 2 commits April 9, 2026 00:36
Cover the clamp in MergeTreeDataPartWriterWide::addStreams with a
regression test using column-level SETTINGS (max_compress_block_size =
INT64_MAX) on wide parts. Verified crash without fix (core dump on
unpatched binary) and pass with fix (CI).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.00% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 100.00% (20/20) · Uncovered code

Full report · Diff report

@groeneai

groeneai commented Apr 9, 2026

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov sorry for the delayed response on the 02859_replicated_db_name_zookeeper note — thanks for fixing that in #101952.

And thanks for flagging the MSan issue as pre-existing (#102158).

CI on the latest commit (b77e15d) is fully green except for 04039_merge_tree_snapshot_teardown_race on amd_msan — that's a known flaky (50 hits across 30 PRs in the last 7 days, 0 master failures). All builds, stress tests, fuzzers, and integration tests pass.

Should be ready to merge whenever convenient.

@groeneai

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov — friendly ping to expedite merge. This PR has been approved since 2026-03-30 (44 days). Meanwhile the bug continues hitting master:

Master hits in last 7 days (STID 0883-4856 — exactly what this PR fixes):

You also re-acknowledged the issue today on #100399 (Tracked by #104570 and seen on master itself (PR=0) on 2026-05-11).

PR state recap:

If you'd like a CI refresh first, I can push an empty commit to retrigger. Otherwise — could you merge it through? Happy to address anything I missed.

Session: cron:clickhouse-ci-task-worker:20260513-024500

@PedroTadim

Copy link
Copy Markdown
Member

@groeneai can you make a separate PR to fix 04039_merge_tree_snapshot_teardown_race test?

@PedroTadim
PedroTadim enabled auto-merge May 13, 2026 08:34
@PedroTadim
PedroTadim added this pull request to the merge queue May 13, 2026
Merged via the queue into ClickHouse:master with commit 2dd6d57 May 13, 2026
161 of 163 checks passed
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 pr-bugfix Pull request with bugfix, not backported by default 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