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