Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 1 | //===-- StackProtector.cpp - Stack Protector Insertion --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 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 |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 13 | // changed, then there was some sort of violation and the program aborts. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #define DEBUG_TYPE "stack-protector" |
| 18 | #include "llvm/CodeGen/Passes.h" |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Triple.h" |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/Dominators.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 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" |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetLowering.h" |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 36 | STATISTIC(NumFunProtected, "Number of functions protected"); |
| 37 | STATISTIC(NumAddrTaken, "Number of local variables that have their address" |
| 38 | " taken."); |
| 39 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 40 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 41 | class StackProtector : public FunctionPass { |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 42 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 43 | /// target type sizes. |
Benjamin Kramer | 69e42db | 2013-01-11 20:05:37 +0000 | [diff] [blame] | 44 | const TargetLoweringBase *TLI; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 45 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 46 | Function *F; |
| 47 | Module *M; |
| 48 | |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 49 | DominatorTree *DT; |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 50 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 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 |
| 54 | /// times. |
| 55 | SmallPtrSet<const PHINode*, 16> VisitedPHIs; |
| 56 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 57 | /// InsertStackProtectors - Insert code into the prologue and epilogue of |
| 58 | /// the function. |
| 59 | /// |
| 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(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 64 | |
| 65 | /// CreateFailBB - Create a basic block to jump to when the stack protector |
| 66 | /// check fails. |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 67 | BasicBlock *CreateFailBB(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 68 | |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 69 | /// ContainsProtectableArray - Check whether the type either is an array or |
| 70 | /// contains an array of sufficient size so that we need stack protectors |
| 71 | /// for it. |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 72 | bool ContainsProtectableArray(Type *Ty, bool Strong = false, |
| 73 | bool InStruct = false) const; |
| 74 | |
| 75 | /// \brief Check whether a stack allocation has its address taken. |
| 76 | bool HasAddressTaken(const Instruction *AI); |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 77 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 78 | /// RequiresStackProtector - Check whether or not this function needs a |
| 79 | /// stack protector based upon the stack protector level. |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 80 | bool RequiresStackProtector(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 81 | public: |
| 82 | static char ID; // Pass identification, replacement for typeid. |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 83 | StackProtector() : FunctionPass(ID), TLI(0) { |
| 84 | initializeStackProtectorPass(*PassRegistry::getPassRegistry()); |
| 85 | } |
Benjamin Kramer | 69e42db | 2013-01-11 20:05:37 +0000 | [diff] [blame] | 86 | StackProtector(const TargetLoweringBase *tli) |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 87 | : FunctionPass(ID), TLI(tli) { |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 88 | initializeStackProtectorPass(*PassRegistry::getPassRegistry()); |
| 89 | } |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 90 | |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 91 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 92 | AU.addPreserved<DominatorTree>(); |
| 93 | } |
| 94 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 95 | virtual bool runOnFunction(Function &Fn); |
| 96 | }; |
| 97 | } // end anonymous namespace |
| 98 | |
| 99 | char StackProtector::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 100 | INITIALIZE_PASS(StackProtector, "stack-protector", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 101 | "Insert stack protectors", false, false) |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 102 | |
Benjamin Kramer | 69e42db | 2013-01-11 20:05:37 +0000 | [diff] [blame] | 103 | FunctionPass *llvm::createStackProtectorPass(const TargetLoweringBase *tli) { |
Bill Wendling | e9e6bdf | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 104 | return new StackProtector(tli); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | bool StackProtector::runOnFunction(Function &Fn) { |
| 108 | F = &Fn; |
| 109 | M = F->getParent(); |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 110 | DT = getAnalysisIfAvailable<DominatorTree>(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 111 | |
| 112 | if (!RequiresStackProtector()) return false; |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 113 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 114 | ++NumFunProtected; |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 115 | return InsertStackProtectors(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 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 |
| 120 | /// for it. |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 121 | bool StackProtector::ContainsProtectableArray(Type *Ty, bool Strong, |
| 122 | bool InStruct) const { |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 123 | if (!Ty) return false; |
| 124 | if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 125 | // In strong mode any array, regardless of type and size, triggers a |
| 126 | // protector |
| 127 | if (Strong) |
| 128 | return true; |
Chad Rosier | 35907e9 | 2012-08-21 16:15:24 +0000 | [diff] [blame] | 129 | const TargetMachine &TM = TLI->getTargetMachine(); |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 130 | if (!AT->getElementType()->isIntegerTy(8)) { |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 131 | Triple Trip(TM.getTargetTriple()); |
| 132 | |
| 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()) |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | // If an array has more than SSPBufferSize bytes of allocated space, then we |
| 140 | // emit stack protectors. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 141 | if (TM.Options.SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT)) |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | |
| 145 | const StructType *ST = dyn_cast<StructType>(Ty); |
| 146 | if (!ST) return false; |
| 147 | |
| 148 | for (StructType::element_iterator I = ST->element_begin(), |
| 149 | E = ST->element_end(); I != E; ++I) |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 150 | if (ContainsProtectableArray(*I, Strong, true)) |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 151 | return true; |
| 152 | |
| 153 | return false; |
| 154 | } |
| 155 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 156 | bool StackProtector::HasAddressTaken(const Instruction *AI) { |
| 157 | for (Value::const_use_iterator UI = AI->use_begin(), UE = AI->use_end(); |
| 158 | UI != UE; ++UI) { |
| 159 | const User *U = *UI; |
| 160 | if (const StoreInst *SI = dyn_cast<StoreInst>(U)) { |
| 161 | if (AI == SI->getValueOperand()) |
| 162 | return true; |
| 163 | } else if (const PtrToIntInst *SI = dyn_cast<PtrToIntInst>(U)) { |
| 164 | if (AI == SI->getOperand(0)) |
| 165 | return true; |
| 166 | } else if (isa<CallInst>(U)) { |
| 167 | return true; |
| 168 | } else if (isa<InvokeInst>(U)) { |
| 169 | return true; |
| 170 | } else if (const SelectInst *SI = dyn_cast<SelectInst>(U)) { |
| 171 | if (HasAddressTaken(SI)) |
| 172 | return true; |
| 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)) |
| 178 | return true; |
| 179 | } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) { |
| 180 | if (HasAddressTaken(GEP)) |
| 181 | return true; |
| 182 | } else if (const BitCastInst *BI = dyn_cast<BitCastInst>(U)) { |
| 183 | if (HasAddressTaken(BI)) |
| 184 | return true; |
| 185 | } |
| 186 | } |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | /// \brief Check whether or not this function needs a stack protector based |
| 191 | /// upon the stack protector level. |
| 192 | /// |
| 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 |
| 202 | /// address taken. |
| 203 | bool StackProtector::RequiresStackProtector() { |
| 204 | bool Strong = false; |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 205 | if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 206 | Attribute::StackProtectReq)) |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 207 | return true; |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 208 | else if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 209 | Attribute::StackProtectStrong)) |
| 210 | Strong = true; |
| 211 | else if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 212 | Attribute::StackProtect)) |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 213 | return false; |
| 214 | |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 215 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 216 | BasicBlock *BB = I; |
| 217 | |
| 218 | for (BasicBlock::iterator |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 219 | II = BB->begin(), IE = BB->end(); II != IE; ++II) { |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 220 | if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) { |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 221 | if (AI->isArrayAllocation()) { |
| 222 | // SSP-Strong: Enable protectors for any call to alloca, regardless |
| 223 | // of size. |
| 224 | if (Strong) |
| 225 | return true; |
| 226 | |
| 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 |
| 232 | // stack protectors. |
| 233 | return true; |
| 234 | } else // A call to alloca with a variable size requires protectors. |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | if (ContainsProtectableArray(AI->getAllocatedType(), Strong)) |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 239 | return true; |
| 240 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 241 | if (Strong && HasAddressTaken(AI)) { |
| 242 | ++NumAddrTaken; |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 243 | return true; |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 244 | } |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 245 | } |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 246 | } |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | return false; |
| 250 | } |
| 251 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 252 | /// InsertStackProtectors - Insert code into the prologue and epilogue of the |
| 253 | /// function. |
| 254 | /// |
| 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() { |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 259 | BasicBlock *FailBB = 0; // The basic block to jump to if check fails. |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 260 | BasicBlock *FailBBDom = 0; // FailBB's dominator. |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 261 | AllocaInst *AI = 0; // Place on stack that stores the stack guard. |
Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 262 | Value *StackGuardVar = 0; // The stack guard variable. |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 263 | |
Bill Wendling | 7205677 | 2008-11-10 21:13:10 +0000 | [diff] [blame] | 264 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ) { |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 265 | BasicBlock *BB = I++; |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 266 | ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()); |
| 267 | if (!RI) continue; |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 268 | |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 269 | if (!FailBB) { |
| 270 | // Insert code into the entry block that stores the __stack_chk_guard |
| 271 | // variable onto the stack: |
| 272 | // |
| 273 | // entry: |
| 274 | // StackGuardSlot = alloca i8* |
| 275 | // StackGuard = load __stack_chk_guard |
| 276 | // call void @llvm.stackprotect.create(StackGuard, StackGuardSlot) |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 277 | // |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 278 | PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext()); |
Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 279 | unsigned AddressSpace, Offset; |
| 280 | if (TLI->getStackCookieLocation(AddressSpace, Offset)) { |
Chris Lattner | f8bd392 | 2010-07-06 15:59:27 +0000 | [diff] [blame] | 281 | Constant *OffsetVal = |
| 282 | ConstantInt::get(Type::getInt32Ty(RI->getContext()), Offset); |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 283 | |
Chris Lattner | f8bd392 | 2010-07-06 15:59:27 +0000 | [diff] [blame] | 284 | StackGuardVar = ConstantExpr::getIntToPtr(OffsetVal, |
| 285 | PointerType::get(PtrTy, AddressSpace)); |
Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 286 | } else { |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 287 | StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy); |
Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 288 | } |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 289 | |
Bill Wendling | 3c288b9 | 2011-03-29 07:28:52 +0000 | [diff] [blame] | 290 | BasicBlock &Entry = F->getEntryBlock(); |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 291 | Instruction *InsPt = &Entry.front(); |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 292 | |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 293 | AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt); |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 294 | LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt); |
Bill Wendling | 7205677 | 2008-11-10 21:13:10 +0000 | [diff] [blame] | 295 | |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 296 | Value *Args[] = { LI, AI }; |
| 297 | CallInst:: |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 298 | Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector), |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 299 | Args, "", InsPt); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 300 | |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 301 | // Create the basic block to jump to when the guard check fails. |
| 302 | FailBB = CreateFailBB(); |
Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 303 | } |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 304 | |
| 305 | // For each block with a return instruction, convert this: |
| 306 | // |
| 307 | // return: |
| 308 | // ... |
| 309 | // ret ... |
| 310 | // |
| 311 | // into this: |
| 312 | // |
| 313 | // return: |
| 314 | // ... |
| 315 | // %1 = load __stack_chk_guard |
Bill Wendling | 733bbc5 | 2008-11-18 07:30:57 +0000 | [diff] [blame] | 316 | // %2 = load StackGuardSlot |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 317 | // %3 = cmp i1 %1, %2 |
| 318 | // br i1 %3, label %SP_return, label %CallStackCheckFailBlk |
| 319 | // |
| 320 | // SP_return: |
| 321 | // ret ... |
| 322 | // |
| 323 | // CallStackCheckFailBlk: |
| 324 | // call void @__stack_chk_fail() |
| 325 | // unreachable |
| 326 | |
| 327 | // Split the basic block before the return instruction. |
| 328 | BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return"); |
Bill Wendling | 3c288b9 | 2011-03-29 07:28:52 +0000 | [diff] [blame] | 329 | |
Bill Wendling | 3f782f4 | 2011-03-29 17:12:55 +0000 | [diff] [blame] | 330 | if (DT && DT->isReachableFromEntry(BB)) { |
Cameron Zwarich | 53aac15 | 2011-03-11 21:51:56 +0000 | [diff] [blame] | 331 | DT->addNewBlock(NewBB, BB); |
Bill Wendling | 3c288b9 | 2011-03-29 07:28:52 +0000 | [diff] [blame] | 332 | FailBBDom = FailBBDom ? DT->findNearestCommonDominator(FailBBDom, BB) :BB; |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 333 | } |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 334 | |
Bill Wendling | 5601699 | 2009-03-06 01:41:15 +0000 | [diff] [blame] | 335 | // Remove default branch instruction to the new BB. |
| 336 | BB->getTerminator()->eraseFromParent(); |
| 337 | |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 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); |
| 341 | |
| 342 | // Generate the stack protector instructions in the old basic block. |
Bill Wendling | 733bbc5 | 2008-11-18 07:30:57 +0000 | [diff] [blame] | 343 | LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB); |
| 344 | LoadInst *LI2 = new LoadInst(AI, "", true, BB); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 345 | ICmpInst *Cmp = new ICmpInst(*BB, CmpInst::ICMP_EQ, LI1, LI2, ""); |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 346 | BranchInst::Create(NewBB, FailBB, Cmp, BB); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 347 | } |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 348 | |
Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 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; |
| 352 | |
Cameron Zwarich | 53aac15 | 2011-03-11 21:51:56 +0000 | [diff] [blame] | 353 | if (DT && FailBBDom) |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 354 | DT->addNewBlock(FailBB, FailBBDom); |
| 355 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 356 | return true; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /// CreateFailBB - Create a basic block to jump to when the stack protector |
| 360 | /// check fails. |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 361 | BasicBlock *StackProtector::CreateFailBB() { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 362 | BasicBlock *FailBB = BasicBlock::Create(F->getContext(), |
| 363 | "CallStackCheckFailBlk", F); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 364 | Constant *StackChkFail = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 365 | M->getOrInsertFunction("__stack_chk_fail", |
| 366 | Type::getVoidTy(F->getContext()), NULL); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 367 | CallInst::Create(StackChkFail, "", FailBB); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 368 | new UnreachableInst(F->getContext(), FailBB); |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 369 | return FailBB; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 370 | } |