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