Remove redundant virtual on overriden functions.
[oota-llvm.git] / include / llvm / CodeGen / ForwardControlFlowIntegrity.h
1 //===-- ForwardControlFlowIntegrity.h: Forward-Edge CFI ---------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This pass instruments indirect calls with checks to ensure that these calls
9 // pass through the appropriate jump-instruction table generated by
10 // JumpInstrTables.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_FORWARDCONTROLFLOWINTEGRITY_H
15 #define LLVM_CODEGEN_FORWARDCONTROLFLOWINTEGRITY_H
16
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Target/TargetOptions.h"
21
22 #include <string>
23
24 namespace llvm {
25
26 class AnalysisUsage;
27 class BasicBlock;
28 class Constant;
29 class Function;
30 class Instruction;
31 class Module;
32 class Value;
33
34 /// ForwardControlFlowIntegrity uses the information from JumpInstrTableInfo to
35 /// prepend checks to indirect calls to make sure that these calls target valid
36 /// locations.
37 class ForwardControlFlowIntegrity : public ModulePass {
38 public:
39   static char ID;
40
41   ForwardControlFlowIntegrity();
42   ForwardControlFlowIntegrity(JumpTable::JumpTableType JTT,
43                               CFIntegrity CFIType,
44                               bool CFIEnforcing, std::string CFIFuncName);
45   ~ForwardControlFlowIntegrity() override;
46
47   /// Runs the CFI pass on a given module. This works best if the module in
48   /// question is the result of link-time optimization (see lib/LTO).
49   bool runOnModule(Module &M) override;
50   const char *getPassName() const override {
51     return "Forward Control-Flow Integrity";
52   }
53   void getAnalysisUsage(AnalysisUsage &AU) const override;
54
55 private:
56   typedef SmallVector<Instruction *, 64> CallSet;
57
58   /// A structure that is used to keep track of constant table information.
59   struct CFIConstants {
60     Constant *StartValue;
61     Constant *MaskValue;
62     Constant *Size;
63   };
64
65   /// A map from function type to the base of the table for this type and a mask
66   /// for the table
67   typedef DenseMap<FunctionType *, CFIConstants> CFITables;
68
69   CallSet IndirectCalls;
70
71   /// The type of jumptable implementation.
72   JumpTable::JumpTableType JTType;
73
74   /// The type of CFI check to add before each indirect call.
75   CFIntegrity CFIType;
76
77   /// A value that controls whether or not CFI violations cause a halt.
78   bool CFIEnforcing;
79
80   /// The name of the function to call in case of a CFI violation when
81   /// CFIEnforcing is false. There is a default function that ignores
82   /// violations.
83   std::string CFIFuncName;
84
85   /// The alignment of each entry in the table, from JumpInstrTableInfo. The
86   /// JumpInstrTableInfo class always makes this a power of two.
87   uint64_t ByteAlignment;
88
89   /// The base-2 logarithm of ByteAlignment, needed for some of the transforms
90   /// (like CFIntegrity::Ror)
91   unsigned LogByteAlignment;
92
93   /// Adds checks to each indirect call site to make sure that it is calling a
94   /// function in our jump table.
95   void updateIndirectCalls(Module &M, CFITables &CFIT);
96
97   /// Walks the instructions to find all the indirect calls.
98   void getIndirectCalls(Module &M);
99
100   /// Adds a function that handles violations in non-enforcing mode
101   /// (!CFIEnforcing). The default warning function simply returns, since the
102   /// exact details of how to handle CFI violations depend on the application.
103   void addWarningFunction(Module &M);
104
105   /// Rewrites a function pointer in a call/invoke instruction to force it into
106   /// a table.
107   void rewriteFunctionPointer(Module &M, Instruction *I, Value *FunPtr,
108                               Constant *JumpTableStart, Constant *JumpTableMask,
109                               Constant *JumpTableSize);
110
111   /// Inserts a check and a call to a warning function at a given instruction
112   /// that must be an indirect call.
113   void insertWarning(Module &M, BasicBlock *Block, Instruction *I,
114                      Value *FunPtr);
115 };
116
117 ModulePass *
118 createForwardControlFlowIntegrityPass(JumpTable::JumpTableType JTT,
119                                       CFIntegrity CFIType,
120                                       bool CFIEnforcing, StringRef CFIFuncName);
121 }
122
123 #endif // LLVM_CODEGEN_FORWARDCONTROLFLOWINTEGRITY_H