1//===-- KCFI.cpp - Generic KCFI operand bundle lowering ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass emits generic KCFI indirect call checks for targets that don't
10// support lowering KCFI operand bundles in the back-end.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Transforms/Instrumentation/KCFI.h"
15#include "llvm/ADT/Statistic.h"
16#include "llvm/IR/Constants.h"
17#include "llvm/IR/DiagnosticInfo.h"
18#include "llvm/IR/DiagnosticPrinter.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/GlobalObject.h"
21#include "llvm/IR/IRBuilder.h"
22#include "llvm/IR/InstIterator.h"
23#include "llvm/IR/Instructions.h"
24#include "llvm/IR/Intrinsics.h"
25#include "llvm/IR/MDBuilder.h"
26#include "llvm/IR/Module.h"
27#include "llvm/Target/TargetMachine.h"
28#include "llvm/Transforms/Utils/BasicBlockUtils.h"
29
30using namespace llvm;
31
32#define DEBUG_TYPE "kcfi"
33
34STATISTIC(NumKCFIChecks, "Number of kcfi operands transformed into checks");
35
36namespace {
37class DiagnosticInfoKCFI : public DiagnosticInfo {
38 const Twine &Msg;
39
40public:
41 DiagnosticInfoKCFI(const Twine &DiagMsg,
42 DiagnosticSeverity Severity = DS_Error)
43 : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
44 void print(DiagnosticPrinter &DP) const override { DP << Msg; }
45};
46} // namespace
47
48PreservedAnalyses KCFIPass::run(Function &F, FunctionAnalysisManager &AM) {
49 Module &M = *F.getParent();
50 if (!M.getModuleFlag(Key: "kcfi"))
51 return PreservedAnalyses::all();
52
53 // Find call instructions with KCFI operand bundles.
54 SmallVector<CallInst *> KCFICalls;
55 for (Instruction &I : instructions(F)) {
56 if (auto *CI = dyn_cast<CallInst>(Val: &I))
57 if (CI->getOperandBundle(ID: LLVMContext::OB_kcfi))
58 KCFICalls.push_back(Elt: CI);
59 }
60
61 if (KCFICalls.empty())
62 return PreservedAnalyses::all();
63
64 LLVMContext &Ctx = M.getContext();
65 // patchable-function-prefix emits nops between the KCFI type identifier
66 // and the function start. As we don't know the size of the emitted nops,
67 // don't allow this attribute with generic lowering.
68 if (F.hasFnAttribute(Kind: "patchable-function-prefix"))
69 Ctx.diagnose(
70 DI: DiagnosticInfoKCFI("-fpatchable-function-entry=N,M, where M>0 is not "
71 "compatible with -fsanitize=kcfi on this target"));
72
73 IntegerType *Int32Ty = Type::getInt32Ty(C&: Ctx);
74 MDNode *VeryUnlikelyWeights = MDBuilder(Ctx).createUnlikelyBranchWeights();
75 Triple T(M.getTargetTriple());
76
77 for (CallInst *CI : KCFICalls) {
78 // Get the expected hash value.
79 const uint32_t ExpectedHash =
80 cast<ConstantInt>(Val: CI->getOperandBundle(ID: LLVMContext::OB_kcfi)->Inputs[0])
81 ->getZExtValue();
82
83 // Drop the KCFI operand bundle.
84 CallBase *Call = CallBase::removeOperandBundle(CB: CI, ID: LLVMContext::OB_kcfi,
85 InsertPt: CI->getIterator());
86 assert(Call != CI);
87 Call->copyMetadata(SrcInst: *CI);
88 CI->replaceAllUsesWith(V: Call);
89 CI->eraseFromParent();
90
91 if (!Call->isIndirectCall())
92 continue;
93
94 // Emit a check and trap if the target hash doesn't match.
95 IRBuilder<> Builder(Call);
96 Value *FuncPtr = Call->getCalledOperand();
97 // ARM uses the least significant bit of the function pointer to select
98 // between ARM and Thumb modes for the callee. Instructions are always
99 // at least 16-bit aligned, so clear the LSB before we compute the hash
100 // location.
101 if (T.isARM() || T.isThumb()) {
102 FuncPtr = Builder.CreateIntToPtr(
103 V: Builder.CreateAnd(LHS: Builder.CreatePtrToInt(V: FuncPtr, DestTy: Int32Ty),
104 RHS: ConstantInt::get(Ty: Int32Ty, V: -2)),
105 DestTy: FuncPtr->getType());
106 }
107 Value *HashPtr = Builder.CreateConstInBoundsGEP1_32(Ty: Int32Ty, Ptr: FuncPtr, Idx0: -1);
108 Value *Test = Builder.CreateICmpNE(LHS: Builder.CreateLoad(Ty: Int32Ty, Ptr: HashPtr),
109 RHS: ConstantInt::get(Ty: Int32Ty, V: ExpectedHash));
110 Instruction *ThenTerm =
111 SplitBlockAndInsertIfThen(Cond: Test, SplitBefore: Call, Unreachable: false, BranchWeights: VeryUnlikelyWeights);
112 Builder.SetInsertPoint(ThenTerm);
113 Builder.CreateCall(Intrinsic::getDeclaration(M: &M, Intrinsic::id: debugtrap));
114 ++NumKCFIChecks;
115 }
116
117 return PreservedAnalyses::none();
118}
119

source code of llvm/lib/Transforms/Instrumentation/KCFI.cpp