54 #define DEBUG_TYPE "stack-protector" 56 STATISTIC(NumFunProtected,
"Number of functions protected");
57 STATISTIC(NumAddrTaken,
"Number of local variables that have their address" 66 "Insert stack protectors",
false,
true)
82 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
84 TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
95 if (!RequiresStackProtector())
107 return InsertStackProtectors();
113 bool StackProtector::ContainsProtectableArray(
Type *Ty,
bool &IsLarge,
115 bool InStruct)
const {
118 if (
ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
119 if (!AT->getElementType()->isIntegerTy(8)) {
124 if (!Strong && (InStruct || !Trip.
isOSDarwin()))
130 if (SSPBufferSize <= M->getDataLayout().getTypeAllocSize(AT)) {
144 bool NeedsProtector =
false;
148 if (ContainsProtectableArray(*
I, IsLarge, Strong,
true)) {
154 NeedsProtector =
true;
157 return NeedsProtector;
161 if (
const auto Intrinsic = dyn_cast<IntrinsicInst>(I)) {
162 const auto Id = Intrinsic->getIntrinsicID();
163 return Id == Intrinsic::lifetime_start ||
Id == Intrinsic::lifetime_end;
168 bool StackProtector::HasAddressTaken(
const Instruction *AI) {
170 if (
const StoreInst *
SI = dyn_cast<StoreInst>(U)) {
171 if (AI ==
SI->getValueOperand())
174 if (AI ==
SI->getOperand(0))
176 }
else if (
const CallInst *CI = dyn_cast<CallInst>(U)) {
180 }
else if (isa<InvokeInst>(U)) {
182 }
else if (
const SelectInst *
SI = dyn_cast<SelectInst>(U)) {
183 if (HasAddressTaken(
SI))
185 }
else if (
const PHINode *PN = dyn_cast<PHINode>(U)) {
188 if (VisitedPHIs.insert(PN).second)
189 if (HasAddressTaken(PN))
192 if (HasAddressTaken(
GEP))
194 }
else if (
const BitCastInst *BI = dyn_cast<BitCastInst>(U)) {
195 if (HasAddressTaken(BI))
215 bool StackProtector::RequiresStackProtector() {
217 bool NeedsProtector =
false;
220 if (
const CallInst *CI = dyn_cast<CallInst>(&
I))
221 if (CI->getCalledFunction() ==
223 Intrinsic::stackprotector))
226 if (F->hasFnAttribute(Attribute::SafeStack))
234 if (F->hasFnAttribute(Attribute::StackProtectReq)) {
237 <<
"Stack protection applied to function " 239 <<
" due to a function attribute or command-line switch";
241 NeedsProtector =
true;
243 }
else if (F->hasFnAttribute(Attribute::StackProtectStrong))
245 else if (HasPrologue)
246 NeedsProtector =
true;
247 else if (!F->hasFnAttribute(Attribute::StackProtect))
252 if (
const AllocaInst *AI = dyn_cast<AllocaInst>(&
I)) {
253 if (AI->isArrayAllocation()) {
254 auto RemarkBuilder = [&]() {
257 <<
"Stack protection applied to function " 259 <<
" due to a call to alloca or use of a variable length " 262 if (
const auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) {
263 if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) {
266 Layout.
insert(std::make_pair(AI,
268 ORE.
emit(RemarkBuilder);
269 NeedsProtector =
true;
272 Layout.
insert(std::make_pair(AI,
274 ORE.
emit(RemarkBuilder);
275 NeedsProtector =
true;
279 Layout.
insert(std::make_pair(AI,
281 ORE.
emit(RemarkBuilder);
282 NeedsProtector =
true;
287 bool IsLarge =
false;
288 if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) {
289 Layout.
insert(std::make_pair(AI, IsLarge
294 <<
"Stack protection applied to function " 296 <<
" due to a stack allocated buffer or struct containing a " 299 NeedsProtector =
true;
303 if (Strong && HasAddressTaken(AI)) {
309 <<
"Stack protection applied to function " 311 <<
" due to the address of a local variable being taken";
313 NeedsProtector =
true;
319 return NeedsProtector;
326 bool *SupportsSelectionDAGSP =
nullptr) {
328 return B.
CreateLoad(Guard,
true,
"StackGuard");
341 if (SupportsSelectionDAGSP)
342 *SupportsSelectionDAGSP =
true;
359 bool SupportsSelectionDAGSP =
false;
362 AI =
B.CreateAlloca(PtrTy,
nullptr,
"StackGuardSlot");
367 return SupportsSelectionDAGSP;
376 bool StackProtector::InsertStackProtectors() {
380 bool SupportsSelectionDAGSP =
399 if (SupportsSelectionDAGSP)
477 .createBranchWeights(SuccessProb.getNumerator(),
478 FailureProb.getNumerator());
529 if (LI == Layout.
end())
Return a value (possibly void), from a function.
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
DILocation * get() const
Get the underlying DILocation.
DiagnosticInfoOptimizationBase::Argument NV
Compute iterated dominance frontiers using a linear time algorithm.
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
unsigned EnableFastISel
EnableFastISel - This flag enables fast-path instruction selection which trades away generated code q...
A Module instance is used to store all the information related to an LLVM module. ...
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
This class represents a function call, abstracting a target machine's calling convention.
virtual const TargetLowering * getTargetLowering() const
void copyToMachineFrameInfo(MachineFrameInfo &MFI) const
LLVMContext & getContext() const
All values hold a context through their type.
STATISTIC(NumFunctions, "Total number of functions")
block Block Frequency true
An instruction for reading from memory.
static bool isLifetimeInst(const Instruction *I)
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI, const TargetLoweringBase *TLI, AllocaInst *&AI)
Insert code into the entry block that stores the stack guard variable onto the stack: ...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
This class represents the LLVM 'select' instruction.
bool isStringAttribute() const
Return true if the attribute is a string (target-dependent) attribute.
Class to represent struct types.
static cl::opt< bool > EnableSelectionDAGSP("enable-selectiondag-sp", cl::init(true), cl::Hidden)
The address of this allocation is exposed and triggered protection.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
This file contains the simple types necessary to represent the attributes associated with functions a...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
DominatorTree & getDomTree()
Target-Independent Code Generator Pass Configuration Options.
This class represents a cast from a pointer to an integer.
Class to represent array types.
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
This class represents a no-op cast from one type to another.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
AttributeList getAttributes() const
Return the attribute list for this Function.
An instruction for storing to memory.
bool hasPersonalityFn() const
Check whether this function has a personality function.
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Type::subtype_iterator element_iterator
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Class to represent pointers.
bool runOnFunction(Function &Fn) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind)
Array or nested array >= SSP-buffer-size.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
iterator find(const_arg_type_t< KeyT > Val)
const BasicBlock & getEntryBlock() const
virtual Value * getIRStackGuard(IRBuilder<> &IRB) const
If the target has a standard location for the stack protector guard, returns the address of that loca...
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
LoadInst * CreateLoad(Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of converting the string to 'bool' fo...
initializer< Ty > init(const Ty &Val)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
UnreachableInst * CreateUnreachable()
DISubprogram * getSubprogram() const
Get the attached subprogram.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const Instruction & front() const
element_iterator element_end() const
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
Represent the analysis usage information of a pass.
static Type * getVoidTy(LLVMContext &C)
FunctionPass class - This class is used to implement most global optimizations.
const Triple & getTargetTriple() const
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
self_iterator getIterator()
FunctionPass * createStackProtectorPass()
createStackProtectorPass - This pass adds stack protectors to functions.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
virtual bool useStackGuardXorFP() const
If this function returns true, stack protection checks should XOR the frame pointer (or whichever poi...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
void setCallingConv(CallingConv::ID CC)
This base class for TargetLowering contains the SelectionDAG-independent parts that can be used from ...
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Iterator for intrusive lists based on ilist_node.
void moveAfter(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it right after MovePos in the function M...
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Module.h This file contains the declarations for the Module class.
virtual const TargetSubtargetInfo * getSubtargetImpl(const Function &) const
Virtual method implemented by subclasses that returns a reference to that target's TargetSubtargetInf...
Constant * CreateGlobalStringPtr(StringRef Str, const Twine &Name="", unsigned AddressSpace=0)
Same as CreateGlobalString, but return a pointer with "i8*" type instead of a pointer to array of i8...
iterator_range< user_iterator > users()
element_iterator element_begin() const
virtual Value * getSSPStackGuardCheck(const Module &M) const
If the target has a standard stack protection check function that performs validation and error handl...
const AllocaInst * getObjectAllocation(int ObjectIdx) const
Return the underlying Alloca of the specified stack object if it exists.
INITIALIZE_PASS_BEGIN(StackProtector, DEBUG_TYPE, "Insert stack protectors", false, true) INITIALIZE_PASS_END(StackProtector
StringRef getValueAsString() const
Return the attribute's value as a string.
StringRef getName() const
Return a constant reference to the value's name.
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
LLVM_NODISCARD bool empty() const
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
Constant * getPersonalityFn() const
Get the personality function associated with this function.
Array or nested array < SSP-buffer-size.
virtual void insertSSPDeclarations(Module &M) const
Inserts necessary declarations for SSP (stack protection) purpose.
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Legacy analysis pass which computes a DominatorTree.
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
static Value * getStackGuard(const TargetLoweringBase *TLI, Module *M, IRBuilder<> &B, bool *SupportsSelectionDAGSP=nullptr)
Create a stack guard loading and populate whether SelectionDAG SSP is supported.
bool shouldEmitSDCheck(const BasicBlock &BB) const
static BranchProbability getBranchProbStackProtector(bool IsLikely)
This file describes how to lower LLVM code to machine code.
an instruction to allocate memory on the stack
CallInst * CreateCall(Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)