1 //===- StackProtector.cpp - Stack Protector Insertion ---------------------===//
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
7 //===----------------------------------------------------------------------===//
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/MemoryLocation.h"
22 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/CodeGen/TargetLowering.h"
25 #include "llvm/CodeGen/TargetPassConfig.h"
26 #include "llvm/CodeGen/TargetSubtargetInfo.h"
27 #include "llvm/IR/Attributes.h"
28 #include "llvm/IR/BasicBlock.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/Dominators.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/IR/IRBuilder.h"
35 #include "llvm/IR/Instruction.h"
36 #include "llvm/IR/Instructions.h"
37 #include "llvm/IR/IntrinsicInst.h"
38 #include "llvm/IR/Intrinsics.h"
39 #include "llvm/IR/MDBuilder.h"
40 #include "llvm/IR/Module.h"
41 #include "llvm/IR/Type.h"
42 #include "llvm/IR/User.h"
43 #include "llvm/InitializePasses.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"
53 #define DEBUG_TYPE "stack-protector"
55 STATISTIC(NumFunProtected
, "Number of functions protected");
56 STATISTIC(NumAddrTaken
, "Number of local variables that have their address"
59 static cl::opt
<bool> EnableSelectionDAGSP("enable-selectiondag-sp",
60 cl::init(true), cl::Hidden
);
62 char StackProtector::ID
= 0;
64 StackProtector::StackProtector() : FunctionPass(ID
), SSPBufferSize(8) {
65 initializeStackProtectorPass(*PassRegistry::getPassRegistry());
68 INITIALIZE_PASS_BEGIN(StackProtector
, DEBUG_TYPE
,
69 "Insert stack protectors", false, true)
70 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig
)
71 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
72 INITIALIZE_PASS_END(StackProtector
, DEBUG_TYPE
,
73 "Insert stack protectors", false, true)
75 FunctionPass
*llvm::createStackProtectorPass() { return new StackProtector(); }
77 void StackProtector::getAnalysisUsage(AnalysisUsage
&AU
) const {
78 AU
.addRequired
<TargetPassConfig
>();
79 AU
.addPreserved
<DominatorTreeWrapperPass
>();
82 bool StackProtector::runOnFunction(Function
&Fn
) {
85 DominatorTreeWrapperPass
*DTWP
=
86 getAnalysisIfAvailable
<DominatorTreeWrapperPass
>();
87 DT
= DTWP
? &DTWP
->getDomTree() : nullptr;
88 TM
= &getAnalysis
<TargetPassConfig
>().getTM
<TargetMachine
>();
89 Trip
= TM
->getTargetTriple();
90 TLI
= TM
->getSubtargetImpl(Fn
)->getTargetLowering();
94 Attribute Attr
= Fn
.getFnAttribute("stack-protector-buffer-size");
95 if (Attr
.isStringAttribute() &&
96 Attr
.getValueAsString().getAsInteger(10, SSPBufferSize
))
97 return false; // Invalid integer string
99 if (!RequiresStackProtector())
102 // TODO(etienneb): Functions with funclets are not correctly supported now.
103 // Do nothing if this is funclet-based personality.
104 if (Fn
.hasPersonalityFn()) {
105 EHPersonality Personality
= classifyEHPersonality(Fn
.getPersonalityFn());
106 if (isFuncletEHPersonality(Personality
))
111 return InsertStackProtectors();
114 /// \param [out] IsLarge is set to true if a protectable array is found and
115 /// it is "large" ( >= ssp-buffer-size). In the case of a structure with
116 /// multiple arrays, this gets set if any of them is large.
117 bool StackProtector::ContainsProtectableArray(Type
*Ty
, bool &IsLarge
,
119 bool InStruct
) const {
122 if (ArrayType
*AT
= dyn_cast
<ArrayType
>(Ty
)) {
123 if (!AT
->getElementType()->isIntegerTy(8)) {
124 // If we're on a non-Darwin platform or we're inside of a structure, don't
125 // add stack protectors unless the array is a character array.
126 // However, in strong mode any array, regardless of type and size,
127 // triggers a protector.
128 if (!Strong
&& (InStruct
|| !Trip
.isOSDarwin()))
132 // If an array has more than SSPBufferSize bytes of allocated space, then we
133 // emit stack protectors.
134 if (SSPBufferSize
<= M
->getDataLayout().getTypeAllocSize(AT
)) {
140 // Require a protector for all arrays in strong mode
144 const StructType
*ST
= dyn_cast
<StructType
>(Ty
);
148 bool NeedsProtector
= false;
149 for (Type
*ET
: ST
->elements())
150 if (ContainsProtectableArray(ET
, IsLarge
, Strong
, true)) {
151 // If the element is a protectable array and is large (>= SSPBufferSize)
152 // then we are done. If the protectable array is not large, then
153 // keep looking in case a subsequent element is a large array.
156 NeedsProtector
= true;
159 return NeedsProtector
;
162 bool StackProtector::HasAddressTaken(const Instruction
*AI
,
163 TypeSize AllocSize
) {
164 const DataLayout
&DL
= M
->getDataLayout();
165 for (const User
*U
: AI
->users()) {
166 const auto *I
= cast
<Instruction
>(U
);
167 // If this instruction accesses memory make sure it doesn't access beyond
168 // the bounds of the allocated object.
169 Optional
<MemoryLocation
> MemLoc
= MemoryLocation::getOrNone(I
);
170 if (MemLoc
&& MemLoc
->Size
.hasValue() &&
171 !TypeSize::isKnownGE(AllocSize
,
172 TypeSize::getFixed(MemLoc
->Size
.getValue())))
174 switch (I
->getOpcode()) {
175 case Instruction::Store
:
176 if (AI
== cast
<StoreInst
>(I
)->getValueOperand())
179 case Instruction::AtomicCmpXchg
:
180 // cmpxchg conceptually includes both a load and store from the same
181 // location. So, like store, the value being stored is what matters.
182 if (AI
== cast
<AtomicCmpXchgInst
>(I
)->getNewValOperand())
185 case Instruction::PtrToInt
:
186 if (AI
== cast
<PtrToIntInst
>(I
)->getOperand(0))
189 case Instruction::Call
: {
190 // Ignore intrinsics that do not become real instructions.
191 // TODO: Narrow this to intrinsics that have store-like effects.
192 const auto *CI
= cast
<CallInst
>(I
);
193 if (!CI
->isDebugOrPseudoInst() && !CI
->isLifetimeStartOrEnd())
197 case Instruction::Invoke
:
199 case Instruction::GetElementPtr
: {
200 // If the GEP offset is out-of-bounds, or is non-constant and so has to be
201 // assumed to be potentially out-of-bounds, then any memory access that
202 // would use it could also be out-of-bounds meaning stack protection is
204 const GetElementPtrInst
*GEP
= cast
<GetElementPtrInst
>(I
);
205 unsigned IndexSize
= DL
.getIndexTypeSizeInBits(I
->getType());
206 APInt
Offset(IndexSize
, 0);
207 if (!GEP
->accumulateConstantOffset(DL
, Offset
))
209 TypeSize OffsetSize
= TypeSize::Fixed(Offset
.getLimitedValue());
210 if (!TypeSize::isKnownGT(AllocSize
, OffsetSize
))
212 // Adjust AllocSize to be the space remaining after this offset.
213 // We can't subtract a fixed size from a scalable one, so in that case
214 // assume the scalable value is of minimum size.
215 TypeSize NewAllocSize
=
216 TypeSize::Fixed(AllocSize
.getKnownMinValue()) - OffsetSize
;
217 if (HasAddressTaken(I
, NewAllocSize
))
221 case Instruction::BitCast
:
222 case Instruction::Select
:
223 case Instruction::AddrSpaceCast
:
224 if (HasAddressTaken(I
, AllocSize
))
227 case Instruction::PHI
: {
228 // Keep track of what PHI nodes we have already visited to ensure
229 // they are only visited once.
230 const auto *PN
= cast
<PHINode
>(I
);
231 if (VisitedPHIs
.insert(PN
).second
)
232 if (HasAddressTaken(PN
, AllocSize
))
236 case Instruction::Load
:
237 case Instruction::AtomicRMW
:
238 case Instruction::Ret
:
239 // These instructions take an address operand, but have load-like or
240 // other innocuous behavior that should not trigger a stack protector.
241 // atomicrmw conceptually has both load and store semantics, but the
242 // value being stored must be integer; so if a pointer is being stored,
243 // we'll catch it in the PtrToInt case above.
246 // Conservatively return true for any instruction that takes an address
247 // operand, but is not handled above.
254 /// Search for the first call to the llvm.stackprotector intrinsic and return it
256 static const CallInst
*findStackProtectorIntrinsic(Function
&F
) {
257 for (const BasicBlock
&BB
: F
)
258 for (const Instruction
&I
: BB
)
259 if (const auto *II
= dyn_cast
<IntrinsicInst
>(&I
))
260 if (II
->getIntrinsicID() == Intrinsic::stackprotector
)
265 /// Check whether or not this function needs a stack protector based
266 /// upon the stack protector level.
268 /// We use two heuristics: a standard (ssp) and strong (sspstrong).
269 /// The standard heuristic which will add a guard variable to functions that
270 /// call alloca with a either a variable size or a size >= SSPBufferSize,
271 /// functions with character buffers larger than SSPBufferSize, and functions
272 /// with aggregates containing character buffers larger than SSPBufferSize. The
273 /// strong heuristic will add a guard variables to functions that call alloca
274 /// regardless of size, functions with any buffer regardless of type and size,
275 /// functions with aggregates that contain any buffer regardless of type and
276 /// size, and functions that contain stack-based variables that have had their
278 bool StackProtector::RequiresStackProtector() {
280 bool NeedsProtector
= false;
282 if (F
->hasFnAttribute(Attribute::SafeStack
))
285 // We are constructing the OptimizationRemarkEmitter on the fly rather than
286 // using the analysis pass to avoid building DominatorTree and LoopInfo which
287 // are not available this late in the IR pipeline.
288 OptimizationRemarkEmitter
ORE(F
);
290 if (F
->hasFnAttribute(Attribute::StackProtectReq
)) {
292 return OptimizationRemark(DEBUG_TYPE
, "StackProtectorRequested", F
)
293 << "Stack protection applied to function "
294 << ore::NV("Function", F
)
295 << " due to a function attribute or command-line switch";
297 NeedsProtector
= true;
298 Strong
= true; // Use the same heuristic as strong to determine SSPLayout
299 } else if (F
->hasFnAttribute(Attribute::StackProtectStrong
))
301 else if (!F
->hasFnAttribute(Attribute::StackProtect
))
304 for (const BasicBlock
&BB
: *F
) {
305 for (const Instruction
&I
: BB
) {
306 if (const AllocaInst
*AI
= dyn_cast
<AllocaInst
>(&I
)) {
307 if (AI
->isArrayAllocation()) {
308 auto RemarkBuilder
= [&]() {
309 return OptimizationRemark(DEBUG_TYPE
, "StackProtectorAllocaOrArray",
311 << "Stack protection applied to function "
312 << ore::NV("Function", F
)
313 << " due to a call to alloca or use of a variable length "
316 if (const auto *CI
= dyn_cast
<ConstantInt
>(AI
->getArraySize())) {
317 if (CI
->getLimitedValue(SSPBufferSize
) >= SSPBufferSize
) {
318 // A call to alloca with size >= SSPBufferSize requires
320 Layout
.insert(std::make_pair(AI
,
321 MachineFrameInfo::SSPLK_LargeArray
));
322 ORE
.emit(RemarkBuilder
);
323 NeedsProtector
= true;
325 // Require protectors for all alloca calls in strong mode.
326 Layout
.insert(std::make_pair(AI
,
327 MachineFrameInfo::SSPLK_SmallArray
));
328 ORE
.emit(RemarkBuilder
);
329 NeedsProtector
= true;
332 // A call to alloca with a variable size requires protectors.
333 Layout
.insert(std::make_pair(AI
,
334 MachineFrameInfo::SSPLK_LargeArray
));
335 ORE
.emit(RemarkBuilder
);
336 NeedsProtector
= true;
341 bool IsLarge
= false;
342 if (ContainsProtectableArray(AI
->getAllocatedType(), IsLarge
, Strong
)) {
343 Layout
.insert(std::make_pair(AI
, IsLarge
344 ? MachineFrameInfo::SSPLK_LargeArray
345 : MachineFrameInfo::SSPLK_SmallArray
));
347 return OptimizationRemark(DEBUG_TYPE
, "StackProtectorBuffer", &I
)
348 << "Stack protection applied to function "
349 << ore::NV("Function", F
)
350 << " due to a stack allocated buffer or struct containing a "
353 NeedsProtector
= true;
357 if (Strong
&& HasAddressTaken(AI
, M
->getDataLayout().getTypeAllocSize(
358 AI
->getAllocatedType()))) {
360 Layout
.insert(std::make_pair(AI
, MachineFrameInfo::SSPLK_AddrOf
));
362 return OptimizationRemark(DEBUG_TYPE
, "StackProtectorAddressTaken",
364 << "Stack protection applied to function "
365 << ore::NV("Function", F
)
366 << " due to the address of a local variable being taken";
368 NeedsProtector
= true;
370 // Clear any PHIs that we visited, to make sure we examine all uses of
371 // any subsequent allocas that we look at.
377 return NeedsProtector
;
380 /// Create a stack guard loading and populate whether SelectionDAG SSP is
382 static Value
*getStackGuard(const TargetLoweringBase
*TLI
, Module
*M
,
384 bool *SupportsSelectionDAGSP
= nullptr) {
385 Value
*Guard
= TLI
->getIRStackGuard(B
);
386 StringRef GuardMode
= M
->getStackProtectorGuard();
387 if ((GuardMode
== "tls" || GuardMode
.empty()) && Guard
)
388 return B
.CreateLoad(B
.getInt8PtrTy(), Guard
, true, "StackGuard");
390 // Use SelectionDAG SSP handling, since there isn't an IR guard.
392 // This is more or less weird, since we optionally output whether we
393 // should perform a SelectionDAG SP here. The reason is that it's strictly
394 // defined as !TLI->getIRStackGuard(B), where getIRStackGuard is also
395 // mutating. There is no way to get this bit without mutating the IR, so
396 // getting this bit has to happen in this right time.
398 // We could have define a new function TLI::supportsSelectionDAGSP(), but that
399 // will put more burden on the backends' overriding work, especially when it
400 // actually conveys the same information getIRStackGuard() already gives.
401 if (SupportsSelectionDAGSP
)
402 *SupportsSelectionDAGSP
= true;
403 TLI
->insertSSPDeclarations(*M
);
404 return B
.CreateCall(Intrinsic::getDeclaration(M
, Intrinsic::stackguard
));
407 /// Insert code into the entry block that stores the stack guard
408 /// variable onto the stack:
411 /// StackGuardSlot = alloca i8*
412 /// StackGuard = <stack guard>
413 /// call void @llvm.stackprotector(StackGuard, StackGuardSlot)
415 /// Returns true if the platform/triple supports the stackprotectorcreate pseudo
417 static bool CreatePrologue(Function
*F
, Module
*M
, ReturnInst
*RI
,
418 const TargetLoweringBase
*TLI
, AllocaInst
*&AI
) {
419 bool SupportsSelectionDAGSP
= false;
420 IRBuilder
<> B(&F
->getEntryBlock().front());
421 PointerType
*PtrTy
= Type::getInt8PtrTy(RI
->getContext());
422 AI
= B
.CreateAlloca(PtrTy
, nullptr, "StackGuardSlot");
424 Value
*GuardSlot
= getStackGuard(TLI
, M
, B
, &SupportsSelectionDAGSP
);
425 B
.CreateCall(Intrinsic::getDeclaration(M
, Intrinsic::stackprotector
),
427 return SupportsSelectionDAGSP
;
430 /// InsertStackProtectors - Insert code into the prologue and epilogue of the
433 /// - The prologue code loads and stores the stack guard onto the stack.
434 /// - The epilogue checks the value stored in the prologue against the original
435 /// value. It calls __stack_chk_fail if they differ.
436 bool StackProtector::InsertStackProtectors() {
437 // If the target wants to XOR the frame pointer into the guard value, it's
438 // impossible to emit the check in IR, so the target *must* support stack
439 // protection in SDAG.
440 bool SupportsSelectionDAGSP
=
441 TLI
->useStackGuardXorFP() ||
442 (EnableSelectionDAGSP
&& !TM
->Options
.EnableFastISel
);
443 AllocaInst
*AI
= nullptr; // Place on stack that stores the stack guard.
445 for (BasicBlock
&BB
: llvm::make_early_inc_range(*F
)) {
446 ReturnInst
*RI
= dyn_cast
<ReturnInst
>(BB
.getTerminator());
450 // Generate prologue instrumentation if not already generated.
453 SupportsSelectionDAGSP
&= CreatePrologue(F
, M
, RI
, TLI
, AI
);
456 // SelectionDAG based code generation. Nothing else needs to be done here.
457 // The epilogue instrumentation is postponed to SelectionDAG.
458 if (SupportsSelectionDAGSP
)
461 // Find the stack guard slot if the prologue was not created by this pass
462 // itself via a previous call to CreatePrologue().
464 const CallInst
*SPCall
= findStackProtectorIntrinsic(*F
);
465 assert(SPCall
&& "Call to llvm.stackprotector is missing");
466 AI
= cast
<AllocaInst
>(SPCall
->getArgOperand(1));
469 // Set HasIRCheck to true, so that SelectionDAG will not generate its own
470 // version. SelectionDAG called 'shouldEmitSDCheck' to check whether
471 // instrumentation has already been generated.
474 // If we're instrumenting a block with a musttail call, the check has to be
475 // inserted before the call rather than between it and the return. The
476 // verifier guarantees that a musttail call is either directly before the
477 // return or with a single correct bitcast of the return value in between so
478 // we don't need to worry about many situations here.
479 Instruction
*CheckLoc
= RI
;
480 Instruction
*Prev
= RI
->getPrevNonDebugInstruction();
481 if (Prev
&& isa
<CallInst
>(Prev
) && cast
<CallInst
>(Prev
)->isMustTailCall())
484 Prev
= Prev
->getPrevNonDebugInstruction();
485 if (Prev
&& isa
<CallInst
>(Prev
) && cast
<CallInst
>(Prev
)->isMustTailCall())
489 // Generate epilogue instrumentation. The epilogue intrumentation can be
490 // function-based or inlined depending on which mechanism the target is
492 if (Function
*GuardCheck
= TLI
->getSSPStackGuardCheck(*M
)) {
493 // Generate the function-based epilogue instrumentation.
494 // The target provides a guard check function, generate a call to it.
495 IRBuilder
<> B(CheckLoc
);
496 LoadInst
*Guard
= B
.CreateLoad(B
.getInt8PtrTy(), AI
, true, "Guard");
497 CallInst
*Call
= B
.CreateCall(GuardCheck
, {Guard
});
498 Call
->setAttributes(GuardCheck
->getAttributes());
499 Call
->setCallingConv(GuardCheck
->getCallingConv());
501 // Generate the epilogue with inline instrumentation.
502 // If we do not support SelectionDAG based calls, generate IR level
505 // For each block with a return instruction, convert this:
515 // %1 = <stack guard>
516 // %2 = load StackGuardSlot
517 // %3 = cmp i1 %1, %2
518 // br i1 %3, label %SP_return, label %CallStackCheckFailBlk
523 // CallStackCheckFailBlk:
524 // call void @__stack_chk_fail()
527 // Create the FailBB. We duplicate the BB every time since the MI tail
528 // merge pass will merge together all of the various BB into one including
529 // fail BB generated by the stack protector pseudo instruction.
530 BasicBlock
*FailBB
= CreateFailBB();
532 // Split the basic block before the return instruction.
534 BB
.splitBasicBlock(CheckLoc
->getIterator(), "SP_return");
536 // Update the dominator tree if we need to.
537 if (DT
&& DT
->isReachableFromEntry(&BB
)) {
538 DT
->addNewBlock(NewBB
, &BB
);
539 DT
->addNewBlock(FailBB
, &BB
);
542 // Remove default branch instruction to the new BB.
543 BB
.getTerminator()->eraseFromParent();
545 // Move the newly created basic block to the point right after the old
546 // basic block so that it's in the "fall through" position.
547 NewBB
->moveAfter(&BB
);
549 // Generate the stack protector instructions in the old basic block.
551 Value
*Guard
= getStackGuard(TLI
, M
, B
);
552 LoadInst
*LI2
= B
.CreateLoad(B
.getInt8PtrTy(), AI
, true);
553 Value
*Cmp
= B
.CreateICmpEQ(Guard
, LI2
);
555 BranchProbabilityInfo::getBranchProbStackProtector(true);
557 BranchProbabilityInfo::getBranchProbStackProtector(false);
558 MDNode
*Weights
= MDBuilder(F
->getContext())
559 .createBranchWeights(SuccessProb
.getNumerator(),
560 FailureProb
.getNumerator());
561 B
.CreateCondBr(Cmp
, NewBB
, FailBB
, Weights
);
565 // Return if we didn't modify any basic blocks. i.e., there are no return
566 // statements in the function.
570 /// CreateFailBB - Create a basic block to jump to when the stack protector
572 BasicBlock
*StackProtector::CreateFailBB() {
573 LLVMContext
&Context
= F
->getContext();
574 BasicBlock
*FailBB
= BasicBlock::Create(Context
, "CallStackCheckFailBlk", F
);
575 IRBuilder
<> B(FailBB
);
576 if (F
->getSubprogram())
577 B
.SetCurrentDebugLocation(
578 DILocation::get(Context
, 0, 0, F
->getSubprogram()));
579 if (Trip
.isOSOpenBSD()) {
580 FunctionCallee StackChkFail
= M
->getOrInsertFunction(
581 "__stack_smash_handler", Type::getVoidTy(Context
),
582 Type::getInt8PtrTy(Context
));
584 B
.CreateCall(StackChkFail
, B
.CreateGlobalStringPtr(F
->getName(), "SSH"));
586 FunctionCallee StackChkFail
=
587 M
->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context
));
589 B
.CreateCall(StackChkFail
, {});
591 B
.CreateUnreachable();
595 bool StackProtector::shouldEmitSDCheck(const BasicBlock
&BB
) const {
596 return HasPrologue
&& !HasIRCheck
&& isa
<ReturnInst
>(BB
.getTerminator());
599 void StackProtector::copyToMachineFrameInfo(MachineFrameInfo
&MFI
) const {
603 for (int I
= 0, E
= MFI
.getObjectIndexEnd(); I
!= E
; ++I
) {
604 if (MFI
.isDeadObjectIndex(I
))
607 const AllocaInst
*AI
= MFI
.getObjectAllocation(I
);
611 SSPLayoutMap::const_iterator LI
= Layout
.find(AI
);
612 if (LI
== Layout
.end())
615 MFI
.setObjectSSPLayout(I
, LI
->second
);