Skip to content

Commit 5ddeb13

Browse files
authored
[DevASAN] Fix missing check for null shadow pointer (#19574)
1 parent d524f4b commit 5ddeb13

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

libdevice/sanitizer/asan_rtl.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ inline uptr MemToShadow_PVC(uptr addr, uint32_t as,
203203
return shadow_ptr;
204204
} else if (as == ADDRESS_SPACE_LOCAL) { // local
205205
const auto shadow_offset = launch_info->LocalShadowOffset;
206-
if (shadow_offset == 0) {
206+
const auto wid = WorkGroupLinearId();
207+
if (shadow_offset == 0 || wid >= ASAN_MAX_WG_LOCAL) {
207208
return 0;
208209
}
209210

210211
// The size of SLM is 128KB on PVC
211212
constexpr unsigned SLM_SIZE = 128 * 1024;
212-
const auto wid = WorkGroupLinearId();
213213

214214
uptr shadow_ptr = shadow_offset + ((wid * SLM_SIZE) >> ASAN_SHADOW_SCALE) +
215215
((addr & (SLM_SIZE - 1)) >> ASAN_SHADOW_SCALE);
@@ -494,6 +494,9 @@ void ReportMisalignError(uptr addr, uint32_t as, bool is_recover,
494494
const DebugInfo *debug) {
495495

496496
auto *shadow = (__SYCL_GLOBAL__ s8 *)MemToShadow(addr, as, debug);
497+
if (!shadow)
498+
return;
499+
497500
while (*shadow >= 0) {
498501
++shadow;
499502
}
@@ -710,6 +713,9 @@ __asan_set_shadow_static_local(uptr ptr, size_t size,
710713
// Set red zone
711714
{
712715
auto shadow_address = MemToShadow(ptr + aligned_size, ADDRESS_SPACE_LOCAL);
716+
if (!shadow_address)
717+
return;
718+
713719
auto count = (size_with_redzone - aligned_size) >> ASAN_SHADOW_SCALE;
714720

715721
ASAN_DEBUG(__spirv_ocl_printf(__mem_set_shadow_local, shadow_address,
@@ -726,6 +732,9 @@ __asan_set_shadow_static_local(uptr ptr, size_t size,
726732
auto user_end = ptr + size;
727733
auto *shadow_end =
728734
(__SYCL_GLOBAL__ s8 *)MemToShadow(user_end, ADDRESS_SPACE_LOCAL);
735+
if (!shadow_end)
736+
return;
737+
729738
auto value = user_end - RoundDownTo(user_end, ASAN_SHADOW_GRANULARITY) + 1;
730739
*shadow_end = value;
731740

@@ -748,7 +757,11 @@ __asan_unpoison_shadow_static_local(uptr ptr, size_t size,
748757
ASAN_DEBUG(__spirv_ocl_printf(__mem_unpoison_shadow_static_local_begin));
749758

750759
auto shadow_begin = MemToShadow(ptr + size, ADDRESS_SPACE_LOCAL);
760+
if (!shadow_begin)
761+
return;
751762
auto shadow_end = MemToShadow(ptr + size_with_redzone, ADDRESS_SPACE_LOCAL);
763+
if (!shadow_end)
764+
return;
752765

753766
ASAN_DEBUG(
754767
__spirv_ocl_printf(__mem_set_shadow_local, shadow_begin, shadow_end, 0));

libdevice/sanitizer/tsan_rtl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ static __SYCL_CONSTANT__ const char __tsan_print_cleanup_local[] =
479479

480480
DEVICE_EXTERN_C_NOINLINE void __tsan_cleanup_static_local(uptr addr,
481481
size_t size) {
482+
if (GetCurrentSid() == -1)
483+
return;
484+
482485
// Update shadow memory of local memory only on first work-item
483486
if (__spirv_LocalInvocationId_x() + __spirv_LocalInvocationId_y() +
484487
__spirv_LocalInvocationId_z() ==
@@ -505,7 +508,7 @@ static __SYCL_CONSTANT__ const char __tsan_print_report_arg_count_incorrect[] =
505508

506509
DEVICE_EXTERN_C_NOINLINE void __tsan_cleanup_dynamic_local(uptr ptr,
507510
uint32_t num_args) {
508-
if (!TsanLaunchInfo->LocalShadowOffset)
511+
if (!TsanLaunchInfo->LocalShadowOffset || GetCurrentSid() == -1)
509512
return;
510513

511514
if (num_args != TsanLaunchInfo->NumLocalArgs) {

0 commit comments

Comments
 (0)