blob: cb22082e4da01df48a2642135938f7c4cb65257e [file] [log] [blame]
Bill Wendling2b58ce52008-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 Wendling80a320d2008-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 Wendling2b58ce52008-11-04 02:10:20 +000013// changed, then there was some sort of violation and the program aborts.
14//
15//===----------------------------------------------------------------------===//
16
17#define DEBUG_TYPE "stack-protector"
Josh Magee18ebd482013-09-27 21:58:43 +000018#include "llvm/CodeGen/StackProtector.h"
Michael Gottesman3480d1b2013-08-20 08:36:53 +000019#include "llvm/CodeGen/Analysis.h"
Bill Wendling2b58ce52008-11-04 02:10:20 +000020#include "llvm/CodeGen/Passes.h"
Bill Wendlinge4957fb2013-01-23 06:43:53 +000021#include "llvm/ADT/SmallPtrSet.h"
22#include "llvm/ADT/Statistic.h"
Cameron Zwarich80f6a502011-01-08 17:01:52 +000023#include "llvm/Analysis/Dominators.h"
Michael Gottesman3480d1b2013-08-20 08:36:53 +000024#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000025#include "llvm/IR/Attributes.h"
26#include "llvm/IR/Constants.h"
27#include "llvm/IR/DataLayout.h"
28#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/Function.h"
Rafael Espindola62ed8d32013-06-07 16:35:57 +000030#include "llvm/IR/GlobalValue.h"
31#include "llvm/IR/GlobalVariable.h"
Benjamin Kramer54cf1412013-09-09 17:38:01 +000032#include "llvm/IR/IRBuilder.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000033#include "llvm/IR/Instructions.h"
Michael Gottesman3480d1b2013-08-20 08:36:53 +000034#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000035#include "llvm/IR/Intrinsics.h"
36#include "llvm/IR/Module.h"
Bill Wendling2b58ce52008-11-04 02:10:20 +000037#include "llvm/Support/CommandLine.h"
Bill Wendling0dcba2f2013-07-22 20:15:21 +000038#include <cstdlib>
Bill Wendling2b58ce52008-11-04 02:10:20 +000039using namespace llvm;
40
Bill Wendlinge4957fb2013-01-23 06:43:53 +000041STATISTIC(NumFunProtected, "Number of functions protected");
42STATISTIC(NumAddrTaken, "Number of local variables that have their address"
43 " taken.");
44
Michael Gottesman3480d1b2013-08-20 08:36:53 +000045static cl::opt<bool>
46EnableSelectionDAGSP("enable-selectiondag-sp", cl::init(true),
47 cl::Hidden);
48
Bill Wendling2b58ce52008-11-04 02:10:20 +000049char StackProtector::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000050INITIALIZE_PASS(StackProtector, "stack-protector",
Owen Andersonce665bd2010-10-07 22:25:06 +000051 "Insert stack protectors", false, false)
Bill Wendling2b58ce52008-11-04 02:10:20 +000052
Bill Wendlingea442812013-06-19 20:51:24 +000053FunctionPass *llvm::createStackProtectorPass(const TargetMachine *TM) {
54 return new StackProtector(TM);
Bill Wendling2b58ce52008-11-04 02:10:20 +000055}
56
57bool StackProtector::runOnFunction(Function &Fn) {
58 F = &Fn;
59 M = F->getParent();
Cameron Zwarich80f6a502011-01-08 17:01:52 +000060 DT = getAnalysisIfAvailable<DominatorTree>();
Bill Wendlingea442812013-06-19 20:51:24 +000061 TLI = TM->getTargetLowering();
Bill Wendling2b58ce52008-11-04 02:10:20 +000062
63 if (!RequiresStackProtector()) return false;
Bill Wendling6d86f3c2012-08-13 21:20:43 +000064
Bill Wendling0dcba2f2013-07-22 20:15:21 +000065 Attribute Attr =
66 Fn.getAttributes().getAttribute(AttributeSet::FunctionIndex,
67 "stack-protector-buffer-size");
68 if (Attr.isStringAttribute())
Benjamin Kramer54cf1412013-09-09 17:38:01 +000069 Attr.getValueAsString().getAsInteger(10, SSPBufferSize);
Bill Wendling0dcba2f2013-07-22 20:15:21 +000070
Bill Wendlinge4957fb2013-01-23 06:43:53 +000071 ++NumFunProtected;
Bill Wendling613f7742008-11-05 00:00:21 +000072 return InsertStackProtectors();
Bill Wendling2b58ce52008-11-04 02:10:20 +000073}
74
Bill Wendlinga67eda72012-08-17 20:59:56 +000075/// ContainsProtectableArray - Check whether the type either is an array or
76/// contains a char array of sufficient size so that we need stack protectors
77/// for it.
Bill Wendlinge4957fb2013-01-23 06:43:53 +000078bool StackProtector::ContainsProtectableArray(Type *Ty, bool Strong,
79 bool InStruct) const {
Bill Wendlinga67eda72012-08-17 20:59:56 +000080 if (!Ty) return false;
81 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
Bill Wendlinge4957fb2013-01-23 06:43:53 +000082 // In strong mode any array, regardless of type and size, triggers a
83 // protector
84 if (Strong)
85 return true;
Bill Wendlinga67eda72012-08-17 20:59:56 +000086 if (!AT->getElementType()->isIntegerTy(8)) {
Bill Wendlinga67eda72012-08-17 20:59:56 +000087 // If we're on a non-Darwin platform or we're inside of a structure, don't
88 // add stack protectors unless the array is a character array.
89 if (InStruct || !Trip.isOSDarwin())
90 return false;
91 }
92
93 // If an array has more than SSPBufferSize bytes of allocated space, then we
94 // emit stack protectors.
Bill Wendling0dcba2f2013-07-22 20:15:21 +000095 if (SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT))
Bill Wendlinga67eda72012-08-17 20:59:56 +000096 return true;
97 }
98
99 const StructType *ST = dyn_cast<StructType>(Ty);
100 if (!ST) return false;
101
102 for (StructType::element_iterator I = ST->element_begin(),
103 E = ST->element_end(); I != E; ++I)
Bill Wendlinge4957fb2013-01-23 06:43:53 +0000104 if (ContainsProtectableArray(*I, Strong, true))
Bill Wendlinga67eda72012-08-17 20:59:56 +0000105 return true;
106
107 return false;
108}
109
Bill Wendlinge4957fb2013-01-23 06:43:53 +0000110bool StackProtector::HasAddressTaken(const Instruction *AI) {
111 for (Value::const_use_iterator UI = AI->use_begin(), UE = AI->use_end();
112 UI != UE; ++UI) {
113 const User *U = *UI;
114 if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
115 if (AI == SI->getValueOperand())
116 return true;
117 } else if (const PtrToIntInst *SI = dyn_cast<PtrToIntInst>(U)) {
118 if (AI == SI->getOperand(0))
119 return true;
120 } else if (isa<CallInst>(U)) {
121 return true;
122 } else if (isa<InvokeInst>(U)) {
123 return true;
124 } else if (const SelectInst *SI = dyn_cast<SelectInst>(U)) {
125 if (HasAddressTaken(SI))
126 return true;
127 } else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
128 // Keep track of what PHI nodes we have already visited to ensure
129 // they are only visited once.
130 if (VisitedPHIs.insert(PN))
131 if (HasAddressTaken(PN))
132 return true;
133 } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
134 if (HasAddressTaken(GEP))
135 return true;
136 } else if (const BitCastInst *BI = dyn_cast<BitCastInst>(U)) {
137 if (HasAddressTaken(BI))
138 return true;
139 }
140 }
141 return false;
142}
143
144/// \brief Check whether or not this function needs a stack protector based
145/// upon the stack protector level.
146///
147/// We use two heuristics: a standard (ssp) and strong (sspstrong).
148/// The standard heuristic which will add a guard variable to functions that
149/// call alloca with a either a variable size or a size >= SSPBufferSize,
150/// functions with character buffers larger than SSPBufferSize, and functions
151/// with aggregates containing character buffers larger than SSPBufferSize. The
152/// strong heuristic will add a guard variables to functions that call alloca
153/// regardless of size, functions with any buffer regardless of type and size,
154/// functions with aggregates that contain any buffer regardless of type and
155/// size, and functions that contain stack-based variables that have had their
156/// address taken.
157bool StackProtector::RequiresStackProtector() {
158 bool Strong = false;
Bill Wendling831737d2012-12-30 10:32:01 +0000159 if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
160 Attribute::StackProtectReq))
Bill Wendlingc3348a72008-11-18 05:32:11 +0000161 return true;
Bill Wendlinge4957fb2013-01-23 06:43:53 +0000162 else if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
163 Attribute::StackProtectStrong))
164 Strong = true;
165 else if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
166 Attribute::StackProtect))
Bill Wendlingc3348a72008-11-18 05:32:11 +0000167 return false;
168
Bill Wendlingc3348a72008-11-18 05:32:11 +0000169 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
170 BasicBlock *BB = I;
171
172 for (BasicBlock::iterator
Bill Wendlinge4957fb2013-01-23 06:43:53 +0000173 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
Bill Wendlingc3348a72008-11-18 05:32:11 +0000174 if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
Bill Wendlinge4957fb2013-01-23 06:43:53 +0000175 if (AI->isArrayAllocation()) {
176 // SSP-Strong: Enable protectors for any call to alloca, regardless
177 // of size.
178 if (Strong)
179 return true;
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000180
Bill Wendlinge4957fb2013-01-23 06:43:53 +0000181 if (const ConstantInt *CI =
182 dyn_cast<ConstantInt>(AI->getArraySize())) {
Bill Wendling0dcba2f2013-07-22 20:15:21 +0000183 if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize)
Bill Wendlinge4957fb2013-01-23 06:43:53 +0000184 // A call to alloca with size >= SSPBufferSize requires
185 // stack protectors.
186 return true;
Bill Wendling0dcba2f2013-07-22 20:15:21 +0000187 } else {
188 // A call to alloca with a variable size requires protectors.
Bill Wendlinge4957fb2013-01-23 06:43:53 +0000189 return true;
Bill Wendling0dcba2f2013-07-22 20:15:21 +0000190 }
Bill Wendlinge4957fb2013-01-23 06:43:53 +0000191 }
192
193 if (ContainsProtectableArray(AI->getAllocatedType(), Strong))
Bill Wendlingc3348a72008-11-18 05:32:11 +0000194 return true;
195
Bill Wendlinge4957fb2013-01-23 06:43:53 +0000196 if (Strong && HasAddressTaken(AI)) {
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000197 ++NumAddrTaken;
Bill Wendlinga67eda72012-08-17 20:59:56 +0000198 return true;
Bill Wendlinge4957fb2013-01-23 06:43:53 +0000199 }
Bill Wendlingc3348a72008-11-18 05:32:11 +0000200 }
Bill Wendlinge4957fb2013-01-23 06:43:53 +0000201 }
Bill Wendlingc3348a72008-11-18 05:32:11 +0000202 }
203
204 return false;
205}
206
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000207static bool InstructionWillNotHaveChain(const Instruction *I) {
208 return !I->mayHaveSideEffects() && !I->mayReadFromMemory() &&
209 isSafeToSpeculativelyExecute(I);
210}
211
212/// Identify if RI has a previous instruction in the "Tail Position" and return
213/// it. Otherwise return 0.
214///
Michael Gottesmanb99272a2013-08-20 08:56:23 +0000215/// This is based off of the code in llvm::isInTailCallPosition. The difference
216/// is that it inverts the first part of llvm::isInTailCallPosition since
217/// isInTailCallPosition is checking if a call is in a tail call position, and
218/// we are searching for an unknown tail call that might be in the tail call
219/// position. Once we find the call though, the code uses the same refactored
220/// code, returnTypeIsEligibleForTailCall.
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000221static CallInst *FindPotentialTailCall(BasicBlock *BB, ReturnInst *RI,
222 const TargetLoweringBase *TLI) {
223 // Establish a reasonable upper bound on the maximum amount of instructions we
224 // will look through to find a tail call.
225 unsigned SearchCounter = 0;
226 const unsigned MaxSearch = 4;
227 bool NoInterposingChain = true;
228
229 for (BasicBlock::reverse_iterator I = llvm::next(BB->rbegin()), E = BB->rend();
230 I != E && SearchCounter < MaxSearch; ++I) {
231 Instruction *Inst = &*I;
232
233 // Skip over debug intrinsics and do not allow them to affect our MaxSearch
234 // counter.
235 if (isa<DbgInfoIntrinsic>(Inst))
236 continue;
237
238 // If we find a call and the following conditions are satisifed, then we
239 // have found a tail call that satisfies at least the target independent
240 // requirements of a tail call:
241 //
242 // 1. The call site has the tail marker.
243 //
244 // 2. The call site either will not cause the creation of a chain or if a
245 // chain is necessary there are no instructions in between the callsite and
246 // the call which would create an interposing chain.
247 //
248 // 3. The return type of the function does not impede tail call
249 // optimization.
250 if (CallInst *CI = dyn_cast<CallInst>(Inst)) {
251 if (CI->isTailCall() &&
252 (InstructionWillNotHaveChain(CI) || NoInterposingChain) &&
253 returnTypeIsEligibleForTailCall(BB->getParent(), CI, RI, *TLI))
254 return CI;
255 }
256
257 // If we did not find a call see if we have an instruction that may create
258 // an interposing chain.
259 NoInterposingChain = NoInterposingChain && InstructionWillNotHaveChain(Inst);
260
261 // Increment max search.
262 SearchCounter++;
263 }
264
265 return 0;
266}
267
Michael Gottesmanc03d5ec2013-07-22 20:44:11 +0000268/// Insert code into the entry block that stores the __stack_chk_guard
269/// variable onto the stack:
270///
271/// entry:
272/// StackGuardSlot = alloca i8*
273/// StackGuard = load __stack_chk_guard
274/// call void @llvm.stackprotect.create(StackGuard, StackGuardSlot)
275///
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000276/// Returns true if the platform/triple supports the stackprotectorcreate pseudo
277/// node.
278static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI,
Michael Gottesmanc03d5ec2013-07-22 20:44:11 +0000279 const TargetLoweringBase *TLI, const Triple &Trip,
280 AllocaInst *&AI, Value *&StackGuardVar) {
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000281 bool SupportsSelectionDAGSP = false;
Michael Gottesmanc03d5ec2013-07-22 20:44:11 +0000282 PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext());
283 unsigned AddressSpace, Offset;
284 if (TLI->getStackCookieLocation(AddressSpace, Offset)) {
285 Constant *OffsetVal =
286 ConstantInt::get(Type::getInt32Ty(RI->getContext()), Offset);
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000287
Michael Gottesmanc03d5ec2013-07-22 20:44:11 +0000288 StackGuardVar = ConstantExpr::getIntToPtr(OffsetVal,
289 PointerType::get(PtrTy,
290 AddressSpace));
291 } else if (Trip.getOS() == llvm::Triple::OpenBSD) {
292 StackGuardVar = M->getOrInsertGlobal("__guard_local", PtrTy);
293 cast<GlobalValue>(StackGuardVar)
294 ->setVisibility(GlobalValue::HiddenVisibility);
295 } else {
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000296 SupportsSelectionDAGSP = true;
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000297 StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);
Michael Gottesmanc03d5ec2013-07-22 20:44:11 +0000298 }
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000299
Benjamin Kramer54cf1412013-09-09 17:38:01 +0000300 IRBuilder<> B(&F->getEntryBlock().front());
301 AI = B.CreateAlloca(PtrTy, 0, "StackGuardSlot");
302 LoadInst *LI = B.CreateLoad(StackGuardVar, "StackGuard");
303 B.CreateCall2(Intrinsic::getDeclaration(M, Intrinsic::stackprotector), LI,
304 AI);
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000305
306 return SupportsSelectionDAGSP;
Michael Gottesmanc03d5ec2013-07-22 20:44:11 +0000307}
308
Bill Wendling613f7742008-11-05 00:00:21 +0000309/// InsertStackProtectors - Insert code into the prologue and epilogue of the
310/// function.
311///
312/// - The prologue code loads and stores the stack guard onto the stack.
313/// - The epilogue checks the value stored in the prologue against the original
314/// value. It calls __stack_chk_fail if they differ.
315bool StackProtector::InsertStackProtectors() {
Michael Gottesman236e3892013-08-09 21:26:18 +0000316 bool HasPrologue = false;
Michael Gottesmand4f47882013-08-20 08:56:26 +0000317 bool SupportsSelectionDAGSP =
318 EnableSelectionDAGSP && !TM->Options.EnableFastISel;
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +0000319 AllocaInst *AI = 0; // Place on stack that stores the stack guard.
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000320 Value *StackGuardVar = 0; // The stack guard variable.
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +0000321
Bill Wendling72056772008-11-10 21:13:10 +0000322 for (Function::iterator I = F->begin(), E = F->end(); I != E; ) {
Bill Wendlingc3348a72008-11-18 05:32:11 +0000323 BasicBlock *BB = I++;
Bill Wendlingc3348a72008-11-18 05:32:11 +0000324 ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
Michael Gottesmanade30752013-08-20 08:56:28 +0000325 if (!RI)
326 continue;
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +0000327
Michael Gottesman236e3892013-08-09 21:26:18 +0000328 if (!HasPrologue) {
329 HasPrologue = true;
Michael Gottesmand4f47882013-08-20 08:56:26 +0000330 SupportsSelectionDAGSP &= CreatePrologue(F, M, RI, TLI, Trip, AI,
331 StackGuardVar);
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000332 }
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000333
Michael Gottesmand4f47882013-08-20 08:56:26 +0000334 if (SupportsSelectionDAGSP) {
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000335 // Since we have a potential tail call, insert the special stack check
336 // intrinsic.
337 Instruction *InsertionPt = 0;
338 if (CallInst *CI = FindPotentialTailCall(BB, RI, TLI)) {
339 InsertionPt = CI;
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000340 } else {
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000341 InsertionPt = RI;
342 // At this point we know that BB has a return statement so it *DOES*
343 // have a terminator.
344 assert(InsertionPt != 0 && "BB must have a terminator instruction at "
345 "this point.");
346 }
347
348 Function *Intrinsic =
349 Intrinsic::getDeclaration(M, Intrinsic::stackprotectorcheck);
Benjamin Kramer54cf1412013-09-09 17:38:01 +0000350 CallInst::Create(Intrinsic, StackGuardVar, "", InsertionPt);
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000351
352 } else {
Michael Gottesman47d6e072013-08-20 08:46:13 +0000353 // If we do not support SelectionDAG based tail calls, generate IR level
354 // tail calls.
355 //
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000356 // For each block with a return instruction, convert this:
357 //
358 // return:
359 // ...
360 // ret ...
361 //
362 // into this:
363 //
364 // return:
365 // ...
366 // %1 = load __stack_chk_guard
367 // %2 = load StackGuardSlot
368 // %3 = cmp i1 %1, %2
369 // br i1 %3, label %SP_return, label %CallStackCheckFailBlk
370 //
371 // SP_return:
372 // ret ...
373 //
374 // CallStackCheckFailBlk:
375 // call void @__stack_chk_fail()
376 // unreachable
377
378 // Create the FailBB. We duplicate the BB every time since the MI tail
379 // merge pass will merge together all of the various BB into one including
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000380 // fail BB generated by the stack protector pseudo instruction.
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000381 BasicBlock *FailBB = CreateFailBB();
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000382
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000383 // Split the basic block before the return instruction.
384 BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return");
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000385
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000386 // Update the dominator tree if we need to.
387 if (DT && DT->isReachableFromEntry(BB)) {
388 DT->addNewBlock(NewBB, BB);
389 DT->addNewBlock(FailBB, BB);
390 }
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000391
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000392 // Remove default branch instruction to the new BB.
393 BB->getTerminator()->eraseFromParent();
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000394
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000395 // Move the newly created basic block to the point right after the old
396 // basic block so that it's in the "fall through" position.
397 NewBB->moveAfter(BB);
Michael Gottesmanc02dbeb2013-08-20 08:46:16 +0000398
Michael Gottesman3480d1b2013-08-20 08:36:53 +0000399 // Generate the stack protector instructions in the old basic block.
Benjamin Kramer54cf1412013-09-09 17:38:01 +0000400 IRBuilder<> B(BB);
401 LoadInst *LI1 = B.CreateLoad(StackGuardVar);
402 LoadInst *LI2 = B.CreateLoad(AI);
403 Value *Cmp = B.CreateICmpEQ(LI1, LI2);
404 B.CreateCondBr(Cmp, NewBB, FailBB);
Bill Wendling1fb615f2008-11-06 23:55:49 +0000405 }
Bill Wendling2b58ce52008-11-04 02:10:20 +0000406 }
Bill Wendling613f7742008-11-05 00:00:21 +0000407
Bill Wendling1fb615f2008-11-06 23:55:49 +0000408 // Return if we didn't modify any basic blocks. I.e., there are no return
409 // statements in the function.
Michael Gottesman236e3892013-08-09 21:26:18 +0000410 if (!HasPrologue)
411 return false;
Cameron Zwarich80f6a502011-01-08 17:01:52 +0000412
Bill Wendling613f7742008-11-05 00:00:21 +0000413 return true;
Bill Wendling2b58ce52008-11-04 02:10:20 +0000414}
415
416/// CreateFailBB - Create a basic block to jump to when the stack protector
417/// check fails.
Bill Wendling613f7742008-11-05 00:00:21 +0000418BasicBlock *StackProtector::CreateFailBB() {
Rafael Espindola62ed8d32013-06-07 16:35:57 +0000419 LLVMContext &Context = F->getContext();
420 BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F);
Benjamin Kramer54cf1412013-09-09 17:38:01 +0000421 IRBuilder<> B(FailBB);
Rafael Espindola62ed8d32013-06-07 16:35:57 +0000422 if (Trip.getOS() == llvm::Triple::OpenBSD) {
423 Constant *StackChkFail = M->getOrInsertFunction(
424 "__stack_smash_handler", Type::getVoidTy(Context),
425 Type::getInt8PtrTy(Context), NULL);
426
Benjamin Kramer54cf1412013-09-09 17:38:01 +0000427 B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
Rafael Espindola62ed8d32013-06-07 16:35:57 +0000428 } else {
429 Constant *StackChkFail = M->getOrInsertFunction(
430 "__stack_chk_fail", Type::getVoidTy(Context), NULL);
Benjamin Kramer54cf1412013-09-09 17:38:01 +0000431 B.CreateCall(StackChkFail);
Rafael Espindola62ed8d32013-06-07 16:35:57 +0000432 }
Benjamin Kramer54cf1412013-09-09 17:38:01 +0000433 B.CreateUnreachable();
Bill Wendling613f7742008-11-05 00:00:21 +0000434 return FailBB;
Bill Wendling2b58ce52008-11-04 02:10:20 +0000435}