llvm-project/llvm/lib/CodeGen/GlobalISel
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
..
CMakeLists.txt [nfc][codegen] Move RegisterBank[Info].cpp under CodeGen 2022-02-15 11:27:15 -08:00
CSEInfo.cpp [CodeGen] Qualify auto variables in for loops (NFC) 2022-07-17 01:33:28 -07:00
CSEMIRBuilder.cpp [GlobalISel] Change ConstantFoldVectorBinop to return vector of APInt 2022-05-13 09:33:07 +01:00
CallLowering.cpp KCFI sanitizer 2022-08-24 22:41:38 +00:00
Combiner.cpp [GlobalISel][DebugInfo] salvageDebugInfo analogue for gMIR 2022-08-01 11:14:53 +02:00
CombinerHelper.cpp [GlobalISel] Fix miscompile of G_UREM + G_UDIV due to not checking for equality 2022-07-25 16:03:05 -07:00
GISelChangeObserver.cpp
GISelKnownBits.cpp [llvm] LLVM_FALLTHROUGH => [[fallthrough]]. NFC 2022-08-08 11:24:15 -07:00
GlobalISel.cpp Cleanup includes: DebugInfo & CodeGen 2022-03-12 17:26:40 +01:00
IRTranslator.cpp Untangle the mess which is MachineBasicBlock::hasAddressTaken(). 2022-08-16 16:15:44 -07:00
InlineAsmLowering.cpp [IR] Don't use blockaddresses as callbr arguments 2022-07-15 10:18:17 +02:00
InstructionSelect.cpp Cleanup includes: DebugInfo & CodeGen 2022-03-12 17:26:40 +01:00
InstructionSelector.cpp [GISel] Don't fold convergent instruction across CFG 2022-07-01 10:24:24 -07:00
LegacyLegalizerInfo.cpp [llvm] LLVM_FALLTHROUGH => [[fallthrough]]. NFC 2022-08-08 11:24:15 -07:00
LegalityPredicates.cpp GlobalISel: Add memSizeNotByteSizePow2 legality helper 2022-04-11 19:43:37 -04:00
LegalizeMutations.cpp GlobalISel: Add LegalizeMutations to help use More/FewerElements 2022-04-19 21:04:32 -04:00
Legalizer.cpp Cleanup include: codegen second round 2022-03-23 13:54:00 +01:00
LegalizerHelper.cpp Remove redundant initialization of Optional (NFC) 2022-08-20 21:18:28 -07:00
LegalizerInfo.cpp [llvm] LLVM_FALLTHROUGH => [[fallthrough]]. NFC 2022-08-08 11:24:15 -07:00
LoadStoreOpt.cpp [CodeGen] Qualify auto variables in for loops (NFC) 2022-07-17 01:33:28 -07:00
Localizer.cpp Cleanup includes: DebugInfo & CodeGen 2022-03-12 17:26:40 +01:00
LostDebugLocObserver.cpp
MachineIRBuilder.cpp [GlobalISel] Handle nullptr constants in dbg.value 2022-07-28 14:58:14 -07:00
RegBankSelect.cpp [globalisel] Select register bank for DBG_VALUE 2022-08-09 13:11:51 +08:00
Utils.cpp Remove redundant initialization of Optional (NFC) 2022-08-20 21:18:28 -07:00