11
11
#include " spirv_vars.h"
12
12
13
13
#include " include/asan_libdevice.hpp"
14
- #include " include/sanitizer_device_utils.hpp"
15
- #include < cstddef>
16
- #include < cstdint>
14
+ #include " include/sanitizer_utils.hpp"
17
15
18
16
using uptr = uintptr_t ;
19
17
using s8 = char ;
@@ -23,11 +21,10 @@ using u16 = unsigned short;
23
21
24
22
DeviceGlobal<uptr> __AsanShadowMemoryGlobalStart;
25
23
DeviceGlobal<uptr> __AsanShadowMemoryGlobalEnd;
26
- DeviceGlobal<uptr> __AsanShadowMemoryLocalStart;
27
- DeviceGlobal<uptr> __AsanShadowMemoryLocalEnd;
28
24
DeviceGlobal<DeviceType> __DeviceType;
29
25
DeviceGlobal<uint64_t > __AsanDebug;
30
- DeviceGlobal<DeviceSanitizerReport> __DeviceSanitizerReportMem;
26
+ // Save the pointer to LaunchInfo
27
+ __SYCL_GLOBAL__ uptr *__SYCL_LOCAL__ __AsanLaunchInfo;
31
28
32
29
#if defined(__SPIR__) || defined(__SPIRV__)
33
30
@@ -134,6 +131,16 @@ inline uptr MemToShadow_DG2(uptr addr, uint32_t as) {
134
131
return shadow_ptr;
135
132
}
136
133
134
+ static __SYCL_CONSTANT__ const char __mem_launch_info[] =
135
+ " [kernel] launch_info: %p (local_shadow=%p~%p, numLocalArgs=%d, "
136
+ " localArgs=%p)\n " ;
137
+
138
+ static __SYCL_CONSTANT__ const char __generic_to[] =
139
+ " [kernel] %p(4) - %p(%d)\n " ;
140
+
141
+ static __SYCL_CONSTANT__ const char __generic_to_fail[] =
142
+ " [kernel] %p(4) - unknown address space\n " ;
143
+
137
144
inline uptr MemToShadow_PVC (uptr addr, uint32_t as) {
138
145
139
146
if (as == ADDRESS_SPACE_GENERIC) {
@@ -172,9 +179,6 @@ inline uptr MemToShadow_PVC(uptr addr, uint32_t as) {
172
179
}
173
180
return shadow_ptr;
174
181
} else if (as == ADDRESS_SPACE_LOCAL) { // local
175
- if (__AsanShadowMemoryLocalStart == 0 ) {
176
- return 0 ;
177
- }
178
182
// The size of SLM is 128KB on PVC
179
183
constexpr unsigned SLM_SIZE = 128 * 1024 ;
180
184
// work-group linear id
@@ -184,14 +188,28 @@ inline uptr MemToShadow_PVC(uptr addr, uint32_t as) {
184
188
__spirv_BuiltInWorkgroupId.y * __spirv_BuiltInNumWorkgroups.z +
185
189
__spirv_BuiltInWorkgroupId.z ;
186
190
187
- uptr shadow_ptr = __AsanShadowMemoryLocalStart +
191
+ auto launch_info = (__SYCL_GLOBAL__ const LaunchInfo *)__AsanLaunchInfo;
192
+ const auto shadow_offset = launch_info->LocalShadowOffset ;
193
+ const auto shadow_offset_end = launch_info->LocalShadowOffsetEnd ;
194
+
195
+ if (shadow_offset == 0 ) {
196
+ return 0 ;
197
+ }
198
+
199
+ if (__AsanDebug)
200
+ __spirv_ocl_printf (__mem_launch_info, launch_info,
201
+ launch_info->LocalShadowOffset ,
202
+ launch_info->LocalShadowOffsetEnd ,
203
+ launch_info->NumLocalArgs , launch_info->LocalArgs );
204
+
205
+ uptr shadow_ptr = shadow_offset +
188
206
((wg_lid * SLM_SIZE) >> ASAN_SHADOW_SCALE) +
189
207
((addr & (SLM_SIZE - 1 )) >> 3 );
190
208
191
- if (shadow_ptr > __AsanShadowMemoryLocalEnd ) {
209
+ if (shadow_ptr > shadow_offset_end ) {
192
210
if (__asan_report_out_of_shadow_bounds () && __AsanDebug) {
193
211
__spirv_ocl_printf (__local_shadow_out_of_bound, addr, shadow_ptr,
194
- wg_lid, (uptr)__AsanShadowMemoryLocalStart );
212
+ wg_lid, (uptr)shadow_offset );
195
213
}
196
214
return 0 ;
197
215
}
@@ -268,22 +286,18 @@ bool MemIsZero(__SYCL_GLOBAL__ const char *beg, uptr size) {
268
286
bool __asan_internal_report_save (DeviceSanitizerErrorType error_type) {
269
287
const int Expected = ASAN_REPORT_NONE;
270
288
int Desired = ASAN_REPORT_START;
271
- if (atomicCompareAndSet (&__DeviceSanitizerReportMem.get ().Flag , Desired,
272
- Expected) == Expected) {
273
- __DeviceSanitizerReportMem.get ().ErrorType = error_type;
289
+ auto &SanitizerReport =
290
+ ((__SYCL_GLOBAL__ LaunchInfo *)__AsanLaunchInfo)->SanitizerReport ;
291
+ if (atomicCompareAndSet (&SanitizerReport.Flag , Desired, Expected) ==
292
+ Expected) {
293
+ SanitizerReport.ErrorType = error_type;
274
294
// Show we've done copying
275
- atomicStore (&__DeviceSanitizerReportMem. get () .Flag , ASAN_REPORT_FINISH);
295
+ atomicStore (&SanitizerReport .Flag , ASAN_REPORT_FINISH);
276
296
return true ;
277
297
}
278
298
return false ;
279
299
}
280
300
281
- #ifdef __SYCL_DEVICE_ONLY__
282
- #define __DEVICE_SANITIZER_REPORT_ACCESSOR __DeviceSanitizerReportMem.get()
283
- #else // __SYCL_DEVICE_ONLY__
284
- #define __DEVICE_SANITIZER_REPORT_ACCESSOR
285
- #endif // __SYCL_DEVICE_ONLY__
286
-
287
301
bool __asan_internal_report_save (
288
302
uptr ptr, uint32_t as, const char __SYCL_CONSTANT__ *file, uint32_t line,
289
303
const char __SYCL_CONSTANT__ *func, bool is_write, uint32_t access_size,
@@ -292,8 +306,20 @@ bool __asan_internal_report_save(
292
306
293
307
const int Expected = ASAN_REPORT_NONE;
294
308
int Desired = ASAN_REPORT_START;
295
- if (atomicCompareAndSet (&__DEVICE_SANITIZER_REPORT_ACCESSOR.Flag , Desired,
296
- Expected) == Expected) {
309
+
310
+ if (__AsanDebug) {
311
+ auto *launch_info = (__SYCL_GLOBAL__ LaunchInfo *)__AsanLaunchInfo;
312
+ __spirv_ocl_printf (__mem_launch_info, launch_info,
313
+ launch_info->LocalShadowOffset ,
314
+ launch_info->LocalShadowOffsetEnd ,
315
+ launch_info->NumLocalArgs , launch_info->LocalArgs );
316
+ }
317
+
318
+ auto &SanitizerReport =
319
+ ((__SYCL_GLOBAL__ LaunchInfo *)__AsanLaunchInfo)->SanitizerReport ;
320
+
321
+ if (atomicCompareAndSet (&SanitizerReport.Flag , Desired, Expected) ==
322
+ Expected) {
297
323
298
324
int FileLength = 0 ;
299
325
int FuncLength = 0 ;
@@ -305,39 +331,40 @@ bool __asan_internal_report_save(
305
331
for (auto *C = func; *C != ' \0 ' ; ++C, ++FuncLength)
306
332
;
307
333
308
- int MaxFileIdx = sizeof (__DEVICE_SANITIZER_REPORT_ACCESSOR .File ) - 1 ;
309
- int MaxFuncIdx = sizeof (__DEVICE_SANITIZER_REPORT_ACCESSOR .Func ) - 1 ;
334
+ int MaxFileIdx = sizeof (SanitizerReport .File ) - 1 ;
335
+ int MaxFuncIdx = sizeof (SanitizerReport .Func ) - 1 ;
310
336
311
337
if (FileLength < MaxFileIdx)
312
338
MaxFileIdx = FileLength;
313
339
if (FuncLength < MaxFuncIdx)
314
340
MaxFuncIdx = FuncLength;
315
341
316
342
for (int Idx = 0 ; Idx < MaxFileIdx; ++Idx)
317
- __DEVICE_SANITIZER_REPORT_ACCESSOR .File [Idx] = file[Idx];
318
- __DEVICE_SANITIZER_REPORT_ACCESSOR .File [MaxFileIdx] = ' \0 ' ;
343
+ SanitizerReport .File [Idx] = file[Idx];
344
+ SanitizerReport .File [MaxFileIdx] = ' \0 ' ;
319
345
320
346
for (int Idx = 0 ; Idx < MaxFuncIdx; ++Idx)
321
- __DEVICE_SANITIZER_REPORT_ACCESSOR .Func [Idx] = func[Idx];
322
- __DEVICE_SANITIZER_REPORT_ACCESSOR .Func [MaxFuncIdx] = ' \0 ' ;
323
-
324
- __DEVICE_SANITIZER_REPORT_ACCESSOR .Line = line;
325
- __DEVICE_SANITIZER_REPORT_ACCESSOR .GID0 = __spirv_GlobalInvocationId_x ();
326
- __DEVICE_SANITIZER_REPORT_ACCESSOR .GID1 = __spirv_GlobalInvocationId_y ();
327
- __DEVICE_SANITIZER_REPORT_ACCESSOR .GID2 = __spirv_GlobalInvocationId_z ();
328
- __DEVICE_SANITIZER_REPORT_ACCESSOR .LID0 = __spirv_LocalInvocationId_x ();
329
- __DEVICE_SANITIZER_REPORT_ACCESSOR .LID1 = __spirv_LocalInvocationId_y ();
330
- __DEVICE_SANITIZER_REPORT_ACCESSOR .LID2 = __spirv_LocalInvocationId_z ();
331
-
332
- __DEVICE_SANITIZER_REPORT_ACCESSOR .Address = ptr;
333
- __DEVICE_SANITIZER_REPORT_ACCESSOR .IsWrite = is_write;
334
- __DEVICE_SANITIZER_REPORT_ACCESSOR .AccessSize = access_size;
335
- __DEVICE_SANITIZER_REPORT_ACCESSOR .ErrorType = error_type;
336
- __DEVICE_SANITIZER_REPORT_ACCESSOR .MemoryType = memory_type;
337
- __DEVICE_SANITIZER_REPORT_ACCESSOR .IsRecover = is_recover;
347
+ SanitizerReport .Func [Idx] = func[Idx];
348
+ SanitizerReport .Func [MaxFuncIdx] = ' \0 ' ;
349
+
350
+ SanitizerReport .Line = line;
351
+ SanitizerReport .GID0 = __spirv_GlobalInvocationId_x ();
352
+ SanitizerReport .GID1 = __spirv_GlobalInvocationId_y ();
353
+ SanitizerReport .GID2 = __spirv_GlobalInvocationId_z ();
354
+ SanitizerReport .LID0 = __spirv_LocalInvocationId_x ();
355
+ SanitizerReport .LID1 = __spirv_LocalInvocationId_y ();
356
+ SanitizerReport .LID2 = __spirv_LocalInvocationId_z ();
357
+
358
+ SanitizerReport .Address = ptr;
359
+ SanitizerReport .IsWrite = is_write;
360
+ SanitizerReport .AccessSize = access_size;
361
+ SanitizerReport .ErrorType = error_type;
362
+ SanitizerReport .MemoryType = memory_type;
363
+ SanitizerReport .IsRecover = is_recover;
338
364
339
365
// Show we've done copying
340
- atomicStore (&__DEVICE_SANITIZER_REPORT_ACCESSOR.Flag , ASAN_REPORT_FINISH);
366
+ atomicStore (&SanitizerReport.Flag , ASAN_REPORT_FINISH);
367
+ return true ;
341
368
}
342
369
return false ;
343
370
}
@@ -545,7 +572,7 @@ ASAN_REPORT_ERROR(store, true, 4)
545
572
DEVICE_EXTERN_C_NOINLINE void __asan_##type##size( \
546
573
uptr addr, uint32_t as, const char __SYCL_CONSTANT__ *file, \
547
574
uint32_t line, const char __SYCL_CONSTANT__ *func) { \
548
- u##size *shadow_address = (u##size *)MemToShadow (addr, as); \
575
+ auto *shadow_address = (__SYCL_GLOBAL__ u##size *)MemToShadow (addr, as); \
549
576
if (shadow_address && *shadow_address) { \
550
577
__asan_report_access_error (addr, as, size, is_write, addr, file, line, \
551
578
func); \
@@ -554,7 +581,7 @@ ASAN_REPORT_ERROR(store, true, 4)
554
581
DEVICE_EXTERN_C_NOINLINE void __asan_##type##size##_noabort( \
555
582
uptr addr, uint32_t as, const char __SYCL_CONSTANT__ *file, \
556
583
uint32_t line, const char __SYCL_CONSTANT__ *func) { \
557
- u##size *shadow_address = (u##size *)MemToShadow (addr, as); \
584
+ auto *shadow_address = (__SYCL_GLOBAL__ u##size *)MemToShadow (addr, as); \
558
585
if (shadow_address && *shadow_address) { \
559
586
__asan_report_access_error (addr, as, size, is_write, addr, file, line, \
560
587
func, true ); \
@@ -595,7 +622,7 @@ static __SYCL_CONSTANT__ const char __mem_set_shadow_local[] =
595
622
"[kernel] set_shadow_local(beg=%p, end=%p, val:%02X)\n";
596
623
597
624
DEVICE_EXTERN_C_NOINLINE void
598
- __asan_set_shadow_local_memory (uptr ptr, size_t size,
625
+ __asan_set_shadow_static_local (uptr ptr, size_t size,
599
626
size_t size_with_redzone) {
600
627
// Since ptr is aligned to ASAN_SHADOW_GRANULARITY,
601
628
// if size != aligned_size, then the buffer tail of ptr is not aligned
@@ -638,4 +665,48 @@ __asan_set_shadow_local_memory(uptr ptr, size_t size,
638
665
}
639
666
}
640
667
668
+ static __SYCL_CONSTANT__ const char __mem_local_arg[] =
669
+ " [kernel] local_arg(index=%d, size=%d, size_rz=%d)\n " ;
670
+
671
+ static __SYCL_CONSTANT__ const char __mem_set_shadow_dynamic_local_begin[] =
672
+ " [kernel] BEGIN __asan_set_shadow_dynamic_local\n " ;
673
+ static __SYCL_CONSTANT__ const char __mem_set_shadow_dynamic_local_end[] =
674
+ " [kernel] END __asan_set_shadow_dynamic_local\n " ;
675
+ static __SYCL_CONSTANT__ const char __mem_report_arg_count_incorrect[] =
676
+ " [kernel] ERROR: The number of local args is incorrect, expect %d, actual "
677
+ " %d\n " ;
678
+
679
+ DEVICE_EXTERN_C_NOINLINE void
680
+ __asan_set_shadow_dynamic_local (uptr ptr, uint32_t num_args) {
681
+ if (__AsanDebug)
682
+ __spirv_ocl_printf (__mem_set_shadow_dynamic_local_begin);
683
+
684
+ auto *launch_info = (__SYCL_GLOBAL__ const LaunchInfo *)__AsanLaunchInfo;
685
+ if (num_args != launch_info->NumLocalArgs ) {
686
+ __spirv_ocl_printf (__mem_report_arg_count_incorrect, num_args,
687
+ launch_info->NumLocalArgs );
688
+ return ;
689
+ }
690
+
691
+ uptr *args = (uptr *)ptr;
692
+ if (__AsanDebug)
693
+ __spirv_ocl_printf (__mem_launch_info, launch_info,
694
+ launch_info->LocalShadowOffset ,
695
+ launch_info->LocalShadowOffsetEnd ,
696
+ launch_info->NumLocalArgs , launch_info->LocalArgs );
697
+
698
+ for (uint32_t i = 0 ; i < num_args; ++i) {
699
+ auto *local_arg = &launch_info->LocalArgs [i];
700
+ if (__AsanDebug)
701
+ __spirv_ocl_printf (__mem_local_arg, i, local_arg->Size ,
702
+ local_arg->SizeWithRedZone );
703
+
704
+ __asan_set_shadow_static_local (args[i], local_arg->Size ,
705
+ local_arg->SizeWithRedZone );
706
+ }
707
+
708
+ if (__AsanDebug)
709
+ __spirv_ocl_printf (__mem_set_shadow_dynamic_local_end);
710
+ }
711
+
641
712
#endif // __SPIR__ || __SPIRV__
0 commit comments