StackProtector.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/CodeGen/MachineFrameInfo.h"
  25. #include "llvm/IR/Instructions.h"
  26. #include "llvm/IR/ValueMap.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. /// A mapping of AllocaInsts to their required SSP layout.
  40. using SSPLayoutMap = DenseMap<const AllocaInst *,
  41. MachineFrameInfo::SSPLayoutKind>;
  42. const TargetMachine *TM = nullptr;
  43. /// TLI - Keep a pointer of a TargetLowering to consult for determining
  44. /// target type sizes.
  45. const TargetLoweringBase *TLI = nullptr;
  46. Triple Trip;
  47. Function *F;
  48. Module *M;
  49. DominatorTree *DT;
  50. /// Layout - Mapping of allocations to the required SSPLayoutKind.
  51. /// StackProtector analysis will update this map when determining if an
  52. /// AllocaInst triggers a stack protector.
  53. SSPLayoutMap Layout;
  54. /// The minimum size of buffers that will receive stack smashing
  55. /// protection when -fstack-protection is used.
  56. unsigned SSPBufferSize = 0;
  57. /// VisitedPHIs - The set of PHI nodes visited when determining
  58. /// if a variable's reference has been taken. This set
  59. /// is maintained to ensure we don't visit the same PHI node multiple
  60. /// times.
  61. SmallPtrSet<const PHINode *, 16> VisitedPHIs;
  62. // A prologue is generated.
  63. bool HasPrologue = false;
  64. // IR checking code is generated.
  65. bool HasIRCheck = false;
  66. /// InsertStackProtectors - Insert code into the prologue and epilogue of
  67. /// the function.
  68. ///
  69. /// - The prologue code loads and stores the stack guard onto the stack.
  70. /// - The epilogue checks the value stored in the prologue against the
  71. /// original value. It calls __stack_chk_fail if they differ.
  72. bool InsertStackProtectors();
  73. /// CreateFailBB - Create a basic block to jump to when the stack protector
  74. /// check fails.
  75. BasicBlock *CreateFailBB();
  76. /// ContainsProtectableArray - Check whether the type either is an array or
  77. /// contains an array of sufficient size so that we need stack protectors
  78. /// for it.
  79. /// \param [out] IsLarge is set to true if a protectable array is found and
  80. /// it is "large" ( >= ssp-buffer-size). In the case of a structure with
  81. /// multiple arrays, this gets set if any of them is large.
  82. bool ContainsProtectableArray(Type *Ty, bool &IsLarge, bool Strong = false,
  83. bool InStruct = false) const;
  84. /// Check whether a stack allocation has its address taken.
  85. bool HasAddressTaken(const Instruction *AI, TypeSize AllocSize);
  86. /// RequiresStackProtector - Check whether or not this function needs a
  87. /// stack protector based upon the stack protector level.
  88. bool RequiresStackProtector();
  89. public:
  90. static char ID; // Pass identification, replacement for typeid.
  91. StackProtector();
  92. void getAnalysisUsage(AnalysisUsage &AU) const override;
  93. // Return true if StackProtector is supposed to be handled by SelectionDAG.
  94. bool shouldEmitSDCheck(const BasicBlock &BB) const;
  95. bool runOnFunction(Function &Fn) override;
  96. void copyToMachineFrameInfo(MachineFrameInfo &MFI) const;
  97. };
  98. } // end namespace llvm
  99. #endif // LLVM_CODEGEN_STACKPROTECTOR_H
  100. #ifdef __GNUC__
  101. #pragma GCC diagnostic pop
  102. #endif