54 #define DEBUG_TYPE "stack-protector" 56 STATISTIC(NumFunProtected,
"Number of functions protected");
57 STATISTIC(NumAddrTaken,
"Number of local variables that have their address" 70 "Insert stack protectors",
false,
true)
86 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
88 TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
99 if (!RequiresStackProtector())
111 return InsertStackProtectors();
117 bool StackProtector::ContainsProtectableArray(
Type *Ty,
bool &IsLarge,
119 bool InStruct)
const {
122 if (
ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
123 if (!AT->getElementType()->isIntegerTy(8)) {
128 if (!Strong && (InStruct || !Trip.
isOSDarwin()))
134 if (SSPBufferSize <= M->getDataLayout().getTypeAllocSize(AT)) {
148 bool NeedsProtector =
false;
152 if (ContainsProtectableArray(*
I, IsLarge, Strong,
true)) {
158 NeedsProtector =
true;
161 return NeedsProtector;
164 bool StackProtector::HasAddressTaken(
const Instruction *AI) {
166 const auto *
I = cast<Instruction>(U);
167 switch (
I->getOpcode()) {
169 if (AI == cast<StoreInst>(
I)->getValueOperand())
172 case Instruction::AtomicCmpXchg:
175 if (AI == cast<AtomicCmpXchgInst>(
I)->getNewValOperand())
178 case Instruction::PtrToInt:
185 const auto *CI = cast<CallInst>(
I);
186 if (!isa<DbgInfoIntrinsic>(CI) && !CI->isLifetimeStartOrEnd())
190 case Instruction::Invoke:
192 case Instruction::BitCast:
193 case Instruction::GetElementPtr:
195 case Instruction::AddrSpaceCast:
196 if (HasAddressTaken(
I))
199 case Instruction::PHI: {
202 const auto *PN = cast<PHINode>(
I);
203 if (VisitedPHIs.insert(PN).second)
204 if (HasAddressTaken(PN))
209 case Instruction::AtomicRMW:
231 if (
const CallInst *CI = dyn_cast<CallInst>(&
I))
232 if (CI->getCalledFunction() ==
251 bool StackProtector::RequiresStackProtector() {
253 bool NeedsProtector =
false;
267 <<
"Stack protection applied to function " 269 <<
" due to a function attribute or command-line switch";
271 NeedsProtector =
true;
275 else if (HasPrologue)
276 NeedsProtector =
true;
282 if (
const AllocaInst *AI = dyn_cast<AllocaInst>(&
I)) {
283 if (AI->isArrayAllocation()) {
284 auto RemarkBuilder = [&]() {
287 <<
"Stack protection applied to function " 289 <<
" due to a call to alloca or use of a variable length " 292 if (
const auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) {
293 if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) {
296 Layout.
insert(std::make_pair(AI,
298 ORE.
emit(RemarkBuilder);
299 NeedsProtector =
true;
302 Layout.
insert(std::make_pair(AI,
304 ORE.
emit(RemarkBuilder);
305 NeedsProtector =
true;
309 Layout.
insert(std::make_pair(AI,
311 ORE.
emit(RemarkBuilder);
312 NeedsProtector =
true;
317 bool IsLarge =
false;
318 if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) {
319 Layout.
insert(std::make_pair(AI, IsLarge
324 <<
"Stack protection applied to function " 326 <<
" due to a stack allocated buffer or struct containing a " 329 NeedsProtector =
true;
333 if (Strong && HasAddressTaken(AI)) {
339 <<
"Stack protection applied to function " 341 <<
" due to the address of a local variable being taken";
343 NeedsProtector =
true;
349 return NeedsProtector;
356 bool *SupportsSelectionDAGSP =
nullptr) {
371 if (SupportsSelectionDAGSP)
372 *SupportsSelectionDAGSP =
true;
389 bool SupportsSelectionDAGSP =
false;
392 AI =
B.CreateAlloca(PtrTy,
nullptr,
"StackGuardSlot");
397 return SupportsSelectionDAGSP;
406 bool StackProtector::InsertStackProtectors() {
410 bool SupportsSelectionDAGSP =
430 if (SupportsSelectionDAGSP)
437 assert(SPCall &&
"Call to llvm.stackprotector is missing");
515 .createBranchWeights(SuccessProb.getNumerator(),
516 FailureProb.getNumerator());
550 return HasPrologue && !HasIRCheck && isa<ReturnInst>(BB.
getTerminator());
566 if (LI == Layout.
end())
Return a value (possibly void), from a function.
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (macOS, iOS, tvOS 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.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
DiagnosticInfoOptimizationBase::Argument NV
This class represents lattice values for constants.
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
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. ...
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
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.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
STATISTIC(NumFunctions, "Total number of functions")
block Block Frequency true
An instruction for reading from memory.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
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: ...
Value * getArgOperand(unsigned i) const
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
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 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.
Class to represent array types.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
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.
Value * getOperand(unsigned i) const
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)
unsigned EnableGlobalISel
EnableGlobalISel - This flag enables global instruction selection.
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...
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 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)
void setCallingConv(CallingConv::ID CC)
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)
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
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
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.
static const CallInst * findStackProtectorIntrinsic(Function &F)
Search for the first call to the llvm.stackprotector intrinsic and return it if present.
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...
Module.h This file contains the declarations for the Module class.
virtual Function * getSSPStackGuardCheck(const Module &M) const
If the target has a standard stack protection check function that performs validation and error handl...
void initializeStackProtectorPass(PassRegistry &)
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
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
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.
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
LLVM_NODISCARD bool empty() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
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