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