LLVM 19.0.0git
MemoryTaggingSupport.cpp
Go to the documentation of this file.
1//== MemoryTaggingSupport.cpp - helpers for memory tagging implementations ===//
2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3// See https://llvm.org/LICENSE.txt for license information.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7//
8// This file declares common infrastructure for HWAddressSanitizer and
9// Aarch64StackTagging.
10//
11//===----------------------------------------------------------------------===//
12
14
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/Analysis/CFG.h"
20#include "llvm/IR/BasicBlock.h"
21#include "llvm/IR/IRBuilder.h"
25
26namespace llvm {
27namespace memtag {
28namespace {
29bool maybeReachableFromEachOther(const SmallVectorImpl<IntrinsicInst *> &Insts,
30 const DominatorTree *DT, const LoopInfo *LI,
31 size_t MaxLifetimes) {
32 // If we have too many lifetime ends, give up, as the algorithm below is N^2.
33 if (Insts.size() > MaxLifetimes)
34 return true;
35 for (size_t I = 0; I < Insts.size(); ++I) {
36 for (size_t J = 0; J < Insts.size(); ++J) {
37 if (I == J)
38 continue;
39 if (isPotentiallyReachable(Insts[I], Insts[J], nullptr, DT, LI))
40 return true;
41 }
42 }
43 return false;
44}
45} // namespace
46
48 const LoopInfo &LI, const Instruction *Start,
51 llvm::function_ref<void(Instruction *)> Callback) {
52 if (Ends.size() == 1 && PDT.dominates(Ends[0], Start)) {
53 Callback(Ends[0]);
54 return true;
55 }
57 for (auto *End : Ends) {
58 EndBlocks.insert(End->getParent());
59 }
60 SmallVector<Instruction *, 8> ReachableRetVec;
61 unsigned NumCoveredExits = 0;
62 for (auto *RI : RetVec) {
63 if (!isPotentiallyReachable(Start, RI, nullptr, &DT, &LI))
64 continue;
65 ReachableRetVec.push_back(RI);
66 // If there is an end in the same basic block as the return, we know for
67 // sure that the return is covered. Otherwise, we can check whether there
68 // is a way to reach the RI from the start of the lifetime without passing
69 // through an end.
70 if (EndBlocks.contains(RI->getParent()) ||
71 !isPotentiallyReachable(Start, RI, &EndBlocks, &DT, &LI)) {
72 ++NumCoveredExits;
73 }
74 }
75 if (NumCoveredExits == ReachableRetVec.size()) {
76 for_each(Ends, Callback);
77 } else {
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 for_each(ReachableRetVec, Callback);
81 // We may have inserted untag outside of the lifetime interval.
82 // Signal the caller to remove the lifetime end call for this alloca.
83 return false;
84 }
85 return true;
86}
87
89 const SmallVectorImpl<IntrinsicInst *> &LifetimeEnd,
90 const DominatorTree *DT, const LoopInfo *LI,
91 size_t MaxLifetimes) {
92 // An alloca that has exactly one start and end in every possible execution.
93 // If it has multiple ends, they have to be unreachable from each other, so
94 // at most one of them is actually used for each execution of the function.
95 return LifetimeStart.size() == 1 &&
96 (LifetimeEnd.size() == 1 ||
97 (LifetimeEnd.size() > 0 &&
98 !maybeReachableFromEachOther(LifetimeEnd, DT, LI, MaxLifetimes)));
99}
100
102 if (isa<ReturnInst>(Inst)) {
104 return CI;
105 return &Inst;
106 }
107 if (isa<ResumeInst, CleanupReturnInst>(Inst)) {
108 return &Inst;
109 }
110 return nullptr;
111}
112
114 // Visit non-intrinsic debug-info records attached to Inst.
116 auto AddIfInteresting = [&](Value *V) {
117 if (auto *AI = dyn_cast_or_null<AllocaInst>(V)) {
118 if (!isInterestingAlloca(*AI))
119 return;
120 AllocaInfo &AInfo = Info.AllocasToInstrument[AI];
121 auto &DVRVec = AInfo.DbgVariableRecords;
122 if (DVRVec.empty() || DVRVec.back() != &DVR)
123 DVRVec.push_back(&DVR);
124 }
125 };
126
127 for_each(DVR.location_ops(), AddIfInteresting);
128 if (DVR.isDbgAssign())
129 AddIfInteresting(DVR.getAddress());
130 }
131
132 if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
133 if (CI->canReturnTwice()) {
134 Info.CallsReturnTwice = true;
135 }
136 }
137 if (AllocaInst *AI = dyn_cast<AllocaInst>(&Inst)) {
138 if (isInterestingAlloca(*AI)) {
139 Info.AllocasToInstrument[AI].AI = AI;
140 }
141 return;
142 }
143 auto *II = dyn_cast<IntrinsicInst>(&Inst);
144 if (II && (II->getIntrinsicID() == Intrinsic::lifetime_start ||
145 II->getIntrinsicID() == Intrinsic::lifetime_end)) {
146 AllocaInst *AI = findAllocaForValue(II->getArgOperand(1));
147 if (!AI) {
148 Info.UnrecognizedLifetimes.push_back(&Inst);
149 return;
150 }
151 if (!isInterestingAlloca(*AI))
152 return;
153 if (II->getIntrinsicID() == Intrinsic::lifetime_start)
154 Info.AllocasToInstrument[AI].LifetimeStart.push_back(II);
155 else
156 Info.AllocasToInstrument[AI].LifetimeEnd.push_back(II);
157 return;
158 }
159 if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&Inst)) {
160 auto AddIfInteresting = [&](Value *V) {
161 if (auto *AI = dyn_cast_or_null<AllocaInst>(V)) {
162 if (!isInterestingAlloca(*AI))
163 return;
164 AllocaInfo &AInfo = Info.AllocasToInstrument[AI];
165 auto &DVIVec = AInfo.DbgVariableIntrinsics;
166 if (DVIVec.empty() || DVIVec.back() != DVI)
167 DVIVec.push_back(DVI);
168 }
169 };
170 for_each(DVI->location_ops(), AddIfInteresting);
171 if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI))
172 AddIfInteresting(DAI->getAddress());
173 }
174
176 if (ExitUntag)
177 Info.RetVec.push_back(ExitUntag);
178}
179
181 return (AI.getAllocatedType()->isSized() &&
182 // FIXME: instrument dynamic allocas, too
183 AI.isStaticAlloca() &&
184 // alloca() may be called with 0 size, ignore it.
186 // We are only interested in allocas not promotable to registers.
187 // Promotable allocas are common under -O0.
188 !isAllocaPromotable(&AI) &&
189 // inalloca allocas are not treated as static, and we don't want
190 // dynamic alloca instrumentation for them as well.
191 !AI.isUsedWithInAlloca() &&
192 // swifterror allocas are register promoted by ISel
193 !AI.isSwiftError()) &&
194 // safe allocas are not interesting
195 !(SSI && SSI->isSafe(AI));
196}
197
199 auto DL = AI.getModule()->getDataLayout();
200 return *AI.getAllocationSize(DL);
201}
202
204 const Align NewAlignment = std::max(Info.AI->getAlign(), Alignment);
205 Info.AI->setAlignment(NewAlignment);
206 auto &Ctx = Info.AI->getFunction()->getContext();
207
209 uint64_t AlignedSize = alignTo(Size, Alignment);
210 if (Size == AlignedSize)
211 return;
212
213 // Add padding to the alloca.
214 Type *AllocatedType =
215 Info.AI->isArrayAllocation()
217 Info.AI->getAllocatedType(),
218 cast<ConstantInt>(Info.AI->getArraySize())->getZExtValue())
219 : Info.AI->getAllocatedType();
220 Type *PaddingType = ArrayType::get(Type::getInt8Ty(Ctx), AlignedSize - Size);
221 Type *TypeWithPadding = StructType::get(AllocatedType, PaddingType);
222 auto *NewAI = new AllocaInst(TypeWithPadding, Info.AI->getAddressSpace(),
223 nullptr, "", Info.AI->getIterator());
224 NewAI->takeName(Info.AI);
225 NewAI->setAlignment(Info.AI->getAlign());
226 NewAI->setUsedWithInAlloca(Info.AI->isUsedWithInAlloca());
227 NewAI->setSwiftError(Info.AI->isSwiftError());
228 NewAI->copyMetadata(*Info.AI);
229
230 Value *NewPtr = NewAI;
231
232 // TODO: Remove when typed pointers dropped
233 if (Info.AI->getType() != NewAI->getType())
234 NewPtr = new BitCastInst(NewAI, Info.AI->getType(), "", Info.AI->getIterator());
235
236 Info.AI->replaceAllUsesWith(NewPtr);
237 Info.AI->eraseFromParent();
238 Info.AI = NewAI;
239}
240
242 auto *II = dyn_cast<IntrinsicInst>(V);
243 return II && II->isLifetimeStartOrEnd();
244}
245
247 Module *M = IRB.GetInsertBlock()->getParent()->getParent();
248 Function *ReadRegister = Intrinsic::getDeclaration(
249 M, Intrinsic::read_register, IRB.getIntPtrTy(M->getDataLayout()));
250 MDNode *MD =
251 MDNode::get(M->getContext(), {MDString::get(M->getContext(), Name)});
252 Value *Args[] = {MetadataAsValue::get(M->getContext(), MD)};
253 return IRB.CreateCall(ReadRegister, Args);
254}
255
256Value *getPC(const Triple &TargetTriple, IRBuilder<> &IRB) {
257 Module *M = IRB.GetInsertBlock()->getParent()->getParent();
258 if (TargetTriple.getArch() == Triple::aarch64)
259 return memtag::readRegister(IRB, "pc");
260 return IRB.CreatePtrToInt(IRB.GetInsertBlock()->getParent(),
261 IRB.getIntPtrTy(M->getDataLayout()));
262}
263
266 Module *M = F->getParent();
267 auto *GetStackPointerFn = Intrinsic::getDeclaration(
268 M, Intrinsic::frameaddress,
269 IRB.getPtrTy(M->getDataLayout().getAllocaAddrSpace()));
270 return IRB.CreatePtrToInt(
271 IRB.CreateCall(GetStackPointerFn,
272 {Constant::getNullValue(IRB.getInt32Ty())}),
273 IRB.getIntPtrTy(M->getDataLayout()));
274}
275
277 Module *M = IRB.GetInsertBlock()->getParent()->getParent();
278 // Android provides a fixed TLS slot for sanitizers. See TLS_SLOT_SANITIZER
279 // in Bionic's libc/private/bionic_tls.h.
280 Function *ThreadPointerFunc =
281 Intrinsic::getDeclaration(M, Intrinsic::thread_pointer);
282 return IRB.CreateConstGEP1_32(IRB.getInt8Ty(),
283 IRB.CreateCall(ThreadPointerFunc), 8 * Slot);
284}
285
286} // namespace memtag
287} // namespace llvm
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file contains some templates that are useful if you are working with the STL at all.
an instruction to allocate memory on the stack
Definition: Instructions.h:59
bool isSwiftError() const
Return true if this alloca is used as a swifterror argument to a call.
Definition: Instructions.h:157
bool isStaticAlloca() const
Return true if this alloca is in the entry block of the function and is a constant size.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Definition: Instructions.h:125
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call.
Definition: Instructions.h:147
std::optional< TypeSize > getAllocationSize(const DataLayout &DL) const
Get allocation size in bytes.
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:647
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:206
const CallInst * getTerminatingMustTailCall() const
Returns the call instruction marked 'musttail' prior to the terminating return instruction of this ba...
Definition: BasicBlock.cpp:293
This class represents a no-op cast from one type to another.
This class represents a function call, abstracting a target machine's calling convention.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
Value * CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, const Twine &Name="")
Definition: IRBuilder.h:1881
IntegerType * getIntPtrTy(const DataLayout &DL, unsigned AddrSpace=0)
Fetch the type of an integer with size at least as big as that of a pointer in the given address spac...
Definition: IRBuilder.h:575
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:174
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2117
PointerType * getPtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer.
Definition: IRBuilder.h:569
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2412
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition: IRBuilder.h:516
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2666
iterator_range< simple_ilist< DbgRecord >::iterator > getDbgRecordRange() const
Return a range over the DbgRecords attached to this instruction.
Definition: Instruction.h:84
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:82
const BasicBlock * getParent() const
Definition: Instruction.h:152
Metadata node.
Definition: Metadata.h:1067
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1541
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:103
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.h:293
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
bool dominates(const Instruction *I1, const Instruction *I2) const
Return true if I1 dominates I2.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:342
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:366
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
bool isSafe(const AllocaInst &AI) const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:373
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:361
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:302
static IntegerType * getInt8Ty(LLVMContext &C)
LLVM Value Representation.
Definition: Value.h:74
An efficient, type-erasing, non-owning reference to a callable.
bool isInterestingAlloca(const AllocaInst &AI)
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1461
Value * getFP(IRBuilder<> &IRB)
bool isStandardLifetime(const SmallVectorImpl< IntrinsicInst * > &LifetimeStart, const SmallVectorImpl< IntrinsicInst * > &LifetimeEnd, const DominatorTree *DT, const LoopInfo *LI, size_t MaxLifetimes)
bool forAllReachableExits(const DominatorTree &DT, const PostDominatorTree &PDT, const LoopInfo &LI, const Instruction *Start, const SmallVectorImpl< IntrinsicInst * > &Ends, const SmallVectorImpl< Instruction * > &RetVec, llvm::function_ref< void(Instruction *)> Callback)
uint64_t getAllocaSizeInBytes(const AllocaInst &AI)
Value * getAndroidSlotPtr(IRBuilder<> &IRB, int Slot)
Value * readRegister(IRBuilder<> &IRB, StringRef Name)
Instruction * getUntagLocationIfFunctionExit(Instruction &Inst)
void alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Align)
Value * getPC(const Triple &TargetTriple, IRBuilder<> &IRB)
bool isLifetimeIntrinsic(Value *V)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
UnaryFunction for_each(R &&Range, UnaryFunction F)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1715
AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
bool isAllocaPromotable(const AllocaInst *AI)
Return true if this alloca is legal for promotion.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
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:231
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
SmallVector< DbgVariableIntrinsic *, 2 > DbgVariableIntrinsics
SmallVector< DbgVariableRecord *, 2 > DbgVariableRecords
MapVector< AllocaInst *, AllocaInfo > AllocasToInstrument
SmallVector< Instruction *, 4 > UnrecognizedLifetimes
SmallVector< Instruction *, 8 > RetVec