From 6ab36e0283d3ffa522de20b1e6d845a48fc2bdd7 Mon Sep 17 00:00:00 2001 From: "Zhao, Yang2" Date: Wed, 25 Dec 2024 06:50:14 +0100 Subject: [PATCH 1/4] fix shadow --- .../layers/sanitizer/msan/msan_shadow.cpp | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/source/loader/layers/sanitizer/msan/msan_shadow.cpp b/source/loader/layers/sanitizer/msan/msan_shadow.cpp index add9813db6..c3f8c665b0 100644 --- a/source/loader/layers/sanitizer/msan/msan_shadow.cpp +++ b/source/loader/layers/sanitizer/msan/msan_shadow.cpp @@ -134,18 +134,20 @@ ur_result_t MsanShadowMemoryGPU::Setup() { // shadow memory for each contexts, this will cause out-of-resource error when user uses // multiple contexts. Therefore, we just create one shadow memory here. static ur_result_t Result = [this]() { - size_t ShadowSize = GetShadowSize(); + const size_t ShadowSize = GetShadowSize(); + // To reserve very large amount of GPU virtual memroy, the pStart param should be beyond + // the SVM range, so that GFX driver will automatically switch to reservation on the GPU + // heap. + const void *StartAddress = (void *)(0x100000000000000ULL); // TODO: Protect Bad Zone auto Result = getContext()->urDdiTable.VirtualMem.pfnReserve( - Context, nullptr, ShadowSize, (void **)&ShadowBegin); - if (Result == UR_RESULT_SUCCESS) { - ShadowEnd = ShadowBegin + ShadowSize; - // Retain the context which reserves shadow memory - getContext()->urDdiTable.Context.pfnRetain(Context); + Context, StartAddress, ShadowSize, (void **)&ShadowBegin); + if (Result != UR_RESULT_SUCCESS) { + return Result; } - - // Set shadow memory for null pointer - ManagedQueue Queue(Context, Device); + ShadowEnd = ShadowBegin + ShadowSize; + // Retain the context which reserves shadow memory + getContext()->urDdiTable.Context.pfnRetain(Context); return UR_RESULT_SUCCESS; }(); return Result; @@ -278,13 +280,21 @@ MsanShadowMemoryGPU::ReleaseShadow(std::shared_ptr AI) { } uptr MsanShadowMemoryPVC::MemToShadow(uptr Ptr) { - assert(Ptr & 0xFF00000000000000ULL && "Ptr must be device USM"); - return ShadowBegin + (Ptr & 0x3FFF'FFFF'FFFFULL); + assert(Ptr & 0xff00'0000'0000'0000ULL && "Ptr must be device USM"); + if (Ptr < ShadowBegin) { + return Ptr + (ShadowBegin - 0xff00'0000'0000'0000ULL); + } else { + return Ptr - (0xff00'ffff'ffff'ffffULL - ShadowEnd); + } } uptr MsanShadowMemoryDG2::MemToShadow(uptr Ptr) { - assert(Ptr & 0xFFFF000000000000ULL && "Ptr must be device USM"); - return ShadowBegin + (Ptr & 0x3FFF'FFFF'FFFFULL); + assert(Ptr & 0xffff'0000'0000'0000ULL && "Ptr must be device USM"); + if (Ptr < ShadowBegin) { + return Ptr + (ShadowBegin - 0xffff'8000'0000'0000ULL); + } else { + return Ptr - (0xffff'ffff'ffff'ffffULL - ShadowEnd) + } } } // namespace msan From 947dd4050eb109563165e245eba00b42e2fe0352 Mon Sep 17 00:00:00 2001 From: "Zhao, Yang2" Date: Wed, 25 Dec 2024 07:01:35 +0100 Subject: [PATCH 2/4] fix build --- source/loader/layers/sanitizer/msan/msan_shadow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/loader/layers/sanitizer/msan/msan_shadow.cpp b/source/loader/layers/sanitizer/msan/msan_shadow.cpp index c3f8c665b0..c2acb53675 100644 --- a/source/loader/layers/sanitizer/msan/msan_shadow.cpp +++ b/source/loader/layers/sanitizer/msan/msan_shadow.cpp @@ -293,7 +293,7 @@ uptr MsanShadowMemoryDG2::MemToShadow(uptr Ptr) { if (Ptr < ShadowBegin) { return Ptr + (ShadowBegin - 0xffff'8000'0000'0000ULL); } else { - return Ptr - (0xffff'ffff'ffff'ffffULL - ShadowEnd) + return Ptr - (0xffff'ffff'ffff'ffffULL - ShadowEnd); } } From 3f4d4896e2b53ac77d5a189f94f568e184ae5dce Mon Sep 17 00:00:00 2001 From: "Zhao, Yang2" Date: Wed, 25 Dec 2024 07:14:49 +0100 Subject: [PATCH 3/4] fix format --- source/loader/layers/sanitizer/msan/msan_shadow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/loader/layers/sanitizer/msan/msan_shadow.cpp b/source/loader/layers/sanitizer/msan/msan_shadow.cpp index c2acb53675..324c983613 100644 --- a/source/loader/layers/sanitizer/msan/msan_shadow.cpp +++ b/source/loader/layers/sanitizer/msan/msan_shadow.cpp @@ -138,7 +138,7 @@ ur_result_t MsanShadowMemoryGPU::Setup() { // To reserve very large amount of GPU virtual memroy, the pStart param should be beyond // the SVM range, so that GFX driver will automatically switch to reservation on the GPU // heap. - const void *StartAddress = (void *)(0x100000000000000ULL); + const void *StartAddress = (void *)(0x100'0000'0000'0000ULL); // TODO: Protect Bad Zone auto Result = getContext()->urDdiTable.VirtualMem.pfnReserve( Context, StartAddress, ShadowSize, (void **)&ShadowBegin); From cf0c9482704b1ccdf26c7960190a3df50734d78c Mon Sep 17 00:00:00 2001 From: "Zhao, Yang2" Date: Wed, 25 Dec 2024 09:51:41 +0100 Subject: [PATCH 4/4] sync to asan --- source/loader/layers/sanitizer/asan/asan_shadow.cpp | 8 ++++++-- source/loader/layers/sanitizer/msan/msan_shadow.cpp | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/source/loader/layers/sanitizer/asan/asan_shadow.cpp b/source/loader/layers/sanitizer/asan/asan_shadow.cpp index de0679687b..145fd232c1 100644 --- a/source/loader/layers/sanitizer/asan/asan_shadow.cpp +++ b/source/loader/layers/sanitizer/asan/asan_shadow.cpp @@ -104,10 +104,14 @@ ur_result_t ShadowMemoryGPU::Setup() { // shadow memory for each contexts, this will cause out-of-resource error when user uses // multiple contexts. Therefore, we just create one shadow memory here. static ur_result_t Result = [this]() { - size_t ShadowSize = GetShadowSize(); + const size_t ShadowSize = GetShadowSize(); + // To reserve very large amount of GPU virtual memroy, the pStart param should be beyond + // the SVM range, so that GFX driver will automatically switch to reservation on the GPU + // heap. + const void *StartAddress = (void *)(0x100'0000'0000'0000ULL); // TODO: Protect Bad Zone auto Result = getContext()->urDdiTable.VirtualMem.pfnReserve( - Context, nullptr, ShadowSize, (void **)&ShadowBegin); + Context, StartAddress, ShadowSize, (void **)&ShadowBegin); if (Result != UR_RESULT_SUCCESS) { getContext()->logger.error( "Shadow memory reserved failed with size {}: {}", diff --git a/source/loader/layers/sanitizer/msan/msan_shadow.cpp b/source/loader/layers/sanitizer/msan/msan_shadow.cpp index 324c983613..75866203f3 100644 --- a/source/loader/layers/sanitizer/msan/msan_shadow.cpp +++ b/source/loader/layers/sanitizer/msan/msan_shadow.cpp @@ -143,6 +143,9 @@ ur_result_t MsanShadowMemoryGPU::Setup() { auto Result = getContext()->urDdiTable.VirtualMem.pfnReserve( Context, StartAddress, ShadowSize, (void **)&ShadowBegin); if (Result != UR_RESULT_SUCCESS) { + getContext()->logger.error( + "Shadow memory reserved failed with size {}: {}", + (void *)ShadowSize, Result); return Result; } ShadowEnd = ShadowBegin + ShadowSize;