Bill Wendling | 05d8417 | 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 | 64adc71 | 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 | 05d8417 | 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 | 8ecfb52 | 2013-09-27 21:58:43 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/StackProtector.h" |
Bill Wendling | 7c8f96a | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallPtrSet.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
Akira Hatanaka | b9991a2 | 2014-12-01 04:27:03 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/Analysis.h" |
| 23 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | 9fb823b | 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 | aad6c24 | 2013-06-07 16:35:57 +0000 | [diff] [blame] | 29 | #include "llvm/IR/GlobalValue.h" |
| 30 | #include "llvm/IR/GlobalVariable.h" |
Benjamin Kramer | d93817f | 2013-09-09 17:38:01 +0000 | [diff] [blame] | 31 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Instructions.h" |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 33 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Intrinsics.h" |
Akira Hatanaka | b9991a2 | 2014-12-01 04:27:03 +0000 | [diff] [blame] | 35 | #include "llvm/IR/MDBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Module.h" |
Bill Wendling | 05d8417 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CommandLine.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetSubtargetInfo.h" |
Bill Wendling | c02a0aa | 2013-07-22 20:15:21 +0000 | [diff] [blame] | 39 | #include <cstdlib> |
Bill Wendling | 05d8417 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 42 | #define DEBUG_TYPE "stack-protector" |
| 43 | |
Bill Wendling | 7c8f96a | 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 | 7245f1d | 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 | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 50 | |
Bill Wendling | 05d8417 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 51 | char StackProtector::ID = 0; |
Josh Magee | 7245f1d | 2013-10-30 02:25:14 +0000 | [diff] [blame] | 52 | INITIALIZE_PASS(StackProtector, "stack-protector", "Insert stack protectors", |
| 53 | false, true) |
Bill Wendling | 05d8417 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 54 | |
Bill Wendling | afc1036 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 55 | FunctionPass *llvm::createStackProtectorPass(const TargetMachine *TM) { |
| 56 | return new StackProtector(TM); |
Bill Wendling | 05d8417 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Josh Magee | 7245f1d | 2013-10-30 02:25:14 +0000 | [diff] [blame] | 59 | StackProtector::SSPLayoutKind |
| 60 | StackProtector::getSSPLayout(const AllocaInst *AI) const { |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 61 | return AI ? Layout.lookup(AI) : SSPLK_None; |
| 62 | } |
| 63 | |
Hal Finkel | a69e5b8 | 2014-01-20 19:49:14 +0000 | [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 | 05d8417 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 85 | bool StackProtector::runOnFunction(Function &Fn) { |
| 86 | F = &Fn; |
| 87 | M = F->getParent(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 88 | DominatorTreeWrapperPass *DTWP = |
| 89 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 90 | DT = DTWP ? &DTWP->getDomTree() : nullptr; |
Eric Christopher | 3372620 | 2015-01-27 08:48:42 +0000 | [diff] [blame] | 91 | TLI = TM->getSubtargetImpl(Fn)->getTargetLowering(); |
Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 92 | HasPrologue = false; |
| 93 | HasIRCheck = false; |
Bill Wendling | 05d8417 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 94 | |
Duncan P. N. Exon Smith | 70eb9c5 | 2015-02-14 01:44:41 +0000 | [diff] [blame] | 95 | Attribute Attr = Fn.getFnAttribute("stack-protector-buffer-size"); |
Renato Golin | e195f9c | 2014-01-21 10:24:35 +0000 | [diff] [blame] | 96 | if (Attr.isStringAttribute() && |
| 97 | Attr.getValueAsString().getAsInteger(10, SSPBufferSize)) |
| 98 | return false; // Invalid integer string |
Bill Wendling | c02a0aa | 2013-07-22 20:15:21 +0000 | [diff] [blame] | 99 | |
Josh Magee | adfde5f | 2014-04-17 19:08:36 +0000 | [diff] [blame] | 100 | if (!RequiresStackProtector()) |
| 101 | return false; |
| 102 | |
Bill Wendling | 7c8f96a | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 103 | ++NumFunProtected; |
Bill Wendling | 782e834 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 104 | return InsertStackProtectors(); |
Bill Wendling | 05d8417 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 107 | /// \param [out] IsLarge is set to true if a protectable array is found and |
| 108 | /// it is "large" ( >= ssp-buffer-size). In the case of a structure with |
| 109 | /// multiple arrays, this gets set if any of them is large. |
| 110 | bool StackProtector::ContainsProtectableArray(Type *Ty, bool &IsLarge, |
Josh Magee | 7245f1d | 2013-10-30 02:25:14 +0000 | [diff] [blame] | 111 | bool Strong, |
| 112 | bool InStruct) const { |
| 113 | if (!Ty) |
| 114 | return false; |
Bill Wendling | bfb9b75 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 115 | if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
| 116 | if (!AT->getElementType()->isIntegerTy(8)) { |
Bill Wendling | bfb9b75 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 117 | // If we're on a non-Darwin platform or we're inside of a structure, don't |
| 118 | // add stack protectors unless the array is a character array. |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 119 | // However, in strong mode any array, regardless of type and size, |
| 120 | // triggers a protector. |
| 121 | if (!Strong && (InStruct || !Trip.isOSDarwin())) |
| 122 | return false; |
Bill Wendling | bfb9b75 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // If an array has more than SSPBufferSize bytes of allocated space, then we |
| 126 | // emit stack protectors. |
Mehdi Amini | ed6edbf | 2015-07-07 23:38:49 +0000 | [diff] [blame] | 127 | if (SSPBufferSize <= M->getDataLayout().getTypeAllocSize(AT)) { |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 128 | IsLarge = true; |
| 129 | return true; |
Josh Magee | 7245f1d | 2013-10-30 02:25:14 +0000 | [diff] [blame] | 130 | } |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 131 | |
| 132 | if (Strong) |
| 133 | // Require a protector for all arrays in strong mode |
Bill Wendling | bfb9b75 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | |
| 137 | const StructType *ST = dyn_cast<StructType>(Ty); |
Josh Magee | 7245f1d | 2013-10-30 02:25:14 +0000 | [diff] [blame] | 138 | if (!ST) |
| 139 | return false; |
Bill Wendling | bfb9b75 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 140 | |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 141 | bool NeedsProtector = false; |
Bill Wendling | bfb9b75 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 142 | for (StructType::element_iterator I = ST->element_begin(), |
Josh Magee | 7245f1d | 2013-10-30 02:25:14 +0000 | [diff] [blame] | 143 | E = ST->element_end(); |
| 144 | I != E; ++I) |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 145 | if (ContainsProtectableArray(*I, IsLarge, Strong, true)) { |
| 146 | // If the element is a protectable array and is large (>= SSPBufferSize) |
| 147 | // then we are done. If the protectable array is not large, then |
| 148 | // keep looking in case a subsequent element is a large array. |
| 149 | if (IsLarge) |
| 150 | return true; |
| 151 | NeedsProtector = true; |
| 152 | } |
Bill Wendling | bfb9b75 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 153 | |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 154 | return NeedsProtector; |
Bill Wendling | bfb9b75 | 2012-08-17 20:59:56 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Bill Wendling | 7c8f96a | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 157 | bool StackProtector::HasAddressTaken(const Instruction *AI) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 158 | for (const User *U : AI->users()) { |
Bill Wendling | 7c8f96a | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 159 | if (const StoreInst *SI = dyn_cast<StoreInst>(U)) { |
| 160 | if (AI == SI->getValueOperand()) |
| 161 | return true; |
| 162 | } else if (const PtrToIntInst *SI = dyn_cast<PtrToIntInst>(U)) { |
| 163 | if (AI == SI->getOperand(0)) |
| 164 | return true; |
| 165 | } else if (isa<CallInst>(U)) { |
| 166 | return true; |
| 167 | } else if (isa<InvokeInst>(U)) { |
| 168 | return true; |
| 169 | } else if (const SelectInst *SI = dyn_cast<SelectInst>(U)) { |
| 170 | if (HasAddressTaken(SI)) |
| 171 | return true; |
| 172 | } else if (const PHINode *PN = dyn_cast<PHINode>(U)) { |
| 173 | // Keep track of what PHI nodes we have already visited to ensure |
| 174 | // they are only visited once. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 175 | if (VisitedPHIs.insert(PN).second) |
Bill Wendling | 7c8f96a | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 176 | if (HasAddressTaken(PN)) |
| 177 | return true; |
| 178 | } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) { |
| 179 | if (HasAddressTaken(GEP)) |
| 180 | return true; |
| 181 | } else if (const BitCastInst *BI = dyn_cast<BitCastInst>(U)) { |
| 182 | if (HasAddressTaken(BI)) |
| 183 | return true; |
| 184 | } |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | /// \brief Check whether or not this function needs a stack protector based |
| 190 | /// upon the stack protector level. |
| 191 | /// |
| 192 | /// We use two heuristics: a standard (ssp) and strong (sspstrong). |
| 193 | /// The standard heuristic which will add a guard variable to functions that |
| 194 | /// call alloca with a either a variable size or a size >= SSPBufferSize, |
| 195 | /// functions with character buffers larger than SSPBufferSize, and functions |
| 196 | /// with aggregates containing character buffers larger than SSPBufferSize. The |
| 197 | /// strong heuristic will add a guard variables to functions that call alloca |
| 198 | /// regardless of size, functions with any buffer regardless of type and size, |
| 199 | /// functions with aggregates that contain any buffer regardless of type and |
| 200 | /// size, and functions that contain stack-based variables that have had their |
| 201 | /// address taken. |
| 202 | bool StackProtector::RequiresStackProtector() { |
| 203 | bool Strong = false; |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 204 | bool NeedsProtector = false; |
Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 205 | for (const BasicBlock &BB : *F) |
| 206 | for (const Instruction &I : BB) |
| 207 | if (const CallInst *CI = dyn_cast<CallInst>(&I)) |
| 208 | if (CI->getCalledFunction() == |
| 209 | Intrinsic::getDeclaration(F->getParent(), |
| 210 | Intrinsic::stackprotector)) |
| 211 | HasPrologue = true; |
| 212 | |
Evgeniy Stepanov | f17120a | 2016-04-11 22:27:48 +0000 | [diff] [blame] | 213 | if (F->hasFnAttribute(Attribute::SafeStack)) |
| 214 | return false; |
| 215 | |
Duncan P. N. Exon Smith | 70eb9c5 | 2015-02-14 01:44:41 +0000 | [diff] [blame] | 216 | if (F->hasFnAttribute(Attribute::StackProtectReq)) { |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 217 | NeedsProtector = true; |
| 218 | Strong = true; // Use the same heuristic as strong to determine SSPLayout |
Duncan P. N. Exon Smith | 70eb9c5 | 2015-02-14 01:44:41 +0000 | [diff] [blame] | 219 | } else if (F->hasFnAttribute(Attribute::StackProtectStrong)) |
Bill Wendling | 7c8f96a | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 220 | Strong = true; |
Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 221 | else if (HasPrologue) |
| 222 | NeedsProtector = true; |
Duncan P. N. Exon Smith | 70eb9c5 | 2015-02-14 01:44:41 +0000 | [diff] [blame] | 223 | else if (!F->hasFnAttribute(Attribute::StackProtect)) |
Bill Wendling | eeb0415 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 224 | return false; |
| 225 | |
Saleem Abdulrasool | 57b5fe5 | 2014-12-20 21:37:51 +0000 | [diff] [blame] | 226 | for (const BasicBlock &BB : *F) { |
| 227 | for (const Instruction &I : BB) { |
| 228 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) { |
Bill Wendling | 7c8f96a | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 229 | if (AI->isArrayAllocation()) { |
| 230 | // SSP-Strong: Enable protectors for any call to alloca, regardless |
| 231 | // of size. |
| 232 | if (Strong) |
| 233 | return true; |
Michael Gottesman | 62c5d71 | 2013-08-20 08:46:16 +0000 | [diff] [blame] | 234 | |
Saleem Abdulrasool | 57b5fe5 | 2014-12-20 21:37:51 +0000 | [diff] [blame] | 235 | if (const auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 236 | if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) { |
Bill Wendling | 7c8f96a | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 237 | // A call to alloca with size >= SSPBufferSize requires |
| 238 | // stack protectors. |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 239 | Layout.insert(std::make_pair(AI, SSPLK_LargeArray)); |
| 240 | NeedsProtector = true; |
| 241 | } else if (Strong) { |
| 242 | // Require protectors for all alloca calls in strong mode. |
| 243 | Layout.insert(std::make_pair(AI, SSPLK_SmallArray)); |
| 244 | NeedsProtector = true; |
| 245 | } |
Bill Wendling | c02a0aa | 2013-07-22 20:15:21 +0000 | [diff] [blame] | 246 | } else { |
| 247 | // A call to alloca with a variable size requires protectors. |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 248 | Layout.insert(std::make_pair(AI, SSPLK_LargeArray)); |
| 249 | NeedsProtector = true; |
Bill Wendling | c02a0aa | 2013-07-22 20:15:21 +0000 | [diff] [blame] | 250 | } |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 251 | continue; |
Bill Wendling | 7c8f96a | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 254 | bool IsLarge = false; |
| 255 | if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) { |
| 256 | Layout.insert(std::make_pair(AI, IsLarge ? SSPLK_LargeArray |
| 257 | : SSPLK_SmallArray)); |
| 258 | NeedsProtector = true; |
| 259 | continue; |
| 260 | } |
Bill Wendling | eeb0415 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 261 | |
Bill Wendling | 7c8f96a | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 262 | if (Strong && HasAddressTaken(AI)) { |
Michael Gottesman | 62c5d71 | 2013-08-20 08:46:16 +0000 | [diff] [blame] | 263 | ++NumAddrTaken; |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 264 | Layout.insert(std::make_pair(AI, SSPLK_AddrOf)); |
| 265 | NeedsProtector = true; |
Bill Wendling | 7c8f96a | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 266 | } |
Bill Wendling | eeb0415 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 267 | } |
Bill Wendling | 7c8f96a | 2013-01-23 06:43:53 +0000 | [diff] [blame] | 268 | } |
Bill Wendling | eeb0415 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Josh Magee | 3f1c0e3 | 2013-10-29 21:16:16 +0000 | [diff] [blame] | 271 | return NeedsProtector; |
Bill Wendling | eeb0415 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Tim Shen | e885d5e | 2016-04-19 19:40:37 +0000 | [diff] [blame^] | 274 | /// Create a stack guard loading and populate whether SelectionDAG SSP is |
| 275 | /// supported. |
| 276 | static Value *getStackGuard(const TargetLoweringBase *TLI, Module *M, |
| 277 | IRBuilder<> &B, |
| 278 | bool *SupportsSelectionDAGSP = nullptr) { |
| 279 | if (Value *Guard = TLI->getIRStackGuard(B)) |
| 280 | return B.CreateLoad(Guard, true, "StackGuard"); |
| 281 | |
| 282 | // Use SelectionDAG SSP handling, since there isn't an IR guard. |
| 283 | // |
| 284 | // This is more or less weird, since we optionally output whether we |
| 285 | // should perform a SelectionDAG SP here. The reason is that it's strictly |
| 286 | // defined as !TLI->getIRStackGuard(B), where getIRStackGuard is also |
| 287 | // mutating. There is no way to get this bit without mutating the IR, so |
| 288 | // getting this bit has to happen in this right time. |
| 289 | // |
| 290 | // We could have define a new function TLI::supportsSelectionDAGSP(), but that |
| 291 | // will put more burden on the backends' overriding work, especially when it |
| 292 | // actually conveys the same information getIRStackGuard() already gives. |
| 293 | if (SupportsSelectionDAGSP) |
| 294 | *SupportsSelectionDAGSP = true; |
| 295 | TLI->insertSSPDeclarations(*M); |
| 296 | return B.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackguard)); |
| 297 | } |
| 298 | |
| 299 | /// Insert code into the entry block that stores the stack guard |
Michael Gottesman | a6188f9 | 2013-07-22 20:44:11 +0000 | [diff] [blame] | 300 | /// variable onto the stack: |
| 301 | /// |
| 302 | /// entry: |
| 303 | /// StackGuardSlot = alloca i8* |
Tim Shen | e885d5e | 2016-04-19 19:40:37 +0000 | [diff] [blame^] | 304 | /// StackGuard = <stack guard> |
| 305 | /// call void @llvm.stackprotector(StackGuard, StackGuardSlot) |
Michael Gottesman | a6188f9 | 2013-07-22 20:44:11 +0000 | [diff] [blame] | 306 | /// |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 307 | /// Returns true if the platform/triple supports the stackprotectorcreate pseudo |
| 308 | /// node. |
| 309 | static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI, |
Tim Shen | e885d5e | 2016-04-19 19:40:37 +0000 | [diff] [blame^] | 310 | const TargetLoweringBase *TLI, AllocaInst *&AI) { |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 311 | bool SupportsSelectionDAGSP = false; |
Evgeniy Stepanov | dde29e2 | 2016-04-05 22:41:50 +0000 | [diff] [blame] | 312 | IRBuilder<> B(&F->getEntryBlock().front()); |
Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 313 | PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext()); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 314 | AI = B.CreateAlloca(PtrTy, nullptr, "StackGuardSlot"); |
Tim Shen | e885d5e | 2016-04-19 19:40:37 +0000 | [diff] [blame^] | 315 | |
| 316 | Value *Guard = getStackGuard(TLI, M, B, &SupportsSelectionDAGSP); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 317 | B.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackprotector), |
Tim Shen | e885d5e | 2016-04-19 19:40:37 +0000 | [diff] [blame^] | 318 | {Guard, AI}); |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 319 | return SupportsSelectionDAGSP; |
Michael Gottesman | a6188f9 | 2013-07-22 20:44:11 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Bill Wendling | 782e834 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 322 | /// InsertStackProtectors - Insert code into the prologue and epilogue of the |
| 323 | /// function. |
| 324 | /// |
| 325 | /// - The prologue code loads and stores the stack guard onto the stack. |
| 326 | /// - The epilogue checks the value stored in the prologue against the original |
| 327 | /// value. It calls __stack_chk_fail if they differ. |
| 328 | bool StackProtector::InsertStackProtectors() { |
Michael Gottesman | 76c44be | 2013-08-20 08:56:26 +0000 | [diff] [blame] | 329 | bool SupportsSelectionDAGSP = |
Josh Magee | 7245f1d | 2013-10-30 02:25:14 +0000 | [diff] [blame] | 330 | EnableSelectionDAGSP && !TM->Options.EnableFastISel; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 331 | AllocaInst *AI = nullptr; // Place on stack that stores the stack guard. |
Bill Wendling | eb4268d | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 332 | |
Josh Magee | 7245f1d | 2013-10-30 02:25:14 +0000 | [diff] [blame] | 333 | for (Function::iterator I = F->begin(), E = F->end(); I != E;) { |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 334 | BasicBlock *BB = &*I++; |
Bill Wendling | eeb0415 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 335 | ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()); |
Michael Gottesman | dc985ef | 2013-08-20 08:56:28 +0000 | [diff] [blame] | 336 | if (!RI) |
| 337 | continue; |
Bill Wendling | eb4268d | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 338 | |
Michael Gottesman | 8afcf3a | 2013-08-09 21:26:18 +0000 | [diff] [blame] | 339 | if (!HasPrologue) { |
| 340 | HasPrologue = true; |
Tim Shen | e885d5e | 2016-04-19 19:40:37 +0000 | [diff] [blame^] | 341 | SupportsSelectionDAGSP &= CreatePrologue(F, M, RI, TLI, AI); |
Michael Gottesman | 62c5d71 | 2013-08-20 08:46:16 +0000 | [diff] [blame] | 342 | } |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 343 | |
Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 344 | if (!SupportsSelectionDAGSP) { |
Michael Gottesman | 56e246b | 2013-08-20 08:46:13 +0000 | [diff] [blame] | 345 | // If we do not support SelectionDAG based tail calls, generate IR level |
| 346 | // tail calls. |
| 347 | // |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 348 | // For each block with a return instruction, convert this: |
| 349 | // |
| 350 | // return: |
| 351 | // ... |
| 352 | // ret ... |
| 353 | // |
| 354 | // into this: |
| 355 | // |
| 356 | // return: |
| 357 | // ... |
Tim Shen | e885d5e | 2016-04-19 19:40:37 +0000 | [diff] [blame^] | 358 | // %1 = <stack guard> |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 359 | // %2 = load StackGuardSlot |
| 360 | // %3 = cmp i1 %1, %2 |
| 361 | // br i1 %3, label %SP_return, label %CallStackCheckFailBlk |
| 362 | // |
| 363 | // SP_return: |
| 364 | // ret ... |
| 365 | // |
| 366 | // CallStackCheckFailBlk: |
| 367 | // call void @__stack_chk_fail() |
| 368 | // unreachable |
| 369 | |
| 370 | // Create the FailBB. We duplicate the BB every time since the MI tail |
| 371 | // merge pass will merge together all of the various BB into one including |
Michael Gottesman | 62c5d71 | 2013-08-20 08:46:16 +0000 | [diff] [blame] | 372 | // fail BB generated by the stack protector pseudo instruction. |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 373 | BasicBlock *FailBB = CreateFailBB(); |
Michael Gottesman | 62c5d71 | 2013-08-20 08:46:16 +0000 | [diff] [blame] | 374 | |
Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 375 | // Set HasIRCheck to true, so that SelectionDAG will not generate its own |
| 376 | // version. |
| 377 | HasIRCheck = true; |
| 378 | |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 379 | // Split the basic block before the return instruction. |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 380 | BasicBlock *NewBB = BB->splitBasicBlock(RI->getIterator(), "SP_return"); |
Michael Gottesman | 62c5d71 | 2013-08-20 08:46:16 +0000 | [diff] [blame] | 381 | |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 382 | // Update the dominator tree if we need to. |
| 383 | if (DT && DT->isReachableFromEntry(BB)) { |
| 384 | DT->addNewBlock(NewBB, BB); |
| 385 | DT->addNewBlock(FailBB, BB); |
| 386 | } |
Michael Gottesman | 62c5d71 | 2013-08-20 08:46:16 +0000 | [diff] [blame] | 387 | |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 388 | // Remove default branch instruction to the new BB. |
| 389 | BB->getTerminator()->eraseFromParent(); |
Michael Gottesman | 62c5d71 | 2013-08-20 08:46:16 +0000 | [diff] [blame] | 390 | |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 391 | // Move the newly created basic block to the point right after the old |
| 392 | // basic block so that it's in the "fall through" position. |
| 393 | NewBB->moveAfter(BB); |
Michael Gottesman | 62c5d71 | 2013-08-20 08:46:16 +0000 | [diff] [blame] | 394 | |
Michael Gottesman | 5e57068 | 2013-08-20 08:36:53 +0000 | [diff] [blame] | 395 | // Generate the stack protector instructions in the old basic block. |
Benjamin Kramer | d93817f | 2013-09-09 17:38:01 +0000 | [diff] [blame] | 396 | IRBuilder<> B(BB); |
Tim Shen | e885d5e | 2016-04-19 19:40:37 +0000 | [diff] [blame^] | 397 | Value *Guard = getStackGuard(TLI, M, B); |
| 398 | LoadInst *LI2 = B.CreateLoad(AI, true); |
| 399 | Value *Cmp = B.CreateICmpEQ(Guard, LI2); |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 400 | auto SuccessProb = |
| 401 | BranchProbabilityInfo::getBranchProbStackProtector(true); |
| 402 | auto FailureProb = |
| 403 | BranchProbabilityInfo::getBranchProbStackProtector(false); |
Akira Hatanaka | b9991a2 | 2014-12-01 04:27:03 +0000 | [diff] [blame] | 404 | MDNode *Weights = MDBuilder(F->getContext()) |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 405 | .createBranchWeights(SuccessProb.getNumerator(), |
| 406 | FailureProb.getNumerator()); |
Akira Hatanaka | b9991a2 | 2014-12-01 04:27:03 +0000 | [diff] [blame] | 407 | B.CreateCondBr(Cmp, NewBB, FailBB, Weights); |
Bill Wendling | a0826e18 | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 408 | } |
Bill Wendling | 05d8417 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 409 | } |
Bill Wendling | 782e834 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 410 | |
Saleem Abdulrasool | 90c224a | 2014-12-21 21:52:38 +0000 | [diff] [blame] | 411 | // Return if we didn't modify any basic blocks. i.e., there are no return |
Bill Wendling | a0826e18 | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 412 | // statements in the function. |
Rafael Espindola | 84921b9 | 2015-10-24 23:11:13 +0000 | [diff] [blame] | 413 | return HasPrologue; |
Bill Wendling | 05d8417 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /// CreateFailBB - Create a basic block to jump to when the stack protector |
| 417 | /// check fails. |
Bill Wendling | 782e834 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 418 | BasicBlock *StackProtector::CreateFailBB() { |
Rafael Espindola | aad6c24 | 2013-06-07 16:35:57 +0000 | [diff] [blame] | 419 | LLVMContext &Context = F->getContext(); |
| 420 | BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F); |
Benjamin Kramer | d93817f | 2013-09-09 17:38:01 +0000 | [diff] [blame] | 421 | IRBuilder<> B(FailBB); |
Simon Pilgrim | 2bfd912 | 2014-11-29 19:18:21 +0000 | [diff] [blame] | 422 | if (Trip.isOSOpenBSD()) { |
Saleem Abdulrasool | 90c224a | 2014-12-21 21:52:38 +0000 | [diff] [blame] | 423 | Constant *StackChkFail = |
| 424 | M->getOrInsertFunction("__stack_smash_handler", |
| 425 | Type::getVoidTy(Context), |
| 426 | Type::getInt8PtrTy(Context), nullptr); |
Rafael Espindola | aad6c24 | 2013-06-07 16:35:57 +0000 | [diff] [blame] | 427 | |
Benjamin Kramer | d93817f | 2013-09-09 17:38:01 +0000 | [diff] [blame] | 428 | B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH")); |
Rafael Espindola | aad6c24 | 2013-06-07 16:35:57 +0000 | [diff] [blame] | 429 | } else { |
Saleem Abdulrasool | 90c224a | 2014-12-21 21:52:38 +0000 | [diff] [blame] | 430 | Constant *StackChkFail = |
| 431 | M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context), |
| 432 | nullptr); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 433 | B.CreateCall(StackChkFail, {}); |
Rafael Espindola | aad6c24 | 2013-06-07 16:35:57 +0000 | [diff] [blame] | 434 | } |
Benjamin Kramer | d93817f | 2013-09-09 17:38:01 +0000 | [diff] [blame] | 435 | B.CreateUnreachable(); |
Bill Wendling | 782e834 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 436 | return FailBB; |
Bill Wendling | 05d8417 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 437 | } |
Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 438 | |
| 439 | bool StackProtector::shouldEmitSDCheck(const BasicBlock &BB) const { |
| 440 | return HasPrologue && !HasIRCheck && dyn_cast<ReturnInst>(BB.getTerminator()); |
| 441 | } |