Skip to content

Fix UBSan out-of-bounds pointer arithmetic in compareSubstrings - #106733

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-compare-substrings-offset-ub
Jun 12, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-compare-substrings-offset-ub

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 8, 2026 •

Copy link
Copy Markdown
Member

compareSubstrings formed the per-row str1 / str2 pointers (adding str1_offset / str2_offset) unconditionally, before the isOverflowComparison bounds check, in executeStringString and executeFixedStringString. When an offset argument is out of range, the pointer arithmetic itself goes out of bounds, which UndefinedBehaviorSanitizer flags as addition of unsigned offset ... overflowed, even though the pointer is only dereferenced inside the (in-bounds) overflow-check branch.

The pointers are now formed only after the overflow check passes, matching the already-safe pattern in the *Const variants. The computation of the pointers and adjusted lengths is unchanged, so results are identical.

Found by the AST fuzzer on PR #105129, e.g.:

SELECT compareSubstrings(s1, s2, 4, 4, 1048577)
FROM (SELECT CAST(s1, 'FixedString(3)') AS s1, ... ) ...

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105129&sha=163f7a6cb74c136b71b84adf20bce9a29e9e5dc3&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

🤖 Generated with Claude Code

Version info

  • Merged into: 26.6.1.724

`compareSubstrings` formed the per-row `str1` / `str2` pointers (adding
`str1_offset` / `str2_offset`) unconditionally, before the
`isOverflowComparison` bounds check, in `executeStringString` and
`executeFixedStringString`. When an offset argument was out of range,
the pointer arithmetic itself went out of bounds, which `UndefinedBehaviorSanitizer`
flags as `addition of unsigned offset ... overflowed`, even though the
pointer was only dereferenced inside the (in-bounds) overflow-check branch.

Form the pointers only after the overflow check passes, matching the
already-safe pattern in the `*Const` variants.

Found by the AST fuzzer, e.g.
`SELECT compareSubstrings(s1, s2, 4, 4, 1048577)` over `FixedString(3)`
columns.

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

clickhouse-gh Bot commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b2b8c8c]

Summary: ✅


AI Review

Summary

This PR fixes compareSubstrings by delaying String / FixedString data pointer formation until after the existing offset-overflow comparison proves both offsets are in range. The changed paths match the already-safe const and fixed/fixed variants, and the added stateless test covers the fuzzer-style out-of-range offsets for String / String, FixedString / String, and reversed String / FixedString inputs. I did not find any current review findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 8, 2026
@george-larionov george-larionov self-assigned this Jun 8, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Merged master into the branch (clean, no conflicts; master did not touch compareSubstrings.cpp) and renamed the test 04324_compare_substrings_out_of_range_offset to 04327_... to avoid a number collision with tests added on master in the meantime.

The sole CI failure, 00377_shard_group_uniq_array_of_string_array in Stateless tests (arm_asan_ubsan, azure, parallel), is unrelated to this change: the server hit the total memory limit (RSS 53.68 GiB of 53.69 GiB, the query was selected to stop by OvercommitTracker) under parallel load. The same test failed today on the unrelated PR #98472 as well.

@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106733&sha=2d7b1a2c5c6096b6036e2071c9fca166a05cfd86&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20azure%2C%20parallel%29 and provide a fix in a separate PR. If the fix is already in progress, link it here.

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov fix is up in a separate PR: #107054.

Confirmed your diagnosis. 00377_shard_group_uniq_array_of_string_array is the OvercommitTracker victim, not over its own limit: 30-day CIDB shows 3 hits across 3 unrelated PRs (this one, #98472, #106808), 0 on master, all on Stateless tests (arm_asan_ubsan, azure, parallel). The server total RSS is at the host cap (53.68 / 53.69 GiB) from concurrent queries and the tracker stops this query's AggregatingTransform.

The PR pins max_threads = 4 on the test. Measured locally (debug build, 10M-row table, 4-way remote() fan-out): peak memory of the heavy fan-out query drops from ~1.1-1.3 GiB to ~0.57-0.67 GiB (roughly halved), lowering its overcommit ratio so it is far less likely to be selected as the victim. Output is unchanged and matches the reference exactly. Same approach as #106667.

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov investigated. Your diagnosis was correct, and the root-cause global fix is already merged.

00377_shard_group_uniq_array_of_string_array is a victim of the runner-wide OOM you described, not a per-test bug: 5 failures across 5 unrelated PRs (#106733, #98472, #106808, #106470, #106985), 0 on master, each wanting only 1.2-6 GiB while the server total was pinned at the 53.69 GiB cgroup cap.

This is a single-lane problem. Code 241 (total) memory limit exceeded on parallel Stateless lanes, 2026-06-09..06-11: 997 rows / 110 PRs on arm_asan_ubsan, azure, parallel alone, vs 2 rows / 1 PR on all other parallel lanes combined.

Root cause and fix: #106962 by Algunenano (merged 2026-06-11), tagging 01603_read_with_backoff_bug no-asan. That test inserts ~25M long-string rows; under ASan the allocator keeps freed pages mapped, leaving the server at ~50 GiB RSS after it finishes, so subsequent parallel tests hit the total-RSS cap. Code-241 volume on the lane is already dropping post-merge (545 rows on 06-10 vs 79 on 06-11).

My per-test PR #107054 (max_threads=4 on 00377) is superseded by the global fix and I have closed it, per Algunenano's review that per-test pins are the wrong approach here.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

CI triage for the previous head (7c765c1d112) — both red checks were unrelated to this PR (which only changes pointer formation order in compareSubstrings.cpp):

  • Stateless tests (arm_asan_ubsan, azure, parallel): 03216_arrayWithConstant_limits failed with (total) memory limit exceeded while the runner-wide RSS was at 52.20 GiB of the 53.69 GiB ceiling — the same runner-wide memory-pressure class as the 00377 failure triaged above, which @groeneai confirmed is a victim of total-server OOM with the root-cause fix already merged on master. The CI database shows this test failing on the same day across master (pull_request_number = 0) and many unrelated PRs, including documentation-only ones.
  • Stress test (arm_asan_ubsan): Logical error: Table A.B not found in catalog raised from ReadFromMergeTree::deserialize via DistributedPlanExecutor. @groeneai root-caused it on ThreadPool: wake most recently idle worker thread (LIFO) when scheduling a job to reduce memory fragmentation #100177 as a master-wide regression from Multi-stage distributed queries #106020 (29 failures across master and ~24 PRs on 2026-06-10), with the fix already merged on master.

Since both fixes are on master, I merged master into the branch (clean, no conflicts; master still does not touch compareSubstrings.cpp, and the test name 04327_compare_substrings_out_of_range_offset has no full-name collision). The new CI run should be green.

@clickhouse-gh

clickhouse-gh Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.70% 84.60% -0.10%
Functions 92.40% 92.40% +0.00%
Branches 77.30% 77.20% -0.10%

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

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 12, 2026
Merged via the queue into master with commit c20a603 Jun 12, 2026
166 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-compare-substrings-offset-ub branch June 12, 2026 19:41
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci 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.

4 participants