Skip to content

Fix segfault in UBSan build when calling JIT-compiled sort description - #99539

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-ubsan-jit-sort-description
Mar 20, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-ubsan-jit-sort-description

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

Summary

  • Wrap JIT-compiled sort description function calls in SortCursor::greaterAt and SimpleSortCursor::greaterAt with the existing callJITFunction (NO_SANITIZE_UNDEFINED) wrapper
  • Change callJITFunction return type from void to decltype(auto) to support non-void JIT functions like JITSortDescriptionFunc

Root cause

UBSan's -fsanitize=function reads a type signature at function_pointer - 8 before every indirect call. JIT-compiled functions lack this metadata prologue. When JIT code happens to be allocated at a page boundary, the pre-call read at pointer - 8 hits unmapped memory, causing a SIGSEGV.

The callJITFunction wrapper with NO_SANITIZE_UNDEFINED already exists and is used by all other JIT call sites (ExpressionJIT, Aggregator), but the two sort description call sites in SortCursor.h were calling the JIT function directly without the wrapper.

Found via: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=96130&sha=041d2e94874b0ca8cc527e2edec4f3e5daf012ee&name_0=PR&name_1=Stateless%20tests%20%28amd_ubsan%2C%20parallel%29
#96130

The crash occurred in test 02381_join_dup_columns_in_plan with randomized settings (join_algorithm = 'full_sorting_merge', compile_sort_description = 1) at address 0x7fbd76affff8 (unmapped page boundary).

Test plan

  • Verified compilation succeeds
  • Verified existing query produces correct results
  • UBSan CI run should no longer crash with this segfault pattern

Changelog category (leave one):

  • CI Fix or Improvement

Changelog entry:

Fix segfault in UBSan builds when JIT-compiled sort description function is allocated at a page boundary.

🤖 Generated with Claude Code

Version info

  • Merged into: 26.4.1.44
  • Backported to: 26.3.10.9

UBSan's `-fsanitize=function` reads a type signature at
`function_pointer - 8` before every indirect call. JIT-compiled
functions lack this prologue, and when the JIT code sits at a page
boundary, the pre-call read hits unmapped memory, causing a SIGSEGV.

The `callJITFunction` wrapper (with `NO_SANITIZE_UNDEFINED`) already
exists and is used for all other JIT function calls, but the two call
sites in `SortCursor::greaterAt` and `SimpleSortCursor::greaterAt`
were missing it.

Also change `callJITFunction` to return `decltype(auto)` instead of
`void` so it can be used with non-void JIT functions like
`JITSortDescriptionFunc` (returns `int8_t`).

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=96130&sha=041d2e94874b0ca8cc527e2edec4f3e5daf012ee&name_0=PR&name_1=Stateless%20tests%20%28amd_ubsan%2C%20parallel%29

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

clickhouse-gh Bot commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5ffe3e7]

Summary: ✅


AI Review

Summary

This PR wraps JIT sort comparator calls with callJITFunction and makes callJITFunction return decltype(auto) so non-void JIT function pointers are supported. The change is small, targeted, and aligned with existing JIT call patterns. I did not find high-confidence correctness, safety, performance, or compatibility issues that require changes.

ClickHouse Rules

Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
Safe rollout ✅
Compilation time ✅

Final Verdict

  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 16, 2026
The `callJITFunction` with `decltype(auto)` return now propagates the
`int8_t` return type of `JITSortDescriptionFunc`, which clang-tidy flags
as `bugprone-signed-char-misuse` when assigned to `int`. Add NOLINT
comments since the sign is meaningful here (comparator returns -1/0/1).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added pr-ci and removed pr-bugfix Pull request with bugfix, not backported by default labels Mar 16, 2026
@Algunenano Algunenano self-assigned this Mar 16, 2026

@Algunenano Algunenano 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.

Looks good. AFAICT with this all places calling JIT functions are covered. I couldn't think of a simple way to cover this with the style check, and building a clang-tidy plugin seems way overkill.

@clickhouse-gh

clickhouse-gh Bot commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 24.00% 24.00% +0.00%
Branches 76.30% 76.30% +0.00%

PR changed lines: PR changed-lines coverage: 71.43% (10/14, 0 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Mar 20, 2026
Merged via the queue into master with commit faedb73 Mar 20, 2026
159 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-ubsan-jit-sort-description branch March 20, 2026 08:01
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 20, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Apr 15, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Apr 15, 2026
Cherry pick #99539 to 26.3: Fix segfault in UBSan build when calling JIT-compiled sort description
robot-clickhouse added a commit that referenced this pull request Apr 15, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Apr 15, 2026
clickhouse-gh Bot added a commit that referenced this pull request Apr 15, 2026
Backport #99539 to 26.3: Fix segfault in UBSan build when calling JIT-compiled sort description
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-ClickHouse__ClickHouse that referenced this pull request Oct 29, 2026
In UBSan builds `-fsanitize=function` reads a type signature at
`callee - 8` before every indirect call. JIT-compiled code has no such
prologue, so when the page right before the JIT code is unmapped the read
faults in the caller, at the call line. ClickHouse#98671 and ClickHouse#99539 fixed this for
the expression, aggregate and sort description JIT with `callJITFunction`
(`NO_SANITIZE_UNDEFINED`); the regexp JIT, added later, calls
`matcher.func` directly in `match`, `extract`, `extractAll` and
`replaceRegexpOne/All`.

This is the SIGSEGV at `MatchImpl.h:183` reported in ClickHouse#127747: for a
`LowCardinality` argument the function runs over the dictionary, so even
the 0-row PREWHERE header evaluation calls the JIT matcher. Whether the
read faults depends on the memory layout around the JIT code pages, so the
query alone does not reproduce it.

`callJITFunction` lives in `compileFunction.h`, which needs
`USE_EMBEDDED_COMPILER` and LLVM headers, while the regexp callers compile
without them. Move it unchanged to a dependency-free
`Interpreters/JIT/callJITFunction.h`, include it from `compileFunction.h`
and `CompileRegexp.h`, and route the four calls through it.

Verified on a `-DSANITIZE=undefined` build: before the change each of the
eight regexp objects has a `cmpl $0xc105cafe,-0x8(%reg)` at the call line;
after it there is none, the indirect call remains, and each object's total
count of such checks drops by exactly the number of call sites. Release
builds are unaffected.

Closes ClickHouse#127747

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-ci pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.3-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants