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" |
Michael Gottesman | 3480d1b | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/Analysis.h" |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/Passes.h" |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Triple.h" |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/Dominators.h" |
Michael Gottesman | 3480d1b | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Attributes.h" |
| 26 | #include "llvm/IR/Constants.h" |
| 27 | #include "llvm/IR/DataLayout.h" |
| 28 | #include "llvm/IR/DerivedTypes.h" |
| 29 | #include "llvm/IR/Function.h" |
Rafael Espindola | 62ed8d3 | 2013-06-07 16:35:57 +0000 | [diff] [blame] | 30 | #include "llvm/IR/GlobalValue.h" |
| 31 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Instructions.h" |
Michael Gottesman | 3480d1b | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 33 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Intrinsics.h" |
| 35 | #include "llvm/IR/Module.h" |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 36 | #include "llvm/Pass.h" |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CommandLine.h" |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetLowering.h" |
Bill Wendling | 0dcba2f | 2013-07-22 20:15:21 +0000 | [diff] [blame] | 39 | #include <cstdlib> |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 42 | STATISTIC(NumFunProtected, "Number of functions protected"); |
| 43 | STATISTIC(NumAddrTaken, "Number of local variables that have their address" |
| 44 | " taken."); |
| 45 | |
Michael Gottesman | 3480d1b | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 46 | static cl::opt<bool> |
| 47 | EnableSelectionDAGSP("enable-selectiondag-sp", cl::init(true), |
| 48 | cl::Hidden); |
| 49 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 50 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 51 | class StackProtector : public FunctionPass { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 52 | const TargetMachine *TM; |
| 53 | |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 54 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 55 | /// target type sizes. |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 56 | const TargetLoweringBase *TLI; |
Rafael Espindola | 62ed8d3 | 2013-06-07 16:35:57 +0000 | [diff] [blame] | 57 | const Triple Trip; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 58 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 59 | Function *F; |
| 60 | Module *M; |
| 61 | |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 62 | DominatorTree *DT; |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 63 | |
Bill Wendling | 0dcba2f | 2013-07-22 20:15:21 +0000 | [diff] [blame] | 64 | /// \brief The minimum size of buffers that will receive stack smashing |
| 65 | /// protection when -fstack-protection is used. |
| 66 | unsigned SSPBufferSize; |
| 67 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 68 | /// VisitedPHIs - The set of PHI nodes visited when determining |
| 69 | /// if a variable's reference has been taken. This set |
| 70 | /// is maintained to ensure we don't visit the same PHI node multiple |
| 71 | /// times. |
| 72 | SmallPtrSet<const PHINode*, 16> VisitedPHIs; |
| 73 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 74 | /// InsertStackProtectors - Insert code into the prologue and epilogue of |
| 75 | /// the function. |
| 76 | /// |
| 77 | /// - The prologue code loads and stores the stack guard onto the stack. |
| 78 | /// - The epilogue checks the value stored in the prologue against the |
| 79 | /// original value. It calls __stack_chk_fail if they differ. |
| 80 | bool InsertStackProtectors(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 81 | |
| 82 | /// CreateFailBB - Create a basic block to jump to when the stack protector |
| 83 | /// check fails. |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 84 | BasicBlock *CreateFailBB(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 85 | |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 86 | /// ContainsProtectableArray - Check whether the type either is an array or |
| 87 | /// contains an array of sufficient size so that we need stack protectors |
| 88 | /// for it. |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 89 | bool ContainsProtectableArray(Type *Ty, bool Strong = false, |
| 90 | bool InStruct = false) const; |
| 91 | |
| 92 | /// \brief Check whether a stack allocation has its address taken. |
| 93 | bool HasAddressTaken(const Instruction *AI); |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 94 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 95 | /// RequiresStackProtector - Check whether or not this function needs a |
| 96 | /// stack protector based upon the stack protector level. |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 97 | bool RequiresStackProtector(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 98 | public: |
| 99 | static char ID; // Pass identification, replacement for typeid. |
Bill Wendling | 0dcba2f | 2013-07-22 20:15:21 +0000 | [diff] [blame] | 100 | StackProtector() : FunctionPass(ID), TM(0), TLI(0), SSPBufferSize(0) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 101 | initializeStackProtectorPass(*PassRegistry::getPassRegistry()); |
| 102 | } |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 103 | StackProtector(const TargetMachine *TM) |
Bill Wendling | 0dcba2f | 2013-07-22 20:15:21 +0000 | [diff] [blame] | 104 | : FunctionPass(ID), TM(TM), TLI(0), Trip(TM->getTargetTriple()), |
| 105 | SSPBufferSize(8) { |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 106 | initializeStackProtectorPass(*PassRegistry::getPassRegistry()); |
| 107 | } |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 108 | |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 109 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 110 | AU.addPreserved<DominatorTree>(); |
| 111 | } |
| 112 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 113 | virtual bool runOnFunction(Function &Fn); |
| 114 | }; |
| 115 | } // end anonymous namespace |
| 116 | |
| 117 | char StackProtector::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 118 | INITIALIZE_PASS(StackProtector, "stack-protector", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 119 | "Insert stack protectors", false, false) |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 120 | |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 121 | FunctionPass *llvm::createStackProtectorPass(const TargetMachine *TM) { |
| 122 | return new StackProtector(TM); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | bool StackProtector::runOnFunction(Function &Fn) { |
| 126 | F = &Fn; |
| 127 | M = F->getParent(); |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 128 | DT = getAnalysisIfAvailable<DominatorTree>(); |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 129 | TLI = TM->getTargetLowering(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 130 | |
| 131 | if (!RequiresStackProtector()) return false; |
Bill Wendling | 6d86f3c | 2012-08-13 21:20:43 +0000 | [diff] [blame] | 132 | |
Bill Wendling | 0dcba2f | 2013-07-22 20:15:21 +0000 | [diff] [blame] | 133 | Attribute Attr = |
| 134 | Fn.getAttributes().getAttribute(AttributeSet::FunctionIndex, |
| 135 | "stack-protector-buffer-size"); |
| 136 | if (Attr.isStringAttribute()) |
| 137 | SSPBufferSize = atoi(Attr.getValueAsString().data()); |
| 138 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 139 | ++NumFunProtected; |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 140 | return InsertStackProtectors(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 143 | /// ContainsProtectableArray - Check whether the type either is an array or |
| 144 | /// contains a char array of sufficient size so that we need stack protectors |
| 145 | /// for it. |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 146 | bool StackProtector::ContainsProtectableArray(Type *Ty, bool Strong, |
| 147 | bool InStruct) const { |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 148 | if (!Ty) return false; |
| 149 | if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 150 | // In strong mode any array, regardless of type and size, triggers a |
| 151 | // protector |
| 152 | if (Strong) |
| 153 | return true; |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 154 | if (!AT->getElementType()->isIntegerTy(8)) { |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 155 | // If we're on a non-Darwin platform or we're inside of a structure, don't |
| 156 | // add stack protectors unless the array is a character array. |
| 157 | if (InStruct || !Trip.isOSDarwin()) |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | // If an array has more than SSPBufferSize bytes of allocated space, then we |
| 162 | // emit stack protectors. |
Bill Wendling | 0dcba2f | 2013-07-22 20:15:21 +0000 | [diff] [blame] | 163 | if (SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT)) |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
| 167 | const StructType *ST = dyn_cast<StructType>(Ty); |
| 168 | if (!ST) return false; |
| 169 | |
| 170 | for (StructType::element_iterator I = ST->element_begin(), |
| 171 | E = ST->element_end(); I != E; ++I) |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 172 | if (ContainsProtectableArray(*I, Strong, true)) |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 173 | return true; |
| 174 | |
| 175 | return false; |
| 176 | } |
| 177 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 178 | bool StackProtector::HasAddressTaken(const Instruction *AI) { |
| 179 | for (Value::const_use_iterator UI = AI->use_begin(), UE = AI->use_end(); |
| 180 | UI != UE; ++UI) { |
| 181 | const User *U = *UI; |
| 182 | if (const StoreInst *SI = dyn_cast<StoreInst>(U)) { |
| 183 | if (AI == SI->getValueOperand()) |
| 184 | return true; |
| 185 | } else if (const PtrToIntInst *SI = dyn_cast<PtrToIntInst>(U)) { |
| 186 | if (AI == SI->getOperand(0)) |
| 187 | return true; |
| 188 | } else if (isa<CallInst>(U)) { |
| 189 | return true; |
| 190 | } else if (isa<InvokeInst>(U)) { |
| 191 | return true; |
| 192 | } else if (const SelectInst *SI = dyn_cast<SelectInst>(U)) { |
| 193 | if (HasAddressTaken(SI)) |
| 194 | return true; |
| 195 | } else if (const PHINode *PN = dyn_cast<PHINode>(U)) { |
| 196 | // Keep track of what PHI nodes we have already visited to ensure |
| 197 | // they are only visited once. |
| 198 | if (VisitedPHIs.insert(PN)) |
| 199 | if (HasAddressTaken(PN)) |
| 200 | return true; |
| 201 | } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) { |
| 202 | if (HasAddressTaken(GEP)) |
| 203 | return true; |
| 204 | } else if (const BitCastInst *BI = dyn_cast<BitCastInst>(U)) { |
| 205 | if (HasAddressTaken(BI)) |
| 206 | return true; |
| 207 | } |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | /// \brief Check whether or not this function needs a stack protector based |
| 213 | /// upon the stack protector level. |
| 214 | /// |
| 215 | /// We use two heuristics: a standard (ssp) and strong (sspstrong). |
| 216 | /// The standard heuristic which will add a guard variable to functions that |
| 217 | /// call alloca with a either a variable size or a size >= SSPBufferSize, |
| 218 | /// functions with character buffers larger than SSPBufferSize, and functions |
| 219 | /// with aggregates containing character buffers larger than SSPBufferSize. The |
| 220 | /// strong heuristic will add a guard variables to functions that call alloca |
| 221 | /// regardless of size, functions with any buffer regardless of type and size, |
| 222 | /// functions with aggregates that contain any buffer regardless of type and |
| 223 | /// size, and functions that contain stack-based variables that have had their |
| 224 | /// address taken. |
| 225 | bool StackProtector::RequiresStackProtector() { |
| 226 | bool Strong = false; |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 227 | if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 228 | Attribute::StackProtectReq)) |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 229 | return true; |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 230 | else if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 231 | Attribute::StackProtectStrong)) |
| 232 | Strong = true; |
| 233 | else if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 234 | Attribute::StackProtect)) |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 235 | return false; |
| 236 | |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 237 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 238 | BasicBlock *BB = I; |
| 239 | |
| 240 | for (BasicBlock::iterator |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 241 | II = BB->begin(), IE = BB->end(); II != IE; ++II) { |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 242 | if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) { |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 243 | if (AI->isArrayAllocation()) { |
| 244 | // SSP-Strong: Enable protectors for any call to alloca, regardless |
| 245 | // of size. |
| 246 | if (Strong) |
| 247 | return true; |
| 248 | |
| 249 | if (const ConstantInt *CI = |
| 250 | dyn_cast<ConstantInt>(AI->getArraySize())) { |
Bill Wendling | 0dcba2f | 2013-07-22 20:15:21 +0000 | [diff] [blame] | 251 | if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 252 | // A call to alloca with size >= SSPBufferSize requires |
| 253 | // stack protectors. |
| 254 | return true; |
Bill Wendling | 0dcba2f | 2013-07-22 20:15:21 +0000 | [diff] [blame] | 255 | } else { |
| 256 | // A call to alloca with a variable size requires protectors. |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 257 | return true; |
Bill Wendling | 0dcba2f | 2013-07-22 20:15:21 +0000 | [diff] [blame] | 258 | } |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | if (ContainsProtectableArray(AI->getAllocatedType(), Strong)) |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 262 | return true; |
| 263 | |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 264 | if (Strong && HasAddressTaken(AI)) { |
| 265 | ++NumAddrTaken; |
Bill Wendling | a67eda7 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 266 | return true; |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 267 | } |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 268 | } |
Bill Wendling | e4957fb | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 269 | } |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | return false; |
| 273 | } |
| 274 | |
Michael Gottesman | 3480d1b | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 275 | static bool InstructionWillNotHaveChain(const Instruction *I) { |
| 276 | return !I->mayHaveSideEffects() && !I->mayReadFromMemory() && |
| 277 | isSafeToSpeculativelyExecute(I); |
| 278 | } |
| 279 | |
| 280 | /// Identify if RI has a previous instruction in the "Tail Position" and return |
| 281 | /// it. Otherwise return 0. |
| 282 | /// |
| 283 | /// This is based off of the code in llvm::isInTailCallPosition |
| 284 | static CallInst *FindPotentialTailCall(BasicBlock *BB, ReturnInst *RI, |
| 285 | const TargetLoweringBase *TLI) { |
| 286 | // Establish a reasonable upper bound on the maximum amount of instructions we |
| 287 | // will look through to find a tail call. |
| 288 | unsigned SearchCounter = 0; |
| 289 | const unsigned MaxSearch = 4; |
| 290 | bool NoInterposingChain = true; |
| 291 | |
| 292 | for (BasicBlock::reverse_iterator I = llvm::next(BB->rbegin()), E = BB->rend(); |
| 293 | I != E && SearchCounter < MaxSearch; ++I) { |
| 294 | Instruction *Inst = &*I; |
| 295 | |
| 296 | // Skip over debug intrinsics and do not allow them to affect our MaxSearch |
| 297 | // counter. |
| 298 | if (isa<DbgInfoIntrinsic>(Inst)) |
| 299 | continue; |
| 300 | |
| 301 | // If we find a call and the following conditions are satisifed, then we |
| 302 | // have found a tail call that satisfies at least the target independent |
| 303 | // requirements of a tail call: |
| 304 | // |
| 305 | // 1. The call site has the tail marker. |
| 306 | // |
| 307 | // 2. The call site either will not cause the creation of a chain or if a |
| 308 | // chain is necessary there are no instructions in between the callsite and |
| 309 | // the call which would create an interposing chain. |
| 310 | // |
| 311 | // 3. The return type of the function does not impede tail call |
| 312 | // optimization. |
| 313 | if (CallInst *CI = dyn_cast<CallInst>(Inst)) { |
| 314 | if (CI->isTailCall() && |
| 315 | (InstructionWillNotHaveChain(CI) || NoInterposingChain) && |
| 316 | returnTypeIsEligibleForTailCall(BB->getParent(), CI, RI, *TLI)) |
| 317 | return CI; |
| 318 | } |
| 319 | |
| 320 | // If we did not find a call see if we have an instruction that may create |
| 321 | // an interposing chain. |
| 322 | NoInterposingChain = NoInterposingChain && InstructionWillNotHaveChain(Inst); |
| 323 | |
| 324 | // Increment max search. |
| 325 | SearchCounter++; |
| 326 | } |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
Michael Gottesman | c03d5ec | 2013-07-22 20:44:11 +0000 | [diff] [blame] | 331 | /// Insert code into the entry block that stores the __stack_chk_guard |
| 332 | /// variable onto the stack: |
| 333 | /// |
| 334 | /// entry: |
| 335 | /// StackGuardSlot = alloca i8* |
| 336 | /// StackGuard = load __stack_chk_guard |
| 337 | /// call void @llvm.stackprotect.create(StackGuard, StackGuardSlot) |
| 338 | /// |
Michael Gottesman | 3480d1b | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 339 | /// Returns true if the platform/triple supports the stackprotectorcreate pseudo |
| 340 | /// node. |
| 341 | static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI, |
Michael Gottesman | c03d5ec | 2013-07-22 20:44:11 +0000 | [diff] [blame] | 342 | const TargetLoweringBase *TLI, const Triple &Trip, |
| 343 | AllocaInst *&AI, Value *&StackGuardVar) { |
Michael Gottesman | 3480d1b | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 344 | bool SupportsSelectionDAGSP = false; |
Michael Gottesman | c03d5ec | 2013-07-22 20:44:11 +0000 | [diff] [blame] | 345 | PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext()); |
| 346 | unsigned AddressSpace, Offset; |
| 347 | if (TLI->getStackCookieLocation(AddressSpace, Offset)) { |
| 348 | Constant *OffsetVal = |
| 349 | ConstantInt::get(Type::getInt32Ty(RI->getContext()), Offset); |
| 350 | |
| 351 | StackGuardVar = ConstantExpr::getIntToPtr(OffsetVal, |
| 352 | PointerType::get(PtrTy, |
| 353 | AddressSpace)); |
| 354 | } else if (Trip.getOS() == llvm::Triple::OpenBSD) { |
| 355 | StackGuardVar = M->getOrInsertGlobal("__guard_local", PtrTy); |
| 356 | cast<GlobalValue>(StackGuardVar) |
| 357 | ->setVisibility(GlobalValue::HiddenVisibility); |
| 358 | } else { |
Michael Gottesman | 3480d1b | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 359 | SupportsSelectionDAGSP = true; |
| 360 | StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy); |
Michael Gottesman | c03d5ec | 2013-07-22 20:44:11 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | BasicBlock &Entry = F->getEntryBlock(); |
| 364 | Instruction *InsPt = &Entry.front(); |
| 365 | |
| 366 | AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt); |
| 367 | LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt); |
| 368 | |
| 369 | Value *Args[] = { LI, AI }; |
| 370 | CallInst:: |
| 371 | Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector), |
| 372 | Args, "", InsPt); |
Michael Gottesman | 3480d1b | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 373 | |
| 374 | return SupportsSelectionDAGSP; |
Michael Gottesman | c03d5ec | 2013-07-22 20:44:11 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 377 | /// InsertStackProtectors - Insert code into the prologue and epilogue of the |
| 378 | /// function. |
| 379 | /// |
| 380 | /// - The prologue code loads and stores the stack guard onto the stack. |
| 381 | /// - The epilogue checks the value stored in the prologue against the original |
| 382 | /// value. It calls __stack_chk_fail if they differ. |
| 383 | bool StackProtector::InsertStackProtectors() { |
Michael Gottesman | 236e389 | 2013-08-09 21:26:18 +0000 | [diff] [blame] | 384 | bool HasPrologue = false; |
Michael Gottesman | 3480d1b | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 385 | bool SupportsSelectionDAGSP = false; |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 386 | AllocaInst *AI = 0; // Place on stack that stores the stack guard. |
Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 387 | Value *StackGuardVar = 0; // The stack guard variable. |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 388 | |
Bill Wendling | 7205677 | 2008-11-10 21:13:10 +0000 | [diff] [blame] | 389 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ) { |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 390 | BasicBlock *BB = I++; |
Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 391 | ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()); |
| 392 | if (!RI) continue; |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 393 | |
Michael Gottesman | 236e389 | 2013-08-09 21:26:18 +0000 | [diff] [blame] | 394 | if (!HasPrologue) { |
| 395 | HasPrologue = true; |
Michael Gottesman | 3480d1b | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 396 | SupportsSelectionDAGSP = CreatePrologue(F, M, RI, TLI, Trip, AI, |
| 397 | StackGuardVar); |
| 398 | } |
| 399 | |
Michael Gottesman | 3480d1b | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 400 | if (EnableSelectionDAGSP && !TM->Options.EnableFastISel && |
| 401 | SupportsSelectionDAGSP) { |
| 402 | // Since we have a potential tail call, insert the special stack check |
| 403 | // intrinsic. |
| 404 | Instruction *InsertionPt = 0; |
| 405 | if (CallInst *CI = FindPotentialTailCall(BB, RI, TLI)) { |
| 406 | InsertionPt = CI; |
| 407 | } else { |
| 408 | InsertionPt = RI; |
| 409 | // At this point we know that BB has a return statement so it *DOES* |
| 410 | // have a terminator. |
| 411 | assert(InsertionPt != 0 && "BB must have a terminator instruction at " |
| 412 | "this point."); |
| 413 | } |
| 414 | |
| 415 | Function *Intrinsic = |
| 416 | Intrinsic::getDeclaration(M, Intrinsic::stackprotectorcheck); |
| 417 | Value *Args[] = { StackGuardVar }; |
| 418 | CallInst::Create(Intrinsic, Args, "", InsertionPt); |
| 419 | |
| 420 | } else { |
Michael Gottesman | 47d6e07 | 2013-08-20 08:46:13 +0000 | [diff] [blame^] | 421 | // If we do not support SelectionDAG based tail calls, generate IR level |
| 422 | // tail calls. |
| 423 | // |
Michael Gottesman | 3480d1b | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 424 | // For each block with a return instruction, convert this: |
| 425 | // |
| 426 | // return: |
| 427 | // ... |
| 428 | // ret ... |
| 429 | // |
| 430 | // into this: |
| 431 | // |
| 432 | // return: |
| 433 | // ... |
| 434 | // %1 = load __stack_chk_guard |
| 435 | // %2 = load StackGuardSlot |
| 436 | // %3 = cmp i1 %1, %2 |
| 437 | // br i1 %3, label %SP_return, label %CallStackCheckFailBlk |
| 438 | // |
| 439 | // SP_return: |
| 440 | // ret ... |
| 441 | // |
| 442 | // CallStackCheckFailBlk: |
| 443 | // call void @__stack_chk_fail() |
| 444 | // unreachable |
| 445 | |
| 446 | // Create the FailBB. We duplicate the BB every time since the MI tail |
| 447 | // merge pass will merge together all of the various BB into one including |
| 448 | // fail BB generated by the stack protector pseudo instruction. |
| 449 | BasicBlock *FailBB = CreateFailBB(); |
| 450 | |
| 451 | // Split the basic block before the return instruction. |
| 452 | BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return"); |
| 453 | |
| 454 | // Update the dominator tree if we need to. |
| 455 | if (DT && DT->isReachableFromEntry(BB)) { |
| 456 | DT->addNewBlock(NewBB, BB); |
| 457 | DT->addNewBlock(FailBB, BB); |
| 458 | } |
| 459 | |
| 460 | // Remove default branch instruction to the new BB. |
| 461 | BB->getTerminator()->eraseFromParent(); |
| 462 | |
| 463 | // Move the newly created basic block to the point right after the old |
| 464 | // basic block so that it's in the "fall through" position. |
| 465 | NewBB->moveAfter(BB); |
| 466 | |
| 467 | // Generate the stack protector instructions in the old basic block. |
| 468 | LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB); |
| 469 | LoadInst *LI2 = new LoadInst(AI, "", true, BB); |
| 470 | ICmpInst *Cmp = new ICmpInst(*BB, CmpInst::ICMP_EQ, LI1, LI2, ""); |
| 471 | BranchInst::Create(NewBB, FailBB, Cmp, BB); |
Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 472 | } |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 473 | } |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 474 | |
Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 475 | // Return if we didn't modify any basic blocks. I.e., there are no return |
| 476 | // statements in the function. |
Michael Gottesman | 236e389 | 2013-08-09 21:26:18 +0000 | [diff] [blame] | 477 | if (!HasPrologue) |
| 478 | return false; |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 479 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 480 | return true; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /// CreateFailBB - Create a basic block to jump to when the stack protector |
| 484 | /// check fails. |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 485 | BasicBlock *StackProtector::CreateFailBB() { |
Rafael Espindola | 62ed8d3 | 2013-06-07 16:35:57 +0000 | [diff] [blame] | 486 | LLVMContext &Context = F->getContext(); |
| 487 | BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F); |
| 488 | if (Trip.getOS() == llvm::Triple::OpenBSD) { |
| 489 | Constant *StackChkFail = M->getOrInsertFunction( |
| 490 | "__stack_smash_handler", Type::getVoidTy(Context), |
| 491 | Type::getInt8PtrTy(Context), NULL); |
| 492 | |
| 493 | Constant *NameStr = ConstantDataArray::getString(Context, F->getName()); |
| 494 | Constant *FuncName = |
| 495 | new GlobalVariable(*M, NameStr->getType(), true, |
| 496 | GlobalVariable::PrivateLinkage, NameStr, "SSH"); |
| 497 | |
| 498 | SmallVector<Constant *, 2> IdxList; |
| 499 | IdxList.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0)); |
| 500 | IdxList.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0)); |
| 501 | |
| 502 | SmallVector<Value *, 1> Args; |
| 503 | Args.push_back(ConstantExpr::getGetElementPtr(FuncName, IdxList)); |
| 504 | |
| 505 | CallInst::Create(StackChkFail, Args, "", FailBB); |
| 506 | } else { |
| 507 | Constant *StackChkFail = M->getOrInsertFunction( |
| 508 | "__stack_chk_fail", Type::getVoidTy(Context), NULL); |
| 509 | CallInst::Create(StackChkFail, "", FailBB); |
| 510 | } |
| 511 | new UnreachableInst(Context, FailBB); |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 512 | return FailBB; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 513 | } |