Skip to content

Link with --icf=safe: --icf=all merges address-taken functions and crashes gRPC compression - #122668

Merged
Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:link-with-icf-safe
Sep 28, 2026
Merged

Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:link-with-icf-safe

Conversation

@groeneai

@groeneai groeneai commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #115121

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

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

Link with --icf=safe instead of --icf=all, so the linker keeps functions whose address is taken distinct. gRPC queries with transport compression (gzip, deflate) no longer crash the server, as they could in official builds since 26.4. Closes #115121.

Description

Since 26.4, official builds crash on gRPC queries with transport compression (SIGSEGV, Address: 0x50, in free from zlib-ng deflateEnd/inflateEnd). Failing job: https://github.com/ClickHouse/ClickHouse/actions/runs/36373749054/job/108786682420

Cause. -Wl,--icf=all (#99474) folds identical functions even when their address is taken: for code, lld ignores the -faddrsig tables the build emits. With ThinLTO, gRPC's zlib free callback zfree_gpr is byte-identical to zlib-ng's default zcfree, so both get one address. zlib-ng 2.1.7 picks the buffer layout by comparing callbacks with its defaults: alloc_aligned sees a custom zalloc and returns a 64-byte aligned pointer, with the original stored below it; free_aligned sees zfree == zcfree and frees the aligned pointer. When it is the second page of a large jemalloc extent (1 in 64 per buffer), the free crashes; otherwise an interior pointer is freed. CI saw it only in WeeklyCFI, the only integration build with ThinLTO.

Change. With --icf=safe, lld keeps functions whose address is significant distinct. Virtual methods, constructors, destructors and functions whose address is never taken still fold.

Validation.

  • Official images, gzip/deflate gRPC loop: 26.8.10.6 crashed in 7/7 runs within 22 queries; 26.3.33.24 (no --icf=all) ran 2000/2000.
  • Local ThinLTO build of master, before: 3/3 crashes, the request side alone 4/4, and with 1 KB requests heap corruption symptoms. After: 34000 queries, no failure. Sites comparing against a folded address: 87 before, 3 after, none an identity test.
  • Size: .text +2.21 MiB (+0.46%), stripped +2.97 MiB (+0.45%), zstd +0.55 MiB (+0.28%).
  • New test 05291_grpc_transport_compression fails on the local LTO build before, passes after. PR CI runs no stateless job on an LTO build; it guards the MasterCI amd_release/arm_release stateless jobs.

Alternative with no size cost: drop the identity shortcuts in the zlib-ng fork (gone upstream since 2.2.0, zlib-ng#1713); it leaves the other comparison sites.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.942-master (included in 26.10 and later)
  • Backported to: 26.9.6.3, 26.8.15.4, 26.7.17.6

`-Wl,--icf=all` (ClickHouse#99474) lets lld fold byte-identical functions even
when their address is taken, which breaks the guarantee that different
functions have different addresses. In ThinLTO builds `gpr_free` is
inlined into gRPC's zlib free callback `zfree_gpr`, which becomes
`mov %rsi,%rdi; jmp free`, byte-identical to zlib-ng's default `zcfree`,
so both get one address. Measured with llvm-nm: the official 26.8.10.6
image and a local ThinLTO build of master fold them (12 functions share
that address locally); 26.3.33.24 and the no-LTO amd_binary do not.

zlib-ng 2.1.7 (`zutil.c`) chooses the buffer layout by comparing the
stream's callbacks with its defaults. `alloc_aligned` sees
`zalloc_gpr != zcalloc`, allocates 8 + n + 64 bytes and returns the
64-byte aligned pointer with the original one stored below it.
`free_aligned` sees `zfree_gpr == zcfree` and frees the aligned pointer
itself. jemalloc places a large allocation at a random cache line of the
extent's first page (JEMALLOC_CACHE_OBLIVIOUS); when it is the last line
(1 in 64), the freed pointer is the extent's second page, which the emap
does not register, and `ifree` -> `prof_free` reads `e_prof_tctx` from a
NULL `edata_t`: `Address: 0x50`, as in the CI crashes. Otherwise an
interior pointer is freed.

Every gRPC call with transport compression goes through this path:
`deflateEnd` for the result and `inflateEnd` for a compressed request.
In CI only the WeeklyCFI integration jobs showed it, because amd_cfi is
the only integration build with ThinLTO.

In safe mode lld does not fold functions whose address is significant
according to the `-faddrsig` tables the build already emits. Virtual
methods, constructors and destructors (`unnamed_addr`) and functions
whose address is never taken are still folded; objects without an
address-significance table (Rust) are not folded.

Measured on a local ThinLTO build of master (clang-22, lld-22, -g0):
- `zfree_gpr` and `zcfree`: one address before, two after.
- `lea F; cmp` sites where F is shared by different functions: 87
  before (20 are zlib-ng's `zfree == zcfree` test inlined into rdkafka,
  rocksdb, arrow, orc, libarchive, minizip, gRPC and zlib-ng itself), 3
  after: the source alias `AES_encrypt`/`asm_AES_encrypt`, and twice a
  destructor passed to `__cxa_throw`, none an identity test.
- gRPC loop with gzip/deflate transport compression: before, the server
  crashed in 3/3 runs within 6 queries (the official 26.8.10.6 image in
  7/7 within 22, 26.3.33.24 ran 2000/2000), the request side alone in
  4/4, and 1 KB requests also gave a crash at a wild address in
  `grpc_core::Arena::~Arena()` and a spurious decompression error.
  After: 34000 queries without a failure.
- Size: `.text` +2.21 MiB (+0.46%), stripped binary +2.97 MiB (+0.45%),
  zstd -3 of the stripped binary +0.55 MiB (+0.28%). Folded code:
  52.0 MiB before, 50.6 MiB after.

The new test sends compressed gRPC requests and results. It fails on
the ThinLTO build without this change and guards the MasterCI release
stateless jobs; PR CI has no ThinLTO build.

An alternative with no size cost is to drop the identity shortcuts from
our zlib-ng fork (upstream 2.2.0 no longer has `alloc_aligned` /
`free_aligned`, zlib-ng#1713), but that leaves the other comparison
sites.

Closes ClickHouse#115121.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Sep 28, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 findings), plus one nit from my own review.

# Sev Finding Verdict Evidence / action
1 ⚠️ The test only checks 200 correct results, so it cannot tell whether compression ran, and no PR lane links with ThinLTO DISAGREE On an --icf=all ThinLTO build the test crashes the server 3/3 in deflateEnd and zlib_decompress; without the long comment the request is sent uncompressed and 3000/3000 pass, so the compression options are live. The server exposes no transport-compression observable, and test_grpc_protocol::test_transport_compression uses the same oracle. PR CI runs no stateless job on an LTO build; the test guards the MasterCI amd_release/arm_release jobs.
2 💡 Category should be Critical Bug Fix DISAGREE The diff is a link flag. A bug-fix category requires a Bugfix validation reproduction on the sanitizer/debug master builds, where these functions are never folded. The changelog entry names the crash.
3 💡 With --icf=safe, lld prints one warning each for the prql and polyglot Rust archives (conservatively ignores SHT_LLVM_ADDRSIG ... with sh_link=0), whose objects cmake/strip_rust_symbols.sh repacks with ld -r AGREE, not blocking Harmless: lld then treats those objects as address-significant and does not fold them.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20260928-094400

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The fold is visible in every ThinLTO build with --icf=all (llvm-nm: zfree_gpr and zcfree at one address), and a gRPC gzip/deflate loop crashed the server in 13/13 runs within 22 queries (official 26.8.10.6 image 7/7, local ThinLTO build 3/3, new test 3/3).
b Root cause explained? --icf=all gives gRPC's zfree_gpr and zlib-ng's zcfree one address; zlib-ng 2.1.7 alloc_aligned takes the custom-allocator layout (aligned pointer, original stored below) while free_aligned sees zfree == zcfree and frees the aligned pointer; on the second page of a large jemalloc extent that is a NULL edata read at 0x50.
c Fix matches root cause? Yes. --icf=safe stops folding functions whose address is significant, which restores the distinct-address guarantee zlib-ng relies on. No guard at the crash site.
d Test intent preserved / new tests added? New 05291_grpc_transport_compression: 200 gRPC queries, gzip and deflate, compressed request and result. Red on the local ThinLTO build before the change (3/3), green after (10/10) and on debug (50/50).
e Both directions demonstrated? Yes, on build-id-checked servers: before, 3/3 crashes (request side 4/4); after, 34000 queries with no failure. Official 26.3.33.24 (no --icf=all) 2000/2000, 26.8.10.6 7/7 crashes.
f Fix is general across code paths? Yes. One flag covers every site that compares a function pointer: 87 such sites against a folded address before (20 of them zlib-ng copies in rdkafka, rocksdb, arrow, orc, libarchive, minizip, gRPC and zlib-ng itself), 3 after, none an identity test.
g Fix generalizes across inputs (params/datatypes/wrappers)? gzip and deflate, response compression and request decompression (isolated), small and 1-2 KB messages, SSL channel and a 1M-row result (test_grpc_protocol_ssl, test_transport_compression pass). Type wrappers: N/A (link flag).
h Backward compatible? (maintainer-approved exception only) Yes. No settings, formats or on-disk changes; .text +0.46%.
i Invariants and contracts preserved? Two different address-significant functions never share an address. unnamed_addr folding (virtual methods, ctors, dtors) and folding of never-referenced functions are kept (folded code 52.0 -> 50.6 MiB). Sanitizer, coverage and Darwin links are unchanged.

Session id: cron:clickhouse-impl-slot-4:20260928-073300

@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bcc3e17]

Summary: ✅


AI Review

Summary

This PR switches Linux lld executable links from --icf=all to --icf=safe and adds a stateless gRPC regression test, which is the right direction for the reported transport_compression server SIGSEGV. I did not find a correctness problem in the code change itself, but the PR metadata understates the user-facing impact: this is a stable-release crash fix, not just a build-system improvement.

PR Metadata
  • The selected Changelog category is not semantically correct for the change. The diff fixes a server exception in official stable builds, so it belongs under Critical Bug Fix, not Build/Testing/Packaging Improvement.
  • The Changelog entry is required for that category and the current entry text is specific and user-readable; only the category needs to change.
  • Exact replacement:
    - Critical Bug Fix (crash, data loss, RBAC)
Findings

💡 Nits

  • [CMakeLists.txt:194] The PR is categorized as Build/Testing/Packaging Improvement, but this line is the actual fix for a user-visible server SIGSEGV in official stable builds. Use Critical Bug Fix so release notes surface it where operators will look for crash fixes.
Final Verdict

✅ Code change looks sound. Please reclassify the changelog category to Critical Bug Fix.

LLVM Coverage Report

Measured on commit bcc3e17.

Metric Baseline Current Δ
Lines 87.90% 87.90% +0.00%
Functions 91.70% 91.80% +0.10%
Branches 79.30% 79.30% +0.00%

Changed lines: Uncovered code analysis did not run: No coverable C/C++ source files changed (contrib/ is excluded from coverage).

Newly covered: +223 lines in 85 files (-116 lines lost coverage) · Details

Full report

@clickhouse-gh clickhouse-gh Bot added the pr-build Pull request with build/testing/packaging improvement label Sep 28, 2026
@Algunenano Algunenano self-assigned this Sep 28, 2026
Comment thread CMakeLists.txt Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-build-system Build system (CMake, compilation, platform builds). label Sep 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bcc3e172d with master b58874494 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of ab6b45e4f; 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 764.78 MiB 765.50 MiB +742.77 KiB (+0.09%)

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.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

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

Job report

@Algunenano Algunenano added pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-critical-bugfix and removed pr-build Pull request with build/testing/packaging improvement labels Sep 28, 2026
@Algunenano
Algunenano added this pull request to the merge queue Sep 28, 2026
Merged via the queue into ClickHouse:master with commit 59bc767 Sep 28, 2026
149 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 28, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 28, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 28, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 28, 2026
Backport #122668 to 26.9: Link with `--icf=safe`: `--icf=all` merges address-taken functions and crashes gRPC compression
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 28, 2026
…address-taken functions and crashes gRPC compression
clickhouse-gh Bot added a commit that referenced this pull request Sep 28, 2026
Backport #122668 to 26.8: Link with `--icf=safe`: `--icf=all` merges address-taken functions and crashes gRPC compression
Algunenano added a commit that referenced this pull request Sep 28, 2026
Backport #122668 to 26.7: Link with `--icf=safe`: `--icf=all` merges address-taken functions and crashes gRPC compression
@azat

azat commented Sep 30, 2026

Copy link
Copy Markdown
Member

Haha, this is PR also fixes attach with lldb, which attaches to __jit_debug_register_code which with -Wl,--icf=all maps too much functions into one address, and it constantly hitting this breakpoint which makes server under debugger unusable.

kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
…l` merges address-taken functions and crashes gRPC compression
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
…l` merges address-taken functions and crashes gRPC compression
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 comp-build-system Build system (CMake, compilation, platform builds). groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server crash (SIGSEGV) in gRPC response compression (zlib_compress / deflateEnd)

6 participants