1 //===-- StackProtector.cpp - Stack Protector Insertion --------------------===//
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 #define DEBUG_TYPE "stack-protector"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Analysis/Dominators.h"
23 #include "llvm/IR/Attributes.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/Intrinsics.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Target/TargetLowering.h"
36 STATISTIC(NumFunProtected, "Number of functions protected");
37 STATISTIC(NumAddrTaken, "Number of local variables that have their address"
41 class StackProtector : public FunctionPass {
42 /// TLI - Keep a pointer of a TargetLowering to consult for determining
43 /// target type sizes.
44 const TargetLoweringBase *TLI;
51 /// VisitedPHIs - The set of PHI nodes visited when determining
52 /// if a variable's reference has been taken. This set
53 /// is maintained to ensure we don't visit the same PHI node multiple
55 SmallPtrSet<const PHINode*, 16> VisitedPHIs;
57 /// InsertStackProtectors - Insert code into the prologue and epilogue of
60 /// - The prologue code loads and stores the stack guard onto the stack.
61 /// - The epilogue checks the value stored in the prologue against the
62 /// original value. It calls __stack_chk_fail if they differ.
63 bool InsertStackProtectors();
65 /// CreateFailBB - Create a basic block to jump to when the stack protector
67 BasicBlock *CreateFailBB();
69 /// ContainsProtectableArray - Check whether the type either is an array or
70 /// contains an array of sufficient size so that we need stack protectors
72 bool ContainsProtectableArray(Type *Ty, bool Strong = false,
73 bool InStruct = false) const;
75 /// \brief Check whether a stack allocation has its address taken.
76 bool HasAddressTaken(const Instruction *AI);
78 /// RequiresStackProtector - Check whether or not this function needs a
79 /// stack protector based upon the stack protector level.
80 bool RequiresStackProtector();
82 static char ID; // Pass identification, replacement for typeid.
83 StackProtector() : FunctionPass(ID), TLI(0) {
84 initializeStackProtectorPass(*PassRegistry::getPassRegistry());
86 StackProtector(const TargetLoweringBase *tli)
87 : FunctionPass(ID), TLI(tli) {
88 initializeStackProtectorPass(*PassRegistry::getPassRegistry());
91 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
92 AU.addPreserved<DominatorTree>();
95 virtual bool runOnFunction(Function &Fn);
97 } // end anonymous namespace
99 char StackProtector::ID = 0;
100 INITIALIZE_PASS(StackProtector, "stack-protector",
101 "Insert stack protectors", false, false)
103 FunctionPass *llvm::createStackProtectorPass(const TargetLoweringBase *tli) {
104 return new StackProtector(tli);
107 bool StackProtector::runOnFunction(Function &Fn) {
110 DT = getAnalysisIfAvailable<DominatorTree>();
112 if (!RequiresStackProtector()) return false;
115 return InsertStackProtectors();
118 /// ContainsProtectableArray - Check whether the type either is an array or
119 /// contains a char array of sufficient size so that we need stack protectors
121 bool StackProtector::ContainsProtectableArray(Type *Ty, bool Strong,
122 bool InStruct) const {
123 if (!Ty) return false;
124 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
125 // In strong mode any array, regardless of type and size, triggers a
129 const TargetMachine &TM = TLI->getTargetMachine();
130 if (!AT->getElementType()->isIntegerTy(8)) {
131 Triple Trip(TM.getTargetTriple());
133 // If we're on a non-Darwin platform or we're inside of a structure, don't
134 // add stack protectors unless the array is a character array.
135 if (InStruct || !Trip.isOSDarwin())
139 // If an array has more than SSPBufferSize bytes of allocated space, then we
140 // emit stack protectors.
141 if (TM.Options.SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT))
145 const StructType *ST = dyn_cast<StructType>(Ty);
146 if (!ST) return false;
148 for (StructType::element_iterator I = ST->element_begin(),
149 E = ST->element_end(); I != E; ++I)
150 if (ContainsProtectableArray(*I, Strong, true))
156 bool StackProtector::HasAddressTaken(const Instruction *AI) {
157 for (Value::const_use_iterator UI = AI->use_begin(), UE = AI->use_end();
160 if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
161 if (AI == SI->getValueOperand())
163 } else if (const PtrToIntInst *SI = dyn_cast<PtrToIntInst>(U)) {
164 if (AI == SI->getOperand(0))
166 } else if (isa<CallInst>(U)) {
168 } else if (isa<InvokeInst>(U)) {
170 } else if (const SelectInst *SI = dyn_cast<SelectInst>(U)) {
171 if (HasAddressTaken(SI))
173 } else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
174 // Keep track of what PHI nodes we have already visited to ensure
175 // they are only visited once.
176 if (VisitedPHIs.insert(PN))
177 if (HasAddressTaken(PN))
179 } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
180 if (HasAddressTaken(GEP))
182 } else if (const BitCastInst *BI = dyn_cast<BitCastInst>(U)) {
183 if (HasAddressTaken(BI))
190 /// \brief Check whether or not this function needs a stack protector based
191 /// upon the stack protector level.
193 /// We use two heuristics: a standard (ssp) and strong (sspstrong).
194 /// The standard heuristic which will add a guard variable to functions that
195 /// call alloca with a either a variable size or a size >= SSPBufferSize,
196 /// functions with character buffers larger than SSPBufferSize, and functions
197 /// with aggregates containing character buffers larger than SSPBufferSize. The
198 /// strong heuristic will add a guard variables to functions that call alloca
199 /// regardless of size, functions with any buffer regardless of type and size,
200 /// functions with aggregates that contain any buffer regardless of type and
201 /// size, and functions that contain stack-based variables that have had their
203 bool StackProtector::RequiresStackProtector() {
205 if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
206 Attribute::StackProtectReq))
208 else if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
209 Attribute::StackProtectStrong))
211 else if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
212 Attribute::StackProtect))
215 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
218 for (BasicBlock::iterator
219 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
220 if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
221 if (AI->isArrayAllocation()) {
222 // SSP-Strong: Enable protectors for any call to alloca, regardless
227 if (const ConstantInt *CI =
228 dyn_cast<ConstantInt>(AI->getArraySize())) {
229 unsigned BufferSize = TLI->getTargetMachine().Options.SSPBufferSize;
230 if (CI->getLimitedValue(BufferSize) >= BufferSize)
231 // A call to alloca with size >= SSPBufferSize requires
234 } else // A call to alloca with a variable size requires protectors.
238 if (ContainsProtectableArray(AI->getAllocatedType(), Strong))
241 if (Strong && HasAddressTaken(AI)) {
252 /// InsertStackProtectors - Insert code into the prologue and epilogue of the
255 /// - The prologue code loads and stores the stack guard onto the stack.
256 /// - The epilogue checks the value stored in the prologue against the original
257 /// value. It calls __stack_chk_fail if they differ.
258 bool StackProtector::InsertStackProtectors() {
259 BasicBlock *FailBB = 0; // The basic block to jump to if check fails.
260 BasicBlock *FailBBDom = 0; // FailBB's dominator.
261 AllocaInst *AI = 0; // Place on stack that stores the stack guard.
262 Value *StackGuardVar = 0; // The stack guard variable.
264 for (Function::iterator I = F->begin(), E = F->end(); I != E; ) {
265 BasicBlock *BB = I++;
266 ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
270 // Insert code into the entry block that stores the __stack_chk_guard
271 // variable onto the stack:
274 // StackGuardSlot = alloca i8*
275 // StackGuard = load __stack_chk_guard
276 // call void @llvm.stackprotect.create(StackGuard, StackGuardSlot)
278 PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext());
279 unsigned AddressSpace, Offset;
280 if (TLI->getStackCookieLocation(AddressSpace, Offset)) {
281 Constant *OffsetVal =
282 ConstantInt::get(Type::getInt32Ty(RI->getContext()), Offset);
284 StackGuardVar = ConstantExpr::getIntToPtr(OffsetVal,
285 PointerType::get(PtrTy, AddressSpace));
287 StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);
290 BasicBlock &Entry = F->getEntryBlock();
291 Instruction *InsPt = &Entry.front();
293 AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt);
294 LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt);
296 Value *Args[] = { LI, AI };
298 Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector),
301 // Create the basic block to jump to when the guard check fails.
302 FailBB = CreateFailBB();
305 // For each block with a return instruction, convert this:
315 // %1 = load __stack_chk_guard
316 // %2 = load StackGuardSlot
317 // %3 = cmp i1 %1, %2
318 // br i1 %3, label %SP_return, label %CallStackCheckFailBlk
323 // CallStackCheckFailBlk:
324 // call void @__stack_chk_fail()
327 // Split the basic block before the return instruction.
328 BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return");
330 if (DT && DT->isReachableFromEntry(BB)) {
331 DT->addNewBlock(NewBB, BB);
332 FailBBDom = FailBBDom ? DT->findNearestCommonDominator(FailBBDom, BB) :BB;
335 // Remove default branch instruction to the new BB.
336 BB->getTerminator()->eraseFromParent();
338 // Move the newly created basic block to the point right after the old basic
339 // block so that it's in the "fall through" position.
340 NewBB->moveAfter(BB);
342 // Generate the stack protector instructions in the old basic block.
343 LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB);
344 LoadInst *LI2 = new LoadInst(AI, "", true, BB);
345 ICmpInst *Cmp = new ICmpInst(*BB, CmpInst::ICMP_EQ, LI1, LI2, "");
346 BranchInst::Create(NewBB, FailBB, Cmp, BB);
349 // Return if we didn't modify any basic blocks. I.e., there are no return
350 // statements in the function.
351 if (!FailBB) return false;
354 DT->addNewBlock(FailBB, FailBBDom);
359 /// CreateFailBB - Create a basic block to jump to when the stack protector
361 BasicBlock *StackProtector::CreateFailBB() {
362 BasicBlock *FailBB = BasicBlock::Create(F->getContext(),
363 "CallStackCheckFailBlk", F);
364 Constant *StackChkFail =
365 M->getOrInsertFunction("__stack_chk_fail",
366 Type::getVoidTy(F->getContext()), NULL);
367 CallInst::Create(StackChkFail, "", FailBB);
368 new UnreachableInst(F->getContext(), FailBB);