Repository navigation
Conversation
Build with `-mbranch-protection=standard`: return addresses are signed with pointer authentication (PAC), and indirect branches may only land on BTI landing pads. Both are hint instructions, so the binary still runs on CPUs without the extensions (they execute as no-ops). On Graviton the kernel and `ld.so` (glibc >= 2.32) map the executable with `PROT_BTI`. The linker enables BTI for the executable only if every input object has the BTI property, and silently drops it otherwise. `-z bti-report=warning` (an error with `--fatal-warnings`) keeps it from being lost. The objects that lacked it: - the startup objects of the sysroot (glibc 2.28 `Scrt1.o`, `crti.o`, `crtn.o`, gcc 8.3 `crtbeginS.o`/`crtendS.o`) and `libc_nonshared.a`. They can't just be marked: `ld.so` and `__libc_start_main` enter `_start` and `__libc_csu_init` through indirect branches. They are replaced by `base/glibc-compatibility/crt/aarch64/crt1.c`, linked with `-nostartfiles`; - the musl `syscall.s`/`longjmp.s` of `glibc-compatibility`; - the XRay trampolines of compiler-rt, entered from the patched sleds with `blr`: a copy with landing pads is generated at configure time; - the ICU data file; - the prebuilt Rust standard library: rebuilt with `-Zbuild-std`. Enabled by default for AArch64 builds with `GLIBC_COMPATIBILITY` and without sanitizers (`ENABLE_BRANCH_PROTECTION`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [e24c0f0] Summary: ✅
AI ReviewSummaryEnables BTI on supported AArch64 Linux builds while retaining return-address signing, including startup code, musl assembly, XRay instrumentation, Rust libraries, and JIT code. Reviewed the current diff and all prior discussion; the earlier XRay protection, startup LTO, and Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit e24c0f0.
Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code |
XRay makes the pages of a patched function writable and then executable again with `mprotect`, without `PROT_BTI`, so `SYSTEM INSTRUMENT ADD` and `REMOVE` left those pages unguarded. Compile a copy of `xray_interface.cpp` that adds `PROT_BTI` to both calls when the kernel supports BTI (`HWCAP2_BTI`, otherwise `mprotect` rejects the flag). Checked on Graviton4: after `SYSTEM INSTRUMENT ADD` and `REMOVE ALL`, the patched pages are split into their own mapping, which keeps the `bt` VmFlag. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`crt1.o` is passed through the linker flags, and the target dependency on it only orders the build, so a change of `crt1.c` left existing executables with the old startup code. `INTERFACE_LINK_DEPENDS` of `glibc-compatibility`, which every executable links, makes them depend on it. Checked with an incremental build: a symbol added to `crt1.c` appears in `programs/clickhouse`, and disappears after reverting. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing Binary sizes
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 Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
contrib/icu-cmake/CMakeFiles/_icudata.dir/icudata_bti.S.o |
new | 31.57 MiB | +31.57 MiB |
contrib/compiler-rt-cmake/CMakeFiles/clang_rt_xray.dir/xray_interface_bti.cpp.o |
new | 81.73 KiB | +81.73 KiB |
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
17352 translation units recompiled, 27372 s compile time in total, 17328 of them have a recent master baseline.
Symbol sizes
| Binary | Symbol | Master | PR | Δ |
|---|---|---|---|---|
programs/clickhouse |
polyglot_sql::dialects::transform_recursive::<alloc::boxed::Box<dyn core::ops::function::Fn<(polygl… |
new | 125.41 KiB | +125.41 KiB |
programs/clickhouse |
polyglot_sql::dialects::transform_recursive::<alloc::boxed::Box<dyn core::ops::function::Fn<(polygl… |
122.03 KiB | removed | -122.03 KiB (-100.00%) |
programs/clickhouse |
_RNvMNtNtNtCsg2FZ5uNKT51_12vortex_array6arrays8listview7rebuildINtNtNtB8_5array5typed5ArrayNtNtB4_6… |
new | 95.55 KiB | +95.55 KiB |
programs/clickhouse |
_RNvMNtNtNtCsiHECVbxDD8I_12vortex_array6arrays8listview7rebuildINtNtNtB8_5array5typed5ArrayNtNtB4_6… |
93.48 KiB | removed | -93.48 KiB (-100.00%) |
programs/clickhouse |
_RNvXsP_NtCsdm63tYlQ8NV_12polyglot_sql11expressionsNtB5_10ExpressionNtNtCs9V8cfPKzrg0_4core5clone5C… |
new | 73.63 KiB | +73.63 KiB |
programs/clickhouse |
anon.22a44b3817b2919f73e742ff8b3d7d94.2995 |
new | 68.86 KiB | +68.86 KiB |
programs/clickhouse |
anon.c6e8f43c6b3d62633f956b37d839031e.2995 |
68.86 KiB | removed | -68.86 KiB (-100.00%) |
With BTI enforced, an indirect branch into a guarded page costs extra on Graviton4, so the two per-row virtual calls of these functions made the `bounding_ratio` and `random_string` performance tests about 19% slower (`ColumnConst::getUInt` -> `ColumnVector::getUInt`, `IColumn::getFloat64`). The code itself was not the cause: the same binary with the BTI property cleared (no enforcement) was as fast as one built without branch protection. - `randomString`: read a constant length once instead of per row. - `boundingRatio`: without GROUP BY, convert the arguments to Float64 in chunks with one type dispatch per chunk, and update a local copy of the state. Other argument types, `-If` and GROUP BY keep the per-row `add`. Graviton4, perf.py against master (both servers pinned to 16 cores), two runs: `random_string` -43%, `bounding_ratio` -15%. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The JIT code was generated without PAC and BTI and its pages were mapped without `PROT_BTI`, so an indirect branch could land anywhere in it - the one place where a query shapes the machine code. Generate it the same way as the binary (the branch protection function attributes, derived from the compiler's `__ARM_FEATURE_BTI_DEFAULT` and `__ARM_FEATURE_PAC_DEFAULT`) and map its pages with `PROT_BTI` when the kernel supports BTI. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Select the attribute values with the preprocessor instead of conditional expressions on the compile-time constant `__ARM_FEATURE_PAC_DEFAULT`. Checked on Graviton4: the JIT code mappings have the `bt` VmFlag and the compiled functions run (`CompiledFunctionExecute` 307), the JIT stateless tests pass apart from `00165_jit_aggregate_functions` (needs `test.hits`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
The generic paths of both functions (any element type without a specialized implementation, e.g. arrays of arrays) called `IColumn::insertFrom` per element, and `ColumnArray::insertFrom` makes one more virtual call into the nested column. Gather the elements with one `IColumn::index` call instead. Graviton4, best of 5, old vs new: `array_reverse_generic` 0.509 s -> 0.344 s, `complex_array_creation` 0.158 s -> 0.089 s. They were among the queries slower with BTI (an indirect branch into a guarded page costs more). Co-Authored-By: Claude Opus 5.5 <[email protected]>
`HashedDictionary::blockToAttributes` read every attribute value with a virtual `IColumn::get` into a `Field`. For a numeric attribute whose column has the attribute type (possibly `Nullable`), read the values and the null map directly. Other attributes keep the `Field` path. Graviton4, `SYSTEM RELOAD DICTIONARY` of a `SPARSE_HASHED` dictionary with 5M keys, best of 5: 0.514 s -> 0.338 s. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The tail of `MurmurHash3_x64_128` is a `switch` on the length that compiles to
a jump table: one indirect branch per hashed string, which needs a `bti j`
landing pad and is slower with BTI enforced.
Graviton4, `murmurHash3_64(materialize(''))` over 500M rows, best of 5:
0.204 s -> 0.168 s.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The generic path gathered the elements with `IColumn::index` and copied the gathered column into the result. Use it as the result's elements instead, including the null map of `Nullable`, which `index` handles as well. Graviton4, best of 5: arrays of arrays (`array_reverse_generic #7`) 0.340 s -> 0.319 s, arrays of tuples (#6) 0.076 s -> 0.073 s. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Skipping a field of an escaped text format went through two non-leaf functions per field, each saving and, with pointer authentication, signing and authenticating its return address: the `Escaped` case of `skipFieldByEscapingRule` only tail-calls `readEscapedStringInto`, and `readEscapedStringIntoImpl` called `find_first_symbols` even for a field of one or two bytes. Make the `Escaped` case a plain tail call, and find the end of a short field inline, calling the original loop only for longer fields and escape sequences. Graviton4, `SELECT WatchID` from 100000 rows of a `test.hits`-shaped file, best of 5: `CustomSeparatedWithNames` 0.221 s -> 0.167 s, `TabSeparatedWithNames` 0.193 s -> 0.166 s (`formats_columns_sampling #1` was 24% slower with PAC on Graviton4, whose QARMA5 authentication stalls `ret`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
`ODBCDriver2BlockOutputFormat` created, finalized and destroyed a `WriteBufferFromString` for every value, which costs more than formatting a number - and with pointer authentication every one of those small calls signs and authenticates its return address. Restart one `WriteBufferFromOwnString` per chunk instead. Graviton4, `INSERT INTO File(ODBCDriver2, '/dev/null') SELECT number FROM numbers(10000000)` with one thread, best of 5: 0.378 s -> 0.150 s (`number_formatting_formats #10` was 16% slower in the ARM performance comparison). The output is byte-identical. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…arch64 # Conflicts: # src/Processors/Formats/Impl/ODBCDriver2BlockOutputFormat.cpp
|
I see only improvements in the last run. Let's roll the dice one more time. |
|
Looks fine again. I will move all perf-related changes into a separate PR. |
…to branch-protection-aarch64
`deserializeBinaryBulkWithMultipleStreams` called both hint callbacks for every substream of every granule. Each call builds the stream name, hashes it and looks it up twice, but only `String` uses the hint. Compact parts read a granule at a time, so with small granules this was a large part of reading. Graviton4, PAC + BTI build, before -> after: `SELECT sum(x)` over the `parallel_index` table (`index_granularity = 4`, compact parts, one thread) 1.68-1.77 s -> 1.47-1.50 s, `parallel_index` #0 0.125-0.139 s -> 0.111-0.124 s, string and array columns of a compact part 0.632 s -> 0.511 s. Results are unchanged for compact and wide parts of `String`, `Nullable`, `Array`, `LowCardinality`, `FixedString`, `Map` and `Tuple` columns. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=130510&sha=13e24739511aa82b59d66aa4e4df27f37100ac16&name_0=PR&name_1=Performance%20Comparison%20(arm_release%2C%20master_head%2C%208%2F8) #130510 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…to branch-protection-aarch64
The merge with master brought ClickHouse/SimSIMD@7cd5fbe8bb08, which adds `branch-protection=none` to the ARMv8.3+ targets (the CPU probe and the bf16 kernels) to keep `retaa` out of them. `none` also drops `bti c`, and these kernels are called through the dispatch table, so with BTI enforced the first call of `simsimd_dot_bf16_neon` from `simsimd_capabilities` raised SIGILL as soon as a table with a vector similarity index was inserted into (`04648_query_condition_cache_vector_search` and the server death in the parallel job). Bump `contrib/SimSIMD` to ClickHouse/SimSIMD@27741d92376b (branch `bti-landing-pads`, on top of 7cd5fbe8bb08), which uses `branch-protection=bti` instead: compiled with `clang-23` from the CI image and the flags of `arm_binary`, `c/lib.c` has 0 `retaa` both with and without `-mbranch-protection=standard`, and every bf16 kernel starts with `bti c`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=130510&sha=latest&name_0=PR PR: #130510 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01DFFRj3kqWUsA1C3A8DapeT
…to branch-protection-aarch64
…to branch-protection-aarch64 # Conflicts: # CMakeLists.txt # contrib/SimSIMD
…to branch-protection-aarch64
…to branch-protection-aarch64
…to branch-protection-aarch64
AArch64 builds link statically against musl since #109239, and branch protection was enabled only with `GLIBC_COMPATIBILITY`, so it was off. musl needs no startup replacement: its startup objects and C sources are built with our flags. Its AArch64 assembly lacked landing pads and the BTI property, which disabled BTI for the whole executable; bump `contrib/musl` to ClickHouse/musl@87f66d7c8359, which adds them. A static executable has no dynamic loader to map it with `PROT_BTI`; the kernel does it. Related: #109239 Co-Authored-By: Claude Opus 5.5 <[email protected]>
libunwind recognizes a signal frame by the instructions `mov x8, #139; svc 0` at the return address, and the `bti c` before them in musl's `__restore_rt` made stack traces taken from a signal handler stop at the signal frame: `system.stack_trace` returned 2 frames per thread instead of 10 (`05175_system_stack_trace_file_offsets`, `05493_system_stack_trace_query_id_of_other_thread`). A handler returns to the restorer with `ret`, which BTI does not check, so it needs no landing pad. ClickHouse/musl@96df4887e869. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=130510&sha=8274d6e1574927d6ac780e8ecc62f9db1e8963a8&name_0=PR&name_1=Fast%20test #130510 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…arch64 # Conflicts: # contrib/musl
AI review of a later commit does not approve the PR, see the AI Review comment.
`__libc_csu_init` and `__libc_csu_fini` are referenced only from the inline assembly of `_start`, which LTO does not see, so with ThinLTO they could be internalized or dropped. musl's startup objects opt out of LTO for the same reason. #130510 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
With a nonzero mask argument, musl's `sigsetjmp` calls `setjmp` from assembly, so `siglongjmp` returned with `br x30` to an instruction without a landing pad and raised SIGILL with BTI enforced. ClickHouse takes this path in `StackTrace` (`sigsetjmp(..., 1)`) to recover from a fault while unwinding an interrupted thread. A static test program linked with the previous musl got SIGILL in the mask-saving `sigsetjmp`/`siglongjmp` round trip on Graviton4 and passes with ClickHouse/musl@2597a0bd1644. #130510 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
…arch64 # Conflicts: # CMakeLists.txt # contrib/corrosion-cmake/CMakeLists.txt
Master signs return addresses on AArch64 Linux with `pac-ret`, and this PR added a second block that appended `-mbranch-protection=standard` after it. Choose the level in one place instead: `standard` with the new `ENABLE_BTI` option, master's `pac-ret` otherwise, so turning BTI off keeps the return address signing. `ENABLE_BRANCH_PROTECTION` and `USE_BRANCH_PROTECTION` are renamed to `ENABLE_BTI` and `USE_BTI`: everything they control is about BTI. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
AArch64 builds now use branch target identification (BTI) in addition to pointer authentication of return addresses: an indirect call or jump can only land at the start of a function, which makes memory corruption bugs harder to turn into code execution.
Master signs return addresses on AArch64 (
-mbranch-protection=pac-ret), which protects the backward edge. This PR adds the forward edge: it builds with-mbranch-protection=standard(bti+pac-ret), so an indirect branch may only land on abtilanding pad, and jumping into the middle of a function (JOP, or a corrupted function pointer or vtable) faults instead of running. The landing pads are hint instructions, so the binary keeps running on CPUs without BTI. Graviton 3/4/5 support it; with glibc >= 2.32 on the host,ld.somaps the executable withPROT_BTI.The linker enables BTI only if every input object has the BTI property and silently drops it otherwise, so the link uses
-z bti-report=warning(fatal with--fatal-warnings). Objects that lacked it:Scrt1.o/crti.o/crtn.o, gcc 8.3crtbeginS.o/crtendS.o) andlibc_nonshared.a. They cannot simply be marked, becauseld.soand__libc_start_mainenter_startand__libc_csu_initthrough indirect branches. They are replaced bybase/glibc-compatibility/crt/aarch64/crt1.c, linked with-nostartfiles.syscall.sandlongjmp.singlibc-compatibility.blr(SYSTEM INSTRUMENTwould otherwise getSIGILL). A copy with landing pads is generated at configure time; upstream LLVM has the same gap. Patching also re-mprotects the pages withoutPROT_BTI, which would leave them unguarded, so a patchedxray_interface.cppaddsPROT_BTIwhen the kernel supports BTI.-Zbuild-std.setjmp/longjmp,clone,syscall_cp,memcpy/memset,crti/crtn, ...).contrib/muslis bumped to ClickHouse/musl@2597a0bd1644, which adds abti clanding pad at every function entry except the signal restorers (unwinders recognize them by their exact instructions, and they are only reached byret), abti jat the return site of thesetjmpcall insidesigsetjmp(siglongjmpreturns there withbr x30when the signal mask is saved, asStackTracedoes), and the property note. musl's own startup objects and C sources are built with our flags. A static executable has no dynamic loader; the kernel maps it withPROT_BTI.JIT code (
compile_expressionsetc.) is generated with the same branch protection as the binary, and its pages are mapped withPROT_BTI.Functions whose own
targetattribute sets the branch protection must keep the landing pad:branch-protection=nonedrops it, and a function called through a pointer then raisesSIGILL. That is what crashed the vector similarity index while SimSIMD's ARMv8.3+ kernels usednone; the SimSIMD version on master no longer does, and all its functions have landing pads.BTI is controlled by the new
ENABLE_BTIoption, in the same CMake block as master'spac-ret: with it the build uses-mbranch-protection=standard, without it master's-mbranch-protection=pac-ret, so turning BTI off keeps the return address signing. It is on by default for AArch64 Linux builds with musl, or with glibc andGLIBC_COMPATIBILITY, without sanitizers (the assembly of the sanitizer runtimes lacks the property).Checked on Graviton4 (
c8g.metal-48xl): the musl build (Release, without Rust) hasaarch64 feature: BTIwith no input lacking the property, its code segment has thebtVmFlag, and exceptions, threads, the query profiler, JIT expressions and aggregates, hedged remote queries, a vector similarity index,system.stack_traceandSYSTEM INSTRUMENTwork. Earlier, on Ubuntu glibc 2.39, a RelWithDebInfo glibc build with Rust:AArch64 feature: BTI, and no input lacks the property.btVmFlag insmaps, so BTI is enforced.SIGILLfor an indirect jump into the middle of a function.system.stack_trace, the query profiler with symbolization, JIT expressions and aggregates, remote and hedged queries (fibers), and all 15SYSTEM INSTRUMENTtests including03642_system_instrument_stress.SYSTEM INSTRUMENT ADDandREMOVE ALL, the patched pages keep thebtVmFlag.Cost on Graviton4:
bti(this PR)pac-ret(already on master)json_key_formatting(6-7%, fixed in #131078 by making theJSONEachRowdelimiters tail calls) andparallel_index#0 (3-5%, offset in #131078 by doing less work per granule when reading compact parts).The two
pac-retcases were found by the performance tests of this PR before master enabledpac-ret; clearing only the BTI property of the binary recovered neither of them.Workflow [PR]
Sync PR [sync-upstream/pr/130510]