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" |
Rafael Espindola | 62ed8d3 | 2013-06-07 16:35:57 +0000 | [diff] [blame] | 28 | #include "llvm/IR/GlobalValue.h" |
| 29 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Instructions.h" |
| 31 | #include "llvm/IR/Intrinsics.h" |
| 32 | #include "llvm/IR/Module.h" |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 33 | #include "llvm/Pass.h" |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetLowering.h" |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 38 | STATISTIC(NumFunProtected, "Number of functions protected"); |
| 39 | STATISTIC(NumAddrTaken, "Number of local variables that have their address" |
| 40 | " taken."); |
| 41 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 42 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 43 | class StackProtector : public FunctionPass { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame^] | 44 | const TargetMachine *TM; |
| 45 | |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 46 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 47 | /// target type sizes. |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame^] | 48 | const TargetLoweringBase *TLI; |
Rafael Espindola | 62ed8d3 | 2013-06-07 16:35:57 +0000 | [diff] [blame] | 49 | const Triple Trip; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 50 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 51 | Function *F; |
| 52 | Module *M; |
| 53 | |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 54 | DominatorTree *DT; |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 55 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 56 | /// VisitedPHIs - The set of PHI nodes visited when determining |
| 57 | /// if a variable's reference has been taken. This set |
| 58 | /// is maintained to ensure we don't visit the same PHI node multiple |
| 59 | /// times. |
| 60 | SmallPtrSet<const PHINode*, 16> VisitedPHIs; |
| 61 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 62 | /// InsertStackProtectors - Insert code into the prologue and epilogue of |
| 63 | /// the function. |
| 64 | /// |
| 65 | /// - The prologue code loads and stores the stack guard onto the stack. |
| 66 | /// - The epilogue checks the value stored in the prologue against the |
| 67 | /// original value. It calls __stack_chk_fail if they differ. |
| 68 | bool InsertStackProtectors(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 69 | |
| 70 | /// CreateFailBB - Create a basic block to jump to when the stack protector |
| 71 | /// check fails. |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 72 | BasicBlock *CreateFailBB(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 73 | |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 74 | /// ContainsProtectableArray - Check whether the type either is an array or |
| 75 | /// contains an array of sufficient size so that we need stack protectors |
| 76 | /// for it. |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 77 | bool ContainsProtectableArray(Type *Ty, bool Strong = false, |
| 78 | bool InStruct = false) const; |
| 79 | |
| 80 | /// \brief Check whether a stack allocation has its address taken. |
| 81 | bool HasAddressTaken(const Instruction *AI); |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 82 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 83 | /// RequiresStackProtector - Check whether or not this function needs a |
| 84 | /// stack protector based upon the stack protector level. |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 85 | bool RequiresStackProtector(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 86 | public: |
| 87 | static char ID; // Pass identification, replacement for typeid. |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame^] | 88 | StackProtector() : FunctionPass(ID), TM(0), TLI(0) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 89 | initializeStackProtectorPass(*PassRegistry::getPassRegistry()); |
| 90 | } |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame^] | 91 | StackProtector(const TargetMachine *TM) |
| 92 | : FunctionPass(ID), TM(TM), TLI(0), Trip(TM->getTargetTriple()) { |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 93 | initializeStackProtectorPass(*PassRegistry::getPassRegistry()); |
| 94 | } |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 95 | |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 96 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 97 | AU.addPreserved<DominatorTree>(); |
| 98 | } |
| 99 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 100 | virtual bool runOnFunction(Function &Fn); |
| 101 | }; |
| 102 | } // end anonymous namespace |
| 103 | |
| 104 | char StackProtector::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 105 | INITIALIZE_PASS(StackProtector, "stack-protector", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 106 | "Insert stack protectors", false, false) |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 107 | |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame^] | 108 | FunctionPass *llvm::createStackProtectorPass(const TargetMachine *TM) { |
| 109 | return new StackProtector(TM); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | bool StackProtector::runOnFunction(Function &Fn) { |
| 113 | F = &Fn; |
| 114 | M = F->getParent(); |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 115 | DT = getAnalysisIfAvailable<DominatorTree>(); |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame^] | 116 | TLI = TM->getTargetLowering(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 117 | |
| 118 | if (!RequiresStackProtector()) return false; |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 119 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 120 | ++NumFunProtected; |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 121 | return InsertStackProtectors(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 124 | /// ContainsProtectableArray - Check whether the type either is an array or |
| 125 | /// contains a char array of sufficient size so that we need stack protectors |
| 126 | /// for it. |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 127 | bool StackProtector::ContainsProtectableArray(Type *Ty, bool Strong, |
| 128 | bool InStruct) const { |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 129 | if (!Ty) return false; |
| 130 | if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 131 | // In strong mode any array, regardless of type and size, triggers a |
| 132 | // protector |
| 133 | if (Strong) |
| 134 | return true; |
Chad Rosier | 35907e9 | 2012-08-21 16:15:24 +0000 | [diff] [blame] | 135 | const TargetMachine &TM = TLI->getTargetMachine(); |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 136 | if (!AT->getElementType()->isIntegerTy(8)) { |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 137 | // If we're on a non-Darwin platform or we're inside of a structure, don't |
| 138 | // add stack protectors unless the array is a character array. |
| 139 | if (InStruct || !Trip.isOSDarwin()) |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | // If an array has more than SSPBufferSize bytes of allocated space, then we |
| 144 | // emit stack protectors. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 145 | if (TM.Options.SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT)) |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | |
| 149 | const StructType *ST = dyn_cast<StructType>(Ty); |
| 150 | if (!ST) return false; |
| 151 | |
| 152 | for (StructType::element_iterator I = ST->element_begin(), |
| 153 | E = ST->element_end(); I != E; ++I) |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 154 | if (ContainsProtectableArray(*I, Strong, true)) |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 155 | return true; |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 160 | bool StackProtector::HasAddressTaken(const Instruction *AI) { |
| 161 | for (Value::const_use_iterator UI = AI->use_begin(), UE = AI->use_end(); |
| 162 | UI != UE; ++UI) { |
| 163 | const User *U = *UI; |
| 164 | if (const StoreInst *SI = dyn_cast<StoreInst>(U)) { |
| 165 | if (AI == SI->getValueOperand()) |
| 166 | return true; |
| 167 | } else if (const PtrToIntInst *SI = dyn_cast<PtrToIntInst>(U)) { |
| 168 | if (AI == SI->getOperand(0)) |
| 169 | return true; |
| 170 | } else if (isa<CallInst>(U)) { |
| 171 | return true; |
| 172 | } else if (isa<InvokeInst>(U)) { |
| 173 | return true; |
| 174 | } else if (const SelectInst *SI = dyn_cast<SelectInst>(U)) { |
| 175 | if (HasAddressTaken(SI)) |
| 176 | return true; |
| 177 | } else if (const PHINode *PN = dyn_cast<PHINode>(U)) { |
| 178 | // Keep track of what PHI nodes we have already visited to ensure |
| 179 | // they are only visited once. |
| 180 | if (VisitedPHIs.insert(PN)) |
| 181 | if (HasAddressTaken(PN)) |
| 182 | return true; |
| 183 | } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) { |
| 184 | if (HasAddressTaken(GEP)) |
| 185 | return true; |
| 186 | } else if (const BitCastInst *BI = dyn_cast<BitCastInst>(U)) { |
| 187 | if (HasAddressTaken(BI)) |
| 188 | return true; |
| 189 | } |
| 190 | } |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | /// \brief Check whether or not this function needs a stack protector based |
| 195 | /// upon the stack protector level. |
| 196 | /// |
| 197 | /// We use two heuristics: a standard (ssp) and strong (sspstrong). |
| 198 | /// The standard heuristic which will add a guard variable to functions that |
| 199 | /// call alloca with a either a variable size or a size >= SSPBufferSize, |
| 200 | /// functions with character buffers larger than SSPBufferSize, and functions |
| 201 | /// with aggregates containing character buffers larger than SSPBufferSize. The |
| 202 | /// strong heuristic will add a guard variables to functions that call alloca |
| 203 | /// regardless of size, functions with any buffer regardless of type and size, |
| 204 | /// functions with aggregates that contain any buffer regardless of type and |
| 205 | /// size, and functions that contain stack-based variables that have had their |
| 206 | /// address taken. |
| 207 | bool StackProtector::RequiresStackProtector() { |
| 208 | bool Strong = false; |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 209 | if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 210 | Attribute::StackProtectReq)) |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 211 | return true; |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 212 | else if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 213 | Attribute::StackProtectStrong)) |
| 214 | Strong = true; |
| 215 | else if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 216 | Attribute::StackProtect)) |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 217 | return false; |
| 218 | |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 219 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 220 | BasicBlock *BB = I; |
| 221 | |
| 222 | for (BasicBlock::iterator |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 223 | II = BB->begin(), IE = BB->end(); II != IE; ++II) { |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 224 | if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) { |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 225 | if (AI->isArrayAllocation()) { |
| 226 | // SSP-Strong: Enable protectors for any call to alloca, regardless |
| 227 | // of size. |
| 228 | if (Strong) |
| 229 | return true; |
| 230 | |
| 231 | if (const ConstantInt *CI = |
| 232 | dyn_cast<ConstantInt>(AI->getArraySize())) { |
| 233 | unsigned BufferSize = TLI->getTargetMachine().Options.SSPBufferSize; |
| 234 | if (CI->getLimitedValue(BufferSize) >= BufferSize) |
| 235 | // A call to alloca with size >= SSPBufferSize requires |
| 236 | // stack protectors. |
| 237 | return true; |
| 238 | } else // A call to alloca with a variable size requires protectors. |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | if (ContainsProtectableArray(AI->getAllocatedType(), Strong)) |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 243 | return true; |
| 244 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 245 | if (Strong && HasAddressTaken(AI)) { |
| 246 | ++NumAddrTaken; |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 247 | return true; |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 248 | } |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 249 | } |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 250 | } |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | return false; |
| 254 | } |
| 255 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 256 | /// InsertStackProtectors - Insert code into the prologue and epilogue of the |
| 257 | /// function. |
| 258 | /// |
| 259 | /// - The prologue code loads and stores the stack guard onto the stack. |
| 260 | /// - The epilogue checks the value stored in the prologue against the original |
| 261 | /// value. It calls __stack_chk_fail if they differ. |
| 262 | bool StackProtector::InsertStackProtectors() { |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 263 | BasicBlock *FailBB = 0; // The basic block to jump to if check fails. |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 264 | BasicBlock *FailBBDom = 0; // FailBB's dominator. |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 265 | AllocaInst *AI = 0; // Place on stack that stores the stack guard. |
Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 266 | Value *StackGuardVar = 0; // The stack guard variable. |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 267 | |
Bill Wendling | 7205677 | 2008-11-10 21:13:10 +0000 | [diff] [blame] | 268 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ) { |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 269 | BasicBlock *BB = I++; |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 270 | ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()); |
| 271 | if (!RI) continue; |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 272 | |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 273 | if (!FailBB) { |
| 274 | // Insert code into the entry block that stores the __stack_chk_guard |
| 275 | // variable onto the stack: |
| 276 | // |
| 277 | // entry: |
| 278 | // StackGuardSlot = alloca i8* |
| 279 | // StackGuard = load __stack_chk_guard |
| 280 | // call void @llvm.stackprotect.create(StackGuard, StackGuardSlot) |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 281 | // |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 282 | PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext()); |
Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 283 | unsigned AddressSpace, Offset; |
| 284 | if (TLI->getStackCookieLocation(AddressSpace, Offset)) { |
Chris Lattner | f8bd392 | 2010-07-06 15:59:27 +0000 | [diff] [blame] | 285 | Constant *OffsetVal = |
| 286 | ConstantInt::get(Type::getInt32Ty(RI->getContext()), Offset); |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 287 | |
Chris Lattner | f8bd392 | 2010-07-06 15:59:27 +0000 | [diff] [blame] | 288 | StackGuardVar = ConstantExpr::getIntToPtr(OffsetVal, |
| 289 | PointerType::get(PtrTy, AddressSpace)); |
Rafael Espindola | 62ed8d3 | 2013-06-07 16:35:57 +0000 | [diff] [blame] | 290 | } else if (Trip.getOS() == llvm::Triple::OpenBSD) { |
| 291 | StackGuardVar = M->getOrInsertGlobal("__guard_local", PtrTy); |
| 292 | cast<GlobalValue>(StackGuardVar) |
| 293 | ->setVisibility(GlobalValue::HiddenVisibility); |
Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 294 | } else { |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 295 | StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy); |
Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 296 | } |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 297 | |
Bill Wendling | 3c288b9 | 2011-03-29 07:28:52 +0000 | [diff] [blame] | 298 | BasicBlock &Entry = F->getEntryBlock(); |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 299 | Instruction *InsPt = &Entry.front(); |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 300 | |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 301 | AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt); |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 302 | LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt); |
Bill Wendling | 7205677 | 2008-11-10 21:13:10 +0000 | [diff] [blame] | 303 | |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 304 | Value *Args[] = { LI, AI }; |
| 305 | CallInst:: |
Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 306 | Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector), |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 307 | Args, "", InsPt); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 308 | |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 309 | // Create the basic block to jump to when the guard check fails. |
| 310 | FailBB = CreateFailBB(); |
Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 311 | } |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 312 | |
| 313 | // For each block with a return instruction, convert this: |
| 314 | // |
| 315 | // return: |
| 316 | // ... |
| 317 | // ret ... |
| 318 | // |
| 319 | // into this: |
| 320 | // |
| 321 | // return: |
| 322 | // ... |
| 323 | // %1 = load __stack_chk_guard |
Bill Wendling | 733bbc5 | 2008-11-18 07:30:57 +0000 | [diff] [blame] | 324 | // %2 = load StackGuardSlot |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 325 | // %3 = cmp i1 %1, %2 |
| 326 | // br i1 %3, label %SP_return, label %CallStackCheckFailBlk |
| 327 | // |
| 328 | // SP_return: |
| 329 | // ret ... |
| 330 | // |
| 331 | // CallStackCheckFailBlk: |
| 332 | // call void @__stack_chk_fail() |
| 333 | // unreachable |
| 334 | |
| 335 | // Split the basic block before the return instruction. |
| 336 | BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return"); |
Bill Wendling | 3c288b9 | 2011-03-29 07:28:52 +0000 | [diff] [blame] | 337 | |
Bill Wendling | 3f782f4 | 2011-03-29 17:12:55 +0000 | [diff] [blame] | 338 | if (DT && DT->isReachableFromEntry(BB)) { |
Cameron Zwarich | 53aac15 | 2011-03-11 21:51:56 +0000 | [diff] [blame] | 339 | DT->addNewBlock(NewBB, BB); |
Bill Wendling | 3c288b9 | 2011-03-29 07:28:52 +0000 | [diff] [blame] | 340 | FailBBDom = FailBBDom ? DT->findNearestCommonDominator(FailBBDom, BB) :BB; |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 341 | } |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 342 | |
Bill Wendling | 5601699 | 2009-03-06 01:41:15 +0000 | [diff] [blame] | 343 | // Remove default branch instruction to the new BB. |
| 344 | BB->getTerminator()->eraseFromParent(); |
| 345 | |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 346 | // Move the newly created basic block to the point right after the old basic |
| 347 | // block so that it's in the "fall through" position. |
| 348 | NewBB->moveAfter(BB); |
| 349 | |
| 350 | // Generate the stack protector instructions in the old basic block. |
Bill Wendling | 733bbc5 | 2008-11-18 07:30:57 +0000 | [diff] [blame] | 351 | LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB); |
| 352 | LoadInst *LI2 = new LoadInst(AI, "", true, BB); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 353 | ICmpInst *Cmp = new ICmpInst(*BB, CmpInst::ICMP_EQ, LI1, LI2, ""); |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 354 | BranchInst::Create(NewBB, FailBB, Cmp, BB); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 355 | } |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 356 | |
Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 357 | // Return if we didn't modify any basic blocks. I.e., there are no return |
| 358 | // statements in the function. |
| 359 | if (!FailBB) return false; |
| 360 | |
Cameron Zwarich | 53aac15 | 2011-03-11 21:51:56 +0000 | [diff] [blame] | 361 | if (DT && FailBBDom) |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 362 | DT->addNewBlock(FailBB, FailBBDom); |
| 363 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 364 | return true; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /// CreateFailBB - Create a basic block to jump to when the stack protector |
| 368 | /// check fails. |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 369 | BasicBlock *StackProtector::CreateFailBB() { |
Rafael Espindola | 62ed8d3 | 2013-06-07 16:35:57 +0000 | [diff] [blame] | 370 | LLVMContext &Context = F->getContext(); |
| 371 | BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F); |
| 372 | if (Trip.getOS() == llvm::Triple::OpenBSD) { |
| 373 | Constant *StackChkFail = M->getOrInsertFunction( |
| 374 | "__stack_smash_handler", Type::getVoidTy(Context), |
| 375 | Type::getInt8PtrTy(Context), NULL); |
| 376 | |
| 377 | Constant *NameStr = ConstantDataArray::getString(Context, F->getName()); |
| 378 | Constant *FuncName = |
| 379 | new GlobalVariable(*M, NameStr->getType(), true, |
| 380 | GlobalVariable::PrivateLinkage, NameStr, "SSH"); |
| 381 | |
| 382 | SmallVector<Constant *, 2> IdxList; |
| 383 | IdxList.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0)); |
| 384 | IdxList.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0)); |
| 385 | |
| 386 | SmallVector<Value *, 1> Args; |
| 387 | Args.push_back(ConstantExpr::getGetElementPtr(FuncName, IdxList)); |
| 388 | |
| 389 | CallInst::Create(StackChkFail, Args, "", FailBB); |
| 390 | } else { |
| 391 | Constant *StackChkFail = M->getOrInsertFunction( |
| 392 | "__stack_chk_fail", Type::getVoidTy(Context), NULL); |
| 393 | CallInst::Create(StackChkFail, "", FailBB); |
| 394 | } |
| 395 | new UnreachableInst(Context, FailBB); |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 396 | return FailBB; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 397 | } |