[Alignment][NFC] Use Align with TargetLowering::setMinFunctionAlignment
[llvm-core.git] / include / llvm / CodeGen / StackProtector.h
blob2bdf4425e24a3076fa0dd8a199a100b337219fe5
1 //===- StackProtector.h - Stack Protector Insertion -------------*- 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 inserts stack protectors into functions which need them. A variable
10 // with a random value in it is stored onto the stack before the local variables
11 // are allocated. Upon exiting the block, the stored value is checked. If it's
12 // changed, then there was some sort of violation and the program aborts.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_STACKPROTECTOR_H
17 #define LLVM_CODEGEN_STACKPROTECTOR_H
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/ValueMap.h"
24 #include "llvm/Pass.h"
26 namespace llvm {
28 class BasicBlock;
29 class DominatorTree;
30 class Function;
31 class Instruction;
32 class Module;
33 class TargetLoweringBase;
34 class TargetMachine;
35 class Type;
37 class StackProtector : public FunctionPass {
38 private:
39 /// A mapping of AllocaInsts to their required SSP layout.
40 using SSPLayoutMap = DenseMap<const AllocaInst *,
41 MachineFrameInfo::SSPLayoutKind>;
43 const TargetMachine *TM = nullptr;
45 /// TLI - Keep a pointer of a TargetLowering to consult for determining
46 /// target type sizes.
47 const TargetLoweringBase *TLI = nullptr;
48 Triple Trip;
50 Function *F;
51 Module *M;
53 DominatorTree *DT;
55 /// Layout - Mapping of allocations to the required SSPLayoutKind.
56 /// StackProtector analysis will update this map when determining if an
57 /// AllocaInst triggers a stack protector.
58 SSPLayoutMap Layout;
60 /// The minimum size of buffers that will receive stack smashing
61 /// protection when -fstack-protection is used.
62 unsigned SSPBufferSize = 0;
64 // A prologue is generated.
65 bool HasPrologue = false;
67 // IR checking code is generated.
68 bool HasIRCheck = false;
70 /// InsertStackProtectors - Insert code into the prologue and epilogue of
71 /// the function.
72 ///
73 /// - The prologue code loads and stores the stack guard onto the stack.
74 /// - The epilogue checks the value stored in the prologue against the
75 /// original value. It calls __stack_chk_fail if they differ.
76 bool InsertStackProtectors();
78 /// CreateFailBB - Create a basic block to jump to when the stack protector
79 /// check fails.
80 BasicBlock *CreateFailBB();
82 /// ContainsProtectableArray - Check whether the type either is an array or
83 /// contains an array of sufficient size so that we need stack protectors
84 /// for it.
85 /// \param [out] IsLarge is set to true if a protectable array is found and
86 /// it is "large" ( >= ssp-buffer-size). In the case of a structure with
87 /// multiple arrays, this gets set if any of them is large.
88 bool ContainsProtectableArray(Type *Ty, bool &IsLarge, bool Strong = false,
89 bool InStruct = false) const;
91 /// Check whether a stack allocation has its address taken.
92 bool HasAddressTaken(const Instruction *AI);
94 /// RequiresStackProtector - Check whether or not this function needs a
95 /// stack protector based upon the stack protector level.
96 bool RequiresStackProtector();
98 public:
99 static char ID; // Pass identification, replacement for typeid.
101 StackProtector() : FunctionPass(ID), SSPBufferSize(8) {
102 initializeStackProtectorPass(*PassRegistry::getPassRegistry());
105 void getAnalysisUsage(AnalysisUsage &AU) const override;
107 // Return true if StackProtector is supposed to be handled by SelectionDAG.
108 bool shouldEmitSDCheck(const BasicBlock &BB) const;
110 bool runOnFunction(Function &Fn) override;
112 void copyToMachineFrameInfo(MachineFrameInfo &MFI) const;
115 } // end namespace llvm
117 #endif // LLVM_CODEGEN_STACKPROTECTOR_H