Skip to content

Commit f8bbfcc

Browse files
authored
[DevSAN] Make device sanitizer kernel metadata has a unique id (#18819)
We decorated kernel metadata global as a device global, if we don't give a unique id to it, device sanitizer will not work in shared libraries.
1 parent 72ea00a commit f8bbfcc

File tree

8 files changed

+40
-8
lines changed

8 files changed

+40
-8
lines changed

llvm/include/llvm/Transforms/Instrumentation/SPIRVSanitizerCommonUtils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_SPIRVSANITIZERCOMMONUTILS_H
1313
#define LLVM_TRANSFORMS_INSTRUMENTATION_SPIRVSANITIZERCOMMONUTILS_H
1414

15+
#include "llvm/ADT/SmallString.h"
1516
#include "llvm/IR/Constants.h"
1617
#include "llvm/IR/DerivedTypes.h"
1718
#include "llvm/IR/Type.h"
@@ -36,6 +37,11 @@ bool isJointMatrixAccess(Value *V);
3637
// it has been used.
3738
void getFunctionsOfUser(User *User, SmallVectorImpl<Function *> &Functions);
3839

40+
// Compute MD5 hash for kernel metadata global as unique id.
41+
SmallString<128>
42+
computeKernelMetadataUniqueId(StringRef Prefix,
43+
SmallVectorImpl<uint8_t> &KernelNamesBytes);
44+
3945
} // namespace llvm
4046

4147
#endif // LLVM_TRANSFORMS_INSTRUMENTATION_SPIRVSANITIZERCOMMONUTILS_H

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,7 @@ static void ExtendSpirKernelArgs(Module &M, FunctionAnalysisManager &FAM,
14021402
bool HasESIMD) {
14031403
SmallVector<Function *> SpirFixupKernels;
14041404
SmallVector<Constant *, 8> SpirKernelsMetadata;
1405+
SmallVector<uint8_t, 256> KernelNamesBytes;
14051406

14061407
const auto &DL = M.getDataLayout();
14071408
Type *IntptrTy = DL.getIntPtrType(M.getContext());
@@ -1438,6 +1439,7 @@ static void ExtendSpirKernelArgs(Module &M, FunctionAnalysisManager &FAM,
14381439
SpirFixupKernels.emplace_back(&F);
14391440

14401441
auto KernelName = F.getName();
1442+
KernelNamesBytes.append(KernelName.begin(), KernelName.end());
14411443
auto *KernelNameGV = GetOrCreateGlobalString(
14421444
M, "__asan_kernel", KernelName, kSpirOffloadConstantAS);
14431445
SpirKernelsMetadata.emplace_back(ConstantStruct::get(
@@ -1459,8 +1461,9 @@ static void ExtendSpirKernelArgs(Module &M, FunctionAnalysisManager &FAM,
14591461
"sycl-device-global-size", std::to_string(DL.getTypeAllocSize(ArrayTy)));
14601462
AsanSpirKernelMetadata->addAttribute("sycl-device-image-scope");
14611463
AsanSpirKernelMetadata->addAttribute("sycl-host-access", "0"); // read only
1462-
AsanSpirKernelMetadata->addAttribute("sycl-unique-id",
1463-
"_Z20__AsanKernelMetadata");
1464+
AsanSpirKernelMetadata->addAttribute(
1465+
"sycl-unique-id",
1466+
computeKernelMetadataUniqueId("__AsanKernelMetadata", KernelNamesBytes));
14641467
AsanSpirKernelMetadata->setDSOLocal(true);
14651468

14661469
// Handle SpirFixupKernels

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@ void MemorySanitizerOnSpirv::instrumentPrivateArguments(
11711171
// kernel
11721172
void MemorySanitizerOnSpirv::instrumentKernelsMetadata() {
11731173
SmallVector<Constant *, 8> SpirKernelsMetadata;
1174+
SmallVector<uint8_t, 256> KernelNamesBytes;
11741175

11751176
// SpirKernelsMetadata only saves fixed kernels, and is described by
11761177
// following structure:
@@ -1189,6 +1190,7 @@ void MemorySanitizerOnSpirv::instrumentKernelsMetadata() {
11891190
continue;
11901191

11911192
auto KernelName = F.getName();
1193+
KernelNamesBytes.append(KernelName.begin(), KernelName.end());
11921194
auto *KernelNameGV = getOrCreateGlobalString("__msan_kernel", KernelName,
11931195
kSpirOffloadConstantAS);
11941196
SpirKernelsMetadata.emplace_back(ConstantStruct::get(
@@ -1213,8 +1215,9 @@ void MemorySanitizerOnSpirv::instrumentKernelsMetadata() {
12131215
MsanSpirKernelMetadata->addAttribute("sycl-device-image-scope");
12141216
MsanSpirKernelMetadata->addAttribute("sycl-host-access",
12151217
"0"); // read only
1216-
MsanSpirKernelMetadata->addAttribute("sycl-unique-id",
1217-
"_Z20__MsanKernelMetadata");
1218+
MsanSpirKernelMetadata->addAttribute(
1219+
"sycl-unique-id",
1220+
computeKernelMetadataUniqueId("__MsanKernelMetadata", KernelNamesBytes));
12181221
MsanSpirKernelMetadata->setDSOLocal(true);
12191222
}
12201223

llvm/lib/Transforms/Instrumentation/SPIRVSanitizerCommonUtils.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "llvm/Transforms/Instrumentation/SPIRVSanitizerCommonUtils.h"
1414
#include "llvm/IR/Instructions.h"
15+
#include "llvm/Support/MD5.h"
1516

1617
using namespace llvm;
1718

@@ -68,4 +69,16 @@ void getFunctionsOfUser(User *User, SmallVectorImpl<Function *> &Functions) {
6869
}
6970
}
7071

72+
SmallString<128>
73+
computeKernelMetadataUniqueId(StringRef Prefix,
74+
SmallVectorImpl<uint8_t> &KernelNamesBytes) {
75+
MD5 Hash;
76+
SmallString<32> UniqueIdSuffix;
77+
SmallString<128> UniqueId(Prefix);
78+
auto R = Hash.hash(KernelNamesBytes);
79+
Hash.stringifyResult(R, UniqueIdSuffix);
80+
UniqueId.append(UniqueIdSuffix);
81+
return UniqueId;
82+
}
83+
7184
} // namespace llvm

llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "llvm/IR/LLVMContext.h"
3838
#include "llvm/IR/Metadata.h"
3939
#include "llvm/IR/Module.h"
40+
#include "llvm/IR/ModuleSummaryIndex.h"
4041
#include "llvm/IR/Type.h"
4142
#include "llvm/ProfileData/InstrProf.h"
4243
#include "llvm/Support/CommandLine.h"
@@ -674,6 +675,7 @@ void ThreadSanitizerOnSpirv::initializeKernelCallerMap(Function *F) {
674675

675676
void ThreadSanitizerOnSpirv::instrumentKernelsMetadata() {
676677
SmallVector<Constant *, 8> SpirKernelsMetadata;
678+
SmallVector<uint8_t, 256> KernelNamesBytes;
677679

678680
// SpirKernelsMetadata only saves fixed kernels, and is described by
679681
// following structure:
@@ -687,6 +689,7 @@ void ThreadSanitizerOnSpirv::instrumentKernelsMetadata() {
687689

688690
if (isSupportedSPIRKernel(F)) {
689691
auto KernelName = F.getName();
692+
KernelNamesBytes.append(KernelName.begin(), KernelName.end());
690693
auto *KernelNameGV = GetOrCreateGlobalString("__tsan_kernel", KernelName,
691694
kSpirOffloadConstantAS);
692695
SpirKernelsMetadata.emplace_back(ConstantStruct::get(
@@ -709,8 +712,9 @@ void ThreadSanitizerOnSpirv::instrumentKernelsMetadata() {
709712
"sycl-device-global-size", std::to_string(DL.getTypeAllocSize(ArrayTy)));
710713
TsanSpirKernelMetadata->addAttribute("sycl-device-image-scope");
711714
TsanSpirKernelMetadata->addAttribute("sycl-host-access", "0"); // read only
712-
TsanSpirKernelMetadata->addAttribute("sycl-unique-id",
713-
"_Z20__TsanKernelMetadata");
715+
TsanSpirKernelMetadata->addAttribute(
716+
"sycl-unique-id",
717+
computeKernelMetadataUniqueId("__TsanKernelMetadata", KernelNamesBytes));
714718
TsanSpirKernelMetadata->setDSOLocal(true);
715719
}
716720

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:
44
target triple = "spir64-unknown-unknown"
55

66
; CHECK: @__AsanKernelMetadata = appending dso_local local_unnamed_addr addrspace(1) global
7+
; CHECK-SAME: [[ATTR0:#[0-9]+]]
78
; CHECK: @__AsanLaunchInfo = external addrspace(3) global ptr addrspace(1)
89

910
define spir_kernel void @sycl_kernel1() #0 {
@@ -25,4 +26,4 @@ entry:
2526
attributes #0 = { sanitize_address }
2627
;; sycl-device-global-size = 16 * 2
2728
;; sycl-host-access = 0 read-only
28-
; CHECK: attributes #{{.*}} = { "sycl-device-global-size"="32" "sycl-device-image-scope" "sycl-host-access"="0" "sycl-unique-id"="_Z20__AsanKernelMetadata" }
29+
; CHECK: attributes [[ATTR0]] = { "sycl-device-global-size"="32" "sycl-device-image-scope" "sycl-host-access"="0" "sycl-unique-id"="__AsanKernelMetadata833c47834a0b74946e370c23c39607cc" }

llvm/test/Instrumentation/MemorySanitizer/SPIRV/instrument_global_address_space.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ entry:
3131
}
3232

3333
; CHECK: attributes [[ATTR0]]
34-
; CHECK-SAME: "sycl-device-global-size"="32" "sycl-device-image-scope" "sycl-host-access"="0" "sycl-unique-id"="_Z20__MsanKernelMetadata"
34+
; CHECK-SAME: "sycl-device-global-size"="32" "sycl-device-image-scope" "sycl-host-access"="0" "sycl-unique-id"="__MsanKernelMetadata3ff767e9a7a43f1f3968062dbb4ee3b4"

llvm/test/Instrumentation/ThreadSanitizer/SPIRV/kernel_metadata.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ target triple = "spir64-unknown-unknown"
44

55
; CHECK-LABEL: @__TsanKernelMetadata = appending dso_local local_unnamed_addr addrspace(1) global
66
; CHECK-SAME: i64 ptrtoint (ptr addrspace(2) @__tsan_kernel to i64
7+
; CHECK-SAME: [[ATTR0:#[0-9]+]]
78

89
; Function Attrs: sanitize_thread
910
define spir_kernel void @test() #0 {
1011
entry:
1112
ret void
1213
}
1314

15+
; CHECK: attributes [[ATTR0]] = {{.*}} "sycl-unique-id"="__TsanKernelMetadata098f6bcd4621d373cade4e832627b4f6"
1416
attributes #0 = { sanitize_thread }

0 commit comments

Comments
 (0)