AddressSanitizerCommon.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--------- Definition of the AddressSanitizer class ---------*- 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 file declares common infrastructure for AddressSanitizer and
  15. // HWAddressSanitizer.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_ADDRESSSANITIZERCOMMON_H
  19. #define LLVM_TRANSFORMS_INSTRUMENTATION_ADDRESSSANITIZERCOMMON_H
  20. #include "llvm/Analysis/CFG.h"
  21. #include "llvm/Analysis/PostDominators.h"
  22. #include "llvm/IR/Dominators.h"
  23. #include "llvm/IR/Instruction.h"
  24. #include "llvm/IR/IntrinsicInst.h"
  25. #include "llvm/IR/Module.h"
  26. namespace llvm {
  27. class InterestingMemoryOperand {
  28. public:
  29. Use *PtrUse;
  30. bool IsWrite;
  31. Type *OpType;
  32. uint64_t TypeSize;
  33. MaybeAlign Alignment;
  34. // The mask Value, if we're looking at a masked load/store.
  35. Value *MaybeMask;
  36. InterestingMemoryOperand(Instruction *I, unsigned OperandNo, bool IsWrite,
  37. class Type *OpType, MaybeAlign Alignment,
  38. Value *MaybeMask = nullptr)
  39. : IsWrite(IsWrite), OpType(OpType), Alignment(Alignment),
  40. MaybeMask(MaybeMask) {
  41. const DataLayout &DL = I->getModule()->getDataLayout();
  42. TypeSize = DL.getTypeStoreSizeInBits(OpType);
  43. PtrUse = &I->getOperandUse(OperandNo);
  44. }
  45. Instruction *getInsn() { return cast<Instruction>(PtrUse->getUser()); }
  46. Value *getPtr() { return PtrUse->get(); }
  47. };
  48. // For an alloca valid between lifetime markers Start and Ends, call the
  49. // Callback for all possible exits out of the lifetime in the containing
  50. // function, which can return from the instructions in RetVec.
  51. //
  52. // Returns whether Ends covered all possible exits. If they did not,
  53. // the caller should remove Ends to ensure that work done at the other
  54. // exits does not happen outside of the lifetime.
  55. template <typename F>
  56. bool forAllReachableExits(const DominatorTree &DT, const PostDominatorTree &PDT,
  57. const Instruction *Start,
  58. const SmallVectorImpl<IntrinsicInst *> &Ends,
  59. const SmallVectorImpl<Instruction *> &RetVec,
  60. F Callback) {
  61. if (Ends.size() == 1 && PDT.dominates(Ends[0], Start)) {
  62. Callback(Ends[0]);
  63. return true;
  64. }
  65. SmallVector<Instruction *, 8> ReachableRetVec;
  66. unsigned NumCoveredExits = 0;
  67. for (auto *RI : RetVec) {
  68. if (!isPotentiallyReachable(Start, RI, nullptr, &DT))
  69. continue;
  70. ReachableRetVec.push_back(RI);
  71. // TODO(fmayer): We don't support diamond shapes, where multiple lifetime
  72. // ends together dominate the RI, but none of them does by itself.
  73. // Check how often this happens and decide whether to support this here.
  74. if (std::any_of(Ends.begin(), Ends.end(),
  75. [&](Instruction *End) { return DT.dominates(End, RI); }))
  76. ++NumCoveredExits;
  77. }
  78. // If there's a mix of covered and non-covered exits, just put the untag
  79. // on exits, so we avoid the redundancy of untagging twice.
  80. if (NumCoveredExits == ReachableRetVec.size()) {
  81. for (auto *End : Ends)
  82. Callback(End);
  83. } else {
  84. for (auto *RI : ReachableRetVec)
  85. Callback(RI);
  86. // We may have inserted untag outside of the lifetime interval.
  87. // Signal the caller to remove the lifetime end call for this alloca.
  88. return false;
  89. }
  90. return true;
  91. }
  92. // Get AddressSanitizer parameters.
  93. void getAddressSanitizerParams(const Triple &TargetTriple, int LongSize,
  94. bool IsKasan, uint64_t *ShadowBase,
  95. int *MappingScale, bool *OrShadowOffset);
  96. } // namespace llvm
  97. #endif
  98. #ifdef __GNUC__
  99. #pragma GCC diagnostic pop
  100. #endif