11#define DEBUG_TYPE "amdgpu-asan-instrumentation"
21 return std::max(32U, 1U << AsanScale);
33 if (SizeInBytes <= MinRZ / 2) {
37 RZ = MinRZ - SizeInBytes;
40 RZ = std::clamp((SizeInBytes / MinRZ / 4) * MinRZ, MinRZ, kMaxRZ);
43 if (SizeInBytes % MinRZ)
44 RZ += MinRZ - (SizeInBytes % MinRZ);
47 assert((RZ + SizeInBytes) % MinRZ == 0);
69 Trm->getParent()->setName(
"asan.report");
81 uint32_t TypeStoreSize,
int AsanScale) {
84 Value *LastAccessedByte =
85 IRB.
CreateAnd(AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
87 if (TypeStoreSize / 8 > 1)
89 LastAccessedByte, ConstantInt::get(IntptrTy, TypeStoreSize / 8 - 1));
100 size_t AccessSizeIndex,
101 Value *SizeArgument,
bool Recover) {
116 AsanErrorCallbackSizedOS.
str(),
124 << (1ULL << AccessSizeIndex) << EndingStr;
127 AsanErrorCallbackOS.
str(),
130 Call = IRB.
CreateCall(AsanErrorCallbackSized, {
Addr, SizeArgument});
135 Call->setCannotMerge();
146 Value *ShadowBase = ConstantInt::get(IntptrTy, AsanOffset);
147 return IRB.
CreateAdd(Shadow, ShadowBase);
154 bool IsWrite,
Value *SizeArgument,
155 bool UseCalls,
bool Recover,
int AsanScale,
158 Type *IntptrTy = M.getDataLayout().getIntPtrType(
163 std::max(8U, TypeStoreSize >> AsanScale));
167 memToShadow(M, IRB, IntptrTy, AddrLong, AsanScale, AsanOffset);
169 std::max<uint64_t>(Alignment.
value() >> AsanScale, 1);
174 TypeStoreSize, AsanScale);
179 AccessSizeIndex, SizeArgument, Recover);
186 TypeSize TypeStoreSize,
bool IsWrite,
187 Value *SizeArgument,
bool UseCalls,
bool Recover,
188 int AsanScale,
int AsanOffset) {
190 unsigned Granularity = 1 << AsanScale;
198 if (Alignment.
value() >= Granularity ||
199 Alignment.
value() >= FixedSize / 8)
201 M, IRB, OrigIns, InsertBefore,
Addr, Alignment, FixedSize, IsWrite,
202 SizeArgument, UseCalls, Recover, AsanScale, AsanOffset);
208 Type *IntptrTy = M.getDataLayout().getIntPtrType(AddrTy);
216 SizeArgument, UseCalls, Recover, AsanScale, AsanOffset);
218 SizeArgument, UseCalls, Recover, AsanScale, AsanOffset);
225 if (
LoadInst *LI = dyn_cast<LoadInst>(
I)) {
226 Interesting.
emplace_back(
I, LI->getPointerOperandIndex(),
false,
227 LI->getType(), LI->getAlign());
228 }
else if (
StoreInst *SI = dyn_cast<StoreInst>(
I)) {
229 Interesting.
emplace_back(
I, SI->getPointerOperandIndex(),
true,
230 SI->getValueOperand()->getType(), SI->getAlign());
232 Interesting.
emplace_back(
I, RMW->getPointerOperandIndex(),
true,
233 RMW->getValOperand()->getType(), std::nullopt);
235 Interesting.
emplace_back(
I, XCHG->getPointerOperandIndex(),
true,
236 XCHG->getCompareOperand()->getType(),
238 }
else if (
auto CI = dyn_cast<CallInst>(
I)) {
239 switch (CI->getIntrinsicID()) {
240 case Intrinsic::masked_load:
241 case Intrinsic::masked_store:
242 case Intrinsic::masked_gather:
243 case Intrinsic::masked_scatter: {
244 bool IsWrite = CI->getType()->isVoidTy();
246 unsigned OpOffset = IsWrite ? 1 : 0;
247 Type *Ty = IsWrite ? CI->getArgOperand(0)->getType() : CI->getType();
250 if (
auto *
Op = dyn_cast<ConstantInt>(CI->getOperand(1 + OpOffset)))
251 Alignment =
Op->getMaybeAlignValue();
252 Value *Mask = CI->getOperand(2 + OpOffset);
253 Interesting.
emplace_back(
I, OpOffset, IsWrite, Ty, Alignment, Mask);
256 case Intrinsic::masked_expandload:
257 case Intrinsic::masked_compressstore: {
258 bool IsWrite = CI->getIntrinsicID() == Intrinsic::masked_compressstore;
259 unsigned OpOffset = IsWrite ? 1 : 0;
260 auto BasePtr = CI->getOperand(OpOffset);
261 MaybeAlign Alignment = BasePtr->getPointerAlignment(
DL);
262 Type *Ty = IsWrite ? CI->getArgOperand(0)->getType() : CI->getType();
264 Value *Mask = CI->getOperand(1 + OpOffset);
265 Type *IntptrTy = M.getDataLayout().getIntPtrType(
266 M.getContext(), BasePtr->getType()->getPointerAddressSpace());
269 Value *ExtMask = IB.CreateZExt(Mask, ExtTy);
270 Value *EVL = IB.CreateAddReduce(ExtMask);
271 Value *TrueMask = ConstantInt::get(Mask->getType(), 1);
272 Interesting.
emplace_back(
I, OpOffset, IsWrite, Ty, Alignment, TrueMask,
276 case Intrinsic::vp_load:
277 case Intrinsic::vp_store:
278 case Intrinsic::experimental_vp_strided_load:
279 case Intrinsic::experimental_vp_strided_store: {
280 auto *VPI = cast<VPIntrinsic>(CI);
281 unsigned IID = CI->getIntrinsicID();
282 bool IsWrite = CI->getType()->isVoidTy();
283 unsigned PtrOpNo = *VPI->getMemoryPointerParamPos(IID);
284 Type *Ty = IsWrite ? CI->getArgOperand(0)->getType() : CI->getType();
285 MaybeAlign Alignment = VPI->getOperand(PtrOpNo)->getPointerAlignment(
DL);
286 Value *Stride =
nullptr;
287 if (IID == Intrinsic::experimental_vp_strided_store ||
288 IID == Intrinsic::experimental_vp_strided_load) {
289 Stride = VPI->getOperand(PtrOpNo + 1);
294 if (!isa<ConstantInt>(Stride) ||
295 cast<ConstantInt>(Stride)->getZExtValue() % PointerAlign != 0)
296 Alignment =
Align(1);
299 VPI->getMaskParam(), VPI->getVectorLengthParam(),
303 case Intrinsic::vp_gather:
304 case Intrinsic::vp_scatter: {
305 auto *VPI = cast<VPIntrinsic>(CI);
306 unsigned IID = CI->getIntrinsicID();
307 bool IsWrite = IID == Intrinsic::vp_scatter;
308 unsigned PtrOpNo = *VPI->getMemoryPointerParamPos(IID);
309 Type *Ty = IsWrite ? CI->getArgOperand(0)->getType() : CI->getType();
310 MaybeAlign Alignment = VPI->getPointerAlignment();
313 VPI->getVectorLengthParam());
316 case Intrinsic::amdgcn_raw_buffer_load:
317 case Intrinsic::amdgcn_raw_ptr_buffer_load:
318 case Intrinsic::amdgcn_raw_buffer_load_format:
319 case Intrinsic::amdgcn_raw_ptr_buffer_load_format:
320 case Intrinsic::amdgcn_raw_tbuffer_load:
321 case Intrinsic::amdgcn_raw_ptr_tbuffer_load:
322 case Intrinsic::amdgcn_struct_buffer_load:
323 case Intrinsic::amdgcn_struct_ptr_buffer_load:
324 case Intrinsic::amdgcn_struct_buffer_load_format:
325 case Intrinsic::amdgcn_struct_ptr_buffer_load_format:
326 case Intrinsic::amdgcn_struct_tbuffer_load:
327 case Intrinsic::amdgcn_struct_ptr_tbuffer_load:
328 case Intrinsic::amdgcn_s_buffer_load:
329 case Intrinsic::amdgcn_global_load_tr_b64:
330 case Intrinsic::amdgcn_global_load_tr_b128: {
331 unsigned PtrOpNo = 0;
332 bool IsWrite =
false;
333 Type *Ty = CI->getType();
334 Value *
Ptr = CI->getArgOperand(PtrOpNo);
336 Interesting.
emplace_back(
I, PtrOpNo, IsWrite, Ty, Alignment);
339 case Intrinsic::amdgcn_raw_tbuffer_store:
340 case Intrinsic::amdgcn_raw_ptr_tbuffer_store:
341 case Intrinsic::amdgcn_raw_buffer_store:
342 case Intrinsic::amdgcn_raw_ptr_buffer_store:
343 case Intrinsic::amdgcn_raw_buffer_store_format:
344 case Intrinsic::amdgcn_raw_ptr_buffer_store_format:
345 case Intrinsic::amdgcn_struct_buffer_store:
346 case Intrinsic::amdgcn_struct_ptr_buffer_store:
347 case Intrinsic::amdgcn_struct_buffer_store_format:
348 case Intrinsic::amdgcn_struct_ptr_buffer_store_format:
349 case Intrinsic::amdgcn_struct_tbuffer_store:
350 case Intrinsic::amdgcn_struct_ptr_tbuffer_store: {
351 unsigned PtrOpNo = 1;
353 Value *
Ptr = CI->getArgOperand(PtrOpNo);
356 Interesting.
emplace_back(
I, PtrOpNo, IsWrite, Ty, Alignment);
360 for (
unsigned ArgNo = 0; ArgNo < CI->arg_size(); ArgNo++) {
361 if (
Type *Ty = CI->getParamByRefType(ArgNo)) {
363 }
else if (
Type *Ty = CI->getParamByValType(ArgNo)) {
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
const char kAsanReportErrorTemplate[]
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
An instruction that atomically checks whether a specified value is in a memory location,...
an instruction that atomically reads a memory location, combines it with another value,...
This class represents a function call, abstracting a target machine's calling convention.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, const char *Name)
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Value * CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name="")
BasicBlock::iterator GetInsertPoint() const
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Value * CreateTypeSize(Type *DstType, TypeSize Size)
Create an expression which evaluates to the number of units in Size at runtime.
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
IntegerType * getInt64Ty()
Fetch the type representing a 64-bit integer.
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg != 0.
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Type * getVoidTy()
Fetch the type representing void.
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="", MDNode *FPMathTag=nullptr)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
An instruction for reading from memory.
MDNode * createUnlikelyBranchWeights()
Return metadata containing two branch weights, with significant bias towards false destination.
A Module instance is used to store all the information related to an LLVM module.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
The instances of the Type class are immutable: once they are created, they are never changed.
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
constexpr ScalarTy getFixedValue() const
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
A raw_ostream that writes to an SmallVector or SmallString.
StringRef str() const
Return a StringRef for the vector contents.
static uint64_t getMinRedzoneSizeForGlobal(int AsanScale)
static Value * memToShadow(Module &M, IRBuilder<> &IRB, Type *IntptrTy, Value *Shadow, int AsanScale, uint32_t AsanOffset)
void getInterestingMemoryOperands(Module &M, Instruction *I, SmallVectorImpl< InterestingMemoryOperand > &Interesting)
Get all the memory operands from the instruction that needs to be instrumented.
static uint64_t getRedzoneSizeForScale(int AsanScale)
static Instruction * generateCrashCode(Module &M, IRBuilder<> &IRB, Type *IntptrTy, Instruction *InsertBefore, Value *Addr, bool IsWrite, size_t AccessSizeIndex, Value *SizeArgument, bool Recover)
static Instruction * genAMDGPUReportBlock(Module &M, IRBuilder<> &IRB, Value *Cond, bool Recover)
static size_t TypeStoreSizeToSizeIndex(uint32_t TypeSize)
static Value * createSlowPathCmp(Module &M, IRBuilder<> &IRB, Type *IntptrTy, Value *AddrLong, Value *ShadowValue, uint32_t TypeStoreSize, int AsanScale)
void instrumentAddress(Module &M, IRBuilder<> &IRB, Instruction *OrigIns, Instruction *InsertBefore, Value *Addr, Align Alignment, TypeSize TypeStoreSize, bool IsWrite, Value *SizeArgument, bool UseCalls, bool Recover, int AsanScale, int AsanOffset)
Instrument the memory operand Addr.
static void instrumentAddressImpl(Module &M, IRBuilder<> &IRB, Instruction *OrigIns, Instruction *InsertBefore, Value *Addr, Align Alignment, uint32_t TypeStoreSize, bool IsWrite, Value *SizeArgument, bool UseCalls, bool Recover, int AsanScale, int AsanOffset)
uint64_t getRedzoneSizeForGlobal(int AsanScale, uint64_t SizeInBytes)
Given SizeInBytes of the Value to be instrunmented, Returns the redzone size corresponding to it.
This is an optimization pass for GlobalISel generic memory operations.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Instruction * SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
This struct is a compact representation of a valid (non-zero power of two) alignment.
uint64_t value() const
This is a hole in the type system and should not be abused.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.