LLVM  14.0.0git
AddressSanitizerCommon.h
Go to the documentation of this file.
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"
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 
26 public:
28  bool IsWrite;
31  // The mask Value, if we're looking at a masked load/store.
33 
34  InterestingMemoryOperand(Instruction *I, unsigned OperandNo, bool IsWrite,
35  class Type *OpType, MaybeAlign Alignment,
36  Value *MaybeMask = nullptr)
38  const DataLayout &DL = I->getModule()->getDataLayout();
39  TypeSize = DL.getTypeStoreSizeInBits(OpType);
40  PtrUse = &I->getOperandUse(OperandNo);
41  }
42 
43  Instruction *getInsn() { return cast<Instruction>(PtrUse->getUser()); }
44 
45  Value *getPtr() { return PtrUse->get(); }
46 };
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>
57  const Instruction *Start,
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 
93 // Get AddressSanitizer parameters.
94 void getAddressSanitizerParams(const Triple &TargetTriple, int LongSize,
95  bool IsKasan, uint64_t *ShadowBase,
96  int *MappingScale, bool *OrShadowOffset);
97 
98 } // namespace llvm
99 
100 #endif
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
IntrinsicInst.h
llvm::SmallVector< Instruction *, 8 >
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:151
llvm::InterestingMemoryOperand
Definition: AddressSanitizerCommon.h:25
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
llvm::forAllReachableExits
bool forAllReachableExits(const DominatorTree &DT, const PostDominatorTree &PDT, const Instruction *Start, const SmallVectorImpl< IntrinsicInst * > &Ends, const SmallVectorImpl< Instruction * > &RetVec, F Callback)
Definition: AddressSanitizerCommon.h:56
llvm::InterestingMemoryOperand::InterestingMemoryOperand
InterestingMemoryOperand(Instruction *I, unsigned OperandNo, bool IsWrite, class Type *OpType, MaybeAlign Alignment, Value *MaybeMask=nullptr)
Definition: AddressSanitizerCommon.h:34
llvm::InterestingMemoryOperand::TypeSize
uint64_t TypeSize
Definition: AddressSanitizerCommon.h:29
F
#define F(x, y, z)
Definition: MD5.cpp:56
Instruction.h
PostDominators.h
llvm::InterestingMemoryOperand::getInsn
Instruction * getInsn()
Definition: AddressSanitizerCommon.h:43
llvm::MaybeAlign
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:109
llvm::isPotentiallyReachable
bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet=nullptr, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction 'To' is reachable from 'From', without passing through any blocks in Ex...
Definition: CFG.cpp:236
llvm::Instruction
Definition: Instruction.h:45
uint64_t
I
#define I(x, y, z)
Definition: MD5.cpp:59
CFG.h
llvm::any_of
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1607
llvm::PostDominatorTree
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
Definition: PostDominators.h:28
llvm::InterestingMemoryOperand::MaybeMask
Value * MaybeMask
Definition: AddressSanitizerCommon.h:32
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::InterestingMemoryOperand::Alignment
MaybeAlign Alignment
Definition: AddressSanitizerCommon.h:30
llvm::TypeSize
Definition: TypeSize.h:416
llvm::InterestingMemoryOperand::getPtr
Value * getPtr()
Definition: AddressSanitizerCommon.h:45
llvm::getAddressSanitizerParams
void getAddressSanitizerParams(const Triple &TargetTriple, int LongSize, bool IsKasan, uint64_t *ShadowBase, int *MappingScale, bool *OrShadowOffset)
Definition: AddressSanitizer.cpp:583
Dominators.h
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::InterestingMemoryOperand::IsWrite
bool IsWrite
Definition: AddressSanitizerCommon.h:28
llvm::InterestingMemoryOperand::PtrUse
Use * PtrUse
Definition: AddressSanitizerCommon.h:27
llvm::PostDominatorTree::dominates
bool dominates(const Instruction *I1, const Instruction *I2) const
Return true if I1 dominates I2.
Definition: PostDominators.cpp:54
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44