File indexing completed on 2026-05-10 08:43:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef LLVM_CODEGEN_STACKPROTECTOR_H
0017 #define LLVM_CODEGEN_STACKPROTECTOR_H
0018
0019 #include "llvm/Analysis/DomTreeUpdater.h"
0020 #include "llvm/CodeGen/MachineFrameInfo.h"
0021 #include "llvm/IR/Instructions.h"
0022 #include "llvm/IR/PassManager.h"
0023 #include "llvm/Pass.h"
0024 #include "llvm/TargetParser/Triple.h"
0025
0026 namespace llvm {
0027
0028 class BasicBlock;
0029 class Function;
0030 class Module;
0031 class TargetLoweringBase;
0032 class TargetMachine;
0033
0034 class SSPLayoutInfo {
0035 friend class StackProtectorPass;
0036 friend class SSPLayoutAnalysis;
0037 friend class StackProtector;
0038 static constexpr unsigned DefaultSSPBufferSize = 8;
0039
0040
0041 using SSPLayoutMap =
0042 DenseMap<const AllocaInst *, MachineFrameInfo::SSPLayoutKind>;
0043
0044
0045
0046
0047 SSPLayoutMap Layout;
0048
0049
0050
0051 unsigned SSPBufferSize = DefaultSSPBufferSize;
0052
0053 bool RequireStackProtector = false;
0054
0055
0056 bool HasPrologue = false;
0057
0058
0059 bool HasIRCheck = false;
0060
0061 public:
0062
0063 bool shouldEmitSDCheck(const BasicBlock &BB) const;
0064
0065 void copyToMachineFrameInfo(MachineFrameInfo &MFI) const;
0066 };
0067
0068 class SSPLayoutAnalysis : public AnalysisInfoMixin<SSPLayoutAnalysis> {
0069 friend AnalysisInfoMixin<SSPLayoutAnalysis>;
0070 using SSPLayoutMap = SSPLayoutInfo::SSPLayoutMap;
0071
0072 static AnalysisKey Key;
0073
0074 public:
0075 using Result = SSPLayoutInfo;
0076
0077 Result run(Function &F, FunctionAnalysisManager &FAM);
0078
0079
0080
0081 static bool requiresStackProtector(Function *F,
0082 SSPLayoutMap *Layout = nullptr);
0083 };
0084
0085 class StackProtectorPass : public PassInfoMixin<StackProtectorPass> {
0086 const TargetMachine *TM;
0087
0088 public:
0089 explicit StackProtectorPass(const TargetMachine *TM) : TM(TM) {}
0090 PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
0091 };
0092
0093 class StackProtector : public FunctionPass {
0094 private:
0095
0096 using SSPLayoutMap = SSPLayoutInfo::SSPLayoutMap;
0097
0098 const TargetMachine *TM = nullptr;
0099
0100 Function *F = nullptr;
0101 Module *M = nullptr;
0102
0103 std::optional<DomTreeUpdater> DTU;
0104
0105 SSPLayoutInfo LayoutInfo;
0106
0107 public:
0108 static char ID;
0109
0110 StackProtector();
0111
0112 SSPLayoutInfo &getLayoutInfo() { return LayoutInfo; }
0113
0114 void getAnalysisUsage(AnalysisUsage &AU) const override;
0115
0116
0117 bool shouldEmitSDCheck(const BasicBlock &BB) const {
0118 return LayoutInfo.shouldEmitSDCheck(BB);
0119 }
0120
0121 bool runOnFunction(Function &Fn) override;
0122
0123 void copyToMachineFrameInfo(MachineFrameInfo &MFI) const {
0124 LayoutInfo.copyToMachineFrameInfo(MFI);
0125 }
0126
0127
0128
0129 static bool requiresStackProtector(Function *F,
0130 SSPLayoutMap *Layout = nullptr) {
0131 return SSPLayoutAnalysis::requiresStackProtector(F, Layout);
0132 }
0133 };
0134
0135 }
0136
0137 #endif