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