Skip to content

Add more checks for mark access for better diagnostics - #100115

Merged
alexey-milovidov merged 5 commits into
masterfrom
fix-segfault-readbits-oob-98579
Mar 23, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
fix-segfault-readbits-oob-98579

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 19, 2026 •

Copy link
Copy Markdown
Member

A segfault occurs in readBits when reading marks from a compact part during mutation execution. The crash happens because MarksInCompressedFile::get is called with an out-of-bounds index, causing lookUpMark to read garbage from the blocks array, which produces an invalid bit offset that accesses unmapped memory.

This is likely caused by a race condition involving the mark cache where cached marks become inconsistent with the reader's expectations.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=f79634708dca0c8dc4bcaaa7f9bbd30ac4c23c6e&name_0=MasterCI&name_1=BuzzHouse%20%28amd_ubsan%29

The fix adds bounds checking at multiple levels:

  • MarksInCompressedFile::get: throws exception instead of segfaulting when idx >= num_marks
  • MergeTreeMarksGetter constructor: validates that marks count is consistent with num_columns_in_mark
  • MergeTreeMarksGetter::getMark: upgrades column_index bounds check from debug-only to always-on

Closes #98579

Changelog category (leave one):

  • Not For Changelog

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

Add bounds checking for mark access in MarksInCompressedFile and MergeTreeMarksGetter to prevent segfault when reading compact parts.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.160

…ergeTreeMarksGetter`

A segfault occurs in `readBits` when reading marks from a compact part
during mutation execution. The crash happens because
`MarksInCompressedFile::get` is called with an out-of-bounds index,
causing `lookUpMark` to read garbage from the `blocks` array, which
produces an invalid bit offset that accesses unmapped memory.

This is likely caused by a race condition involving the mark cache where
cached marks become inconsistent with the reader's expectations.

The fix adds bounds checking at multiple levels:
- `MarksInCompressedFile::get`: throws exception instead of segfaulting
  when `idx >= num_marks`
- `MergeTreeMarksGetter` constructor: validates that marks count is
  consistent with `num_columns_in_mark`
- `MergeTreeMarksGetter::getMark`: upgrades `column_index` bounds check
  from debug-only to always-on

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=f79634708dca0c8dc4bcaaa7f9bbd30ac4c23c6e&name_0=MasterCI&name_1=BuzzHouse%20%28amd_ubsan%29
Closes: #98579

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [103a37f]

Summary: ✅


AI Review

Summary

This PR hardens mark access by adding runtime bounds checks in MarksInCompressedFile::get and MergeTreeMarksGetter, and adds a stateless regression test for issue #98579. I did not find correctness, safety, or compatibility problems in the code changes themselves.

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 ✅

Final Verdict

  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Mar 19, 2026
@alexey-milovidov alexey-milovidov removed pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-critical-bugfix labels Mar 19, 2026
@alexey-milovidov alexey-milovidov changed the title Add bounds checking for mark access to prevent segfault in compact part reads Add more checks for mark access for better diagnostics Mar 19, 2026
…c = 2` setting

The `SYSTEM SYNC MUTATIONS` command does not exist in ClickHouse.
Use `mutations_sync = 2` setting on the ALTER statement instead.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Mar 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.90% +0.10%
Functions 24.70% 24.70% +0.00%
Branches 76.40% 76.50% +0.10%

PR changed lines: PR changed-lines coverage: 65.22% (15/23, 5 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good, no changes in behavior, test-only change.

@alexey-milovidov alexey-milovidov self-assigned this Mar 23, 2026
@alexey-milovidov
alexey-milovidov merged commit 0b2787d into master Mar 23, 2026
148 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-segfault-readbits-oob-98579 branch March 23, 2026 20:20
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Segmentation fault (STID: 1064-5149)

2 participants