Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:36

0001 //===- StackProtector.h - Stack Protector Insertion -------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This pass inserts stack protectors into functions which need them. A variable
0010 // with a random value in it is stored onto the stack before the local variables
0011 // are allocated. Upon exiting the block, the stored value is checked. If it's
0012 // changed, then there was some sort of violation and the program aborts.
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   /// A mapping of AllocaInsts to their required SSP layout.
0041   using SSPLayoutMap =
0042       DenseMap<const AllocaInst *, MachineFrameInfo::SSPLayoutKind>;
0043 
0044   /// Layout - Mapping of allocations to the required SSPLayoutKind.
0045   /// StackProtector analysis will update this map when determining if an
0046   /// AllocaInst triggers a stack protector.
0047   SSPLayoutMap Layout;
0048 
0049   /// The minimum size of buffers that will receive stack smashing
0050   /// protection when -fstack-protection is used.
0051   unsigned SSPBufferSize = DefaultSSPBufferSize;
0052 
0053   bool RequireStackProtector = false;
0054 
0055   // A prologue is generated.
0056   bool HasPrologue = false;
0057 
0058   // IR checking code is generated.
0059   bool HasIRCheck = false;
0060 
0061 public:
0062   // Return true if StackProtector is supposed to be handled by SelectionDAG.
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   /// Check whether or not \p F needs a stack protector based upon the stack
0080   /// protector level.
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   /// A mapping of AllocaInsts to their required SSP layout.
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; // Pass identification, replacement for typeid.
0109 
0110   StackProtector();
0111 
0112   SSPLayoutInfo &getLayoutInfo() { return LayoutInfo; }
0113 
0114   void getAnalysisUsage(AnalysisUsage &AU) const override;
0115 
0116   // Return true if StackProtector is supposed to be handled by SelectionDAG.
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   /// Check whether or not \p F needs a stack protector based upon the stack
0128   /// protector level.
0129   static bool requiresStackProtector(Function *F,
0130                                      SSPLayoutMap *Layout = nullptr) {
0131     return SSPLayoutAnalysis::requiresStackProtector(F, Layout);
0132   }
0133 };
0134 
0135 } // end namespace llvm
0136 
0137 #endif // LLVM_CODEGEN_STACKPROTECTOR_H