Skip to content

Enable fast libcxx hardening for release builds - #94757

Merged
mstetsyuk merged 36 commits into
masterfrom
fast-libcxx-hardening-for-release-builds
Feb 10, 2026
Merged

mstetsyuk merged 36 commits into
masterfrom
fast-libcxx-hardening-for-release-builds

Conversation

@mstetsyuk

@mstetsyuk mstetsyuk commented Jan 21, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Enable fast libcxx hardening for release builds. This is mostly needed for out-of-bounds checks. Given performance tests results, no noticeable performance impact is expected.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Description

In my experience, most out-of-bounds-related segfaults occur only after we go out of bounds and, as a result, they are very difficult to debug as we can't connect "pointer XYZ contained garbage and dereferencing it caused segfault" type of segfaults to concrete out-of-bounds calls. We can only hypothesize that "maybe that vector we accessed higher up in the call stack was actually empty".

Bounds checking in STL containers will make out-of-bounds crashes safe (no memory corruption) and simpler to debug:

  1. If we go out of bounds, it will execute a trap instruction, which will result in receiving SIGABRT -> capturing the stack trace and logging all the info into system.crash_log -> having the crash processed by the observability bot.
  2. SIGILL in the issue will be strongly indicative that it's an out-of-bounds issue (we almost never get such crashes otherwise).
  3. We will know the exact stack trace of the out-of-bounds call - much more likely to be debuggable.
  4. When debugging other issues, we may assume that we couldn't have gone out of bounds.

This is why we need to enable fast libcxx hardening for release builds. Effectively, it only enables bounds checking.

Fast mode is supposed to have very small performance impact and be suitable for production use. In Google, this resulted in only 0.3% regression and around 30% fewer segfaults. This is consistent with the performance tests results. There were a few specific workloads that saw 10-15% degradation, but that has been remediated by disabling the hardening on the per-TU basis.

Version info

  • Merged into: 26.2.1.523

@clickhouse-gh

clickhouse-gh Bot commented Jan 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9570042]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_binary, old analyzer, s3 storage, DatabaseReplicated, sequential) failure
03144_parallel_alter_add_drop_column_zookeeper_on_steroids FAIL cidb
Integration tests (amd_asan, db disk, old analyzer, 5/6) failure
test_acme_tls/test_single_node.py::test_acme_authorization FAIL cidb
test_acme_tls/test_multi_node.py::test_coordinated_acme_authorization FAIL cidb
Integration tests (amd_binary, 5/5) failure
test_acme_tls/test_multi_node.py::test_coordinated_acme_authorization FAIL cidb
test_acme_tls/test_single_node.py::test_acme_authorization FAIL cidb
Integration tests (arm_binary, distributed plan, 1/4) failure
test_acme_tls/test_multi_node.py::test_coordinated_acme_authorization FAIL cidb
test_acme_tls/test_single_node.py::test_acme_authorization FAIL cidb
Integration tests (amd_tsan, 5/6) failure
test_acme_tls/test_single_node.py::test_acme_authorization FAIL cidb
test_acme_tls/test_multi_node.py::test_coordinated_acme_authorization FAIL cidb

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jan 21, 2026
Comment thread cmake/cxx.cmake Resolved
@rschu1ze rschu1ze self-assigned this Jan 21, 2026
@clickhouse-gh clickhouse-gh Bot added the submodule changed At least one submodule changed in this PR. label Jan 26, 2026

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

Please investigate the perf test results. We don't have to accept all slowdowns - maybe we could use PODArray in critical places or be even more creative.

@mstetsyuk

mstetsyuk commented Jan 30, 2026 •

Copy link
Copy Markdown
Member Author

identified a short list of clickbench tests with sustained degradations (~10-15%)

so far, flame graphs are not pointing exactly where the issue is:
hardening.zip
master.zip

so I've prepared a script to build a diff flame graph: https://pastila.clickhouse.com/?0013b4b5/67323b50b24e445ba18c3328c8e836b0#gtKaKkG81LWc/rAYqEOgzg==GCM

@mstetsyuk

Copy link
Copy Markdown
Member Author

profiling results:
before.zip
after.zip
diff.zip

tldr:

  • The hash join code only struggles in IColumnHelper<ColumnString, IColumn>::fillFromRowRefs.
  • The whole code of the WindowTransform class requires refactoring as there are way too many random access operations.

will optimise both in separate PRs and reassess the performance impact

btw, here's the script I used to automate spinning up servers, creating and populating tables, running queries, dumping traces, and generating flame graphs: https://pastila.clickhouse.com/?0013b4b5/67323b50b24e445ba18c3328c8e836b0#gtKaKkG81LWc/rAYqEOgzg==GCM

@mstetsyuk

mstetsyuk commented Feb 1, 2026 •

Copy link
Copy Markdown
Member Author

PR for optimising window functions: #95746 (closed - will land the changes here)

@mstetsyuk

mstetsyuk commented Feb 2, 2026 •

Copy link
Copy Markdown
Member Author

WindowTransform optimisation

Enabling fast libc++ hardening makes WindowTransform around 10% slower, according to ClickBench. This is because in many places it performs std::vector (or std::deque) random access for each processed row which, with libc++ hardening, also involves bounds checking.

To optimise WindowTransform, we need to get rid of superfluous std::vector (or std::deque) random accesses:

  1. Avoid calling WindowTransform::inputAt and WindowTransform::blockAt for every processed row. Reuse the result for each block where possible.
  2. For WindowTransform::partition_by_indices, WindowTransform::order_by_indices, WindowFunctionWorkspace::argument_column_indices, use PODArray instead of std::vector.
  3. Do std::vector<...>::data() where necessary.

Even with libc++ hardening enabled, profiling shows >10% less CPU consumed in WindowTransform as a result of these changes (in most cases).

Overview:
https://pastila.clickhouse.com/?01c1b747/6e10ee7e12fe3d01c8b7d39a8e3280bb#A2ewmTZgTuhngIZoJnJpHw==GCM

Flame graphs:
before.zip
after.zip

Hash Join optimisation

Much more trivial.

  1. Use PODArray instead of std::vector in ColumnsInfo::replicated_columns.
  2. Use std::vector<...>::data() in a few places.

With these changes, profiling is showing the same query durations as before.

/// to the replicated column for each column in the list above.
/// If columns is not Replicated, pointer will be nullptr.
std::vector<const ColumnReplicated *> replicated_columns;
PODArray<const ColumnReplicated *> replicated_columns;

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.

It would be nice if you could add a brief comment why PODArray was chosen (basically because it does no bound check whereas std::vector does due to hardening).

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

I'll approve to unblock you. Didn't look at the window transform changes (am not familiar with this code) - git blame will be your friend if you like a review 😄

@nickitat nickitat self-assigned this Feb 2, 2026
@nickitat

nickitat commented Feb 3, 2026

Copy link
Copy Markdown
Member

Please provide a short changelog entry and move the current PR description into a separate place

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

Regarding the files that required significant rewriting (especially the use of raw pointers), can we consider overriding the hardening mode on a per-TU basis?

Comment thread cmake/cxx.cmake Resolved
Comment thread src/CMakeLists.txt Resolved
@mstetsyuk
mstetsyuk marked this pull request as draft February 4, 2026 22:01
Comment thread contrib/llvm-project Resolved
Comment thread src/CMakeLists.txt Resolved
Comment thread src/Dictionaries/CMakeLists.txt Resolved
@mstetsyuk mstetsyuk removed the submodule changed At least one submodule changed in this PR. label Feb 9, 2026
@mstetsyuk
mstetsyuk marked this pull request as ready for review February 9, 2026 17:25
@mstetsyuk
mstetsyuk added this pull request to the merge queue Feb 10, 2026
Merged via the queue into master with commit 19310c7 Feb 10, 2026
125 of 130 checks passed
@mstetsyuk
mstetsyuk deleted the fast-libcxx-hardening-for-release-builds branch February 10, 2026 14:46
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance pr-improvement Pull request with some product improvements 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