Skip to content

Commit c3723e4

Browse files
zhaomaosukbenzie
authored andcommitted
Fix several misc issues in device memory sanitizer (#18026)
* Disable inserting unreachable instruction since we have another mechanism to exit current kernel execution. * Cleanup clean shadow after it's allocated. * Remove redzone maximum limitation.
1 parent 6f0cd4f commit c3723e4

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ ur_result_t MsanInterceptor::prepareLaunch(
479479
UR_CALL(getContext()->urDdiTable.USM.pfnDeviceAlloc(
480480
ContextInfo->Handle, DeviceInfo->Handle, nullptr, nullptr,
481481
ContextInfo->MaxAllocatedSize, (void **)&LaunchInfo.Data->CleanShadow));
482+
UR_CALL(EnqueueUSMBlockingSet(Queue, (void *)LaunchInfo.Data->CleanShadow, 0,
483+
ContextInfo->MaxAllocatedSize, 0, nullptr,
484+
nullptr));
482485

483486
if (LaunchInfo.LocalWorkSize.empty()) {
484487
LaunchInfo.LocalWorkSize.resize(LaunchInfo.WorkDim);

source/loader/layers/sanitizer/sanitizer_common/sanitizer_common.hpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ inline constexpr bool IsPowerOfTwo(uptr x) {
3434
return (x & (x - 1)) == 0 && x != 0;
3535
}
3636

37+
inline constexpr uptr RoundUpToPowerOfTwo(uptr x) {
38+
if (x == 0)
39+
return 1;
40+
x--;
41+
x |= x >> 1;
42+
x |= x >> 2;
43+
x |= x >> 4;
44+
x |= x >> 8;
45+
x |= x >> 16;
46+
x |= x >> 32;
47+
x++;
48+
assert(IsPowerOfTwo(x));
49+
return x;
50+
}
51+
3752
inline constexpr uptr RoundUpTo(uptr Size, uptr boundary) {
3853
assert(IsPowerOfTwo(boundary));
3954
return (Size + boundary - 1) & ~(boundary - 1);
@@ -48,16 +63,12 @@ inline constexpr bool IsAligned(uptr a, uptr alignment) {
4863
return (a & (alignment - 1)) == 0;
4964
}
5065

51-
// Valid redzone sizes are 16, 32, 64, ... 2048, so we encode them in 3 bits.
66+
// Valid redzone sizes are 16, 32, 64 ..., so we encode them in 3 bits.
5267
// We use adaptive redzones: for larger allocation larger redzones are used.
53-
inline constexpr uptr RZLog2Size(uptr rz_log) {
54-
assert(rz_log < 8);
55-
return 16 << rz_log;
56-
}
68+
inline constexpr uptr RZLog2Size(uptr rz_log) { return 16 << rz_log; }
5769

5870
inline constexpr uptr RZSize2Log(uptr rz_size) {
5971
assert(rz_size >= 16);
60-
assert(rz_size <= 2048);
6172
assert(IsPowerOfTwo(rz_size));
6273
uptr res = log2(rz_size) - 4;
6374
assert(rz_size == RZLog2Size(res));

source/loader/layers/sanitizer/sanitizer_common/sanitizer_options.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
#include "sanitizer_options.hpp"
15+
#include "sanitizer_common.hpp"
1516
#include "sanitizer_options_impl.hpp"
1617

1718
#include <cstring>
@@ -47,7 +48,13 @@ void SanitizerOptions::Init(const std::string &EnvName,
4748
Parser.ParseBool("halt_on_error", HaltOnError);
4849

4950
Parser.ParseUint64("quarantine_size_mb", MaxQuarantineSizeMB, 0, UINT32_MAX);
50-
Parser.ParseUint64("redzone", MinRZSize, 16, 2048);
51+
Parser.ParseUint64("redzone", MinRZSize, 16);
52+
MinRZSize =
53+
IsPowerOfTwo(MinRZSize) ? MinRZSize : RoundUpToPowerOfTwo(MinRZSize);
54+
if (MinRZSize > 16) {
55+
Logger.warning(
56+
"Increasing the redzone size may cause excessive memory overhead");
57+
}
5158
}
5259

5360
} // namespace ur_sanitizer_layer

0 commit comments

Comments
 (0)