xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/StackProtector.cpp (revision 6966ac055c3b7a39266fb982493330df7a097997)
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.
13 //
14 //===----------------------------------------------------------------------===//
15 
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>
50 
51 using namespace llvm;
52 
53 #define DEBUG_TYPE "stack-protector"
54 
55 STATISTIC(NumFunProtected, "Number of functions protected");
56 STATISTIC(NumAddrTaken, "Number of local variables that have their address"
57                         " taken.");
58 
59 static cl::opt<bool> EnableSelectionDAGSP("enable-selectiondag-sp",
60                                           cl::init(true), cl::Hidden);
61 
62 char StackProtector::ID = 0;
63 
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)
69 
70 FunctionPass *llvm::createStackProtectorPass() { return new StackProtector(); }
71 
72 void StackProtector::getAnalysisUsage(AnalysisUsage &AU) const {
73   AU.addRequired<TargetPassConfig>();
74   AU.addPreserved<DominatorTreeWrapperPass>();
75 }
76 
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;
88 
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
93 
94   if (!RequiresStackProtector())
95     return false;
96 
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;
103   }
104 
105   ++NumFunProtected;
106   return InsertStackProtectors();
107 }
108 
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;
125     }
126 
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;
132     }
133 
134     if (Strong)
135       // Require a protector for all arrays in strong mode
136       return true;
137   }
138 
139   const StructType *ST = dyn_cast<StructType>(Ty);
140   if (!ST)
141     return false;
142 
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;
154     }
155 
156   return NeedsProtector;
157 }
158 
159 bool StackProtector::HasAddressTaken(const Instruction *AI,
160                                 SmallPtrSetImpl<const PHINode *> &VisitedPHIs) {
161   for (const User *U : AI->users()) {
162     const auto *I = cast<Instruction>(U);
163     switch (I->getOpcode()) {
164     case Instruction::Store:
165       if (AI == cast<StoreInst>(I)->getValueOperand())
166         return true;
167       break;
168     case Instruction::AtomicCmpXchg:
169       // cmpxchg conceptually includes both a load and store from the same
170       // location. So, like store, the value being stored is what matters.
171       if (AI == cast<AtomicCmpXchgInst>(I)->getNewValOperand())
172         return true;
173       break;
174     case Instruction::PtrToInt:
175       if (AI == cast<PtrToIntInst>(I)->getOperand(0))
176         return true;
177       break;
178     case Instruction::Call: {
179       // Ignore intrinsics that do not become real instructions.
180       // TODO: Narrow this to intrinsics that have store-like effects.
181       const auto *CI = cast<CallInst>(I);
182       if (!isa<DbgInfoIntrinsic>(CI) && !CI->isLifetimeStartOrEnd())
183         return true;
184       break;
185     }
186     case Instruction::Invoke:
187       return true;
188     case Instruction::BitCast:
189     case Instruction::GetElementPtr:
190     case Instruction::Select:
191     case Instruction::AddrSpaceCast:
192       if (HasAddressTaken(I, VisitedPHIs))
193         return true;
194       break;
195     case Instruction::PHI: {
196       // Keep track of what PHI nodes we have already visited to ensure
197       // they are only visited once.
198       const auto *PN = cast<PHINode>(I);
199       if (VisitedPHIs.insert(PN).second)
200         if (HasAddressTaken(PN, VisitedPHIs))
201           return true;
202       break;
203     }
204     case Instruction::Load:
205     case Instruction::AtomicRMW:
206     case Instruction::Ret:
207       // These instructions take an address operand, but have load-like or
208       // other innocuous behavior that should not trigger a stack protector.
209       // atomicrmw conceptually has both load and store semantics, but the
210       // value being stored must be integer; so if a pointer is being stored,
211       // we'll catch it in the PtrToInt case above.
212       break;
213     default:
214       // Conservatively return true for any instruction that takes an address
215       // operand, but is not handled above.
216       return true;
217     }
218   }
219   return false;
220 }
221 
222 /// Search for the first call to the llvm.stackprotector intrinsic and return it
223 /// if present.
224 static const CallInst *findStackProtectorIntrinsic(Function &F) {
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(), Intrinsic::stackprotector))
230           return CI;
231   return nullptr;
232 }
233 
234 /// Check whether or not this function needs a stack protector based
235 /// upon the stack protector level.
236 ///
237 /// We use two heuristics: a standard (ssp) and strong (sspstrong).
238 /// The standard heuristic which will add a guard variable to functions that
239 /// call alloca with a either a variable size or a size >= SSPBufferSize,
240 /// functions with character buffers larger than SSPBufferSize, and functions
241 /// with aggregates containing character buffers larger than SSPBufferSize. The
242 /// strong heuristic will add a guard variables to functions that call alloca
243 /// regardless of size, functions with any buffer regardless of type and size,
244 /// functions with aggregates that contain any buffer regardless of type and
245 /// size, and functions that contain stack-based variables that have had their
246 /// address taken.
247 bool StackProtector::RequiresStackProtector() {
248   bool Strong = false;
249   bool NeedsProtector = false;
250   HasPrologue = findStackProtectorIntrinsic(*F);
251 
252   if (F->hasFnAttribute(Attribute::SafeStack))
253     return false;
254 
255   // We are constructing the OptimizationRemarkEmitter on the fly rather than
256   // using the analysis pass to avoid building DominatorTree and LoopInfo which
257   // are not available this late in the IR pipeline.
258   OptimizationRemarkEmitter ORE(F);
259 
260   if (F->hasFnAttribute(Attribute::StackProtectReq)) {
261     ORE.emit([&]() {
262       return OptimizationRemark(DEBUG_TYPE, "StackProtectorRequested", F)
263              << "Stack protection applied to function "
264              << ore::NV("Function", F)
265              << " due to a function attribute or command-line switch";
266     });
267     NeedsProtector = true;
268     Strong = true; // Use the same heuristic as strong to determine SSPLayout
269   } else if (F->hasFnAttribute(Attribute::StackProtectStrong))
270     Strong = true;
271   else if (HasPrologue)
272     NeedsProtector = true;
273   else if (!F->hasFnAttribute(Attribute::StackProtect))
274     return false;
275 
276   /// VisitedPHIs - The set of PHI nodes visited when determining
277   /// if a variable's reference has been taken.  This set
278   /// is maintained to ensure we don't visit the same PHI node multiple
279   /// times.
280   SmallPtrSet<const PHINode *, 16> VisitedPHIs;
281 
282   for (const BasicBlock &BB : *F) {
283     for (const Instruction &I : BB) {
284       if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
285         if (AI->isArrayAllocation()) {
286           auto RemarkBuilder = [&]() {
287             return OptimizationRemark(DEBUG_TYPE, "StackProtectorAllocaOrArray",
288                                       &I)
289                    << "Stack protection applied to function "
290                    << ore::NV("Function", F)
291                    << " due to a call to alloca or use of a variable length "
292                       "array";
293           };
294           if (const auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) {
295             if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) {
296               // A call to alloca with size >= SSPBufferSize requires
297               // stack protectors.
298               Layout.insert(std::make_pair(AI,
299                                            MachineFrameInfo::SSPLK_LargeArray));
300               ORE.emit(RemarkBuilder);
301               NeedsProtector = true;
302             } else if (Strong) {
303               // Require protectors for all alloca calls in strong mode.
304               Layout.insert(std::make_pair(AI,
305                                            MachineFrameInfo::SSPLK_SmallArray));
306               ORE.emit(RemarkBuilder);
307               NeedsProtector = true;
308             }
309           } else {
310             // A call to alloca with a variable size requires protectors.
311             Layout.insert(std::make_pair(AI,
312                                          MachineFrameInfo::SSPLK_LargeArray));
313             ORE.emit(RemarkBuilder);
314             NeedsProtector = true;
315           }
316           continue;
317         }
318 
319         bool IsLarge = false;
320         if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) {
321           Layout.insert(std::make_pair(AI, IsLarge
322                                        ? MachineFrameInfo::SSPLK_LargeArray
323                                        : MachineFrameInfo::SSPLK_SmallArray));
324           ORE.emit([&]() {
325             return OptimizationRemark(DEBUG_TYPE, "StackProtectorBuffer", &I)
326                    << "Stack protection applied to function "
327                    << ore::NV("Function", F)
328                    << " due to a stack allocated buffer or struct containing a "
329                       "buffer";
330           });
331           NeedsProtector = true;
332           continue;
333         }
334 
335         if (Strong && HasAddressTaken(AI, VisitedPHIs)) {
336           ++NumAddrTaken;
337           Layout.insert(std::make_pair(AI, MachineFrameInfo::SSPLK_AddrOf));
338           ORE.emit([&]() {
339             return OptimizationRemark(DEBUG_TYPE, "StackProtectorAddressTaken",
340                                       &I)
341                    << "Stack protection applied to function "
342                    << ore::NV("Function", F)
343                    << " due to the address of a local variable being taken";
344           });
345           NeedsProtector = true;
346         }
347       }
348     }
349   }
350 
351   return NeedsProtector;
352 }
353 
354 /// Create a stack guard loading and populate whether SelectionDAG SSP is
355 /// supported.
356 static Value *getStackGuard(const TargetLoweringBase *TLI, Module *M,
357                             IRBuilder<> &B,
358                             bool *SupportsSelectionDAGSP = nullptr) {
359   if (Value *Guard = TLI->getIRStackGuard(B))
360     return B.CreateLoad(B.getInt8PtrTy(), Guard, true, "StackGuard");
361 
362   // Use SelectionDAG SSP handling, since there isn't an IR guard.
363   //
364   // This is more or less weird, since we optionally output whether we
365   // should perform a SelectionDAG SP here. The reason is that it's strictly
366   // defined as !TLI->getIRStackGuard(B), where getIRStackGuard is also
367   // mutating. There is no way to get this bit without mutating the IR, so
368   // getting this bit has to happen in this right time.
369   //
370   // We could have define a new function TLI::supportsSelectionDAGSP(), but that
371   // will put more burden on the backends' overriding work, especially when it
372   // actually conveys the same information getIRStackGuard() already gives.
373   if (SupportsSelectionDAGSP)
374     *SupportsSelectionDAGSP = true;
375   TLI->insertSSPDeclarations(*M);
376   return B.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackguard));
377 }
378 
379 /// Insert code into the entry block that stores the stack guard
380 /// variable onto the stack:
381 ///
382 ///   entry:
383 ///     StackGuardSlot = alloca i8*
384 ///     StackGuard = <stack guard>
385 ///     call void @llvm.stackprotector(StackGuard, StackGuardSlot)
386 ///
387 /// Returns true if the platform/triple supports the stackprotectorcreate pseudo
388 /// node.
389 static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI,
390                            const TargetLoweringBase *TLI, AllocaInst *&AI) {
391   bool SupportsSelectionDAGSP = false;
392   IRBuilder<> B(&F->getEntryBlock().front());
393   PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext());
394   AI = B.CreateAlloca(PtrTy, nullptr, "StackGuardSlot");
395 
396   Value *GuardSlot = getStackGuard(TLI, M, B, &SupportsSelectionDAGSP);
397   B.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackprotector),
398                {GuardSlot, AI});
399   return SupportsSelectionDAGSP;
400 }
401 
402 /// InsertStackProtectors - Insert code into the prologue and epilogue of the
403 /// function.
404 ///
405 ///  - The prologue code loads and stores the stack guard onto the stack.
406 ///  - The epilogue checks the value stored in the prologue against the original
407 ///    value. It calls __stack_chk_fail if they differ.
408 bool StackProtector::InsertStackProtectors() {
409   // If the target wants to XOR the frame pointer into the guard value, it's
410   // impossible to emit the check in IR, so the target *must* support stack
411   // protection in SDAG.
412   bool SupportsSelectionDAGSP =
413       TLI->useStackGuardXorFP() ||
414       (EnableSelectionDAGSP && !TM->Options.EnableFastISel &&
415        !TM->Options.EnableGlobalISel);
416   AllocaInst *AI = nullptr;       // Place on stack that stores the stack guard.
417 
418   for (Function::iterator I = F->begin(), E = F->end(); I != E;) {
419     BasicBlock *BB = &*I++;
420     ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
421     if (!RI)
422       continue;
423 
424     // Generate prologue instrumentation if not already generated.
425     if (!HasPrologue) {
426       HasPrologue = true;
427       SupportsSelectionDAGSP &= CreatePrologue(F, M, RI, TLI, AI);
428     }
429 
430     // SelectionDAG based code generation. Nothing else needs to be done here.
431     // The epilogue instrumentation is postponed to SelectionDAG.
432     if (SupportsSelectionDAGSP)
433       break;
434 
435     // Find the stack guard slot if the prologue was not created by this pass
436     // itself via a previous call to CreatePrologue().
437     if (!AI) {
438       const CallInst *SPCall = findStackProtectorIntrinsic(*F);
439       assert(SPCall && "Call to llvm.stackprotector is missing");
440       AI = cast<AllocaInst>(SPCall->getArgOperand(1));
441     }
442 
443     // Set HasIRCheck to true, so that SelectionDAG will not generate its own
444     // version. SelectionDAG called 'shouldEmitSDCheck' to check whether
445     // instrumentation has already been generated.
446     HasIRCheck = true;
447 
448     // Generate epilogue instrumentation. The epilogue intrumentation can be
449     // function-based or inlined depending on which mechanism the target is
450     // providing.
451     if (Function *GuardCheck = TLI->getSSPStackGuardCheck(*M)) {
452       // Generate the function-based epilogue instrumentation.
453       // The target provides a guard check function, generate a call to it.
454       IRBuilder<> B(RI);
455       LoadInst *Guard = B.CreateLoad(B.getInt8PtrTy(), AI, true, "Guard");
456       CallInst *Call = B.CreateCall(GuardCheck, {Guard});
457       Call->setAttributes(GuardCheck->getAttributes());
458       Call->setCallingConv(GuardCheck->getCallingConv());
459     } else {
460       // Generate the epilogue with inline instrumentation.
461       // If we do not support SelectionDAG based tail calls, generate IR level
462       // tail calls.
463       //
464       // For each block with a return instruction, convert this:
465       //
466       //   return:
467       //     ...
468       //     ret ...
469       //
470       // into this:
471       //
472       //   return:
473       //     ...
474       //     %1 = <stack guard>
475       //     %2 = load StackGuardSlot
476       //     %3 = cmp i1 %1, %2
477       //     br i1 %3, label %SP_return, label %CallStackCheckFailBlk
478       //
479       //   SP_return:
480       //     ret ...
481       //
482       //   CallStackCheckFailBlk:
483       //     call void @__stack_chk_fail()
484       //     unreachable
485 
486       // Create the FailBB. We duplicate the BB every time since the MI tail
487       // merge pass will merge together all of the various BB into one including
488       // fail BB generated by the stack protector pseudo instruction.
489       BasicBlock *FailBB = CreateFailBB();
490 
491       // Split the basic block before the return instruction.
492       BasicBlock *NewBB = BB->splitBasicBlock(RI->getIterator(), "SP_return");
493 
494       // Update the dominator tree if we need to.
495       if (DT && DT->isReachableFromEntry(BB)) {
496         DT->addNewBlock(NewBB, BB);
497         DT->addNewBlock(FailBB, BB);
498       }
499 
500       // Remove default branch instruction to the new BB.
501       BB->getTerminator()->eraseFromParent();
502 
503       // Move the newly created basic block to the point right after the old
504       // basic block so that it's in the "fall through" position.
505       NewBB->moveAfter(BB);
506 
507       // Generate the stack protector instructions in the old basic block.
508       IRBuilder<> B(BB);
509       Value *Guard = getStackGuard(TLI, M, B);
510       LoadInst *LI2 = B.CreateLoad(B.getInt8PtrTy(), AI, true);
511       Value *Cmp = B.CreateICmpEQ(Guard, LI2);
512       auto SuccessProb =
513           BranchProbabilityInfo::getBranchProbStackProtector(true);
514       auto FailureProb =
515           BranchProbabilityInfo::getBranchProbStackProtector(false);
516       MDNode *Weights = MDBuilder(F->getContext())
517                             .createBranchWeights(SuccessProb.getNumerator(),
518                                                  FailureProb.getNumerator());
519       B.CreateCondBr(Cmp, NewBB, FailBB, Weights);
520     }
521   }
522 
523   // Return if we didn't modify any basic blocks. i.e., there are no return
524   // statements in the function.
525   return HasPrologue;
526 }
527 
528 /// CreateFailBB - Create a basic block to jump to when the stack protector
529 /// check fails.
530 BasicBlock *StackProtector::CreateFailBB() {
531   LLVMContext &Context = F->getContext();
532   BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F);
533   IRBuilder<> B(FailBB);
534   B.SetCurrentDebugLocation(DebugLoc::get(0, 0, F->getSubprogram()));
535   if (Trip.isOSOpenBSD()) {
536     FunctionCallee StackChkFail = M->getOrInsertFunction(
537         "__stack_smash_handler", Type::getVoidTy(Context),
538         Type::getInt8PtrTy(Context));
539 
540     B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
541   } else {
542     FunctionCallee StackChkFail =
543         M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
544 
545     B.CreateCall(StackChkFail, {});
546   }
547   B.CreateUnreachable();
548   return FailBB;
549 }
550 
551 bool StackProtector::shouldEmitSDCheck(const BasicBlock &BB) const {
552   return HasPrologue && !HasIRCheck && isa<ReturnInst>(BB.getTerminator());
553 }
554 
555 void StackProtector::copyToMachineFrameInfo(MachineFrameInfo &MFI) const {
556   if (Layout.empty())
557     return;
558 
559   for (int I = 0, E = MFI.getObjectIndexEnd(); I != E; ++I) {
560     if (MFI.isDeadObjectIndex(I))
561       continue;
562 
563     const AllocaInst *AI = MFI.getObjectAllocation(I);
564     if (!AI)
565       continue;
566 
567     SSPLayoutMap::const_iterator LI = Layout.find(AI);
568     if (LI == Layout.end())
569       continue;
570 
571     MFI.setObjectSSPLayout(I, LI->second);
572   }
573 }
574