Skip to content

Commit 4850738

Browse files
committed
fix shadow
1 parent 667d4d1 commit 4850738

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,20 @@ ur_result_t MsanShadowMemoryGPU::Setup() {
134134
// shadow memory for each contexts, this will cause out-of-resource error when user uses
135135
// multiple contexts. Therefore, we just create one shadow memory here.
136136
static ur_result_t Result = [this]() {
137-
size_t ShadowSize = GetShadowSize();
137+
const size_t ShadowSize = GetShadowSize();
138+
// To reserve very large amount of GPU virtual memroy, the pStart param should be beyond
139+
// the SVM range, so that GFX driver will automatically switch to reservation on the GPU
140+
// heap.
141+
const void *StartAddress = (void *)(0x100000000000000ULL);
138142
// TODO: Protect Bad Zone
139143
auto Result = getContext()->urDdiTable.VirtualMem.pfnReserve(
140-
Context, nullptr, ShadowSize, (void **)&ShadowBegin);
141-
if (Result == UR_RESULT_SUCCESS) {
142-
ShadowEnd = ShadowBegin + ShadowSize;
143-
// Retain the context which reserves shadow memory
144-
getContext()->urDdiTable.Context.pfnRetain(Context);
144+
Context, StartAddress, ShadowSize, (void **)&ShadowBegin);
145+
if (Result != UR_RESULT_SUCCESS) {
146+
return Result;
145147
}
146-
147-
// Set shadow memory for null pointer
148-
ManagedQueue Queue(Context, Device);
148+
ShadowEnd = ShadowBegin + ShadowSize;
149+
// Retain the context which reserves shadow memory
150+
getContext()->urDdiTable.Context.pfnRetain(Context);
149151
return UR_RESULT_SUCCESS;
150152
}();
151153
return Result;
@@ -278,13 +280,21 @@ MsanShadowMemoryGPU::ReleaseShadow(std::shared_ptr<MsanAllocInfo> AI) {
278280
}
279281

280282
uptr MsanShadowMemoryPVC::MemToShadow(uptr Ptr) {
281-
assert(Ptr & 0xFF00000000000000ULL && "Ptr must be device USM");
282-
return ShadowBegin + (Ptr & 0x3FFF'FFFF'FFFFULL);
283+
assert(Ptr & 0xff00'0000'0000'0000ULL && "Ptr must be device USM");
284+
if (Ptr < ShadowBegin) {
285+
return Ptr + (ShadowBegin - 0xff00'0000'0000'0000ULL);
286+
} else {
287+
return Ptr - (0xff00'ffff'ffff'ffffULL - ShadowEnd);
288+
}
283289
}
284290

285291
uptr MsanShadowMemoryDG2::MemToShadow(uptr Ptr) {
286-
assert(Ptr & 0xFFFF000000000000ULL && "Ptr must be device USM");
287-
return ShadowBegin + (Ptr & 0x3FFF'FFFF'FFFFULL);
292+
assert(Ptr & 0xffff'0000'0000'0000ULL && "Ptr must be device USM");
293+
if (Ptr < ShadowBegin) {
294+
return Ptr + (ShadowBegin - 0xffff'8000'0000'0000ULL);
295+
} else {
296+
return Ptr - (0xffff'ffff'ffff'ffffULL - ShadowEnd)
297+
}
288298
}
289299

290300
} // namespace msan

0 commit comments

Comments
 (0)