Skip to content

Commit 644a763

Browse files
authored
[DeviceASAN] Implement asan_load/store for different address space (intel#15936)
1 parent 691cb90 commit 644a763

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

libdevice/sanitizer_utils.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -668,10 +668,10 @@ constexpr size_t AlignMask(size_t n) { return n - 1; }
668668
/// ASAN Load/Store Report Built-ins
669669
///
670670

671-
#define ASAN_REPORT_ERROR(type, is_write, size) \
672-
DEVICE_EXTERN_C_NOINLINE void __asan_##type##size( \
673-
uptr addr, uint32_t as, const char __SYCL_CONSTANT__ *file, \
674-
uint32_t line, const char __SYCL_CONSTANT__ *func) { \
671+
#define ASAN_REPORT_ERROR_BASE(type, is_write, size, as) \
672+
DEVICE_EXTERN_C_NOINLINE void __asan_##type##size##_as##as( \
673+
uptr addr, const char __SYCL_CONSTANT__ *file, uint32_t line, \
674+
const char __SYCL_CONSTANT__ *func) { \
675675
if (addr & AlignMask(size)) { \
676676
__asan_report_misalign_error(addr, as, size, is_write, addr, file, line, \
677677
func); \
@@ -681,9 +681,9 @@ constexpr size_t AlignMask(size_t n) { return n - 1; }
681681
func); \
682682
} \
683683
} \
684-
DEVICE_EXTERN_C_NOINLINE void __asan_##type##size##_noabort( \
685-
uptr addr, uint32_t as, const char __SYCL_CONSTANT__ *file, \
686-
uint32_t line, const char __SYCL_CONSTANT__ *func) { \
684+
DEVICE_EXTERN_C_NOINLINE void __asan_##type##size##_as##as##_noabort( \
685+
uptr addr, const char __SYCL_CONSTANT__ *file, uint32_t line, \
686+
const char __SYCL_CONSTANT__ *func) { \
687687
if (addr & AlignMask(size)) { \
688688
__asan_report_misalign_error(addr, as, size, is_write, addr, file, line, \
689689
func, true); \
@@ -694,6 +694,13 @@ constexpr size_t AlignMask(size_t n) { return n - 1; }
694694
} \
695695
}
696696

697+
#define ASAN_REPORT_ERROR(type, is_write, size) \
698+
ASAN_REPORT_ERROR_BASE(type, is_write, size, 0) \
699+
ASAN_REPORT_ERROR_BASE(type, is_write, size, 1) \
700+
ASAN_REPORT_ERROR_BASE(type, is_write, size, 2) \
701+
ASAN_REPORT_ERROR_BASE(type, is_write, size, 3) \
702+
ASAN_REPORT_ERROR_BASE(type, is_write, size, 4)
703+
697704
ASAN_REPORT_ERROR(load, false, 1)
698705
ASAN_REPORT_ERROR(load, false, 2)
699706
ASAN_REPORT_ERROR(load, false, 4)
@@ -705,24 +712,31 @@ ASAN_REPORT_ERROR(store, true, 4)
705712
ASAN_REPORT_ERROR(store, true, 8)
706713
ASAN_REPORT_ERROR(store, true, 16)
707714

708-
#define ASAN_REPORT_ERROR_N(type, is_write) \
709-
DEVICE_EXTERN_C_NOINLINE void __asan_##type##N( \
710-
uptr addr, size_t size, uint32_t as, const char __SYCL_CONSTANT__ *file, \
715+
#define ASAN_REPORT_ERROR_N_BASE(type, is_write, as) \
716+
DEVICE_EXTERN_C_NOINLINE void __asan_##type##N_as##as( \
717+
uptr addr, size_t size, const char __SYCL_CONSTANT__ *file, \
711718
uint32_t line, const char __SYCL_CONSTANT__ *func) { \
712719
if (auto poisoned_addr = __asan_region_is_poisoned(addr, as, size)) { \
713720
__asan_report_access_error(addr, as, size, is_write, poisoned_addr, \
714721
file, line, func); \
715722
} \
716723
} \
717-
DEVICE_EXTERN_C_NOINLINE void __asan_##type##N_noabort( \
718-
uptr addr, size_t size, uint32_t as, const char __SYCL_CONSTANT__ *file, \
724+
DEVICE_EXTERN_C_NOINLINE void __asan_##type##N_as##as##_noabort( \
725+
uptr addr, size_t size, const char __SYCL_CONSTANT__ *file, \
719726
uint32_t line, const char __SYCL_CONSTANT__ *func) { \
720727
if (auto poisoned_addr = __asan_region_is_poisoned(addr, as, size)) { \
721728
__asan_report_access_error(addr, as, size, is_write, poisoned_addr, \
722729
file, line, func, true); \
723730
} \
724731
}
725732

733+
#define ASAN_REPORT_ERROR_N(type, is_write) \
734+
ASAN_REPORT_ERROR_N_BASE(type, is_write, 0) \
735+
ASAN_REPORT_ERROR_N_BASE(type, is_write, 1) \
736+
ASAN_REPORT_ERROR_N_BASE(type, is_write, 2) \
737+
ASAN_REPORT_ERROR_N_BASE(type, is_write, 3) \
738+
ASAN_REPORT_ERROR_N_BASE(type, is_write, 4)
739+
726740
ASAN_REPORT_ERROR_N(load, false)
727741
ASAN_REPORT_ERROR_N(store, true)
728742

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ const char kAsanMemToShadow[] = "__asan_mem_to_shadow";
183183

184184
// Accesses sizes are powers of two: 1, 2, 4, 8, 16.
185185
static const size_t kNumberOfAccessSizes = 5;
186+
static const size_t kNumberOfAddressSpace = 5;
186187

187188
static const uint64_t kAllocaRzSize = 32;
188189

@@ -897,10 +898,13 @@ struct AddressSanitizer {
897898
// These arrays is indexed by AccessIsWrite, Experiment and log2(AccessSize).
898899
FunctionCallee AsanErrorCallback[2][2][kNumberOfAccessSizes];
899900
FunctionCallee AsanMemoryAccessCallback[2][2][kNumberOfAccessSizes];
901+
FunctionCallee AsanMemoryAccessCallbackAS[2][2][kNumberOfAccessSizes]
902+
[kNumberOfAddressSpace];
900903

901904
// These arrays is indexed by AccessIsWrite and Experiment.
902905
FunctionCallee AsanErrorCallbackSized[2][2];
903906
FunctionCallee AsanMemoryAccessCallbackSized[2][2];
907+
FunctionCallee AsanMemoryAccessCallbackSizedAS[2][2][kNumberOfAddressSpace];
904908

905909
FunctionCallee AsanMemmove, AsanMemcpy, AsanMemset;
906910
Value *LocalDynamicShadow = nullptr;
@@ -1403,7 +1407,8 @@ static void ExtendSpirKernelArgs(Module &M, FunctionAnalysisManager &FAM) {
14031407
auto *CurF = CI->getFunction();
14041408
Args.push_back(CurF->getArg(CurF->arg_size() - 1));
14051409

1406-
CallInst *NewCI = CallInst::Create(NewF, Args, CI->getName(), CI);
1410+
CallInst *NewCI =
1411+
CallInst::Create(NewF, Args, CI->getName(), CI->getIterator());
14071412
NewCI->setCallingConv(CI->getCallingConv());
14081413
NewCI->setAttributes(CI->getAttributes());
14091414
NewCI->setDebugLoc(CI->getDebugLoc());
@@ -1547,7 +1552,7 @@ static bool isJointMatrixAccess(Value *V) {
15471552
for (Value *Op : CI->args()) {
15481553
if (auto *AI = dyn_cast<AllocaInst>(Op->stripInBoundsOffsets()))
15491554
if (auto *TargetTy = getTargetExtType(AI->getAllocatedType()))
1550-
return TargetTy->getName().startswith("spirv.") &&
1555+
return TargetTy->getName().starts_with("spirv.") &&
15511556
TargetTy->getName().contains("Matrix");
15521557
}
15531558
}
@@ -1623,11 +1628,6 @@ void AddressSanitizer::AppendDebugInfoToArgs(Instruction *InsertBefore,
16231628
PointerType *ConstASPtrTy =
16241629
Type::getInt8Ty(C)->getPointerTo(kSpirOffloadConstantAS);
16251630

1626-
// Address Space
1627-
Type *PtrTy = cast<PointerType>(Addr->getType()->getScalarType());
1628-
Args.push_back(
1629-
ConstantInt::get(Type::getInt32Ty(C), PtrTy->getPointerAddressSpace()));
1630-
16311631
// File & Line
16321632
if (Loc) {
16331633
llvm::SmallString<128> Source = Loc->getDirectory();
@@ -2322,10 +2322,13 @@ void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
23222322
if (Exp == 0) {
23232323
if (TargetTriple.isSPIROrSPIRV()) {
23242324
SmallVector<Value *, 5> Args;
2325+
auto AS = cast<PointerType>(Addr->getType()->getScalarType())
2326+
->getPointerAddressSpace();
23252327
Args.push_back(AddrLong);
23262328
AppendDebugInfoToArgs(InsertBefore, Addr, Args);
23272329
RTCI.createRuntimeCall(
2328-
IRB, AsanMemoryAccessCallback[IsWrite][0][AccessSizeIndex], Args);
2330+
IRB, AsanMemoryAccessCallbackAS[IsWrite][0][AccessSizeIndex][AS],
2331+
Args);
23292332
} else {
23302333
RTCI.createRuntimeCall(
23312334
IRB, AsanMemoryAccessCallback[IsWrite][0][AccessSizeIndex], AddrLong);
@@ -2404,11 +2407,13 @@ void AddressSanitizer::instrumentUnusualSizeOrAlignment(
24042407
if (Exp == 0) {
24052408
if (TargetTriple.isSPIROrSPIRV()) {
24062409
SmallVector<Value *, 6> Args;
2410+
auto AS = cast<PointerType>(Addr->getType()->getScalarType())
2411+
->getPointerAddressSpace();
24072412
Args.push_back(AddrLong);
24082413
Args.push_back(Size);
24092414
AppendDebugInfoToArgs(InsertBefore, Addr, Args);
2410-
RTCI.createRuntimeCall(IRB, AsanMemoryAccessCallbackSized[IsWrite][0],
2411-
Args);
2415+
RTCI.createRuntimeCall(
2416+
IRB, AsanMemoryAccessCallbackSizedAS[IsWrite][0][AS], Args);
24122417
} else {
24132418
RTCI.createRuntimeCall(IRB, AsanMemoryAccessCallbackSized[IsWrite][0],
24142419
{AddrLong, Size});
@@ -3286,7 +3291,6 @@ void AddressSanitizer::initializeCallbacks(const TargetLibraryInfo *TLI) {
32863291

32873292
// __asan_loadX/__asan_storeX(
32883293
// ...
3289-
// int32_t as, // Address Space
32903294
// char* file,
32913295
// unsigned int line,
32923296
// char* func
@@ -3295,15 +3299,39 @@ void AddressSanitizer::initializeCallbacks(const TargetLibraryInfo *TLI) {
32953299
auto *Int8PtrTy =
32963300
Type::getInt8Ty(*C)->getPointerTo(kSpirOffloadConstantAS);
32973301

3298-
Args1.push_back(Type::getInt32Ty(*C)); // address_space
32993302
Args1.push_back(Int8PtrTy); // file
33003303
Args1.push_back(Type::getInt32Ty(*C)); // line
33013304
Args1.push_back(Int8PtrTy); // func
33023305

3303-
Args2.push_back(Type::getInt32Ty(*C)); // address_space
33043306
Args2.push_back(Int8PtrTy); // file
33053307
Args2.push_back(Type::getInt32Ty(*C)); // line
33063308
Args2.push_back(Int8PtrTy); // func
3309+
3310+
for (size_t AddressSpaceIndex = 0;
3311+
AddressSpaceIndex < kNumberOfAddressSpace; AddressSpaceIndex++) {
3312+
AsanMemoryAccessCallbackSizedAS
3313+
[AccessIsWrite][Exp][AddressSpaceIndex] = M.getOrInsertFunction(
3314+
ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" +
3315+
"_as" + itostr(AddressSpaceIndex) + EndingStr,
3316+
FunctionType::get(IRB.getVoidTy(), Args2, false), AL2);
3317+
3318+
for (size_t AccessSizeIndex = 0;
3319+
AccessSizeIndex < kNumberOfAccessSizes; AccessSizeIndex++) {
3320+
const std::string Suffix = TypeStr +
3321+
itostr(1ULL << AccessSizeIndex) + "_as" +
3322+
itostr(AddressSpaceIndex);
3323+
AsanMemoryAccessCallbackAS[AccessIsWrite][Exp][AccessSizeIndex]
3324+
[AddressSpaceIndex] =
3325+
M.getOrInsertFunction(
3326+
ClMemoryAccessCallbackPrefix +
3327+
ExpStr + Suffix + EndingStr,
3328+
FunctionType::get(IRB.getVoidTy(),
3329+
Args1, false),
3330+
AL1);
3331+
}
3332+
}
3333+
3334+
continue;
33073335
}
33083336
AsanErrorCallbackSized[AccessIsWrite][Exp] = M.getOrInsertFunction(
33093337
kAsanReportErrorTemplate + ExpStr + TypeStr + "_n" + EndingStr,

llvm/test/Instrumentation/AddressSanitizer/SPIRV/sycl_esimd.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ define spir_kernel void @sycl_kernel(ptr addrspace(1) %p) #0 {
1212
entry:
1313
%0 = load i32, ptr addrspace(1) %p, align 4
1414
; CHECK-NOT: store ptr addrspace(1) %__asan_launch, ptr addrspace(3) @__AsanLaunchInfo, align 8
15-
; CHECK-NOT: call void @__asan_load4
15+
; CHECK-NOT: call void @__asan_load4_as1
1616
ret void
1717
}
1818

0 commit comments

Comments
 (0)