StackProtector.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- StackProtector.h - Stack Protector Insertion -------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This pass inserts stack protectors into functions which need them. A variable
  15. // with a random value in it is stored onto the stack before the local variables
  16. // are allocated. Upon exiting the block, the stored value is checked. If it's
  17. // changed, then there was some sort of violation and the program aborts.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_CODEGEN_STACKPROTECTOR_H
  21. #define LLVM_CODEGEN_STACKPROTECTOR_H
  22. #include "llvm/ADT/SmallPtrSet.h"
  23. #include "llvm/ADT/Triple.h"
  24. #include "llvm/Analysis/DomTreeUpdater.h"
  25. #include "llvm/CodeGen/MachineFrameInfo.h"
  26. #include "llvm/IR/Instructions.h"
  27. #include "llvm/Pass.h"
  28. namespace llvm {
  29. class BasicBlock;
  30. class DominatorTree;
  31. class Function;
  32. class Instruction;
  33. class Module;
  34. class TargetLoweringBase;
  35. class TargetMachine;
  36. class Type;
  37. class StackProtector : public FunctionPass {
  38. private:
  39. static constexpr unsigned DefaultSSPBufferSize = 8;
  40. /// A mapping of AllocaInsts to their required SSP layout.
  41. using SSPLayoutMap = DenseMap<const AllocaInst *,
  42. MachineFrameInfo::SSPLayoutKind>;
  43. const TargetMachine *TM = nullptr;
  44. /// TLI - Keep a pointer of a TargetLowering to consult for determining
  45. /// target type sizes.
  46. const TargetLoweringBase *TLI = nullptr;
  47. Triple Trip;
  48. Function *F;
  49. Module *M;
  50. std::optional<DomTreeUpdater> DTU;
  51. /// Layout - Mapping of allocations to the required SSPLayoutKind.
  52. /// StackProtector analysis will update this map when determining if an
  53. /// AllocaInst triggers a stack protector.
  54. SSPLayoutMap Layout;
  55. /// The minimum size of buffers that will receive stack smashing
  56. /// protection when -fstack-protection is used.
  57. unsigned SSPBufferSize = DefaultSSPBufferSize;
  58. /// VisitedPHIs - The set of PHI nodes visited when determining
  59. /// if a variable's reference has been taken. This set
  60. /// is maintained to ensure we don't visit the same PHI node multiple
  61. /// times.
  62. SmallPtrSet<const PHINode *, 16> VisitedPHIs;
  63. // A prologue is generated.
  64. bool HasPrologue = false;
  65. // IR checking code is generated.
  66. bool HasIRCheck = false;
  67. /// InsertStackProtectors - Insert code into the prologue and epilogue of
  68. /// the function.
  69. ///
  70. /// - The prologue code loads and stores the stack guard onto the stack.
  71. /// - The epilogue checks the value stored in the prologue against the
  72. /// original value. It calls __stack_chk_fail if they differ.
  73. bool InsertStackProtectors();
  74. /// CreateFailBB - Create a basic block to jump to when the stack protector
  75. /// check fails.
  76. BasicBlock *CreateFailBB();
  77. /// ContainsProtectableArray - Check whether the type either is an array or
  78. /// contains an array of sufficient size so that we need stack protectors
  79. /// for it.
  80. /// \param [out] IsLarge is set to true if a protectable array is found and
  81. /// it is "large" ( >= ssp-buffer-size). In the case of a structure with
  82. /// multiple arrays, this gets set if any of them is large.
  83. bool ContainsProtectableArray(Type *Ty, bool &IsLarge, bool Strong = false,
  84. bool InStruct = false) const;
  85. /// Check whether a stack allocation has its address taken.
  86. bool HasAddressTaken(const Instruction *AI, TypeSize AllocSize);
  87. /// RequiresStackProtector - Check whether or not this function needs a
  88. /// stack protector based upon the stack protector level.
  89. bool RequiresStackProtector();
  90. public:
  91. static char ID; // Pass identification, replacement for typeid.
  92. StackProtector();
  93. void getAnalysisUsage(AnalysisUsage &AU) const override;
  94. // Return true if StackProtector is supposed to be handled by SelectionDAG.
  95. bool shouldEmitSDCheck(const BasicBlock &BB) const;
  96. bool runOnFunction(Function &Fn) override;
  97. void copyToMachineFrameInfo(MachineFrameInfo &MFI) const;
  98. };
  99. } // end namespace llvm
  100. #endif // LLVM_CODEGEN_STACKPROTECTOR_H
  101. #ifdef __GNUC__
  102. #pragma GCC diagnostic pop
  103. #endif