LLVM 20.0.0git
Macros | Functions | Variables
StackProtector.cpp File Reference
#include "llvm/CodeGen/StackProtector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/MemoryLocation.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetLowering.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/EHPersonalities.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/User.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include <optional>
#include <utility>

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "stack-protector"
 

Functions

 STATISTIC (NumFunProtected, "Number of functions protected")
 
 STATISTIC (NumAddrTaken, "Number of local variables that have their address" " taken.")
 
static bool InsertStackProtectors (const TargetMachine *TM, Function *F, DomTreeUpdater *DTU, bool &HasPrologue, bool &HasIRCheck)
 InsertStackProtectors - Insert code into the prologue and epilogue of the function.
 
static BasicBlockCreateFailBB (Function *F, const Triple &Trip)
 CreateFailBB - Create a basic block to jump to when the stack protector check fails.
 
 INITIALIZE_PASS_BEGIN (StackProtector, DEBUG_TYPE, "Insert stack protectors", false, true) INITIALIZE_PASS_END(StackProtector
 
static bool ContainsProtectableArray (Type *Ty, Module *M, unsigned SSPBufferSize, bool &IsLarge, bool Strong, bool InStruct)
 
static bool HasAddressTaken (const Instruction *AI, TypeSize AllocSize, Module *M, SmallPtrSet< const PHINode *, 16 > &VisitedPHIs)
 Check whether a stack allocation has its address taken.
 
static const CallInstfindStackProtectorIntrinsic (Function &F)
 Search for the first call to the llvm.stackprotector intrinsic and return it if present.
 
static ValuegetStackGuard (const TargetLoweringBase *TLI, Module *M, IRBuilder<> &B, bool *SupportsSelectionDAGSP=nullptr)
 Create a stack guard loading and populate whether SelectionDAG SSP is supported.
 
static bool CreatePrologue (Function *F, Module *M, Instruction *CheckLoc, const TargetLoweringBase *TLI, AllocaInst *&AI)
 Insert code into the entry block that stores the stack guard variable onto the stack:
 

Variables

static cl::opt< boolEnableSelectionDAGSP ("enable-selectiondag-sp", cl::init(true), cl::Hidden)
 
static cl::opt< boolDisableCheckNoReturn ("disable-check-noreturn-call", cl::init(false), cl::Hidden)
 
 DEBUG_TYPE
 
Insert stack protectors
 
Insert stack false
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "stack-protector"

Definition at line 56 of file StackProtector.cpp.

Function Documentation

◆ ContainsProtectableArray()

static bool ContainsProtectableArray ( Type Ty,
Module M,
unsigned  SSPBufferSize,
bool IsLarge,
bool  Strong,
bool  InStruct 
)
static
Parameters
[out]IsLargeis set to true if a protectable array is found and it is "large" ( >= ssp-buffer-size). In the case of a structure with multiple arrays, this gets set if any of them is large.

Definition at line 209 of file StackProtector.cpp.

References ContainsProtectableArray(), and llvm::Triple::isOSDarwin().

Referenced by ContainsProtectableArray(), and llvm::SSPLayoutAnalysis::requiresStackProtector().

◆ CreateFailBB()

BasicBlock * CreateFailBB ( Function F,
const Triple Trip 
)
static

CreateFailBB - Create a basic block to jump to when the stack protector check fails.

Definition at line 694 of file StackProtector.cpp.

References B, llvm::BasicBlock::Create(), F, llvm::MDNode::get(), llvm::FunctionCallee::getCallee(), llvm::PointerType::getUnqual(), llvm::Type::getVoidTy(), and llvm::Triple::isOSOpenBSD().

Referenced by InsertStackProtectors().

◆ CreatePrologue()

static bool CreatePrologue ( Function F,
Module M,
Instruction CheckLoc,
const TargetLoweringBase TLI,
AllocaInst *&  AI 
)
static

Insert code into the entry block that stores the stack guard variable onto the stack:

entry: StackGuardSlot = alloca i8* StackGuard = <stack guard> call void @llvm.stackprotector(StackGuard, StackGuardSlot)

Returns true if the platform/triple supports the stackprotectorcreate pseudo node.

Definition at line 535 of file StackProtector.cpp.

References B, F, llvm::Value::getContext(), llvm::Intrinsic::getDeclaration(), getStackGuard(), and llvm::PointerType::getUnqual().

Referenced by InsertStackProtectors().

◆ findStackProtectorIntrinsic()

static const CallInst * findStackProtectorIntrinsic ( Function F)
static

Search for the first call to the llvm.stackprotector intrinsic and return it if present.

Definition at line 349 of file StackProtector.cpp.

References F, I, and II.

Referenced by InsertStackProtectors().

◆ getStackGuard()

static Value * getStackGuard ( const TargetLoweringBase TLI,
Module M,
IRBuilder<> &  B,
bool SupportsSelectionDAGSP = nullptr 
)
static

Create a stack guard loading and populate whether SelectionDAG SSP is supported.

Definition at line 500 of file StackProtector.cpp.

References B, llvm::StringRef::empty(), llvm::Intrinsic::getDeclaration(), llvm::TargetLoweringBase::getIRStackGuard(), and llvm::TargetLoweringBase::insertSSPDeclarations().

Referenced by CreatePrologue(), and InsertStackProtectors().

◆ HasAddressTaken()

static bool HasAddressTaken ( const Instruction AI,
TypeSize  AllocSize,
Module M,
SmallPtrSet< const PHINode *, 16 > &  VisitedPHIs 
)
static

◆ INITIALIZE_PASS_BEGIN()

INITIALIZE_PASS_BEGIN ( StackProtector  ,
DEBUG_TYPE  ,
"Insert stack protectors"  ,
false  ,
true   
)

◆ InsertStackProtectors()

bool InsertStackProtectors ( const TargetMachine TM,
Function F,
DomTreeUpdater DTU,
bool HasPrologue,
bool HasIRCheck 
)
static

InsertStackProtectors - Insert code into the prologue and epilogue of the function.

  • The prologue code loads and stores the stack guard onto the stack.
  • The epilogue checks the value stored in the prologue against the original value. It calls __stack_chk_fail if they differ.

Definition at line 548 of file StackProtector.cpp.

References assert(), B, llvm::MDBuilder::createBranchWeights(), CreateFailBB(), CreatePrologue(), DisableCheckNoReturn, EnableSelectionDAGSP, F, findStackProtectorIntrinsic(), llvm::CallBase::getArgOperand(), llvm::BranchProbabilityInfo::getBranchProbStackProtector(), llvm::Instruction::getPrevNonDebugInstruction(), getStackGuard(), llvm::make_early_inc_range(), llvm::BasicBlock::moveAfter(), llvm::Value::setName(), and llvm::SplitBlockAndInsertIfThen().

Referenced by llvm::StackProtectorPass::run(), and llvm::StackProtector::runOnFunction().

◆ STATISTIC() [1/2]

STATISTIC ( NumAddrTaken  ,
"Number of local variables that have their address" " taken."   
)

◆ STATISTIC() [2/2]

STATISTIC ( NumFunProtected  ,
"Number of functions protected"   
)

Variable Documentation

◆ DEBUG_TYPE

DEBUG_TYPE

Definition at line 161 of file StackProtector.cpp.

◆ DisableCheckNoReturn

cl::opt< bool > DisableCheckNoReturn("disable-check-noreturn-call", cl::init(false), cl::Hidden) ( "disable-check-noreturn-call"  ,
cl::init(false)  ,
cl::Hidden   
)
static

Referenced by InsertStackProtectors().

◆ EnableSelectionDAGSP

cl::opt< bool > EnableSelectionDAGSP("enable-selectiondag-sp", cl::init(true), cl::Hidden) ( "enable-selectiondag-sp"  ,
cl::init(true ,
cl::Hidden   
)
static

Referenced by InsertStackProtectors().

◆ false

Insert stack false

Definition at line 162 of file StackProtector.cpp.

◆ protectors

Insert stack protectors

Definition at line 162 of file StackProtector.cpp.