Skip to content

Commit be932df

Browse files
AllanZynemartygrant
authored andcommitted
[DeviceASAN] Fix ASAN with kernel assert (#16256)
UR: oneapi-src/unified-runtime#2415 --------- Co-authored-by: Martin Morrison-Grant <[email protected]>
1 parent 1573d4f commit be932df

File tree

7 files changed

+85
-37
lines changed

7 files changed

+85
-37
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

+17-13
Original file line numberDiff line numberDiff line change
@@ -1333,13 +1333,27 @@ static void ExtendSpirKernelArgs(Module &M, FunctionAnalysisManager &FAM,
13331333

13341334
if (!HasESIMD)
13351335
for (Function &F : M) {
1336-
if (F.getCallingConv() != CallingConv::SPIR_KERNEL)
1337-
continue;
1338-
13391336
if (!F.hasFnAttribute(Attribute::SanitizeAddress) ||
13401337
F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))
13411338
continue;
13421339

1340+
if (F.getName().contains("__sycl_service_kernel__")) {
1341+
F.addFnAttr(Attribute::DisableSanitizerInstrumentation);
1342+
continue;
1343+
}
1344+
1345+
// Skip referenced-indirectly function as we insert access to shared
1346+
// local memory (SLM) __AsanLaunchInfo and access to SLM in
1347+
// referenced-indirectly function isn't supported yet in
1348+
// intel-graphics-compiler.
1349+
if (F.hasFnAttribute("referenced-indirectly")) {
1350+
F.addFnAttr(Attribute::DisableSanitizerInstrumentation);
1351+
continue;
1352+
}
1353+
1354+
if (F.getCallingConv() != CallingConv::SPIR_KERNEL)
1355+
continue;
1356+
13431357
SpirFixupKernels.emplace_back(&F);
13441358

13451359
auto KernelName = F.getName();
@@ -3683,16 +3697,6 @@ bool AddressSanitizer::instrumentFunction(Function &F,
36833697
if (F.isPresplitCoroutine())
36843698
return false;
36853699

3686-
if (TargetTriple.isSPIROrSPIRV()) {
3687-
if (F.getName().contains("__sycl_service_kernel__"))
3688-
return false;
3689-
// Skip referenced-indirectly function as we insert access to shared local
3690-
// memory (SLM) __AsanLaunchInfo and access to SLM in referenced-indirectly
3691-
// function isn't supported yet in intel-graphics-compiler.
3692-
if (F.hasFnAttribute("referenced-indirectly"))
3693-
return false;
3694-
}
3695-
36963700
bool FunctionModified = false;
36973701

36983702
// Do not apply any instrumentation for naked functions.

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: opt < %s -passes=asan -asan-instrumentation-with-call-threshold=0 | FileCheck %s
1+
; RUN: opt < %s -passes=asan -asan-instrumentation-with-call-threshold=0 -S | FileCheck %s
22

33
; Check referenced-indirectly function isn't instrumented.
44

@@ -9,6 +9,7 @@ target triple = "spir64-unknown-unknown"
99
@_ZTV8Derived1 = linkonce_odr addrspace(1) constant %structtype { [3 x ptr addrspace(4)] [ptr addrspace(4) null, ptr addrspace(4) null, ptr addrspace(4) addrspacecast (ptr @_ZN8Derived17displayEv to ptr addrspace(4))] }, align 8, !spirv.Decorations !0
1010

1111
define linkonce_odr spir_func i32 @_ZN8Derived17displayEv(ptr addrspace(4) align 8 %this) sanitize_address "referenced-indirectly" {
12+
; CHECK: @_ZN8Derived17displayEv{{.*}}#1
1213
entry:
1314
; CHECK-NOT: call void @__asan_load
1415

@@ -17,6 +18,8 @@ entry:
1718
ret i32 %1
1819
}
1920

21+
; CHECK: #1 {{.*}} disable_sanitizer_instrumentation
22+
2023
!0 = !{!1, !2, !3}
2124
!1 = !{i32 22}
2225
!2 = !{i32 41, !"_ZTV8Derived1", i32 2}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: opt < %s -passes=asan -asan-instrumentation-with-call-threshold=0 -S | FileCheck %s
2+
3+
; Check "sycl_service_kernel" function isn't instrumented.
4+
5+
target triple = "spir64-unknown-unknown"
6+
7+
%structtype = type { [3 x ptr addrspace(4)] }
8+
%class.Base = type <{ ptr addrspace(4), i32, [4 x i8] }>
9+
10+
define linkonce_odr spir_func i32 @_ZTSN4sycl3_V16detail23__sycl_service_kernel__16AssertInfoCopierE(ptr addrspace(4) align 8 %this) sanitize_address "referenced-indirectly" {
11+
; CHECK: @_ZTSN4sycl3_V16detail23__sycl_service_kernel__16AssertInfoCopierE{{.*}}#1
12+
entry:
13+
; CHECK-NOT: call void @__asan_load
14+
%base_data = getelementptr inbounds %class.Base, ptr addrspace(4) %this, i64 0, i32 1
15+
%1 = load i32, ptr addrspace(4) %base_data, align 8
16+
ret i32 %1
17+
}
18+
19+
; CHECK: #1 {{.*}} disable_sanitizer_instrumentation
+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# commit 39df0317814c164f5242eda8d6f08550f6268492
2-
# Merge: 68d93efd be27d8f0
3-
# Author: Kenneth Benzie (Benie) <k.benzie@codeplay.com>
4-
# Date: Mon Dec 16 13:53:13 2024 +0000
5-
# Merge pull request #2467 from nrspruit/fix_external_import_function_call
6-
# [L0] Fix external semaphore import function calls to match the header
7-
set(UNIFIED_RUNTIME_TAG 39df0317814c164f5242eda8d6f08550f6268492)
1+
# commit d18d52393aadf0083a32912096baaac558378a99
2+
# Merge: c45de9a5f7bf 05f94a8ab2a9
3+
# Author: Martin Grant <martin.morrisongrant@codeplay.com>
4+
# Date: Wed Dec 18 15:01:30 2024 +0000
5+
# Merge pull request #2415 from AllanZyne/review/yang/fix_metadata_assert
6+
# [DeviceASAN] Fix ASAN with kernel assert
7+
set(UNIFIED_RUNTIME_TAG d18d52393aadf0083a32912096baaac558378a99)
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fun:*MyKernel*
1+
fun:*NoSanitized*

sycl/test-e2e/AddressSanitizer/common/kernel-filter.cpp

+34-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,51 @@
1-
// REQUIRES: linux
2-
// RUN: %{build} %device_asan_flags -O2 -fsanitize-ignorelist=%p/ignorelist.txt -o %t
3-
// RUN: %{run} %t 2>&1 | FileCheck %s
4-
// RUN: %{build} %device_asan_flags %if cpu %{ -fsycl-targets=spir64_x86_64 %} %if gpu %{ -fsycl-targets=spir64_gen -Xsycl-target-backend=spir64_gen %gpu_aot_target_opts %} -O2 -fsanitize-ignorelist=%p/ignorelist.txt -o %t2
1+
// REQUIRES: linux, cpu || (gpu && level_zero)
2+
// RUN: %{build} %device_asan_flags -O2 -fsanitize-ignorelist=%p/ignorelist.txt -o %t1
3+
// RUN: %{run} %t1 2>&1 | FileCheck %s
4+
// RUN: %{build} %device_asan_aot_flags -O2 -fsanitize-ignorelist=%p/ignorelist.txt -o %t2
55
// RUN: %{run} %t2 2>&1 | FileCheck %s
66

77
#include <sycl/detail/core.hpp>
88
#include <sycl/usm.hpp>
99

1010
int main() {
11+
constexpr std::size_t N = 8;
12+
constexpr std::size_t group_size = 4;
13+
1114
sycl::queue Q;
12-
constexpr std::size_t N = 16;
15+
1316
auto *array = sycl::malloc_device<int>(N, Q);
1417

18+
std::vector<int> v(N);
19+
sycl::buffer<int, 1> buf(v.data(), v.size());
20+
1521
Q.submit([&](sycl::handler &h) {
16-
h.parallel_for<class MyKernel>(
17-
sycl::nd_range<1>(N + 1, 1),
18-
[=](sycl::nd_item<1> item) { ++array[item.get_global_id(0)]; });
22+
auto buf_acc = buf.get_access<sycl::access::mode::read_write>(h);
23+
auto loc_acc = sycl::local_accessor<int>(group_size, h);
24+
h.parallel_for<class NoSanitized>(
25+
sycl::nd_range<1>(N, group_size), [=](sycl::nd_item<1> item) {
26+
auto gid = item.get_global_id(0);
27+
auto lid = item.get_local_id(0);
28+
array[gid] = buf_acc[gid] + loc_acc[lid];
29+
});
30+
});
31+
Q.wait();
32+
// CHECK-NOT: ERROR: DeviceSanitizer: out-of-bounds-access
33+
34+
Q.submit([&](sycl::handler &h) {
35+
auto buf_acc = buf.get_access<sycl::access::mode::read_write>(h);
36+
auto loc_acc = sycl::local_accessor<int>(group_size, h);
37+
h.parallel_for<class Sanitized>(sycl::nd_range<1>(N, group_size),
38+
[=](sycl::nd_item<1> item) {
39+
auto gid = item.get_global_id(0);
40+
auto lid = item.get_local_id(0);
41+
array[gid] = buf_acc[gid] + loc_acc[lid];
42+
});
1943
});
2044
Q.wait();
2145

2246
sycl::free(array, Q);
2347
std::cout << "PASS" << std::endl;
48+
// CHECK: PASS
49+
2450
return 0;
2551
}
26-
27-
// CHECK: PASS
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
# This test assumes it can only run in CPU/PVC/DG2 devices, which support usm_device_allocations aspect
2-
31
config.substitutions.append(
42
("%device_asan_flags", "-Xarch_device -fsanitize=address")
53
)
6-
4+
config.substitutions.append(
5+
("%device_asan_aot_flags", "-Xarch_device -fsanitize=address %if cpu %{ -fsycl-targets=spir64_x86_64 %} %if gpu %{ -fsycl-targets=spir64_gen -Xsycl-target-backend=spir64_gen %gpu_aot_target_opts %}")
6+
)
77
config.substitutions.append(
88
("%force_device_asan_rt", "env UR_ENABLE_LAYERS=UR_LAYER_ASAN")
99
)
1010

11-
config.unsupported_features += ['cuda', 'hip']
12-
1311
# https://github.com/intel/llvm/issues/15953
1412
config.unsupported_features += ['gpu-intel-gen12']

0 commit comments

Comments
 (0)