Repository navigation
Fix the Clang CFI build with all checks enabled - #132527
Merged
Merged
Conversation
The submodule moves from upstream `v36.1` to the `ClickHouse/v36.2` branch of https://github.com/ClickHouse/protobuf: upstream `v36.2` plus one commit that stops `FlatAllocation::End` from casting a one-past-the-end pointer to `U *`. Clang CFI (`cfi-unrelated-cast`) rejects that cast, because the memory is not an object of type `U`; `Begin` already avoids the same cast for that reason. Upstream `main` still has it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`clock_gettime` and `sched_getcpu` called the vDSO through function pointers that stayed writable for the whole process, and that replaced themselves on first use (musl's scheme). Overwriting one would redirect every call, and Clang CFI cannot check them, because the vDSO is outside the binary. As glibc does (it keeps them in RELRO), resolve both once, in a constructor in `vdso.c`, and store them on a page of their own (64 KiB, the largest page size) that is then made read-only; if that fails, the process traps. Until the constructor has run, both use the system call. The calls into the vDSO go through one small helper each, which is exempt from `cfi-icall`. The time32 wrapper is gone: none of our architectures uses it. musl builds keep musl's own implementation, which is built without CFI for now: add a TODO to build musl with CFI too, with the same treatment of these pointers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Poco's `*_API` macros gave its classes `visibility("default")`. That only matters for shared
libraries, and we link Poco statically into one executable that exports no Poco symbols.
Clang checks virtual calls and casts (and devirtualizes them) only for classes with hidden
visibility, so these classes would stay unchecked under CFI. Define the macros empty.
`writeString` in `JSONString.cpp` wrote through a pointer to the member function
`&std::ostream::write`, which `cfi-mfcall` rejects. Take a callable instead; the two callers
pass lambdas that call `write` and `append` directly.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
`ENABLE_CFI` used only `cfi-vcall` and `cfi-derived-cast`, with `-fvisibility=default`. Clang checks virtual calls and casts only for classes with hidden LTO visibility, and only those can be devirtualized under CFI, so most classes were neither checked nor devirtualized. Now: - `-fsanitize=cfi` (all checks: also `cfi-icall`, `cfi-nvcall`, `cfi-mfcall` and `cfi-unrelated-cast`) for C and C++, in trap mode. - `-fvisibility=hidden` for C++, and the visibility annotations of libc++ and libc++abi disabled, so that their classes are checked too. C keeps the default visibility: it has no classes, and the libc replacements it defines must stay visible to the shared libc. - `cmake/cfi-ignorelist.txt`, only for build tools that are not linked into `clickhouse` (`llvm-tblgen` casts a matcher to a class it is not). - libunwind is built with CFI too: `-fno-sanitize=all` (to keep other sanitizers out of the unwinder) also removed the CFI types of the callbacks it passes. - OpenSSL checks its own indirect calls with all pointer types treated alike (`-fsanitize-cfi-icall-generalize-pointers`), until its callback casts are fixed. - `ENABLE_CFI_DIAGNOSTICS` reports violations through our UBSan runtime and continues, so that one test run finds all of them. It is for finding violations only, and cannot be combined with XRay (both runtimes bundle `sanitizer_common`). In that mode `phdr_cache.cpp` is built without `cfi-icall`, because the UBSan runtime lists modules through `dl_iterate_phdr` with a callback that has no CFI type. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Our `dl_iterate_phdr` override, which caches the program headers so that stack unwinding needs no lock, reached libc's implementation through a pointer from `dlsym(RTLD_NEXT)`. Clang CFI rejects calls through such a pointer, because the target is outside the binary. Bind a separate name, `libc_dl_iterate_phdr`, to the versioned libc symbol with `.symver` instead: the linker resolves it to libc, and the call is a plain one. The override is now hidden. Exported, an unversioned definition in the executable would also satisfy the `dl_iterate_phdr@GLIBC_*` reference, so the call to libc would come back to it and loop. Code in the binary still uses the override; libc calls it internally through its own alias, and other shared objects get libc's directly. The per-architecture table of the oldest glibc symbol versions moves from `ThreadFuzzer.cpp` to `base/glibc_symver.h`, now shared (and with `loongarch64`, which the PHDR cache supports). The `pthread_mutex_lock` and `pthread_mutex_unlock` wrappers of ThreadFuzzer are exported explicitly, as libc must still see them under hidden visibility. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The executable replaces some libc functions, and libc must call these replacements too. An executable exports such a symbol only if its visibility is default, so with `-fvisibility=hidden` (used by `ENABLE_CFI`) they would silently stop replacing libc's: - `malloc`, `free` and the rest of `malloc.cpp`: libc would allocate with its own allocator and we would free with jemalloc. This was the "SIGSEGV before main" that kept `ENABLE_CFI` on `-fvisibility=default`. - The `dlopen` stubs of `clickhouse` and `clickhouse-keeper`: libc's real `dlopen` would work. - `memmem`. Mark them with default visibility at their definitions. Builds without `ENABLE_CFI` already use default visibility, so their exported symbols do not change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`ShellCommand` and the client's editor launcher called `vfork` through a pointer from `dlsym`, to keep the lazy binding of `vfork` out of the call itself. That was a precaution from an article about Solaris, from the first version of `ShellCommand` (2015). On Linux it no longer has a purpose: glibc builds link with `-z now`, which binds every symbol at startup, and musl builds are static. It is also a call through a writable pointer to a target outside the binary, which Clang CFI (`cfi-icall`) rejects. Keep it on macOS only: x86_64 macOS builds bind library symbols lazily, and macOS builds do not use CFI. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`clickhouse-local` resolved the relative path against the working directory, so the test left `uuid_<database>.parquet` in the repository root in CI. Also remove the file at the end. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… `CLICKHOUSE_TMP` The test wrote `queries_02352` to the working directory and removed it with a spawned `rm` that nothing waited for, so the file was left in the repository root in CI. Put it in `CLICKHOUSE_TMP`, and create and remove it with `exec`, which waits: the spawned `echo` could also race with the client reading the file. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…LICKHOUSE_TMP` The test wrote the private key to `ssh_key` in the working directory and never removed it, so it was left in the repository root in CI. Put it in `CLICKHOUSE_TMP` and remove it on exit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Its config gives the local disk a relative path, which `clickhouse-disks` resolves against the working directory, so the test left `disks/03566` in the repository root in CI. Run the test from `CLICKHOUSE_TMP` and remove the directory at the end. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…CLICKHOUSE_TMP` `clickhouse-local` resolves the relative paths of `file` against the working directory, not `user_files`, so the files of its three cases were left in the repository root in CI: the test only removed them from `user_files`. The two cases that go through the server keep using `user_files`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…alues` in a temporary directory `clickhouse-local` caches the schemas given with `format_schema_source` in `__cache__` under its working directory, so the test left them in the repository root in CI. Run it from a directory under `CLICKHOUSE_TMP` and remove that at the end. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ry_multirow` in a temporary directory `clickhouse-local` caches the schemas given with `format_schema_source` in `__cache__` under its working directory, so the test left them in the repository root in CI. Run it from a directory under `CLICKHOUSE_TMP` and remove that at the end. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n a temporary directory `clickhouse-local` caches the schemas given with `format_schema_source` in `__cache__` under its working directory, so the test left them in the repository root in CI. Run it from a directory under `CLICKHOUSE_TMP` and remove that at the end. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ain` in a temporary directory `clickhouse-local` caches the schemas given with `format_schema_source` in `__cache__` under its working directory, so the test left them in the repository root in CI. Run it from a directory under `CLICKHOUSE_TMP` and remove that at the end. Co-Authored-By: Claude Opus 5.5 <[email protected]>
JIT-compiled functions are generated at run time, so they have no Clang CFI type, and the check of `cfi-icall` cannot pass for them. Exempt `callJITFunction`, which every call into JIT code goes through, as it already is from UBSan's `-fsanitize=function` for the same reason. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Calls into JIT code are exempt from Clang CFI, which has no type for generated code, so a corrupted function pointer could send `callJITFunction` anywhere. Do the check CFI would do instead: `callJITFunction` traps unless its target is the entry of a live JIT-compiled function, called with the type it was handed out as. - `CHJIT::getFunction<F>` replaces the casts of the symbols of compiled modules: it registers the address with type `F`, and throws for a missing symbol, where `operator[]` returned null. - `compileModule` checks that every compiled function lies in the code of its module. - `~JITModuleMemoryManager` unregisters the functions in its code before freeing it, so the functions of deleted modules, including those freed with their JIT instance (for example by `SYSTEM DROP COMPILED EXPRESSION CACHE`), cannot be called any more. A call through a pointer moved into the middle of a function, through a pointer to other code, or through a pointer of another type traps. The check is a lookup per call of compiled code, which happens per block, not per row. Co-Authored-By: Claude Opus 5.5 <[email protected]>
With it, jemalloc's background threads find `pthread_create` with `dlsym(RTLD_NEXT)`, and call it through the returned pointer. Clang CFI (`ENABLE_CFI`) rejects that call, because the target is outside the binary. The lookup only serves applications that wrap `pthread_create`, which we don't, and lazy locking (which needs it) is off. Without it, jemalloc calls `pthread_create` directly. The `jemalloc_internal_defs.h.in` headers are `configure` output kept by hand, so document the edit in the README, with the command that reapplies it, like the other edits there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`jemallocDeallocationTracker`, registered as `experimental.hooks.prof_sample_free`, took the size as `unsigned`, while jemalloc calls the hook as `void (const void *, size_t)` (`prof_sample_free_hook_t`). Calling a function through a pointer of another type is undefined behavior; on x86-64 and aarch64 it truncated sizes to 32 bits, and Clang CFI (`cfi-icall`) rejects the call. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… Lake writer `exportRecordBatch` fills delta-kernel's `ffi::FFI_ArrowSchema` and `ffi::FFI_ArrowArray` with Arrow C++'s exporter, so their `release` callbacks are Arrow C++ functions that take `ArrowSchema *` and `ArrowArray *`. The cleanup called them through the `ffi::` structs, whose `release` has another function type, which Clang CFI (`cfi-icall`) rejects. Call them through Arrow C++'s own structs, which have the same layout. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…functions The kernels were resolved as `simsimd_metric_dense_punned_t`, which takes `const void *`, and called through it, while each kernel takes pointers to its own scalar type (for example `simsimd_i8_t`). Calling a function through a pointer of another type is undefined behavior, and Clang CFI (`cfi-icall`) rejects it. Resolve them as a pointer with the scalar type of the calculation type, and cast the data pointers at the call. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The dynamic dispatchers cached the kernels they find in a punned function pointer type, which takes `void const *`, and called them through it, while the kernels take pointers to their own scalar type. That is undefined behavior, and Clang CFI (`cfi-icall`) rejects the calls. They now cache them with the exact type of the dispatcher, which matches the kernels. Co-Authored-By: Claude Opus 5.5 <[email protected]>
usearch called SimSIMD kernels through `simsimd_metric_dense_punned_t`, which takes `void const *`, while the kernels take pointers to their own scalar type, and reached that call through a pointer to member cast to drop `noexcept`. Both are calls through a pointer of another type, which is undefined behavior and which Clang CFI (`cfi-icall`, `cfi-mfcall`) rejects. Each element type now has its own instantiation, chosen once when the metric is configured, that calls the kernel through its exact type. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…face helpers Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…it first Co-Authored-By: Claude Opus 5.5 <[email protected]>
The standalone UBSan runtime, also linked by `ENABLE_CFI_DIAGNOSTICS`, intercepts `sigaction` and `signal`, but in a static musl binary its `REAL` pointers to the originals were null, so every `sigaction` call failed and the server could not set its signal handlers. Bind them to musl's own definitions at link time. The renamed musl archive used by the other sanitizers cannot be made from the LTO bitcode musl is built to with CFI. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Nov 19, 2026
Keep `llvm/IR/Constants.h` out of `CHJIT.h`: its `CR1` parameters clash with the `CR1` macro of `termios.h` on ppc64le. Bind `dl_iterate_phdr@GLIBC_2.2.4` on s390x, where the symbol is newer than the oldest glibc version. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Algunenano
enabled auto-merge
November 20, 2026 10:10
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=4 K_base=19 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
Merged
via the queue into
ClickHouse:master
with commit Nov 20, 2026
a323e86
383 of 384 checks passed
This was referenced Nov 20, 2026
Merged
Open
alexey-milovidov
added a commit
that referenced
this pull request
Nov 21, 2026
Master builds `contrib/jemalloc` without `JEMALLOC_HAVE_DLSYM` (#132527): the background threads no longer find `pthread_create` with `dlsym(RTLD_NEXT)` and call it through the returned pointer, which Clang CFI rejects, but call `pthread_create` directly. Do the same in the C++ allocator with the new `config::have_dlsym`. The lookup is kept with `config::lazy_lock` (FreeBSD), where `pthread_create` is interposed and a direct call would recurse; jemalloc without `JEMALLOC_HAVE_DLSYM` aborts at boot there. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This was referenced Nov 23, 2026
PedroTadim
pushed a commit
to Dolaxom/ClickHouse
that referenced
this pull request
Nov 23, 2026
Since ClickHouse#132527 the CFI build enables every Clang CFI check, including cfi-icall, and WeeklyCFI uses the diagnostics mode. With `remap_executable = 1` the server then never finishes starting, and all three tests of `test_remap_executable` fail in `Integration tests (amd_cfi, 1/4)`: src/Common/remapExecutable.cpp:167:5: runtime error: control flow integrity check for type 'void (void *, unsigned long, void *, void *, void *)' failed during indirect function call remapExecutable copies the text mapping into an anonymous scratch mmap and runs from that copy while it re-maps the original: - remapToHugeStep1 jumps to remapToHugeStep2 + offset, inside the copy. The target is not a CFI jump-table entry, so the check fails (the report above), and the diagnostics runtime continues. - remapToHugeStep2 runs from the copy. Its checks load the jump-table base RIP-relatively, so they compute the copy's table: the syscall calls pass by accident (their target is in the copy too), but step3 is called at its original address and fails. The failure handler is then reached by a relative call from the copy, i.e. a shifted copy of the UBSan runtime with shifted data references: no second report, and the single remaining thread ends up asleep with the handled signals blocked. In trap mode the first check is a ud1 instead. Exempt the two functions from cfi-icall with no_sanitize("cfi-icall"), the attribute ClickHouse#132527 uses for the vDSO and JIT calls that CFI cannot type. Every other check stays on, and builds without CFI compile the same code. Measured on a local amd_cfi build (BuildTypes.AMD_CFI flags): - unmodified: 6/6 starts report at line 167 and never answer SELECT 1; test_remap_executable 3/3 ERROR at setup; - only remapToHugeStep1 exempted: 6/6 starts still hang, without a report, so both attributes are needed; - fixed: 3/3 starts remap (748.55 MiB) and answer SELECT 1, no CFI report; test_remap_executable 3/3 passed, node stderr clean. Disassembly of the fixed binary: no CFI check left in step1 and step2, and step2 has no RIP-relative reference left. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a build PR: it makes
ENABLE_CFIbuild ClickHouse with every Clang CFI check (cfi-icall,cfi-vcall,cfi-nvcall,cfi-derived-cast,cfi-unrelated-cast,cfi-mfcall) and hidden visibility, for glibc release builds (withGLIBC_COMPATIBILITY) and musl builds, and fixes the violations it finds by patching the code rather than excluding it. CFI stays disabled by default.It is pushed now, before CFI can be enabled by default, because most of the fixes are submodule updates and keeping them on a branch means constant conflicts with master. The Rust contribs that still fail CFI (the Arrow C data interface in arrow-rs, delta-kernel, wasmtime, ipnet), enabling CFI by default and the performance evaluation will come in later PRs.
What changes:
callJITFunctiontraps unless it matches.ENABLE_CFIis rejected withENABLE_OPENSSL_DYNAMIC, and on Linux withoutGLIBC_COMPATIBILITYor musl.ENABLE_CFI_DIAGNOSTICS, so a violation is reported with its location, types and stack trace and the server continues; integration tests now fail on such a UBSan report.CLICKHOUSE_TMP.Related: #131685
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix the build with Clang Control Flow Integrity (
ENABLE_CFI) with all its checks enabled, for glibc and musl. It stays disabled by default.Workflow [PR]
Sync PR [sync-upstream/pr/132527]
Version info
26.12.1.60-master(included in26.12and later)