1 //===- StackProtector.h - Stack Protector Insertion -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass inserts stack protectors into functions which need them. A variable
11 // with a random value in it is stored onto the stack before the local variables
12 // are allocated. Upon exiting the block, the stored value is checked. If it's
13 // changed, then there was some sort of violation and the program aborts.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CODEGEN_STACKPROTECTOR_H
18 #define LLVM_CODEGEN_STACKPROTECTOR_H
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/ValueMap.h"
25 #include "llvm/Pass.h"
34 class TargetLoweringBase;
38 class StackProtector : public FunctionPass {
40 /// A mapping of AllocaInsts to their required SSP layout.
41 using SSPLayoutMap = DenseMap<const AllocaInst *,
42 MachineFrameInfo::SSPLayoutKind>;
44 const TargetMachine *TM = nullptr;
46 /// TLI - Keep a pointer of a TargetLowering to consult for determining
47 /// target type sizes.
48 const TargetLoweringBase *TLI = nullptr;
56 /// Layout - Mapping of allocations to the required SSPLayoutKind.
57 /// StackProtector analysis will update this map when determining if an
58 /// AllocaInst triggers a stack protector.
61 /// The minimum size of buffers that will receive stack smashing
62 /// protection when -fstack-protection is used.
63 unsigned SSPBufferSize = 0;
65 /// VisitedPHIs - The set of PHI nodes visited when determining
66 /// if a variable's reference has been taken. This set
67 /// is maintained to ensure we don't visit the same PHI node multiple
69 SmallPtrSet<const PHINode *, 16> VisitedPHIs;
71 // A prologue is generated.
72 bool HasPrologue = false;
74 // IR checking code is generated.
75 bool HasIRCheck = false;
77 /// InsertStackProtectors - Insert code into the prologue and epilogue of
80 /// - The prologue code loads and stores the stack guard onto the stack.
81 /// - The epilogue checks the value stored in the prologue against the
82 /// original value. It calls __stack_chk_fail if they differ.
83 bool InsertStackProtectors();
85 /// CreateFailBB - Create a basic block to jump to when the stack protector
87 BasicBlock *CreateFailBB();
89 /// ContainsProtectableArray - Check whether the type either is an array or
90 /// contains an array of sufficient size so that we need stack protectors
92 /// \param [out] IsLarge is set to true if a protectable array is found and
93 /// it is "large" ( >= ssp-buffer-size). In the case of a structure with
94 /// multiple arrays, this gets set if any of them is large.
95 bool ContainsProtectableArray(Type *Ty, bool &IsLarge, bool Strong = false,
96 bool InStruct = false) const;
98 /// Check whether a stack allocation has its address taken.
99 bool HasAddressTaken(const Instruction *AI);
101 /// RequiresStackProtector - Check whether or not this function needs a
102 /// stack protector based upon the stack protector level.
103 bool RequiresStackProtector();
106 static char ID; // Pass identification, replacement for typeid.
108 StackProtector() : FunctionPass(ID), SSPBufferSize(8) {
109 initializeStackProtectorPass(*PassRegistry::getPassRegistry());
112 void getAnalysisUsage(AnalysisUsage &AU) const override;
114 // Return true if StackProtector is supposed to be handled by SelectionDAG.
115 bool shouldEmitSDCheck(const BasicBlock &BB) const;
117 bool runOnFunction(Function &Fn) override;
119 void copyToMachineFrameInfo(MachineFrameInfo &MFI) const;
122 } // end namespace llvm
124 #endif // LLVM_CODEGEN_STACKPROTECTOR_H