Skip to content

Commit 054a3cb

Browse files
AllanZynekbenzie
authored andcommitted
Check use-of-uninitialized value on private memory (#17309)
Support check use-of-uninitialized value on private memory
1 parent dfa83cf commit 054a3cb

File tree

5 files changed

+92
-9
lines changed

5 files changed

+92
-9
lines changed

source/loader/layers/sanitizer/msan/msan_interceptor.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,30 @@ ur_result_t MsanInterceptor::prepareLaunch(
517517
getContext()->logger.warning("Skip checking local memory of kernel <{}> ",
518518
GetKernelName(Kernel));
519519
} else {
520-
getContext()->logger.info("ShadowMemory(Local, WorkGroup={}, {} - {})",
521-
NumWG,
522-
(void *)LaunchInfo.Data->LocalShadowOffset,
523-
(void *)LaunchInfo.Data->LocalShadowOffsetEnd);
520+
getContext()->logger.debug("ShadowMemory(Local, WorkGroup={}, {} - {})",
521+
NumWG,
522+
(void *)LaunchInfo.Data->LocalShadowOffset,
523+
(void *)LaunchInfo.Data->LocalShadowOffsetEnd);
524+
}
525+
}
526+
527+
// Write shadow memory offset for private memory
528+
if (KernelInfo.IsCheckPrivates) {
529+
if (DeviceInfo->Shadow->AllocPrivateShadow(
530+
Queue, NumWG, LaunchInfo.Data->PrivateShadowOffset,
531+
LaunchInfo.Data->PrivateShadowOffsetEnd) != UR_RESULT_SUCCESS) {
532+
getContext()->logger.warning(
533+
"Failed to allocate shadow memory for private "
534+
"memory, maybe the number of workgroup ({}) is too "
535+
"large",
536+
NumWG);
537+
getContext()->logger.warning(
538+
"Skip checking private memory of kernel <{}>", GetKernelName(Kernel));
539+
} else {
540+
getContext()->logger.debug(
541+
"ShadowMemory(Private, WorkGroup={}, {} - {})", NumWG,
542+
(void *)LaunchInfo.Data->PrivateShadowOffset,
543+
(void *)LaunchInfo.Data->PrivateShadowOffsetEnd);
524544
}
525545
// Write local arguments info
526546
if (!KernelInfo.LocalArgs.empty()) {
@@ -535,11 +555,11 @@ ur_result_t MsanInterceptor::prepareLaunch(
535555
}
536556

537557
getContext()->logger.info(
538-
"LaunchInfo {} (GlobalShadow={}, LocalShadow={}, CleanShadow={}, "
539-
"LocalArgs={}, NumLocalArgs={}, "
540-
"Device={}, Debug={})",
558+
"LaunchInfo {} (GlobalShadow={}, LocalShadow={}, PrivateShadow={}, "
559+
"CleanShadow={}, LocalArgs={}, NumLocalArgs={}, Device={}, Debug={})",
541560
(void *)LaunchInfo.Data, (void *)LaunchInfo.Data->GlobalShadowOffset,
542561
(void *)LaunchInfo.Data->LocalShadowOffset,
562+
(void *)LaunchInfo.Data->PrivateShadowOffset,
543563
(void *)LaunchInfo.Data->CleanShadow, (void *)LaunchInfo.Data->LocalArgs,
544564
LaunchInfo.Data->NumLocalArgs, ToString(LaunchInfo.Data->DeviceTy),
545565
LaunchInfo.Data->Debug);

source/loader/layers/sanitizer/msan/msan_libdevice.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct MsanErrorReport {
3737

3838
uint32_t AccessSize = 0;
3939
ErrorType ErrorTy = ErrorType::UNKNOWN;
40+
uintptr_t Origin;
4041
};
4142

4243
struct MsanLocalArgsInfo {
@@ -50,6 +51,9 @@ struct MsanLaunchInfo {
5051
uintptr_t LocalShadowOffset = 0;
5152
uintptr_t LocalShadowOffsetEnd = 0;
5253

54+
uintptr_t PrivateShadowOffset = 0;
55+
uintptr_t PrivateShadowOffsetEnd = 0;
56+
5357
uintptr_t CleanShadow = 0;
5458

5559
DeviceType DeviceTy = DeviceType::UNKNOWN;

source/loader/layers/sanitizer/msan/msan_report.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ void ReportUsesUninitializedValue(const MsanErrorReport &Report,
3030
// Try to demangle the kernel name
3131
KernelName = DemangleName(KernelName);
3232

33-
getContext()->logger.always(
34-
"====WARNING: DeviceSanitizer: use-of-uninitialized-value");
33+
if (Report.Origin) {
34+
getContext()->logger.always(
35+
"====WARNING: DeviceSanitizer: use-of-uninitialized-value (shadow: {})",
36+
(void *)Report.Origin);
37+
} else {
38+
getContext()->logger.always(
39+
"====WARNING: DeviceSanitizer: use-of-uninitialized-value)");
40+
}
41+
3542
getContext()->logger.always(
3643
"use of size {} at kernel <{}> LID({}, {}, {}) GID({}, "
3744
"{}, {})",

source/loader/layers/sanitizer/msan/msan_shadow.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,42 @@ ur_result_t MsanShadowMemoryGPU::AllocLocalShadow(ur_queue_handle_t Queue,
333333
return UR_RESULT_SUCCESS;
334334
}
335335

336+
ur_result_t MsanShadowMemoryGPU::AllocPrivateShadow(ur_queue_handle_t Queue,
337+
uint32_t NumWG, uptr &Begin,
338+
uptr &End) {
339+
const size_t RequiredShadowSize = NumWG * MSAN_PRIVATE_SIZE;
340+
static size_t LastAllocedSize = 0;
341+
if (RequiredShadowSize > LastAllocedSize) {
342+
auto ContextInfo = getMsanInterceptor()->getContextInfo(Context);
343+
if (PrivateShadowOffset) {
344+
UR_CALL(getContext()->urDdiTable.USM.pfnFree(
345+
Context, (void *)PrivateShadowOffset));
346+
PrivateShadowOffset = 0;
347+
LastAllocedSize = 0;
348+
}
349+
350+
UR_CALL(getContext()->urDdiTable.USM.pfnDeviceAlloc(
351+
Context, Device, nullptr, nullptr, RequiredShadowSize,
352+
(void **)&PrivateShadowOffset));
353+
354+
// Initialize shadow memory
355+
ur_result_t URes = EnqueueUSMBlockingSet(Queue, (void *)PrivateShadowOffset,
356+
0, RequiredShadowSize);
357+
if (URes != UR_RESULT_SUCCESS) {
358+
UR_CALL(getContext()->urDdiTable.USM.pfnFree(
359+
Context, (void *)PrivateShadowOffset));
360+
PrivateShadowOffset = 0;
361+
LastAllocedSize = 0;
362+
}
363+
364+
LastAllocedSize = RequiredShadowSize;
365+
}
366+
367+
Begin = PrivateShadowOffset;
368+
End = PrivateShadowOffset + RequiredShadowSize - 1;
369+
return UR_RESULT_SUCCESS;
370+
}
371+
336372
uptr MsanShadowMemoryPVC::MemToShadow(uptr Ptr) {
337373
assert(MsanShadowMemoryPVC::IsDeviceUSM(Ptr) && "Ptr must be device USM");
338374
if (Ptr < ShadowBegin) {

source/loader/layers/sanitizer/msan/msan_shadow.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ struct MsanShadowMemory {
4646
virtual ur_result_t AllocLocalShadow(ur_queue_handle_t Queue, uint32_t NumWG,
4747
uptr &Begin, uptr &End) = 0;
4848

49+
virtual ur_result_t AllocPrivateShadow(ur_queue_handle_t Queue,
50+
uint32_t NumWG, uptr &Begin,
51+
uptr &End) = 0;
52+
4953
ur_context_handle_t Context{};
5054

5155
ur_device_handle_t Device{};
@@ -95,6 +99,13 @@ struct MsanShadowMemoryCPU final : public MsanShadowMemory {
9599
End = ShadowEnd;
96100
return UR_RESULT_SUCCESS;
97101
}
102+
103+
ur_result_t AllocPrivateShadow(ur_queue_handle_t, uint32_t, uptr &Begin,
104+
uptr &End) override {
105+
Begin = ShadowBegin;
106+
End = ShadowEnd;
107+
return UR_RESULT_SUCCESS;
108+
}
98109
};
99110

100111
struct MsanShadowMemoryGPU : public MsanShadowMemory {
@@ -116,6 +127,9 @@ struct MsanShadowMemoryGPU : public MsanShadowMemory {
116127
ur_result_t AllocLocalShadow(ur_queue_handle_t Queue, uint32_t NumWG,
117128
uptr &Begin, uptr &End) override final;
118129

130+
ur_result_t AllocPrivateShadow(ur_queue_handle_t Queue, uint32_t NumWG,
131+
uptr &Begin, uptr &End) override final;
132+
119133
virtual size_t GetShadowSize() = 0;
120134

121135
virtual uptr GetStartAddress() { return 0; }
@@ -132,6 +146,8 @@ struct MsanShadowMemoryGPU : public MsanShadowMemory {
132146
ur_mutex VirtualMemMapsMutex;
133147

134148
uptr LocalShadowOffset = 0;
149+
150+
uptr PrivateShadowOffset = 0;
135151
};
136152

137153
// clang-format off

0 commit comments

Comments
 (0)