Skip to content

Build the release binaries position dependent again - #125278

Closed
groeneai wants to merge 5 commits into
ClickHouse:masterfrom
groeneai:release-position-dependent
Closed

groeneai wants to merge 5 commits into
ClickHouse:masterfrom
groeneai:release-position-dependent

Conversation

@groeneai

@groeneai groeneai commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Performance Improvement

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

The amd64 and aarch64 Linux release builds are linked as position dependent executables again, as before 26.9. As position independent executables, some queries ran 15-40% slower in some server processes, depending on the address the binary was loaded at. The code of these binaries is no longer subject to ASLR; the heap, stack and shared libraries still are. randomString with short strings is up to 2.9x faster.

Description

Related: #122447

Since #117449 / #119284 the ThinLTO builds (amd_release, arm_release, amd_cfi) are PIE, so the executable is loaded at a random address on every start. In some processes some hot loops run 15-40% slower. Performance Comparison reports whichever side drew a slow placement as a regression on unrelated PRs, for example logical_functions_small #15 on #111932. Servers running a 26.9 release can draw the same start-dependent speed.

Master-vs-master comparisons with one server more than 10% slower, on unchanged hardware
query before #117449 #119279 revert in after #119284
arm logical_functions_small #15 1/342 0/118 65/643
arm multiif #1 0/589 6/214 91/1109
amd decimal_parse #0 0/589 1/213 177/1105
amd join_squashing_blocks #0 20/589 12/213 160/1107

This restores the pre-#117449 carve-out for the builds that get -flto=thin (ENABLE_THINLTO with RelWithDebInfo, without tests or sanitizers), so these three builds fall through to the existing -fno-pie / -no-pie branch. Every other build keeps -fPIC and -pie, so the file-offset stack traces stay exercised on PIE images. Compatibility check now asserts the packaged binary is ET_EXEC.

randomString read its length through a virtual getUInt per row; in this PR's amd_release binary that loop crossed a 64-byte boundary and random_string #4 was 23% slower. It now reads integer and constant lengths directly: locally #4 takes 0.54x and #0 0.35x of master's time.

Validation: an amd_release build is ET_EXEC and loads at 0x200000 on 12 of 12 starts; the 25 stateless tests that use stack traces and symbols pass on it; the new check passes on ET_EXEC and fails on the PIE builds.

Trade-off: the release binaries' own code loses ASLR. If you prefer to keep PIE, close this and I will make the harness confirm these rows instead.

🤖 Generated with Claude Code


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

groeneai and others added 3 commits October 11, 2026 18:26
Since the release builds became position independent executables (ClickHouse#117449,
re-landed by ClickHouse#119284), the kernel loads the executable at a random address on
every start, and in some processes some hot loops run 15-40% slower depending on
that address. A restart of the same binary redraws it. In the Performance
Comparison jobs, whichever server draws a slow placement looks like a
regression, so unrelated PRs are flagged on both architectures (for example
`logical_functions_small` ClickHouse#15 on arm, `decimal_parse` #0 and
`join_squashing_blocks` #0 on amd), and users' servers of these releases draw
the same start-dependent speed.

The share of master-vs-master comparisons with one server more than 10% slower
follows the PIE commits on unchanged hardware (m7i.4xlarge / m8g.4xlarge):
arm `logical_functions_small` ClickHouse#15 1/342 before, 0/118 while the revert was in,
65/643 after the re-land; amd `decimal_parse` #0 0/589, 1/213, 177/1105.

Restore the `NOT ENABLE_THINLTO` condition, so `amd_release`, `arm_release`
and `amd_cfi` fall through to the existing `-fno-pie` / `-no-pie` branch.
Every other amd64 and aarch64 build keeps `-fPIC` and `-pie`, so the file
offset stack traces keep being exercised on PIE images by the debug and
sanitizer jobs; for an `ET_EXEC` image the main object's load address is 0,
so the stored offset equals the link-time address and resolves as before.
The compatibility check now asserts that the packaged release binary is
`ET_EXEC`, so the relocation model stays guarded.

The trade-off: the release binaries' own code is no longer subject to ASLR
(heap, stack, mmap and shared libraries still are).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Release builds with ENABLE_THINLTO get no -flto=thin (it is only added to the
RelWithDebInfo flags), so they keep -fPIC and -pie as on master.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 11, 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 rounds, 2 findings in round 1, 0 in round 2) plus my own review.

# Sev Finding Verdict Evidence / action
1 ⚠️ The carve-out tested the ENABLE_THINLTO option, but -flto=thin is only in the RelWithDebInfo flags, so a Release build with the option on would lose PIE without getting LTO (CMakeLists.txt) AGREE, fixed @ 939fb46 the condition is now ENABLE_THINLTO with RelWithDebInfo; Release + ThinLTO configures byte-identically to master (-fPIC / -pie), the CI release configuration is unchanged
2 💡 A test_crash_log comment still said every amd64/aarch64 build is PIE AGREE, fixed @ 939fb46 comment corrected; the test's assertions still hold on the position dependent amd_cfi binary

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-slot-10:20261011-224000

@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Oct 11, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? The CIDB census in the description is re-runnable and gives the same counts. The per-start variable is deterministic: the official PIE amd_release loaded at 12 different bases in 12 server starts, this build at 0x200000 in 12 of 12. The per-query slow mode itself did not reproduce on my Intel Granite Rapids box; the PR's own Performance Comparison is the arbiter.
b Root cause explained? The ThinLTO builds became -fPIC + -pie, so the kernel loads the image at a random base per start; in some processes some hot loops are 15-40% slower depending on that base, and the comparison reports whichever side drew it.
c Fix matches root cause? Yes: it removes the random image base at the link mode, the layer that creates it. No harness tolerance, no per-query change.
d Test intent preserved / new tests added? Build-configuration change: the existing Compatibility check now asserts ET_EXEC for the packaged release binary. No test of the test infrastructure is added.
e Both directions demonstrated? The new check returns True on this build and on the last position dependent official build (f7c5fb3), False with the FAILED line on the official PIE builds (36b76e1, a347fef) and on a debug build.
f Fix is general across code paths? All three ThinLTO builds (amd_release, arm_release, amd_cfi) change; a debug tree and a Release build with ENABLE_THINLTO (no -flto=thin) configure byte-identically to master. The 25 stateless tests using stack traces, system.symbols, addressToLine and XRay pass on this ET_EXEC build and on the official PIE build of the same base.
g Fix generalizes across inputs (params/datatypes/wrappers)? Both architectures: amd built and linked, arm configured with the same -fno-pie / -no-pie flags (its link is verified by Build (arm_release)).
h Backward compatible? (maintainer-approved exception only) No setting or format change. For an ET_EXEC image the stored trace value is the link-time address, which still resolves with addressToLine. Trade-off stated in the description: the release image's own code loses ASLR.
i Invariants and contracts preserved? Non-ThinLTO builds keep -fPIC / -pie, so the file-offset traces stay tested on PIE images; RELRO, -z separate-code and the stack-clash flags are unchanged.

Session id: cron:clickhouse-impl-slot-7:20261011-222900

@clickhouse-gh

clickhouse-gh Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6804dd0]

Summary: ✅


AI Review

Summary

This PR restores non-PIE linking for the actual ThinLTO RelWithDebInfo release builds, updates the compatibility guard to assert ET_EXEC on the packaged release binary, and removes a per-row virtual getUInt call from randomString for native integer and constant lengths. I did not find any remaining correctness, safety, or contract issues in the current diff, and the earlier over-broad ENABLE_THINLTO carve-out reported on this PR has been fixed in the current code.

Final Verdict
  • Status: ✅ Approve

@groeneai groeneai mentioned this pull request Oct 11, 2026
1 task done
@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 11, 2026
Comment thread CMakeLists.txt Resolved
-flto=thin is added only when ENABLE_THINLTO is set without ENABLE_TESTS and
SANITIZE, and only to the RelWithDebInfo flags. With
FAIL_ON_UNSUPPORTED_OPTIONS_COMBINATION=OFF, a configure such as
ENABLE_THINLTO=1 ENABLE_TESTS=1 only warns "Cannot enable ThinLTO", and the
carve-out still dropped -pie/-fPIC although nothing was built with LTO.
Use the same predicate as the -flto=thin block.

Configure-only check: the CI amd_release, arm_release and Release+ThinLTO
build graphs are identical to the previous commit; the ThinLTO+tests and
ThinLTO+ASan configs with FAIL_ON_UNSUPPORTED_OPTIONS_COMBINATION=OFF are
identical to master again (-pie -Wl,-pie -fPIC).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-build-system Build system (CMake, compilation, platform builds). label Oct 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6804dd0c5 with master 39e8259ca (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

⚠️ Significant changes: binary sizes, symbol sizes.

Binary sizes ⚠️
Binary Master PR Δ
programs/clickhouse-stripped 785.50 MiB 747.98 MiB -37.52 MiB (-4.78%)

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.

Object file sizes

16047 object files changed (+3.39 MiB total), 0 added.

Object file Master PR Δ
contrib/hive-metastore-cmake/CMakeFiles/_hivemetastore.dir/__/hive-metastore/ThriftHiveMetastore.cp… 5.69 MiB 5.82 MiB +134.79 KiB (+2.32%)
contrib/google-cloud-cpp-cmake/CMakeFiles/google_cloud_cpp_storage_protos.dir/google/storage/v2/sto… 1.71 MiB 1.75 MiB +44.55 KiB (+2.55%)
contrib/google-protobuf-cmake/CMakeFiles/_libprotobuf.dir/__/google-protobuf/src/google/protobuf/co… 339.96 KiB 378.15 KiB +38.19 KiB (+11.23%)
contrib/vectorscan-cmake/CMakeFiles/_vectorscan.dir/__/vectorscan/src/rose/rose_build_program.cpp.o 698.62 KiB 732.56 KiB +33.94 KiB (+4.86%)
contrib/arrow-cmake/CMakeFiles/_parquet.dir/__/arrow/cpp/src/generated/parquet_types.cpp.o 987.15 KiB 1018.02 KiB +30.87 KiB (+3.13%)
base/poco/Foundation/CMakeFiles/_poco_foundation.dir/src/Exception.cpp.o 328.03 KiB 356.80 KiB +28.77 KiB (+8.77%)
contrib/libhdfs3-cmake/CMakeFiles/_hdfs3.dir/ClientNamenodeProtocol.pb.cc.o 1.05 MiB 1.07 MiB +24.22 KiB (+2.26%)
contrib/icu-cmake/CMakeFiles/_icui18n.dir/__/icu/icu4c/source/i18n/messageformat2_data_model.cpp.o 168.89 KiB 192.73 KiB +23.83 KiB (+14.11%)
src/CMakeFiles/dbms.dir/Interpreters/ActionsDAG.cpp.o 1.33 MiB 1.35 MiB +22.02 KiB (+1.62%)
contrib/icu-cmake/CMakeFiles/_icui18n.dir/__/icu/icu4c/source/i18n/number_fluent.cpp.o 104.31 KiB 124.84 KiB +20.53 KiB (+19.68%)
contrib/llvm-project-cmake/CMakeFiles/LLVMAnalysis.dir/__/llvm-project/llvm/lib/Analysis/ScalarEvol… 1.30 MiB 1.32 MiB +18.72 KiB (+1.40%)
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeData.cpp.o 4.24 MiB 4.25 MiB +18.57 KiB (+0.43%)
contrib/llvm-project-cmake/CMakeFiles/LLVMCore.dir/__/llvm-project/llvm/lib/IR/ConstantRange.cpp.o 257.03 KiB 273.71 KiB +16.68 KiB (+6.49%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

16204 translation units recompiled, 33297 s compile time in total, 15698 of them have a recent master baseline.

Translation unit Master PR Δ vs median
contrib/aws-cmake/__/aws/generated/src/aws-cpp-sdk-s3/source/S3Client.cpp 19.9 s 13.7 s -6.2 s (-31%)
contrib/grpc-cmake/__/grpc/src/core/client_channel/client_channel.cc 6.7 s 12.7 s +6.1 s (+91%)
contrib/grpc-cmake/__/grpc/src/core/filter/fused_filters.cc 7.0 s 12.9 s +5.9 s (+85%)
contrib/grpc-cmake/__/grpc/src/core/ext/transport/chttp2/transport/frame_settings.cc 5.2 s 11.1 s +5.8 s (+112%)
contrib/grpc-cmake/__/grpc/src/core/xds/grpc/xds_listener_parser.cc 6.0 s 11.5 s +5.5 s (+91%)
contrib/grpc-cmake/__/grpc/src/core/call/server_call.cc 5.4 s 10.6 s +5.3 s (+98%)
contrib/grpc-cmake/__/grpc/src/core/ext/transport/chttp2/transport/http2_client_transport.cc 10.6 s 15.7 s +5.1 s (+48%)

Slowest changed entities of contrib/grpc-cmake/__/grpc/src/core/filter/fused_filters.cc:

  • InstantiateFunction grpc_core::MakePromiseBasedFilter<grpc_core::filters_detail::FusedFilter<grpc_core::FilterEndpoint:…: +0.6 s (+238%)
  • InstantiateFunction grpc_core::promise_filter_detail::ChannelFilterWithFlagsMethods<grpc_core::filters_detail::FusedFil…: +0.6 s (+239%)
  • InstantiateFunction grpc_core::filters_detail::FusedFilter<grpc_core::FilterEndpoint::kClient, '\r', grpc_core::ClientA…: +0.6 s (+239%)
Symbol sizes ⚠️
Binary Symbol Master PR Δ
programs/clickhouse-keeper _ZZN24TopLevelDomainLookupHash7isValidEPKcmE8wordlist 864.07 KiB removed -864.07 KiB (-100.00%)
programs/clickhouse _ZZN24TopLevelDomainLookupHash7isValidEPKcmE8wordlist 864.07 KiB removed -864.07 KiB (-100.00%)
programs/clickhouse-keeper wordlist 242.38 KiB removed -242.38 KiB (-100.00%)
programs/clickhouse wordlist 242.38 KiB removed -242.38 KiB (-100.00%)
programs/clickhouse rd_kafka_properties 131.25 KiB removed -131.25 KiB (-100.00%)
programs/clickhouse _ZGRN2DB12_GLOBAL__N_15nounsE_ 102.33 KiB removed -102.33 KiB (-100.00%)
programs/clickhouse-keeper _ZGRN2DB12_GLOBAL__N_15nounsE_ 102.33 KiB removed -102.33 KiB (-100.00%)
programs/clickhouse a_2 82.50 KiB removed -82.50 KiB (-100.00%)
programs/clickhouse-keeper a_2 82.50 KiB removed -82.50 KiB (-100.00%)

Job report

@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 31 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: b3e8b705-4538-4303-9783-ee7b985b432a
  • MIRAI run: aff35cd8-9447-441d-998b-7d0bf1cd7873
  • PR check IDs:
    • clickbench_1003144_1791786374
    • clickbench_1003157_1791786374
    • clickbench_1003163_1791786374
    • tpch_adapted_1_official_1003174_1791786374
    • tpch_adapted_1_official_1003201_1791786374
    • tpch_adapted_1_official_1003215_1791786374

randomString filled its offsets with IColumn::getUInt, an indirect call per
row. In this PR's position dependent amd_release binary that loop crosses a
64-byte boundary (master's PIE build and the position dependent master of
2026-09-07 keep it inside one line), and Performance Comparison measured
random_string #0/ClickHouse#1/ClickHouse#4/ClickHouse#5 0.03-0.04 s slower per 100M rows on Sapphire
Rapids, ClickHouse#4 by 23%.

Integer and constant lengths are now read directly, so the loop has no call;
floats, decimals and wide integers keep getUInt. Results and errors are
unchanged for every length type. On Granite Rapids random_string ClickHouse#4 takes
0.54x and #0 0.35x of the time of the master build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

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

@groeneai THis is a horrible PR. Breaking security features just so you can forget about some performance investigations is definitely not ok.

@Algunenano Algunenano closed this Oct 14, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

You are right, trading ASLR for quieter perf comparisons was the wrong call. The release binaries stay PIE; I will investigate why some PIE server processes run these queries slower and fix the cause instead.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Here is what I found. The slowdown follows the alignment of the code relative to 2 MiB, and PIE draws that alignment again at every server start.

On arm (Graviton4, kernel 6.8) the server is a PIE with 64 KiB segment alignment, so its load base is random in 64 KiB steps (gcd of 1866 measured bases: 0x10000). The text is on 4 KiB pages (no remap_executable on aarch64). For a fixed build, base mod 2 MiB alone decides whether a process is slow:

  • build 010807a35d71, the reference of 121 PR runs, json_to_string Repair subtree2 #1: the 22 runs with base mod 2 MiB in [0x140000, 0x190000) were all slow (median +51%), and none of the other 99 was more than 5.3% above the median.
  • build 3d9e5ef147f1, 117 runs: all 18 runs at 5 of the 32 residues were slow (median +19%), and none elsewhere was more than 11.2% above the median.
  • the slow runs of rand #0, vectorize_aggregation_combinators #0 and optimize_time_filter_with_preimage fix docs #6 also fall on a few residues per build.

Before PIE the binary was position dependent, so this alignment was fixed per build. I have not identified which structure in the core is sensitive to these address bits. It is not huge pages.

Two ways to remove the per-start draw:

  1. Link the release binaries with 2 MiB segment alignment (-z max-page-size=0x200000). The base stays random, but in 2 MiB steps, which also removes the variance for users. Cost: 5 bits of ASLR entropy for the executable on aarch64 and 9 on x86-64.
  2. Keep the binaries as they are and start the performance comparison servers with ASLR disabled (setarch -R, which needs seccomp=unconfined for that container). Each build then runs at one fixed alignment, as it did before PIE.

I recommend 2. @Algunenano, should I send 1 or 2?

A separate x86 observation: the comparison remaps the text onto huge pages, and since PIE the second server's text is only partly on huge pages in 74% of runs (0 of 35 before PIE). In 70 one-sided amd consistent_hashes runs, the slow side had the lower coverage 35 times and the higher 15 times.

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai

Link the release binaries with 2 MiB segment alignment

Let's do this.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Acknowledged. I will send the 2 MiB segment alignment for the release binaries as a new PR and link it here.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR: #125949

@motsc

motsc commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@groeneai follow-up to this investigation and to the "Let's do this" for 2 MiB segment alignment. We want to keep the release binaries PIE and remove the load-address-dependent slowdowns.

Please open a PR against master that:

  1. Links the ThinLTO release builds (amd_release, arm_release, amd_cfi) with -Wl,-z,max-page-size=0x200000 (2 MiB PT_LOAD alignment), keeping -pie.
  2. Gets the pr-performance label so the Performance Comparison runs on both amd_release and arm_release.

And please report, in the PR description:

  • the same master-vs-master table as above (runs with one server more than 10% slower for logical_functions_small dbms: fix build #15, multiif Repair subtree2 #1, decimal_parse #0, join_squashing_blocks #0), measured with ASLR enabled and many server restarts, for: non-PIE (pre-Print file offsets in the bare fatal stack traces and build the release binary as PIE #117449), PIE today, and PIE + 2 MiB alignment;
  • the geomean amd_release and arm_release runtime cost of PIE vs non-PIE (never measured for amd_release so far);
  • the ASLR entropy of the executable base with and without the 2 MiB alignment on x86-64 and AArch64 (e.g. from 100 starts, cat /proc/self/maps of the server).

@motsc

motsc commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@groeneai please hold: do not open a PR for this request for now. We will follow up here when it should proceed.

@motsc

motsc commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

The request above moved to #127892; please ignore it here.

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-request PR origin: a maintainer pinged or directed groeneai manual approve Manual approve required to run CI pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants