Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / CodeGen / StackProtector.cpp
blob34942bc1b6c33138a2ef0f314aeed6b377c735f3
1 //===- StackProtector.cpp - Stack Protector Insertion ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass inserts stack protectors into functions which need them. A variable
10 // with a random value in it is stored onto the stack before the local variables
11 // are allocated. Upon exiting the block, the stored value is checked. If it's
12 // changed, then there was some sort of violation and the program aborts.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/CodeGen/StackProtector.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/BranchProbabilityInfo.h"
20 #include "llvm/Analysis/EHPersonalities.h"
21 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/CodeGen/TargetLowering.h"
24 #include "llvm/CodeGen/TargetPassConfig.h"
25 #include "llvm/CodeGen/TargetSubtargetInfo.h"
26 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/BasicBlock.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/DebugInfo.h"
31 #include "llvm/IR/DebugLoc.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/Dominators.h"
34 #include "llvm/IR/Function.h"
35 #include "llvm/IR/IRBuilder.h"
36 #include "llvm/IR/Instruction.h"
37 #include "llvm/IR/Instructions.h"
38 #include "llvm/IR/IntrinsicInst.h"
39 #include "llvm/IR/Intrinsics.h"
40 #include "llvm/IR/MDBuilder.h"
41 #include "llvm/IR/Module.h"
42 #include "llvm/IR/Type.h"
43 #include "llvm/IR/User.h"
44 #include "llvm/Pass.h"
45 #include "llvm/Support/Casting.h"
46 #include "llvm/Support/CommandLine.h"
47 #include "llvm/Target/TargetMachine.h"
48 #include "llvm/Target/TargetOptions.h"
49 #include <utility>
51 using namespace llvm;
53 #define DEBUG_TYPE "stack-protector"
55 STATISTIC(NumFunProtected, "Number of functions protected");
56 STATISTIC(NumAddrTaken, "Number of local variables that have their address"
57 " taken.");
59 static cl::opt<bool> EnableSelectionDAGSP("enable-selectiondag-sp",
60 cl::init(true), cl::Hidden);
62 char StackProtector::ID = 0;
64 INITIALIZE_PASS_BEGIN(StackProtector, DEBUG_TYPE,
65 "Insert stack protectors", false, true)
66 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
67 INITIALIZE_PASS_END(StackProtector, DEBUG_TYPE,
68 "Insert stack protectors", false, true)
70 FunctionPass *llvm::createStackProtectorPass() { return new StackProtector(); }
72 void StackProtector::getAnalysisUsage(AnalysisUsage &AU) const {
73 AU.addRequired<TargetPassConfig>();
74 AU.addPreserved<DominatorTreeWrapperPass>();
77 bool StackProtector::runOnFunction(Function &Fn) {
78 F = &Fn;
79 M = F->getParent();
80 DominatorTreeWrapperPass *DTWP =
81 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
82 DT = DTWP ? &DTWP->getDomTree() : nullptr;
83 TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
84 Trip = TM->getTargetTriple();
85 TLI = TM->getSubtargetImpl(Fn)->getTargetLowering();
86 HasPrologue = false;
87 HasIRCheck = false;
89 Attribute Attr = Fn.getFnAttribute("stack-protector-buffer-size");
90 if (Attr.isStringAttribute() &&
91 Attr.getValueAsString().getAsInteger(10, SSPBufferSize))
92 return false; // Invalid integer string
94 if (!RequiresStackProtector())
95 return false;
97 // TODO(etienneb): Functions with funclets are not correctly supported now.
98 // Do nothing if this is funclet-based personality.
99 if (Fn.hasPersonalityFn()) {
100 EHPersonality Personality = classifyEHPersonality(Fn.getPersonalityFn());
101 if (isFuncletEHPersonality(Personality))
102 return false;
105 ++NumFunProtected;
106 return InsertStackProtectors();
109 /// \param [out] IsLarge is set to true if a protectable array is found and
110 /// it is "large" ( >= ssp-buffer-size). In the case of a structure with
111 /// multiple arrays, this gets set if any of them is large.
112 bool StackProtector::ContainsProtectableArray(Type *Ty, bool &IsLarge,
113 bool Strong,
114 bool InStruct) const {
115 if (!Ty)
116 return false;
117 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
118 if (!AT->getElementType()->isIntegerTy(8)) {
119 // If we're on a non-Darwin platform or we're inside of a structure, don't
120 // add stack protectors unless the array is a character array.
121 // However, in strong mode any array, regardless of type and size,
122 // triggers a protector.
123 if (!Strong && (InStruct || !Trip.isOSDarwin()))
124 return false;
127 // If an array has more than SSPBufferSize bytes of allocated space, then we
128 // emit stack protectors.
129 if (SSPBufferSize <= M->getDataLayout().getTypeAllocSize(AT)) {
130 IsLarge = true;
131 return true;
134 if (Strong)
135 // Require a protector for all arrays in strong mode
136 return true;
139 const StructType *ST = dyn_cast<StructType>(Ty);
140 if (!ST)
141 return false;
143 bool NeedsProtector = false;
144 for (StructType::element_iterator I = ST->element_begin(),
145 E = ST->element_end();
146 I != E; ++I)
147 if (ContainsProtectableArray(*I, IsLarge, Strong, true)) {
148 // If the element is a protectable array and is large (>= SSPBufferSize)
149 // then we are done. If the protectable array is not large, then
150 // keep looking in case a subsequent element is a large array.
151 if (IsLarge)
152 return true;
153 NeedsProtector = true;
156 return NeedsProtector;
159 bool StackProtector::HasAddressTaken(const Instruction *AI) {
160 for (const User *U : AI->users()) {
161 if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
162 if (AI == SI->getValueOperand())
163 return true;
164 } else if (const PtrToIntInst *SI = dyn_cast<PtrToIntInst>(U)) {
165 if (AI == SI->getOperand(0))
166 return true;
167 } else if (const CallInst *CI = dyn_cast<CallInst>(U)) {
168 // Ignore intrinsics that are not calls. TODO: Use isLoweredToCall().
169 if (!isa<DbgInfoIntrinsic>(CI) && !CI->isLifetimeStartOrEnd())
170 return true;
171 } else if (isa<InvokeInst>(U)) {
172 return true;
173 } else if (const SelectInst *SI = dyn_cast<SelectInst>(U)) {
174 if (HasAddressTaken(SI))
175 return true;
176 } else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
177 // Keep track of what PHI nodes we have already visited to ensure
178 // they are only visited once.
179 if (VisitedPHIs.insert(PN).second)
180 if (HasAddressTaken(PN))
181 return true;
182 } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
183 if (HasAddressTaken(GEP))
184 return true;
185 } else if (const BitCastInst *BI = dyn_cast<BitCastInst>(U)) {
186 if (HasAddressTaken(BI))
187 return true;
190 return false;
193 /// Search for the first call to the llvm.stackprotector intrinsic and return it
194 /// if present.
195 static const CallInst *findStackProtectorIntrinsic(Function &F) {
196 for (const BasicBlock &BB : F)
197 for (const Instruction &I : BB)
198 if (const CallInst *CI = dyn_cast<CallInst>(&I))
199 if (CI->getCalledFunction() ==
200 Intrinsic::getDeclaration(F.getParent(), Intrinsic::stackprotector))
201 return CI;
202 return nullptr;
205 /// Check whether or not this function needs a stack protector based
206 /// upon the stack protector level.
208 /// We use two heuristics: a standard (ssp) and strong (sspstrong).
209 /// The standard heuristic which will add a guard variable to functions that
210 /// call alloca with a either a variable size or a size >= SSPBufferSize,
211 /// functions with character buffers larger than SSPBufferSize, and functions
212 /// with aggregates containing character buffers larger than SSPBufferSize. The
213 /// strong heuristic will add a guard variables to functions that call alloca
214 /// regardless of size, functions with any buffer regardless of type and size,
215 /// functions with aggregates that contain any buffer regardless of type and
216 /// size, and functions that contain stack-based variables that have had their
217 /// address taken.
218 bool StackProtector::RequiresStackProtector() {
219 bool Strong = false;
220 bool NeedsProtector = false;
221 HasPrologue = findStackProtectorIntrinsic(*F);
223 if (F->hasFnAttribute(Attribute::SafeStack))
224 return false;
226 // We are constructing the OptimizationRemarkEmitter on the fly rather than
227 // using the analysis pass to avoid building DominatorTree and LoopInfo which
228 // are not available this late in the IR pipeline.
229 OptimizationRemarkEmitter ORE(F);
231 if (F->hasFnAttribute(Attribute::StackProtectReq)) {
232 ORE.emit([&]() {
233 return OptimizationRemark(DEBUG_TYPE, "StackProtectorRequested", F)
234 << "Stack protection applied to function "
235 << ore::NV("Function", F)
236 << " due to a function attribute or command-line switch";
238 NeedsProtector = true;
239 Strong = true; // Use the same heuristic as strong to determine SSPLayout
240 } else if (F->hasFnAttribute(Attribute::StackProtectStrong))
241 Strong = true;
242 else if (HasPrologue)
243 NeedsProtector = true;
244 else if (!F->hasFnAttribute(Attribute::StackProtect))
245 return false;
247 for (const BasicBlock &BB : *F) {
248 for (const Instruction &I : BB) {
249 if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
250 if (AI->isArrayAllocation()) {
251 auto RemarkBuilder = [&]() {
252 return OptimizationRemark(DEBUG_TYPE, "StackProtectorAllocaOrArray",
254 << "Stack protection applied to function "
255 << ore::NV("Function", F)
256 << " due to a call to alloca or use of a variable length "
257 "array";
259 if (const auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) {
260 if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) {
261 // A call to alloca with size >= SSPBufferSize requires
262 // stack protectors.
263 Layout.insert(std::make_pair(AI,
264 MachineFrameInfo::SSPLK_LargeArray));
265 ORE.emit(RemarkBuilder);
266 NeedsProtector = true;
267 } else if (Strong) {
268 // Require protectors for all alloca calls in strong mode.
269 Layout.insert(std::make_pair(AI,
270 MachineFrameInfo::SSPLK_SmallArray));
271 ORE.emit(RemarkBuilder);
272 NeedsProtector = true;
274 } else {
275 // A call to alloca with a variable size requires protectors.
276 Layout.insert(std::make_pair(AI,
277 MachineFrameInfo::SSPLK_LargeArray));
278 ORE.emit(RemarkBuilder);
279 NeedsProtector = true;
281 continue;
284 bool IsLarge = false;
285 if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) {
286 Layout.insert(std::make_pair(AI, IsLarge
287 ? MachineFrameInfo::SSPLK_LargeArray
288 : MachineFrameInfo::SSPLK_SmallArray));
289 ORE.emit([&]() {
290 return OptimizationRemark(DEBUG_TYPE, "StackProtectorBuffer", &I)
291 << "Stack protection applied to function "
292 << ore::NV("Function", F)
293 << " due to a stack allocated buffer or struct containing a "
294 "buffer";
296 NeedsProtector = true;
297 continue;
300 if (Strong && HasAddressTaken(AI)) {
301 ++NumAddrTaken;
302 Layout.insert(std::make_pair(AI, MachineFrameInfo::SSPLK_AddrOf));
303 ORE.emit([&]() {
304 return OptimizationRemark(DEBUG_TYPE, "StackProtectorAddressTaken",
306 << "Stack protection applied to function "
307 << ore::NV("Function", F)
308 << " due to the address of a local variable being taken";
310 NeedsProtector = true;
316 return NeedsProtector;
319 /// Create a stack guard loading and populate whether SelectionDAG SSP is
320 /// supported.
321 static Value *getStackGuard(const TargetLoweringBase *TLI, Module *M,
322 IRBuilder<> &B,
323 bool *SupportsSelectionDAGSP = nullptr) {
324 if (Value *Guard = TLI->getIRStackGuard(B))
325 return B.CreateLoad(B.getInt8PtrTy(), Guard, true, "StackGuard");
327 // Use SelectionDAG SSP handling, since there isn't an IR guard.
329 // This is more or less weird, since we optionally output whether we
330 // should perform a SelectionDAG SP here. The reason is that it's strictly
331 // defined as !TLI->getIRStackGuard(B), where getIRStackGuard is also
332 // mutating. There is no way to get this bit without mutating the IR, so
333 // getting this bit has to happen in this right time.
335 // We could have define a new function TLI::supportsSelectionDAGSP(), but that
336 // will put more burden on the backends' overriding work, especially when it
337 // actually conveys the same information getIRStackGuard() already gives.
338 if (SupportsSelectionDAGSP)
339 *SupportsSelectionDAGSP = true;
340 TLI->insertSSPDeclarations(*M);
341 return B.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackguard));
344 /// Insert code into the entry block that stores the stack guard
345 /// variable onto the stack:
347 /// entry:
348 /// StackGuardSlot = alloca i8*
349 /// StackGuard = <stack guard>
350 /// call void @llvm.stackprotector(StackGuard, StackGuardSlot)
352 /// Returns true if the platform/triple supports the stackprotectorcreate pseudo
353 /// node.
354 static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI,
355 const TargetLoweringBase *TLI, AllocaInst *&AI) {
356 bool SupportsSelectionDAGSP = false;
357 IRBuilder<> B(&F->getEntryBlock().front());
358 PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext());
359 AI = B.CreateAlloca(PtrTy, nullptr, "StackGuardSlot");
361 Value *GuardSlot = getStackGuard(TLI, M, B, &SupportsSelectionDAGSP);
362 B.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackprotector),
363 {GuardSlot, AI});
364 return SupportsSelectionDAGSP;
367 /// InsertStackProtectors - Insert code into the prologue and epilogue of the
368 /// function.
370 /// - The prologue code loads and stores the stack guard onto the stack.
371 /// - The epilogue checks the value stored in the prologue against the original
372 /// value. It calls __stack_chk_fail if they differ.
373 bool StackProtector::InsertStackProtectors() {
374 // If the target wants to XOR the frame pointer into the guard value, it's
375 // impossible to emit the check in IR, so the target *must* support stack
376 // protection in SDAG.
377 bool SupportsSelectionDAGSP =
378 TLI->useStackGuardXorFP() ||
379 (EnableSelectionDAGSP && !TM->Options.EnableFastISel &&
380 !TM->Options.EnableGlobalISel);
381 AllocaInst *AI = nullptr; // Place on stack that stores the stack guard.
383 for (Function::iterator I = F->begin(), E = F->end(); I != E;) {
384 BasicBlock *BB = &*I++;
385 ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
386 if (!RI)
387 continue;
389 // Generate prologue instrumentation if not already generated.
390 if (!HasPrologue) {
391 HasPrologue = true;
392 SupportsSelectionDAGSP &= CreatePrologue(F, M, RI, TLI, AI);
395 // SelectionDAG based code generation. Nothing else needs to be done here.
396 // The epilogue instrumentation is postponed to SelectionDAG.
397 if (SupportsSelectionDAGSP)
398 break;
400 // Find the stack guard slot if the prologue was not created by this pass
401 // itself via a previous call to CreatePrologue().
402 if (!AI) {
403 const CallInst *SPCall = findStackProtectorIntrinsic(*F);
404 assert(SPCall && "Call to llvm.stackprotector is missing");
405 AI = cast<AllocaInst>(SPCall->getArgOperand(1));
408 // Set HasIRCheck to true, so that SelectionDAG will not generate its own
409 // version. SelectionDAG called 'shouldEmitSDCheck' to check whether
410 // instrumentation has already been generated.
411 HasIRCheck = true;
413 // Generate epilogue instrumentation. The epilogue intrumentation can be
414 // function-based or inlined depending on which mechanism the target is
415 // providing.
416 if (Function *GuardCheck = TLI->getSSPStackGuardCheck(*M)) {
417 // Generate the function-based epilogue instrumentation.
418 // The target provides a guard check function, generate a call to it.
419 IRBuilder<> B(RI);
420 LoadInst *Guard = B.CreateLoad(B.getInt8PtrTy(), AI, true, "Guard");
421 CallInst *Call = B.CreateCall(GuardCheck, {Guard});
422 Call->setAttributes(GuardCheck->getAttributes());
423 Call->setCallingConv(GuardCheck->getCallingConv());
424 } else {
425 // Generate the epilogue with inline instrumentation.
426 // If we do not support SelectionDAG based tail calls, generate IR level
427 // tail calls.
429 // For each block with a return instruction, convert this:
431 // return:
432 // ...
433 // ret ...
435 // into this:
437 // return:
438 // ...
439 // %1 = <stack guard>
440 // %2 = load StackGuardSlot
441 // %3 = cmp i1 %1, %2
442 // br i1 %3, label %SP_return, label %CallStackCheckFailBlk
444 // SP_return:
445 // ret ...
447 // CallStackCheckFailBlk:
448 // call void @__stack_chk_fail()
449 // unreachable
451 // Create the FailBB. We duplicate the BB every time since the MI tail
452 // merge pass will merge together all of the various BB into one including
453 // fail BB generated by the stack protector pseudo instruction.
454 BasicBlock *FailBB = CreateFailBB();
456 // Split the basic block before the return instruction.
457 BasicBlock *NewBB = BB->splitBasicBlock(RI->getIterator(), "SP_return");
459 // Update the dominator tree if we need to.
460 if (DT && DT->isReachableFromEntry(BB)) {
461 DT->addNewBlock(NewBB, BB);
462 DT->addNewBlock(FailBB, BB);
465 // Remove default branch instruction to the new BB.
466 BB->getTerminator()->eraseFromParent();
468 // Move the newly created basic block to the point right after the old
469 // basic block so that it's in the "fall through" position.
470 NewBB->moveAfter(BB);
472 // Generate the stack protector instructions in the old basic block.
473 IRBuilder<> B(BB);
474 Value *Guard = getStackGuard(TLI, M, B);
475 LoadInst *LI2 = B.CreateLoad(B.getInt8PtrTy(), AI, true);
476 Value *Cmp = B.CreateICmpEQ(Guard, LI2);
477 auto SuccessProb =
478 BranchProbabilityInfo::getBranchProbStackProtector(true);
479 auto FailureProb =
480 BranchProbabilityInfo::getBranchProbStackProtector(false);
481 MDNode *Weights = MDBuilder(F->getContext())
482 .createBranchWeights(SuccessProb.getNumerator(),
483 FailureProb.getNumerator());
484 B.CreateCondBr(Cmp, NewBB, FailBB, Weights);
488 // Return if we didn't modify any basic blocks. i.e., there are no return
489 // statements in the function.
490 return HasPrologue;
493 /// CreateFailBB - Create a basic block to jump to when the stack protector
494 /// check fails.
495 BasicBlock *StackProtector::CreateFailBB() {
496 LLVMContext &Context = F->getContext();
497 BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F);
498 IRBuilder<> B(FailBB);
499 B.SetCurrentDebugLocation(DebugLoc::get(0, 0, F->getSubprogram()));
500 if (Trip.isOSOpenBSD()) {
501 FunctionCallee StackChkFail = M->getOrInsertFunction(
502 "__stack_smash_handler", Type::getVoidTy(Context),
503 Type::getInt8PtrTy(Context));
505 B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
506 } else {
507 FunctionCallee StackChkFail =
508 M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
510 B.CreateCall(StackChkFail, {});
512 B.CreateUnreachable();
513 return FailBB;
516 bool StackProtector::shouldEmitSDCheck(const BasicBlock &BB) const {
517 return HasPrologue && !HasIRCheck && dyn_cast<ReturnInst>(BB.getTerminator());
520 void StackProtector::copyToMachineFrameInfo(MachineFrameInfo &MFI) const {
521 if (Layout.empty())
522 return;
524 for (int I = 0, E = MFI.getObjectIndexEnd(); I != E; ++I) {
525 if (MFI.isDeadObjectIndex(I))
526 continue;
528 const AllocaInst *AI = MFI.getObjectAllocation(I);
529 if (!AI)
530 continue;
532 SSPLayoutMap::const_iterator LI = Layout.find(AI);
533 if (LI == Layout.end())
534 continue;
536 MFI.setObjectSSPLayout(I, LI->second);