blob: 58e4ad971e8097176926c776fd7834008b687b2e [file] [log] [blame]
Bill Wendling05d84172008-11-04 02:10:20 +00001//===-- 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 Wendling64adc712008-11-04 21:53:09 +000010// 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 Wendling05d84172008-11-04 02:10:20 +000013// changed, then there was some sort of violation and the program aborts.
14//
15//===----------------------------------------------------------------------===//
16
Josh Magee8ecfb522013-09-27 21:58:43 +000017#include "llvm/CodeGen/StackProtector.h"
Bill Wendling7c8f96a2013-01-23 06:43:53 +000018#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/Statistic.h"
Akira Hatanakab9991a22014-12-01 04:27:03 +000020#include "llvm/Analysis/BranchProbabilityInfo.h"
Michael Gottesman5e570682013-08-20 08:36:53 +000021#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "llvm/CodeGen/Analysis.h"
23#include "llvm/CodeGen/Passes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#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 Espindolaaad6c242013-06-07 16:35:57 +000029#include "llvm/IR/GlobalValue.h"
30#include "llvm/IR/GlobalVariable.h"
Benjamin Kramerd93817f2013-09-09 17:38:01 +000031#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Instructions.h"
Michael Gottesman5e570682013-08-20 08:36:53 +000033#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/Intrinsics.h"
Akira Hatanakab9991a22014-12-01 04:27:03 +000035#include "llvm/IR/MDBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000036#include "llvm/IR/Module.h"
Bill Wendling05d84172008-11-04 02:10:20 +000037#include "llvm/Support/CommandLine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000038#include "llvm/Target/TargetSubtargetInfo.h"
Bill Wendlingc02a0aa2013-07-22 20:15:21 +000039#include <cstdlib>
Bill Wendling05d84172008-11-04 02:10:20 +000040using namespace llvm;
41
Chandler Carruth1b9dde02014-04-22 02:02:50 +000042#define DEBUG_TYPE "stack-protector"
43
Bill Wendling7c8f96a2013-01-23 06:43:53 +000044STATISTIC(NumFunProtected, "Number of functions protected");
45STATISTIC(NumAddrTaken, "Number of local variables that have their address"
46 " taken.");
47
Josh Magee7245f1d2013-10-30 02:25:14 +000048static cl::opt<bool> EnableSelectionDAGSP("enable-selectiondag-sp",
49 cl::init(true), cl::Hidden);
Michael Gottesman5e570682013-08-20 08:36:53 +000050
Bill Wendling05d84172008-11-04 02:10:20 +000051char StackProtector::ID = 0;
Josh Magee7245f1d2013-10-30 02:25:14 +000052INITIALIZE_PASS(StackProtector, "stack-protector", "Insert stack protectors",
53 false, true)
Bill Wendling05d84172008-11-04 02:10:20 +000054
Bill Wendlingafc10362013-06-19 20:51:24 +000055FunctionPass *llvm::createStackProtectorPass(const TargetMachine *TM) {
56 return new StackProtector(TM);
Bill Wendling05d84172008-11-04 02:10:20 +000057}
58
Josh Magee7245f1d2013-10-30 02:25:14 +000059StackProtector::SSPLayoutKind
60StackProtector::getSSPLayout(const AllocaInst *AI) const {
Josh Magee3f1c0e32013-10-29 21:16:16 +000061 return AI ? Layout.lookup(AI) : SSPLK_None;
62}
63
Hal Finkela69e5b82014-01-20 19:49:14 +000064void 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 Wendling05d84172008-11-04 02:10:20 +000085bool StackProtector::runOnFunction(Function &Fn) {
86 F = &Fn;
87 M = F->getParent();
Chandler Carruth73523022014-01-13 13:07:17 +000088 DominatorTreeWrapperPass *DTWP =
89 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Craig Topperc0196b12014-04-14 00:51:57 +000090 DT = DTWP ? &DTWP->getDomTree() : nullptr;
Eric Christopherd9134482014-08-04 21:25:23 +000091 TLI = TM->getSubtargetImpl()->getTargetLowering();
Bill Wendling05d84172008-11-04 02:10:20 +000092
Josh Magee7245f1d2013-10-30 02:25:14 +000093 Attribute Attr = Fn.getAttributes().getAttribute(
94 AttributeSet::FunctionIndex, "stack-protector-buffer-size");
Renato Goline195f9c2014-01-21 10:24:35 +000095 if (Attr.isStringAttribute() &&
96 Attr.getValueAsString().getAsInteger(10, SSPBufferSize))
97 return false; // Invalid integer string
Bill Wendlingc02a0aa2013-07-22 20:15:21 +000098
Josh Mageeadfde5f2014-04-17 19:08:36 +000099 if (!RequiresStackProtector())
100 return false;
101
Bill Wendling7c8f96a2013-01-23 06:43:53 +0000102 ++NumFunProtected;
Bill Wendling782e8342008-11-05 00:00:21 +0000103 return InsertStackProtectors();
Bill Wendling05d84172008-11-04 02:10:20 +0000104}
105
Josh Magee3f1c0e32013-10-29 21:16:16 +0000106/// \param [out] IsLarge is set to true if a protectable array is found and
107/// it is "large" ( >= ssp-buffer-size). In the case of a structure with
108/// multiple arrays, this gets set if any of them is large.
109bool StackProtector::ContainsProtectableArray(Type *Ty, bool &IsLarge,
Josh Magee7245f1d2013-10-30 02:25:14 +0000110 bool Strong,
111 bool InStruct) const {
112 if (!Ty)
113 return false;
Bill Wendlingbfb9b752012-08-17 20:59:56 +0000114 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
115 if (!AT->getElementType()->isIntegerTy(8)) {
Bill Wendlingbfb9b752012-08-17 20:59:56 +0000116 // If we're on a non-Darwin platform or we're inside of a structure, don't
117 // add stack protectors unless the array is a character array.
Josh Magee3f1c0e32013-10-29 21:16:16 +0000118 // However, in strong mode any array, regardless of type and size,
119 // triggers a protector.
120 if (!Strong && (InStruct || !Trip.isOSDarwin()))
121 return false;
Bill Wendlingbfb9b752012-08-17 20:59:56 +0000122 }
123
124 // If an array has more than SSPBufferSize bytes of allocated space, then we
125 // emit stack protectors.
Josh Magee3f1c0e32013-10-29 21:16:16 +0000126 if (SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT)) {
127 IsLarge = true;
128 return true;
Josh Magee7245f1d2013-10-30 02:25:14 +0000129 }
Josh Magee3f1c0e32013-10-29 21:16:16 +0000130
131 if (Strong)
132 // Require a protector for all arrays in strong mode
Bill Wendlingbfb9b752012-08-17 20:59:56 +0000133 return true;
134 }
135
136 const StructType *ST = dyn_cast<StructType>(Ty);
Josh Magee7245f1d2013-10-30 02:25:14 +0000137 if (!ST)
138 return false;
Bill Wendlingbfb9b752012-08-17 20:59:56 +0000139
Josh Magee3f1c0e32013-10-29 21:16:16 +0000140 bool NeedsProtector = false;
Bill Wendlingbfb9b752012-08-17 20:59:56 +0000141 for (StructType::element_iterator I = ST->element_begin(),
Josh Magee7245f1d2013-10-30 02:25:14 +0000142 E = ST->element_end();
143 I != E; ++I)
Josh Magee3f1c0e32013-10-29 21:16:16 +0000144 if (ContainsProtectableArray(*I, IsLarge, Strong, true)) {
145 // If the element is a protectable array and is large (>= SSPBufferSize)
146 // then we are done. If the protectable array is not large, then
147 // keep looking in case a subsequent element is a large array.
148 if (IsLarge)
149 return true;
150 NeedsProtector = true;
151 }
Bill Wendlingbfb9b752012-08-17 20:59:56 +0000152
Josh Magee3f1c0e32013-10-29 21:16:16 +0000153 return NeedsProtector;
Bill Wendlingbfb9b752012-08-17 20:59:56 +0000154}
155
Bill Wendling7c8f96a2013-01-23 06:43:53 +0000156bool StackProtector::HasAddressTaken(const Instruction *AI) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000157 for (const User *U : AI->users()) {
Bill Wendling7c8f96a2013-01-23 06:43:53 +0000158 if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
159 if (AI == SI->getValueOperand())
160 return true;
161 } else if (const PtrToIntInst *SI = dyn_cast<PtrToIntInst>(U)) {
162 if (AI == SI->getOperand(0))
163 return true;
164 } else if (isa<CallInst>(U)) {
165 return true;
166 } else if (isa<InvokeInst>(U)) {
167 return true;
168 } else if (const SelectInst *SI = dyn_cast<SelectInst>(U)) {
169 if (HasAddressTaken(SI))
170 return true;
171 } else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
172 // Keep track of what PHI nodes we have already visited to ensure
173 // they are only visited once.
David Blaikie70573dc2014-11-19 07:49:26 +0000174 if (VisitedPHIs.insert(PN).second)
Bill Wendling7c8f96a2013-01-23 06:43:53 +0000175 if (HasAddressTaken(PN))
176 return true;
177 } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
178 if (HasAddressTaken(GEP))
179 return true;
180 } else if (const BitCastInst *BI = dyn_cast<BitCastInst>(U)) {
181 if (HasAddressTaken(BI))
182 return true;
183 }
184 }
185 return false;
186}
187
188/// \brief Check whether or not this function needs a stack protector based
189/// upon the stack protector level.
190///
191/// We use two heuristics: a standard (ssp) and strong (sspstrong).
192/// The standard heuristic which will add a guard variable to functions that
193/// call alloca with a either a variable size or a size >= SSPBufferSize,
194/// functions with character buffers larger than SSPBufferSize, and functions
195/// with aggregates containing character buffers larger than SSPBufferSize. The
196/// strong heuristic will add a guard variables to functions that call alloca
197/// regardless of size, functions with any buffer regardless of type and size,
198/// functions with aggregates that contain any buffer regardless of type and
199/// size, and functions that contain stack-based variables that have had their
200/// address taken.
201bool StackProtector::RequiresStackProtector() {
202 bool Strong = false;
Josh Magee3f1c0e32013-10-29 21:16:16 +0000203 bool NeedsProtector = false;
Bill Wendling698e84f2012-12-30 10:32:01 +0000204 if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
Josh Magee3f1c0e32013-10-29 21:16:16 +0000205 Attribute::StackProtectReq)) {
206 NeedsProtector = true;
207 Strong = true; // Use the same heuristic as strong to determine SSPLayout
208 } else if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
209 Attribute::StackProtectStrong))
Bill Wendling7c8f96a2013-01-23 06:43:53 +0000210 Strong = true;
211 else if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
212 Attribute::StackProtect))
Bill Wendlingeeb04152008-11-18 05:32:11 +0000213 return false;
214
Bill Wendlingeeb04152008-11-18 05:32:11 +0000215 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
216 BasicBlock *BB = I;
217
Josh Magee7245f1d2013-10-30 02:25:14 +0000218 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;
219 ++II) {
Bill Wendlingeeb04152008-11-18 05:32:11 +0000220 if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
Bill Wendling7c8f96a2013-01-23 06:43:53 +0000221 if (AI->isArrayAllocation()) {
222 // SSP-Strong: Enable protectors for any call to alloca, regardless
223 // of size.
224 if (Strong)
225 return true;
Michael Gottesman62c5d712013-08-20 08:46:16 +0000226
Bill Wendling7c8f96a2013-01-23 06:43:53 +0000227 if (const ConstantInt *CI =
Josh Magee7245f1d2013-10-30 02:25:14 +0000228 dyn_cast<ConstantInt>(AI->getArraySize())) {
Josh Magee3f1c0e32013-10-29 21:16:16 +0000229 if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) {
Bill Wendling7c8f96a2013-01-23 06:43:53 +0000230 // A call to alloca with size >= SSPBufferSize requires
231 // stack protectors.
Josh Magee3f1c0e32013-10-29 21:16:16 +0000232 Layout.insert(std::make_pair(AI, SSPLK_LargeArray));
233 NeedsProtector = true;
234 } else if (Strong) {
235 // Require protectors for all alloca calls in strong mode.
236 Layout.insert(std::make_pair(AI, SSPLK_SmallArray));
237 NeedsProtector = true;
238 }
Bill Wendlingc02a0aa2013-07-22 20:15:21 +0000239 } else {
240 // A call to alloca with a variable size requires protectors.
Josh Magee3f1c0e32013-10-29 21:16:16 +0000241 Layout.insert(std::make_pair(AI, SSPLK_LargeArray));
242 NeedsProtector = true;
Bill Wendlingc02a0aa2013-07-22 20:15:21 +0000243 }
Josh Magee3f1c0e32013-10-29 21:16:16 +0000244 continue;
Bill Wendling7c8f96a2013-01-23 06:43:53 +0000245 }
246
Josh Magee3f1c0e32013-10-29 21:16:16 +0000247 bool IsLarge = false;
248 if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) {
249 Layout.insert(std::make_pair(AI, IsLarge ? SSPLK_LargeArray
250 : SSPLK_SmallArray));
251 NeedsProtector = true;
252 continue;
253 }
Bill Wendlingeeb04152008-11-18 05:32:11 +0000254
Bill Wendling7c8f96a2013-01-23 06:43:53 +0000255 if (Strong && HasAddressTaken(AI)) {
Michael Gottesman62c5d712013-08-20 08:46:16 +0000256 ++NumAddrTaken;
Josh Magee3f1c0e32013-10-29 21:16:16 +0000257 Layout.insert(std::make_pair(AI, SSPLK_AddrOf));
258 NeedsProtector = true;
Bill Wendling7c8f96a2013-01-23 06:43:53 +0000259 }
Bill Wendlingeeb04152008-11-18 05:32:11 +0000260 }
Bill Wendling7c8f96a2013-01-23 06:43:53 +0000261 }
Bill Wendlingeeb04152008-11-18 05:32:11 +0000262 }
263
Josh Magee3f1c0e32013-10-29 21:16:16 +0000264 return NeedsProtector;
Bill Wendlingeeb04152008-11-18 05:32:11 +0000265}
266
Michael Gottesman5e570682013-08-20 08:36:53 +0000267static bool InstructionWillNotHaveChain(const Instruction *I) {
268 return !I->mayHaveSideEffects() && !I->mayReadFromMemory() &&
Josh Magee7245f1d2013-10-30 02:25:14 +0000269 isSafeToSpeculativelyExecute(I);
Michael Gottesman5e570682013-08-20 08:36:53 +0000270}
271
272/// Identify if RI has a previous instruction in the "Tail Position" and return
273/// it. Otherwise return 0.
274///
Michael Gottesman1977d152013-08-20 08:56:23 +0000275/// This is based off of the code in llvm::isInTailCallPosition. The difference
276/// is that it inverts the first part of llvm::isInTailCallPosition since
277/// isInTailCallPosition is checking if a call is in a tail call position, and
278/// we are searching for an unknown tail call that might be in the tail call
279/// position. Once we find the call though, the code uses the same refactored
280/// code, returnTypeIsEligibleForTailCall.
Michael Gottesman5e570682013-08-20 08:36:53 +0000281static CallInst *FindPotentialTailCall(BasicBlock *BB, ReturnInst *RI,
282 const TargetLoweringBase *TLI) {
283 // Establish a reasonable upper bound on the maximum amount of instructions we
284 // will look through to find a tail call.
285 unsigned SearchCounter = 0;
286 const unsigned MaxSearch = 4;
287 bool NoInterposingChain = true;
288
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000289 for (BasicBlock::reverse_iterator I = std::next(BB->rbegin()), E = BB->rend();
Michael Gottesman5e570682013-08-20 08:36:53 +0000290 I != E && SearchCounter < MaxSearch; ++I) {
291 Instruction *Inst = &*I;
292
293 // Skip over debug intrinsics and do not allow them to affect our MaxSearch
294 // counter.
295 if (isa<DbgInfoIntrinsic>(Inst))
296 continue;
297
298 // If we find a call and the following conditions are satisifed, then we
299 // have found a tail call that satisfies at least the target independent
300 // requirements of a tail call:
301 //
302 // 1. The call site has the tail marker.
303 //
304 // 2. The call site either will not cause the creation of a chain or if a
305 // chain is necessary there are no instructions in between the callsite and
306 // the call which would create an interposing chain.
307 //
308 // 3. The return type of the function does not impede tail call
309 // optimization.
310 if (CallInst *CI = dyn_cast<CallInst>(Inst)) {
311 if (CI->isTailCall() &&
312 (InstructionWillNotHaveChain(CI) || NoInterposingChain) &&
313 returnTypeIsEligibleForTailCall(BB->getParent(), CI, RI, *TLI))
314 return CI;
315 }
316
317 // If we did not find a call see if we have an instruction that may create
318 // an interposing chain.
Josh Magee7245f1d2013-10-30 02:25:14 +0000319 NoInterposingChain =
320 NoInterposingChain && InstructionWillNotHaveChain(Inst);
Michael Gottesman5e570682013-08-20 08:36:53 +0000321
322 // Increment max search.
323 SearchCounter++;
324 }
325
Craig Topperc0196b12014-04-14 00:51:57 +0000326 return nullptr;
Michael Gottesman5e570682013-08-20 08:36:53 +0000327}
328
Michael Gottesmana6188f92013-07-22 20:44:11 +0000329/// Insert code into the entry block that stores the __stack_chk_guard
330/// variable onto the stack:
331///
332/// entry:
333/// StackGuardSlot = alloca i8*
334/// StackGuard = load __stack_chk_guard
335/// call void @llvm.stackprotect.create(StackGuard, StackGuardSlot)
336///
Michael Gottesman5e570682013-08-20 08:36:53 +0000337/// Returns true if the platform/triple supports the stackprotectorcreate pseudo
338/// node.
339static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI,
Michael Gottesmana6188f92013-07-22 20:44:11 +0000340 const TargetLoweringBase *TLI, const Triple &Trip,
341 AllocaInst *&AI, Value *&StackGuardVar) {
Michael Gottesman5e570682013-08-20 08:36:53 +0000342 bool SupportsSelectionDAGSP = false;
Michael Gottesmana6188f92013-07-22 20:44:11 +0000343 PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext());
344 unsigned AddressSpace, Offset;
345 if (TLI->getStackCookieLocation(AddressSpace, Offset)) {
346 Constant *OffsetVal =
Josh Magee7245f1d2013-10-30 02:25:14 +0000347 ConstantInt::get(Type::getInt32Ty(RI->getContext()), Offset);
Michael Gottesman62c5d712013-08-20 08:46:16 +0000348
Josh Magee7245f1d2013-10-30 02:25:14 +0000349 StackGuardVar = ConstantExpr::getIntToPtr(
350 OffsetVal, PointerType::get(PtrTy, AddressSpace));
Simon Pilgrim2bfd9122014-11-29 19:18:21 +0000351 } else if (Trip.isOSOpenBSD()) {
Michael Gottesmana6188f92013-07-22 20:44:11 +0000352 StackGuardVar = M->getOrInsertGlobal("__guard_local", PtrTy);
353 cast<GlobalValue>(StackGuardVar)
Josh Magee7245f1d2013-10-30 02:25:14 +0000354 ->setVisibility(GlobalValue::HiddenVisibility);
Michael Gottesmana6188f92013-07-22 20:44:11 +0000355 } else {
Michael Gottesman5e570682013-08-20 08:36:53 +0000356 SupportsSelectionDAGSP = true;
Michael Gottesman62c5d712013-08-20 08:46:16 +0000357 StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);
Michael Gottesmana6188f92013-07-22 20:44:11 +0000358 }
Michael Gottesman62c5d712013-08-20 08:46:16 +0000359
Benjamin Kramerd93817f2013-09-09 17:38:01 +0000360 IRBuilder<> B(&F->getEntryBlock().front());
Craig Topperc0196b12014-04-14 00:51:57 +0000361 AI = B.CreateAlloca(PtrTy, nullptr, "StackGuardSlot");
Benjamin Kramerd93817f2013-09-09 17:38:01 +0000362 LoadInst *LI = B.CreateLoad(StackGuardVar, "StackGuard");
363 B.CreateCall2(Intrinsic::getDeclaration(M, Intrinsic::stackprotector), LI,
364 AI);
Michael Gottesman5e570682013-08-20 08:36:53 +0000365
366 return SupportsSelectionDAGSP;
Michael Gottesmana6188f92013-07-22 20:44:11 +0000367}
368
Bill Wendling782e8342008-11-05 00:00:21 +0000369/// InsertStackProtectors - Insert code into the prologue and epilogue of the
370/// function.
371///
372/// - The prologue code loads and stores the stack guard onto the stack.
373/// - The epilogue checks the value stored in the prologue against the original
374/// value. It calls __stack_chk_fail if they differ.
375bool StackProtector::InsertStackProtectors() {
Michael Gottesman8afcf3a2013-08-09 21:26:18 +0000376 bool HasPrologue = false;
Michael Gottesman76c44be2013-08-20 08:56:26 +0000377 bool SupportsSelectionDAGSP =
Josh Magee7245f1d2013-10-30 02:25:14 +0000378 EnableSelectionDAGSP && !TM->Options.EnableFastISel;
Craig Topperc0196b12014-04-14 00:51:57 +0000379 AllocaInst *AI = nullptr; // Place on stack that stores the stack guard.
380 Value *StackGuardVar = nullptr; // The stack guard variable.
Bill Wendlingeb4268d2008-11-07 01:23:58 +0000381
Josh Magee7245f1d2013-10-30 02:25:14 +0000382 for (Function::iterator I = F->begin(), E = F->end(); I != E;) {
Bill Wendlingeeb04152008-11-18 05:32:11 +0000383 BasicBlock *BB = I++;
Bill Wendlingeeb04152008-11-18 05:32:11 +0000384 ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
Michael Gottesmandc985ef2013-08-20 08:56:28 +0000385 if (!RI)
386 continue;
Bill Wendlingeb4268d2008-11-07 01:23:58 +0000387
Michael Gottesman8afcf3a2013-08-09 21:26:18 +0000388 if (!HasPrologue) {
389 HasPrologue = true;
Josh Magee7245f1d2013-10-30 02:25:14 +0000390 SupportsSelectionDAGSP &=
391 CreatePrologue(F, M, RI, TLI, Trip, AI, StackGuardVar);
Michael Gottesman62c5d712013-08-20 08:46:16 +0000392 }
Michael Gottesman5e570682013-08-20 08:36:53 +0000393
Michael Gottesman76c44be2013-08-20 08:56:26 +0000394 if (SupportsSelectionDAGSP) {
Michael Gottesman5e570682013-08-20 08:36:53 +0000395 // Since we have a potential tail call, insert the special stack check
396 // intrinsic.
Craig Topperc0196b12014-04-14 00:51:57 +0000397 Instruction *InsertionPt = nullptr;
Michael Gottesman5e570682013-08-20 08:36:53 +0000398 if (CallInst *CI = FindPotentialTailCall(BB, RI, TLI)) {
399 InsertionPt = CI;
Michael Gottesman62c5d712013-08-20 08:46:16 +0000400 } else {
Michael Gottesman5e570682013-08-20 08:36:53 +0000401 InsertionPt = RI;
402 // At this point we know that BB has a return statement so it *DOES*
403 // have a terminator.
Craig Topperc0196b12014-04-14 00:51:57 +0000404 assert(InsertionPt != nullptr && "BB must have a terminator instruction at "
Josh Magee7245f1d2013-10-30 02:25:14 +0000405 "this point.");
Michael Gottesman5e570682013-08-20 08:36:53 +0000406 }
407
408 Function *Intrinsic =
Josh Magee7245f1d2013-10-30 02:25:14 +0000409 Intrinsic::getDeclaration(M, Intrinsic::stackprotectorcheck);
Benjamin Kramerd93817f2013-09-09 17:38:01 +0000410 CallInst::Create(Intrinsic, StackGuardVar, "", InsertionPt);
Michael Gottesman5e570682013-08-20 08:36:53 +0000411
412 } else {
Michael Gottesman56e246b2013-08-20 08:46:13 +0000413 // If we do not support SelectionDAG based tail calls, generate IR level
414 // tail calls.
415 //
Michael Gottesman5e570682013-08-20 08:36:53 +0000416 // For each block with a return instruction, convert this:
417 //
418 // return:
419 // ...
420 // ret ...
421 //
422 // into this:
423 //
424 // return:
425 // ...
426 // %1 = load __stack_chk_guard
427 // %2 = load StackGuardSlot
428 // %3 = cmp i1 %1, %2
429 // br i1 %3, label %SP_return, label %CallStackCheckFailBlk
430 //
431 // SP_return:
432 // ret ...
433 //
434 // CallStackCheckFailBlk:
435 // call void @__stack_chk_fail()
436 // unreachable
437
438 // Create the FailBB. We duplicate the BB every time since the MI tail
439 // merge pass will merge together all of the various BB into one including
Michael Gottesman62c5d712013-08-20 08:46:16 +0000440 // fail BB generated by the stack protector pseudo instruction.
Michael Gottesman5e570682013-08-20 08:36:53 +0000441 BasicBlock *FailBB = CreateFailBB();
Michael Gottesman62c5d712013-08-20 08:46:16 +0000442
Michael Gottesman5e570682013-08-20 08:36:53 +0000443 // Split the basic block before the return instruction.
444 BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return");
Michael Gottesman62c5d712013-08-20 08:46:16 +0000445
Michael Gottesman5e570682013-08-20 08:36:53 +0000446 // Update the dominator tree if we need to.
447 if (DT && DT->isReachableFromEntry(BB)) {
448 DT->addNewBlock(NewBB, BB);
449 DT->addNewBlock(FailBB, BB);
450 }
Michael Gottesman62c5d712013-08-20 08:46:16 +0000451
Michael Gottesman5e570682013-08-20 08:36:53 +0000452 // Remove default branch instruction to the new BB.
453 BB->getTerminator()->eraseFromParent();
Michael Gottesman62c5d712013-08-20 08:46:16 +0000454
Michael Gottesman5e570682013-08-20 08:36:53 +0000455 // Move the newly created basic block to the point right after the old
456 // basic block so that it's in the "fall through" position.
457 NewBB->moveAfter(BB);
Michael Gottesman62c5d712013-08-20 08:46:16 +0000458
Michael Gottesman5e570682013-08-20 08:36:53 +0000459 // Generate the stack protector instructions in the old basic block.
Benjamin Kramerd93817f2013-09-09 17:38:01 +0000460 IRBuilder<> B(BB);
461 LoadInst *LI1 = B.CreateLoad(StackGuardVar);
462 LoadInst *LI2 = B.CreateLoad(AI);
463 Value *Cmp = B.CreateICmpEQ(LI1, LI2);
Akira Hatanakab9991a22014-12-01 04:27:03 +0000464 unsigned SuccessWeight =
465 BranchProbabilityInfo::getBranchWeightStackProtector(true);
466 unsigned FailureWeight =
467 BranchProbabilityInfo::getBranchWeightStackProtector(false);
468 MDNode *Weights = MDBuilder(F->getContext())
469 .createBranchWeights(SuccessWeight, FailureWeight);
470 B.CreateCondBr(Cmp, NewBB, FailBB, Weights);
Bill Wendlinga0826e182008-11-06 23:55:49 +0000471 }
Bill Wendling05d84172008-11-04 02:10:20 +0000472 }
Bill Wendling782e8342008-11-05 00:00:21 +0000473
Bill Wendlinga0826e182008-11-06 23:55:49 +0000474 // Return if we didn't modify any basic blocks. I.e., there are no return
475 // statements in the function.
Michael Gottesman8afcf3a2013-08-09 21:26:18 +0000476 if (!HasPrologue)
477 return false;
Cameron Zwarich84986b22011-01-08 17:01:52 +0000478
Bill Wendling782e8342008-11-05 00:00:21 +0000479 return true;
Bill Wendling05d84172008-11-04 02:10:20 +0000480}
481
482/// CreateFailBB - Create a basic block to jump to when the stack protector
483/// check fails.
Bill Wendling782e8342008-11-05 00:00:21 +0000484BasicBlock *StackProtector::CreateFailBB() {
Rafael Espindolaaad6c242013-06-07 16:35:57 +0000485 LLVMContext &Context = F->getContext();
486 BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F);
Benjamin Kramerd93817f2013-09-09 17:38:01 +0000487 IRBuilder<> B(FailBB);
Simon Pilgrim2bfd9122014-11-29 19:18:21 +0000488 if (Trip.isOSOpenBSD()) {
Rafael Espindolaaad6c242013-06-07 16:35:57 +0000489 Constant *StackChkFail = M->getOrInsertFunction(
490 "__stack_smash_handler", Type::getVoidTy(Context),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000491 Type::getInt8PtrTy(Context), nullptr);
Rafael Espindolaaad6c242013-06-07 16:35:57 +0000492
Benjamin Kramerd93817f2013-09-09 17:38:01 +0000493 B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
Rafael Espindolaaad6c242013-06-07 16:35:57 +0000494 } else {
495 Constant *StackChkFail = M->getOrInsertFunction(
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000496 "__stack_chk_fail", Type::getVoidTy(Context), nullptr);
Benjamin Kramerd93817f2013-09-09 17:38:01 +0000497 B.CreateCall(StackChkFail);
Rafael Espindolaaad6c242013-06-07 16:35:57 +0000498 }
Benjamin Kramerd93817f2013-09-09 17:38:01 +0000499 B.CreateUnreachable();
Bill Wendling782e8342008-11-05 00:00:21 +0000500 return FailBB;
Bill Wendling05d84172008-11-04 02:10:20 +0000501}