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 "CodeGenOptions.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/Analysis/BranchProbabilityInfo.h"
21#include "llvm/Analysis/MemoryLocation.h"
22#include "llvm/Analysis/OptimizationRemarkEmitter.h"
23#include "llvm/CodeGen/Analysis.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/CodeGen/TargetLowering.h"
26#include "llvm/CodeGen/TargetPassConfig.h"
27#include "llvm/CodeGen/TargetSubtargetInfo.h"
28#include "llvm/IR/Attributes.h"
29#include "llvm/IR/BasicBlock.h"
30#include "llvm/IR/Constants.h"
31#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/DerivedTypes.h"
33#include "llvm/IR/Dominators.h"
34#include "llvm/IR/EHPersonalities.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/IRBuilder.h"
37#include "llvm/IR/Instruction.h"
38#include "llvm/IR/Instructions.h"
39#include "llvm/IR/IntrinsicInst.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/MDBuilder.h"
42#include "llvm/IR/Module.h"
43#include "llvm/IR/Type.h"
44#include "llvm/IR/User.h"
45#include "llvm/InitializePasses.h"
46#include "llvm/Pass.h"
47#include "llvm/Support/Casting.h"
48#include "llvm/Target/TargetMachine.h"
49#include "llvm/Target/TargetOptions.h"
50#include "llvm/Transforms/Utils/BasicBlockUtils.h"
51#include <optional>
52
53using namespace llvm;
54
55#define DEBUG_TYPE "stack-protector"
56
57STATISTIC(NumFunProtected, "Number of functions protected");
58STATISTIC(NumAddrTaken, "Number of local variables that have their address"
59 " taken.");
60
61/// InsertStackProtectors - Insert code into the prologue and epilogue of the
62/// function.
63///
64/// - The prologue code loads and stores the stack guard onto the stack.
65/// - The epilogue checks the value stored in the prologue against the original
66/// value. It calls __stack_chk_fail if they differ.
67static bool InsertStackProtectors(const TargetLowering &TLI,
68 const LibcallLoweringInfo &Libcalls,
69 Function *F, DomTreeUpdater *DTU,
70 bool &HasPrologue, bool &HasIRCheck);
71
72/// CreateFailBB - Create a basic block to jump to when the stack protector
73/// check fails.
74static BasicBlock *CreateFailBB(Function *F,
75 const LibcallLoweringInfo &Libcalls);
76
77bool SSPLayoutInfo::shouldEmitSDCheck(const BasicBlock &BB) const {
78 return HasPrologue && !HasIRCheck && isa<ReturnInst>(Val: BB.getTerminator());
79}
80
81void SSPLayoutInfo::copyToMachineFrameInfo(MachineFrameInfo &MFI) const {
82 if (Layout.empty())
83 return;
84
85 for (int I = 0, E = MFI.getObjectIndexEnd(); I != E; ++I) {
86 if (MFI.isDeadObjectIndex(ObjectIdx: I))
87 continue;
88
89 const AllocaInst *AI = MFI.getObjectAllocation(ObjectIdx: I);
90 if (!AI)
91 continue;
92
93 SSPLayoutMap::const_iterator LI = Layout.find(Val: AI);
94 if (LI == Layout.end())
95 continue;
96
97 MFI.setObjectSSPLayout(ObjectIdx: I, Kind: LI->second);
98 }
99}
100
101SSPLayoutInfo SSPLayoutAnalysis::run(Function &F,
102 FunctionAnalysisManager &FAM) {
103
104 SSPLayoutInfo Info;
105 Info.RequireStackProtector =
106 SSPLayoutAnalysis::requiresStackProtector(F: &F, Layout: &Info.Layout);
107 Info.SSPBufferSize = F.getFnAttributeAsParsedInteger(
108 Kind: "stack-protector-buffer-size", Default: SSPLayoutInfo::DefaultSSPBufferSize);
109 return Info;
110}
111
112AnalysisKey SSPLayoutAnalysis::Key;
113
114PreservedAnalyses StackProtectorPass::run(Function &F,
115 FunctionAnalysisManager &FAM) {
116 auto &Info = FAM.getResult<SSPLayoutAnalysis>(IR&: F);
117 auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(IR&: F);
118 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
119
120 if (!Info.RequireStackProtector)
121 return PreservedAnalyses::all();
122
123 // TODO(etienneb): Functions with funclets are not correctly supported now.
124 // Do nothing if this is funclet-based personality.
125 if (F.hasPersonalityFn()) {
126 EHPersonality Personality = classifyEHPersonality(Pers: F.getPersonalityFn());
127 if (isFuncletEHPersonality(Pers: Personality))
128 return PreservedAnalyses::all();
129 }
130
131 auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(IR&: F);
132 const ModuleLibcallLoweringInfo *LibcallLowering =
133 MAMProxy.getCachedResult<LibcallLoweringModuleAnalysis>(IR&: *F.getParent());
134
135 if (!LibcallLowering) {
136 F.getContext().emitError(ErrorStr: "'" + LibcallLoweringModuleAnalysis::name() +
137 "' analysis required");
138 return PreservedAnalyses::all();
139 }
140
141 const TargetSubtargetInfo *STI = TM->getSubtargetImpl(F);
142 const TargetLowering *TLI = STI->getTargetLowering();
143 const LibcallLoweringInfo &Libcalls =
144 getLibcallLowering(ModuleInfo: *LibcallLowering, Subtarget: *STI);
145
146 ++NumFunProtected;
147 bool Changed = InsertStackProtectors(TLI: *TLI, Libcalls, F: &F, DTU: DT ? &DTU : nullptr,
148 HasPrologue&: Info.HasPrologue, HasIRCheck&: Info.HasIRCheck);
149#ifdef EXPENSIVE_CHECKS
150 assert((!DT ||
151 DTU.getDomTree().verify(DominatorTree::VerificationLevel::Full)) &&
152 "Failed to maintain validity of domtree!");
153#endif
154
155 if (!Changed)
156 return PreservedAnalyses::all();
157 PreservedAnalyses PA;
158 PA.preserve<SSPLayoutAnalysis>();
159 PA.preserve<DominatorTreeAnalysis>();
160 return PA;
161}
162
163char StackProtector::ID = 0;
164
165StackProtector::StackProtector() : FunctionPass(ID) {}
166
167INITIALIZE_PASS_BEGIN(StackProtector, DEBUG_TYPE,
168 "Insert stack protectors", false, true)
169INITIALIZE_PASS_DEPENDENCY(LibcallLoweringInfoWrapper)
170INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
171INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
172INITIALIZE_PASS_END(StackProtector, DEBUG_TYPE,
173 "Insert stack protectors", false, true)
174
175FunctionPass *llvm::createStackProtectorPass() { return new StackProtector(); }
176
177void StackProtector::getAnalysisUsage(AnalysisUsage &AU) const {
178 AU.addRequired<LibcallLoweringInfoWrapper>();
179 AU.addRequired<TargetPassConfig>();
180 AU.addPreserved<DominatorTreeWrapperPass>();
181}
182
183bool StackProtector::runOnFunction(Function &Fn) {
184 F = &Fn;
185 M = F->getParent();
186 if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>())
187 DTU.emplace(args&: DTWP->getDomTree(), args: DomTreeUpdater::UpdateStrategy::Lazy);
188 TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
189 LayoutInfo.HasPrologue = false;
190 LayoutInfo.HasIRCheck = false;
191
192 LayoutInfo.SSPBufferSize = Fn.getFnAttributeAsParsedInteger(
193 Kind: "stack-protector-buffer-size", Default: SSPLayoutInfo::DefaultSSPBufferSize);
194 if (!requiresStackProtector(F, Layout: &LayoutInfo.Layout))
195 return false;
196
197 // TODO(etienneb): Functions with funclets are not correctly supported now.
198 // Do nothing if this is funclet-based personality.
199 if (Fn.hasPersonalityFn()) {
200 EHPersonality Personality = classifyEHPersonality(Pers: Fn.getPersonalityFn());
201 if (isFuncletEHPersonality(Pers: Personality))
202 return false;
203 }
204
205 const TargetSubtargetInfo *Subtarget = TM->getSubtargetImpl(Fn);
206 const LibcallLoweringInfo &Libcalls =
207 getAnalysis<LibcallLoweringInfoWrapper>().getLibcallLowering(M: *M,
208 Subtarget: *Subtarget);
209
210 const TargetLowering *TLI = Subtarget->getTargetLowering();
211
212 ++NumFunProtected;
213 bool Changed =
214 InsertStackProtectors(TLI: *TLI, Libcalls, F, DTU: DTU ? &*DTU : nullptr,
215 HasPrologue&: LayoutInfo.HasPrologue, HasIRCheck&: LayoutInfo.HasIRCheck);
216#ifdef EXPENSIVE_CHECKS
217 assert((!DTU ||
218 DTU->getDomTree().verify(DominatorTree::VerificationLevel::Full)) &&
219 "Failed to maintain validity of domtree!");
220#endif
221 DTU.reset();
222 return Changed;
223}
224
225/// \param [out] IsLarge is set to true if a protectable array is found and
226/// it is "large" ( >= ssp-buffer-size). In the case of a structure with
227/// multiple arrays, this gets set if any of them is large.
228static bool ContainsProtectableArray(Type *Ty, Module *M, unsigned SSPBufferSize,
229 bool &IsLarge, bool Strong,
230 bool InStruct) {
231 if (!Ty)
232 return false;
233 if (ArrayType *AT = dyn_cast<ArrayType>(Val: Ty)) {
234 if (!AT->getElementType()->isIntegerTy(BitWidth: 8)) {
235 // If we're on a non-Darwin platform or we're inside of a structure, don't
236 // add stack protectors unless the array is a character array.
237 // However, in strong mode any array, regardless of type and size,
238 // triggers a protector.
239 if (!Strong && (InStruct || !M->getTargetTriple().isOSDarwin()))
240 return false;
241 }
242
243 // If an array has more than SSPBufferSize bytes of allocated space, then we
244 // emit stack protectors.
245 if (SSPBufferSize <= M->getDataLayout().getTypeAllocSize(Ty: AT)) {
246 IsLarge = true;
247 return true;
248 }
249
250 if (Strong)
251 // Require a protector for all arrays in strong mode
252 return true;
253 }
254
255 const StructType *ST = dyn_cast<StructType>(Val: Ty);
256 if (!ST)
257 return false;
258
259 bool NeedsProtector = false;
260 for (Type *ET : ST->elements())
261 if (ContainsProtectableArray(Ty: ET, M, SSPBufferSize, IsLarge, Strong, InStruct: true)) {
262 // If the element is a protectable array and is large (>= SSPBufferSize)
263 // then we are done. If the protectable array is not large, then
264 // keep looking in case a subsequent element is a large array.
265 if (IsLarge)
266 return true;
267 NeedsProtector = true;
268 }
269
270 return NeedsProtector;
271}
272
273/// Maximum remaining allocation size observed for a phi node, and how often
274/// the allocation size has already been decreased. We only allow a limited
275/// number of decreases.
276struct PhiInfo {
277 TypeSize AllocSize;
278 unsigned NumDecreased = 0;
279 static constexpr unsigned MaxNumDecreased = 3;
280 PhiInfo(TypeSize AllocSize) : AllocSize(AllocSize) {}
281};
282using PhiMap = SmallDenseMap<const PHINode *, PhiInfo, 16>;
283
284/// Check whether a stack allocation has its address taken.
285static bool HasAddressTaken(const Instruction *AI, TypeSize AllocSize,
286 Module *M,
287 PhiMap &VisitedPHIs) {
288 const DataLayout &DL = M->getDataLayout();
289 for (const User *U : AI->users()) {
290 const auto *I = cast<Instruction>(Val: U);
291 // If this instruction accesses memory make sure it doesn't access beyond
292 // the bounds of the allocated object.
293 std::optional<MemoryLocation> MemLoc = MemoryLocation::getOrNone(Inst: I);
294 if (MemLoc && MemLoc->Size.hasValue() &&
295 !TypeSize::isKnownGE(LHS: AllocSize, RHS: MemLoc->Size.getValue()))
296 return true;
297 switch (I->getOpcode()) {
298 case Instruction::Store:
299 if (AI == cast<StoreInst>(Val: I)->getValueOperand())
300 return true;
301 break;
302 case Instruction::AtomicCmpXchg:
303 // cmpxchg conceptually includes both a load and store from the same
304 // location. So, like store, the value being stored is what matters.
305 if (AI == cast<AtomicCmpXchgInst>(Val: I)->getNewValOperand())
306 return true;
307 break;
308 case Instruction::AtomicRMW:
309 if (AI == cast<AtomicRMWInst>(Val: I)->getValOperand())
310 return true;
311 break;
312 case Instruction::PtrToInt:
313 if (AI == cast<PtrToIntInst>(Val: I)->getOperand(i_nocapture: 0))
314 return true;
315 break;
316 case Instruction::Call: {
317 // Ignore intrinsics that do not become real instructions.
318 // TODO: Narrow this to intrinsics that have store-like effects.
319 const auto *CI = cast<CallInst>(Val: I);
320 if (!isa<PseudoProbeInst>(Val: CI) && !CI->isLifetimeStartOrEnd())
321 return true;
322 break;
323 }
324 case Instruction::Invoke:
325 return true;
326 case Instruction::GetElementPtr: {
327 // If the GEP offset is out-of-bounds, or is non-constant and so has to be
328 // assumed to be potentially out-of-bounds, then any memory access that
329 // would use it could also be out-of-bounds meaning stack protection is
330 // required.
331 const GetElementPtrInst *GEP = cast<GetElementPtrInst>(Val: I);
332 unsigned IndexSize = DL.getIndexTypeSizeInBits(Ty: I->getType());
333 APInt Offset(IndexSize, 0);
334 if (!GEP->accumulateConstantOffset(DL, Offset))
335 return true;
336 TypeSize OffsetSize = TypeSize::getFixed(ExactSize: Offset.getLimitedValue());
337 if (!TypeSize::isKnownGT(LHS: AllocSize, RHS: OffsetSize))
338 return true;
339 // Adjust AllocSize to be the space remaining after this offset.
340 // We can't subtract a fixed size from a scalable one, so in that case
341 // assume the scalable value is of minimum size.
342 TypeSize NewAllocSize =
343 TypeSize::getFixed(ExactSize: AllocSize.getKnownMinValue()) - OffsetSize;
344 if (HasAddressTaken(AI: I, AllocSize: NewAllocSize, M, VisitedPHIs))
345 return true;
346 break;
347 }
348 case Instruction::BitCast:
349 case Instruction::Select:
350 case Instruction::AddrSpaceCast:
351 if (HasAddressTaken(AI: I, AllocSize, M, VisitedPHIs))
352 return true;
353 break;
354 case Instruction::PHI: {
355 // Keep track of what PHI nodes we have already visited to ensure
356 // they are only visited once.
357 const auto *PN = cast<PHINode>(Val: I);
358 auto [It, Inserted] = VisitedPHIs.try_emplace(Key: PN, Args&: AllocSize);
359 if (!Inserted) {
360 if (TypeSize::isKnownGE(LHS: AllocSize, RHS: It->second.AllocSize))
361 break;
362
363 // Check again with smaller size.
364 if (It->second.NumDecreased == PhiInfo::MaxNumDecreased)
365 return true;
366
367 It->second.AllocSize = AllocSize;
368 ++It->second.NumDecreased;
369 }
370 if (HasAddressTaken(AI: PN, AllocSize, M, VisitedPHIs))
371 return true;
372 break;
373 }
374 case Instruction::Load:
375 case Instruction::Ret:
376 // These instructions take an address operand, but have load-like or
377 // other innocuous behavior that should not trigger a stack protector.
378 break;
379 default:
380 // Conservatively return true for any instruction that takes an address
381 // operand, but is not handled above.
382 return true;
383 }
384 }
385 return false;
386}
387
388/// Search for the first call to the llvm.stackprotector intrinsic and return it
389/// if present.
390static const CallInst *findStackProtectorIntrinsic(Function &F) {
391 for (const BasicBlock &BB : F)
392 for (const Instruction &I : BB)
393 if (const auto *II = dyn_cast<IntrinsicInst>(Val: &I))
394 if (II->getIntrinsicID() == Intrinsic::stackprotector)
395 return II;
396 return nullptr;
397}
398
399/// Check whether or not this function needs a stack protector based
400/// upon the stack protector level.
401///
402/// We use two heuristics: a standard (ssp) and strong (sspstrong).
403/// The standard heuristic which will add a guard variable to functions that
404/// call alloca with a either a variable size or a size >= SSPBufferSize,
405/// functions with character buffers larger than SSPBufferSize, and functions
406/// with aggregates containing character buffers larger than SSPBufferSize. The
407/// strong heuristic will add a guard variables to functions that call alloca
408/// regardless of size, functions with any buffer regardless of type and size,
409/// functions with aggregates that contain any buffer regardless of type and
410/// size, and functions that contain stack-based variables that have had their
411/// address taken.
412bool SSPLayoutAnalysis::requiresStackProtector(Function *F,
413 SSPLayoutMap *Layout) {
414 Module *M = F->getParent();
415 bool Strong = false;
416 bool NeedsProtector = false;
417
418 // The set of PHI nodes visited when determining if a variable's reference has
419 // been taken. This set is maintained to ensure we don't visit the same PHI
420 // node multiple times.
421 PhiMap VisitedPHIs;
422
423 unsigned SSPBufferSize = F->getFnAttributeAsParsedInteger(
424 Kind: "stack-protector-buffer-size", Default: SSPLayoutInfo::DefaultSSPBufferSize);
425
426 if (F->hasFnAttribute(Kind: Attribute::SafeStack))
427 return false;
428
429 // We are constructing the OptimizationRemarkEmitter on the fly rather than
430 // using the analysis pass to avoid building DominatorTree and LoopInfo which
431 // are not available this late in the IR pipeline.
432 OptimizationRemarkEmitter ORE(F);
433
434 if (F->hasFnAttribute(Kind: Attribute::StackProtectReq)) {
435 if (!Layout)
436 return true;
437 ORE.emit(RemarkBuilder: [&]() {
438 return OptimizationRemark(DEBUG_TYPE, "StackProtectorRequested", F)
439 << "Stack protection applied to function "
440 << ore::NV("Function", F)
441 << " due to a function attribute or command-line switch";
442 });
443 NeedsProtector = true;
444 Strong = true; // Use the same heuristic as strong to determine SSPLayout
445 } else if (F->hasFnAttribute(Kind: Attribute::StackProtectStrong))
446 Strong = true;
447 else if (!F->hasFnAttribute(Kind: Attribute::StackProtect))
448 return false;
449
450 for (const BasicBlock &BB : *F) {
451 for (const Instruction &I : BB) {
452 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Val: &I)) {
453 if (const MDNode *MD = AI->getMetadata(Kind: "stack-protector")) {
454 const auto *CI = mdconst::dyn_extract<ConstantInt>(MD: MD->getOperand(I: 0));
455 if (CI->isZero())
456 continue;
457 }
458 if (AI->isArrayAllocation()) {
459 auto RemarkBuilder = [&]() {
460 return OptimizationRemark(DEBUG_TYPE, "StackProtectorAllocaOrArray",
461 &I)
462 << "Stack protection applied to function "
463 << ore::NV("Function", F)
464 << " due to a call to alloca or use of a variable length "
465 "array";
466 };
467 if (const auto *CI = dyn_cast<ConstantInt>(Val: AI->getArraySize())) {
468 if (CI->getLimitedValue(Limit: SSPBufferSize) >= SSPBufferSize) {
469 // A call to alloca with size >= SSPBufferSize requires
470 // stack protectors.
471 if (!Layout)
472 return true;
473 Layout->insert(
474 KV: std::make_pair(x&: AI, y: MachineFrameInfo::SSPLK_LargeArray));
475 ORE.emit(RemarkBuilder);
476 NeedsProtector = true;
477 } else if (Strong) {
478 // Require protectors for all alloca calls in strong mode.
479 if (!Layout)
480 return true;
481 Layout->insert(
482 KV: std::make_pair(x&: AI, y: MachineFrameInfo::SSPLK_SmallArray));
483 ORE.emit(RemarkBuilder);
484 NeedsProtector = true;
485 }
486 } else {
487 // A call to alloca with a variable size requires protectors.
488 if (!Layout)
489 return true;
490 Layout->insert(
491 KV: std::make_pair(x&: AI, y: MachineFrameInfo::SSPLK_LargeArray));
492 ORE.emit(RemarkBuilder);
493 NeedsProtector = true;
494 }
495 continue;
496 }
497
498 bool IsLarge = false;
499 if (ContainsProtectableArray(Ty: AI->getAllocatedType(), M, SSPBufferSize,
500 IsLarge, Strong, InStruct: false)) {
501 if (!Layout)
502 return true;
503 Layout->insert(KV: std::make_pair(
504 x&: AI, y: IsLarge ? MachineFrameInfo::SSPLK_LargeArray
505 : MachineFrameInfo::SSPLK_SmallArray));
506 ORE.emit(RemarkBuilder: [&]() {
507 return OptimizationRemark(DEBUG_TYPE, "StackProtectorBuffer", &I)
508 << "Stack protection applied to function "
509 << ore::NV("Function", F)
510 << " due to a stack allocated buffer or struct containing a "
511 "buffer";
512 });
513 NeedsProtector = true;
514 continue;
515 }
516
517 if (Strong) {
518 std::optional<TypeSize> AllocSize =
519 AI->getAllocationSize(DL: M->getDataLayout());
520 if (!AllocSize || HasAddressTaken(AI, AllocSize: *AllocSize, M, VisitedPHIs)) {
521 ++NumAddrTaken;
522 if (!Layout)
523 return true;
524 Layout->insert(KV: std::make_pair(x&: AI, y: MachineFrameInfo::SSPLK_AddrOf));
525 ORE.emit(RemarkBuilder: [&]() {
526 return OptimizationRemark(DEBUG_TYPE,
527 "StackProtectorAddressTaken", &I)
528 << "Stack protection applied to function "
529 << ore::NV("Function", F)
530 << " due to the address of a local variable being taken";
531 });
532 NeedsProtector = true;
533 }
534 }
535 // Clear any PHIs that we visited, to make sure we examine all uses of
536 // any subsequent allocas that we look at.
537 VisitedPHIs.clear();
538 }
539 }
540 }
541
542 return NeedsProtector;
543}
544
545/// Create a stack guard loading and populate whether SelectionDAG SSP is
546/// supported.
547static Value *getStackGuard(const TargetLoweringBase &TLI,
548 const LibcallLoweringInfo &Libcalls, Module *M,
549 IRBuilder<> &B,
550 bool *SupportsSelectionDAGSP = nullptr) {
551 Value *Guard = TLI.getIRStackGuard(IRB&: B, Libcalls);
552 StringRef GuardMode = M->getStackProtectorGuard();
553 if ((GuardMode == "tls" || GuardMode.empty()) && Guard)
554 return B.CreateLoad(Ty: B.getPtrTy(), Ptr: Guard, isVolatile: true, Name: "StackGuard");
555
556 // Use SelectionDAG SSP handling, since there isn't an IR guard.
557 //
558 // This is more or less weird, since we optionally output whether we
559 // should perform a SelectionDAG SP here. The reason is that it's strictly
560 // defined as !TLI->getIRStackGuard(B), where getIRStackGuard is also
561 // mutating. There is no way to get this bit without mutating the IR, so
562 // getting this bit has to happen in this right time.
563 //
564 // We could have define a new function TLI::supportsSelectionDAGSP(), but that
565 // will put more burden on the backends' overriding work, especially when it
566 // actually conveys the same information getIRStackGuard() already gives.
567 if (SupportsSelectionDAGSP)
568 *SupportsSelectionDAGSP = true;
569 TLI.insertSSPDeclarations(M&: *M, Libcalls);
570 return B.CreateIntrinsic(ID: Intrinsic::stackguard, Args: {});
571}
572
573/// Insert code into the entry block that stores the stack guard
574/// variable onto the stack:
575///
576/// entry:
577/// StackGuardSlot = alloca i8*
578/// StackGuard = <stack guard>
579/// call void @llvm.stackprotector(StackGuard, StackGuardSlot)
580///
581/// Returns true if the platform/triple supports the stackprotectorcreate pseudo
582/// node.
583static bool CreatePrologue(Function *F, Module *M, Instruction *CheckLoc,
584 const TargetLoweringBase *TLI,
585 const LibcallLoweringInfo &Libcalls,
586 AllocaInst *&AI) {
587 bool SupportsSelectionDAGSP = false;
588 IRBuilder<> B(&F->getEntryBlock().front());
589 PointerType *PtrTy = PointerType::getUnqual(C&: CheckLoc->getContext());
590 AI = B.CreateAlloca(Ty: PtrTy, ArraySize: nullptr, Name: "StackGuardSlot");
591
592 Value *GuardSlot =
593 getStackGuard(TLI: *TLI, Libcalls, M, B, SupportsSelectionDAGSP: &SupportsSelectionDAGSP);
594 B.CreateIntrinsic(ID: Intrinsic::stackprotector, Args: {GuardSlot, AI});
595 return SupportsSelectionDAGSP;
596}
597
598bool InsertStackProtectors(const TargetLowering &TLI,
599 const LibcallLoweringInfo &Libcalls, Function *F,
600 DomTreeUpdater *DTU, bool &HasPrologue,
601 bool &HasIRCheck) {
602 const CodeGenOptions &Opts = getCodeGenOptions(F: *F);
603 auto *M = F->getParent();
604
605 // If the target wants to XOR the frame pointer into the guard value, it's
606 // impossible to emit the check in IR, so the target *must* support stack
607 // protection in SDAG.
608 bool SupportsSelectionDAGSP =
609 TLI.useStackGuardMixFP() ||
610 (Opts.stack_protector_selectiondag &&
611 !TLI.getTargetMachine().Options.EnableFastISel);
612 AllocaInst *AI = nullptr; // Place on stack that stores the stack guard.
613 BasicBlock *FailBB = nullptr;
614
615 for (BasicBlock &BB : llvm::make_early_inc_range(Range&: *F)) {
616 // This is stack protector auto generated check BB, skip it.
617 if (&BB == FailBB)
618 continue;
619 Instruction *CheckLoc = dyn_cast<ReturnInst>(Val: BB.getTerminator());
620 if (!CheckLoc && Opts.stack_protector_check_noreturn_call)
621 for (auto &Inst : BB) {
622 if (IntrinsicInst *IB = dyn_cast<IntrinsicInst>(Val: &Inst);
623 IB && (IB->getIntrinsicID() == Intrinsic::eh_sjlj_callsite)) {
624 // eh_sjlj_callsite has to be in same BB as the
625 // bb terminator. Don't insert within this range.
626 CheckLoc = IB;
627 break;
628 }
629 if (auto *CB = dyn_cast<CallBase>(Val: &Inst))
630 // Do stack check before noreturn calls that aren't nounwind (e.g:
631 // __cxa_throw).
632 if (CB->doesNotReturn() && !CB->doesNotThrow()) {
633 CheckLoc = CB;
634 break;
635 }
636 }
637
638 if (!CheckLoc)
639 continue;
640
641 // Generate prologue instrumentation if not already generated.
642 if (!HasPrologue) {
643 HasPrologue = true;
644 SupportsSelectionDAGSP &=
645 CreatePrologue(F, M, CheckLoc, TLI: &TLI, Libcalls, AI);
646 }
647
648 // SelectionDAG based code generation. Nothing else needs to be done here.
649 // The epilogue instrumentation is postponed to SelectionDAG.
650 if (SupportsSelectionDAGSP)
651 break;
652
653 // Find the stack guard slot if the prologue was not created by this pass
654 // itself via a previous call to CreatePrologue().
655 if (!AI) {
656 const CallInst *SPCall = findStackProtectorIntrinsic(F&: *F);
657 assert(SPCall && "Call to llvm.stackprotector is missing");
658 AI = cast<AllocaInst>(Val: SPCall->getArgOperand(i: 1));
659 }
660
661 // Set HasIRCheck to true, so that SelectionDAG will not generate its own
662 // version. SelectionDAG called 'shouldEmitSDCheck' to check whether
663 // instrumentation has already been generated.
664 HasIRCheck = true;
665
666 // If we're instrumenting a block with a tail call, the check has to be
667 // inserted before the call rather than between it and the return.
668 Instruction *Prev = CheckLoc->getPrevNode();
669 if (auto *CI = dyn_cast_if_present<CallInst>(Val: Prev))
670 if (CI->isTailCall() && isInTailCallPosition(Call: *CI, TM: TLI.getTargetMachine()))
671 CheckLoc = Prev;
672
673 // Generate epilogue instrumentation. The epilogue intrumentation can be
674 // function-based or inlined depending on which mechanism the target is
675 // providing.
676 if (Function *GuardCheck = TLI.getSSPStackGuardCheck(M: *M, Libcalls)) {
677 // Generate the function-based epilogue instrumentation.
678 // The target provides a guard check function, generate a call to it.
679 IRBuilder<> B(CheckLoc);
680 LoadInst *Guard = B.CreateLoad(Ty: B.getPtrTy(), Ptr: AI, isVolatile: true, Name: "Guard");
681 CallInst *Call = B.CreateCall(Callee: GuardCheck, Args: {Guard});
682 Call->setAttributes(GuardCheck->getAttributes());
683 Call->setCallingConv(GuardCheck->getCallingConv());
684 } else {
685 // Generate the epilogue with inline instrumentation.
686 // If we do not support SelectionDAG based calls, generate IR level
687 // calls.
688 //
689 // For each block with a return instruction, convert this:
690 //
691 // return:
692 // ...
693 // ret ...
694 //
695 // into this:
696 //
697 // return:
698 // ...
699 // %1 = <stack guard>
700 // %2 = load StackGuardSlot
701 // %3 = icmp ne i1 %1, %2
702 // br i1 %3, label %CallStackCheckFailBlk, label %SP_return
703 //
704 // SP_return:
705 // ret ...
706 //
707 // CallStackCheckFailBlk:
708 // call void @__stack_chk_fail()
709 // unreachable
710
711 // Create the FailBB. We duplicate the BB every time since the MI tail
712 // merge pass will merge together all of the various BB into one including
713 // fail BB generated by the stack protector pseudo instruction.
714 if (!FailBB)
715 FailBB = CreateFailBB(F, Libcalls);
716
717 IRBuilder<> B(CheckLoc);
718 Value *Guard = getStackGuard(TLI, Libcalls, M, B);
719 LoadInst *LI2 = B.CreateLoad(Ty: B.getPtrTy(), Ptr: AI, isVolatile: true);
720 auto *Cmp = cast<ICmpInst>(Val: B.CreateICmpNE(LHS: Guard, RHS: LI2));
721 auto SuccessProb =
722 BranchProbabilityInfo::getBranchProbStackProtector(IsLikely: true);
723 auto FailureProb =
724 BranchProbabilityInfo::getBranchProbStackProtector(IsLikely: false);
725 MDNode *Weights = MDBuilder(F->getContext())
726 .createBranchWeights(TrueWeight: FailureProb.getNumerator(),
727 FalseWeight: SuccessProb.getNumerator());
728
729 SplitBlockAndInsertIfThen(Cond: Cmp, SplitBefore: CheckLoc,
730 /*Unreachable=*/false, BranchWeights: Weights, DTU,
731 /*LI=*/nullptr, /*ThenBlock=*/FailBB);
732
733 auto *BI = cast<CondBrInst>(Val: Cmp->getParent()->getTerminator());
734 BasicBlock *NewBB = BI->getSuccessor(i: 1);
735 NewBB->setName("SP_return");
736 NewBB->moveAfter(MovePos: &BB);
737
738 Cmp->setPredicate(Cmp->getInversePredicate());
739 BI->swapSuccessors();
740 }
741 }
742
743 // Return if we didn't modify any basic blocks. i.e., there are no return
744 // statements in the function.
745 return HasPrologue;
746}
747
748BasicBlock *CreateFailBB(Function *F, const LibcallLoweringInfo &Libcalls) {
749 auto *M = F->getParent();
750 LLVMContext &Context = F->getContext();
751 BasicBlock *FailBB = BasicBlock::Create(Context, Name: "CallStackCheckFailBlk", Parent: F);
752 IRBuilder<> B(FailBB);
753 if (F->getSubprogram())
754 B.SetCurrentDebugLocation(
755 DILocation::get(Context, Line: 0, Column: 0, Scope: F->getSubprogram()));
756 bool IsSmashHandler = false;
757 RTLIB::LibcallImpl ChkFailImpl =
758 Libcalls.getLibcallImpl(Call: RTLIB::STACKPROTECTOR_CHECK_FAIL);
759 if (ChkFailImpl == RTLIB::Unsupported) {
760 ChkFailImpl = Libcalls.getLibcallImpl(Call: RTLIB::STACK_SMASH_HANDLER);
761 IsSmashHandler = true;
762 }
763
764 if (ChkFailImpl == RTLIB::Unsupported) {
765 Context.emitError(ErrorStr: "no libcall available for stack protector");
766 B.CreateUnreachable();
767 return FailBB;
768 }
769
770 FunctionCallee StackChkFail =
771 Libcalls.getRuntimeLibcallsInfo().getOrInsertLibcallDecl(M&: *M, LibcallImpl: ChkFailImpl);
772
773 CallInst *Call;
774 if (IsSmashHandler) {
775 Call =
776 B.CreateCall(Callee: StackChkFail, Args: B.CreateGlobalString(Str: F->getName(), Name: "SSH"));
777 } else {
778 Call = B.CreateCall(Callee: StackChkFail);
779 }
780
781 Call->setCallingConv(Libcalls.getLibcallImplCallingConv(Call: ChkFailImpl));
782 Call->addFnAttr(Kind: Attribute::NoReturn);
783 B.CreateUnreachable();
784 return FailBB;
785}
786