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