Skip to content

Enable -fstack-protector-strong - #119684

Closed
alexey-milovidov wants to merge 1 commit into
masterfrom
enable-stack-protector-strong
Closed

alexey-milovidov wants to merge 1 commit into
masterfrom
enable-stack-protector-strong

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 12, 2026 •

Copy link
Copy Markdown
Member

An overflow of a stack local currently corrupts the frame silently. Nothing in the build emits a canary, and _FORTIFY_SOURCE is not set either, so a memcpy with an unchecked length just writes through its destination and, about two kilobytes further on, through the saved return address.

-fstack-protector-strong instruments every function that has a local array, a local whose address is taken, or a variable-length allocation, which is the shape these bugs actually take. The plain -fstack-protector is not enough: it covers only character arrays of at least eight bytes, so it would not have instrumented the BIT column overflow fixed in #119680, whose destination was a UInt64 whose address had been taken. I verified both claims by disassembling the real object files.

Measured per translation unit on an aarch64 release build, using the actual compile commands:

translation unit text size functions canaried
Columns/ColumnVector +1.92% 1054/3717 (28%)
Interpreters/Aggregator +2.13% 1869/2593 (72%)
Parsers/ExpressionElementParsers +2.63% 97/299 (32%)
Processors/Sources/MySQLSource +3.06% 76/225 (34%)

For reference, -fstack-protector-all costs +10.4% and +12.4% of text on the latter two.

Runtime, on Neoverse V2, minimum over five interleaved runs of fifteen repetitions:

shape baseline -fstack-protector-strong delta
non-inlined call, four-instruction body 1.0908 ns/call 1.9375 ns/call +77.6%
same prologue, 65528 elements per call 0.17976 ns/elem 0.17968 ns/elem -0.04% (noise)

So the cost is +0.85 ns per instrumented call, invisible in vectorised code and real in call-bound code. ClickHouse spends its time in the former shape but has plenty of the latter in the parser and the analyzer, which is exactly what the performance tests are for - hence this draft.

One consequence to accept deliberately: this converts silent corruption into SIGABRT, so a latent overflow becomes a server restart rather than continued execution on a corrupted frame.

Related: #119680
Related: #119685

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

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

Build with -fstack-protector-strong, so that an overflow of a stack local aborts immediately instead of silently corrupting the stack frame.


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

An overflow of a stack local currently corrupts the frame silently: nothing in
the build emits a canary, and `_FORTIFY_SOURCE` is not set either, so a
`memcpy` with an unchecked length simply writes through its destination and,
about two kilobytes further on, through the saved return address.

`-fstack-protector-strong` instruments every function with a local array, a
local whose address is taken, or a variable-length allocation, which is the
shape such bugs actually take. The plain `-fstack-protector` is not enough: it
covers only character arrays of at least eight bytes, so it would not have
instrumented the `BIT` column overflow fixed in #119680, whose destination was
a `UInt64` whose address was taken.

Measured on an aarch64 release build, per translation unit, against the real
compile commands:

    Columns/ColumnVector                +1.92% text,  28% of functions canaried
    Interpreters/Aggregator             +2.13% text,  72%
    Parsers/ExpressionElementParsers    +2.63% text,  32%
    Processors/Sources/MySQLSource      +3.06% text,  34%

A microbenchmark on Neoverse V2 puts the cost at +0.85 ns per instrumented
call, which is +78% for a function whose body is four instructions and below
the noise floor once a call processes a block of values: summing 65528 elements
per call measured -0.04%, i.e. no difference. ClickHouse spends its time in the
latter shape, but it has plenty of the former in the parser and the analyzer,
which is what the performance tests need to price.

Draft: opened to get a performance comparison and a full CI run.

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

clickhouse-gh Bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e5bcc6f]

Summary: ❌

job_name test_name status info comment
Upgrade check (amd_release) FAIL
Logical error: Virtual row has different type for A. Expected B, got C (STID: 1637-309b) FAIL cidb

AI Review

Summary

This PR enables -fstack-protector-strong globally by appending it to the top-level COMPILER_FLAGS. The build-system change itself is straightforward and the changelog metadata looks correct, but I cannot approve it yet because the rollout still lacks ClickHouse-level performance evidence: the dedicated Performance Comparison reports for this commit are empty, so we do not have proof that the parser/analyzer-heavy call-bound paths mentioned in the PR description stay within an acceptable cost envelope.

Tests
  • ⚠️ [CMakeLists.txt:385] The current evidence is a synthetic microbenchmark plus a few translation-unit text-size deltas, but the PR has no actual Performance Comparison results because it is not labeled pr-performance. Please run the standard perf comparison, or provide equivalent focused before/after measurements on the call-bound parser/analyzer surfaces that this change is most likely to affect.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: provide real ClickHouse-level before/after performance evidence for the global -fstack-protector-strong rollout, ideally by running the standard Performance Comparison jobs for this PR.

LLVM Coverage Report

Measured on commit e5bcc6f.

Metric Baseline Current Δ
Lines 89.00% 89.00% +0.00%
Functions 91.80% 91.70% -0.10%
Branches 81.40% 81.30% -0.10%

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

Newly covered: +271 lines in 73 files (-170 lines lost coverage) · Details

Full report

@clickhouse-gh clickhouse-gh Bot added pr-build Pull request with build/testing/packaging improvement comp-build-system Build system (CMake, compilation, platform builds). labels Sep 12, 2026
Comment thread CMakeLists.txt
# address. `-strong` rather than the plain `-fstack-protector`, which covers only character arrays
# of at least eight bytes and so misses, for instance, a `memcpy` into an integer local whose
# address was taken.
set (COMPILER_FLAGS "${COMPILER_FLAGS} -fstack-protector-strong")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The rollout contract here is not just "pass the build" but "enable this globally without an unacceptable cost on real ClickHouse workloads." Right now the PR still has no ClickHouse-level before/after data: the dedicated Performance Comparison reports for this commit are empty because the PR is not labeled pr-performance, so the only evidence is a synthetic microbenchmark plus a few translation-unit text-size deltas. Given that the description itself calls out parser/analyzer code as the sensitive call-bound case, please run the standard perf comparison (or attach equivalent focused before/after measurements on those paths) before merging.

@clickhouse-gh

clickhouse-gh Bot commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing e5bcc6fc9 with master 261845422 (stripped binary size, per-symbol sizes and ThinLTO time; 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 715.46 MiB 717.14 MiB +1.68 MiB (+0.23%)

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

16007 translation units recompiled, 31074 s compile time in total, 15538 of them have a recent master baseline.

Translation unit Master PR Δ vs median
contrib/grpc-cmake/__/grpc/src/core/ext/transport/chttp2/transport/http2_client_transport.cc 10.3 s 15.7 s +5.5 s (+53%)

Job report

@alexey-milovidov
alexey-milovidov marked this pull request as ready for review September 13, 2026 12:59

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

No significant degradations.

@Algunenano

Copy link
Copy Markdown
Member

Duplicated: #119039

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-build-system Build system (CMake, compilation, platform builds). pr-build Pull request with build/testing/packaging improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants