LLVM 20.0.0git
KCFI.cpp
Go to the documentation of this file.
1//===---- KCFI.cpp - Implements Kernel Control-Flow Integrity (KCFI) ------===//
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 implements Kernel Control-Flow Integrity (KCFI) indirect call
10// check lowering. For each call instruction with a cfi-type attribute, it
11// emits an arch-specific check before the call, and bundles the check and
12// the call to prevent unintentional modifications.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/ADT/Statistic.h"
23#include "llvm/IR/Module.h"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "kcfi"
29#define KCFI_PASS_NAME "Insert KCFI indirect call checks"
30
31STATISTIC(NumKCFIChecksAdded, "Number of indirect call checks added");
32
33namespace {
34class KCFI : public MachineFunctionPass {
35public:
36 static char ID;
37
38 KCFI() : MachineFunctionPass(ID) {}
39
40 StringRef getPassName() const override { return KCFI_PASS_NAME; }
41 bool runOnMachineFunction(MachineFunction &MF) override;
42
43private:
44 /// Machine instruction info used throughout the class.
45 const TargetInstrInfo *TII = nullptr;
46
47 /// Target lowering for arch-specific parts.
48 const TargetLowering *TLI = nullptr;
49
50 /// Emits a KCFI check before an indirect call.
51 /// \returns true if the check was added and false otherwise.
52 bool emitCheck(MachineBasicBlock &MBB,
54};
55
56char KCFI::ID = 0;
57} // end anonymous namespace
58
59INITIALIZE_PASS(KCFI, DEBUG_TYPE, KCFI_PASS_NAME, false, false)
60
61FunctionPass *llvm::createKCFIPass() { return new KCFI(); }
62
63bool KCFI::emitCheck(MachineBasicBlock &MBB,
65 assert(TII && "Target instruction info was not initialized");
66 assert(TLI && "Target lowering was not initialized");
67
68 // If the call instruction is bundled, we can only emit a check safely if
69 // it's the first instruction in the bundle.
70 if (MBBI->isBundled() && !std::prev(MBBI)->isBundle())
71 report_fatal_error("Cannot emit a KCFI check for a bundled call");
72
73 // Emit a KCFI check for the call instruction at MBBI. The implementation
74 // must unfold memory operands if applicable.
75 MachineInstr *Check = TLI->EmitKCFICheck(MBB, MBBI, TII);
76
77 // Clear the original call's CFI type.
78 assert(MBBI->isCall() && "Unexpected instruction type");
79 MBBI->setCFIType(*MBB.getParent(), 0);
80
81 // If not already bundled, bundle the check and the call to prevent
82 // further changes.
83 if (!MBBI->isBundled())
84 finalizeBundle(MBB, Check->getIterator(), std::next(MBBI->getIterator()));
85
86 ++NumKCFIChecksAdded;
87 return true;
88}
89
90bool KCFI::runOnMachineFunction(MachineFunction &MF) {
91 const Module *M = MF.getFunction().getParent();
92 if (!M->getModuleFlag("kcfi"))
93 return false;
94
95 const auto &SubTarget = MF.getSubtarget();
96 TII = SubTarget.getInstrInfo();
97 TLI = SubTarget.getTargetLowering();
98
99 bool Changed = false;
100 for (MachineBasicBlock &MBB : MF) {
101 // Use instr_iterator because we don't want to skip bundles.
103 MIE = MBB.instr_end();
104 MII != MIE; ++MII) {
105 if (MII->isCall() && MII->getCFIType())
106 Changed |= emitCheck(MBB, MII);
107 }
108 }
109
110 return Changed;
111}
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator MBBI
#define KCFI_PASS_NAME
Definition: KCFI.cpp:29
#define DEBUG_TYPE
Definition: KCFI.cpp:28
#define Check(C,...)
const HexagonInstrInfo * TII
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition: MD5.cpp:58
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
This file describes how to lower LLVM code to machine code.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
instr_iterator instr_begin()
Instructions::iterator instr_iterator
instr_iterator instr_end()
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:69
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
TargetInstrInfo - Interface to description of machine instruction set.
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
self_iterator getIterator()
Definition: ilist_node.h:132
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void finalizeBundle(MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
finalizeBundle - Finalize a machine instruction bundle which includes a sequence of instructions star...
FunctionPass * createKCFIPass()
Lowers KCFI operand bundles for indirect calls.
Definition: KCFI.cpp:61
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167