Issue 237978
Summary [KCFI][clang] Segfault instead of "not compatible" error for -fsanitize=kcfi -fpatchable-function-entry=N,M (M>0) on targets using the generic KCFI pass
Labels
Assignees
Reporter zjy-dev
    On targets without backend KCFI lowering (e.g. LoongArch), clang runs the generic `KCFIPass`. That
pass rejects `patchable-function-prefix` with an error diagnostic, but reports it as a
`DiagnosticInfo(DK_Linker, ...)`. Clang's `BackendConsumer` handles `DK_Linker` by dereferencing
`CurLinkModule`, which is null outside of `-mlink-bitcode-file` linking, so clang segfaults
instead of printing the error. `opt` prints the intended error, and
`llvm/test/Transforms/KCFI/kcfi-patchable-function-prefix.ll` checks for it.

```c
void call(void (*fp)(void)) { fp(); }
```

Compile flag: `clang --target=loongarch64-linux-gnu -O2 -fsanitize=kcfi -fpatchable-function-entry=8,2 -S`
(also at -O0; x86_64, aarch64 and riscv64 lower KCFI in the backend and are not affected)

```
Stack dump:
4.	Running pass "kcfi" on function "call"
 #5 clang::BackendConsumer::DiagnosticHandlerImpl(llvm::DiagnosticInfo const&) (.cold.0)
 #6 clang::ClangDiagnosticHandler::handleDiagnostics(llvm::DiagnosticInfo const&)
 #7 llvm::LLVMContext::diagnose(llvm::DiagnosticInfo const&)
 #8 llvm::KCFIPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&)
exit=139 # SIGSEGV

$ opt -passes=kcfi trigger.ll                 # same IR, from -disable-llvm-passes
error: -fpatchable-function-entry=N,M, where M>0 is not compatible with -fsanitize=kcfi on this target
```

Godbolt clang trunk (2026-11-01) also crashes (`clang::BackendDiagnosticConsumer::handleDiagnostics`).

**Root cause.** `llvm/lib/Transforms/Instrumentation/KCFI.cpp:37-44`: `DiagnosticInfoKCFI` derives
from `DiagnosticInfo(DK_Linker, ...)`. `clang/lib/CodeGen/CodeGenAction.cpp:939-941` handles `DK_Linker`
with `assert(CurLinkModule && ...); Diags.Report(DiagID) << CurLinkModule->getModuleIdentifier()`,
so the null `CurLinkModule` is dereferenced in release builds.

**Suggested fix.** Use a non-linker diagnostic kind in KCFIPass (e.g. `DiagnosticInfoGeneric` /
`DK_Generic`, or a dedicated kind). Clang should also not crash on a `DK_Linker` diagnostic when
`CurLinkModule` is null. Optionally reject the flag combination in the driver for these targets.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to