Skip to content

Commit 33f1aed

Browse files
authored
[AMDGPU] Update instrumentAddress method to support aligned size and unusual size accesses. (llvm#104804)
This PR updates instrumentAddress api to support properly aligned sizes and unsual size accesses. Changes ported from asan pass.
1 parent cbd3024 commit 33f1aed

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

llvm/lib/Target/AMDGPU/AMDGPUAsanInstrumentation.cpp

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,13 @@ static Value *memToShadow(Module &M, IRBuilder<> &IRB, Type *IntptrTy,
147147
return IRB.CreateAdd(Shadow, ShadowBase);
148148
}
149149

150-
void instrumentAddress(Module &M, IRBuilder<> &IRB, Instruction *OrigIns,
151-
Instruction *InsertBefore, Value *Addr,
152-
MaybeAlign Alignment, uint32_t TypeStoreSize,
153-
bool IsWrite, Value *SizeArgument, bool UseCalls,
154-
bool Recover, int AsanScale, int AsanOffset) {
150+
static void instrumentAddressImpl(Module &M, IRBuilder<> &IRB,
151+
Instruction *OrigIns,
152+
Instruction *InsertBefore, Value *Addr,
153+
Align Alignment, uint32_t TypeStoreSize,
154+
bool IsWrite, Value *SizeArgument,
155+
bool UseCalls, bool Recover, int AsanScale,
156+
int AsanOffset) {
155157
Type *AddrTy = Addr->getType();
156158
Type *IntptrTy = M.getDataLayout().getIntPtrType(
157159
M.getContext(), AddrTy->getPointerAddressSpace());
@@ -164,7 +166,7 @@ void instrumentAddress(Module &M, IRBuilder<> &IRB, Instruction *OrigIns,
164166
Value *ShadowPtr =
165167
memToShadow(M, IRB, IntptrTy, AddrLong, AsanScale, AsanOffset);
166168
const uint64_t ShadowAlign =
167-
std::max<uint64_t>(Alignment.valueOrOne().value() >> AsanScale, 1);
169+
std::max<uint64_t>(Alignment.value() >> AsanScale, 1);
168170
Value *ShadowValue = IRB.CreateAlignedLoad(
169171
ShadowTy, IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy), Align(ShadowAlign));
170172
Value *Cmp = IRB.CreateIsNotNull(ShadowValue);
@@ -179,6 +181,43 @@ void instrumentAddress(Module &M, IRBuilder<> &IRB, Instruction *OrigIns,
179181
return;
180182
}
181183

184+
void instrumentAddress(Module &M, IRBuilder<> &IRB, Instruction *OrigIns,
185+
Instruction *InsertBefore, Value *Addr, Align Alignment,
186+
TypeSize TypeStoreSize, bool IsWrite,
187+
Value *SizeArgument, bool UseCalls, bool Recover,
188+
int AsanScale, int AsanOffset) {
189+
if (!TypeStoreSize.isScalable()) {
190+
unsigned Granularity = 1 << AsanScale;
191+
const auto FixedSize = TypeStoreSize.getFixedValue();
192+
switch (FixedSize) {
193+
case 8:
194+
case 16:
195+
case 32:
196+
case 64:
197+
case 128:
198+
if (Alignment.value() >= Granularity ||
199+
Alignment.value() >= FixedSize / 8)
200+
return instrumentAddressImpl(
201+
M, IRB, OrigIns, InsertBefore, Addr, Alignment, FixedSize, IsWrite,
202+
SizeArgument, UseCalls, Recover, AsanScale, AsanOffset);
203+
}
204+
}
205+
// Instrument unusual size or unusual alignment.
206+
IRB.SetInsertPoint(InsertBefore);
207+
Type *AddrTy = Addr->getType();
208+
Type *IntptrTy = M.getDataLayout().getIntPtrType(AddrTy);
209+
Value *NumBits = IRB.CreateTypeSize(IntptrTy, TypeStoreSize);
210+
Value *Size = IRB.CreateLShr(NumBits, ConstantInt::get(IntptrTy, 3));
211+
Value *AddrLong = IRB.CreatePtrToInt(Addr, IntptrTy);
212+
Value *SizeMinusOne = IRB.CreateAdd(Size, ConstantInt::get(IntptrTy, -1));
213+
Value *LastByte =
214+
IRB.CreateIntToPtr(IRB.CreateAdd(AddrLong, SizeMinusOne), AddrTy);
215+
instrumentAddressImpl(M, IRB, OrigIns, InsertBefore, Addr, {}, 8, IsWrite,
216+
SizeArgument, UseCalls, Recover, AsanScale, AsanOffset);
217+
instrumentAddressImpl(M, IRB, OrigIns, InsertBefore, LastByte, {}, 8, IsWrite,
218+
SizeArgument, UseCalls, Recover, AsanScale, AsanOffset);
219+
}
220+
182221
void getInterestingMemoryOperands(
183222
Module &M, Instruction *I,
184223
SmallVectorImpl<InterestingMemoryOperand> &Interesting) {

llvm/lib/Target/AMDGPU/AMDGPUAsanInstrumentation.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ uint64_t getRedzoneSizeForGlobal(int Scale, uint64_t SizeInBytes);
4343
/// Instrument the memory operand Addr.
4444
/// Generates report blocks that catch the addressing errors.
4545
void instrumentAddress(Module &M, IRBuilder<> &IRB, Instruction *OrigIns,
46-
Instruction *InsertBefore, Value *Addr,
47-
MaybeAlign Alignment, uint32_t TypeStoreSize,
48-
bool IsWrite, Value *SizeArgument, bool UseCalls,
49-
bool Recover, int Scale, int Offset);
46+
Instruction *InsertBefore, Value *Addr, Align Alignment,
47+
TypeSize TypeStoreSize, bool IsWrite,
48+
Value *SizeArgument, bool UseCalls, bool Recover,
49+
int Scale, int Offset);
5050

5151
/// Get all the memory operands from the instruction
5252
/// that needs to be instrumented

0 commit comments

Comments
 (0)