llvm-project/llvm/lib/Transforms/Utils
Sami Tolvanen cff5bef948 KCFI sanitizer
The KCFI sanitizer, enabled with `-fsanitize=kcfi`, implements a
forward-edge control flow integrity scheme for indirect calls. It
uses a !kcfi_type metadata node to attach a type identifier for each
function and injects verification code before indirect calls.

Unlike the current CFI schemes implemented in LLVM, KCFI does not
require LTO, does not alter function references to point to a jump
table, and never breaks function address equality. KCFI is intended
to be used in low-level code, such as operating system kernels,
where the existing schemes can cause undue complications because
of the aforementioned properties. However, unlike the existing
schemes, KCFI is limited to validating only function pointers and is
not compatible with executable-only memory.

KCFI does not provide runtime support, but always traps when a
type mismatch is encountered. Users of the scheme are expected
to handle the trap. With `-fsanitize=kcfi`, Clang emits a `kcfi`
operand bundle to indirect calls, and LLVM lowers this to a
known architecture-specific sequence of instructions for each
callsite to make runtime patching easier for users who require this
functionality.

A KCFI type identifier is a 32-bit constant produced by taking the
lower half of xxHash64 from a C++ mangled typename. If a program
contains indirect calls to assembly functions, they must be
manually annotated with the expected type identifiers to prevent
errors. To make this easier, Clang generates a weak SHN_ABS
`__kcfi_typeid_<function>` symbol for each address-taken function
declaration, which can be used to annotate functions in assembly
as long as at least one C translation unit linked into the program
takes the function address. For example on AArch64, we might have
the following code:

```
.c:
  int f(void);
  int (*p)(void) = f;
  p();

.s:
  .4byte __kcfi_typeid_f
  .global f
  f:
    ...
```

Note that X86 uses a different preamble format for compatibility
with Linux kernel tooling. See the comments in
`X86AsmPrinter::emitKCFITypeId` for details.

As users of KCFI may need to locate trap locations for binary
validation and error handling, LLVM can additionally emit the
locations of traps to a `.kcfi_traps` section.

Similarly to other sanitizers, KCFI checking can be disabled for a
function with a `no_sanitize("kcfi")` function attribute.

Relands 67504c9549 with a fix for
32-bit builds.

Reviewed By: nickdesaulniers, kees, joaomoreira, MaskRay

Differential Revision: https://reviews.llvm.org/D119296
2022-08-24 22:41:38 +00:00
..
AMDGPUEmitPrintf.cpp [AMDGPU] replace hostcall module flag with function attribute 2022-02-11 22:51:56 +05:30
ASanStackFrameLayout.cpp Cleanup includes: TransformsUtils 2022-03-01 21:00:07 +01:00
AddDiscriminators.cpp [llvm] Don't use Optional::getValue (NFC) 2022-06-20 22:45:45 -07:00
AssumeBundleBuilder.cpp Cleanup includes: TransformsUtils 2022-03-01 21:00:07 +01:00
BasicBlockUtils.cpp [llvm] Qualify auto (NFC) 2022-08-07 23:55:27 -07:00
BreakCriticalEdges.cpp [BasicBlockUtils] Allow critical edge splitting with callbr terminators 2022-07-08 09:20:44 +02:00
BuildLibCalls.cpp [InstCombine] Remove assumptions about int having 32 bits 2022-08-16 15:35:08 -06:00
BypassSlowDivision.cpp
CMakeLists.txt [misexpect] Re-implement MisExpect Diagnostics 2022-04-19 21:23:48 +00:00
CallGraphUpdater.cpp Cleanup includes: TransformsUtils 2022-03-01 21:00:07 +01:00
CallPromotionUtils.cpp [WPD] Extend checking mode to support fallback to indirect call 2022-03-14 10:16:28 -07:00
CanonicalizeAliases.cpp [llvm] Fix comment typos (NFC) 2022-08-07 00:16:14 -07:00
CanonicalizeFreezeInLoops.cpp Cleanup includes: TransformsUtils 2022-03-01 21:00:07 +01:00
CloneFunction.cpp [llvm] Qualify auto (NFC) 2022-08-07 23:55:27 -07:00
CloneModule.cpp Cleanup includes: TransformsUtils 2022-03-01 21:00:07 +01:00
CodeExtractor.cpp [llvm] Qualify auto in range-based for loops (NFC) 2022-08-13 12:55:42 -07:00
CodeLayout.cpp extending code layout alg 2022-08-24 09:40:25 -07:00
CodeMoverUtils.cpp [llvm] Qualify auto in range-based for loops (NFC) 2022-08-13 12:55:42 -07:00
CtorUtils.cpp [GlobalOpt] Enable optimization of constructors with different priorities 2022-05-13 22:19:29 +00:00
Debugify.cpp [Debugify] Port verify-debuginfo-preserve to NewPM 2022-07-06 17:07:20 +02:00
DemoteRegToStack.cpp Cleanup includes: TransformsUtils 2022-03-01 21:00:07 +01:00
EntryExitInstrumenter.cpp [LegacyPM] Remove {,PostInline}EntryExitInstrumenterPass 2022-07-23 15:30:15 -07:00
EscapeEnumerator.cpp
Evaluator.cpp [GlobalOpt] Enable evaluation of atomic loads 2022-07-21 21:36:11 +00:00
FixIrreducible.cpp [llvm] Qualify auto (NFC) 2022-08-07 23:55:27 -07:00
FlattenCFG.cpp [FlattenCFG] avoid crash on malformed code 2022-08-16 15:11:00 -04:00
FunctionComparator.cpp [llvm] Qualify auto (NFC) 2022-08-07 23:55:27 -07:00
FunctionImportUtils.cpp [Transforms] Qualify auto in range-based for loops (NFC) 2022-08-14 12:51:58 -07:00
GlobalStatus.cpp [GlobalOpt] Perform store->dominated load forwarding for stored once globals 2022-06-24 09:09:26 -07:00
GuardUtils.cpp
HelloWorld.cpp
InjectTLIMappings.cpp Cleanup includes: TransformsUtils 2022-03-01 21:00:07 +01:00
InlineFunction.cpp KCFI sanitizer 2022-08-24 22:41:38 +00:00
InstructionNamer.cpp
IntegerDivision.cpp Cleanup includes: TransformsUtils 2022-03-01 21:00:07 +01:00
LCSSA.cpp [Transforms] Qualify auto in range-based for loops (NFC) 2022-08-14 12:51:58 -07:00
LibCallsShrinkWrap.cpp Cleanup includes: TransformsUtils 2022-03-01 21:00:07 +01:00
Local.cpp [Transforms] Qualify auto in range-based for loops (NFC) 2022-08-14 12:51:58 -07:00
LoopPeel.cpp Remove redundant initialization of Optional (NFC) 2022-08-20 21:18:28 -07:00
LoopRotationUtils.cpp [llvm] LLVM_FALLTHROUGH => [[fallthrough]]. NFC 2022-08-08 11:24:15 -07:00
LoopSimplify.cpp [IR] Don't treat callbr as indirect terminator 2022-07-18 09:32:08 +02:00
LoopUnroll.cpp [llvm] Don't use Optional::getValue (NFC) 2022-06-20 22:45:45 -07:00
LoopUnrollAndJam.cpp [llvm] Don't use Optional::getValue (NFC) 2022-06-20 22:45:45 -07:00
LoopUnrollRuntime.cpp [Transforms] Fix comment typos (NFC) 2022-08-07 23:55:24 -07:00
LoopUtils.cpp [LoopUtils] Remove unused Loop arg from addDiffRuntimeChecks (NFC). 2022-08-23 10:15:28 +01:00
LoopVersioning.cpp [PSE] Remove assumption that top level predicate is union from public interface [NFC*] 2022-02-10 16:14:52 -08:00
LowerAtomic.cpp [NFC] Switch a few uses of undef to poison as placeholders for unreachable code 2022-07-23 21:50:11 +01:00
LowerGlobalDtors.cpp Reland "Lower `@llvm.global_dtors` using `__cxa_atexit` on MachO" 2022-03-23 18:36:55 -07:00
LowerInvoke.cpp Cleanup includes: TransformsUtils 2022-03-01 21:00:07 +01:00
LowerMemIntrinsics.cpp [Transforms] Fix comment typos (NFC) 2022-08-07 23:55:24 -07:00
LowerSwitch.cpp LowerSwitch: Avoid inserting NewDefault block 2022-04-14 13:30:56 +08:00
MatrixUtils.cpp [Matrix] Refactor tiled loops in a struct. NFC 2022-07-26 11:02:22 -07:00
Mem2Reg.cpp
MemoryOpRemark.cpp
MemoryTaggingSupport.cpp [NFC] Use AllocaInst's getAddressSpace helper 2022-08-01 10:11:16 +01:00
MetaRenamer.cpp
MisExpect.cpp [llvm] Remove std::clamp equivalent in `Transforms/Utils/MisExpect.cpp` 2022-08-18 15:11:25 -06:00
ModuleUtils.cpp [llvm] Use value instead of getValue (NFC) 2022-07-13 23:11:56 -07:00
NameAnonGlobals.cpp [LegacyPM] Remove NameAnonGlobalLegacyPass 2022-07-17 14:38:29 -07:00
PredicateInfo.cpp [llvm] Qualify auto (NFC) 2022-08-07 23:55:27 -07:00
PromoteMemoryToRegister.cpp [Mem2Reg] Consistently preserve nonnull assume for uninit load 2022-07-12 12:53:08 +02:00
RelLookupTableConverter.cpp [RelLookupTableConverter] Bail on invalid pointer size (x32) 2022-08-09 09:36:39 +02:00
SCCPSolver.cpp [SCCP] Make check for unknown/undef in unary op handling more explicit (NFCI) 2022-07-14 10:56:11 +02:00
SSAUpdater.cpp [NFC] Switch a few uses of undef to poison as placeholders for unreachable code 2022-07-30 13:55:56 +01:00
SSAUpdaterBulk.cpp
SampleProfileInference.cpp [Transforms] Qualify auto in range-based for loops (NFC) 2022-08-14 12:51:58 -07:00
SampleProfileLoaderBaseUtil.cpp Remove unneeded cl::ZeroOrMore for cl::opt/cl::list options 2022-06-05 00:31:44 -07:00
SanitizerStats.cpp Cleanup includes: TransformsUtils 2022-03-01 21:00:07 +01:00
ScalarEvolutionExpander.cpp [llvm] Qualify auto in range-based for loops (NFC) 2022-08-13 12:55:42 -07:00
SimplifyCFG.cpp [CostModel] Replace getUserCost with getInstructionCost 2022-08-18 11:55:23 +01:00
SimplifyIndVar.cpp [SCEV] Prove condition invariance via context, try 2 2022-08-22 14:31:19 +07:00
SimplifyLibCalls.cpp [InstCombine] convert second std::min argument to same type as first 2022-08-16 17:34:33 -06:00
SizeOpts.cpp [llvm] Remove unneeded cl::ZeroOrMore for cl::opt options. NFC 2022-06-03 21:59:05 -07:00
SplitModule.cpp [llvm] Qualify auto in range-based for loops (NFC) 2022-08-13 12:55:42 -07:00
StripGCRelocates.cpp [NFC] Rename Instrinsic to Intrinsic 2022-04-25 18:13:23 +01:00
StripNonLineTableDebugInfo.cpp
SymbolRewriter.cpp Cleanup includes: TransformsUtils 2022-03-01 21:00:07 +01:00
UnifyFunctionExitNodes.cpp
UnifyLoopExits.cpp Revert "[UnifyLoopExits] Reduce number of guard blocks" 2022-07-14 10:33:52 -05:00
Utils.cpp [LegacyPM] Remove NameAnonGlobalLegacyPass 2022-07-17 14:38:29 -07:00
VNCoercion.cpp [VNCoercion] Separate constant/non-constant mem intrinsic implementations (NFCI) 2022-06-30 15:26:06 +02:00
ValueMapper.cpp